将YUV写入文件会导致重复的帧

Writing YUV to file results in duplicated frames

本文关键字:YUV 文件      更新时间:2023-10-16

我正在将YUV (YV12)帧写入YUV文件。我正好保存了101帧。但是当我播放输出YUV文件时,我有2倍多的帧,而每一帧总是空的。

下面是我的代码:
            size_t lenght=_viewWidth * _viewHeight * 3;
    BYTE *bytes=(BYTE*)malloc(lenght);
    ///////////////  read pixels from tex  /////////////////////
    glBindTexture(GL_TEXTURE_2D,tex);
    glGetTexImage(GL_TEXTURE_2D,0,GL_RGB,GL_UNSIGNED_BYTE,bytes);
    glBindTexture(GL_TEXTURE_2D,0);
        BYTE *uvOut= new uint8_t[_viewWidth * _viewHeight *3];
            if(cfg.seqStart <= cfg.seqEnd)
    {
    hOutFile = fopen( outFileName.c_str(), cfg.appendMode ? "ab" : "wb" );
    assert(hOutFile!=0);

       RGBtoYUV420PSameSize(bytes,uvOut,3,0,_viewWidth,_viewHeight);
     fwrite(uvOut,_viewWidth* 3, _viewHeight, hOutFile); // Write V line
    fclose(hOutFile);
        cfg.seqStart++;
    }else{
        printf("done");
    }
    delete uvOut;
    free(bytes);

我运行这个块101次。我又检查了一遍。另一个框架从何而来?

我对opengl一无所知,但是yv12格式的帧的大小是

width * height * 1.5

。色度部分在水平和垂直方向上被2倍的采样。上面我经常看到3,将该值改为1.5

如果您对yuv格式转换工具感兴趣,请检查我用python编写的这个。还有一个基于SDL的查看器。这里有很多灵感:-)