[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);
相关推荐
我材不敲代码2 小时前
Python实现打包贪吃蛇游戏
开发语言·python·游戏
身如柳絮随风扬3 小时前
Java中的CAS机制详解
java·开发语言
-dzk-4 小时前
【代码随想录】LC 59.螺旋矩阵 II
c++·线性代数·算法·矩阵·模拟
韩立学长4 小时前
【开题答辩实录分享】以《基于Python的大学超市仓储信息管理系统的设计与实现》为例进行选题答辩实录分享
开发语言·python
froginwe115 小时前
Scala 循环
开发语言
m0_706653235 小时前
C++编译期数组操作
开发语言·c++·算法
故事和你915 小时前
sdut-Java面向对象-06 继承和多态、抽象类和接口(函数题:10-18题)
java·开发语言·算法·面向对象·基础语法·继承和多态·抽象类和接口
Bruk.Liu5 小时前
(LangChain实战2):LangChain消息(message)的使用
开发语言·langchain
qq_423233905 小时前
C++与Python混合编程实战
开发语言·c++·算法
m0_715575345 小时前
分布式任务调度系统
开发语言·c++·算法