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
;
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" );
115 // Default window size
116 this->window_width
= ( double )this->height
* display_ratio
;
117 this->window_height
= this->height
;
120 //this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
121 // Experimental settings
122 this->sdl_flags
= SDL_RESIZABLE
| SDL_DOUBLEBUF
;
124 // Allow thread to be started/stopped
125 parent
->start
= consumer_start
;
126 parent
->stop
= consumer_stop
;
127 parent
->is_stopped
= consumer_is_stopped
;
129 // Register specific events
130 mlt_events_register( this->properties
, "consumer-sdl-event", ( mlt_transmitter
)consumer_sdl_event
);
132 // Return the consumer produced
136 // malloc or consumer init failed
143 static void consumer_sdl_event( mlt_listener listener
, mlt_properties owner
, mlt_service
this, void **args
)
145 if ( listener
!= NULL
)
146 listener( owner
, this, ( SDL_Event
* )args
[ 0 ] );
149 static int consumer_start( mlt_consumer parent
)
151 consumer_sdl
this = parent
->child
;
153 if ( !this->running
)
155 // Attach a colour space converter
156 if ( !this->filtered
)
158 mlt_filter filter
= mlt_factory_filter( "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 if ( mlt_properties_get_int( this->properties
, "resize" ) )
174 mlt_properties_set_int( this->properties
, "width", this->width
);
175 mlt_properties_set_int( this->properties
, "height", this->height
);
178 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent
), "sdl_started" ) == 0 )
180 if ( SDL_Init( SDL_INIT_VIDEO
| SDL_INIT_NOPARACHUTE
) < 0 )
182 fprintf( stderr
, "Failed to initialize SDL: %s\n", SDL_GetError() );
186 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY
, SDL_DEFAULT_REPEAT_INTERVAL
);
187 SDL_EnableUNICODE( 1 );
191 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( parent
), "changed", 2 );
192 if ( SDL_GetVideoSurface( ) != NULL
)
194 this->sdl_screen
= SDL_GetVideoSurface( );
195 consumer_get_dimensions( &this->window_width
, &this->window_height
);
196 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( parent
), "changed", 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 // Kill the thread and clean up
218 pthread_join( this->thread
, NULL
);
221 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent
), "sdl_started" ) == 0 )
224 this->sdl_screen
= NULL
;
230 static int consumer_is_stopped( mlt_consumer parent
)
232 consumer_sdl
this = parent
->child
;
233 return !this->running
;
236 static int sdl_lock_display( )
238 SDL_Surface
*screen
= SDL_GetVideoSurface( );
239 return screen
!= NULL
&& ( !SDL_MUSTLOCK( screen
) || SDL_LockSurface( screen
) >= 0 );
242 static void sdl_unlock_display( )
244 SDL_Surface
*screen
= SDL_GetVideoSurface( );
245 if ( screen
!= NULL
&& SDL_MUSTLOCK( screen
) )
246 SDL_UnlockSurface( screen
);
249 static inline void display_1( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
251 // Generate the affine transform scaling values
252 int scale_width
= ( width
<< 16 ) / rect
.w
;
253 int scale_height
= ( height
<< 16 ) / rect
.h
;
254 int stride
= width
* 3;
258 // Constants defined for clarity and optimisation
259 int scanlength
= screen
->pitch
;
260 uint8_t *start
= ( uint8_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
263 // Iterate through the screen using a very basic scaling algorithm
264 for ( y
= 0; y
< rect
.h
; y
++ )
267 row_index
= ( scale_height
* y
) >> 16;
268 row
= image
+ stride
* row_index
;
269 for ( x
= 0; x
< rect
.w
; x
++ )
271 q
= row
+ ( ( ( scale_width
* x
) >> 16 ) * 3 );
272 *p
++ = SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
278 static inline void display_2( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
280 // Generate the affine transform scaling values
281 int scale_width
= ( width
<< 16 ) / rect
.w
;
282 int scale_height
= ( height
<< 16 ) / rect
.h
;
283 int stride
= width
* 3;
287 // Constants defined for clarity and optimisation
288 int scanlength
= screen
->pitch
/ 2;
289 uint16_t *start
= ( uint16_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
292 // Iterate through the screen using a very basic scaling algorithm
293 for ( y
= 0; y
< rect
.h
; y
++ )
296 row_index
= ( scale_height
* y
) >> 16;
297 row
= image
+ stride
* row_index
;
298 for ( x
= 0; x
< rect
.w
; x
++ )
300 q
= row
+ ( ( ( scale_width
* x
) >> 16 ) * 3 );
301 *p
++ = SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
307 static inline void display_3( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
309 // Generate the affine transform scaling values
310 int scale_width
= ( width
<< 16 ) / rect
.w
;
311 int scale_height
= ( height
<< 16 ) / rect
.h
;
312 int stride
= width
* 3;
316 // Constants defined for clarity and optimisation
317 int scanlength
= screen
->pitch
;
318 uint8_t *start
= ( uint8_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
322 // Iterate through the screen using a very basic scaling algorithm
323 for ( y
= 0; y
< rect
.h
; y
++ )
326 row_index
= ( scale_height
* y
) >> 16;
327 row
= image
+ stride
* row_index
;
328 for ( x
= 0; x
< rect
.w
; x
++ )
330 q
= row
+ ( ( ( scale_width
* x
) >> 16 ) * 3 );
331 pixel
= SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
332 *p
++ = (pixel
& 0xFF0000) >> 16;
333 *p
++ = (pixel
& 0x00FF00) >> 8;
334 *p
++ = (pixel
& 0x0000FF);
340 static inline void display_4( SDL_Surface
*screen
, SDL_Rect rect
, uint8_t *image
, int width
, int height
)
342 // Generate the affine transform scaling values
343 int scale_width
= ( width
<< 16 ) / rect
.w
;
344 int scale_height
= ( height
<< 16 ) / rect
.h
;
345 int stride
= width
* 3;
349 // Constants defined for clarity and optimisation
350 int scanlength
= screen
->pitch
/ 4;
351 uint32_t *start
= ( uint32_t * )screen
->pixels
+ rect
.y
* scanlength
+ rect
.x
;
354 // Iterate through the screen using a very basic scaling algorithm
355 for ( y
= 0; y
< rect
.h
; y
++ )
358 row_index
= ( scale_height
* y
) >> 16;
359 row
= image
+ stride
* row_index
;
360 for ( x
= 0; x
< rect
.w
; x
++ )
362 q
= row
+ ( ( ( scale_width
* x
) >> 16 ) * 3 );
363 *p
++ = SDL_MapRGB( screen
->format
, *q
, *( q
+ 1 ), *( q
+ 2 ) );
369 static int consumer_play_video( consumer_sdl
this, mlt_frame frame
)
371 // Get the properties of this consumer
372 mlt_properties properties
= this->properties
;
374 mlt_image_format vfmt
= mlt_image_rgb24
;
375 int height
= this->height
;
376 int width
= this->width
;
377 uint8_t *image
= NULL
;
379 double display_ratio
= mlt_properties_get_double( this->properties
, "display_ratio" );
381 void ( *lock
)( void ) = mlt_properties_get_data( properties
, "app_lock", NULL
);
382 void ( *unlock
)( void ) = mlt_properties_get_data( properties
, "app_unlock", NULL
);
384 if ( lock
!= NULL
) lock( );
389 if ( this->sdl_screen
!= NULL
)
393 changed
= consumer_get_dimensions( &this->window_width
, &this->window_height
);
395 while ( SDL_PollEvent( &event
) )
397 mlt_events_fire( this->properties
, "consumer-sdl-event", &event
, NULL
);
401 case SDL_VIDEORESIZE
:
402 this->window_width
= event
.resize
.w
;
403 this->window_height
= event
.resize
.h
;
411 mlt_producer producer
= mlt_properties_get_data( properties
, "transport_producer", NULL
);
412 char keyboard
[ 2 ] = " ";
413 void (*callback
)( mlt_producer
, char * ) = mlt_properties_get_data( properties
, "transport_callback", NULL
);
414 if ( callback
!= NULL
&& producer
!= NULL
&& event
.key
.keysym
.unicode
< 0x80 && event
.key
.keysym
.unicode
> 0 )
416 keyboard
[ 0 ] = ( char )event
.key
.keysym
.unicode
;
417 callback( producer
, keyboard
);
425 if ( this->sdl_screen
== NULL
|| changed
|| mlt_properties_get_int( properties
, "changed" ) == 2 )
428 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
429 if ( consumer_get_dimensions( &this->window_width
, &this->window_height
) )
430 this->sdl_screen
= SDL_SetVideoMode( this->window_width
, this->window_height
, 0, this->sdl_flags
);
433 mlt_properties_set_int( properties
, "changed", 0 );
438 mlt_properties_set_int( properties
, "changed", 0 );
442 this->last_position
== mlt_frame_get_position( frame
) &&
443 this->last_producer
== mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "_producer", NULL
) )
445 sdl_unlock_display( );
446 if ( unlock
!= NULL
)
451 // Update last frame shown info
452 this->last_position
= mlt_frame_get_position( frame
);
453 this->last_producer
= mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame
), "_producer", NULL
);
455 // Get the image, width and height
456 mlt_events_fire( properties
, "consumer-frame-show", frame
, NULL
);
457 mlt_frame_get_image( frame
, &image
, &vfmt
, &width
, &height
, 0 );
461 char *rescale
= mlt_properties_get( properties
, "rescale" );
462 if ( rescale
!= NULL
&& strcmp( rescale
, "none" ) )
464 double this_aspect
= display_ratio
/ ( ( double )this->window_width
/ ( double )this->window_height
);
465 this->rect
.w
= this_aspect
* this->window_width
;
466 this->rect
.h
= this->window_height
;
467 if ( this->rect
.w
> this->window_width
)
469 this->rect
.w
= this->window_width
;
470 this->rect
.h
= ( 1.0 / this_aspect
) * this->window_height
;
475 double frame_aspect
= mlt_frame_get_aspect_ratio( frame
) * width
/ height
;
476 this->rect
.w
= frame_aspect
* this->window_height
;
477 this->rect
.h
= this->window_height
;
478 if ( this->rect
.w
> this->window_width
)
480 this->rect
.w
= this->window_width
;
481 this->rect
.h
= ( 1.0 / frame_aspect
) * this->window_width
;
485 this->rect
.x
= ( this->window_width
- this->rect
.w
) / 2;
486 this->rect
.y
= ( this->window_height
- this->rect
.h
) / 2;
488 mlt_properties_set_int( this->properties
, "rect_x", this->rect
.x
);
489 mlt_properties_set_int( this->properties
, "rect_y", this->rect
.y
);
490 mlt_properties_set_int( this->properties
, "rect_w", this->rect
.w
);
491 mlt_properties_set_int( this->properties
, "rect_h", this->rect
.h
);
494 if ( !mlt_consumer_is_stopped( &this->parent
) && SDL_GetVideoSurface( ) != NULL
&& this->sdl_screen
!= NULL
&& this->sdl_screen
->pixels
!= NULL
)
496 memset( this->sdl_screen
->pixels
, 0, this->window_width
* this->window_height
* this->sdl_screen
->format
->BytesPerPixel
);
498 switch( this->sdl_screen
->format
->BytesPerPixel
)
501 display_1( this->sdl_screen
, this->rect
, image
, width
, height
);
504 display_2( this->sdl_screen
, this->rect
, image
, width
, height
);
507 display_3( this->sdl_screen
, this->rect
, image
, width
, height
);
510 display_4( this->sdl_screen
, this->rect
, image
, width
, height
);
513 fprintf( stderr
, "Unsupported video depth %d\n", this->sdl_screen
->format
->BytesPerPixel
);
517 // Flip it into sight
518 SDL_Flip( this->sdl_screen
);
521 sdl_unlock_display();
523 if ( unlock
!= NULL
) unlock( );
528 /** Threaded wrapper for pipe.
531 static void *consumer_thread( void *arg
)
534 consumer_sdl
this = arg
;
537 mlt_consumer consumer
= &this->parent
;
539 // internal intialization
540 mlt_frame frame
= NULL
;
542 // Loop until told not to
543 while( this->running
)
545 // Get a frame from the attached producer
546 frame
= mlt_consumer_rt_frame( consumer
);
548 // Ensure that we have a frame
549 if ( this->running
&& frame
!= NULL
)
551 consumer_play_video( this, frame
);
552 mlt_frame_close( frame
);
556 if ( frame
) mlt_frame_close( frame
);
564 static int consumer_get_dimensions( int *width
, int *height
)
568 // SDL windows manager structure
571 // Specify the SDL Version
572 SDL_VERSION( &wm
.version
);
574 // Get the wm structure
575 if ( SDL_GetWMInfo( &wm
) == 1 )
578 // Check that we have the X11 wm
579 if ( wm
.subsystem
== SDL_SYSWM_X11
)
581 // Get the SDL window
582 Window window
= wm
.info
.x11
.window
;
584 // Get the display session
585 Display
*display
= wm
.info
.x11
.display
;
587 // Get the window attributes
588 XWindowAttributes attr
;
589 XGetWindowAttributes( display
, window
, &attr
);
591 // Determine whether window has changed
592 changed
= *width
!= attr
.width
|| *height
!= attr
.height
;
594 // Return width and height
596 *height
= attr
.height
;
604 /** Callback to allow override of the close method.
607 static void consumer_close( mlt_consumer parent
)
609 // Get the actual object
610 consumer_sdl
this = parent
->child
;
613 mlt_consumer_stop( parent
);
615 // Now clean up the rest
616 mlt_consumer_close( parent
);
618 // Finally clean up this