如何将YUVJ420P格式的FFMPEG AVFrame转换为AVFoundation cVPixelBufferRef?

7
我有一个FFMPEG中的AVFrame,格式为YUVJ420P,我想将其转换为CVPixelBufferRef,并使用CVPixelBufferCreateWithBytes。我这么做的原因是要使用AVFoundation来显示/编码帧。
我选择了kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange并尝试进行转换,因为AVFrame的数据在三个平面Y480 Cb240 Cr240中。根据我的研究,这与所选的kCVPixelFormatType相匹配。由于是双平面,我需要将它转换为包含交错的Y480CbCr480的缓冲区。
我尝试创建一个具有2个平面的缓冲区: - frame->data[0]在第一个平面上, - frame->data[1]和frame->data[2]交错在第二个平面上。
但是,我从CVPixelBufferCreateWithBytes得到了返回错误-6661(无效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)

这个框架是带有从FFMPEG解码中获得的原始数据的AVFrame


cvPixelBufferSample 是一个 CVPixelBufferRef 吗?如果是的话,那么 CVPixelBufferCreateWithBytes() 中的最后一个参数应该是 &cvPixelBufferSample,以便通过引用返回新像素缓冲区的指针。 - Brad Larson
1个回答

7

我的C语言技能也不是顶尖的,所以也许我在这里犯了一个基本错误。

你正在犯下几个错误:

  • 你应该使用CVPixelBufferCreateWithPlanarBytes()。我不知道CVPixelBufferCreateWithBytes()是否可以用于创建平面视频帧;如果可以,它将需要一个指向“平面描述符块”的指针(我在文档中找不到这个结构体)。
  • frame->linesize[0]是每行的字节数,而不是整个图像的大小。文档不清楚,但用法相当明确。
  • 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而不是释放回调函数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接