mediacodec:h264 Hard decode Core code summary __ Streaming Media

Source: Internet
Author: User

The decoding process is divided into two processes:

1. Get the H264 view screen flow, keep abreast of the video streaming into the decoder in the buffer

2. Obtain the buffer of the decoder and obtain the decoded data in the buffer and set it to the Surfaceview for display

Display interface:

Msurfaceview = (Surfaceview) Findviewbyid (R.ID.SV);
Msurfaceholder = Msurfaceview.getholder ();
Msurfaceholder.addcallback (this);

Open the loop thread in the surfacecreated callback that reads the decoded data in 2.

public class Harddecodethread extends thread{@Override public void Run () {try {Mmediacodec = Mediacodec.crea
			Tedecoderbytype ("VIDEO/AVC");
			Mediaformat format = Mediaformat.createvideoformat ("VIDEO/AVC", 1280, 720);
			Mmediacodec.configure (format, msurfaceholder.getsurface (), null, 0);
			Mmediacodec.start ()//Pay attention to open the code, or the following will be an error Showlog ("Start get output Veido info");
				while (isplay) {bufferinfo bufinfo = new Bufferinfo ();
				int outputindex = 0; while ((Outputindex = Mmediacodec.dequeueoutputbuffer (bufinfo,20 * 1000)) >= 0) {mmediacodec.releaseoutputbuffer (o
				Utputindex, True);
				} if (Outputindex = = mediacodec.info_output_buffers_changed) {showlog ("info_output_buffers_changed");
				}else if (Outputindex = = mediacodec.info_output_format_changed) {showlog ("info_output_format_changed");
				} if (Mmediacodec!= null) {mmediacodec.stop ();
				Mmediacodec.release ();
			Mmediacodec = null; catch (Exceptione) {e.printstacktrace ();
		Showlog ("Not supported hard coded _error:" + e.getmessage ()); }

Perform 1 parts: continuously obtain H264 code stream for decoding

if (Mmediacodec!= null) {
	bytebuffer[] buffers = Mmediacodec.getinputbuffers ();
	int inputbufferindex = mmediacodec.dequeueinputbuffer (* 1000);
	Showlog ("Inputbufferindex:" + inputbufferindex);
	if (inputbufferindex >= 0) {
		bytebuffer buffer = Buffers[inputbufferindex];
		Buffer.clear ();
		Buffer.put (data);
		Mmediacodec.queueinputbuffer (inputbufferindex, 0, data.length, 0, 0);//data byte array representing H264

Related Article

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: 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.