10bit YUV(P010)的存储结构和处理

5,364 阅读3分钟

10bit YUV

前面讨论关于 YUV 图像 NV21、YUYV 等格式的处理,都是 8 bit YUV 格式,即每个 Y、U、V 分量分别占用 8 个 bit (一个字节)。

可以类比,10bit YUV 就是每个 Y、U、V 分量分别占用 10 个 bit ,但是实际处理中,我们是以字节为单位进行存储和处理的,所以最终处理的数据是以 2 个字节来存储 10bit 的有效数据。

也就是说 10bit YUV ,每个像素( Y 分量)将占用 16bit 两个字节,但是其中 6 个 bit 是 padding ,补 0 。

P010 结构.png

为什么要了解 10bit YUV ? 最近发现越来越多的视频解码出来是 10bit YUV 的图像,毫无疑问 10bit YUV 会有更好的动态范围,能表现出更丰富的颜色和更多的信息。

随着计算机处理信息的能力越来越厉害,这种能表现更高动态范围的图像存储格式将会逐渐成为主流,但是现在很多算法都不能直接处理 10bit 的 YUV ,都是先将其转换为 8bit YUV ,然后再进行处理,这实际上是丢弃了 10bit YUV 的图像高动态范围优势。

令人遗憾的是在渲染图像时,目前 OpenGL 也无法直接对 10bit YUV 进行渲染,也是需要先转换为 8bit YUV

接下来以一种常见的 10bit YUV (P010) 格式为例,介绍一下 10bit YUV 到 8bit YUV 的转换过程。

P010 最早是微软定义的格式,表示的是 YUV 4:2:0 的采样方式,也就是说 P010 表示的是一类 YUV 格式,它的内存排布方式可能是 NV21、NV12、YU12、YV12 。

微软定义的其他 10bit 和 16bit YUV 格式:

10bit YUV.png 下面我们讨论的 P010 格式的内存排布方式跟 NV21 格式一致,只是每个 Y、U、V 分量分别占用 2 个字节, 10bit 有效位。

(0  ~  3) Y00  Y01  Y02  Y03  
(4  ~  7) Y10  Y11  Y12  Y13  
(8  ~ 11) Y20  Y21  Y22  Y23  
(12 ~ 15) Y30  Y31  Y32  Y33  

(16 ~ 19) V00  U00  V01  U01 
(20 ~ 23) V10  U10  V11  U11

P010 到 8bit YUV 转换

根据上述 10bit YUV 的结构图,P010 转换为 8bit YUV 可以通过向右移位(移 8 位)实现,而 8bit YUV 可以向左移 8 位,刚好低 6 位都是填 0 。

图像定义:

struct NativeImage
{
	int width;
	int height;
	int format;
	uint8_t *ppPlane[3];
};

P010 转换为 8bit YUV(NV21):

static int ConvertP010toNV21(NativeImage* pP010Img, NativeImage* pNV21Img) {
    if(pP010Img == nullptr
    || pNV21Img == nullptr
    || pP010Img->format != IMAGE_FORMAT_P010
    || pNV21Img->format != IMAGE_FORMAT_NV21) return -1;

    int width = pP010Img->width, height = pP010Img->height;
    for (int i = 0; i < height; ++i) {
        uint16_t *pu16YData = (uint16_t *)(pP010Img->ppPlane[0] + pP010Img->width * 2 * i);//每一行的起始位置
        uint8_t  *pu8YData = pNV21Img->ppPlane[0] + pNV21Img->width * i;
        for (int j = 0; j < width; j++, pu8YData++, pu16YData++) {
            *pu8YData = (u_int8_t)(*pu16YData >> 8);   //Y 分量向右移位(移 8 位)
        }
    }

    width /= 2; height /= 2;
    for (int i = 0; i < height; ++i) {
        uint16_t *pu16UVData = (uint16_t *)(pP010Img->ppPlane[1] + pP010Img->width * 2 * i);//每一行的起始位置
        uint8_t  *pu8UVData = pNV21Img->ppPlane[1] + pNV21Img->width * i;
        for (int j = 0; j < width; ++j, pu8UVData+=2, pu16UVData+=2) {
            *pu8UVData = *pu16UVData >> 8;             //V 分量向右移位(移 8 位)
            *(pu8UVData + 1) = *(pu16UVData + 1) >> 8; //U 分量向右移位(移 8 位)
        }
    }
    return 0;
}

8bit YUV(NV21)转换为 P010 :

static int ConvertNV21toP010(NativeImage* pNV21Img, NativeImage* pP010Img) {
    if(pP010Img == nullptr
       || pNV21Img == nullptr
       || pP010Img->format != IMAGE_FORMAT_P010
       || pNV21Img->format != IMAGE_FORMAT_NV21) return -1;

    int width = pP010Img->width, height = pP010Img->height;
    for (int i = 0; i < height; ++i) {
        uint16_t *pu16YData = (uint16_t *)(pP010Img->ppPlane[0] + pP010Img->width * 2 * i);//每一行的起始位置
        uint8_t  *pu8YData = pNV21Img->ppPlane[0] + pNV21Img->width * i;
        for (int j = 0; j < width; j++, pu8YData++, pu16YData++) {
            *pu16YData = (u_int16_t)*pu8YData << 8;//Y 分量向左移位(移 8 位)
        }
    }

    width /= 2; height /= 2;
    for (int i = 0; i < height; ++i) {
        uint16_t *pu16UVData = (uint16_t *)(pP010Img->ppPlane[1] + pP010Img->width * 2 * i);//每一行的起始位置
        uint8_t  *pu8UVData = pNV21Img->ppPlane[1] + pNV21Img->width * i;
        for (int j = 0; j < width; ++j, pu8UVData+=2, pu16UVData+=2) {
            *pu16UVData = (u_int16_t)*pu8UVData << 8;   //V 分量向左移位(移 8 位)
            *(pu16UVData + 1) = (u_int16_t)*(pu8UVData + 1) << 8; //U 分量向左移位(移 8 位)
        }
    }

    return 0;
}

关于 P010 和 NV21 之间格式转换测试,可以参考项目 github.com/githubhaoha… , sample/YUVP010Example.h 源码。

class YUVP010Example {
public:
    static void YUVP010Test() {
        NativeImage p010Img, nv21Img;
        p010Img.width = 4406;
        p010Img.height = 3108;
        p010Img.format = IMAGE_FORMAT_P010;

        nv21Img = p010Img;
        nv21Img.format = IMAGE_FORMAT_NV21;

        //申请内存
        NativeImageUtil::AllocNativeImage(&p010Img);
        NativeImageUtil::AllocNativeImage(&nv21Img);

        //加载 NV21 图片
        char filePath[512] = {0};
        sprintf(filePath, "%s/yuv/%s", DEFAULT_OGL_ASSETS_DIR, DEFAULT_YUV_IMAGE_NAME);
        NativeImageUtil::LoadNativeImage(&nv21Img, filePath);

        //NV21 转换为 P010
        {
            BEGIN_TIME("NativeImageUtil::ConvertNV21toP010")
            NativeImageUtil::ConvertNV21toP010(&nv21Img, &p010Img);
            END_TIME("NativeImageUtil::ConvertNV21toP010")
        }

        //保存 P010 图像到手机
        NativeImageUtil::DumpNativeImage(&p010Img, DEFAULT_OGL_ASSETS_DIR, "IMAGE_P010");

        //P010 转换为 NV21
        {
            BEGIN_TIME("NativeImageUtil::ConvertP010toNV21")
            NativeImageUtil::ConvertP010toNV21(&p010Img, &nv21Img);
            END_TIME("NativeImageUtil::ConvertP010toNV21")
        }

        //多线程实现 P010 转换为 NV21
        {
            BEGIN_TIME("NativeImageUtil::ConvertP010toNV21 MultiThread")
            std::thread *pThreads[3] = {nullptr};
            pThreads[0] = new std::thread(NativeImageUtil::ConvertP010PlaneTo8Bit, (u_int16_t*)p010Img.ppPlane[0], nv21Img.ppPlane[0], nv21Img.width, nv21Img.height / 2);
            pThreads[1] = new std::thread(NativeImageUtil::ConvertP010PlaneTo8Bit, (u_int16_t*)p010Img.ppPlane[0] + p010Img.height * p010Img.width / 2, nv21Img.ppPlane[0] + nv21Img.height * nv21Img.width / 2, nv21Img.width, nv21Img.height / 2);
            pThreads[2] = new std::thread(NativeImageUtil::ConvertP010PlaneTo8Bit, (u_int16_t*)p010Img.ppPlane[1], nv21Img.ppPlane[1], nv21Img.width, nv21Img.height / 2);
            for (int i = 0; i < 3; ++i) {
                pThreads[i]->join();
            }

            for (int i = 0; i < 3; ++i) {
                delete pThreads[i];
            }
            END_TIME("NativeImageUtil::ConvertP010toNV21 MultiThread")

        }

        NativeImageUtil::DumpNativeImage(&nv21Img, DEFAULT_OGL_ASSETS_DIR, "IMAGE_NV21");

        //释放内存
        NativeImageUtil::FreeNativeImage(&p010Img);
        NativeImageUtil::FreeNativeImage(&nv21Img);
    }
};

代码中通过多线程实现格式转换,并与单线程转换的性能进行对比,多线程转换性能提升明显:

youdu图片20211019164418.png

参考: docs.microsoft.com/en-us/windo…