如何使用Android NDK通过openGLES2渲染视频

时间:2013-08-26 03:16:17

标签: android android-ndk ffmpeg opengl-es-2.0 egl

我使用NDK制作了一个apk代码,通过ffmpeg解码获取视频帧,并渲染到opengles 2.0,但屏幕没有显示任何内容。所以我犯了错误吗?

我的opengles2.0 ini代码如下:

static int window_init_display(void)
{
EGLint attribs [] = {
  EGL_RED_SIZE,        8,
  EGL_GREEN_SIZE,      8,
  EGL_BLUE_SIZE,       8,
  EGL_ALPHA_SIZE,      8,
  EGL_DEPTH_SIZE,     16,
  EGL_STENCIL_SIZE,    0,
  EGL_SAMPLE_BUFFERS,  0,
  EGL_SAMPLES,         0,
  EGL_SURFACE_TYPE,    EGL_WINDOW_BIT,
  EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  EGL_NONE
};

EGLint w, h, format;
EGLint numConfigs;
EGLConfig config;
EGLSurface surface;
EGLContext context;

EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

eglInitialize(display, 0, 0);

eglChooseConfig(display, attribs, &config, 1, &numConfigs);

eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);

ANativeWindow_setBuffersGeometry(g_application->window, 0, 0, format);

surface = eglCreateWindowSurface(display, config, g_application->window, NULL);

eglQuerySurface(display, surface, EGL_WIDTH, &w);    
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
LOGI("EGL_WIDTH=%d, EGL_HEIGHT=%d",w,h);

eglBindAPI(EGL_OPENGL_ES_API);

EGLint contextAttrs[] =
{
   EGL_CONTEXT_CLIENT_VERSION, 2,
   EGL_NONE
};
context = eglCreateContext(display, config, NULL, contextAttrs);

if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) 
{         
        LOGW("Unable to eglMakeCurrent");      
        return -1;   
}

LOGI("OK init EGL !!!!");

我的渲染代码如下:

int m_rgbBufferSize = pPicture->iDisplayWidth*pPicture->iDisplayHeight*4;
m_rgbBuffer = new unsigned char[m_rgbBufferSize];

struct SwsContext *m_sw_context=NULL;
m_sw_context = m_dllSwScale.sws_getCachedContext(m_sw_context,
                                                  pPicture->iWidth, pPicture->iHeight, PIX_FMT_YUV420P,
                                                  pPicture->iWidth, pPicture->iHeight, PIX_FMT_RGBA,
                                                  SWS_FAST_BILINEAR, NULL, NULL, NULL);

uint8_t *psrc[] = { pPicture->data[0], pPicture->data[1], pPicture->data[2], 0 };
int srcStride[] = { pPicture->iLineSize[0], pPicture->iLineSize[1], pPicture->iLineSize[2], 0 };
uint8_t *dst[]  = { m_rgbBuffer, 0, 0, 0 };
int dstStride[] = { pPicture->iDisplayWidth*4, 0, 0, 0 };
m_dllSwScale.sws_scale(m_sw_context, psrc, srcStride, 0, pPicture->iDisplayHeight, dst, dstStride);
//LOGV("swscale OK");
glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
if(textureid == 0)
    glGenTextures(1, &textureid);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, textureid);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 
                pPicture->iDisplayWidth, pPicture->iDisplayHeight, GL_RGBA, GL_UNSIGNED_BYTE, m_rgbBuffer);

0 个答案:

没有答案