Android platform for H264 video hard decoding

Source: Internet
Author: User

This article describes how to use the Android standard API (MEDIACODEC) to implement H264 hardware decoding.

Originally, we used the hardware decoding interface provided by the JNI call platform to get the YUV frame, and then put it into OpenGL script to deal with the rendering. But after changing the new platform, did not get the bottom interface, so these two days to find on the Android H264 decoding scheme. The day before yesterday in the friend's prompt to find Mediacodec This class, Android developer above has mediacodec description and usage, still calculate detailed can slowly grope. But the use of this class on the Internet is relatively small.

So here's a code to introduce.

1 //Video Constants2     Private Final StaticString Mime_type = "VIDEO/AVC";//h . Advanced Video3     Private Final Static intVideo_width = 1280;4     Private Final Static intVideo_height = 720;5     Private Final Static intTime_internal = 30;6 7      Public voidInitdecoder () {8 9Mcodec =Mediacodec.createdecoderbytype (mime_type);TenMediaformat Mediaformat =Mediaformat.createvideoformat (Mime_type, One video_width, video_height); A mcodec.configure (Mediaformat, Msurfaceview.getholder (). Getsurface (), -                 NULL, 0); - Mcodec.start (); the}

This is the initialization decoder operation, specifically to set the decoding type, height, width, and a surface to display the video.

1      Public BooleanOnframe (byte[] buf,intOffsetintlength) {2LOG.E ("Media", "Onframe start");3LOG.E ("Media", "Onframe Thread:" +Thread.CurrentThread (). GetId ());4         //Get Input Buffer index5bytebuffer[] Inputbuffers =mcodec.getinputbuffers ();6         intInputbufferindex = Mcodec.dequeueinputbuffer (100);7 8LOG.E ("Media", "Onframe index:" +inputbufferindex);9         if(Inputbufferindex >= 0) {TenBytebuffer InputBuffer =Inputbuffers[inputbufferindex]; One inputbuffer.clear (); A inputbuffer.put (buf, offset, length); -Mcodec.queueinputbuffer (inputbufferindex, 0, length, MCount -* time_internal, 0); themcount++; -}Else { -             return false; -         } +  -         //Get Output Buffer index +Mediacodec.bufferinfo Bufferinfo =Newmediacodec.bufferinfo (); A         intOutputbufferindex = Mcodec.dequeueoutputbuffer (bufferinfo, 100); at          while(Outputbufferindex >= 0) { -Mcodec.releaseoutputbuffer (Outputbufferindex,true); -Outputbufferindex = Mcodec.dequeueoutputbuffer (bufferinfo, 0); -         } -LOG.E ("Media", "Onframe End"); -         return true; in}
Specific process:

1. Get an index of available inputbuffer (dequeue)

2. Place a frame of data into InputBuffer

3. Decode the InputBuffer into row

4. Get an index of OutputBuffer (dequeue)

5. Release OutputBuffer

6. In 4, 5 cycles until no outputbuffer can be listed as a stop

Here the decoder has more than one input buffer (I tested there are 3), implemented without Kaka.

Common problems:

Dequeueinputbuffer and Dequeueoutputbuffer often get buffers (related to the performance of the machine), and if the parameter is-1, they wait, and if the argument is positive, it waits for the corresponding microsecond and returns-1 if no buffer is available.

Conclusion:
The test program reads a H264 bare stream file, identifies each frame and then puts it into the decoder decoding and rendering. Read and recognize the code in the demo. After testing, the decoding effect of different platforms is quite large. I'm playing a 720p H264 file. The best is the RK (Rui Micro) platform, the buffer gets very few failures, but because of overheating restart the machine, high-pass platform to test several machines (hammers, millet 4, as well as individual custom machines), often do not get the buffer situation. MTK only one (Tcl, the DA), the direct initialization of the decoder can not ...
This is related to GPU performance, we are the solution is 720p8m bitrate video, replaced by lower bit rate lower resolution, most of the machine can still operate normally (except for the DA). Of course, some platforms may be implemented as soft decoding (using the CPU,FFMPEG scheme), which I have not yet encountered.

The demo link is as follows (note the path to be placed on the bare stream file):
Http://files.cnblogs.com/files/superping/H264Demo.zip

Android platform for H264 video hard decoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.