]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_still.c
9957dd1cf748363b6b22f257fd95b1dbf0c7b4a5
[mlt] / src / modules / sdl / consumer_sdl_still.c
1 /*
2  * consumer_sdl_still.c -- A Simple DirectMedia Layer consumer
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Charles Yates
5  *
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.
10  *
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.
15  *
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.
19  */
20
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>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <SDL/SDL.h>
31 #include <SDL/SDL_syswm.h>
32 #include <sys/time.h>
33
34 /** This classes definition.
35 */
36
37 typedef struct consumer_sdl_s *consumer_sdl;
38
39 struct consumer_sdl_s
40 {
41         struct mlt_consumer_s parent;
42         mlt_properties properties;
43         pthread_t thread;
44         int joined;
45         int running;
46         int window_width;
47         int window_height;
48         int width;
49         int height;
50         int playing;
51         int sdl_flags;
52         SDL_Surface *sdl_screen;
53         SDL_Rect rect;
54         uint8_t *buffer;
55         int last_position;
56         mlt_producer last_producer;
57         int filtered;
58 };
59
60 /** Forward references to static functions.
61 */
62
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 );
70
71 /** This is what will be called by the factory - anything can be passed in
72         via the argument, but keep it simple.
73 */
74
75 mlt_consumer consumer_sdl_still_init( char *arg )
76 {
77         // Create the consumer object
78         consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
79
80         // If no malloc'd and consumer init ok
81         if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
82         {
83                 // Get the parent consumer object
84                 mlt_consumer parent = &this->parent;
85
86                 // get a handle on properties
87                 mlt_service service = MLT_CONSUMER_SERVICE( parent );
88                 this->properties = MLT_SERVICE_PROPERTIES( service );
89
90                 // Get the default display ratio
91                 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
92
93                 // We have stuff to clean up, so override the close method
94                 parent->close = consumer_close;
95
96                 // Default scaler (for now we'll use nearest)
97                 mlt_properties_set( this->properties, "rescale", "nearest" );
98
99                 // We're always going to run this in non-realtime mode
100                 mlt_properties_set( this->properties, "real_time", "0" );
101
102                 // Default progressive true
103                 mlt_properties_set_int( this->properties, "progressive", 1 );
104
105                 // Ensure we don't join on a non-running object
106                 this->joined = 1;
107                 
108                 // process actual param
109                 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
110                 {
111                         this->width = mlt_properties_get_int( this->properties, "width" );
112                         this->height = mlt_properties_get_int( this->properties, "height" );
113                 }
114
115                 // Default window size
116                 this->window_width = ( double )this->height * display_ratio;
117                 this->window_height = this->height;
118
119                 // Set the sdl flags
120                 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
121
122                 // Allow thread to be started/stopped
123                 parent->start = consumer_start;
124                 parent->stop = consumer_stop;
125                 parent->is_stopped = consumer_is_stopped;
126
127                 // Register specific events
128                 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
129
130                 // Return the consumer produced
131                 return parent;
132         }
133
134         // malloc or consumer init failed
135         free( this );
136
137         // Indicate failure
138         return NULL;
139 }
140
141 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
142 {
143         if ( listener != NULL )
144                 listener( owner, this, ( SDL_Event * )args[ 0 ] );
145 }
146
147 static int consumer_start( mlt_consumer parent )
148 {
149         consumer_sdl this = parent->child;
150
151         if ( !this->running )
152         {
153                 // Attach a colour space converter
154                 if ( !this->filtered )
155                 {
156                         mlt_filter filter = mlt_factory_filter( "avcolour_space", NULL );
157                         mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "forced", mlt_image_yuv422 );
158                         mlt_service_attach( MLT_CONSUMER_SERVICE( parent ), filter );
159                         mlt_filter_close( filter );
160                         this->filtered = 1;
161                 }
162         
163                 consumer_stop( parent );
164
165                 this->last_position = -1;
166                 this->running = 1;
167                 this->joined = 0;
168
169                 // Allow the user to force resizing to window size
170                 if ( mlt_properties_get_int( this->properties, "resize" ) )
171                 {
172                         mlt_properties_set_int( this->properties, "width", this->width );
173                         mlt_properties_set_int( this->properties, "height", this->height );
174                 }
175
176                 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
177                 {
178                         if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
179                         {
180                                 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
181                                 return -1;
182                         }
183
184                         SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
185                         SDL_EnableUNICODE( 1 );
186                 }
187                 else
188                 {
189                         if ( SDL_GetVideoSurface( ) != NULL )
190                         {
191                                 this->sdl_screen = SDL_GetVideoSurface( );
192                                 consumer_get_dimensions( &this->window_width, &this->window_height );
193                         }
194                 }
195
196                 if ( this->sdl_screen == NULL )
197                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
198
199                 pthread_create( &this->thread, NULL, consumer_thread, this );
200         }
201
202         return 0;
203 }
204
205 static int consumer_stop( mlt_consumer parent )
206 {
207         // Get the actual object
208         consumer_sdl this = parent->child;
209
210         if ( this->joined == 0 )
211         {
212                 // Kill the thread and clean up
213                 this->running = 0;
214
215                 pthread_join( this->thread, NULL );
216                 this->joined = 1;
217
218                 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" ) == 0 )
219                         SDL_Quit( );
220
221                 this->sdl_screen = NULL;
222         }
223
224         return 0;
225 }
226
227 static int consumer_is_stopped( mlt_consumer parent )
228 {
229         consumer_sdl this = parent->child;
230         return !this->running;
231 }
232
233 static int sdl_lock_display( )
234 {
235         SDL_Surface *screen = SDL_GetVideoSurface( );
236         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
237 }
238
239 static void sdl_unlock_display( )
240 {
241         SDL_Surface *screen = SDL_GetVideoSurface( );
242         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
243                 SDL_UnlockSurface( screen );
244 }
245
246 static inline void display_1( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
247 {
248         // Generate the affine transform scaling values
249         int scale_width = ( width << 16 ) / rect.w;
250         int scale_height = ( height << 16 ) / rect.h;
251         int stride = width * 3;
252         int x, y, row_index;
253         uint8_t *q, *row;
254
255         // Constants defined for clarity and optimisation
256         int scanlength = screen->pitch;
257         uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
258         uint8_t *p;
259
260         // Iterate through the screen using a very basic scaling algorithm
261         for ( y = 0; y < rect.h; y ++ )
262         {
263                 p = start;
264                 row_index = ( scale_height * y ) >> 16;
265                 row = image + stride * row_index;
266                 for ( x = 0; x < rect.w; x ++ )
267                 {
268                         q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
269                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
270                 }
271                 start += scanlength;
272         }
273 }
274
275 static inline void display_2( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
276 {
277         // Generate the affine transform scaling values
278         int scale_width = ( width << 16 ) / rect.w;
279         int scale_height = ( height << 16 ) / rect.h;
280         int stride = width * 3;
281         int x, y, row_index;
282         uint8_t *q, *row;
283
284         // Constants defined for clarity and optimisation
285         int scanlength = screen->pitch / 2;
286         uint16_t *start = ( uint16_t * )screen->pixels + rect.y * scanlength + rect.x;
287         uint16_t *p;
288
289         // Iterate through the screen using a very basic scaling algorithm
290         for ( y = 0; y < rect.h; y ++ )
291         {
292                 p = start;
293                 row_index = ( scale_height * y ) >> 16;
294                 row = image + stride * row_index;
295                 for ( x = 0; x < rect.w; x ++ )
296                 {
297                         q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
298                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
299                 }
300                 start += scanlength;
301         }
302 }
303
304 static inline void display_3( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
305 {
306         // Generate the affine transform scaling values
307         int scale_width = ( width << 16 ) / rect.w;
308         int scale_height = ( height << 16 ) / rect.h;
309         int stride = width * 3;
310         int x, y, row_index;
311         uint8_t *q, *row;
312
313         // Constants defined for clarity and optimisation
314         int scanlength = screen->pitch;
315         uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
316         uint8_t *p;
317         uint32_t pixel;
318
319         // Iterate through the screen using a very basic scaling algorithm
320         for ( y = 0; y < rect.h; y ++ )
321         {
322                 p = start;
323                 row_index = ( scale_height * y ) >> 16;
324                 row = image + stride * row_index;
325                 for ( x = 0; x < rect.w; x ++ )
326                 {
327                         q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
328                         pixel = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
329                         *p ++ = (pixel & 0xFF0000) >> 16;
330                         *p ++ = (pixel & 0x00FF00) >> 8;
331                         *p ++ = (pixel & 0x0000FF);
332                 }
333                 start += scanlength;
334         }
335 }
336
337 static inline void display_4( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
338 {
339         // Generate the affine transform scaling values
340         int scale_width = ( width << 16 ) / rect.w;
341         int scale_height = ( height << 16 ) / rect.h;
342         int stride = width * 3;
343         int x, y, row_index;
344         uint8_t *q, *row;
345
346         // Constants defined for clarity and optimisation
347         int scanlength = screen->pitch / 4;
348         uint32_t *start = ( uint32_t * )screen->pixels + rect.y * scanlength + rect.x;
349         uint32_t *p;
350
351         // Iterate through the screen using a very basic scaling algorithm
352         for ( y = 0; y < rect.h; y ++ )
353         {
354                 p = start;
355                 row_index = ( scale_height * y ) >> 16;
356                 row = image + stride * row_index;
357                 for ( x = 0; x < rect.w; x ++ )
358                 {
359                         q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
360                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
361                 }
362                 start += scanlength;
363         }
364 }
365
366 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
367 {
368         // Get the properties of this consumer
369         mlt_properties properties = this->properties;
370
371         mlt_image_format vfmt = mlt_image_rgb24;
372         int height = this->height;
373         int width = this->width;
374         uint8_t *image = NULL;
375         int changed = 0;
376         double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
377
378         void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
379         void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
380
381         if ( lock != NULL ) lock( );
382
383         sdl_lock_display();
384         
385         // Handle events
386         if ( this->sdl_screen != NULL )
387         {
388                 SDL_Event event;
389
390                 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
391
392                 while ( SDL_PollEvent( &event ) )
393                 {
394                         mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
395
396                         switch( event.type )
397                         {
398                                 case SDL_VIDEORESIZE:
399                                         this->window_width = event.resize.w;
400                                         this->window_height = event.resize.h;
401                                         changed = 1;
402                                         break;
403                                 case SDL_QUIT:
404                                         this->running = 0;
405                                         break;
406                                 case SDL_KEYDOWN:
407                                         {
408                                                 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
409                                                 char keyboard[ 2 ] = " ";
410                                                 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
411                                                 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
412                                                 {
413                                                         keyboard[ 0 ] = ( char )event.key.keysym.unicode;
414                                                         callback( producer, keyboard );
415                                                 }
416                                         }
417                                         break;
418                         }
419                 }
420         }
421
422         if ( this->sdl_screen == NULL || changed )
423         {
424                 // open SDL window 
425                 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
426                 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
427                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
428                 changed = 1;
429         }
430         else
431         {
432                 changed = 1;
433         }
434
435         if ( changed == 0 &&
436                  this->last_position == mlt_frame_get_position( frame ) &&
437                  this->last_producer == mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "_producer", NULL ) )
438         {
439                 sdl_unlock_display( );
440                 if ( unlock != NULL )
441                         unlock( );
442                 return 0;
443         }
444
445         // Update last frame shown info
446         this->last_position = mlt_frame_get_position( frame );
447         this->last_producer = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "_producer", NULL );
448
449         // Get the image, width and height
450         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
451         mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
452
453         if ( image != NULL )
454         {
455                 char *rescale = mlt_properties_get( properties, "rescale" );
456                 if ( rescale != NULL && strcmp( rescale, "none" ) )
457                 {
458                         double this_aspect = display_ratio / ( ( double )this->window_width / ( double )this->window_height );
459                         this->rect.w = this_aspect * this->window_width;
460                         this->rect.h = this->window_height;
461                         if ( this->rect.w > this->window_width )
462                         {
463                                 this->rect.w = this->window_width;
464                                 this->rect.h = ( 1.0 / this_aspect ) * this->window_height;
465                         }
466                 }
467                 else
468                 {
469                         double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
470                         this->rect.w = frame_aspect * this->window_height;
471                         this->rect.h = this->window_height;
472                         if ( this->rect.w > this->window_width )
473                         {
474                                 this->rect.w = this->window_width;
475                                 this->rect.h = ( 1.0 / frame_aspect ) * this->window_width;
476                         }
477                 }
478
479                 this->rect.x = ( this->window_width - this->rect.w ) / 2;
480                 this->rect.y = ( this->window_height - this->rect.h ) / 2;
481
482                 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
483                 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
484                 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
485                 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
486         }
487         
488         if ( !mlt_consumer_is_stopped( &this->parent ) && SDL_GetVideoSurface( ) != NULL && this->sdl_screen != NULL && this->sdl_screen->pixels != NULL )
489         {
490                 memset( this->sdl_screen->pixels, 0, this->window_width * this->window_height * this->sdl_screen->format->BytesPerPixel );
491
492                 switch( this->sdl_screen->format->BytesPerPixel )
493                 {
494                         case 1:
495                                 display_1( this->sdl_screen, this->rect, image, width, height );
496                                 break;
497                         case 2:
498                                 display_2( this->sdl_screen, this->rect, image, width, height );
499                                 break;
500                         case 3:
501                                 display_3( this->sdl_screen, this->rect, image, width, height );
502                                 break;
503                         case 4:
504                                 display_4( this->sdl_screen, this->rect, image, width, height );
505                                 break;
506                         default:
507                                 fprintf( stderr, "Unsupported video depth %d\n", this->sdl_screen->format->BytesPerPixel );
508                                 break;
509                 }
510
511                 // Flip it into sight
512                 SDL_Flip( this->sdl_screen );
513         }
514
515         sdl_unlock_display();
516
517         if ( unlock != NULL ) unlock( );
518
519         return 1;
520 }
521
522 /** Threaded wrapper for pipe.
523 */
524
525 static void *consumer_thread( void *arg )
526 {
527         // Identify the arg
528         consumer_sdl this = arg;
529
530         // Get the consumer
531         mlt_consumer consumer = &this->parent;
532
533         // internal intialization
534         mlt_frame frame = NULL;
535
536         // Loop until told not to
537         while( this->running )
538         {
539                 // Get a frame from the attached producer
540                 frame = mlt_consumer_rt_frame( consumer );
541
542                 // Ensure that we have a frame
543                 if ( this->running && frame != NULL )
544                 {
545                         consumer_play_video( this, frame );
546                         mlt_frame_close( frame );
547                 }
548                 else
549                 {
550                         if ( frame ) mlt_frame_close( frame );
551                         this->running = 0;
552                 }
553         }
554
555         return NULL;
556 }
557
558 static int consumer_get_dimensions( int *width, int *height )
559 {
560         int changed = 0;
561
562         // SDL windows manager structure
563         SDL_SysWMinfo wm;
564
565         // Specify the SDL Version
566         SDL_VERSION( &wm.version );
567
568         // Get the wm structure
569         if ( SDL_GetWMInfo( &wm ) == 1 )
570         {
571 #ifndef __DARWIN__
572                 // Check that we have the X11 wm
573                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
574                 {
575                         // Get the SDL window
576                         Window window = wm.info.x11.window;
577
578                         // Get the display session
579                         Display *display = wm.info.x11.display;
580
581                         // Get the window attributes
582                         XWindowAttributes attr;
583                         XGetWindowAttributes( display, window, &attr );
584
585                         // Determine whether window has changed
586                         changed = *width != attr.width || *height != attr.height;
587
588                         // Return width and height
589                         *width = attr.width;
590                         *height = attr.height;
591                 }
592 #endif
593         }
594
595         return changed;
596 }
597
598 /** Callback to allow override of the close method.
599 */
600
601 static void consumer_close( mlt_consumer parent )
602 {
603         // Get the actual object
604         consumer_sdl this = parent->child;
605
606         // Stop the consumer
607         mlt_consumer_stop( parent );
608
609         // Now clean up the rest
610         mlt_consumer_close( parent );
611
612         // Finally clean up this
613         free( this );
614 }