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