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 typedef struct producer_ffmpeg_s
*producer_ffmpeg
;
32 struct producer_ffmpeg_s
34 struct mlt_producer_s parent
;
46 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
);
47 static void producer_close( mlt_producer parent
);
49 /** Consutruct an ffmpeg producer.
52 mlt_producer
producer_ffmpeg_init( char *file
)
54 producer_ffmpeg
this = calloc( sizeof( struct producer_ffmpeg_s
), 1 );
55 if ( this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
58 mlt_producer producer
= &this->parent
;
60 // Get the properties of the producer
61 mlt_properties properties
= mlt_producer_properties( producer
);
63 // Override get_frame and close methods
64 producer
->get_frame
= producer_get_frame
;
65 producer
->close
= producer_close
;
68 mlt_properties_set( properties
, "mlt_type", "producer_ffmpeg" );
70 if ( file
!= NULL
&& !strcmp( file
, "v4l" ) )
72 mlt_properties_set( properties
, "video_type", "v4l" );
73 mlt_properties_set( properties
, "video_file", "/dev/video0" );
74 mlt_properties_set( properties
, "video_size", "640x480" );
75 mlt_properties_set( properties
, "audio_type", "dsp" );
76 mlt_properties_set( properties
, "audio_file", "/dev/dsp" );
80 mlt_properties_set( properties
, "video_type", "file" );
81 mlt_properties_set( properties
, "video_file", file
);
82 mlt_properties_set( properties
, "video_size", "" );
83 mlt_properties_set( properties
, "audio_type", "file" );
84 mlt_properties_set( properties
, "audio_file", file
);
87 mlt_properties_set_int( properties
, "audio_rate", 48000 );
88 mlt_properties_set_int( properties
, "audio_channels", 2 );
89 mlt_properties_set_int( properties
, "audio_track", 0 );
91 this->buffer
= malloc( 1024 * 1024 * 2 );
99 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
101 // Get the frames properties
102 mlt_properties properties
= mlt_frame_properties( this );
104 if ( mlt_properties_get_int( properties
, "has_image" ) )
106 // Get width and height
107 *format
= mlt_image_yuv422
;
108 *width
= mlt_properties_get_int( properties
, "width" );
109 *height
= mlt_properties_get_int( properties
, "height" );
111 // Specify format and image
112 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
116 mlt_frame_get_image( this, buffer
, format
, width
, height
, writable
);
122 FILE *producer_ffmpeg_run_video( producer_ffmpeg
this )
124 if ( this->video
== NULL
)
127 mlt_producer producer
= &this->parent
;
129 // Get the properties of the producer
130 mlt_properties properties
= mlt_producer_properties( producer
);
132 // Get the video loop property
133 int video_loop
= mlt_properties_get_int( properties
, "video_loop" );
135 if ( !this->open
|| video_loop
)
137 const char *mlt_prefix
= mlt_factory_prefix( );
138 char *video_type
= mlt_properties_get( properties
, "video_type" );
139 char *video_file
= mlt_properties_get( properties
, "video_file" );
140 float video_rate
= mlt_properties_get_double( properties
, "fps" );
141 char *video_size
= mlt_properties_get( properties
, "video_size" );
142 char command
[ 1024 ] = "";
143 float position
= mlt_producer_position( &this->parent
);
145 if ( video_loop
) position
= 0;
147 sprintf( command
, "%s/ffmpeg/video.sh \"%s\" \"%s\" \"%s\" %f %f 2>/dev/null",
155 this->video
= popen( command
, "r" );
161 FILE *producer_ffmpeg_run_audio( producer_ffmpeg
this )
164 mlt_producer producer
= &this->parent
;
166 // Get the properties of the producer
167 mlt_properties properties
= mlt_producer_properties( producer
);
169 if ( this->audio
== NULL
)
171 int audio_loop
= mlt_properties_get_int( properties
, "audio_loop" );
173 if ( !this->open
|| audio_loop
)
175 const char *mlt_prefix
= mlt_factory_prefix( );
176 char *audio_type
= mlt_properties_get( properties
, "audio_type" );
177 char *audio_file
= mlt_properties_get( properties
, "audio_file" );
178 int frequency
= mlt_properties_get_int( properties
, "audio_rate" );
179 int channels
= mlt_properties_get_int( properties
, "audio_channels" );
180 int track
= mlt_properties_get_int( properties
, "audio_track" );
181 char command
[ 1024 ] = "";
182 float position
= mlt_producer_position( &this->parent
);
184 if ( audio_loop
) position
= 0;
186 sprintf( command
, "%s/ffmpeg/audio.sh \"%s\" \"%s\" %f %d %d %d 2>/dev/null",
195 this->audio
= popen( command
, "r" );
201 static void producer_ffmpeg_position( producer_ffmpeg
this, uint64_t requested
, int *skip
)
205 if ( this->open
&& requested
> this->expected
)
207 // Skip the following n frames
208 *skip
= requested
- this->expected
;
210 else if ( requested
!= this->expected
)
212 // Close the video pipe
213 if ( this->video
!= NULL
)
214 pclose( this->video
);
217 // Close the audio pipe
218 if ( this->audio
!= NULL
)
219 pclose( this->audio
);
222 // We should not be open now
224 this->end_of_video
= 0;
225 this->end_of_audio
= 0;
228 // This is the next frame we expect
229 this->expected
= mlt_producer_frame( &this->parent
) + 1;
232 this->video
= producer_ffmpeg_run_video( this );
234 // Open the audio pipe
235 this->audio
= producer_ffmpeg_run_audio( this );
237 // We should be open now
241 static int sample_calculator( float fps
, int frequency
, int64_t position
)
247 samples
= frequency
/ 25;
249 else if ( fps
>= 29.97 && fps
< 29.98 )
251 samples
= frequency
/ 30;
256 if ( position
% 5 != 0 )
260 if ( position
% 300 == 0 )
262 else if ( position
% 30 == 0 )
264 else if ( position
% 2 == 0 )
270 if ( position
% 30 == 0 )
272 else if ( position
% 29 == 0 )
274 else if ( position
% 4 == 2 )
286 samples
= frequency
/ fps
;
292 static int producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
294 // Get the frames properties
295 mlt_properties properties
= mlt_frame_properties( this );
297 producer_ffmpeg producer
= mlt_properties_get_data( properties
, "producer_ffmpeg", NULL
);
298 mlt_properties producer_properties
= mlt_producer_properties( &producer
->parent
);
300 int64_t target
= mlt_properties_get_double( properties
, "target" );
301 int skip
= mlt_properties_get_int( properties
, "skip" );
303 float fps
= mlt_properties_get_double( producer_properties
, "fps" );
304 *frequency
= mlt_properties_get_int( producer_properties
, "audio_rate" );
305 *channels
= mlt_properties_get_int( producer_properties
, "audio_channels" );
308 int size
= ( *frequency
/ 25 ) * *channels
* 2;
311 *buffer
= malloc( size
);
314 if ( producer
->audio
!= NULL
)
318 *samples
= sample_calculator( fps
, *frequency
, target
- skip
);
319 if ( fread( *buffer
, *samples
* *channels
* 2, 1, producer
->audio
) != 1 )
321 pclose( producer
->audio
);
322 producer
->audio
= NULL
;
323 producer
->end_of_audio
= 1;
326 while( producer
->audio
!= NULL
&& skip
-- );
330 *samples
= sample_calculator( fps
, *frequency
, target
);
331 memset( *buffer
, 0, size
);
334 // Pass the data on the frame properties
335 mlt_properties_set_data( properties
, "audio", *buffer
, size
, free
, NULL
);
337 // Set the producer properties
338 mlt_properties_set_int( producer_properties
, "end_of_clip", producer
->end_of_video
&& producer
->end_of_audio
);
343 static int read_ffmpeg_header( producer_ffmpeg
this, int *width
, int *height
)
347 FILE *video
= this->video
;
349 if ( fgets( temp
, 132, video
) )
351 if ( strncmp( temp
, "FRAME", 5 ) )
353 if ( strstr( temp
, " W" ) != NULL
)
354 *width
= atoi( strstr( temp
, " W" ) + 2 );
355 if ( strstr( temp
, " H" ) != NULL
)
356 *height
= atoi( strstr( temp
, " H" ) + 2 );
358 fgets( temp
, 132, video
);
359 this->width
= *width
;
360 this->height
= *height
;
364 *width
= this->width
;
365 *height
= this->height
;
372 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
374 producer_ffmpeg
this = producer
->child
;
379 // Construct a test frame
380 *frame
= mlt_frame_init( );
382 // Are we at the position expected?
383 producer_ffmpeg_position( this, mlt_producer_frame( producer
), &skip
);
385 // Get properties objects
386 mlt_properties producer_properties
= mlt_producer_properties( &this->parent
);
388 // Get the frames properties
389 mlt_properties properties
= mlt_frame_properties( *frame
);
391 FILE *video
= this->video
;
393 mlt_properties_set_double( properties
, "target", mlt_producer_frame( producer
) );
394 mlt_properties_set_int( properties
, "skip", skip
);
397 if ( video
!= NULL
&& read_ffmpeg_header( this, &width
, &height
) == 2 )
400 uint8_t *image
= malloc( width
* height
* 2 );
405 if ( fread( this->buffer
, width
* height
* 3 / 2, 1, video
) == 1 )
406 read_ffmpeg_header( this, &width
, &height
);
411 fread( this->buffer
, width
* height
* 3 / 2, 1, video
);
414 mlt_convert_yuv420p_to_yuv422( this->buffer
, width
, height
, width
, image
);
416 // Pass the data on the frame properties
417 mlt_properties_set_data( properties
, "image", image
, width
* height
* 2, free
, NULL
);
418 mlt_properties_set_int( properties
, "width", width
);
419 mlt_properties_set_int( properties
, "height", height
);
420 mlt_properties_set_int( properties
, "has_image", 1 );
422 // Push the image callback
423 mlt_frame_push_get_image( *frame
, producer_get_image
);
429 if ( this->video
!= NULL
)
431 int video_loop
= mlt_properties_get_int( producer_properties
, "video_loop" );
433 // Inform caller that end of clip is reached
434 this->end_of_video
= !video_loop
;
435 pclose( this->video
);
439 // Push the image callback
440 mlt_frame_push_get_image( *frame
, producer_get_image
);
443 // Set the audio pipe
444 mlt_properties_set_data( properties
, "producer_ffmpeg", this, 0, NULL
, NULL
);
445 mlt_properties_set_int( properties
, "end_of_clip", this->end_of_video
&& this->end_of_audio
);
447 // Hmm - register audio callback
448 ( *frame
)->get_audio
= producer_get_audio
;
450 // Get the additional properties
451 double aspect_ratio
= mlt_properties_get_double( producer_properties
, "aspect_ratio" );
452 double speed
= mlt_properties_get_double( producer_properties
, "speed" );
454 // Set them on the frame
455 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
456 mlt_properties_set_double( properties
, "speed", speed
);
458 // Set the out point on the producer
459 if ( !this->end_of_video
|| !this->end_of_audio
)
460 mlt_producer_set_in_and_out( &this->parent
, mlt_producer_get_in( &this->parent
), mlt_producer_position( &this->parent
) + 1 );
462 mlt_producer_set_in_and_out( &this->parent
, mlt_producer_get_in( &this->parent
), mlt_producer_position( &this->parent
) );
464 // Update timecode on the frame we're creating
465 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
467 // Calculate the next timecode
468 mlt_producer_prepare_next( producer
);
473 static void producer_close( mlt_producer parent
)
475 producer_ffmpeg
this = parent
->child
;
477 pclose( this->video
);
479 pclose( this->audio
);
480 parent
->close
= NULL
;
481 mlt_producer_close( parent
);
482 free( this->buffer
);