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