]> git.sesse.net Git - mlt/blob - src/modules/sdl/consumer_sdl_still.c
SDL Preview provisional checkin
[mlt] / 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                 // Inherit the scheduling priority
180                 pthread_attr_init( &thread_attributes );
181                 pthread_attr_setinheritsched( &thread_attributes, PTHREAD_INHERIT_SCHED );
182
183                 pthread_create( &this->thread, &thread_attributes, consumer_thread, this );
184         }
185
186         return 0;
187 }
188
189 static int consumer_stop( mlt_consumer parent )
190 {
191         // Get the actual object
192         consumer_sdl this = parent->child;
193
194         if ( this->joined == 0 )
195         {
196                 // Kill the thread and clean up
197                 this->running = 0;
198
199                 pthread_join( this->thread, NULL );
200                 this->joined = 1;
201
202                 mlt_frame_close( parent->put );
203                 parent->put = NULL;
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 int consumer_play_video( consumer_sdl this, mlt_frame frame )
229 {
230         // Get the properties of this consumer
231         mlt_properties properties = this->properties;
232
233         mlt_image_format vfmt = mlt_image_rgb24;
234         int width = this->width, height = this->height;
235         uint8_t *image;
236         int changed = 0;
237
238         
239         // Handle events
240         if ( this->sdl_screen != NULL )
241         {
242                 SDL_Event event;
243
244                 changed = consumer_get_dimensions( &this->window_width, &this->window_height );
245
246                 while ( SDL_PollEvent( &event ) )
247                 {
248                         mlt_events_fire( this->properties, "consumer-sdl-event", &event, NULL );
249
250                         switch( event.type )
251                         {
252                                 case SDL_VIDEORESIZE:
253                                         this->window_width = event.resize.w;
254                                         this->window_height = event.resize.h;
255                                         changed = 1;
256                                         break;
257                                 case SDL_QUIT:
258                                         this->running = 0;
259                                         break;
260                                 case SDL_KEYDOWN:
261                                         {
262                                                 mlt_producer producer = mlt_properties_get_data( properties, "transport_producer", NULL );
263                                                 char keyboard[ 2 ] = " ";
264                                                 void (*callback)( mlt_producer, char * ) = mlt_properties_get_data( properties, "transport_callback", NULL );
265                                                 if ( callback != NULL && producer != NULL && event.key.keysym.unicode < 0x80 && event.key.keysym.unicode > 0 )
266                                                 {
267                                                         keyboard[ 0 ] = ( char )event.key.keysym.unicode;
268                                                         callback( producer, keyboard );
269                                                 }
270                                         }
271                                         break;
272                         }
273                 }
274         }
275
276         if ( width != this->width || height != this->height || 
277                  ( ( int )( this->last_frame_aspect * 1000 ) != ( int )( mlt_frame_get_aspect_ratio( frame ) * 1000 ) &&
278                  ( mlt_frame_get_aspect_ratio( frame ) != 1.0 || this->last_frame_aspect == 0.0 ) ) )
279
280         {
281                 this->width = width;
282                 this->height = height;
283                 this->last_frame_aspect = mlt_frame_get_aspect_ratio( frame );
284                 changed = 1;
285         }
286
287         if ( this->sdl_screen == NULL || changed )
288         {
289                 // Determine frame's display aspect ratio
290                 float frame_aspect = mlt_frame_get_aspect_ratio( frame ) * this->width / this->height;
291                 
292                 // Determine window's new display aspect ratio
293                 float this_aspect = ( float )this->window_width / this->window_height;
294
295                 // If using hardware scaler
296                 if ( mlt_properties_get( properties, "rescale" ) != NULL &&
297                         !strcmp( mlt_properties_get( properties, "rescale" ), "none" ) )
298                 {
299                         // Special case optimisation to negate odd effect of sample aspect ratio
300                         // not corresponding exactly with image resolution.
301                         if ( ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) && 
302                                  ( (int)( mlt_frame_get_aspect_ratio( frame ) * 1000 ) == (int)( this->aspect_ratio * 1000 ) ) )
303                         {
304                                 this->rect.w = this->window_width;
305                                 this->rect.h = this->window_height;
306                         }
307                         else
308                         {
309                                 // Use hardware scaler to normalise display aspect ratio
310                                 this->rect.w = frame_aspect / this_aspect * this->window_width;
311                                 this->rect.h = this->window_height;
312                                 if ( this->rect.w > this->window_width )
313                                 {
314                                         this->rect.w = this->window_width;
315                                         this->rect.h = this_aspect / frame_aspect * this->window_height;
316                                 }
317                         }
318                 }
319                 // Special case optimisation to negate odd effect of sample aspect ratio
320                 // not corresponding exactly with image resolution.
321                 else if ( (int)( this_aspect * 1000 ) == (int)( this->display_aspect * 1000 ) ) 
322                 {
323                         this->rect.w = this->window_width;
324                         this->rect.h = this->window_height;
325                 }
326                 // Use hardware scaler to normalise sample aspect ratio
327                 else if ( this->window_height * this->display_aspect > this->window_width )
328                 {
329                         this->rect.w = this->window_width;
330                         this->rect.h = this->window_width / this->display_aspect;
331                 }
332                 else
333                 {
334                         this->rect.w = this->window_height * this->display_aspect;
335                         this->rect.h = this->window_height;
336                 }
337                 
338                 this->rect.x = ( this->window_width - this->rect.w ) / 2;
339                 this->rect.y = ( this->window_height - this->rect.h ) / 2;
340
341                 mlt_properties_set_int( this->properties, "rect_x", this->rect.x );
342                 mlt_properties_set_int( this->properties, "rect_y", this->rect.y );
343                 mlt_properties_set_int( this->properties, "rect_w", this->rect.w );
344                 mlt_properties_set_int( this->properties, "rect_h", this->rect.h );
345                 
346                 // open SDL window 
347                 sdl_lock_display();
348                 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 16, this->sdl_flags );
349                 consumer_get_dimensions( &this->window_width, &this->window_height );
350                 sdl_unlock_display();
351                 changed = 1;
352         }
353                 
354         if ( mlt_properties_get_int( properties, "changed" ) )
355         {
356                 sdl_lock_display();
357                 this->sdl_screen = SDL_SetVideoMode( this->window_width, this->window_height, 16, this->sdl_flags );
358                 SDL_SetClipRect( this->sdl_screen, &this->rect );
359                 SDL_Flip( this->sdl_screen );
360                 consumer_get_dimensions( &this->window_width, &this->window_height );
361                 sdl_unlock_display();
362                 mlt_properties_set_int( properties, "changed", 0 );
363                 changed = 1;
364         }
365
366         if ( changed == 0 &&
367                  this->last_position == mlt_frame_get_position( frame ) &&
368                  this->last_producer == mlt_properties_get_data( mlt_frame_properties( frame ), "_producer", NULL ) )
369                 return 0;
370
371         // Update last frame shown info
372         this->last_position = mlt_frame_get_position( frame );
373         this->last_producer = mlt_properties_get_data( mlt_frame_properties( frame ), "_producer", NULL );
374
375         // Get the image, width and height
376         mlt_events_fire( properties, "consumer-frame-show", frame, NULL );
377         mlt_frame_get_image( frame, &image, &vfmt, &width, &height, 0 );
378         
379         if ( this->sdl_screen != NULL )
380         {
381                 sdl_lock_display();
382
383                 // Calculate the scan length
384                 int scanlength = this->sdl_screen->pitch / 2;
385
386                 // Obtain the clip rect from the screen
387                 SDL_Rect rect = this->rect;
388
389                 // Generate the affine transform scaling values
390                 float scale_width = ( float )width / ( float )rect.w;
391                 float scale_height = ( float )height / ( float )rect.h;
392
393                 // Constants defined for clarity and optimisation
394                 int stride = width * 3;
395                 uint16_t *start = ( uint16_t * )this->sdl_screen->pixels + rect.y * scanlength + rect.x;
396
397                 int y;
398
399                 // Iterate through the screen using a very basic scaling algorithm
400                 for ( y = 0; y < rect.h; y ++ )
401                 {
402                         // Obtain the pointer to the current screen row
403                         uint16_t *p = start;
404
405                         // Calculate the row_index
406                         int row_index = ( int )( scale_height * y );
407
408                         // Calculate the pointer for the y offset (positioned on B instead of R)
409                         uint8_t *row = image + stride * row_index;
410
411                         int x;
412
413                         // Iterate through the screen width
414                         for ( x = 0; x < rect.w; x ++ )
415                         {
416                                 // Obtain the pixel pointer
417                                 uint8_t *q = row + ( ( int )( scale_width * x ) * 3 );
418
419                                 // Map the BGR colour from the frame to the SDL format
420                                 *p ++ = SDL_MapRGB( this->sdl_screen->format, *q, *( q + 1 ), *( q + 2 ) );
421                         }
422
423                         // Move to the next row
424                         start += scanlength;
425                 }
426
427                 // Flip it into sight
428                 SDL_Flip( this->sdl_screen );
429
430                 sdl_unlock_display();
431         }
432
433         return 0;
434 }
435
436 /** Threaded wrapper for pipe.
437 */
438
439 static void *consumer_thread( void *arg )
440 {
441         // Identify the arg
442         consumer_sdl this = arg;
443
444         // Get the consumer
445         mlt_consumer consumer = &this->parent;
446
447         // internal intialization
448         mlt_frame frame = NULL;
449         struct timespec tm = { 0, 1000 };
450
451         if ( mlt_properties_get_int( mlt_consumer_properties( consumer ), "sdl_started" ) == 0 )
452         {
453                 if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE ) < 0 )
454                 {
455                         fprintf( stderr, "Failed to initialize SDL: %s\n", SDL_GetError() );
456                         return NULL;
457                 }
458
459                 SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL );
460                 SDL_EnableUNICODE( 1 );
461         }
462         else
463         {
464                 this->sdl_screen = SDL_GetVideoSurface( );
465                 mlt_properties_set_int( mlt_consumer_properties( consumer ), "changed", 1 );
466         }
467
468         // Loop until told not to
469         while( this->running )
470         {
471                 // Get a frame from the attached producer
472                 frame = mlt_consumer_rt_frame( consumer );
473
474                 // Ensure that we have a frame
475                 if ( frame != NULL )
476                 {
477                         consumer_play_video( this, frame );
478                         mlt_frame_close( frame );
479                         nanosleep( &tm, NULL );
480                 }
481         }
482
483         if ( mlt_properties_get_int( mlt_consumer_properties( consumer ), "sdl_started" ) == 0 )
484                 SDL_Quit( );
485
486         this->sdl_screen = NULL;
487
488         return NULL;
489 }
490
491 static int consumer_get_dimensions( int *width, int *height )
492 {
493         int changed = 0;
494
495         // SDL windows manager structure
496         SDL_SysWMinfo wm;
497
498         // Specify the SDL Version
499         SDL_VERSION( &wm.version );
500
501         // Lock the display
502         sdl_lock_display();
503
504         // Get the wm structure
505         if ( SDL_GetWMInfo( &wm ) == 1 )
506         {
507                 // Check that we have the X11 wm
508                 if ( wm.subsystem == SDL_SYSWM_X11 ) 
509                 {
510                         // Get the SDL window
511                         Window window = wm.info.x11.window;
512
513                         // Get the display session
514                         Display *display = wm.info.x11.display;
515
516                         // Get the window attributes
517                         XWindowAttributes attr;
518                         XGetWindowAttributes( display, window, &attr );
519
520                         // Determine whether window has changed
521                         changed = *width != attr.width || *height != attr.height;
522
523                         // Return width and height
524                         *width = attr.width;
525                         *height = attr.height;
526                 }
527         }
528
529         // Unlock the display
530         sdl_lock_display();
531
532         return changed;
533 }
534
535 /** Callback to allow override of the close method.
536 */
537
538 static void consumer_close( mlt_consumer parent )
539 {
540         // Get the actual object
541         consumer_sdl this = parent->child;
542
543         // Stop the consumer
544         mlt_consumer_stop( parent );
545
546         // Now clean up the rest
547         mlt_consumer_close( parent );
548
549         // Finally clean up this
550         free( this );
551 }