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