aac.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package codec
  2. import "errors"
  3. // Table 31 – Profiles
  4. // index profile
  5. // 0 Main profile
  6. // 1 Low Complexity profile (LC)
  7. // 2 Scalable Sampling Rate profile (SSR)
  8. // 3 (reserved)
  9. type AAC_PROFILE int
  10. const (
  11. MAIN AAC_PROFILE = iota
  12. LC
  13. SSR
  14. )
  15. type AAC_SAMPLING_FREQUENCY int
  16. const (
  17. AAC_SAMPLE_96000 AAC_SAMPLING_FREQUENCY = iota
  18. AAC_SAMPLE_88200
  19. AAC_SAMPLE_64000
  20. AAC_SAMPLE_48000
  21. AAC_SAMPLE_44100
  22. AAC_SAMPLE_32000
  23. AAC_SAMPLE_24000
  24. AAC_SAMPLE_22050
  25. AAC_SAMPLE_16000
  26. AAC_SAMPLE_11025
  27. AAC_SAMPLE_8000
  28. )
  29. var AAC_Sampling_Idx [11]int = [11]int{96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 11025, 8000}
  30. // Table 4 – Syntax of adts_sequence()
  31. // adts_sequence() {
  32. // while (nextbits() == syncword) {
  33. // adts_frame();
  34. // }
  35. // }
  36. // Table 5 – Syntax of adts_frame()
  37. // adts_frame() {
  38. // adts_fixed_header();
  39. // adts_variable_header();
  40. // if (number_of_raw_data_blocks_in_frame == 0) {
  41. // adts_error_check();
  42. // raw_data_block();
  43. // }
  44. // else {
  45. // adts_header_error_check();
  46. // for (i = 0; i <= number_of_raw_data_blocks_in_frame;i++ {
  47. // raw_data_block();
  48. // adts_raw_data_block_error_check();
  49. // }
  50. // }
  51. // }
  52. // adts_fixed_header()
  53. // {
  54. // syncword; 12 bslbf
  55. // ID; 1 bslbf
  56. // layer; 2 uimsbf
  57. // protection_absent; 1 bslbf
  58. // profile; 2 uimsbf
  59. // sampling_frequency_index; 4 uimsbf
  60. // private_bit; 1 bslbf
  61. // channel_configuration; 3 uimsbf
  62. // original/copy; 1 bslbf
  63. // home; 1 bslbf
  64. // }
  65. type ADTS_Fix_Header struct {
  66. ID uint8
  67. Layer uint8
  68. Protection_absent uint8
  69. Profile uint8
  70. Sampling_frequency_index uint8
  71. Private_bit uint8
  72. Channel_configuration uint8
  73. Originalorcopy uint8
  74. Home uint8
  75. }
  76. // adts_variable_header() {
  77. // copyright_identification_bit; 1 bslbf
  78. // copyright_identification_start; 1 bslbf
  79. // frame_length; 13 bslbf
  80. // adts_buffer_fullness; 11 bslbf
  81. // number_of_raw_data_blocks_in_frame; 2 uimsfb
  82. // }
  83. type ADTS_Variable_Header struct {
  84. Copyright_identification_bit uint8
  85. copyright_identification_start uint8
  86. Frame_length uint16
  87. Adts_buffer_fullness uint16
  88. Number_of_raw_data_blocks_in_frame uint8
  89. }
  90. type ADTS_Frame_Header struct {
  91. Fix_Header ADTS_Fix_Header
  92. Variable_Header ADTS_Variable_Header
  93. }
  94. func NewAdtsFrameHeader() *ADTS_Frame_Header {
  95. return &ADTS_Frame_Header{
  96. Fix_Header: ADTS_Fix_Header{
  97. ID: 0,
  98. Layer: 0,
  99. Protection_absent: 1,
  100. Profile: uint8(MAIN),
  101. Sampling_frequency_index: uint8(AAC_SAMPLE_44100),
  102. Private_bit: 0,
  103. Channel_configuration: 0,
  104. Originalorcopy: 0,
  105. Home: 0,
  106. },
  107. Variable_Header: ADTS_Variable_Header{
  108. copyright_identification_start: 0,
  109. Copyright_identification_bit: 0,
  110. Frame_length: 0,
  111. Adts_buffer_fullness: 0,
  112. Number_of_raw_data_blocks_in_frame: 0,
  113. },
  114. }
  115. }
  116. func (frame *ADTS_Frame_Header) Decode(aac []byte) error {
  117. _ = aac[6]
  118. frame.Fix_Header.ID = aac[1] >> 3
  119. frame.Fix_Header.Layer = aac[1] >> 1 & 0x03
  120. frame.Fix_Header.Protection_absent = aac[1] & 0x01
  121. frame.Fix_Header.Profile = aac[2] >> 6 & 0x03
  122. frame.Fix_Header.Sampling_frequency_index = aac[2] >> 2 & 0x0F
  123. frame.Fix_Header.Private_bit = aac[2] >> 1 & 0x01
  124. frame.Fix_Header.Channel_configuration = (aac[2] & 0x01 << 2) | (aac[3] >> 6)
  125. frame.Fix_Header.Originalorcopy = aac[3] >> 5 & 0x01
  126. frame.Fix_Header.Home = aac[3] >> 4 & 0x01
  127. frame.Variable_Header.Copyright_identification_bit = aac[3] >> 3 & 0x01
  128. frame.Variable_Header.copyright_identification_start = aac[3] >> 2 & 0x01
  129. frame.Variable_Header.Frame_length = (uint16(aac[3]&0x03) << 11) | (uint16(aac[4]) << 3) | (uint16(aac[5]>>5) & 0x07)
  130. frame.Variable_Header.Adts_buffer_fullness = (uint16(aac[5]&0x1F) << 6) | uint16(aac[6]>>2)
  131. frame.Variable_Header.Number_of_raw_data_blocks_in_frame = aac[6] & 0x03
  132. return nil
  133. }
  134. func (frame *ADTS_Frame_Header) Encode() []byte {
  135. var hdr []byte
  136. if frame.Fix_Header.Protection_absent == 1 {
  137. hdr = make([]byte, 7)
  138. } else {
  139. hdr = make([]byte, 9)
  140. }
  141. hdr[0] = 0xFF
  142. hdr[1] = 0xF0
  143. hdr[1] = hdr[1] | (frame.Fix_Header.ID << 3) | (frame.Fix_Header.Layer << 1) | frame.Fix_Header.Protection_absent
  144. hdr[2] = frame.Fix_Header.Profile<<6 | frame.Fix_Header.Sampling_frequency_index<<2 | frame.Fix_Header.Private_bit<<1 | frame.Fix_Header.Channel_configuration>>2
  145. hdr[3] = frame.Fix_Header.Channel_configuration<<6 | frame.Fix_Header.Originalorcopy<<5 | frame.Fix_Header.Home<<4
  146. hdr[3] = hdr[3] | frame.Variable_Header.copyright_identification_start<<3 | frame.Variable_Header.Copyright_identification_bit<<2 | byte(frame.Variable_Header.Frame_length<<11)
  147. hdr[4] = byte(frame.Variable_Header.Frame_length >> 3)
  148. hdr[5] = byte((frame.Variable_Header.Frame_length&0x07)<<5) | byte(frame.Variable_Header.Adts_buffer_fullness>>3)
  149. hdr[6] = byte(frame.Variable_Header.Adts_buffer_fullness&0x3F<<2) | frame.Variable_Header.Number_of_raw_data_blocks_in_frame
  150. return hdr
  151. }
  152. func SampleToAACSampleIndex(sampling int) int {
  153. for i, v := range AAC_Sampling_Idx {
  154. if v == sampling {
  155. return i
  156. }
  157. }
  158. panic("not Found AAC Sample Index")
  159. }
  160. func AACSampleIdxToSample(idx int) int {
  161. return AAC_Sampling_Idx[idx]
  162. }
  163. // +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  164. // | audio object type(5 bits) | sampling frequency index(4 bits) | channel configuration(4 bits) | GA framelength flag(1 bits) | GA Depends on core coder(1 bits) | GA Extension Flag(1 bits) |
  165. // +--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  166. type AudioSpecificConfiguration struct {
  167. Audio_object_type uint8
  168. Sample_freq_index uint8
  169. Channel_configuration uint8
  170. GA_framelength_flag uint8
  171. GA_depends_on_core_coder uint8
  172. GA_extension_flag uint8
  173. }
  174. func NewAudioSpecificConfiguration() *AudioSpecificConfiguration {
  175. return &AudioSpecificConfiguration{
  176. Audio_object_type: 0,
  177. Sample_freq_index: 0,
  178. Channel_configuration: 0,
  179. GA_framelength_flag: 0,
  180. GA_depends_on_core_coder: 0,
  181. GA_extension_flag: 0,
  182. }
  183. }
  184. func (asc *AudioSpecificConfiguration) Encode() []byte {
  185. buf := make([]byte, 2)
  186. buf[0] = (asc.Audio_object_type & 0x1f << 3) | (asc.Sample_freq_index & 0x0F >> 1)
  187. buf[1] = (asc.Sample_freq_index & 0x0F << 7) | (asc.Channel_configuration & 0x0F << 3) | (asc.GA_framelength_flag & 0x01 << 2) | (asc.GA_depends_on_core_coder & 0x01 << 1) | (asc.GA_extension_flag & 0x01)
  188. return buf
  189. }
  190. func (asc *AudioSpecificConfiguration) Decode(buf []byte) error {
  191. if len(buf) < 2 {
  192. return errors.New("len of buf < 2 ")
  193. }
  194. asc.Audio_object_type = buf[0] >> 3
  195. asc.Sample_freq_index = (buf[0] & 0x07 << 1) | (buf[1] >> 7)
  196. asc.Channel_configuration = buf[1] >> 3 & 0x0F
  197. asc.GA_framelength_flag = buf[1] >> 2 & 0x01
  198. asc.GA_depends_on_core_coder = buf[1] >> 1 & 0x01
  199. asc.GA_extension_flag = buf[1] & 0x01
  200. return nil
  201. }
  202. func ConvertADTSToASC(frame []byte) ([]byte, error) {
  203. if len(frame) < 7 {
  204. return nil, errors.New("len of frame < 7")
  205. }
  206. adts := NewAdtsFrameHeader()
  207. adts.Decode(frame)
  208. asc := NewAudioSpecificConfiguration()
  209. asc.Audio_object_type = adts.Fix_Header.Profile + 1
  210. asc.Channel_configuration = adts.Fix_Header.Channel_configuration
  211. asc.Sample_freq_index = adts.Fix_Header.Sampling_frequency_index
  212. return asc.Encode(), nil
  213. }
  214. func ConvertASCToADTS(asc []byte, aacbytes int) []byte {
  215. aac_asc := NewAudioSpecificConfiguration()
  216. aac_asc.Decode(asc)
  217. aac_adts := NewAdtsFrameHeader()
  218. aac_adts.Fix_Header.Profile = aac_asc.Audio_object_type - 1
  219. aac_adts.Fix_Header.Channel_configuration = aac_asc.Channel_configuration
  220. aac_adts.Fix_Header.Sampling_frequency_index = aac_asc.Sample_freq_index
  221. aac_adts.Fix_Header.Protection_absent = 1
  222. aac_adts.Variable_Header.Adts_buffer_fullness = 0x3F
  223. aac_adts.Variable_Header.Frame_length = uint16(aacbytes)
  224. return aac_adts.Encode()
  225. }