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