default progressive on
[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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <pthread.h>
27 #include <SDL/SDL.h>
28 #include <SDL/SDL_syswm.h>
29
30 /** This classes definition.
31 */
32
33 typedef struct consumer_sdl_s *consumer_sdl;
34
35 struct consumer_sdl_s
36 {
37 struct mlt_consumer_s parent;
38 mlt_properties properties;
39 int format;
40 int video;
41 pthread_t thread;
42 int running;
43 uint8_t audio_buffer[ 4096 * 3 ];
44 int audio_avail;
45 pthread_mutex_t audio_mutex;
46 pthread_cond_t audio_cond;
47 int window_width;
48 int window_height;
49 float aspect_ratio;
50 int width;
51 int height;
52 int playing;
53 mlt_frame *queue;
54 int size;
55 int count;
56 int sdl_flags;
57 SDL_Surface *sdl_screen;
58 SDL_Overlay *sdl_overlay;
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 // Get the parent consumer object
85 mlt_consumer parent = &this->parent;
86
87 // We have stuff to clean up, so override the close method
88 parent->close = consumer_close;
89
90 // get a handle on properties
91 mlt_service service = mlt_consumer_service( parent );
92 this->properties = mlt_service_properties( service );
93
94 // Set the default volume
95 mlt_properties_set_double( this->properties, "volume", 1.0 );
96
97 // This is the initialisation of the consumer
98 pthread_mutex_init( &this->audio_mutex, NULL );
99 pthread_cond_init( &this->audio_cond, NULL);
100
101 // Default scaler (for now we'll use nearest)
102 mlt_properties_set( this->properties, "rescale", "nearest" );
103
104 // Default progressive true
105 mlt_properties_set_int( this->properties, "progressive", 1 );
106
107 // Get aspect ratio
108 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
109
110 // process actual param
111 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
112 {
113 this->width = mlt_properties_get_int( this->properties, "width" );
114 this->height = mlt_properties_get_int( this->properties, "height" );
115 }
116
117 // Default window size
118 this->window_width = (int)( (float)this->height * this->aspect_ratio ) + 1;
119 this->window_height = this->height;
120
121 // Set the sdl flags
122 this->sdl_flags = SDL_HWSURFACE | SDL_DOUBLEBUF | SDL_HWACCEL | SDL_RESIZABLE;
123
124 // Allow thread to be started/stopped
125 parent->start = consumer_start;
126 parent->stop = consumer_stop;
127 parent->is_stopped = consumer_is_stopped;
128
129 // Return the consumer produced
130 return parent;
131 }
132
133 // malloc or consumer init failed
134 free( this );
135
136 // Indicate failure
137 return NULL;
138 }
139
140 int consumer_start( mlt_consumer parent )
141 {
142 consumer_sdl this = parent->child;
143
144 if ( !this->running )
145 {
146 this->running = 1;
147 pthread_create( &this->thread, NULL, consumer_thread, this );
148 }
149
150 return 0;
151 }
152
153 int consumer_stop( mlt_consumer parent )
154 {
155 // Get the actual object
156 consumer_sdl this = parent->child;
157
158 if ( this->running )
159 {
160 // Kill the thread and clean up
161 this->running = 0;
162
163 pthread_mutex_lock( &this->audio_mutex );
164 pthread_cond_broadcast( &this->audio_cond );
165 pthread_mutex_unlock( &this->audio_mutex );
166
167 pthread_join( this->thread, NULL );
168 }
169
170 return 0;
171 }
172
173 int consumer_is_stopped( mlt_consumer parent )
174 {
175 consumer_sdl this = parent->child;
176 return !this->running;
177 }
178
179 static int sdl_lock_display( )
180 {
181 SDL_Surface *screen = SDL_GetVideoSurface( );
182 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
183 }
184
185 static void sdl_unlock_display( )
186 {
187 SDL_Surface *screen = SDL_GetVideoSurface( );
188 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
189 SDL_UnlockSurface( screen );
190 }
191
192 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
193 {
194 consumer_sdl this = udata;
195
196 // Get the volume
197 float volume = mlt_properties_get_double( this->properties, "volume" );
198
199 pthread_mutex_lock( &this->audio_mutex );
200
201 // Block until audio received
202 while ( this->running && len > this->audio_avail )
203 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
204
205 if ( this->audio_avail >= len )
206 {
207 // Place in the audio buffer
208 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
209
210 // Remove len from the audio available
211 this->audio_avail -= len;
212
213 // Remove the samples
214 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
215 }
216 else
217 {
218 // Just to be safe, wipe the stream first
219 memset( stream, 0, len );
220
221 // Copy what we have into the stream
222 memcpy( stream, this->audio_buffer, this->audio_avail );
223
224 // Mix the audio
225 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
226
227 // No audio left
228 this->audio_avail = 0;
229 }
230
231 // We're definitely playing now
232 this->playing = 1;
233
234 pthread_cond_broadcast( &this->audio_cond );
235 pthread_mutex_unlock( &this->audio_mutex );
236 }
237
238 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio )
239 {
240 // Get the properties of this consumer
241 mlt_properties properties = this->properties;
242 mlt_audio_format afmt = mlt_audio_pcm;
243
244 // Set the preferred params of the test card signal
245 int channels = 2;
246 int frequency = 48000;
247 static int counter = 0;
248 if ( mlt_properties_get_int( properties, "frequency" ) != 0 )
249 frequency = mlt_properties_get_int( properties, "frequency" );
250
251 int samples = mlt_sample_calculator( mlt_properties_get_double( this->properties, "fps" ), frequency, counter++ );
252
253 int16_t *pcm;
254 int bytes;
255
256 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
257
258 if ( mlt_properties_get_int( properties, "audio_off" ) )
259 {
260 this->playing = 1;
261 return init_audio;
262 }
263
264 if ( init_audio == 1 )
265 {
266 SDL_AudioSpec request;
267 SDL_AudioSpec got;
268
269 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
270 SDL_EnableUNICODE( 1 );
271
272 // specify audio format
273 memset( &request, 0, sizeof( SDL_AudioSpec ) );
274 this->playing = 0;
275 request.freq = frequency;
276 request.format = AUDIO_S16;
277 request.channels = channels;
278 request.samples = 1024;
279 request.callback = sdl_fill_audio;
280 request.userdata = (void *)this;
281 if ( SDL_OpenAudio( &request, &got ) != 0 )
282 {
283 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
284 init_audio = 2;
285 }
286 else if ( got.size != 0 )
287 {
288 SDL_PauseAudio( 0 );
289 init_audio = 0;
290 }
291 }
292
293 if ( init_audio == 0 )
294 {
295 bytes = ( samples * channels * 2 );
296 pthread_mutex_lock( &this->audio_mutex );
297 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
298 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
299 mlt_properties properties = mlt_frame_properties( frame );
300 if ( mlt_properties_get_double( properties, "_speed" ) == 1 )
301 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
302 else
303 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
304 this->audio_avail += bytes;
305 pthread_cond_broadcast( &this->audio_cond );
306 pthread_mutex_unlock( &this->audio_mutex );
307 }
308 else
309 {
310 this->playing = 1;
311 }
312
313 return init_audio;
314 }
315
316 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
317 {
318 // Get the properties of this consumer
319 mlt_properties properties = this->properties;
320
321 mlt_image_format vfmt = mlt_image_yuv422;
322 int width = this->width, height = this->height;
323 uint8_t *image;
324 int changed = 0;
325
326 if ( mlt_properties_get_int( properties, "video_off" ) )
327 {
328 mlt_frame_close( frame );
329 return 0;
330 }
331
332 if ( this->count == this->size )
333 {
334 this->size += 25;
335 this->queue = realloc( this->queue, sizeof( mlt_frame ) * this->size );
336 }
337 this->queue[ this->count ++ ] = frame;
338
339 if ( this->playing )
340 {
341 // We're working on the oldest frame now
342 frame = this->queue[ 0 ];
343
344 // Shunt the frames in the queue down
345 int i = 0;
346 for ( i = 1; i < this->count; i ++ )
347 this->queue[ i - 1 ] = this->queue[ i ];
348 this->count --;
349
350 // Get the image, width and height
351 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
352
353 if ( this->sdl_screen != NULL )
354 {
355 SDL_Event event;
356
357 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
358
359 while ( SDL_PollEvent( &event ) )
360 {
361 switch( event.type )
362 {
363 case SDL_VIDEORESIZE:
364 this->window_width = event.resize.w;
365 this->window_height = event.resize.h;
366 changed = 1;
367 break;
368 case SDL_KEYDOWN:
369 {
370 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
371 char keyboard[ 2 ] = " ";
372 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
373 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
374 {
375 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
376 callback( producer, keyboard );
377 }
378 }
379 break;
380 }
381 }
382
383 }
384
385 if ( width != this->width || height != this->height )
386 {
387 this->width = width;
388 this->height = height;
389 changed = 1;
390 }
391
392 if ( this->sdl_screen == NULL || changed )
393 {
394 double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
395 float display_aspect_ratio = (float)width / (float)height;
396 SDL_Rect rect;
397
398 if ( mlt_properties_get_double( properties, "aspect_ratio" ) )
399 aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
400
401 if ( aspect_ratio == 1 )
402 {
403 rect.w = this->window_width;
404 rect.h = this->window_height;
405 }
406 else if ( this->window_width < this->window_height * aspect_ratio )
407 {
408 rect.w = this->window_width;
409 rect.h = this->window_width / aspect_ratio;
410 }
411 else
412 {
413 rect.w = this->window_height * aspect_ratio;
414 rect.h = this->window_height;
415 }
416
417 if ( mlt_properties_get_int( properties, "scale_overlay" ) )
418 {
419 if ( ( float )rect.w * display_aspect_ratio < this->window_width )
420 rect.w = ( int )( ( float )rect.w * display_aspect_ratio );
421 else if ( ( float )rect.h * display_aspect_ratio < this->window_height )
422 rect.h = ( int )( ( float )rect.h * display_aspect_ratio );
423 }
424
425 rect.x = ( this->window_width - rect.w ) / 2;
426 rect.y = ( this->window_height - rect.h ) / 2;
427
428 // Force an overlay recreation
429 if ( this->sdl_overlay != NULL )
430 SDL_FreeYUVOverlay( this->sdl_overlay );
431
432 // open SDL window with video overlay, if possible
433 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
434
435 if ( this->sdl_screen != NULL )
436 {
437 SDL_SetClipRect( this->sdl_screen, &rect );
438
439 sdl_lock_display();
440 this->sdl_overlay = SDL_CreateYUVOverlay( this->width - (this->width % 4), this->height- (this->height % 2 ), SDL_YUY2_OVERLAY, this->sdl_screen );
441 sdl_unlock_display();
442 }
443 }
444
445 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
446 {
447 this->buffer = this->sdl_overlay->pixels[ 0 ];
448 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
449 {
450 mlt_resize_yuv422( this->buffer, this->width - (this->width % 4 ), this->height- (this->height % 2 ), image, width, height );
451 SDL_UnlockYUVOverlay( this->sdl_overlay );
452 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
453 }
454 }
455 }
456 else
457 {
458 frame = NULL;
459 }
460
461 // Close the frame
462 if ( frame != NULL )
463 mlt_frame_close( frame );
464
465 if ( this->count )
466 {
467 // Tell the producers about our scale relative to the normalisation
468 mlt_properties_set_double( mlt_frame_properties( this->queue[ this->count - 1 ] ), "consumer_scale",
469 ( double )height / mlt_properties_get_double( properties, "height" ) );
470 mlt_frame_get_image( this->queue[ this->count - 1 ], &image, &vfmt, &width, &height, 0 );
471 }
472
473 return 0;
474 }
475
476 /** Threaded wrapper for pipe.
477 */
478
479 static void *consumer_thread( void *arg )
480 {
481 // Identify the arg
482 consumer_sdl this = arg;
483
484 // Get the consumer
485 mlt_consumer consumer = &this->parent;
486
487 // internal intialization
488 int init_audio = 1;
489
490 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
491 {
492 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
493 return NULL;
494 }
495
496 // Loop until told not to
497 while( this->running )
498 {
499 // Get a frame from the attached producer
500 mlt_frame frame = mlt_consumer_get_frame( consumer );
501
502 // Ensure that we have a frame
503 if ( frame != NULL )
504 {
505 // SDL adapts display aspect, but set this so pixel aspect can be normalised
506 // mlt_properties_set_double( mlt_frame_properties( frame ), "consumer_aspect_ratio",
507 // mlt_frame_get_aspect_ratio( frame ) );
508
509 init_audio = consumer_play_audio( this, frame, init_audio );
510 consumer_play_video( this, frame );
511 }
512 }
513
514 // internal cleanup
515 if ( init_audio == 0 )
516 SDL_AudioQuit( );
517 if ( this->sdl_overlay != NULL )
518 SDL_FreeYUVOverlay( this->sdl_overlay );
519 SDL_Quit( );
520
521 while( -- this->count >= 0 )
522 mlt_frame_close( this->queue[ this->count ] );
523
524 this->sdl_screen = NULL;
525 this->sdl_overlay = NULL;
526 this->audio_avail = 0;
527
528 return NULL;
529 }
530
531 static int consumer_get_dimensions( int *width, int *height )
532 {
533 int changed = 0;
534
535 // SDL windows manager structure
536 SDL_SysWMinfo wm;
537
538 // Specify the SDL Version
539 SDL_VERSION( &wm.version );
540
541 // Get the wm structure
542 if ( SDL_GetWMInfo( &wm ) == 1 )
543 {
544 // Check that we have the X11 wm
545 if ( wm.subsystem == SDL_SYSWM_X11 )
546 {
547 // Get the SDL window
548 Window window = wm.info.x11.window;
549
550 // Get the display session
551 Display *display = wm.info.x11.display;
552
553 // Get the window attributes
554 XWindowAttributes attr;
555 XGetWindowAttributes( display, window, &attr );
556
557 // Determine whether window has changed
558 changed = *width != attr.width || *height != attr.height;
559
560 // Return width and height
561 *width = attr.width;
562 *height = attr.height;
563 }
564 }
565
566 return changed;
567 }
568
569 /** Callback to allow override of the close method.
570 */
571
572 static void consumer_close( mlt_consumer parent )
573 {
574 // Get the actual object
575 consumer_sdl this = parent->child;
576
577 // Stop the consumer
578 mlt_consumer_stop( parent );
579
580 // Destroy mutexes
581 pthread_mutex_destroy( &this->audio_mutex );
582 pthread_cond_destroy( &this->audio_cond );
583
584 // Now clean up the rest
585 mlt_consumer_close( parent );
586
587 // Finally clean up this
588 free( this );
589 }
590