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