在Android上的OpenGL中绘制视频帧

Sam*_*on3 2 java android opengl-es

我目前正在研究一种在Android上通过OpenGL ES 1.0显示视频帧(作为位图)的系统.我的问题是我无法获得超过10 fps的速度.

经过一些测试后,我确定最大的瓶颈之一就是需要位图的宽度和高度都是2的幂.例如,640x480视频必须扩展到1024x1024.没有缩放,我已经能够获得大约40-50fps,但纹理只是显示为白色,这对我没有好处.

我知道OpenGL ES 2.0支持使用两种纹理的非幂,但我没有使用着色器/ 2.0中的任何其他新功能

有什么办法可以解决这个问题吗?与我的相比,其他视频播放如何获得如此好的表现?我已经包含了一些代码供参考.

private Bitmap makePowerOfTwo(Bitmap bitmap)
{
    // If one of the bitmap's resolutions is not a power of two
    if(!isPowerOfTwo(bitmap.getWidth()) || !isPowerOfTwo(bitmap.getHeight()))
    {
        int newWidth = nextPowerOfTwo(bitmap.getWidth());
        int newHeight = nextPowerOfTwo(bitmap.getHeight());

        // Generate a new bitmap which has the needed padding
        return Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
    }
    else
    {
        return bitmap;
    }
}

private static boolean isPowerOfTwo(int num)
{
    // Check if a bitwise and of the number and itself minus one is zero
    return (num & (num - 1)) == 0;
}

private static int nextPowerOfTwo(int num)
{
    if(num <= 0)
    {
        return 0;
    }

    int nextPowerOfTwo = 1;

    while(nextPowerOfTwo < num)
    {
        nextPowerOfTwo <<= 1; // Bitwise shift left one bit
    }

    return nextPowerOfTwo;
}
Run Code Online (Sandbox Code Playgroud)

Tim*_*Tim 5

仅仅因为纹理必须是2的幂,并不意味着你的数据必须是2的幂.

您可以在初始化期间自由创建1024x1024(或1024x512)纹理glTexImage,使用您的位图数据填充较低的640x480 glTexSubImage,然后使用一些智能texcoords显示较低的640x480纹理(0,0) to (640/1024, 480/1024).纹理的其余部分将包含从未见过的空白空间.

  • 我将特别添加一些glTexSubimage参考,因为OP可能在每一帧上都使用glTexImage()(即使你正在更新整个图像,它也会产生其他负面的性能影响). (2认同)