8777fb1173e8e1a69ff7209e225ba0a26f50ed6a
[melted] / mlt / src / modules / ffmpeg / producer_ffmpeg.c
1 /*
2 * producer_ffmpeg.c -- simple ffmpeg test case
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "producer_ffmpeg.h"
22
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_factory.h>
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 typedef struct producer_ffmpeg_s *producer_ffmpeg;
31
32 struct producer_ffmpeg_s
33 {
34 struct mlt_producer_s parent;
35 FILE *video;
36 FILE *audio;
37 uint64_t expected;
38 uint8_t *buffer;
39 int open;
40 int width;
41 int height;
42 int end_of_video;
43 int end_of_audio;
44 };
45
46 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
47 static void producer_close( mlt_producer parent );
48
49 /** Consutruct an ffmpeg producer.
50 */
51
52 mlt_producer producer_ffmpeg_init( char *file )
53 {
54 producer_ffmpeg this = calloc( sizeof( struct producer_ffmpeg_s ), 1 );
55 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
56 {
57 // Get the producer
58 mlt_producer producer = &this->parent;
59
60 // Get the properties of the producer
61 mlt_properties properties = mlt_producer_properties( producer );
62
63 // Override get_frame and close methods
64 producer->get_frame = producer_get_frame;
65 producer->close = producer_close;
66
67 // Set the properties
68 mlt_properties_set( properties, "mlt_type", "producer_ffmpeg" );
69
70 if ( file != NULL && !strcmp( file, "v4l" ) )
71 {
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" );
77 }
78 else
79 {
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 );
85 }
86
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 );
90
91 mlt_properties_set( properties, "log_id", file );
92
93 this->buffer = malloc( 1024 * 1024 * 2 );
94
95 return producer;
96 }
97 free( this );
98 return NULL;
99 }
100
101 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
102 {
103 // Get the frames properties
104 mlt_properties properties = mlt_frame_properties( this );
105
106 if ( mlt_properties_get_int( properties, "has_image" ) )
107 {
108 // Get width and height
109 *format = mlt_image_yuv422;
110 *width = mlt_properties_get_int( properties, "width" );
111 *height = mlt_properties_get_int( properties, "height" );
112
113 // Specify format and image
114 *buffer = mlt_properties_get_data( properties, "image", NULL );
115 }
116 else
117 {
118 mlt_frame_get_image( this, buffer, format, width, height, writable );
119 }
120
121 return 0;
122 }
123
124 FILE *producer_ffmpeg_run_video( producer_ffmpeg this, mlt_timecode position )
125 {
126 if ( this->video == NULL )
127 {
128 // Get the producer
129 mlt_producer producer = &this->parent;
130
131 // Get the properties of the producer
132 mlt_properties properties = mlt_producer_properties( producer );
133
134 // Get the video loop property
135 int video_loop = mlt_properties_get_int( properties, "video_loop" );
136
137 if ( !this->open || video_loop )
138 {
139 const char *mlt_prefix = mlt_factory_prefix( );
140 char *video_type = mlt_properties_get( properties, "video_type" );
141 char *video_file = mlt_properties_get( properties, "video_file" );
142 float video_rate = mlt_properties_get_double( properties, "fps" );
143 char *video_size = mlt_properties_get( properties, "video_size" );
144 char command[ 1024 ] = "";
145
146 sprintf( command, "%s/ffmpeg/video.sh \"%s\" \"%s\" \"%s\" %f %f 2>/dev/null",
147 mlt_prefix,
148 video_type,
149 video_file,
150 video_size,
151 video_rate,
152 ( float )position );
153
154 this->video = popen( command, "r" );
155 }
156 }
157 return this->video;
158 }
159
160 FILE *producer_ffmpeg_run_audio( producer_ffmpeg this, mlt_timecode position )
161 {
162 // Get the producer
163 mlt_producer producer = &this->parent;
164
165 // Get the properties of the producer
166 mlt_properties properties = mlt_producer_properties( producer );
167
168 if ( this->audio == NULL )
169 {
170 int audio_loop = mlt_properties_get_int( properties, "audio_loop" );
171
172 if ( !this->open || audio_loop )
173 {
174 const char *mlt_prefix = mlt_factory_prefix( );
175 char *audio_type = mlt_properties_get( properties, "audio_type" );
176 char *audio_file = mlt_properties_get( properties, "audio_file" );
177 int frequency = mlt_properties_get_int( properties, "audio_rate" );
178 int channels = mlt_properties_get_int( properties, "audio_channels" );
179 int track = mlt_properties_get_int( properties, "audio_track" );
180 char command[ 1024 ] = "";
181
182 sprintf( command, "%s/ffmpeg/audio.sh \"%s\" \"%s\" %f %d %d %d 2>/dev/null",
183 mlt_prefix,
184 audio_type,
185 audio_file,
186 ( float )position,
187 frequency,
188 channels,
189 track );
190
191 this->audio = popen( command, "r" );
192 }
193 }
194 return this->audio;
195 }
196
197 static void producer_ffmpeg_position( producer_ffmpeg this, uint64_t requested, int *skip )
198 {
199 *skip = 0;
200
201 if ( this->open && requested > this->expected )
202 {
203 // Skip the following n frames
204 *skip = requested - this->expected;
205 }
206 else if ( requested != this->expected )
207 {
208 // Close the video pipe
209 if ( this->video != NULL )
210 pclose( this->video );
211 this->video = NULL;
212
213 // Close the audio pipe
214 if ( this->audio != NULL )
215 pclose( this->audio );
216 this->audio = NULL;
217
218 // We should not be open now
219 this->open = 0;
220 this->end_of_video = 0;
221 this->end_of_audio = 0;
222 }
223
224 // This is the next frame we expect
225 this->expected = requested + 1;
226
227 // Open the pipe
228 this->video = producer_ffmpeg_run_video( this, 0 );
229
230 // Open the audio pipe
231 this->audio = producer_ffmpeg_run_audio( this, 0 );
232
233 // We should be open now
234 this->open = 1;
235 }
236
237 static int sample_calculator( float fps, int frequency, int64_t position )
238 {
239 int samples = 0;
240
241 if ( fps > 29 && fps <= 30 )
242 {
243 samples = frequency / 30;
244
245 switch ( frequency )
246 {
247 case 48000:
248 if ( position % 5 != 0 )
249 samples += 2;
250 break;
251 case 44100:
252 if ( position % 300 == 0 )
253 samples = 1471;
254 else if ( position % 30 == 0 )
255 samples = 1470;
256 else if ( position % 2 == 0 )
257 samples = 1472;
258 else
259 samples = 1471;
260 break;
261 case 32000:
262 if ( position % 30 == 0 )
263 samples = 1068;
264 else if ( position % 29 == 0 )
265 samples = 1067;
266 else if ( position % 4 == 2 )
267 samples = 1067;
268 else
269 samples = 1068;
270 break;
271 default:
272 samples = 0;
273 }
274 }
275 else if ( fps != 0 )
276 {
277 samples = frequency / fps;
278 }
279
280 return samples;
281 }
282
283 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
284 {
285 // Get the frames properties
286 mlt_properties properties = mlt_frame_properties( this );
287
288 producer_ffmpeg producer = mlt_properties_get_data( properties, "producer_ffmpeg", NULL );
289 mlt_properties producer_properties = mlt_producer_properties( &producer->parent );
290
291 int64_t target = mlt_properties_get_double( properties, "target" );
292 int skip = mlt_properties_get_int( properties, "skip" );
293
294 float fps = mlt_properties_get_double( producer_properties, "fps" );
295 *frequency = mlt_properties_get_int( producer_properties, "audio_rate" );
296 *channels = mlt_properties_get_int( producer_properties, "audio_channels" );
297
298 // Maximum Size (?)
299 int size = ( *frequency / 25 ) * *channels * 2;
300
301 // Allocate an image
302 *buffer = malloc( size );
303
304 // Read it
305 if ( producer->audio != NULL )
306 {
307 do
308 {
309 *samples = sample_calculator( fps, *frequency, target - skip );
310 if ( fread( *buffer, *samples * *channels * 2, 1, producer->audio ) != 1 )
311 {
312 pclose( producer->audio );
313 producer->audio = NULL;
314 producer->end_of_audio = 1;
315 }
316 }
317 while( producer->audio != NULL && skip -- );
318 }
319 else
320 {
321 *samples = sample_calculator( fps, *frequency, target );
322 memset( *buffer, 0, size );
323 }
324
325 // Pass the data on the frame properties
326 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
327
328 // Set the producer properties
329 mlt_properties_set_int( producer_properties, "end_of_clip", producer->end_of_video && producer->end_of_audio );
330
331 return 0;
332 }
333
334 static int read_ffmpeg_header( producer_ffmpeg this, int *width, int *height )
335 {
336 int count = 0;
337 char temp[ 132 ];
338 FILE *video = this->video;
339
340 if ( fgets( temp, 132, video ) )
341 {
342 if ( strncmp( temp, "FRAME", 5 ) )
343 {
344 if ( strstr( temp, " W" ) != NULL )
345 *width = atoi( strstr( temp, " W" ) + 2 );
346 if ( strstr( temp, " H" ) != NULL )
347 *height = atoi( strstr( temp, " H" ) + 2 );
348 count = 2;
349 fgets( temp, 132, video );
350 this->width = *width;
351 this->height = *height;
352 }
353 else
354 {
355 *width = this->width;
356 *height = this->height;
357 count = 2;
358 }
359 }
360 return count;
361 }
362
363 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
364 {
365 producer_ffmpeg this = producer->child;
366 int width;
367 int height;
368 int skip;
369
370 // Construct a test frame
371 *frame = mlt_frame_init( );
372
373 // Are we at the position expected?
374 producer_ffmpeg_position( this, mlt_producer_frame( producer ), &skip );
375
376 // Get properties objects
377 mlt_properties producer_properties = mlt_producer_properties( &this->parent );
378
379 // Get the frames properties
380 mlt_properties properties = mlt_frame_properties( *frame );
381
382 FILE *video = this->video;
383
384 mlt_properties_set_double( properties, "target", mlt_producer_frame( producer ) );
385 mlt_properties_set_int( properties, "skip", skip );
386
387 // Read the video
388 if ( video != NULL && read_ffmpeg_header( this, &width, &height ) == 2 )
389 {
390 // Allocate an image
391 uint8_t *image = malloc( width * height * 2 );
392
393 // Read it
394 while( skip -- )
395 {
396 if ( fread( this->buffer, width * height * 3 / 2, 1, video ) == 1 )
397 read_ffmpeg_header( this, &width, &height );
398 else
399 skip = 0;
400 }
401
402 fread( this->buffer, width * height * 3 / 2, 1, video );
403
404 // Convert it
405 mlt_convert_yuv420p_to_yuv422( this->buffer, width, height, width, image );
406
407 // Pass the data on the frame properties
408 mlt_properties_set_data( properties, "image", image, width * height * 2, free, NULL );
409 mlt_properties_set_int( properties, "width", width );
410 mlt_properties_set_int( properties, "height", height );
411 mlt_properties_set_int( properties, "has_image", 1 );
412
413 // Push the image callback
414 mlt_frame_push_get_image( *frame, producer_get_image );
415
416 }
417 else
418 {
419 // Clean up
420 if ( this->video != NULL )
421 {
422 int video_loop = mlt_properties_get_int( producer_properties, "video_loop" );
423
424 // Inform caller that end of clip is reached
425 this->end_of_video = !video_loop;
426 pclose( this->video );
427 this->video = NULL;
428 }
429
430 // Push the image callback
431 mlt_frame_push_get_image( *frame, producer_get_image );
432 }
433
434 // Set the audio pipe
435 mlt_properties_set_data( properties, "producer_ffmpeg", this, 0, NULL, NULL );
436
437 // Hmm - register audio callback
438 ( *frame )->get_audio = producer_get_audio;
439
440 // Get the additional properties
441 double aspect_ratio = mlt_properties_get_double( producer_properties, "aspect_ratio" );
442 double speed = mlt_properties_get_double( producer_properties, "speed" );
443 char *video_file = mlt_properties_get( producer_properties, "video_file" );
444
445 // Set them on the frame
446 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
447 mlt_properties_set_double( properties, "speed", speed );
448 if ( strchr( video_file, '/' ) != NULL )
449 mlt_properties_set( properties, "file", strrchr( video_file, '/' ) + 1 );
450 else
451 mlt_properties_set( properties, "file", video_file );
452
453
454 // Set the out point on the producer
455 if ( this->end_of_video && this->end_of_audio )
456 {
457 mlt_properties_set_int( properties, "end_of_clip", 1 );
458 mlt_properties_set_timecode( producer_properties, "length", mlt_producer_position( &this->parent ) );
459 mlt_producer_set_in_and_out( &this->parent, mlt_producer_get_in( &this->parent ), mlt_producer_position( &this->parent ) );
460 }
461
462 // Update timecode on the frame we're creating
463 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
464
465 // Calculate the next timecode
466 mlt_producer_prepare_next( producer );
467
468 return 0;
469 }
470
471 static void producer_close( mlt_producer parent )
472 {
473 producer_ffmpeg this = parent->child;
474 if ( this->video )
475 pclose( this->video );
476 if ( this->audio )
477 pclose( this->audio );
478 parent->close = NULL;
479 mlt_producer_close( parent );
480 free( this->buffer );
481 free( this );
482 }
483