2 * consumer_sdl_still.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <framework/mlt_consumer.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;
52 SDL_Surface *sdl_screen;
56 mlt_producer last_producer;
60 /** Forward references to static functions.
63 static int consumer_start( mlt_consumer parent );
64 static int consumer_stop( mlt_consumer parent );
65 static int consumer_is_stopped( mlt_consumer parent );
66 static void consumer_close( mlt_consumer parent );
67 static void *consumer_thread( void * );
68 static int consumer_get_dimensions( int *width, int *height );
69 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
71 /** This is what will be called by the factory - anything can be passed in
72 via the argument, but keep it simple.
75 mlt_consumer consumer_sdl_still_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
77 // Create the consumer object
78 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
80 // If no malloc'd and consumer init ok
81 if ( this != NULL && mlt_consumer_init( &this->parent, this, profile ) == 0 )
83 // Get the parent consumer object
84 mlt_consumer parent = &this->parent;
86 // get a handle on properties
87 mlt_service service = MLT_CONSUMER_SERVICE( parent );
88 this->properties = MLT_SERVICE_PROPERTIES( service );
90 // We have stuff to clean up, so override the close method
91 parent->close = consumer_close;
93 // Default scaler (for now we'll use nearest)
94 mlt_properties_set( this->properties, "rescale", "nearest" );
96 // We're always going to run this in non-realtime mode
97 mlt_properties_set( this->properties, "real_time", "0" );
99 // Default progressive true
100 mlt_properties_set_int( this->properties, "progressive", 1 );
102 // Ensure we don't join on a non-running object
105 // process actual param
106 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
108 this->width = mlt_properties_get_int( this->properties, "width" );
109 this->height = mlt_properties_get_int( this->properties, "height" );
113 mlt_properties_set_int( this->properties, "width", this->width );
114 mlt_properties_set_int( this->properties, "height", this->height );
118 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
120 // Allow thread to be started/stopped
121 parent->start = consumer_start;
122 parent->stop = consumer_stop;
123 parent->is_stopped = consumer_is_stopped;
125 // Register specific events
126 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
128 // Return the consumer produced
132 // malloc or consumer init failed
139 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
141 if ( listener != NULL )
142 listener( owner, this, ( SDL_Event * )args[ 0 ] );
145 static int consumer_start( mlt_consumer parent )
147 consumer_sdl this = parent->child;
149 if ( !this->running )
151 int preview_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "preview_off" );
152 int sdl_started = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" );
154 // Attach a colour space converter
155 if ( !this->filtered )
157 mlt_profile profile = mlt_service_profile( MLT_CONSUMER_SERVICE( parent ) );
158 mlt_filter filter = mlt_factory_filter( profile, "avcolour_space", NULL );
159 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "forced", mlt_image_yuv422 );
160 mlt_service_attach( MLT_CONSUMER_SERVICE( parent ), filter );
161 mlt_filter_close( filter );
165 consumer_stop( parent );
167 this->last_position = -1;
171 // Allow the user to force resizing to window size
172 this->width = mlt_properties_get_int( this->properties, "width" );
173 this->height = mlt_properties_get_int( this->properties, "height" );
175 // Default window size
176 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
177 this->window_width = ( double )this->height * display_ratio;
178 this->window_height = this->height;
180 if ( sdl_started == 0 && preview_off == 0 )
182 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
184 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
188 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
189 SDL_EnableUNICODE( 1 );
191 else if ( preview_off == 0 )
193 if ( SDL_GetVideoSurface( ) != NULL )
195 this->sdl_screen = SDL_GetVideoSurface( );
199 if ( this->sdl_screen == NULL && preview_off == 0 )
200 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
202 pthread_create( &this->thread, NULL, consumer_thread, this );
208 static int consumer_stop( mlt_consumer parent )
210 // Get the actual object
211 consumer_sdl this = parent->child;
213 if ( this->joined == 0 )
215 int preview_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "preview_off" );
216 int sdl_started = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" );
218 // Kill the thread and clean up
221 pthread_join( this->thread, NULL );
224 if ( sdl_started == 0 && preview_off == 0 )
227 this->sdl_screen = NULL;
233 static int consumer_is_stopped( mlt_consumer parent )
235 consumer_sdl this = parent->child;
236 return !this->running;
239 static int sdl_lock_display( )
241 SDL_Surface *screen = SDL_GetVideoSurface( );
242 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
245 static void sdl_unlock_display( )
247 SDL_Surface *screen = SDL_GetVideoSurface( );
248 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
249 SDL_UnlockSurface( screen );
252 static inline void display_1( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
254 // Generate the affine transform scaling values
255 if ( rect.w == 0 || rect.h == 0 ) return;
256 int scale_width = ( width << 16 ) / rect.w;
257 int scale_height = ( height << 16 ) / rect.h;
258 int stride = width * 3;
262 // Constants defined for clarity and optimisation
263 int scanlength = screen->pitch;
264 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
267 // Iterate through the screen using a very basic scaling algorithm
268 for ( y = 0; y < rect.h; y ++ )
271 row_index = ( 32768 + scale_height * y ) >> 16;
272 row = image + stride * row_index;
273 for ( x = 0; x < rect.w; x ++ )
275 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 3 );
276 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
282 static inline void display_2( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
284 // Generate the affine transform scaling values
285 if ( rect.w == 0 || rect.h == 0 ) return;
286 int scale_width = ( width << 16 ) / rect.w;
287 int scale_height = ( height << 16 ) / rect.h;
288 int stride = width * 3;
292 // Constants defined for clarity and optimisation
293 int scanlength = screen->pitch / 2;
294 uint16_t *start = ( uint16_t * )screen->pixels + rect.y * scanlength + rect.x;
297 // Iterate through the screen using a very basic scaling algorithm
298 for ( y = 0; y < rect.h; y ++ )
301 row_index = ( 32768 + scale_height * y ) >> 16;
302 row = image + stride * row_index;
303 for ( x = 0; x < rect.w; x ++ )
305 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 3 );
306 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
312 static inline void display_3( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
314 // Generate the affine transform scaling values
315 if ( rect.w == 0 || rect.h == 0 ) return;
316 int scale_width = ( width << 16 ) / rect.w;
317 int scale_height = ( height << 16 ) / rect.h;
318 int stride = width * 3;
322 // Constants defined for clarity and optimisation
323 int scanlength = screen->pitch;
324 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
328 // Iterate through the screen using a very basic scaling algorithm
329 for ( y = 0; y < rect.h; y ++ )
332 row_index = ( 32768 + scale_height * y ) >> 16;
333 row = image + stride * row_index;
334 for ( x = 0; x < rect.w; x ++ )
336 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 3 );
337 pixel = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
338 *p ++ = (pixel & 0xFF0000) >> 16;
339 *p ++ = (pixel & 0x00FF00) >> 8;
340 *p ++ = (pixel & 0x0000FF);
346 static inline void display_4( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
348 // Generate the affine transform scaling values
349 if ( rect.w == 0 || rect.h == 0 ) return;
350 int scale_width = ( width << 16 ) / rect.w;
351 int scale_height = ( height << 16 ) / rect.h;
352 int stride = width * 3;
356 // Constants defined for clarity and optimisation
357 int scanlength = screen->pitch / 4;
358 uint32_t *start = ( uint32_t * )screen->pixels + rect.y * scanlength + rect.x;
361 // Iterate through the screen using a very basic scaling algorithm
362 for ( y = 0; y < rect.h; y ++ )
365 row_index = ( 32768 + scale_height * y ) >> 16;
366 row = image + stride * row_index;
367 for ( x = 0; x < rect.w; x ++ )
369 q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 3 );
370 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
376 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
378 // Get the properties of this consumer
379 mlt_properties properties = this->properties;
381 mlt_image_format vfmt = mlt_image_rgb24;
382 int height = this->height;
383 int width = this->width;
384 uint8_t *image = NULL;
386 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
388 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
389 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
391 if ( lock != NULL ) lock( );
396 if ( this->sdl_screen != NULL )
400 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
402 while ( SDL_PollEvent( &event ) )
404 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
408 case SDL_VIDEORESIZE:
409 this->window_width = event.resize.w;
410 this->window_height = event.resize.h;
418 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
419 char keyboard[ 2 ] = " ";
420 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
421 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
423 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
424 callback( producer, keyboard );
432 if ( this->sdl_screen == NULL || changed )
435 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
436 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
437 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
438 uint32_t color = mlt_properties_get_int( this->properties, "window_background" );
439 SDL_FillRect( this->sdl_screen, NULL, color >> 8 );
448 this->last_position == mlt_frame_get_position( frame ) &&
449 this->last_producer == mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "_producer", NULL ) )
451 sdl_unlock_display( );
452 if ( unlock != NULL ) unlock( );
456 // Update last frame shown info
457 this->last_position = mlt_frame_get_position( frame );
458 this->last_producer = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "_producer", NULL );
460 // Get the image, width and height
461 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
462 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
466 char *rescale = mlt_properties_get( properties, "rescale" );
467 if ( rescale != NULL && strcmp( rescale, "none" ) )
469 double this_aspect = display_ratio / ( ( double )this->window_width / ( double )this->window_height );
470 this->rect.w = this_aspect * this->window_width;
471 this->rect.h = this->window_height;
472 if ( this->rect.w > this->window_width )
474 this->rect.w = this->window_width;
475 this->rect.h = ( 1.0 / this_aspect ) * this->window_height;
480 double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
481 this->rect.w = frame_aspect * this->window_height;
482 this->rect.h = this->window_height;
483 if ( this->rect.w > this->window_width )
485 this->rect.w = this->window_width;
486 this->rect.h = ( 1.0 / frame_aspect ) * this->window_width;
490 this->rect.x = ( this->window_width - this->rect.w ) / 2;
491 this->rect.y = ( this->window_height - this->rect.h ) / 2;
493 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
494 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
495 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
496 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
499 if ( !mlt_consumer_is_stopped( &this->parent ) && SDL_GetVideoSurface( ) != NULL && this->sdl_screen != NULL && this->sdl_screen->pixels != NULL )
501 switch( this->sdl_screen->format->BytesPerPixel )
504 display_1( this->sdl_screen, this->rect, image, width, height );
507 display_2( this->sdl_screen, this->rect, image, width, height );
510 display_3( this->sdl_screen, this->rect, image, width, height );
513 display_4( this->sdl_screen, this->rect, image, width, height );
516 fprintf( stderr, "Unsupported video depth %d\n", this->sdl_screen->format->BytesPerPixel );
520 // Flip it into sight
521 SDL_Flip( this->sdl_screen );
524 sdl_unlock_display();
526 if ( unlock != NULL ) unlock( );
531 /** Threaded wrapper for pipe.
534 static void *consumer_thread( void *arg )
537 consumer_sdl this = arg;
540 mlt_consumer consumer = &this->parent;
541 mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
543 // internal intialization
544 mlt_frame frame = NULL;
545 mlt_image_format vfmt = mlt_image_rgb24a;
546 int height = this->height;
547 int width = this->width;
548 uint8_t *image = NULL;
550 // Allow the hosting app to provide the preview
551 int preview_off = mlt_properties_get_int( properties, "preview_off" );
552 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
554 // Check if a specific colour space has been requested
555 if ( preview_off && preview_format != mlt_image_none )
556 vfmt = preview_format;
558 // Loop until told not to
559 while( this->running )
561 // Get a frame from the attached producer
562 frame = mlt_consumer_rt_frame( consumer );
564 // Ensure that we have a frame
565 if ( this->running && frame != NULL )
567 if ( preview_off == 0 )
569 consumer_play_video( this, frame );
573 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
574 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "format", vfmt );
575 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
577 mlt_frame_close( frame );
581 if ( frame ) mlt_frame_close( frame );
589 static int consumer_get_dimensions( int *width, int *height )
593 // SDL windows manager structure
596 // Specify the SDL Version
597 SDL_VERSION( &wm.version );
599 // Get the wm structure
600 if ( SDL_GetWMInfo( &wm ) == 1 )
603 // Check that we have the X11 wm
604 if ( wm.subsystem == SDL_SYSWM_X11 )
606 // Get the SDL window
607 Window window = wm.info.x11.window;
609 // Get the display session
610 Display *display = wm.info.x11.display;
612 // Get the window attributes
613 XWindowAttributes attr;
614 XGetWindowAttributes( display, window, &attr );
616 // Determine whether window has changed
617 changed = *width != attr.width || *height != attr.height;
619 // Return width and height
621 *height = attr.height;
629 /** Callback to allow override of the close method.
632 static void consumer_close( mlt_consumer parent )
634 // Get the actual object
635 consumer_sdl this = parent->child;
638 mlt_consumer_stop( parent );
640 // Now clean up the rest
641 mlt_consumer_close( parent );
643 // Finally clean up this