remove debug msg
[melted] / src / modules / core / producer_framebuffer.c
1 /*
2 * producer_framebuffer.c -- create subspeed frames
3 * Author: Jean-Baptiste Mardelle, based on the code of motion_est by Zachary Drew
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #include "producer_framebuffer.h"
21 #include <framework/mlt.h>
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
27 #include <sys/time.h>
28 #include <assert.h>
29
30 // Image stack(able) method
31 static int framebuffer_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
32 {
33
34 // Get the filter object and properties
35 mlt_producer producer = mlt_frame_pop_service( this );
36 mlt_frame first_frame = mlt_frame_pop_service( this );
37
38 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
39
40
41 // Frame properties objects
42 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( this );
43 mlt_properties first_frame_properties = MLT_FRAME_PROPERTIES( first_frame );
44
45 // image stride
46 int size, xstride, ystride;
47 switch( *format ){
48 case mlt_image_yuv422:
49 size = *width * *height * 2;
50 xstride = 2;
51 ystride = 2 * *width;
52 break;
53 default:
54 fprintf(stderr, "Unsupported image format\n");
55 return -1;
56 }
57
58 uint8_t *output = mlt_properties_get_data( producer_properties, "output_buffer", 0 );
59 if( output == NULL )
60 {
61 output = mlt_pool_alloc( size );
62
63 // Let someone else clean up
64 mlt_properties_set_data( producer_properties, "output_buffer", output, size, mlt_pool_release, NULL );
65 }
66
67 uint8_t *first_image = mlt_properties_get_data( first_frame_properties, "image", NULL );
68
69 // which frames are buffered?
70
71 int error = 0;
72
73 if( first_image == NULL )
74 {
75 error = mlt_frame_get_image( first_frame, &first_image, format, width, height, writable );
76
77 if( error != 0 ) {
78 fprintf(stderr, "first_image == NULL get image died\n");
79 return error;
80 }
81 }
82
83 // Start with a base image
84 memcpy( output, first_image, size );
85
86 *image = output;
87 mlt_properties_set_data( frame_properties, "image", output, size, NULL, NULL );
88
89 // Make sure that no further scaling is done
90 mlt_properties_set( frame_properties, "rescale.interps", "none" );
91 mlt_properties_set( frame_properties, "scale", "off" );
92
93 mlt_frame_close( first_frame );
94
95 return 0;
96 }
97
98 static int framebuffer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
99 {
100 // Construct a new frame
101 *frame = mlt_frame_init( );
102 mlt_properties properties = MLT_PRODUCER_PROPERTIES(this);
103
104 if( frame != NULL )
105 {
106 mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
107
108 mlt_position first_position = (first_frame != NULL) ? mlt_frame_get_position( first_frame ) : -1;
109
110 // Get the real producer
111 mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
112
113 // Our "in" needs to be the same, keep it so
114 mlt_properties_pass_list( MLT_PRODUCER_PROPERTIES( real_producer ), properties, "in" );
115
116 // get properties
117 int strobe = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES (this), "strobe");
118 double freeze = mlt_properties_get_double( MLT_PRODUCER_PROPERTIES (this), "freeze");
119 int freeze_after = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES (this), "freeze_after");
120 int freeze_before = mlt_properties_get_int( MLT_PRODUCER_PROPERTIES (this), "freeze_before");
121
122 mlt_position need_first;
123
124 if (!freeze || freeze_after || freeze_before) {
125 double prod_speed = mlt_properties_get_double( properties, "_speed");
126 double prod_end_speed = mlt_properties_get_double( properties, "end_speed");
127
128 // calculate actual speed and position
129 double actual_speed = prod_speed + ((double)mlt_producer_position( this ) / (double)mlt_producer_get_length(this)) * (prod_end_speed - prod_speed);
130 double actual_position = actual_speed * (double)mlt_producer_position( this );
131 if (mlt_properties_get_int( properties, "reverse")) actual_position = mlt_producer_get_length(this) - actual_position;
132
133 if (strobe < 2)
134 {
135 need_first = floor( actual_position );
136 }
137 else
138 {
139 // Strobe effect wanted, calculate frame position
140 need_first = floor( actual_position );
141 need_first -= need_first%strobe;
142 }
143 if (freeze)
144 {
145 if (freeze_after && need_first > freeze) need_first = freeze;
146 else if (freeze_before && need_first < freeze) need_first = freeze;
147 }
148 }
149 else need_first = freeze;
150
151 if( need_first != first_position )
152 {
153 mlt_frame_close( first_frame );
154 first_position = -1;
155 first_frame = NULL;
156 }
157
158 if( first_frame == NULL )
159 {
160 // Seek the producer to the correct place
161 mlt_producer_seek( real_producer, need_first );
162
163 // Get the frame
164 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
165 }
166
167
168 // Make sure things are in their place
169 mlt_properties_set_data( properties, "first_frame", first_frame, 0, NULL, NULL );
170
171 // Stack the producer and producer's get image
172 mlt_frame_push_service( *frame, first_frame );
173 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( first_frame ) );
174
175 mlt_frame_push_service( *frame, this );
176 mlt_frame_push_service( *frame, framebuffer_get_image );
177
178 // Give the returned frame temporal identity
179 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
180
181 }
182
183 return 0;
184 }
185
186
187 mlt_producer producer_framebuffer_init( char *arg )
188 {
189 mlt_producer this = mlt_producer_new( );
190
191 // Wrap fezzik
192 mlt_producer real_producer;
193
194 // Check if a speed was specified.
195 /**
196
197 * Speed must be appended to the filename with ':'. To play your video at 50%:
198 inigo framebuffer:my_video.mpg:0.5
199
200 * You can have a variabl speed by specifying a start and an end speed:
201 inigo framebuffer:my_video.mpg:0.5:1.0
202
203 * Stroboscope effect can be obtained by adding a stobe=x parameter, where
204 x is the number of frames that will be ignored.
205
206 * You can play the movie backwards by adding reverse=1
207
208 * You can freeze the clip at a determined position by adding freeze=frame_pos
209 add freeze_after=1 to freeze only paste position or freeze_before to freeze before it
210
211 **/
212
213 double speed;
214 double end_speed;
215 int count;
216 char *props = strdup( arg );
217 char *ptr = props;
218 count = strcspn( ptr, ":" );
219 ptr[count] = '\0';
220 real_producer = mlt_factory_producer( "fezzik", ptr );
221
222 ptr += count + 1;
223 ptr += strspn( ptr, ":" );
224 count = strcspn( ptr, ":" );
225 ptr[count] = '\0';
226 speed = atof(ptr);
227
228 ptr += count + 1;
229 ptr += strspn( ptr, ":" );
230 count = strcspn( ptr, ":" );
231 ptr[count] = '\0';
232 end_speed = atof(ptr);
233 free( props );
234
235 // If no end speed specified, use constant speed
236 if (speed == 0.0) speed = 1.0;
237 if (end_speed == 0.0) end_speed = speed;
238
239
240 if ( this != NULL && real_producer != NULL)
241 {
242 // Get the properties of this producer
243 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
244
245 // Fezzik normalised it for us already
246 mlt_properties_set_int( properties, "fezzik_normalised", 1);
247
248 // Store the producer and fitler
249 mlt_properties_set_data( properties, "producer", real_producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
250
251 // Grap some stuff from the real_producer
252 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ),
253 "in, out, length, resource" );
254
255 if (speed != 1.0 || end_speed !=1.0)
256 {
257 // Speed is not 1.0, so adjust the clip length
258 mlt_position real_out = mlt_properties_get_position(properties, "out");
259 mlt_properties_set_position( properties, "out", real_out * 2 / (speed + end_speed));
260 mlt_properties_set_position( properties, "length", real_out * 2 / (speed + end_speed) + 1);
261 mlt_properties_set_double( properties, "_speed", speed);
262 mlt_properties_set_double( properties, "end_speed", end_speed);
263 }
264 else mlt_properties_set_double( properties, "end_speed", 1.0);
265
266 // Since we control the seeking, prevent it from seeking on its own
267 mlt_producer_set_speed( real_producer, 0 );
268 mlt_producer_set_speed( this, speed );
269
270 // Override the get_frame method
271 this->get_frame = framebuffer_get_frame;
272
273 }
274 else
275 {
276 if ( this )
277 mlt_producer_close( this );
278 if ( real_producer )
279 mlt_producer_close( real_producer );
280
281 this = NULL;
282 }
283 return this;
284 }