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 producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
348 // Get the frames properties
349 mlt_properties properties
= mlt_frame_properties( this );
351 producer_ffmpeg producer
= mlt_properties_get_data( properties
, "producer_ffmpeg", NULL
);
352 mlt_properties producer_properties
= mlt_producer_properties( &producer
->parent
);
354 int64_t target
= mlt_properties_get_double( properties
, "target" );
355 int skip
= mlt_properties_get_int( properties
, "skip" );
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" );
362 int size
= ( *frequency
/ 25 ) * *channels
* 2;
365 *buffer
= malloc( size
);
368 if ( producer
->audio
!= NULL
)
372 *samples
= mlt_sample_calculator( fps
, *frequency
, target
- skip
);
373 if ( fread( *buffer
, *samples
* *channels
* 2, 1, producer
->audio
) != 1 )
375 rwpipe_close( producer
->audio_pipe
);
376 producer
->audio
= NULL
;
377 producer
->end_of_audio
= 1;
380 while( producer
->audio
!= NULL
&& skip
-- );
384 *samples
= mlt_sample_calculator( fps
, *frequency
, target
);
385 memset( *buffer
, 0, size
);
388 // Pass the data on the frame properties
389 mlt_properties_set_data( properties
, "audio", *buffer
, size
, free
, NULL
);
391 // Set the producer properties
392 mlt_properties_set_int( producer_properties
, "end_of_clip", producer
->end_of_video
&& producer
->end_of_audio
);
397 static int read_ffmpeg_header( producer_ffmpeg
this, int *width
, int *height
)
401 FILE *video
= this->video
;
403 if ( fgets( temp
, 132, video
) )
405 if ( strncmp( temp
, "FRAME", 5 ) )
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 );
412 fgets( temp
, 132, video
);
413 this->width
= *width
;
414 this->height
= *height
;
418 *width
= this->width
;
419 *height
= this->height
;
426 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
428 producer_ffmpeg
this = producer
->child
;
433 // Construct a test frame
434 *frame
= mlt_frame_init( );
436 // Are we at the position expected?
437 producer_ffmpeg_position( this, mlt_producer_frame( producer
), &skip
);
439 // Get properties objects
440 mlt_properties producer_properties
= mlt_producer_properties( &this->parent
);
442 // Get the frames properties
443 mlt_properties properties
= mlt_frame_properties( *frame
);
445 FILE *video
= this->video
;
447 mlt_properties_set_double( properties
, "target", mlt_producer_frame( producer
) );
448 mlt_properties_set_int( properties
, "skip", skip
);
451 if ( video
!= NULL
&& read_ffmpeg_header( this, &width
, &height
) == 2 )
454 uint8_t *image
= malloc( width
* height
* 2 );
459 if ( fread( this->buffer
, width
* height
* 3 / 2, 1, video
) == 1 )
460 read_ffmpeg_header( this, &width
, &height
);
465 fread( this->buffer
, width
* height
* 3 / 2, 1, video
);
468 mlt_convert_yuv420p_to_yuv422( this->buffer
, width
, height
, width
, image
);
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 );
476 // Push the image callback
477 mlt_frame_push_get_image( *frame
, producer_get_image
);
483 if ( this->video
!= NULL
)
485 int video_loop
= mlt_properties_get_int( producer_properties
, "video_loop" );
487 // Inform caller that end of clip is reached
488 this->end_of_video
= !video_loop
;
489 rwpipe_close( this->video_pipe
);
493 // Push the image callback
494 mlt_frame_push_get_image( *frame
, producer_get_image
);
497 // Set the audio pipe
498 mlt_properties_set_data( properties
, "producer_ffmpeg", this, 0, NULL
, NULL
);
500 // Hmm - register audio callback
501 ( *frame
)->get_audio
= producer_get_audio
;
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" );
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 );
514 mlt_properties_set( properties
, "file", video_file
);
517 // Set the out point on the producer
518 if ( this->end_of_video
&& this->end_of_audio
)
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
) );
525 // Update timecode on the frame we're creating
526 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
528 // Calculate the next timecode
529 mlt_producer_prepare_next( producer
);
534 static void producer_close( mlt_producer parent
)
536 producer_ffmpeg
this = parent
->child
;
538 rwpipe_close( this->video_pipe
);
540 rwpipe_close( this->audio_pipe
);
541 parent
->close
= NULL
;
542 mlt_producer_close( parent
);
543 free( this->buffer
);