producer_avformat.c: add version check to use AVCodec->long_name
[melted] / src / modules / avformat / producer_avformat.c
1 /*
2 * producer_avformat.c -- avformat producer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Much code borrowed from ffmpeg.c: Copyright (c) 2000-2003 Fabrice Bellard
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 // MLT Header files
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_frame.h>
25
26 // ffmpeg Header files
27 #include <avformat.h>
28 #include <opt.h>
29 #ifdef SWSCALE
30 # include <swscale.h>
31 #endif
32 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
33 # include "audioconvert.h"
34 #endif
35
36 // System header files
37 #include <stdlib.h>
38 #include <string.h>
39 #include <pthread.h>
40 #include <math.h>
41
42 void avformat_lock( );
43 void avformat_unlock( );
44
45 // Forward references.
46 static int producer_open( mlt_producer this, mlt_profile profile, char *file );
47 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
48
49 /** Constructor for libavformat.
50 */
51
52 mlt_producer producer_avformat_init( mlt_profile profile, char *file )
53 {
54 int error = 0;
55
56 // Report information about available demuxers and codecs as YAML Tiny
57 if ( file && strstr( file, "f-list" ) )
58 {
59 fprintf( stderr, "---\nformats:\n" );
60 AVInputFormat *format = NULL;
61 while ( ( format = av_iformat_next( format ) ) )
62 fprintf( stderr, " - %s\n", format->name );
63 fprintf( stderr, "...\n" );
64 error = 1;
65 }
66 if ( file && strstr( file, "acodec-list" ) )
67 {
68 fprintf( stderr, "---\naudio_codecs:\n" );
69 AVCodec *codec = NULL;
70 while ( ( codec = av_codec_next( codec ) ) )
71 if ( codec->decode && codec->type == CODEC_TYPE_AUDIO )
72 fprintf( stderr, " - %s\n", codec->name );
73 fprintf( stderr, "...\n" );
74 error = 1;
75 }
76 if ( file && strstr( file, "vcodec-list" ) )
77 {
78 fprintf( stderr, "---\nvideo_codecs:\n" );
79 AVCodec *codec = NULL;
80 while ( ( codec = av_codec_next( codec ) ) )
81 if ( codec->decode && codec->type == CODEC_TYPE_VIDEO )
82 fprintf( stderr, " - %s\n", codec->name );
83 fprintf( stderr, "...\n" );
84 error = 1;
85 }
86 if ( error )
87 return NULL;
88
89 mlt_producer this = NULL;
90
91 // Check that we have a non-NULL argument
92 if ( file != NULL )
93 {
94 // Construct the producer
95 this = calloc( 1, sizeof( struct mlt_producer_s ) );
96
97 // Initialise it
98 if ( mlt_producer_init( this, NULL ) == 0 )
99 {
100 // Get the properties
101 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
102
103 // Set the resource property (required for all producers)
104 mlt_properties_set( properties, "resource", file );
105
106 // Register our get_frame implementation
107 this->get_frame = producer_get_frame;
108
109 // Open the file
110 if ( producer_open( this, profile, file ) != 0 )
111 {
112 // Clean up
113 mlt_producer_close( this );
114 this = NULL;
115 }
116 else
117 {
118 // Close the file to release resources for large playlists - reopen later as needed
119 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
120 mlt_properties_set_data( properties, "audio_context", NULL, 0, NULL, NULL );
121 mlt_properties_set_data( properties, "video_context", NULL, 0, NULL, NULL );
122
123 // Default the user-selectable indices from the auto-detected indices
124 mlt_properties_set_int( properties, "audio_index", mlt_properties_get_int( properties, "_audio_index" ) );
125 mlt_properties_set_int( properties, "video_index", mlt_properties_get_int( properties, "_video_index" ) );
126 }
127 }
128 }
129
130 return this;
131 }
132
133 /** Find the default streams.
134 */
135
136 static mlt_properties find_default_streams( mlt_properties meta_media, AVFormatContext *context, int *audio_index, int *video_index )
137 {
138 int i;
139 char key[200];
140
141 mlt_properties_set_int( meta_media, "meta.media.nb_streams", context->nb_streams );
142
143 // Allow for multiple audio and video streams in the file and select first of each (if available)
144 for( i = 0; i < context->nb_streams; i++ )
145 {
146 // Get the codec context
147 AVStream *stream = context->streams[ i ];
148 if ( ! stream ) continue;
149 AVCodecContext *codec_context = stream->codec;
150 if ( ! codec_context ) continue;
151 AVCodec *codec = avcodec_find_decoder( codec_context->codec_id );
152 if ( ! codec ) continue;
153
154 snprintf( key, sizeof(key), "meta.media.%d.stream.type", i );
155
156 // Determine the type and obtain the first index of each type
157 switch( codec_context->codec_type )
158 {
159 case CODEC_TYPE_VIDEO:
160 if ( *video_index < 0 )
161 *video_index = i;
162 mlt_properties_set( meta_media, key, "video" );
163 snprintf( key, sizeof(key), "meta.media.%d.stream.frame_rate", i );
164 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->r_frame_rate ) );
165 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
166 snprintf( key, sizeof(key), "meta.media.%d.stream.sample_aspect_ratio", i );
167 mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->sample_aspect_ratio ) );
168 #endif
169 snprintf( key, sizeof(key), "meta.media.%d.codec.pix_fmt", i );
170 mlt_properties_set( meta_media, key, avcodec_get_pix_fmt_name( codec_context->pix_fmt ) );
171 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_aspect_ratio", i );
172 mlt_properties_set_double( meta_media, key, av_q2d( codec_context->sample_aspect_ratio ) );
173 break;
174 case CODEC_TYPE_AUDIO:
175 if ( *audio_index < 0 )
176 *audio_index = i;
177 mlt_properties_set( meta_media, key, "audio" );
178 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
179 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_fmt", i );
180 mlt_properties_set( meta_media, key, avcodec_get_sample_fmt_name( codec_context->sample_fmt ) );
181 #endif
182 snprintf( key, sizeof(key), "meta.media.%d.codec.sample_rate", i );
183 mlt_properties_set_int( meta_media, key, codec_context->sample_rate );
184 snprintf( key, sizeof(key), "meta.media.%d.codec.channels", i );
185 mlt_properties_set_int( meta_media, key, codec_context->channels );
186 break;
187 default:
188 break;
189 }
190 // snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
191 // mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
192 snprintf( key, sizeof(key), "meta.media.%d.codec.name", i );
193 mlt_properties_set( meta_media, key, codec->name );
194 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
195 snprintf( key, sizeof(key), "meta.media.%d.codec.long_name", i );
196 mlt_properties_set( meta_media, key, codec->long_name );
197 #endif
198 snprintf( key, sizeof(key), "meta.media.%d.codec.bit_rate", i );
199 mlt_properties_set_int( meta_media, key, codec_context->bit_rate );
200 // snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
201 // mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
202 snprintf( key, sizeof(key), "meta.media.%d.codec.profile", i );
203 mlt_properties_set_int( meta_media, key, codec_context->profile );
204 snprintf( key, sizeof(key), "meta.media.%d.codec.level", i );
205 mlt_properties_set_int( meta_media, key, codec_context->level );
206 }
207
208 return meta_media;
209 }
210
211 /** Producer file destructor.
212 */
213
214 static void producer_file_close( void *context )
215 {
216 if ( context != NULL )
217 {
218 // Lock the mutex now
219 avformat_lock( );
220
221 // Close the file
222 av_close_input_file( context );
223
224 // Unlock the mutex now
225 avformat_unlock( );
226 }
227 }
228
229 /** Producer file destructor.
230 */
231
232 static void producer_codec_close( void *codec )
233 {
234 if ( codec != NULL )
235 {
236 // Lock the mutex now
237 avformat_lock( );
238
239 // Close the file
240 avcodec_close( codec );
241
242 // Unlock the mutex now
243 avformat_unlock( );
244 }
245 }
246
247 static inline int dv_is_pal( AVPacket *pkt )
248 {
249 return pkt->data[3] & 0x80;
250 }
251
252 static int dv_is_wide( AVPacket *pkt )
253 {
254 int i = 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
255
256 for ( ; i < pkt->size; i += 5 /* packet size */ )
257 {
258 if ( pkt->data[ i ] == 0x61 )
259 {
260 uint8_t x = pkt->data[ i + 2 ] & 0x7;
261 return ( x == 2 ) || ( x == 7 );
262 }
263 }
264 return 0;
265 }
266
267 static double get_aspect_ratio( AVStream *stream, AVCodecContext *codec_context, AVPacket *pkt )
268 {
269 double aspect_ratio = 1.0;
270
271 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
272 {
273 if ( pkt )
274 {
275 if ( dv_is_pal( pkt ) )
276 {
277 aspect_ratio = dv_is_wide( pkt )
278 ? 64.0/45.0 // 16:9 PAL
279 : 16.0/15.0; // 4:3 PAL
280 }
281 else
282 {
283 aspect_ratio = dv_is_wide( pkt )
284 ? 32.0/27.0 // 16:9 NTSC
285 : 8.0/9.0; // 4:3 NTSC
286 }
287 }
288 else
289 {
290 AVRational ar =
291 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
292 stream->sample_aspect_ratio;
293 #else
294 codec_context->sample_aspect_ratio;
295 #endif
296 // Override FFmpeg's notion of DV aspect ratios, which are
297 // based upon a width of 704. Since we do not have a normaliser
298 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
299 // we just coerce the values to facilitate a passive behaviour through
300 // the rescale normaliser when using equivalent producers and consumers.
301 // = display_aspect / (width * height)
302 if ( ar.num == 10 && ar.den == 11 )
303 aspect_ratio = 8.0/9.0; // 4:3 NTSC
304 else if ( ar.num == 59 && ar.den == 54 )
305 aspect_ratio = 16.0/15.0; // 4:3 PAL
306 else if ( ar.num == 40 && ar.den == 33 )
307 aspect_ratio = 32.0/27.0; // 16:9 NTSC
308 else if ( ar.num == 118 && ar.den == 81 )
309 aspect_ratio = 64.0/45.0; // 16:9 PAL
310 }
311 }
312 else
313 {
314 AVRational codec_sar = codec_context->sample_aspect_ratio;
315 AVRational stream_sar =
316 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
317 stream->sample_aspect_ratio;
318 #else
319 { 0, 1 };
320 #endif
321 if ( codec_sar.num > 0 )
322 aspect_ratio = av_q2d( codec_sar );
323 else if ( stream_sar.num > 0 )
324 aspect_ratio = av_q2d( stream_sar );
325 }
326 return aspect_ratio;
327 }
328
329 /** Open the file.
330 */
331
332 static int producer_open( mlt_producer this, mlt_profile profile, char *file )
333 {
334 // Return an error code (0 == no error)
335 int error = 0;
336
337 // Context for avformat
338 AVFormatContext *context = NULL;
339
340 // Get the properties
341 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
342
343 // We will treat everything with the producer fps
344 double fps = mlt_profile_fps( profile );
345
346 // Lock the mutex now
347 avformat_lock( );
348
349 // If "MRL", then create AVInputFormat
350 AVInputFormat *format = NULL;
351 AVFormatParameters *params = NULL;
352 char *standard = NULL;
353 char *mrl = strchr( file, ':' );
354
355 // AV option (0 = both, 1 = video, 2 = audio)
356 int av = 0;
357
358 // Setting lowest log level
359 av_log_set_level( -1 );
360
361 // Only if there is not a protocol specification that avformat can handle
362 if ( mrl && !url_exist( file ) )
363 {
364 // 'file' becomes format abbreviation
365 mrl[0] = 0;
366
367 // Lookup the format
368 format = av_find_input_format( file );
369
370 // Eat the format designator
371 file = ++mrl;
372
373 if ( format )
374 {
375 // Allocate params
376 params = calloc( sizeof( AVFormatParameters ), 1 );
377
378 // These are required by video4linux (defaults)
379 params->width = 640;
380 params->height = 480;
381 params->time_base= (AVRational){1,25};
382 // params->device = file;
383 params->channels = 2;
384 params->sample_rate = 48000;
385 }
386
387 // XXX: this does not work anymore since avdevice
388 // TODO: make producer_avddevice?
389 // Parse out params
390 mrl = strchr( file, '?' );
391 while ( mrl )
392 {
393 mrl[0] = 0;
394 char *name = strdup( ++mrl );
395 char *value = strchr( name, ':' );
396 if ( value )
397 {
398 value[0] = 0;
399 value++;
400 char *t = strchr( value, '&' );
401 if ( t )
402 t[0] = 0;
403 if ( !strcmp( name, "frame_rate" ) )
404 params->time_base.den = atoi( value );
405 else if ( !strcmp( name, "frame_rate_base" ) )
406 params->time_base.num = atoi( value );
407 else if ( !strcmp( name, "sample_rate" ) )
408 params->sample_rate = atoi( value );
409 else if ( !strcmp( name, "channels" ) )
410 params->channels = atoi( value );
411 else if ( !strcmp( name, "width" ) )
412 params->width = atoi( value );
413 else if ( !strcmp( name, "height" ) )
414 params->height = atoi( value );
415 else if ( !strcmp( name, "standard" ) )
416 {
417 standard = strdup( value );
418 params->standard = standard;
419 }
420 else if ( !strcmp( name, "av" ) )
421 av = atoi( value );
422 }
423 free( name );
424 mrl = strchr( mrl, '&' );
425 }
426 }
427
428 // Now attempt to open the file
429 error = av_open_input_file( &context, file, format, 0, params ) < 0;
430
431 // Cleanup AVFormatParameters
432 free( standard );
433 free( params );
434
435 // If successful, then try to get additional info
436 if ( error == 0 )
437 {
438 // Get the stream info
439 error = av_find_stream_info( context ) < 0;
440
441 // Continue if no error
442 if ( error == 0 )
443 {
444 // We will default to the first audio and video streams found
445 int audio_index = -1;
446 int video_index = -1;
447 int av_bypass = 0;
448
449 // Now set properties where we can (use default unknowns if required)
450 if ( context->duration != AV_NOPTS_VALUE )
451 {
452 // This isn't going to be accurate for all formats
453 mlt_position frames = ( mlt_position )( ( ( double )context->duration / ( double )AV_TIME_BASE ) * fps + 0.5 );
454 mlt_properties_set_position( properties, "out", frames - 1 );
455 mlt_properties_set_position( properties, "length", frames );
456 }
457
458 // Find default audio and video streams
459 find_default_streams( properties, context, &audio_index, &video_index );
460
461 if ( context->start_time != AV_NOPTS_VALUE )
462 mlt_properties_set_double( properties, "_start_time", context->start_time );
463
464 // Check if we're seekable (something funny about mpeg here :-/)
465 if ( strcmp( file, "pipe:" ) && strncmp( file, "http://", 6 ) && strncmp( file, "udp:", 4 ) && strncmp( file, "tcp:", 4 ) && strncmp( file, "rtsp:", 5 ) && strncmp( file, "rtp:", 4 ) )
466 {
467 mlt_properties_set_int( properties, "seekable", av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ), AVSEEK_FLAG_BACKWARD ) >= 0 );
468 mlt_properties_set_data( properties, "dummy_context", context, 0, producer_file_close, NULL );
469 av_open_input_file( &context, file, NULL, 0, NULL );
470 av_find_stream_info( context );
471 }
472 else
473 av_bypass = 1;
474
475 // Store selected audio and video indexes on properties
476 mlt_properties_set_int( properties, "_audio_index", audio_index );
477 mlt_properties_set_int( properties, "_video_index", video_index );
478 mlt_properties_set_int( properties, "_last_position", -1 );
479
480 // Fetch the width, height and aspect ratio
481 if ( video_index != -1 )
482 {
483 AVCodecContext *codec_context = context->streams[ video_index ]->codec;
484 mlt_properties_set_int( properties, "width", codec_context->width );
485 mlt_properties_set_int( properties, "height", codec_context->height );
486
487 if ( codec_context->codec_id == CODEC_ID_DVVIDEO )
488 {
489 // Fetch the first frame of DV so we can read it directly
490 AVPacket pkt;
491 int ret = 0;
492 while ( ret >= 0 )
493 {
494 ret = av_read_frame( context, &pkt );
495 if ( ret >= 0 && pkt.stream_index == video_index && pkt.size > 0 )
496 {
497 mlt_properties_set_double( properties, "aspect_ratio",
498 get_aspect_ratio( context->streams[ video_index ], codec_context, &pkt ) );
499 break;
500 }
501 }
502 }
503 else
504 {
505 mlt_properties_set_double( properties, "aspect_ratio",
506 get_aspect_ratio( context->streams[ video_index ], codec_context, NULL ) );
507 }
508 }
509
510 // Read Metadata
511 if (context->title != NULL)
512 mlt_properties_set(properties, "meta.attr.title.markup", context->title );
513 if (context->author != NULL)
514 mlt_properties_set(properties, "meta.attr.author.markup", context->author );
515 if (context->copyright != NULL)
516 mlt_properties_set(properties, "meta.attr.copyright.markup", context->copyright );
517 if (context->comment != NULL)
518 mlt_properties_set(properties, "meta.attr.comment.markup", context->comment );
519 if (context->album != NULL)
520 mlt_properties_set(properties, "meta.attr.album.markup", context->album );
521 if (context->year != 0)
522 mlt_properties_set_int(properties, "meta.attr.year.markup", context->year );
523 if (context->track != 0)
524 mlt_properties_set_int(properties, "meta.attr.track.markup", context->track );
525
526 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
527 if ( av == 0 && audio_index != -1 && video_index != -1 )
528 {
529 // We'll use the open one as our video_context
530 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
531
532 // And open again for our audio context
533 av_open_input_file( &context, file, NULL, 0, NULL );
534 av_find_stream_info( context );
535
536 // Audio context
537 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
538 }
539 else if ( av != 2 && video_index != -1 )
540 {
541 // We only have a video context
542 mlt_properties_set_data( properties, "video_context", context, 0, producer_file_close, NULL );
543 }
544 else if ( audio_index != -1 )
545 {
546 // We only have an audio context
547 mlt_properties_set_data( properties, "audio_context", context, 0, producer_file_close, NULL );
548 }
549 else
550 {
551 // Something has gone wrong
552 error = -1;
553 }
554
555 mlt_properties_set_int( properties, "av_bypass", av_bypass );
556 }
557 }
558
559 // Unlock the mutex now
560 avformat_unlock( );
561
562 return error;
563 }
564
565 /** Convert a frame position to a time code.
566 */
567
568 static double producer_time_of_frame( mlt_producer this, mlt_position position )
569 {
570 return ( double )position / mlt_producer_get_fps( this );
571 }
572
573 static inline void convert_image( AVFrame *frame, uint8_t *buffer, int pix_fmt, mlt_image_format format, int width, int height )
574 {
575 #ifdef SWSCALE
576 if ( format == mlt_image_yuv420p )
577 {
578 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
579 width, height, PIX_FMT_YUV420P, SWS_FAST_BILINEAR, NULL, NULL, NULL);
580 AVPicture output;
581 output.data[0] = buffer;
582 output.data[1] = buffer + width * height;
583 output.data[2] = buffer + ( 3 * width * height ) / 2;
584 output.linesize[0] = width;
585 output.linesize[1] = width >> 1;
586 output.linesize[2] = width >> 1;
587 sws_scale( context, frame->data, frame->linesize, 0, height,
588 output.data, output.linesize);
589 sws_freeContext( context );
590 }
591 else if ( format == mlt_image_rgb24 )
592 {
593 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
594 width, height, PIX_FMT_RGB24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
595 AVPicture output;
596 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
597 sws_scale( context, frame->data, frame->linesize, 0, height,
598 output.data, output.linesize);
599 sws_freeContext( context );
600 }
601 else
602 {
603 struct SwsContext *context = sws_getContext( width, height, pix_fmt,
604 width, height, PIX_FMT_YUYV422, SWS_FAST_BILINEAR, NULL, NULL, NULL);
605 AVPicture output;
606 avpicture_fill( &output, buffer, PIX_FMT_YUYV422, width, height );
607 sws_scale( context, frame->data, frame->linesize, 0, height,
608 output.data, output.linesize);
609 sws_freeContext( context );
610 }
611 #else
612 if ( format == mlt_image_yuv420p )
613 {
614 AVPicture pict;
615 pict.data[0] = buffer;
616 pict.data[1] = buffer + width * height;
617 pict.data[2] = buffer + ( 3 * width * height ) / 2;
618 pict.linesize[0] = width;
619 pict.linesize[1] = width >> 1;
620 pict.linesize[2] = width >> 1;
621 img_convert( &pict, PIX_FMT_YUV420P, (AVPicture *)frame, pix_fmt, width, height );
622 }
623 else if ( format == mlt_image_rgb24 )
624 {
625 AVPicture output;
626 avpicture_fill( &output, buffer, PIX_FMT_RGB24, width, height );
627 img_convert( &output, PIX_FMT_RGB24, (AVPicture *)frame, pix_fmt, width, height );
628 }
629 else
630 {
631 AVPicture output;
632 avpicture_fill( &output, buffer, PIX_FMT_YUV422, width, height );
633 img_convert( &output, PIX_FMT_YUV422, (AVPicture *)frame, pix_fmt, width, height );
634 }
635 #endif
636 }
637
638 /** Allocate the image buffer and set it on the frame.
639 */
640
641 static int allocate_buffer( mlt_properties frame_properties, AVCodecContext *codec_context, uint8_t **buffer, mlt_image_format *format, int *width, int *height )
642 {
643 int size = 0;
644
645 if ( codec_context->width == 0 || codec_context->height == 0 )
646 return size;
647
648 *width = codec_context->width;
649 *height = codec_context->height;
650 mlt_properties_set_int( frame_properties, "width", *width );
651 mlt_properties_set_int( frame_properties, "height", *height );
652
653 switch ( *format )
654 {
655 case mlt_image_yuv420p:
656 size = *width * 3 * ( *height + 1 ) / 2;
657 break;
658 case mlt_image_rgb24:
659 size = *width * ( *height + 1 ) * 3;
660 break;
661 default:
662 *format = mlt_image_yuv422;
663 size = *width * ( *height + 1 ) * 2;
664 break;
665 }
666
667 // Construct the output image
668 *buffer = mlt_pool_alloc( size );
669 if ( *buffer )
670 mlt_properties_set_data( frame_properties, "image", *buffer, size, (mlt_destructor)mlt_pool_release, NULL );
671 else
672 size = 0;
673
674 return size;
675 }
676
677 /** Get an image from a frame.
678 */
679
680 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
681 {
682 // Get the properties from the frame
683 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
684
685 // Obtain the frame number of this frame
686 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
687
688 // Get the producer
689 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
690
691 // Get the producer properties
692 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
693
694 // Fetch the video_context
695 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
696
697 // Get the video_index
698 int index = mlt_properties_get_int( properties, "video_index" );
699
700 // Obtain the expected frame numer
701 mlt_position expected = mlt_properties_get_position( properties, "_video_expected" );
702
703 // Get the video stream
704 AVStream *stream = context->streams[ index ];
705
706 // Get codec context
707 AVCodecContext *codec_context = stream->codec;
708
709 // Packet
710 AVPacket pkt;
711
712 // Get the conversion frame
713 AVFrame *av_frame = mlt_properties_get_data( properties, "av_frame", NULL );
714
715 // Special case pause handling flag
716 int paused = 0;
717
718 // Special case ffwd handling
719 int ignore = 0;
720
721 // We may want to use the source fps if available
722 double source_fps = mlt_properties_get_double( properties, "source_fps" );
723 double fps = mlt_producer_get_fps( this );
724
725 // This is the physical frame position in the source
726 int req_position = ( int )( position / fps * source_fps + 0.5 );
727
728 // Get the seekable status
729 int seekable = mlt_properties_get_int( properties, "seekable" );
730
731 // Hopefully provide better support for streams...
732 int av_bypass = mlt_properties_get_int( properties, "av_bypass" );
733
734 // Determines if we have to decode all frames in a sequence
735 int must_decode = 1;
736
737 // Temporary hack to improve intra frame only
738 must_decode = strcmp( codec_context->codec->name, "mjpeg" ) &&
739 strcmp( codec_context->codec->name, "rawvideo" ) &&
740 strcmp( codec_context->codec->name, "dvvideo" );
741
742 // Seek if necessary
743 if ( position != expected )
744 {
745 if ( av_frame != NULL && position + 1 == expected )
746 {
747 // We're paused - use last image
748 paused = 1;
749 }
750 else if ( !seekable && position > expected && ( position - expected ) < 250 )
751 {
752 // Fast forward - seeking is inefficient for small distances - just ignore following frames
753 ignore = ( int )( ( position - expected ) / fps * source_fps );
754 }
755 else if ( seekable && ( position < expected || position - expected >= 12 ) )
756 {
757 // Calculate the timestamp for the requested frame
758 int64_t timestamp = ( int64_t )( ( double )req_position / source_fps * AV_TIME_BASE + 0.5 );
759 if ( ( uint64_t )context->start_time != AV_NOPTS_VALUE )
760 timestamp += context->start_time;
761 if ( must_decode )
762 timestamp -= AV_TIME_BASE;
763 if ( timestamp < 0 )
764 timestamp = 0;
765
766 // Set to the timestamp
767 av_seek_frame( context, -1, timestamp, AVSEEK_FLAG_BACKWARD );
768
769 // Remove the cached info relating to the previous position
770 mlt_properties_set_int( properties, "_current_position", -1 );
771 mlt_properties_set_int( properties, "_last_position", -1 );
772 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
773 av_frame = NULL;
774 }
775 }
776
777 // Duplicate the last image if necessary (see comment on rawvideo below)
778 int current_position = mlt_properties_get_int( properties, "_current_position" );
779 int got_picture = mlt_properties_get_int( properties, "_got_picture" );
780 if ( av_frame != NULL && got_picture && ( paused || current_position >= req_position ) && av_bypass == 0 )
781 {
782 // Duplicate it
783 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
784 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
785 else
786 mlt_frame_get_image( frame, buffer, format, width, height, writable );
787 }
788 else
789 {
790 int ret = 0;
791 int int_position = 0;
792 got_picture = 0;
793
794 av_init_packet( &pkt );
795
796 // Construct an AVFrame for YUV422 conversion
797 if ( av_frame == NULL )
798 av_frame = avcodec_alloc_frame( );
799
800 while( ret >= 0 && !got_picture )
801 {
802 // Read a packet
803 ret = av_read_frame( context, &pkt );
804
805 // We only deal with video from the selected video_index
806 if ( ret >= 0 && pkt.stream_index == index && pkt.size > 0 )
807 {
808 // Determine time code of the packet
809 int_position = ( int )( av_q2d( stream->time_base ) * pkt.dts * source_fps + 0.5 );
810 if ( context->start_time != AV_NOPTS_VALUE )
811 int_position -= ( int )( context->start_time * source_fps / AV_TIME_BASE + 0.5 );
812 int last_position = mlt_properties_get_int( properties, "_last_position" );
813 if ( int_position == last_position )
814 int_position = last_position + 1;
815 mlt_properties_set_int( properties, "_last_position", int_position );
816
817 // Decode the image
818 if ( must_decode || int_position >= req_position )
819 ret = avcodec_decode_video( codec_context, av_frame, &got_picture, pkt.data, pkt.size );
820
821 if ( got_picture )
822 {
823 // Handle ignore
824 if ( int_position < req_position )
825 {
826 ignore = 0;
827 got_picture = 0;
828 }
829 else if ( int_position >= req_position )
830 {
831 ignore = 0;
832 }
833 else if ( ignore -- )
834 {
835 got_picture = 0;
836 }
837 }
838 av_free_packet( &pkt );
839 }
840 else if ( ret >= 0 )
841 {
842 av_free_packet( &pkt );
843 }
844
845 // Now handle the picture if we have one
846 if ( got_picture )
847 {
848 if ( allocate_buffer( frame_properties, codec_context, buffer, format, width, height ) )
849 {
850 convert_image( av_frame, *buffer, codec_context->pix_fmt, *format, *width, *height );
851 mlt_properties_set_int( frame_properties, "progressive", !av_frame->interlaced_frame );
852 mlt_properties_set_int( properties, "top_field_first", av_frame->top_field_first );
853 mlt_properties_set_int( properties, "_current_position", int_position );
854 mlt_properties_set_int( properties, "_got_picture", 1 );
855 mlt_properties_set_data( properties, "av_frame", av_frame, 0, av_free, NULL );
856 }
857 else
858 {
859 got_picture = 0;
860 }
861 }
862 }
863 if ( !got_picture )
864 mlt_frame_get_image( frame, buffer, format, width, height, writable );
865 }
866
867 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
868 // above will break the pause behaviour - so we wipe the frame now
869 if ( !strcmp( codec_context->codec->name, "rawvideo" ) )
870 mlt_properties_set_data( properties, "av_frame", NULL, 0, NULL, NULL );
871
872 // Set the field order property for this frame
873 mlt_properties_set_int( frame_properties, "top_field_first", mlt_properties_get_int( properties, "top_field_first" ) );
874
875 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
876 mlt_properties_set_position( properties, "_video_expected", position + 1 );
877
878 return 0;
879 }
880
881 /** Process properties as AVOptions and apply to AV context obj
882 */
883
884 static void apply_properties( void *obj, mlt_properties properties, int flags )
885 {
886 int i;
887 int count = mlt_properties_count( properties );
888 for ( i = 0; i < count; i++ )
889 {
890 const char *opt_name = mlt_properties_get_name( properties, i );
891 const AVOption *opt = av_find_opt( obj, opt_name, NULL, flags, flags );
892 if ( opt != NULL )
893 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
894 av_set_string3( obj, opt_name, mlt_properties_get( properties, opt_name), 0, NULL );
895 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
896 av_set_string2( obj, opt_name, mlt_properties_get( properties, opt_name), 0 );
897 #else
898 av_set_string( obj, opt_name, mlt_properties_get( properties, opt_name) );
899 #endif
900 }
901 }
902
903 /** Set up video handling.
904 */
905
906 static void producer_set_up_video( mlt_producer this, mlt_frame frame )
907 {
908 // Get the properties
909 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
910
911 // Fetch the video_context
912 AVFormatContext *context = mlt_properties_get_data( properties, "video_context", NULL );
913
914 // Get the video_index
915 int index = mlt_properties_get_int( properties, "video_index" );
916
917 // Reopen the file if necessary
918 if ( !context && index > -1 )
919 {
920 mlt_events_block( properties, this );
921 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
922 mlt_properties_get( properties, "resource" ) );
923 context = mlt_properties_get_data( properties, "video_context", NULL );
924 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
925 mlt_events_unblock( properties, this );
926
927 // Process properties as AVOptions
928 apply_properties( context, properties, AV_OPT_FLAG_DECODING_PARAM );
929 }
930
931 // Exception handling for video_index
932 if ( context && index >= (int) context->nb_streams )
933 {
934 // Get the last video stream
935 for ( index = context->nb_streams - 1; index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO; --index );
936 mlt_properties_set_int( properties, "video_index", index );
937 }
938 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_VIDEO )
939 {
940 // Invalidate the video stream
941 index = -1;
942 mlt_properties_set_int( properties, "video_index", index );
943 }
944
945 // Get the frame properties
946 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
947
948 if ( context && index > -1 )
949 {
950 // Get the video stream
951 AVStream *stream = context->streams[ index ];
952
953 // Get codec context
954 AVCodecContext *codec_context = stream->codec;
955
956 // Get the codec
957 AVCodec *codec = mlt_properties_get_data( properties, "video_codec", NULL );
958
959 // Update the video properties if the index changed
960 if ( index != mlt_properties_get_int( properties, "_video_index" ) )
961 {
962 // Reset the video properties if the index changed
963 mlt_properties_set_int( properties, "_video_index", index );
964 mlt_properties_set_data( properties, "video_codec", NULL, 0, NULL, NULL );
965 mlt_properties_set_int( properties, "width", codec_context->width );
966 mlt_properties_set_int( properties, "height", codec_context->height );
967 // TODO: get the first usable AVPacket and reset the stream position
968 mlt_properties_set_double( properties, "aspect_ratio",
969 get_aspect_ratio( context->streams[ index ], codec_context, NULL ) );
970 codec = NULL;
971 }
972
973 // Initialise the codec if necessary
974 if ( codec == NULL )
975 {
976 // Initialise multi-threading
977 int thread_count = mlt_properties_get_int( properties, "threads" );
978 if ( thread_count == 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
979 thread_count = atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
980 if ( thread_count > 1 )
981 {
982 avcodec_thread_init( codec_context, thread_count );
983 codec_context->thread_count = thread_count;
984 }
985
986 // Find the codec
987 codec = avcodec_find_decoder( codec_context->codec_id );
988
989 // If we don't have a codec and we can't initialise it, we can't do much more...
990 avformat_lock( );
991 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
992 {
993 // Now store the codec with its destructor
994 mlt_properties_set_data( properties, "video_codec", codec_context, 0, producer_codec_close, NULL );
995 }
996 else
997 {
998 // Remember that we can't use this later
999 mlt_properties_set_int( properties, "video_index", -1 );
1000 index = -1;
1001 }
1002 avformat_unlock( );
1003
1004 // Process properties as AVOptions
1005 apply_properties( codec_context, properties, AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1006 }
1007
1008 // No codec, no show...
1009 if ( codec && index > -1 )
1010 {
1011 double source_fps = 0;
1012 double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
1013 double aspect_ratio = ( force_aspect_ratio > 0.0 ) ?
1014 force_aspect_ratio : mlt_properties_get_double( properties, "aspect_ratio" );
1015
1016 // Determine the fps
1017 source_fps = ( double )codec_context->time_base.den / ( codec_context->time_base.num == 0 ? 1 : codec_context->time_base.num );
1018
1019 // We'll use fps if it's available
1020 if ( source_fps > 0 )
1021 mlt_properties_set_double( properties, "source_fps", source_fps );
1022 else
1023 mlt_properties_set_double( properties, "source_fps", mlt_producer_get_fps( this ) );
1024 mlt_properties_set_double( properties, "aspect_ratio", aspect_ratio );
1025
1026 // Set the width and height
1027 mlt_properties_set_int( frame_properties, "width", codec_context->width );
1028 mlt_properties_set_int( frame_properties, "height", codec_context->height );
1029 mlt_properties_set_int( frame_properties, "real_width", codec_context->width );
1030 mlt_properties_set_int( frame_properties, "real_height", codec_context->height );
1031 mlt_properties_set_double( frame_properties, "aspect_ratio", aspect_ratio );
1032
1033 mlt_frame_push_get_image( frame, producer_get_image );
1034 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
1035 }
1036 else
1037 {
1038 mlt_properties_set_int( frame_properties, "test_image", 1 );
1039 }
1040 }
1041 else
1042 {
1043 mlt_properties_set_int( frame_properties, "test_image", 1 );
1044 }
1045 }
1046
1047 /** Get the audio from a frame.
1048 */
1049
1050 static int producer_get_audio( mlt_frame frame, int16_t **buffer, mlt_audio_format *format, int *frequency, int *channels, int *samples )
1051 {
1052 // Get the properties from the frame
1053 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1054
1055 // Obtain the frame number of this frame
1056 mlt_position position = mlt_properties_get_position( frame_properties, "avformat_position" );
1057
1058 // Get the producer
1059 mlt_producer this = mlt_properties_get_data( frame_properties, "avformat_producer", NULL );
1060
1061 // Get the producer properties
1062 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
1063
1064 // Fetch the audio_context
1065 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
1066
1067 // Get the audio_index
1068 int index = mlt_properties_get_int( properties, "audio_index" );
1069
1070 // Get the seekable status
1071 int seekable = mlt_properties_get_int( properties, "seekable" );
1072
1073 // Obtain the expected frame numer
1074 mlt_position expected = mlt_properties_get_position( properties, "_audio_expected" );
1075
1076 // Obtain the resample context if it exists (not always needed)
1077 ReSampleContext *resample = mlt_properties_get_data( properties, "audio_resample", NULL );
1078
1079 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1080 // Get the format converter context if it exists
1081 AVAudioConvert *convert = mlt_properties_get_data( properties, "audio_convert", NULL );
1082 #endif
1083
1084 // Obtain the audio buffers
1085 int16_t *audio_buffer = mlt_properties_get_data( properties, "audio_buffer", NULL );
1086 int16_t *decode_buffer = mlt_properties_get_data( properties, "decode_buffer", NULL );
1087 int16_t *convert_buffer = mlt_properties_get_data( properties, "convert_buffer", NULL );
1088
1089 // Get amount of audio used
1090 int audio_used = mlt_properties_get_int( properties, "_audio_used" );
1091
1092 // Calculate the real time code
1093 double real_timecode = producer_time_of_frame( this, position );
1094
1095 // Get the audio stream
1096 AVStream *stream = context->streams[ index ];
1097
1098 // Get codec context
1099 AVCodecContext *codec_context = stream->codec;
1100
1101 // Packet
1102 AVPacket pkt;
1103
1104 // Number of frames to ignore (for ffwd)
1105 int ignore = 0;
1106
1107 // Flag for paused (silence)
1108 int paused = 0;
1109
1110 // Check for resample and create if necessary
1111 if ( resample == NULL && codec_context->channels <= 2 )
1112 {
1113 // Create the resampler
1114 resample = audio_resample_init( *channels, codec_context->channels, *frequency, codec_context->sample_rate );
1115
1116 // And store it on properties
1117 mlt_properties_set_data( properties, "audio_resample", resample, 0, ( mlt_destructor )audio_resample_close, NULL );
1118 }
1119 else if ( resample == NULL )
1120 {
1121 *channels = codec_context->channels;
1122 *frequency = codec_context->sample_rate;
1123 }
1124
1125 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1126 // Check for audio format converter and create if necessary
1127 // TODO: support higher resolutions than 16-bit.
1128 if ( convert == NULL && codec_context->sample_fmt != SAMPLE_FMT_S16 )
1129 {
1130 // Create single channel converter for interleaved with no mixing matrix
1131 convert = av_audio_convert_alloc( SAMPLE_FMT_S16, 1, codec_context->sample_fmt, 1, NULL, 0 );
1132 mlt_properties_set_data( properties, "audio_convert", convert, 0, ( mlt_destructor )av_audio_convert_free, NULL );
1133 }
1134 #endif
1135
1136 // Check for audio buffer and create if necessary
1137 if ( audio_buffer == NULL )
1138 {
1139 // Allocate the audio buffer
1140 audio_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1141
1142 // And store it on properties for reuse
1143 mlt_properties_set_data( properties, "audio_buffer", audio_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1144 }
1145
1146 // Check for decoder buffer and create if necessary
1147 if ( decode_buffer == NULL )
1148 {
1149 // Allocate the audio buffer
1150 decode_buffer = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1151
1152 // And store it on properties for reuse
1153 mlt_properties_set_data( properties, "decode_buffer", decode_buffer, 0, ( mlt_destructor )av_free, NULL );
1154 }
1155
1156 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1157 // Check for format converter buffer and create if necessary
1158 if ( resample && convert && convert_buffer == NULL )
1159 {
1160 // Allocate the audio buffer
1161 convert_buffer = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
1162
1163 // And store it on properties for reuse
1164 mlt_properties_set_data( properties, "convert_buffer", convert_buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1165 }
1166 #endif
1167
1168 // Seek if necessary
1169 if ( position != expected )
1170 {
1171 if ( position + 1 == expected )
1172 {
1173 // We're paused - silence required
1174 paused = 1;
1175 }
1176 else if ( !seekable && position > expected && ( position - expected ) < 250 )
1177 {
1178 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1179 ignore = position - expected;
1180 }
1181 else if ( position < expected || position - expected >= 12 )
1182 {
1183 // Set to the real timecode
1184 if ( av_seek_frame( context, -1, mlt_properties_get_double( properties, "_start_time" ) + real_timecode * 1000000.0, AVSEEK_FLAG_BACKWARD ) != 0 )
1185 paused = 1;
1186
1187 // Clear the usage in the audio buffer
1188 audio_used = 0;
1189 }
1190 }
1191
1192 // Get the audio if required
1193 if ( !paused )
1194 {
1195 int ret = 0;
1196 int got_audio = 0;
1197
1198 av_init_packet( &pkt );
1199
1200 while( ret >= 0 && !got_audio )
1201 {
1202 // Check if the buffer already contains the samples required
1203 if ( audio_used >= *samples && ignore == 0 )
1204 {
1205 got_audio = 1;
1206 break;
1207 }
1208
1209 // Read a packet
1210 ret = av_read_frame( context, &pkt );
1211
1212 int len = pkt.size;
1213 uint8_t *ptr = pkt.data;
1214
1215 // We only deal with audio from the selected audio_index
1216 while ( ptr != NULL && ret >= 0 && pkt.stream_index == index && len > 0 )
1217 {
1218 int data_size = sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE;
1219
1220 // Decode the audio
1221 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1222 ret = avcodec_decode_audio2( codec_context, decode_buffer, &data_size, ptr, len );
1223 #else
1224 ret = avcodec_decode_audio( codec_context, decode_buffer, &data_size, ptr, len );
1225 #endif
1226 if ( ret < 0 )
1227 {
1228 ret = 0;
1229 break;
1230 }
1231
1232 len -= ret;
1233 ptr += ret;
1234
1235 if ( data_size > 0 )
1236 {
1237 int src_stride[6]= { av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 };
1238 int dst_stride[6]= { av_get_bits_per_sample_format( SAMPLE_FMT_S16 ) / 8 };
1239
1240 if ( resample )
1241 {
1242 int16_t *source = decode_buffer;
1243 int16_t *dest = &audio_buffer[ audio_used * *channels ];
1244 int convert_samples = data_size / src_stride[0];
1245
1246 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1247 if ( convert )
1248 {
1249 const void *src_buf[6] = { decode_buffer };
1250 void *dst_buf[6] = { convert_buffer };
1251 av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, convert_samples );
1252 source = convert_buffer;
1253 }
1254 #endif
1255 audio_used += audio_resample( resample, dest, source, convert_samples / codec_context->channels );
1256 }
1257 else
1258 {
1259 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1260 if ( convert )
1261 {
1262 const void *src_buf[6] = { decode_buffer };
1263 void *dst_buf[6] = { &audio_buffer[ audio_used * *channels ] };
1264 av_audio_convert( convert, dst_buf, dst_stride, src_buf, src_stride, data_size / src_stride[0] );
1265 }
1266 else
1267 #endif
1268 {
1269 memcpy( &audio_buffer[ audio_used * *channels ], decode_buffer, data_size );
1270 }
1271 audio_used += data_size / *channels / src_stride[0];
1272 }
1273
1274 // Handle ignore
1275 while ( ignore && audio_used > *samples )
1276 {
1277 ignore --;
1278 audio_used -= *samples;
1279 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * sizeof( int16_t ) );
1280 }
1281 }
1282
1283 // If we're behind, ignore this packet
1284 if ( pkt.pts >= 0 )
1285 {
1286 float current_pts = av_q2d( stream->time_base ) * pkt.pts;
1287 if ( seekable && ( !ignore && current_pts <= ( real_timecode - 0.02 ) ) )
1288 ignore = 1;
1289 }
1290 }
1291
1292 // We're finished with this packet regardless
1293 av_free_packet( &pkt );
1294 }
1295
1296 *buffer = mlt_pool_alloc( *samples * *channels * sizeof( int16_t ) );
1297 mlt_properties_set_data( frame_properties, "audio", *buffer, 0, ( mlt_destructor )mlt_pool_release, NULL );
1298
1299 // Now handle the audio if we have enough
1300 if ( audio_used >= *samples )
1301 {
1302 memcpy( *buffer, audio_buffer, *samples * *channels * sizeof( int16_t ) );
1303 audio_used -= *samples;
1304 memmove( audio_buffer, &audio_buffer[ *samples * *channels ], audio_used * *channels * sizeof( int16_t ) );
1305 }
1306 else
1307 {
1308 memset( *buffer, 0, *samples * *channels * sizeof( int16_t ) );
1309 }
1310
1311 // Store the number of audio samples still available
1312 mlt_properties_set_int( properties, "_audio_used", audio_used );
1313 }
1314 else
1315 {
1316 // Get silence and don't touch the context
1317 mlt_frame_get_audio( frame, buffer, format, frequency, channels, samples );
1318 }
1319
1320 // Regardless of speed (other than paused), we expect to get the next frame
1321 if ( !paused )
1322 mlt_properties_set_position( properties, "_audio_expected", position + 1 );
1323
1324 return 0;
1325 }
1326
1327 /** Set up audio handling.
1328 */
1329
1330 static void producer_set_up_audio( mlt_producer this, mlt_frame frame )
1331 {
1332 // Get the properties
1333 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
1334
1335 // Fetch the audio_context
1336 AVFormatContext *context = mlt_properties_get_data( properties, "audio_context", NULL );
1337
1338 // Get the audio_index
1339 int index = mlt_properties_get_int( properties, "audio_index" );
1340
1341 // Reopen the file if necessary
1342 if ( !context && index > -1 )
1343 {
1344 mlt_events_block( properties, this );
1345 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
1346 mlt_properties_get( properties, "resource" ) );
1347 context = mlt_properties_get_data( properties, "audio_context", NULL );
1348 mlt_properties_set_data( properties, "dummy_context", NULL, 0, NULL, NULL );
1349 mlt_events_unblock( properties, this );
1350 }
1351
1352 // Exception handling for audio_index
1353 if ( context && index >= (int) context->nb_streams )
1354 {
1355 for ( index = context->nb_streams - 1; index >= 0 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO; --index );
1356 mlt_properties_set_int( properties, "audio_index", index );
1357 }
1358 if ( context && index > -1 && context->streams[ index ]->codec->codec_type != CODEC_TYPE_AUDIO )
1359 {
1360 index = -1;
1361 mlt_properties_set_int( properties, "audio_index", index );
1362 }
1363
1364 // Update the audio properties if the index changed
1365 if ( index > -1 && index != mlt_properties_get_int( properties, "_audio_index" ) )
1366 {
1367 mlt_properties_set_int( properties, "_audio_index", index );
1368 mlt_properties_set_data( properties, "audio_codec", NULL, 0, NULL, NULL );
1369 }
1370
1371 // Deal with audio context
1372 if ( context != NULL && index > -1 )
1373 {
1374 // Get the frame properties
1375 mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
1376
1377 // Get the audio stream
1378 AVStream *stream = context->streams[ index ];
1379
1380 // Get codec context
1381 AVCodecContext *codec_context = stream->codec;
1382
1383 // Get the codec
1384 AVCodec *codec = mlt_properties_get_data( properties, "audio_codec", NULL );
1385
1386 // Initialise the codec if necessary
1387 if ( codec == NULL )
1388 {
1389 // Find the codec
1390 codec = avcodec_find_decoder( codec_context->codec_id );
1391
1392 // If we don't have a codec and we can't initialise it, we can't do much more...
1393 avformat_lock( );
1394 if ( codec != NULL && avcodec_open( codec_context, codec ) >= 0 )
1395 {
1396 // Now store the codec with its destructor
1397 mlt_properties_set_data( properties, "audio_codec", codec_context, 0, producer_codec_close, NULL );
1398
1399 }
1400 else
1401 {
1402 // Remember that we can't use this later
1403 mlt_properties_set_int( properties, "audio_index", -1 );
1404 index = -1;
1405 }
1406 avformat_unlock( );
1407
1408 // Process properties as AVOptions
1409 apply_properties( codec_context, properties, AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM );
1410 }
1411
1412 // No codec, no show...
1413 if ( codec && index > -1 )
1414 {
1415 mlt_frame_push_audio( frame, producer_get_audio );
1416 mlt_properties_set_data( frame_properties, "avformat_producer", this, 0, NULL, NULL );
1417 mlt_properties_set_int( frame_properties, "frequency", codec_context->sample_rate );
1418 mlt_properties_set_int( frame_properties, "channels", codec_context->channels );
1419 }
1420 }
1421 }
1422
1423 /** Our get frame implementation.
1424 */
1425
1426 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
1427 {
1428 // Create an empty frame
1429 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
1430
1431 // Update timecode on the frame we're creating
1432 mlt_frame_set_position( *frame, mlt_producer_position( this ) );
1433
1434 // Set the position of this producer
1435 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame ), "avformat_position", mlt_producer_frame( this ) );
1436
1437 // Set up the video
1438 producer_set_up_video( this, *frame );
1439
1440 // Set up the audio
1441 producer_set_up_audio( this, *frame );
1442
1443 // Set the aspect_ratio
1444 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame ), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1445
1446 // Calculate the next timecode
1447 mlt_producer_prepare_next( this );
1448
1449 return 0;
1450 }