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