]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_still.c
Remove local references to SDL_Surface. (SF-186)
[mlt] / src / modules / sdl / consumer_sdl_still.c
1 /*
2  * consumer_sdl_still.c -- A Simple DirectMedia Layer consumer
3  * Copyright (C) 2003-2004, 2010 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.h>
32 #include <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_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( 1, sizeof( struct consumer_sdl_s ) );
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 + 0.5;
166                 this->window_height = this->height;
167
168                 if ( sdl_started == 0 && preview_off == 0 )
169                 {
170                         pthread_mutex_lock( &mlt_sdl_mutex );
171                         int ret = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE );
172                         pthread_mutex_unlock( &mlt_sdl_mutex );
173                         if ( ret < 0 )
174                         {
175                                 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
176                                 return -1;
177                         }
178
179                         SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
180                         SDL_EnableUNICODE( 1 );
181                 }
182
183                 pthread_mutex_lock( &mlt_sdl_mutex );
184                 if ( !SDL_GetVideoSurface() && preview_off == 0 )
185                         SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
186                 pthread_mutex_unlock( &mlt_sdl_mutex );
187
188                 pthread_create( &this->thread, NULL, consumer_thread, this );
189         }
190
191         return 0;
192 }
193
194 static int consumer_stop( mlt_consumer parent )
195 {
196         // Get the actual object
197         consumer_sdl this = parent->child;
198
199         if ( this->joined == 0 )
200         {
201                 int preview_off = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "preview_off" );
202                 int sdl_started = mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( parent ), "sdl_started" );
203         
204                 // Kill the thread and clean up
205                 this->running = 0;
206
207                 pthread_join( this->thread, NULL );
208                 this->joined = 1;
209
210                 if ( sdl_started == 0 && preview_off == 0 )
211                 {
212                         pthread_mutex_lock( &mlt_sdl_mutex );
213                         SDL_Quit( );
214                         pthread_mutex_unlock( &mlt_sdl_mutex );
215                 }
216         }
217
218         return 0;
219 }
220
221 static int consumer_is_stopped( mlt_consumer parent )
222 {
223         consumer_sdl this = parent->child;
224         return !this->running;
225 }
226
227 static int sdl_lock_display( )
228 {
229         pthread_mutex_lock( &mlt_sdl_mutex );
230         SDL_Surface *screen = SDL_GetVideoSurface( );
231         int result = screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
232         pthread_mutex_unlock( &mlt_sdl_mutex );
233         return result;
234 }
235
236 static void sdl_unlock_display( )
237 {
238         pthread_mutex_lock( &mlt_sdl_mutex );
239         SDL_Surface *screen = SDL_GetVideoSurface( );
240         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
241                 SDL_UnlockSurface( screen );
242         pthread_mutex_unlock( &mlt_sdl_mutex );
243 }
244
245 static inline void display_1( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
246 {
247         // Generate the affine transform scaling values
248         if ( rect.w == 0 || rect.h == 0 ) return;
249         int scale_width = ( width << 16 ) / rect.w;
250         int scale_height = ( height << 16 ) / rect.h;
251         int stride = width * 4;
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 = ( 32768 + scale_height * y ) >> 16;
265                 row = image + stride * row_index;
266                 for ( x = 0; x < rect.w; x ++ )
267                 {
268                         q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
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         if ( rect.w == 0 || rect.h == 0 ) return;
279         int scale_width = ( width << 16 ) / rect.w;
280         int scale_height = ( height << 16 ) / rect.h;
281         int stride = width * 4;
282         int x, y, row_index;
283         uint8_t *q, *row;
284
285         // Constants defined for clarity and optimisation
286         int scanlength = screen->pitch / 2;
287         uint16_t *start = ( uint16_t * )screen->pixels + rect.y * scanlength + rect.x;
288         uint16_t *p;
289
290         // Iterate through the screen using a very basic scaling algorithm
291         for ( y = 0; y < rect.h; y ++ )
292         {
293                 p = start;
294                 row_index = ( 32768 + scale_height * y ) >> 16;
295                 row = image + stride * row_index;
296                 for ( x = 0; x < rect.w; x ++ )
297                 {
298                         q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
299                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
300                 }
301                 start += scanlength;
302         }
303 }
304
305 static inline void display_3( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
306 {
307         // Generate the affine transform scaling values
308         if ( rect.w == 0 || rect.h == 0 ) return;
309         int scale_width = ( width << 16 ) / rect.w;
310         int scale_height = ( height << 16 ) / rect.h;
311         int stride = width * 4;
312         int x, y, row_index;
313         uint8_t *q, *row;
314
315         // Constants defined for clarity and optimisation
316         int scanlength = screen->pitch;
317         uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
318         uint8_t *p;
319         uint32_t pixel;
320
321         // Iterate through the screen using a very basic scaling algorithm
322         for ( y = 0; y < rect.h; y ++ )
323         {
324                 p = start;
325                 row_index = ( 32768 + scale_height * y ) >> 16;
326                 row = image + stride * row_index;
327                 for ( x = 0; x < rect.w; x ++ )
328                 {
329                         q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
330                         pixel = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
331                         *p ++ = (pixel & 0xFF0000) >> 16;
332                         *p ++ = (pixel & 0x00FF00) >> 8;
333                         *p ++ = (pixel & 0x0000FF);
334                 }
335                 start += scanlength;
336         }
337 }
338
339 static inline void display_4( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
340 {
341         // Generate the affine transform scaling values
342         if ( rect.w == 0 || rect.h == 0 ) return;
343         int scale_width = ( width << 16 ) / rect.w;
344         int scale_height = ( height << 16 ) / rect.h;
345         int stride = width * 4;
346         int x, y, row_index;
347         uint8_t *q, *row;
348
349         // Constants defined for clarity and optimisation
350         int scanlength = screen->pitch / 4;
351         uint32_t *start = ( uint32_t * )screen->pixels + rect.y * scanlength + rect.x;
352         uint32_t *p;
353
354         // Iterate through the screen using a very basic scaling algorithm
355         for ( y = 0; y < rect.h; y ++ )
356         {
357                 p = start;
358                 row_index = ( 32768 + scale_height * y ) >> 16;
359                 row = image + stride * row_index;
360                 for ( x = 0; x < rect.w; x ++ )
361                 {
362                         q = row + ( ( ( 32768 + scale_width * x ) >> 16 ) * 4 );
363                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
364                 }
365                 start += scanlength;
366         }
367 }
368
369 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
370 {
371         // Get the properties of this consumer
372         mlt_properties properties = this->properties;
373
374         mlt_image_format vfmt = mlt_image_rgb24a;
375         int height = this->height;
376         int width = this->width;
377         uint8_t *image = NULL;
378         int changed = 0;
379         double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
380
381         void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
382         void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
383
384         if ( lock != NULL ) lock( );
385         void *pool = mlt_cocoa_autorelease_init();
386         sdl_lock_display();
387         
388         // Handle events
389         if ( SDL_GetVideoSurface() )
390         {
391                 SDL_Event event;
392
393                 pthread_mutex_lock( &mlt_sdl_mutex );
394                 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
395                 pthread_mutex_unlock( &mlt_sdl_mutex );
396
397                 while ( SDL_PollEvent( &event ) )
398                 {
399                         mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
400
401                         switch( event.type )
402                         {
403                                 case SDL_VIDEORESIZE:
404                                         this->window_width = event.resize.w;
405                                         this->window_height = event.resize.h;
406                                         changed = 1;
407                                         break;
408                                 case SDL_QUIT:
409                                         this->running = 0;
410                                         break;
411                                 case SDL_KEYDOWN:
412                                         {
413                                                 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
414                                                 char keyboard[ 2 ] = " ";
415                                                 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
416                                                 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
417                                                 {
418                                                         keyboard[ 0 ] = ( char )event.key.keysym.unicode;
419                                                         callback( producer, keyboard );
420                                                 }
421                                         }
422                                         break;
423                         }
424                 }
425         }
426
427         if ( !SDL_GetVideoSurface() || changed )
428         {
429                 // open SDL window
430                 pthread_mutex_lock( &mlt_sdl_mutex );
431                 consumer_get_dimensions( &this->window_width, &this->window_height );
432                 SDL_Surface *screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
433
434                 if ( screen )
435                 {
436                         uint32_t color = mlt_properties_get_int( this->properties, "window_background" );
437                         SDL_FillRect( screen, NULL, color >> 8 );
438                         changed = 1;
439                 }
440                 pthread_mutex_unlock( &mlt_sdl_mutex );
441         }
442         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "test_audio", 1 );
443         if ( changed == 0 &&
444                  this->last_position == mlt_frame_get_position( frame ) &&
445                  this->last_producer == mlt_frame_get_original_producer( frame ) &&
446                  !mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "refresh" ) )
447         {
448                 sdl_unlock_display( );
449                 mlt_cocoa_autorelease_close( pool );
450                 if ( unlock != NULL ) unlock( );
451                 struct timespec tm = { 0, 100000 };
452                 nanosleep( &tm, NULL );
453                 return 0;
454         }
455
456         // Update last frame shown info
457         this->last_position = mlt_frame_get_position( frame );
458         this->last_producer = mlt_frame_get_original_producer( frame );
459
460         // Get the image, width and height
461         mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
462
463         if ( image != NULL )
464         {
465                 char *rescale = mlt_properties_get( properties, "rescale" );
466                 if ( rescale != NULL && strcmp( rescale, "none" ) )
467                 {
468                         double this_aspect = display_ratio / ( ( double )this->window_width / ( double )this->window_height );
469                         this->rect.w = this_aspect * this->window_width;
470                         this->rect.h = this->window_height;
471                         if ( this->rect.w > this->window_width )
472                         {
473                                 this->rect.w = this->window_width;
474                                 this->rect.h = ( 1.0 / this_aspect ) * this->window_height;
475                         }
476                 }
477                 else
478                 {
479                         double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
480                         this->rect.w = frame_aspect * this->window_height;
481                         this->rect.h = this->window_height;
482                         if ( this->rect.w > this->window_width )
483                         {
484                                 this->rect.w = this->window_width;
485                                 this->rect.h = ( 1.0 / frame_aspect ) * this->window_width;
486                         }
487                 }
488
489                 this->rect.x = ( this->window_width - this->rect.w ) / 2;
490                 this->rect.y = ( this->window_height - this->rect.h ) / 2;
491
492                 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
493                 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
494                 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
495                 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
496         }
497         
498         pthread_mutex_lock( &mlt_sdl_mutex );
499         SDL_Surface *screen = SDL_GetVideoSurface( );
500         if ( !mlt_consumer_is_stopped( &this->parent ) && screen && screen->pixels )
501         {
502                 switch( screen->format->BytesPerPixel )
503                 {
504                         case 1:
505                                 display_1( screen, this->rect, image, width, height );
506                                 break;
507                         case 2:
508                                 display_2( screen, this->rect, image, width, height );
509                                 break;
510                         case 3:
511                                 display_3( screen, this->rect, image, width, height );
512                                 break;
513                         case 4:
514                                 display_4( screen, this->rect, image, width, height );
515                                 break;
516                         default:
517                                 fprintf( stderr, "Unsupported video depth %d\n", screen->format->BytesPerPixel );
518                                 break;
519                 }
520
521                 // Flip it into sight
522                 SDL_Flip( screen );
523         }
524         pthread_mutex_unlock( &mlt_sdl_mutex );
525
526         sdl_unlock_display();
527         mlt_cocoa_autorelease_close( pool );
528         if ( unlock != NULL ) unlock( );
529         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
530
531         return 1;
532 }
533
534 /** Threaded wrapper for pipe.
535 */
536
537 static void *consumer_thread( void *arg )
538 {
539         // Identify the arg
540         consumer_sdl this = arg;
541
542         // Get the consumer
543         mlt_consumer consumer = &this->parent;
544         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
545         mlt_frame frame = NULL;
546
547         // Allow the hosting app to provide the preview
548         int preview_off = mlt_properties_get_int( properties, "preview_off" );
549
550         // Loop until told not to
551         while( this->running )
552         {
553                 // Get a frame from the attached producer
554                 frame = mlt_consumer_rt_frame( consumer );
555
556                 // Ensure that we have a frame
557                 if ( this->running && frame != NULL )
558                 {
559                         if ( preview_off == 0 )
560                         {
561                                 consumer_play_video( this, frame );
562                         }
563                         else
564                         {
565                                 mlt_image_format vfmt = mlt_image_rgb24a;
566                                 int height = this->height;
567                                 int width = this->width;
568                                 uint8_t *image = NULL;
569                                 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
570
571                                 // Check if a specific colour space has been requested
572                                 if ( preview_off && preview_format != mlt_image_none )
573                                         vfmt = preview_format;
574                         
575                                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
576                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "format", vfmt );
577                                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
578                         }
579                         mlt_frame_close( frame );
580                 }
581                 else
582                 {
583                         if ( frame ) mlt_frame_close( frame );
584                         this->running = 0;
585                 }
586         }
587
588         return NULL;
589 }
590
591 static int consumer_get_dimensions( int *width, int *height )
592 {
593         int changed = 0;
594
595         // SDL windows manager structure
596         SDL_SysWMinfo wm;
597
598         // Specify the SDL Version
599         SDL_VERSION( &wm.version );
600
601 #ifndef __DARWIN__
602         // Get the wm structure
603         if ( SDL_GetWMInfo( &wm ) == 1 )
604         {
605 #ifndef WIN32
606                 // Check that we have the X11 wm
607                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
608                 {
609                         // Get the SDL window
610                         Window window = wm.info.x11.window;
611
612                         // Get the display session
613                         Display *display = wm.info.x11.display;
614
615                         // Get the window attributes
616                         XWindowAttributes attr;
617                         XGetWindowAttributes( display, window, &attr );
618
619                         // Determine whether window has changed
620                         changed = *width != attr.width || *height != attr.height;
621
622                         // Return width and height
623                         *width = attr.width;
624                         *height = attr.height;
625                 }
626 #endif
627         }
628 #endif
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 }