Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

665 řádky
19KB

  1. #include "mpeg4-aac.h"
  2. #include "mpeg4-bits.h"
  3. #include <assert.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. // Table 4.85 - Syntactic elements (p533)
  7. enum {
  8. ID_SCE = 0x0, // single channel element()
  9. ID_CPE = 0x1, // channel_pair_element()
  10. ID_CCE = 0x2, // coupling_channel_element()
  11. ID_LFE = 0x3, // lfe_channel_element()
  12. ID_DSE = 0x4, // data_stream_element()
  13. ID_PCE = 0x5, // program_config_element()
  14. ID_FIL = 0x6, // fill_element()
  15. ID_END = 0x7,
  16. };
  17. // ISO-14496-3 4.4.1.1 Program config element (p488)
  18. // There may be up to 16 such elements per raw data block, each one must have a unique element_instance_tag
  19. // PCEs must come before all other syntactic elements in a raw_data_block().
  20. /*
  21. program_config_element()
  22. {
  23. element_instance_tag; 4 uimsbf
  24. object_type; 2 uimsbf
  25. sampling_frequency_index; 4 uimsbf
  26. num_front_channel_elements; 4 uimsbf
  27. num_side_channel_elements; 4 uimsbf
  28. num_back_channel_elements; 4 uimsbf
  29. num_lfe_channel_elements; 2 uimsbf
  30. num_assoc_data_elements; 3 uimsbf
  31. num_valid_cc_elements; 4 uimsbf
  32. mono_mixdown_present; 1 uimsbf
  33. if (mono_mixdown_present == 1 )
  34. mono_mixdown_element_number; 4 uimsbf
  35. stereo_mixdown_present; 1 uimsbf
  36. if (stereo_mixdown_present == 1 )
  37. stereo_mixdown_element_number; 4 uimsbf
  38. matrix_mixdown_idx_present; 1 uimsbf
  39. if (matrix_mixdown_idx_present == 1 ) {
  40. matrix_mixdown_idx ; 2 uimsbf
  41. pseudo_surround_enable; 1 uimsbf
  42. }
  43. for (i = 0; i < num_front_channel_elements; i++) {
  44. front_element_is_cpe[i]; 1 bslbf
  45. front_element_tag_select[i]; 4 uimsbf
  46. }
  47. for (i = 0; i < num_side_channel_elements; i++) {
  48. side_element_is_cpe[i]; 1 bslbf
  49. side_element_tag_select[i]; 4 uimsbf
  50. }
  51. for (i = 0; i < num_back_channel_elements; i++) {
  52. back_element_is_cpe[i]; 1 bslbf
  53. back_element_tag_select[i]; 4 uimsbf
  54. }
  55. for (i = 0; i < num_lfe_channel_elements; i++)
  56. lfe_element_tag_select[i]; 4 uimsbf
  57. for ( i = 0; i < num_assoc_data_elements; i++)
  58. assoc_data_element_tag_select[i]; 4 uimsbf
  59. for (i = 0; i < num_valid_cc_elements; i++) {
  60. cc_element_is_ind_sw[i]; 1 uimsbf
  61. valid_cc_element_tag_select[i]; 4 uimsbf
  62. }
  63. byte_alignment(); Note 1
  64. comment_field_bytes; 8 uimsbf
  65. for (i = 0; i < comment_field_bytes; i++)
  66. comment_field_data[i]; 8 uimsbf
  67. }
  68. */
  69. static inline uint64_t mpeg4_bits_copy(struct mpeg4_bits_t* dst, struct mpeg4_bits_t* src, int n)
  70. {
  71. uint64_t v;
  72. v = mpeg4_bits_read_n(src, n);
  73. mpeg4_bits_write_n(dst, v, n);
  74. return v;
  75. }
  76. static int mpeg4_aac_pce_load(struct mpeg4_bits_t* bits, struct mpeg4_aac_t* aac, struct mpeg4_bits_t* pce)
  77. {
  78. uint64_t i, cpe, tag;
  79. uint64_t element_instance_tag;
  80. uint64_t object_type;
  81. uint64_t sampling_frequency_index;
  82. uint64_t num_front_channel_elements;
  83. uint64_t num_side_channel_elements;
  84. uint64_t num_back_channel_elements;
  85. uint64_t num_lfe_channel_elements;
  86. uint64_t num_assoc_data_elements;
  87. uint64_t num_valid_cc_elements;
  88. uint64_t comment_field_bytes;
  89. aac->channels = 0;
  90. element_instance_tag = mpeg4_bits_copy(pce, bits, 4);
  91. object_type = mpeg4_bits_copy(pce, bits, 2);
  92. sampling_frequency_index = mpeg4_bits_copy(pce, bits, 4);
  93. num_front_channel_elements = mpeg4_bits_copy(pce, bits, 4);
  94. num_side_channel_elements = mpeg4_bits_copy(pce, bits, 4);
  95. num_back_channel_elements = mpeg4_bits_copy(pce, bits, 4);
  96. num_lfe_channel_elements = mpeg4_bits_copy(pce, bits, 2);
  97. num_assoc_data_elements = mpeg4_bits_copy(pce, bits, 3);
  98. num_valid_cc_elements = mpeg4_bits_copy(pce, bits, 4);
  99. if (mpeg4_bits_copy(pce, bits, 1))
  100. mpeg4_bits_copy(pce, bits, 4); // MONO
  101. if (mpeg4_bits_copy(pce, bits, 1))
  102. mpeg4_bits_copy(pce, bits, 4); // STEREO
  103. if (mpeg4_bits_copy(pce, bits, 1))
  104. mpeg4_bits_copy(pce, bits, 3); // Matrix, Pseudo surround
  105. for (i = 0; i < num_front_channel_elements; i++)
  106. {
  107. cpe = mpeg4_bits_copy(pce, bits, 1); // front_element_is_cpe
  108. tag = mpeg4_bits_copy(pce, bits, 4); // front_element_tag_select
  109. aac->channels += (cpe || aac->ps) ? 2 : 1;
  110. }
  111. for (i = 0; i < num_side_channel_elements; i++)
  112. {
  113. cpe = mpeg4_bits_copy(pce, bits, 1); // side_element_is_cpe
  114. tag = mpeg4_bits_copy(pce, bits, 4); // side_element_tag_select
  115. aac->channels += (cpe || aac->ps) ? 2 : 1;
  116. }
  117. for (i = 0; i < num_back_channel_elements; i++)
  118. {
  119. cpe = mpeg4_bits_copy(pce, bits, 1); // back_element_is_cpe
  120. tag = mpeg4_bits_copy(pce, bits, 4); // back_element_tag_select
  121. aac->channels += (cpe || aac->ps) ? 2 : 1;
  122. }
  123. for (i = 0; i < num_lfe_channel_elements; i++)
  124. {
  125. tag = mpeg4_bits_copy(pce, bits, 4); // lfe_element_tag_select
  126. aac->channels += 1;
  127. }
  128. for (i = 0; i < num_assoc_data_elements; i++)
  129. {
  130. tag = mpeg4_bits_copy(pce, bits, 4); // assoc_data_element_tag_select
  131. }
  132. for (i = 0; i < num_valid_cc_elements; i++)
  133. {
  134. cpe = mpeg4_bits_copy(pce, bits, 1); // cc_element_is_ind_sw
  135. tag = mpeg4_bits_copy(pce, bits, 4); // valid_cc_element_tag_select
  136. }
  137. mpeg4_bits_aligment(bits, 8); // byte_alignment();
  138. mpeg4_bits_aligment(pce, 8);
  139. comment_field_bytes = mpeg4_bits_copy(pce, bits, 8);
  140. for (i = 0; i < comment_field_bytes; i++)
  141. mpeg4_bits_copy(pce, bits, 8); // comment_field_data
  142. assert(aac->sampling_frequency_index == sampling_frequency_index);
  143. assert(aac->profile == object_type + 1);
  144. return (int)((pce->bits + 7) / 8);
  145. }
  146. // 4.4.1 Decoder configuration (GASpecificConfig) (p487)
  147. /*
  148. GASpecificConfig (samplingFrequencyIndex, channelConfiguration, audioObjectType)
  149. {
  150. frameLengthFlag; 1 bslbf
  151. dependsOnCoreCoder; 1 bslbf
  152. if (dependsOnCoreCoder) {
  153. coreCoderDelay; 14 uimsbf
  154. }
  155. extensionFlag; 1 bslbf
  156. if (! channelConfiguration) {
  157. program_config_element ();
  158. }
  159. if ((audioObjectType == 6) || (audioObjectType == 20)) {
  160. layerNr; 3 uimsbf
  161. }
  162. if (extensionFlag) {
  163. if (audioObjectType == 22) {
  164. numOfSubFrame; 5 bslbf
  165. layer_length; 11 bslbf
  166. }
  167. if (audioObjectType == 17 || audioObjectType == 19 || audioObjectType == 20 || audioObjectType == 23) {
  168. aacSectionDataResilienceFlag; 1 bslbf
  169. aacScalefactorDataResilienceFlag; 1 bslbf
  170. aacSpectralDataResilienceFlag; 1 bslbf
  171. }
  172. extensionFlag3; 1 bslbf
  173. if (extensionFlag3) {
  174. // tbd in version 3
  175. }
  176. }
  177. }
  178. */
  179. static int mpeg4_aac_ga_specific_config_load(struct mpeg4_bits_t* bits, struct mpeg4_aac_t* aac)
  180. {
  181. int extensionFlag;
  182. struct mpeg4_bits_t pce;
  183. mpeg4_bits_read(bits); // frameLengthFlag
  184. if (mpeg4_bits_read(bits)) // dependsOnCoreCoder
  185. mpeg4_bits_read_uint16(bits, 14); // coreCoderDelay
  186. extensionFlag = mpeg4_bits_read(bits); // extensionFlag
  187. if (0 == aac->channel_configuration)
  188. {
  189. mpeg4_bits_init(&pce, aac->pce, sizeof(aac->pce));
  190. aac->npce = mpeg4_aac_pce_load(bits, aac, &pce); // update channel count
  191. }
  192. if (6 == aac->profile || 20 == aac->profile)
  193. mpeg4_bits_read_uint8(bits, 3); // layerNr
  194. if (extensionFlag)
  195. {
  196. if (22 == aac->profile)
  197. {
  198. mpeg4_bits_read_uint8(bits, 5); // numOfSubFrame
  199. mpeg4_bits_read_uint16(bits, 11); // layer_length
  200. }
  201. if (17 == aac->profile || 19 == aac->profile || 20 == aac->profile || 23 == aac->profile)
  202. {
  203. mpeg4_bits_read(bits); // aacSectionDataResilienceFlag
  204. mpeg4_bits_read(bits); // aacScalefactorDataResilienceFlag
  205. mpeg4_bits_read(bits); // aacSpectralDataResilienceFlag
  206. }
  207. if (mpeg4_bits_read(bits)) // extensionFlag3
  208. {
  209. // tbd in version 3
  210. assert(0);
  211. }
  212. }
  213. return mpeg4_bits_error(bits);
  214. }
  215. static int mpeg4_aac_celp_specific_config_load(struct mpeg4_bits_t* bits, struct mpeg4_aac_t* aac)
  216. {
  217. int ExcitationMode;
  218. if (mpeg4_bits_read(bits)) // isBaseLayer
  219. {
  220. // CelpHeader
  221. ExcitationMode = mpeg4_bits_read(bits);
  222. mpeg4_bits_read(bits); // SampleRateMode
  223. mpeg4_bits_read(bits); // FineRateControl
  224. // Table 3.50 - Description of ExcitationMode
  225. if (ExcitationMode == 1 /*RPE*/)
  226. {
  227. mpeg4_bits_read_n(bits, 3); // RPE_Configuration
  228. }
  229. if (ExcitationMode == 0 /*MPE*/)
  230. {
  231. mpeg4_bits_read_n(bits, 5); // MPE_Configuration
  232. mpeg4_bits_read_n(bits, 2); // NumEnhLayers
  233. mpeg4_bits_read(bits); // BandwidthScalabilityMode
  234. }
  235. }
  236. else
  237. {
  238. if (mpeg4_bits_read(bits)) // isBWSLayer
  239. mpeg4_bits_read_n(bits, 2); // BWS_configuration
  240. else
  241. mpeg4_bits_read_n(bits, 2); // CELP-BRS-id
  242. }
  243. (void)aac;
  244. return mpeg4_bits_error(bits);
  245. }
  246. // ISO/IEC 23003-1 Table 9.1 ¡ª Syntax of SpatialSpecificConfig()
  247. /*
  248. SpatialSpecificConfig()
  249. {
  250. bsSamplingFrequencyIndex; 4 uimsbf
  251. if ( bsSamplingFrequencyIndex == 0xf ) {
  252. bsSamplingFrequency; 24 uimsbf
  253. }
  254. bsFrameLength; 7 uimsbf
  255. bsFreqRes; 3 uimsbf
  256. bsTreeConfig; 4 uimsbf
  257. if (bsTreeConfig == ¡®0111¡¯) {
  258. bsNumInCh; 4 uimsbf
  259. bsNumLFE 2 uimsbf
  260. bsHasSpeakerConfig 1 uimsbf
  261. if ( bsHasSpeakerConfig == 1 ) {
  262. audioChannelLayout = SpeakerConfig3d(); Note 1
  263. }
  264. }
  265. bsQuantMode; 2 uimsbf
  266. bsOneIcc; 1 uimsbf
  267. bsArbitraryDownmix; 1 uimsbf
  268. bsFixedGainSur; 3 uimsbf
  269. bsFixedGainLFE; 3 uimsbf
  270. bsFixedGainDMX; 3 uimsbf
  271. bsMatrixMode; 1 uimsbf
  272. bsTempShapeConfig; 2 uimsbf
  273. bsDecorrConfig; 2 uimsbf
  274. bs3DaudioMode; 1 uimsbf
  275. if ( bsTreeConfig == ¡®0111¡¯ ) {
  276. for (i=0; i< NumInCh - NumLfe; i++) {
  277. defaultCld[i] = 1;
  278. ottModelfe[i] = 0;
  279. }
  280. for (i= NumInCh - NumLfe; i< NumInCh; i++) {
  281. defaultCld[i] = 1;
  282. ottModelfe[i] = 1;
  283. }
  284. }
  285. for (i=0; i<numOttBoxes; i++) { Note 2
  286. OttConfig(i);
  287. }
  288. for (i=0; i<numTttBoxes; i++) { Note 2
  289. TttConfig(i);
  290. }
  291. if (bsTempShapeConfig == 2) {
  292. bsEnvQuantMode 1 uimsbf
  293. }
  294. if (bs3DaudioMode) {
  295. bs3DaudioHRTFset; 2 uimsbf
  296. if (bs3DaudioHRTFset==0) {
  297. ParamHRTFset();
  298. }
  299. }
  300. ByteAlign();
  301. SpatialExtensionConfig();
  302. }
  303. */
  304. static int SpatialSpecificConfig(struct mpeg4_bits_t* bits, struct mpeg4_aac_t* aac)
  305. {
  306. return 0;
  307. }
  308. static inline uint8_t mpeg4_aac_get_audio_object_type(struct mpeg4_bits_t* bits)
  309. {
  310. uint8_t audioObjectType;
  311. audioObjectType = mpeg4_bits_read_uint8(bits, 5);
  312. if (31 == audioObjectType)
  313. audioObjectType = 32 + mpeg4_bits_read_uint8(bits, 6);
  314. return audioObjectType;
  315. }
  316. static inline uint8_t mpeg4_aac_get_sampling_frequency(struct mpeg4_bits_t* bits)
  317. {
  318. uint8_t samplingFrequencyIndex;
  319. uint32_t samplingFrequency;
  320. samplingFrequencyIndex = mpeg4_bits_read_uint8(bits, 4);
  321. if (0x0F == samplingFrequencyIndex)
  322. samplingFrequency = mpeg4_bits_read_uint32(bits, 24);
  323. return samplingFrequencyIndex;
  324. }
  325. /// @return asc bits
  326. static size_t mpeg4_aac_audio_specific_config_load3(struct mpeg4_bits_t* bits, struct mpeg4_aac_t* aac)
  327. {
  328. uint16_t syncExtensionType;
  329. // uint8_t audioObjectType;
  330. uint8_t extensionAudioObjectType = 0;
  331. // uint8_t samplingFrequencyIndex = 0;
  332. uint8_t extensionSamplingFrequencyIndex = 0;
  333. // uint8_t channelConfiguration = 0;
  334. uint8_t extensionChannelConfiguration = 0;
  335. uint8_t epConfig;
  336. size_t offset;
  337. offset = bits->bits;
  338. aac->profile = mpeg4_aac_get_audio_object_type(bits);
  339. aac->sampling_frequency_index = mpeg4_aac_get_sampling_frequency(bits);
  340. aac->channel_configuration = mpeg4_bits_read_uint8(bits, 4);
  341. aac->channels = mpeg4_aac_channel_count(aac->channel_configuration);
  342. aac->sampling_frequency = mpeg4_aac_audio_frequency_to(aac->sampling_frequency_index);
  343. aac->extension_frequency = aac->sampling_frequency;
  344. if (5 == aac->profile || 29 == aac->profile)
  345. {
  346. extensionAudioObjectType = 5;
  347. aac->sbr = 1;
  348. if (29 == aac->profile)
  349. aac->ps = 1;
  350. extensionSamplingFrequencyIndex = mpeg4_aac_get_sampling_frequency(bits);
  351. aac->extension_frequency = mpeg4_aac_audio_frequency_to(extensionSamplingFrequencyIndex);
  352. aac->profile = mpeg4_aac_get_audio_object_type(bits);
  353. if (22 == aac->profile)
  354. extensionChannelConfiguration = mpeg4_bits_read_uint8(bits, 4);
  355. }
  356. else
  357. {
  358. extensionAudioObjectType = 0;
  359. }
  360. switch (aac->profile)
  361. {
  362. case 1: case 2: case 3: case 4: case 6: case 7:
  363. case 17: case 19: case 20: case 21: case 22: case 23:
  364. mpeg4_aac_ga_specific_config_load(bits, aac);
  365. break;
  366. case 8:
  367. mpeg4_aac_celp_specific_config_load(bits, aac);
  368. break;
  369. case 30:
  370. /*sacPayloadEmbedding=*/ mpeg4_bits_read(bits);
  371. break;
  372. default:
  373. assert(0);
  374. return bits->bits - offset;
  375. }
  376. switch (aac->profile)
  377. {
  378. case 17: case 19: case 20: case 21: case 22:
  379. case 23: case 24: case 25: case 26: case 27: case 39:
  380. epConfig = mpeg4_bits_read_uint8(bits, 2);
  381. if (2 == epConfig || 3 == epConfig)
  382. {
  383. // 1.8.2.1 Error protection specific configuration (p96)
  384. // TODO: ErrorProtectionSpecificConfig();
  385. assert(0);
  386. }
  387. if (3 == epConfig)
  388. {
  389. if (mpeg4_bits_read(bits)) // directMapping
  390. {
  391. // tbd
  392. assert(0);
  393. }
  394. }
  395. break;
  396. default:
  397. break; // do nothing;
  398. }
  399. if (5 != extensionAudioObjectType && mpeg4_bits_remain(bits) >= 16)
  400. {
  401. syncExtensionType = mpeg4_bits_read_uint16(bits, 11);
  402. if (0x2b7 == syncExtensionType)
  403. {
  404. extensionAudioObjectType = mpeg4_aac_get_audio_object_type(bits);
  405. if (5 == extensionAudioObjectType)
  406. {
  407. aac->sbr = mpeg4_bits_read(bits);
  408. if (aac->sbr)
  409. {
  410. extensionSamplingFrequencyIndex = mpeg4_aac_get_sampling_frequency(bits);
  411. aac->extension_frequency = mpeg4_aac_audio_frequency_to(extensionSamplingFrequencyIndex);
  412. if (mpeg4_bits_remain(bits) >= 12)
  413. {
  414. syncExtensionType = mpeg4_bits_read_uint16(bits, 11);
  415. if (0x548 == syncExtensionType)
  416. aac->ps = mpeg4_bits_read(bits);
  417. }
  418. }
  419. }
  420. if (22 == extensionAudioObjectType)
  421. {
  422. aac->sbr = mpeg4_bits_read(bits);
  423. if (aac->sbr)
  424. {
  425. extensionSamplingFrequencyIndex = mpeg4_aac_get_sampling_frequency(bits);
  426. aac->extension_frequency = mpeg4_aac_audio_frequency_to(extensionSamplingFrequencyIndex);
  427. }
  428. extensionChannelConfiguration = mpeg4_bits_read_uint8(bits, 4);
  429. }
  430. }
  431. }
  432. return bits->bits - offset;
  433. }
  434. int mpeg4_aac_audio_specific_config_load2(const uint8_t* data, size_t bytes, struct mpeg4_aac_t* aac)
  435. {
  436. struct mpeg4_bits_t bits;
  437. mpeg4_bits_init(&bits, (void*)data, bytes);
  438. mpeg4_aac_audio_specific_config_load3(&bits, aac);
  439. mpeg4_bits_aligment(&bits, 8);
  440. return mpeg4_bits_error(&bits) ? -1 : (int)(bits.bits / 8);
  441. }
  442. int mpeg4_aac_audio_specific_config_save2(const struct mpeg4_aac_t* aac, uint8_t* data, size_t bytes)
  443. {
  444. if (bytes < 2 + (size_t)aac->npce)
  445. return -1;
  446. memcpy(data + 2, aac->pce, aac->npce);
  447. return 2 + aac->npce;
  448. //data[2 + aac->npce] = 0x56;
  449. //data[2 + aac->npce + 1] = 0xe5;
  450. //data[2 + aac->npce + 2] = 0x00;
  451. //return 2 + aac->npce + 3;
  452. }
  453. int mpeg4_aac_adts_pce_load(const uint8_t* data, size_t bytes, struct mpeg4_aac_t* aac)
  454. {
  455. uint8_t i;
  456. size_t offset = 7;
  457. struct mpeg4_bits_t bits, pce;
  458. if (0 == (data[1] & 0x01)) // protection_absent
  459. {
  460. // number_of_raw_data_blocks_in_frame
  461. for (i = 1; i <= (data[6] & 0x03); i++)
  462. offset += 2; // raw_data_block_position 16-bits
  463. offset += 2; // crc_check 16-bits
  464. }
  465. if (bytes <= offset)
  466. return (int)offset;
  467. mpeg4_bits_init(&bits, (uint8_t*)data + offset, bytes - offset);
  468. if (ID_PCE == mpeg4_bits_read_uint8(&bits, 3))
  469. {
  470. mpeg4_bits_init(&pce, aac->pce, sizeof(aac->pce));
  471. aac->npce = mpeg4_aac_pce_load(&bits, aac, &pce);
  472. return mpeg4_bits_error(&bits) ? -1 : (int)(7 + (pce.bits + 7) / 8);
  473. }
  474. return 7;
  475. }
  476. int mpeg4_aac_adts_pce_save(uint8_t* data, size_t bytes, const struct mpeg4_aac_t* aac)
  477. {
  478. struct mpeg4_aac_t src;
  479. struct mpeg4_bits_t pce, adts;
  480. if ((size_t)aac->npce + 7 > bytes)
  481. return 0;
  482. memcpy(&src, aac, sizeof(src));
  483. // assert(data[1] & 0x01); // disable protection_absent
  484. mpeg4_bits_init(&pce, (uint8_t*)aac->pce, aac->npce);
  485. mpeg4_bits_init(&adts, (uint8_t*)data + 7, bytes - 7);
  486. mpeg4_bits_write_uint8(&adts, ID_PCE, 3);
  487. mpeg4_aac_pce_load(&pce, &src, &adts);
  488. assert(src.channels == aac->channels && (adts.bits + 7) / 8 <= bytes);
  489. return mpeg4_bits_error(&pce) ? 0 : (int)((adts.bits+7) / 8);
  490. }
  491. static size_t mpeg4_aac_stream_mux_config_load3(struct mpeg4_bits_t* bits, struct mpeg4_aac_t* aac)
  492. {
  493. uint8_t audioMuxVersion = 0;
  494. uint8_t numSubFrames;
  495. uint8_t numProgram;
  496. uint8_t numLayer;
  497. uint8_t allStreamsSameTimeFraming;
  498. uint8_t profile = 0;
  499. uint64_t ascLen;
  500. size_t offset;
  501. int streamCnt, prog, lay;
  502. offset = bits->bits;
  503. audioMuxVersion = (uint8_t)mpeg4_bits_read(bits);
  504. if (!audioMuxVersion || 0 == mpeg4_bits_read(bits))
  505. {
  506. if (1 == audioMuxVersion)
  507. /*taraBufferFullness =*/ mpeg4_bits_read_latm(bits);
  508. streamCnt = 0;
  509. allStreamsSameTimeFraming = (uint8_t)mpeg4_bits_read(bits);
  510. numSubFrames = (uint8_t)mpeg4_bits_read_n(bits, 6);
  511. numProgram = (uint8_t)mpeg4_bits_read_n(bits, 4);
  512. for (prog = 0; prog <= numProgram; prog++)
  513. {
  514. numLayer = (uint8_t)mpeg4_bits_read_n(bits, 3);
  515. for (lay = 0; lay <= numLayer; lay++)
  516. {
  517. //progSIndx[streamCnt] = prog;
  518. //laySIndx[streamCnt] = lay;
  519. //streamID[prog][lay] = streamCnt++;
  520. if ( (prog == 0 && lay == 0) || 0 == (uint8_t)mpeg4_bits_read(bits))
  521. {
  522. profile = aac->profile; // previous profile
  523. if (audioMuxVersion == 0) {
  524. mpeg4_aac_audio_specific_config_load3(bits, aac);
  525. } else {
  526. ascLen = mpeg4_bits_read_latm(bits);
  527. ascLen -= mpeg4_aac_audio_specific_config_load3(bits, aac);
  528. mpeg4_bits_skip(bits, (size_t)ascLen);
  529. }
  530. }
  531. //frameLengthType[streamID[prog][lay]] = (uint8_t)mpeg4_bits_read_n(bits, 3);
  532. //switch (frameLengthType[streamID[prog][lay]])
  533. switch (mpeg4_bits_read_n(bits, 3))
  534. {
  535. case 0:
  536. /*latmBufferFullness[streamID[prog][lay]] =*/ (uint8_t)mpeg4_bits_read_n(bits, 8);
  537. if (!allStreamsSameTimeFraming)
  538. {
  539. // fixme
  540. //if ((AudioObjectType[lay] == 6 || AudioObjectType[lay] == 20) &&
  541. // (AudioObjectType[lay - 1] == 8 || AudioObjectType[lay - 1] == 24))
  542. if( (aac->profile == 6 || aac->profile == 20) && (profile == 8 || profile == 24) )
  543. {
  544. /*coreFrameOffset =*/ (uint8_t)mpeg4_bits_read_n(bits, 6);
  545. }
  546. }
  547. break;
  548. case 1:
  549. /*frameLength[streamID[prog][lay]] =*/ (uint16_t)mpeg4_bits_read_n(bits, 9);
  550. break;
  551. case 3:
  552. case 4:
  553. case 5:
  554. /*CELPframeLengthTableIndex[streamID[prog][lay]] =*/ (uint16_t)mpeg4_bits_read_n(bits, 6);
  555. break;
  556. case 6:
  557. case 7:
  558. /*HVXCframeLengthTableIndex[streamID[prog][lay]] =*/ (uint16_t)mpeg4_bits_read_n(bits, 1);
  559. break;
  560. default:
  561. // nothing to do
  562. break;
  563. }
  564. }
  565. }
  566. // otherDataPresent
  567. if (mpeg4_bits_read(bits))
  568. {
  569. if (audioMuxVersion == 1)
  570. {
  571. /*otherDataLenBits =*/ mpeg4_bits_read_latm(bits);
  572. }
  573. else
  574. {
  575. /*otherDataLenBits =*/ mpeg4_bits_read_n(bits, 8); /* helper variable 32bit */
  576. while(mpeg4_bits_read(bits))
  577. {
  578. /*otherDataLenBits <<= 8;
  579. otherDataLenBits +=*/ mpeg4_bits_read_n(bits, 8);
  580. }
  581. }
  582. }
  583. // crcCheckPresent
  584. if (mpeg4_bits_read(bits))
  585. /*crcCheckSum =*/ mpeg4_bits_read_n(bits, 8);
  586. }
  587. else
  588. {
  589. /*tbd*/
  590. }
  591. return bits->bits - offset;
  592. }
  593. int mpeg4_aac_stream_mux_config_load2(const uint8_t* data, size_t bytes, struct mpeg4_aac_t* aac)
  594. {
  595. struct mpeg4_bits_t bits;
  596. mpeg4_bits_init(&bits, (void*)data, bytes);
  597. mpeg4_aac_stream_mux_config_load3(&bits, aac);
  598. mpeg4_bits_aligment(&bits, 8);
  599. return mpeg4_bits_error(&bits) ? -1 : (int)(bits.bits / 8);
  600. }