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 "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
;
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( 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 ) == 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 // Get the default display ratio
91 double display_ratio
= mlt_properties_get_double( this->properties
, "display_ratio" );
93 // We have stuff to clean up, so override the close method
94 parent
->close
= consumer_close
;
96 // Default scaler (for now we'll use nearest)
97 mlt_properties_set( this->properties
, "rescale", "nearest" );
99 // We're always going to run this in non-realtime mode
100 mlt_properties_set( this->properties
, "real_time", "0" );
102 // Default progressive true
103 mlt_properties_set_int( this->properties
, "progressive", 1 );
105 // Ensure we don't join on a non-running object
108 // process actual param
109 if ( arg
== NULL
|| sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
111 this->width
= mlt_properties_get_int( this->properties
, "width" );
112 this->height
= mlt_properties_get_int( this->properties
, "height" );
116 mlt_properties_set_int( this->properties
, "width", this->width
);
117 mlt_properties_set_int( this->properties
, "height", this->height
);
120 // Default window size
121 this->window_width
= ( double )this->height
* display_ratio
;
122 this->window_height
= this->height
;
125 this->sdl_flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
| SDL_RESIZABLE
| SDL_DOUBLEBUF
;
127 // Allow thread to be started/stopped
128 parent
->start
= consumer_start
;
129 parent
->stop
= consumer_stop
;
130 parent
->is_stopped
= consumer_is_stopped
;
132 // Register specific events
133 mlt_events_register( this->properties
, "consumer-sdl-event", ( mlt_transmitter
)consumer_sdl_event
);
135 // Return the consumer produced
139 // malloc or consumer init failed
146 static void consumer_sdl_event( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
148 if ( listener
!= NULL
)
149 listener( owner
, this, ( SDL_Event
* )args
[ 0 ] );
152 static int consumer_start( mlt_consumer parent
)
154 consumer_sdl
this = parent
->child
;
156 if ( !this->running
)
158 int preview_off
= mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent
), "preview_off" );
159 int sdl_started
= mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent
), "sdl_started" );
161 // Attach a colour space converter
162 if ( !this->filtered
)
164 mlt_filter filter
= mlt_factory_filter( "avcolour_space", NULL
);
165 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter
), "forced", mlt_image_yuv422
);
166 mlt_service_attach( MLT_CONSUMER_SERVICE( parent
), filter
);
167 mlt_filter_close( filter
);
171 consumer_stop( parent
);
173 this->last_position
= -1;
177 // Allow the user to force resizing to window size
178 this->width
= mlt_properties_get_int( this->properties
, "width" );
179 this->height
= mlt_properties_get_int( this->properties
, "height" );
181 if ( sdl_started
== 0 && preview_off
== 0 )
183 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
) < 0 )
185 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
189 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
190 SDL_EnableUNICODE( 1 );
192 else if ( preview_off
== 0 )
194 if ( SDL_GetVideoSurface( ) != NULL
)
196 this->sdl_screen
= SDL_GetVideoSurface( );
197 consumer_get_dimensions( &this->window_width
, &this->window_height
);
201 if ( this->sdl_screen
== NULL
&& preview_off
== 0 )
202 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
204 pthread_create( &this->thread
, NULL
, consumer_thread
, this );
210 static int consumer_stop( mlt_consumer parent
)
212 // Get the actual object
213 consumer_sdl
this = parent
->child
;
215 if ( this->joined
== 0 )
217 int preview_off
= mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent
), "preview_off" );
218 int sdl_started
= mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent
), "sdl_started" );
220 // Kill the thread and clean up
223 pthread_join( this->thread
, NULL
);
226 if ( sdl_started
== 0 && preview_off
== 0 )
229 this->sdl_screen
= NULL
;
235 static int consumer_is_stopped( mlt_consumer parent
)
237 consumer_sdl
this = parent
->child
;
238 return !this->running
;
241 static int sdl_lock_display( )
243 SDL_Surface
*screen
= SDL_GetVideoSurface( );
244 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
247 static void sdl_unlock_display( )
249 SDL_Surface
*screen
= SDL_GetVideoSurface( );
250 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
251 SDL_UnlockSurface( screen
);
254 static inline void display_1( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
256 // Generate the affine transform scaling values
257 int scale_width
= ( width
<< 16 ) / rect
.w
;
258 int scale_height
= ( height
<< 16 ) / rect
.h
;
259 int stride
= width
* 3;
263 // Constants defined for clarity and optimisation
264 int scanlength
= screen
->pitch
;
265 uint8_t *start
= ( uint8_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
268 // Iterate through the screen using a very basic scaling algorithm
269 for ( y
= 0; y
< rect
.h
; y
++ )
272 row_index
= ( 32768 + scale_height
* y
) >> 16;
273 row
= image
+ stride
* row_index
;
274 for ( x
= 0; x
< rect
.w
; x
++ )
276 q
= row
+ ( ( ( 32768 + scale_width
* x
) >> 16 ) * 3 );
277 *p
++ = SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
283 static inline void display_2( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
285 // Generate the affine transform scaling values
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 int scale_width
= ( width
<< 16 ) / rect
.w
;
316 int scale_height
= ( height
<< 16 ) / rect
.h
;
317 int stride
= width
* 3;
321 // Constants defined for clarity and optimisation
322 int scanlength
= screen
->pitch
;
323 uint8_t *start
= ( uint8_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
327 // Iterate through the screen using a very basic scaling algorithm
328 for ( y
= 0; y
< rect
.h
; y
++ )
331 row_index
= ( 32768 + scale_height
* y
) >> 16;
332 row
= image
+ stride
* row_index
;
333 for ( x
= 0; x
< rect
.w
; x
++ )
335 q
= row
+ ( ( ( 32768 + scale_width
* x
) >> 16 ) * 3 );
336 pixel
= SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
337 *p
++ = (pixel
& 0xFF0000) >> 16;
338 *p
++ = (pixel
& 0x00FF00) >> 8;
339 *p
++ = (pixel
& 0x0000FF);
345 static inline void display_4( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
347 // Generate the affine transform scaling values
348 int scale_width
= ( width
<< 16 ) / rect
.w
;
349 int scale_height
= ( height
<< 16 ) / rect
.h
;
350 int stride
= width
* 3;
354 // Constants defined for clarity and optimisation
355 int scanlength
= screen
->pitch
/ 4;
356 uint32_t *start
= ( uint32_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
359 // Iterate through the screen using a very basic scaling algorithm
360 for ( y
= 0; y
< rect
.h
; y
++ )
363 row_index
= ( 32768 + scale_height
* y
) >> 16;
364 row
= image
+ stride
* row_index
;
365 for ( x
= 0; x
< rect
.w
; x
++ )
367 q
= row
+ ( ( ( 32768 + scale_width
* x
) >> 16 ) * 3 );
368 *p
++ = SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
374 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
376 // Get the properties of this consumer
377 mlt_properties properties
= this->properties
;
379 mlt_image_format vfmt
= mlt_image_rgb24
;
380 int height
= this->height
;
381 int width
= this->width
;
382 uint8_t *image
= NULL
;
384 double display_ratio
= mlt_properties_get_double( this->properties
, "display_ratio" );
386 void ( *lock
)( void ) = mlt_properties_get_data( properties
, "app_lock", NULL
);
387 void ( *unlock
)( void ) = mlt_properties_get_data( properties
, "app_unlock", NULL
);
389 if ( lock
!= NULL
) lock( );
394 if ( this->sdl_screen
!= NULL
)
398 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
400 while ( SDL_PollEvent( &event
) )
402 mlt_events_fire( this->properties
, "consumer-sdl-event", &event
, NULL
);
406 case SDL_VIDEORESIZE
:
407 this->window_width
= event
.resize
.w
;
408 this->window_height
= event
.resize
.h
;
416 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
417 char keyboard
[ 2 ] = " ";
418 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
419 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
421 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
422 callback( producer
, keyboard
);
430 if ( this->sdl_screen
== NULL
|| changed
)
433 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
434 if ( consumer_get_dimensions( &this->window_width
, &this->window_height
) )
435 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
444 this->last_position
== mlt_frame_get_position( frame
) &&
445 this->last_producer
== mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "_producer", NULL
) )
447 sdl_unlock_display( );
448 if ( unlock
!= NULL
) unlock( );
452 // Update last frame shown info
453 this->last_position
= mlt_frame_get_position( frame
);
454 this->last_producer
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "_producer", NULL
);
456 // Get the image, width and height
457 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
458 mlt_events_fire( properties
, "consumer-frame-show", frame
, NULL
);
462 char *rescale
= mlt_properties_get( properties
, "rescale" );
463 if ( rescale
!= NULL
&& strcmp( rescale
, "none" ) )
465 double this_aspect
= display_ratio
/ ( ( double )this->window_width
/ ( double )this->window_height
);
466 this->rect
.w
= this_aspect
* this->window_width
;
467 this->rect
.h
= this->window_height
;
468 if ( this->rect
.w
> this->window_width
)
470 this->rect
.w
= this->window_width
;
471 this->rect
.h
= ( 1.0 / this_aspect
) * this->window_height
;
476 double frame_aspect
= mlt_frame_get_aspect_ratio( frame
) * width
/ height
;
477 this->rect
.w
= frame_aspect
* this->window_height
;
478 this->rect
.h
= this->window_height
;
479 if ( this->rect
.w
> this->window_width
)
481 this->rect
.w
= this->window_width
;
482 this->rect
.h
= ( 1.0 / frame_aspect
) * this->window_width
;
486 this->rect
.x
= ( this->window_width
- this->rect
.w
) / 2;
487 this->rect
.y
= ( this->window_height
- this->rect
.h
) / 2;
489 mlt_properties_set_int( this->properties
, "rect_x", this->rect
.x
);
490 mlt_properties_set_int( this->properties
, "rect_y", this->rect
.y
);
491 mlt_properties_set_int( this->properties
, "rect_w", this->rect
.w
);
492 mlt_properties_set_int( this->properties
, "rect_h", this->rect
.h
);
495 if ( !mlt_consumer_is_stopped( &this->parent
) && SDL_GetVideoSurface( ) != NULL
&& this->sdl_screen
!= NULL
&& this->sdl_screen
->pixels
!= NULL
)
497 memset( this->sdl_screen
->pixels
, 0, this->window_width
* this->window_height
* this->sdl_screen
->format
->BytesPerPixel
);
499 switch( this->sdl_screen
->format
->BytesPerPixel
)
502 display_1( this->sdl_screen
, this->rect
, image
, width
, height
);
505 display_2( this->sdl_screen
, this->rect
, image
, width
, height
);
508 display_3( this->sdl_screen
, this->rect
, image
, width
, height
);
511 display_4( this->sdl_screen
, this->rect
, image
, width
, height
);
514 fprintf( stderr
, "Unsupported video depth %d\n", this->sdl_screen
->format
->BytesPerPixel
);
518 // Flip it into sight
519 SDL_Flip( this->sdl_screen
);
522 sdl_unlock_display();
524 if ( unlock
!= NULL
) unlock( );
529 /** Threaded wrapper for pipe.
532 static void *consumer_thread( void *arg
)
535 consumer_sdl
this = arg
;
538 mlt_consumer consumer
= &this->parent
;
539 mlt_properties properties
= MLT_CONSUMER_PROPERTIES( consumer
);
541 // internal intialization
542 mlt_frame frame
= NULL
;
543 mlt_image_format vfmt
= mlt_image_rgb24a
;
544 int height
= this->height
;
545 int width
= this->width
;
546 uint8_t *image
= NULL
;
548 // Allow the hosting app to provide the preview
549 int preview_off
= mlt_properties_get_int( properties
, "preview_off" );
550 mlt_image_format preview_format
= mlt_properties_get_int( properties
, "preview_format" );
552 // Check if a specific colour space has been requested
553 if ( preview_off
&& preview_format
!= mlt_image_none
)
554 vfmt
= preview_format
;
556 // Loop until told not to
557 while( this->running
)
559 // Get a frame from the attached producer
560 frame
= mlt_consumer_rt_frame( consumer
);
562 // Ensure that we have a frame
563 if ( this->running
&& frame
!= NULL
)
565 if ( preview_off
== 0 )
567 consumer_play_video( this, frame
);
571 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
572 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame
), "format", vfmt
);
573 mlt_events_fire( properties
, "consumer-frame-show", frame
, NULL
);
575 mlt_frame_close( frame
);
579 if ( frame
) mlt_frame_close( frame
);
587 static int consumer_get_dimensions( int *width
, int *height
)
591 // SDL windows manager structure
594 // Specify the SDL Version
595 SDL_VERSION( &wm
.version
);
597 // Get the wm structure
598 if ( SDL_GetWMInfo( &wm
) == 1 )
601 // Check that we have the X11 wm
602 if ( wm
.subsystem
== SDL_SYSWM_X11
)
604 // Get the SDL window
605 Window window
= wm
.info
.x11
.window
;
607 // Get the display session
608 Display
*display
= wm
.info
.x11
.display
;
610 // Get the window attributes
611 XWindowAttributes attr
;
612 XGetWindowAttributes( display
, window
, &attr
);
614 // Determine whether window has changed
615 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
617 // Return width and height
619 *height
= attr
.height
;
627 /** Callback to allow override of the close method.
630 static void consumer_close( mlt_consumer parent
)
632 // Get the actual object
633 consumer_sdl
this = parent
->child
;
636 mlt_consumer_stop( parent
);
638 // Now clean up the rest
639 mlt_consumer_close( parent
);
641 // Finally clean up this