]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_still.c
3ac12c0f65011f4e825fe2ecc2f3ce660827ce8a
[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 program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "consumer_sdl.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         float aspect_ratio;
49         float display_aspect;
50         double last_frame_aspect;
51         int width;
52         int height;
53         int playing;
54         int sdl_flags;
55         SDL_Surface *sdl_screen;
56         SDL_Rect rect;
57         uint8_t *buffer;
58         int last_position;
59         mlt_producer last_producer;
60         int filtered;
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( 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 ) == 0 )
85         {
86                 // Get the parent consumer object
87                 mlt_consumer parent = &this->parent;
88
89                 // We have stuff to clean up, so override the close method
90                 parent->close = consumer_close;
91
92                 // get a handle on properties
93                 mlt_service service = mlt_consumer_service( parent );
94                 this->properties = mlt_service_properties( service );
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                 // Default progressive true
103                 mlt_properties_set_int( this->properties, "progressive", 1 );
104
105                 // Get sample aspect ratio
106                 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
107
108                 // Ensure we don't join on a non-running object
109                 this->joined = 1;
110                 
111                 // Default display aspect ratio
112                 this->display_aspect = 4.0 / 3.0;
113                 
114                 // process actual param
115                 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
116                 {
117                         this->width = mlt_properties_get_int( this->properties, "width" );
118                         this->height = mlt_properties_get_int( this->properties, "height" );
119                 }
120
121                 // Default window size
122                 this->window_width = ( float )this->height * this->display_aspect;
123                 this->window_height = this->height;
124
125                 // Set the sdl flags
126                 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
127
128                 // Allow thread to be started/stopped
129                 parent->start = consumer_start;
130                 parent->stop = consumer_stop;
131                 parent->is_stopped = consumer_is_stopped;
132
133                 // Register specific events
134                 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
135
136                 // Return the consumer produced
137                 return parent;
138         }
139
140         // malloc or consumer init failed
141         free( this );
142
143         // Indicate failure
144         return NULL;
145 }
146
147 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
148 {
149         if ( listener != NULL )
150                 listener( owner, this, ( SDL_Event * )args[ 0 ] );
151 }
152
153 static int consumer_start( mlt_consumer parent )
154 {
155         consumer_sdl this = parent->child;
156
157         if ( !this->running )
158         {
159                 pthread_attr_t thread_attributes;
160
161                 // Attach a colour space converter
162                 if ( !this->filtered )
163                 {
164                         mlt_filter filter = mlt_factory_filter( "avcolour_space", NULL );
165                         mlt_properties_set_int( mlt_filter_properties( filter ), "forced", mlt_image_yuv422 );
166                         mlt_service_attach( mlt_consumer_service( &this->parent ), filter );
167                         mlt_filter_close( filter );
168                         this->filtered = 1;
169                 }
170         
171                 consumer_stop( parent );
172
173                 this->last_position = -1;
174                 this->running = 1;
175                 this->joined = 0;
176
177                 // Allow the user to force resizing to window size
178                 if ( mlt_properties_get_int( this->properties, "resize" ) )
179                 {
180                         mlt_properties_set_int( this->properties, "width", this->width );
181                         mlt_properties_set_int( this->properties, "height", this->height );
182                 }
183
184                 //this->width = this->height * this->display_aspect;
185
186                 // Inherit the scheduling priority
187                 pthread_attr_init( &thread_attributes );
188                 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
189
190                 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
191         }
192
193         return 0;
194 }
195
196 static int consumer_stop( mlt_consumer parent )
197 {
198         // Get the actual object
199         consumer_sdl this = parent->child;
200
201         if ( this->joined == 0 )
202         {
203                 // Kill the thread and clean up
204                 this->running = 0;
205
206                 pthread_join( this->thread, NULL );
207                 this->joined = 1;
208         }
209
210         return 0;
211 }
212
213 static int consumer_is_stopped( mlt_consumer parent )
214 {
215         consumer_sdl this = parent->child;
216         return !this->running;
217 }
218
219 static int sdl_lock_display( )
220 {
221         SDL_Surface *screen = SDL_GetVideoSurface( );
222         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
223 }
224
225 static void sdl_unlock_display( )
226 {
227         SDL_Surface *screen = SDL_GetVideoSurface( );
228         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
229                 SDL_UnlockSurface( screen );
230 }
231
232 static inline void display_1( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
233 {
234         // Generate the affine transform scaling values
235         int scale_width = ( width << 16 ) / rect.w;
236         int scale_height = ( height << 16 ) / rect.h;
237         int stride = width * 3;
238         int x, y, row_index;
239         uint8_t *q, *row;
240
241         // Constants defined for clarity and optimisation
242         int scanlength = screen->pitch;
243         uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
244         uint8_t *p;
245
246         // Iterate through the screen using a very basic scaling algorithm
247         for ( y = 0; y < rect.h; y ++ )
248         {
249                 p = start;
250                 row_index = ( scale_height * y ) >> 16;
251                 row = image + stride * row_index;
252                 for ( x = 0; x < rect.w; x ++ )
253                 {
254                         q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
255                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
256                 }
257                 start += scanlength;
258         }
259 }
260
261 static inline void display_2( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
262 {
263         // Generate the affine transform scaling values
264         int scale_width = ( width << 16 ) / rect.w;
265         int scale_height = ( height << 16 ) / rect.h;
266         int stride = width * 3;
267         int x, y, row_index;
268         uint8_t *q, *row;
269
270         // Constants defined for clarity and optimisation
271         int scanlength = screen->pitch / 2;
272         uint16_t *start = ( uint16_t * )screen->pixels + rect.y * scanlength + rect.x;
273         uint16_t *p;
274
275         // Iterate through the screen using a very basic scaling algorithm
276         for ( y = 0; y < rect.h; y ++ )
277         {
278                 p = start;
279                 row_index = ( scale_height * y ) >> 16;
280                 row = image + stride * row_index;
281                 for ( x = 0; x < rect.w; x ++ )
282                 {
283                         q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
284                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
285                 }
286                 start += scanlength;
287         }
288 }
289
290 static inline void display_3( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
291 {
292         // Generate the affine transform scaling values
293         int scale_width = ( width << 16 ) / rect.w;
294         int scale_height = ( height << 16 ) / rect.h;
295         int stride = width * 3;
296         int x, y, row_index;
297         uint8_t *q, *row;
298
299         // Constants defined for clarity and optimisation
300         int scanlength = screen->pitch;
301         uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
302         uint8_t *p;
303         uint32_t pixel;
304
305         // Iterate through the screen using a very basic scaling algorithm
306         for ( y = 0; y < rect.h; y ++ )
307         {
308                 p = start;
309                 row_index = ( scale_height * y ) >> 16;
310                 row = image + stride * row_index;
311                 for ( x = 0; x < rect.w; x ++ )
312                 {
313                         q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
314                         pixel = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
315                         *p ++ = (pixel & 0xFF0000) >> 16;
316                         *p ++ = (pixel & 0x00FF00) >> 8;
317                         *p ++ = (pixel & 0x0000FF);
318                 }
319                 start += scanlength;
320         }
321 }
322
323 static inline void display_4( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
324 {
325         // Generate the affine transform scaling values
326         int scale_width = ( width << 16 ) / rect.w;
327         int scale_height = ( height << 16 ) / rect.h;
328         int stride = width * 3;
329         int x, y, row_index;
330         uint8_t *q, *row;
331
332         // Constants defined for clarity and optimisation
333         int scanlength = screen->pitch / 4;
334         uint32_t *start = ( uint32_t * )screen->pixels + rect.y * scanlength + rect.x;
335         uint32_t *p;
336
337         // Iterate through the screen using a very basic scaling algorithm
338         for ( y = 0; y < rect.h; y ++ )
339         {
340                 p = start;
341                 row_index = ( scale_height * y ) >> 16;
342                 row = image + stride * row_index;
343                 for ( x = 0; x < rect.w; x ++ )
344                 {
345                         q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
346                         *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
347                 }
348                 start += scanlength;
349         }
350 }
351
352 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
353 {
354         // Get the properties of this consumer
355         mlt_properties properties = this->properties;
356
357         mlt_image_format vfmt = mlt_image_rgb24;
358         int height = this->height;
359         int width = this->width;
360         uint8_t *image = NULL;
361         int changed = 0;
362
363         void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
364         void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
365
366         if ( lock != NULL ) lock( );
367
368         sdl_lock_display();
369         
370         // Handle events
371         if ( this->sdl_screen != NULL )
372         {
373                 SDL_Event event;
374
375                 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
376
377                 while ( SDL_PollEvent( &event ) )
378                 {
379                         mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
380
381                         switch( event.type )
382                         {
383                                 case SDL_VIDEORESIZE:
384                                         this->window_width = event.resize.w;
385                                         this->window_height = event.resize.h;
386                                         changed = 1;
387                                         break;
388                                 case SDL_QUIT:
389                                         this->running = 0;
390                                         break;
391                                 case SDL_KEYDOWN:
392                                         {
393                                                 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
394                                                 char keyboard[ 2 ] = " ";
395                                                 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
396                                                 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
397                                                 {
398                                                         keyboard[ 0 ] = ( char )event.key.keysym.unicode;
399                                                         callback( producer, keyboard );
400                                                 }
401                                         }
402                                         break;
403                         }
404                 }
405         }
406
407         if ( this->sdl_screen == NULL || changed || mlt_properties_get_int( properties, "changed" ) == 2 )
408         {
409                 // open SDL window 
410                 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
411                 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
412                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
413
414                 changed = 1;
415                 mlt_properties_set_int( properties, "changed", 0 );
416         }
417         else
418         {
419                 changed = mlt_properties_get_int( properties, "changed" ) | mlt_properties_get_int( mlt_frame_properties( frame ), "refresh" );
420                 mlt_properties_set_int( properties, "changed", 0 );
421         }
422
423         
424         if ( changed == 0 &&
425                  this->last_position == mlt_frame_get_position( frame ) &&
426                  this->last_producer == mlt_properties_get_data( mlt_frame_properties( frame ), "_producer", NULL ) )
427         {
428                 sdl_unlock_display( );
429                 if ( unlock != NULL )
430                         unlock( );
431                 return 0;
432         }
433
434         // Update last frame shown info
435         this->last_position = mlt_frame_get_position( frame );
436         this->last_producer = mlt_properties_get_data( mlt_frame_properties( frame ), "_producer", NULL );
437
438         // Get the image, width and height
439         if ( image == NULL )
440         {
441                 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
442
443                 // I would like to provide upstream scaling here, but this is incorrect
444                 // Something? (or everything?) is too sensitive to aspect ratio
445                 //width = this->rect.w;
446                 //height = this->rect.h;
447                 //mlt_properties_set( mlt_frame_properties( frame ), "distort", "true" );
448                 //mlt_properties_set_int( mlt_frame_properties( frame ), "normalised_width", width );
449                 //mlt_properties_set_int( mlt_frame_properties( frame ), "normalised_height", height );
450
451                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
452         }
453
454         if ( image != NULL )
455         {
456                 char *rescale = mlt_properties_get( properties, "rescale" );
457                 if ( rescale != NULL && strcmp( rescale, "none" ) )
458                 {
459                         float this_aspect = this->display_aspect / ( ( float )this->window_width / ( float )this->window_height );
460                         this->rect.w = this_aspect * this->window_width;
461                         this->rect.h = this->window_height;
462                         if ( this->rect.w > this->window_width )
463                         {
464                                 this->rect.w = this->window_width;
465                                 this->rect.h = ( 1.0 / this_aspect ) * this->window_height;
466                         }
467                 }
468                 else
469                 {
470                         float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
471                         this->rect.w = frame_aspect * this->window_height;
472                         this->rect.h = this->window_height;
473                         if ( this->rect.w > this->window_width )
474                         {
475                                 this->rect.w = this->window_width;
476                                 this->rect.h = ( 1.0 / frame_aspect ) * this->window_width;
477                         }
478                 }
479
480                 this->rect.x = ( this->window_width - this->rect.w ) / 2;
481                 this->rect.y = ( this->window_height - this->rect.h ) / 2;
482
483                 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
484                 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
485                 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
486                 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
487         }
488         
489         if ( this->sdl_screen != NULL )
490         {
491                 memset( this->sdl_screen->pixels, 0, this->window_width * this->window_height * this->sdl_screen->format->BytesPerPixel );
492
493                 switch( this->sdl_screen->format->BytesPerPixel )
494                 {
495                         case 1:
496                                 display_1( this->sdl_screen, this->rect, image, width, height );
497                                 break;
498                         case 2:
499                                 display_2( this->sdl_screen, this->rect, image, width, height );
500                                 break;
501                         case 3:
502                                 display_3( this->sdl_screen, this->rect, image, width, height );
503                                 break;
504                         case 4:
505                                 display_4( this->sdl_screen, this->rect, image, width, height );
506                                 break;
507                         default:
508                                 fprintf( stderr, "Unsupported video depth %d\n", this->sdl_screen->format->BytesPerPixel );
509                                 break;
510                 }
511
512                 // Flip it into sight
513                 SDL_Flip( this->sdl_screen );
514         }
515
516         sdl_unlock_display();
517
518         if ( unlock != NULL ) unlock( );
519
520         return 1;
521 }
522
523 /** Threaded wrapper for pipe.
524 */
525
526 static void *consumer_thread( void *arg )
527 {
528         // Identify the arg
529         consumer_sdl this = arg;
530
531         // Get the consumer
532         mlt_consumer consumer = &this->parent;
533
534         // internal intialization
535         mlt_frame frame = NULL;
536         struct timespec tm = { 0, 10000000 };
537
538         if ( mlt_properties_get_int( mlt_consumer_properties( consumer ), "sdl_started" ) == 0 )
539         {
540                 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
541                 {
542                         fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
543                         return NULL;
544                 }
545
546                 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
547                 SDL_EnableUNICODE( 1 );
548         }
549         else
550         {
551                 mlt_properties_set_int( mlt_consumer_properties( consumer ), "changed", 2 );
552                 if ( SDL_GetVideoSurface( ) != NULL )
553                 {
554                         this->sdl_screen = SDL_GetVideoSurface( );
555                         consumer_get_dimensions( &this->window_width, &this->window_height );
556                         mlt_properties_set_int( mlt_consumer_properties( consumer ), "changed", 0 );
557                 }
558         }
559
560         // Loop until told not to
561         while( this->running )
562         {
563                 // Get a frame from the attached producer
564                 frame = mlt_consumer_rt_frame( consumer );
565
566                 // Ensure that we have a frame
567                 if ( frame != NULL )
568                 {
569                         if ( consumer_play_video( this, frame ) == 0 )
570                                 nanosleep( &tm, NULL );
571                         mlt_frame_close( frame );
572                 }
573         }
574
575         if ( mlt_properties_get_int( mlt_consumer_properties( consumer ), "sdl_started" ) == 0 )
576                 SDL_Quit( );
577
578         this->sdl_screen = NULL;
579
580         return NULL;
581 }
582
583 static int consumer_get_dimensions( int *width, int *height )
584 {
585         int changed = 0;
586
587         // SDL windows manager structure
588         SDL_SysWMinfo wm;
589
590         // Specify the SDL Version
591         SDL_VERSION( &wm.version );
592
593         // Get the wm structure
594         if ( SDL_GetWMInfo( &wm ) == 1 )
595         {
596                 // Check that we have the X11 wm
597                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
598                 {
599                         // Get the SDL window
600                         Window window = wm.info.x11.window;
601
602                         // Get the display session
603                         Display *display = wm.info.x11.display;
604
605                         // Get the window attributes
606                         XWindowAttributes attr;
607                         XGetWindowAttributes( display, window, &attr );
608
609                         // Determine whether window has changed
610                         changed = *width != attr.width || *height != attr.height;
611
612                         // Return width and height
613                         *width = attr.width;
614                         *height = attr.height;
615                 }
616         }
617
618         return changed;
619 }
620
621 /** Callback to allow override of the close method.
622 */
623
624 static void consumer_close( mlt_consumer parent )
625 {
626         // Get the actual object
627         consumer_sdl this = parent->child;
628
629         // Stop the consumer
630         mlt_consumer_stop( parent );
631
632         // Now clean up the rest
633         mlt_consumer_close( parent );
634
635         // Finally clean up this
636         free( this );
637 }