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>
25 #include <framework/mlt_profile.h>
27 // ffmpeg Header files
33 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
34 # include "audioconvert.h"
37 // System header files
43 void avformat_lock( );
44 void avformat_unlock( );
46 // Forward references.
47 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
);
48 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
);
50 /** Constructor for libavformat.
53 mlt_producer
producer_avformat_init( mlt_profile profile
, char *file
)
57 // Report information about available demuxers and codecs as YAML Tiny
58 if ( file
&& strstr( file
, "f-list" ) )
60 fprintf( stderr
, "---\nformats:\n" );
61 AVInputFormat
*format
= NULL
;
62 while ( ( format
= av_iformat_next( format
) ) )
63 fprintf( stderr
, " - %s\n", format
->name
);
64 fprintf( stderr
, "...\n" );
67 if ( file
&& strstr( file
, "acodec-list" ) )
69 fprintf( stderr
, "---\naudio_codecs:\n" );
70 AVCodec
*codec
= NULL
;
71 while ( ( codec
= av_codec_next( codec
) ) )
72 if ( codec
->decode
&& codec
->type
== CODEC_TYPE_AUDIO
)
73 fprintf( stderr
, " - %s\n", codec
->name
);
74 fprintf( stderr
, "...\n" );
77 if ( file
&& strstr( file
, "vcodec-list" ) )
79 fprintf( stderr
, "---\nvideo_codecs:\n" );
80 AVCodec
*codec
= NULL
;
81 while ( ( codec
= av_codec_next( codec
) ) )
82 if ( codec
->decode
&& codec
->type
== CODEC_TYPE_VIDEO
)
83 fprintf( stderr
, " - %s\n", codec
->name
);
84 fprintf( stderr
, "...\n" );
90 mlt_producer
this = NULL
;
92 // Check that we have a non-NULL argument
95 // Construct the producer
96 this = calloc( 1, sizeof( struct mlt_producer_s
) );
99 if ( mlt_producer_init( this, NULL
) == 0 )
101 // Get the properties
102 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
104 // Set the resource property (required for all producers)
105 mlt_properties_set( properties
, "resource", file
);
107 // Register our get_frame implementation
108 this->get_frame
= producer_get_frame
;
111 if ( producer_open( this, profile
, file
) != 0 )
114 mlt_producer_close( this );
119 // Close the file to release resources for large playlists - reopen later as needed
120 mlt_properties_set_data( properties
, "dummy_context", NULL
, 0, NULL
, NULL
);
121 mlt_properties_set_data( properties
, "audio_context", NULL
, 0, NULL
, NULL
);
122 mlt_properties_set_data( properties
, "video_context", NULL
, 0, NULL
, NULL
);
124 // Default the user-selectable indices from the auto-detected indices
125 mlt_properties_set_int( properties
, "audio_index", mlt_properties_get_int( properties
, "_audio_index" ) );
126 mlt_properties_set_int( properties
, "video_index", mlt_properties_get_int( properties
, "_video_index" ) );
134 /** Find the default streams.
137 static mlt_properties
find_default_streams( mlt_properties meta_media
, AVFormatContext
*context
, int *audio_index
, int *video_index
)
142 mlt_properties_set_int( meta_media
, "meta.media.nb_streams", context
->nb_streams
);
144 // Allow for multiple audio and video streams in the file and select first of each (if available)
145 for( i
= 0; i
< context
->nb_streams
; i
++ )
147 // Get the codec context
148 AVStream
*stream
= context
->streams
[ i
];
149 if ( ! stream
) continue;
150 AVCodecContext
*codec_context
= stream
->codec
;
151 if ( ! codec_context
) continue;
152 AVCodec
*codec
= avcodec_find_decoder( codec_context
->codec_id
);
153 if ( ! codec
) continue;
155 snprintf( key
, sizeof(key
), "meta.media.%d.stream.type", i
);
157 // Determine the type and obtain the first index of each type
158 switch( codec_context
->codec_type
)
160 case CODEC_TYPE_VIDEO
:
161 if ( *video_index
< 0 )
163 mlt_properties_set( meta_media
, key
, "video" );
164 snprintf( key
, sizeof(key
), "meta.media.%d.stream.frame_rate", i
);
165 mlt_properties_set_double( meta_media
, key
, av_q2d( context
->streams
[ i
]->r_frame_rate
) );
166 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
167 snprintf( key
, sizeof(key
), "meta.media.%d.stream.sample_aspect_ratio", i
);
168 mlt_properties_set_double( meta_media
, key
, av_q2d( context
->streams
[ i
]->sample_aspect_ratio
) );
170 snprintf( key
, sizeof(key
), "meta.media.%d.codec.pix_fmt", i
);
171 mlt_properties_set( meta_media
, key
, avcodec_get_pix_fmt_name( codec_context
->pix_fmt
) );
172 snprintf( key
, sizeof(key
), "meta.media.%d.codec.sample_aspect_ratio", i
);
173 mlt_properties_set_double( meta_media
, key
, av_q2d( codec_context
->sample_aspect_ratio
) );
175 case CODEC_TYPE_AUDIO
:
176 if ( *audio_index
< 0 )
178 mlt_properties_set( meta_media
, key
, "audio" );
179 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
180 snprintf( key
, sizeof(key
), "meta.media.%d.codec.sample_fmt", i
);
181 mlt_properties_set( meta_media
, key
, avcodec_get_sample_fmt_name( codec_context
->sample_fmt
) );
183 snprintf( key
, sizeof(key
), "meta.media.%d.codec.sample_rate", i
);
184 mlt_properties_set_int( meta_media
, key
, codec_context
->sample_rate
);
185 snprintf( key
, sizeof(key
), "meta.media.%d.codec.channels", i
);
186 mlt_properties_set_int( meta_media
, key
, codec_context
->channels
);
191 // snprintf( key, sizeof(key), "meta.media.%d.stream.time_base", i );
192 // mlt_properties_set_double( meta_media, key, av_q2d( context->streams[ i ]->time_base ) );
193 snprintf( key
, sizeof(key
), "meta.media.%d.codec.name", i
);
194 mlt_properties_set( meta_media
, key
, codec
->name
);
195 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(55<<8)+0))
196 snprintf( key
, sizeof(key
), "meta.media.%d.codec.long_name", i
);
197 mlt_properties_set( meta_media
, key
, codec
->long_name
);
199 snprintf( key
, sizeof(key
), "meta.media.%d.codec.bit_rate", i
);
200 mlt_properties_set_int( meta_media
, key
, codec_context
->bit_rate
);
201 // snprintf( key, sizeof(key), "meta.media.%d.codec.time_base", i );
202 // mlt_properties_set_double( meta_media, key, av_q2d( codec_context->time_base ) );
203 snprintf( key
, sizeof(key
), "meta.media.%d.codec.profile", i
);
204 mlt_properties_set_int( meta_media
, key
, codec_context
->profile
);
205 snprintf( key
, sizeof(key
), "meta.media.%d.codec.level", i
);
206 mlt_properties_set_int( meta_media
, key
, codec_context
->level
);
212 /** Producer file destructor.
215 static void producer_file_close( void *context
)
217 if ( context
!= NULL
)
219 // Lock the mutex now
223 av_close_input_file( context
);
225 // Unlock the mutex now
230 /** Producer file destructor.
233 static void producer_codec_close( void *codec
)
237 // Lock the mutex now
241 avcodec_close( codec
);
243 // Unlock the mutex now
248 static inline int dv_is_pal( AVPacket
*pkt
)
250 return pkt
->data
[3] & 0x80;
253 static int dv_is_wide( AVPacket
*pkt
)
255 int i
= 80 /* block size */ *3 /* VAUX starts at block 3 */ +3 /* skip block header */;
257 for ( ; i
< pkt
->size
; i
+= 5 /* packet size */ )
259 if ( pkt
->data
[ i
] == 0x61 )
261 uint8_t x
= pkt
->data
[ i
+ 2 ] & 0x7;
262 return ( x
== 2 ) || ( x
== 7 );
268 static double get_aspect_ratio( AVStream
*stream
, AVCodecContext
*codec_context
, AVPacket
*pkt
)
270 double aspect_ratio
= 1.0;
272 if ( codec_context
->codec_id
== CODEC_ID_DVVIDEO
)
276 if ( dv_is_pal( pkt
) )
278 aspect_ratio
= dv_is_wide( pkt
)
279 ?
64.0/45.0 // 16:9 PAL
280 : 16.0/15.0; // 4:3 PAL
284 aspect_ratio
= dv_is_wide( pkt
)
285 ?
32.0/27.0 // 16:9 NTSC
286 : 8.0/9.0; // 4:3 NTSC
292 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
293 stream
->sample_aspect_ratio
;
295 codec_context
->sample_aspect_ratio
;
297 // Override FFmpeg's notion of DV aspect ratios, which are
298 // based upon a width of 704. Since we do not have a normaliser
299 // that crops (nor is cropping 720 wide ITU-R 601 video always desirable)
300 // we just coerce the values to facilitate a passive behaviour through
301 // the rescale normaliser when using equivalent producers and consumers.
302 // = display_aspect / (width * height)
303 if ( ar
.num
== 10 && ar
.den
== 11 )
304 aspect_ratio
= 8.0/9.0; // 4:3 NTSC
305 else if ( ar
.num
== 59 && ar
.den
== 54 )
306 aspect_ratio
= 16.0/15.0; // 4:3 PAL
307 else if ( ar
.num
== 40 && ar
.den
== 33 )
308 aspect_ratio
= 32.0/27.0; // 16:9 NTSC
309 else if ( ar
.num
== 118 && ar
.den
== 81 )
310 aspect_ratio
= 64.0/45.0; // 16:9 PAL
315 AVRational codec_sar
= codec_context
->sample_aspect_ratio
;
316 AVRational stream_sar
=
317 #if LIBAVFORMAT_VERSION_INT >= ((52<<16)+(21<<8)+0)
318 stream
->sample_aspect_ratio
;
322 if ( codec_sar
.num
> 0 )
323 aspect_ratio
= av_q2d( codec_sar
);
324 else if ( stream_sar
.num
> 0 )
325 aspect_ratio
= av_q2d( stream_sar
);
333 static int producer_open( mlt_producer
this, mlt_profile profile
, char *file
)
335 // Return an error code (0 == no error)
338 // Context for avformat
339 AVFormatContext
*context
= NULL
;
341 // Get the properties
342 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
344 // We will treat everything with the producer fps
345 double fps
= mlt_profile_fps( profile
);
347 // Lock the mutex now
350 // If "MRL", then create AVInputFormat
351 AVInputFormat
*format
= NULL
;
352 AVFormatParameters
*params
= NULL
;
353 char *standard
= NULL
;
354 char *mrl
= strchr( file
, ':' );
356 // AV option (0 = both, 1 = video, 2 = audio)
359 // Setting lowest log level
360 av_log_set_level( -1 );
362 // Only if there is not a protocol specification that avformat can handle
363 if ( mrl
&& !url_exist( file
) )
365 // 'file' becomes format abbreviation
369 format
= av_find_input_format( file
);
371 // Eat the format designator
377 params
= calloc( sizeof( AVFormatParameters
), 1 );
379 // These are required by video4linux (defaults)
381 params
->height
= 480;
382 params
->time_base
= (AVRational
){1,25};
383 // params->device = file;
384 params
->channels
= 2;
385 params
->sample_rate
= 48000;
388 // XXX: this does not work anymore since avdevice
389 // TODO: make producer_avddevice?
391 mrl
= strchr( file
, '?' );
395 char *name
= strdup( ++mrl
);
396 char *value
= strchr( name
, ':' );
401 char *t
= strchr( value
, '&' );
404 if ( !strcmp( name
, "frame_rate" ) )
405 params
->time_base
.den
= atoi( value
);
406 else if ( !strcmp( name
, "frame_rate_base" ) )
407 params
->time_base
.num
= atoi( value
);
408 else if ( !strcmp( name
, "sample_rate" ) )
409 params
->sample_rate
= atoi( value
);
410 else if ( !strcmp( name
, "channels" ) )
411 params
->channels
= atoi( value
);
412 else if ( !strcmp( name
, "width" ) )
413 params
->width
= atoi( value
);
414 else if ( !strcmp( name
, "height" ) )
415 params
->height
= atoi( value
);
416 else if ( !strcmp( name
, "standard" ) )
418 standard
= strdup( value
);
419 params
->standard
= standard
;
421 else if ( !strcmp( name
, "av" ) )
425 mrl
= strchr( mrl
, '&' );
429 // Now attempt to open the file
430 error
= av_open_input_file( &context
, file
, format
, 0, params
) < 0;
432 // Cleanup AVFormatParameters
436 // If successful, then try to get additional info
439 // Get the stream info
440 error
= av_find_stream_info( context
) < 0;
442 // Continue if no error
445 // We will default to the first audio and video streams found
446 int audio_index
= -1;
447 int video_index
= -1;
450 // Now set properties where we can (use default unknowns if required)
451 if ( context
->duration
!= AV_NOPTS_VALUE
)
453 // This isn't going to be accurate for all formats
454 mlt_position frames
= ( mlt_position
)( ( ( double )context
->duration
/ ( double )AV_TIME_BASE
) * fps
+ 0.5 );
455 mlt_properties_set_position( properties
, "out", frames
- 1 );
456 mlt_properties_set_position( properties
, "length", frames
);
459 // Find default audio and video streams
460 find_default_streams( properties
, context
, &audio_index
, &video_index
);
462 if ( context
->start_time
!= AV_NOPTS_VALUE
)
463 mlt_properties_set_double( properties
, "_start_time", context
->start_time
);
465 // Check if we're seekable (something funny about mpeg here :-/)
466 if ( strcmp( file
, "pipe:" ) && strncmp( file
, "http://", 6 ) && strncmp( file
, "udp:", 4 ) && strncmp( file
, "tcp:", 4 ) && strncmp( file
, "rtsp:", 5 ) && strncmp( file
, "rtp:", 4 ) )
468 mlt_properties_set_int( properties
, "seekable", av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ), AVSEEK_FLAG_BACKWARD
) >= 0 );
469 mlt_properties_set_data( properties
, "dummy_context", context
, 0, producer_file_close
, NULL
);
470 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
471 av_find_stream_info( context
);
476 // Store selected audio and video indexes on properties
477 mlt_properties_set_int( properties
, "_audio_index", audio_index
);
478 mlt_properties_set_int( properties
, "_video_index", video_index
);
479 mlt_properties_set_int( properties
, "_last_position", -1 );
481 // Fetch the width, height and aspect ratio
482 if ( video_index
!= -1 )
484 AVCodecContext
*codec_context
= context
->streams
[ video_index
]->codec
;
485 mlt_properties_set_int( properties
, "width", codec_context
->width
);
486 mlt_properties_set_int( properties
, "height", codec_context
->height
);
488 if ( codec_context
->codec_id
== CODEC_ID_DVVIDEO
)
490 // Fetch the first frame of DV so we can read it directly
495 ret
= av_read_frame( context
, &pkt
);
496 if ( ret
>= 0 && pkt
.stream_index
== video_index
&& pkt
.size
> 0 )
498 mlt_properties_set_double( properties
, "aspect_ratio",
499 get_aspect_ratio( context
->streams
[ video_index
], codec_context
, &pkt
) );
506 mlt_properties_set_double( properties
, "aspect_ratio",
507 get_aspect_ratio( context
->streams
[ video_index
], codec_context
, NULL
) );
512 if (context
->title
!= NULL
)
513 mlt_properties_set(properties
, "meta.attr.title.markup", context
->title
);
514 if (context
->author
!= NULL
)
515 mlt_properties_set(properties
, "meta.attr.author.markup", context
->author
);
516 if (context
->copyright
!= NULL
)
517 mlt_properties_set(properties
, "meta.attr.copyright.markup", context
->copyright
);
518 if (context
->comment
!= NULL
)
519 mlt_properties_set(properties
, "meta.attr.comment.markup", context
->comment
);
520 if (context
->album
!= NULL
)
521 mlt_properties_set(properties
, "meta.attr.album.markup", context
->album
);
522 if (context
->year
!= 0)
523 mlt_properties_set_int(properties
, "meta.attr.year.markup", context
->year
);
524 if (context
->track
!= 0)
525 mlt_properties_set_int(properties
, "meta.attr.track.markup", context
->track
);
527 // We're going to cheat here - for a/v files, we will have two contexts (reasoning will be clear later)
528 if ( av
== 0 && audio_index
!= -1 && video_index
!= -1 )
530 // We'll use the open one as our video_context
531 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
533 // And open again for our audio context
534 av_open_input_file( &context
, file
, NULL
, 0, NULL
);
535 av_find_stream_info( context
);
538 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
540 else if ( av
!= 2 && video_index
!= -1 )
542 // We only have a video context
543 mlt_properties_set_data( properties
, "video_context", context
, 0, producer_file_close
, NULL
);
545 else if ( audio_index
!= -1 )
547 // We only have an audio context
548 mlt_properties_set_data( properties
, "audio_context", context
, 0, producer_file_close
, NULL
);
552 // Something has gone wrong
556 mlt_properties_set_int( properties
, "av_bypass", av_bypass
);
560 // Unlock the mutex now
566 /** Convert a frame position to a time code.
569 static double producer_time_of_frame( mlt_producer
this, mlt_position position
)
571 return ( double )position
/ mlt_producer_get_fps( this );
574 static inline void convert_image( AVFrame
*frame
, uint8_t *buffer
, int pix_fmt
, mlt_image_format format
, int width
, int height
)
577 if ( format
== mlt_image_yuv420p
)
579 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
580 width
, height
, PIX_FMT_YUV420P
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
582 output
.data
[0] = buffer
;
583 output
.data
[1] = buffer
+ width
* height
;
584 output
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
585 output
.linesize
[0] = width
;
586 output
.linesize
[1] = width
>> 1;
587 output
.linesize
[2] = width
>> 1;
588 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
589 output
.data
, output
.linesize
);
590 sws_freeContext( context
);
592 else if ( format
== mlt_image_rgb24
)
594 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
595 width
, height
, PIX_FMT_RGB24
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
597 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
598 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
599 output
.data
, output
.linesize
);
600 sws_freeContext( context
);
604 struct SwsContext
*context
= sws_getContext( width
, height
, pix_fmt
,
605 width
, height
, PIX_FMT_YUYV422
, SWS_FAST_BILINEAR
, NULL
, NULL
, NULL
);
607 avpicture_fill( &output
, buffer
, PIX_FMT_YUYV422
, width
, height
);
608 sws_scale( context
, frame
->data
, frame
->linesize
, 0, height
,
609 output
.data
, output
.linesize
);
610 sws_freeContext( context
);
613 if ( format
== mlt_image_yuv420p
)
616 pict
.data
[0] = buffer
;
617 pict
.data
[1] = buffer
+ width
* height
;
618 pict
.data
[2] = buffer
+ ( 3 * width
* height
) / 2;
619 pict
.linesize
[0] = width
;
620 pict
.linesize
[1] = width
>> 1;
621 pict
.linesize
[2] = width
>> 1;
622 img_convert( &pict
, PIX_FMT_YUV420P
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
624 else if ( format
== mlt_image_rgb24
)
627 avpicture_fill( &output
, buffer
, PIX_FMT_RGB24
, width
, height
);
628 img_convert( &output
, PIX_FMT_RGB24
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
633 avpicture_fill( &output
, buffer
, PIX_FMT_YUV422
, width
, height
);
634 img_convert( &output
, PIX_FMT_YUV422
, (AVPicture
*)frame
, pix_fmt
, width
, height
);
639 /** Allocate the image buffer and set it on the frame.
642 static int allocate_buffer( mlt_properties frame_properties
, AVCodecContext
*codec_context
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
)
646 if ( codec_context
->width
== 0 || codec_context
->height
== 0 )
649 *width
= codec_context
->width
;
650 *height
= codec_context
->height
;
651 mlt_properties_set_int( frame_properties
, "width", *width
);
652 mlt_properties_set_int( frame_properties
, "height", *height
);
656 case mlt_image_yuv420p
:
657 size
= *width
* 3 * ( *height
+ 1 ) / 2;
659 case mlt_image_rgb24
:
660 size
= *width
* ( *height
+ 1 ) * 3;
663 *format
= mlt_image_yuv422
;
664 size
= *width
* ( *height
+ 1 ) * 2;
668 // Construct the output image
669 *buffer
= mlt_pool_alloc( size
);
671 mlt_properties_set_data( frame_properties
, "image", *buffer
, size
, (mlt_destructor
)mlt_pool_release
, NULL
);
678 /** Get an image from a frame.
681 static int producer_get_image( mlt_frame frame
, uint8_t **buffer
, mlt_image_format
*format
, int *width
, int *height
, int writable
)
683 // Get the properties from the frame
684 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
686 // Obtain the frame number of this frame
687 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
690 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
692 // Get the producer properties
693 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
695 // Fetch the video_context
696 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
698 // Get the video_index
699 int index
= mlt_properties_get_int( properties
, "video_index" );
701 // Obtain the expected frame numer
702 mlt_position expected
= mlt_properties_get_position( properties
, "_video_expected" );
704 // Get the video stream
705 AVStream
*stream
= context
->streams
[ index
];
708 AVCodecContext
*codec_context
= stream
->codec
;
713 // Get the conversion frame
714 AVFrame
*av_frame
= mlt_properties_get_data( properties
, "av_frame", NULL
);
716 // Special case pause handling flag
719 // Special case ffwd handling
722 // We may want to use the source fps if available
723 double source_fps
= mlt_properties_get_double( properties
, "source_fps" );
724 double fps
= mlt_producer_get_fps( this );
726 // This is the physical frame position in the source
727 int req_position
= ( int )( position
/ fps
* source_fps
+ 0.5 );
729 // Get the seekable status
730 int seekable
= mlt_properties_get_int( properties
, "seekable" );
732 // Hopefully provide better support for streams...
733 int av_bypass
= mlt_properties_get_int( properties
, "av_bypass" );
735 // Determines if we have to decode all frames in a sequence
738 // Temporary hack to improve intra frame only
739 must_decode
= strcmp( codec_context
->codec
->name
, "mjpeg" ) &&
740 strcmp( codec_context
->codec
->name
, "rawvideo" ) &&
741 strcmp( codec_context
->codec
->name
, "dvvideo" );
744 if ( position
!= expected
)
746 if ( av_frame
!= NULL
&& position
+ 1 == expected
)
748 // We're paused - use last image
751 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
753 // Fast forward - seeking is inefficient for small distances - just ignore following frames
754 ignore
= ( int )( ( position
- expected
) / fps
* source_fps
);
756 else if ( seekable
&& ( position
< expected
|| position
- expected
>= 12 ) )
758 // Calculate the timestamp for the requested frame
759 int64_t timestamp
= ( int64_t )( ( double )req_position
/ source_fps
* AV_TIME_BASE
+ 0.5 );
760 if ( ( uint64_t )context
->start_time
!= AV_NOPTS_VALUE
)
761 timestamp
+= context
->start_time
;
763 timestamp
-= AV_TIME_BASE
;
767 // Set to the timestamp
768 av_seek_frame( context
, -1, timestamp
, AVSEEK_FLAG_BACKWARD
);
770 // Remove the cached info relating to the previous position
771 mlt_properties_set_int( properties
, "_current_position", -1 );
772 mlt_properties_set_int( properties
, "_last_position", -1 );
773 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
778 // Duplicate the last image if necessary (see comment on rawvideo below)
779 int current_position
= mlt_properties_get_int( properties
, "_current_position" );
780 int got_picture
= mlt_properties_get_int( properties
, "_got_picture" );
781 if ( av_frame
!= NULL
&& got_picture
&& ( paused
|| current_position
>= req_position
) && av_bypass
== 0 )
784 if ( allocate_buffer( frame_properties
, codec_context
, buffer
, format
, width
, height
) )
785 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
787 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
792 int int_position
= 0;
795 av_init_packet( &pkt
);
797 // Construct an AVFrame for YUV422 conversion
798 if ( av_frame
== NULL
)
799 av_frame
= avcodec_alloc_frame( );
801 while( ret
>= 0 && !got_picture
)
804 ret
= av_read_frame( context
, &pkt
);
806 // We only deal with video from the selected video_index
807 if ( ret
>= 0 && pkt
.stream_index
== index
&& pkt
.size
> 0 )
809 // Determine time code of the packet
810 int_position
= ( int )( av_q2d( stream
->time_base
) * pkt
.dts
* source_fps
+ 0.5 );
811 if ( context
->start_time
!= AV_NOPTS_VALUE
)
812 int_position
-= ( int )( context
->start_time
* source_fps
/ AV_TIME_BASE
+ 0.5 );
813 int last_position
= mlt_properties_get_int( properties
, "_last_position" );
814 if ( int_position
== last_position
)
815 int_position
= last_position
+ 1;
816 mlt_properties_set_int( properties
, "_last_position", int_position
);
819 if ( must_decode
|| int_position
>= req_position
)
820 ret
= avcodec_decode_video( codec_context
, av_frame
, &got_picture
, pkt
.data
, pkt
.size
);
825 if ( int_position
< req_position
)
830 else if ( int_position
>= req_position
)
834 else if ( ignore
-- )
839 av_free_packet( &pkt
);
843 av_free_packet( &pkt
);
846 // Now handle the picture if we have one
849 if ( allocate_buffer( frame_properties
, codec_context
, buffer
, format
, width
, height
) )
851 convert_image( av_frame
, *buffer
, codec_context
->pix_fmt
, *format
, *width
, *height
);
852 mlt_properties_set_int( frame_properties
, "progressive", !av_frame
->interlaced_frame
);
853 mlt_properties_set_int( properties
, "top_field_first", av_frame
->top_field_first
);
854 mlt_properties_set_int( properties
, "_current_position", int_position
);
855 mlt_properties_set_int( properties
, "_got_picture", 1 );
856 mlt_properties_set_data( properties
, "av_frame", av_frame
, 0, av_free
, NULL
);
865 mlt_frame_get_image( frame
, buffer
, format
, width
, height
, writable
);
868 // Very untidy - for rawvideo, the packet contains the frame, hence the free packet
869 // above will break the pause behaviour - so we wipe the frame now
870 if ( !strcmp( codec_context
->codec
->name
, "rawvideo" ) )
871 mlt_properties_set_data( properties
, "av_frame", NULL
, 0, NULL
, NULL
);
873 // Set the field order property for this frame
874 mlt_properties_set_int( frame_properties
, "top_field_first", mlt_properties_get_int( properties
, "top_field_first" ) );
876 // Regardless of speed, we expect to get the next frame (cos we ain't too bright)
877 mlt_properties_set_position( properties
, "_video_expected", position
+ 1 );
882 /** Process properties as AVOptions and apply to AV context obj
885 static void apply_properties( void *obj
, mlt_properties properties
, int flags
)
888 int count
= mlt_properties_count( properties
);
889 for ( i
= 0; i
< count
; i
++ )
891 const char *opt_name
= mlt_properties_get_name( properties
, i
);
892 const AVOption
*opt
= av_find_opt( obj
, opt_name
, NULL
, flags
, flags
);
894 #if LIBAVCODEC_VERSION_INT >= ((52<<16)+(7<<8)+0)
895 av_set_string3( obj
, opt_name
, mlt_properties_get( properties
, opt_name
), 0, NULL
);
896 #elif LIBAVCODEC_VERSION_INT >= ((51<<16)+(59<<8)+0)
897 av_set_string2( obj
, opt_name
, mlt_properties_get( properties
, opt_name
), 0 );
899 av_set_string( obj
, opt_name
, mlt_properties_get( properties
, opt_name
) );
904 /** Set up video handling.
907 static void producer_set_up_video( mlt_producer
this, mlt_frame frame
)
909 // Get the properties
910 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
912 // Fetch the video_context
913 AVFormatContext
*context
= mlt_properties_get_data( properties
, "video_context", NULL
);
915 // Get the video_index
916 int index
= mlt_properties_get_int( properties
, "video_index" );
918 // Reopen the file if necessary
919 if ( !context
&& index
> -1 )
921 mlt_events_block( properties
, this );
922 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
923 mlt_properties_get( properties
, "resource" ) );
924 context
= mlt_properties_get_data( properties
, "video_context", NULL
);
925 mlt_properties_set_data( properties
, "dummy_context", NULL
, 0, NULL
, NULL
);
926 mlt_events_unblock( properties
, this );
928 // Process properties as AVOptions
929 apply_properties( context
, properties
, AV_OPT_FLAG_DECODING_PARAM
);
932 // Exception handling for video_index
933 if ( context
&& index
>= (int) context
->nb_streams
)
935 // Get the last video stream
936 for ( index
= context
->nb_streams
- 1; index
>= 0 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_VIDEO
; --index
);
937 mlt_properties_set_int( properties
, "video_index", index
);
939 if ( context
&& index
> -1 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_VIDEO
)
941 // Invalidate the video stream
943 mlt_properties_set_int( properties
, "video_index", index
);
946 // Get the frame properties
947 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
949 if ( context
&& index
> -1 )
951 // Get the video stream
952 AVStream
*stream
= context
->streams
[ index
];
955 AVCodecContext
*codec_context
= stream
->codec
;
958 AVCodec
*codec
= mlt_properties_get_data( properties
, "video_codec", NULL
);
960 // Update the video properties if the index changed
961 if ( index
!= mlt_properties_get_int( properties
, "_video_index" ) )
963 // Reset the video properties if the index changed
964 mlt_properties_set_int( properties
, "_video_index", index
);
965 mlt_properties_set_data( properties
, "video_codec", NULL
, 0, NULL
, NULL
);
966 mlt_properties_set_int( properties
, "width", codec_context
->width
);
967 mlt_properties_set_int( properties
, "height", codec_context
->height
);
968 // TODO: get the first usable AVPacket and reset the stream position
969 mlt_properties_set_double( properties
, "aspect_ratio",
970 get_aspect_ratio( context
->streams
[ index
], codec_context
, NULL
) );
974 // Initialise the codec if necessary
977 // Initialise multi-threading
978 int thread_count
= mlt_properties_get_int( properties
, "threads" );
979 if ( thread_count
== 0 && getenv( "MLT_AVFORMAT_THREADS" ) )
980 thread_count
= atoi( getenv( "MLT_AVFORMAT_THREADS" ) );
981 if ( thread_count
> 1 )
983 avcodec_thread_init( codec_context
, thread_count
);
984 codec_context
->thread_count
= thread_count
;
988 codec
= avcodec_find_decoder( codec_context
->codec_id
);
990 // If we don't have a codec and we can't initialise it, we can't do much more...
992 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
994 // Now store the codec with its destructor
995 mlt_properties_set_data( properties
, "video_codec", codec_context
, 0, producer_codec_close
, NULL
);
999 // Remember that we can't use this later
1000 mlt_properties_set_int( properties
, "video_index", -1 );
1005 // Process properties as AVOptions
1006 apply_properties( codec_context
, properties
, AV_OPT_FLAG_VIDEO_PARAM
| AV_OPT_FLAG_DECODING_PARAM
);
1009 // No codec, no show...
1010 if ( codec
&& index
> -1 )
1012 double source_fps
= 0;
1013 double force_aspect_ratio
= mlt_properties_get_double( properties
, "force_aspect_ratio" );
1014 double aspect_ratio
= ( force_aspect_ratio
> 0.0 ) ?
1015 force_aspect_ratio
: mlt_properties_get_double( properties
, "aspect_ratio" );
1017 // Determine the fps
1018 source_fps
= ( double )codec_context
->time_base
.den
/ ( codec_context
->time_base
.num
== 0 ?
1 : codec_context
->time_base
.num
);
1020 // We'll use fps if it's available
1021 if ( source_fps
> 0 )
1022 mlt_properties_set_double( properties
, "source_fps", source_fps
);
1024 mlt_properties_set_double( properties
, "source_fps", mlt_producer_get_fps( this ) );
1025 mlt_properties_set_double( properties
, "aspect_ratio", aspect_ratio
);
1027 // Set the width and height
1028 mlt_properties_set_int( frame_properties
, "width", codec_context
->width
);
1029 mlt_properties_set_int( frame_properties
, "height", codec_context
->height
);
1030 mlt_properties_set_int( frame_properties
, "real_width", codec_context
->width
);
1031 mlt_properties_set_int( frame_properties
, "real_height", codec_context
->height
);
1032 mlt_properties_set_double( frame_properties
, "aspect_ratio", aspect_ratio
);
1034 mlt_frame_push_get_image( frame
, producer_get_image
);
1035 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
1039 mlt_properties_set_int( frame_properties
, "test_image", 1 );
1044 mlt_properties_set_int( frame_properties
, "test_image", 1 );
1048 /** Get the audio from a frame.
1051 static int producer_get_audio( mlt_frame frame
, int16_t **buffer
, mlt_audio_format
*format
, int *frequency
, int *channels
, int *samples
)
1053 // Get the properties from the frame
1054 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
1056 // Obtain the frame number of this frame
1057 mlt_position position
= mlt_properties_get_position( frame_properties
, "avformat_position" );
1060 mlt_producer
this = mlt_properties_get_data( frame_properties
, "avformat_producer", NULL
);
1062 // Get the producer properties
1063 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
1065 // Fetch the audio_context
1066 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1068 // Get the audio_index
1069 int index
= mlt_properties_get_int( properties
, "audio_index" );
1071 // Get the seekable status
1072 int seekable
= mlt_properties_get_int( properties
, "seekable" );
1074 // Obtain the expected frame numer
1075 mlt_position expected
= mlt_properties_get_position( properties
, "_audio_expected" );
1077 // Obtain the resample context if it exists (not always needed)
1078 ReSampleContext
*resample
= mlt_properties_get_data( properties
, "audio_resample", NULL
);
1080 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1081 // Get the format converter context if it exists
1082 AVAudioConvert
*convert
= mlt_properties_get_data( properties
, "audio_convert", NULL
);
1085 // Obtain the audio buffers
1086 int16_t *audio_buffer
= mlt_properties_get_data( properties
, "audio_buffer", NULL
);
1087 int16_t *decode_buffer
= mlt_properties_get_data( properties
, "decode_buffer", NULL
);
1088 int16_t *convert_buffer
= mlt_properties_get_data( properties
, "convert_buffer", NULL
);
1090 // Get amount of audio used
1091 int audio_used
= mlt_properties_get_int( properties
, "_audio_used" );
1093 // Calculate the real time code
1094 double real_timecode
= producer_time_of_frame( this, position
);
1096 // Get the audio stream
1097 AVStream
*stream
= context
->streams
[ index
];
1099 // Get codec context
1100 AVCodecContext
*codec_context
= stream
->codec
;
1105 // Number of frames to ignore (for ffwd)
1108 // Flag for paused (silence)
1111 // Check for resample and create if necessary
1112 if ( resample
== NULL
&& codec_context
->channels
<= 2 )
1114 // Create the resampler
1115 resample
= audio_resample_init( *channels
, codec_context
->channels
, *frequency
, codec_context
->sample_rate
);
1117 // And store it on properties
1118 mlt_properties_set_data( properties
, "audio_resample", resample
, 0, ( mlt_destructor
)audio_resample_close
, NULL
);
1120 else if ( resample
== NULL
)
1122 *channels
= codec_context
->channels
;
1123 *frequency
= codec_context
->sample_rate
;
1126 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1127 // Check for audio format converter and create if necessary
1128 // TODO: support higher resolutions than 16-bit.
1129 if ( convert
== NULL
&& codec_context
->sample_fmt
!= SAMPLE_FMT_S16
)
1131 // Create single channel converter for interleaved with no mixing matrix
1132 convert
= av_audio_convert_alloc( SAMPLE_FMT_S16
, 1, codec_context
->sample_fmt
, 1, NULL
, 0 );
1133 mlt_properties_set_data( properties
, "audio_convert", convert
, 0, ( mlt_destructor
)av_audio_convert_free
, NULL
);
1137 // Check for audio buffer and create if necessary
1138 if ( audio_buffer
== NULL
)
1140 // Allocate the audio buffer
1141 audio_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1143 // And store it on properties for reuse
1144 mlt_properties_set_data( properties
, "audio_buffer", audio_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
1147 // Check for decoder buffer and create if necessary
1148 if ( decode_buffer
== NULL
)
1150 // Allocate the audio buffer
1151 decode_buffer
= av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1153 // And store it on properties for reuse
1154 mlt_properties_set_data( properties
, "decode_buffer", decode_buffer
, 0, ( mlt_destructor
)av_free
, NULL
);
1157 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1158 // Check for format converter buffer and create if necessary
1159 if ( resample
&& convert
&& convert_buffer
== NULL
)
1161 // Allocate the audio buffer
1162 convert_buffer
= mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE
* sizeof( int16_t ) );
1164 // And store it on properties for reuse
1165 mlt_properties_set_data( properties
, "convert_buffer", convert_buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
1169 // Seek if necessary
1170 if ( position
!= expected
)
1172 if ( position
+ 1 == expected
)
1174 // We're paused - silence required
1177 else if ( !seekable
&& position
> expected
&& ( position
- expected
) < 250 )
1179 // Fast forward - seeking is inefficient for small distances - just ignore following frames
1180 ignore
= position
- expected
;
1182 else if ( position
< expected
|| position
- expected
>= 12 )
1184 // Set to the real timecode
1185 if ( av_seek_frame( context
, -1, mlt_properties_get_double( properties
, "_start_time" ) + real_timecode
* 1000000.0, AVSEEK_FLAG_BACKWARD
) != 0 )
1188 // Clear the usage in the audio buffer
1193 // Get the audio if required
1199 av_init_packet( &pkt
);
1201 while( ret
>= 0 && !got_audio
)
1203 // Check if the buffer already contains the samples required
1204 if ( audio_used
>= *samples
&& ignore
== 0 )
1211 ret
= av_read_frame( context
, &pkt
);
1214 uint8_t *ptr
= pkt
.data
;
1216 // We only deal with audio from the selected audio_index
1217 while ( ptr
!= NULL
&& ret
>= 0 && pkt
.stream_index
== index
&& len
> 0 )
1219 int data_size
= sizeof( int16_t ) * AVCODEC_MAX_AUDIO_FRAME_SIZE
;
1222 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(29<<8)+0))
1223 ret
= avcodec_decode_audio2( codec_context
, decode_buffer
, &data_size
, ptr
, len
);
1225 ret
= avcodec_decode_audio( codec_context
, decode_buffer
, &data_size
, ptr
, len
);
1236 if ( data_size
> 0 )
1238 int src_stride
[6]= { av_get_bits_per_sample_format( codec_context
->sample_fmt
) / 8 };
1239 int dst_stride
[6]= { av_get_bits_per_sample_format( SAMPLE_FMT_S16
) / 8 };
1243 int16_t *source
= decode_buffer
;
1244 int16_t *dest
= &audio_buffer
[ audio_used
* *channels
];
1245 int convert_samples
= data_size
/ src_stride
[0];
1247 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1250 const void *src_buf
[6] = { decode_buffer
};
1251 void *dst_buf
[6] = { convert_buffer
};
1252 av_audio_convert( convert
, dst_buf
, dst_stride
, src_buf
, src_stride
, convert_samples
);
1253 source
= convert_buffer
;
1256 audio_used
+= audio_resample( resample
, dest
, source
, convert_samples
/ codec_context
->channels
);
1260 #if (LIBAVCODEC_VERSION_INT >= ((51<<16)+(71<<8)+0))
1263 const void *src_buf
[6] = { decode_buffer
};
1264 void *dst_buf
[6] = { &audio_buffer
[ audio_used
* *channels
] };
1265 av_audio_convert( convert
, dst_buf
, dst_stride
, src_buf
, src_stride
, data_size
/ src_stride
[0] );
1270 memcpy( &audio_buffer
[ audio_used
* *channels
], decode_buffer
, data_size
);
1272 audio_used
+= data_size
/ *channels
/ src_stride
[0];
1276 while ( ignore
&& audio_used
> *samples
)
1279 audio_used
-= *samples
;
1280 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* sizeof( int16_t ) );
1284 // If we're behind, ignore this packet
1287 float current_pts
= av_q2d( stream
->time_base
) * pkt
.pts
;
1288 if ( seekable
&& ( !ignore
&& current_pts
<= ( real_timecode
- 0.02 ) ) )
1293 // We're finished with this packet regardless
1294 av_free_packet( &pkt
);
1297 *buffer
= mlt_pool_alloc( *samples
* *channels
* sizeof( int16_t ) );
1298 mlt_properties_set_data( frame_properties
, "audio", *buffer
, 0, ( mlt_destructor
)mlt_pool_release
, NULL
);
1300 // Now handle the audio if we have enough
1301 if ( audio_used
>= *samples
)
1303 memcpy( *buffer
, audio_buffer
, *samples
* *channels
* sizeof( int16_t ) );
1304 audio_used
-= *samples
;
1305 memmove( audio_buffer
, &audio_buffer
[ *samples
* *channels
], audio_used
* *channels
* sizeof( int16_t ) );
1309 memset( *buffer
, 0, *samples
* *channels
* sizeof( int16_t ) );
1312 // Store the number of audio samples still available
1313 mlt_properties_set_int( properties
, "_audio_used", audio_used
);
1317 // Get silence and don't touch the context
1318 mlt_frame_get_audio( frame
, buffer
, format
, frequency
, channels
, samples
);
1321 // Regardless of speed (other than paused), we expect to get the next frame
1323 mlt_properties_set_position( properties
, "_audio_expected", position
+ 1 );
1328 /** Set up audio handling.
1331 static void producer_set_up_audio( mlt_producer
this, mlt_frame frame
)
1333 // Get the properties
1334 mlt_properties properties
= MLT_PRODUCER_PROPERTIES( this );
1336 // Fetch the audio_context
1337 AVFormatContext
*context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1339 // Get the audio_index
1340 int index
= mlt_properties_get_int( properties
, "audio_index" );
1342 // Reopen the file if necessary
1343 if ( !context
&& index
> -1 )
1345 mlt_events_block( properties
, this );
1346 producer_open( this, mlt_service_profile( MLT_PRODUCER_SERVICE(this) ),
1347 mlt_properties_get( properties
, "resource" ) );
1348 context
= mlt_properties_get_data( properties
, "audio_context", NULL
);
1349 mlt_properties_set_data( properties
, "dummy_context", NULL
, 0, NULL
, NULL
);
1350 mlt_events_unblock( properties
, this );
1353 // Exception handling for audio_index
1354 if ( context
&& index
>= (int) context
->nb_streams
)
1356 for ( index
= context
->nb_streams
- 1; index
>= 0 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_AUDIO
; --index
);
1357 mlt_properties_set_int( properties
, "audio_index", index
);
1359 if ( context
&& index
> -1 && context
->streams
[ index
]->codec
->codec_type
!= CODEC_TYPE_AUDIO
)
1362 mlt_properties_set_int( properties
, "audio_index", index
);
1365 // Update the audio properties if the index changed
1366 if ( index
> -1 && index
!= mlt_properties_get_int( properties
, "_audio_index" ) )
1368 mlt_properties_set_int( properties
, "_audio_index", index
);
1369 mlt_properties_set_data( properties
, "audio_codec", NULL
, 0, NULL
, NULL
);
1372 // Deal with audio context
1373 if ( context
!= NULL
&& index
> -1 )
1375 // Get the frame properties
1376 mlt_properties frame_properties
= MLT_FRAME_PROPERTIES( frame
);
1378 // Get the audio stream
1379 AVStream
*stream
= context
->streams
[ index
];
1381 // Get codec context
1382 AVCodecContext
*codec_context
= stream
->codec
;
1385 AVCodec
*codec
= mlt_properties_get_data( properties
, "audio_codec", NULL
);
1387 // Initialise the codec if necessary
1388 if ( codec
== NULL
)
1391 codec
= avcodec_find_decoder( codec_context
->codec_id
);
1393 // If we don't have a codec and we can't initialise it, we can't do much more...
1395 if ( codec
!= NULL
&& avcodec_open( codec_context
, codec
) >= 0 )
1397 // Now store the codec with its destructor
1398 mlt_properties_set_data( properties
, "audio_codec", codec_context
, 0, producer_codec_close
, NULL
);
1403 // Remember that we can't use this later
1404 mlt_properties_set_int( properties
, "audio_index", -1 );
1409 // Process properties as AVOptions
1410 apply_properties( codec_context
, properties
, AV_OPT_FLAG_AUDIO_PARAM
| AV_OPT_FLAG_DECODING_PARAM
);
1413 // No codec, no show...
1414 if ( codec
&& index
> -1 )
1416 mlt_frame_push_audio( frame
, producer_get_audio
);
1417 mlt_properties_set_data( frame_properties
, "avformat_producer", this, 0, NULL
, NULL
);
1418 mlt_properties_set_int( frame_properties
, "frequency", codec_context
->sample_rate
);
1419 mlt_properties_set_int( frame_properties
, "channels", codec_context
->channels
);
1424 /** Our get frame implementation.
1427 static int producer_get_frame( mlt_producer
this, mlt_frame_ptr frame
, int index
)
1429 // Create an empty frame
1430 *frame
= mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
1432 // Update timecode on the frame we're creating
1433 mlt_frame_set_position( *frame
, mlt_producer_position( this ) );
1435 // Set the position of this producer
1436 mlt_properties_set_position( MLT_FRAME_PROPERTIES( *frame
), "avformat_position", mlt_producer_frame( this ) );
1439 producer_set_up_video( this, *frame
);
1442 producer_set_up_audio( this, *frame
);
1444 // Set the aspect_ratio
1445 mlt_properties_set_double( MLT_FRAME_PROPERTIES( *frame
), "aspect_ratio", mlt_properties_get_double( MLT_PRODUCER_PROPERTIES( this ), "aspect_ratio" ) );
1447 // Calculate the next timecode
1448 mlt_producer_prepare_next( this );