b3cbf019b33d5a6280b20cf9fcb1743c77b9affa
[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 prod_speed = mlt_properties_get_double( properties, "_speed");
119 double prod_end_speed = mlt_properties_get_double( properties, "end_speed");
120
121 // calculate actual speed and position
122 double actual_speed = prod_speed + ((double)mlt_producer_position( this ) / (double)mlt_producer_get_length(this)) * (prod_end_speed - prod_speed);
123 double actual_position = actual_speed * (double)mlt_producer_position( this );
124 if (mlt_properties_get_int( properties, "reverse")) actual_position = mlt_producer_get_length(this) - actual_position;
125
126 mlt_position need_first;
127
128
129 if (strobe == 1)
130 {
131 need_first = floor( actual_position );
132 }
133 else
134 {
135 // Strobe effect wanted, calculate frame position
136 need_first = floor( actual_position );
137 need_first -= need_first%strobe;
138 }
139
140 if( need_first != first_position )
141 {
142 mlt_frame_close( first_frame );
143 first_position = -1;
144 first_frame = NULL;
145 }
146
147 if( first_frame == NULL )
148 {
149 // Seek the producer to the correct place
150 mlt_producer_seek( real_producer, need_first );
151
152 // Get the frame
153 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
154 }
155
156
157 // Make sure things are in their place
158 mlt_properties_set_data( properties, "first_frame", first_frame, 0, NULL, NULL );
159
160 // Stack the producer and producer's get image
161 mlt_frame_push_service( *frame, first_frame );
162 mlt_properties_inc_ref( MLT_FRAME_PROPERTIES( first_frame ) );
163
164 mlt_frame_push_service( *frame, this );
165 mlt_frame_push_service( *frame, framebuffer_get_image );
166
167 // Give the returned frame temporal identity
168 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
169
170 }
171
172 return 0;
173 }
174
175
176 mlt_producer producer_framebuffer_init( char *arg )
177 {
178 mlt_producer this = mlt_producer_new( );
179 fprintf( stderr, " + + ++ USING FRAMEBUFF %s\n", arg);
180 // Wrap fezzik
181 mlt_producer real_producer;
182
183 // Check if a speed was specified.
184 /**
185
186 * Speed must be appended to the filename with ':'. To play your video at 50%:
187 inigo framebuffer:my_video.mpg:0.5
188
189 * You can have a variabl speed by specifying a start and an end speed:
190 inigo framebuffer:my_video.mpg:0.5:1.0
191
192 * Stroboscope effect can be obtained by adding a stobe=x parameter, where
193 x is the number of frames that will be ignored.
194
195 * You can play the movie backwards by adding reverse=1
196 **/
197
198 double speed;
199 double end_speed;
200 int count;
201 char *props = strdup( arg );
202 char *ptr = props;
203 count = strcspn( ptr, ":" );
204 ptr[count] = '\0';
205 real_producer = mlt_factory_producer( "fezzik", ptr );
206
207 ptr += count + 1;
208 ptr += strspn( ptr, ":" );
209 count = strcspn( ptr, ":" );
210 ptr[count] = '\0';
211 speed = atof(ptr);
212
213 ptr += count + 1;
214 ptr += strspn( ptr, ":" );
215 count = strcspn( ptr, ":" );
216 ptr[count] = '\0';
217 end_speed = atof(ptr);
218 free( props );
219
220 // If no end speed specified, use constant speed
221 if (speed == 0.0) speed = 1.0;
222 if (end_speed == 0.0) end_speed = speed;
223
224
225 if ( this != NULL && real_producer != NULL)
226 {
227 // Get the properties of this producer
228 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
229
230 // Fezzik normalised it for us already
231 mlt_properties_set_int( properties, "fezzik_normalised", 1);
232
233 // Store the producer and fitler
234 mlt_properties_set_data( properties, "producer", real_producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
235
236 // Grap some stuff from the real_producer
237 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ),
238 "in, out, length, resource" );
239
240 if (speed != 1.0 || end_speed !=1.0)
241 {
242 // Speed is not 1.0, so adjust the clip length
243 mlt_position real_out = mlt_properties_get_position(properties, "out");
244 mlt_properties_set_position( properties, "out", real_out * 2 / (speed + end_speed));
245 mlt_properties_set_position( properties, "length", real_out * 2 / (speed + end_speed) + 1);
246 mlt_properties_set_double( properties, "_speed", speed);
247 mlt_properties_set_double( properties, "end_speed", end_speed);
248 }
249 else mlt_properties_set_double( properties, "end_speed", 1.0);
250
251 // Since we control the seeking, prevent it from seeking on its own
252 mlt_producer_set_speed( real_producer, 0 );
253 mlt_producer_set_speed( this, speed );
254
255 // Override the get_frame method
256 this->get_frame = framebuffer_get_frame;
257
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 }