github.com/cnotch/ipchub@v1.1.0/av/codec/aac/adtsheader.go (about) 1 // Copyright (c) 2019,CAOHONGJU All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package aac 6 7 // ADTSHeader adts header include fixed and varlable header 8 type ADTSHeader [7]byte 9 10 func NewADTSHeader(profile, sampleRateIdx, channelConfig byte, payloadSize int) ADTSHeader { 11 // AAC-ADTS 12 // 6.2 Audio Data Transport Stream, ADTS 13 // in aac-iso-13818-7.pdf, page 26. 14 // fixed 7bytes header 15 adtsHeader := ADTSHeader{0xff, 0xf1, 0x00, 0x00, 0x00, 0x0f, 0xfc} 16 // the frame length is the AAC raw data plus the adts header size. 17 frameLen := payloadSize + 7 18 19 // adts_fixed_header 20 // 2B, 16bits 21 // int16_t syncword; //12bits, '1111 1111 1111' 22 // int8_t ID; //1bit, '0' 23 // int8_t layer; //2bits, '00' 24 // int8_t protection_absent; //1bit, can be '1' 25 26 // 12bits 27 // int8_t profile; //2bit, 7.1 Profiles, page 40 28 // TSAacSampleFrequency sampling_frequency_index; //4bits, Table 35, page 46 29 // int8_t private_bit; //1bit, can be '0' 30 // int8_t channel_configuration; //3bits, Table 8 31 // int8_t original_or_copy; //1bit, can be '0' 32 // int8_t home; //1bit, can be '0' 33 34 // adts_variable_header 35 // 28bits 36 // int8_t copyright_identification_bit; //1bit, can be '0' 37 // int8_t copyright_identification_start; //1bit, can be '0' 38 // int16_t frame_length; //13bits 39 // int16_t adts_buffer_fullness; //11bits, 7FF signals that the bitstream is a variable rate bitstream. 40 // int8_t number_of_raw_data_blocks_in_frame; //2bits, 0 indicating 1 raw_data_block() 41 42 // profile, 2bits 43 adtsHeader[2] = (profile << 6) & 0xc0 44 // sampling_frequency_index 4bits 45 adtsHeader[2] |= (sampleRateIdx << 2) & 0x3c 46 // channel_configuration 3bits 47 adtsHeader[2] |= (channelConfig >> 2) & 0x01 48 adtsHeader[3] = (channelConfig << 6) & 0xc0 49 // frame_length 13bits 50 adtsHeader[3] |= uint8((frameLen >> 11) & 0x03) 51 adtsHeader[4] = uint8((frameLen >> 3) & 0xff) 52 adtsHeader[5] = uint8((frameLen << 5) & 0xe0) 53 // adts_buffer_fullness; //11bits 54 adtsHeader[5] |= 0x1f 55 56 return adtsHeader 57 } 58 59 func (h ADTSHeader) Profile() uint8 { 60 return (h[2] >> 6) 61 } 62 63 func (h ADTSHeader) SamplingIndex() uint8 { 64 return h[2] >> 2 & 0xf 65 } 66 67 func (h ADTSHeader) SampleRate() int { 68 return SampleRates[int(h.SamplingIndex())] 69 } 70 71 func (h ADTSHeader) ChannelConfig() uint8 { 72 return (h[2]&0x1)<<2 | h[3]>>6 73 } 74 75 func (h ADTSHeader) Channels() uint8 { 76 return aacAudioChannels[int(h.ChannelConfig())] 77 } 78 79 func (h ADTSHeader) FrameLength() int { 80 return int((uint32(h[3]&0x3) << 11) | 81 (uint32(h[4]) << 3) | 82 uint32((h[5]>>5)&0x7)) 83 } 84 85 func (h ADTSHeader) PayloadSize() int { 86 return h.FrameLength() - len(h) 87 } 88 89 func (h ADTSHeader) ToAsc() []byte { 90 return Encode2BytesASC(h.Profile() + 1,h.SamplingIndex(),h.ChannelConfig()) 91 }