SwrContext实现音频重采样

音频的重采样可以使用SwrContext这个结构体来实现首先要对这个结构体进行初始化代码如下

- (SwrContext *)get_swrcontext_fa_oc{
    int ret = 0;
    SwrContext *s_ctx = NULL;
    uint64_t src_channel_layout_in = self.resample_a_config.channel_layout_in;
    int src_sample_fmt = self.resample_a_config.sample_fmt_in;
    int src_sample_rate = self.resample_a_config.sample_rate_in;
    
    uint64_t dst_channel_layout_out = self.resample_a_config.channel_layout_out;
    int dst_sample_fmt = self.resample_a_config.sample_fmt_out;
    int dst_sample_rate = self.resample_a_config.sample_rate_out;
//    s_ctx = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_FLTP, 44100, src_channel_layout_in, src_sample_fmt, src_sample_rate, 0, NULL);
    s_ctx = swr_alloc_set_opts(NULL, dst_channel_layout_out, dst_sample_fmt, dst_sample_rate, src_channel_layout_in, src_sample_fmt, src_sample_rate, 0, NULL);
    if (!s_ctx) {
        fprintf(stderr, "Failed to open s_ctx");
        return NULL;
    }
    ret = swr_init(s_ctx);
    if (ret != 0) {
        fprintf(stderr,"swr_init failed");
    } else {
        fprintf(stderr,"swr_init success");
    }
    return s_ctx;
}



//或者这样设置参数
/*
av_opt_set_int(swrCtx, "in_channel_layout", src_ch_layout, 0);
av_opt_set_int(swrCtx, "in_sample_rate", src_rate, 0);
av_opt_set_sample_fmt(swrCtx, "in_sample_fmt", src_sample_fmt, 0);
    
av_opt_set_int(swrCtx, "out_channel_layout", dst_ch_layout, 0);
av_opt_set_int(swrCtx, "out_sample_rate", dst_rate, 0);
av_opt_set_sample_fmt(swrCtx, "out_sample_fmt", dst_sample_fmt, 0);
*/

这个结构体是输出参数在前输入参数在后而视频裁剪的结构体SwsContext与之不同,是输入在前输出在后

之前应该是在ffmpeg3.0以前,使用ffmpeg进行音频编码音频的采样位深需要的是S16格式即可,自从改版后需要的是FLTP格式所以要对其进行重采样.

关于位深的说明这里说一下个人的理解,如果有不对的地方请指正

1.S16 这种格式单个采样点大小占2个字节左右声道数据是存储在一起的类似这种方式LRLRLRLR

2.S16P这种方式单个采样点大小同样占2个字节但是左右声道数据是分开存储的分别在data[0]以及data[1]中存储方式为

data[0]=LLLLLLLLLLLLLL data[1]=RRRRRRRRRRR单通道的样本数也是分开存储在linesize[0]当中,因为左右声道样本数是一样的,而非P模式下linesize[0]存储的是LRLR全部的字节数

进行重采样不光可以改变采样位深也可以改变采样率,通常情况下采样率由大到小不会出现问题,但是由小到大则需要重新计算单通道采样数使用如下API

        av_rescale_rnd((int64_t)src_nb_samples_d,(int64_t) dst_sample_rate_d, (int64_t)src_sample_rate_d, AV_ROUND_UP);

如果改变采样率可以参看如下代码然后再去实现封装

static int get_format_from_sample_fmt(const char **fmt,
                                      enum AVSampleFormat sample_fmt)
{
    int i;
    struct sample_fmt_entry {
        enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
    } sample_fmt_entries[] = {
        { AV_SAMPLE_FMT_U8,  "u8",    "u8"    },
        { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
        { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
        { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
        { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
    };
    *fmt = NULL;

    for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
        struct sample_fmt_entry *entry = &sample_fmt_entries[i];
        if (sample_fmt == entry->sample_fmt) {
            *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
            return 0;
        }
    }

    fprintf(stderr,
            "Sample format %s not supported as output format\n",
            av_get_sample_fmt_name(sample_fmt));
    return AVERROR(EINVAL);
}

/**
 * Fill dst buffer with nb_samples, generated starting from t.
 */
static void fill_samples(double *dst, int nb_samples, int nb_channels, int sample_rate, double *t)
{
    int i, j;
    double tincr = 1.0 / sample_rate, *dstp = dst;
    const double c = 2 * M_PI * 440.0;

    /* generate sin tone with 440Hz frequency and duplicated channels */
    for (i = 0; i < nb_samples; i++) {
        *dstp = sin(c * *t);
        for (j = 1; j < nb_channels; j++)
            dstp[j] = dstp[0];
        dstp += nb_channels;
        *t += tincr;
    }
}

int main(int argc, char **argv)
{
    int64_t src_ch_layout = AV_CH_LAYOUT_STEREO, dst_ch_layout = AV_CH_LAYOUT_SURROUND;
    int src_rate = 48000, dst_rate = 44100;
    uint8_t **src_data = NULL, **dst_data = NULL;
    int src_nb_channels = 0, dst_nb_channels = 0;
    int src_linesize, dst_linesize;
    int src_nb_samples = 1024, dst_nb_samples, max_dst_nb_samples;
    enum AVSampleFormat src_sample_fmt = AV_SAMPLE_FMT_DBL, dst_sample_fmt = AV_SAMPLE_FMT_S16;
    const char *dst_filename = NULL;
    FILE *dst_file;
    int dst_bufsize;
    const char *fmt;
    struct SwrContext *swr_ctx;
    double t;
    int ret;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s output_file\n"
                "API example program to show how to resample an audio stream with libswresample.\n"
                "This program generates a series of audio frames, resamples them to a specified "
                "output format and rate and saves them to an output file named output_file.\n",
            argv[0]);
        exit(1);
    }
    dst_filename = argv[1];

    dst_file = fopen(dst_filename, "wb");
    if (!dst_file) {
        fprintf(stderr, "Could not open destination file %s\n", dst_filename);
        exit(1);
    }

    /* create resampler context */
    swr_ctx = swr_alloc();
    if (!swr_ctx) {
        fprintf(stderr, "Could not allocate resampler context\n");
        ret = AVERROR(ENOMEM);
        goto end;
    }

    /* set options */
    av_opt_set_int(swr_ctx, "in_channel_layout",    src_ch_layout, 0);
    av_opt_set_int(swr_ctx, "in_sample_rate",       src_rate, 0);
    av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);

    av_opt_set_int(swr_ctx, "out_channel_layout",    dst_ch_layout, 0);
    av_opt_set_int(swr_ctx, "out_sample_rate",       dst_rate, 0);
    av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);

    /* initialize the resampling context */
    if ((ret = swr_init(swr_ctx)) < 0) {
        fprintf(stderr, "Failed to initialize the resampling context\n");
        goto end;
    }

    /* allocate source and destination samples buffers */

    src_nb_channels = av_get_channel_layout_nb_channels(src_ch_layout);
    ret = av_samples_alloc_array_and_samples(&src_data, &src_linesize, src_nb_channels,
                                             src_nb_samples, src_sample_fmt, 0);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate source samples\n");
        goto end;
    }

    /* compute the number of converted samples: buffering is avoided
     * ensuring that the output buffer will contain at least all the
     * converted input samples */
    max_dst_nb_samples = dst_nb_samples =
        av_rescale_rnd(src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);

    /* buffer is going to be directly written to a rawaudio file, no alignment */
    dst_nb_channels = av_get_channel_layout_nb_channels(dst_ch_layout);
    ret = av_samples_alloc_array_and_samples(&dst_data, &dst_linesize, dst_nb_channels,
                                             dst_nb_samples, dst_sample_fmt, 0);
    if (ret < 0) {
        fprintf(stderr, "Could not allocate destination samples\n");
        goto end;
    }

    t = 0;
    do {
        /* generate synthetic audio */
        fill_samples((double *)src_data[0], src_nb_samples, src_nb_channels, src_rate, &t);

        /* compute destination number of samples */
        dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, src_rate) +
                                        src_nb_samples, dst_rate, src_rate, AV_ROUND_UP);
        if (dst_nb_samples > max_dst_nb_samples) {
            av_freep(&dst_data[0]);
            ret = av_samples_alloc(dst_data, &dst_linesize, dst_nb_channels,
                                   dst_nb_samples, dst_sample_fmt, 1);
            if (ret < 0)
                break;
            max_dst_nb_samples = dst_nb_samples;
        }

        /* convert to destination format */
        ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples);
        if (ret < 0) {
            fprintf(stderr, "Error while converting\n");
            goto end;
        }
        dst_bufsize = av_samples_get_buffer_size(&dst_linesize, dst_nb_channels,
                                                 ret, dst_sample_fmt, 1);
        if (dst_bufsize < 0) {
            fprintf(stderr, "Could not get sample buffer size\n");
            goto end;
        }
        printf("t:%f in:%d out:%d\n", t, src_nb_samples, ret);
        fwrite(dst_data[0], 1, dst_bufsize, dst_file);
    } while (t < 10);

    if ((ret = get_format_from_sample_fmt(&fmt, dst_sample_fmt)) < 0)
        goto end;
    fprintf(stderr, "Resampling succeeded. Play the output file with the command:\n"
            "ffplay -f %s -channel_layout %"PRId64" -channels %d -ar %d %s\n",
            fmt, dst_ch_layout, dst_nb_channels, dst_rate, dst_filename);

end:
    fclose(dst_file);

    if (src_data)
        av_freep(&src_data[0]);
    av_freep(&src_data);

    if (dst_data)
        av_freep(&dst_data[0]);
    av_freep(&dst_data);

    swr_free(&swr_ctx);
    return ret < 0;
}

如果不改变采样率大小 在调用时直接这样调用就可以了

        ret = swr_convert(swr_ctx, dst_data, dst_nb_samples, (const uint8_t **)src_data, src_nb_samples);
还有一种情况就是改变重采样的nb_sampes 我单独由1024重采样为512 不改变采样率以及采用格式,然后输出pcm数据进行播放发现输出的声音有问题这块我也调研半天,一直没有搞懂是怎么回事,网上也没看见有好的说明,可能是不能修改单通道采样数的大小吧.

进行重采样之前分别建立了输入输出缓冲区

    int src_nb_channels_d = self.resample_a_config.channels_in;
    int src_nb_samples_d = self.resample_a_config.nb_samples_in;
    int src_sample_fmt_d = self.resample_a_config.sample_fmt_in;
    int dst_nb_channels_d = self.resample_a_config.channels_out;
    int dst_nb_samples_d = self.resample_a_config.nb_samples_out;
    int dst_sample_fmt_d = self.resample_a_config.sample_fmt_out;
    av_samples_alloc_array_and_samples(&src_data, &in_count, src_nb_channels_d , src_nb_samples_d, src_sample_fmt_d, 0);
    av_samples_alloc_array_and_samples(&dst_data, &out_count, dst_nb_channels_d, dst_nb_samples_d, dst_sample_fmt_d,0);
    memset(&src_data[0][0], 0, in_count);
    memset(&dst_data[0][0], 0, out_count);

然后调用如下方法进行重采样

- (void)resample_audio_with_avframe:(AVFrame *)avframe{
    
    int src_sample_rate_d = self.resample_a_config.sample_rate_in;
    int dst_sample_rate_d = self.resample_a_config.sample_rate_out;
    int src_nb_samples_d = self.resample_a_config.nb_samples_in;
    int dst_nb_channels_d = self.resample_a_config.channels_out;
    int dst_sample_fmt_d = self.resample_a_config.sample_fmt_out;
    memset(&src_data[0][0], 0, in_count);
    memset(&dst_data[0][0], 0, out_count);
    int64_t dst_nb_samples,max_dst_nb_samples;
    int ret = -1;
    max_dst_nb_samples = dst_nb_samples =
        av_rescale_rnd((int64_t)src_nb_samples_d,(int64_t) dst_sample_rate_d, (int64_t)src_sample_rate_d, AV_ROUND_UP);
    dst_nb_samples = av_rescale_rnd(swr_get_delay(s_audio_ctx, src_sample_rate_d) + src_nb_samples_d, dst_sample_rate_d, src_sample_rate_d, AV_ROUND_UP);
    if (dst_nb_samples > max_dst_nb_samples) {
        av_freep(&dst_data[0]);
        ret = av_samples_alloc(dst_data, &out_count, dst_nb_channels_d,
                               (int)dst_nb_samples, dst_sample_fmt_d, 1);
        max_dst_nb_samples = dst_nb_samples;
    }
    memcpy((void *)src_data[0], (void*)avframe->data[0], avframe->linesize[0]);
    swr_convert(s_audio_ctx, dst_data,(int) dst_nb_samples, (const uint8_t **)src_data, src_nb_samples_d);
    memcpy((void *)output_a_frame->data[0], dst_data[0],out_count);
    output_a_frame->pts =  avframe->pts;
    self.callback(Collect_Data_Audio,output_a_frame);
}

同样将重采样后的数据以callback的方式返回,在外部可以进行编码操作

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值