ChinaFFmpeg

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9535|回复: 2

[源码] 【求助】ffmpeg音频重采样问题

[复制链接]
发表于 2013-10-10 15:53:24 | 显示全部楼层 |阅读模式
本帖最后由 洛月 于 2013-10-10 16:05 编辑

这是我的代码
return 1的时候 开始编码 写入容器后 存在爆音 音频长度被拉长
int Codec::AFrameResample(AVFrame *dst, AVFrame *src)
{
    if ((mAInCodeCtx->sample_fmt != mAOutCodeCtx->sample_fmt)||
        (mAInCodeCtx->sample_rate != mAOutCodeCtx->sample_rate)||
        (mAInCodeCtx->channels != mAOutCodeCtx->channels))
    {
        int dst_buf_size = AVCODEC_MAX_AUDIO_FRAME_SIZE * 4;
        *dst = *src;
        dst->linesize[0] = src->linesize[0];
        dst->type = 0;
        avcodec_fill_audio_frame(dst, mAOutCodeCtx->channels, AV_SAMPLE_FMT_S16, dst->data[0], dst_buf_size, 0);
        if(!mASwrCtx)
        {
            uint64_t in_channel_layout = av_get_default_channel_layout(mAInCodeCtx->channels);
            uint64_t out_channel_layout = av_get_default_channel_layout(mAOutCodeCtx->channels);
            mASwrCtx = swr_alloc_set_opts(NULL,
                                          out_channel_layout,
                                          AV_SAMPLE_FMT_S16,
                                          mAOutCodeCtx->sample_rate,
                                          in_channel_layout,
                                          mAInCodeCtx->sample_fmt,
                                          mAInCodeCtx->sample_rate,
                                          0,
                                          NULL);
            swr_init(mASwrCtx);
        }
        if(mASwrCtx)
        {
            int out_count = dst_buf_size / mAOutCodeCtx->channels / av_get_bytes_per_sample(AV_SAMPLE_FMT_S16);
            int in_count = src->linesize[0] / mAInCodeCtx->channels / av_get_bytes_per_sample(mAInCodeCtx->sample_fmt);
            int ret = swr_convert(mASwrCtx,
                                  dst->data,
                                  out_count,
                                  (const uint8_t**)src->data,
                                  in_count);
            if (ret < 0)return -1;
            src->linesize[0] = ret * av_get_bytes_per_sample(AV_SAMPLE_FMT_S16) * mAOutCodeCtx->channels;
//            memcpy(src->data[0], dst->data[0], src->linesize[0]);
            av_fifo_realloc2(mAFifo, av_fifo_size(mAFifo)+dst->linesize[0]);
            av_fifo_generic_write(mAFifo, dst->data[0], src->linesize[0], NULL);
        }
        if(av_fifo_size(mAFifo) < dst->linesize[0])
        {
            return 0;
        }
        else
        {
            av_fifo_generic_read(mAFifo,
                                 dst->data[0],
                                 dst->linesize[0],
                                 NULL);
            return 1;
        }
    }
    else
    {
        dst = src;
        return 1;
    }
}
回复

使用道具 举报

发表于 2013-10-10 15:58:42 | 显示全部楼层
一般都是sampleRate设置有问题,看是不是设置小了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-10-10 16:02:45 | 显示全部楼层
是指编码器的mAOutCodeCtx->sample_rate吗?

重采样就是为了解决源文件和目标文件采样率不一致的问题啊?
G726采样率 只能是8000 源文件采样率48000
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|Archiver|ChinaFFmpeg

GMT+8, 2025-5-18 19:03 , Processed in 0.063752 second(s), 14 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表