在
Android上,我试图在相机帧上执行一些OpenGL处理,在相机预览中显示这些帧,然后在视频文件中对帧进行编码.我正在尝试使用OpenGL,使用GLSurfaceView和GLSurfaceView.Renderer以及使用FFMPEG进行视频编码.
我已经使用着色器成功处理了图像帧.现在我需要将处理过的帧编码为视频. GLSurfaceView.Renderer提供onDrawFrame(GL10 ..)方法.在这种方法中,我试图仅使用glreadPixels()读取图像帧,然后将帧放在队列上以便编码为视频.就它而言,glreadPixels()太慢了 – 我的帧速率是单位数.我正在尝试使用像素缓冲对象来加快速度.这不起作用.插入pbo后,帧速率不变.这是我第一次使用OpenGL,我不知道从哪里开始寻找问题.我这样做了吗?谁能给我一些方向?提前致谢.
public class MainRenderer implements GLSurfaceView.Renderer,SurfaceTexture.OnFrameAvailableListener {
.
.
public void onDrawFrame ( GL10 gl10 ) {
//Create a buffer to hold the image frame
ByteBuffer byte_buffer = ByteBuffer.allocateDirect(this.width * this.height * 4);
byte_buffer.order(ByteOrder.nativeOrder());
//Generate a pointer to the frame buffers
IntBuffer image_buffers = IntBuffer.allocate(1);
GLES20.glGenBuffers(1,image_buffers);
//Create the buffer
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,image_buffers.get(0));
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER,byte_buffer.limit(),byte_buffer,GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,image_buffers.get(0));
//Read the pixel data into the buffer
gl10.glreadPixels(0,this.width,this.height,GL10.GL_RGBA,GL10.GL_UNSIGNED_BYTE,byte_buffer);
//encode the frame to video
enQueueForEncoding(byte_buffer);
//unbind the buffer
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER,0);
}
.
.
}
解决方法
我记得glBufferData()没有将你的内部缓冲区映射到GPU内存,它只是将内存中的数据复制到缓冲区中(初始化).
要访问由glBufferData()分配的内存,您应该使用glMapBufferRange().该函数返回一个可以读取的Java Buffer对象.