【问题标题】:How to convert an FFMPEG AVFrame in YUVJ420P to AVFoundation cVPixelBufferRef?如何将 YUVJ420P 中的 FFMPEG AVFrame 转换为 AVFoundation cVPixelBufferRef?
【发布时间】:2013-02-21 21:21:27
【问题描述】:

我在YUVJ420P 中有一个FFMPEG AVFrame,我想用CVPixelBufferCreateWithBytes 将它转换为CVPixelBufferRef。我想这样做的原因是使用 AVFoundation 来显示/编码帧。

我选择了kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange 并尝试对其进行转换,因为 AVFrame 的数据位于三个平面中 Y480Cb240Cr240。根据我的研究,这与选定的kCVPixelFormatType 匹配。由于是双平面的,我需要将其转换为包含 Y480CbCr480 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 解码的 rawData。

【问题讨论】:

  • cvPixelBufferSample 是 CVPixelBufferRef 吗?如果是这样,CVPixelBufferCreateWithBytes() 中的最后一个参数需要是&amp;cvPixelBufferSample,以允许通过引用返回指向新像素缓冲区的指针。

标签: ios ffmpeg avfoundation yuv


【解决方案1】:

我的 C 技能也不是一流的,所以我可能在这里犯了一个基本错误。

你正在制作几个:

  • 您应该使用CVPixelBufferCreateWithPlanarBytes()。不知道CVPixelBufferCreateWithBytes()能不能用来制作平面视频帧;如果是这样,它将需要一个指向“平面描述符块”的指针(我似乎无法在文档中找到该结构)。
  • frame-&gt;linesize[0]每行的字节数,而不是整个图像的大小。文档不清楚,但usage 相当明确。
  • frame-&gt;linesize[0] 指 Y 平面;你关心紫外线平面。
  • sizeof(int) 来自哪里?
  • 你传入cvPixelBufferSample;你可能是指&amp;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 而不是释放回调。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-13
    • 2015-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多