Key technology of decryption 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.

    • Configuration information parsing for H264

      As we know earlier, FFmpeg's avformat_find_stream_info function can obtain many kinds of audio and video media, such as playback duration, audio and video compression format, audio track information, subtitle information, frame rate, sampling rate and so on. In the information results there is an extended data description (in the avcodec.h file):

Avcodeccontext is defined as follows:

If the video stream is H264, the extradate contains H264 configuration information, which is defined as follows:

You can refer to the "iso-14496-15 AVC file Format" document for detailed explanations. The most important is the NAL length and sps,pps data and the corresponding length information. The parsing of this data has a ready-made function in FFmpeg: Ff_h264_decode_extradata, in my project, I write the extended data parsing.

  • AAC configuration information parsing and setting

    If the audio data is an AAC stream, it needs to be Adts (audio data Transport stream) head when decoding, whether it is container encapsulation or streaming media, without which it is generally not playable. A lot of friends in the AAC stream play when the sound is not broadcast, it is probably the cause. The data required for

    Adts is still placed in the extended data extradata above, we need to decode the extension data, and then re-encapsulated from the decoded data information into the ADTS header information, added 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 please refer to "iso-iec-14496-3 (Audio)" Part of the Audiospecificconfig. The most important part of this is the sampling frequency, channel configuration, and audio object type, which are generally the configuration parameters required by the AAC decoder.

        This data also has a corresponding decoding function in FFmpeg: Avpriv_aac_parse_header. In my project, I did not use this function, but I implemented it myself:

  • 1234567 typedefstruct{      int write_adts;      int objecttype;      int sample_rate_index;      intchannel_conf;}ADTSContext;

      

    12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 intaac_decode_extradata(ADTSContext *adts, unsigned char*pbuf, intbufsize){      intaot, aotext, samfreindex;      int i, channelconfig;      unsigned char*p = pbuf;         if(!adts || !pbuf || bufsize<2)      {            return -1;      }      aot = (p[0]>>3)&0x1f;      if(aot == 31)      {            aotext = (p[0]<<3 | (p[1]>>5)) & 0x3f;            aot = 32 + 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;         return0;}

      

    The pbuf above is extradata.

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

  • 123456789101112131415161718192021222324252627282930313233 intaac_set_adts_head(ADTSContext *acfg, unsigned char*buf, intsize){            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;         return0;}

      

    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, play all kinds 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), sweep the QR code below or search number Coder_online can pay attention to, there are a lot of android,chromium, Linux and other related articles waiting for you, we can also communicate online.

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

Key technology of decryption 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.