6

I am currently attempting to change the orientation of a CMSampleBuffer by first converting it to a CVPixelBuffer and then using vImageRotate90_ARGB8888 to convert the buffer. The problem with my code is that when vImageRotate90_ARGB8888 executes, it crashes immediately. I know there are answers (like this one or this one), but all of these solutions fail to work in my case, and I really cannot find any type of error, or think of anything that would cause this behavior. My current code is below:

- (CVPixelBufferRef)rotateBuffer:(CMSampleBufferRef)sampleBuffer {
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(pixelBuffer, 0);

size_t bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer);
size_t width = CVPixelBufferGetWidth(pixelBuffer);
size_t height = CVPixelBufferGetHeight(pixelBuffer);
size_t currSize = bytesPerRow * height * sizeof(unsigned char);
size_t bytesPerRowOut = 4 * height * sizeof(unsigned char);

OSType pixelFormat = CVPixelBufferGetPixelFormatType(pixelBuffer);

void *baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer);
unsigned char *outPixelData = (unsigned char *)malloc(currSize);

vImage_Buffer sourceBuffer = {baseAddress, height, width, bytesPerRow};
vImage_Buffer destinationBuffer = {outPixelData, width, height, bytesPerRowOut};

uint8_t rotation = kRotate90DegreesClockwise;

Pixel_8888 bgColor = {0, 0, 0, 0};
vImageRotate90_ARGB8888(&sourceBuffer, &destinationBuffer, rotation, bgColor, kvImageNoFlags); // Crash!

CVPixelBufferRef rotatedBuffer = NULL;
CVPixelBufferCreateWithBytes(kCFAllocatorDefault, destinationBuffer.width, destinationBuffer.height, pixelFormat, destinationBuffer.data, destinationBuffer.rowBytes, freePixelBufferData, NULL, NULL, &rotatedBuffer);

CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);

return rotatedBuffer; 
}

void freePixelBufferData(void *releaseRefCon, const void *baseAddress) {
free((void *)baseAddress);
}
anthonya1999
  • 253
  • 3
  • 13
  • 1
    `vImageRotate90_ARGB8888` requires that both the source and destination buffers are ARGB8888. Is your source buffer ARGB8888? – Flex Monkey Mar 16 '18 at 15:45
  • @SimonGladman In all honesty, I am not 100% sure. I'm getting the buffers from ReplayKit, so I assume it is of type kCVPixelFormatType_32BGRA. Does it need to be kCVPixelFormatType_32ARGB or is ARGB8888 a special type? – anthonya1999 Mar 16 '18 at 18:11

0 Answers0