【问题标题】:Drawing Video Frames in OpenGL on Android在 Android 上使用 OpenGL 绘制视频帧
【发布时间】:2012-06-18 21:17:19
【问题描述】:

我目前正在开发一个通过 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;
}

【问题讨论】:

    标签: java android opengl-es


    【解决方案1】:

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

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

    【讨论】:

    • 我会专门添加一些对 glTexSubimage 的引用,因为 OP 也有可能在每一帧上都使用 glTexImage() (即使您正在更新整个图像,也会对性能产生负面影响)。
    • 每帧调用 glTexImage() 与 glTexSubimage() 相比有什么缺点?感谢您的帮助!
    • 相信glTexImage会在VRAM中分配一个新的内存区域,然后丢弃旧数据。 glTexSubImage 只是覆盖现有内存中的字节,因此不必分配/释放内存。 @Samusaaron3
    • 您能否进一步解释一下,因为我无法理解。首先我们创建位图 1024*1024,我们使用 texImage2D() 将其作为背景,然后当我们想要使用 texSubImage2D 放置另一个图像(不是 2 的幂)时?它是否正确?你能在这里放一些代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 2013-10-16
    • 2019-03-10
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多