]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl.c
Clean up and border preservation
[mlt] / src / modules / sdl / consumer_sdl.c
1 /*
2  * consumer_sdl.c -- A Simple DirectMedia Layer consumer
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
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 <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <pthread.h>
28 #include <SDL/SDL.h>
29 #include <SDL/SDL_syswm.h>
30 #include <sys/time.h>
31
32 /** This classes definition.
33 */
34
35 typedef struct consumer_sdl_s *consumer_sdl;
36
37 struct consumer_sdl_s
38 {
39         struct mlt_consumer_s parent;
40         mlt_properties properties;
41         mlt_deque queue;
42         pthread_t thread;
43         int running;
44         uint8_t audio_buffer[ 4096 * 19 ];
45         int audio_avail;
46         pthread_mutex_t audio_mutex;
47         pthread_cond_t audio_cond;
48         int window_width;
49         int window_height;
50         float aspect_ratio;
51         float display_aspect;
52         int width;
53         int height;
54         int playing;
55         int sdl_flags;
56         SDL_Surface *sdl_screen;
57         SDL_Overlay *sdl_overlay;
58         SDL_Rect rect;
59         uint8_t *buffer;
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
72 /** This is what will be called by the factory - anything can be passed in
73         via the argument, but keep it simple.
74 */
75
76 mlt_consumer consumer_sdl_init( char *arg )
77 {
78         // Create the consumer object
79         consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
80
81         // If no malloc'd and consumer init ok
82         if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
83         {
84                 // Create the queue
85                 this->queue = mlt_deque_init( );
86
87                 // Get the parent consumer object
88                 mlt_consumer parent = &this->parent;
89
90                 // We have stuff to clean up, so override the close method
91                 parent->close = consumer_close;
92
93                 // get a handle on properties
94                 mlt_service service = mlt_consumer_service( parent );
95                 this->properties = mlt_service_properties( service );
96
97                 // Set the default volume
98                 mlt_properties_set_double( this->properties, "volume", 1.0 );
99
100                 // This is the initialisation of the consumer
101                 pthread_mutex_init( &this->audio_mutex, NULL );
102                 pthread_cond_init( &this->audio_cond, NULL);
103                 
104                 // Default scaler (for now we'll use nearest)
105                 mlt_properties_set( this->properties, "rescale", "nearest" );
106
107                 // Default buffer for low latency
108                 mlt_properties_set_int( this->properties, "buffer", 1 );
109
110                 // Default progressive true
111                 mlt_properties_set_int( this->properties, "progressive", 0 );
112
113                 // Default audio buffer
114                 mlt_properties_set_int( this->properties, "audio_buffer", 1024 );
115
116                 // Get sample aspect ratio
117                 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
118                 
119                 // Default display aspect ratio
120                 this->display_aspect = 4.0 / 3.0;
121                 
122                 // process actual param
123                 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
124                 {
125                         this->width = mlt_properties_get_int( this->properties, "width" );
126                         this->height = mlt_properties_get_int( this->properties, "height" );
127                         
128                         // Default window size
129                         this->window_width = ( float )this->height * this->display_aspect + 0.5;
130                         this->window_height = this->height;
131                 }
132                 else
133                 {
134                         if ( (int)( ( float )this->width / this->height * 1000 ) != 
135                                  (int)( this->display_aspect * 1000 ) ) 
136                         {
137                                 // Override these
138                                 this->display_aspect = ( float )this->width / this->height;
139                                 this->aspect_ratio = 1.0;
140                                 mlt_properties_set_double( this->properties, "aspect_ratio", this->aspect_ratio );
141                         }
142                         
143                         // Set window size
144                         this->window_width = this->width;
145                         this->window_height = this->height;
146                 }
147
148                 // Set the sdl flags
149                 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
150
151                 // Allow thread to be started/stopped
152                 parent->start = consumer_start;
153                 parent->stop = consumer_stop;
154                 parent->is_stopped = consumer_is_stopped;
155
156                 // Return the consumer produced
157                 return parent;
158         }
159
160         // malloc or consumer init failed
161         free( this );
162
163         // Indicate failure
164         return NULL;
165 }
166
167 int consumer_start( mlt_consumer parent )
168 {
169         consumer_sdl this = parent->child;
170
171         if ( !this->running )
172         {
173                 pthread_attr_t thread_attributes;
174                 
175                 this->running = 1;
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                 // Inherit the scheduling priority
185                 pthread_attr_init( &thread_attributes );
186                 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
187         
188                 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
189         }
190
191         return 0;
192 }
193
194 int consumer_stop( mlt_consumer parent )
195 {
196         // Get the actual object
197         consumer_sdl this = parent->child;
198
199         if ( this->running )
200         {
201                 // Kill the thread and clean up
202                 this->running = 0;
203
204                 pthread_mutex_lock( &this->audio_mutex );
205                 pthread_cond_broadcast( &this->audio_cond );
206                 pthread_mutex_unlock( &this->audio_mutex );
207
208                 pthread_join( this->thread, NULL );
209         }
210
211         return 0;
212 }
213
214 int consumer_is_stopped( mlt_consumer parent )
215 {
216         consumer_sdl this = parent->child;
217         return !this->running;
218 }
219
220 static int sdl_lock_display( )
221 {
222         SDL_Surface *screen = SDL_GetVideoSurface( );
223         return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
224 }
225
226 static void sdl_unlock_display( )
227 {
228         SDL_Surface *screen = SDL_GetVideoSurface( );
229         if ( screen != NULL && SDL_MUSTLOCK( screen ) )
230                 SDL_UnlockSurface( screen );
231 }
232
233 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
234 {
235         consumer_sdl this = udata;
236
237         // Get the volume
238         float volume = mlt_properties_get_double( this->properties, "volume" );
239
240         pthread_mutex_lock( &this->audio_mutex );
241
242         // Block until audio received
243         while ( this->running && len > this->audio_avail )
244                 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
245
246         if ( this->audio_avail >= len )
247         {
248                 // Place in the audio buffer
249                 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
250
251                 // Remove len from the audio available
252                 this->audio_avail -= len;
253
254                 // Remove the samples
255                 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
256         }
257         else
258         {
259                 // Just to be safe, wipe the stream first
260                 memset( stream, 0, len );
261
262                 // Copy what we have into the stream
263                 memcpy( stream, this->audio_buffer, this->audio_avail );
264
265                 // Mix the audio 
266                 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
267
268                 // No audio left
269                 this->audio_avail = 0;
270         }
271
272         // We're definitely playing now
273         this->playing = 1;
274
275         pthread_cond_broadcast( &this->audio_cond );
276         pthread_mutex_unlock( &this->audio_mutex );
277 }
278
279 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio, int *duration )
280 {
281         // Get the properties of this consumer
282         mlt_properties properties = this->properties;
283         mlt_audio_format afmt = mlt_audio_pcm;
284
285         // Set the preferred params of the test card signal
286         int channels = mlt_properties_get_int( properties, "channels" );
287         int frequency = mlt_properties_get_int( properties, "frequency" );
288         static int counter = 0;
289
290         int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
291         
292         int16_t *pcm;
293         int bytes;
294
295         mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
296         *duration = ( ( samples * 1000 ) / frequency );
297
298         if ( mlt_properties_get_int( properties, "audio_off" ) )
299         {
300                 this->playing = 1;
301                 init_audio = 1;
302                 return init_audio;
303         }
304
305         if ( init_audio == 1 )
306         {
307                 SDL_AudioSpec request;
308                 SDL_AudioSpec got;
309
310                 int audio_buffer = mlt_properties_get_int( properties, "audio_buffer" );
311
312                 // specify audio format
313                 memset( &request, 0, sizeof( SDL_AudioSpec ) );
314                 this->playing = 0;
315                 request.freq = frequency;
316                 request.format = AUDIO_S16;
317                 request.channels = channels;
318                 request.samples = audio_buffer;
319                 request.callback = sdl_fill_audio;
320                 request.userdata = (void *)this;
321                 if ( SDL_OpenAudio( &request, &got ) != 0 )
322                 {
323                         fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
324                         init_audio = 2;
325                 }
326                 else if ( got.size != 0 )
327                 {
328                         SDL_PauseAudio( 0 );
329                         init_audio = 0;
330                 }
331         }
332
333         if ( init_audio == 0 )
334         {
335                 mlt_properties properties = mlt_frame_properties( frame );
336                 bytes = ( samples * channels * 2 );
337                 pthread_mutex_lock( &this->audio_mutex );
338                 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
339                         pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
340                 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
341                         memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
342                 else
343                         memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
344                 this->audio_avail += bytes;
345                 pthread_cond_broadcast( &this->audio_cond );
346                 pthread_mutex_unlock( &this->audio_mutex );
347         }
348         else
349         {
350                 this->playing = 1;
351         }
352
353         return init_audio;
354 }
355
356 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
357 {
358         // Get the properties of this consumer
359         mlt_properties properties = this->properties;
360
361         mlt_image_format vfmt = mlt_image_yuv422;
362         int width = this->width, height = this->height;
363         uint8_t *image;
364         int changed = 0;
365
366         if ( mlt_properties_get_int( properties, "video_off" ) == 0 )
367         {
368                 // Get the image, width and height
369                 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
370                 
371                 // Handle events
372                 if ( this->sdl_screen != NULL )
373                 {
374                         SDL_Event event;
375         
376                         changed = consumer_get_dimensions( &this->window_width, &this->window_height );
377         
378                         while ( SDL_PollEvent( &event ) )
379                         {
380                                 switch( event.type )
381                                 {
382                                         case SDL_VIDEORESIZE:
383                                                 this->window_width = event.resize.w;
384                                                 this->window_height = event.resize.h;
385                                                 changed = 1;
386                                                 break;
387                                         case SDL_QUIT:
388                                                 this->running = 0;
389                                                 break;
390                                         case SDL_KEYDOWN:
391                                                 {
392                                                         mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
393                                                         char keyboard[ 2 ] = " ";
394                                                         void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
395                                                         if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
396                                                         {
397                                                                 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
398                                                                 callback( producer, keyboard );
399                                                         }
400                                                 }
401                                                 break;
402                                 }
403                         }
404                 }
405         
406                 if ( width != this->width || height != this->height )
407                 {
408                         this->width = width;
409                         this->height = height;
410                         changed = 1;
411                 }
412
413                 if ( this->sdl_screen == NULL || changed )
414                 {
415                         // Determine frame's display aspect ratio
416                         float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * this->width / this->height;
417                         
418                         // Determine window's new display aspect ratio
419                         float this_aspect = ( float )this->window_width / this->window_height;
420
421                         // If using hardware scaler
422                         if ( mlt_properties_get( properties, "rescale" ) != NULL &&
423                                 !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
424                         {
425                                 // Special case optimisation to negate odd effect of sample aspect ratio
426                                 // not corresponding exactly with image resolution.
427                                 if ( ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) && 
428                                          ( (int)( mlt_frame_get_aspect_ratio( frame ) * 1000 ) == (int)( this->aspect_ratio * 1000 ) ) )
429                                 {
430                                         this->rect.w = this->window_width;
431                                         this->rect.h = this->window_height;
432                                 }
433                                 else
434                                 {
435                                         // Use hardware scaler to normalise display aspect ratio
436                                         this->rect.w = frame_aspect / this_aspect * this->window_width + 0.5;
437                                         this->rect.h = this->window_height;
438                                         if ( this->rect.w > this->window_width )
439                                         {
440                                                 this->rect.w = this->window_width;
441                                                 this->rect.h = this_aspect / frame_aspect * this->window_height + 0.5;
442                                         }
443                                 }
444                         }
445                         // Special case optimisation to negate odd effect of sample aspect ratio
446                         // not corresponding exactly with image resolution.
447                         else if ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) 
448                         {
449                                 this->rect.w = this->window_width;
450                                 this->rect.h = this->window_height;
451                         }
452                         // Use hardware scaler to normalise sample aspect ratio
453                         else if ( this->window_height * frame_aspect > this->window_width )
454                         {
455                                 this->rect.w = this->window_width;
456                                 this->rect.h = this->window_width / frame_aspect + 0.5;
457                         }
458                         else
459                         {
460                                 this->rect.w = this->window_height * frame_aspect + 0.5;
461                                 this->rect.h = this->window_height;
462                         }
463                         
464                         this->rect.x = ( this->window_width - this->rect.w ) / 2;
465                         this->rect.y = ( this->window_height - this->rect.h ) / 2;
466                         
467                         // Force an overlay recreation
468                         if ( this->sdl_overlay != NULL )
469                                 SDL_FreeYUVOverlay( this->sdl_overlay );
470
471                         // open SDL window with video overlay, if possible
472                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
473
474                         if ( this->sdl_screen != NULL )
475                         {
476                                 SDL_SetClipRect( this->sdl_screen, &this->rect );
477                                 sdl_lock_display();
478                                 this->sdl_overlay = SDL_CreateYUVOverlay( this->width, this->height, SDL_YUY2_OVERLAY, this->sdl_screen );
479                                 sdl_unlock_display();
480                         }
481                 }
482                         
483                 if ( mlt_properties_get_int( properties, "changed" ) )
484                 {
485                         sdl_lock_display();
486                         this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
487                         SDL_SetClipRect( this->sdl_screen, &this->rect );
488                         SDL_Flip( this->sdl_screen );
489                         sdl_unlock_display();
490                         mlt_properties_set_int( properties, "changed", 0 );
491                 }
492
493                 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
494                 {
495                         this->buffer = this->sdl_overlay->pixels[ 0 ];
496                         if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
497                         {
498                                 if ( image != NULL )
499                                         memcpy( this->buffer, image, width * height * 2 );
500                                 SDL_UnlockYUVOverlay( this->sdl_overlay );
501                                 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
502                         }
503                 }
504
505         }
506
507         return 0;
508 }
509
510 /** Threaded wrapper for pipe.
511 */
512
513 static void *consumer_thread( void *arg )
514 {
515         // Identify the arg
516         consumer_sdl this = arg;
517
518         // Get the consumer
519         mlt_consumer consumer = &this->parent;
520
521         // internal intialization
522         int init_audio = 1;
523
524         // Obtain time of thread start
525         struct timeval now;
526         int64_t start = 0;
527         int64_t elapsed = 0;
528         int duration = 0;
529         int64_t playtime = 0;
530         struct timespec tm;
531         mlt_frame next = NULL;
532         mlt_frame frame = NULL;
533         mlt_properties properties = NULL;
534
535         if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
536         {
537                 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
538                 return NULL;
539         }
540
541         SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
542         SDL_EnableUNICODE( 1 );
543
544         // Loop until told not to
545         while( this->running )
546         {
547                 // Get a frame from the attached producer
548                 frame = mlt_consumer_rt_frame( consumer );
549
550                 // Ensure that we have a frame
551                 if ( frame != NULL )
552                 {
553                         // Get the frame properties
554                         properties =  mlt_frame_properties( frame );
555
556                         // Play audio
557                         init_audio = consumer_play_audio( this, frame, init_audio, &duration );
558
559                         // Determine the start time now
560                         if ( this->playing && start == 0 )
561                         {
562                                 // Get the current time
563                                 gettimeofday( &now, NULL );
564
565                                 // Determine start time
566                                 start = ( int64_t )now.tv_sec * 1000000 + now.tv_usec;
567                         }
568
569                         // Set playtime for this frame
570                         mlt_properties_set_position( properties, "playtime", playtime );
571
572                         // Push this frame to the back of the queue
573                         mlt_deque_push_back( this->queue, frame );
574
575                         // Calculate the next playtime
576                         playtime += ( duration * 1000 );
577                 }
578
579                 // Pop the next frame
580                 next = mlt_deque_pop_front( this->queue );
581
582                 while ( next != NULL && this->playing )
583                 {
584                         // Get the properties
585                         properties =  mlt_frame_properties( next );
586
587                         // Get the current time
588                         gettimeofday( &now, NULL );
589
590                         // Get the elapsed time
591                         elapsed = ( ( int64_t )now.tv_sec * 1000000 + now.tv_usec ) - start;
592
593                         // See if we have to delay the display of the current frame
594                         if ( mlt_properties_get_int( properties, "rendered" ) == 1 )
595                         {
596                                 // Obtain the scheduled playout time
597                                 mlt_position scheduled = mlt_properties_get_position( properties, "playtime" );
598
599                                 // Determine the difference between the elapsed time and the scheduled playout time
600                                 mlt_position difference = scheduled - elapsed;
601
602                                 // If the frame is quite some way in the future, go get another
603                                 if ( difference > 80000 && mlt_deque_count( this->queue ) < 6 )
604                                         break;
605
606                                 // Smooth playback a bit
607                                 if ( difference > 20000 && mlt_properties_get_double( properties, "_speed" ) == 1.0 )
608                                 {
609                                         tm.tv_sec = difference / 1000000;
610                                         tm.tv_nsec = ( difference % 1000000 ) * 1000;
611                                         nanosleep( &tm, NULL );
612                                 }
613
614                                 // Show current frame if not too old
615                                 if ( difference > -10000 || mlt_properties_get_double( properties, "_speed" ) != 1.0 )
616                                         consumer_play_video( this, next );
617                                 else
618                                         start = start - difference;
619                         }
620
621                         // This is an unrendered frame - just close it
622                         mlt_frame_close( next );
623
624                         // Pop the next frame
625                         next = mlt_deque_pop_front( this->queue );
626                 }
627
628                 if ( next != NULL )
629                         mlt_deque_push_front( this->queue, next );
630         }
631
632         // internal cleanup
633         if ( init_audio == 0 )
634                 SDL_AudioQuit( );
635         if ( this->sdl_overlay != NULL )
636                 SDL_FreeYUVOverlay( this->sdl_overlay );
637         SDL_Quit( );
638
639         while( mlt_deque_count( this->queue ) )
640                 mlt_frame_close( mlt_deque_pop_back( this->queue ) );
641
642         this->sdl_screen = NULL;
643         this->sdl_overlay = NULL;
644         this->audio_avail = 0;
645
646         return NULL;
647 }
648
649 static int consumer_get_dimensions( int *width, int *height )
650 {
651         int changed = 0;
652
653         // SDL windows manager structure
654         SDL_SysWMinfo wm;
655
656         // Specify the SDL Version
657         SDL_VERSION( &wm.version );
658
659         // Get the wm structure
660         if ( SDL_GetWMInfo( &wm ) == 1 )
661         {
662                 // Check that we have the X11 wm
663                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
664                 {
665                         // Get the SDL window
666                         Window window = wm.info.x11.window;
667
668                         // Get the display session
669                         Display *display = wm.info.x11.display;
670
671                         // Get the window attributes
672                         XWindowAttributes attr;
673                         XGetWindowAttributes( display, window, &attr );
674
675                         // Determine whether window has changed
676                         changed = *width != attr.width || *height != attr.height;
677
678                         // Return width and height
679                         *width = attr.width;
680                         *height = attr.height;
681                 }
682         }
683
684         return changed;
685 }
686
687 /** Callback to allow override of the close method.
688 */
689
690 static void consumer_close( mlt_consumer parent )
691 {
692         // Get the actual object
693         consumer_sdl this = parent->child;
694
695         // Stop the consumer
696         mlt_consumer_stop( parent );
697
698         // Close the queue
699         mlt_deque_close( this->queue );
700
701         // Destroy mutexes
702         pthread_mutex_destroy( &this->audio_mutex );
703         pthread_cond_destroy( &this->audio_cond );
704                 
705         // Now clean up the rest
706         mlt_consumer_close( parent );
707
708         // Finally clean up this
709         free( this );
710 }