QML using OpenGL rendering Yuv__opengl

Source: Internet
Author: User

Preface the comparison of two architectures for making players with QML the shoulders of the predecessors loaded shader language Sharder brief description of Sharder loading QML rendering process OpenGL rendering OpenGL for YUV data requirements

Preface

Use QML to make a video player. In the video frame decoding out, finally unavoidable also to give QML also show. using QML to make the player, the comparison between the two architectures

QML provides the image incoming interface, after decoding completes, will qimage hand over to qml display.
Such a direct abandonment.
The reason is:
1, the picture transmission is too slow
2, the whole action is to hand over the CPU to execute, in the weak performance of the machine, refresh the picture is very slow
Implement your own classes that inherit from Qquickitem, the data flows inside the item, and then invoke the appropriate functionality in the same way that you use ordinary QML controls.
This is the recommended way to use. That's what Qtav did. But it's packaged too well, and a lot of the time, we probably just need a little bit of the functional module, which is hard to disassemble. So I had to build the wheel again. the shoulders of our predecessors

Qt5_qml_opengl_shader, this blog is almost done. But write it again, some details may not be the same as you understand. Some details should be paid special attention to deal with. Load Shader Language Sharder Brief Description

Sharder's description, online voluminous.
The features we use: The short point is, tell OpenGL the three-dimensional object shape you want to pass in and the color you want to render. Loading of Sharder

I didn't choose to include the Sharder content directly in the code, but instead put it in a resource file.

Instead of choosing to include sharder content directly in your code, you put it in a single resource file.
M_program = new Qopenglshaderprogram ();
BOOL Ok0 = M_program->addshaderfromsourcefile (Qopenglshader::vertex, ":/os/openglresource/ Qmlopenglvideovertexshader.vsh ");
BOOL Ok1 = false;
if (Qopenglcontext::currentcontext ()->isopengles ())
{//here OpenGL and opengles load different shader, not the same because, There is a little bit of difference between the two (keyword precision not in opengl)
     Ok1 = M_program->addshaderfromsourcefile (Qopenglshader::fragment, " :/os/openglresource/qmlopenglvideofragmentshader.fsh ");
}
else
{
     Ok1 = M_program->addshaderfromsourcefile (Qopenglshader::fragment, ":/os/openglresource/ Qmlopenglvideofragmentshader.frag ");
}

The OpenGL ES load a different fragmentshader because OpenGL and OpenGL ES are a little different, depending on the device
The main difference here is:

Macros here must be added, or most of the Android TV will not show
#ifdef gl_fragment_precision_high
PRECISION highp float;
#else
Precision Mediump float;
#endif
QML Rendering Process

OpenGL is QML used as a component, and there is a problem of who first renders it. The same area, after rendering will overwrite the rendering before.

The order of the rendering determines the stacking order.

Render QML First, then render OpenGL
Connect (Window (), &qquickwindow::afterrendering, M_renderer, & Alopenglqmlvideoshowrenderer::p aint, Qt::D irectconnection);
Render OpenGL first, then render qml
Connect (Window (), &qquickwindow::afterrendering, M_renderer, & Alopenglqmlvideoshowrenderer::p aint, Qt::D irectconnection);
rendering of OpenGL

In fact, there is nothing to say, feel the whole process is dead, not too much play space.
In the whole demo, I try to use the OpenGL class QT, there is no way to use OpenGL native.
But I always think Qt is not doing very well here.

M_program->bind ();
int vertslocation = m_program->attributelocation ("Vertexin");
int texturelocation = m_program->attributelocation ("Texturein");
M_program->enableattributearray (vertslocation);
M_program->enableattributearray (texturelocation);
M_program->setattributearray (Vertslocation, Gl_float, vertexvertices,2);

M_program->setattributearray (Texturelocation, Gl_float, texturevertices,2);
Textureuniformy = M_program->uniformlocation ("tex_y");
Textureuniformu = M_program->uniformlocation ("Tex_u");
TEXTUREUNIFORMV = M_program->uniformlocation ("Tex_v");
Glviewport (M_nx, M_viewportsize.height ()-M_ny-m_nheight, M_nwidth, m_nheight); Qbytearray t_bytearray;//{qmutexlocker locker (&m_imagedatamutex); t_bytearray= m_imagedata;} m_pBufYuv420p = (
unsigned char*) t_bytearray.data ();
Glactivetexture (GL_TEXTURE0);
Generate textures from Y data glbindtexture (gl_texture_2d, id_y); Create true y-data textures using m_pbufyuv420p data in memory glteximage2d (gl_texture_2d, 0, Gl_luminance, M_nvideow, M_nvidEoh, 0, Gl_luminance, Gl_unsigned_byte, m_pbufyuv420p);
Gltexparameteri (gl_texture_2d,gl_texture_mag_filter,gl_linear);
Gltexparameteri (gl_texture_2d,gl_texture_min_filter,gl_linear);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, Gl_clamp_to_edge);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, Gl_clamp_to_edge);
Load u data Texture glactivetexture (gl_texture1);//Activate Texture unit gl_texture1 glbindtexture (gl_texture_2d, Id_u); Glteximage2d (gl_texture_2d, 0, Gl_luminance, M_NVIDEOW/2, M_NVIDEOH/2, 0, Gl_luminance, Gl_unsigned_byte, (char*) m_
pbufyuv420p + M_nvideoh*m_nvideow);
Gltexparameteri (gl_texture_2d,gl_texture_mag_filter,gl_linear);
Gltexparameteri (gl_texture_2d,gl_texture_min_filter,gl_linear);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, Gl_clamp_to_edge);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, Gl_clamp_to_edge);
Load v Data texture glactivetexture (GL_TEXTURE2);//Activate Texture unit gl_texture2 glbindtexture (gl_texture_2d, Id_v); Glteximage2d (gl_texture_2d, 0, Gl_luminance, M_NVIDEOW/2, M_NVIDEOH/2,0, Gl_luminance, Gl_unsigned_byte, (char*) m_pbufyuv420p + M_nvideoh*m_nvideow * 5/4);
Gltexparameteri (gl_texture_2d,gl_texture_mag_filter,gl_linear);
Gltexparameteri (gl_texture_2d,gl_texture_min_filter,gl_linear);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, Gl_clamp_to_edge);
Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, Gl_clamp_to_edge);
Gluniform1i (textureuniformy, 0);
Specifies the U-texture to use the new value gluniform1i (TEXTUREUNIFORMU, 1);
Specifies the V texture to use the new value gluniform1i (TEXTUREUNIFORMV, 2);
Glclearcolor (0.0, 0.5, 0.0, 1.0); Glclear (Gl_color_buffer_bit |
Gl_depth_buffer_bit);
Draw the triangle! Gldrawarrays (Gl_triangle_strip, 0, 4); Starting from Vertex 0;
3 vertices total-> 1 triangle M_program->disableattributearray (texturelocation);
M_program->disableattributearray (vertslocation);
M_program->release ();
Not strictly needed to this example, but generally useful to when//mixing with raw OpenGL. M_window->resetopenglstate ();

One of the things that needs to be noted

Glclearcolor (0.0, 0.5, 0.0, 1.0); 
 Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);

The effect is to clear the background, if opened. The previously drawn content will be emptied. OpenGL requirements for YUV data

In addition to the format requirements, there is no useless data in the incoming data.
That is, the memory alignment is 1.

Glteximage2d (gl_texture_2d, 0, Gl_luminance, M_NVIDEOW/2, M_NVIDEOH/2, 0, Gl_luminance, Gl_unsigned_byte, (char*) m_ pbufyuv420p + M_nvideoh*m_nvideow * 5/4);

Here source code
Learn_openglunderqml_5.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.