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