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