如何使用 ffmpeg 的 swscale 从 NV12 转换为 RGB32?

How can I use ffmpeg's swscale to convert from NV12 to RGB32?

本文关键字:转换 RGB32 NV12 何使用 ffmpeg swscale      更新时间:2023-10-16

假设我在内存中有一个NV12帧作为字节数组。我知道:

  • 其宽度和高度
  • 它的Stride(一条线的总宽度,包括padding),这是相同的Y和UV组件根据NV12规范
  • 我知道Y从哪里开始,U从Y + (Stride * Height)开始,V从U + 1(与U交叉)开始。

现在我写的是:

SwsContext* context = sws_getContext(frameWidth, frameHeight, AV_PIX_FMT_NV12, frameWidth, frameHeight, AV_PIX_FMT_RGB32, 0, nullptr, nullptr, nullptr);
sws_scale(context, 

我不知道sws_scale的参数应该是什么

  • srcSlice:指向字节数组的指针?显然应该是a一个指针指向另一个指针,但我得到的只是一个单维空间
  • srcStride:显然需要一个跨步数组,但是我只有一个跨步用于整个文件。我应该传递一个数组吗一个元素?
  • srcSliceY:我猜第一个字节的偏移量?那么应该是0。
  • srcSliceH:帧高度,我猜
  • dst:再一次,指针指向指针,但我的目标输出实际上只是另一个字节数组…
  • dstStride:宽度* 4我猜?

感谢您的帮助。

/// uint8_t * Y;
/// uint8_t * out;
// 2 planes for NV12: Y and interleaved UV
uint8_t * data[2] = {Y, Y + Stride * Height}; 
// Strides for Y and UV
// U and V have Stride/2 bytes per line; 
// Thus, we have 2 * Stride/2 bytes per line in UV plane
int linesize[2] = {Stride, Stride}; 
uint8_t * outData[1] = {out}; // RGB have one plane 
int outLinesize[1] = {frameWidth*4}; // RGB32 Stride
sws_scale(context, data, linesize, 0, Height, outData, outLinesize);