#include "qvideowidget_yuy2.h"
#include "qvideoshader.h"


QVideoWidget_YUY2::QVideoWidget_YUY2(QWidget* parent, Qt::WindowFlags f):QVideoWidget(parent, f)
{
    m_nTextureWidth = 1920;
    m_nTextureHeight = 1080;
}

QVideoWidget_YUY2::~QVideoWidget_YUY2()
{

}

void QVideoWidget_YUY2::initializeGL()
{
    QOpenGLContext * pCurrent = QOpenGLContext::currentContext();

    initializeOpenGLFunctions();

    setUpdateBehavior(QOpenGLWidget::NoPartialUpdate);
    //setUpdateBehavior(QOpenGLWidget::PartialUpdate);

    GLuint nFrameBufferObject = defaultFramebufferObject();

    int i=0;

    for(i=0 ; i<18 ; i++)
    {
        m_fNormalRect[i] = -1;
    }

    GLfloat afTexCoord[] = {
        0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f,
                    1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f
    };

    memcpy(m_fNormalRectTexture, afTexCoord, sizeof(GLfloat)*2*6);

    m_nTmpDisplayType = 0;

    QSize nSize = size();

    int nWidth = nSize.width();
    int nHeight = nSize.height();

    nWidth = m_nTextureWidth;
    nHeight = m_nTextureHeight;



    m_pVideoShader = new QVideoShader;    
    m_pVideoShader->CreateTexture(nWidth*2, nHeight, QOpenGLTexture::Target2D, QOpenGLTexture::R8_UNorm);

    char *pSrcVertex =
            "#version 300 es\n"
        "attribute highp vec4 vertex;\n"
        "attribute highp vec4 texCoord;\n"
        "uniform mediump mat4 matrix;\n"
        "varying highp vec4 texc;\n"
        "void main(void)\n"
        "{\n"
        "    gl_Position = matrix * vertex;\n"
        "    texc = texCoord;\n"
        "}\n";


    char *pSrcFragmentCaptureEnvent =
            "#version 300 es\n"
            "in highp vec4 texc;\n"
            "uniform sampler2D tex;\n"
            "out lowp vec4 FragColor;\n"
            "uniform lowp int nImageWidth;\n"
            "uniform lowp int nImageHeight;\n"
            "void main(void)\n"
            "{\n"
            "   int nX=0;\n"
            "   int nY=0;\n"
            "   int nIndex=0;\n"
            "   highp float fX = 0.0;\n"
            "   highp float fY = 0.0;\n"
            "   int nInternalIndex = 0;\n"
            "   highp float fImageWidth = float(nImageWidth);\n"
            "   highp float fImageHeight = float(nImageHeight);\n"
            "   fX = floor(texc.s*(fImageWidth));\n"
            "   fY = floor(texc.t*fImageHeight);\n"
            "   nX = int(fX);\n"
            "   nY = int(fY);\n"
            "   int nStartX = int((int(nX/4)) * 4);\n"
            "   int nIndexY = nStartX + 0 + int(mod(float(nX/2), 2.0)) * 2;\n"
            "   int nIndexU = nStartX+1;\n"
            "   int nIndexV = nStartX+3;\n"
            //"   if(nX<4) { FragColor = vec4(0.0, 0.0, 0.0, 1.0); return; }\n"
            "   fX = float((nX/2)*2+1)/(fImageWidth);\n"
            "   fY = float(nY)/(fImageHeight);\n"
            "   highp vec4 colorY = texture2D(tex, vec2(float(nIndexY)/(fImageWidth)+0.5/fImageWidth,fY+0.5/fImageHeight));\n"
            "   highp vec4 colorU = texture2D(tex, vec2(float(nIndexU)/(fImageWidth)+0.5/fImageWidth,fY+0.5/fImageHeight));\n"
            "   highp vec4 colorV = texture2D(tex, vec2(float(nIndexV)/(fImageWidth)+0.5/fImageWidth,fY+0.5/fImageHeight));\n"
            "   highp float fColorY = colorY.r;\n"
            "   highp float fColorU = colorU.r;\n"
            "   highp float fColorV = colorV.r;\n"
            //"   FragColor = vec4(fColorY, fColorU, fColorV, 1.0); return;\n"


            "   highp float fR = fColorY;\n"
            "   highp float fG = fColorY;\n"
            "   highp float fB = fColorY;\n"

            "   fColorY=1.1643*(fColorY-0.0625);"
            "   fColorU=fColorU-0.5;"
            "   fColorV=fColorV-0.5;"

            "   fR=clamp(fColorY+1.5958*fColorV, 0.0, 1.0);"
            "   fG=clamp(fColorY-0.39173*fColorU-0.81290*fColorV, 0.0, 1.0);"
            "   fB=clamp(fColorY+2.017*fColorU, 0.0, 1.0);"

            "   FragColor = vec4(fR, fG, fB, 1.0);\n"
            "   highp float fTmpColor = colorU\.r;\n"
            //"   FragColor = vec4(fTmpColor, fTmpColor, fTmpColor, 1.0);\n"

            "}\n";

    /*
    char *pSrcFragmentCaptureEnvent =
            "#version 300 es\n"
            "in highp vec4 texc;\n"
            "uniform sampler2D tex;\n"
            "out lowp vec4 FragColor;\n"
            "uniform lowp int nImageWidth;\n"
            "uniform lowp int nImageHeight;\n"
            "void main(void)\n"
            "{\n"
            "   int nX=0;\n"
            "   int nY=0;\n"
            "   int nIndex=0;\n"
            "   highp float fX = 0.0;\n"
            "   highp float fY = 0.0;\n"
            "   int nInternalIndex = 0;\n"
            "   highp float fImageWidth = float(nImageWidth);\n"
            "   highp float fImageHeight = float(nImageHeight);\n"
            "   fX = floor(texc.s*(fImageWidth));\n"
            "   fY = floor(texc.t*fImageHeight);\n"
            "   nX = int(fX);\n"
            "   nY = int(fY);\n"
            "   if(nX<4) { FragColor = vec4(0.0, 0.0, 0.0, 1.0); return; }\n"
            "   fX = float((nX/2)*2+1)/(fImageWidth-1.0);\n"
            "   highp vec4 colorY = texture2D(tex, vec2(fX,texc.t));\n"
            "   highp float fColorY = colorY.r;\n"
            "   highp float fR = fColorY;\n"
            "   highp float fG = fColorY;\n"
            "   highp float fB = fColorY;\n"
            "   FragColor = vec4(fR, fG, fB, 1.0);\n"

            "}\n";
    */


    m_pVideoShader->CreateShader((const char*)pSrcVertex, (const char*)pSrcFragmentCaptureEnvent);
    m_pVideoShader->CreateGeometry();



    if (!m_VBOVideo.isCreated()) {
        //float fRatioWidth = 1572.0f/1920.0f;
        float fRatioWidth = 1.0;
        GLfloat afVertices[] = {
            -1, -fRatioWidth, -1,
            1,-fRatioWidth,-1,

            -1,fRatioWidth,-1,
            1, fRatioWidth,-1,

            -1,fRatioWidth,-1,
            1,-fRatioWidth,-1
        };

        GLfloat afTexCoord[] = {
            0.0f,1.0f, 1.0f,1.0f, 0.0f,0.0f,
                        1.0f,0.0f, 0.0f,0.0f, 1.0f,1.0f
        };

        GLfloat afNormals[] = {

            0,0,1, 0,0,1, 0,0,1,
            0,0,1, 0,0,1, 0,0,1
        };

        m_VBOVideo.create();
        m_VBOVideo.bind();

        m_VBOVideo.allocate(6 * 5 * sizeof(GLfloat));
        m_VBOVideo.write(0, afVertices, 6*3*sizeof(GLfloat));
        m_VBOVideo.write(6*3*sizeof(GLfloat), afTexCoord, 6*2*sizeof(GLfloat));
        //m_VBOVideo.write(6*5*sizeof(GLfloat), afNormals, 6*3*sizeof(GLfloat));

        m_VBOVideo.release();
    }

    GLfloat afVertices[] = {
        -1,1, -1,      -0.767,1,-1,       -1,-1,-1,
         -0.767, -1,-1,  -1,-1,-1,        -0.767,1,-1
    };


    QImage image("desk_blue.jpg");
    //QImage image("desk_blue.png");
    int nImageWidth = image.width();
    int nImageHeight = image.height();

    QImage image2 = image.convertToFormat(QImage::Format_RGB888);

    int nBytes = image2.byteCount();
    uint8_t* pImageData = (uint8_t*)image2.bits();

    memcpy(m_pData, pImageData, nBytes);
    m_bChange = true;
    UpdateTexture();
}
