More sdl experimental mods, pixbuf writable work around and minor fixes
[melted] / src / modules / ffmpeg / producer_ffmpeg.c
1 /*
2 * producer_ffmpeg.c -- simple ffmpeg test case
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include "producer_ffmpeg.h"
22 #include <framework/mlt_frame.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 typedef struct producer_ffmpeg_s *producer_ffmpeg;
27
28 struct producer_ffmpeg_s
29 {
30 struct mlt_producer_s parent;
31 FILE *video;
32 FILE *audio;
33 uint64_t expected;
34 uint8_t *buffer;
35 int open;
36 int width;
37 int height;
38 int end_of_video;
39 int end_of_audio;
40 };
41
42 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
43 static void producer_close( mlt_producer parent );
44
45 /** Consutruct an ffmpeg producer.
46 */
47
48 mlt_producer producer_ffmpeg_init( char *file )
49 {
50 producer_ffmpeg this = calloc( sizeof( struct producer_ffmpeg_s ), 1 );
51 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
52 {
53 // Get the producer
54 mlt_producer producer = &this->parent;
55
56 // Get the properties of the producer
57 mlt_properties properties = mlt_producer_properties( producer );
58
59 // Override get_frame and close methods
60 producer->get_frame = producer_get_frame;
61 producer->close = producer_close;
62
63 // Set the properties
64 mlt_properties_set( properties, "mlt_type", "producer_ffmpeg" );
65 mlt_properties_set_int( properties, "known_length", 0 );
66 mlt_properties_set( properties, "video_type", file );
67 if ( file != NULL && !strcmp( file, "v4l" ) )
68 {
69 mlt_properties_set( properties, "video_file", "/dev/video0" );
70 mlt_properties_set( properties, "audio_file", "/dev/dsp" );
71 }
72 else
73 {
74 mlt_properties_set( properties, "video_file", file );
75 mlt_properties_set( properties, "audio_file", file );
76 }
77
78 this->buffer = malloc( 1024 * 1024 * 2 );
79
80 return producer;
81 }
82 free( this );
83 return NULL;
84 }
85
86 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
87 {
88 // Get the frames properties
89 mlt_properties properties = mlt_frame_properties( this );
90
91 if ( mlt_properties_get_int( properties, "has_image" ) )
92 {
93 // Get width and height
94 *format = mlt_image_yuv422;
95 *width = mlt_properties_get_int( properties, "width" );
96 *height = mlt_properties_get_int( properties, "height" );
97
98 // Specify format and image
99 *buffer = mlt_properties_get_data( properties, "image", NULL );
100 }
101 else
102 {
103 mlt_frame_get_image( this, buffer, format, width, height, writable );
104 }
105
106 return 0;
107 }
108
109 FILE *producer_ffmpeg_run_video( producer_ffmpeg this )
110 {
111 // Get the producer
112 mlt_producer producer = &this->parent;
113
114 // Get the properties of the producer
115 mlt_properties properties = mlt_producer_properties( producer );
116
117 char *video_type = mlt_properties_get( properties, "video_type" );
118 char *video_file = mlt_properties_get( properties, "video_file" );
119 int video_loop = mlt_properties_get_int( properties, "video_loop" );
120
121 if ( this->video == NULL )
122 {
123 if ( !this->open || video_loop )
124 {
125 char command[ 1024 ] = "";
126 float fps = mlt_producer_get_fps( &this->parent );
127 float position = mlt_producer_position( &this->parent );
128
129 if ( video_loop ) position = 0;
130
131 if ( video_type != NULL && !strcmp( video_type, "v4l" ) )
132 sprintf( command, "ffmpeg -r %f -s 640x480 -vd \"%s\" -f imagepipe -f yuv4mpegpipe - 2>/dev/null", fps, video_file );
133 else if ( video_file != NULL && strcmp( video_file, "" ) )
134 sprintf( command, "ffmpeg -i \"%s\" -ss %f -f imagepipe -r %f -f yuv4mpegpipe - 2>/dev/null", video_file, position, fps );
135
136 if ( strcmp( command, "" ) )
137 this->video = popen( command, "r" );
138 }
139 }
140 return this->video;
141 }
142
143 FILE *producer_ffmpeg_run_audio( producer_ffmpeg this )
144 {
145 // Get the producer
146 mlt_producer producer = &this->parent;
147
148 // Get the properties of the producer
149 mlt_properties properties = mlt_producer_properties( producer );
150
151 char *video_type = mlt_properties_get( properties, "video_type" );
152 char *audio_file = mlt_properties_get( properties, "audio_file" );
153 int audio_loop = mlt_properties_get_int( properties, "audio_loop" );
154
155 if ( this->audio == NULL )
156 {
157 if ( !this->open || audio_loop )
158 {
159 char command[ 1024 ] = "";
160 float position = mlt_producer_position( &this->parent );
161
162 if ( audio_loop ) position = 0;
163
164 if ( video_type != NULL && !strcmp( video_type, "v4l" ) )
165 sprintf( command, "ffmpeg -ad \"%s\" -f s16le -ar 48000 -ac 2 - 2>/dev/null", audio_file );
166 else if ( audio_file != NULL )
167 sprintf( command, "ffmpeg -i \"%s\" -ss %f -f s16le -ar 48000 -ac 2 - 2>/dev/null", audio_file, position );
168
169 if ( strcmp( command, "" ) )
170 this->audio = popen( command, "r" );
171 }
172 }
173 return this->audio;
174 }
175
176 static void producer_ffmpeg_position( producer_ffmpeg this, uint64_t requested, int *skip )
177 {
178 *skip = 0;
179
180 if ( this->open && requested > this->expected )
181 {
182 // Skip the following n frames
183 *skip = requested - this->expected;
184 }
185 else if ( requested != this->expected )
186 {
187 // Close the video pipe
188 if ( this->video != NULL )
189 pclose( this->video );
190 this->video = NULL;
191
192 // Close the audio pipe
193 if ( this->audio != NULL )
194 pclose( this->audio );
195 this->audio = NULL;
196
197 // We should not be open now
198 this->open = 0;
199 this->end_of_video = 0;
200 this->end_of_audio = 0;
201 }
202
203 // This is the next frame we expect
204 this->expected = mlt_producer_frame( &this->parent ) + 1;
205
206 // Open the pipe
207 this->video = producer_ffmpeg_run_video( this );
208
209 // Open the audio pipe
210 this->audio = producer_ffmpeg_run_audio( this );
211
212 // We should be open now
213 this->open = 1;
214 }
215
216 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
217 {
218 // Get the frames properties
219 mlt_properties properties = mlt_frame_properties( this );
220
221 producer_ffmpeg producer = mlt_properties_get_data( properties, "producer_ffmpeg", NULL );
222
223 int skip = mlt_properties_get_int( properties, "skip" );
224
225 *frequency = 48000;
226 *channels = 2;
227 *samples = 1920;
228
229 // Size
230 int size = *samples * *channels * 2;
231
232 // Allocate an image
233 *buffer = malloc( size );
234
235 // Read it
236 if ( producer->audio != NULL )
237 {
238 do
239 {
240 if ( fread( *buffer, size, 1, producer->audio ) != 1 )
241 {
242 pclose( producer->audio );
243 producer->audio = NULL;
244 producer->end_of_audio = 1;
245 }
246 }
247 while( skip -- );
248 }
249 else
250 {
251 memset( *buffer, 0, size );
252 }
253
254 // Pass the data on the frame properties
255 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
256
257 return 0;
258 }
259
260 static int read_ffmpeg_header( producer_ffmpeg this, int *width, int *height )
261 {
262 int count = 0;
263 char temp[ 132 ];
264 FILE *video = this->video;
265
266 if ( fgets( temp, 132, video ) )
267 {
268 if ( strncmp( temp, "FRAME", 5 ) )
269 {
270 if ( strstr( temp, " W" ) != NULL )
271 *width = atoi( strstr( temp, " W" ) + 2 );
272 if ( strstr( temp, " H" ) != NULL )
273 *height = atoi( strstr( temp, " H" ) + 2 );
274 count = 2;
275 fgets( temp, 132, video );
276 this->width = *width;
277 this->height = *height;
278 }
279 else
280 {
281 *width = this->width;
282 *height = this->height;
283 count = 2;
284 }
285 }
286 return count;
287 }
288
289 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
290 {
291 producer_ffmpeg this = producer->child;
292 int width;
293 int height;
294 int skip;
295
296 // Construct a test frame
297 *frame = mlt_frame_init( );
298
299 // Are we at the position expected?
300 producer_ffmpeg_position( this, mlt_producer_frame( producer ), &skip );
301
302 // Get the frames properties
303 mlt_properties properties = mlt_frame_properties( *frame );
304
305 FILE *video = this->video;
306
307 mlt_properties_set_int( properties, "skip", skip );
308
309 // Read the video
310 if ( video != NULL && read_ffmpeg_header( this, &width, &height ) == 2 )
311 {
312 // Allocate an image
313 uint8_t *image = malloc( width * height * 2 );
314
315 // Read it
316 while( skip -- )
317 {
318 if ( fread( this->buffer, width * height * 3 / 2, 1, video ) == 1 )
319 read_ffmpeg_header( this, &width, &height );
320 else
321 skip = 0;
322 }
323
324 fread( this->buffer, width * height * 3 / 2, 1, video );
325
326 // Convert it
327 mlt_convert_yuv420p_to_yuv422( this->buffer, width, height, width, image );
328
329 // Pass the data on the frame properties
330 mlt_properties_set_data( properties, "image", image, width * height * 2, free, NULL );
331 mlt_properties_set_int( properties, "width", width );
332 mlt_properties_set_int( properties, "height", height );
333 mlt_properties_set_int( properties, "has_image", 1 );
334
335 // Push the image callback
336 mlt_frame_push_get_image( *frame, producer_get_image );
337
338 }
339 else
340 {
341 // Clean up
342 if ( this->video != NULL )
343 {
344 // Inform caller that end of clip is reached
345 this->end_of_video = 1;
346 pclose( this->video );
347 this->video = NULL;
348 }
349
350 // Push the image callback
351 mlt_frame_push_get_image( *frame, producer_get_image );
352 }
353
354 // Set the audio pipe
355 mlt_properties_set_data( properties, "producer_ffmpeg", this, 0, NULL, NULL );
356 mlt_properties_set_int( properties, "end_of_clip", this->end_of_video && this->end_of_audio );
357
358 // Hmm - register audio callback
359 ( *frame )->get_audio = producer_get_audio;
360
361 // Get properties objects
362 mlt_properties producer_properties = mlt_producer_properties( &this->parent );
363
364 // Get the additional properties
365 double aspect_ratio = mlt_properties_get_double( producer_properties, "aspect_ratio" );
366 double speed = mlt_properties_get_double( producer_properties, "speed" );
367
368 // Set them on the frame
369 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
370 mlt_properties_set_double( properties, "speed", speed );
371
372 // Set the out point on the producer
373 mlt_producer_set_in_and_out( &this->parent, mlt_producer_get_in( &this->parent ), mlt_producer_position( &this->parent ) + 1 );
374
375 // Update timecode on the frame we're creating
376 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
377
378 // Calculate the next timecode
379 mlt_producer_prepare_next( producer );
380
381 return 0;
382 }
383
384 static void producer_close( mlt_producer parent )
385 {
386 producer_ffmpeg this = parent->child;
387 if ( this->video )
388 pclose( this->video );
389 if ( this->audio )
390 pclose( this->audio );
391 parent->close = NULL;
392 mlt_producer_close( parent );
393 free( this->buffer );
394 free( this );
395 }
396