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