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