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

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

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

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

> frame->第一个平面上的数据[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,cvPixelBufferSample)

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

解决方法

My C skills aren’t top of the line either so maybe im making a basic mistake here.

你做了几个:

>您应该使用CVPixelBufferCreateWithPlanarBytes().我不知道CVPixelBufferCreateWithBytes()是否可用于创建平面视频帧;如果是这样,它将需要一个指向“平面描述符块”的指针(我似乎无法在文档中找到结构).
> 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,2,planeBaseAddress,planeWidth,planeHeight,planeBytesPerRow,YOUR_RELEASE_CALLBACK,YOUR_RELEASE_CALLBACK_CONTEXT,&cvPixelBufferSample);

内存管理留给读者,但对于测试代码,您可能会通过传递NULL而不是释放回调来逃避.

以上是来客网为你收集整理的ios – 如何将YUVJ420P中的FFMPEG AVFrame转换为AVFoundation cVPixelBufferRef?全部内容,希望文章能够帮你解决ios – 如何将YUVJ420P中的FFMPEG AVFrame转换为AVFoundation cVPixelBufferRef?所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。