module.audio.aac.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.aac.php //
  11. // module for analyzing AAC Audio files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_aac
  16. {
  17. // new combined constructor
  18. function getid3_aac(&$fd, &$ThisFileInfo, $option) {
  19. if ($option === 'adif') {
  20. $this->getAACADIFheaderFilepointer($fd, $ThisFileInfo);
  21. }
  22. elseif ($option === 'adts') {
  23. $this->getAACADTSheaderFilepointer($fd, $ThisFileInfo);
  24. }
  25. }
  26. function getAACADIFheaderFilepointer(&$fd, &$ThisFileInfo) {
  27. $ThisFileInfo['fileformat'] = 'aac';
  28. $ThisFileInfo['audio']['dataformat'] = 'aac';
  29. $ThisFileInfo['audio']['lossless'] = false;
  30. fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
  31. $AACheader = fread($fd, 1024);
  32. $offset = 0;
  33. if (substr($AACheader, 0, 4) == 'ADIF') {
  34. // http://faac.sourceforge.net/wiki/index.php?page=ADIF
  35. // http://libmpeg.org/mpeg4/doc/w2203tfs.pdf
  36. // adif_header() {
  37. // adif_id 32
  38. // copyright_id_present 1
  39. // if( copyright_id_present )
  40. // copyright_id 72
  41. // original_copy 1
  42. // home 1
  43. // bitstream_type 1
  44. // bitrate 23
  45. // num_program_config_elements 4
  46. // for (i = 0; i < num_program_config_elements + 1; i++ ) {
  47. // if( bitstream_type == '0' )
  48. // adif_buffer_fullness 20
  49. // program_config_element()
  50. // }
  51. // }
  52. $AACheaderBitstream = getid3_lib::BigEndian2Bin($AACheader);
  53. $bitoffset = 0;
  54. $ThisFileInfo['aac']['header_type'] = 'ADIF';
  55. $bitoffset += 32;
  56. $ThisFileInfo['aac']['header']['mpeg_version'] = 4;
  57. $ThisFileInfo['aac']['header']['copyright'] = (bool) (substr($AACheaderBitstream, $bitoffset, 1) == '1');
  58. $bitoffset += 1;
  59. if ($ThisFileInfo['aac']['header']['copyright']) {
  60. $ThisFileInfo['aac']['header']['copyright_id'] = getid3_lib::Bin2String(substr($AACheaderBitstream, $bitoffset, 72));
  61. $bitoffset += 72;
  62. }
  63. $ThisFileInfo['aac']['header']['original_copy'] = (bool) (substr($AACheaderBitstream, $bitoffset, 1) == '1');
  64. $bitoffset += 1;
  65. $ThisFileInfo['aac']['header']['home'] = (bool) (substr($AACheaderBitstream, $bitoffset, 1) == '1');
  66. $bitoffset += 1;
  67. $ThisFileInfo['aac']['header']['is_vbr'] = (bool) (substr($AACheaderBitstream, $bitoffset, 1) == '1');
  68. $bitoffset += 1;
  69. if ($ThisFileInfo['aac']['header']['is_vbr']) {
  70. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  71. $ThisFileInfo['aac']['header']['bitrate_max'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 23));
  72. $bitoffset += 23;
  73. } else {
  74. $ThisFileInfo['audio']['bitrate_mode'] = 'cbr';
  75. $ThisFileInfo['aac']['header']['bitrate'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 23));
  76. $bitoffset += 23;
  77. $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['aac']['header']['bitrate'];
  78. }
  79. if ($ThisFileInfo['audio']['bitrate'] == 0) {
  80. $ThisFileInfo['error'][] = 'Corrupt AAC file: bitrate_audio == zero';
  81. return false;
  82. }
  83. $ThisFileInfo['aac']['header']['num_program_configs'] = 1 + getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  84. $bitoffset += 4;
  85. for ($i = 0; $i < $ThisFileInfo['aac']['header']['num_program_configs']; $i++) {
  86. // http://www.audiocoding.com/wiki/index.php?page=program_config_element
  87. // buffer_fullness 20
  88. // element_instance_tag 4
  89. // object_type 2
  90. // sampling_frequency_index 4
  91. // num_front_channel_elements 4
  92. // num_side_channel_elements 4
  93. // num_back_channel_elements 4
  94. // num_lfe_channel_elements 2
  95. // num_assoc_data_elements 3
  96. // num_valid_cc_elements 4
  97. // mono_mixdown_present 1
  98. // mono_mixdown_element_number 4 if mono_mixdown_present == 1
  99. // stereo_mixdown_present 1
  100. // stereo_mixdown_element_number 4 if stereo_mixdown_present == 1
  101. // matrix_mixdown_idx_present 1
  102. // matrix_mixdown_idx 2 if matrix_mixdown_idx_present == 1
  103. // pseudo_surround_enable 1 if matrix_mixdown_idx_present == 1
  104. // for (i = 0; i < num_front_channel_elements; i++) {
  105. // front_element_is_cpe[i] 1
  106. // front_element_tag_select[i] 4
  107. // }
  108. // for (i = 0; i < num_side_channel_elements; i++) {
  109. // side_element_is_cpe[i] 1
  110. // side_element_tag_select[i] 4
  111. // }
  112. // for (i = 0; i < num_back_channel_elements; i++) {
  113. // back_element_is_cpe[i] 1
  114. // back_element_tag_select[i] 4
  115. // }
  116. // for (i = 0; i < num_lfe_channel_elements; i++) {
  117. // lfe_element_tag_select[i] 4
  118. // }
  119. // for (i = 0; i < num_assoc_data_elements; i++) {
  120. // assoc_data_element_tag_select[i] 4
  121. // }
  122. // for (i = 0; i < num_valid_cc_elements; i++) {
  123. // cc_element_is_ind_sw[i] 1
  124. // valid_cc_element_tag_select[i] 4
  125. // }
  126. // byte_alignment() VAR
  127. // comment_field_bytes 8
  128. // for (i = 0; i < comment_field_bytes; i++) {
  129. // comment_field_data[i] 8
  130. // }
  131. if (!$ThisFileInfo['aac']['header']['is_vbr']) {
  132. $ThisFileInfo['aac']['program_configs'][$i]['buffer_fullness'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 20));
  133. $bitoffset += 20;
  134. }
  135. $ThisFileInfo['aac']['program_configs'][$i]['element_instance_tag'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  136. $bitoffset += 4;
  137. $ThisFileInfo['aac']['program_configs'][$i]['object_type'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 2));
  138. $bitoffset += 2;
  139. $ThisFileInfo['aac']['program_configs'][$i]['sampling_frequency_index'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  140. $bitoffset += 4;
  141. $ThisFileInfo['aac']['program_configs'][$i]['num_front_channel_elements'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  142. $bitoffset += 4;
  143. $ThisFileInfo['aac']['program_configs'][$i]['num_side_channel_elements'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  144. $bitoffset += 4;
  145. $ThisFileInfo['aac']['program_configs'][$i]['num_back_channel_elements'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  146. $bitoffset += 4;
  147. $ThisFileInfo['aac']['program_configs'][$i]['num_lfe_channel_elements'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 2));
  148. $bitoffset += 2;
  149. $ThisFileInfo['aac']['program_configs'][$i]['num_assoc_data_elements'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 3));
  150. $bitoffset += 3;
  151. $ThisFileInfo['aac']['program_configs'][$i]['num_valid_cc_elements'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  152. $bitoffset += 4;
  153. $ThisFileInfo['aac']['program_configs'][$i]['mono_mixdown_present'] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  154. $bitoffset += 1;
  155. if ($ThisFileInfo['aac']['program_configs'][$i]['mono_mixdown_present']) {
  156. $ThisFileInfo['aac']['program_configs'][$i]['mono_mixdown_element_number'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  157. $bitoffset += 4;
  158. }
  159. $ThisFileInfo['aac']['program_configs'][$i]['stereo_mixdown_present'] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  160. $bitoffset += 1;
  161. if ($ThisFileInfo['aac']['program_configs'][$i]['stereo_mixdown_present']) {
  162. $ThisFileInfo['aac']['program_configs'][$i]['stereo_mixdown_element_number'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  163. $bitoffset += 4;
  164. }
  165. $ThisFileInfo['aac']['program_configs'][$i]['matrix_mixdown_idx_present'] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  166. $bitoffset += 1;
  167. if ($ThisFileInfo['aac']['program_configs'][$i]['matrix_mixdown_idx_present']) {
  168. $ThisFileInfo['aac']['program_configs'][$i]['matrix_mixdown_idx'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 2));
  169. $bitoffset += 2;
  170. $ThisFileInfo['aac']['program_configs'][$i]['pseudo_surround_enable'] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  171. $bitoffset += 1;
  172. }
  173. for ($j = 0; $j < $ThisFileInfo['aac']['program_configs'][$i]['num_front_channel_elements']; $j++) {
  174. $ThisFileInfo['aac']['program_configs'][$i]['front_element_is_cpe'][$j] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  175. $bitoffset += 1;
  176. $ThisFileInfo['aac']['program_configs'][$i]['front_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  177. $bitoffset += 4;
  178. }
  179. for ($j = 0; $j < $ThisFileInfo['aac']['program_configs'][$i]['num_side_channel_elements']; $j++) {
  180. $ThisFileInfo['aac']['program_configs'][$i]['side_element_is_cpe'][$j] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  181. $bitoffset += 1;
  182. $ThisFileInfo['aac']['program_configs'][$i]['side_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  183. $bitoffset += 4;
  184. }
  185. for ($j = 0; $j < $ThisFileInfo['aac']['program_configs'][$i]['num_back_channel_elements']; $j++) {
  186. $ThisFileInfo['aac']['program_configs'][$i]['back_element_is_cpe'][$j] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  187. $bitoffset += 1;
  188. $ThisFileInfo['aac']['program_configs'][$i]['back_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  189. $bitoffset += 4;
  190. }
  191. for ($j = 0; $j < $ThisFileInfo['aac']['program_configs'][$i]['num_lfe_channel_elements']; $j++) {
  192. $ThisFileInfo['aac']['program_configs'][$i]['lfe_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  193. $bitoffset += 4;
  194. }
  195. for ($j = 0; $j < $ThisFileInfo['aac']['program_configs'][$i]['num_assoc_data_elements']; $j++) {
  196. $ThisFileInfo['aac']['program_configs'][$i]['assoc_data_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  197. $bitoffset += 4;
  198. }
  199. for ($j = 0; $j < $ThisFileInfo['aac']['program_configs'][$i]['num_valid_cc_elements']; $j++) {
  200. $ThisFileInfo['aac']['program_configs'][$i]['cc_element_is_ind_sw'][$j] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  201. $bitoffset += 1;
  202. $ThisFileInfo['aac']['program_configs'][$i]['valid_cc_element_tag_select'][$j] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  203. $bitoffset += 4;
  204. }
  205. $bitoffset = ceil($bitoffset / 8) * 8;
  206. $ThisFileInfo['aac']['program_configs'][$i]['comment_field_bytes'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 8));
  207. $bitoffset += 8;
  208. $ThisFileInfo['aac']['program_configs'][$i]['comment_field'] = getid3_lib::Bin2String(substr($AACheaderBitstream, $bitoffset, 8 * $ThisFileInfo['aac']['program_configs'][$i]['comment_field_bytes']));
  209. $bitoffset += 8 * $ThisFileInfo['aac']['program_configs'][$i]['comment_field_bytes'];
  210. $ThisFileInfo['aac']['header']['profile_text'] = $this->AACprofileLookup($ThisFileInfo['aac']['program_configs'][$i]['object_type'], $ThisFileInfo['aac']['header']['mpeg_version']);
  211. $ThisFileInfo['aac']['program_configs'][$i]['sampling_frequency'] = $this->AACsampleRateLookup($ThisFileInfo['aac']['program_configs'][$i]['sampling_frequency_index']);
  212. $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['aac']['program_configs'][$i]['sampling_frequency'];
  213. $ThisFileInfo['audio']['channels'] = $this->AACchannelCountCalculate($ThisFileInfo['aac']['program_configs'][$i]);
  214. if ($ThisFileInfo['aac']['program_configs'][$i]['comment_field']) {
  215. $ThisFileInfo['aac']['comments'][] = $ThisFileInfo['aac']['program_configs'][$i]['comment_field'];
  216. }
  217. }
  218. $ThisFileInfo['playtime_seconds'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['audio']['bitrate'];
  219. $ThisFileInfo['audio']['encoder_options'] = $ThisFileInfo['aac']['header_type'].' '.$ThisFileInfo['aac']['header']['profile_text'];
  220. return true;
  221. } else {
  222. unset($ThisFileInfo['fileformat']);
  223. unset($ThisFileInfo['aac']);
  224. $ThisFileInfo['error'][] = 'AAC-ADIF synch not found at offset '.$ThisFileInfo['avdataoffset'].' (expected "ADIF", found "'.substr($AACheader, 0, 4).'" instead)';
  225. return false;
  226. }
  227. }
  228. function getAACADTSheaderFilepointer(&$fd, &$ThisFileInfo, $MaxFramesToScan=1000000, $ReturnExtendedInfo=false) {
  229. // based loosely on code from AACfile by Jurgen Faul <jfaulØgmx.de>
  230. // http://jfaul.de/atl or http://j-faul.virtualave.net/atl/atl.html
  231. // http://faac.sourceforge.net/wiki/index.php?page=ADTS
  232. // * ADTS Fixed Header: these don't change from frame to frame
  233. // syncword 12 always: '111111111111'
  234. // ID 1 0: MPEG-4, 1: MPEG-2
  235. // layer 2 always: '00'
  236. // protection_absent 1
  237. // profile 2
  238. // sampling_frequency_index 4
  239. // private_bit 1
  240. // channel_configuration 3
  241. // original/copy 1
  242. // home 1
  243. // emphasis 2 only if ID == 0 (ie MPEG-4)
  244. // * ADTS Variable Header: these can change from frame to frame
  245. // copyright_identification_bit 1
  246. // copyright_identification_start 1
  247. // aac_frame_length 13 length of the frame including header (in bytes)
  248. // adts_buffer_fullness 11 0x7FF indicates VBR
  249. // no_raw_data_blocks_in_frame 2
  250. // * ADTS Error check
  251. // crc_check 16 only if protection_absent == 0
  252. $byteoffset = 0;
  253. $framenumber = 0;
  254. // Init bit pattern array
  255. static $decbin = array();
  256. // Populate $bindec
  257. for ($i = 0; $i < 256; $i++) {
  258. $decbin[chr($i)] = str_pad(decbin($i), 8, '0', STR_PAD_LEFT);
  259. }
  260. // used to calculate bitrate below
  261. $BitrateCache = array();
  262. while (true) {
  263. // breaks out when end-of-file encountered, or invalid data found,
  264. // or MaxFramesToScan frames have been scanned
  265. if ($byteoffset >= pow(2, 31)) {
  266. $ThisFileInfo['warning'][] = 'Unable to parse AAC file beyond '.ftell($fd).' (PHP does not support file operations beyond 2GB)';
  267. return false;
  268. }
  269. fseek($fd, $byteoffset, SEEK_SET);
  270. // First get substring
  271. $substring = fread($fd, 10);
  272. $substringlength = strlen($substring);
  273. if ($substringlength != 10) {
  274. $ThisFileInfo['error'][] = 'Failed to read 10 bytes at offset '.(ftell($fd) - $substringlength).' (only read '.$substringlength.' bytes)';
  275. return false;
  276. }
  277. // Initialise $AACheaderBitstream
  278. $AACheaderBitstream = '';
  279. // Loop thru substring chars
  280. for ($i = 0; $i < 10; $i++) {
  281. $AACheaderBitstream .= $decbin[$substring{$i}];
  282. }
  283. $bitoffset = 0;
  284. $synctest = bindec(substr($AACheaderBitstream, $bitoffset, 12));
  285. $bitoffset += 12;
  286. if ($synctest != 0x0FFF) {
  287. $ThisFileInfo['error'][] = 'Synch pattern (0x0FFF) not found at offset '.(ftell($fd) - 10).' (found 0x0'.strtoupper(dechex($synctest)).' instead)';
  288. if ($ThisFileInfo['fileformat'] == 'aac') {
  289. return true;
  290. }
  291. return false;
  292. }
  293. // Gather info for first frame only - this takes time to do 1000 times!
  294. if ($framenumber > 0) {
  295. if (!$AACheaderBitstream[$bitoffset]) {
  296. // MPEG-4
  297. $bitoffset += 20;
  298. } else {
  299. // MPEG-2
  300. $bitoffset += 18;
  301. }
  302. } else {
  303. $ThisFileInfo['aac']['header_type'] = 'ADTS';
  304. $ThisFileInfo['aac']['header']['synch'] = $synctest;
  305. $ThisFileInfo['fileformat'] = 'aac';
  306. $ThisFileInfo['audio']['dataformat'] = 'aac';
  307. $ThisFileInfo['aac']['header']['mpeg_version'] = ((substr($AACheaderBitstream, $bitoffset, 1) == '0') ? 4 : 2);
  308. $bitoffset += 1;
  309. $ThisFileInfo['aac']['header']['layer'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 2));
  310. $bitoffset += 2;
  311. if ($ThisFileInfo['aac']['header']['layer'] != 0) {
  312. $ThisFileInfo['error'][] = 'Layer error - expected 0x00, found 0x'.dechex($ThisFileInfo['aac']['header']['layer']).' instead';
  313. return false;
  314. }
  315. $ThisFileInfo['aac']['header']['crc_present'] = ((substr($AACheaderBitstream, $bitoffset, 1) == '0') ? true : false);
  316. $bitoffset += 1;
  317. $ThisFileInfo['aac']['header']['profile_id'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 2));
  318. $bitoffset += 2;
  319. $ThisFileInfo['aac']['header']['profile_text'] = $this->AACprofileLookup($ThisFileInfo['aac']['header']['profile_id'], $ThisFileInfo['aac']['header']['mpeg_version']);
  320. $ThisFileInfo['aac']['header']['sample_frequency_index'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 4));
  321. $bitoffset += 4;
  322. $ThisFileInfo['aac']['header']['sample_frequency'] = $this->AACsampleRateLookup($ThisFileInfo['aac']['header']['sample_frequency_index']);
  323. if ($ThisFileInfo['aac']['header']['sample_frequency'] == 0) {
  324. $ThisFileInfo['error'][] = 'Corrupt AAC file: sample_frequency == zero';
  325. return false;
  326. }
  327. $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['aac']['header']['sample_frequency'];
  328. $ThisFileInfo['aac']['header']['private'] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  329. $bitoffset += 1;
  330. $ThisFileInfo['aac']['header']['channel_configuration'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 3));
  331. $bitoffset += 3;
  332. $ThisFileInfo['audio']['channels'] = $ThisFileInfo['aac']['header']['channel_configuration'];
  333. $ThisFileInfo['aac']['header']['original'] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  334. $bitoffset += 1;
  335. $ThisFileInfo['aac']['header']['home'] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  336. $bitoffset += 1;
  337. if ($ThisFileInfo['aac']['header']['mpeg_version'] == 4) {
  338. $ThisFileInfo['aac']['header']['emphasis'] = getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 2));
  339. $bitoffset += 2;
  340. }
  341. if ($ReturnExtendedInfo) {
  342. $ThisFileInfo['aac'][$framenumber]['copyright_id_bit'] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  343. $bitoffset += 1;
  344. $ThisFileInfo['aac'][$framenumber]['copyright_id_start'] = (bool) getid3_lib::Bin2Dec(substr($AACheaderBitstream, $bitoffset, 1));
  345. $bitoffset += 1;
  346. } else {
  347. $bitoffset += 2;
  348. }
  349. }
  350. $FrameLength = bindec(substr($AACheaderBitstream, $bitoffset, 13));
  351. if (!isset($BitrateCache[$FrameLength])) {
  352. $BitrateCache[$FrameLength] = ($ThisFileInfo['aac']['header']['sample_frequency'] / 1024) * $FrameLength * 8;
  353. }
  354. @$ThisFileInfo['aac']['bitrate_distribution'][$BitrateCache[$FrameLength]]++;
  355. $ThisFileInfo['aac'][$framenumber]['aac_frame_length'] = $FrameLength;
  356. $bitoffset += 13;
  357. $ThisFileInfo['aac'][$framenumber]['adts_buffer_fullness'] = bindec(substr($AACheaderBitstream, $bitoffset, 11));
  358. $bitoffset += 11;
  359. if ($ThisFileInfo['aac'][$framenumber]['adts_buffer_fullness'] == 0x07FF) {
  360. $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
  361. } else {
  362. $ThisFileInfo['audio']['bitrate_mode'] = 'cbr';
  363. }
  364. $ThisFileInfo['aac'][$framenumber]['num_raw_data_blocks'] = bindec(substr($AACheaderBitstream, $bitoffset, 2));
  365. $bitoffset += 2;
  366. if ($ThisFileInfo['aac']['header']['crc_present']) {
  367. //$ThisFileInfo['aac'][$framenumber]['crc'] = bindec(substr($AACheaderBitstream, $bitoffset, 16));
  368. $bitoffset += 16;
  369. }
  370. if (!$ReturnExtendedInfo) {
  371. unset($ThisFileInfo['aac'][$framenumber]);
  372. }
  373. $byteoffset += $FrameLength;
  374. if ((++$framenumber < $MaxFramesToScan) && (($byteoffset + 10) < $ThisFileInfo['avdataend'])) {
  375. // keep scanning
  376. } else {
  377. $ThisFileInfo['aac']['frames'] = $framenumber;
  378. $ThisFileInfo['playtime_seconds'] = ($ThisFileInfo['avdataend'] / $byteoffset) * (($framenumber * 1024) / $ThisFileInfo['aac']['header']['sample_frequency']); // (1 / % of file scanned) * (samples / (samples/sec)) = seconds
  379. if ($ThisFileInfo['playtime_seconds'] == 0) {
  380. $ThisFileInfo['error'][] = 'Corrupt AAC file: playtime_seconds == zero';
  381. return false;
  382. }
  383. $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds'];
  384. ksort($ThisFileInfo['aac']['bitrate_distribution']);
  385. $ThisFileInfo['audio']['encoder_options'] = $ThisFileInfo['aac']['header_type'].' '.$ThisFileInfo['aac']['header']['profile_text'];
  386. return true;
  387. }
  388. }
  389. // should never get here.
  390. }
  391. function AACsampleRateLookup($samplerateid) {
  392. static $AACsampleRateLookup = array();
  393. if (empty($AACsampleRateLookup)) {
  394. $AACsampleRateLookup[0] = 96000;
  395. $AACsampleRateLookup[1] = 88200;
  396. $AACsampleRateLookup[2] = 64000;
  397. $AACsampleRateLookup[3] = 48000;
  398. $AACsampleRateLookup[4] = 44100;
  399. $AACsampleRateLookup[5] = 32000;
  400. $AACsampleRateLookup[6] = 24000;
  401. $AACsampleRateLookup[7] = 22050;
  402. $AACsampleRateLookup[8] = 16000;
  403. $AACsampleRateLookup[9] = 12000;
  404. $AACsampleRateLookup[10] = 11025;
  405. $AACsampleRateLookup[11] = 8000;
  406. $AACsampleRateLookup[12] = 0;
  407. $AACsampleRateLookup[13] = 0;
  408. $AACsampleRateLookup[14] = 0;
  409. $AACsampleRateLookup[15] = 0;
  410. }
  411. return (isset($AACsampleRateLookup[$samplerateid]) ? $AACsampleRateLookup[$samplerateid] : 'invalid');
  412. }
  413. function AACprofileLookup($profileid, $mpegversion) {
  414. static $AACprofileLookup = array();
  415. if (empty($AACprofileLookup)) {
  416. $AACprofileLookup[2][0] = 'Main profile';
  417. $AACprofileLookup[2][1] = 'Low Complexity profile (LC)';
  418. $AACprofileLookup[2][2] = 'Scalable Sample Rate profile (SSR)';
  419. $AACprofileLookup[2][3] = '(reserved)';
  420. $AACprofileLookup[4][0] = 'AAC_MAIN';
  421. $AACprofileLookup[4][1] = 'AAC_LC';
  422. $AACprofileLookup[4][2] = 'AAC_SSR';
  423. $AACprofileLookup[4][3] = 'AAC_LTP';
  424. }
  425. return (isset($AACprofileLookup[$mpegversion][$profileid]) ? $AACprofileLookup[$mpegversion][$profileid] : 'invalid');
  426. }
  427. function AACchannelCountCalculate($program_configs) {
  428. $channels = 0;
  429. for ($i = 0; $i < $program_configs['num_front_channel_elements']; $i++) {
  430. $channels++;
  431. if ($program_configs['front_element_is_cpe'][$i]) {
  432. // each front element is channel pair (CPE = Channel Pair Element)
  433. $channels++;
  434. }
  435. }
  436. for ($i = 0; $i < $program_configs['num_side_channel_elements']; $i++) {
  437. $channels++;
  438. if ($program_configs['side_element_is_cpe'][$i]) {
  439. // each side element is channel pair (CPE = Channel Pair Element)
  440. $channels++;
  441. }
  442. }
  443. for ($i = 0; $i < $program_configs['num_back_channel_elements']; $i++) {
  444. $channels++;
  445. if ($program_configs['back_element_is_cpe'][$i]) {
  446. // each back element is channel pair (CPE = Channel Pair Element)
  447. $channels++;
  448. }
  449. }
  450. for ($i = 0; $i < $program_configs['num_lfe_channel_elements']; $i++) {
  451. $channels++;
  452. }
  453. return $channels;
  454. }
  455. }
  456. ?>