android - 使用 Android NDK 通过 openGLES2 渲染视频帧

标签 android opengl-es android-ndk ffmpeg render

我用NDK做了一个bin码,通过ffmpeg解码得到视频帧,渲染到opengles,但是画面没有任何变化,还是显示launcher。

*但是当我制作一个apk启动时,它总是显示黑色。*

我的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);

为什么没有显示?我应该使用 abk 而不是 bin 文件来运行吗?

最佳答案

Surface 默认对某些其他 View 隐藏。检查您是否在表面放置了一些 View 。或在顶部设置 surfaceZOrder。

关于android - 使用 Android NDK 通过 openGLES2 渲染视频帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18245163/

相关文章:

java - Android - 获取 View 的可见区域?

ios - iOS glGenerateMipmap 是同步的,还是可能是异步的?

android - 手动调用 android_main(struct android_app* state)?

android - 房间 - 使用@get :Query instead @Query

android - 我们如何设置折叠底部的最小高度

android - listviewitem 上未显示警告框单击 android

android - 在 OpenGL ES Android 中加载模型的最快方法是什么?

performance - 通过最小化着色器/状态更改来优化 WebGL 性能的指南

android - 编译 LOCAL_SRC_FILES 时 Cocos2d-x 错误指向丢失的文件(和文件夹)Windows

android - DalvikVM 上的 CLI 在 JNI 库上失败