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