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

arb*_*ief 7 ffmpeg yuv avfoundation ios

我有一个FFMPEG AVFrameYUVJ420P,我想将其转换为一个CVPixelBufferRefCVPixelBufferCreateWithBytes.我想这样做的原因是使用AVFoundation来显示/编码帧.

我选择kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange并尝试转换它,因为AVFrame有三个平面的数据 Y480 Cb240 Cr240.根据我所研究的内容,这与所选的相匹配kCVPixelFormatType.通过双平面我需要将其转换为包含Y480CbCr480Interleaved 的缓冲区.

我尝试用2个平面创建一个缓冲区:

  • frame->data[0] 在第一架飞机上,
  • frame->data[1]frame->data[2]在第二个平面上交错.

但是,我收到的返回错误-6661 (invalid a)来自CVPixelBufferCreateWithBytes:

"Invalid function parameter. For example, out of range or the wrong type."
Run Code Online (Sandbox Code Playgroud)

我根本没有关于图像处理的专业知识,所以任何指向文档的指针都可以让我从正确的方法开始解决这个问题.我的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)
Run Code Online (Sandbox Code Playgroud)

该帧是AVFrame来自FFMPEG解码的rawData.

tc.*_*tc. 6

我的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);
Run Code Online (Sandbox Code Playgroud)

内存管理留给读者一个练习,但对于测试代码,你可能会通过传入NULL而不是发布回调来逃避.