You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

358 lines
15KB

  1. #include "mpeg4-aac.h"
  2. #include <assert.h>
  3. #include <string.h>
  4. int mpeg4_aac_adts_pce_load(const uint8_t* data, size_t bytes, struct mpeg4_aac_t* aac);
  5. int mpeg4_aac_adts_pce_save(uint8_t* data, size_t bytes, const struct mpeg4_aac_t* aac);
  6. int mpeg4_aac_audio_specific_config_load2(const uint8_t* data, size_t bytes, struct mpeg4_aac_t* aac);
  7. int mpeg4_aac_audio_specific_config_save2(const struct mpeg4_aac_t* aac, uint8_t* data, size_t bytes);
  8. int mpeg4_aac_stream_mux_config_load2(const uint8_t* data, size_t bytes, struct mpeg4_aac_t* aac);
  9. /*
  10. // ISO-14496-3 adts_frame (p122)
  11. adts_fixed_header()
  12. {
  13. syncword; 12 bslbf
  14. ID; 1 bslbf
  15. layer; 2 uimsbf
  16. protection_absent; 1 bslbf
  17. profile_ObjectType; 2 uimsbf
  18. sampling_frequency_index; 4 uimsbf
  19. private_bit; 1 bslbf
  20. channel_configuration; 3 uimsbf
  21. original_copy; 1 bslbf
  22. home; 1 bslbf
  23. }
  24. adts_variable_header()
  25. {
  26. copyright_identification_bit; 1 bslbf
  27. copyright_identification_start; 1 bslbf
  28. aac_frame_length; 13 bslbf
  29. adts_buffer_fullness; 11 bslbf
  30. number_of_raw_data_blocks_in_frame; 2 uimsbf
  31. }
  32. */
  33. /// @return >=0-adts header length, <0-error
  34. int mpeg4_aac_adts_load(const uint8_t* data, size_t bytes, struct mpeg4_aac_t* aac)
  35. {
  36. if (bytes < 7) return -1;
  37. memset(aac, 0, sizeof(struct mpeg4_aac_t));
  38. assert(0xFF == data[0] && 0xF0 == (data[1] & 0xF0)); /* syncword */
  39. aac->profile = ((data[2] >> 6) & 0x03) + 1; // 2 bits: the MPEG-2 Audio Object Type add 1
  40. aac->sampling_frequency_index = (data[2] >> 2) & 0x0F; // 4 bits: MPEG-4 Sampling Frequency Index (15 is forbidden)
  41. aac->channel_configuration = ((data[2] & 0x01) << 2) | ((data[3] >> 6) & 0x03); // 3 bits: MPEG-4 Channel Configuration
  42. assert(aac->profile > 0 && aac->profile < 31);
  43. assert(aac->channel_configuration >= 0 && aac->channel_configuration <= 7);
  44. assert(aac->sampling_frequency_index >= 0 && aac->sampling_frequency_index <= 0xc);
  45. aac->channels = mpeg4_aac_channel_count(aac->channel_configuration);
  46. aac->sampling_frequency = mpeg4_aac_audio_frequency_to(aac->sampling_frequency_index);
  47. aac->extension_frequency = aac->sampling_frequency;
  48. if (0 == aac->channel_configuration)
  49. return mpeg4_aac_adts_pce_load(data, bytes, aac);
  50. return 7;
  51. }
  52. /// @return >=0-adts header length, <0-error
  53. int mpeg4_aac_adts_save(const struct mpeg4_aac_t* aac, size_t payload, uint8_t* data, size_t bytes)
  54. {
  55. const uint8_t ID = 0; // 0-MPEG4/1-MPEG2
  56. size_t len = payload + 7;
  57. if (bytes < 7 || len >= (1 << 12)) return -1;
  58. if (0 == aac->channel_configuration && aac->npce > 0)
  59. len += mpeg4_aac_adts_pce_save(data, bytes, aac);
  60. assert(aac->profile > 0 && aac->profile < 31);
  61. assert(aac->channel_configuration >= 0 && aac->channel_configuration <= 7);
  62. assert(aac->sampling_frequency_index >= 0 && aac->sampling_frequency_index <= 0xc);
  63. data[0] = 0xFF; /* 12-syncword */
  64. data[1] = 0xF0 /* 12-syncword */ | (ID << 3)/*1-ID*/ | (0x00 << 2) /*2-layer*/ | 0x01 /*1-protection_absent*/;
  65. data[2] = ((aac->profile - 1) << 6) | ((aac->sampling_frequency_index & 0x0F) << 2) | ((aac->channel_configuration >> 2) & 0x01);
  66. data[3] = ((aac->channel_configuration & 0x03) << 6) | ((len >> 11) & 0x03); /*0-original_copy*/ /*0-home*/ /*0-copyright_identification_bit*/ /*0-copyright_identification_start*/
  67. data[4] = (uint8_t)(len >> 3);
  68. data[5] = ((len & 0x07) << 5) | 0x1F;
  69. data[6] = 0xFC /*| ((len / (1024 * aac->channels)) & 0x03)*/;
  70. return (int)(len - payload);
  71. }
  72. int mpeg4_aac_adts_frame_length(const uint8_t* data, size_t bytes)
  73. {
  74. uint16_t len;
  75. if (bytes < 7) return -1;
  76. assert(0xFF == data[0] && 0xF0 == (data[1] & 0xF0)); /* syncword */
  77. len = ((uint16_t)(data[3] & 0x03) << 11) | ((uint16_t)data[4] << 3) | ((uint16_t)(data[5] >> 5) & 0x07);
  78. return len;
  79. }
  80. // ISO-14496-3 AudioSpecificConfig (p52)
  81. /*
  82. audioObjectType; 5 uimsbf
  83. if (audioObjectType == 31) {
  84. audioObjectType = 32 + audioObjectTypeExt; 6 uimsbf
  85. }
  86. samplingFrequencyIndex; 4 bslbf
  87. if ( samplingFrequencyIndex == 0xf ) {
  88. samplingFrequency; 24 uimsbf
  89. }
  90. channelConfiguration; 4 bslbf
  91. */
  92. /// @return >=0-adts header length, <0-error
  93. int mpeg4_aac_audio_specific_config_load(const uint8_t* data, size_t bytes, struct mpeg4_aac_t* aac)
  94. {
  95. if (bytes < 2) return -1;
  96. memset(aac, 0, sizeof(struct mpeg4_aac_t));
  97. aac->profile = (data[0] >> 3) & 0x1F;
  98. aac->sampling_frequency_index = ((data[0] & 0x7) << 1) | ((data[1] >> 7) & 0x01);
  99. aac->channel_configuration = (data[1] >> 3) & 0x0F;
  100. assert(aac->profile > 0 && aac->profile < 31);
  101. assert(aac->channel_configuration >= 0 && aac->channel_configuration <= 7);
  102. assert(aac->sampling_frequency_index >= 0 && aac->sampling_frequency_index <= 0xc);
  103. aac->channels = mpeg4_aac_channel_count(aac->channel_configuration);
  104. aac->sampling_frequency = mpeg4_aac_audio_frequency_to(aac->sampling_frequency_index);
  105. aac->extension_frequency = aac->sampling_frequency;
  106. if (bytes > 2)
  107. return mpeg4_aac_audio_specific_config_load2(data, bytes, aac);
  108. return 2;
  109. }
  110. // ISO-14496-3 AudioSpecificConfig
  111. int mpeg4_aac_audio_specific_config_save(const struct mpeg4_aac_t* aac, uint8_t* data, size_t bytes)
  112. {
  113. uint8_t channel_configuration;
  114. if (bytes < 2+ (size_t)aac->npce) return -1;
  115. channel_configuration = aac->npce > 0 ? 0 : aac->channel_configuration;
  116. assert(aac->profile > 0 && aac->profile < 31);
  117. assert(aac->channel_configuration >= 0 && aac->channel_configuration <= 7);
  118. assert(aac->sampling_frequency_index >= 0 && aac->sampling_frequency_index <= 0xc);
  119. data[0] = (aac->profile << 3) | ((aac->sampling_frequency_index >> 1) & 0x07);
  120. data[1] = ((aac->sampling_frequency_index & 0x01) << 7) | ((channel_configuration & 0xF) << 3) | (0 << 2) /* frame length-1024 samples*/ | (0 << 1) /* don't depend on core */ | 0 /* not extension */;
  121. if (0 == aac->channel_configuration && aac->npce > 0)
  122. return mpeg4_aac_audio_specific_config_save2(aac, data, bytes);
  123. return 2;
  124. }
  125. // ISO/IEC 14496-3:2009(E) Table 1.42 - Syntax of StreamMuxConfig() (p83)
  126. int mpeg4_aac_stream_mux_config_load(const uint8_t* data, size_t bytes, struct mpeg4_aac_t* aac)
  127. {
  128. if (bytes < 6) return -1;
  129. memset(aac, 0, sizeof(*aac));
  130. if (6 == bytes && 0x40 == data[0] && 0 == (data[1] & 0xFE))
  131. {
  132. // fast path
  133. // [0] 0-audioMuxVersion(1), 1-allStreamsSameTimeFraming(1), 0-numSubFrames(6)
  134. assert(0 == (0x80 & data[0])); // audioMuxVersion: 0
  135. aac->profile = ((data[1] & 0x01) << 4) | (data[2] >> 4); // 0-numProgram(4), 0-numLayer(3), 1-ASC(1)
  136. aac->sampling_frequency_index = data[2] & 0x0F;
  137. aac->channel_configuration = data[3] >> 4;
  138. assert(aac->profile > 0 && aac->profile < 31);
  139. assert(aac->channel_configuration >= 0 && aac->channel_configuration <= 7);
  140. assert(aac->sampling_frequency_index >= 0 && aac->sampling_frequency_index <= 0xc);
  141. aac->channels = mpeg4_aac_channel_count(aac->channel_configuration);
  142. aac->sampling_frequency = mpeg4_aac_audio_frequency_to(aac->sampling_frequency_index);
  143. aac->extension_frequency = aac->sampling_frequency;
  144. return 6;
  145. }
  146. return mpeg4_aac_stream_mux_config_load2(data, bytes, aac);
  147. }
  148. // ISO/IEC 14496-3:2009(E) Table 1.42 - Syntax of StreamMuxConfig() (p83)
  149. int mpeg4_aac_stream_mux_config_save(const struct mpeg4_aac_t* aac, uint8_t* data, size_t bytes)
  150. {
  151. int profile;
  152. int frequncy;
  153. if (bytes < 6) return -1;
  154. profile = aac->ps ? MPEG4_AAC_PS : aac->profile;
  155. frequncy = mpeg4_aac_audio_frequency_from(aac->extension_frequency);
  156. frequncy = (aac->sbr || aac->ps) && -1 != frequncy ? frequncy : 0;
  157. assert(aac->profile > 0 && aac->profile < 31);
  158. assert(aac->channel_configuration >= 0 && aac->channel_configuration <= 7);
  159. assert(aac->sampling_frequency_index >= 0 && aac->sampling_frequency_index <= 0xc);
  160. data[0] = 0x40; // 0-audioMuxVersion(1), 1-allStreamsSameTimeFraming(1), 0-numSubFrames(6)
  161. data[1] = 0x00 | ((profile >> 4) & 0x01); // 0-numProgram(4), 0-numLayer(3)
  162. data[2] = ((profile & 0x0F) << 4) | (aac->sampling_frequency_index & 0x0F);
  163. data[3] = ((aac->channel_configuration & 0x0F) << 4) | (-1 != frequncy ? (frequncy & 0x0F) : 0); // 0-GASpecificConfig(3), 0-frameLengthType(1)
  164. data[4] = 0x3F; // 0-frameLengthType(2), 111111-latmBufferFullness(6)
  165. data[5] = 0xC0; // 11-latmBufferFullness(2), 0-otherDataPresent, 0-crcCheckPresent
  166. return 6;
  167. }
  168. // Table 1.6 ¨C Levels for the High Quality Audio Profile
  169. static int mpeg4_aac_high_quality_level(const struct mpeg4_aac_t* aac)
  170. {
  171. if (aac->sampling_frequency <= 22050)
  172. {
  173. if (aac->channel_configuration <= 2)
  174. return 1; // Level 1/5
  175. }
  176. else if (aac->sampling_frequency <= 48000)
  177. {
  178. if (aac->channel_configuration <= 2)
  179. return 2; // Level 2/6
  180. else if (aac->channel_configuration <= 5)
  181. return 3; // Level 3/4/7/8
  182. }
  183. return 8;
  184. }
  185. // Table 1.10 ¨C Levels for the AAC Profile
  186. static int mpeg4_aac_level(const struct mpeg4_aac_t* aac)
  187. {
  188. if (aac->sampling_frequency <= 24000)
  189. {
  190. if (aac->channel_configuration <= 2)
  191. return 1; // AAC Profile, Level 1
  192. }
  193. else if (aac->sampling_frequency <= 48000)
  194. {
  195. if (aac->channel_configuration <= 2)
  196. return 2; // Level 2
  197. else if (aac->channel_configuration <= 5)
  198. return 4; // Level 4
  199. }
  200. else if (aac->sampling_frequency <= 96000)
  201. {
  202. if (aac->channel_configuration <= 5)
  203. return 5; // Level 5
  204. }
  205. return 5;
  206. }
  207. static int mpeg4_aac_he_level(const struct mpeg4_aac_t* aac)
  208. {
  209. if (aac->sampling_frequency <= 48000)
  210. {
  211. if (aac->channel_configuration <= 2)
  212. return aac->sbr ? 3 : 2; // Level 2/3
  213. else if (aac->channel_configuration <= 5)
  214. return 4; // Level 4
  215. }
  216. else if (aac->sampling_frequency <= 96000)
  217. {
  218. if (aac->channel_configuration <= 5)
  219. return 5; // Level 5
  220. }
  221. return 5;
  222. }
  223. // ISO/IEC 14496-3:2009(E) Table 1.14 - audioProfileLevelIndication values (p51)
  224. int mpeg4_aac_profile_level(const struct mpeg4_aac_t* aac)
  225. {
  226. // Table 1.10 - Levels for the AAC Profile (p49)
  227. // Table 1.14 - audioProfileLevelIndication values (p51)
  228. switch (aac->profile)
  229. {
  230. case MPEG4_AAC_LC:
  231. return mpeg4_aac_level(aac) - 1 + 0x28; // AAC Profile
  232. case MPEG4_AAC_SBR:
  233. return mpeg4_aac_he_level(aac) - 2 + 0x2C; // High Efficiency AAC Profile
  234. case MPEG4_AAC_PS:
  235. return mpeg4_aac_he_level(aac) - 2 + 0x30; // High Efficiency AAC v2 Profile
  236. case MPEG4_AAC_CELP:
  237. return mpeg4_aac_high_quality_level(aac) - 1 + 0x0E; // High Quality Audio Profile
  238. default:
  239. return 1; // Main Audio Profile, Level 1
  240. }
  241. }
  242. #define ARRAYOF(arr) sizeof(arr)/sizeof(arr[0])
  243. static const int s_frequency[] = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350 };
  244. int mpeg4_aac_audio_frequency_to(enum mpeg4_aac_frequency index)
  245. {
  246. if (index < 0 || index >= ARRAYOF(s_frequency))
  247. return 0;
  248. return s_frequency[index];
  249. }
  250. int mpeg4_aac_audio_frequency_from(int frequence)
  251. {
  252. int i = 0;
  253. while (i < ARRAYOF(s_frequency) && s_frequency[i] != frequence) i++;
  254. return i >= ARRAYOF(s_frequency) ? -1 : i;
  255. }
  256. uint8_t mpeg4_aac_channel_count(uint8_t channel_configuration)
  257. {
  258. static const uint8_t s_channels[] = { 0, 1, 2, 3, 4, 5, 6, 8 };
  259. if (channel_configuration < 0 || channel_configuration >= ARRAYOF(s_channels))
  260. return 0;
  261. return s_channels[channel_configuration];
  262. }
  263. #undef ARRAYOF
  264. #if defined(_DEBUG) || defined(DEBUG)
  265. void mpeg4_aac_test(void)
  266. {
  267. struct mpeg4_aac_t aac, aac2;
  268. const unsigned char asc[] = { 0x13, 0x88 };
  269. const unsigned char adts[] = { 0xFF, 0xF1, 0x5C, 0x40, 0x01, 0x1F, 0xFC };
  270. // const unsigned char ascsbr[] = { 0x13, 0x10, 0x56, 0xe5, 0x9d, 0x48, 0x00 };
  271. const unsigned char ascsbr[] = { 0x2b, 0x92, 0x08, 0x00 };
  272. const unsigned char asc8ch[] = { 0x12, 0x00, 0x05, 0x08, 0x48, 0x00, 0x20, 0x00, 0xC6, 0x40, 0x0D, 0x4C, 0x61, 0x76, 0x63, 0x35, 0x38, 0x2E, 0x39, 0x37, 0x2E, 0x31, 0x30, 0x32, 0x56, 0xE5, 0x00 };
  273. // https://datatracker.ietf.org/doc/html/rfc6416#page-25
  274. const unsigned char mux1[] = { 0x40, 0x00, 0x8B, 0x18, 0x38, 0x83, 0x80 }; // 6 kbit/s CELP
  275. const unsigned char mux2[] = { 0x40, 0x00, 0x26, 0x20, 0x3f, 0xc0 }; // 64 kbit/s AAC LC Stereo
  276. const unsigned char mux3[] = { 0x40, 0x00, 0x56, 0x23, 0x10, 0x1f, 0xe0 }; // Hierarchical Signaling of SBR
  277. const unsigned char mux4[] = { 0x40, 0x00, 0x26, 0x10, 0x3f, 0xc0 }; // HE AAC v2 Signaling
  278. const unsigned char mux5[] = { 0x40, 0x01, 0xd6, 0x13, 0x10, 0x1f, 0xe0 }; // Hierarchical Signaling of PS
  279. const unsigned char mux6[] = { 0x8F, 0xF8, 0x00, 0x41, 0x92, 0xB1, 0x18, 0x80, 0xFF, 0x0D, 0xDE, 0x36, 0x99, 0xF2, 0x40, 0x8C, 0x00, 0x53, 0x6C, 0x02, 0x31, 0x3C, 0xF3, 0xCE, 0x0F, 0xF0 }; // MPEG Surround
  280. const unsigned char mux7[] = { 0x40, 0x00, 0x56, 0x23, 0x10, 0x1f, 0xe0 }; // MPEG Surround with Extended SDP Parameters
  281. const unsigned char mux8[] = { 0x8F, 0xF8, 0x00, 0x06, 0x52, 0xB9, 0x20, 0x87, 0x6A, 0x83, 0xA1, 0xF4, 0x40, 0x88, 0x40, 0x53, 0x62, 0x0F, 0xF0 }; // MPEG Surround with Single-Layer Configuration
  282. unsigned char data[32];
  283. assert(sizeof(ascsbr) == mpeg4_aac_audio_specific_config_load(ascsbr, sizeof(ascsbr), &aac));
  284. assert(2 == aac.profile && 7 == aac.sampling_frequency_index && 2 == aac.channel_configuration);
  285. //assert(sizeof(ascsbr) == mpeg4_aac_audio_specific_config_save(&aac, data, sizeof(data)));
  286. //assert(0 == memcmp(ascsbr, data, sizeof(ascsbr)));
  287. assert(sizeof(asc) == mpeg4_aac_audio_specific_config_load(asc, sizeof(asc), &aac));
  288. assert(2 == aac.profile && 7 == aac.sampling_frequency_index && 1 == aac.channel_configuration);
  289. assert(sizeof(asc) == mpeg4_aac_audio_specific_config_save(&aac, data, sizeof(data)));
  290. assert(0 == memcmp(asc, data, sizeof(asc)));
  291. assert(sizeof(adts) == mpeg4_aac_adts_save(&aac, 1, data, sizeof(data)));
  292. assert(0 == memcmp(adts, data, sizeof(adts)));
  293. assert(7 == mpeg4_aac_adts_load(data, sizeof(adts), &aac2));
  294. assert(0 == memcmp(&aac, &aac2, sizeof(aac)));
  295. assert(22050 == mpeg4_aac_audio_frequency_to(aac.sampling_frequency_index));
  296. assert(aac.sampling_frequency_index == mpeg4_aac_audio_frequency_from(22050));
  297. //assert(sizeof(ascsbr) == mpeg4_aac_audio_specific_config_load(ascsbr, sizeof(ascsbr), &aac));
  298. //assert(2 == aac.profile && 6 == aac.sampling_frequency_index && 1 == aac.channel_configuration);
  299. assert(sizeof(asc8ch) == mpeg4_aac_audio_specific_config_load(asc8ch, sizeof(asc8ch), &aac));
  300. assert(2 == aac.profile && 4 == aac.sampling_frequency_index && 8 == aac.channels);
  301. assert(29 == mpeg4_aac_adts_save(&aac, 1, data, sizeof(data)));
  302. memset(&aac, 0, sizeof(aac));
  303. mpeg4_aac_stream_mux_config_load(mux1, sizeof(mux1), &aac);
  304. mpeg4_aac_stream_mux_config_load(mux2, sizeof(mux2), &aac);
  305. mpeg4_aac_stream_mux_config_load(mux3, sizeof(mux3), &aac);
  306. mpeg4_aac_stream_mux_config_load(mux4, sizeof(mux4), &aac);
  307. mpeg4_aac_stream_mux_config_load(mux5, sizeof(mux5), &aac);
  308. //mpeg4_aac_stream_mux_config_load(mux6, sizeof(mux6), &aac);
  309. //mpeg4_aac_stream_mux_config_load(mux7, sizeof(mux7), &aac);
  310. //mpeg4_aac_stream_mux_config_load(mux8, sizeof(mux8), &aac);
  311. mpeg4_aac_stream_mux_config_save(&aac, data, sizeof(data));
  312. //assert(0 == memcmp(data, mux1, sizeof(mux1)));
  313. }
  314. #endif