1 /*
2 * producer_sdl_image.c -- Image loader which wraps SDL_image
3 * Copyright (C) 2005-2014 Meltytech, LLC
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <framework/mlt_producer.h>
21 #include <framework/mlt_frame.h>
22 #include <framework/mlt_pool.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <dirent.h>
31 #include <math.h>
32
33 #include <SDL_image.h>
34
producer_get_image(mlt_frame frame,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)35 static int producer_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
36 {
37 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
38 SDL_Surface *surface = mlt_properties_get_data( properties, "surface", NULL );
39 SDL_Surface *converted = NULL;
40
41 *width = surface->w;
42 *height = surface->h;
43 int image_size = *width * *height * 3;
44
45 if ( surface->format->BitsPerPixel != 32 && surface->format->BitsPerPixel != 24 )
46 {
47 SDL_PixelFormat fmt;
48 fmt.BitsPerPixel = 24;
49 fmt.BytesPerPixel = 3;
50 fmt.Rshift = 16;
51 fmt.Gshift = 8;
52 fmt.Bshift = 0;
53 fmt.Rmask = 0xff << 16;
54 fmt.Gmask = 0xff << 8;
55 fmt.Bmask = 0xff;
56 converted = SDL_ConvertSurface( surface, &fmt, 0 );
57 }
58
59 switch( surface->format->BitsPerPixel )
60 {
61 case 32:
62 *format = mlt_image_rgb24a;
63 image_size = *width * *height * 4;
64 *image = mlt_pool_alloc( image_size );
65 memcpy( *image, surface->pixels, image_size );
66 break;
67 default:
68 *format = mlt_image_rgb24;
69 *image = mlt_pool_alloc( image_size );
70 memcpy( *image, surface->pixels, image_size );
71 break;
72 }
73
74 if ( converted )
75 SDL_FreeSurface( converted );
76
77 // Update the frame
78 mlt_frame_set_image( frame, *image, image_size, mlt_pool_release );
79
80 return 0;
81 }
82
filter_files(const struct dirent * de)83 static int filter_files( const struct dirent *de )
84 {
85 return de->d_name[ 0 ] != '.';
86 }
87
parse_file_names(char * resource)88 static mlt_properties parse_file_names( char *resource )
89 {
90 mlt_properties properties = mlt_properties_new( );
91
92 if ( strstr( resource, "/.all." ) != NULL )
93 {
94 char *dir_name = strdup( resource );
95 char *extension = strrchr( resource, '.' );
96 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
97 char fullname[ 1024 ];
98 strcpy( fullname, dir_name );
99 struct dirent **de = NULL;
100 int n = scandir( fullname, &de, filter_files, alphasort );
101 int i;
102 struct stat info;
103
104 for (i = 0; i < n; i++ )
105 {
106 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
107 if ( strstr( fullname, extension ) && lstat( fullname, &info ) == 0 &&
108 ( S_ISREG( info.st_mode ) || info.st_mode | S_IXUSR ) )
109 {
110 char temp[ 20 ];
111 sprintf( temp, "%d", i );
112 mlt_properties_set( properties, temp, fullname );
113 }
114 free( de[ i ] );
115 }
116
117 free( de );
118 free( dir_name );
119 }
120 else
121 {
122 mlt_properties_set( properties, "0", resource );
123 }
124
125 return properties;
126 }
127
load_image(mlt_producer producer)128 static SDL_Surface *load_image( mlt_producer producer )
129 {
130 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
131 char *resource = mlt_properties_get( properties, "resource" );
132 char *last_resource = mlt_properties_get( properties, "_last_resource" );
133 int image_idx = 0;
134 char *this_resource = NULL;
135 double ttl = mlt_properties_get_int( properties, "ttl" );
136 mlt_position position = mlt_producer_position( producer );
137 SDL_Surface *surface = mlt_properties_get_data( properties, "_surface", NULL );
138 mlt_properties filenames = mlt_properties_get_data( properties, "_filenames", NULL );
139
140 if ( filenames == NULL )
141 {
142 filenames = parse_file_names( resource );
143 mlt_properties_set_data( properties, "_filenames", filenames, 0, ( mlt_destructor )mlt_properties_close, 0 );
144 mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
145 }
146
147 if ( mlt_properties_count( filenames ) )
148 {
149 image_idx = ( int )floor( ( double )position / ttl ) % mlt_properties_count( filenames );
150 this_resource = mlt_properties_get_value( filenames, image_idx );
151
152 if ( surface == NULL || last_resource == NULL || strcmp( last_resource, this_resource ) )
153 {
154 surface = IMG_Load( this_resource );
155 if ( surface != NULL )
156 {
157 surface->refcount ++;
158 mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
159 mlt_properties_set( properties, "_last_resource", this_resource );
160 mlt_properties_set_int( properties, "meta.media.width", surface->w );
161 mlt_properties_set_int( properties, "meta.media.height", surface->h );
162 }
163 }
164 else if ( surface != NULL )
165 {
166 surface->refcount ++;
167 }
168 }
169
170 return surface;
171 }
172
producer_get_frame(mlt_producer producer,mlt_frame_ptr frame,int index)173 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
174 {
175 // Generate a frame
176 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
177
178 if ( *frame != NULL )
179 {
180 // Create the surface for the current image
181 SDL_Surface *surface = load_image( producer );
182
183 if ( surface != NULL )
184 {
185 // Obtain properties of frame and producer
186 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
187
188 // Obtain properties of producer
189 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
190
191 // Update timecode on the frame we're creating
192 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
193
194 // Set producer-specific frame properties
195 mlt_properties_set_int( properties, "progressive", 1 );
196 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
197 mlt_properties_set_data( properties, "surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, NULL );
198
199 // Push the get_image method
200 mlt_frame_push_get_image( *frame, producer_get_image );
201 }
202 }
203
204 // Calculate the next timecode
205 mlt_producer_prepare_next( producer );
206
207 return 0;
208 }
209
producer_close(mlt_producer producer)210 static void producer_close( mlt_producer producer )
211 {
212 producer->close = NULL;
213 mlt_producer_close( producer );
214 free( producer );
215 }
216
producer_sdl_image_init(mlt_profile profile,mlt_service_type type,const char * id,char * file)217 mlt_producer producer_sdl_image_init( mlt_profile profile, mlt_service_type type, const char *id, char *file )
218 {
219 mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
220 if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
221 {
222 // Get the properties interface
223 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
224
225 // Callback registration
226 producer->get_frame = producer_get_frame;
227 producer->close = ( mlt_destructor )producer_close;
228
229 // Set the default properties
230 mlt_properties_set( properties, "resource", file );
231 mlt_properties_set( properties, "_resource", "" );
232 mlt_properties_set_double( properties, "aspect_ratio", 1 );
233 mlt_properties_set_int( properties, "ttl", 25 );
234 mlt_properties_set_int( properties, "progressive", 1 );
235
236 // Validate the resource
237 SDL_Surface *surface = NULL;
238 if ( file && ( surface = load_image( producer ) ) )
239 {
240 SDL_FreeSurface( surface );
241 mlt_properties_set_data( properties, "_surface", NULL, 0, NULL, NULL );
242 }
243 else
244 {
245 producer_close( producer );
246 producer = NULL;
247 }
248 return producer;
249 }
250 free( producer );
251 return NULL;
252 }
253
254
255