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