title: Rotate CMSampleBuffer/CVPixelBuffer
body: <p>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 <a href="https://stackoverflow.com/questions/4528879/how-to-directly-rotate-cvimagebuffer-image-in-ios-4-without-converting-to-uiimag">this one</a> or <a href="https://stackoverflow.com/questions/22745824/rotating-video-without-rotating-avcaptureconnection-and-in-the-middle-of-avasset">this one</a>), 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:</p>
  
  <pre><code>- (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(&amp;sourceBuffer, &amp;destinationBuffer, rotation, bgColor, kvImageNoFlags); // Crash!
  
  CVPixelBufferRef rotatedBuffer = NULL;
  CVPixelBufferCreateWithBytes(kCFAllocatorDefault, destinationBuffer.width, destinationBuffer.height, pixelFormat, destinationBuffer.data, destinationBuffer.rowBytes, freePixelBufferData, NULL, NULL, &amp;rotatedBuffer);
  
  CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
  
  return rotatedBuffer; 
  }
  
  void freePixelBufferData(void *releaseRefCon, const void *baseAddress) {
  free((void *)baseAddress);
  }
  </code></pre>