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