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