我们如何才能使ExtractMpegFramesTest中的saveFrame()方法更有效? [英] How can we make the saveFrame() method in ExtractMpegFramesTest more efficient?

查看:219
本文介绍了我们如何才能使ExtractMpegFramesTest中的saveFrame()方法更有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[edit]在fadden @ suggestion之后重新格式化为问答格式。

[edit] Reformatting into question and answer format following fadden@ suggestion.

ExtractMpegFramesTest_egl14.java.txt ,方法saveFrame(),有一个循环用于将RGBA重新排序为ARGB以进行Bitmap png压缩(参见下面的文件引用),这怎么可能是否优化?

In ExtractMpegFramesTest_egl14.java.txt, method saveFrame(), there is a loop for reordering RGBA into ARGB for Bitmap png compression (see below quotes from that file), how can this be optimised?

// glReadPixels gives us a ByteBuffer filled with what is essentially big-endian RGBA
// data (i.e. a byte of red, followed by a byte of green...).  We need an int[] filled
// with little-endian ARGB data to feed to Bitmap.
//

...

// So... we set the ByteBuffer to little-endian, which should turn the bulk IntBuffer
// get() into a straight memcpy on most Android devices.  Our ints will hold ABGR data.
// Swapping B and R gives us ARGB.  We need about 30ms for the bulk get(), and another
// 270ms for the color swap.

...

for (int i = 0; i < pixelCount; i++) {
    int c = colors[i];
    colors[i] = (c & 0xff00ff00) | ((c & 0x00ff0000) >> 16) | ((c & 0x000000ff) << 16);
}


推荐答案

事实证明这是一个平等的更快的方法。

It turns out there's an even faster approach.

使用@ elmiguelao答案中的建议,我修改了片段着色器来进行像素交换。这允许我从saveFrame()中删除交换代码。由于我不再需要内存中像素的临时副本,因此我完全取消了 int [] 缓冲区,从此切换:

Using the suggestion in @elmiguelao's answer, I modified the fragment shader to do the pixel swap. This allowed me to remove the swap code from saveFrame(). Since I no longer needed a temporary copy of the pixels in memory, I eliminated the int[] buffer entirely, switching from this:

int[] colors = [... copy from mPixelBuf, swap ...]
Bitmap.createBitmap(colors, mWidth, mHeight, Bitmap.Config.ARGB_8888);

到此:

Bitmap bmp = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(mPixelBuf);

一旦我这样做,我的所有颜色都错了。

As soon as I did that, all of my colors were wrong.

事实证明 Bitmap#copyPixelsFromBuffer()想要RGBA顺序的像素, ARGB顺序。来自 glReadPixels()的值已经采用正确的格式。因此,通过这种方式,我避免交换,避免不必要的副本,并且根本不需要调整片段着色器。

It turns out that Bitmap#copyPixelsFromBuffer() wants the pixels in RGBA order, not ARGB order. The values coming out of glReadPixels() are already in the right format. So by doing it this way I avoid the swap, avoid an unnecessary copy, and don't need to tweak the fragment shader at all.

这篇关于我们如何才能使ExtractMpegFramesTest中的saveFrame()方法更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆