Fixes threaded pixbuf usage and removes flash when swicthing between sdl preview...
[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 };
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 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args );
72
73 /** This is what will be called by the factory - anything can be passed in
74 via the argument, but keep it simple.
75 */
76
77 mlt_consumer consumer_sdl_still_init( char *arg )
78 {
79 // Create the consumer object
80 consumer_sdl this = calloc( sizeof( struct consumer_sdl_s ), 1 );
81
82 // If no malloc'd and consumer init ok
83 if ( this != NULL && mlt_consumer_init( &this->parent, this ) == 0 )
84 {
85 // Get the parent consumer object
86 mlt_consumer parent = &this->parent;
87
88 // Attach a colour space converter
89 mlt_filter filter = mlt_factory_filter( "avcolour_space", NULL );
90 mlt_properties_set_int( mlt_filter_properties( filter ), "forced", mlt_image_yuv422 );
91 mlt_service_attach( mlt_consumer_service( &this->parent ), filter );
92 mlt_filter_close( filter );
93
94 // We have stuff to clean up, so override the close method
95 parent->close = consumer_close;
96
97 // get a handle on properties
98 mlt_service service = mlt_consumer_service( parent );
99 this->properties = mlt_service_properties( service );
100
101 // Default scaler (for now we'll use nearest)
102 mlt_properties_set( this->properties, "rescale", "nearest" );
103
104 // We're always going to run this in non-realtime mode
105 mlt_properties_set( this->properties, "real_time", "0" );
106
107 // Default progressive true
108 mlt_properties_set_int( this->properties, "progressive", 1 );
109
110 // Get sample aspect ratio
111 this->aspect_ratio = mlt_properties_get_double( this->properties, "aspect_ratio" );
112
113 // Ensure we don't join on a non-running object
114 this->joined = 1;
115
116 // Default display aspect ratio
117 this->display_aspect = 4.0 / 3.0;
118
119 // process actual param
120 if ( arg == NULL || sscanf( arg, "%dx%d", &this->width, &this->height ) != 2 )
121 {
122 this->width = mlt_properties_get_int( this->properties, "width" );
123 this->height = mlt_properties_get_int( this->properties, "height" );
124 }
125
126 // Default window size
127 this->window_width = ( float )this->height * this->display_aspect;
128 this->window_height = this->height;
129
130 // Set the sdl flags
131 this->sdl_flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL | SDL_RESIZABLE | SDL_DOUBLEBUF;
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 // Register specific events
139 mlt_events_register( this->properties, "consumer-sdl-event", ( mlt_transmitter )consumer_sdl_event );
140
141 // Return the consumer produced
142 return parent;
143 }
144
145 // malloc or consumer init failed
146 free( this );
147
148 // Indicate failure
149 return NULL;
150 }
151
152 static void consumer_sdl_event( mlt_listener listener, mlt_properties owner, mlt_service this, void **args )
153 {
154 if ( listener != NULL )
155 listener( owner, this, ( SDL_Event * )args[ 0 ] );
156 }
157
158 static int consumer_start( mlt_consumer parent )
159 {
160 consumer_sdl this = parent->child;
161
162 if ( !this->running )
163 {
164 pthread_attr_t thread_attributes;
165
166 consumer_stop( parent );
167
168 this->last_position = -1;
169 this->running = 1;
170 this->joined = 0;
171
172 // Allow the user to force resizing to window size
173 if ( mlt_properties_get_int( this->properties, "resize" ) )
174 {
175 mlt_properties_set_int( this->properties, "width", this->width );
176 mlt_properties_set_int( this->properties, "height", this->height );
177 }
178
179 //this->width = this->height * this->display_aspect;
180
181 // Inherit the scheduling priority
182 pthread_attr_init( &thread_attributes );
183 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
184
185 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
186 }
187
188 return 0;
189 }
190
191 static int consumer_stop( mlt_consumer parent )
192 {
193 // Get the actual object
194 consumer_sdl this = parent->child;
195
196 if ( this->joined == 0 )
197 {
198 // Kill the thread and clean up
199 this->running = 0;
200
201 pthread_join( this->thread, NULL );
202 this->joined = 1;
203 }
204
205 return 0;
206 }
207
208 static int consumer_is_stopped( mlt_consumer parent )
209 {
210 consumer_sdl this = parent->child;
211 return !this->running;
212 }
213
214 static int sdl_lock_display( )
215 {
216 SDL_Surface *screen = SDL_GetVideoSurface( );
217 return screen != NULL && ( !SDL_MUSTLOCK( screen ) || SDL_LockSurface( screen ) >= 0 );
218 }
219
220 static void sdl_unlock_display( )
221 {
222 SDL_Surface *screen = SDL_GetVideoSurface( );
223 if ( screen != NULL && SDL_MUSTLOCK( screen ) )
224 SDL_UnlockSurface( screen );
225 }
226
227 static inline void display_1( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
228 {
229 // Generate the affine transform scaling values
230 int scale_width = ( width << 16 ) / rect.w;
231 int scale_height = ( height << 16 ) / rect.h;
232 int stride = width * 3;
233 int x, y, row_index;
234 uint8_t *q, *row;
235
236 // Constants defined for clarity and optimisation
237 int scanlength = screen->pitch;
238 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
239 uint8_t *p;
240
241 // Iterate through the screen using a very basic scaling algorithm
242 for ( y = 0; y < rect.h; y ++ )
243 {
244 p = start;
245 row_index = ( scale_height * y ) >> 16;
246 row = image + stride * row_index;
247 for ( x = 0; x < rect.w; x ++ )
248 {
249 q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
250 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
251 }
252 start += scanlength;
253 }
254 }
255
256 static inline void display_2( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
257 {
258 // Generate the affine transform scaling values
259 int scale_width = ( width << 16 ) / rect.w;
260 int scale_height = ( height << 16 ) / rect.h;
261 int stride = width * 3;
262 int x, y, row_index;
263 uint8_t *q, *row;
264
265 // Constants defined for clarity and optimisation
266 int scanlength = screen->pitch / 2;
267 uint16_t *start = ( uint16_t * )screen->pixels + rect.y * scanlength + rect.x;
268 uint16_t *p;
269
270 // Iterate through the screen using a very basic scaling algorithm
271 for ( y = 0; y < rect.h; y ++ )
272 {
273 p = start;
274 row_index = ( scale_height * y ) >> 16;
275 row = image + stride * row_index;
276 for ( x = 0; x < rect.w; x ++ )
277 {
278 q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
279 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
280 }
281 start += scanlength;
282 }
283 }
284
285 static inline void display_3( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
286 {
287 // Generate the affine transform scaling values
288 int scale_width = ( width << 16 ) / rect.w;
289 int scale_height = ( height << 16 ) / rect.h;
290 int stride = width * 3;
291 int x, y, row_index;
292 uint8_t *q, *row;
293
294 // Constants defined for clarity and optimisation
295 int scanlength = screen->pitch;
296 uint8_t *start = ( uint8_t * )screen->pixels + rect.y * scanlength + rect.x;
297 uint8_t *p;
298 uint32_t pixel;
299
300 // Iterate through the screen using a very basic scaling algorithm
301 for ( y = 0; y < rect.h; y ++ )
302 {
303 p = start;
304 row_index = ( scale_height * y ) >> 16;
305 row = image + stride * row_index;
306 for ( x = 0; x < rect.w; x ++ )
307 {
308 q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
309 pixel = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
310 *p ++ = (pixel & 0xFF0000) >> 16;
311 *p ++ = (pixel & 0x00FF00) >> 8;
312 *p ++ = (pixel & 0x0000FF);
313 }
314 start += scanlength;
315 }
316 }
317
318 static inline void display_4( SDL_Surface *screen, SDL_Rect rect, uint8_t *image, int width, int height )
319 {
320 // Generate the affine transform scaling values
321 int scale_width = ( width << 16 ) / rect.w;
322 int scale_height = ( height << 16 ) / rect.h;
323 int stride = width * 3;
324 int x, y, row_index;
325 uint8_t *q, *row;
326
327 // Constants defined for clarity and optimisation
328 int scanlength = screen->pitch / 4;
329 uint32_t *start = ( uint32_t * )screen->pixels + rect.y * scanlength + rect.x;
330 uint32_t *p;
331
332 // Iterate through the screen using a very basic scaling algorithm
333 for ( y = 0; y < rect.h; y ++ )
334 {
335 p = start;
336 row_index = ( scale_height * y ) >> 16;
337 row = image + stride * row_index;
338 for ( x = 0; x < rect.w; x ++ )
339 {
340 q = row + ( ( ( scale_width * x ) >> 16 ) * 3 );
341 *p ++ = SDL_MapRGB( screen->format, *q, *( q + 1 ), *( q + 2 ) );
342 }
343 start += scanlength;
344 }
345 }
346
347 static int consumer_play_video( consumer_sdl this, mlt_frame frame )
348 {
349 // Get the properties of this consumer
350 mlt_properties properties = this->properties;
351
352 mlt_image_format vfmt = mlt_image_rgb24;
353 int height = this->height;
354 int width = this->width;
355 uint8_t *image = NULL;
356 int changed = 0;
357
358 void ( *lock )( void ) = mlt_properties_get_data( properties, "app_lock", NULL );
359 void ( *unlock )( void ) = mlt_properties_get_data( properties, "app_unlock", NULL );
360
361 if ( lock != NULL ) lock( );
362
363 sdl_lock_display();
364
365 // Handle events
366 if ( this->sdl_screen != NULL )
367 {
368 SDL_Event event;
369
370 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
371
372 while ( SDL_PollEvent( &event ) )
373 {
374 mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
375
376 switch( event.type )
377 {
378 case SDL_VIDEORESIZE:
379 this->window_width = event.resize.w;
380 this->window_height = event.resize.h;
381 changed = 1;
382 break;
383 case SDL_QUIT:
384 this->running = 0;
385 break;
386 case SDL_KEYDOWN:
387 {
388 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
389 char keyboard[ 2 ] = " ";
390 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
391 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
392 {
393 keyboard[ 0 ] = ( char )event.key.keysym.unicode;
394 callback( producer, keyboard );
395 }
396 }
397 break;
398 }
399 }
400 }
401
402 if ( this->sdl_screen == NULL || changed || mlt_properties_get_int( properties, "changed" ) == 2 )
403 {
404 // open SDL window
405 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
406 if ( consumer_get_dimensions( &this->window_width, &this->window_height ) )
407 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 0, this->sdl_flags );
408
409 changed = 1;
410 mlt_properties_set_int( properties, "changed", 0 );
411 }
412 else
413 {
414 changed = mlt_properties_get_int( properties, "changed" ) | mlt_properties_get_int( mlt_frame_properties( frame ), "refresh" );
415 mlt_properties_set_int( properties, "changed", 0 );
416 }
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 ( 1 )
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 ( this->sdl_screen != 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 0;
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 struct timespec tm = { 0, 1000 };
532
533 if ( mlt_properties_get_int( mlt_consumer_properties( consumer ), "sdl_started" ) == 0 )
534 {
535 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
536 {
537 fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
538 return NULL;
539 }
540
541 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
542 SDL_EnableUNICODE( 1 );
543 }
544 else
545 {
546 mlt_properties_set_int( mlt_consumer_properties( consumer ), "changed", 2 );
547 if ( SDL_GetVideoSurface( ) != NULL )
548 {
549 this->sdl_screen = SDL_GetVideoSurface( );
550 consumer_get_dimensions( &this->window_width, &this->window_height );
551 mlt_properties_set_int( mlt_consumer_properties( consumer ), "changed", 0 );
552 }
553 }
554
555 // Loop until told not to
556 while( this->running )
557 {
558 // Get a frame from the attached producer
559 frame = mlt_consumer_rt_frame( consumer );
560
561 // Ensure that we have a frame
562 if ( frame != NULL )
563 {
564 consumer_play_video( this, frame );
565 mlt_frame_close( frame );
566 nanosleep( &tm, NULL );
567 }
568 }
569
570 if ( mlt_properties_get_int( mlt_consumer_properties( consumer ), "sdl_started" ) == 0 )
571 SDL_Quit( );
572
573 this->sdl_screen = NULL;
574
575 return NULL;
576 }
577
578 static int consumer_get_dimensions( int *width, int *height )
579 {
580 int changed = 0;
581
582 // SDL windows manager structure
583 SDL_SysWMinfo wm;
584
585 // Specify the SDL Version
586 SDL_VERSION( &wm.version );
587
588 // Get the wm structure
589 if ( SDL_GetWMInfo( &wm ) == 1 )
590 {
591 // Check that we have the X11 wm
592 if ( wm.subsystem == SDL_SYSWM_X11 )
593 {
594 // Get the SDL window
595 Window window = wm.info.x11.window;
596
597 // Get the display session
598 Display *display = wm.info.x11.display;
599
600 // Get the window attributes
601 XWindowAttributes attr;
602 XGetWindowAttributes( display, window, &attr );
603
604 // Determine whether window has changed
605 changed = *width != attr.width || *height != attr.height;
606
607 // Return width and height
608 *width = attr.width;
609 *height = attr.height;
610 }
611 }
612
613 return changed;
614 }
615
616 /** Callback to allow override of the close method.
617 */
618
619 static void consumer_close( mlt_consumer parent )
620 {
621 // Get the actual object
622 consumer_sdl this = parent->child;
623
624 // Stop the consumer
625 mlt_consumer_stop( parent );
626
627 // Now clean up the rest
628 mlt_consumer_close( parent );
629
630 // Finally clean up this
631 free( this );
632 }