我们如何才能使ExtractMpegFramesTest中的saveFrame()方法更有效?

[编辑]在fadden @ suggestion之后重新格式化为问题和答案格式。

在ExtractMpegFramesTest_egl14.java.txt方法saveFrame()中,有一个循环用于将RGBA重新排序为ARGB以进行位图png压缩(参见下面的文件引用),如何对其进行优化?

// glReadPixels gives us a ByteBuffer filled with what is essentially big-endian RGBA // data (ie 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 > 16) | ((c & 0x000000ff) << 16); } 

事实certificate,这是一种更快的方法。

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

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

我一做到这一点,我的所有颜色都错了。

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

[编辑]在fadden @ suggestion之后重新格式化为问题和答案格式

我想建议通过更改行可以在FragmentShader中进行此转换

 gl_FragColor = texture2D(sTexture, vTextureCoord); 

 gl_FragColor = texture2D(sTexture, vTextureCoord).argb; 

这是一个有效的快捷方式,可以在GPU中重新排序着色器的输出通道,也可以通过其他方式工作: .abgr甚至.bggr等。