move audio sample calculator to mlt_frame and use from ffmpeg and mcmpeg, add mlt_fra...
[melted] / 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 <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 void consumer_close( mlt_consumer parent );
66 static void *consumer_thread( void * );
67 static int consumer_get_dimensions( int *width, int *height );
68
69 /** This is what will be called by the factory - anything can be passed in
70 via the argument, but keep it simple.
71 */
72
73 mlt_consumer consumer_sdl_init( char *arg )
74 {
75 // Create the consumer object
76 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
77
78 // If no malloc'd and consumer init ok
79 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
80 {
81 // Get the parent consumer object
82 mlt_consumer parent = &this->parent;
83
84 // We have stuff to clean up, so override the close method
85 parent->close = consumer_close;
86
87 // get a handle on properties
88 mlt_service service = mlt_consumer_service( parent );
89 this->properties = mlt_service_properties( service );
90
91 // Set the default volume
92 mlt_properties_set_double( this->properties, "volume", 1.0 );
93
94 // This is the initialisation of the consumer
95 this->running = 1;
96 pthread_mutex_init( &this->audio_mutex, NULL );
97 pthread_cond_init( &this->audio_cond, NULL);
98
99 // process actual param
100 if ( arg == NULL || !strcmp( arg, "PAL" ) )
101 {
102 this->width = 720;
103 this->height = 576;
104 }
105 else if ( !strcmp( arg, "NTSC" ) )
106 {
107 this->width = 720;
108 this->height = 480;
109 }
110 else if ( sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
111 {
112 this->width = 720;
113 this->height = 576;
114 }
115
116 // Default window size and aspect ratio
117 this->aspect_ratio = 4.0 / 3.0;
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 // Create the the thread
125 pthread_create( &this->thread, NULL, consumer_thread, this );
126
127 // Return the consumer produced
128 return parent;
129 }
130
131 // malloc or consumer init failed
132 free( this );
133
134 // Indicate failure
135 return NULL;
136 }
137
138 static int sdl_lock_display( )
139 {
140 SDL_Surface *screen = SDL_GetVideoSurface( );
141 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
142 }
143
144 static void sdl_unlock_display( )
145 {
146 SDL_Surface *screen = SDL_GetVideoSurface( );
147 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
148 SDL_UnlockSurface( screen );
149 }
150
151 static void sdl_fill_audio( void *udata, uint8_t *stream, int len )
152 {
153 consumer_sdl this = udata;
154
155 // Get the volume
156 float volume = mlt_properties_get_double( this->properties, "volume" );
157
158 pthread_mutex_lock( &this->audio_mutex );
159
160 // Block until audio received
161 while ( this->running && len > this->audio_avail )
162 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
163
164 if ( this->audio_avail >= len )
165 {
166 // Place in the audio buffer
167 SDL_MixAudio( stream, this->audio_buffer, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
168
169 // Remove len from the audio available
170 this->audio_avail -= len;
171
172 // Remove the samples
173 memmove( this->audio_buffer, this->audio_buffer + len, this->audio_avail );
174 }
175 else
176 {
177 // Just to be safe, wipe the stream first
178 memset( stream, 0, len );
179
180 // Copy what we have into the stream
181 memcpy( stream, this->audio_buffer, this->audio_avail );
182
183 // Mix the audio
184 SDL_MixAudio( stream, stream, len, ( int )( ( float )SDL_MIX_MAXVOLUME * volume ) );
185
186 // No audio left
187 this->audio_avail = 0;
188 }
189
190 // We're definitely playing now
191 this->playing = 1;
192
193 pthread_cond_broadcast( &this->audio_cond );
194 pthread_mutex_unlock( &this->audio_mutex );
195 }
196
197 static int consumer_play_audio( consumer_sdl this, mlt_frame frame, int init_audio )
198 {
199 // Get the properties of this consumer
200 mlt_properties properties = this->properties;
201 mlt_audio_format afmt = mlt_audio_pcm;
202 int channels = 0;
203 int samples;
204 int frequency;
205 int16_t *pcm;
206 int bytes;
207
208 mlt_frame_get_audio( frame, &pcm, &afmt, &frequency, &channels, &samples );
209
210 if ( mlt_properties_get_int( properties, "audio_off" ) )
211 {
212 this->playing = 1;
213 return init_audio;
214 }
215
216 if ( init_audio == 1 )
217 {
218 SDL_AudioSpec request;
219 SDL_AudioSpec got;
220
221 // specify audio format
222 memset( &request, 0, sizeof( SDL_AudioSpec ) );
223 this->playing = 0;
224 request.freq = frequency;
225 request.format = AUDIO_S16;
226 request.channels = channels;
227 request.samples = 1024;
228 request.callback = sdl_fill_audio;
229 request.userdata = (void *)this;
230 if ( SDL_OpenAudio( &request, &got ) != 0 )
231 {
232 fprintf( stderr, "SDL failed to open audio: %s\n", SDL_GetError() );
233 init_audio = 2;
234 }
235 else if ( got.size != 0 )
236 {
237 SDL_PauseAudio( 0 );
238 init_audio = 0;
239 }
240 }
241
242 if ( init_audio == 0 )
243 {
244 bytes = ( samples * channels * 2 );
245 pthread_mutex_lock( &this->audio_mutex );
246 while ( bytes > ( sizeof( this->audio_buffer) - this->audio_avail ) )
247 pthread_cond_wait( &this->audio_cond, &this->audio_mutex );
248 mlt_properties properties = mlt_frame_properties( frame );
249 if ( mlt_properties_get_double( properties, "speed" ) == 1 )
250 memcpy( &this->audio_buffer[ this->audio_avail ], pcm, bytes );
251 else
252 memset( &this->audio_buffer[ this->audio_avail ], 0, bytes );
253 this->audio_avail += bytes;
254 pthread_cond_broadcast( &this->audio_cond );
255 pthread_mutex_unlock( &this->audio_mutex );
256 }
257 else
258 {
259 this->playing = 1;
260 }
261
262 return init_audio;
263 }
264
265 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
266 {
267 // Get the properties of this consumer
268 mlt_properties properties = this->properties;
269
270 mlt_image_format vfmt = mlt_image_yuv422;
271 int width = this->width, height = this->height;
272 uint8_t *image;
273 int changed = 0;
274
275 if ( mlt_properties_get_int( properties, "video_off" ) )
276 {
277 mlt_frame_close( frame );
278 return 0;
279 }
280
281 if ( this->count == this->size )
282 {
283 this->size += 25;
284 this->queue = realloc( this->queue, sizeof( mlt_frame ) * this->size );
285 }
286 this->queue[ this->count ++ ] = frame;
287
288 if ( this->playing )
289 {
290 // We're working on the oldest frame now
291 frame = this->queue[ 0 ];
292
293 // Shunt the frames in the queue down
294 int i = 0;
295 for ( i = 1; i < this->count; i ++ )
296 this->queue[ i - 1 ] = this->queue[ i ];
297 this->count --;
298
299 // Get the image, width and height
300 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
301
302 if ( this->sdl_screen != NULL )
303 {
304 SDL_Event event;
305
306 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
307
308 while ( SDL_PollEvent( &event ) )
309 {
310 switch( event.type )
311 {
312 case SDL_VIDEORESIZE:
313 this->window_width = event.resize.w;
314 this->window_height = event.resize.h;
315 changed = 1;
316 break;
317 case SDL_KEYDOWN:
318 {
319 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
320 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
321 if ( callback != NULL && producer != NULL )
322 callback( producer, SDL_GetKeyName(event.key.keysym.sym) );
323 }
324 break;
325 }
326 }
327
328 }
329
330 if ( width != this->width || height != this->height )
331 {
332 this->width = width;
333 this->height = height;
334 changed = 1;
335 }
336
337 if ( this->sdl_screen == NULL || changed )
338 {
339 double aspect_ratio = mlt_frame_get_aspect_ratio( frame );
340 float display_aspect_ratio = (float)width / (float)height;
341 SDL_Rect rect;
342
343 if ( mlt_properties_get_double( properties, "aspect_ratio" ) )
344 aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
345
346 if ( aspect_ratio == 1 )
347 {
348 rect.w = this->window_width;
349 rect.h = this->window_height;
350 }
351 else if ( this->window_width < this->window_height * aspect_ratio )
352 {
353 rect.w = this->window_width;
354 rect.h = this->window_width / aspect_ratio;
355 }
356 else
357 {
358 rect.w = this->window_height * aspect_ratio;
359 rect.h = this->window_height;
360 }
361
362 if ( mlt_properties_get_int( properties, "scale_overlay" ) )
363 {
364 if ( ( float )rect.w * display_aspect_ratio < this->window_width )
365 rect.w = ( int )( ( float )rect.w * display_aspect_ratio );
366 else if ( ( float )rect.h * display_aspect_ratio < this->window_height )
367 rect.h = ( int )( ( float )rect.h * display_aspect_ratio );
368 }
369
370 rect.x = ( this->window_width - rect.w ) / 2;
371 rect.y = ( this->window_height - rect.h ) / 2;
372
373 // Force an overlay recreation
374 if ( this->sdl_overlay != NULL )
375 SDL_FreeYUVOverlay( this->sdl_overlay );
376
377 // open SDL window with video overlay, if possible
378 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
379
380 if ( this->sdl_screen != NULL )
381 {
382 SDL_SetClipRect( this->sdl_screen, &rect );
383
384 sdl_lock_display();
385 this->sdl_overlay = SDL_CreateYUVOverlay( this->width - (this->width % 4), this->height- (this->height % 2 ), SDL_YUY2_OVERLAY, this->sdl_screen );
386 sdl_unlock_display();
387 }
388 }
389
390 if ( this->sdl_screen != NULL && this->sdl_overlay != NULL )
391 {
392 this->buffer = this->sdl_overlay->pixels[ 0 ];
393 if ( SDL_LockYUVOverlay( this->sdl_overlay ) >= 0 )
394 {
395 mlt_resize_yuv422( this->buffer, this->width - (this->width % 4 ), this->height- (this->height % 2 ), image, width, height );
396 SDL_UnlockYUVOverlay( this->sdl_overlay );
397 SDL_DisplayYUVOverlay( this->sdl_overlay, &this->sdl_screen->clip_rect );
398 }
399 }
400 }
401 else
402 {
403 frame = NULL;
404 }
405
406 // Close the frame
407 if ( frame != NULL )
408 mlt_frame_close( frame );
409
410 if ( this->count )
411 mlt_frame_get_image( this->queue[ this->count - 1 ], &image, &vfmt, &width, &height, 0 );
412
413 return 0;
414 }
415
416 /** Threaded wrapper for pipe.
417 */
418
419 static void *consumer_thread( void *arg )
420 {
421 // Identify the arg
422 consumer_sdl this = arg;
423
424 // Get the consumer
425 mlt_consumer consumer = &this->parent;
426
427 // Get the service assoicated to the consumer
428 mlt_service service = mlt_consumer_service( consumer );
429
430 // Define a frame pointer
431 mlt_frame frame;
432
433 // internal intialization
434 int init_audio = 1;
435
436 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE ) < 0 )
437 {
438 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
439 return NULL;
440 }
441
442 // Loop until told not to
443 while( this->running )
444 {
445 // Get a frame from the service (should never return anything other than 0)
446 if ( mlt_service_get_frame( service, &frame, 0 ) == 0 )
447 {
448 init_audio = consumer_play_audio( this, frame, init_audio );
449 consumer_play_video( this, frame );
450 }
451 }
452
453 // internal cleanup
454 if ( init_audio == 0 )
455 SDL_AudioQuit( );
456 if ( this->sdl_overlay != NULL )
457 SDL_FreeYUVOverlay( this->sdl_overlay );
458 SDL_Quit( );
459
460 return NULL;
461 }
462
463 static int consumer_get_dimensions( int *width, int *height )
464 {
465 int changed = 0;
466
467 // SDL windows manager structure
468 SDL_SysWMinfo wm;
469
470 // Specify the SDL Version
471 SDL_VERSION( &wm.version );
472
473 // Get the wm structure
474 if ( SDL_GetWMInfo( &wm ) == 1 )
475 {
476 // Check that we have the X11 wm
477 if ( wm.subsystem == SDL_SYSWM_X11 )
478 {
479 // Get the SDL window
480 Window window = wm.info.x11.window;
481
482 // Get the display session
483 Display *display = wm.info.x11.display;
484
485 // Get the window attributes
486 XWindowAttributes attr;
487 XGetWindowAttributes( display, window, &attr );
488
489 // Determine whether window has changed
490 changed = *width != attr.width || *height != attr.height;
491
492 // Return width and height
493 *width = attr.width;
494 *height = attr.height;
495 }
496 }
497
498 return changed;
499 }
500
501 /** Callback to allow override of the close method.
502 */
503
504 static void consumer_close( mlt_consumer parent )
505 {
506 // Get the actual object
507 consumer_sdl this = parent->child;
508
509 // Kill the thread and clean up
510 this->running = 0;
511
512 pthread_mutex_lock( &this->audio_mutex );
513 pthread_cond_broadcast( &this->audio_cond );
514 pthread_mutex_unlock( &this->audio_mutex );
515
516 pthread_join( this->thread, NULL );
517 pthread_mutex_destroy( &this->audio_mutex );
518 pthread_cond_destroy( &this->audio_cond );
519
520 // Now clean up the rest (the close = NULL is a bit nasty but needed for now)
521 parent->close = NULL;
522 mlt_consumer_close( parent );
523
524 // Finally clean up this
525 free( this );
526 }
527