CVPixelBufferCreate不关心平面格式

时间:2018-04-19 22:29:25

标签: ios replaykit

我尝试旋转CoreVideo' 420f'图像无需转移到RGBA。 传入的CMSampleBuffer Y平面bytesPerRow是宽度+32。

这意味着Y平面行大小为8位*宽度+ sizeof(CVPlanarComponentInfo)。

但如果我调用CVPixelBufferCreate(,,,' 420f' ,,),BytesPerRow == width。 CVPixelBufferCreate()不关心平面格式,也没有添加32字节。

我试过

vImage_Buffer myYBuffer = {buf, height, width, bytePerRow};

但是bitsPerPixel没有参数。我不能用于UVBuffer。

我试过

vImageBuffer_Init(buf, height, width, bitPerPixel, flag);

但是bytesPerRow没有参数。

我想知道如何使用' 420f'创建vImageBuffer或CVPixelBuffer。平面格式。

这是用于轮换的施工代码

NS_INLINE void dumpData(NSString* tag, unsigned char* p, size_t w) {
    NSMutableString* str = [tag mutableCopy];
    for(int i=0;i<w+100;++i) {
        [str appendString:[NSString stringWithFormat:@"%02x ", *(p + i)]];
    }
    NSLog(@"%@", str);
}
- (CVPixelBufferRef) RotateBuffer:(CMSampleBufferRef)sampleBuffer withConstant:(uint8_t)rotationConstant
{
    vImage_Error err = kvImageNoError;
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer, 0);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    size_t outHeight = width;
    size_t outWidth = height;

    assert(CVPixelBufferGetPixelFormatType(imageBuffer) == kCVPixelFormatType_420YpCbCr8BiPlanarFullRange);
    assert(CVPixelBufferGetPlaneCount(imageBuffer) == 2);
    NSLog(@"YBuffer %ld %ld %ld",   CVPixelBufferGetWidthOfPlane(imageBuffer, 0), CVPixelBufferGetHeightOfPlane(imageBuffer, 0),
        CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0)); // BytesPerRow = width + 32
    dumpData(@"Base=", CVPixelBufferGetBaseAddress(imageBuffer), width);
    dumpData(@"Plane0=", CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0), width);

    CVPixelBufferRef rotatedBuffer = NULL;
    CVReturn ret = CVPixelBufferCreate(kCFAllocatorDefault, outWidth, outHeight, kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, NULL, &rotatedBuffer);
    NSLog(@"CVPixelBufferCreate err=%d", ret);
    CVPixelBufferLockBaseAddress(rotatedBuffer, 0);
    NSLog(@"CVPixelBufferCreate init %ld %ld %ld p=%p",   CVPixelBufferGetWidthOfPlane(rotatedBuffer, 0), CVPixelBufferGetHeightOfPlane(rotatedBuffer, 0),
        CVPixelBufferGetBytesPerRowOfPlane(rotatedBuffer, 0), CVPixelBufferGetBaseAddressOfPlane(rotatedBuffer, 0));
        // BytesPerRow = width ??? should be width + 32

    // rotate Y plane
    vImage_Buffer originalYBuffer = { CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0), CVPixelBufferGetHeightOfPlane(imageBuffer, 0),
        CVPixelBufferGetWidthOfPlane(imageBuffer, 0), CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0) };
    vImage_Buffer rotatedYBuffer = { CVPixelBufferGetBaseAddressOfPlane(rotatedBuffer, 0), CVPixelBufferGetHeightOfPlane(rotatedBuffer, 0),
        CVPixelBufferGetWidthOfPlane(rotatedBuffer, 0), CVPixelBufferGetBytesPerRowOfPlane(rotatedBuffer, 0) };
    err = vImageRotate90_Planar8(&originalYBuffer, &rotatedYBuffer, 1, 0.0, kvImageNoFlags);
    NSLog(@"rotatedYBuffer rotated %ld %ld %ld p=%p", rotatedYBuffer.width, rotatedYBuffer.height, rotatedYBuffer.rowBytes, rotatedYBuffer.data);
    NSLog(@"RotateY err=%ld", err);
    dumpData(@"Rotated Plane0=", rotatedYBuffer.data, outWidth);

    // rotate UV plane
    vImage_Buffer originalUVBuffer = { CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 1), CVPixelBufferGetHeightOfPlane(imageBuffer, 1),
        CVPixelBufferGetWidthOfPlane(imageBuffer, 1), CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 1) };
    vImage_Buffer rotatedUVBuffer = { CVPixelBufferGetBaseAddressOfPlane(rotatedBuffer, 1), CVPixelBufferGetHeightOfPlane(rotatedBuffer, 1),
    CVPixelBufferGetWidthOfPlane(rotatedBuffer, 1), CVPixelBufferGetBytesPerRowOfPlane(rotatedBuffer, 1) };
    err = vImageRotate90_Planar16U(&originalUVBuffer, &rotatedUVBuffer, 1, 0.0, kvImageNoFlags);
    NSLog(@"RotateUV err=%ld", err);
    dumpData(@"Rotated Plane1=", rotatedUVBuffer.data, outWidth);

    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
    CVPixelBufferUnlockBaseAddress(rotatedBuffer, 0);

    return rotatedBuffer;
}

1 个答案:

答案 0 :(得分:0)

我发现vImageBuffer BytesPerRow额外的32字节是可选的。一些Apple API在每行上添加32个字节,某些API不会添加。

实际上质疑的代码工作正常。 CVPixelBufferCreate()创建缓冲区而不需要额外的32字节。 vImageRotate90_Planar8()支持两种格式,32字节且没有32字节。