1

我尝试在不传输到 RGBA 的情况下旋转 CoreVideo '420f' 图像。传入的 CMSampleBuffer Y 平面 bytesPerRow 为 width + 32。

这意味着 Y 平面的行大小是 8bit * width + sizeof(CVPlanarComponentInfo)。

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

我试过了

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;
}
4

1 回答 1

0

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

实际上有问题的代码工作正常。CVPixelBufferCreate() 创建没有额外 32 字节的缓冲区。vImageRotate90_Planar8() 支持两种格式,有 32 字节和没有 32 字节。

于 2018-04-20T02:27:02.077 回答