颜色空间转换CSConvert:NV12toYVYU或NV12toYUYV

  1. 1、NV12\YVYU\YUYV格式说明
  2. 2、NV12转YVYU
  3. 3、NV12转YUYV
  4. THE END!

1、NV12\YVYU\YUYV格式说明

NV12格式是YUV420sp,Intel原生态支持NV12格式;
YVYU格式属于YUV422,按照Y,V,Y,U一个平面排列。
YUYV格式与YVYU格式类似,都属于YUV422,按照Y,U,Y,V一个平面排列。

2、NV12转YVYU

下面实现NV12格式转换成YVYU或者YUYV功能:

NV12toYVYU.c

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

int main(int argc, char** argv)
{
    int width, height, frame_size;
    FILE *fin, *fou;
    char *y, *uv, *y2, *temp_y, *temp_uv, *temp_y2;
    int i, j, frame_num = 0;

    printf("\nUsage: NV12toYVYU.exe infile outfile width height\n\n");
    if (argc != 5)
    {
        return -1;
    }
    fin = fopen(argv[1], "rb");
    if (fin == NULL)
    {
        printf("ERROR: open %s fail\n", argv[1]);
        return -1;
    }
    fou = fopen(argv[2], "wb");
    if (fou == NULL)
    {
        printf("RRROR: open %s fail\n", argv[2]);
        return -1;
    }

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

    y2 = (char *)malloc(frame_size * 2);//++YVYU
    if (y2 == NULL)
    {
        printf("ERROR: malloc y2 fail\n");
        return -1;
    }

    while (!feof(fin))
    {
        int n = 0;
        n = fread(y, 1, width*height, fin);
        n += fread(uv, 1, width*height >> 1, fin);
        if (n != frame_size *3/2)
        {
            break;
        }

        temp_y = y;
        temp_uv = uv;
        temp_y2 = y2;

        for (j = 0; j < height;j++)
        {
            for (i = 0; i < width / 2;i++)
            {
                temp_y2[0] = temp_y[2 * i];//y
                temp_y2[1] = temp_uv[2 * i + 1];//v
                temp_y2[2] = temp_y[2 * i + 1];//y
                temp_y2[3] = temp_uv[2 * i];//u
                temp_y2 += 4;
            }
            temp_y += width;
            if (j & 1)
            {
                temp_uv += width;
            }
        }
        fwrite(y2, 1, frame_size * 2, fou);
        printf("%dth frame ok!!\n", frame_num);
        frame_num++;
    }
    printf("NV12 to YVYU successfully!!,total frames: %d\n", frame_num);

    free(y);
    y = NULL;
    free(y2);
    y2 = NULL;
    fclose(fin);
    fclose(fou);

    return 0;
}

3、NV12转YUYV

NV12toYUYV.c

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

int main(int argc, char** argv)
{
    int width, height, frame_size;
    FILE *fin, *fou;
    char *y, *uv, *y2, *temp_y, *temp_uv, *temp_y2;
    int i, j, frame_num = 0;

    printf("\nUsage: NV12toYVYU.exe infile outfile width height\n\n");
    if (argc != 5)
    {
        return -1;
    }
    fin = fopen(argv[1], "rb");
    if (fin == NULL)
    {
        printf("ERROR: open %s fail\n", argv[1]);
        return -1;
    }
    fou = fopen(argv[2], "wb");
    if (fou == NULL)
    {
        printf("RRROR: open %s fail\n", argv[2]);
        return -1;
    }

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

    y2 = (char *)malloc(frame_size * 2);//++YVYU
    if (y2 == NULL)
    {
        printf("ERROR: malloc y2 fail\n");
        return -1;
    }

    while (!feof(fin))
    {
        int n = 0;
        n = fread(y, 1, width*height, fin);
        n += fread(uv, 1, width*height >> 1, fin);
        if (n != frame_size *3/2)
        {
            break;
        }

        temp_y = y;
        temp_uv = uv;
        temp_y2 = y2;

        for (j = 0; j < height;j++)
        {
            for (i = 0; i < width / 2;i++)
            {
                temp_y2[0] = temp_y[2 * i];//y
                temp_y2[1] = temp_uv[2 * i];//u
                temp_y2[2] = temp_y[2 * i + 1];//y
                temp_y2[3] = temp_uv[2 * i + 1];//v
                temp_y2 += 4;
            }
            temp_y += width;
            if (j & 1)
            {
                temp_uv += width;
            }
        }
        fwrite(y2, 1, frame_size * 2, fou);
        printf("%dth frame ok!!\n", frame_num);
        frame_num++;
    }
    printf("NV12 to YVYU successfully!!,total frames: %d\n", frame_num);

    free(y);
    y = NULL;
    free(y2);
    y2 = NULL;
    fclose(fin);
    fclose(fou);

    return 0;
}

THE END!


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

文章标题:颜色空间转换CSConvert:NV12toYVYU或NV12toYUYV

字数:666

本文作者:Soaring Lee

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

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

原始链接:https://soaringleefighting.github.io/2019/07/13/【Codecs系列】颜色空间转换CSConvert:NV12toYVYU或NV12toYUYV/

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

×

喜欢就点赞,疼爱就打赏

相册