add video_standard enum to mlt_frame, add mlt_consumer_properties, add properties...
[melted] / src / modules / gtk2 / producer_pixbuf.c
1 /*
2 * producer_pixbuf.c -- raster image loader based upon gdk-pixbuf
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Dan Dennedy <dan@dennedy.org>
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 "producer_pixbuf.h"
22 #include <framework/mlt_frame.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <gdk-pixbuf/gdk-pixbuf.h>
27
28 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
29 static void producer_close( mlt_producer parent );
30
31 typedef enum
32 {
33 SIGNAL_FORMAT_PAL,
34 SIGNAL_FORMAT_NTSC
35 } mlt_signal_format;
36
37 mlt_producer producer_pixbuf_init( const char *filename )
38 {
39 producer_pixbuf this = calloc( sizeof( struct producer_pixbuf_s ), 1 );
40 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
41 {
42 mlt_producer producer = &this->parent;
43
44 producer->get_frame = producer_get_frame;
45 producer->close = producer_close;
46
47 this->filename = strdup( filename );
48 this->counter = -1;
49
50 // Get the properties interface
51 mlt_properties properties = mlt_producer_properties( &this->parent );
52
53 // Set the default properties
54 mlt_properties_set_int( properties, "video_standard", mlt_video_standard_pal );
55
56 g_type_init();
57
58 return producer;
59 }
60 free( this );
61 return NULL;
62 }
63
64 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
65 {
66 // Obtain properties of frame
67 mlt_properties properties = mlt_frame_properties( this );
68
69 // May need to know the size of the image to clone it
70 int size = 0;
71
72 // Get the image
73 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
74
75 // Get width and height
76 *width = mlt_properties_get_int( properties, "width" );
77 *height = mlt_properties_get_int( properties, "height" );
78
79 // Clone if necessary
80 if ( writable )
81 {
82 // Clone our image
83 uint8_t *copy = malloc( size );
84 memcpy( copy, image, size );
85
86 // We're going to pass the copy on
87 image = copy;
88
89 // Now update properties so we free the copy after
90 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
91 }
92
93 // Pass on the image
94 *buffer = image;
95
96 return 0;
97 }
98
99 static uint8_t *producer_get_alpha_mask( mlt_frame this )
100 {
101 // Obtain properties of frame
102 mlt_properties properties = mlt_frame_properties( this );
103
104 // Return the alpha mask
105 return mlt_properties_get_data( properties, "alpha", NULL );
106 }
107
108 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
109 {
110 producer_pixbuf this = producer->child;
111 GdkPixbuf *pixbuf = NULL;
112 GError *error = NULL;
113
114 // Generate a frame
115 *frame = mlt_frame_init( );
116
117 // Obtain properties of frame
118 mlt_properties properties = mlt_frame_properties( *frame );
119
120 // optimization for subsequent iterations on single picture
121 if ( this->image != NULL )
122 {
123 // Set width/height
124 mlt_properties_set_int( properties, "width", this->width );
125 mlt_properties_set_int( properties, "height", this->height );
126
127 // if picture sequence pass the image and alpha data without destructor
128 mlt_properties_set_data( properties, "image", this->image, 0, NULL, NULL );
129 mlt_properties_set_data( properties, "alpha", this->alpha, 0, NULL, NULL );
130
131 // Set alpha mask call back
132 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
133
134 // Stack the get image callback
135 mlt_frame_push_get_image( *frame, producer_get_image );
136
137 }
138 else if ( strchr( this->filename, '%' ) != NULL )
139 {
140 // handle picture sequences
141 char filename[1024];
142 filename[1023] = 0;
143 int current = this->counter;
144 do
145 {
146 ++this->counter;
147 snprintf( filename, 1023, this->filename, this->counter );
148 pixbuf = gdk_pixbuf_new_from_file( filename, &error );
149 // allow discontinuity in frame numbers up to 99
150 error = NULL;
151 } while ( pixbuf == NULL && ( this->counter - current ) < 100 );
152 }
153 else
154 {
155 pixbuf = gdk_pixbuf_new_from_file( this->filename, &error );
156 }
157
158 // If we have a pixbuf
159 if ( pixbuf )
160 {
161 // Scale to adjust for sample aspect ratio
162 if ( mlt_properties_get_int( properties, "video_stadnard" ) == mlt_video_standard_pal )
163 {
164 GdkPixbuf *temp = pixbuf;
165 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
166 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 54.0/59.0),
167 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
168 pixbuf = scaled;
169 g_object_unref( temp );
170 }
171 else
172 {
173 GdkPixbuf *temp = pixbuf;
174 GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
175 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 11.0/10.0 ),
176 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
177 pixbuf = scaled;
178 g_object_unref( temp );
179 }
180
181 // Store width and height
182 this->width = gdk_pixbuf_get_width( pixbuf );
183 this->height = gdk_pixbuf_get_height( pixbuf );
184
185 // Allocate/define image and alpha
186 uint8_t *image = malloc( this->width * this->height * 2 );
187 uint8_t *alpha = NULL;
188
189 // Extract YUV422 and alpha
190 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
191 {
192 // Allocate the alpha mask
193 alpha = malloc( this->width * this->height );
194
195 // Convert the image
196 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
197 this->width, this->height,
198 gdk_pixbuf_get_rowstride( pixbuf ),
199 image, alpha );
200 }
201 else
202 {
203 // No alpha to extract
204 mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
205 this->width, this->height,
206 gdk_pixbuf_get_rowstride( pixbuf ),
207 image );
208 }
209
210 // Finished with pixbuf now
211 g_object_unref( pixbuf );
212
213 // Set width/height of frame
214 mlt_properties_set_int( properties, "width", this->width );
215 mlt_properties_set_int( properties, "height", this->height );
216
217 // Pass alpha and image on properties with or without destructor
218 if ( this->counter >= 0 )
219 {
220 // if picture sequence pass the image and alpha data with destructor
221 mlt_properties_set_data( properties, "image", image, 0, free, NULL );
222 mlt_properties_set_data( properties, "alpha", alpha, 0, free, NULL );
223 }
224 else
225 {
226 // if single picture, reference the image and alpha in the producer
227 this->image = image;
228 this->alpha = alpha;
229
230 // pass the image and alpha data without destructor
231 mlt_properties_set_data( properties, "image", image, 0, NULL, NULL );
232 mlt_properties_set_data( properties, "alpha", alpha, 0, NULL, NULL );
233 }
234
235 // Set alpha call back
236 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
237
238 // Push the get_image method
239 mlt_frame_push_get_image( *frame, producer_get_image );
240 }
241
242 // Update timecode on the frame we're creating
243 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
244
245 // Calculate the next timecode
246 mlt_producer_prepare_next( producer );
247
248 return 0;
249 }
250
251 static void producer_close( mlt_producer parent )
252 {
253 producer_pixbuf this = parent->child;
254 if ( this->filename )
255 free( this->filename );
256 if ( this->image )
257 free( this->image );
258 if ( this->alpha )
259 free( this->alpha );
260 parent->close = NULL;
261 mlt_producer_close( parent );
262 free( this );
263 }
264