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>
35 typedef struct producer_ffmpeg_s
*producer_ffmpeg
;
37 /** Bi-directional pipe structure.
48 /** Create a bidirectional pipe for the given command.
51 rwpipe
*rwpipe_open( char *command
)
53 rwpipe
*this = malloc( sizeof( rwpipe
) );
67 signal( SIGPIPE
, SIG_DFL
);
68 signal( SIGHUP
, SIG_DFL
);
69 signal( SIGINT
, SIG_DFL
);
70 signal( SIGTERM
, SIG_DFL
);
71 signal( SIGSTOP
, SIG_DFL
);
72 signal( SIGCHLD
, SIG_DFL
);
74 dup2( output
[ 0 ], STDIN_FILENO
);
75 dup2( input
[ 1 ], STDOUT_FILENO
);
82 execl( "/bin/sh", "sh", "-c", command
, NULL
);
87 setpgid( this->pid
, this->pid
);
92 this->reader
= fdopen( input
[ 0 ], "r" );
93 this->writer
= fdopen( output
[ 1 ], "w" );
100 /** Read data from the pipe.
103 FILE *rwpipe_reader( rwpipe
*this )
111 /** Write data to the pipe.
114 FILE *rwpipe_writer( rwpipe
*this )
122 /** Close the pipe and process.
125 void rwpipe_close( rwpipe
*this )
129 fclose( this->reader
);
130 fclose( this->writer
);
131 kill( - this->pid
, SIGKILL
);
132 waitpid( - this->pid
, NULL
, 0 );
137 struct producer_ffmpeg_s
139 struct mlt_producer_s parent
;
153 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
);
154 static void producer_close( mlt_producer parent
);
156 /** Consutruct an ffmpeg producer.
159 mlt_producer
producer_ffmpeg_init( char *file
)
161 producer_ffmpeg
this = calloc( sizeof( struct producer_ffmpeg_s
), 1 );
162 if ( file
!= NULL
&& this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
167 mlt_producer producer
= &this->parent
;
169 // Get the properties of the producer
170 mlt_properties properties
= mlt_producer_properties( producer
);
172 // Override get_frame and close methods
173 producer
->get_frame
= producer_get_frame
;
174 producer
->close
= producer_close
;
176 // Set the properties
177 mlt_properties_set( properties
, "mlt_type", "producer_ffmpeg" );
179 if ( !strcmp( file
, "v4l" ) )
181 mlt_properties_set( properties
, "video_type", "v4l" );
182 mlt_properties_set( properties
, "video_file", "/dev/video0" );
183 mlt_properties_set( properties
, "video_size", "640x480" );
184 mlt_properties_set( properties
, "audio_type", "dsp" );
185 mlt_properties_set( properties
, "audio_file", "/dev/dsp" );
190 if ( stat( file
, &buf
) != 0 || !S_ISREG( buf
.st_mode
) )
192 mlt_properties_set( properties
, "video_type", "file" );
193 mlt_properties_set( properties
, "video_file", file
);
194 mlt_properties_set( properties
, "video_size", "" );
195 mlt_properties_set( properties
, "audio_type", "file" );
196 mlt_properties_set( properties
, "audio_file", file
);
199 mlt_properties_set_int( properties
, "audio_rate", 48000 );
200 mlt_properties_set_int( properties
, "audio_channels", 2 );
201 mlt_properties_set_int( properties
, "audio_track", 0 );
203 mlt_properties_set( properties
, "log_id", file
);
204 mlt_properties_set( properties
, "resource", file
);
205 mlt_properties_set_position( properties
, "length", 36000 );
206 mlt_properties_set_position( properties
, "out", 36000 );
208 this->buffer
= malloc( 1024 * 1024 * 2 );
212 mlt_producer_close( &this->parent
);
222 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
224 // Get the frames properties
225 mlt_properties properties
= mlt_frame_properties( this );
227 if ( mlt_properties_get_int( properties
, "has_image" ) )
229 // Get width and height
230 *format
= mlt_image_yuv422
;
231 *width
= mlt_properties_get_int( properties
, "width" );
232 *height
= mlt_properties_get_int( properties
, "height" );
234 // Specify format and image
235 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
239 mlt_frame_get_image( this, buffer
, format
, width
, height
, writable
);
245 FILE *producer_ffmpeg_run_video( producer_ffmpeg
this, mlt_position position
)
247 if ( this->video
== NULL
)
250 mlt_producer producer
= &this->parent
;
252 // Get the properties of the producer
253 mlt_properties properties
= mlt_producer_properties( producer
);
255 // Get the video loop property
256 int video_loop
= mlt_properties_get_int( properties
, "video_loop" );
258 if ( !this->open
|| video_loop
)
260 const char *mlt_prefix
= mlt_factory_prefix( );
261 char *video_type
= mlt_properties_get( properties
, "video_type" );
262 char *video_file
= mlt_properties_get( properties
, "video_file" );
263 float video_rate
= mlt_properties_get_double( properties
, "fps" );
264 char *video_size
= mlt_properties_get( properties
, "video_size" );
265 char command
[ 1024 ] = "";
267 sprintf( command
, "%s/ffmpeg/video.sh \"%s\" \"%s\" \"%s\" %f %f 2>/dev/null",
275 this->video_pipe
= rwpipe_open( command
);
276 this->video
= rwpipe_reader( this->video_pipe
);
282 FILE *producer_ffmpeg_run_audio( producer_ffmpeg
this, mlt_position position
)
285 mlt_producer producer
= &this->parent
;
287 // Get the properties of the producer
288 mlt_properties properties
= mlt_producer_properties( producer
);
290 if ( this->audio
== NULL
)
292 int audio_loop
= mlt_properties_get_int( properties
, "audio_loop" );
294 if ( !this->open
|| audio_loop
)
296 const char *mlt_prefix
= mlt_factory_prefix( );
297 char *audio_type
= mlt_properties_get( properties
, "audio_type" );
298 char *audio_file
= mlt_properties_get( properties
, "audio_file" );
299 int frequency
= mlt_properties_get_int( properties
, "audio_rate" );
300 int channels
= mlt_properties_get_int( properties
, "audio_channels" );
301 int track
= mlt_properties_get_int( properties
, "audio_track" );
302 char command
[ 1024 ] = "";
304 sprintf( command
, "%s/ffmpeg/audio.sh \"%s\" \"%s\" %f %d %d %d 2>/dev/null",
313 this->audio_pipe
= rwpipe_open( command
);
314 this->audio
= rwpipe_reader( this->audio_pipe
);
320 static void producer_ffmpeg_position( producer_ffmpeg
this, uint64_t requested
, int *skip
)
324 if ( this->open
&& requested
> this->expected
)
326 // Skip the following n frames
327 *skip
= requested
- this->expected
;
329 else if ( requested
!= this->expected
)
331 // Close the video pipe
332 if ( this->video
!= NULL
)
333 rwpipe_close( this->video_pipe
);
336 // Close the audio pipe
337 if ( this->audio
!= NULL
)
338 rwpipe_close( this->audio_pipe
);
341 // We should not be open now
343 this->end_of_video
= 0;
344 this->end_of_audio
= 0;
347 // This is the next frame we expect
348 this->expected
= requested
+ 1;
351 this->video
= producer_ffmpeg_run_video( this, 0 );
353 // Open the audio pipe
354 this->audio
= producer_ffmpeg_run_audio( this, 0 );
356 // We should be open now
360 static int producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
362 // Get the frames properties
363 mlt_properties properties
= mlt_frame_properties( this );
365 producer_ffmpeg producer
= mlt_properties_get_data( properties
, "producer_ffmpeg", NULL
);
366 mlt_properties producer_properties
= mlt_producer_properties( &producer
->parent
);
368 int64_t target
= mlt_properties_get_double( properties
, "target" );
369 int skip
= mlt_properties_get_int( properties
, "skip" );
371 float fps
= mlt_properties_get_double( producer_properties
, "fps" );
372 *frequency
= mlt_properties_get_int( producer_properties
, "audio_rate" );
373 *channels
= mlt_properties_get_int( producer_properties
, "audio_channels" );
376 int size
= ( *frequency
/ 25 ) * *channels
* 2;
379 *buffer
= malloc( size
);
382 if ( producer
->audio
!= NULL
)
386 *samples
= mlt_sample_calculator( fps
, *frequency
, target
- skip
);
387 if ( fread( *buffer
, *samples
* *channels
* 2, 1, producer
->audio
) != 1 )
389 rwpipe_close( producer
->audio_pipe
);
390 producer
->audio
= NULL
;
391 producer
->end_of_audio
= 1;
394 while( producer
->audio
!= NULL
&& skip
-- );
398 *samples
= mlt_sample_calculator( fps
, *frequency
, target
);
399 memset( *buffer
, 0, size
);
402 // Pass the data on the frame properties
403 mlt_properties_set_data( properties
, "audio", *buffer
, size
, free
, NULL
);
405 // Set the producer properties
406 mlt_properties_set_int( producer_properties
, "end_of_clip", producer
->end_of_video
&& producer
->end_of_audio
);
411 static int read_ffmpeg_header( producer_ffmpeg
this, int *width
, int *height
)
415 FILE *video
= this->video
;
417 if ( fgets( temp
, 132, video
) )
419 if ( strncmp( temp
, "FRAME", 5 ) )
421 if ( strstr( temp
, " W" ) != NULL
)
422 *width
= atoi( strstr( temp
, " W" ) + 2 );
423 if ( strstr( temp
, " H" ) != NULL
)
424 *height
= atoi( strstr( temp
, " H" ) + 2 );
426 fgets( temp
, 132, video
);
427 this->width
= *width
;
428 this->height
= *height
;
432 *width
= this->width
;
433 *height
= this->height
;
440 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
442 producer_ffmpeg
this = producer
->child
;
447 // Construct a test frame
448 *frame
= mlt_frame_init( );
450 // Are we at the position expected?
451 producer_ffmpeg_position( this, mlt_producer_frame( producer
), &skip
);
453 // Get properties objects
454 mlt_properties producer_properties
= mlt_producer_properties( &this->parent
);
456 // Get the frames properties
457 mlt_properties properties
= mlt_frame_properties( *frame
);
459 FILE *video
= this->video
;
461 mlt_properties_set_double( properties
, "target", mlt_producer_frame( producer
) );
462 mlt_properties_set_int( properties
, "skip", skip
);
465 if ( video
!= NULL
&& read_ffmpeg_header( this, &width
, &height
) == 2 )
468 uint8_t *image
= mlt_pool_alloc( width
* ( height
+ 1 ) * 2 );
473 if ( fread( this->buffer
, width
* height
* 3 / 2, 1, video
) == 1 )
474 read_ffmpeg_header( this, &width
, &height
);
479 fread( this->buffer
, width
* height
* 3 / 2, 1, video
);
482 mlt_convert_yuv420p_to_yuv422( this->buffer
, width
, height
, width
, image
);
484 // Pass the data on the frame properties
485 mlt_properties_set_data( properties
, "image", image
, width
* ( height
+ 1 ) * 2, ( mlt_destructor
)mlt_pool_release
, NULL
);
486 mlt_properties_set_int( properties
, "width", width
);
487 mlt_properties_set_int( properties
, "height", height
);
488 mlt_properties_set_int( properties
, "has_image", 1 );
490 // Push the image callback
491 mlt_frame_push_get_image( *frame
, producer_get_image
);
496 if ( this->video
!= NULL
)
498 int video_loop
= mlt_properties_get_int( producer_properties
, "video_loop" );
500 // Inform caller that end of clip is reached
501 this->end_of_video
= !video_loop
;
502 rwpipe_close( this->video_pipe
);
506 // Push the image callback
507 if ( !this->end_of_video
)
508 mlt_frame_push_get_image( *frame
, producer_get_image
);
511 // Set the audio pipe
512 mlt_properties_set_data( properties
, "producer_ffmpeg", this, 0, NULL
, NULL
);
514 // Hmm - register audio callback
515 if ( !this->end_of_audio
)
516 ( *frame
)->get_audio
= producer_get_audio
;
518 // Get the additional properties
519 double aspect_ratio
= mlt_properties_get_double( producer_properties
, "aspect_ratio" );
520 double speed
= mlt_producer_get_speed( producer
);
522 // Set them on the frame
523 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
524 mlt_properties_set_double( properties
, "_speed", speed
);
526 // Set the out point on the producer
527 if ( this->end_of_video
&& this->end_of_audio
)
529 mlt_properties_set_int( properties
, "end_of_clip", 1 );
530 mlt_properties_set_position( producer_properties
, "length", mlt_producer_position( &this->parent
) );
531 mlt_producer_set_in_and_out( &this->parent
, mlt_producer_get_in( &this->parent
), mlt_producer_position( &this->parent
) );
534 // Update timecode on the frame we're creating
535 mlt_frame_set_position( *frame
, mlt_producer_position( producer
) );
537 // Calculate the next timecode
538 mlt_producer_prepare_next( producer
);
543 static void producer_close( mlt_producer parent
)
545 producer_ffmpeg
this = parent
->child
;
547 rwpipe_close( this->video_pipe
);
549 rwpipe_close( this->audio_pipe
);
550 parent
->close
= NULL
;
551 mlt_producer_close( parent
);
552 free( this->buffer
);