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