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
);
206 this->buffer
= malloc( 1024 * 1024 * 2 );
210 mlt_producer_close( &this->parent
);
220 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
222 // Get the frames properties
223 mlt_properties properties
= mlt_frame_properties( this );
225 if ( mlt_properties_get_int( properties
, "has_image" ) )
227 // Get width and height
228 *format
= mlt_image_yuv422
;
229 *width
= mlt_properties_get_int( properties
, "width" );
230 *height
= mlt_properties_get_int( properties
, "height" );
232 // Specify format and image
233 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
237 mlt_frame_get_image( this, buffer
, format
, width
, height
, writable
);
243 FILE *producer_ffmpeg_run_video( producer_ffmpeg
this, mlt_timecode position
)
245 if ( this->video
== NULL
)
248 mlt_producer producer
= &this->parent
;
250 // Get the properties of the producer
251 mlt_properties properties
= mlt_producer_properties( producer
);
253 // Get the video loop property
254 int video_loop
= mlt_properties_get_int( properties
, "video_loop" );
256 if ( !this->open
|| video_loop
)
258 const char *mlt_prefix
= mlt_factory_prefix( );
259 char *video_type
= mlt_properties_get( properties
, "video_type" );
260 char *video_file
= mlt_properties_get( properties
, "video_file" );
261 float video_rate
= mlt_properties_get_double( properties
, "fps" );
262 char *video_size
= mlt_properties_get( properties
, "video_size" );
263 char command
[ 1024 ] = "";
265 sprintf( command
, "%s/ffmpeg/video.sh \"%s\" \"%s\" \"%s\" %f %f 2>/dev/null",
273 this->video_pipe
= rwpipe_open( command
);
274 this->video
= rwpipe_reader( this->video_pipe
);
280 FILE *producer_ffmpeg_run_audio( producer_ffmpeg
this, mlt_timecode position
)
283 mlt_producer producer
= &this->parent
;
285 // Get the properties of the producer
286 mlt_properties properties
= mlt_producer_properties( producer
);
288 if ( this->audio
== NULL
)
290 int audio_loop
= mlt_properties_get_int( properties
, "audio_loop" );
292 if ( !this->open
|| audio_loop
)
294 const char *mlt_prefix
= mlt_factory_prefix( );
295 char *audio_type
= mlt_properties_get( properties
, "audio_type" );
296 char *audio_file
= mlt_properties_get( properties
, "audio_file" );
297 int frequency
= mlt_properties_get_int( properties
, "audio_rate" );
298 int channels
= mlt_properties_get_int( properties
, "audio_channels" );
299 int track
= mlt_properties_get_int( properties
, "audio_track" );
300 char command
[ 1024 ] = "";
302 sprintf( command
, "%s/ffmpeg/audio.sh \"%s\" \"%s\" %f %d %d %d 2>/dev/null",
311 this->audio_pipe
= rwpipe_open( command
);
312 this->audio
= rwpipe_reader( this->audio_pipe
);
318 static void producer_ffmpeg_position( producer_ffmpeg
this, uint64_t requested
, int *skip
)
322 if ( this->open
&& requested
> this->expected
)
324 // Skip the following n frames
325 *skip
= requested
- this->expected
;
327 else if ( requested
!= this->expected
)
329 // Close the video pipe
330 if ( this->video
!= NULL
)
331 rwpipe_close( this->video_pipe
);
334 // Close the audio pipe
335 if ( this->audio
!= NULL
)
336 rwpipe_close( this->audio_pipe
);
339 // We should not be open now
341 this->end_of_video
= 0;
342 this->end_of_audio
= 0;
345 // This is the next frame we expect
346 this->expected
= requested
+ 1;
349 this->video
= producer_ffmpeg_run_video( this, 0 );
351 // Open the audio pipe
352 this->audio
= producer_ffmpeg_run_audio( this, 0 );
354 // We should be open now
358 static int producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
360 // Get the frames properties
361 mlt_properties properties
= mlt_frame_properties( this );
363 producer_ffmpeg producer
= mlt_properties_get_data( properties
, "producer_ffmpeg", NULL
);
364 mlt_properties producer_properties
= mlt_producer_properties( &producer
->parent
);
366 int64_t target
= mlt_properties_get_double( properties
, "target" );
367 int skip
= mlt_properties_get_int( properties
, "skip" );
369 float fps
= mlt_properties_get_double( producer_properties
, "fps" );
370 *frequency
= mlt_properties_get_int( producer_properties
, "audio_rate" );
371 *channels
= mlt_properties_get_int( producer_properties
, "audio_channels" );
374 int size
= ( *frequency
/ 25 ) * *channels
* 2;
377 *buffer
= malloc( size
);
380 if ( producer
->audio
!= NULL
)
384 *samples
= mlt_sample_calculator( fps
, *frequency
, target
- skip
);
385 if ( fread( *buffer
, *samples
* *channels
* 2, 1, producer
->audio
) != 1 )
387 rwpipe_close( producer
->audio_pipe
);
388 producer
->audio
= NULL
;
389 producer
->end_of_audio
= 1;
392 while( producer
->audio
!= NULL
&& skip
-- );
396 *samples
= mlt_sample_calculator( fps
, *frequency
, target
);
397 memset( *buffer
, 0, size
);
400 // Pass the data on the frame properties
401 mlt_properties_set_data( properties
, "audio", *buffer
, size
, free
, NULL
);
403 // Set the producer properties
404 mlt_properties_set_int( producer_properties
, "end_of_clip", producer
->end_of_video
&& producer
->end_of_audio
);
409 static int read_ffmpeg_header( producer_ffmpeg
this, int *width
, int *height
)
413 FILE *video
= this->video
;
415 if ( fgets( temp
, 132, video
) )
417 if ( strncmp( temp
, "FRAME", 5 ) )
419 if ( strstr( temp
, " W" ) != NULL
)
420 *width
= atoi( strstr( temp
, " W" ) + 2 );
421 if ( strstr( temp
, " H" ) != NULL
)
422 *height
= atoi( strstr( temp
, " H" ) + 2 );
424 fgets( temp
, 132, video
);
425 this->width
= *width
;
426 this->height
= *height
;
430 *width
= this->width
;
431 *height
= this->height
;
438 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
440 producer_ffmpeg
this = producer
->child
;
445 // Construct a test frame
446 *frame
= mlt_frame_init( );
448 // Are we at the position expected?
449 producer_ffmpeg_position( this, mlt_producer_frame( producer
), &skip
);
451 // Get properties objects
452 mlt_properties producer_properties
= mlt_producer_properties( &this->parent
);
454 // Get the frames properties
455 mlt_properties properties
= mlt_frame_properties( *frame
);
457 FILE *video
= this->video
;
459 mlt_properties_set_double( properties
, "target", mlt_producer_frame( producer
) );
460 mlt_properties_set_int( properties
, "skip", skip
);
463 if ( video
!= NULL
&& read_ffmpeg_header( this, &width
, &height
) == 2 )
466 uint8_t *image
= malloc( width
* height
* 2 );
471 if ( fread( this->buffer
, width
* height
* 3 / 2, 1, video
) == 1 )
472 read_ffmpeg_header( this, &width
, &height
);
477 fread( this->buffer
, width
* height
* 3 / 2, 1, video
);
480 mlt_convert_yuv420p_to_yuv422( this->buffer
, width
, height
, width
, image
);
482 // Pass the data on the frame properties
483 mlt_properties_set_data( properties
, "image", image
, width
* height
* 2, free
, NULL
);
484 mlt_properties_set_int( properties
, "width", width
);
485 mlt_properties_set_int( properties
, "height", height
);
486 mlt_properties_set_int( properties
, "has_image", 1 );
488 // Push the image callback
489 mlt_frame_push_get_image( *frame
, producer_get_image
);
495 if ( this->video
!= NULL
)
497 int video_loop
= mlt_properties_get_int( producer_properties
, "video_loop" );
499 // Inform caller that end of clip is reached
500 this->end_of_video
= !video_loop
;
501 rwpipe_close( this->video_pipe
);
505 // Push the image callback
506 mlt_frame_push_get_image( *frame
, producer_get_image
);
509 // Set the audio pipe
510 mlt_properties_set_data( properties
, "producer_ffmpeg", this, 0, NULL
, NULL
);
512 // Hmm - register audio callback
513 ( *frame
)->get_audio
= producer_get_audio
;
515 // Get the additional properties
516 double aspect_ratio
= mlt_properties_get_double( producer_properties
, "aspect_ratio" );
517 double speed
= mlt_properties_get_double( producer_properties
, "speed" );
518 char *video_file
= mlt_properties_get( producer_properties
, "video_file" );
520 // Set them on the frame
521 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
522 mlt_properties_set_double( properties
, "speed", speed
);
523 if ( strchr( video_file
, '/' ) != NULL
)
524 mlt_properties_set( properties
, "file", strrchr( video_file
, '/' ) + 1 );
526 mlt_properties_set( properties
, "file", video_file
);
529 // Set the out point on the producer
530 if ( this->end_of_video
&& this->end_of_audio
)
532 mlt_properties_set_int( properties
, "end_of_clip", 1 );
533 mlt_properties_set_timecode( producer_properties
, "length", mlt_producer_position( &this->parent
) );
534 mlt_producer_set_in_and_out( &this->parent
, mlt_producer_get_in( &this->parent
), mlt_producer_position( &this->parent
) );
537 // Update timecode on the frame we're creating
538 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
540 // Calculate the next timecode
541 mlt_producer_prepare_next( producer
);
546 static void producer_close( mlt_producer parent
)
548 producer_ffmpeg
this = parent
->child
;
550 rwpipe_close( this->video_pipe
);
552 rwpipe_close( this->audio_pipe
);
553 parent
->close
= NULL
;
554 mlt_producer_close( parent
);
555 free( this->buffer
);