Fix slowmotion producer (no more variable speed, but at least it works now).
[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 // Forward references.
31 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
32
33 /** Image stack(able) method
34 */
35
36 static int framebuffer_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
37 {
38
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 );
42
43 mlt_properties producer_properties = MLT_PRODUCER_PROPERTIES( producer );
44
45 // Frame properties objects
46 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( this );
47 mlt_properties first_frame_properties = MLT_FRAME_PROPERTIES( first_frame );
48
49 // image stride
50 int size, xstride, ystride;
51 switch( *format ){
52 case mlt_image_yuv422:
53 size = *width * *height * 2;
54 xstride = 2;
55 ystride = 2 * *width;
56 break;
57 default:
58 fprintf(stderr, "Unsupported image format\n");
59 return -1;
60 }
61
62 uint8_t *output = mlt_properties_get_data( producer_properties, "output_buffer", 0 );
63 if( output == NULL )
64 {
65 output = mlt_pool_alloc( size );
66
67 // Let someone else clean up
68 mlt_properties_set_data( producer_properties, "output_buffer", output, size, mlt_pool_release, NULL );
69 }
70
71 uint8_t *first_image = mlt_properties_get_data( first_frame_properties, "image", NULL );
72
73 // which frames are buffered?
74
75 int error = 0;
76
77 if( first_image == NULL )
78 {
79 error = mlt_frame_get_image( first_frame, &first_image, format, width, height, writable );
80
81 if( error != 0 ) {
82 fprintf(stderr, "first_image == NULL get image died\n");
83 return error;
84 }
85 }
86
87 // Start with a base image
88 memcpy( output, first_image, size );
89
90 *image = output;
91 mlt_properties_set_data( frame_properties, "image", output, size, NULL, NULL );
92
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" );
96
97 mlt_frame_close( first_frame );
98
99 return 0;
100 }
101
102 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
103 {
104 // Construct a new frame
105 *frame = mlt_frame_init( );
106 mlt_properties properties = MLT_PRODUCER_PROPERTIES(this);
107
108 if( frame != NULL )
109 {
110 mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
111
112 mlt_position first_position = (first_frame != NULL) ? mlt_frame_get_position( first_frame ) : -1;
113
114 // Get the real producer
115 mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
116
117 // get properties
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");
122
123 mlt_position need_first;
124
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 );
128
129 if (mlt_properties_get_int( properties, "reverse")) actual_position = mlt_producer_get_playtime(this) - actual_position;
130
131 if (strobe < 2)
132 {
133 need_first = floor( actual_position );
134 }
135 else
136 {
137 // Strobe effect wanted, calculate frame position
138 need_first = floor( actual_position );
139 need_first -= need_first%strobe;
140 }
141 if (freeze)
142 {
143 if (freeze_after && need_first > freeze) need_first = freeze;
144 else if (freeze_before && need_first < freeze) need_first = freeze;
145 }
146 }
147 else need_first = freeze;
148
149 if( need_first != first_position )
150 {
151 mlt_frame_close( first_frame );
152 first_position = -1;
153 first_frame = NULL;
154 }
155
156 if( first_frame == NULL )
157 {
158 // Seek the producer to the correct place
159 mlt_producer_seek( real_producer, need_first );
160
161 // Get the frame
162 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
163 }
164
165
166 // Make sure things are in their place
167 mlt_properties_set_data( properties, "first_frame", first_frame, 0, NULL, NULL );
168
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 ) );
172
173 mlt_frame_push_service( *frame, this );
174 mlt_frame_push_service( *frame, framebuffer_get_image );
175
176 // Give the returned frame temporal identity
177 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
178
179 }
180
181 return 0;
182 }
183
184
185 mlt_producer producer_framebuffer_init( char *arg )
186 {
187
188 mlt_producer this = NULL;
189 this = calloc( 1, sizeof( struct mlt_producer_s ) );
190 mlt_producer_init( this, NULL );
191
192 // Wrap fezzik
193 mlt_producer real_producer;
194
195 // Check if a speed was specified.
196 /**
197
198 * Speed must be appended to the filename with ':'. To play your video at 50%:
199 inigo framebuffer:my_video.mpg:0.5
200
201 * Stroboscope effect can be obtained by adding a stobe=x parameter, where
202 x is the number of frames that will be ignored.
203
204 * You can play the movie backwards by adding reverse=1
205
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
208
209 **/
210
211 double speed;
212
213 int count;
214 char *props = strdup( arg );
215 char *ptr = props;
216 count = strcspn( ptr, ":" );
217 ptr[count] = '\0';
218
219 real_producer = mlt_factory_producer( "fezzik", props );
220
221 ptr += count + 1;
222 ptr += strspn( ptr, ":" );
223 count = strcspn( ptr, ":" );
224 ptr[count] = '\0';
225 speed = atof(ptr);
226 free( props );
227
228 if (speed == 0.0) speed = 1.0;
229
230
231 if ( this != NULL && real_producer != NULL)
232 {
233 // Get the properties of this producer
234 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
235
236 // Fezzik normalised it for us already
237 mlt_properties_set_int( properties, "fezzik_normalised", 1);
238
239 // Store the producer and fitler
240 mlt_properties_set_data( properties, "producer", real_producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
241
242 // Grap some stuff from the real_producer
243 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ),
244 "length,resource,width,height" );
245
246 if ( speed != 1.0 )
247 {
248 double real_length = (double) mlt_producer_get_length( real_producer );
249 mlt_properties_set_position( properties, "length", real_length * 2 / speed );
250 }
251
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 );
255
256 // Override the get_frame method
257 this->get_frame = producer_get_frame;
258 }
259 else
260 {
261 if ( this )
262 mlt_producer_close( this );
263 if ( real_producer )
264 mlt_producer_close( real_producer );
265
266 this = NULL;
267 }
268 return this;
269 }