inigo rewrite, producer, serialise and deserialise
[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 #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
206 this->buffer = malloc( 1024 * 1024 * 2 );
207
208 if ( !usable )
209 {
210 mlt_producer_close( &this->parent );
211 producer = NULL;
212 }
213
214 return producer;
215 }
216 free( this );
217 return NULL;
218 }
219
220 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
221 {
222 // Get the frames properties
223 mlt_properties properties = mlt_frame_properties( this );
224
225 if ( mlt_properties_get_int( properties, "has_image" ) )
226 {
227 // Get width and height
228 *format = mlt_image_yuv422;
229 *width = mlt_properties_get_int( properties, "width" );
230 *height = mlt_properties_get_int( properties, "height" );
231
232 // Specify format and image
233 *buffer = mlt_properties_get_data( properties, "image", NULL );
234 }
235 else
236 {
237 mlt_frame_get_image( this, buffer, format, width, height, writable );
238 }
239
240 return 0;
241 }
242
243 FILE *producer_ffmpeg_run_video( producer_ffmpeg this, mlt_timecode position )
244 {
245 if ( this->video == NULL )
246 {
247 // Get the producer
248 mlt_producer producer = &this->parent;
249
250 // Get the properties of the producer
251 mlt_properties properties = mlt_producer_properties( producer );
252
253 // Get the video loop property
254 int video_loop = mlt_properties_get_int( properties, "video_loop" );
255
256 if ( !this->open || video_loop )
257 {
258 const char *mlt_prefix = mlt_factory_prefix( );
259 char *video_type = mlt_properties_get( properties, "video_type" );
260 char *video_file = mlt_properties_get( properties, "video_file" );
261 float video_rate = mlt_properties_get_double( properties, "fps" );
262 char *video_size = mlt_properties_get( properties, "video_size" );
263 char command[ 1024 ] = "";
264
265 sprintf( command, "%s/ffmpeg/video.sh \"%s\" \"%s\" \"%s\" %f %f 2>/dev/null",
266 mlt_prefix,
267 video_type,
268 video_file,
269 video_size,
270 video_rate,
271 ( float )position );
272
273 this->video_pipe = rwpipe_open( command );
274 this->video = rwpipe_reader( this->video_pipe );
275 }
276 }
277 return this->video;
278 }
279
280 FILE *producer_ffmpeg_run_audio( producer_ffmpeg this, mlt_timecode position )
281 {
282 // Get the producer
283 mlt_producer producer = &this->parent;
284
285 // Get the properties of the producer
286 mlt_properties properties = mlt_producer_properties( producer );
287
288 if ( this->audio == NULL )
289 {
290 int audio_loop = mlt_properties_get_int( properties, "audio_loop" );
291
292 if ( !this->open || audio_loop )
293 {
294 const char *mlt_prefix = mlt_factory_prefix( );
295 char *audio_type = mlt_properties_get( properties, "audio_type" );
296 char *audio_file = mlt_properties_get( properties, "audio_file" );
297 int frequency = mlt_properties_get_int( properties, "audio_rate" );
298 int channels = mlt_properties_get_int( properties, "audio_channels" );
299 int track = mlt_properties_get_int( properties, "audio_track" );
300 char command[ 1024 ] = "";
301
302 sprintf( command, "%s/ffmpeg/audio.sh \"%s\" \"%s\" %f %d %d %d 2>/dev/null",
303 mlt_prefix,
304 audio_type,
305 audio_file,
306 ( float )position,
307 frequency,
308 channels,
309 track );
310
311 this->audio_pipe = rwpipe_open( command );
312 this->audio = rwpipe_reader( this->audio_pipe );
313 }
314 }
315 return this->audio;
316 }
317
318 static void producer_ffmpeg_position( producer_ffmpeg this, uint64_t requested, int *skip )
319 {
320 *skip = 0;
321
322 if ( this->open && requested > this->expected )
323 {
324 // Skip the following n frames
325 *skip = requested - this->expected;
326 }
327 else if ( requested != this->expected )
328 {
329 // Close the video pipe
330 if ( this->video != NULL )
331 rwpipe_close( this->video_pipe );
332 this->video = NULL;
333
334 // Close the audio pipe
335 if ( this->audio != NULL )
336 rwpipe_close( this->audio_pipe );
337 this->audio = NULL;
338
339 // We should not be open now
340 this->open = 0;
341 this->end_of_video = 0;
342 this->end_of_audio = 0;
343 }
344
345 // This is the next frame we expect
346 this->expected = requested + 1;
347
348 // Open the pipe
349 this->video = producer_ffmpeg_run_video( this, 0 );
350
351 // Open the audio pipe
352 this->audio = producer_ffmpeg_run_audio( this, 0 );
353
354 // We should be open now
355 this->open = 1;
356 }
357
358 static int producer_get_audio( mlt_frame this, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
359 {
360 // Get the frames properties
361 mlt_properties properties = mlt_frame_properties( this );
362
363 producer_ffmpeg producer = mlt_properties_get_data( properties, "producer_ffmpeg", NULL );
364 mlt_properties producer_properties = mlt_producer_properties( &producer->parent );
365
366 int64_t target = mlt_properties_get_double( properties, "target" );
367 int skip = mlt_properties_get_int( properties, "skip" );
368
369 float fps = mlt_properties_get_double( producer_properties, "fps" );
370 *frequency = mlt_properties_get_int( producer_properties, "audio_rate" );
371 *channels = mlt_properties_get_int( producer_properties, "audio_channels" );
372
373 // Maximum Size (?)
374 int size = ( *frequency / 25 ) * *channels * 2;
375
376 // Allocate an image
377 *buffer = malloc( size );
378
379 // Read it
380 if ( producer->audio != NULL )
381 {
382 do
383 {
384 *samples = mlt_sample_calculator( fps, *frequency, target - skip );
385 if ( fread( *buffer, *samples * *channels * 2, 1, producer->audio ) != 1 )
386 {
387 rwpipe_close( producer->audio_pipe );
388 producer->audio = NULL;
389 producer->end_of_audio = 1;
390 }
391 }
392 while( producer->audio != NULL && skip -- );
393 }
394 else
395 {
396 *samples = mlt_sample_calculator( fps, *frequency, target );
397 memset( *buffer, 0, size );
398 }
399
400 // Pass the data on the frame properties
401 mlt_properties_set_data( properties, "audio", *buffer, size, free, NULL );
402
403 // Set the producer properties
404 mlt_properties_set_int( producer_properties, "end_of_clip", producer->end_of_video && producer->end_of_audio );
405
406 return 0;
407 }
408
409 static int read_ffmpeg_header( producer_ffmpeg this, int *width, int *height )
410 {
411 int count = 0;
412 char temp[ 132 ];
413 FILE *video = this->video;
414
415 if ( fgets( temp, 132, video ) )
416 {
417 if ( strncmp( temp, "FRAME", 5 ) )
418 {
419 if ( strstr( temp, " W" ) != NULL )
420 *width = atoi( strstr( temp, " W" ) + 2 );
421 if ( strstr( temp, " H" ) != NULL )
422 *height = atoi( strstr( temp, " H" ) + 2 );
423 count = 2;
424 fgets( temp, 132, video );
425 this->width = *width;
426 this->height = *height;
427 }
428 else
429 {
430 *width = this->width;
431 *height = this->height;
432 count = 2;
433 }
434 }
435 return count;
436 }
437
438 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
439 {
440 producer_ffmpeg this = producer->child;
441 int width;
442 int height;
443 int skip;
444
445 // Construct a test frame
446 *frame = mlt_frame_init( );
447
448 // Are we at the position expected?
449 producer_ffmpeg_position( this, mlt_producer_frame( producer ), &skip );
450
451 // Get properties objects
452 mlt_properties producer_properties = mlt_producer_properties( &this->parent );
453
454 // Get the frames properties
455 mlt_properties properties = mlt_frame_properties( *frame );
456
457 FILE *video = this->video;
458
459 mlt_properties_set_double( properties, "target", mlt_producer_frame( producer ) );
460 mlt_properties_set_int( properties, "skip", skip );
461
462 // Read the video
463 if ( video != NULL && read_ffmpeg_header( this, &width, &height ) == 2 )
464 {
465 // Allocate an image
466 uint8_t *image = malloc( width * height * 2 );
467
468 // Read it
469 while( skip -- )
470 {
471 if ( fread( this->buffer, width * height * 3 / 2, 1, video ) == 1 )
472 read_ffmpeg_header( this, &width, &height );
473 else
474 skip = 0;
475 }
476
477 fread( this->buffer, width * height * 3 / 2, 1, video );
478
479 // Convert it
480 mlt_convert_yuv420p_to_yuv422( this->buffer, width, height, width, image );
481
482 // Pass the data on the frame properties
483 mlt_properties_set_data( properties, "image", image, width * height * 2, free, NULL );
484 mlt_properties_set_int( properties, "width", width );
485 mlt_properties_set_int( properties, "height", height );
486 mlt_properties_set_int( properties, "has_image", 1 );
487
488 // Push the image callback
489 mlt_frame_push_get_image( *frame, producer_get_image );
490
491 }
492 else
493 {
494 // Clean up
495 if ( this->video != NULL )
496 {
497 int video_loop = mlt_properties_get_int( producer_properties, "video_loop" );
498
499 // Inform caller that end of clip is reached
500 this->end_of_video = !video_loop;
501 rwpipe_close( this->video_pipe );
502 this->video = NULL;
503 }
504
505 // Push the image callback
506 mlt_frame_push_get_image( *frame, producer_get_image );
507 }
508
509 // Set the audio pipe
510 mlt_properties_set_data( properties, "producer_ffmpeg", this, 0, NULL, NULL );
511
512 // Hmm - register audio callback
513 ( *frame )->get_audio = producer_get_audio;
514
515 // Get the additional properties
516 double aspect_ratio = mlt_properties_get_double( producer_properties, "aspect_ratio" );
517 double speed = mlt_properties_get_double( producer_properties, "speed" );
518 char *video_file = mlt_properties_get( producer_properties, "video_file" );
519
520 // Set them on the frame
521 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
522 mlt_properties_set_double( properties, "speed", speed );
523 if ( strchr( video_file, '/' ) != NULL )
524 mlt_properties_set( properties, "file", strrchr( video_file, '/' ) + 1 );
525 else
526 mlt_properties_set( properties, "file", video_file );
527
528
529 // Set the out point on the producer
530 if ( this->end_of_video && this->end_of_audio )
531 {
532 mlt_properties_set_int( properties, "end_of_clip", 1 );
533 mlt_properties_set_timecode( producer_properties, "length", mlt_producer_position( &this->parent ) );
534 mlt_producer_set_in_and_out( &this->parent, mlt_producer_get_in( &this->parent ), mlt_producer_position( &this->parent ) );
535 }
536
537 // Update timecode on the frame we're creating
538 mlt_frame_set_timecode( *frame, mlt_producer_position( producer ) );
539
540 // Calculate the next timecode
541 mlt_producer_prepare_next( producer );
542
543 return 0;
544 }
545
546 static void producer_close( mlt_producer parent )
547 {
548 producer_ffmpeg this = parent->child;
549 if ( this->video )
550 rwpipe_close( this->video_pipe );
551 if ( this->audio )
552 rwpipe_close( this->audio_pipe );
553 parent->close = NULL;
554 mlt_producer_close( parent );
555 free( this->buffer );
556 free( this );
557 }
558