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