move audio sample calculator to mlt_frame and use from ffmpeg and mcmpeg, add mlt_fra...
[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
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_factory.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33
34 typedef struct producer_ffmpeg_s *producer_ffmpeg;
35
36 /** Bi-directional pipe structure.
37 */
38
39 typedef struct rwpipe
40 {
41 int pid;
42 FILE *reader;
43 FILE *writer;
44 }
45 rwpipe;
46
47 /** Create a bidirectional pipe for the given command.
48 */
49
50 rwpipe *rwpipe_open( char *command )
51 {
52 rwpipe *this = malloc( sizeof( rwpipe ) );
53
54 if ( this != NULL )
55 {
56 int input[ 2 ];
57 int output[ 2 ];
58
59 pipe( input );
60 pipe( output );
61
62 this->pid = fork();
63
64 if ( this->pid == 0 )
65 {
66 signal( SIGPIPE, SIG_DFL );
67 signal( SIGHUP, SIG_DFL );
68 signal( SIGINT, SIG_DFL );
69 signal( SIGTERM, SIG_DFL );
70 signal( SIGSTOP, SIG_DFL );
71 signal( SIGCHLD, SIG_DFL );
72
73 dup2( output[ 0 ], STDIN_FILENO );
74 dup2( input[ 1 ], STDOUT_FILENO );
75
76 close( input[ 0 ] );
77 close( input[ 1 ] );
78 close( output[ 0 ] );
79 close( output[ 1 ] );
80
81 execl( "/bin/sh", "sh", "-c", command, NULL );
82 exit( 255 );
83 }
84 else
85 {
86 setpgid( this->pid, this->pid );
87
88 close( input[ 1 ] );
89 close( output[ 0 ] );
90
91 this->reader = fdopen( input[ 0 ], "r" );
92 this->writer = fdopen( output[ 1 ], "w" );
93 }
94 }
95
96 return this;
97 }
98
99 /** Read data from the pipe.
100 */
101
102 FILE *rwpipe_reader( rwpipe *this )
103 {
104 if ( this != NULL )
105 return this->reader;
106 else
107 return NULL;
108 }
109
110 /** Write data to the pipe.
111 */
112
113 FILE *rwpipe_writer( rwpipe *this )
114 {
115 if ( this != NULL )
116 return this->writer;
117 else
118 return NULL;
119 }
120
121 /** Close the pipe and process.
122 */
123
124 void rwpipe_close( rwpipe *this )
125 {
126 if ( this != NULL )
127 {
128 fclose( this->reader );
129 fclose( this->writer );
130 kill( - this->pid, SIGKILL );
131 waitpid( - this->pid, NULL, 0 );
132 free( this );
133 }
134 }
135
136 struct producer_ffmpeg_s
137 {
138 struct mlt_producer_s parent;
139 rwpipe *video_pipe;
140 rwpipe *audio_pipe;
141 FILE *video;
142 FILE *audio;
143 uint64_t expected;
144 uint8_t *buffer;
145 int open;
146 int width;
147 int height;
148 int end_of_video;
149 int end_of_audio;
150 };
151
152 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
153 static void producer_close( mlt_producer parent );
154
155 /** Consutruct an ffmpeg producer.
156 */
157
158 mlt_producer producer_ffmpeg_init( char *file )
159 {
160 producer_ffmpeg this = calloc( sizeof( struct producer_ffmpeg_s ), 1 );
161 if ( file != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
162 {
163 // Get the producer
164 mlt_producer producer = &this->parent;
165
166 // Get the properties of the producer
167 mlt_properties properties = mlt_producer_properties( producer );
168
169 // Override get_frame and close methods
170 producer->get_frame = producer_get_frame;
171 producer->close = producer_close;
172
173 // Set the properties
174 mlt_properties_set( properties, "mlt_type", "producer_ffmpeg" );
175
176 if ( !strcmp( file, "v4l" ) )
177 {
178 mlt_properties_set( properties, "video_type", "v4l" );
179 mlt_properties_set( properties, "video_file", "/dev/video0" );
180 mlt_properties_set( properties, "video_size", "640x480" );
181 mlt_properties_set( properties, "audio_type", "dsp" );
182 mlt_properties_set( properties, "audio_file", "/dev/dsp" );
183 }
184 else
185 {
186 mlt_properties_set( properties, "video_type", "file" );
187 mlt_properties_set( properties, "video_file", file );
188 mlt_properties_set( properties, "video_size", "" );
189 mlt_properties_set( properties, "audio_type", "file" );
190 mlt_properties_set( properties, "audio_file", file );
191 }
192
193 mlt_properties_set_int( properties, "audio_rate", 48000 );
194 mlt_properties_set_int( properties, "audio_channels", 2 );
195 mlt_properties_set_int( properties, "audio_track", 0 );
196
197 mlt_properties_set( properties, "log_id", file );
198 mlt_properties_set( properties, "resource", file );
199
200 this->buffer = malloc( 1024 * 1024 * 2 );
201
202 return producer;
203 }
204 free( this );
205 return NULL;
206 }
207
208 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
209 {
210 // Get the frames properties
211 mlt_properties properties = mlt_frame_properties( this );
212
213 if ( mlt_properties_get_int( properties, "has_image" ) )
214 {
215 // Get width and height
216 *format = mlt_image_yuv422;
217 *width = mlt_properties_get_int( properties, "width" );
218 *height = mlt_properties_get_int( properties, "height" );
219
220 // Specify format and image
221 *buffer = mlt_properties_get_data( properties, "image", NULL );
222 }
223 else
224 {
225 mlt_frame_get_image( this, buffer, format, width, height, writable );
226 }
227
228 return 0;
229 }
230
231 FILE *producer_ffmpeg_run_video( producer_ffmpeg this, mlt_timecode position )
232 {
233 if ( this->video == NULL )
234 {
235 // Get the producer
236 mlt_producer producer = &this->parent;
237
238 // Get the properties of the producer
239 mlt_properties properties = mlt_producer_properties( producer );
240
241 // Get the video loop property
242 int video_loop = mlt_properties_get_int( properties, "video_loop" );
243
244 if ( !this->open || video_loop )
245 {
246 const char *mlt_prefix = mlt_factory_prefix( );
247 char *video_type = mlt_properties_get( properties, "video_type" );
248 char *video_file = mlt_properties_get( properties, "video_file" );
249 float video_rate = mlt_properties_get_double( properties, "fps" );
250 char *video_size = mlt_properties_get( properties, "video_size" );
251 char command[ 1024 ] = "";
252
253 sprintf( command, "%s/ffmpeg/video.sh \"%s\" \"%s\" \"%s\" %f %f 2>/dev/null",
254 mlt_prefix,
255 video_type,
256 video_file,
257 video_size,
258 video_rate,
259 ( float )position );
260
261 this->video_pipe = rwpipe_open( command );
262 this->video = rwpipe_reader( this->video_pipe );
263 }
264 }
265 return this->video;
266 }
267
268 FILE *producer_ffmpeg_run_audio( producer_ffmpeg this, mlt_timecode position )
269 {
270 // Get the producer
271 mlt_producer producer = &this->parent;
272
273 // Get the properties of the producer
274 mlt_properties properties = mlt_producer_properties( producer );
275
276 if ( this->audio == NULL )
277 {
278 int audio_loop = mlt_properties_get_int( properties, "audio_loop" );
279
280 if ( !this->open || audio_loop )
281 {
282 const char *mlt_prefix = mlt_factory_prefix( );
283 char *audio_type = mlt_properties_get( properties, "audio_type" );
284 char *audio_file = mlt_properties_get( properties, "audio_file" );
285 int frequency = mlt_properties_get_int( properties, "audio_rate" );
286 int channels = mlt_properties_get_int( properties, "audio_channels" );
287 int track = mlt_properties_get_int( properties, "audio_track" );
288 char command[ 1024 ] = "";
289
290 sprintf( command, "%s/ffmpeg/audio.sh \"%s\" \"%s\" %f %d %d %d 2>/dev/null",
291 mlt_prefix,
292 audio_type,
293 audio_file,
294 ( float )position,
295 frequency,
296 channels,
297 track );
298
299 this->audio_pipe = rwpipe_open( command );
300 this->audio = rwpipe_reader( this->audio_pipe );
301 }
302 }
303 return this->audio;
304 }
305
306 static void producer_ffmpeg_position( producer_ffmpeg this, uint64_t requested, int *skip )
307 {
308 *skip = 0;
309
310 if ( this->open && requested > this->expected )
311 {
312 // Skip the following n frames
313 *skip = requested - this->expected;
314 }
315 else if ( requested != this->expected )
316 {
317 // Close the video pipe
318 if ( this->video != NULL )
319 rwpipe_close( this->video_pipe );
320 this->video = NULL;
321
322 // Close the audio pipe
323 if ( this->audio != NULL )
324 rwpipe_close( this->audio_pipe );
325 this->audio = NULL;
326
327 // We should not be open now
328 this->open = 0;
329 this->end_of_video = 0;
330 this->end_of_audio = 0;
331 }
332
333 // This is the next frame we expect
334 this->expected = requested + 1;
335
336 // Open the pipe
337 this->video = producer_ffmpeg_run_video( this, 0 );
338
339 // Open the audio pipe
340 this->audio = producer_ffmpeg_run_audio( this, 0 );
341
342 // We should be open now
343 this->open = 1;
344 }
345
346 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
347 {
348 // Get the frames properties
349 mlt_properties properties = mlt_frame_properties( this );
350
351 producer_ffmpeg producer = mlt_properties_get_data( properties, "producer_ffmpeg", NULL );
352 mlt_properties producer_properties = mlt_producer_properties( &producer->parent );
353
354 int64_t target = mlt_properties_get_double( properties, "target" );
355 int skip = mlt_properties_get_int( properties, "skip" );
356
357 float fps = mlt_properties_get_double( producer_properties, "fps" );
358 *frequency = mlt_properties_get_int( producer_properties, "audio_rate" );
359 *channels = mlt_properties_get_int( producer_properties, "audio_channels" );
360
361 // Maximum Size (?)
362 int size = ( *frequency / 25 ) * *channels * 2;
363
364 // Allocate an image
365 *buffer = malloc( size );
366
367 // Read it
368 if ( producer->audio != NULL )
369 {
370 do
371 {
372 *samples = mlt_sample_calculator( fps, *frequency, target - skip );
373 if ( fread( *buffer, *samples * *channels * 2, 1, producer->audio ) != 1 )
374 {
375 rwpipe_close( producer->audio_pipe );
376 producer->audio = NULL;
377 producer->end_of_audio = 1;
378 }
379 }
380 while( producer->audio != NULL && skip -- );
381 }
382 else
383 {
384 *samples = mlt_sample_calculator( fps, *frequency, target );
385 memset( *buffer, 0, size );
386 }
387
388 // Pass the data on the frame properties
389 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
390
391 // Set the producer properties
392 mlt_properties_set_int( producer_properties, "end_of_clip", producer->end_of_video && producer->end_of_audio );
393
394 return 0;
395 }
396
397 static int read_ffmpeg_header( producer_ffmpeg this, int *width, int *height )
398 {
399 int count = 0;
400 char temp[ 132 ];
401 FILE *video = this->video;
402
403 if ( fgets( temp, 132, video ) )
404 {
405 if ( strncmp( temp, "FRAME", 5 ) )
406 {
407 if ( strstr( temp, " W" ) != NULL )
408 *width = atoi( strstr( temp, " W" ) + 2 );
409 if ( strstr( temp, " H" ) != NULL )
410 *height = atoi( strstr( temp, " H" ) + 2 );
411 count = 2;
412 fgets( temp, 132, video );
413 this->width = *width;
414 this->height = *height;
415 }
416 else
417 {
418 *width = this->width;
419 *height = this->height;
420 count = 2;
421 }
422 }
423 return count;
424 }
425
426 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
427 {
428 producer_ffmpeg this = producer->child;
429 int width;
430 int height;
431 int skip;
432
433 // Construct a test frame
434 *frame = mlt_frame_init( );
435
436 // Are we at the position expected?
437 producer_ffmpeg_position( this, mlt_producer_frame( producer ), &skip );
438
439 // Get properties objects
440 mlt_properties producer_properties = mlt_producer_properties( &this->parent );
441
442 // Get the frames properties
443 mlt_properties properties = mlt_frame_properties( *frame );
444
445 FILE *video = this->video;
446
447 mlt_properties_set_double( properties, "target", mlt_producer_frame( producer ) );
448 mlt_properties_set_int( properties, "skip", skip );
449
450 // Read the video
451 if ( video != NULL && read_ffmpeg_header( this, &width, &height ) == 2 )
452 {
453 // Allocate an image
454 uint8_t *image = malloc( width * height * 2 );
455
456 // Read it
457 while( skip -- )
458 {
459 if ( fread( this->buffer, width * height * 3 / 2, 1, video ) == 1 )
460 read_ffmpeg_header( this, &width, &height );
461 else
462 skip = 0;
463 }
464
465 fread( this->buffer, width * height * 3 / 2, 1, video );
466
467 // Convert it
468 mlt_convert_yuv420p_to_yuv422( this->buffer, width, height, width, image );
469
470 // Pass the data on the frame properties
471 mlt_properties_set_data( properties, "image", image, width * height * 2, free, NULL );
472 mlt_properties_set_int( properties, "width", width );
473 mlt_properties_set_int( properties, "height", height );
474 mlt_properties_set_int( properties, "has_image", 1 );
475
476 // Push the image callback
477 mlt_frame_push_get_image( *frame, producer_get_image );
478
479 }
480 else
481 {
482 // Clean up
483 if ( this->video != NULL )
484 {
485 int video_loop = mlt_properties_get_int( producer_properties, "video_loop" );
486
487 // Inform caller that end of clip is reached
488 this->end_of_video = !video_loop;
489 rwpipe_close( this->video_pipe );
490 this->video = NULL;
491 }
492
493 // Push the image callback
494 mlt_frame_push_get_image( *frame, producer_get_image );
495 }
496
497 // Set the audio pipe
498 mlt_properties_set_data( properties, "producer_ffmpeg", this, 0, NULL, NULL );
499
500 // Hmm - register audio callback
501 ( *frame )->get_audio = producer_get_audio;
502
503 // Get the additional properties
504 double aspect_ratio = mlt_properties_get_double( producer_properties, "aspect_ratio" );
505 double speed = mlt_properties_get_double( producer_properties, "speed" );
506 char *video_file = mlt_properties_get( producer_properties, "video_file" );
507
508 // Set them on the frame
509 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
510 mlt_properties_set_double( properties, "speed", speed );
511 if ( strchr( video_file, '/' ) != NULL )
512 mlt_properties_set( properties, "file", strrchr( video_file, '/' ) + 1 );
513 else
514 mlt_properties_set( properties, "file", video_file );
515
516
517 // Set the out point on the producer
518 if ( this->end_of_video && this->end_of_audio )
519 {
520 mlt_properties_set_int( properties, "end_of_clip", 1 );
521 mlt_properties_set_timecode( producer_properties, "length", mlt_producer_position( &this->parent ) );
522 mlt_producer_set_in_and_out( &this->parent, mlt_producer_get_in( &this->parent ), mlt_producer_position( &this->parent ) );
523 }
524
525 // Update timecode on the frame we're creating
526 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
527
528 // Calculate the next timecode
529 mlt_producer_prepare_next( producer );
530
531 return 0;
532 }
533
534 static void producer_close( mlt_producer parent )
535 {
536 producer_ffmpeg this = parent->child;
537 if ( this->video )
538 rwpipe_close( this->video_pipe );
539 if ( this->audio )
540 rwpipe_close( this->audio_pipe );
541 parent->close = NULL;
542 mlt_producer_close( parent );
543 free( this->buffer );
544 free( this );
545 }
546