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
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.
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.
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
23 #include <framework/mlt_producer.h>
24 #include <framework/mlt_frame.h>
26 // ffmpeg Header files
32 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
33 # include "audioconvert.h"
36 // System header files
42 void avformat_lock( );
43 void avformat_unlock( );
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
);
49 /** Constructor for libavformat.
52 mlt_producer
producer_avformat_init( mlt_profile profile
, char *file
)
56 // Report information about available demuxers and codecs as YAML Tiny
57 if ( file
&& strstr( file
, "f-list" ) )
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" );
66 if ( file
&& strstr( file
, "acodec-list" ) )
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" );
76 if ( file
&& strstr( file
, "vcodec-list" ) )
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" );
89 mlt_producer
this = NULL
;
91 // Check that we have a non-NULL argument
94 // Construct the producer
95 this = calloc( 1, sizeof( struct mlt_producer_s
) );
98 if ( mlt_producer_init( this, NULL
) == 0 )
100 // Get the properties
101 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
103 // Set the resource property (required for all producers)
104 mlt_properties_set( properties
, "resource", file
);
106 // Register our get_frame implementation
107 this->get_frame
= producer_get_frame
;
110 if ( producer_open( this, profile
, file
) != 0 )
113 mlt_producer_close( this );
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
);
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" ) );
133 /** Find the default streams.
136 static mlt_properties
find_default_streams( mlt_properties meta_media
, AVFormatContext
*context
, int *audio_index
, int *video_index
)
141 mlt_properties_set_int( meta_media
, "meta.media.nb_streams", context
->nb_streams
);
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
++ )
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;
154 snprintf( key
, sizeof(key
), "meta.media.%d.stream.type", i
);
156 // Determine the type and obtain the first index of each type
157 switch( codec_context
->codec_type
)
159 case CODEC_TYPE_VIDEO
:
160 if ( *video_index
< 0 )
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
) );
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
) );
174 case CODEC_TYPE_AUDIO
:
175 if ( *audio_index
< 0 )
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
) );
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
);
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
);
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
);
211 /** Producer file destructor.
214 static void producer_file_close( void *context
)
216 if ( context
!= NULL
)
218 // Lock the mutex now
222 av_close_input_file( context
);
224 // Unlock the mutex now
229 /** Producer file destructor.
232 static void producer_codec_close( void *codec
)
236 // Lock the mutex now
240 avcodec_close( codec
);
242 // Unlock the mutex now
247 static inline int dv_is_pal( AVPacket
*pkt
)
249 return pkt
->data
[3] & 0x80;
252 static int dv_is_wide( AVPacket
*pkt
)
254 int i
= 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
256 for ( ; i
< pkt
->size
; i
+= 5 /* packet size */ )
258 if ( pkt
->data
[ i
] == 0x61 )
260 uint8_t x
= pkt
->data
[ i
+ 2 ] & 0x7;
261 return ( x
== 2 ) || ( x
== 7 );
267 static double get_aspect_ratio( AVStream
*stream
, AVCodecContext
*codec_context
, AVPacket
*pkt
)
269 double aspect_ratio
= 1.0;
271 if ( codec_context
->codec_id
== CODEC_ID_DVVIDEO
)
275 if ( dv_is_pal( pkt
) )
277 aspect_ratio
= dv_is_wide( pkt
)
278 ?
64.0/45.0 // 16:9 PAL
279 : 16.0/15.0; // 4:3 PAL
283 aspect_ratio
= dv_is_wide( pkt
)
284 ?
32.0/27.0 // 16:9 NTSC
285 : 8.0/9.0; // 4:3 NTSC
291 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
292 stream
->sample_aspect_ratio
;
294 codec_context
->sample_aspect_ratio
;
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
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
;
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
);
332 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
)
334 // Return an error code (0 == no error)
337 // Context for avformat
338 AVFormatContext
*context
= NULL
;
340 // Get the properties
341 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
343 // We will treat everything with the producer fps
344 double fps
= mlt_profile_fps( profile
);
346 // Lock the mutex now
349 // If "MRL", then create AVInputFormat
350 AVInputFormat
*format
= NULL
;
351 AVFormatParameters
*params
= NULL
;
352 char *standard
= NULL
;
353 char *mrl
= strchr( file
, ':' );
355 // AV option (0 = both, 1 = video, 2 = audio)
358 // Setting lowest log level
359 av_log_set_level( -1 );
361 // Only if there is not a protocol specification that avformat can handle
362 if ( mrl
&& !url_exist( file
) )
364 // 'file' becomes format abbreviation
368 format
= av_find_input_format( file
);
370 // Eat the format designator
376 params
= calloc( sizeof( AVFormatParameters
), 1 );
378 // These are required by video4linux (defaults)
380 params
->height
= 480;
381 params
->time_base
= (AVRational
){1,25};
382 // params->device = file;
383 params
->channels
= 2;
384 params
->sample_rate
= 48000;
387 // XXX: this does not work anymore since avdevice
388 // TODO: make producer_avddevice?
390 mrl
= strchr( file
, '?' );
394 char *name
= strdup( ++mrl
);
395 char *value
= strchr( name
, ':' );
400 char *t
= strchr( value
, '&' );
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" ) )
417 standard
= strdup( value
);
418 params
->standard
= standard
;
420 else if ( !strcmp( name
, "av" ) )
424 mrl
= strchr( mrl
, '&' );
428 // Now attempt to open the file
429 error
= av_open_input_file( &context
, file
, format
, 0, params
) < 0;
431 // Cleanup AVFormatParameters
435 // If successful, then try to get additional info
438 // Get the stream info
439 error
= av_find_stream_info( context
) < 0;
441 // Continue if no error
444 // We will default to the first audio and video streams found
445 int audio_index
= -1;
446 int video_index
= -1;
449 // Now set properties where we can (use default unknowns if required)
450 if ( context
->duration
!= AV_NOPTS_VALUE
)
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
);
458 // Find default audio and video streams
459 find_default_streams( properties
, context
, &audio_index
, &video_index
);
461 if ( context
->start_time
!= AV_NOPTS_VALUE
)
462 mlt_properties_set_double( properties
, "_start_time", context
->start_time
);
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 ) )
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
);
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 );
480 // Fetch the width, height and aspect ratio
481 if ( video_index
!= -1 )
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
);
487 if ( codec_context
->codec_id
== CODEC_ID_DVVIDEO
)
489 // Fetch the first frame of DV so we can read it directly
494 ret
= av_read_frame( context
, &pkt
);
495 if ( ret
>= 0 && pkt
.stream_index
== video_index
&& pkt
.size
> 0 )
497 mlt_properties_set_double( properties
, "aspect_ratio",
498 get_aspect_ratio( context
->streams
[ video_index
], codec_context
, &pkt
) );
505 mlt_properties_set_double( properties
, "aspect_ratio",
506 get_aspect_ratio( context
->streams
[ video_index
], codec_context
, NULL
) );
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
);
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 )
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
);
532 // And open again for our audio context
533 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
534 av_find_stream_info( context
);
537 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
539 else if ( av
!= 2 && video_index
!= -1 )
541 // We only have a video context
542 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
544 else if ( audio_index
!= -1 )
546 // We only have an audio context
547 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
551 // Something has gone wrong
555 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
559 // Unlock the mutex now
565 /** Convert a frame position to a time code.
568 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
570 return ( double )position
/ mlt_producer_get_fps( this );
573 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
576 if ( format
== mlt_image_yuv420p
)
578 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
579 width
, height
, PIX_FMT_YUV420P
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
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
);
591 else if ( format
== mlt_image_rgb24
)
593 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
594 width
, height
, PIX_FMT_RGB24
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
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
);
603 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
604 width
, height
, PIX_FMT_YUYV422
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
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
);
612 if ( format
== mlt_image_yuv420p
)
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
);
623 else if ( format
== mlt_image_rgb24
)
626 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
627 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
632 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
633 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
638 /** Allocate the image buffer and set it on the frame.
641 static int allocate_buffer( mlt_properties frame_properties
, AVCodecContext
*codec_context
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
)
645 if ( codec_context
->width
== 0 || codec_context
->height
== 0 )
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
);
655 case mlt_image_yuv420p
:
656 size
= *width
* 3 * ( *height
+ 1 ) / 2;
658 case mlt_image_rgb24
:
659 size
= *width
* ( *height
+ 1 ) * 3;
662 *format
= mlt_image_yuv422
;
663 size
= *width
* ( *height
+ 1 ) * 2;
667 // Construct the output image
668 *buffer
= mlt_pool_alloc( size
);
670 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
677 /** Get an image from a frame.
680 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
682 // Get the properties from the frame
683 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
685 // Obtain the frame number of this frame
686 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
689 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
691 // Get the producer properties
692 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
694 // Fetch the video_context
695 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
697 // Get the video_index
698 int index
= mlt_properties_get_int( properties
, "video_index" );
700 // Obtain the expected frame numer
701 mlt_position expected
= mlt_properties_get_position( properties
, "_video_expected" );
703 // Get the video stream
704 AVStream
*stream
= context
->streams
[ index
];
707 AVCodecContext
*codec_context
= stream
->codec
;
712 // Get the conversion frame
713 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
715 // Special case pause handling flag
718 // Special case ffwd handling
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 );
725 // This is the physical frame position in the source
726 int req_position
= ( int )( position
/ fps
* source_fps
+ 0.5 );
728 // Get the seekable status
729 int seekable
= mlt_properties_get_int( properties
, "seekable" );
731 // Hopefully provide better support for streams...
732 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
734 // Determines if we have to decode all frames in a sequence
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" );
743 if ( position
!= expected
)
745 if ( av_frame
!= NULL
&& position
+ 1 == expected
)
747 // We're paused - use last image
750 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
752 // Fast forward - seeking is inefficient for small distances - just ignore following frames
753 ignore
= ( int )( ( position
- expected
) / fps
* source_fps
);
755 else if ( seekable
&& ( position
< expected
|| position
- expected
>= 12 ) )
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
;
762 timestamp
-= AV_TIME_BASE
;
766 // Set to the timestamp
767 av_seek_frame( context
, -1, timestamp
, AVSEEK_FLAG_BACKWARD
);
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
);
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 )
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
);
786 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
791 int int_position
= 0;
794 av_init_packet( &pkt
);
796 // Construct an AVFrame for YUV422 conversion
797 if ( av_frame
== NULL
)
798 av_frame
= avcodec_alloc_frame( );
800 while( ret
>= 0 && !got_picture
)
803 ret
= av_read_frame( context
, &pkt
);
805 // We only deal with video from the selected video_index
806 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
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
);
818 if ( must_decode
|| int_position
>= req_position
)
819 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
824 if ( int_position
< req_position
)
829 else if ( int_position
>= req_position
)
833 else if ( ignore
-- )
838 av_free_packet( &pkt
);
842 av_free_packet( &pkt
);
845 // Now handle the picture if we have one
848 if ( allocate_buffer( frame_properties
, codec_context
, buffer
, format
, width
, height
) )
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
);
864 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
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
);
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" ) );
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 );
881 /** Process properties as AVOptions and apply to AV context obj
884 static void apply_properties( void *obj
, mlt_properties properties
, int flags
)
887 int count
= mlt_properties_count( properties
);
888 for ( i
= 0; i
< count
; i
++ )
890 const char *opt_name
= mlt_properties_get_name( properties
, i
);
891 const AVOption
*opt
= av_find_opt( obj
, opt_name
, NULL
, flags
, flags
);
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 );
898 av_set_string( obj
, opt_name
, mlt_properties_get( properties
, opt_name
) );
903 /** Set up video handling.
906 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
908 // Get the properties
909 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
911 // Fetch the video_context
912 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
914 // Get the video_index
915 int index
= mlt_properties_get_int( properties
, "video_index" );
917 // Reopen the file if necessary
918 if ( !context
&& index
> -1 )
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 );
927 // Process properties as AVOptions
928 apply_properties( context
, properties
, AV_OPT_FLAG_DECODING_PARAM
);
931 // Exception handling for video_index
932 if ( context
&& index
>= (int) context
->nb_streams
)
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
);
938 if ( context
&& index
> -1 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_VIDEO
)
940 // Invalidate the video stream
942 mlt_properties_set_int( properties
, "video_index", index
);
945 // Get the frame properties
946 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
948 if ( context
&& index
> -1 )
950 // Get the video stream
951 AVStream
*stream
= context
->streams
[ index
];
954 AVCodecContext
*codec_context
= stream
->codec
;
957 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
959 // Update the video properties if the index changed
960 if ( index
!= mlt_properties_get_int( properties
, "_video_index" ) )
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
) );
973 // Initialise the codec if necessary
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 )
982 avcodec_thread_init( codec_context
, thread_count
);
983 codec_context
->thread_count
= thread_count
;
987 codec
= avcodec_find_decoder( codec_context
->codec_id
);
989 // If we don't have a codec and we can't initialise it, we can't do much more...
991 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
993 // Now store the codec with its destructor
994 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
998 // Remember that we can't use this later
999 mlt_properties_set_int( properties
, "video_index", -1 );
1004 // Process properties as AVOptions
1005 apply_properties( codec_context
, properties
, AV_OPT_FLAG_VIDEO_PARAM
| AV_OPT_FLAG_DECODING_PARAM
);
1008 // No codec, no show...
1009 if ( codec
&& index
> -1 )
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" );
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
);
1019 // We'll use fps if it's available
1020 if ( source_fps
> 0 )
1021 mlt_properties_set_double( properties
, "source_fps", source_fps
);
1023 mlt_properties_set_double( properties
, "source_fps", mlt_producer_get_fps( this ) );
1024 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
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
);
1033 mlt_frame_push_get_image( frame
, producer_get_image
);
1034 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
1038 mlt_properties_set_int( frame_properties
, "test_image", 1 );
1043 mlt_properties_set_int( frame_properties
, "test_image", 1 );
1047 /** Get the audio from a frame.
1050 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
1052 // Get the properties from the frame
1053 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
1055 // Obtain the frame number of this frame
1056 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
1059 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
1061 // Get the producer properties
1062 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
1064 // Fetch the audio_context
1065 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1067 // Get the audio_index
1068 int index
= mlt_properties_get_int( properties
, "audio_index" );
1070 // Get the seekable status
1071 int seekable
= mlt_properties_get_int( properties
, "seekable" );
1073 // Obtain the expected frame numer
1074 mlt_position expected
= mlt_properties_get_position( properties
, "_audio_expected" );
1076 // Obtain the resample context if it exists (not always needed)
1077 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
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
);
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
);
1089 // Get amount of audio used
1090 int audio_used
= mlt_properties_get_int( properties
, "_audio_used" );
1092 // Calculate the real time code
1093 double real_timecode
= producer_time_of_frame( this, position
);
1095 // Get the audio stream
1096 AVStream
*stream
= context
->streams
[ index
];
1098 // Get codec context
1099 AVCodecContext
*codec_context
= stream
->codec
;
1104 // Number of frames to ignore (for ffwd)
1107 // Flag for paused (silence)
1110 // Check for resample and create if necessary
1111 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
1113 // Create the resampler
1114 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
1116 // And store it on properties
1117 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
1119 else if ( resample
== NULL
)
1121 *channels
= codec_context
->channels
;
1122 *frequency
= codec_context
->sample_rate
;
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
)
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
);
1136 // Check for audio buffer and create if necessary
1137 if ( audio_buffer
== NULL
)
1139 // Allocate the audio buffer
1140 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
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
);
1146 // Check for decoder buffer and create if necessary
1147 if ( decode_buffer
== NULL
)
1149 // Allocate the audio buffer
1150 decode_buffer
= av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1152 // And store it on properties for reuse
1153 mlt_properties_set_data( properties
, "decode_buffer", decode_buffer
, 0, ( mlt_destructor
)av_free
, NULL
);
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
)
1160 // Allocate the audio buffer
1161 convert_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
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
);
1168 // Seek if necessary
1169 if ( position
!= expected
)
1171 if ( position
+ 1 == expected
)
1173 // We're paused - silence required
1176 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
1178 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1179 ignore
= position
- expected
;
1181 else if ( position
< expected
|| position
- expected
>= 12 )
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 )
1187 // Clear the usage in the audio buffer
1192 // Get the audio if required
1198 av_init_packet( &pkt
);
1200 while( ret
>= 0 && !got_audio
)
1202 // Check if the buffer already contains the samples required
1203 if ( audio_used
>= *samples
&& ignore
== 0 )
1210 ret
= av_read_frame( context
, &pkt
);
1213 uint8_t *ptr
= pkt
.data
;
1215 // We only deal with audio from the selected audio_index
1216 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
1218 int data_size
= sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
;
1221 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1222 ret
= avcodec_decode_audio2( codec_context
, decode_buffer
, &data_size
, ptr
, len
);
1224 ret
= avcodec_decode_audio( codec_context
, decode_buffer
, &data_size
, ptr
, len
);
1235 if ( data_size
> 0 )
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 };
1242 int16_t *source
= decode_buffer
;
1243 int16_t *dest
= &audio_buffer
[ audio_used
* *channels
];
1244 int convert_samples
= data_size
/ src_stride
[0];
1246 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
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
;
1255 audio_used
+= audio_resample( resample
, dest
, source
, convert_samples
/ codec_context
->channels
);
1259 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
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] );
1269 memcpy( &audio_buffer
[ audio_used
* *channels
], decode_buffer
, data_size
);
1271 audio_used
+= data_size
/ *channels
/ src_stride
[0];
1275 while ( ignore
&& audio_used
> *samples
)
1278 audio_used
-= *samples
;
1279 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
1283 // If we're behind, ignore this packet
1286 float current_pts
= av_q2d( stream
->time_base
) * pkt
.pts
;
1287 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
1292 // We're finished with this packet regardless
1293 av_free_packet( &pkt
);
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
);
1299 // Now handle the audio if we have enough
1300 if ( audio_used
>= *samples
)
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 ) );
1308 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
1311 // Store the number of audio samples still available
1312 mlt_properties_set_int( properties
, "_audio_used", audio_used
);
1316 // Get silence and don't touch the context
1317 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
1320 // Regardless of speed (other than paused), we expect to get the next frame
1322 mlt_properties_set_position( properties
, "_audio_expected", position
+ 1 );
1327 /** Set up audio handling.
1330 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
1332 // Get the properties
1333 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
1335 // Fetch the audio_context
1336 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1338 // Get the audio_index
1339 int index
= mlt_properties_get_int( properties
, "audio_index" );
1341 // Reopen the file if necessary
1342 if ( !context
&& index
> -1 )
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 );
1352 // Exception handling for audio_index
1353 if ( context
&& index
>= (int) context
->nb_streams
)
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
);
1358 if ( context
&& index
> -1 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_AUDIO
)
1361 mlt_properties_set_int( properties
, "audio_index", index
);
1364 // Update the audio properties if the index changed
1365 if ( index
> -1 && index
!= mlt_properties_get_int( properties
, "_audio_index" ) )
1367 mlt_properties_set_int( properties
, "_audio_index", index
);
1368 mlt_properties_set_data( properties
, "audio_codec", NULL
, 0, NULL
, NULL
);
1371 // Deal with audio context
1372 if ( context
!= NULL
&& index
> -1 )
1374 // Get the frame properties
1375 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
1377 // Get the audio stream
1378 AVStream
*stream
= context
->streams
[ index
];
1380 // Get codec context
1381 AVCodecContext
*codec_context
= stream
->codec
;
1384 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
1386 // Initialise the codec if necessary
1387 if ( codec
== NULL
)
1390 codec
= avcodec_find_decoder( codec_context
->codec_id
);
1392 // If we don't have a codec and we can't initialise it, we can't do much more...
1394 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
1396 // Now store the codec with its destructor
1397 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
1402 // Remember that we can't use this later
1403 mlt_properties_set_int( properties
, "audio_index", -1 );
1408 // Process properties as AVOptions
1409 apply_properties( codec_context
, properties
, AV_OPT_FLAG_AUDIO_PARAM
| AV_OPT_FLAG_DECODING_PARAM
);
1412 // No codec, no show...
1413 if ( codec
&& index
> -1 )
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
);
1423 /** Our get frame implementation.
1426 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
1428 // Create an empty frame
1429 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
1431 // Update timecode on the frame we're creating
1432 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
1434 // Set the position of this producer
1435 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
1438 producer_set_up_video( this, *frame
);
1441 producer_set_up_audio( this, *frame
);
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" ) );
1446 // Calculate the next timecode
1447 mlt_producer_prepare_next( this );