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