the header analysis adts aac

        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-833878f763.css">
                                    <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-833878f763.css">
            <div class="htmledit_views" id="content_views">
                                        <h2 style="margin-left:0px;"><a name="t0"></a><a name="t0"></a>&nbsp;</h2>

 

遵循: BY-SA

Attribution - Share Alike 4.0 protocol

 

Author: Tan Dong

Time: October 28, 2016

Environment: Windows 7

 

 

ADTS is short for Audio Data Transport Stream.

 

AAC audio files are common transmission format.

Sometimes when you bare AAC encoded streams, encounter written AAC files can not be played on PC and mobile phones, it is much more likely that each frame AAC files in the lack of packaging splice ADTS header file . Only need to add header files ADTS can be. AAC an original data block length is variable, the ADTS header of original frame plus a package for ADTS, ADTS frame is formed.

 

The following analysis of the ADTS header information and file structure:

Each frame of AAC audio file by ADTS Header AAC Audio Data and composition.

 

 

You can use the AAC Audio ES Viewer utility to view the AAC ADTS Header.

 

ADTS header of each frame of an audio file contains the sample rate, channel, frame length information, so that the decoder to parse read.

ADTS header information in normal circumstances are 7 bytes, divided into two parts:

adts_fixed_header();

adts_variable_header();

First look: adts_fixed_header ();

 

  • syncword: Always 0xFFF , representative of a start ADTS frame for synchronization.
    The decoder can 0xFFF determine the start position of each of the ADTS.
    Because of its presence, decoding may start anywhere in the stream, i.e., the frame may be any decoding.
  • ID:MPEG Version: 0 for MPEG-4,1 for MPEG-2
  • Layer:always: '00'
  • protection_absent:Warning, set to 1 if there is no CRC and 0 if there is CRC
  • profile:表示使用哪个级别的AAC,如01 Low Complexity(LC) -- AAC LC
    profile的值等于 Audio Object Type的值减1.
    profile = MPEG-4 Audio Object Type - 1
  • sampling_frequency_index:采样率的下标
  • channel_configuration:声道数,比如2表示立体声双声道

接下来看下adts_variable_header();

  • aac_frame_length:一个ADTS帧的长度包括ADTS头和AAC原始流。frame length, this value must include 7 or 9 bytes of header length:
    aac_frame_length = (protection_absent == 1 ? 7 : 9) + size(AACFrame)

    protection_absent=0时, header length=9bytes
    protection_absent=1时, header length=7bytes
  • adts_buffer_fullness:0x7FF 说明是码率可变的码流。
  • number_of_raw_data_blocks_in_frame:表示ADTS帧中有number_of_raw_data_blocks_in_frame + 1个AAC原始帧。
    所以说number_of_raw_data_blocks_in_frame == 0 表示说ADTS帧中有一个AAC数据块。
    (一个AAC原始帧包含一段时间内1024个采样及相关数据)

 

两个头文件就讲到这里。

 

这里给出JAVA和Android里的给AAC添加ADTS头文件的部分方法代码,针对ByteBuffer。

 


  
  
  1. /**
  2. * 添加ADTS头
  3. *
  4. * @param packet
  5. * @param packetLen
  6. */
  7. private void addADTStoPacket(byte[] packet, int packetLen) {
  8. int profile = 2; // AAC LC
  9. int freqIdx = 4; // 44.1KHz
  10. int chanCfg = 2; // CPE
  11. // fill in ADTS data
  12. packet[ 0] = ( byte) 0xFF;
  13. packet[ 1] = ( byte) 0xF9;
  14. packet[ 2] = ( byte) (((profile - 1) << 6) + (freqIdx << 2) + (chanCfg >> 2));
  15. packet[ 3] = ( byte) (((chanCfg & 3) << 6) + (packetLen >> 11));
  16. packet[ 4] = ( byte) ((packetLen & 0x7FF) >> 3);
  17. packet[ 5] = ( byte) (((packetLen & 7) << 5) + 0x1F);
  18. packet[ 6] = ( byte) 0xFC;
  19. }


这里是调用:(可根据自己需求,这里的AudioBufFrame是我们自己项目里的类,其实就是个ByteBuffer封装的类)


  
  
  1. AudioBufFrame audioBufFrame = (AudioBufFrame) o;
  2. int outBufferSize = audioBufFrame.buf.limit() + 7;
  3. byte[] aacBytes = new byte[outBufferSize];
  4. try {
  5. if (!write) {
  6. fout = new FileOutputStream(MyConfiguration.VIDEO_PATH + "/audio.aac");
  7. write = true;
  8. }
  9. addADTStoPacket(aacBytes, outBufferSize);
  10. audioBufFrame.buf.get(aacBytes, 7, audioBufFrame.buf.limit());
  11. fout.write(aacBytes);
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }

 

下面是FFMPEG参考的C的添加ADTS头文件代码,供大家参考。


  
  
  1. int ff_adts_write_frame_header(ADTSContext *ctx,
  2. uint8_t *buf, int size, int pce_size)
  3. {
  4. PutBitContext pb;
  5. init_put_bits(&pb, buf, ADTS_HEADER_SIZE);
  6. /* adts_fixed_header */
  7. put_bits(&pb, 12, 0xfff); /* syncword */
  8. put_bits(&pb, 1, 0); /* ID */
  9. put_bits(&pb, 2, 0); /* layer */
  10. put_bits(&pb, 1, 1); /* protection_absent */
  11. put_bits(&pb, 2, ctx->objecttype); /* profile_objecttype */
  12. put_bits(&pb, 4, ctx->sample_rate_index);
  13. put_bits(&pb, 1, 0); /* private_bit */
  14. put_bits(&pb, 3, ctx->channel_conf); /* channel_configuration */
  15. put_bits(&pb, 1, 0); /* original_copy */
  16. put_bits(&pb, 1, 0); /* home */
  17. /* adts_variable_header */
  18. put_bits(&pb, 1, 0); /* copyright_identification_bit */
  19. put_bits(&pb, 1, 0); /* copyright_identification_start */
  20. put_bits(&pb, 13, ADTS_HEADER_SIZE + size + pce_size); /* aac_frame_length */
  21. put_bits(&pb, 11, 0x7ff); /* adts_buffer_fullness */
  22. put_bits(&pb, 2, 0); /* number_of_raw_data_blocks_in_frame */
  23. flush_put_bits(&pb);
  24. return 0;
  25. }



 

遵循:BY-NC-SA

署名-非商业性使用-相同方式共享 4.0协议

署名-非商业性使用-相同方式共享

                        <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">8</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/jay100500">
                    <img src="https://profile.csdnimg.cn/D/5/2/3_jay100500" class="avatar_pic" username="jay100500">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/9.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/jay100500" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">范特西_jay</a></span>
                                                    <span class="flag expert">
                                <a href="https://blog.csdn.net/home/help.html#classicfication" target="_blank">
                                    <svg class="icon" aria-hidden="true">
                                        <use xlink:href="#csdnc-blogexpert"></use>
                                    </svg>
                                    博客专家
                                </a>
                            </span>
                                            </div>
                    <div class="text"><span>发布了111 篇原创文章</span> · <span>获赞 146</span> · <span>访问量 37万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://im.csdn.net/im/main.html?userName=jay100500" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
    
发布了6 篇原创文章 · 获赞 0 · 访问量 327

Guess you like

Origin blog.csdn.net/qq_42602394/article/details/104472967