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