I have an FFMPEG AVFrame
in YUVJ420P
and I want to convert it to a CVPixelBufferRef
with CVPixelBufferCreateWithBytes
. The reason I want to do this is to use AVFoundation to show/encode the frames.
I selected kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
and tried converting it since the AVFrame has the data in three planes
Y480
Cb240
Cr240
. And according to what I've researched this matches the selected kCVPixelFormatType
. By being biplanar I need to convert it into a buffer that contains Y480
and CbCr480
Interleaved.
I tried to create a buffer with 2 planes:
frame->data[0]
on the first plane,frame->data[1]
andframe->data[2]
interleaved on the second plane.
However, I'm getting return error -6661 (invalid a)
from CVPixelBufferCreateWithBytes
:
"Invalid function parameter. For example, out of range or the wrong type."
I don't have expertise on image processing at all, so any pointers to documentation that can get me started in the right approach to this problem are appreciated. My C skills aren't top of the line either so maybe I'm making a basic mistake here.
uint8_t **buffer = malloc(2*sizeof(int *));
buffer[0] = frame->data[0];
buffer[1] = malloc(frame->linesize[0]*sizeof(int));
for(int i = 0; i<frame->linesize[0]; i++){
if(i%2){
buffer[1][i]=frame->data[1][i/2];
}else{
buffer[1][i]=frame->data[2][i/2];
}
}
int ret = CVPixelBufferCreateWithBytes(NULL, frame->width, frame->height, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange, buffer, frame->linesize[0], NULL, 0, NULL, cvPixelBufferSample)
The frame is the AVFrame
with the rawData from FFMPEG Decoding.