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