]> git.sesse.net Git - mlt/blob - mlt/src/modules/ffmpeg/producer_ffmpeg.c
miracle part 1
[mlt] / 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 #include <ctype.h>
30 #include <sys/types.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33
34 typedef struct producer_ffmpeg_s *producer_ffmpeg;
35
36 /** Bi-directional pipe structure.
37 */
38
39 typedef struct rwpipe
40 {
41     int pid;
42     FILE *reader;
43     FILE *writer;
44 }
45 rwpipe;
46
47 /** Create a bidirectional pipe for the given command.
48 */
49
50 rwpipe *rwpipe_open( char *command )
51 {
52     rwpipe *this = malloc( sizeof( rwpipe ) );
53
54     if ( this != NULL )
55     {
56         int input[ 2 ];
57         int output[ 2 ];
58
59         pipe( input );
60         pipe( output );
61
62         this->pid = fork();
63
64         if ( this->pid == 0 )
65         {
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 );
72
73             dup2( output[ 0 ], STDIN_FILENO );
74             dup2( input[ 1 ], STDOUT_FILENO );
75
76             close( input[ 0 ] );
77             close( input[ 1 ] );
78             close( output[ 0 ] );
79             close( output[ 1 ] );
80
81                         execl( "/bin/sh", "sh", "-c", command, NULL );
82             exit( 255 );
83         }
84         else
85         {
86                         setpgid( this->pid, this->pid );
87
88             close( input[ 1 ] );
89             close( output[ 0 ] );
90
91             this->reader = fdopen( input[ 0 ], "r" );
92             this->writer = fdopen( output[ 1 ], "w" );
93         }
94     }
95
96     return this;
97 }
98
99 /** Read data from the pipe.
100 */
101
102 FILE *rwpipe_reader( rwpipe *this )
103 {
104     if ( this != NULL )
105         return this->reader;
106     else
107         return NULL;
108 }
109
110 /** Write data to the pipe.
111 */
112
113 FILE *rwpipe_writer( rwpipe *this )
114 {
115     if ( this != NULL )
116         return this->writer;
117     else
118         return NULL;
119 }
120
121 /** Close the pipe and process.
122 */
123
124 void rwpipe_close( rwpipe *this )
125 {
126     if ( this != NULL )
127     {
128                 fclose( this->reader );
129                 fclose( this->writer );
130                 kill( - this->pid, SIGKILL );
131         waitpid( - this->pid, NULL, 0 );
132         free( this );
133     }
134 }
135
136 struct producer_ffmpeg_s
137 {
138         struct mlt_producer_s parent;
139         rwpipe *video_pipe;
140         rwpipe *audio_pipe;
141         FILE *video;
142         FILE *audio;
143         uint64_t expected;
144         uint8_t *buffer;
145         int open;
146         int width;
147         int height;
148         int end_of_video;
149         int end_of_audio;
150 };
151
152 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
153 static void producer_close( mlt_producer parent );
154
155 /** Consutruct an ffmpeg producer.
156 */
157
158 mlt_producer producer_ffmpeg_init( char *file )
159 {
160         producer_ffmpeg this = calloc( sizeof( struct producer_ffmpeg_s ), 1 );
161         if ( file != NULL && this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
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                 // Override get_frame and close methods
170                 producer->get_frame = producer_get_frame;
171                 producer->close = producer_close;
172
173                 // Set the properties
174                 mlt_properties_set( properties, "mlt_type", "producer_ffmpeg" );
175
176                 if ( !strcmp( file, "v4l" ) )
177                 {
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" );
183                 }
184                 else
185                 {
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 );
191                 }
192
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 );
196
197                 mlt_properties_set( properties, "log_id", file );
198                 mlt_properties_set( properties, "resource", file );
199
200                 this->buffer = malloc( 1024 * 1024 * 2 );
201
202                 return producer;
203         }
204         free( this );
205         return NULL;
206 }
207
208 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
209 {
210         // Get the frames properties
211         mlt_properties properties = mlt_frame_properties( this );
212
213         if ( mlt_properties_get_int( properties, "has_image" ) )
214         {
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" );
219
220                 // Specify format and image
221                 *buffer = mlt_properties_get_data( properties, "image", NULL );
222         }
223         else
224         {
225                 mlt_frame_get_image( this, buffer, format, width, height, writable );
226         }
227
228         return 0;
229 }
230
231 FILE *producer_ffmpeg_run_video( producer_ffmpeg this, mlt_timecode position )
232 {
233         if ( this->video == NULL )
234         {
235                 // Get the producer
236                 mlt_producer producer = &this->parent;
237
238                 // Get the properties of the producer
239                 mlt_properties properties = mlt_producer_properties( producer );
240
241                 // Get the video loop property
242                 int video_loop = mlt_properties_get_int( properties, "video_loop" );
243
244                 if ( !this->open || video_loop )
245                 {
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 ] = "";
252
253                         sprintf( command, "%s/ffmpeg/video.sh \"%s\" \"%s\" \"%s\" %f %f 2>/dev/null",
254                                                           mlt_prefix,
255                                                           video_type,
256                                                           video_file,
257                                                           video_size,
258                                                           video_rate,
259                                                           ( float )position );
260
261                         this->video_pipe = rwpipe_open( command );
262                         this->video = rwpipe_reader( this->video_pipe );
263                 }
264         }
265         return this->video;
266 }
267
268 FILE *producer_ffmpeg_run_audio( producer_ffmpeg this, mlt_timecode position )
269 {
270         // Get the producer
271         mlt_producer producer = &this->parent;
272
273         // Get the properties of the producer
274         mlt_properties properties = mlt_producer_properties( producer );
275
276         if ( this->audio == NULL )
277         {
278                 int audio_loop = mlt_properties_get_int( properties, "audio_loop" );
279
280                 if ( !this->open || audio_loop )
281                 {
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 ] = "";
289
290                         sprintf( command, "%s/ffmpeg/audio.sh \"%s\" \"%s\" %f %d %d %d 2>/dev/null",
291                                                           mlt_prefix,
292                                                           audio_type,
293                                                           audio_file,
294                                                           ( float )position,
295                                                           frequency,
296                                                           channels,
297                                                           track );
298
299                         this->audio_pipe = rwpipe_open( command );
300                         this->audio = rwpipe_reader( this->audio_pipe );
301                 }
302         }
303         return this->audio;
304 }
305
306 static void producer_ffmpeg_position( producer_ffmpeg this, uint64_t requested, int *skip )
307 {
308         *skip = 0;
309
310         if ( this->open && requested > this->expected )
311         {
312                 // Skip the following n frames
313                 *skip = requested - this->expected;
314         }
315         else if ( requested != this->expected )
316         {
317                 // Close the video pipe
318                 if ( this->video != NULL )
319                         rwpipe_close( this->video_pipe );
320                 this->video = NULL;
321
322                 // Close the audio pipe
323                 if ( this->audio != NULL )
324                         rwpipe_close( this->audio_pipe );
325                 this->audio = NULL;
326         
327                 // We should not be open now
328                 this->open = 0;
329                 this->end_of_video = 0;
330                 this->end_of_audio = 0;
331         }
332
333         // This is the next frame we expect
334         this->expected = requested + 1;
335
336         // Open the pipe
337         this->video = producer_ffmpeg_run_video( this, 0 );
338
339         // Open the audio pipe
340         this->audio = producer_ffmpeg_run_audio( this, 0 );
341
342         // We should be open now
343         this->open = 1;
344 }
345
346 static int sample_calculator( float fps, int frequency, int64_t position )
347 {
348         int samples = 0;
349
350         if ( fps > 29 && fps <= 30 )
351         {
352                 samples = frequency / 30;
353
354                 switch ( frequency )
355                 {
356                         case 48000:
357                                 if ( position % 5 != 0 )
358                                         samples += 2;
359                                 break;
360                         case 44100:
361                                 if ( position % 300 == 0 )
362                                         samples = 1471;
363                                 else if ( position % 30 == 0 )
364                                         samples = 1470;
365                                 else if ( position % 2 == 0 )
366                                         samples = 1472;
367                                 else
368                                         samples = 1471;
369                                 break;
370                         case 32000:
371                                 if ( position % 30 == 0 )
372                                         samples = 1068;
373                                 else if ( position % 29 == 0 )
374                                         samples = 1067;
375                                 else if ( position % 4 == 2 )
376                                         samples = 1067;
377                                 else
378                                         samples = 1068;
379                                 break;
380                         default:
381                                 samples = 0;
382                 }
383         }
384         else if ( fps != 0 )
385         {
386                 samples = frequency / fps;
387         }
388
389         return samples;
390 }
391
392 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
393 {
394         // Get the frames properties
395         mlt_properties properties = mlt_frame_properties( this );
396
397         producer_ffmpeg producer = mlt_properties_get_data( properties, "producer_ffmpeg", NULL );
398         mlt_properties producer_properties = mlt_producer_properties( &producer->parent );
399
400         int64_t target = mlt_properties_get_double( properties, "target" );
401         int skip = mlt_properties_get_int( properties, "skip" );
402
403         float fps = mlt_properties_get_double( producer_properties, "fps" );
404         *frequency = mlt_properties_get_int( producer_properties, "audio_rate" );
405         *channels = mlt_properties_get_int( producer_properties, "audio_channels" );
406
407         // Maximum Size (?)
408         int size = ( *frequency / 25 ) * *channels * 2;
409
410         // Allocate an image
411         *buffer = malloc( size );
412                 
413         // Read it
414         if ( producer->audio != NULL )
415         {
416                 do
417                 {
418                         *samples = sample_calculator( fps, *frequency, target - skip );
419                         if ( fread( *buffer, *samples * *channels * 2, 1, producer->audio ) != 1 )
420                         {
421                                 rwpipe_close( producer->audio_pipe );
422                                 producer->audio = NULL;
423                                 producer->end_of_audio = 1;
424                         }
425                 }
426                 while( producer->audio != NULL && skip -- );
427         }
428         else
429         {
430                 *samples = sample_calculator( fps, *frequency, target );
431                 memset( *buffer, 0, size );
432         }
433
434         // Pass the data on the frame properties
435         mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
436
437         // Set the producer properties
438         mlt_properties_set_int( producer_properties, "end_of_clip", producer->end_of_video && producer->end_of_audio );
439
440         return 0;
441 }
442
443 static int read_ffmpeg_header( producer_ffmpeg this, int *width, int *height )
444 {
445         int count = 0;
446         char temp[ 132 ];
447         FILE *video = this->video;
448
449         if ( fgets( temp, 132, video ) )
450         {
451                 if ( strncmp( temp, "FRAME", 5 ) )
452                 {
453                         if ( strstr( temp, " W" ) != NULL )
454                                 *width = atoi( strstr( temp, " W" ) + 2 );
455                         if ( strstr( temp, " H" ) != NULL )
456                                 *height = atoi( strstr( temp, " H" ) + 2 );
457                         count = 2;
458                         fgets( temp, 132, video );
459                         this->width = *width;
460                         this->height = *height;
461                 }
462                 else
463                 {
464                         *width = this->width;
465                         *height = this->height;
466                         count = 2;
467                 }
468         }
469         return count;
470 }
471
472 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
473 {
474         producer_ffmpeg this = producer->child;
475         int width;
476         int height;
477         int skip;
478
479         // Construct a test frame
480         *frame = mlt_frame_init( );
481
482         // Are we at the position expected?
483         producer_ffmpeg_position( this, mlt_producer_frame( producer ), &skip );
484
485         // Get properties objects
486         mlt_properties producer_properties = mlt_producer_properties( &this->parent );
487
488         // Get the frames properties
489         mlt_properties properties = mlt_frame_properties( *frame );
490
491         FILE *video = this->video;
492
493         mlt_properties_set_double( properties, "target", mlt_producer_frame( producer ) );
494         mlt_properties_set_int( properties, "skip", skip );
495
496         // Read the video
497         if ( video != NULL && read_ffmpeg_header( this, &width, &height ) == 2 )
498         {
499                 // Allocate an image
500                 uint8_t *image = malloc( width * height * 2 );
501                 
502                 // Read it
503                 while( skip -- )
504                 {
505                         if ( fread( this->buffer, width * height * 3 / 2, 1, video ) == 1 )
506                                 read_ffmpeg_header( this, &width, &height );
507                         else
508                                 skip = 0;
509                 }
510
511                 fread( this->buffer, width * height * 3 / 2, 1, video );
512
513                 // Convert it
514                 mlt_convert_yuv420p_to_yuv422( this->buffer, width, height, width, image );
515
516                 // Pass the data on the frame properties
517                 mlt_properties_set_data( properties, "image", image, width * height * 2, free, NULL );
518                 mlt_properties_set_int( properties, "width", width );
519                 mlt_properties_set_int( properties, "height", height );
520                 mlt_properties_set_int( properties, "has_image", 1 );
521
522                 // Push the image callback
523                 mlt_frame_push_get_image( *frame, producer_get_image );
524
525         }
526         else
527         {
528                 // Clean up
529                 if ( this->video != NULL )
530                 {
531                         int video_loop = mlt_properties_get_int( producer_properties, "video_loop" );
532
533                         // Inform caller that end of clip is reached
534                         this->end_of_video = !video_loop;
535                         rwpipe_close( this->video_pipe );
536                         this->video = NULL;
537                 }
538
539                 // Push the image callback
540                 mlt_frame_push_get_image( *frame, producer_get_image );
541         }
542
543         // Set the audio pipe
544         mlt_properties_set_data( properties, "producer_ffmpeg", this, 0, NULL, NULL );
545
546         // Hmm - register audio callback
547         ( *frame )->get_audio = producer_get_audio;
548
549         // Get the additional properties
550         double aspect_ratio = mlt_properties_get_double( producer_properties, "aspect_ratio" );
551         double speed = mlt_properties_get_double( producer_properties, "speed" );
552         char *video_file = mlt_properties_get( producer_properties, "video_file" );
553
554         // Set them on the frame
555         mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
556         mlt_properties_set_double( properties, "speed", speed );
557         if ( strchr( video_file, '/' ) != NULL )
558                 mlt_properties_set( properties, "file", strrchr( video_file, '/' ) + 1 );
559         else
560                 mlt_properties_set( properties, "file", video_file );
561
562
563         // Set the out point on the producer
564         if ( this->end_of_video && this->end_of_audio )
565         {
566                 mlt_properties_set_int( properties, "end_of_clip", 1 );
567                 mlt_properties_set_timecode( producer_properties, "length", mlt_producer_position( &this->parent ) );
568                 mlt_producer_set_in_and_out( &this->parent, mlt_producer_get_in( &this->parent ), mlt_producer_position( &this->parent ) );
569         }
570
571         // Update timecode on the frame we're creating
572         mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
573
574         // Calculate the next timecode
575         mlt_producer_prepare_next( producer );
576
577         return 0;
578 }
579
580 static void producer_close( mlt_producer parent )
581 {
582         producer_ffmpeg this = parent->child;
583         if ( this->video )
584                 rwpipe_close( this->video_pipe );
585         if ( this->audio )
586                 rwpipe_close( this->audio_pipe );
587         parent->close = NULL;
588         mlt_producer_close( parent );
589         free( this->buffer );
590         free( this );
591 }
592