在Android上的OpenGL中绘制视频帧

时间:2012-06-18 21:17:19

标签: 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;
}

1 个答案:

答案 0 :(得分:5)

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

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