复制CVPixelBufferRef

时间:2016-07-15 21:54:17

标签: ios multithreading core-video

copyPixelBufferNow中的代码太长了。 : - (

@property (nonatomic,assign)CVPixelBufferRef pixelBufferNowRef;

- (CVPixelBufferRef)copyPixelBufferNow {
    if (_pixelBufferNowRef == NULL) {
        return nil;
    }

    CVPixelBufferRef pixelBufferOut = NULL;
    CVReturn ret = kCVReturnError;
    size_t height = CVPixelBufferGetHeight(_pixelBufferNowRef);
    size_t width = CVPixelBufferGetWidth(_pixelBufferNowRef);
    size_t bytersPerRow = CVPixelBufferGetBytesPerRow(_pixelBufferNowRef);
    CFDictionaryRef attrs = NULL;
    const void *keys[] = { kCVPixelBufferPixelFormatTypeKey };
    //      kCVPixelFormatType_420YpCbCr8Planar is YUV420
    //      kCVPixelFormatType_420YpCbCr8BiPlanarFullRange is NV12
    uint32_t v = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
    const void *values[] = { CFNumberCreate(NULL, kCFNumberSInt32Type, &v) };
    attrs = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);

    ret = CVPixelBufferCreate(NULL,
                              width,
                              height,
                              CVPixelBufferGetPixelFormatType(_pixelBufferNowRef),
                              attrs,
                              &pixelBufferOut);
    CVPixelBufferLockBaseAddress(_pixelBufferNowRef, kCVPixelBufferLock_ReadOnly);
    CVPixelBufferLockBaseAddress(pixelBufferOut, kCVPixelBufferLock_ReadOnly);
    CFRelease(attrs);
    if (ret == kCVReturnSuccess) {
        memcpy(CVPixelBufferGetBaseAddress(pixelBufferOut), CVPixelBufferGetBaseAddress(_pixelBufferNowRef), height * bytersPerRow);
    } else {
        printf("why copy pixlbuffer error %d",ret);
    }
    CVPixelBufferUnlockBaseAddress(_pixelBufferNowRef, kCVPixelBufferLock_ReadOnly);
    CVPixelBufferUnlockBaseAddress(pixelBufferOut, kCVPixelBufferLock_ReadOnly);

    return pixelBufferOut;
}

- (void)setPixelBufferNowRef:(CVPixelBufferRef)sender {
    if (_pixelBufferNowRef != sender) {
        CVPixelBufferRelease(_pixelBufferNowRef);
        _pixelBufferNowRef = sender;
        CVPixelBufferRetain(_pixelBufferNowRef);
    }
}

我有一个属性pixelBufferNowRef

如何防止在copyPixelBufferNow中修改它?

0 个答案:

没有答案