]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_still.c
A little debugging.
[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                 SDL_Surface *screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
432                 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
433                         screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
434
435                 if ( screen )
436                 {
437                         uint32_t color = mlt_properties_get_int( this->properties, "window_background" );
438                         SDL_FillRect( screen, NULL, color >> 8 );
439                         changed = 1;
440                 }
441                 pthread_mutex_unlock( &mlt_sdl_mutex );
442         }
443         mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "test_audio", 1 );
444         if ( changed == 0 &&
445                  this->last_position == mlt_frame_get_position( frame ) &&
446                  this->last_producer == mlt_frame_get_original_producer( frame ) &&
447                  !mlt_properties_get_int( MLT_FRAME_PROPERTIES( frame ), "refresh" ) )
448         {
449                 sdl_unlock_display( );
450                 mlt_cocoa_autorelease_close( pool );
451                 if ( unlock != NULL ) unlock( );
452                 struct timespec tm = { 0, 100000 };
453                 nanosleep( &tm, NULL );
454                 return 0;
455         }
456
457         // Update last frame shown info
458         this->last_position = mlt_frame_get_position( frame );
459         this->last_producer = mlt_frame_get_original_producer( frame );
460
461         // Get the image, width and height
462         mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
463
464         if ( image != NULL )
465         {
466                 char *rescale = mlt_properties_get( properties, "rescale" );
467                 if ( rescale != NULL && strcmp( rescale, "none" ) )
468                 {
469                         double this_aspect = display_ratio / ( ( double )this->window_width / ( double )this->window_height );
470                         this->rect.w = this_aspect * this->window_width;
471                         this->rect.h = this->window_height;
472                         if ( this->rect.w > this->window_width )
473                         {
474                                 this->rect.w = this->window_width;
475                                 this->rect.h = ( 1.0 / this_aspect ) * this->window_height;
476                         }
477                 }
478                 else
479                 {
480                         double frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
481                         this->rect.w = frame_aspect * this->window_height;
482                         this->rect.h = this->window_height;
483                         if ( this->rect.w > this->window_width )
484                         {
485                                 this->rect.w = this->window_width;
486                                 this->rect.h = ( 1.0 / frame_aspect ) * this->window_width;
487                         }
488                 }
489
490                 this->rect.x = ( this->window_width - this->rect.w ) / 2;
491                 this->rect.y = ( this->window_height - this->rect.h ) / 2;
492
493                 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
494                 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
495                 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
496                 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
497         }
498         
499         pthread_mutex_lock( &mlt_sdl_mutex );
500         SDL_Surface *screen = SDL_GetVideoSurface( );
501         if ( !mlt_consumer_is_stopped( &this->parent ) && screen && screen->pixels )
502         {
503                 switch( screen->format->BytesPerPixel )
504                 {
505                         case 1:
506                                 display_1( screen, this->rect, image, width, height );
507                                 break;
508                         case 2:
509                                 display_2( screen, this->rect, image, width, height );
510                                 break;
511                         case 3:
512                                 display_3( screen, this->rect, image, width, height );
513                                 break;
514                         case 4:
515                                 display_4( screen, this->rect, image, width, height );
516                                 break;
517                         default:
518                                 fprintf( stderr, "Unsupported video depth %d\n", screen->format->BytesPerPixel );
519                                 break;
520                 }
521
522                 // Flip it into sight
523                 SDL_Flip( screen );
524         }
525         pthread_mutex_unlock( &mlt_sdl_mutex );
526
527         sdl_unlock_display();
528         mlt_cocoa_autorelease_close( pool );
529         if ( unlock != NULL ) unlock( );
530         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
531
532         return 1;
533 }
534
535 /** Threaded wrapper for pipe.
536 */
537
538 static void *consumer_thread( void *arg )
539 {
540         // Identify the arg
541         consumer_sdl this = arg;
542
543         // Get the consumer
544         mlt_consumer consumer = &this->parent;
545         mlt_properties properties = MLT_CONSUMER_PROPERTIES( consumer );
546         mlt_frame frame = NULL;
547
548         // Allow the hosting app to provide the preview
549         int preview_off = mlt_properties_get_int( properties, "preview_off" );
550
551         // Loop until told not to
552         while( this->running )
553         {
554                 // Get a frame from the attached producer
555                 frame = mlt_consumer_rt_frame( consumer );
556
557                 // Ensure that we have a frame
558                 if ( this->running && frame != NULL )
559                 {
560                         if ( preview_off == 0 )
561                         {
562                                 consumer_play_video( this, frame );
563                         }
564                         else
565                         {
566                                 mlt_image_format vfmt = mlt_image_rgb24a;
567                                 int height = this->height;
568                                 int width = this->width;
569                                 uint8_t *image = NULL;
570                                 mlt_image_format preview_format = mlt_properties_get_int( properties, "preview_format" );
571
572                                 // Check if a specific colour space has been requested
573                                 if ( preview_off && preview_format != mlt_image_none )
574                                         vfmt = preview_format;
575                         
576                                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
577                                 mlt_properties_set_int( MLT_FRAME_PROPERTIES( frame ), "format", vfmt );
578                                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
579                         }
580                         mlt_frame_close( frame );
581                 }
582                 else
583                 {
584                         if ( frame ) mlt_frame_close( frame );
585                         this->running = 0;
586                 }
587         }
588
589         return NULL;
590 }
591
592 static int consumer_get_dimensions( int *width, int *height )
593 {
594         int changed = 0;
595
596         // SDL windows manager structure
597         SDL_SysWMinfo wm;
598
599         // Specify the SDL Version
600         SDL_VERSION( &wm.version );
601
602 #ifndef __DARWIN__
603         // Get the wm structure
604         if ( SDL_GetWMInfo( &wm ) == 1 )
605         {
606 #ifndef WIN32
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 #endif
630
631         return changed;
632 }
633
634 /** Callback to allow override of the close method.
635 */
636
637 static void consumer_close( mlt_consumer parent )
638 {
639         // Get the actual object
640         consumer_sdl this = parent->child;
641
642         // Stop the consumer
643         mlt_consumer_stop( parent );
644
645         // Now clean up the rest
646         mlt_consumer_close( parent );
647
648         // Finally clean up this
649         free( this );
650 }