Reputation: 826
How convert using libav API AV_SAMPLE_FMT_FLTP
to AV_SAMPLE_FMT_S16
I'm trying to figure out how to resample and encode PCM (captured from Microphone) 44.1KHz to AAC 48.0KHz
That's my resampler initializer:
void initialize_resampler(SwrContext*& resamplerCtx, AVCodecContext* encoder, AVFrame*& rawResampledAudioFrame, AVStream* audioFormatStream)
{
int nb_samples = (encoder->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE) ? encoder->sample_rate : encoder->frame_size;
int encoderFrameSize = encoder->channels * av_get_bytes_per_sample(encoder->sample_fmt) * encoder->frame_size;
rawResampledAudioFrame = allocate_audioframe(encoder->sample_fmt, encoder->channel_layout, encoder->sample_rate, nb_samples);
// Copy the stream parameters to the muxer
check(avcodec_parameters_from_context(audioFormatStream->codecpar, encoder));
// Create resampler context
resamplerCtx = swr_alloc();
if (resamplerCtx == nullptr)
throw std::runtime_error("Could not allocate resampler context");
// Set options
check(av_opt_set_int(resamplerCtx, "in_channel_count", 2, 0));
check(av_opt_set_int(resamplerCtx, "in_sample_rate", 44100, 0));
check(av_opt_set_sample_fmt(resamplerCtx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0));
check(av_opt_set_int(resamplerCtx, "out_channel_count", encoder->channels, 0));
check(av_opt_set_int(resamplerCtx, "out_sample_rate", encoder->sample_rate, 0));
check(av_opt_set_sample_fmt(resamplerCtx, "out_sample_fmt", encoder->sample_fmt, 0));
// initialize the resampling context
check(swr_init(resamplerCtx));
}
And for resample I've this code:
AVPacket pkt{};
while (true)
{
AVPacket input_packet;
av_init_packet(&input_packet);
check(av_read_frame(inputContext, &input_packet));
check(avcodec_send_packet(decoderContext, &input_packet));
check(avcodec_receive_frame(decoderContext, decodedFrame));
// WHAT DO HERE swr_convert(resamplerContext, )
av_packet_unref(&input_packet);
av_init_packet(&pkt);
auto in_stream = inputContext->streams[pkt.stream_index];
auto out_stream = outputContext->streams[pkt.stream_index];
pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
pkt.pos = -1;
check(avcodec_send_frame(encoderContext, decodedFrame));
check(avcodec_receive_packet(encoderContext, &pkt));
check(av_interleaved_write_frame(outputContext, &pkt));
av_packet_unref(&pkt);
}
REading the docs I can't figure out what exactly I need to pass to function. I have this code to encode PCM to MP2 (output is AV_SAMPLE_FMT_S16
)
const uint8_t* inPtr[] { const_cast<const uint8_t*>(&pcmData[0]), nullptr, nullptr,nullptr,nullptr,nullptr,nullptr,nullptr };
uint8_t* outPtr[] { &resampledAudioData[0], nullptr, nullptr,nullptr,nullptr,nullptr,nullptr,nullptr };
int resampledSamplesCount{ swr_convert(
resamplerCtx,
outPtr,
maxResampledSamplesCount,
inPtr,
inputSampleCount) };
// Negativo indica erro.
check(resampledSamplesCount);
pcmData is raw data from input AVPacket
(PCM)
What I get here is: MP2 isn't planar so it uses the same outPtr[0] different from plannar which needs two valid pointers to same writable data. But what I need to pass to inPtr, for example?
When I try to use the same code, ffmpeg try to write on outPtr[1] which is nullptr.
Upvotes: 1
Views: 973
Reputation: 1988
WHAT TO DO HERE part should be something like this:
int out_samples = swr_convert(swr,
&audio_buf, /* out */
(int)out_count, /* out */
(const uint8_t**)decodedFrame->extended_data, /* in */
decodedFrame->nb_samples); /* in */
As for out_count
something like this can be used (you can improve this):
double frame_nb = 1.0 * encoder->sample_rate / audio_st->codec->sample_rate * decodedFrame->nb_samples;
out_count = floor(frame_nb);
audio_buf
is your output buffer allocate beforehand (48000*4 is good size).
Finally now the question is how much data written the buffer. This is the formula:
int data_size = out_samples * av_get_bytes_per_sample(encoder->sample_fmt) * decodedFrame->channels;
Hope this helps.
Upvotes: 1