I aim to use the Android MediaCodec for decoding a video stream, then use the output images for further image processing in native code.
Platform: ASUS tf700t android 4.
If you configure an output Surface, the decoded data is written to a graphic buffer that can be used as an OpenGL ES texture (via the "external texture" extension). The various bits of hardware get to hand data around in a format they like, and the CPU doesn't have to copy the data.
If you don't configure a Surface, the output goes into a java.nio.ByteBuffer
. There's at least one buffer copy to get the data from the MediaCodec-allocated buffer to your ByteByffer
, and presumably another copy to get the data back out into your JNI code. I expect what you're seeing is the overhead cost rather than software decoding cost.
You might be able to improve matters by sending the output to a SurfaceTexture
, rending into an FBO or pbuffer, and then using glReadPixels
to extract the data. If you read into a "direct" ByteBuffer
or call glReadPixels
from native code, you reduce your JNI overhead. The down side to this approach is that your data will be in RGB rather than YCbCr. (OTOH, if your desired transformations can be expressed in a GLES 2.0 fragment shader, you can get the GPU to do the work instead of the CPU.)
As noted in another answer, the decoders on different devices output ByteBuffer
data in different formats, so interpreting the data in software may not be viable if portability is important to you.
Edit: Grafika now has an example of using the GPU to do image processing. You can see a demo video here.