颜色空间转换CSConvert:YUV420P(I420)转NV12

  1. 1、颜色空间I420和NV12说明
  2. 2、I420转NV12实现
  3. THE END!

1、颜色空间I420和NV12说明

I420即常用的YUV420P格式,YUV存储按照Y\U\V三个平面顺序存放。
NV12是YUV420sp的格式,Y分量占用一个平面,UV分量交错存放占用一个平面。

2、I420转NV12实现

I420toNV12.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char** argv)
{
    int width, height;
    int frame_size;
    FILE *fin, *fou;
    char *y, *uv, *u, *v;
    int i;
    char outname[512] = { 0, };
    printf("\nUsage:I420toNV12.exe input.yuv width height\n\n");
    if (argc != 4)
    {
        return -1;
    }
    fin = fopen(argv[1], "rb");
    if (fin == NULL)
    {
        printf("error:open %s fail\n", argv[1]);
        return -1;
    }

    width = atoi(argv[2]);
    height = atoi(argv[3]);
    frame_size = width*height * 3 / 2;//++YUV420
    y = (char *)malloc(frame_size);
    if (y == NULL)
    {
        printf("malloc y fail\n");
        return -1;
    }
    uv = y + width*height;

    u = (char *)malloc(width*height >> 2);
    v = (char *)malloc(width*height >> 2);

    if (u == NULL || v == NULL)
    {
        printf("malloc u or v fail\n");
        return -1;
    }
    memset(outname, 0, sizeof(outname));
    sprintf_s(outname, "%s_NV12.yuv", argv[1]);
    fou = fopen(outname, "wb");
    if (fou == NULL)
    {
        printf("error: open %s fail\n", outname);
        return -1;
    }

    while (!feof(fin))
    {
        int n = 0;
        n = fread(y, 1, width*height, fin);
        n += fread(u, 1, width*height >> 2, fin);
        n += fread(v, 1, width*height >> 2, fin);

        if (n != frame_size)
        {
            break;
        }
        for (i = 0; i < height*width >> 2; i++)
        {
             uv[2 * i + 0] = u[i];
             uv[2 * i + 1] = v[i];
        }
        fwrite(y, 1, height*width, fou);
        fwrite(uv, 1, height*width >> 1, fou);
    }
    printf("I420 to NV12 successfully!");
    free(y);
    free(u);
    free(v);
    fclose(fin);
    fclose(fou);

    return 0;
}

THE END!


本博文只能阅读,谢绝转载,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 2963033731@qq.com

文章标题:颜色空间转换CSConvert:YUV420P(I420)转NV12

字数:356

本文作者:Soaring Lee

发布时间:2019-07-13, 21:23:47

最后更新:2021-06-14, 12:13:44

原始链接:https://soaringleefighting.github.io/2019/07/13/【Codecs系列】颜色空间转换CSConvert:YUV420P(I420)转NV12/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

×

喜欢就点赞,疼爱就打赏

相册