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"
22 #include <framework/mlt_frame.h>
26 typedef struct producer_ffmpeg_s
*producer_ffmpeg
;
28 struct producer_ffmpeg_s
30 struct mlt_producer_s parent
;
41 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
);
42 static void producer_close( mlt_producer parent
);
44 mlt_producer
producer_ffmpeg_init( char *command
)
46 producer_ffmpeg
this = calloc( sizeof( struct producer_ffmpeg_s
), 1 );
47 if ( this != NULL
&& mlt_producer_init( &this->parent
, this ) == 0 )
49 mlt_producer producer
= &this->parent
;
51 producer
->get_frame
= producer_get_frame
;
52 producer
->close
= producer_close
;
54 if ( command
!= NULL
)
55 this->command
= strdup( command
);
57 this->buffer
= malloc( 1024 * 1024 );
65 static int producer_get_image( mlt_frame
this, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
67 // Get the frames properties
68 mlt_properties properties
= mlt_frame_properties( this );
70 if ( mlt_properties_get_int( properties
, "has_image" ) )
72 // Get width and height
73 *format
= mlt_image_yuv422
;
74 *width
= mlt_properties_get_int( properties
, "width" );
75 *height
= mlt_properties_get_int( properties
, "height" );
77 // Specify format and image
78 *buffer
= mlt_properties_get_data( properties
, "image", NULL
);
82 mlt_frame_get_image( this, buffer
, format
, width
, height
, writable
);
88 FILE *producer_ffmpeg_run_video( producer_ffmpeg
this )
90 if ( this->video
== NULL
&& !this->open
)
92 if ( this->command
!= NULL
)
95 float fps
= mlt_producer_get_fps( &this->parent
);
96 float position
= mlt_producer_position( &this->parent
);
97 sprintf( command
, "ffmpeg -i \"%s\" -ss %f -f imagepipe -r %f -f yuv4mpegpipe - 2>/dev/null", this->command
, position
, fps
);
98 this->video
= popen( command
, "r" );
104 FILE *producer_ffmpeg_run_audio( producer_ffmpeg
this )
106 if ( this->audio
== NULL
&& !this->open
)
108 if ( this->command
!= NULL
)
110 char command
[ 1024 ];
111 float position
= mlt_producer_position( &this->parent
);
112 sprintf( command
, "ffmpeg -i \"%s\" -ss %f -f s16le -ar 48000 -ac 2 - 2>/dev/null", this->command
, position
);
113 this->audio
= popen( command
, "r" );
119 static void producer_ffmpeg_position( producer_ffmpeg
this, uint64_t requested
)
121 if ( requested
!= this->expected
)
123 if ( this->video
!= NULL
)
124 pclose( this->video
);
126 if ( this->audio
!= NULL
)
127 pclose( this->audio
);
130 // We should not be open now
134 // This is the next frame we expect
135 this->expected
= mlt_producer_frame( &this->parent
) + 1;
138 this->video
= producer_ffmpeg_run_video( this );
140 // Open the audio pipe
141 this->audio
= producer_ffmpeg_run_audio( this );
143 // We should be open now
147 static int producer_get_audio( mlt_frame
this, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
149 // Get the frames properties
150 mlt_properties properties
= mlt_frame_properties( this );
152 producer_ffmpeg producer
= mlt_properties_get_data( properties
, "producer_ffmpeg", NULL
);
159 int size
= *samples
* *channels
* 2;
162 *buffer
= malloc( size
);
165 if ( producer
->audio
!= NULL
)
167 if ( fread( *buffer
, size
, 1, producer
->audio
) != 1 )
169 pclose( producer
->audio
);
170 producer
->audio
= NULL
;
175 memset( *buffer
, 0, size
);
178 // Pass the data on the frame properties
179 mlt_properties_set_data( properties
, "audio", *buffer
, size
, free
, NULL
);
184 static int read_ffmpeg_header( producer_ffmpeg
this, int *width
, int *height
)
188 FILE *video
= this->video
;
190 if ( fgets( temp
, 132, video
) )
192 if ( strncmp( temp
, "FRAME", 5 ) )
194 if ( strstr( temp
, " W" ) != NULL
)
195 *width
= atoi( strstr( temp
, " W" ) + 2 );
196 if ( strstr( temp
, " H" ) != NULL
)
197 *height
= atoi( strstr( temp
, " H" ) + 2 );
199 fgets( temp
, 132, video
);
200 this->width
= *width
;
201 this->height
= *height
;
205 *width
= this->width
;
206 *height
= this->height
;
213 static int producer_get_frame( mlt_producer producer
, mlt_frame_ptr frame
, int index
)
215 producer_ffmpeg
this = producer
->child
;
219 // Construct a test frame
220 *frame
= mlt_frame_init( );
222 // Are we at the position expected?
223 producer_ffmpeg_position( this, mlt_producer_frame( producer
) );
225 // Get the frames properties
226 mlt_properties properties
= mlt_frame_properties( *frame
);
228 FILE *video
= this->video
;
231 if ( video
!= NULL
&& read_ffmpeg_header( this, &width
, &height
) == 2 )
234 uint8_t *image
= malloc( width
* height
* 2 );
237 fread( this->buffer
, width
* height
* 3 / 2, 1, video
);
240 mlt_convert_yuv420p_to_yuv422( this->buffer
, width
, height
, width
, image
);
242 // Pass the data on the frame properties
243 mlt_properties_set_data( properties
, "image", image
, width
* height
* 2, free
, NULL
);
244 mlt_properties_set_int( properties
, "width", width
);
245 mlt_properties_set_int( properties
, "height", height
);
246 mlt_properties_set_int( properties
, "has_image", 1 );
248 // Push the image callback
249 mlt_frame_push_get_image( *frame
, producer_get_image
);
254 if ( this->video
!= NULL
)
256 pclose( this->video
);
260 // Push the image callback
261 mlt_frame_push_get_image( *frame
, producer_get_image
);
264 // Set the audio pipe
265 mlt_properties_set_data( properties
, "producer_ffmpeg", this, 0, NULL
, NULL
);
267 // Hmm - register audio callback
268 ( *frame
)->get_audio
= producer_get_audio
;
270 // Update timecode on the frame we're creating
271 mlt_frame_set_timecode( *frame
, mlt_producer_position( producer
) );
273 // Calculate the next timecode
274 mlt_producer_prepare_next( producer
);
279 static void producer_close( mlt_producer parent
)
281 producer_ffmpeg
this = parent
->child
;
283 pclose( this->video
);
285 pclose( this->audio
);
286 free( this->command
);
287 parent
->close
= NULL
;
288 mlt_producer_close( parent
);