int max_channel;
int max_frequency;
unsigned int invalid_pts_counter;
+ double resample_factor;
};
typedef struct producer_avformat_s *producer_avformat;
// Register our get_frame implementation
producer->get_frame = producer_get_frame;
+
+ this->resample_factor = 1.0;
// Open the file
if ( producer_open( this, profile, file ) != 0 )
AVPicture output;
output.data[0] = buffer;
output.data[1] = buffer + width * height;
- output.data[2] = buffer + ( 3 * width * height ) / 2;
+ output.data[2] = buffer + ( 5 * width * height ) / 4;
output.linesize[0] = width;
output.linesize[1] = width >> 1;
output.linesize[2] = width >> 1;
AVPicture pict;
pict.data[0] = buffer;
pict.data[1] = buffer + width * height;
- pict.data[2] = buffer + ( 3 * width * height ) / 2;
+ pict.data[2] = buffer + ( 5 * width * height ) / 4;
pict.linesize[0] = width;
pict.linesize[1] = width >> 1;
pict.linesize[2] = width >> 1;
// Determine the fps first from the codec
double source_fps = (double) this->video_codec->time_base.den /
( this->video_codec->time_base.num == 0 ? 1 : this->video_codec->time_base.num );
-
- // If the muxer reports a frame rate different than the codec
- double muxer_fps = av_q2d( stream->r_frame_rate );
- if ( source_fps != muxer_fps )
+
+ if ( mlt_properties_get( properties, "force_fps" ) )
+ {
+ source_fps = mlt_properties_get_double( properties, "force_fps" );
+ stream->time_base = av_d2q( source_fps, 255 );
+ }
+ else
+ {
+ // If the muxer reports a frame rate different than the codec
+ double muxer_fps = av_q2d( stream->r_frame_rate );
// Choose the lesser - the wrong tends to be off by some multiple of 10
- source_fps = muxer_fps < source_fps ? muxer_fps : source_fps;
+ source_fps = FFMIN( source_fps, muxer_fps );
+ }
// We'll use fps if it's available
if ( source_fps > 0 )
return paused;
}
-static int decode_audio( producer_avformat this, int *ignore, AVPacket *pkt, int samples, double timecode, double source_fps )
+static int decode_audio( producer_avformat this, int *ignore, AVPacket *pkt, int channels, int samples, double timecode, double source_fps )
{
// Fetch the audio_format
AVFormatContext *context = this->audio_format;
int16_t *decode_buffer = this->decode_buffer[ index ];
int audio_used = this->audio_used[ index ];
- int channels = codec_context->channels;
uint8_t *ptr = pkt->data;
int len = pkt->size;
int ret = 0;
// If decoded successfully
if ( data_size > 0 )
{
+ // Figure out how many samples will be needed after resampling
+ int convert_samples = data_size / codec_context->channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
+ int samples_needed = lrint( this->resample_factor * convert_samples );
+
// Resize audio buffer to prevent overflow
- if ( audio_used * channels + data_size > this->audio_buffer_size[ index ] )
+ if ( audio_used * channels + samples_needed > this->audio_buffer_size[ index ] )
{
- mlt_pool_release( this->audio_buffer[ index ] );
- this->audio_buffer_size[ index ] = audio_used * channels * sizeof(int16_t) + data_size * 2;
- audio_buffer = this->audio_buffer[ index ] = mlt_pool_alloc( this->audio_buffer_size[ index ] );
+ this->audio_buffer_size[ index ] *= 2;
+ audio_buffer = this->audio_buffer[ index ] = mlt_pool_realloc( audio_buffer, this->audio_buffer_size[ index ] * sizeof(int16_t) );
}
if ( resample )
{
// Copy to audio buffer while resampling
int16_t *source = decode_buffer;
int16_t *dest = &audio_buffer[ audio_used * channels ];
- int convert_samples = data_size / channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
audio_used += audio_resample( resample, dest, source, convert_samples );
}
else
{
// Straight copy to audio buffer
- memcpy( &audio_buffer[ audio_used * channels ], decode_buffer, data_size );
- audio_used += data_size / channels / ( av_get_bits_per_sample_format( codec_context->sample_fmt ) / 8 );
+ memcpy( &audio_buffer[ audio_used * codec_context->channels ], decode_buffer, data_size );
+ audio_used += convert_samples;
}
// Handle ignore
{
*ignore -= 1;
audio_used -= samples;
- memmove( audio_buffer, &audio_buffer[ samples * channels ], audio_used * sizeof( int16_t ) );
+ memmove( audio_buffer, &audio_buffer[ samples * (resample? channels : codec_context->channels) ],
+ audio_used * sizeof( int16_t ) );
}
}
}
// Fetch the audio_format
AVFormatContext *context = this->audio_format;
-
+
// Determine the tracks to use
int index = this->audio_index;
int index_max = this->audio_index + 1;
// Check for resample and create if necessary
if ( codec_context->channels <= 2 )
{
+ // Determine by how much resampling will increase number of samples
+ double resample_factor = this->audio_index == INT_MAX ? 1 : (double) *channels / codec_context->channels;
+ resample_factor *= (double) *frequency / codec_context->sample_rate;
+ if ( resample_factor > this->resample_factor )
+ this->resample_factor = resample_factor;
+
// Create the resampler
#if (LIBAVCODEC_VERSION_INT >= ((52<<16)+(15<<8)+0))
this->audio_resample[ index ] = av_audio_resample_init(
}
// Check for audio buffer and create if necessary
- this->audio_buffer[ index ] = mlt_pool_alloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
this->audio_buffer_size[ index ] = AVCODEC_MAX_AUDIO_FRAME_SIZE;
+ this->audio_buffer[ index ] = mlt_pool_alloc( this->audio_buffer_size[ index ] * sizeof( int16_t ) );
// Check for decoder buffer and create if necessary
this->decode_buffer[ index ] = av_malloc( AVCODEC_MAX_AUDIO_FRAME_SIZE * sizeof( int16_t ) );
// We only deal with audio from the selected audio index
if ( ret >= 0 && pkt.data && pkt.size > 0 && ( pkt.stream_index == this->audio_index ||
( this->audio_index == INT_MAX && context->streams[ pkt.stream_index ]->codec->codec_type == CODEC_TYPE_AUDIO ) ) )
- ret = decode_audio( this, &ignore, &pkt, *samples, real_timecode, source_fps );
+ ret = decode_audio( this, &ignore, &pkt, *channels, *samples, real_timecode, source_fps );
av_free_packet( &pkt );
if ( this->audio_index == INT_MAX && ret >= 0 )