yuv转bgr速度优化

一、转换代码

int yuv2bgr(unsigned char * yuv_img, unsigned char *rgb_img,int width, int height)
{

    unsigned char * ydata = yuv_img;
    unsigned char *uvdata = ydata + width * height;
    int indexY, indexU, indexV;
    unsigned char Y, U, V;
    int B, G, R;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            indexY = i * width + j;
            Y = ydata[indexY];

            if (j % 2 == 0)
            {
                indexU = i / 2 * width + j;
                indexV = indexU + 1;
                U = uvdata[indexU];
                V = uvdata[indexV];
            }
            else
            {
                indexV = i / 2 * width + j;
                indexU = indexV - 1;
                U = uvdata[indexU];
                V = uvdata[indexV];
            }
            //nv21
            R = (unsigned char)(Y + 1.4075 * (U - 128));
            G = (unsigned char)(Y - 0.3455 * (V - 128) - 0.7169 * (U - 128));
            B = (unsigned char)(Y + 1.779 * (V - 128));
            //nv12
            //R = (unsigned char)(Y + 1.4075 * (V - 128));
            //G = (unsigned char)(Y - 0.3455 * (U - 128) - 0.7169 * (V - 128));
            //B = (unsigned char)(Y + 1.779 * (U - 128));
            
            rgb_img[indexY * 3 + 0] = clamp_g(B, 0, 255);
            rgb_img[indexY * 3 + 1] = clamp_g(G, 0, 255);
            rgb_img[indexY * 3 + 2] = clamp_g(R, 0, 255);

        }
    }
    return 0;
}


二、优化代码

int yuv2bgr2(unsigned char * yuv_img, unsigned char *rgb_img, int width, int height)
{

    unsigned char * ydata = yuv_img;
    unsigned char *uvdata = ydata + width * height;
    int indexY, indexU, indexV;
    unsigned char Y, U, V;
    int B, G, R;

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j+=2)
        {
            indexY = i * width + j;
            Y = ydata[indexY];

            indexU = i / 2 * width + j;
            indexV = indexU + 1;
            U = uvdata[indexU];
            V = uvdata[indexV];
            /*first*/
            //nv21
            R = (int)(Y * 4096 + 5765 * (U - 128));
            G = (int)(Y * 4096 - 1415 * (V - 128) - 2936 * (U - 128));
            B = (int)(Y * 4096 + 7286 * (V - 128));
            //nv12
            //R = (unsigned char)(Y + 1.4075 * (V - 128));
            //G = (unsigned char)(Y - 0.3455 * (U - 128) - 0.7169 * (V - 128));
            //B = (unsigned char)(Y + 1.779 * (U - 128));

            rgb_img[indexY * 3 + 0] = B >> 12;// clamp_g(B >> 12, 0, 255);
            rgb_img[indexY * 3 + 1] = G >> 12;// clamp_g(G >> 12, 0, 255);
            rgb_img[indexY * 3 + 2] = R >> 12;//clamp_g(R >> 12, 0, 255);
            /*second*/
            indexY = indexY + 1;
            Y = ydata[indexY];
            //nv21
            R = (int)(Y * 4096 + 5765 * (U - 128));
            G = (int)(Y * 4096 - 1415 * (V - 128) - 2936 * (U - 128));
            B = (int)(Y * 4096 + 7286 * (V - 128));
            rgb_img[indexY * 3 + 0] = B >> 12;// clamp_g(B >> 12, 0, 255);
            rgb_img[indexY * 3 + 1] = G >> 12;// clamp_g(G >> 12, 0, 255);
            rgb_img[indexY * 3 + 2] = R >> 12;//clamp_g(R >> 12, 0, 255);
        }
    }
    return 0;
}

三、速度对比

nv21转bgr的平均时间(输入大小720p):

AveTime:13.7835ms

改进代码:

改进后平均时间:AveTime:5.52088ms

速度提升60%,结果完全一致。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值