YUV转rgb

作者: 陆号 | 来源:发表于2018-07-11 19:19 被阅读16次

图文详解YUV420数据格式

-(void)YUVPixelBuffer2RGB:(CMSampleBufferRef)sampleBuffer
{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer, 0);
    
    int width = (int) CVPixelBufferGetWidth(imageBuffer);
    int height = (int) CVPixelBufferGetHeight(imageBuffer);
    
    uint8_t *yBuffer =(uint8_t *) CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);;
    size_t yPitch = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);
    uint8_t *cbCrBuffer =(uint8_t *) CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 1);
    size_t cbCrPitch = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 1);
    
    if (yBuffer == NULL)
    {
        NSLog(@"yBuffer is NULL");
        return ;
    }
    int bytesPerPixel = 4;
    
    for(int y = 0; y < height; y++) {// height
        uint8_t *rgbBufferLine = &rgbBuffer[y * width * bytesPerPixel];
        uint8_t *yBufferLine = &yBuffer[y* yPitch];
        uint8_t *cbCrBufferLine = &cbCrBuffer[(y >> 1) * cbCrPitch];
        
        for(int x = 0; x < width; x++) {//width
            int16_t y = yBufferLine[x];
            int16_t cb = cbCrBufferLine[x & ~1] - 128;
            int16_t cr = cbCrBufferLine[x | 1] - 128;
            
            uint8_t *rgbOutput = &rgbBufferLine[x*bytesPerPixel];
            
            int16_t r = (int16_t)roundf( y + cr *  1.4 );
            int16_t g = (int16_t)roundf( y + cb * -0.343 + cr * -0.711 );
            int16_t b = (int16_t)roundf( y + cb *  1.765);
            
            rgbOutput[0] = 0xff;
            rgbOutput[1] = clamp(r);
            rgbOutput[2] = clamp(g);
            rgbOutput[3] = clamp(b);
        }
    }
    CVPixelBufferUnlockBaseAddress(imageBuffer,0);
}

相关文章

  • YUV420转换RGB公式

    YUV420转换RGB YUV420转换RGB公式

  • 转码的四个案例

    一,swcale实现rgb24转yuv420p 二,swcale实现YUV转RGB

  • YUV转rgb

    图文详解YUV420数据格式

  • 图像处理学习资料

    RGB、YUV和HSV颜色空间模型 RGB立方体模型RGB YUV:其中“Y”表示明亮度(Luminance或Lu...

  • FFmpeg - 播放YUV,视频帧格式转换

    播放YUV 定时读取YUV的视频帧 将YUV转换为RGB数据 用RGB数据生成CGimage 在view上绘制CG...

  • YUV和RGB

    色彩空间 我们经常用到的色彩空间主要有RGB、YUV, CMYK, HSB, HSL等等,其中YUV和RGB是视讯...

  • RGB、YUV

    RGB和YUV是什么? RGB和YUV是色彩空间模型,还有诸如HSV不是存储格式,如PEG、BMP、JPEG、GI...

  • RGB和YUV简单学习记录

    RGB和YUV是一种颜色编码格式。这里简单介绍一下RGB、YUV和HSV。 出处:一文读懂 YUV 的采样与格式h...

  • YUV和RGB

    YUV 的采样与格式 YUV 是⼀种颜⾊编码⽅法,和它等同的还有 RGB 颜⾊编码⽅法。 RGB 颜⾊编码 R G...

  • FFmpeg解码帧数据上传至OpenGL ES及GPU实现YUV

    本文档描述了经FFmpeg解码得到的多个YUV格式或RGB格式数据上传至OpenGL ES及YUV转换RGB的办法...

网友评论

      本文标题:YUV转rgb

      本文链接:https://www.haomeiwen.com/subject/wjkkpftx.html