美文网首页
使用libyuv 进行 i420 转 BGRA PixelBuf

使用libyuv 进行 i420 转 BGRA PixelBuf

作者: yxibng | 来源:发表于2020-03-04 18:16 被阅读0次

    参考 demo

    typedef struct {
        uint8_t *y_frame;
        uint8_t *u_frame;
        uint8_t *v_frame;
        
        int width;
        int height;
        
        int stride_y;
        int stride_u;
        int stride_v;
    } I420Buffer;
    
    + (CVPixelBufferRef)convertToBGRAPixelBufferWithI420Buffer:(I420Buffer)i420Buffer
    {
        if (!i420Buffer.y_frame) {
            return NULL;
        }
        
        //create rgb buffer
        NSDictionary *att = @{(NSString *)kCVPixelBufferIOSurfacePropertiesKey : @{}};
    
        CVPixelBufferRef bgraBuffer;
        CVPixelBufferCreate(kCFAllocatorDefault,
                            (size_t)i420Buffer.width,
                            (size_t)i420Buffer.height,
                            kCVPixelFormatType_32BGRA,
                            (__bridge CFDictionaryRef _Nullable)att,
                            &bgraBuffer);
        if (!bgraBuffer) {
            //create error
            NSLog(@"%s, create error,  line = %d", __func__, __LINE__);
            return NULL;
        }
    
        CVPixelBufferLockBaseAddress(bgraBuffer, 0);
    
        uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(bgraBuffer);
        int bytesPerRow = (int)CVPixelBufferGetBytesPerRow(bgraBuffer);
    
        int ret =  libyuv::I420ToARGB(i420Buffer.y_frame, i420Buffer.stride_y,
                           i420Buffer.u_frame, i420Buffer.stride_u,
                           i420Buffer.v_frame, i420Buffer.stride_v,
                           baseAddress, bytesPerRow,
                           i420Buffer.width, i420Buffer.height);
    
        assert(ret == 0);
        CVPixelBufferUnlockBaseAddress(bgraBuffer, 0);
        return (CVPixelBufferRef)CFAutorelease(bgraBuffer);
    }
    
    

    相关文章

      网友评论

          本文标题:使用libyuv 进行 i420 转 BGRA PixelBuf

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