Firefly Open Source Community
Title: Android hardware encoding and decoding [Print This Page]
Author: Andy Time: 12/12/2014 11:41
Title: Android hardware encoding and decoding
Android hardware encoding mainly realized by MediaCodec.
The official documentation of Android shows as below.
- MediaCodec codec = MediaCodec.createDecoderByType(type);
- codec.configure(format, ...);
- codec.start();
- ByteBuffer[] inputBuffers = codec.getInputBuffers();
- ByteBuffer[] outputBuffers = codec.getOutputBuffers();
- for (;;) {
- int inputBufferIndex = codec.dequeueInputBuffer(timeoutUs);
- if (inputBufferIndex >= 0) {
- // fill inputBuffers[inputBufferIndex] with valid data
- ...
- codec.queueInputBuffer(inputBufferIndex, ...);
- }
-
- int outputBufferIndex = codec.dequeueOutputBuffer(timeoutUs);
- if (outputBufferIndex >= 0) {
- // outputBuffer is ready to be processed or rendered.
- ...
- codec.releaseOutputBuffer(outputBufferIndex, ...);
- } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
- outputBuffers = codec.getOutputBuffers();
- } else if (outputBufferIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
- // Subsequent data will conform to new format.
- MediaFormat format = codec.getOutputFormat();
- ...
- }
- }
- codec.stop();
- codec.release();
- codec = null;
Copy the code
We made a demo using MdeiaCodec to realize the hardware encoding and decoding of camera.
This is the GUI of the demo.
The GUI is simple£¬the preview of camera located at the lower right corner.
The window on the middle is the picture after decoding.
- private void doCodec(byte[] data){
- int et = mEncoder.encode(data , 0 , data.length, buffer1, 0);
- Log.d("buncodec","encode et="+et);
-
- int dt = mDecoder.decode(buffer1, 0 , et , buffer2 , 0);
- Log.d("buncodec","decode dt="+dt);
-
-
- // The data of NV21 showed by YUV Image, efficient less, just a demo.
- YuvImage image = new YuvImage(buffer2,
- getPreviewFormat(), getPreviewWidth(), getPreviewHeight(),
- null);
- setData(image,getPreviewWidth(),getPreviewHeight());
-
- image = null ;
- }
Copy the code
The information of logcat show as below:
From the information above, you can see the preview data length of 1280*720 pixels is 1280*720*3/2=1382400£»
Et stands for the length after encoding,it¡¯s within 50000.
Dt stands for the length after decoding,it¡¯s back to 1382400.
Author: ZZP Time: 12/13/2014 09:26
handsome boy
Welcome Firefly Open Source Community (https://bbs.t-firefly.com/) |
Powered by Discuz! X3.1 |