2 * producer_framebuffer.c -- create subspeed frames
3 * Author: Jean-Baptiste Mardelle, based on the code of motion_est by Zachary Drew
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "producer_framebuffer.h"
21 #include <framework/mlt.h>
30 // Forward references.
31 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
33 /** Image stack(able) method
36 static int framebuffer_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
39 // Get the filter object and properties
40 mlt_producer producer = mlt_frame_pop_service( this );
41 mlt_frame first_frame = mlt_frame_pop_service( this );
43 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
45 // Frame properties objects
46 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( this );
47 mlt_properties first_frame_properties = MLT_FRAME_PROPERTIES( first_frame );
52 case mlt_image_yuv422:
53 size = *width * *height * 2;
56 fprintf(stderr, "Unsupported image format\n");
60 uint8_t *output = mlt_properties_get_data( producer_properties, "output_buffer", NULL );
63 output = mlt_pool_alloc( size );
65 // Let someone else clean up
66 mlt_properties_set_data( producer_properties, "output_buffer", output, size, mlt_pool_release, NULL );
69 uint8_t *first_image = mlt_properties_get_data( first_frame_properties, "image", NULL );
71 // which frames are buffered?
73 mlt_properties_set_double( first_frame_properties, "consumer_aspect_ratio", mlt_properties_get_double(frame_properties, "consumer_aspect_ratio"));
75 if( first_image == NULL )
77 error = mlt_frame_get_image( first_frame, &first_image, format, width, height, writable );
80 fprintf(stderr, "first_image == NULL get image died\n");
84 // Start with a base image
85 memcpy( output, first_image, size );
88 mlt_properties_set_data( frame_properties, "image", output, size, NULL, NULL );
90 // Make sure that no further scaling is done
91 mlt_properties_set( frame_properties, "rescale.interps", "none" );
92 mlt_properties_set( frame_properties, "scale", "off" );
94 mlt_frame_close( first_frame );
99 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
101 // Construct a new frame
102 *frame = mlt_frame_init( );
103 mlt_properties properties = MLT_PRODUCER_PROPERTIES(this);
107 mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
109 mlt_position first_position = (first_frame != NULL) ? mlt_frame_get_position( first_frame ) : -1;
111 // Get the real producer
112 mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
115 int strobe = mlt_properties_get_int( properties, "strobe");
116 int freeze = mlt_properties_get_int( properties, "freeze");
117 int freeze_after = mlt_properties_get_int( properties, "freeze_after");
118 int freeze_before = mlt_properties_get_int( properties, "freeze_before");
120 mlt_position need_first;
122 if (!freeze || freeze_after || freeze_before) {
123 double prod_speed = mlt_properties_get_double( properties, "_speed");
124 double actual_position = prod_speed * (double) mlt_producer_position( this );
126 if (mlt_properties_get_int( properties, "reverse")) actual_position = mlt_producer_get_playtime(this) - actual_position;
130 need_first = floor( actual_position );
134 // Strobe effect wanted, calculate frame position
135 need_first = floor( actual_position );
136 need_first -= need_first%strobe;
140 if (freeze_after && need_first > freeze) need_first = freeze;
141 else if (freeze_before && need_first < freeze) need_first = freeze;
144 else need_first = freeze;
146 if( need_first != first_position )
148 mlt_frame_close( first_frame );
153 if( first_frame == NULL )
155 // Seek the producer to the correct place
156 mlt_producer_seek( real_producer, need_first );
159 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
163 // Make sure things are in their place
164 mlt_properties_set_data( properties, "first_frame", first_frame, 0, NULL, NULL );
166 // Stack the producer and producer's get image
167 mlt_frame_push_service( *frame, first_frame );
168 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( first_frame ) );
170 mlt_frame_push_service( *frame, this );
171 mlt_frame_push_service( *frame, framebuffer_get_image );
173 // Give the returned frame temporal identity
174 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
181 mlt_producer producer_framebuffer_init( char *arg )
184 mlt_producer this = NULL;
185 this = calloc( 1, sizeof( struct mlt_producer_s ) );
186 mlt_producer_init( this, NULL );
189 mlt_producer real_producer;
191 // Check if a speed was specified.
194 * Speed must be appended to the filename with ':'. To play your video at 50%:
195 inigo framebuffer:my_video.mpg:0.5
197 * Stroboscope effect can be obtained by adding a stobe=x parameter, where
198 x is the number of frames that will be ignored.
200 * You can play the movie backwards by adding reverse=1
202 * You can freeze the clip at a determined position by adding freeze=frame_pos
203 add freeze_after=1 to freeze only paste position or freeze_before to freeze before it
210 char *props = strdup( arg );
212 count = strcspn( ptr, ":" );
215 real_producer = mlt_factory_producer( "fezzik", props );
218 ptr += strspn( ptr, ":" );
219 count = strcspn( ptr, ":" );
224 if (speed == 0.0) speed = 1.0;
227 if ( this != NULL && real_producer != NULL)
229 // Get the properties of this producer
230 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
232 // Fezzik normalised it for us already
233 mlt_properties_set_int( properties, "fezzik_normalised", 1);
235 // Store the producer and fitler
236 mlt_properties_set_data( properties, "producer", real_producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
238 // Grap some stuff from the real_producer
239 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ),
240 "length,resource,width,height" );
244 double real_length = (double) mlt_producer_get_length( real_producer );
245 mlt_properties_set_position( properties, "length", real_length * 2 / speed );
248 // Since we control the seeking, prevent it from seeking on its own
249 mlt_producer_set_speed( real_producer, 0 );
250 mlt_producer_set_speed( this, speed );
252 // Override the get_frame method
253 this->get_frame = producer_get_frame;
258 mlt_producer_close( this );
260 mlt_producer_close( real_producer );