c0848dbec4e0b46c77bc699b6615f862df358e74
[melted] / src / modules / sdl / consumer_sdl_still.c
1 /*
2 * consumer_sdl_still.c -- A Simple DirectMedia Layer consumer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates
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 <framework/mlt_factory.h>
25 #include <framework/mlt_filter.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <pthread.h>
30 #include <SDL/SDL.h>
31 #include <SDL/SDL_syswm.h>
32 #include <sys/time.h>
33
34 /** This classes definition.
35 */
36
37 typedef struct consumer_sdl_s *consumer_sdl;
38
39 struct consumer_sdl_s
40 {
41 struct mlt_consumer_s parent;
42 mlt_properties properties;
43 pthread_t thread;
44 int joined;
45 int running;
46 int window_width;
47 int window_height;
48 int width;
49 int height;
50 int playing;
51 int sdl_flags;
52 SDL_Surface *sdl_screen;
53 SDL_Rect rect;
54 uint8_t *buffer;
55 int last_position;
56 mlt_producer last_producer;
57 int filtered;
58 };
59
60 /** Forward references to static functions.
61 */
62
63 static int consumer_start( mlt_consumer parent );
64 static int consumer_stop( mlt_consumer parent );
65 static int consumer_is_stopped( mlt_consumer parent );
66 static void consumer_close( mlt_consumer parent );
67 static void *consumer_thread( void * );
68 static int consumer_get_dimensions( int *width, int *height );
69 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
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_still_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 // Get the parent consumer object
84 mlt_consumer parent = &this->parent;
85
86 // get a handle on properties
87 mlt_service service = MLT_CONSUMER_SERVICE( parent );
88 this->properties = MLT_SERVICE_PROPERTIES( service );
89
90 // Get the default display ratio
91 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
92
93 // We have stuff to clean up, so override the close method
94 parent->close = consumer_close;
95
96 // Default scaler (for now we'll use nearest)
97 mlt_properties_set( this->properties, "rescale", "nearest" );
98
99 // We're always going to run this in non-realtime mode
100 mlt_properties_set( this->properties, "real_time", "0" );
101
102 // Default progressive true
103 mlt_properties_set_int( this->properties, "progressive", 1 );
104
105 // Ensure we don't join on a non-running object
106 this->joined = 1;
107
108 // process actual param
109 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
110 {
111 this->width = mlt_properties_get_int( this->properties, "width" );
112 this->height = mlt_properties_get_int( this->properties, "height" );
113 }
114
115 // Default window size
116 this->window_width = ( float )this->height * display_ratio;
117 this->window_height = this->height;
118
119 // Set the sdl flags
120 //this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
121 // Experimental settings
122 this->sdl_flags = SDL_RESIZABLE | SDL_DOUBLEBUF;
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 // Register specific events
130 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
131
132 // Return the consumer produced
133 return parent;
134 }
135
136 // malloc or consumer init failed
137 free( this );
138
139 // Indicate failure
140 return NULL;
141 }
142
143 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
144 {
145 if ( listener != NULL )
146 listener( owner, this, ( SDL_Event * )args[ 0 ] );
147 }
148
149 static int consumer_start( mlt_consumer parent )
150 {
151 consumer_sdl this = parent->child;
152
153 if ( !this->running )
154 {
155 // Attach a colour space converter
156 if ( !this->filtered )
157 {
158 mlt_filter filter = mlt_factory_filter( "avcolour_space", NULL );
159 mlt_properties_set_int( MLT_FILTER_PROPERTIES( filter ), "forced", mlt_image_yuv422 );
160 mlt_service_attach( MLT_CONSUMER_SERVICE( parent ), filter );
161 mlt_filter_close( filter );
162 this->filtered = 1;
163 }
164
165 consumer_stop( parent );
166
167 this->last_position = -1;
168 this->running = 1;
169 this->joined = 0;
170
171 // Allow the user to force resizing to window size
172 if ( mlt_properties_get_int( this->properties, "resize" ) )
173 {
174 mlt_properties_set_int( this->properties, "width", this->width );
175 mlt_properties_set_int( this->properties, "height", this->height );
176 }
177
178 pthread_create( &this->thread, NULL, consumer_thread, this );
179 }
180
181 return 0;
182 }
183
184 static int consumer_stop( mlt_consumer parent )
185 {
186 // Get the actual object
187 consumer_sdl this = parent->child;
188
189 if ( this->joined == 0 )
190 {
191 // Kill the thread and clean up
192 this->running = 0;
193
194 pthread_join( this->thread, NULL );
195 this->joined = 1;
196 }
197
198 return 0;
199 }
200
201 static int consumer_is_stopped( mlt_consumer parent )
202 {
203 consumer_sdl this = parent->child;
204 return !this->running;
205 }
206
207 static int sdl_lock_display( )
208 {
209 SDL_Surface *screen = SDL_GetVideoSurface( );
210 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
211 }
212
213 static void sdl_unlock_display( )
214 {
215 SDL_Surface *screen = SDL_GetVideoSurface( );
216 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
217 SDL_UnlockSurface( screen );
218 }
219
220 static inline void display_1( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
221 {
222 // Generate the affine transform scaling values
223 int scale_width = ( width << 16 ) / rect.w;
224 int scale_height = ( height << 16 ) / rect.h;
225 int stride = width * 3;
226 int x, y, row_index;
227 uint8_t *q, *row;
228
229 // Constants defined for clarity and optimisation
230 int scanlength = screen->pitch;
231 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
232 uint8_t *p;
233
234 // Iterate through the screen using a very basic scaling algorithm
235 for ( y = 0; y < rect.h; y ++ )
236 {
237 p = start;
238 row_index = ( scale_height * y ) >> 16;
239 row = image + stride * row_index;
240 for ( x = 0; x < rect.w; x ++ )
241 {
242 q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
243 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
244 }
245 start += scanlength;
246 }
247 }
248
249 static inline void display_2( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
250 {
251 // Generate the affine transform scaling values
252 int scale_width = ( width << 16 ) / rect.w;
253 int scale_height = ( height << 16 ) / rect.h;
254 int stride = width * 3;
255 int x, y, row_index;
256 uint8_t *q, *row;
257
258 // Constants defined for clarity and optimisation
259 int scanlength = screen->pitch / 2;
260 uint16_t *start = ( uint16_t * )screen->pixels + rect.y * scanlength + rect.x;
261 uint16_t *p;
262
263 // Iterate through the screen using a very basic scaling algorithm
264 for ( y = 0; y < rect.h; y ++ )
265 {
266 p = start;
267 row_index = ( scale_height * y ) >> 16;
268 row = image + stride * row_index;
269 for ( x = 0; x < rect.w; x ++ )
270 {
271 q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
272 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
273 }
274 start += scanlength;
275 }
276 }
277
278 static inline void display_3( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
279 {
280 // Generate the affine transform scaling values
281 int scale_width = ( width << 16 ) / rect.w;
282 int scale_height = ( height << 16 ) / rect.h;
283 int stride = width * 3;
284 int x, y, row_index;
285 uint8_t *q, *row;
286
287 // Constants defined for clarity and optimisation
288 int scanlength = screen->pitch;
289 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
290 uint8_t *p;
291 uint32_t pixel;
292
293 // Iterate through the screen using a very basic scaling algorithm
294 for ( y = 0; y < rect.h; y ++ )
295 {
296 p = start;
297 row_index = ( scale_height * y ) >> 16;
298 row = image + stride * row_index;
299 for ( x = 0; x < rect.w; x ++ )
300 {
301 q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
302 pixel = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
303 *p ++ = (pixel & 0xFF0000) >> 16;
304 *p ++ = (pixel & 0x00FF00) >> 8;
305 *p ++ = (pixel & 0x0000FF);
306 }
307 start += scanlength;
308 }
309 }
310
311 static inline void display_4( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
312 {
313 // Generate the affine transform scaling values
314 int scale_width = ( width << 16 ) / rect.w;
315 int scale_height = ( height << 16 ) / rect.h;
316 int stride = width * 3;
317 int x, y, row_index;
318 uint8_t *q, *row;
319
320 // Constants defined for clarity and optimisation
321 int scanlength = screen->pitch / 4;
322 uint32_t *start = ( uint32_t * )screen->pixels + rect.y * scanlength + rect.x;
323 uint32_t *p;
324
325 // Iterate through the screen using a very basic scaling algorithm
326 for ( y = 0; y < rect.h; y ++ )
327 {
328 p = start;
329 row_index = ( scale_height * y ) >> 16;
330 row = image + stride * row_index;
331 for ( x = 0; x < rect.w; x ++ )
332 {
333 q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
334 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
335 }
336 start += scanlength;
337 }
338 }
339
340 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
341 {
342 // Get the properties of this consumer
343 mlt_properties properties = this->properties;
344
345 mlt_image_format vfmt = mlt_image_rgb24;
346 int height = this->height;
347 int width = this->width;
348 uint8_t *image = NULL;
349 int changed = 0;
350 double display_ratio = mlt_properties_get_double( this->properties, "display_ratio" );
351
352 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
353 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
354
355 if ( lock != NULL ) lock( );
356
357 sdl_lock_display();
358
359 // Handle events
360 if ( this->sdl_screen != NULL )
361 {
362 SDL_Event event;
363
364 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
365
366 while ( SDL_PollEvent( &event ) )
367 {
368 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
369
370 switch( event.type )
371 {
372 case SDL_VIDEORESIZE:
373 this->window_width = event.resize.w;
374 this->window_height = event.resize.h;
375 changed = 1;
376 break;
377 case SDL_QUIT:
378 this->running = 0;
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 if ( this->sdl_screen == NULL || changed || mlt_properties_get_int( properties, "changed" ) == 2 )
397 {
398 // open SDL window
399 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
400 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
401 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
402
403 changed = 1;
404 mlt_properties_set_int( properties, "changed", 0 );
405 }
406 else
407 {
408 changed = 1;
409 mlt_properties_set_int( properties, "changed", 0 );
410 }
411
412 if ( changed == 0 &&
413 this->last_position == mlt_frame_get_position( frame ) &&
414 this->last_producer == mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "_producer", NULL ) )
415 {
416 sdl_unlock_display( );
417 if ( unlock != NULL )
418 unlock( );
419 return 0;
420 }
421
422 // Update last frame shown info
423 this->last_position = mlt_frame_get_position( frame );
424 this->last_producer = mlt_properties_get_data( MLT_FRAME_PROPERTIES( frame ), "_producer", NULL );
425
426 // Get the image, width and height
427 mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
428 mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
429
430 if ( image != NULL )
431 {
432 char *rescale = mlt_properties_get( properties, "rescale" );
433 if ( rescale != NULL && strcmp( rescale, "none" ) )
434 {
435 float this_aspect = display_ratio / ( ( float )this->window_width / ( float )this->window_height );
436 this->rect.w = this_aspect * this->window_width;
437 this->rect.h = this->window_height;
438 if ( this->rect.w > this->window_width )
439 {
440 this->rect.w = this->window_width;
441 this->rect.h = ( 1.0 / this_aspect ) * this->window_height;
442 }
443 }
444 else
445 {
446 float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * width / height;
447 this->rect.w = frame_aspect * this->window_height;
448 this->rect.h = this->window_height;
449 if ( this->rect.w > this->window_width )
450 {
451 this->rect.w = this->window_width;
452 this->rect.h = ( 1.0 / frame_aspect ) * this->window_width;
453 }
454 }
455
456 this->rect.x = ( this->window_width - this->rect.w ) / 2;
457 this->rect.y = ( this->window_height - this->rect.h ) / 2;
458
459 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
460 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
461 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
462 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
463 }
464
465 if ( !mlt_consumer_is_stopped( &this->parent ) && SDL_GetVideoSurface( ) != NULL && this->sdl_screen != NULL && this->sdl_screen->pixels != NULL )
466 {
467 memset( this->sdl_screen->pixels, 0, this->window_width * this->window_height * this->sdl_screen->format->BytesPerPixel );
468
469 switch( this->sdl_screen->format->BytesPerPixel )
470 {
471 case 1:
472 display_1( this->sdl_screen, this->rect, image, width, height );
473 break;
474 case 2:
475 display_2( this->sdl_screen, this->rect, image, width, height );
476 break;
477 case 3:
478 display_3( this->sdl_screen, this->rect, image, width, height );
479 break;
480 case 4:
481 display_4( this->sdl_screen, this->rect, image, width, height );
482 break;
483 default:
484 fprintf( stderr, "Unsupported video depth %d\n", this->sdl_screen->format->BytesPerPixel );
485 break;
486 }
487
488 // Flip it into sight
489 SDL_Flip( this->sdl_screen );
490 }
491
492 sdl_unlock_display();
493
494 if ( unlock != NULL ) unlock( );
495
496 return 1;
497 }
498
499 /** Threaded wrapper for pipe.
500 */
501
502 static void *consumer_thread( void *arg )
503 {
504 // Identify the arg
505 consumer_sdl this = arg;
506
507 // Get the consumer
508 mlt_consumer consumer = &this->parent;
509
510 // internal intialization
511 mlt_frame frame = NULL;
512
513 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
514 {
515 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
516 {
517 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
518 return NULL;
519 }
520
521 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
522 SDL_EnableUNICODE( 1 );
523 }
524 else
525 {
526 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "changed", 2 );
527 if ( SDL_GetVideoSurface( ) != NULL )
528 {
529 this->sdl_screen = SDL_GetVideoSurface( );
530 consumer_get_dimensions( &this->window_width, &this->window_height );
531 mlt_properties_set_int( MLT_CONSUMER_PROPERTIES( consumer ), "changed", 0 );
532 }
533 }
534
535 // Loop until told not to
536 while( this->running )
537 {
538 // Get a frame from the attached producer
539 frame = mlt_consumer_rt_frame( consumer );
540
541 // Ensure that we have a frame
542 if ( frame != NULL )
543 {
544 consumer_play_video( this, frame );
545 mlt_frame_close( frame );
546 }
547 else
548 {
549 this->running = 0;
550 }
551 }
552
553 if ( mlt_properties_get_int( MLT_CONSUMER_PROPERTIES( consumer ), "sdl_started" ) == 0 )
554 SDL_Quit( );
555
556 this->sdl_screen = NULL;
557
558 return NULL;
559 }
560
561 static int consumer_get_dimensions( int *width, int *height )
562 {
563 int changed = 0;
564
565 // SDL windows manager structure
566 SDL_SysWMinfo wm;
567
568 // Specify the SDL Version
569 SDL_VERSION( &wm.version );
570
571 // Get the wm structure
572 if ( SDL_GetWMInfo( &wm ) == 1 )
573 {
574 #ifndef __DARWIN__
575 // Check that we have the X11 wm
576 if ( wm.subsystem == SDL_SYSWM_X11 )
577 {
578 // Get the SDL window
579 Window window = wm.info.x11.window;
580
581 // Get the display session
582 Display *display = wm.info.x11.display;
583
584 // Get the window attributes
585 XWindowAttributes attr;
586 XGetWindowAttributes( display, window, &attr );
587
588 // Determine whether window has changed
589 changed = *width != attr.width || *height != attr.height;
590
591 // Return width and height
592 *width = attr.width;
593 *height = attr.height;
594 }
595 #endif
596 }
597
598 return changed;
599 }
600
601 /** Callback to allow override of the close method.
602 */
603
604 static void consumer_close( mlt_consumer parent )
605 {
606 // Get the actual object
607 consumer_sdl this = parent->child;
608
609 // Stop the consumer
610 mlt_consumer_stop( parent );
611
612 // Now clean up the rest
613 mlt_consumer_close( parent );
614
615 // Finally clean up this
616 free( this );
617 }