]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_still.c
acb50982e4710081ceb47179bcefd87dc5234b2e
[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                 else
115                 {
116                         mlt_properties_set_int( this->properties, "width", this->width );
117                         mlt_properties_set_int( this->properties, "height", this->height );
118                 }
119
120                 // Default window size
121                 this->window_width = ( double )this->height * display_ratio;
122                 this->window_height = this->height;
123
124                 // Set the sdl flags
125                 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
126
127                 // Allow thread to be started/stopped
128                 parent->start = consumer_start;
129                 parent->stop = consumer_stop;
130                 parent->is_stopped = consumer_is_stopped;
131
132                 // Register specific events
133                 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
134
135                 // Return the consumer produced
136                 return parent;
137         }
138
139         // malloc or consumer init failed
140         free( this );
141
142         // Indicate failure
143         return NULL;
144 }
145
146 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
147 {
148         if ( listener != NULL )
149                 listener( owner, this, ( SDL_Event * )args[ 0 ] );
150 }
151
152 static int consumer_start( mlt_consumer parent )
153 {
154         consumer_sdl this = parent->child;
155
156         if ( !this->running )
157         {
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" );
160
161                 // Attach a colour space converter
162                 if ( !this->filtered )
163                 {
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 );
168                         this->filtered = 1;
169                 }
170         
171                 consumer_stop( parent );
172
173                 this->last_position = -1;
174                 this->running = 1;
175                 this->joined = 0;
176
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" );
180
181                 if ( sdl_started == 0 && preview_off == 0 )
182                 {
183                         if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
184                         {
185                                 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
186                                 return -1;
187                         }
188
189                         SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
190                         SDL_EnableUNICODE( 1 );
191                 }
192                 else if ( preview_off == 0 )
193                 {
194                         if ( SDL_GetVideoSurface( ) != NULL )
195                         {
196                                 this->sdl_screen = SDL_GetVideoSurface( );
197                                 consumer_get_dimensions( &this->window_width, &this->window_height );
198                         }
199                 }
200
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 );
203
204                 pthread_create( &this->thread, NULL, consumer_thread, this );
205         }
206
207         return 0;
208 }
209
210 static int consumer_stop( mlt_consumer parent )
211 {
212         // Get the actual object
213         consumer_sdl this = parent->child;
214
215         if ( this->joined == 0 )
216         {
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" );
219
220                 // Kill the thread and clean up
221                 this->running = 0;
222
223                 pthread_join( this->thread, NULL );
224                 this->joined = 1;
225
226                 if ( sdl_started == 0 && preview_off == 0 )
227                         SDL_Quit( );
228
229                 this->sdl_screen = NULL;
230         }
231
232         return 0;
233 }
234
235 static int consumer_is_stopped( mlt_consumer parent )
236 {
237         consumer_sdl this = parent->child;
238         return !this->running;
239 }
240
241 static int sdl_lock_display( )
242 {
243         SDL_Surface *screen = SDL_GetVideoSurface( );
244         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
245 }
246
247 static void sdl_unlock_display( )
248 {
249         SDL_Surface *screen = SDL_GetVideoSurface( );
250         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
251                 SDL_UnlockSurface( screen );
252 }
253
254 static inline void display_1( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
255 {
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;
260         int x, y, row_index;
261         uint8_t *q, *row;
262
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;
266         uint8_t *p;
267
268         // Iterate through the screen using a very basic scaling algorithm
269         for ( y = 0; y < rect.h; y ++ )
270         {
271                 p = start;
272                 row_index = ( 32768 + scale_height * y ) >> 16;
273                 row = image + stride * row_index;
274                 for ( x = 0; x < rect.w; x ++ )
275                 {
276                         q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 3 );
277                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
278                 }
279                 start += scanlength;
280         }
281 }
282
283 static inline void display_2( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
284 {
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;
289         int x, y, row_index;
290         uint8_t *q, *row;
291
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;
295         uint16_t *p;
296
297         // Iterate through the screen using a very basic scaling algorithm
298         for ( y = 0; y < rect.h; y ++ )
299         {
300                 p = start;
301                 row_index = ( 32768 + scale_height * y ) >> 16;
302                 row = image + stride * row_index;
303                 for ( x = 0; x < rect.w; x ++ )
304                 {
305                         q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 3 );
306                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
307                 }
308                 start += scanlength;
309         }
310 }
311
312 static inline void display_3( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
313 {
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;
318         int x, y, row_index;
319         uint8_t *q, *row;
320
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;
324         uint8_t *p;
325         uint32_t pixel;
326
327         // Iterate through the screen using a very basic scaling algorithm
328         for ( y = 0; y < rect.h; y ++ )
329         {
330                 p = start;
331                 row_index = ( 32768 + scale_height * y ) >> 16;
332                 row = image + stride * row_index;
333                 for ( x = 0; x < rect.w; x ++ )
334                 {
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);
340                 }
341                 start += scanlength;
342         }
343 }
344
345 static inline void display_4( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
346 {
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;
351         int x, y, row_index;
352         uint8_t *q, *row;
353
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;
357         uint32_t *p;
358
359         // Iterate through the screen using a very basic scaling algorithm
360         for ( y = 0; y < rect.h; y ++ )
361         {
362                 p = start;
363                 row_index = ( 32768 + scale_height * y ) >> 16;
364                 row = image + stride * row_index;
365                 for ( x = 0; x < rect.w; x ++ )
366                 {
367                         q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 3 );
368                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
369                 }
370                 start += scanlength;
371         }
372 }
373
374 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
375 {
376         // Get the properties of this consumer
377         mlt_properties properties = this->properties;
378
379         mlt_image_format vfmt = mlt_image_rgb24;
380         int height = this->height;
381         int width = this->width;
382         uint8_t *image = NULL;
383         int changed = 0;
384         double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
385
386         void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
387         void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
388
389         if ( lock != NULL ) lock( );
390
391         sdl_lock_display();
392         
393         // Handle events
394         if ( this->sdl_screen != NULL )
395         {
396                 SDL_Event event;
397
398                 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
399
400                 while ( SDL_PollEvent( &event ) )
401                 {
402                         mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
403
404                         switch( event.type )
405                         {
406                                 case SDL_VIDEORESIZE:
407                                         this->window_width = event.resize.w;
408                                         this->window_height = event.resize.h;
409                                         changed = 1;
410                                         break;
411                                 case SDL_QUIT:
412                                         this->running = 0;
413                                         break;
414                                 case SDL_KEYDOWN:
415                                         {
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 )
420                                                 {
421                                                         keyboard[ 0 ] = ( char )event.key.keysym.unicode;
422                                                         callback( producer, keyboard );
423                                                 }
424                                         }
425                                         break;
426                         }
427                 }
428         }
429
430         if ( this->sdl_screen == NULL || changed )
431         {
432                 // open SDL window 
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 );
436                 changed = 1;
437         }
438         else
439         {
440                 changed = 1;
441         }
442
443         if ( changed == 0 &&
444                  this->last_position == mlt_frame_get_position( frame ) &&
445                  this->last_producer == mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "_producer", NULL ) )
446         {
447                 sdl_unlock_display( );
448                 if ( unlock != NULL ) unlock( );
449                 return 0;
450         }
451
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 );
455
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 );
459
460         if ( image != NULL )
461         {
462                 char *rescale = mlt_properties_get( properties, "rescale" );
463                 if ( rescale != NULL && strcmp( rescale, "none" ) )
464                 {
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 )
469                         {
470                                 this->rect.w = this->window_width;
471                                 this->rect.h = ( 1.0 / this_aspect ) * this->window_height;
472                         }
473                 }
474                 else
475                 {
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 )
480                         {
481                                 this->rect.w = this->window_width;
482                                 this->rect.h = ( 1.0 / frame_aspect ) * this->window_width;
483                         }
484                 }
485
486                 this->rect.x = ( this->window_width - this->rect.w ) / 2;
487                 this->rect.y = ( this->window_height - this->rect.h ) / 2;
488
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 );
493         }
494         
495         if ( !mlt_consumer_is_stopped( &this->parent ) && SDL_GetVideoSurface( ) != NULL && this->sdl_screen != NULL && this->sdl_screen->pixels != NULL )
496         {
497                 memset( this->sdl_screen->pixels, 0, this->window_width * this->window_height * this->sdl_screen->format->BytesPerPixel );
498
499                 switch( this->sdl_screen->format->BytesPerPixel )
500                 {
501                         case 1:
502                                 display_1( this->sdl_screen, this->rect, image, width, height );
503                                 break;
504                         case 2:
505                                 display_2( this->sdl_screen, this->rect, image, width, height );
506                                 break;
507                         case 3:
508                                 display_3( this->sdl_screen, this->rect, image, width, height );
509                                 break;
510                         case 4:
511                                 display_4( this->sdl_screen, this->rect, image, width, height );
512                                 break;
513                         default:
514                                 fprintf( stderr, "Unsupported video depth %d\n", this->sdl_screen->format->BytesPerPixel );
515                                 break;
516                 }
517
518                 // Flip it into sight
519                 SDL_Flip( this->sdl_screen );
520         }
521
522         sdl_unlock_display();
523
524         if ( unlock != NULL ) unlock( );
525
526         return 1;
527 }
528
529 /** Threaded wrapper for pipe.
530 */
531
532 static void *consumer_thread( void *arg )
533 {
534         // Identify the arg
535         consumer_sdl this = arg;
536
537         // Get the consumer
538         mlt_consumer consumer = &this->parent;
539         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
540
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;
547
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" );
551
552         // Check if a specific colour space has been requested
553         if ( preview_off && preview_format != mlt_image_none )
554                 vfmt = preview_format;
555
556         // Loop until told not to
557         while( this->running )
558         {
559                 // Get a frame from the attached producer
560                 frame = mlt_consumer_rt_frame( consumer );
561
562                 // Ensure that we have a frame
563                 if ( this->running && frame != NULL )
564                 {
565                         if ( preview_off == 0 )
566                         {
567                                 consumer_play_video( this, frame );
568                         }
569                         else
570                         {
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 );
574                         }
575                         mlt_frame_close( frame );
576                 }
577                 else
578                 {
579                         if ( frame ) mlt_frame_close( frame );
580                         this->running = 0;
581                 }
582         }
583
584         return NULL;
585 }
586
587 static int consumer_get_dimensions( int *width, int *height )
588 {
589         int changed = 0;
590
591         // SDL windows manager structure
592         SDL_SysWMinfo wm;
593
594         // Specify the SDL Version
595         SDL_VERSION( &wm.version );
596
597         // Get the wm structure
598         if ( SDL_GetWMInfo( &wm ) == 1 )
599         {
600 #ifndef __DARWIN__
601                 // Check that we have the X11 wm
602                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
603                 {
604                         // Get the SDL window
605                         Window window = wm.info.x11.window;
606
607                         // Get the display session
608                         Display *display = wm.info.x11.display;
609
610                         // Get the window attributes
611                         XWindowAttributes attr;
612                         XGetWindowAttributes( display, window, &attr );
613
614                         // Determine whether window has changed
615                         changed = *width != attr.width || *height != attr.height;
616
617                         // Return width and height
618                         *width = attr.width;
619                         *height = attr.height;
620                 }
621 #endif
622         }
623
624         return changed;
625 }
626
627 /** Callback to allow override of the close method.
628 */
629
630 static void consumer_close( mlt_consumer parent )
631 {
632         // Get the actual object
633         consumer_sdl this = parent->child;
634
635         // Stop the consumer
636         mlt_consumer_stop( parent );
637
638         // Now clean up the rest
639         mlt_consumer_close( parent );
640
641         // Finally clean up this
642         free( this );
643 }