PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/getid3/module.audio.aac.php

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