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
);
50 int size
, xstride
, ystride
;
52 case mlt_image_yuv422
:
53 size
= *width
* *height
* 2;
58 fprintf(stderr
, "Unsupported image format\n");
62 uint8_t *output
= mlt_properties_get_data( producer_properties
, "output_buffer", 0 );
65 output
= mlt_pool_alloc( size
);
67 // Let someone else clean up
68 mlt_properties_set_data( producer_properties
, "output_buffer", output
, size
, mlt_pool_release
, NULL
);
71 uint8_t *first_image
= mlt_properties_get_data( first_frame_properties
, "image", NULL
);
73 // which frames are buffered?
77 if( first_image
== NULL
)
79 error
= mlt_frame_get_image( first_frame
, &first_image
, format
, width
, height
, writable
);
82 fprintf(stderr
, "first_image == NULL get image died\n");
87 // Start with a base image
88 memcpy( output
, first_image
, size
);
91 mlt_properties_set_data( frame_properties
, "image", output
, size
, NULL
, NULL
);
93 // Make sure that no further scaling is done
94 mlt_properties_set( frame_properties
, "rescale.interps", "none" );
95 mlt_properties_set( frame_properties
, "scale", "off" );
97 mlt_frame_close( first_frame
);
102 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
104 // Construct a new frame
105 *frame
= mlt_frame_init( );
106 mlt_properties properties
= MLT_PRODUCER_PROPERTIES(this);
110 mlt_frame first_frame
= mlt_properties_get_data( properties
, "first_frame", NULL
);
112 mlt_position first_position
= (first_frame
!= NULL
) ?
mlt_frame_get_position( first_frame
) : -1;
114 // Get the real producer
115 mlt_producer real_producer
= mlt_properties_get_data( properties
, "producer", NULL
);
118 int strobe
= mlt_properties_get_int( properties
, "strobe");
119 int freeze
= mlt_properties_get_int( properties
, "freeze");
120 int freeze_after
= mlt_properties_get_int( properties
, "freeze_after");
121 int freeze_before
= mlt_properties_get_int( properties
, "freeze_before");
123 mlt_position need_first
;
125 if (!freeze
|| freeze_after
|| freeze_before
) {
126 double prod_speed
= mlt_properties_get_double( properties
, "_speed");
127 double actual_position
= prod_speed
* (double) mlt_producer_position( this );
129 if (mlt_properties_get_int( properties
, "reverse")) actual_position
= mlt_producer_get_playtime(this) - actual_position
;
133 need_first
= floor( actual_position
);
137 // Strobe effect wanted, calculate frame position
138 need_first
= floor( actual_position
);
139 need_first
-= need_first
%strobe
;
143 if (freeze_after
&& need_first
> freeze
) need_first
= freeze
;
144 else if (freeze_before
&& need_first
< freeze
) need_first
= freeze
;
147 else need_first
= freeze
;
149 if( need_first
!= first_position
)
151 mlt_frame_close( first_frame
);
156 if( first_frame
== NULL
)
158 // Seek the producer to the correct place
159 mlt_producer_seek( real_producer
, need_first
);
162 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer
), &first_frame
, index
);
166 // Make sure things are in their place
167 mlt_properties_set_data( properties
, "first_frame", first_frame
, 0, NULL
, NULL
);
169 // Stack the producer and producer's get image
170 mlt_frame_push_service( *frame
, first_frame
);
171 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( first_frame
) );
173 mlt_frame_push_service( *frame
, this );
174 mlt_frame_push_service( *frame
, framebuffer_get_image
);
176 // Give the returned frame temporal identity
177 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
185 mlt_producer
producer_framebuffer_init( char *arg
)
188 mlt_producer
this = NULL
;
189 this = calloc( 1, sizeof( struct mlt_producer_s
) );
190 mlt_producer_init( this, NULL
);
193 mlt_producer real_producer
;
195 // Check if a speed was specified.
198 * Speed must be appended to the filename with ':'. To play your video at 50%:
199 inigo framebuffer:my_video.mpg:0.5
201 * Stroboscope effect can be obtained by adding a stobe=x parameter, where
202 x is the number of frames that will be ignored.
204 * You can play the movie backwards by adding reverse=1
206 * You can freeze the clip at a determined position by adding freeze=frame_pos
207 add freeze_after=1 to freeze only paste position or freeze_before to freeze before it
214 char *props
= strdup( arg
);
216 count
= strcspn( ptr
, ":" );
219 real_producer
= mlt_factory_producer( "fezzik", props
);
222 ptr
+= strspn( ptr
, ":" );
223 count
= strcspn( ptr
, ":" );
228 if (speed
== 0.0) speed
= 1.0;
231 if ( this != NULL
&& real_producer
!= NULL
)
233 // Get the properties of this producer
234 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
236 // Fezzik normalised it for us already
237 mlt_properties_set_int( properties
, "fezzik_normalised", 1);
239 // Store the producer and fitler
240 mlt_properties_set_data( properties
, "producer", real_producer
, 0, ( mlt_destructor
)mlt_producer_close
, NULL
);
242 // Grap some stuff from the real_producer
243 mlt_properties_pass_list( properties
, MLT_PRODUCER_PROPERTIES( real_producer
),
244 "length,resource,width,height" );
248 double real_length
= (double) mlt_producer_get_length( real_producer
);
249 mlt_properties_set_position( properties
, "length", real_length
* 2 / speed
);
252 // Since we control the seeking, prevent it from seeking on its own
253 mlt_producer_set_speed( real_producer
, 0 );
254 mlt_producer_set_speed( this, speed
);
256 // Override the get_frame method
257 this->get_frame
= producer_get_frame
;
262 mlt_producer_close( this );
264 mlt_producer_close( real_producer
);