This document uses the h264 video stream as an example to decode the stream data.

Source: Internet
Author: User

This document uses the h264 video stream as an example to decode the stream data.

To highlight this point, this article only focuses on decoding video stream data and does not involve anything else (such as the configuration of the development environment ). If you need this information, please contact me.

  • Prepare Variables

Define avcodeccontext. If you use a class, you can define it as a class member. It is defined as a global variable.

Static avcodeccontext * g_pcodecctx = NULL;

Defines an avframe. avframe describes a multimedia frame. The decoded data will be placed in it.

Static avframe * g_pavfframe = NULL;

  • Initialize Decoder

Initialize your decoder. I include the entire initialization process in a function. Unless you have a better idea, I suggest you do the same. The function looks like this:

Bool h1__init ()

{

...

}

To initialize libavcodec, the following must be called:

Avcodec_init ();

Attach all the codes. Maybe only one h264 codec can be mounted. I have never tried:

Av_register_all ();

Obtain the h264 decoder:

Avcodec * pcodec = avcodec_find_decoder (codec_id_h264 );

Create an avcodeccontext and use the default value for initialization:

G_pcodecctx = avcodec_alloc_context ();

Change the values of some member variables of g_pcodecctx. You should obtain these variable values from the decoder:

G_pcodecctx-> time_base.num = 1; // The two rows: 25 frames per second

G_pcodecctx-> time_base.den = 25;

G_pcodecctx-> bit_rate = 0; // Initialization is 0

G_pcodecctx-> frame_number = 1; // one video frame per packet

G_pcodecctx-> codec_type = codec_type_video;

G_pcodecctx-> width = 704; // the width and height of the video.

G_pcodecctx-> Height = 576;

Open codec. If it is enabled successfully, assign avframe:

If (avcodec_open (g_pcodecctx, pcodec)> = 0)

{

G_pavfframe = avcodec_alloc_frame (); // allocate Video Frame

}

List the complete code for initializing the decoder Library:

  • Decoding

If you only need to calculate the YUV 420i data, you only need to call it once:

Avcodec_decode_video (g_pcodecctx, g_pavfframe, (int *) & ngot, (unsigned
_ Int8 *) psrcdata, dwdatalen );

Here, ngot is used to return whether the decoding is successful. After avcodec_decode_video is called, if ngot is not equal to 0, the decoding is successful. Otherwise, the video frame is not decoded.

Psrcdata is a data stream encoded in h264 to be decrypted. dwdatalen indicates the length of the data stream in bytes.

The decoded video frame (YUV data) is saved into g_pavfframe, g_pavfframe-> data [0],
G_pavfframe-> data [1], g_pavfframe-> data [2] is YUV data. The following sample code puts the YUV data under one
In the block memory, the arrangement is as follows:

YY

YY

U

V

This function has a return value: If the decoding is successful, the number of bitstream bytes used for this decoding is returned; otherwise, 0 is returned. For simplicity, I suppose psrcdata only contains one video frame.

Similarly, out of the modular requirements and the convenience of code maintenance, I included the decoding action in a function:

Bool h1__decode (const pbyte psrcdata, const DWORD dwdatalen, pbyte pdedata,
Int * pnwidth, int * pnheight)

Psrcdata-data to be decoded

Dwdatalen-number of data bytes to be decoded

Pdedata-used to return decoded YUV data

Pnwidth, pnheight-used to return the video length and width

The complete code is listed below:

  • Release Decoder

The above has actually completed the task of this article, but from a responsible point of view, we must start and end well.

There is nothing to say about the release process. Similarly, I wrapped them in a function:

(Sorry, the article was originally written in word. The code block is a text box, but it turns into an image after being pasted here .)

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.