2 * producer_ffmpeg.c -- simple ffmpeg test case
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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.
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.
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.
21 #include "producer_ffmpeg.h"
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_factory.h>
30 #include <sys/types.h>
34 typedef struct producer_ffmpeg_s
*producer_ffmpeg
;
36 /** Bi-directional pipe structure.
47 /** Create a bidirectional pipe for the given command.
50 rwpipe
*rwpipe_open( char *command
)
52 rwpipe
*this = malloc( sizeof( rwpipe
) );
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
);
73 dup2( output
[ 0 ], STDIN_FILENO
);
74 dup2( input
[ 1 ], STDOUT_FILENO
);
81 execl( "/bin/sh", "sh", "-c", command
, NULL
);
86 setpgid( this->pid
, this->pid
);
91 this->reader
= fdopen( input
[ 0 ], "r" );
92 this->writer
= fdopen( output
[ 1 ], "w" );
99 /** Read data from the pipe.
102 FILE *rwpipe_reader( rwpipe
*this )
110 /** Write data to the pipe.
113 FILE *rwpipe_writer( rwpipe
*this )
121 /** Close the pipe and process.
124 void rwpipe_close( rwpipe
*this )
128 fclose( this->reader
);
129 fclose( this->writer
);
130 kill( - this->pid
, SIGKILL
);
131 waitpid( - this->pid
, NULL
, 0 );
136 struct producer_ffmpeg_s
138 struct mlt_producer_s parent
;
152 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
);
153 static void producer_close( mlt_producer parent
);
155 /** Consutruct an ffmpeg producer.
158 mlt_producer
producer_ffmpeg_init( char *file
)
160 producer_ffmpeg
this = calloc( sizeof( struct producer_ffmpeg_s
), 1 );
161 if ( file
!= NULL
&& this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
164 mlt_producer producer
= &this->parent
;
166 // Get the properties of the producer
167 mlt_properties properties
= mlt_producer_properties( producer
);
169 // Override get_frame and close methods
170 producer
->get_frame
= producer_get_frame
;
171 producer
->close
= producer_close
;
173 // Set the properties
174 mlt_properties_set( properties
, "mlt_type", "producer_ffmpeg" );
176 if ( !strcmp( file
, "v4l" ) )
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" );
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
);
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 );
197 mlt_properties_set( properties
, "log_id", file
);
198 mlt_properties_set( properties
, "resource", file
);
200 this->buffer
= malloc( 1024 * 1024 * 2 );
208 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
210 // Get the frames properties
211 mlt_properties properties
= mlt_frame_properties( this );
213 if ( mlt_properties_get_int( properties
, "has_image" ) )
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" );
220 // Specify format and image
221 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
225 mlt_frame_get_image( this, buffer
, format
, width
, height
, writable
);
231 FILE *producer_ffmpeg_run_video( producer_ffmpeg
this, mlt_timecode position
)
233 if ( this->video
== NULL
)
236 mlt_producer producer
= &this->parent
;
238 // Get the properties of the producer
239 mlt_properties properties
= mlt_producer_properties( producer
);
241 // Get the video loop property
242 int video_loop
= mlt_properties_get_int( properties
, "video_loop" );
244 if ( !this->open
|| video_loop
)
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 ] = "";
253 sprintf( command
, "%s/ffmpeg/video.sh \"%s\" \"%s\" \"%s\" %f %f 2>/dev/null",
261 this->video_pipe
= rwpipe_open( command
);
262 this->video
= rwpipe_reader( this->video_pipe
);
268 FILE *producer_ffmpeg_run_audio( producer_ffmpeg
this, mlt_timecode position
)
271 mlt_producer producer
= &this->parent
;
273 // Get the properties of the producer
274 mlt_properties properties
= mlt_producer_properties( producer
);
276 if ( this->audio
== NULL
)
278 int audio_loop
= mlt_properties_get_int( properties
, "audio_loop" );
280 if ( !this->open
|| audio_loop
)
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 ] = "";
290 sprintf( command
, "%s/ffmpeg/audio.sh \"%s\" \"%s\" %f %d %d %d 2>/dev/null",
299 this->audio_pipe
= rwpipe_open( command
);
300 this->audio
= rwpipe_reader( this->audio_pipe
);
306 static void producer_ffmpeg_position( producer_ffmpeg
this, uint64_t requested
, int *skip
)
310 if ( this->open
&& requested
> this->expected
)
312 // Skip the following n frames
313 *skip
= requested
- this->expected
;
315 else if ( requested
!= this->expected
)
317 // Close the video pipe
318 if ( this->video
!= NULL
)
319 rwpipe_close( this->video_pipe
);
322 // Close the audio pipe
323 if ( this->audio
!= NULL
)
324 rwpipe_close( this->audio_pipe
);
327 // We should not be open now
329 this->end_of_video
= 0;
330 this->end_of_audio
= 0;
333 // This is the next frame we expect
334 this->expected
= requested
+ 1;
337 this->video
= producer_ffmpeg_run_video( this, 0 );
339 // Open the audio pipe
340 this->audio
= producer_ffmpeg_run_audio( this, 0 );
342 // We should be open now
346 static int sample_calculator( float fps
, int frequency
, int64_t position
)
350 if ( fps
> 29 && fps
<= 30 )
352 samples
= frequency
/ 30;
357 if ( position
% 5 != 0 )
361 if ( position
% 300 == 0 )
363 else if ( position
% 30 == 0 )
365 else if ( position
% 2 == 0 )
371 if ( position
% 30 == 0 )
373 else if ( position
% 29 == 0 )
375 else if ( position
% 4 == 2 )
386 samples
= frequency
/ fps
;
392 static int producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
394 // Get the frames properties
395 mlt_properties properties
= mlt_frame_properties( this );
397 producer_ffmpeg producer
= mlt_properties_get_data( properties
, "producer_ffmpeg", NULL
);
398 mlt_properties producer_properties
= mlt_producer_properties( &producer
->parent
);
400 int64_t target
= mlt_properties_get_double( properties
, "target" );
401 int skip
= mlt_properties_get_int( properties
, "skip" );
403 float fps
= mlt_properties_get_double( producer_properties
, "fps" );
404 *frequency
= mlt_properties_get_int( producer_properties
, "audio_rate" );
405 *channels
= mlt_properties_get_int( producer_properties
, "audio_channels" );
408 int size
= ( *frequency
/ 25 ) * *channels
* 2;
411 *buffer
= malloc( size
);
414 if ( producer
->audio
!= NULL
)
418 *samples
= sample_calculator( fps
, *frequency
, target
- skip
);
419 if ( fread( *buffer
, *samples
* *channels
* 2, 1, producer
->audio
) != 1 )
421 rwpipe_close( producer
->audio_pipe
);
422 producer
->audio
= NULL
;
423 producer
->end_of_audio
= 1;
426 while( producer
->audio
!= NULL
&& skip
-- );
430 *samples
= sample_calculator( fps
, *frequency
, target
);
431 memset( *buffer
, 0, size
);
434 // Pass the data on the frame properties
435 mlt_properties_set_data( properties
, "audio", *buffer
, size
, free
, NULL
);
437 // Set the producer properties
438 mlt_properties_set_int( producer_properties
, "end_of_clip", producer
->end_of_video
&& producer
->end_of_audio
);
443 static int read_ffmpeg_header( producer_ffmpeg
this, int *width
, int *height
)
447 FILE *video
= this->video
;
449 if ( fgets( temp
, 132, video
) )
451 if ( strncmp( temp
, "FRAME", 5 ) )
453 if ( strstr( temp
, " W" ) != NULL
)
454 *width
= atoi( strstr( temp
, " W" ) + 2 );
455 if ( strstr( temp
, " H" ) != NULL
)
456 *height
= atoi( strstr( temp
, " H" ) + 2 );
458 fgets( temp
, 132, video
);
459 this->width
= *width
;
460 this->height
= *height
;
464 *width
= this->width
;
465 *height
= this->height
;
472 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
474 producer_ffmpeg
this = producer
->child
;
479 // Construct a test frame
480 *frame
= mlt_frame_init( );
482 // Are we at the position expected?
483 producer_ffmpeg_position( this, mlt_producer_frame( producer
), &skip
);
485 // Get properties objects
486 mlt_properties producer_properties
= mlt_producer_properties( &this->parent
);
488 // Get the frames properties
489 mlt_properties properties
= mlt_frame_properties( *frame
);
491 FILE *video
= this->video
;
493 mlt_properties_set_double( properties
, "target", mlt_producer_frame( producer
) );
494 mlt_properties_set_int( properties
, "skip", skip
);
497 if ( video
!= NULL
&& read_ffmpeg_header( this, &width
, &height
) == 2 )
500 uint8_t *image
= malloc( width
* height
* 2 );
505 if ( fread( this->buffer
, width
* height
* 3 / 2, 1, video
) == 1 )
506 read_ffmpeg_header( this, &width
, &height
);
511 fread( this->buffer
, width
* height
* 3 / 2, 1, video
);
514 mlt_convert_yuv420p_to_yuv422( this->buffer
, width
, height
, width
, image
);
516 // Pass the data on the frame properties
517 mlt_properties_set_data( properties
, "image", image
, width
* height
* 2, free
, NULL
);
518 mlt_properties_set_int( properties
, "width", width
);
519 mlt_properties_set_int( properties
, "height", height
);
520 mlt_properties_set_int( properties
, "has_image", 1 );
522 // Push the image callback
523 mlt_frame_push_get_image( *frame
, producer_get_image
);
529 if ( this->video
!= NULL
)
531 int video_loop
= mlt_properties_get_int( producer_properties
, "video_loop" );
533 // Inform caller that end of clip is reached
534 this->end_of_video
= !video_loop
;
535 rwpipe_close( this->video_pipe
);
539 // Push the image callback
540 mlt_frame_push_get_image( *frame
, producer_get_image
);
543 // Set the audio pipe
544 mlt_properties_set_data( properties
, "producer_ffmpeg", this, 0, NULL
, NULL
);
546 // Hmm - register audio callback
547 ( *frame
)->get_audio
= producer_get_audio
;
549 // Get the additional properties
550 double aspect_ratio
= mlt_properties_get_double( producer_properties
, "aspect_ratio" );
551 double speed
= mlt_properties_get_double( producer_properties
, "speed" );
552 char *video_file
= mlt_properties_get( producer_properties
, "video_file" );
554 // Set them on the frame
555 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
556 mlt_properties_set_double( properties
, "speed", speed
);
557 if ( strchr( video_file
, '/' ) != NULL
)
558 mlt_properties_set( properties
, "file", strrchr( video_file
, '/' ) + 1 );
560 mlt_properties_set( properties
, "file", video_file
);
563 // Set the out point on the producer
564 if ( this->end_of_video
&& this->end_of_audio
)
566 mlt_properties_set_int( properties
, "end_of_clip", 1 );
567 mlt_properties_set_timecode( producer_properties
, "length", mlt_producer_position( &this->parent
) );
568 mlt_producer_set_in_and_out( &this->parent
, mlt_producer_get_in( &this->parent
), mlt_producer_position( &this->parent
) );
571 // Update timecode on the frame we're creating
572 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
574 // Calculate the next timecode
575 mlt_producer_prepare_next( producer
);
580 static void producer_close( mlt_producer parent
)
582 producer_ffmpeg
this = parent
->child
;
584 rwpipe_close( this->video_pipe
);
586 rwpipe_close( this->audio_pipe
);
587 parent
->close
= NULL
;
588 mlt_producer_close( parent
);
589 free( this->buffer
);