记录一下在uyuv 转 planar yuv420 的做法

1、uyuv 排列格式

8bit   8bit   8bit  8bit

 

u        y        u     v

 

planar yuv420 排列格式

8bit 

y y y y y y y y

u u

v v

 

转换代码是

 

void YUVCSCWgt::on_btn_spyuv420_clicked()
{
    char uyuv_buf[LINENUM*2];
    char y_buf[LINENUM];
    char u_buf[LINENUM/2];
    char v_buf[LINENUM/2];

    InitFile();

    for(int i=0;i<1080;i++)
    {
        rw_len=m_yvuFile->read(uyuv_buf,LINENUM*2);
        if(rw_len!=LINENUM*2)
        {
            printf("read line num error! %d \n",rw_len);
        }

        for(int j=0;j<LINENUM*2;j++)
        {
            if(i%2==0)
            {
                if(j%4==0)
                {
                    u_buf[j/4]=uyuv_buf[j];
                }
                else if(j%4==2)
                {
                    v_buf[j/4]=uyuv_buf[j];
                }
            }

            if(j%4==3 || j%4==1)
            {
                y_buf[j/2]=uyuv_buf[j];
            }
        }

        m_yuvFile_y->write(y_buf,LINENUM);
        m_yuvFile_y->flush();
        if(i%2==0)
        {
            m_yuvFile_u->write(u_buf,LINENUM/2);
            m_yuvFile_u->flush();
            m_yuvFile_v->write(v_buf,LINENUM/2);
            m_yuvFile_v->flush();
        }
    }

    //write to yuvfile
    m_yuvFile_y->seek(0);
    m_yuvFile_u->seek(0);
    m_yuvFile_v->seek(0);

    for(int i=0;i<1080;i++)
    {
        rw_len=m_yuvFile_y->read(uyuv_buf,LINENUM);
        if(rw_len!=LINENUM)
        {
            printf("read line num error! %d \n",rw_len);
        }
        rw_len=m_yuvFile->write(uyuv_buf,LINENUM);
        if(rw_len!=LINENUM)
        {
            printf("read line num error! %d \n",rw_len);
        }
        m_yuvFile->flush();
    }
    //m_yuvFile_y->close();

    for(int i=0;i<270;i++)
    {
        rw_len=m_yuvFile_u->read(uyuv_buf,LINENUM);
        if(rw_len!=LINENUM)
        {
            printf("read line num error! %d \n",rw_len);
        }
        rw_len=m_yuvFile->write(uyuv_buf,LINENUM);
        if(rw_len!=LINENUM)
        {
            printf("read line num error! %d \n",rw_len);
        }
        m_yuvFile->flush();
    }
    //m_yuvFile_v->close();

    for(int i=0;i<270;i++)
    {
        rw_len=m_yuvFile_v->read(uyuv_buf,LINENUM);
        if(rw_len!=LINENUM)
        {
            printf("read line num error! %d \n",rw_len);
        }
        rw_len=m_yuvFile->write(uyuv_buf,LINENUM);
        if(rw_len!=LINENUM)
        {
            printf("read line num error! %d \n",rw_len);
        }
        m_yuvFile->flush();
    }

    m_yvuFile->close();
    m_yuvFile->close();
    m_yuvFile_y->close();
    m_yuvFile_u->close();
    m_yuvFile_v->close();
}

 

posted @ 2019-11-27 10:56  eastgeneral  阅读(554)  评论(0编辑  收藏  举报