2 * consumer_sdl_still.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #include "consumer_sdl.h"
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_deque.h>
24 #include <framework/mlt_factory.h>
25 #include <framework/mlt_filter.h>
31 #include <SDL/SDL_syswm.h>
34 /** This classes definition.
37 typedef struct consumer_sdl_s
*consumer_sdl
;
41 struct mlt_consumer_s parent
;
42 mlt_properties properties
;
50 double last_frame_aspect
;
55 SDL_Surface
*sdl_screen
;
59 mlt_producer last_producer
;
62 /** Forward references to static functions.
65 static int consumer_start( mlt_consumer parent
);
66 static int consumer_stop( mlt_consumer parent
);
67 static int consumer_is_stopped( mlt_consumer parent
);
68 static void consumer_close( mlt_consumer parent
);
69 static void *consumer_thread( void * );
70 static int consumer_get_dimensions( int *width
, int *height
);
71 static void consumer_sdl_event( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
73 /** This is what will be called by the factory - anything can be passed in
74 via the argument, but keep it simple.
77 mlt_consumer
consumer_sdl_still_init( char *arg
)
79 // Create the consumer object
80 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
82 // If no malloc'd and consumer init ok
83 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
85 // Get the parent consumer object
86 mlt_consumer parent
= &this->parent
;
88 // Attach a colour space converter
89 mlt_filter filter
= mlt_factory_filter( "avcolour_space", NULL
);
90 mlt_properties_set_int( mlt_filter_properties( filter
), "forced", mlt_image_yuv422
);
91 mlt_service_attach( mlt_consumer_service( &this->parent
), filter
);
92 mlt_filter_close( filter
);
94 // We have stuff to clean up, so override the close method
95 parent
->close
= consumer_close
;
97 // get a handle on properties
98 mlt_service service
= mlt_consumer_service( parent
);
99 this->properties
= mlt_service_properties( service
);
101 // Default scaler (for now we'll use nearest)
102 mlt_properties_set( this->properties
, "rescale", "nearest" );
104 // We're always going to run this in non-realtime mode
105 mlt_properties_set( this->properties
, "real_time", "0" );
107 // Default progressive true
108 mlt_properties_set_int( this->properties
, "progressive", 1 );
110 // Get sample aspect ratio
111 this->aspect_ratio
= mlt_properties_get_double( this->properties
, "aspect_ratio" );
113 // Ensure we don't join on a non-running object
116 // Default display aspect ratio
117 this->display_aspect
= 4.0 / 3.0;
119 // process actual param
120 if ( arg
== NULL
|| sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
122 this->width
= mlt_properties_get_int( this->properties
, "width" );
123 this->height
= mlt_properties_get_int( this->properties
, "height" );
126 // Default window size
127 this->window_width
= ( float )this->height
* this->display_aspect
;
128 this->window_height
= this->height
;
131 this->sdl_flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
| SDL_RESIZABLE
| SDL_DOUBLEBUF
;
133 // Allow thread to be started/stopped
134 parent
->start
= consumer_start
;
135 parent
->stop
= consumer_stop
;
136 parent
->is_stopped
= consumer_is_stopped
;
138 // Register specific events
139 mlt_events_register( this->properties
, "consumer-sdl-event", ( mlt_transmitter
)consumer_sdl_event
);
141 // Return the consumer produced
145 // malloc or consumer init failed
152 static void consumer_sdl_event( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
154 if ( listener
!= NULL
)
155 listener( owner
, this, ( SDL_Event
* )args
[ 0 ] );
158 static int consumer_start( mlt_consumer parent
)
160 consumer_sdl
this = parent
->child
;
162 if ( !this->running
)
164 pthread_attr_t thread_attributes
;
166 consumer_stop( parent
);
168 this->last_position
= -1;
172 // Allow the user to force resizing to window size
173 if ( mlt_properties_get_int( this->properties
, "resize" ) )
175 mlt_properties_set_int( this->properties
, "width", this->width
);
176 mlt_properties_set_int( this->properties
, "height", this->height
);
179 // Inherit the scheduling priority
180 pthread_attr_init( &thread_attributes
);
181 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_INHERIT_SCHED
);
183 pthread_create( &this->thread
, &thread_attributes
, consumer_thread
, this );
189 static int consumer_stop( mlt_consumer parent
)
191 // Get the actual object
192 consumer_sdl
this = parent
->child
;
194 if ( this->joined
== 0 )
196 // Kill the thread and clean up
199 pthread_join( this->thread
, NULL
);
206 static int consumer_is_stopped( mlt_consumer parent
)
208 consumer_sdl
this = parent
->child
;
209 return !this->running
;
212 static int sdl_lock_display( )
214 SDL_Surface
*screen
= SDL_GetVideoSurface( );
215 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
218 static void sdl_unlock_display( )
220 SDL_Surface
*screen
= SDL_GetVideoSurface( );
221 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
222 SDL_UnlockSurface( screen
);
225 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
227 // Get the properties of this consumer
228 mlt_properties properties
= this->properties
;
230 mlt_image_format vfmt
= mlt_image_rgb24
;
231 int width
= this->width
, height
= this->height
;
235 void ( *lock
)( void ) = mlt_properties_get_data( properties
, "app_lock", NULL
);
236 void ( *unlock
)( void ) = mlt_properties_get_data( properties
, "app_unlock", NULL
);
238 if ( lock
!= NULL
) lock( );
243 if ( this->sdl_screen
!= NULL
)
247 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
249 while ( SDL_PollEvent( &event
) )
251 mlt_events_fire( this->properties
, "consumer-sdl-event", &event
, NULL
);
255 case SDL_VIDEORESIZE
:
256 this->window_width
= event
.resize
.w
;
257 this->window_height
= event
.resize
.h
;
265 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
266 char keyboard
[ 2 ] = " ";
267 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
268 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
270 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
271 callback( producer
, keyboard
);
279 if ( width
!= this->width
|| height
!= this->height
||
280 ( ( int )( this->last_frame_aspect
* 1000 ) != ( int )( mlt_frame_get_aspect_ratio( frame
) * 1000 ) &&
281 ( mlt_frame_get_aspect_ratio( frame
) != 1.0 || this->last_frame_aspect
== 0.0 ) ) )
285 this->height
= height
;
286 this->last_frame_aspect
= mlt_frame_get_aspect_ratio( frame
);
290 if ( this->sdl_screen
== NULL
|| changed
|| mlt_properties_get_int( properties
, "changed" ) == 2 )
293 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 16, this->sdl_flags
);
294 if ( consumer_get_dimensions( &this->window_width
, &this->window_height
) )
295 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 16, this->sdl_flags
);
297 mlt_properties_set_int( properties
, "changed", 0 );
299 // Determine frame's display aspect ratio
300 float frame_aspect
= mlt_frame_get_aspect_ratio( frame
) * this->width
/ this->height
;
302 // Determine window's new display aspect ratio
303 float this_aspect
= ( float )this->window_width
/ this->window_height
;
305 // If using hardware scaler
306 if ( mlt_properties_get( properties
, "rescale" ) != NULL
&&
307 !strcmp( mlt_properties_get( properties
, "rescale" ), "none" ) )
309 // Special case optimisation to negate odd effect of sample aspect ratio
310 // not corresponding exactly with image resolution.
311 if ( ( (int)( this_aspect
* 1000 ) == (int)( this->display_aspect
* 1000 ) ) &&
312 ( (int)( mlt_frame_get_aspect_ratio( frame
) * 1000 ) == (int)( this->aspect_ratio
* 1000 ) ) )
314 this->rect
.w
= this->window_width
;
315 this->rect
.h
= this->window_height
;
319 // Use hardware scaler to normalise display aspect ratio
320 this->rect
.w
= frame_aspect
/ this_aspect
* this->window_width
;
321 this->rect
.h
= this->window_height
;
322 if ( this->rect
.w
> this->window_width
)
324 this->rect
.w
= this->window_width
;
325 this->rect
.h
= this_aspect
/ frame_aspect
* this->window_height
;
329 // Special case optimisation to negate odd effect of sample aspect ratio
330 // not corresponding exactly with image resolution.
331 else if ( (int)( this_aspect
* 1000 ) == (int)( this->display_aspect
* 1000 ) )
333 this->rect
.w
= this->window_width
;
334 this->rect
.h
= this->window_height
;
336 // Use hardware scaler to normalise sample aspect ratio
337 else if ( this->window_height
* this->display_aspect
> this->window_width
)
339 this->rect
.w
= this->window_width
;
340 this->rect
.h
= this->window_width
/ this->display_aspect
;
344 this->rect
.w
= this->window_height
* this->display_aspect
;
345 this->rect
.h
= this->window_height
;
348 this->rect
.x
= ( this->window_width
- this->rect
.w
) / 2;
349 this->rect
.y
= ( this->window_height
- this->rect
.h
) / 2;
351 mlt_properties_set_int( this->properties
, "rect_x", this->rect
.x
);
352 mlt_properties_set_int( this->properties
, "rect_y", this->rect
.y
);
353 mlt_properties_set_int( this->properties
, "rect_w", this->rect
.w
);
354 mlt_properties_set_int( this->properties
, "rect_h", this->rect
.h
);
358 changed
= mlt_properties_get_int( properties
, "changed" ) | mlt_properties_get_int( mlt_frame_properties( frame
), "refresh" );
359 mlt_properties_set_int( properties
, "changed", 0 );
363 this->last_position
== mlt_frame_get_position( frame
) &&
364 this->last_producer
== mlt_properties_get_data( mlt_frame_properties( frame
), "_producer", NULL
) )
366 sdl_unlock_display( );
367 if ( unlock
!= NULL
)
372 // Update last frame shown info
373 this->last_position
= mlt_frame_get_position( frame
);
374 this->last_producer
= mlt_properties_get_data( mlt_frame_properties( frame
), "_producer", NULL
);
376 // Get the image, width and height
377 mlt_events_fire( properties
, "consumer-frame-show", frame
, NULL
);
378 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
380 if ( this->sdl_screen
!= NULL
)
382 // Calculate the scan length
383 int scanlength
= this->sdl_screen
->pitch
/ 2;
385 // Obtain the clip rect from the screen
386 SDL_Rect rect
= this->rect
;
388 // Generate the affine transform scaling values
389 int scale_width
= ( width
<< 16 ) / rect
.w
;
390 int scale_height
= ( height
<< 16 ) / rect
.h
;
392 // Constants defined for clarity and optimisation
393 int stride
= width
* 3;
394 uint16_t *start
= ( uint16_t * )this->sdl_screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
400 // Iterate through the screen using a very basic scaling algorithm
401 for ( y
= 0; y
< rect
.h
&& this->last_position
!= -1; y
++ )
403 // Obtain the pointer to the current screen row
406 // Calculate the row_index
407 row_index
= ( scale_height
* y
) >> 16;
409 // Calculate the pointer for the y offset (positioned on B instead of R)
410 row
= image
+ stride
* row_index
;
412 // Iterate through the screen width
413 for ( x
= 0; x
< rect
.w
; x
++ )
415 // Obtain the pixel pointer
416 q
= row
+ ( ( ( scale_width
* x
) >> 16 ) * 3 );
418 // Map the BGR colour from the frame to the SDL format
419 *p
++ = SDL_MapRGB( this->sdl_screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
422 // Move to the next row
426 // Flip it into sight
427 SDL_Flip( this->sdl_screen
);
430 sdl_unlock_display();
432 if ( unlock
!= NULL
) unlock( );
437 /** Threaded wrapper for pipe.
440 static void *consumer_thread( void *arg
)
443 consumer_sdl
this = arg
;
446 mlt_consumer consumer
= &this->parent
;
448 // internal intialization
449 mlt_frame frame
= NULL
;
450 struct timespec tm
= { 0, 1000 };
452 if ( mlt_properties_get_int( mlt_consumer_properties( consumer
), "sdl_started" ) == 0 )
454 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
) < 0 )
456 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
460 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
461 SDL_EnableUNICODE( 1 );
465 mlt_properties_set_int( mlt_consumer_properties( consumer
), "changed", 2 );
466 if ( SDL_GetVideoSurface( ) != NULL
)
467 consumer_get_dimensions( &this->window_width
, &this->window_height
);
470 // Loop until told not to
471 while( this->running
)
473 // Get a frame from the attached producer
474 frame
= mlt_consumer_rt_frame( consumer
);
476 // Ensure that we have a frame
479 consumer_play_video( this, frame
);
480 mlt_frame_close( frame
);
481 nanosleep( &tm
, NULL
);
485 if ( mlt_properties_get_int( mlt_consumer_properties( consumer
), "sdl_started" ) == 0 )
488 this->sdl_screen
= NULL
;
493 static int consumer_get_dimensions( int *width
, int *height
)
497 // SDL windows manager structure
500 // Specify the SDL Version
501 SDL_VERSION( &wm
.version
);
503 // Get the wm structure
504 if ( SDL_GetWMInfo( &wm
) == 1 )
506 // Check that we have the X11 wm
507 if ( wm
.subsystem
== SDL_SYSWM_X11
)
509 // Get the SDL window
510 Window window
= wm
.info
.x11
.window
;
512 // Get the display session
513 Display
*display
= wm
.info
.x11
.display
;
515 // Get the window attributes
516 XWindowAttributes attr
;
517 XGetWindowAttributes( display
, window
, &attr
);
519 // Determine whether window has changed
520 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
522 // Return width and height
524 *height
= attr
.height
;
531 /** Callback to allow override of the close method.
534 static void consumer_close( mlt_consumer parent
)
536 // Get the actual object
537 consumer_sdl
this = parent
->child
;
540 mlt_consumer_stop( parent
);
542 // Now clean up the rest
543 mlt_consumer_close( parent
);
545 // Finally clean up this