0

Currently I am developing a video player using FFMPEG. I'm trying to convert YUV420P to RGB by Shader to reduce performance hit and I could see it works fine. The problem is caused when I try to change image size.

Case 1. YUV to RGB is perfect. but the image is not exactly fit to Texture Bounds. For example, if i play 640x360 video, right (640-512) part is cropped and bottom (512-360) is filled with green colored rectangle.

FRAME_X=512; //This is texture size
FRAME_Y=512;

    avpicture_fill((AVPicture *) f, [currentVideoBuffer.data mutableBytes],
               enc->pix_fmt,
               FRAME_X, FRAME_Y);

    av_picture_copy((AVPicture *) f, (AVPicture *) avFrame,
                enc->pix_fmt,
                enc->width, enc->height);

....

int yuvWidth= FRAME_X ;
int  yuvHeight= FRAME_Y;
glBindTexture ( GL_TEXTURE_2D, textureIdY );
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 
yuvWidth, yuvHeight, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, y_channel);


glBindTexture ( GL_TEXTURE_2D, textureIdU );
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE,
yuvWidth/2, yuvHeight/2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, u_channel);

glBindTexture ( GL_TEXTURE_2D, textureIdV );
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 
yuvWidth/2, yuvHeight/2, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, v_channel);

Case 2. If i set actual image size to texture size, then image is exactly fit to texture but the color of image is a little bit strange. It has too much green color.

Does anybody give me some clues for this?? Thanks in advance.

4

1 回答 1

4

首先,您需要为非幂的两个纹理设置正确的包裹模式。

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

然后,尝试将过滤模式设置为 GL_LINEAR

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  

和着色器。

  1. 顶点着色器

    attribute vec4 position;  
    attribute vec2 texcoord;  
    uniform mat4 modelViewProjectionMatrix;  
    varying vec2 v_texcoord;
    
    void main()  
    {  
      gl_Position = modelViewProjectionMatrix * position;  
      v_texcoord = texcoord.xy;  
    }  
    
  2. 设置顶点

    vertices[0] = -1.0f;  // x0
    vertices[1] = -1.0f;  // y0
    vertices[2] =  1.0f;  // ..
    vertices[3] = -1.0f;
    vertices[4] = -1.0f;
    vertices[5] =  1.0f;
    vertices[6] =  1.0f;  // x3
    vertices[7] =  1.0f;  // y3
    
  3. 为 2d 图像(精灵)加载适当的 viewProj 矩阵,最好使用正射投影。

    GLfloat modelviewProj[16];  
    mat4f_LoadOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, modelviewProj);  
    glUniformMatrix4fv(_uniformMatrix, 1, GL_FALSE, modelviewProj);  
    
  4. 接下来,确保使用正确的文本坐标,像这样

    GLfloat texCoords[] = {  
      0.0f, 1.0f,  
      1.0f, 1.0f,  
      0.0f, 0.0f,  
      1.0f, 0.0f,  
    };  
    
  5. 最后,像这样的片段着色器:

    varying highp vec2 v_texcoord;  
    uniform sampler2D s_texture_y;  
    uniform sampler2D s_texture_u;  
    uniform sampler2D s_texture_v;
    
    void main()  
    {  
      highp float y = texture2D(s_texture_y, v_texcoord).r;  
      highp float u = texture2D(s_texture_u, v_texcoord).r - 0.5;  
      highp float v = texture2D(s_texture_v, v_texcoord).r - 0.5;  
      highp float r = y +             1.402 * v;  
      highp float g = y - 0.344 * u - 0.714 * v;  
      highp float b = y + 1.772 * u;       
      gl_FragColor = vec4(r,g,b,1.0);  
    }  
    

您可以将我的代码视为在 iOS 上使用 FFmpeg 和 OpenGLES 的示例:https ://github.com/kolyvan/kxmovie 。

于 2012-10-27T04:01:13.090 回答