1 /*
2 * producer_framebuffer.c -- create subspeed frames
3 * Copyright (C) 2007 Jean-Baptiste Mardelle <jb@ader.ch>
4 * Copyright (C) 2021 Meltytech, LLC
5 * Author: Jean-Baptiste Mardelle, based on the code of motion_est by Zachary Drew
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
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 producer, mlt_frame_ptr frame, int index );
33
34 /** Image stack(able) method
35 */
36
framebuffer_get_image(mlt_frame frame,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)37 static int framebuffer_get_image( mlt_frame frame, 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( frame );
42 int index = mlt_frame_pop_service_int( frame );
43 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
44
45 mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
46
47 // Frame properties objects
48 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
49 mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
50
51 // Get producer parameters
52 int strobe = mlt_properties_get_int( properties, "strobe" );
53 int freeze = mlt_properties_get_int( properties, "freeze" );
54 int freeze_after = mlt_properties_get_int( properties, "freeze_after" );
55 int freeze_before = mlt_properties_get_int( properties, "freeze_before" );
56 int in = mlt_properties_get_position( properties, "in" );
57
58 // Determine the position
59 mlt_position first_position = (first_frame != NULL) ? mlt_frame_get_position( first_frame ) : -1;
60 mlt_position need_first = freeze;
61
62 if ( !freeze || freeze_after || freeze_before )
63 {
64 double prod_speed = mlt_properties_get_double( properties, "_speed" );
65 double actual_position = prod_speed * ( in + mlt_producer_position( producer ) );
66
67 if ( mlt_properties_get_int( properties, "reverse" ) )
68 actual_position = mlt_producer_get_playtime( producer ) - actual_position;
69
70 if ( strobe < 2 )
71 {
72 need_first = floor( actual_position );
73 }
74 else
75 {
76 // Strobe effect wanted, calculate frame position
77 need_first = floor( actual_position );
78 need_first -= MLT_POSITION_MOD(need_first, strobe);
79 }
80 if ( freeze )
81 {
82 if ( freeze_after && need_first > freeze ) need_first = freeze;
83 else if ( freeze_before && need_first < freeze ) need_first = freeze;
84 }
85 }
86
87 if ( *format == mlt_image_none )
88 {
89 // set format to the original's producer format
90 *format = (mlt_image_format) mlt_properties_get_int( properties, "_original_format" );
91 }
92 // Determine output buffer size
93 *width = mlt_properties_get_int( frame_properties, "width" );
94 *height = mlt_properties_get_int( frame_properties, "height" );
95 int size = mlt_image_format_size( *format, *width, *height, NULL );
96
97 // Get output buffer
98 int buffersize = 0;
99 int alphasize = *width * *height;
100 uint8_t *output = mlt_properties_get_data( properties, "output_buffer", &buffersize );
101 uint8_t *output_alpha = mlt_properties_get_data( properties, "output_alpha", NULL );
102 if( buffersize == 0 || buffersize != size )
103 {
104 // invalidate cached frame
105 first_position = -1;
106 }
107
108 if ( need_first != first_position )
109 {
110 // invalidate cached frame
111 first_position = -1;
112
113 // Bust the cached frame
114 mlt_properties_set_data( properties, "first_frame", NULL, 0, NULL, NULL );
115 first_frame = NULL;
116 }
117
118 if ( output && first_position != -1 ) {
119 // Using the cached frame
120 uint8_t *image_copy = mlt_pool_alloc( size );
121 memcpy( image_copy, output, size );
122 uint8_t *alpha_copy = mlt_pool_alloc( alphasize );
123 memcpy( alpha_copy, output_alpha, alphasize );
124
125 // Set the output image
126 *image = image_copy;
127 mlt_frame_set_image( frame, image_copy, size, mlt_pool_release );
128 mlt_frame_set_alpha( frame, alpha_copy, alphasize, mlt_pool_release );
129
130 *width = mlt_properties_get_int( properties, "_output_width" );
131 *height = mlt_properties_get_int( properties, "_output_height" );
132 *format = mlt_properties_get_int( properties, "_output_format" );
133
134 mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
135 return 0;
136 }
137
138 // Get the cached frame
139 if ( first_frame == NULL )
140 {
141 // Get the frame to cache from the real producer
142 mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
143
144 // Seek the producer to the correct place
145 mlt_producer_seek( real_producer, need_first );
146
147 // Get the frame
148 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
149
150 // Cache the frame
151 mlt_properties_set_data( properties, "first_frame", first_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
152 }
153 mlt_properties first_frame_properties = MLT_FRAME_PROPERTIES( first_frame );
154
155
156 // Which frames are buffered?
157 uint8_t *first_image = mlt_properties_get_data( first_frame_properties, "image", NULL );
158 uint8_t *first_alpha = mlt_properties_get_data( first_frame_properties, "alpha", NULL );
159 if ( !first_image )
160 {
161 mlt_properties_set( first_frame_properties, "rescale.interp", mlt_properties_get( frame_properties, "rescale.interp" ) );
162
163 int error = mlt_frame_get_image( first_frame, &first_image, format, width, height, writable );
164
165 if ( error != 0 ) {
166 mlt_log_warning( MLT_PRODUCER_SERVICE( producer ), "first_image == NULL get image died\n" );
167 mlt_properties_set_data( properties, "first_frame", NULL, 0, NULL, NULL );
168 mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
169 return error;
170 }
171 output = mlt_pool_alloc( size );
172 memcpy( output, first_image, size );
173 // Let someone else clean up
174 mlt_properties_set_data( properties, "output_buffer", output, size, mlt_pool_release, NULL );
175 mlt_properties_set_int( properties, "_output_width", *width );
176 mlt_properties_set_int( properties, "_output_height", *height );
177 mlt_properties_set_int( properties, "_output_format", *format );
178 }
179
180 if ( !first_alpha )
181 {
182 alphasize = *width * *height;
183 first_alpha = mlt_frame_get_alpha( first_frame );
184 if ( !first_alpha )
185 {
186 first_alpha = mlt_pool_alloc( alphasize );
187 memset( first_alpha, 255, alphasize );
188 mlt_frame_set_alpha( first_frame, first_alpha, alphasize, mlt_pool_release );
189 }
190 output_alpha = mlt_pool_alloc( alphasize );
191 memcpy( output_alpha, first_alpha, alphasize );
192 mlt_properties_set_data( properties, "output_alpha", output_alpha, alphasize, mlt_pool_release, NULL );
193 }
194
195 mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
196
197 // Create a copy
198 uint8_t *image_copy = mlt_pool_alloc( size );
199 memcpy( image_copy, first_image, size );
200 uint8_t *alpha_copy = mlt_pool_alloc( alphasize );
201 memcpy( alpha_copy, first_alpha, alphasize );
202
203 // Set the output image
204 *image = image_copy;
205 mlt_frame_set_image( frame, image_copy, size, mlt_pool_release );
206 mlt_frame_set_alpha( frame, alpha_copy, alphasize, mlt_pool_release );
207
208 return 0;
209 }
210
producer_get_frame(mlt_producer producer,mlt_frame_ptr frame,int index)211 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
212 {
213 if ( frame )
214 {
215 // Construct a new frame
216 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
217
218 // Stack the producer and producer's get image
219 mlt_frame_push_service_int( *frame, index );
220 mlt_frame_push_service( *frame, producer );
221 mlt_frame_push_service( *frame, framebuffer_get_image );
222
223 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
224 mlt_properties frame_properties = MLT_FRAME_PROPERTIES(*frame);
225
226 // Get frame from the real producer
227 mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
228
229 if ( first_frame == NULL )
230 {
231 // Get the frame to cache from the real producer
232 mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
233
234 // Get the producer speed
235 double prod_speed = mlt_properties_get_double( properties, "_speed" );
236
237 // Seek the producer to the correct place
238 mlt_producer_seek( real_producer, mlt_producer_position( producer ) * prod_speed );
239
240 // Get the frame
241 mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
242
243 // Cache the frame
244 mlt_properties_set_data( properties, "first_frame", first_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
245
246 // Find the original producer's format
247 int width = 0;
248 int height = 0;
249 mlt_image_format format = mlt_image_none;
250 uint8_t *image = NULL;
251 int error = mlt_frame_get_image( first_frame, &image, &format, &width, &height, 0 );
252 if ( !error )
253 {
254 // cache the original producer's pixel format
255 mlt_properties_set_int( properties, "_original_format", (int) format );
256 }
257 }
258
259 mlt_properties_inherit( frame_properties, MLT_FRAME_PROPERTIES(first_frame) );
260
261 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
262 if ( force_aspect_ratio <= 0.0 ) force_aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
263 mlt_properties_set_double( frame_properties, "aspect_ratio", force_aspect_ratio );
264
265 // Give the returned frame temporal identity
266 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
267
268 mlt_properties_set_int( frame_properties, "meta.media.width", mlt_properties_get_int( properties, "width" ) );
269 mlt_properties_set_int( frame_properties, "meta.media.height", mlt_properties_get_int( properties, "height" ) );
270 mlt_properties_pass_list( frame_properties, properties, "width, height" );
271 }
272
273 return 0;
274 }
275
276
producer_framebuffer_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)277 mlt_producer producer_framebuffer_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
278 {
279 if ( !arg ) return NULL;
280 mlt_producer producer = NULL;
281 producer = calloc( 1, sizeof( struct mlt_producer_s ) );
282 if ( !producer )
283 return NULL;
284
285 if ( mlt_producer_init( producer, NULL ) )
286 {
287 free( producer );
288 return NULL;
289 }
290
291 // Wrap loader
292 mlt_producer real_producer;
293
294 // Check if a speed was specified.
295 /**
296
297 * Speed must be appended to the filename with '?'. To play your video at 50%:
298 melt framebuffer:my_video.mpg?0.5
299
300 * Stroboscope effect can be obtained by adding a stobe=x parameter, where
301 x is the number of frames that will be ignored.
302
303 * You can play the movie backwards by adding reverse=1
304
305 * You can freeze the clip at a determined position by adding freeze=frame_pos
306 add freeze_after=1 to freeze only paste position or freeze_before to freeze before it
307
308 **/
309
310 double speed = 0.0;
311 char *props = strdup( arg );
312 char *ptr = strrchr( props, '?' );
313
314 if ( ptr )
315 {
316 speed = atof( ptr + 1 );
317 if ( speed != 0.0 )
318 // If speed was valid, then strip it and the delimiter.
319 // Otherwise, an invalid speed probably means this '?' was not a delimiter.
320 *ptr = '\0';
321 }
322
323 real_producer = mlt_factory_producer( profile, "abnormal", props );
324 free( props );
325
326 if (speed == 0.0) speed = 1.0;
327
328 if ( producer != NULL && real_producer != NULL)
329 {
330 // Get the properties of this producer
331 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
332
333 mlt_properties_set( properties, "resource", arg);
334
335 // Store the producer and filter
336 mlt_properties_set_data( properties, "producer", real_producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
337
338 // Grab some stuff from the real_producer
339 mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "progressive, length, width, height, aspect_ratio" );
340
341 if ( speed < 0 )
342 {
343 speed = -speed;
344 mlt_properties_set_int( properties, "reverse", 1 );
345 }
346
347 if ( speed != 1.0 )
348 {
349 double real_length = ( (double) mlt_producer_get_length( real_producer ) ) / speed;
350 mlt_properties_set_position( properties, "length", real_length );
351 mlt_properties real_properties = MLT_PRODUCER_PROPERTIES( real_producer );
352 const char* service = mlt_properties_get( real_properties, "mlt_service" );
353 if ( service && !strcmp( service, "avformat" ) )
354 {
355 int n = mlt_properties_count( real_properties );
356 int i;
357 for ( i = 0; i < n; i++ )
358 {
359 if ( strstr( mlt_properties_get_name( real_properties, i ), "stream.frame_rate" ) )
360 {
361 double source_fps = mlt_properties_get_double( real_properties, mlt_properties_get_name( real_properties, i ) );
362 if ( source_fps > mlt_profile_fps( profile ) )
363 {
364 mlt_properties_set_double( real_properties, "force_fps", source_fps * speed );
365 mlt_properties_set_position( real_properties, "length", real_length );
366 mlt_properties_set_position( real_properties, "out", real_length - 1 );
367 speed = 1.0;
368 }
369 break;
370 }
371 }
372 }
373 }
374 mlt_properties_set_position( properties, "out", mlt_producer_get_length( producer ) - 1 );
375
376 // Since we control the seeking, prevent it from seeking on its own
377 mlt_producer_set_speed( real_producer, 0 );
378 mlt_producer_set_speed( producer, speed );
379
380 // Override the get_frame method
381 producer->get_frame = producer_get_frame;
382 }
383 else
384 {
385 if ( producer )
386 mlt_producer_close( producer );
387 if ( real_producer )
388 mlt_producer_close( real_producer );
389
390 producer = NULL;
391 }
392 return producer;
393 }
394