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