You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

swr_convert写入空缓冲区:是BUG还是使用不当?

同格式平面音频重采样时swr_convert崩溃问题

我用FFmpeg的swr_convert处理音频,将AV_SAMPLE_FMT_FLTP格式转成AV_SAMPLE_FMT_FLT、AV_SAMPLE_FMT_S16等格式都能成功,但保持AV_SAMPLE_FMT_FLTP格式仅修改采样率时程序会崩溃。

测试场景是把22050Hz的AV_SAMPLE_FMT_FLTP音频转成16000Hz的同格式音频,SwrContext初始化代码如下:

if (swr_alloc_set_opts2(
            &resample_context,
            &pAVContext->ch_layout, AV_SAMPLE_FMT_FLTP, 16000,
            &pAVContext->ch_layout, AV_SAMPLE_FMT_FLTP, 22050, 0, NULL) < 0)
            return ERR_SWR_INIT_FAIL;

if(swr_init(resample_context) < 0)
    return ERR_SWR_INIT_FAIL;

调用swr_convert时,程序因尝试写入空缓冲区崩溃,调用代码如下:

samples_decoded = swr_convert(ctx->pSwrContext,
    &pDecodedAudio, numOutSamples,
    (const uint8_t**)&pDecodedFrame->data, pDecodedFrame->nb_samples);

排查后发现问题出在swr_convert_internal的这段代码:

if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar
       && !(s->out_sample_fmt==AV_SAMPLE_FMT_S32P && (s->dither.output_sample_bits&31))){
        //Sample format is planar and input format is same as output format
        if(preout==in){
            out_count= FFMIN(out_count, in_count); 
            av_assert0(s->in.planar);
            copy(out, in, out_count);
            return out_count;
        }
        else if(preout==postin) preout= midbuf= postin= out;
        else if(preout==midbuf) preout= midbuf= out;
        else                    preout= out;
    }

这段代码会将未初始化的out赋值给preout,后续FFmpeg尝试写入这块未初始化的内存导致崩溃。我在FFmpeg 5.1版本及快照版本中都复现了该问题,请问这是我的使用错误还是FFmpeg的BUG?


内容的提问来源于stack exchange,提问作者MBucari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 01:50:20