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