Я столкнулся с той же проблемой. Я пытаюсь визуализировать видеокадр в формате YV12. По какой-то причине я вижу синие и зеленые пятна на кадре: Это визуализированное изображение
Кто-нибудь может помочь мне исправить это. Я уже 5 дней потратил на это
Вот код:
+ (CVPixelBufferRef)yuvPixelBufferWithData:(NSData *)dataFrame
width:(size_t)w
heigth:(size_t)h
{
unsigned char* buffer = (unsigned char*) dataFrame.bytes;
CVPixelBufferRef getCroppedPixelBuffer = [self copyDataFromBuffer:buffer
toYUVPixelBufferWithWidth:w Height:h];
return getCroppedPixelBuffer;
}
+ (CVPixelBufferRef) copyDataFromBuffer:(const unsigned char*)buffer toYUVPixelBufferWithWidth:(size_t)w Height:(size_t)h
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
kCVPixelBufferCGImageCompatibilityKey,
[NSNumber numberWithBool:YES],
kCVPixelBufferCGBitmapContextCompatibilityKey,
[NSNumber numberWithBool:YES],
kCVPixelBufferMetalCompatibilityKey,
nil];
CVPixelBufferRef pixelBuffer;
CVPixelBufferCreate(NULL, w, h,
kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, (__bridge CFDictionaryRef).
(options), &pixelBuffer);
size_t count = CVPixelBufferGetPlaneCount(pixelBuffer);
NSLog(@"PlaneCount = %zu", count); // 2
CVPixelBufferLockBaseAddress(pixelBuffer, 0);
size_t d = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0);
const unsigned char* src = buffer;
unsigned char* dst = (unsigned char
*)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0);
for (unsigned int rIdx = 0; rIdx < h; ++rIdx, dst += d, src += w) {
memcpy(dst, src, w);
}
d = CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1);
dst = (unsigned char *)CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1);
h = h >> 1;
for (unsigned int rIdx = 0; rIdx < h; ++rIdx, dst += d, src += w) {
memcpy(dst, src, w);
}
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
return pixelBuffer;
}