How can we make the saveFrame() method in ExtractMpegFramesTest more efficient?

前端 未结 2 1854
轮回少年
轮回少年 2021-01-07 05:32

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

In ExtractMpegFramesTest_egl14.java.txt, method saveFrame(), there is a loop for re

相关标签:
2条回答
  • 2021-01-07 06:08

    It turns out there's an even faster approach.

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

    to this:

    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.

    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.

    0 讨论(0)
  • 2021-01-07 06:13

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

    I wanted to suggest that this conversion can happen in the FragmentShader by changing the line

    gl_FragColor = texture2D(sTexture, vTextureCoord);
    

    into

    gl_FragColor = texture2D(sTexture, vTextureCoord).argb;
    

    which is an efficient shortcut to reorder in GPU the shader's output channels, that works in other ways too: .abgr or even .bggr etc.

    0 讨论(0)
提交回复
热议问题