FFmpeg入门 - 格式转换

2023-05-16

1、音频分⽚(plane)与打包(packed)


解码出来的AVFrame,它的data字段放的是视频像素数据或者音频的PCM裸流数据,linesize字段放的是对齐后的画面行长度或者音频的分片长度:

/**

* For video, size in bytes of each picture line.

* For audio, size in bytes of each plane.

*

* For audio, only linesize[0] may be set. For planar audio, each channel

* plane must be the same size.

*

* For video the linesizes should be multiples of the CPUs alignment

* preference, this is 16 or 32 for modern desktop CPUs.

* Some code requires such alignment other code can be slower without

* correct alignment, for yet other it makes no difference.

*

* @note The linesize may be larger than the size of usable data -- there

* may be extra padding present for performance reasons.

*/

intlinesize[AV_NUM_DATA_POINTERS];

视频相关的在之前的博客中有介绍,音频的话可以看到它只有linesize[0]会被设置,如果有多个分片,每个分片的size都是相等的。

要理解这里的分片size,先要理解音频数据的两种存储格式分⽚(plane)与打包(packed)。以常见的双声道音频为例子,

分⽚存储的数据左声道和右声道分开存储,左声道存储在data[0],右声道存储在data[1],他们的数据buffer的size都是linesize[0]。

打包存储的数据按照LRLRLR...的形式交替存储在data[0]中,这个数据buffer的size是linesize[0]。

AVSampleFormat枚举音频的格式,带P后缀的格式是分配存储的:

AV_SAMPLE_FMT_U8P, ///< unsigned 8 bits, planar

AV_SAMPLE_FMT_S16P, ///< signed 16 bits, planar

AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar

AV_SAMPLE_FMT_FLTP, ///< float, planar

AV_SAMPLE_FMT_DBLP, ///< double, planar

不带P后缀的格式是打包存储的:

AV_SAMPLE_FMT_U8, ///< unsigned 8 bits

AV_SAMPLE_FMT_S16, ///< signed 16 bits

AV_SAMPLE_FMT_S32, ///< signed 32 bits

AV_SAMPLE_FMT_FLT, ///< float

AV_SAMPLE_FMT_DBL, ///< double

2、音频数据的实际长度


这里有个坑点备注里面也写的很清楚了,linesize标明的大小可能会大于实际的音视频数据大小,因为可能会有额外的填充。

@note The linesize may be larger than the size of usable data -- there
may be extra padding present for performance reasons.

所以音频数据实际的长度需要用音频的参数计算出来:

intchannelCount=audioStreamDecoder.GetChannelCount();

intbytePerSample=audioStreamDecoder.GetBytePerSample();

intsize=frame->nb_samples*channelCount*bytePerSample;

3、音频格式转换


视频之前的demo中已经可以使用OpenGL播放,而音频可以交给OpenSL来播放,之前我写过一篇《OpenSL ES 学习笔记》详细的使用细节我就不展开介绍了,直接将代码拷贝来使用。

但是由于OpenSLES只支持打包的几种音频格式:

#define SL_PCMSAMPLEFORMAT_FIXED_8 ((SLuint16) 0x0008)

#define SL_PCMSAMPLEFORMAT_FIXED_16 ((SLuint16) 0x0010)

#define SL_PCMSAMPLEFORMAT_FIXED_20 ((SLuint16) 0x0014)

#define SL_PCMSAMPLEFORMAT_FIXED_24 ((SLuint16) 0x0018)

#define SL_PCMSAMPLEFORMAT_FIXED_28 ((SLuint16) 0x001C)

#define SL_PCMSAMPLEFORMAT_FIXED_32 ((SLuint16) 0x0020)

这里我们指的AudioStreamDecoder的目标格式为AV_SAMPLE_FMT_S16,如果原始音频格式不是它,则对音频做转码:

audioStreamDecoder.Init(reader, audioIndex, AVSampleFormat::AV_SAMPLE_FMT_S16);

boolAudioStreamDecoder::Init(MediaReader*reader, intstreamIndex, AVSampleFormatsampleFormat) {

...

boolresult=StreamDecoder::Init(reader, streamIndex);

if (sampleFormat==AVSampleFormat::AV_SAMPLE_FMT_NONE) {

mSampleFormat=mCodecContext->sample_fmt;

} else {

mSampleFormat=sampleFormat;

}

if (mSampleFormat!=mCodecContext->sample_fmt) {

mSwrContext=swr_alloc_set_opts(

NULL,

mCodecContext->channel_layout,

mSampleFormat,

mCodecContext->sample_rate,

mCodecContext->channel_layout,

mCodecContext->sample_fmt,

mCodecContext->sample_rate,

0,

NULL);

swr_init(mSwrContext);

// 虽然前面的swr_alloc_set_opts已经设置了这几个参数

// 但是用于接收的AVFrame不设置这几个参数也会接收不到数据

// 原因是后面的swr_convert_frame函数会通过av_frame_get_buffer创建数据的buff

// 而av_frame_get_buffer需要AVFrame设置好这些参数去计算buff的大小

mSwrFrame=av_frame_alloc();

mSwrFrame->channel_layout=mCodecContext->channel_layout;

mSwrFrame->sample_rate=mCodecContext->sample_rate;

mSwrFrame->format=mSampleFormat;

}

returnresult;

}

AVFrame*AudioStreamDecoder::NextFrame() {

AVFrame*frame=StreamDecoder::NextFrame();

if (NULL==frame) {

returnNULL;

}

if (NULL==mSwrContext) {

returnframe;

}

swr_convert_frame(mSwrContext, mSwrFrame, frame);

returnmSwrFrame;

}

这里我们使用swr_convert_frame进行转码:

intswr_convert_frame(SwrContext*swr, // 转码上下文

AVFrame*output, // 转码后输出到这个AVFrame

constAVFrame*input// 原始输入AVFrame

);

这个方法要求输入输出的AVFrame都设置了channel_layout、 sample_rate、format参数,然后回调用av_frame_get_buffer为output创建数据buff:

/**

* ...

*

* Input and output AVFrames must have channel_layout, sample_rate and format set.

*

* If the output AVFrame does not have the data pointers allocated the nb_samples

* field will be set using av_frame_get_buffer()

* is called to allocate the frame.

* ...

*/

intswr_convert_frame(SwrContext*swr,

AVFrame*output, constAVFrame*input);

SwrContext为转码的上下文,通过swr_alloc_set_opts和swr_init创建,需要把转码前后的音频channel_layout、 sample_rate、format信息传入:

structSwrContext*swr_alloc_set_opts(structSwrContext*s,

int64_tout_ch_layout, enumAVSampleFormatout_sample_fmt, intout_sample_rate,

int64_t in_ch_layout, enumAVSampleFormat in_sample_fmt, int in_sample_rate,

intlog_offset, void*log_ctx);

intswr_init(structSwrContext*s);

4、视频格式转换


之前的demo里面我们判断了视频格式不为AV_PIX_FMT_YUV420P则直接报错,这里我们仿照音频转换的例子,判断原始视频格式不为AV_PIX_FMT_YUV420P则使用sws_scale进行格式转换:

boolVideoStreamDecoder::Init(MediaReader*reader, intstreamIndex, AVPixelFormatpixelFormat) {

...

boolresult=StreamDecoder::Init(reader, streamIndex);

if (AVPixelFormat::AV_PIX_FMT_NONE==pixelFormat) {

mPixelFormat=mCodecContext->pix_fmt;

} else {

mPixelFormat=pixelFormat;

}

if (mPixelFormat!=mCodecContext->pix_fmt) {

intwidth=mCodecContext->width;

intheight=mCodecContext->height;

mSwrFrame=av_frame_alloc();

// 方式一,使用av_frame_get_buffer创建数据存储空间,av_frame_free的时候会自动释放

mSwrFrame->width=width;

mSwrFrame->height=height;

mSwrFrame->format=mPixelFormat;

av_frame_get_buffer(mSwrFrame, 0);

// 方式二,使用av_image_fill_arrays指定存储空间,需要我们手动调用av_malloc、av_free去创建、释放空间

// unsigned char* buffer = (unsigned char *)av_malloc(

// av_image_get_buffer_size(mPixelFormat, width, height, 16)

// );

// av_image_fill_arrays(mSwrFrame->data, mSwrFrame->linesize, buffer, mPixelFormat, width, height, 16);

mSwsContext=sws_getContext(

mCodecContext->width, mCodecContext->height, mCodecContext->pix_fmt,

width, height, mPixelFormat, SWS_BICUBIC,

NULL, NULL, NULL

);

}

returnresult;

}

AVFrame*VideoStreamDecoder::NextFrame() {

AVFrame*frame=StreamDecoder::NextFrame();

if (NULL==frame) {

returnNULL;

}

if (NULL==mSwsContext) {

returnframe;

}

sws_scale(mSwsContext, frame->data,

frame->linesize, 0, mCodecContext->height,

mSwrFrame->data, mSwrFrame->linesize);

returnmSwrFrame;

}

sws_scale看名字虽然是缩放,但它实际上也会对format进行转换,转换的参数由SwsContext提供:

structSwsContext*sws_getContext(

intsrcW, // 源图像的宽

intsrcH, // 源图像的高

enumAVPixelFormatsrcFormat, // 源图像的格式

intdstW, // 目标图像的宽

intdstH, // 目标图像的高

enumAVPixelFormatdstFormat, // 目标图像的格式

intflags, // 暂时可忽略

SwsFilter*srcFilter, // 暂时可忽略

SwsFilter*dstFilter, // 暂时可忽略

constdouble*param // 暂时可忽略

);

sws_scale支持区域转码,可以如我们的demo将整幅图像进行转码,也可以将图像切成多个区域分别转码,这样方便实用多线程加快转码效率:

intsws_scale(

structSwsContext*c, // 转码上下文

constuint8_t*constsrcSlice[], // 源画面区域像素数据,对应源AVFrame的data字段

constintsrcStride[], // 源画面区域行宽数据,对应源AVFrame的linesize字段

intsrcSliceY, // 源画面区域起始Y坐标,用于计算应该放到目标图像的哪个位置

intsrcSliceH, // 源画面区域行数,用于计算应该放到目标图像的哪个位置

uint8_t*constdst[], // 转码后图像数据存储,对应目标AVFrame的data字段

constintdstStride[] // 转码后行宽数据存储,对应目标AVFrame的linesize字段

);

srcSlice和srcStride存储了源图像部分区域的图像数据,srcSliceY和srcSliceH告诉转码器这部分区域的坐标范围,用于计算偏移量将转码结果存放到dst和dstStride中。

例如下面的代码就将一幅完整的图像分成上下两部分分别进行转码:

inthalfHeight=mCodecContext->height/2;

// 转码上半部分图像

uint8_t*dataTop[AV_NUM_DATA_POINTERS] = {

frame->data[0],

frame->data[1],

frame->data[2]

};

sws_scale(mSwsContext, dataTop,

frame->linesize, 0,

halfHeight,

mSwrFrame->data, mSwrFrame->linesize);

// 转码下半部分图像

uint8_t*dataBottom[AV_NUM_DATA_POINTERS] = {

frame->data[0] + (frame->linesize[0] *halfHeight),

frame->data[1] + (frame->linesize[1] *halfHeight),

frame->data[2] + (frame->linesize[2] *halfHeight),

};

sws_scale(mSwsContext, dataBottom,

frame->linesize, halfHeight,

mCodecContext->height-halfHeight,

mSwrFrame->data, mSwrFrame->linesize);

5、AVFrame内存管理机制


我们创建了一个新的AVFrame用于接收转码后的图像:

mSwrFrame=av_frame_alloc();

// 方式一,使用av_frame_get_buffer创建数据存储空间,av_frame_free的时候会自动释放

mSwrFrame->width=width;

mSwrFrame->height=height;

mSwrFrame->format=mPixelFormat;

av_frame_get_buffer(mSwrFrame, 0);

// 方式二,使用av_image_fill_arrays指定存储空间,需要我们手动调用av_malloc、av_free去创建、释放buffer的空间

// int bufferSize = av_image_get_buffer_size(mPixelFormat, width, height, 16);

// unsigned char* buffer = (unsigned char *)av_malloc(bufferSize);

// av_image_fill_arrays(mSwrFrame->data, mSwrFrame->linesize, buffer, mPixelFormat, width, height, 16);

av_frame_alloc创建出来的AVFrame只是一个壳,我们需要为它提供实际存储像素数据和行宽数据的内存空间,如上所示有两种方法:

1.通过av_frame_get_buffer创建存储空间,data成员的空间实际上是由buf[0]->data提供的:

LOGD("mSwrFrame --> buf : 0x%X~0x%X, data[0]: 0x%X, data[1]: 0x%X, data[2]: 0x%X",

mSwrFrame->buf[0]->data,

mSwrFrame->buf[0]->data+mSwrFrame->buf[0]->size,

mSwrFrame->data[0],

mSwrFrame->data[1],

mSwrFrame->data[2]

);

// mSwrFrame --> buf : 0x2E6E8AC0~0x2E753F40, data[0]: 0x2E6E8AC0, data[1]: 0x2E7302E0, data[2]: 0x2E742100

  1. 通过av_image_fill_arrays指定外部存储空间,data成员的空间就是我们指的的外部空间,而buf成员是NULL:

LOGD("mSwrFrame --> buffer : 0x%X~0x%X, buf : 0x%X, data[0]: 0x%X, data[1]: 0x%X, data[2]: 0x%X",

buffer,

buffer+bufferSize,

mSwrFrame->buf[0],

mSwrFrame->data[0],

mSwrFrame->data[1],

mSwrFrame->data[2]

);

// FFmpegDemo: mSwrFrame --> buffer : 0x2DAE4DC0~0x2DB4D5C0, buf : 0x0, data[0]: 0x2DAE4DC0, data[1]: 0x2DB2A780, data[2]: 0x2DB3BEA0

而av_frame_free内部会去释放AVFrame里buf的空间,对于data成员它只是简单的把指针赋值为0,所以通过av_frame_get_buffer创建存储空间,而通过av_image_fill_arrays指定外部存储空间需要我们手动调用av_free去释放外部空间。

6、align


细心的同学可能还看到了av_image_get_buffer_size和av_image_fill_arrays都传了个16的align,这里对应的就是之前讲的linesize的字节对齐,会填充数据让linesize变成16、或者32的整数倍:

@paramalign thevalueusedinsrcforlinesizealignment

这里如果为0会填充失败:

1.png

而为1不做填充会出现和实际解码中的linesize不一致导致画面异常:

2.png

av_frame_get_buffer则比较人性化,它推荐你填0让它自己去判断应该按多少对齐:

*@paramalignRequiredbuffersizealignment. Ifequalto0, alignmentwillbe

* chosenautomaticallyforthecurrentCPU. Itishighly

* recommendedtopass0hereunlessyouknowwhatyouaredoing.

7、完整代码


完整的demo代码已经放到Github上,感兴趣的同学可以下载来看看

原文:https://www.jianshu.com/p/9a093d8b91ef

★文末名片可以免费领取音视频开发学习资料,内容包括(FFmpeg ,webRTC ,rtmp ,hls ,rtsp ,ffplay ,srs)以及音视频学习路线图等等。

见下方!↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

FFmpeg入门 - 格式转换 的相关文章

随机推荐

  • 滑动窗口【区间最大值区间&最小值】【单调队列】

    问题描述 ZJM 有一个长度为 n 的数列和一个大小为 k 的窗口 窗口可以在数列上来回移动 现在 ZJM 想知道在窗口从左往右滑的时候 xff0c 每次窗口内数的最大值和最小值分别是多少 例如 xff1a 数列是 1 3 1 3 5 3
  • Q老师的考验【矩阵快速幂】【斐波那契数列】

    问题描述 Q老师 对数列有一种非同一般的热爱 xff0c 尤其是优美的斐波那契数列 这一天 xff0c Q老师 为了增强大家对于斐波那契数列的理解 xff0c 决定在斐波那契的基础上创建一个新的数列 f x 来考一考大家 数列 f x 定义
  • Q老师度假【动态规划dp】【矩阵快速幂优化】

    问题描述 忙碌了一个学期的 Q老师 决定奖励自己 N 天假期 假期中不同的穿衣方式会有不同的快乐值 已知 Q老师 一共有 M 件衬衫 xff0c 且如果昨天穿的是衬衫 A xff0c 今天穿的是衬衫 B xff0c 则 Q老师 今天可以获得
  • 插值算法[数学建模]

    插值 插值算法一维数据插值方法分段线性插值分段二次插值 xff08 分段抛物插值 xff09 拉格朗日插值法龙格现象 牛顿插值法埃尔米特插值法分段三次埃尔米特插值PCHIP 样条插值三次样条插值 n维数据插值应用 插值算法 在工程和数学应用
  • 时间序列【数学建模】

    时间序列 确定性时间序列分析方法移动平均法简单移动平均法加权移动平均法趋势移动平均法 指数平滑法一次指数平滑法二次指数平滑法三次指数平滑法 差分指数平滑法一阶差分指数平滑法二阶差分指数平滑法 具有季节性特点的时间序列的预测 平稳时间序列模型
  • UITabBarController的简单使用和属性方法总结

    一 引言 与导航控制器相类似 xff0c 标签控制器也是用于管理视图控制器的一个UI控件 xff0c 在其内部封装了一个标签栏 xff0c 与导航不同的是 xff0c 导航的管理方式是纵向的 xff0c 采用push与pop切换控制器 xf
  • 多元分析——聚类分析【数学建模】

    聚类分析 Q型聚类分析样本的相似性度量类与类间的相似性度量聚类图最短距离法的聚类举例Matlab 聚类分析的相关命令pdistlinkageclusterzsore X H 61 dendrogram Z P T 61 clusterdat
  • 多元分析——主成分分析【数学建模】

    主成分分析的主要目的是希望用较少的变量去解释原来资料中的大部分变异 xff0c 将许多相关性较高的变量转化为彼此相互独立或不相关的变量 通常是选出的比原始变量个数少 xff0c 能解释大部份资料中的变异的几个新变量 xff0c 即所谓主成分
  • Pytorch Resnet101

    Pytorch Resnet101 模型使用模板 模型 span class token string 34 34 span 34 model span class token operator span span class token
  • t-SNE可视化

    t SNE的全称是t Distributed Stochastic Neighbor Embedding xff0c 是一种降低维度的技术 xff0c 适用于将高维度数据可视化 它将数据点之间的相似性转换为联合概率 xff0c 并试图最小化
  • ubuntu下为apt-get设置代理

    debian mint都可以按此设置 xff08 debian系均可 xff0c 直接基于debian或间接基于debian的 xff09 现在公司很多都是通过代理上网的 xff0c 通过代理上网apt get install无法通网络进行
  • 开源虚拟化集群管理平台ProxmoxVE-安装介绍以及PCI直通和嵌套虚拟化说明

    说明 xff1a 当前测试环境为PVE7 0版本 官网 xff1a Proxmox Powerful open source server solutions 目录 背景 安装系统 PCI直通配置 xff08 开启iommu xff09 允
  • qt Linux arm 交叉编译

    1 操作系统 ubuntu1 20 04 2 qt5 9 9 Index of archive qt 5 9 5 9 9 下载qt源码 qt everywhere opensource src 5 9 9 tar xz 和程序 qt ope
  • 三角函数:图像和性质关系

    紧接上一篇 xff1a http blog csdn net yinhun2012 article details 79377728 这次我们通过函数图文和unity程序来观察三角函数的图像 xff0c 首先从基本的来 xff1a 1 f
  • Linux 每天定时关机 设置

    sudo gedit etc crontab 输入密码后 xff0c 在该文件插入一行 50 02 root sbin shutdown h now 保存后 xff0c 系统会在每天02 50 自动关机
  • anaconda如何配置环境变量

    anaconda安装好后 xff0c 在cmd输入conda xff0c 显示 xff1a conda 不是内部或外部命令 xff0c 也不是可运行的程序 或批处理文件 原因是 xff1a anaconda没有配置环境变量 那接下来我就教大
  • Windows下用 Code blocks + mingw 搭建 Fortran 编译环境

    方法一 xff1a 下载页面 xff1a http www codeblocks org downloads binaries 下载 codeblocks 17 12mingw fortran setup exe 这样的版本 方法二 xff
  • 音频编码格式介绍-AAC

    目录 概述 帧格式 算法简介 开源的软件 AAC和MP3的关键性不同 参考阅读 1 概述 AAC xff08 Advanced Audio Coding xff09 xff0c 被认为是MP3的继任者 xff0c 相对MP3有更高的压缩效率
  • FFmpeg入门 - 视频播放

    音视频最好从能够直接看到东西 也更加贴近用户的播放开始学起 音视频编解码基础 我们可以通过http rtmp或者本地的视频文件去播放视频 这里的 34 视频 34 实际上指的是mp4 avi这种既有音频也有视频的文件格式 这样的视频文件可能
  • FFmpeg入门 - 格式转换

    1 音频分 plane 与打包 packed 解码出来的AVFrame 它的data字段放的是视频像素数据或者音频的PCM裸流数据 linesize字段放的是对齐后的画面行长度或者音频的分片长度 For video size in byte