且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-12-25 00:52:50

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

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.