当前位置: 首页 > article >正文

[ffmpeg]音频格式转换

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

API 调用

常用 API

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

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 一些参数,才能正确初始化。

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 调用

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);
}

其他

所有接口

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);

http://www.kler.cn/news/309336.html

相关文章:

  • matlab绘制不同区域不同色彩的图,并显示数据(代码)
  • 【每日一题】LeetCode 2848.与车相交的点(数组、哈希表、前缀和)
  • python知识点100篇系列(18)-解析m3u8文件的下载视频
  • 东土科技加码芯片业务投资,携手神经元共建新型工业生态
  • 【开放词汇检测】基于MMDetection的MM-Grounding-DINO实战
  • 动态ip切换过快,会引起我的账号下次登录异常吗
  • CentOS详细解析及其配置方法
  • golang 字符串浅析
  • 使用Python实现深度学习模型:智能家电控制与优化
  • Nginx搭建直播服务器,并用rtmp,http-flv,hls三种模式拉流观看直播的流程
  • iOS 知识点记录
  • 区块链积分系统:革新支付安全与用户体验的未来
  • 【Petri网导论学习笔记】Petri网导论入门学习(三)
  • 启信产业大脑助力市北高新园区数字化升级,开启智慧园区新篇章
  • 自己建网站怎么建
  • OpenCV结构分析与形状描述符(24)检测两个旋转矩形之间是否相交的一个函数rotatedRectangleIntersection()的使用
  • 网络安全学习(五)Burpsuite实战
  • 解决Matlab报错:MEX 文件 ‘D:\MATLAB\toolbox\maple\maplemex.mexw64‘ 无效: 缺少依赖共享库
  • 2024年某大厂HW蓝队面试题分享
  • Github打不开解决方法
  • 产品经理有必要学习大模型技术吗?
  • 【Elasticsearch系列十一】聚合 DSL API
  • 地平线秋招2025
  • 大数据处理技术:企业岗位需求决策
  • 【机器学习】--- 深度学习中的注意力机制
  • bootstrap application nacos环境配置失效
  • C++——stack和queue的模拟实现
  • 重学SpringBoot3-SpringApplicationRunListener
  • GORM查询指南:高效检索数据
  • 认识数学建模,什么是数学建模