d1c13f97f9462247fe939745303482500480bcb1
[melted] / 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 program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser 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 "producer_sdl_image.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 uint8_t *alpha;
41
42 *width = surface->w;
43 *height = surface->h;
44 *format = mlt_image_yuv422;
45 *image = mlt_pool_alloc( *width * *height * 2 );
46 alpha = mlt_pool_alloc( *width * *height );
47
48 switch( surface->format->BitsPerPixel )
49 {
50 case 32:
51 mlt_convert_rgb24a_to_yuv422( surface->pixels, *width, *height, surface->pitch, *image, alpha );
52 break;
53 case 24:
54 mlt_convert_rgb24_to_yuv422( surface->pixels, *width, *height, surface->pitch, *image );
55 memset( alpha, 255, *width * *height );
56 break;
57 default:
58 break;
59 }
60
61 // Update the frame
62 mlt_properties_set_data( properties, "image", *image, *width * *height * 2, mlt_pool_release, NULL );
63 mlt_properties_set_data( properties, "alpha", alpha, *width * *height, mlt_pool_release, NULL );
64 mlt_properties_set_int( properties, "width", *width );
65 mlt_properties_set_int( properties, "height", *height );
66
67 return 0;
68 }
69
70 static int filter_files( const struct dirent *de )
71 {
72 return de->d_name[ 0 ] != '.';
73 }
74
75 static mlt_properties parse_file_names( char *resource )
76 {
77 mlt_properties properties = mlt_properties_new( );
78
79 if ( strstr( resource, "/.all." ) != NULL )
80 {
81 char *dir_name = strdup( resource );
82 char *extension = strrchr( resource, '.' );
83 *( strstr( dir_name, "/.all." ) + 1 ) = '\0';
84 char fullname[ 1024 ];
85 strcpy( fullname, dir_name );
86 struct dirent **de = NULL;
87 int n = scandir( fullname, &de, filter_files, alphasort );
88 int i;
89 struct stat info;
90
91 for (i = 0; i < n; i++ )
92 {
93 snprintf( fullname, 1023, "%s%s", dir_name, de[i]->d_name );
94 if ( strstr( fullname, extension ) && lstat( fullname, &info ) == 0 &&
95 ( S_ISREG( info.st_mode ) || info.st_mode | S_IXUSR ) )
96 {
97 char temp[ 20 ];
98 sprintf( temp, "%d", i );
99 mlt_properties_set( properties, temp, fullname );
100 }
101 free( de[ i ] );
102 }
103
104 free( de );
105 free( dir_name );
106 }
107 else
108 {
109 mlt_properties_set( properties, "0", resource );
110 }
111
112 return properties;
113 }
114
115 static SDL_Surface *load_image( mlt_producer producer )
116 {
117 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
118 char *resource = mlt_properties_get( properties, "resource" );
119 char *last_resource = mlt_properties_get( properties, "_last_resource" );
120 int image_idx = 0;
121 char *this_resource = NULL;
122 double ttl = mlt_properties_get_int( properties, "ttl" );
123 mlt_position position = mlt_producer_position( producer );
124 SDL_Surface *surface = mlt_properties_get_data( properties, "_surface", NULL );
125 mlt_properties filenames = mlt_properties_get_data( properties, "_filenames", NULL );
126
127 if ( filenames == NULL )
128 {
129 filenames = parse_file_names( resource );
130 mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
131 }
132
133 image_idx = ( int )floor( ( double )position / ttl ) % mlt_properties_count( filenames );
134 this_resource = mlt_properties_get_value( filenames, image_idx );
135
136 if ( last_resource == NULL || strcmp( last_resource, this_resource ) )
137 {
138 surface = IMG_Load( this_resource );
139 if ( surface != NULL )
140 {
141 surface->refcount ++;
142 mlt_properties_set_data( properties, "_surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, 0 );
143 mlt_properties_set( properties, "_last_resource", this_resource );
144 }
145 }
146 else if ( surface != NULL )
147 {
148 surface->refcount ++;
149 }
150
151 return surface;
152 }
153
154 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
155 {
156 // Generate a frame
157 *frame = mlt_frame_init( );
158
159 if ( *frame != NULL )
160 {
161 // Create the surface for the current image
162 SDL_Surface *surface = load_image( producer );
163
164 if ( surface != NULL )
165 {
166 // Obtain properties of frame and producer
167 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
168
169 // Obtain properties of producer
170 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
171
172 // Update timecode on the frame we're creating
173 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
174
175 // Set producer-specific frame properties
176 mlt_properties_set_int( properties, "progressive", 1 );
177 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
178 mlt_properties_set_data( properties, "surface", surface, 0, ( mlt_destructor )SDL_FreeSurface, NULL );
179 mlt_properties_set_int( properties, "real_width", surface->w );
180 mlt_properties_set_int( properties, "real_height", surface->h );
181
182 // Push the get_image method
183 mlt_frame_push_get_image( *frame, producer_get_image );
184 }
185 }
186
187 // Calculate the next timecode
188 mlt_producer_prepare_next( producer );
189
190 return 0;
191 }
192
193 static void producer_close( mlt_producer producer )
194 {
195 producer->close = NULL;
196 mlt_producer_close( producer );
197 free( producer );
198 }
199
200 mlt_producer producer_sdl_image_init( char *file )
201 {
202 mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
203 if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
204 {
205 // Get the properties interface
206 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
207
208 // Callback registration
209 producer->get_frame = producer_get_frame;
210 producer->close = ( mlt_destructor )producer_close;
211
212 // Set the default properties
213 mlt_properties_set( properties, "resource", file );
214 mlt_properties_set( properties, "_resource", "" );
215 mlt_properties_set_double( properties, "aspect_ratio", 1 );
216 mlt_properties_set_int( properties, "ttl", 25 );
217 mlt_properties_set_int( properties, "progressive", 1 );
218
219 return producer;
220 }
221 free( producer );
222 return NULL;
223 }
224
225