我在 YUVJ420P
中有一个 FFMPEG AVFrame
,我想用 CVPixelBufferCreateWithBytes
将它转换为 CVPixelBufferRef
。我想这样做的原因是使用 AVFoundation 来显示/编码帧。
我选择了 kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange
并尝试转换它,因为 AVFrame 具有三个平面的数据
Y480
Cb240
Cr240
。根据我的研究,这与所选的 kCVPixelFormatType
相匹配。由于是双面的,我需要将其转换为包含 Y480
和 CbCr480
Interleaved 的缓冲区。
我尝试创建一个包含 2 个平面的缓冲区:
frame->data[0]
在第一个平面上,frame->data[1]
和frame->data[2]
在第二个平面上交错。
但是,我从 CVPixelBufferCreateWithBytes
收到返回错误 -6661 (invalid a)
:
"Invalid function parameter. For example, out of range or the wrong type."
我完全没有图像处理方面的专业知识,因此非常感谢任何可以帮助我开始以正确方法解决此问题的文档。我的 C 技能也不是一流的,所以我可能在这里犯了一个基本错误。
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)
帧是 AVFrame
,带有来自 FFMPEG 解码的原始数据。
最佳答案
My C skills aren't top of the line either so maybe im making a basic mistake here.
你正在做几个:
- 您应该使用
CVPixelBufferCreateWithPlanarBytes()
。我不知道CVPixelBufferCreateWithBytes()
是否可以用来创建平面视频帧;如果是这样,它将需要一个指向“平面描述符 block ”的指针(我似乎无法在文档中找到该结构)。 frame->linesize[0]
是每行的字节数,而不是整个图像的大小。文档不清楚,但是 usage是相当明确的。frame->linesize[0]
指的是Y平面;你关心 UV 平面。sizeof(int)
来自哪里?- 您正在传递
cvPixelBufferSample
;你可能是指&cvPixelBufferSample
。 - 您没有传递发布回调。文档没有说你可以传递
NULL
。
尝试这样的事情:
size_t srcPlaneSize = frame->linesize[1]*frame->height;
size_t dstPlaneSize = srcPlaneSize *2;
uint8_t *dstPlane = malloc(dstPlaneSize);
void *planeBaseAddress[2] = { frame->data[0], dstPlane };
// This loop is very naive and assumes that the line sizes are the same.
// It also copies padding bytes.
assert(frame->linesize[1] == frame->linesize[2]);
for(size_t i = 0; i<srcPlaneSize; i++){
// These might be the wrong way round.
dstPlane[2*i ]=frame->data[2][i];
dstPlane[2*i+1]=frame->data[1][i];
}
// This assumes the width and height are even (it's 420 after all).
assert(!frame->width%2 && !frame->height%2);
size_t planeWidth[2] = {frame->width, frame->width/2};
size_t planeHeight[2] = {frame->height, frame->height/2};
// I'm not sure where you'd get this.
size_t planeBytesPerRow[2] = {frame->linesize[0], frame->linesize[1]*2};
int ret = CVPixelBufferCreateWithPlanarBytes(
NULL,
frame->width,
frame->height,
kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange,
NULL,
0,
2,
planeBaseAddress,
planeWidth,
planeHeight,
planeBytesPerRow,
YOUR_RELEASE_CALLBACK,
YOUR_RELEASE_CALLBACK_CONTEXT,
NULL,
&cvPixelBufferSample);
内存管理留给读者作为练习,但对于测试代码,您可能会传入 NULL
而不是发布回调。
关于ios - 如何将 YUVJ420P 中的 FFMPEG AVFrame 转换为 AVFoundation cVPixelBufferRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15258290/