Android NV21 format video rendered via OpenGL ES

Source: Internet
Author: User

Android NV21 format video rendered via OpenGL ES

The first time to write a blog (mainly to their own), please the Great God!!!

Extracting YUV components from NV21 format video
    1. YUV, divided into three components, "Y" for luminance (luminance or luma), that is, gray values, and "U" and "V" is the chroma (chrominance or chroma), the role is to describe the image color and saturation, to specify the color of the pixel.
    2. For Android camera recording video, most of the data used are NV21 (default format) and NV12 format
    3. NV21 format data is arranged in yyyyyyyyyy (w*h) Vuvuvuvuvu (W*H/2). For the NV12 format, the arrangement is yyyyyyy (w*h) Uvuvuvuvuvuv (W*H/2).

According to the above-mentioned NV21 data arrangement format, actually extracting YUV component is very simple (the following code has been tested in person)

//nv21 format width and height, for camera is the selected resolutionFinal intSize = width * height;Final byte[] y =New byte[Size];//Note here that UV unification accounts for 1/2 of Y, then each occupiesFinal byte[] U =New byte[size/4];Final byte[] v =New byte[size/4];//Extract y componentSystem.arraycopy (data,0Y0, size);//Extract VU component for(inti =0, k =0; I < size/4; I++,k + =2) {V[i] = data[size + K]; U[i] =data[size + k +1];}

According to the above Extract NV21 format YUV component, actually extract NV12 or others are very simple

//Extract YUV component in NV12 formatFinal intSize = width * height;Final byte[] y =New byte[Size];Final byte[] U =New byte[size/4];Final byte[] v =New byte[size/4]; System.arraycopy (data,0Y0, size); for(inti =0, k =0; I < size/4; I++,k + =2) {U[i] = data[size + K]; V[i] =data[size + k +1];}
OpenGL es for display

I do not understand OpenGL Es, which is now just learning, so can only copy the code on the Internet, thank the blogger God to provide the method: Android on the use of OpenGLES2.0 display YUV data

Here, I use my own way to do a description, I mainly used for Android camera data rendering.


    1. Before opening the camera preview, I set the width height by calling mglrenderer.update (width, height)
      2, in the camera data callback, the separation of YUV components and call Mglrenderer.update (Ydata, UData, VData);
My changes.

1, in order to achieve the vertical screen preview effect, I modified glprogram in the Coordvertices
java
// 横屏
private static float[] coordVertices = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, }
//竖屏
private static float[] coordVertices = {1.0f,1.0f,1.0f,0.0f,0.0f,1.0f,0.0f,0.0f,};

As for the principle, please see http://www.lai18.com/content/1455210.html

2, I need to use a full-screen preview form, even if the image is pulled up also does not matter, so need to change the update (int w, int h) method:

  if00) {         prog.createBuffers(GLProgram.squareVertices);   if (w != mVideoWidth && h != mVideoHeight) {     this.mVideoWidth = w;     this.mVideoHeight = h;     int yarraySize = w * h;     int4;     synchronized (this) {     y = ByteBuffer.allocate(yarraySize);     u = ByteBuffer.allocate(uvarraySize);     v = ByteBuffer.allocate(uvarraySize);    }}

Well, after the above changes, my program can already run up!!

I will write more blogs later. The writing is not good, please advise .....

Android renders NV21 format video via OpenGL ES

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.