【问题标题】:How can we make the saveFrame() method in ExtractMpegFramesTest more efficient?我们如何才能使 ExtractMpegFramesTest 中的 saveFrame() 方法更高效?
【发布时间】:2014-02-07 17:46:54
【问题描述】:

[edit] 按照 fadden@ 的建议重新格式化为问答格式。

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

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

【问题讨论】:

  • 好主意!这个交换循环在 Java 中并不是特别快。
  • FWIW,我认为提供建议的“官方”方式是问,“我们如何才能使 ExtractMpegFramesTest 中的 saveFrame() 方法更有效?”,然后回答您自己的问题。这样一来,其他人也被鼓励提出自己的答案。见stackoverflow.com/help/self-answer
  • 重新格式化,谢谢fadden!

标签: java android android-mediacodec


【解决方案1】:

事实证明还有一种更快的方法。

使用@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);

当我这样做时,我所有的颜色都是错误的。

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

【讨论】:

    【解决方案2】:

    [编辑] 按照 fadden@ 建议重新格式化为问答格式

    我想建议这种转换可以通过更改行在 FragmentShader 中发生

    gl_FragColor = texture2D(sTexture, vTextureCoord);
    

    进入

    gl_FragColor = texture2D(sTexture, vTextureCoord).argb;
    

    这是在 GPU 中重新排序着色器输出通道的有效快捷方式,它也适用于其他方式:.abgr 甚至 .bggr 等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 2022-12-18
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 2016-10-18
      • 1970-01-01
      相关资源
      最近更新 更多