[FFMpeg] 非标准分辨率视频Dump YUV注意事项
背景
做视频编解码相关开发的过程中我们经常会遇到要把视频原始YUV数据保存下来查看的情况。
使用FFMpeg
对视频解码之后原始图片数据都是保存在AVFrame
这一结构中,很多时候我们都按照图像的长宽来对数据进行保存,这在绝大部分标准分辨率中都是可行的,因为图像的宽度在内存中能刚好满足CPU的字节对齐。
而对于非标准分辨率的视频来说,应该注意AVFrame
的linesize
可能比实际的图像宽度要大。
如我在解码一个858x360分辨率的视频成YUV420P的时候,其解码出来的帧的linesize
分别为{896,448,448}
,可以看到linesize[0]
大于858,相关解释可以见AVFrame
结构体定义的备注:
/**
* 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. // <-- Suiyek: Ref Here
* 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.
*/
int linesize[AV_NUM_DATA_POINTERS];
不难看出,AVFrame.data
的数据存放格式是这样的:
data[i]:
[ (width >> component-shift) data | padding data ]
|<- - linesize[i] - ->|
所以我们在dump YUV的时候不能仅仅使用宽高来计算数据大小,应该去掉linesize
的对齐长度(以YUV420P为例):
void DumpYUV420P(const AVFrame* frame, const char* name) {
if (!frame->width || !frame->height)
return;
int ws, hs;
// 取得当前像素格式的U/V分量(若有)的右移系数(其实就是除以多少..)
av_pix_fmt_get_chroma_sub_sample(static_cast<AVPixelFormat>(frame->format), &ws, &hs);
int offset = 0;
std::ofstream ofs(name, std::ios::binary | std::ios::app);
for (int i = 0; i < frame->height; i++)
{
ofs.write((char*)(frame->data[0] + offset), frame->width);
offset += frame->linesize[0];
}
offset = 0;
for (int i = 0; i < frame->height >> hs; i++)
{
ofs.write((char*)(frame->data[1] + offset), frame->width >> ws);
offset += frame->linesize[1];
}
offset = 0;
for (int i = 0; i < frame->height >> hs; i++)
{
ofs.write((char*)(frame->data[2] + offset), frame->width >> ws);
offset += frame->linesize[2];
}
ofs.close();
}
参考
FFMpeg在很多地方都有做数据字节对齐的优化,字节对齐是很常见的计算机软件优化手段,可以参考:Data structure alignment
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步