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