yuv422转420

#include <stdio.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

//write u or v
int32_t wr_u_or_v(int32_t fd, uint8_t *src, int32_t width, int32_t height)
{
    int32_t r, i, j;

    for (i = 0; i < height; i++)
    {
        //去掉奇数行的U or V
        if (i & 0x1)
        {
            src += width;
            continue;
        }

        for (j = 0; j < width/4; j++)
        {
            r = write(fd, src, 1);
            if (r != 1)
            {
                printf("i = %d, j = %d, r = %d, errno = %d\n", 
                        i, j, r, errno);
                return r;
            }
            src += 4;
        }
    }

    return 0;
}

int32_t main(void)
{
    uint8_t *y, *u, *v;
    uint8_t *srcBuf;
    int32_t fdIn, fdOut;
    int32_t width = 1920, height = 540;
    uint32_t bufSize = width * height; //byte
    int32_t r;


    srcBuf = (uint8_t *)calloc(1, bufSize);

    fdIn = open("1920x540_yuyv.raw", O_RDONLY);
    assert(fdIn >= 0);
    fdOut = open("out.raw", O_RDWR|O_CREAT|O_TRUNC, 0666);
    assert(fdOut >= 0);

    r = read(fdIn, srcBuf, bufSize);
    assert(r == bufSize);

    //YUYVYUYVYUYV
    y = srcBuf;
    u = srcBuf + 1;
    v = srcBuf + 3;

    //取所有Y分量
    for (; y < srcBuf+bufSize; y += 2)
    {
        //一个Y占1byte,隔一个是一个Y分量
        r = write(fdOut, y, 1);
        assert(r == 1);
    }
    printf("write Y complit\n");

    //取一半U分量
    r = wr_u_or_v(fdOut, u, width, height);
    assert(r == 0);
    printf("write U complit\n");

    //取一半V分量
    r = wr_u_or_v(fdOut, v, width, height);
    assert(r == 0);
    printf("write V complit\n");

    close(fdIn);
    close(fdOut);

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值