Hi,
I've made a program using libav that reads any libav supported format as
input and output it as a mono/44100/pcm_16le buffer (or wav file).
Everything is running smoothly, apparently, but in fact, the call to
avresample_convert is not working.
If the input is in 44100Hz, there's no problem. However, if avresample
really need to resample (e.g., input is 48KHz), it doesn't do anything
and doesn't even fail or warns about a problem.
Here is a part of the code is use. Please tell me if you see something
weird.
//prepare the resampler/downmixer
AVAudioResampleContext *avr = avresample_alloc_context();
av_opt_set_int(avr, "in_channel_layout",
_iCodecCtx->channel_layout, 0);
av_opt_set_int(avr, "out_channel_layout",
_oCodecCtx->channel_layout, 0);
av_opt_set_int(avr, "in_sample_rate", _iCodecCtx->sample_rate, 0);
av_opt_set_int(avr, "out_sample_rate", _oCodecCtx->sample_rate, 0);
av_opt_set_int(avr, "in_sample_fmt", _iCodecCtx->sample_fmt, 0);
av_opt_set_int(avr, "out_sample_fmt", _oCodecCtx->sample_fmt, 0);
//open the sampler
avresample_open(avr);
int len;
int frameFinished = 0;
int64_t first_pts = -1;
_nbSamples = 0;
AVFrame* oframe = avcodec_alloc_frame();
while (av_read_frame(_iFormatCtx, &packet) >= 0) {
//packet of another audio or video stream
if (packet.stream_index != _streamId) {
continue;
}
//check for corrupted packets/invalid mp3 header
if (packet.duration == 0) {
continue;
}
if (first_pts == -1 && packet.pts != AV_NOPTS_VALUE)
first_pts = packet.pts;
avcodec_get_frame_defaults(frame);
len = avcodec_decode_audio4(_iCodecCtx, frame,
&frameFinished, &packet);
if (len < 0) {
char error[128];
av_strerror(len, error, 128);
#ifdef GLOG_IN_USE
DLOG(INFO) << "Error decoding audio " << error;
#endif
continue;
}
if (!frameFinished) {
#ifdef GLOG_IN_USE
DLOG(ERROR) << "Frame not finished";
#endif
return false;
}
avcodec_get_frame_defaults(oframe);
oframe->nb_samples = avresample_available(avr) +
av_rescale_rnd(avresample_get_delay(avr) +
frame->nb_samples,
_oCodecCtx->sample_rate,
_iCodecCtx->sample_rate,
AV_ROUND_UP);
av_samples_alloc(oframe->data, oframe->linesize,
_oCodecCtx->channels, oframe->nb_samples, AV_SAMPLE_FMT_S16, 0);
int nb_samples = avresample_convert(avr, oframe->data,
oframe->linesize[0], oframe->nb_samples, frame->data,
frame->linesize[0], frame->nb_samples);
if (!(this->*_callback)(oframe)) {
#ifdef GLOG_IN_USE
DLOG(ERROR) << "Error handling audio buffer";
#endif
av_free_packet(&packet);
av_freep(&oframe->data[0]);
break;
}
av_freep(&oframe->data[0]);
_nbSamples += oframe->nb_samples;
if (packet.data != NULL) {
av_free_packet(&packet);
}
}
i've triple-checked tha iCodecCtx->sample_rate and
oCodecCtx->sample_rate have the expected values.
thanks for your help
Florian
_______________________________________________
libav-api mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-api