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
;
63 /** Forward references to static functions.
66 static int consumer_start( mlt_consumer parent
);
67 static int consumer_stop( mlt_consumer parent
);
68 static int consumer_is_stopped( mlt_consumer parent
);
69 static void consumer_close( mlt_consumer parent
);
70 static void *consumer_thread( void * );
71 static int consumer_get_dimensions( int *width
, int *height
);
72 static void consumer_sdl_event( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
);
74 /** This is what will be called by the factory - anything can be passed in
75 via the argument, but keep it simple.
78 mlt_consumer
consumer_sdl_still_init( char *arg
)
80 // Create the consumer object
81 consumer_sdl
this = calloc( sizeof( struct consumer_sdl_s
), 1 );
83 // If no malloc'd and consumer init ok
84 if ( this != NULL
&& mlt_consumer_init( &this->parent
, this ) == 0 )
86 // Get the parent consumer object
87 mlt_consumer parent
= &this->parent
;
89 // We have stuff to clean up, so override the close method
90 parent
->close
= consumer_close
;
92 // get a handle on properties
93 mlt_service service
= mlt_consumer_service( parent
);
94 this->properties
= mlt_service_properties( service
);
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 // Get sample aspect ratio
106 this->aspect_ratio
= mlt_properties_get_double( this->properties
, "aspect_ratio" );
108 // Ensure we don't join on a non-running object
111 // Default display aspect ratio
112 this->display_aspect
= 4.0 / 3.0;
114 // process actual param
115 if ( arg
== NULL
|| sscanf( arg
, "%dx%d", &this->width
, &this->height
) != 2 )
117 this->width
= mlt_properties_get_int( this->properties
, "width" );
118 this->height
= mlt_properties_get_int( this->properties
, "height" );
121 // Default window size
122 this->window_width
= ( float )this->height
* this->display_aspect
;
123 this->window_height
= this->height
;
126 this->sdl_flags
= SDL_HWSURFACE
| SDL_ASYNCBLIT
| SDL_HWACCEL
| SDL_RESIZABLE
| SDL_DOUBLEBUF
;
128 // Allow thread to be started/stopped
129 parent
->start
= consumer_start
;
130 parent
->stop
= consumer_stop
;
131 parent
->is_stopped
= consumer_is_stopped
;
133 // Register specific events
134 mlt_events_register( this->properties
, "consumer-sdl-event", ( mlt_transmitter
)consumer_sdl_event
);
136 // Return the consumer produced
140 // malloc or consumer init failed
147 static void consumer_sdl_event( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
149 if ( listener
!= NULL
)
150 listener( owner
, this, ( SDL_Event
* )args
[ 0 ] );
153 static int consumer_start( mlt_consumer parent
)
155 consumer_sdl
this = parent
->child
;
157 if ( !this->running
)
159 pthread_attr_t thread_attributes
;
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( &this->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 if ( mlt_properties_get_int( this->properties
, "resize" ) )
180 mlt_properties_set_int( this->properties
, "width", this->width
);
181 mlt_properties_set_int( this->properties
, "height", this->height
);
184 //this->width = this->height * this->display_aspect;
186 // Inherit the scheduling priority
187 pthread_attr_init( &thread_attributes
);
188 pthread_attr_setinheritsched( &thread_attributes
, PTHREAD_INHERIT_SCHED
);
190 pthread_create( &this->thread
, &thread_attributes
, consumer_thread
, this );
196 static int consumer_stop( mlt_consumer parent
)
198 // Get the actual object
199 consumer_sdl
this = parent
->child
;
201 if ( this->joined
== 0 )
203 // Kill the thread and clean up
206 pthread_join( this->thread
, NULL
);
213 static int consumer_is_stopped( mlt_consumer parent
)
215 consumer_sdl
this = parent
->child
;
216 return !this->running
;
219 static int sdl_lock_display( )
221 SDL_Surface
*screen
= SDL_GetVideoSurface( );
222 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
225 static void sdl_unlock_display( )
227 SDL_Surface
*screen
= SDL_GetVideoSurface( );
228 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
229 SDL_UnlockSurface( screen
);
232 static inline void display_1( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
234 // Generate the affine transform scaling values
235 int scale_width
= ( width
<< 16 ) / rect
.w
;
236 int scale_height
= ( height
<< 16 ) / rect
.h
;
237 int stride
= width
* 3;
241 // Constants defined for clarity and optimisation
242 int scanlength
= screen
->pitch
;
243 uint8_t *start
= ( uint8_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
246 // Iterate through the screen using a very basic scaling algorithm
247 for ( y
= 0; y
< rect
.h
; y
++ )
250 row_index
= ( scale_height
* y
) >> 16;
251 row
= image
+ stride
* row_index
;
252 for ( x
= 0; x
< rect
.w
; x
++ )
254 q
= row
+ ( ( ( scale_width
* x
) >> 16 ) * 3 );
255 *p
++ = SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
261 static inline void display_2( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
263 // Generate the affine transform scaling values
264 int scale_width
= ( width
<< 16 ) / rect
.w
;
265 int scale_height
= ( height
<< 16 ) / rect
.h
;
266 int stride
= width
* 3;
270 // Constants defined for clarity and optimisation
271 int scanlength
= screen
->pitch
/ 2;
272 uint16_t *start
= ( uint16_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
275 // Iterate through the screen using a very basic scaling algorithm
276 for ( y
= 0; y
< rect
.h
; y
++ )
279 row_index
= ( scale_height
* y
) >> 16;
280 row
= image
+ stride
* row_index
;
281 for ( x
= 0; x
< rect
.w
; x
++ )
283 q
= row
+ ( ( ( scale_width
* x
) >> 16 ) * 3 );
284 *p
++ = SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
290 static inline void display_3( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
292 // Generate the affine transform scaling values
293 int scale_width
= ( width
<< 16 ) / rect
.w
;
294 int scale_height
= ( height
<< 16 ) / rect
.h
;
295 int stride
= width
* 3;
299 // Constants defined for clarity and optimisation
300 int scanlength
= screen
->pitch
;
301 uint8_t *start
= ( uint8_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
305 // Iterate through the screen using a very basic scaling algorithm
306 for ( y
= 0; y
< rect
.h
; y
++ )
309 row_index
= ( scale_height
* y
) >> 16;
310 row
= image
+ stride
* row_index
;
311 for ( x
= 0; x
< rect
.w
; x
++ )
313 q
= row
+ ( ( ( scale_width
* x
) >> 16 ) * 3 );
314 pixel
= SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
315 *p
++ = (pixel
& 0xFF0000) >> 16;
316 *p
++ = (pixel
& 0x00FF00) >> 8;
317 *p
++ = (pixel
& 0x0000FF);
323 static inline void display_4( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
325 // Generate the affine transform scaling values
326 int scale_width
= ( width
<< 16 ) / rect
.w
;
327 int scale_height
= ( height
<< 16 ) / rect
.h
;
328 int stride
= width
* 3;
332 // Constants defined for clarity and optimisation
333 int scanlength
= screen
->pitch
/ 4;
334 uint32_t *start
= ( uint32_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
337 // Iterate through the screen using a very basic scaling algorithm
338 for ( y
= 0; y
< rect
.h
; y
++ )
341 row_index
= ( scale_height
* y
) >> 16;
342 row
= image
+ stride
* row_index
;
343 for ( x
= 0; x
< rect
.w
; x
++ )
345 q
= row
+ ( ( ( scale_width
* x
) >> 16 ) * 3 );
346 *p
++ = SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
352 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
354 // Get the properties of this consumer
355 mlt_properties properties
= this->properties
;
357 mlt_image_format vfmt
= mlt_image_rgb24
;
358 int height
= this->height
;
359 int width
= this->width
;
360 uint8_t *image
= NULL
;
363 void ( *lock
)( void ) = mlt_properties_get_data( properties
, "app_lock", NULL
);
364 void ( *unlock
)( void ) = mlt_properties_get_data( properties
, "app_unlock", NULL
);
366 if ( lock
!= NULL
) lock( );
371 if ( this->sdl_screen
!= NULL
)
375 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
377 while ( SDL_PollEvent( &event
) )
379 mlt_events_fire( this->properties
, "consumer-sdl-event", &event
, NULL
);
383 case SDL_VIDEORESIZE
:
384 this->window_width
= event
.resize
.w
;
385 this->window_height
= event
.resize
.h
;
393 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
394 char keyboard
[ 2 ] = " ";
395 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
396 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
398 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
399 callback( producer
, keyboard
);
407 if ( this->sdl_screen
== NULL
|| changed
|| mlt_properties_get_int( properties
, "changed" ) == 2 )
410 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
411 if ( consumer_get_dimensions( &this->window_width
, &this->window_height
) )
412 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
415 mlt_properties_set_int( properties
, "changed", 0 );
419 changed
= mlt_properties_get_int( properties
, "changed" ) | mlt_properties_get_int( mlt_frame_properties( frame
), "refresh" );
420 mlt_properties_set_int( properties
, "changed", 0 );
425 this->last_position
== mlt_frame_get_position( frame
) &&
426 this->last_producer
== mlt_properties_get_data( mlt_frame_properties( frame
), "_producer", NULL
) )
428 sdl_unlock_display( );
429 if ( unlock
!= NULL
)
434 // Update last frame shown info
435 this->last_position
= mlt_frame_get_position( frame
);
436 this->last_producer
= mlt_properties_get_data( mlt_frame_properties( frame
), "_producer", NULL
);
438 // Get the image, width and height
441 mlt_events_fire( properties
, "consumer-frame-show", frame
, NULL
);
443 // I would like to provide upstream scaling here, but this is incorrect
444 // Something? (or everything?) is too sensitive to aspect ratio
445 //width = this->rect.w;
446 //height = this->rect.h;
447 //mlt_properties_set( mlt_frame_properties( frame ), "distort", "true" );
448 //mlt_properties_set_int( mlt_frame_properties( frame ), "normalised_width", width );
449 //mlt_properties_set_int( mlt_frame_properties( frame ), "normalised_height", height );
451 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
456 char *rescale
= mlt_properties_get( properties
, "rescale" );
457 if ( rescale
!= NULL
&& strcmp( rescale
, "none" ) )
459 float this_aspect
= this->display_aspect
/ ( ( float )this->window_width
/ ( float )this->window_height
);
460 this->rect
.w
= this_aspect
* this->window_width
;
461 this->rect
.h
= this->window_height
;
462 if ( this->rect
.w
> this->window_width
)
464 this->rect
.w
= this->window_width
;
465 this->rect
.h
= ( 1.0 / this_aspect
) * this->window_height
;
470 float frame_aspect
= mlt_frame_get_aspect_ratio( frame
) * width
/ height
;
471 this->rect
.w
= frame_aspect
* this->window_height
;
472 this->rect
.h
= this->window_height
;
473 if ( this->rect
.w
> this->window_width
)
475 this->rect
.w
= this->window_width
;
476 this->rect
.h
= ( 1.0 / frame_aspect
) * this->window_width
;
480 this->rect
.x
= ( this->window_width
- this->rect
.w
) / 2;
481 this->rect
.y
= ( this->window_height
- this->rect
.h
) / 2;
483 mlt_properties_set_int( this->properties
, "rect_x", this->rect
.x
);
484 mlt_properties_set_int( this->properties
, "rect_y", this->rect
.y
);
485 mlt_properties_set_int( this->properties
, "rect_w", this->rect
.w
);
486 mlt_properties_set_int( this->properties
, "rect_h", this->rect
.h
);
489 if ( this->sdl_screen
!= NULL
)
491 memset( this->sdl_screen
->pixels
, 0, this->window_width
* this->window_height
* this->sdl_screen
->format
->BytesPerPixel
);
493 switch( this->sdl_screen
->format
->BytesPerPixel
)
496 display_1( this->sdl_screen
, this->rect
, image
, width
, height
);
499 display_2( this->sdl_screen
, this->rect
, image
, width
, height
);
502 display_3( this->sdl_screen
, this->rect
, image
, width
, height
);
505 display_4( this->sdl_screen
, this->rect
, image
, width
, height
);
508 fprintf( stderr
, "Unsupported video depth %d\n", this->sdl_screen
->format
->BytesPerPixel
);
512 // Flip it into sight
513 SDL_Flip( this->sdl_screen
);
516 sdl_unlock_display();
518 if ( unlock
!= NULL
) unlock( );
523 /** Threaded wrapper for pipe.
526 static void *consumer_thread( void *arg
)
529 consumer_sdl
this = arg
;
532 mlt_consumer consumer
= &this->parent
;
534 // internal intialization
535 mlt_frame frame
= NULL
;
536 struct timespec tm
= { 0, 10000000 };
538 if ( mlt_properties_get_int( mlt_consumer_properties( consumer
), "sdl_started" ) == 0 )
540 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
) < 0 )
542 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
546 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
547 SDL_EnableUNICODE( 1 );
551 mlt_properties_set_int( mlt_consumer_properties( consumer
), "changed", 2 );
552 if ( SDL_GetVideoSurface( ) != NULL
)
554 this->sdl_screen
= SDL_GetVideoSurface( );
555 consumer_get_dimensions( &this->window_width
, &this->window_height
);
556 mlt_properties_set_int( mlt_consumer_properties( consumer
), "changed", 0 );
560 // Loop until told not to
561 while( this->running
)
563 // Get a frame from the attached producer
564 frame
= mlt_consumer_rt_frame( consumer
);
566 // Ensure that we have a frame
569 if ( consumer_play_video( this, frame
) == 0 )
570 nanosleep( &tm
, NULL
);
571 mlt_frame_close( frame
);
575 if ( mlt_properties_get_int( mlt_consumer_properties( consumer
), "sdl_started" ) == 0 )
578 this->sdl_screen
= NULL
;
583 static int consumer_get_dimensions( int *width
, int *height
)
587 // SDL windows manager structure
590 // Specify the SDL Version
591 SDL_VERSION( &wm
.version
);
593 // Get the wm structure
594 if ( SDL_GetWMInfo( &wm
) == 1 )
596 // Check that we have the X11 wm
597 if ( wm
.subsystem
== SDL_SYSWM_X11
)
599 // Get the SDL window
600 Window window
= wm
.info
.x11
.window
;
602 // Get the display session
603 Display
*display
= wm
.info
.x11
.display
;
605 // Get the window attributes
606 XWindowAttributes attr
;
607 XGetWindowAttributes( display
, window
, &attr
);
609 // Determine whether window has changed
610 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
612 // Return width and height
614 *height
= attr
.height
;
621 /** Callback to allow override of the close method.
624 static void consumer_close( mlt_consumer parent
)
626 // Get the actual object
627 consumer_sdl
this = parent
->child
;
630 mlt_consumer_stop( parent
);
632 // Now clean up the rest
633 mlt_consumer_close( parent
);
635 // Finally clean up this