]> git.sesse.net Git - mlt/blob - src/modules/swfdec/producer_swfdec.c
replace legacy real_width and _height with meta.media.width and .height
[mlt] / src / modules / swfdec / producer_swfdec.c
1 /*
2  * producer_swfdec.c -- swfdec producer for Flash files
3  * Copyright (C) 2010 Dan Dennedy <dan@dennedy.org>
4  *
5  * swfdec 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  * swfdec 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 swfdec library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <framework/mlt.h>
21 #include <swfdec/swfdec.h>
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <math.h>
27 #include <limits.h>
28
29 typedef struct
30 {
31         struct mlt_producer_s parent;
32     SwfdecPlayer *player;
33     SwfdecURL *url;
34     cairo_surface_t *surface;
35     cairo_t *cairo;
36     mlt_position last_position;
37         guint width;
38         guint height;
39 } *producer_swfdec;
40
41 void swfdec_open( producer_swfdec swfdec, mlt_profile profile )
42 {
43         mlt_properties properties = MLT_PRODUCER_PROPERTIES( &swfdec->parent );
44
45         // Setup the swfdec player
46         swfdec->player = swfdec_player_new( NULL );
47         if ( mlt_properties_get( properties, "variables") )
48                 swfdec_player_set_variables( swfdec->player, mlt_properties_get( properties, "variables" ) );
49         swfdec_player_set_url( swfdec->player, swfdec->url );
50         swfdec_player_set_maximum_runtime( swfdec->player, 10000 );
51
52         // Setup size
53         swfdec_player_get_default_size( swfdec->player, &swfdec->width, &swfdec->height );
54         if ( swfdec->width == 0 || swfdec->height == 0 )
55         {
56                 swfdec_player_set_size( swfdec->player, profile->width, profile->height );
57                 swfdec->width = profile->width;
58                 swfdec->height = profile->height;
59         }
60
61         // Setup scaling
62         double scale = 1.0;
63         if ( swfdec->width > 2 * swfdec->height )
64                 scale = 0.5 * profile->width / swfdec->height;
65         else if ( swfdec->height > 2 * swfdec->width )
66                 scale = 0.5 * profile->height / swfdec->width;
67         else
68                 scale = (double) profile->width / MAX( swfdec->width, swfdec->height );
69         swfdec->width = ceil( scale * swfdec->width );
70         swfdec->height = ceil( scale * swfdec->height );
71
72         // Compute the centering translation
73         double x = swfdec->width > profile->width ? (swfdec->width - profile->width) / 2 : 0;
74         double y = swfdec->height > profile->height ? (swfdec->height - profile->height) / 2 : 0;
75
76         // Setup cairo
77         swfdec->surface = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, MIN(profile->width, swfdec->width), MIN(profile->height, swfdec->height) );
78         swfdec->cairo = cairo_create( swfdec->surface );
79         cairo_translate( swfdec->cairo, -x, -y );
80         cairo_scale( swfdec->cairo, scale, scale );
81 }
82
83 void swfdec_close( producer_swfdec swfdec )
84 {
85         if ( swfdec->cairo )
86                 cairo_destroy( swfdec->cairo );
87         swfdec->cairo = NULL;
88         if ( swfdec->player )
89                 g_object_unref( swfdec->player );
90         swfdec->player = NULL;
91         if ( swfdec->surface )
92                 cairo_surface_destroy( swfdec->surface );
93         swfdec->surface = NULL;
94 }
95
96 // Cairo uses 32 bit native endian ARGB
97 static void bgra_to_rgba( uint8_t *src, uint8_t* dst, int width, int height )
98 {
99         int n = width * height + 1;
100
101         while ( --n )
102         {
103                 *dst++ = src[2];
104                 *dst++ = src[1];
105                 *dst++ = src[0];
106                 *dst++ = src[3];
107                 src += 4;
108         }       
109 }
110
111 static int get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
112 {
113         producer_swfdec swfdec = mlt_frame_pop_service( frame );
114         mlt_service service = MLT_PRODUCER_SERVICE( &swfdec->parent );
115         mlt_profile profile = mlt_service_profile( service );
116         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
117
118         mlt_service_lock( service );
119         
120         if ( !swfdec->player )
121                 swfdec_open( swfdec, profile );
122
123         // Set width and height
124         *width = swfdec->width;
125         *height = swfdec->height;
126         *format = mlt_image_rgb24a;
127
128         *buffer = mlt_pool_alloc( *width * ( *height + 1 ) * 4 );
129         mlt_frame_set_image( frame, *buffer, *width * ( *height + 1 ) * 4, mlt_pool_release );
130
131         // Seek
132         mlt_position pos = mlt_properties_get_position( properties, "swfdec.position" );
133         if ( pos > swfdec->last_position )
134         {
135                 gulong msec = 1000UL * ( pos - swfdec->last_position ) * profile->frame_rate_den / profile->frame_rate_num;
136                 while ( msec > 0 )
137                         msec -= swfdec_player_advance( swfdec->player, msec );
138         }
139         else if ( pos < swfdec->last_position )
140         {
141                 swfdec_close( swfdec );
142                 swfdec_open( swfdec, mlt_service_profile( service ) );
143                 gulong msec = 1000UL * pos * profile->frame_rate_den / profile->frame_rate_num;
144                 while ( msec > 0 )
145                         msec -= swfdec_player_advance( swfdec->player, msec );
146         }
147         swfdec->last_position = pos;
148
149         // Render
150         cairo_save( swfdec->cairo );
151         //cairo_set_source_rgba( swfdec->cairo, r, g, b, a );
152         cairo_set_operator( swfdec->cairo, CAIRO_OPERATOR_CLEAR );
153         cairo_paint( swfdec->cairo );
154         cairo_restore( swfdec->cairo );
155         swfdec_player_render( swfdec->player, swfdec->cairo );
156
157         // Get image from surface
158         uint8_t *image = cairo_image_surface_get_data( swfdec->surface );
159         
160         mlt_service_unlock( service );
161         
162         // Convert to RGBA
163         bgra_to_rgba( image, *buffer, swfdec->width, swfdec->height );
164
165         return 0;
166 }
167
168 static int get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
169 {
170         // Access the private data
171         producer_swfdec swfdec = producer->child;
172
173         // Create an empty frame
174         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
175
176         // Get the frames properties
177         mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
178
179         // Update other info on the frame
180         mlt_properties_set_int( properties, "test_image", 0 );
181         mlt_properties_set_int( properties, "width", swfdec->width );
182         mlt_properties_set_int( properties, "height", swfdec->height );
183         mlt_properties_set_int( properties, "progressive", 1 );
184         mlt_properties_set_double( properties, "aspect_ratio", 1.0 );
185         mlt_properties_set_position( properties, "swfdec.position", mlt_producer_frame( producer ) );
186
187         // Push the get_image method on to the stack
188         mlt_frame_push_service( *frame, swfdec );
189         mlt_frame_push_get_image( *frame, get_image );
190         
191         // Update timecode on the frame we're creating
192         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
193
194         // Calculate the next timecode
195         mlt_producer_prepare_next( producer );
196
197         return 0;
198 }
199
200 static void producer_close( mlt_producer parent )
201 {
202         // Obtain swfdec
203         producer_swfdec swfdec = parent->child;
204
205         // Close the file
206         swfdec_close( swfdec );
207         if ( swfdec->url )
208                 swfdec_url_free( swfdec->url );
209
210         // Close the parent
211         parent->close = NULL;
212         mlt_producer_close( parent );
213
214         // Free the memory
215         free( swfdec );
216 }
217
218 mlt_producer producer_swfdec_init( mlt_profile profile, mlt_service_type type, const char *id, char *filename )
219 {
220         if ( !filename ) return NULL;
221         producer_swfdec swfdec = calloc( 1, sizeof( *swfdec ) );
222         mlt_producer producer = NULL;
223
224         if ( swfdec && mlt_producer_init( &swfdec->parent, swfdec ) == 0 )
225         {
226                 // Initialize swfdec and try to open the file
227                 swfdec->url = swfdec_url_new_from_input( filename );
228                 if ( swfdec->url )
229                 {
230                         // Set the return value
231                         producer = &swfdec->parent;
232
233                         // Set the resource property (required for all producers)
234                         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
235                         mlt_properties_set( properties, "resource", filename );
236
237                         // Set the callbacks
238                         producer->close = (mlt_destructor) producer_close;
239                         producer->get_frame = get_frame;
240
241                         // Set the meta media attributes
242                         swfdec->width = profile->width;
243                         swfdec->height = profile->height;
244                         mlt_properties_set_int( properties, "meta.media.nb_streams", 1 );
245                         mlt_properties_set( properties, "meta.media.0.stream.type", "video" );
246                         mlt_properties_set( properties, "meta.media.0.codec.name", "swf" );
247                         mlt_properties_set( properties, "meta.media.0.codec.long_name", "Adobe Flash" );
248                         mlt_properties_set( properties, "meta.media.0.codec.pix_fmt", "bgra" );
249                         mlt_properties_set_int( properties, "meta.media.width", profile->width );
250                         mlt_properties_set_int( properties, "meta.media.height", profile->height );
251                         mlt_properties_set_double( properties, "meta.media.sample_aspect_num", 1.0 );
252                         mlt_properties_set_double( properties, "meta.media.sample_aspect_den", 1.0 );
253                         mlt_properties_set_int( properties, "meta.media.frame_rate_num", profile->frame_rate_num );
254                         mlt_properties_set_int( properties, "meta.media.frame_rate_den", profile->frame_rate_den );
255                         mlt_properties_set_int( properties, "meta.media.progressive", 1 );
256                 }
257                 else
258                 {
259                         g_object_unref( swfdec->player );
260                         mlt_producer_close( &swfdec->parent );
261                         free( swfdec );
262                 }
263         }
264         else
265         {
266                 free( swfdec );
267         }
268
269         return producer;
270 }
271
272 static mlt_properties metadata( mlt_service_type type, const char *id, void *data )
273 {
274         char file[ PATH_MAX ];
275         snprintf( file, PATH_MAX, "%s/swfdec/%s", mlt_environment( "MLT_DATA" ), (char*) data );
276         return mlt_properties_parse_yaml( file );
277 }
278
279 MLT_REPOSITORY
280 {
281         swfdec_init();
282         MLT_REGISTER( producer_type, "swfdec", producer_swfdec_init );
283         MLT_REGISTER_METADATA( producer_type, "swfdec", metadata, "producer_swfdec.yml" );
284 }