2 * producer_framebuffer.c -- create subspeed frames
3 * Copyright (C) 2007 Jean-Baptiste Mardelle <jb@ader.ch>
4 * Author: Jean-Baptiste Mardelle, based on the code of motion_est by Zachary Drew
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 int index = ( int )mlt_frame_pop_service( this );
42 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
44 // Frame properties objects
45 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( this );
46 mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
48 // Get producer parameters
49 int strobe = mlt_properties_get_int( properties, "strobe" );
50 int freeze = mlt_properties_get_int( properties, "freeze" );
51 int freeze_after = mlt_properties_get_int( properties, "freeze_after" );
52 int freeze_before = mlt_properties_get_int( properties, "freeze_before" );
54 // Determine the position
55 mlt_position first_position = (first_frame != NULL) ? mlt_frame_get_position( first_frame ) : -1;
56 mlt_position need_first = freeze;
58 if ( !freeze || freeze_after || freeze_before )
60 double prod_speed = mlt_properties_get_double( properties, "_speed" );
61 double actual_position = prod_speed * (double) mlt_producer_position( producer );
63 if ( mlt_properties_get_int( properties, "reverse" ) )
64 actual_position = mlt_producer_get_playtime( producer ) - actual_position;
68 need_first = floor( actual_position );
72 // Strobe effect wanted, calculate frame position
73 need_first = floor( actual_position );
74 need_first -= need_first % strobe;
78 if ( freeze_after && need_first > freeze ) need_first = freeze;
79 else if ( freeze_before && need_first < freeze ) need_first = freeze;
83 // Determine output buffer size
84 *width = mlt_properties_get_int( frame_properties, "width" );
85 *height = mlt_properties_get_int( frame_properties, "height" );
90 case mlt_image_yuv420p:
91 size = *width * 3 * ( *height + 1 ) / 2;
94 size = *width * ( *height + 1 ) * 3;
96 case mlt_image_rgb24a:
97 case mlt_image_opengl:
98 size = *width * ( *height + 1 ) * 4;
101 *format = mlt_image_yuv422;
102 size = *width * ( *height + 1 ) * 2;
109 uint8_t *output = mlt_properties_get_data( properties, "output_buffer", &buffersize );
111 if (output != NULL && buffersize != size)
113 // buffer size changed
115 mlt_properties_set_data( properties, "output_buffer", NULL, 0, NULL, NULL );
117 // invalidate cached frame
121 if ( need_first != first_position )
123 // Bust the cached frame
125 mlt_properties_set_data( properties, "first_frame", NULL, 0, NULL, NULL );
128 // Get the cached frame
129 if ( first_frame == NULL )
131 // Get the frame to cache from the real producer
132 mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
134 // Seek the producer to the correct place
135 mlt_producer_seek( real_producer, need_first );
138 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
141 mlt_properties_set_data( properties, "first_frame", first_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
143 mlt_properties first_frame_properties = MLT_FRAME_PROPERTIES( first_frame );
147 output = mlt_pool_alloc( size );
149 // Let someone else clean up
150 mlt_properties_set_data( properties, "output_buffer", output, size, mlt_pool_release, NULL );
153 // Which frames are buffered?
154 uint8_t *first_image = mlt_properties_get_data( first_frame_properties, "image", NULL );
155 if( first_image == NULL )
157 mlt_properties props = MLT_FRAME_PROPERTIES( this );
158 mlt_properties test_properties = MLT_FRAME_PROPERTIES( first_frame );
159 mlt_properties_set_double( test_properties, "consumer_aspect_ratio", mlt_properties_get_double( props, "consumer_aspect_ratio" ) );
160 mlt_properties_set( test_properties, "rescale.interp", mlt_properties_get( props, "rescale.interp" ) );
162 int error = mlt_frame_get_image( first_frame, &first_image, format, width, height, writable );
165 fprintf(stderr, "first_image == NULL get image died\n");
170 // Start with a base image
171 memcpy( output, first_image, size );
173 // Set the output image
175 mlt_properties_set_data( frame_properties, "image", output, size, NULL, NULL );
177 // Make sure that no further scaling is done
178 mlt_properties_set( frame_properties, "rescale.interps", "none" );
179 mlt_properties_set( frame_properties, "scale", "off" );
184 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
186 // Construct a new frame
187 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
190 // Stack the producer and producer's get image
191 mlt_frame_push_service( *frame, (void*) index );
192 mlt_frame_push_service( *frame, this );
193 mlt_frame_push_service( *frame, framebuffer_get_image );
195 // Give the returned frame temporal identity
196 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
203 mlt_producer producer_framebuffer_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
205 if ( !arg ) return NULL;
206 mlt_producer this = NULL;
207 this = calloc( 1, sizeof( struct mlt_producer_s ) );
208 mlt_producer_init( this, NULL );
211 mlt_producer real_producer;
213 // Check if a speed was specified.
216 * Speed must be appended to the filename with '?'. To play your video at 50%:
217 melt framebuffer:my_video.mpg?0.5
219 * Stroboscope effect can be obtained by adding a stobe=x parameter, where
220 x is the number of frames that will be ignored.
222 * You can play the movie backwards by adding reverse=1
224 * You can freeze the clip at a determined position by adding freeze=frame_pos
225 add freeze_after=1 to freeze only paste position or freeze_before to freeze before it
230 char *props = strdup( arg );
231 char *ptr = strrchr( props, '?' );
235 speed = atof( ptr + 1 );
237 // If speed was valid, then strip it and the delimiter.
238 // Otherwise, an invalid speed probably means this '?' was not a delimiter.
242 real_producer = mlt_factory_producer( profile, NULL, props );
245 if (speed == 0.0) speed = 1.0;
247 if ( this != NULL && real_producer != NULL)
249 // Get the properties of this producer
250 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
252 // The loader normalised it for us already
253 mlt_properties_set_int( properties, "loader_normalised", 1);
254 mlt_properties_set( properties, "resource", arg);
256 // Store the producer and fitler
257 mlt_properties_set_data( properties, "producer", real_producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
259 // Grab some stuff from the real_producer
260 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "length, width,height" );
265 mlt_properties_set_int( properties, "reverse", 1 );
270 double real_length = ( (double) mlt_producer_get_length( real_producer ) ) / speed;
271 mlt_properties_set_position( properties, "length", real_length );
273 mlt_properties_set_position( properties, "out", mlt_producer_get_length( this ) - 1 );
275 // Since we control the seeking, prevent it from seeking on its own
276 mlt_producer_set_speed( real_producer, 0 );
277 mlt_producer_set_speed( this, speed );
279 // Override the get_frame method
280 this->get_frame = producer_get_frame;
285 mlt_producer_close( this );
287 mlt_producer_close( real_producer );