[ffmpeg]音频格式转换

本文主要梳理 ffmpeg 中的音频格式转换。由于采集的音频数据和编码器支持的音频格式可能不一样,所以经常需要进行格式转换。

API 调用

常用 API

c 复制代码
struct SwrContext *swr_alloc(void);
int swr_init(struct SwrContext *s);
struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
                                      int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
                                      int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,
                                      int log_offset, void *log_ctx);
void swr_free(struct SwrContext **s);
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
                                const uint8_t **in , int in_count);
int swr_convert_frame(SwrContext *swr,
                      AVFrame *output, const AVFrame *input);

初始化和销毁相关

swr_alloc 创建 context 上下文结构体

swr_init 初始化 context 结构体

swr_free 销毁结构体


SwsContext class 定义 libswresample\options.c

c 复制代码
static const AVClass av_class = {
    .class_name                = "SWResampler",
    .item_name                 = context_to_name,
    .option                    = options,
    .version                   = LIBAVUTIL_VERSION_INT,
    .log_level_offset_offset   = OFFSET(log_level_offset),
    .parent_log_context_offset = OFFSET(log_ctx),
    .category                  = AV_CLASS_CATEGORY_SWRESAMPLER,
};

swr_init 之前需要配置 context 一些参数,才能正确初始化。

c 复制代码
if (av_opt_set_int(s, "ocl", out_ch_layout,   0) < 0)
     goto fail;
 if (av_opt_set_int(s, "osf", out_sample_fmt,  0) < 0)
     goto fail;
 if (av_opt_set_int(s, "osr", out_sample_rate, 0) < 0)
     goto fail;
 if (av_opt_set_int(s, "icl", in_ch_layout,    0) < 0)
     goto fail;
 if (av_opt_set_int(s, "isf", in_sample_fmt,   0) < 0)
     goto fail;
 if (av_opt_set_int(s, "isr", in_sample_rate,  0) < 0)
     goto fail;
 if (av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> user_in_ch_layout), 0) < 0)
     goto fail;
 if (av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->user_out_ch_layout), 0) < 0)
     goto fail;

为了简化调用所以有了 swr_alloc_set_opts接口,其主要就是做了 1. swr_alloc调用;2.参数设置。

由于其不像视频转换的 sws_getContext 接口,内部不会调用 swr_init,所以还需要调用一下初始化接口。

类型转换

swr_convert

swr_convert_frame

输出结果是直接写在输入的内存上的,所以 data 需要提前分配好内存

demo 调用

c 复制代码
m_asc = swr_alloc_set_opts(m_asc,
			m_ac->channel_layout, m_ac->sample_fmt, m_ac->sample_rate,
			av_get_default_channel_layout(m_inChannels), (AVSampleFormat)m_inSampleFmt, m_inSampleRate,
			0, 0);
ret = swr_init(m_asc);
const uint8_t* data[1];
data[0] = (uint8_t*)pcm;
int len = swr_convert(m_asc, m_pcm->data, m_pcm->nb_samples,
	data, m_pcm->nb_samples);
if (m_asc)
{
	swr_free(&m_asc);
}

其他

所有接口

c 复制代码
const AVClass *swr_get_class(void);
struct SwrContext *swr_alloc(void);
int swr_init(struct SwrContext *s);
int swr_is_initialized(struct SwrContext *s);
struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
                                      int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
                                      int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,
                                      int log_offset, void *log_ctx);
void swr_free(struct SwrContext **s);
void swr_close(struct SwrContext *s);
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
                                const uint8_t **in , int in_count);
int64_t swr_next_pts(struct SwrContext *s, int64_t pts);
int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
int swr_build_matrix(uint64_t in_layout, uint64_t out_layout,
                     double center_mix_level, double surround_mix_level,
                     double lfe_mix_level, double rematrix_maxval,
                     double rematrix_volume, double *matrix,
                     int stride, enum AVMatrixEncoding matrix_encoding,
                     void *log_ctx);
int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
int swr_drop_output(struct SwrContext *s, int count);
int swr_inject_silence(struct SwrContext *s, int count);
int64_t swr_get_delay(struct SwrContext *s, int64_t base);
int swr_get_out_samples(struct SwrContext *s, int in_samples);
unsigned swresample_version(void);
const char *swresample_configuration(void);
const char *swresample_license(void);
int swr_convert_frame(SwrContext *swr,
                      AVFrame *output, const AVFrame *input);
int swr_config_frame(SwrContext *swr, const AVFrame *out, const AVFrame *in);
相关推荐
牛奔17 分钟前
Go 如何避免频繁抢占?
开发语言·后端·golang
寻星探路4 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
lly2024066 小时前
Bootstrap 警告框
开发语言
2601_949146536 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧6 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
在路上看风景7 小时前
19. 成员初始化列表和初始化对象
c++
KYGALYX7 小时前
服务异步通信
开发语言·后端·微服务·ruby
zmzb01037 小时前
C++课后习题训练记录Day98
开发语言·c++
念风零壹7 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
猫头虎7 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven