]> git.sesse.net Git - mlt/blob - src/modules/sdl/producer_sdl_image.c
Refactor to use mlt_frame_set_image/_alpha.
[mlt] / src / modules / sdl / producer_sdl_image.c
1 /*
2  * producer_sdl_image.c -- Image loader which wraps SDL_image
3  * Copyright (C) 2005 Visual Media FX
4  * Author: Charles Yates <charles.yates@gmail.com>
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 <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_pool.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <dirent.h>
32 #include <math.h>
33
34 #include <SDL_image.h>
35
36 static int producer_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
37 {
38         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
39         SDL_Surface *surface = mlt_properties_get_data( properties, "surface", NULL );
40         SDL_Surface *converted = NULL;
41
42         *width = surface->w;
43         *height = surface->h;
44         int image_size = *width * *height * 3;
45
46         if ( surface->format->BitsPerPixel != 32 && surface->format->BitsPerPixel != 24 )
47         {
48                 SDL_PixelFormat fmt;
49                 fmt.BitsPerPixel = 24;
50                 fmt.BytesPerPixel = 3;
51                 fmt.Rshift = 16;
52                 fmt.Gshift = 8;
53                 fmt.Bshift = 0;
54                 fmt.Rmask = 0xff << 16;
55                 fmt.Gmask = 0xff << 8;
56                 fmt.Bmask = 0xff;
57                 converted = SDL_ConvertSurface( surface, &fmt, 0 );
58         }
59
60         switch( surface->format->BitsPerPixel )
61         {
62                 case 32:
63                         *format = mlt_image_rgb24a;
64                         image_size = *width * *height * 4;
65                         *image = mlt_pool_alloc( image_size );
66                         memcpy( *image, surface->pixels, image_size );
67                         break;
68                 case 24:
69                         *format = mlt_image_rgb24;
70                         *image = mlt_pool_alloc( image_size );
71                         memcpy( *image, surface->pixels, image_size );
72                         break;
73                 default:
74                         *image = mlt_pool_alloc( image_size );
75                         memcpy( *image, converted->pixels, image_size );
76                         break;
77         }
78
79         if ( converted )
80                 SDL_FreeSurface( converted );
81
82         // Update the frame
83         mlt_frame_set_image( frame, *image, image_size, mlt_pool_release );
84
85         return 0;
86 }
87
88 static int filter_files( const struct dirent *de )
89 {
90         return de->d_name[ 0 ] != '.';
91 }
92
93 static mlt_properties parse_file_names( char *resource )
94 {
95         mlt_properties properties = mlt_properties_new( );
96
97         if ( strstr( resource, "/.all." ) != NULL )
98         {
99                 char *dir_name = strdup( resource );
100                 char *extension = strrchr( resource, '.' );
101                 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
102                 char fullname[ 1024 ];
103                 strcpy( fullname, dir_name );
104                 struct dirent **de = NULL;
105                 int n = scandir( fullname, &de, filter_files, alphasort );
106                 int i;
107                 struct stat info;
108
109                 for (i = 0; i < n; i++ )
110                 {
111                         snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
112                         if ( strstr( fullname, extension ) && lstat( fullname, &info ) == 0 &&
113                                 ( S_ISREG( info.st_mode ) || info.st_mode | S_IXUSR ) )
114                         {
115                                 char temp[ 20 ];
116                                 sprintf( temp, "%d", i );
117                                 mlt_properties_set( properties, temp, fullname );
118                         }
119                         free( de[ i ] );
120                 }
121
122                 free( de );
123                 free( dir_name );
124         }
125         else
126         {
127                 mlt_properties_set( properties, "0", resource );
128         }
129
130         return properties;
131 }
132
133 static SDL_Surface *load_image( mlt_producer producer )
134 {
135         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
136         char *resource = mlt_properties_get( properties, "resource" );
137         char *last_resource = mlt_properties_get( properties, "_last_resource" );
138         int image_idx = 0;
139         char *this_resource = NULL;
140         double ttl = mlt_properties_get_int( properties, "ttl" );
141         mlt_position position = mlt_producer_position( producer );
142         SDL_Surface *surface = mlt_properties_get_data( properties, "_surface", NULL );
143         mlt_properties filenames = mlt_properties_get_data( properties, "_filenames", NULL );
144
145         if ( filenames == NULL )
146         {
147                 filenames = parse_file_names( resource );
148                 mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
149         }
150
151         if ( mlt_properties_count( filenames ) ) 
152         {
153                 image_idx = ( int )floor( ( double )position / ttl ) % mlt_properties_count( filenames );
154                 this_resource = mlt_properties_get_value( filenames, image_idx );
155
156                 if ( surface == NULL || last_resource == NULL || strcmp( last_resource, this_resource ) )
157                 {
158                         surface = IMG_Load( this_resource );
159                         if ( surface != NULL )
160                         {
161                                 surface->refcount ++;
162                                 mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
163                                 mlt_properties_set( properties, "_last_resource", this_resource );
164                                 mlt_properties_set_int( properties, "_real_width", surface->w );
165                                 mlt_properties_set_int( properties, "_real_height", surface->h );
166                         }
167                 }
168                 else if ( surface != NULL )
169                 {
170                         surface->refcount ++;
171                 }
172         }
173
174         return surface;
175 }
176
177 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
178 {
179         // Generate a frame
180         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
181
182         if ( *frame != NULL )
183         {
184                 // Create the surface for the current image
185                 SDL_Surface *surface = load_image( producer );
186
187                 if ( surface != NULL )
188                 {
189                         // Obtain properties of frame and producer
190                         mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
191
192                         // Obtain properties of producer
193                         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
194
195                         // Update timecode on the frame we're creating
196                         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
197
198                         // Set producer-specific frame properties
199                         mlt_properties_set_int( properties, "progressive", 1 );
200                         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
201                         mlt_properties_set_data( properties, "surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, NULL );
202                         mlt_properties_set_int( properties, "real_width", surface->w );
203                         mlt_properties_set_int( properties, "real_height", surface->h );
204
205                         // Push the get_image method
206                         mlt_frame_push_get_image( *frame, producer_get_image );
207                 }
208         }
209
210         // Calculate the next timecode
211         mlt_producer_prepare_next( producer );
212
213         return 0;
214 }
215
216 static void producer_close( mlt_producer producer )
217 {
218         producer->close = NULL;
219         mlt_producer_close( producer );
220         free( producer );
221 }
222
223 mlt_producer producer_sdl_image_init( mlt_profile profile, mlt_service_type type, const char *id, char *file )
224 {
225         mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
226         if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
227         {
228                 // Get the properties interface
229                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
230         
231                 // Callback registration
232                 producer->get_frame = producer_get_frame;
233                 producer->close = ( mlt_destructor )producer_close;
234
235                 // Set the default properties
236                 mlt_properties_set( properties, "resource", file );
237                 mlt_properties_set( properties, "_resource", "" );
238                 mlt_properties_set_double( properties, "aspect_ratio", 1 );
239                 mlt_properties_set_int( properties, "ttl", 25 );
240                 mlt_properties_set_int( properties, "progressive", 1 );
241                 
242                 // Validate the resource
243                 SDL_Surface *surface = NULL;
244                 if ( file && ( surface = load_image( producer ) ) )
245                 {
246                         SDL_FreeSurface( surface );
247                         mlt_properties_set_data( properties, "_surface", NULL, 0, NULL, NULL );
248                 }
249                 else
250                 {
251                         producer_close( producer );
252                         producer = NULL;
253                 }
254                 return producer;
255         }
256         free( producer );
257         return NULL;
258 }
259
260