首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >swr_convert浮动平面到S16

swr_convert浮动平面到S16
EN

Stack Overflow用户
提问于 2020-01-07 04:25:00
回答 1查看 486关注 0票数 1

如何使用libav接口AV_SAMPLE_FMT_FLTP转换为AV_SAMPLE_FMT_S16

我正在尝试弄清楚如何重采样和编码PCM (从麦克风捕获)44.1 AAC到AAC 48.0 AAC

这是我的重采样器初始化器:

代码语言:javascript
复制
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));
}

为了重新采样,我有这个代码:

代码语言:javascript
复制
    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这些文档,我不知道我到底需要传递什么给函数。我有这段代码把PCM编码成MP2 (输出是AV_SAMPLE_FMT_S16)

代码语言:javascript
复制
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是来自输入AVPacket (PCM)的原始数据

我在这里得到的是: MP2不是平面的,所以它使用相同的outPtr,与plannar不同,plannar需要两个有效的指针指向相同的可写数据。但是,例如,我需要传递给inPtr的是什么?

当我尝试使用相同的代码时,ffmpeg尝试在为空的outPtr1上编写代码。

EN

回答 1

Stack Overflow用户

发布于 2020-01-08 22:56:01

这里要做的部分应该是这样的:

代码语言:javascript
复制
int out_samples = swr_convert(swr,
                             &audio_buf,                   /* out */
                         (int)out_count,                   /* out */
             (const uint8_t**)decodedFrame->extended_data, /* in  */
                              decodedFrame->nb_samples);   /* in  */

对于out_count,可以使用类似这样的东西(您可以改进这一点):

代码语言:javascript
复制
double frame_nb = 1.0 * encoder->sample_rate / audio_st->codec->sample_rate * decodedFrame->nb_samples;
out_count = floor(frame_nb);

audio_buf是预先分配的输出缓冲区(48000*4是合适的大小)。最后,现在的问题是缓冲区中写入了多少数据。公式是这样的:

代码语言:javascript
复制
int data_size = out_samples * av_get_bytes_per_sample(encoder->sample_fmt) * decodedFrame->channels;

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59618487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档