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