Analysis of key techniques in hardware decoding

Source: Internet
Author: User

In the previous article, we used ffmpeg to separate the audio and video data from a multimedia container, but it is possible that the data could not be decoded correctly. Why is it? Because the decoder needs to be configured before decoding the data, it is typical of the current popular HD coded "Golden Partner" combination H264 + AAC . This article will describe the key decoding configuration parameters of H264 and AAC, and without these configuration information, the data frames are often incomplete, causing the decoder to not decode.

    • h264 configuration information parsing

      Front we know, ffmpeg< Span style= "padding:0px; margin:0px; Font-family: The avformat_find_stream_info avcodec.h

Avcodeccontext is defined as follows:

if the video stream is H264, this extradate contains the configuration information of H264, the extension data is defined as follows:

For a detailed explanation, refer to" iso-14496-15 AVC file format" documentation. The most important thing isNALlength andSps,Ppsdata and the corresponding length information. The parsing of this data isFfmpegThere are ready-made functions:Ff_h264_decode_extradata, in my project is to write their own extended data parsing.

  • aac configuration information parsing and setting

    If the audio data is Aac stream, in decoding need Adts The head, whether it is container encapsulation or streaming media, without this, is generally not playable. Many friends are doing AAC The stream is playing with no sound, which is probably the cause.

    ADTS adts header information, add to each frame AAC data before sending the decoder, so that it can be decoded normally.

    The extradate data is defined as follows:

        For more information and instructions, refer to " iso-iec-14496-3 (Audio) " Audiospecificconfig aac configuration parameters required by the decoder.

    This data also has the corresponding decoding functionin the ffmpeg: Avpriv_aac_parse_header. In my project, I did not use this function, but I implemented it myself:

  • typedef struct{      int Write_adts;      int objecttype;      int sample_rate_index;      int channel_conf;} Adtscontext;

    int Aac_decode_extradata (Adtscontext *adts, unsigned char *pbuf, int bufsize) {int AOT, aotext, Samfreindex;      int I, channelconfig;         unsigned char *p = pbuf;      if (!adts | |!pbuf | | bufsize<2) {return-1;      } AOT = (p[0]>>3) &0x1f; if (AOT = =) {Aotext = (p[0]<<3 | (p[1]>>5))            & 0x3f;            AOT = + Aotext;                         Samfreindex = (p[1]>>1) & 0x0f; if (Samfreindex = = 0x0f) {Channelconfig = ((p[4]<<3) | (p[5]>>5))            & 0x0f; } else {channelconfig = ((p[1]<<3) | (            p[2]>>5)) & 0x0f;            }} else {Samfreindex = ((p[0]<<1) |p[1]>>7) & 0x0f;            if (Samfreindex = = 0x0f) {channelconfig = (p[4]>>3) & 0x0f;               } else {   Channelconfig = (p[1]>>3) & 0x0f;      }} #ifdef Aot_profile_ctrl if (AOT < 2) AOT = 2; #endif adts->objecttype = aot-1;      Adts->sample_rate_index = Samfreindex;      adts->channel_conf = Channelconfig;         Adts->write_adts = 1; return 0;}

    the pbuf above is extradata.

    Next, use the Adtscontext data encoding to insert the ADTS header information in front of each AAC frame:


  • int Aac_set_adts_head (Adtscontext *acfg, unsigned char *buf, int size) {unsigned char byte;      if (Size < adts_header_size) {return-1;      } buf[0] = 0xFF;      BUF[1] = 0xf1;      byte = 0;      Byte |= (Acfg->objecttype & 0x03) << 6;      Byte |= (Acfg->sample_rate_index & 0x0f) << 2;      Byte |= (acfg->channel_conf & 0x07) >> 2;      BUF[2] = byte;      byte = 0;      Byte |= (acfg->channel_conf & 0x07) << 6;      Byte |= (adts_header_size + SIZE) >> 11;      BUF[3] = byte;      byte = 0;      Byte |= (adts_header_size + SIZE) >> 3;      BUF[4] = byte;      byte = 0;      Byte |= ((Adts_header_size + SIZE) & 0x7) << 5;      Byte |= (0x7ff >> 6) & 0x1f;      BUF[5] = byte;      byte = 0;      Byte |= (0x7ff & 0x3f) << 2;         BUF[6] = byte; return 0;}

    the head is a fixed 7- byte length, so the 7 bytes can be vacated in advance for ADTS occupancy.

    through the above to H264 and AAC Extended Data processing, playing a variety of "gold partner" multimedia files, streaming media, video-on-demand, etc. should be no problem.

      want to get more original articles in the first time, please pay attention to the personal public platform: programmer interaction Alliance (coder_online) can be concerned, there are a lot of it in a large number of "" " padding:0px; margin:0px; Font-family: Song Body ", chromium linux and other related articles waiting for you, we can also communicate online.

    Excerpt from: http://my.oschina.net/u/2336532/blog/400790


Analysis of key techniques in hardware 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.