ffmpeg audio dub
[melted] / mlt / 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 mlt_properties producer_properties = mlt_producer_properties( &producer->parent );
223
224 int skip = mlt_properties_get_int( properties, "skip" );
225
226 *frequency = 48000;
227 *channels = 2;
228 *samples = 1920;
229
230 // Size
231 int size = *samples * *channels * 2;
232
233 // Allocate an image
234 *buffer = malloc( size );
235
236 // Read it
237 if ( producer->audio != NULL )
238 {
239 do
240 {
241 if ( fread( *buffer, size, 1, producer->audio ) != 1 )
242 {
243 pclose( producer->audio );
244 producer->audio = NULL;
245 producer->end_of_audio = 1;
246 }
247 }
248 while( producer->audio != NULL && skip -- );
249 }
250 else
251 {
252 memset( *buffer, 0, size );
253 }
254
255 // Pass the data on the frame properties
256 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
257
258 // Set the producer properties
259 mlt_properties_set_int( producer_properties, "end_of_clip", producer->end_of_video && producer->end_of_audio );
260
261 return 0;
262 }
263
264 static int read_ffmpeg_header( producer_ffmpeg this, int *width, int *height )
265 {
266 int count = 0;
267 char temp[ 132 ];
268 FILE *video = this->video;
269
270 if ( fgets( temp, 132, video ) )
271 {
272 if ( strncmp( temp, "FRAME", 5 ) )
273 {
274 if ( strstr( temp, " W" ) != NULL )
275 *width = atoi( strstr( temp, " W" ) + 2 );
276 if ( strstr( temp, " H" ) != NULL )
277 *height = atoi( strstr( temp, " H" ) + 2 );
278 count = 2;
279 fgets( temp, 132, video );
280 this->width = *width;
281 this->height = *height;
282 }
283 else
284 {
285 *width = this->width;
286 *height = this->height;
287 count = 2;
288 }
289 }
290 return count;
291 }
292
293 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
294 {
295 producer_ffmpeg this = producer->child;
296 int width;
297 int height;
298 int skip;
299
300 // Construct a test frame
301 *frame = mlt_frame_init( );
302
303 // Are we at the position expected?
304 producer_ffmpeg_position( this, mlt_producer_frame( producer ), &skip );
305
306 // Get the frames properties
307 mlt_properties properties = mlt_frame_properties( *frame );
308
309 FILE *video = this->video;
310
311 mlt_properties_set_int( properties, "skip", skip );
312
313 // Read the video
314 if ( video != NULL && read_ffmpeg_header( this, &width, &height ) == 2 )
315 {
316 // Allocate an image
317 uint8_t *image = malloc( width * height * 2 );
318
319 // Read it
320 while( skip -- )
321 {
322 if ( fread( this->buffer, width * height * 3 / 2, 1, video ) == 1 )
323 read_ffmpeg_header( this, &width, &height );
324 else
325 skip = 0;
326 }
327
328 fread( this->buffer, width * height * 3 / 2, 1, video );
329
330 // Convert it
331 mlt_convert_yuv420p_to_yuv422( this->buffer, width, height, width, image );
332
333 // Pass the data on the frame properties
334 mlt_properties_set_data( properties, "image", image, width * height * 2, free, NULL );
335 mlt_properties_set_int( properties, "width", width );
336 mlt_properties_set_int( properties, "height", height );
337 mlt_properties_set_int( properties, "has_image", 1 );
338
339 // Push the image callback
340 mlt_frame_push_get_image( *frame, producer_get_image );
341
342 }
343 else
344 {
345 // Clean up
346 if ( this->video != NULL )
347 {
348 // Inform caller that end of clip is reached
349 this->end_of_video = 1;
350 pclose( this->video );
351 this->video = NULL;
352 }
353
354 // Push the image callback
355 mlt_frame_push_get_image( *frame, producer_get_image );
356 }
357
358 // Set the audio pipe
359 mlt_properties_set_data( properties, "producer_ffmpeg", this, 0, NULL, NULL );
360 mlt_properties_set_int( properties, "end_of_clip", this->end_of_video && this->end_of_audio );
361
362 // Hmm - register audio callback
363 ( *frame )->get_audio = producer_get_audio;
364
365 // Get properties objects
366 mlt_properties producer_properties = mlt_producer_properties( &this->parent );
367
368 // Get the additional properties
369 double aspect_ratio = mlt_properties_get_double( producer_properties, "aspect_ratio" );
370 double speed = mlt_properties_get_double( producer_properties, "speed" );
371
372 // Set them on the frame
373 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
374 mlt_properties_set_double( properties, "speed", speed );
375
376 // Set the out point on the producer
377 if ( !this->end_of_video || !this->end_of_audio )
378 mlt_producer_set_in_and_out( &this->parent, mlt_producer_get_in( &this->parent ), mlt_producer_position( &this->parent ) + 1 );
379 else
380 mlt_producer_set_in_and_out( &this->parent, mlt_producer_get_in( &this->parent ), mlt_producer_position( &this->parent ) );
381
382 // Update timecode on the frame we're creating
383 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
384
385 // Calculate the next timecode
386 mlt_producer_prepare_next( producer );
387
388 return 0;
389 }
390
391 static void producer_close( mlt_producer parent )
392 {
393 producer_ffmpeg this = parent->child;
394 if ( this->video )
395 pclose( this->video );
396 if ( this->audio )
397 pclose( this->audio );
398 parent->close = NULL;
399 mlt_producer_close( parent );
400 free( this->buffer );
401 free( this );
402 }
403