PageRenderTime 37ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/Vendor/getid3/module.audio.mp3.php

https://bitbucket.org/nova-atlantis/simple-server-media-player
PHP | 2012 lines | 1432 code | 283 blank | 297 comment | 398 complexity | eb87257b2f0507f4ac5591d4226dfc86 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. // also https://github.com/JamesHeinrich/getID3 //
  7. /////////////////////////////////////////////////////////////////
  8. // See readme.txt for more details //
  9. /////////////////////////////////////////////////////////////////
  10. // //
  11. // module.audio.mp3.php //
  12. // module for analyzing MP3 files //
  13. // dependencies: NONE //
  14. // ///
  15. /////////////////////////////////////////////////////////////////
  16. // number of frames to scan to determine if MPEG-audio sequence is valid
  17. // Lower this number to 5-20 for faster scanning
  18. // Increase this number to 50+ for most accurate detection of valid VBR/CBR
  19. // mpeg-audio streams
  20. define('GETID3_MP3_VALID_CHECK_FRAMES', 35);
  21. class getid3_mp3 extends getid3_handler
  22. {
  23. public $allow_bruteforce = false; // forces getID3() to scan the file byte-by-byte and log all the valid audio frame headers - extremely slow, unrecommended, but may provide data from otherwise-unusuable files
  24. public function Analyze() {
  25. $info = &$this->getid3->info;
  26. $initialOffset = $info['avdataoffset'];
  27. if (!$this->getOnlyMPEGaudioInfo($info['avdataoffset'])) {
  28. if ($this->allow_bruteforce) {
  29. $info['error'][] = 'Rescanning file in BruteForce mode';
  30. $this->getOnlyMPEGaudioInfoBruteForce($this->getid3->fp, $info);
  31. }
  32. }
  33. if (isset($info['mpeg']['audio']['bitrate_mode'])) {
  34. $info['audio']['bitrate_mode'] = strtolower($info['mpeg']['audio']['bitrate_mode']);
  35. }
  36. if (((isset($info['id3v2']['headerlength']) && ($info['avdataoffset'] > $info['id3v2']['headerlength'])) || (!isset($info['id3v2']) && ($info['avdataoffset'] > 0) && ($info['avdataoffset'] != $initialOffset)))) {
  37. $synchoffsetwarning = 'Unknown data before synch ';
  38. if (isset($info['id3v2']['headerlength'])) {
  39. $synchoffsetwarning .= '(ID3v2 header ends at '.$info['id3v2']['headerlength'].', then '.($info['avdataoffset'] - $info['id3v2']['headerlength']).' bytes garbage, ';
  40. } elseif ($initialOffset > 0) {
  41. $synchoffsetwarning .= '(should be at '.$initialOffset.', ';
  42. } else {
  43. $synchoffsetwarning .= '(should be at beginning of file, ';
  44. }
  45. $synchoffsetwarning .= 'synch detected at '.$info['avdataoffset'].')';
  46. if (isset($info['audio']['bitrate_mode']) && ($info['audio']['bitrate_mode'] == 'cbr')) {
  47. if (!empty($info['id3v2']['headerlength']) && (($info['avdataoffset'] - $info['id3v2']['headerlength']) == $info['mpeg']['audio']['framelength'])) {
  48. $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90-3.92) DLL in CBR mode.';
  49. $info['audio']['codec'] = 'LAME';
  50. $CurrentDataLAMEversionString = 'LAME3.';
  51. } elseif (empty($info['id3v2']['headerlength']) && ($info['avdataoffset'] == $info['mpeg']['audio']['framelength'])) {
  52. $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90 - 3.92) DLL in CBR mode.';
  53. $info['audio']['codec'] = 'LAME';
  54. $CurrentDataLAMEversionString = 'LAME3.';
  55. }
  56. }
  57. $info['warning'][] = $synchoffsetwarning;
  58. }
  59. if (isset($info['mpeg']['audio']['LAME'])) {
  60. $info['audio']['codec'] = 'LAME';
  61. if (!empty($info['mpeg']['audio']['LAME']['long_version'])) {
  62. $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['long_version'], "\x00");
  63. } elseif (!empty($info['mpeg']['audio']['LAME']['short_version'])) {
  64. $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['short_version'], "\x00");
  65. }
  66. }
  67. $CurrentDataLAMEversionString = (!empty($CurrentDataLAMEversionString) ? $CurrentDataLAMEversionString : (isset($info['audio']['encoder']) ? $info['audio']['encoder'] : ''));
  68. if (!empty($CurrentDataLAMEversionString) && (substr($CurrentDataLAMEversionString, 0, 6) == 'LAME3.') && !preg_match('[0-9\)]', substr($CurrentDataLAMEversionString, -1))) {
  69. // a version number of LAME that does not end with a number like "LAME3.92"
  70. // or with a closing parenthesis like "LAME3.88 (alpha)"
  71. // or a version of LAME with the LAMEtag-not-filled-in-DLL-mode bug (3.90-3.92)
  72. // not sure what the actual last frame length will be, but will be less than or equal to 1441
  73. $PossiblyLongerLAMEversion_FrameLength = 1441;
  74. // Not sure what version of LAME this is - look in padding of last frame for longer version string
  75. $PossibleLAMEversionStringOffset = $info['avdataend'] - $PossiblyLongerLAMEversion_FrameLength;
  76. $this->fseek($PossibleLAMEversionStringOffset);
  77. $PossiblyLongerLAMEversion_Data = $this->fread($PossiblyLongerLAMEversion_FrameLength);
  78. switch (substr($CurrentDataLAMEversionString, -1)) {
  79. case 'a':
  80. case 'b':
  81. // "LAME3.94a" will have a longer version string of "LAME3.94 (alpha)" for example
  82. // need to trim off "a" to match longer string
  83. $CurrentDataLAMEversionString = substr($CurrentDataLAMEversionString, 0, -1);
  84. break;
  85. }
  86. if (($PossiblyLongerLAMEversion_String = strstr($PossiblyLongerLAMEversion_Data, $CurrentDataLAMEversionString)) !== false) {
  87. if (substr($PossiblyLongerLAMEversion_String, 0, strlen($CurrentDataLAMEversionString)) == $CurrentDataLAMEversionString) {
  88. $PossiblyLongerLAMEversion_NewString = substr($PossiblyLongerLAMEversion_String, 0, strspn($PossiblyLongerLAMEversion_String, 'LAME0123456789., (abcdefghijklmnopqrstuvwxyzJFSOND)')); //"LAME3.90.3" "LAME3.87 (beta 1, Sep 27 2000)" "LAME3.88 (beta)"
  89. if (empty($info['audio']['encoder']) || (strlen($PossiblyLongerLAMEversion_NewString) > strlen($info['audio']['encoder']))) {
  90. $info['audio']['encoder'] = $PossiblyLongerLAMEversion_NewString;
  91. }
  92. }
  93. }
  94. }
  95. if (!empty($info['audio']['encoder'])) {
  96. $info['audio']['encoder'] = rtrim($info['audio']['encoder'], "\x00 ");
  97. }
  98. switch (isset($info['mpeg']['audio']['layer']) ? $info['mpeg']['audio']['layer'] : '') {
  99. case 1:
  100. case 2:
  101. $info['audio']['dataformat'] = 'mp'.$info['mpeg']['audio']['layer'];
  102. break;
  103. }
  104. if (isset($info['fileformat']) && ($info['fileformat'] == 'mp3')) {
  105. switch ($info['audio']['dataformat']) {
  106. case 'mp1':
  107. case 'mp2':
  108. case 'mp3':
  109. $info['fileformat'] = $info['audio']['dataformat'];
  110. break;
  111. default:
  112. $info['warning'][] = 'Expecting [audio][dataformat] to be mp1/mp2/mp3 when fileformat == mp3, [audio][dataformat] actually "'.$info['audio']['dataformat'].'"';
  113. break;
  114. }
  115. }
  116. if (empty($info['fileformat'])) {
  117. unset($info['fileformat']);
  118. unset($info['audio']['bitrate_mode']);
  119. unset($info['avdataoffset']);
  120. unset($info['avdataend']);
  121. return false;
  122. }
  123. $info['mime_type'] = 'audio/mpeg';
  124. $info['audio']['lossless'] = false;
  125. // Calculate playtime
  126. if (!isset($info['playtime_seconds']) && isset($info['audio']['bitrate']) && ($info['audio']['bitrate'] > 0)) {
  127. $info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['audio']['bitrate'];
  128. }
  129. $info['audio']['encoder_options'] = $this->GuessEncoderOptions();
  130. return true;
  131. }
  132. public function GuessEncoderOptions() {
  133. // shortcuts
  134. $info = &$this->getid3->info;
  135. if (!empty($info['mpeg']['audio'])) {
  136. $thisfile_mpeg_audio = &$info['mpeg']['audio'];
  137. if (!empty($thisfile_mpeg_audio['LAME'])) {
  138. $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
  139. }
  140. }
  141. $encoder_options = '';
  142. static $NamedPresetBitrates = array(16, 24, 40, 56, 112, 128, 160, 192, 256);
  143. if (isset($thisfile_mpeg_audio['VBR_method']) && ($thisfile_mpeg_audio['VBR_method'] == 'Fraunhofer') && !empty($thisfile_mpeg_audio['VBR_quality'])) {
  144. $encoder_options = 'VBR q'.$thisfile_mpeg_audio['VBR_quality'];
  145. } elseif (!empty($thisfile_mpeg_audio_lame['preset_used']) && (!in_array($thisfile_mpeg_audio_lame['preset_used_id'], $NamedPresetBitrates))) {
  146. $encoder_options = $thisfile_mpeg_audio_lame['preset_used'];
  147. } elseif (!empty($thisfile_mpeg_audio_lame['vbr_quality'])) {
  148. static $KnownEncoderValues = array();
  149. if (empty($KnownEncoderValues)) {
  150. //$KnownEncoderValues[abrbitrate_minbitrate][vbr_quality][raw_vbr_method][raw_noise_shaping][raw_stereo_mode][ath_type][lowpass_frequency] = 'preset name';
  151. $KnownEncoderValues[0xFF][58][1][1][3][2][20500] = '--alt-preset insane'; // 3.90, 3.90.1, 3.92
  152. $KnownEncoderValues[0xFF][58][1][1][3][2][20600] = '--alt-preset insane'; // 3.90.2, 3.90.3, 3.91
  153. $KnownEncoderValues[0xFF][57][1][1][3][4][20500] = '--alt-preset insane'; // 3.94, 3.95
  154. $KnownEncoderValues['**'][78][3][2][3][2][19500] = '--alt-preset extreme'; // 3.90, 3.90.1, 3.92
  155. $KnownEncoderValues['**'][78][3][2][3][2][19600] = '--alt-preset extreme'; // 3.90.2, 3.91
  156. $KnownEncoderValues['**'][78][3][1][3][2][19600] = '--alt-preset extreme'; // 3.90.3
  157. $KnownEncoderValues['**'][78][4][2][3][2][19500] = '--alt-preset fast extreme'; // 3.90, 3.90.1, 3.92
  158. $KnownEncoderValues['**'][78][4][2][3][2][19600] = '--alt-preset fast extreme'; // 3.90.2, 3.90.3, 3.91
  159. $KnownEncoderValues['**'][78][3][2][3][4][19000] = '--alt-preset standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  160. $KnownEncoderValues['**'][78][3][1][3][4][19000] = '--alt-preset standard'; // 3.90.3
  161. $KnownEncoderValues['**'][78][4][2][3][4][19000] = '--alt-preset fast standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  162. $KnownEncoderValues['**'][78][4][1][3][4][19000] = '--alt-preset fast standard'; // 3.90.3
  163. $KnownEncoderValues['**'][88][4][1][3][3][19500] = '--r3mix'; // 3.90, 3.90.1, 3.92
  164. $KnownEncoderValues['**'][88][4][1][3][3][19600] = '--r3mix'; // 3.90.2, 3.90.3, 3.91
  165. $KnownEncoderValues['**'][67][4][1][3][4][18000] = '--r3mix'; // 3.94, 3.95
  166. $KnownEncoderValues['**'][68][3][2][3][4][18000] = '--alt-preset medium'; // 3.90.3
  167. $KnownEncoderValues['**'][68][4][2][3][4][18000] = '--alt-preset fast medium'; // 3.90.3
  168. $KnownEncoderValues[0xFF][99][1][1][1][2][0] = '--preset studio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  169. $KnownEncoderValues[0xFF][58][2][1][3][2][20600] = '--preset studio'; // 3.90.3, 3.93.1
  170. $KnownEncoderValues[0xFF][58][2][1][3][2][20500] = '--preset studio'; // 3.93
  171. $KnownEncoderValues[0xFF][57][2][1][3][4][20500] = '--preset studio'; // 3.94, 3.95
  172. $KnownEncoderValues[0xC0][88][1][1][1][2][0] = '--preset cd'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  173. $KnownEncoderValues[0xC0][58][2][2][3][2][19600] = '--preset cd'; // 3.90.3, 3.93.1
  174. $KnownEncoderValues[0xC0][58][2][2][3][2][19500] = '--preset cd'; // 3.93
  175. $KnownEncoderValues[0xC0][57][2][1][3][4][19500] = '--preset cd'; // 3.94, 3.95
  176. $KnownEncoderValues[0xA0][78][1][1][3][2][18000] = '--preset hifi'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  177. $KnownEncoderValues[0xA0][58][2][2][3][2][18000] = '--preset hifi'; // 3.90.3, 3.93, 3.93.1
  178. $KnownEncoderValues[0xA0][57][2][1][3][4][18000] = '--preset hifi'; // 3.94, 3.95
  179. $KnownEncoderValues[0x80][67][1][1][3][2][18000] = '--preset tape'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  180. $KnownEncoderValues[0x80][67][1][1][3][2][15000] = '--preset radio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  181. $KnownEncoderValues[0x70][67][1][1][3][2][15000] = '--preset fm'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  182. $KnownEncoderValues[0x70][58][2][2][3][2][16000] = '--preset tape/radio/fm'; // 3.90.3, 3.93, 3.93.1
  183. $KnownEncoderValues[0x70][57][2][1][3][4][16000] = '--preset tape/radio/fm'; // 3.94, 3.95
  184. $KnownEncoderValues[0x38][58][2][2][0][2][10000] = '--preset voice'; // 3.90.3, 3.93, 3.93.1
  185. $KnownEncoderValues[0x38][57][2][1][0][4][15000] = '--preset voice'; // 3.94, 3.95
  186. $KnownEncoderValues[0x38][57][2][1][0][4][16000] = '--preset voice'; // 3.94a14
  187. $KnownEncoderValues[0x28][65][1][1][0][2][7500] = '--preset mw-us'; // 3.90, 3.90.1, 3.92
  188. $KnownEncoderValues[0x28][65][1][1][0][2][7600] = '--preset mw-us'; // 3.90.2, 3.91
  189. $KnownEncoderValues[0x28][58][2][2][0][2][7000] = '--preset mw-us'; // 3.90.3, 3.93, 3.93.1
  190. $KnownEncoderValues[0x28][57][2][1][0][4][10500] = '--preset mw-us'; // 3.94, 3.95
  191. $KnownEncoderValues[0x28][57][2][1][0][4][11200] = '--preset mw-us'; // 3.94a14
  192. $KnownEncoderValues[0x28][57][2][1][0][4][8800] = '--preset mw-us'; // 3.94a15
  193. $KnownEncoderValues[0x18][58][2][2][0][2][4000] = '--preset phon+/lw/mw-eu/sw'; // 3.90.3, 3.93.1
  194. $KnownEncoderValues[0x18][58][2][2][0][2][3900] = '--preset phon+/lw/mw-eu/sw'; // 3.93
  195. $KnownEncoderValues[0x18][57][2][1][0][4][5900] = '--preset phon+/lw/mw-eu/sw'; // 3.94, 3.95
  196. $KnownEncoderValues[0x18][57][2][1][0][4][6200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a14
  197. $KnownEncoderValues[0x18][57][2][1][0][4][3200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a15
  198. $KnownEncoderValues[0x10][58][2][2][0][2][3800] = '--preset phone'; // 3.90.3, 3.93.1
  199. $KnownEncoderValues[0x10][58][2][2][0][2][3700] = '--preset phone'; // 3.93
  200. $KnownEncoderValues[0x10][57][2][1][0][4][5600] = '--preset phone'; // 3.94, 3.95
  201. }
  202. if (isset($KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) {
  203. $encoder_options = $KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']];
  204. } elseif (isset($KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) {
  205. $encoder_options = $KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']];
  206. } elseif ($info['audio']['bitrate_mode'] == 'vbr') {
  207. // http://gabriel.mp3-tech.org/mp3infotag.html
  208. // int Quality = (100 - 10 * gfp->VBR_q - gfp->quality)h
  209. $LAME_V_value = 10 - ceil($thisfile_mpeg_audio_lame['vbr_quality'] / 10);
  210. $LAME_q_value = 100 - $thisfile_mpeg_audio_lame['vbr_quality'] - ($LAME_V_value * 10);
  211. $encoder_options = '-V'.$LAME_V_value.' -q'.$LAME_q_value;
  212. } elseif ($info['audio']['bitrate_mode'] == 'cbr') {
  213. $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
  214. } else {
  215. $encoder_options = strtoupper($info['audio']['bitrate_mode']);
  216. }
  217. } elseif (!empty($thisfile_mpeg_audio_lame['bitrate_abr'])) {
  218. $encoder_options = 'ABR'.$thisfile_mpeg_audio_lame['bitrate_abr'];
  219. } elseif (!empty($info['audio']['bitrate'])) {
  220. if ($info['audio']['bitrate_mode'] == 'cbr') {
  221. $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
  222. } else {
  223. $encoder_options = strtoupper($info['audio']['bitrate_mode']);
  224. }
  225. }
  226. if (!empty($thisfile_mpeg_audio_lame['bitrate_min'])) {
  227. $encoder_options .= ' -b'.$thisfile_mpeg_audio_lame['bitrate_min'];
  228. }
  229. if (!empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev']) || !empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'])) {
  230. $encoder_options .= ' --nogap';
  231. }
  232. if (!empty($thisfile_mpeg_audio_lame['lowpass_frequency'])) {
  233. $ExplodedOptions = explode(' ', $encoder_options, 4);
  234. if ($ExplodedOptions[0] == '--r3mix') {
  235. $ExplodedOptions[1] = 'r3mix';
  236. }
  237. switch ($ExplodedOptions[0]) {
  238. case '--preset':
  239. case '--alt-preset':
  240. case '--r3mix':
  241. if ($ExplodedOptions[1] == 'fast') {
  242. $ExplodedOptions[1] .= ' '.$ExplodedOptions[2];
  243. }
  244. switch ($ExplodedOptions[1]) {
  245. case 'portable':
  246. case 'medium':
  247. case 'standard':
  248. case 'extreme':
  249. case 'insane':
  250. case 'fast portable':
  251. case 'fast medium':
  252. case 'fast standard':
  253. case 'fast extreme':
  254. case 'fast insane':
  255. case 'r3mix':
  256. static $ExpectedLowpass = array(
  257. 'insane|20500' => 20500,
  258. 'insane|20600' => 20600, // 3.90.2, 3.90.3, 3.91
  259. 'medium|18000' => 18000,
  260. 'fast medium|18000' => 18000,
  261. 'extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95
  262. 'extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1
  263. 'fast extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95
  264. 'fast extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1
  265. 'standard|19000' => 19000,
  266. 'fast standard|19000' => 19000,
  267. 'r3mix|19500' => 19500, // 3.90, 3.90.1, 3.92
  268. 'r3mix|19600' => 19600, // 3.90.2, 3.90.3, 3.91
  269. 'r3mix|18000' => 18000, // 3.94, 3.95
  270. );
  271. if (!isset($ExpectedLowpass[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio_lame['lowpass_frequency']]) && ($thisfile_mpeg_audio_lame['lowpass_frequency'] < 22050) && (round($thisfile_mpeg_audio_lame['lowpass_frequency'] / 1000) < round($thisfile_mpeg_audio['sample_rate'] / 2000))) {
  272. $encoder_options .= ' --lowpass '.$thisfile_mpeg_audio_lame['lowpass_frequency'];
  273. }
  274. break;
  275. default:
  276. break;
  277. }
  278. break;
  279. }
  280. }
  281. if (isset($thisfile_mpeg_audio_lame['raw']['source_sample_freq'])) {
  282. if (($thisfile_mpeg_audio['sample_rate'] == 44100) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 1)) {
  283. $encoder_options .= ' --resample 44100';
  284. } elseif (($thisfile_mpeg_audio['sample_rate'] == 48000) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 2)) {
  285. $encoder_options .= ' --resample 48000';
  286. } elseif ($thisfile_mpeg_audio['sample_rate'] < 44100) {
  287. switch ($thisfile_mpeg_audio_lame['raw']['source_sample_freq']) {
  288. case 0: // <= 32000
  289. // may or may not be same as source frequency - ignore
  290. break;
  291. case 1: // 44100
  292. case 2: // 48000
  293. case 3: // 48000+
  294. $ExplodedOptions = explode(' ', $encoder_options, 4);
  295. switch ($ExplodedOptions[0]) {
  296. case '--preset':
  297. case '--alt-preset':
  298. switch ($ExplodedOptions[1]) {
  299. case 'fast':
  300. case 'portable':
  301. case 'medium':
  302. case 'standard':
  303. case 'extreme':
  304. case 'insane':
  305. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  306. break;
  307. default:
  308. static $ExpectedResampledRate = array(
  309. 'phon+/lw/mw-eu/sw|16000' => 16000,
  310. 'mw-us|24000' => 24000, // 3.95
  311. 'mw-us|32000' => 32000, // 3.93
  312. 'mw-us|16000' => 16000, // 3.92
  313. 'phone|16000' => 16000,
  314. 'phone|11025' => 11025, // 3.94a15
  315. 'radio|32000' => 32000, // 3.94a15
  316. 'fm/radio|32000' => 32000, // 3.92
  317. 'fm|32000' => 32000, // 3.90
  318. 'voice|32000' => 32000);
  319. if (!isset($ExpectedResampledRate[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio['sample_rate']])) {
  320. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  321. }
  322. break;
  323. }
  324. break;
  325. case '--r3mix':
  326. default:
  327. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  328. break;
  329. }
  330. break;
  331. }
  332. }
  333. }
  334. if (empty($encoder_options) && !empty($info['audio']['bitrate']) && !empty($info['audio']['bitrate_mode'])) {
  335. //$encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
  336. $encoder_options = strtoupper($info['audio']['bitrate_mode']);
  337. }
  338. return $encoder_options;
  339. }
  340. public function decodeMPEGaudioHeader($offset, &$info, $recursivesearch=true, $ScanAsCBR=false, $FastMPEGheaderScan=false) {
  341. static $MPEGaudioVersionLookup;
  342. static $MPEGaudioLayerLookup;
  343. static $MPEGaudioBitrateLookup;
  344. static $MPEGaudioFrequencyLookup;
  345. static $MPEGaudioChannelModeLookup;
  346. static $MPEGaudioModeExtensionLookup;
  347. static $MPEGaudioEmphasisLookup;
  348. if (empty($MPEGaudioVersionLookup)) {
  349. $MPEGaudioVersionLookup = self::MPEGaudioVersionArray();
  350. $MPEGaudioLayerLookup = self::MPEGaudioLayerArray();
  351. $MPEGaudioBitrateLookup = self::MPEGaudioBitrateArray();
  352. $MPEGaudioFrequencyLookup = self::MPEGaudioFrequencyArray();
  353. $MPEGaudioChannelModeLookup = self::MPEGaudioChannelModeArray();
  354. $MPEGaudioModeExtensionLookup = self::MPEGaudioModeExtensionArray();
  355. $MPEGaudioEmphasisLookup = self::MPEGaudioEmphasisArray();
  356. }
  357. if ($this->fseek($offset) != 0) {
  358. $info['error'][] = 'decodeMPEGaudioHeader() failed to seek to next offset at '.$offset;
  359. return false;
  360. }
  361. //$headerstring = $this->fread(1441); // worst-case max length = 32kHz @ 320kbps layer 3 = 1441 bytes/frame
  362. $headerstring = $this->fread(226); // LAME header at offset 36 + 190 bytes of Xing/LAME data
  363. // MP3 audio frame structure:
  364. // $aa $aa $aa $aa [$bb $bb] $cc...
  365. // where $aa..$aa is the four-byte mpeg-audio header (below)
  366. // $bb $bb is the optional 2-byte CRC
  367. // and $cc... is the audio data
  368. $head4 = substr($headerstring, 0, 4);
  369. static $MPEGaudioHeaderDecodeCache = array();
  370. if (isset($MPEGaudioHeaderDecodeCache[$head4])) {
  371. $MPEGheaderRawArray = $MPEGaudioHeaderDecodeCache[$head4];
  372. } else {
  373. $MPEGheaderRawArray = self::MPEGaudioHeaderDecode($head4);
  374. $MPEGaudioHeaderDecodeCache[$head4] = $MPEGheaderRawArray;
  375. }
  376. static $MPEGaudioHeaderValidCache = array();
  377. if (!isset($MPEGaudioHeaderValidCache[$head4])) { // Not in cache
  378. //$MPEGaudioHeaderValidCache[$head4] = self::MPEGaudioHeaderValid($MPEGheaderRawArray, false, true); // allow badly-formatted freeformat (from LAME 3.90 - 3.93.1)
  379. $MPEGaudioHeaderValidCache[$head4] = self::MPEGaudioHeaderValid($MPEGheaderRawArray, false, false);
  380. }
  381. // shortcut
  382. if (!isset($info['mpeg']['audio'])) {
  383. $info['mpeg']['audio'] = array();
  384. }
  385. $thisfile_mpeg_audio = &$info['mpeg']['audio'];
  386. if ($MPEGaudioHeaderValidCache[$head4]) {
  387. $thisfile_mpeg_audio['raw'] = $MPEGheaderRawArray;
  388. } else {
  389. $info['error'][] = 'Invalid MPEG audio header ('.getid3_lib::PrintHexBytes($head4).') at offset '.$offset;
  390. return false;
  391. }
  392. if (!$FastMPEGheaderScan) {
  393. $thisfile_mpeg_audio['version'] = $MPEGaudioVersionLookup[$thisfile_mpeg_audio['raw']['version']];
  394. $thisfile_mpeg_audio['layer'] = $MPEGaudioLayerLookup[$thisfile_mpeg_audio['raw']['layer']];
  395. $thisfile_mpeg_audio['channelmode'] = $MPEGaudioChannelModeLookup[$thisfile_mpeg_audio['raw']['channelmode']];
  396. $thisfile_mpeg_audio['channels'] = (($thisfile_mpeg_audio['channelmode'] == 'mono') ? 1 : 2);
  397. $thisfile_mpeg_audio['sample_rate'] = $MPEGaudioFrequencyLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['raw']['sample_rate']];
  398. $thisfile_mpeg_audio['protection'] = !$thisfile_mpeg_audio['raw']['protection'];
  399. $thisfile_mpeg_audio['private'] = (bool) $thisfile_mpeg_audio['raw']['private'];
  400. $thisfile_mpeg_audio['modeextension'] = $MPEGaudioModeExtensionLookup[$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['modeextension']];
  401. $thisfile_mpeg_audio['copyright'] = (bool) $thisfile_mpeg_audio['raw']['copyright'];
  402. $thisfile_mpeg_audio['original'] = (bool) $thisfile_mpeg_audio['raw']['original'];
  403. $thisfile_mpeg_audio['emphasis'] = $MPEGaudioEmphasisLookup[$thisfile_mpeg_audio['raw']['emphasis']];
  404. $info['audio']['channels'] = $thisfile_mpeg_audio['channels'];
  405. $info['audio']['sample_rate'] = $thisfile_mpeg_audio['sample_rate'];
  406. if ($thisfile_mpeg_audio['protection']) {
  407. $thisfile_mpeg_audio['crc'] = getid3_lib::BigEndian2Int(substr($headerstring, 4, 2));
  408. }
  409. }
  410. if ($thisfile_mpeg_audio['raw']['bitrate'] == 15) {
  411. // http://www.hydrogenaudio.org/?act=ST&f=16&t=9682&st=0
  412. $info['warning'][] = 'Invalid bitrate index (15), this is a known bug in free-format MP3s encoded by LAME v3.90 - 3.93.1';
  413. $thisfile_mpeg_audio['raw']['bitrate'] = 0;
  414. }
  415. $thisfile_mpeg_audio['padding'] = (bool) $thisfile_mpeg_audio['raw']['padding'];
  416. $thisfile_mpeg_audio['bitrate'] = $MPEGaudioBitrateLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['bitrate']];
  417. if (($thisfile_mpeg_audio['bitrate'] == 'free') && ($offset == $info['avdataoffset'])) {
  418. // only skip multiple frame check if free-format bitstream found at beginning of file
  419. // otherwise is quite possibly simply corrupted data
  420. $recursivesearch = false;
  421. }
  422. // For Layer 2 there are some combinations of bitrate and mode which are not allowed.
  423. if (!$FastMPEGheaderScan && ($thisfile_mpeg_audio['layer'] == '2')) {
  424. $info['audio']['dataformat'] = 'mp2';
  425. switch ($thisfile_mpeg_audio['channelmode']) {
  426. case 'mono':
  427. if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] <= 192000)) {
  428. // these are ok
  429. } else {
  430. $info['error'][] = $thisfile_mpeg_audio['bitrate'].'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
  431. return false;
  432. }
  433. break;
  434. case 'stereo':
  435. case 'joint stereo':
  436. case 'dual channel':
  437. if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] == 64000) || ($thisfile_mpeg_audio['bitrate'] >= 96000)) {
  438. // these are ok
  439. } else {
  440. $info['error'][] = intval(round($thisfile_mpeg_audio['bitrate'] / 1000)).'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
  441. return false;
  442. }
  443. break;
  444. }
  445. }
  446. if ($info['audio']['sample_rate'] > 0) {
  447. $thisfile_mpeg_audio['framelength'] = self::MPEGaudioFrameLength($thisfile_mpeg_audio['bitrate'], $thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['layer'], (int) $thisfile_mpeg_audio['padding'], $info['audio']['sample_rate']);
  448. }
  449. $nextframetestoffset = $offset + 1;
  450. if ($thisfile_mpeg_audio['bitrate'] != 'free') {
  451. $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
  452. if (isset($thisfile_mpeg_audio['framelength'])) {
  453. $nextframetestoffset = $offset + $thisfile_mpeg_audio['framelength'];
  454. } else {
  455. $info['error'][] = 'Frame at offset('.$offset.') is has an invalid frame length.';
  456. return false;
  457. }
  458. }
  459. $ExpectedNumberOfAudioBytes = 0;
  460. ////////////////////////////////////////////////////////////////////////////////////
  461. // Variable-bitrate headers
  462. if (substr($headerstring, 4 + 32, 4) == 'VBRI') {
  463. // Fraunhofer VBR header is hardcoded 'VBRI' at offset 0x24 (36)
  464. // specs taken from http://minnie.tuhs.org/pipermail/mp3encoder/2001-January/001800.html
  465. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  466. $thisfile_mpeg_audio['VBR_method'] = 'Fraunhofer';
  467. $info['audio']['codec'] = 'Fraunhofer';
  468. $SideInfoData = substr($headerstring, 4 + 2, 32);
  469. $FraunhoferVBROffset = 36;
  470. $thisfile_mpeg_audio['VBR_encoder_version'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 4, 2)); // VbriVersion
  471. $thisfile_mpeg_audio['VBR_encoder_delay'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 6, 2)); // VbriDelay
  472. $thisfile_mpeg_audio['VBR_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 8, 2)); // VbriQuality
  473. $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 10, 4)); // VbriStreamBytes
  474. $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 14, 4)); // VbriStreamFrames
  475. $thisfile_mpeg_audio['VBR_seek_offsets'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 18, 2)); // VbriTableSize
  476. $thisfile_mpeg_audio['VBR_seek_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 20, 2)); // VbriTableScale
  477. $thisfile_mpeg_audio['VBR_entry_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 22, 2)); // VbriEntryBytes
  478. $thisfile_mpeg_audio['VBR_entry_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 24, 2)); // VbriEntryFrames
  479. $ExpectedNumberOfAudioBytes = $thisfile_mpeg_audio['VBR_bytes'];
  480. $previousbyteoffset = $offset;
  481. for ($i = 0; $i < $thisfile_mpeg_audio['VBR_seek_offsets']; $i++) {
  482. $Fraunhofer_OffsetN = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset, $thisfile_mpeg_audio['VBR_entry_bytes']));
  483. $FraunhoferVBROffset += $thisfile_mpeg_audio['VBR_entry_bytes'];
  484. $thisfile_mpeg_audio['VBR_offsets_relative'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']);
  485. $thisfile_mpeg_audio['VBR_offsets_absolute'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']) + $previousbyteoffset;
  486. $previousbyteoffset += $Fraunhofer_OffsetN;
  487. }
  488. } else {
  489. // Xing VBR header is hardcoded 'Xing' at a offset 0x0D (13), 0x15 (21) or 0x24 (36)
  490. // depending on MPEG layer and number of channels
  491. $VBRidOffset = self::XingVBRidOffset($thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['channelmode']);
  492. $SideInfoData = substr($headerstring, 4 + 2, $VBRidOffset - 4);
  493. if ((substr($headerstring, $VBRidOffset, strlen('Xing')) == 'Xing') || (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Info')) {
  494. // 'Xing' is traditional Xing VBR frame
  495. // 'Info' is LAME-encoded CBR (This was done to avoid CBR files to be recognized as traditional Xing VBR files by some decoders.)
  496. // 'Info' *can* legally be used to specify a VBR file as well, however.
  497. // http://www.multiweb.cz/twoinches/MP3inside.htm
  498. //00..03 = "Xing" or "Info"
  499. //04..07 = Flags:
  500. // 0x01 Frames Flag set if value for number of frames in file is stored
  501. // 0x02 Bytes Flag set if value for filesize in bytes is stored
  502. // 0x04 TOC Flag set if values for TOC are stored
  503. // 0x08 VBR Scale Flag set if values for VBR scale is stored
  504. //08..11 Frames: Number of frames in file (including the first Xing/Info one)
  505. //12..15 Bytes: File length in Bytes
  506. //16..115 TOC (Table of Contents):
  507. // Contains of 100 indexes (one Byte length) for easier lookup in file. Approximately solves problem with moving inside file.
  508. // Each Byte has a value according this formula:
  509. // (TOC[i] / 256) * fileLenInBytes
  510. // So if song lasts eg. 240 sec. and you want to jump to 60. sec. (and file is 5 000 000 Bytes length) you can use:
  511. // TOC[(60/240)*100] = TOC[25]
  512. // and corresponding Byte in file is then approximately at:
  513. // (TOC[25]/256) * 5000000
  514. //116..119 VBR Scale
  515. // should be safe to leave this at 'vbr' and let it be overriden to 'cbr' if a CBR preset/mode is used by LAME
  516. // if (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Xing') {
  517. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  518. $thisfile_mpeg_audio['VBR_method'] = 'Xing';
  519. // } else {
  520. // $ScanAsCBR = true;
  521. // $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  522. // }
  523. $thisfile_mpeg_audio['xing_flags_raw'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 4, 4));
  524. $thisfile_mpeg_audio['xing_flags']['frames'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000001);
  525. $thisfile_mpeg_audio['xing_flags']['bytes'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000002);
  526. $thisfile_mpeg_audio['xing_flags']['toc'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000004);
  527. $thisfile_mpeg_audio['xing_flags']['vbr_scale'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000008);
  528. if ($thisfile_mpeg_audio['xing_flags']['frames']) {
  529. $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 8, 4));
  530. //$thisfile_mpeg_audio['VBR_frames']--; // don't count header Xing/Info frame
  531. }
  532. if ($thisfile_mpeg_audio['xing_flags']['bytes']) {
  533. $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 12, 4));
  534. }
  535. //if (($thisfile_mpeg_audio['bitrate'] == 'free') && !empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
  536. if (!empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
  537. $framelengthfloat = $thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames'];
  538. if ($thisfile_mpeg_audio['layer'] == '1') {
  539. // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12
  540. //$info['audio']['bitrate'] = ((($framelengthfloat / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12;
  541. $info['audio']['bitrate'] = ($framelengthfloat / 4) * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 12;
  542. } else {
  543. // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144
  544. //$info['audio']['bitrate'] = (($framelengthfloat - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144;
  545. $info['audio']['bitrate'] = $framelengthfloat * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 144;
  546. }
  547. $thisfile_mpeg_audio['framelength'] = floor($framelengthfloat);
  548. }
  549. if ($thisfile_mpeg_audio['xing_flags']['toc']) {
  550. $LAMEtocData = substr($headerstring, $VBRidOffset + 16, 100);
  551. for ($i = 0; $i < 100; $i++) {
  552. $thisfile_mpeg_audio['toc'][$i] = ord($LAMEtocData{$i});
  553. }
  554. }
  555. if ($thisfile_mpeg_audio['xing_flags']['vbr_scale']) {
  556. $thisfile_mpeg_audio['VBR_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 116, 4));
  557. }
  558. // http://gabriel.mp3-tech.org/mp3infotag.html
  559. if (substr($headerstring, $VBRidOffset + 120, 4) == 'LAME') {
  560. // shortcut
  561. $thisfile_mpeg_audio['LAME'] = array();
  562. $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
  563. $thisfile_mpeg_audio_lame['long_version'] = substr($headerstring, $VBRidOffset + 120, 20);
  564. $thisfile_mpeg_audio_lame['short_version'] = substr($thisfile_mpeg_audio_lame['long_version'], 0, 9);
  565. if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.90') {
  566. // extra 11 chars are not part of version string when LAMEtag present
  567. unset($thisfile_mpeg_audio_lame['long_version']);
  568. // It the LAME tag was only introduced in LAME v3.90
  569. // http://www.hydrogenaudio.org/?act=ST&f=15&t=9933
  570. // Offsets of various bytes in http://gabriel.mp3-tech.org/mp3infotag.html
  571. // are assuming a 'Xing' identifier offset of 0x24, which is the case for
  572. // MPEG-1 non-mono, but not for other combinations
  573. $LAMEtagOffsetContant = $VBRidOffset - 0x24;
  574. // shortcuts
  575. $thisfile_mpeg_audio_lame['RGAD'] = array('track'=>array(), 'album'=>array());
  576. $thisfile_mpeg_audio_lame_RGAD = &$thisfile_mpeg_audio_lame['RGAD'];
  577. $thisfile_mpeg_audio_lame_RGAD_track = &$thisfile_mpeg_audio_lame_RGAD['track'];
  578. $thisfile_mpeg_audio_lame_RGAD_album = &$thisfile_mpeg_audio_lame_RGAD['album'];
  579. $thisfile_mpeg_audio_lame['raw'] = array();
  580. $thisfile_mpeg_audio_lame_raw = &$thisfile_mpeg_audio_lame['raw'];
  581. // byte $9B VBR Quality
  582. // This field is there to indicate a quality level, although the scale was not precised in the original Xing specifications.
  583. // Actually overwrites original Xing bytes
  584. unset($thisfile_mpeg_audio['VBR_scale']);
  585. $thisfile_mpeg_audio_lame['vbr_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0x9B, 1));
  586. // bytes $9C-$A4 Encoder short VersionString
  587. $thisfile_mpeg_audio_lame['short_version'] = substr($headerstring, $LAMEtagOffsetContant + 0x9C, 9);
  588. // byte $A5 Info Tag revision + VBR method
  589. $LAMEtagRevisionVBRmethod = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA5, 1));
  590. $thisfile_mpeg_audio_lame['tag_revision'] = ($LAMEtagRevisionVBRmethod & 0xF0) >> 4;
  591. $thisfile_mpeg_audio_lame_raw['vbr_method'] = $LAMEtagRevisionVBRmethod & 0x0F;
  592. $thisfile_mpeg_audio_lame['vbr_method'] = self::LAMEvbrMethodLookup($thisfile_mpeg_audio_lame_raw['vbr_method']);
  593. $thisfile_mpeg_audio['bitrate_mode'] = substr($thisfile_mpeg_audio_lame['vbr_method'], 0, 3); // usually either 'cbr' or 'vbr', but truncates 'vbr-old / vbr-rh' to 'vbr'
  594. // byte $A6 Lowpass filter value
  595. $thisfile_mpeg_audio_lame['lowpass_frequency'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA6, 1)) * 100;
  596. // bytes $A7-$AE Replay Gain
  597. // http://privatewww.essex.ac.uk/~djmrob/replaygain/rg_data_format.html
  598. // bytes $A7-$AA : 32 bit floating point "Peak signal amplitude"
  599. if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.94b') {
  600. // LAME 3.94a16 and later - 9.23 fixed point
  601. // ie 0x0059E2EE / (2^23) = 5890798 / 8388608 = 0.7022378444671630859375
  602. $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = (float) ((getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4))) / 8388608);
  603. } else {
  604. // LAME 3.94a15 and earlier - 32-bit floating point
  605. // Actually 3.94a16 will fall in here too and be WRONG, but is hard to detect 3.94a16 vs 3.94a15
  606. $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = getid3_lib::LittleEndian2Float(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4));
  607. }
  608. if ($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] == 0) {
  609. unset($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
  610. } else {
  611. $thisfile_mpeg_audio_lame_RGAD['peak_db'] = getid3_lib::RGADamplitude2dB($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
  612. }
  613. $thisfile_mpeg_audio_lame_raw['RGAD_track'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAB, 2));
  614. $thisfile_mpeg_audio_lame_raw['RGAD_album'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAD, 2));
  615. if ($thisfile_mpeg_audio_lame_raw['RGAD_track'] != 0) {
  616. $thisfile_mpeg_audio_lame_RGAD_track['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0xE000) >> 13;
  617. $thisfile_mpeg_audio_lame_RGAD_track['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x1C00) >> 10;
  618. $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x0200) >> 9;
  619. $thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x01FF;
  620. $thisfile_mpeg_audio_lame_RGAD_track['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['name']);
  621. $thisfile_mpeg_audio_lame_RGAD_track['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['originator']);
  622. $thisfile_mpeg_audio_lame_RGAD_track['gain_db'] = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit']);
  623. if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
  624. $info['replay_gain']['track']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
  625. }
  626. $info['replay_gain']['track']['originator'] = $thisfile_mpeg_audio_lame_RGAD_track['originator'];
  627. $info['replay_gain']['track']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_track['gain_db'];
  628. } else {
  629. unset($thisfile_mpeg_audio_lame_RGAD['track']);
  630. }
  631. if ($thisfile_mpeg_audio_lame_raw['RGAD_album'] != 0) {
  632. $thisfile_mpeg_audio_lame_RGAD_album['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0xE000) >> 13;
  633. $thisfile_mpeg_audio_lame_RGAD_album['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x1C00) >> 10;
  634. $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x0200) >> 9;
  635. $thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x01FF;
  636. $thisfile_mpeg_audio_lame_RGAD_album['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['name']);
  637. $thisfile_mpeg_audio_lame_RGAD_album['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['originator']);
  638. $thisfile_mpeg_audio_lame_RGAD_album['gain_db'] = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit']);
  639. if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
  640. $info['replay_gain']['album']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
  641. }
  642. $info['replay_gain']['album']['originator'] = $thisfile_mpeg_audio_lame_RGAD_album['originator'];
  643. $info['replay_gain']['album']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_album['gain_db'];
  644. } else {
  645. unset($thisfile_mpeg_audio_lame_RGAD['album']);
  646. }
  647. if (empty($thisfile_mpeg_audio_lame_RGAD)) {
  648. unset($thisfile_mpeg_audio_lame['RGAD']);
  649. }
  650. // byte $AF Encoding flags + ATH Type
  651. $EncodingFlagsATHtype = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAF, 1));
  652. $thisfile_mpeg_audio_lame['encoding_flags']['nspsytune'] = (bool) ($EncodingFlagsATHtype & 0x10);
  653. $thisfile_mpeg_audio_lame['encoding_flags']['nssafejoint'] = (bool) ($EncodingFlagsATHtype & 0x20);
  654. $thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'] = (bool) ($EncodingFlagsATHtype & 0x40);
  655. $thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev'] = (bool) ($EncodingFlagsATHtype & 0x80);
  656. $thisfile_mpeg_audio_lame['ath_type'] = $EncodingFlagsATHtype & 0x0F;
  657. // byte $B0 if ABR {specified bitrate} else {minimal bitrate}
  658. $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB0, 1));
  659. if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 2) { // Average BitRate (ABR)
  660. $thisfile_mpeg_audio_lame['bitrate_abr'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
  661. } elseif ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { // Constant BitRate (CBR)
  662. // ignore
  663. } elseif ($thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] > 0) { // Variable BitRate (VBR) - minimum bitrate
  664. $thisfile_mpeg_audio_lame['bitrate_min'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
  665. }
  666. // bytes $B1-$B3 Encoder delays
  667. $EncoderDelays = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB1, 3));
  668. $thisfile_mpeg_audio_lame['encoder_delay'] = ($EncoderDelays & 0xFFF000) >> 12;
  669. $thisfile_mpeg_audio_lame['end_padding'] = $EncoderDelays & 0x000FFF;
  670. // byte $B4 Misc
  671. $MiscByte = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB4, 1));
  672. $thisfile_mpeg_audio_lame_raw['noise_shaping'] = ($MiscByte & 0x03);
  673. $thisfile_mpeg_audio_lame_raw['stereo_mode'] = ($MiscByte & 0x1C) >> 2;
  674. $thisfile_mpeg_audio_lame_raw['not_optimal_quality'] = ($MiscByte & 0x20) >> 5;
  675. $thisfile_mpeg_audio_lame_raw['source_sample_freq'] = ($MiscByte & 0xC0) >> 6;
  676. $thisfile_mpeg_audio_lame['noise_shaping'] = $thisfile_mpeg_audio_lame_raw['noise_shaping'];
  677. $thisfile_mpeg_audio_lame['stereo_mode'] = self::LAMEmiscStereoModeLookup($thisfile_mpeg_audio_lame_raw['stereo_mode']);
  678. $thisfile_mpeg_audio_lame['not_optimal_quality'] = (bool) $thisfile_mpeg_audio_lame_raw['not_optimal_quality'];
  679. $thisfile_mpeg_audio_lame['source_sample_freq'] = self::LAMEmiscSourceSampleFrequencyLookup($thisfile_mpeg_audio_lame_raw['source_sample_freq']);
  680. // byte $B5 MP3 Gain
  681. $thisfile_mpeg_audio_lame_raw['mp3_gain'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB5, 1), false, true);
  682. $thisfile_mpeg_audio_lame['mp3_gain_db'] = (getid3_lib::RGADamplitude2dB(2) / 4) * $thisfile_mpeg_audio_lame_raw['mp3_gain'];
  683. $thisfile_mpeg_audio_lame['mp3_gain_factor'] = pow(2, ($thisfile_mpeg_audio_lame['mp3_gain_db'] / 6));
  684. // bytes $B6-$B7 Preset and surround info
  685. $PresetSurroundBytes = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB6, 2));
  686. // Reserved = ($PresetSurroundBytes & 0xC000);
  687. $thisfile_mpeg_audio_lame_raw['surround_info'] = ($PresetSurroundBytes & 0x3800);
  688. $thisfile_mpeg_audio_lame['surround_info'] = self::LAMEsurroundInfoLookup($thisfile_mpeg_audio_lame_raw['surround_info']);
  689. $thisfile_mpeg_audio_lame['preset_used_id'] = ($PresetSurroundBytes & 0x07FF);
  690. $thisfile_mpeg_audio_lame['preset_used'] = self::LAMEpresetUsedLookup($thisfile_mpeg_audio_lame);
  691. if (!empty($thisfile_mpeg_audio_lame['preset_used_id']) && empty($thisfile_mpeg_audio_lame['preset_used'])) {
  692. $info['warning'][] = 'Unknown LAME preset used ('.$thisfile_mpeg_audio_lame['preset_used_id'].') - please report to info@getid3.org';
  693. }
  694. if (($thisfile_mpeg_audio_lame['short_version'] == 'LAME3.90.') && !empty($thisfile_mpeg_audio_lame['preset_used_id'])) {
  695. // this may change if 3.90.4 ever comes out
  696. $thisfile_mpeg_audio_lame['short_version'] = 'LAME3.90.3';
  697. }
  698. // bytes $B8-$BB MusicLength
  699. $thisfile_mpeg_audio_lame['audio_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB8, 4));
  700. $ExpectedNumberOfAudioBytes = (($thisfile_mpeg_audio_lame['audio_bytes'] > 0) ? $thisfile_mpeg_audio_lame['audio_bytes'] : $thisfile_mpeg_audio['VBR_bytes']);
  701. // bytes $BC-$BD MusicCRC
  702. $thisfile_mpeg_audio_lame['music_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBC, 2));
  703. // bytes $BE-$BF CRC-16 of Info Tag
  704. $thisfile_mpeg_audio_lame['lame_tag_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBE, 2));
  705. // LAME CBR
  706. if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) {
  707. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  708. $thisfile_mpeg_audio['bitrate'] = self::ClosestStandardMP3Bitrate($thisfile_mpeg_audio['bitrate']);
  709. $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
  710. //if (empty($thisfile_mpeg_audio['bitrate']) || (!empty($thisfile_mpeg_audio_lame['bitrate_min']) && ($thisfile_mpeg_audio_lame['bitrate_min'] != 255))) {
  711. // $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio_lame['bitrate_min'];
  712. //}
  713. }
  714. }
  715. }
  716. } else {
  717. // not Fraunhofer or Xing VBR methods, most likely CBR (but could be VBR with no header)
  718. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  719. if ($recursivesearch) {
  720. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  721. if ($this->RecursiveFrameScanning($offset, $nextframetestoffset, true)) {
  722. $recursivesearch = false;
  723. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  724. }
  725. if ($thisfile_mpeg_audio['bitrate_mode'] == 'vbr') {
  726. $info['warning'][] = 'VBR file with no VBR header. Bitrate values calculated from actual frame bitrates.';
  727. }
  728. }
  729. }
  730. }
  731. if (($ExpectedNumberOfAudioBytes > 0) && ($ExpectedNumberOfAudioBytes != ($info['avdataend'] - $info['avdataoffset']))) {
  732. if ($ExpectedNumberOfAudioBytes > ($info['avdataend'] - $info['avdataoffset'])) {
  733. if ($this->isDependencyFor('matroska') || $this->isDependencyFor('riff')) {
  734. // ignore, audio data is broken into chunks so will always be data "missing"
  735. }
  736. elseif (($ExpectedNumberOfAudioBytes - ($info['avdataend'] - $info['avdataoffset'])) == 1) {
  737. $this->warning('Last byte of data truncated (this is a known bug in Meracl ID3 Tag Writer before v1.3.5)');
  738. }
  739. else {
  740. $this->warning('Probable truncated file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, only found '.($info['avdataend'] - $info['avdataoffset']).' (short by '.($ExpectedNumberOfAudioBytes - ($info['avdataend'] - $info['avdataoffset'])).' bytes)');
  741. }
  742. } else {
  743. if ((($info['avdataend'] - $info['avdataoffset']) - $ExpectedNumberOfAudioBytes) == 1) {
  744. // $prenullbytefileoffset = $this->ftell();
  745. // $this->fseek($info['avdataend']);
  746. // $PossibleNullByte = $this->fread(1);
  747. // $this->fseek($prenullbytefileoffset);
  748. // if ($PossibleNullByte === "\x00") {
  749. $info['avdataend']--;
  750. // $info['warning'][] = 'Extra null byte at end of MP3 data assumed to be RIFF padding and therefore ignored';
  751. // } else {
  752. // $info['warning'][] = 'Too much data in file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, found '.($info['avdataend'] - $info['avdataoffset']).' ('.(($info['avdataend'] - $info['avdataoffset']) - $ExpectedNumberOfAudioBytes).' bytes too many)';
  753. // }
  754. } else {
  755. $info['warning'][] = 'Too much data in file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, found '.($info['avdataend'] - $info['avdataoffset']).' ('.(($info['avdataend'] - $info['avdataoffset']) - $ExpectedNumberOfAudioBytes).' bytes too many)';
  756. }
  757. }
  758. }
  759. if (($thisfile_mpeg_audio['bitrate'] == 'free') && empty($info['audio']['bitrate'])) {
  760. if (($offset == $info['avdataoffset']) && empty($thisfile_mpeg_audio['VBR_frames'])) {
  761. $framebytelength = $this->FreeFormatFrameLength($offset, true);
  762. if ($framebytelength > 0) {
  763. $thisfile_mpeg_audio['framelength'] = $framebytelength;
  764. if ($thisfile_mpeg_audio['layer'] == '1') {
  765. // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12
  766. $info['audio']['bitrate'] = ((($framebytelength / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12;
  767. } else {
  768. // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144
  769. $info['audio']['bitrate'] = (($framebytelength - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144;
  770. }
  771. } else {
  772. $info['error'][] = 'Error calculating frame length of free-format MP3 without Xing/LAME header';
  773. }
  774. }
  775. }
  776. if (isset($thisfile_mpeg_audio['VBR_frames']) ? $thisfile_mpeg_audio['VBR_frames'] : '') {
  777. switch ($thisfile_mpeg_audio['bitrate_mode']) {
  778. case 'vbr':
  779. case 'abr':
  780. $bytes_per_frame = 1152;
  781. if (($thisfile_mpeg_audio['version'] == '1') && ($thisfile_mpeg_audio['layer'] == 1)) {
  782. $bytes_per_frame = 384;
  783. } elseif ((($thisfile_mpeg_audio['version'] == '2') || ($thisfile_mpeg_audio['version'] == '2.5')) && ($thisfile_mpeg_audio['layer'] == 3)) {
  784. $bytes_per_frame = 576;
  785. }
  786. $thisfile_mpeg_audio['VBR_bitrate'] = (isset($thisfile_mpeg_audio['VBR_bytes']) ? (($thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames']) * 8) * ($info['audio']['sample_rate'] / $bytes_per_frame) : 0);
  787. if ($thisfile_mpeg_audio['VBR_bitrate'] > 0) {
  788. $info['audio']['bitrate'] = $thisfile_mpeg_audio['VBR_bitrate'];
  789. $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio['VBR_bitrate']; // to avoid confusion
  790. }
  791. break;
  792. }
  793. }
  794. // End variable-bitrate headers
  795. ////////////////////////////////////////////////////////////////////////////////////
  796. if ($recursivesearch) {
  797. if (!$this->RecursiveFrameScanning($offset, $nextframetestoffset, $ScanAsCBR)) {
  798. return false;
  799. }
  800. }
  801. //if (false) {
  802. // // experimental side info parsing section - not returning anything useful yet
  803. //
  804. // $SideInfoBitstream = getid3_lib::BigEndian2Bin($SideInfoData);
  805. // $SideInfoOffset = 0;
  806. //
  807. // if ($thisfile_mpeg_audio['version'] == '1') {
  808. // if ($thisfile_mpeg_audio['channelmode'] == 'mono') {
  809. // // MPEG-1 (mono)
  810. // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 9);
  811. // $SideInfoOffset += 9;
  812. // $SideInfoOffset += 5;
  813. // } else {
  814. // // MPEG-1 (stereo, joint-stereo, dual-channel)
  815. // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 9);
  816. // $SideInfoOffset += 9;
  817. // $SideInfoOffset += 3;
  818. // }
  819. // } else { // 2 or 2.5
  820. // if ($thisfile_mpeg_audio['channelmode'] == 'mono') {
  821. // // MPEG-2, MPEG-2.5 (mono)
  822. // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 8);
  823. // $SideInfoOffset += 8;
  824. // $SideInfoOffset += 1;
  825. // } else {
  826. // // MPEG-2, MPEG-2.5 (stereo, joint-stereo, dual-channel)
  827. // $thisfile_mpeg_audio['side_info']['main_data_begin'] = substr($SideInfoBitstream, $SideInfoOffset, 8);
  828. // $SideInfoOffset += 8;
  829. // $SideInfoOffset += 2;
  830. // }
  831. // }
  832. //
  833. // if ($thisfile_mpeg_audio['version'] == '1') {
  834. // for ($channel = 0; $channel < $info['audio']['channels']; $channel++) {
  835. // for ($scfsi_band = 0; $scfsi_band < 4; $scfsi_band++) {
  836. // $thisfile_mpeg_audio['scfsi'][$channel][$scfsi_band] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  837. // $SideInfoOffset += 2;
  838. // }
  839. // }
  840. // }
  841. // for ($granule = 0; $granule < (($thisfile_mpeg_audio['version'] == '1') ? 2 : 1); $granule++) {
  842. // for ($channel = 0; $channel < $info['audio']['channels']; $channel++) {
  843. // $thisfile_mpeg_audio['part2_3_length'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 12);
  844. // $SideInfoOffset += 12;
  845. // $thisfile_mpeg_audio['big_values'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 9);
  846. // $SideInfoOffset += 9;
  847. // $thisfile_mpeg_audio['global_gain'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 8);
  848. // $SideInfoOffset += 8;
  849. // if ($thisfile_mpeg_audio['version'] == '1') {
  850. // $thisfile_mpeg_audio['scalefac_compress'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 4);
  851. // $SideInfoOffset += 4;
  852. // } else {
  853. // $thisfile_mpeg_audio['scalefac_compress'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 9);
  854. // $SideInfoOffset += 9;
  855. // }
  856. // $thisfile_mpeg_audio['window_switching_flag'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  857. // $SideInfoOffset += 1;
  858. //
  859. // if ($thisfile_mpeg_audio['window_switching_flag'][$granule][$channel] == '1') {
  860. //
  861. // $thisfile_mpeg_audio['block_type'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 2);
  862. // $SideInfoOffset += 2;
  863. // $thisfile_mpeg_audio['mixed_block_flag'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  864. // $SideInfoOffset += 1;
  865. //
  866. // for ($region = 0; $region < 2; $region++) {
  867. // $thisfile_mpeg_audio['table_select'][$granule][$channel][$region] = substr($SideInfoBitstream, $SideInfoOffset, 5);
  868. // $SideInfoOffset += 5;
  869. // }
  870. // $thisfile_mpeg_audio['table_select'][$granule][$channel][2] = 0;
  871. //
  872. // for ($window = 0; $window < 3; $window++) {
  873. // $thisfile_mpeg_audio['subblock_gain'][$granule][$channel][$window] = substr($SideInfoBitstream, $SideInfoOffset, 3);
  874. // $SideInfoOffset += 3;
  875. // }
  876. //
  877. // } else {
  878. //
  879. // for ($region = 0; $region < 3; $region++) {
  880. // $thisfile_mpeg_audio['table_select'][$granule][$channel][$region] = substr($SideInfoBitstream, $SideInfoOffset, 5);
  881. // $SideInfoOffset += 5;
  882. // }
  883. //
  884. // $thisfile_mpeg_audio['region0_count'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 4);
  885. // $SideInfoOffset += 4;
  886. // $thisfile_mpeg_audio['region1_count'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 3);
  887. // $SideInfoOffset += 3;
  888. // $thisfile_mpeg_audio['block_type'][$granule][$channel] = 0;
  889. // }
  890. //
  891. // if ($thisfile_mpeg_audio['version'] == '1') {
  892. // $thisfile_mpeg_audio['preflag'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  893. // $SideInfoOffset += 1;
  894. // }
  895. // $thisfile_mpeg_audio['scalefac_scale'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  896. // $SideInfoOffset += 1;
  897. // $thisfile_mpeg_audio['count1table_select'][$granule][$channel] = substr($SideInfoBitstream, $SideInfoOffset, 1);
  898. // $SideInfoOffset += 1;
  899. // }
  900. // }
  901. //}
  902. return true;
  903. }
  904. public function RecursiveFrameScanning(&$offset, &$nextframetestoffset, $ScanAsCBR) {
  905. $info = &$this->getid3->info;
  906. $firstframetestarray = array('error'=>'', 'warning'=>'', 'avdataend'=>$info['avdataend'], 'avdataoffset'=>$info['avdataoffset']);
  907. $this->decodeMPEGaudioHeader($offset, $firstframetestarray, false);
  908. for ($i = 0; $i < GETID3_MP3_VALID_CHECK_FRAMES; $i++) {
  909. // check next GETID3_MP3_VALID_CHECK_FRAMES frames for validity, to make sure we haven't run across a false synch
  910. if (($nextframetestoffset + 4) >= $info['avdataend']) {
  911. // end of file
  912. return true;
  913. }
  914. $nextframetestarray = array('error'=>'', 'warning'=>'', 'avdataend'=>$info['avdataend'], 'avdataoffset'=>$info['avdataoffset']);
  915. if ($this->decodeMPEGaudioHeader($nextframetestoffset, $nextframetestarray, false)) {
  916. if ($ScanAsCBR) {
  917. // force CBR mode, used for trying to pick out invalid audio streams with valid(?) VBR headers, or VBR streams with no VBR header
  918. if (!isset($nextframetestarray['mpeg']['audio']['bitrate']) || !isset($firstframetestarray['mpeg']['audio']['bitrate']) || ($nextframetestarray['mpeg']['audio']['bitrate'] != $firstframetestarray['mpeg']['audio']['bitrate'])) {
  919. return false;
  920. }
  921. }
  922. // next frame is OK, get ready to check the one after that
  923. if (isset($nextframetestarray['mpeg']['audio']['framelength']) && ($nextframetestarray['mpeg']['audio']['framelength'] > 0)) {
  924. $nextframetestoffset += $nextframetestarray['mpeg']['audio']['framelength'];
  925. } else {
  926. $info['error'][] = 'Frame at offset ('.$offset.') is has an invalid frame length.';
  927. return false;
  928. }
  929. } elseif (!empty($firstframetestarray['mpeg']['audio']['framelength']) && (($nextframetestoffset + $firstframetestarray['mpeg']['audio']['framelength']) > $info['avdataend'])) {
  930. // it's not the end of the file, but there's not enough data left for another frame, so assume it's garbage/padding and return OK
  931. return true;
  932. } else {
  933. // next frame is not valid, note the error and fail, so scanning can contiue for a valid frame sequence
  934. $info['warning'][] = 'Frame at offset ('.$offset.') is valid, but the next one at ('.$nextframetestoffset.') is not.';
  935. return false;
  936. }
  937. }
  938. return true;
  939. }
  940. public function FreeFormatFrameLength($offset, $deepscan=false) {
  941. $info = &$this->getid3->info;
  942. $this->fseek($offset);
  943. $MPEGaudioData = $this->fread(32768);
  944. $SyncPattern1 = substr($MPEGaudioData, 0, 4);
  945. // may be different pattern due to padding
  946. $SyncPattern2 = $SyncPattern1{0}.$SyncPattern1{1}.chr(ord($SyncPattern1{2}) | 0x02).$SyncPattern1{3};
  947. if ($SyncPattern2 === $SyncPattern1) {
  948. $SyncPattern2 = $SyncPattern1{0}.$SyncPattern1{1}.chr(ord($SyncPattern1{2}) & 0xFD).$SyncPattern1{3};
  949. }
  950. $framelength = false;
  951. $framelength1 = strpos($MPEGaudioData, $SyncPattern1, 4);
  952. $framelength2 = strpos($MPEGaudioData, $SyncPattern2, 4);
  953. if ($framelength1 > 4) {
  954. $framelength = $framelength1;
  955. }
  956. if (($framelength2 > 4) && ($framelength2 < $framelength1)) {
  957. $framelength = $framelength2;
  958. }
  959. if (!$framelength) {
  960. // LAME 3.88 has a different value for modeextension on the first frame vs the rest
  961. $framelength1 = strpos($MPEGaudioData, substr($SyncPattern1, 0, 3), 4);
  962. $framelength2 = strpos($MPEGaudioData, substr($SyncPattern2, 0, 3), 4);
  963. if ($framelength1 > 4) {
  964. $framelength = $framelength1;
  965. }
  966. if (($framelength2 > 4) && ($framelength2 < $framelength1)) {
  967. $framelength = $framelength2;
  968. }
  969. if (!$framelength) {
  970. $info['error'][] = 'Cannot find next free-format synch pattern ('.getid3_lib::PrintHexBytes($SyncPattern1).' or '.getid3_lib::PrintHexBytes($SyncPattern2).') after offset '.$offset;
  971. return false;
  972. } else {
  973. $info['warning'][] = 'ModeExtension varies between first frame and other frames (known free-format issue in LAME 3.88)';
  974. $info['audio']['codec'] = 'LAME';
  975. $info['audio']['encoder'] = 'LAME3.88';
  976. $SyncPattern1 = substr($SyncPattern1, 0, 3);
  977. $SyncPattern2 = substr($SyncPattern2, 0, 3);
  978. }
  979. }
  980. if ($deepscan) {
  981. $ActualFrameLengthValues = array();
  982. $nextoffset = $offset + $framelength;
  983. while ($nextoffset < ($info['avdataend'] - 6)) {
  984. $this->fseek($nextoffset - 1);
  985. $NextSyncPattern = $this->fread(6);
  986. if ((substr($NextSyncPattern, 1, strlen($SyncPattern1)) == $SyncPattern1) || (substr($NextSyncPattern, 1, strlen($SyncPattern2)) == $SyncPattern2)) {
  987. // good - found where expected
  988. $ActualFrameLengthValues[] = $framelength;
  989. } elseif ((substr($NextSyncPattern, 0, strlen($SyncPattern1)) == $SyncPattern1) || (substr($NextSyncPattern, 0, strlen($SyncPattern2)) == $SyncPattern2)) {
  990. // ok - found one byte earlier than expected (last frame wasn't padded, first frame was)
  991. $ActualFrameLengthValues[] = ($framelength - 1);
  992. $nextoffset--;
  993. } elseif ((substr($NextSyncPattern, 2, strlen($SyncPattern1)) == $SyncPattern1) || (substr($NextSyncPattern, 2, strlen($SyncPattern2)) == $SyncPattern2)) {
  994. // ok - found one byte later than expected (last frame was padded, first frame wasn't)
  995. $ActualFrameLengthValues[] = ($framelength + 1);
  996. $nextoffset++;
  997. } else {
  998. $info['error'][] = 'Did not find expected free-format sync pattern at offset '.$nextoffset;
  999. return false;
  1000. }
  1001. $nextoffset += $framelength;
  1002. }
  1003. if (count($ActualFrameLengthValues) > 0) {
  1004. $framelength = intval(round(array_sum($ActualFrameLengthValues) / count($ActualFrameLengthValues)));
  1005. }
  1006. }
  1007. return $framelength;
  1008. }
  1009. public function getOnlyMPEGaudioInfoBruteForce() {
  1010. $MPEGaudioHeaderDecodeCache = array();
  1011. $MPEGaudioHeaderValidCache = array();
  1012. $MPEGaudioHeaderLengthCache = array();
  1013. $MPEGaudioVersionLookup = self::MPEGaudioVersionArray();
  1014. $MPEGaudioLayerLookup = self::MPEGaudioLayerArray();
  1015. $MPEGaudioBitrateLookup = self::MPEGaudioBitrateArray();
  1016. $MPEGaudioFrequencyLookup = self::MPEGaudioFrequencyArray();
  1017. $MPEGaudioChannelModeLookup = self::MPEGaudioChannelModeArray();
  1018. $MPEGaudioModeExtensionLookup = self::MPEGaudioModeExtensionArray();
  1019. $MPEGaudioEmphasisLookup = self::MPEGaudioEmphasisArray();
  1020. $LongMPEGversionLookup = array();
  1021. $LongMPEGlayerLookup = array();
  1022. $LongMPEGbitrateLookup = array();
  1023. $LongMPEGpaddingLookup = array();
  1024. $LongMPEGfrequencyLookup = array();
  1025. $Distribution['bitrate'] = array();
  1026. $Distribution['frequency'] = array();
  1027. $Distribution['layer'] = array();
  1028. $Distribution['version'] = array();
  1029. $Distribution['padding'] = array();
  1030. $info = &$this->getid3->info;
  1031. $this->fseek($info['avdataoffset']);
  1032. $max_frames_scan = 5000;
  1033. $frames_scanned = 0;
  1034. $previousvalidframe = $info['avdataoffset'];
  1035. while ($this->ftell() < $info['avdataend']) {
  1036. set_time_limit(30);
  1037. $head4 = $this->fread(4);
  1038. if (strlen($head4) < 4) {
  1039. break;
  1040. }
  1041. if ($head4{0} != "\xFF") {
  1042. for ($i = 1; $i < 4; $i++) {
  1043. if ($head4{$i} == "\xFF") {
  1044. $this->fseek($i - 4, SEEK_CUR);
  1045. continue 2;
  1046. }
  1047. }
  1048. continue;
  1049. }
  1050. if (!isset($MPEGaudioHeaderDecodeCache[$head4])) {
  1051. $MPEGaudioHeaderDecodeCache[$head4] = self::MPEGaudioHeaderDecode($head4);
  1052. }
  1053. if (!isset($MPEGaudioHeaderValidCache[$head4])) {
  1054. $MPEGaudioHeaderValidCache[$head4] = self::MPEGaudioHeaderValid($MPEGaudioHeaderDecodeCache[$head4], false, false);
  1055. }
  1056. if ($MPEGaudioHeaderValidCache[$head4]) {
  1057. if (!isset($MPEGaudioHeaderLengthCache[$head4])) {
  1058. $LongMPEGversionLookup[$head4] = $MPEGaudioVersionLookup[$MPEGaudioHeaderDecodeCache[$head4]['version']];
  1059. $LongMPEGlayerLookup[$head4] = $MPEGaudioLayerLookup[$MPEGaudioHeaderDecodeCache[$head4]['layer']];
  1060. $LongMPEGbitrateLookup[$head4] = $MPEGaudioBitrateLookup[$LongMPEGversionLookup[$head4]][$LongMPEGlayerLookup[$head4]][$MPEGaudioHeaderDecodeCache[$head4]['bitrate']];
  1061. $LongMPEGpaddingLookup[$head4] = (bool) $MPEGaudioHeaderDecodeCache[$head4]['padding'];
  1062. $LongMPEGfrequencyLookup[$head4] = $MPEGaudioFrequencyLookup[$LongMPEGversionLookup[$head4]][$MPEGaudioHeaderDecodeCache[$head4]['sample_rate']];
  1063. $MPEGaudioHeaderLengthCache[$head4] = self::MPEGaudioFrameLength(
  1064. $LongMPEGbitrateLookup[$head4],
  1065. $LongMPEGversionLookup[$head4],
  1066. $LongMPEGlayerLookup[$head4],
  1067. $LongMPEGpaddingLookup[$head4],
  1068. $LongMPEGfrequencyLookup[$head4]);
  1069. }
  1070. if ($MPEGaudioHeaderLengthCache[$head4] > 4) {
  1071. $WhereWeWere = $this->ftell();
  1072. $this->fseek($MPEGaudioHeaderLengthCache[$head4] - 4, SEEK_CUR);
  1073. $next4 = $this->fread(4);
  1074. if ($next4{0} == "\xFF") {
  1075. if (!isset($MPEGaudioHeaderDecodeCache[$next4])) {
  1076. $MPEGaudioHeaderDecodeCache[$next4] = self::MPEGaudioHeaderDecode($next4);
  1077. }
  1078. if (!isset($MPEGaudioHeaderValidCache[$next4])) {
  1079. $MPEGaudioHeaderValidCache[$next4] = self::MPEGaudioHeaderValid($MPEGaudioHeaderDecodeCache[$next4], false, false);
  1080. }
  1081. if ($MPEGaudioHeaderValidCache[$next4]) {
  1082. $this->fseek(-4, SEEK_CUR);
  1083. getid3_lib::safe_inc($Distribution['bitrate'][$LongMPEGbitrateLookup[$head4]]);
  1084. getid3_lib::safe_inc($Distribution['layer'][$LongMPEGlayerLookup[$head4]]);
  1085. getid3_lib::safe_inc($Distribution['version'][$LongMPEGversionLookup[$head4]]);
  1086. getid3_lib::safe_inc($Distribution['padding'][intval($LongMPEGpaddingLookup[$head4])]);
  1087. getid3_lib::safe_inc($Distribution['frequency'][$LongMPEGfrequencyLookup[$head4]]);
  1088. if ($max_frames_scan && (++$frames_scanned >= $max_frames_scan)) {
  1089. $pct_data_scanned = ($this->ftell() - $info['avdataoffset']) / ($info['avdataend'] - $info['avdataoffset']);
  1090. $info['warning'][] = 'too many MPEG audio frames to scan, only scanned first '.$max_frames_scan.' frames ('.number_format($pct_data_scanned * 100, 1).'% of file) and extrapolated distribution, playtime and bitrate may be incorrect.';
  1091. foreach ($Distribution as $key1 => $value1) {
  1092. foreach ($value1 as $key2 => $value2) {
  1093. $Distribution[$key1][$key2] = round($value2 / $pct_data_scanned);
  1094. }
  1095. }
  1096. break;
  1097. }
  1098. continue;
  1099. }
  1100. }
  1101. unset($next4);
  1102. $this->fseek($WhereWeWere - 3);
  1103. }
  1104. }
  1105. }
  1106. foreach ($Distribution as $key => $value) {
  1107. ksort($Distribution[$key], SORT_NUMERIC);
  1108. }
  1109. ksort($Distribution['version'], SORT_STRING);
  1110. $info['mpeg']['audio']['bitrate_distribution'] = $Distribution['bitrate'];
  1111. $info['mpeg']['audio']['frequency_distribution'] = $Distribution['frequency'];
  1112. $info['mpeg']['audio']['layer_distribution'] = $Distribution['layer'];
  1113. $info['mpeg']['audio']['version_distribution'] = $Distribution['version'];
  1114. $info['mpeg']['audio']['padding_distribution'] = $Distribution['padding'];
  1115. if (count($Distribution['version']) > 1) {
  1116. $info['error'][] = 'Corrupt file - more than one MPEG version detected';
  1117. }
  1118. if (count($Distribution['layer']) > 1) {
  1119. $info['error'][] = 'Corrupt file - more than one MPEG layer detected';
  1120. }
  1121. if (count($Distribution['frequency']) > 1) {
  1122. $info['error'][] = 'Corrupt file - more than one MPEG sample rate detected';
  1123. }
  1124. $bittotal = 0;
  1125. foreach ($Distribution['bitrate'] as $bitratevalue => $bitratecount) {
  1126. if ($bitratevalue != 'free') {
  1127. $bittotal += ($bitratevalue * $bitratecount);
  1128. }
  1129. }
  1130. $info['mpeg']['audio']['frame_count'] = array_sum($Distribution['bitrate']);
  1131. if ($info['mpeg']['audio']['frame_count'] == 0) {
  1132. $info['error'][] = 'no MPEG audio frames found';
  1133. return false;
  1134. }
  1135. $info['mpeg']['audio']['bitrate'] = ($bittotal / $info['mpeg']['audio']['frame_count']);
  1136. $info['mpeg']['audio']['bitrate_mode'] = ((count($Distribution['bitrate']) > 0) ? 'vbr' : 'cbr');
  1137. $info['mpeg']['audio']['sample_rate'] = getid3_lib::array_max($Distribution['frequency'], true);
  1138. $info['audio']['bitrate'] = $info['mpeg']['audio']['bitrate'];
  1139. $info['audio']['bitrate_mode'] = $info['mpeg']['audio']['bitrate_mode'];
  1140. $info['audio']['sample_rate'] = $info['mpeg']['audio']['sample_rate'];
  1141. $info['audio']['dataformat'] = 'mp'.getid3_lib::array_max($Distribution['layer'], true);
  1142. $info['fileformat'] = $info['audio']['dataformat'];
  1143. return true;
  1144. }
  1145. public function getOnlyMPEGaudioInfo($avdataoffset, $BitrateHistogram=false) {
  1146. // looks for synch, decodes MPEG audio header
  1147. $info = &$this->getid3->info;
  1148. static $MPEGaudioVersionLookup;
  1149. static $MPEGaudioLayerLookup;
  1150. static $MPEGaudioBitrateLookup;
  1151. if (empty($MPEGaudioVersionLookup)) {
  1152. $MPEGaudioVersionLookup = self::MPEGaudioVersionArray();
  1153. $MPEGaudioLayerLookup = self::MPEGaudioLayerArray();
  1154. $MPEGaudioBitrateLookup = self::MPEGaudioBitrateArray();
  1155. }
  1156. $this->fseek($avdataoffset);
  1157. $sync_seek_buffer_size = min(128 * 1024, $info['avdataend'] - $avdataoffset);
  1158. if ($sync_seek_buffer_size <= 0) {
  1159. $info['error'][] = 'Invalid $sync_seek_buffer_size at offset '.$avdataoffset;
  1160. return false;
  1161. }
  1162. $header = $this->fread($sync_seek_buffer_size);
  1163. $sync_seek_buffer_size = strlen($header);
  1164. $SynchSeekOffset = 0;
  1165. while ($SynchSeekOffset < $sync_seek_buffer_size) {
  1166. if ((($avdataoffset + $SynchSeekOffset) < $info['avdataend']) && !feof($this->getid3->fp)) {
  1167. if ($SynchSeekOffset > $sync_seek_buffer_size) {
  1168. // if a synch's not found within the first 128k bytes, then give up
  1169. $info['error'][] = 'Could not find valid MPEG audio synch within the first '.round($sync_seek_buffer_size / 1024).'kB';
  1170. if (isset($info['audio']['bitrate'])) {
  1171. unset($info['audio']['bitrate']);
  1172. }
  1173. if (isset($info['mpeg']['audio'])) {
  1174. unset($info['mpeg']['audio']);
  1175. }
  1176. if (empty($info['mpeg'])) {
  1177. unset($info['mpeg']);
  1178. }
  1179. return false;
  1180. } elseif (feof($this->getid3->fp)) {
  1181. $info['error'][] = 'Could not find valid MPEG audio synch before end of file';
  1182. if (isset($info['audio']['bitrate'])) {
  1183. unset($info['audio']['bitrate']);
  1184. }
  1185. if (isset($info['mpeg']['audio'])) {
  1186. unset($info['mpeg']['audio']);
  1187. }
  1188. if (isset($info['mpeg']) && (!is_array($info['mpeg']) || (count($info['mpeg']) == 0))) {
  1189. unset($info['mpeg']);
  1190. }
  1191. return false;
  1192. }
  1193. }
  1194. if (($SynchSeekOffset + 1) >= strlen($header)) {
  1195. $info['error'][] = 'Could not find valid MPEG synch before end of file';
  1196. return false;
  1197. }
  1198. if (($header{$SynchSeekOffset} == "\xFF") && ($header{($SynchSeekOffset + 1)} > "\xE0")) { // synch detected
  1199. if (!isset($FirstFrameThisfileInfo) && !isset($info['mpeg']['audio'])) {
  1200. $FirstFrameThisfileInfo = $info;
  1201. $FirstFrameAVDataOffset = $avdataoffset + $SynchSeekOffset;
  1202. if (!$this->decodeMPEGaudioHeader($FirstFrameAVDataOffset, $FirstFrameThisfileInfo, false)) {
  1203. // if this is the first valid MPEG-audio frame, save it in case it's a VBR header frame and there's
  1204. // garbage between this frame and a valid sequence of MPEG-audio frames, to be restored below
  1205. unset($FirstFrameThisfileInfo);
  1206. }
  1207. }
  1208. $dummy = $info; // only overwrite real data if valid header found
  1209. if ($this->decodeMPEGaudioHeader($avdataoffset + $SynchSeekOffset, $dummy, true)) {
  1210. $info = $dummy;
  1211. $info['avdataoffset'] = $avdataoffset + $SynchSeekOffset;
  1212. switch (isset($info['fileformat']) ? $info['fileformat'] : '') {
  1213. case '':
  1214. case 'id3':
  1215. case 'ape':
  1216. case 'mp3':
  1217. $info['fileformat'] = 'mp3';
  1218. $info['audio']['dataformat'] = 'mp3';
  1219. break;
  1220. }
  1221. if (isset($FirstFrameThisfileInfo['mpeg']['audio']['bitrate_mode']) && ($FirstFrameThisfileInfo['mpeg']['audio']['bitrate_mode'] == 'vbr')) {
  1222. if (!(abs($info['audio']['bitrate'] - $FirstFrameThisfileInfo['audio']['bitrate']) <= 1)) {
  1223. // If there is garbage data between a valid VBR header frame and a sequence
  1224. // of valid MPEG-audio frames the VBR data is no longer discarded.
  1225. $info = $FirstFrameThisfileInfo;
  1226. $info['avdataoffset'] = $FirstFrameAVDataOffset;
  1227. $info['fileformat'] = 'mp3';
  1228. $info['audio']['dataformat'] = 'mp3';
  1229. $dummy = $info;
  1230. unset($dummy['mpeg']['audio']);
  1231. $GarbageOffsetStart = $FirstFrameAVDataOffset + $FirstFrameThisfileInfo['mpeg']['audio']['framelength'];
  1232. $GarbageOffsetEnd = $avdataoffset + $SynchSeekOffset;
  1233. if ($this->decodeMPEGaudioHeader($GarbageOffsetEnd, $dummy, true, true)) {
  1234. $info = $dummy;
  1235. $info['avdataoffset'] = $GarbageOffsetEnd;
  1236. $info['warning'][] = 'apparently-valid VBR header not used because could not find '.GETID3_MP3_VALID_CHECK_FRAMES.' consecutive MPEG-audio frames immediately after VBR header (garbage data for '.($GarbageOffsetEnd - $GarbageOffsetStart).' bytes between '.$GarbageOffsetStart.' and '.$GarbageOffsetEnd.'), but did find valid CBR stream starting at '.$GarbageOffsetEnd;
  1237. } else {
  1238. $info['warning'][] = 'using data from VBR header even though could not find '.GETID3_MP3_VALID_CHECK_FRAMES.' consecutive MPEG-audio frames immediately after VBR header (garbage data for '.($GarbageOffsetEnd - $GarbageOffsetStart).' bytes between '.$GarbageOffsetStart.' and '.$GarbageOffsetEnd.')';
  1239. }
  1240. }
  1241. }
  1242. if (isset($info['mpeg']['audio']['bitrate_mode']) && ($info['mpeg']['audio']['bitrate_mode'] == 'vbr') && !isset($info['mpeg']['audio']['VBR_method'])) {
  1243. // VBR file with no VBR header
  1244. $BitrateHistogram = true;
  1245. }
  1246. if ($BitrateHistogram) {
  1247. $info['mpeg']['audio']['stereo_distribution'] = array('stereo'=>0, 'joint stereo'=>0, 'dual channel'=>0, 'mono'=>0);
  1248. $info['mpeg']['audio']['version_distribution'] = array('1'=>0, '2'=>0, '2.5'=>0);
  1249. if ($info['mpeg']['audio']['version'] == '1') {
  1250. if ($info['mpeg']['audio']['layer'] == 3) {
  1251. $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 40000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 160000=>0, 192000=>0, 224000=>0, 256000=>0, 320000=>0);
  1252. } elseif ($info['mpeg']['audio']['layer'] == 2) {
  1253. $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 160000=>0, 192000=>0, 224000=>0, 256000=>0, 320000=>0, 384000=>0);
  1254. } elseif ($info['mpeg']['audio']['layer'] == 1) {
  1255. $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 64000=>0, 96000=>0, 128000=>0, 160000=>0, 192000=>0, 224000=>0, 256000=>0, 288000=>0, 320000=>0, 352000=>0, 384000=>0, 416000=>0, 448000=>0);
  1256. }
  1257. } elseif ($info['mpeg']['audio']['layer'] == 1) {
  1258. $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 32000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 144000=>0, 160000=>0, 176000=>0, 192000=>0, 224000=>0, 256000=>0);
  1259. } else {
  1260. $info['mpeg']['audio']['bitrate_distribution'] = array('free'=>0, 8000=>0, 16000=>0, 24000=>0, 32000=>0, 40000=>0, 48000=>0, 56000=>0, 64000=>0, 80000=>0, 96000=>0, 112000=>0, 128000=>0, 144000=>0, 160000=>0);
  1261. }
  1262. $dummy = array('error'=>$info['error'], 'warning'=>$info['warning'], 'avdataend'=>$info['avdataend'], 'avdataoffset'=>$info['avdataoffset']);
  1263. $synchstartoffset = $info['avdataoffset'];
  1264. $this->fseek($info['avdataoffset']);
  1265. // you can play with these numbers:
  1266. $max_frames_scan = 50000;
  1267. $max_scan_segments = 10;
  1268. // don't play with these numbers:
  1269. $FastMode = false;
  1270. $SynchErrorsFound = 0;
  1271. $frames_scanned = 0;
  1272. $this_scan_segment = 0;
  1273. $frames_scan_per_segment = ceil($max_frames_scan / $max_scan_segments);
  1274. $pct_data_scanned = 0;
  1275. for ($current_segment = 0; $current_segment < $max_scan_segments; $current_segment++) {
  1276. $frames_scanned_this_segment = 0;
  1277. if ($this->ftell() >= $info['avdataend']) {
  1278. break;
  1279. }
  1280. $scan_start_offset[$current_segment] = max($this->ftell(), $info['avdataoffset'] + round($current_segment * (($info['avdataend'] - $info['avdataoffset']) / $max_scan_segments)));
  1281. if ($current_segment > 0) {
  1282. $this->fseek($scan_start_offset[$current_segment]);
  1283. $buffer_4k = $this->fread(4096);
  1284. for ($j = 0; $j < (strlen($buffer_4k) - 4); $j++) {
  1285. if (($buffer_4k{$j} == "\xFF") && ($buffer_4k{($j + 1)} > "\xE0")) { // synch detected
  1286. if ($this->decodeMPEGaudioHeader($scan_start_offset[$current_segment] + $j, $dummy, false, false, $FastMode)) {
  1287. $calculated_next_offset = $scan_start_offset[$current_segment] + $j + $dummy['mpeg']['audio']['framelength'];
  1288. if ($this->decodeMPEGaudioHeader($calculated_next_offset, $dummy, false, false, $FastMode)) {
  1289. $scan_start_offset[$current_segment] += $j;
  1290. break;
  1291. }
  1292. }
  1293. }
  1294. }
  1295. }
  1296. $synchstartoffset = $scan_start_offset[$current_segment];
  1297. while ($this->decodeMPEGaudioHeader($synchstartoffset, $dummy, false, false, $FastMode)) {
  1298. $FastMode = true;
  1299. $thisframebitrate = $MPEGaudioBitrateLookup[$MPEGaudioVersionLookup[$dummy['mpeg']['audio']['raw']['version']]][$MPEGaudioLayerLookup[$dummy['mpeg']['audio']['raw']['layer']]][$dummy['mpeg']['audio']['raw']['bitrate']];
  1300. if (empty($dummy['mpeg']['audio']['framelength'])) {
  1301. $SynchErrorsFound++;
  1302. $synchstartoffset++;
  1303. } else {
  1304. getid3_lib::safe_inc($info['mpeg']['audio']['bitrate_distribution'][$thisframebitrate]);
  1305. getid3_lib::safe_inc($info['mpeg']['audio']['stereo_distribution'][$dummy['mpeg']['audio']['channelmode']]);
  1306. getid3_lib::safe_inc($info['mpeg']['audio']['version_distribution'][$dummy['mpeg']['audio']['version']]);
  1307. $synchstartoffset += $dummy['mpeg']['audio']['framelength'];
  1308. }
  1309. $frames_scanned++;
  1310. if ($frames_scan_per_segment && (++$frames_scanned_this_segment >= $frames_scan_per_segment)) {
  1311. $this_pct_scanned = ($this->ftell() - $scan_start_offset[$current_segment]) / ($info['avdataend'] - $info['avdataoffset']);
  1312. if (($current_segment == 0) && (($this_pct_scanned * $max_scan_segments) >= 1)) {
  1313. // file likely contains < $max_frames_scan, just scan as one segment
  1314. $max_scan_segments = 1;
  1315. $frames_scan_per_segment = $max_frames_scan;
  1316. } else {
  1317. $pct_data_scanned += $this_pct_scanned;
  1318. break;
  1319. }
  1320. }
  1321. }
  1322. }
  1323. if ($pct_data_scanned > 0) {
  1324. $info['warning'][] = 'too many MPEG audio frames to scan, only scanned '.$frames_scanned.' frames in '.$max_scan_segments.' segments ('.number_format($pct_data_scanned * 100, 1).'% of file) and extrapolated distribution, playtime and bitrate may be incorrect.';
  1325. foreach ($info['mpeg']['audio'] as $key1 => $value1) {
  1326. if (!preg_match('#_distribution$#i', $key1)) {
  1327. continue;
  1328. }
  1329. foreach ($value1 as $key2 => $value2) {
  1330. $info['mpeg']['audio'][$key1][$key2] = round($value2 / $pct_data_scanned);
  1331. }
  1332. }
  1333. }
  1334. if ($SynchErrorsFound > 0) {
  1335. $info['warning'][] = 'Found '.$SynchErrorsFound.' synch errors in histogram analysis';
  1336. //return false;
  1337. }
  1338. $bittotal = 0;
  1339. $framecounter = 0;
  1340. foreach ($info['mpeg']['audio']['bitrate_distribution'] as $bitratevalue => $bitratecount) {
  1341. $framecounter += $bitratecount;
  1342. if ($bitratevalue != 'free') {
  1343. $bittotal += ($bitratevalue * $bitratecount);
  1344. }
  1345. }
  1346. if ($framecounter == 0) {
  1347. $info['error'][] = 'Corrupt MP3 file: framecounter == zero';
  1348. return false;
  1349. }
  1350. $info['mpeg']['audio']['frame_count'] = getid3_lib::CastAsInt($framecounter);
  1351. $info['mpeg']['audio']['bitrate'] = ($bittotal / $framecounter);
  1352. $info['audio']['bitrate'] = $info['mpeg']['audio']['bitrate'];
  1353. // Definitively set VBR vs CBR, even if the Xing/LAME/VBRI header says differently
  1354. $distinct_bitrates = 0;
  1355. foreach ($info['mpeg']['audio']['bitrate_distribution'] as $bitrate_value => $bitrate_count) {
  1356. if ($bitrate_count > 0) {
  1357. $distinct_bitrates++;
  1358. }
  1359. }
  1360. if ($distinct_bitrates > 1) {
  1361. $info['mpeg']['audio']['bitrate_mode'] = 'vbr';
  1362. } else {
  1363. $info['mpeg']['audio']['bitrate_mode'] = 'cbr';
  1364. }
  1365. $info['audio']['bitrate_mode'] = $info['mpeg']['audio']['bitrate_mode'];
  1366. }
  1367. break; // exit while()
  1368. }
  1369. }
  1370. $SynchSeekOffset++;
  1371. if (($avdataoffset + $SynchSeekOffset) >= $info['avdataend']) {
  1372. // end of file/data
  1373. if (empty($info['mpeg']['audio'])) {
  1374. $info['error'][] = 'could not find valid MPEG synch before end of file';
  1375. if (isset($info['audio']['bitrate'])) {
  1376. unset($info['audio']['bitrate']);
  1377. }
  1378. if (isset($info['mpeg']['audio'])) {
  1379. unset($info['mpeg']['audio']);
  1380. }
  1381. if (isset($info['mpeg']) && (!is_array($info['mpeg']) || empty($info['mpeg']))) {
  1382. unset($info['mpeg']);
  1383. }
  1384. return false;
  1385. }
  1386. break;
  1387. }
  1388. }
  1389. $info['audio']['channels'] = $info['mpeg']['audio']['channels'];
  1390. $info['audio']['channelmode'] = $info['mpeg']['audio']['channelmode'];
  1391. $info['audio']['sample_rate'] = $info['mpeg']['audio']['sample_rate'];
  1392. return true;
  1393. }
  1394. public static function MPEGaudioVersionArray() {
  1395. static $MPEGaudioVersion = array('2.5', false, '2', '1');
  1396. return $MPEGaudioVersion;
  1397. }
  1398. public static function MPEGaudioLayerArray() {
  1399. static $MPEGaudioLayer = array(false, 3, 2, 1);
  1400. return $MPEGaudioLayer;
  1401. }
  1402. public static function MPEGaudioBitrateArray() {
  1403. static $MPEGaudioBitrate;
  1404. if (empty($MPEGaudioBitrate)) {
  1405. $MPEGaudioBitrate = array (
  1406. '1' => array (1 => array('free', 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000),
  1407. 2 => array('free', 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000),
  1408. 3 => array('free', 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000)
  1409. ),
  1410. '2' => array (1 => array('free', 32000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000),
  1411. 2 => array('free', 8000, 16000, 24000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 144000, 160000),
  1412. )
  1413. );
  1414. $MPEGaudioBitrate['2'][3] = $MPEGaudioBitrate['2'][2];
  1415. $MPEGaudioBitrate['2.5'] = $MPEGaudioBitrate['2'];
  1416. }
  1417. return $MPEGaudioBitrate;
  1418. }
  1419. public static function MPEGaudioFrequencyArray() {
  1420. static $MPEGaudioFrequency;
  1421. if (empty($MPEGaudioFrequency)) {
  1422. $MPEGaudioFrequency = array (
  1423. '1' => array(44100, 48000, 32000),
  1424. '2' => array(22050, 24000, 16000),
  1425. '2.5' => array(11025, 12000, 8000)
  1426. );
  1427. }
  1428. return $MPEGaudioFrequency;
  1429. }
  1430. public static function MPEGaudioChannelModeArray() {
  1431. static $MPEGaudioChannelMode = array('stereo', 'joint stereo', 'dual channel', 'mono');
  1432. return $MPEGaudioChannelMode;
  1433. }
  1434. public static function MPEGaudioModeExtensionArray() {
  1435. static $MPEGaudioModeExtension;
  1436. if (empty($MPEGaudioModeExtension)) {
  1437. $MPEGaudioModeExtension = array (
  1438. 1 => array('4-31', '8-31', '12-31', '16-31'),
  1439. 2 => array('4-31', '8-31', '12-31', '16-31'),
  1440. 3 => array('', 'IS', 'MS', 'IS+MS')
  1441. );
  1442. }
  1443. return $MPEGaudioModeExtension;
  1444. }
  1445. public static function MPEGaudioEmphasisArray() {
  1446. static $MPEGaudioEmphasis = array('none', '50/15ms', false, 'CCIT J.17');
  1447. return $MPEGaudioEmphasis;
  1448. }
  1449. public static function MPEGaudioHeaderBytesValid($head4, $allowBitrate15=false) {
  1450. return self::MPEGaudioHeaderValid(self::MPEGaudioHeaderDecode($head4), false, $allowBitrate15);
  1451. }
  1452. public static function MPEGaudioHeaderValid($rawarray, $echoerrors=false, $allowBitrate15=false) {
  1453. if (($rawarray['synch'] & 0x0FFE) != 0x0FFE) {
  1454. return false;
  1455. }
  1456. static $MPEGaudioVersionLookup;
  1457. static $MPEGaudioLayerLookup;
  1458. static $MPEGaudioBitrateLookup;
  1459. static $MPEGaudioFrequencyLookup;
  1460. static $MPEGaudioChannelModeLookup;
  1461. static $MPEGaudioModeExtensionLookup;
  1462. static $MPEGaudioEmphasisLookup;
  1463. if (empty($MPEGaudioVersionLookup)) {
  1464. $MPEGaudioVersionLookup = self::MPEGaudioVersionArray();
  1465. $MPEGaudioLayerLookup = self::MPEGaudioLayerArray();
  1466. $MPEGaudioBitrateLookup = self::MPEGaudioBitrateArray();
  1467. $MPEGaudioFrequencyLookup = self::MPEGaudioFrequencyArray();
  1468. $MPEGaudioChannelModeLookup = self::MPEGaudioChannelModeArray();
  1469. $MPEGaudioModeExtensionLookup = self::MPEGaudioModeExtensionArray();
  1470. $MPEGaudioEmphasisLookup = self::MPEGaudioEmphasisArray();
  1471. }
  1472. if (isset($MPEGaudioVersionLookup[$rawarray['version']])) {
  1473. $decodedVersion = $MPEGaudioVersionLookup[$rawarray['version']];
  1474. } else {
  1475. echo ($echoerrors ? "\n".'invalid Version ('.$rawarray['version'].')' : '');
  1476. return false;
  1477. }
  1478. if (isset($MPEGaudioLayerLookup[$rawarray['layer']])) {
  1479. $decodedLayer = $MPEGaudioLayerLookup[$rawarray['layer']];
  1480. } else {
  1481. echo ($echoerrors ? "\n".'invalid Layer ('.$rawarray['layer'].')' : '');
  1482. return false;
  1483. }
  1484. if (!isset($MPEGaudioBitrateLookup[$decodedVersion][$decodedLayer][$rawarray['bitrate']])) {
  1485. echo ($echoerrors ? "\n".'invalid Bitrate ('.$rawarray['bitrate'].')' : '');
  1486. if ($rawarray['bitrate'] == 15) {
  1487. // known issue in LAME 3.90 - 3.93.1 where free-format has bitrate ID of 15 instead of 0
  1488. // let it go through here otherwise file will not be identified
  1489. if (!$allowBitrate15) {
  1490. return false;
  1491. }
  1492. } else {
  1493. return false;
  1494. }
  1495. }
  1496. if (!isset($MPEGaudioFrequencyLookup[$decodedVersion][$rawarray['sample_rate']])) {
  1497. echo ($echoerrors ? "\n".'invalid Frequency ('.$rawarray['sample_rate'].')' : '');
  1498. return false;
  1499. }
  1500. if (!isset($MPEGaudioChannelModeLookup[$rawarray['channelmode']])) {
  1501. echo ($echoerrors ? "\n".'invalid ChannelMode ('.$rawarray['channelmode'].')' : '');
  1502. return false;
  1503. }
  1504. if (!isset($MPEGaudioModeExtensionLookup[$decodedLayer][$rawarray['modeextension']])) {
  1505. echo ($echoerrors ? "\n".'invalid Mode Extension ('.$rawarray['modeextension'].')' : '');
  1506. return false;
  1507. }
  1508. if (!isset($MPEGaudioEmphasisLookup[$rawarray['emphasis']])) {
  1509. echo ($echoerrors ? "\n".'invalid Emphasis ('.$rawarray['emphasis'].')' : '');
  1510. return false;
  1511. }
  1512. // These are just either set or not set, you can't mess that up :)
  1513. // $rawarray['protection'];
  1514. // $rawarray['padding'];
  1515. // $rawarray['private'];
  1516. // $rawarray['copyright'];
  1517. // $rawarray['original'];
  1518. return true;
  1519. }
  1520. public static function MPEGaudioHeaderDecode($Header4Bytes) {
  1521. // AAAA AAAA AAAB BCCD EEEE FFGH IIJJ KLMM
  1522. // A - Frame sync (all bits set)
  1523. // B - MPEG Audio version ID
  1524. // C - Layer description
  1525. // D - Protection bit
  1526. // E - Bitrate index
  1527. // F - Sampling rate frequency index
  1528. // G - Padding bit
  1529. // H - Private bit
  1530. // I - Channel Mode
  1531. // J - Mode extension (Only if Joint stereo)
  1532. // K - Copyright
  1533. // L - Original
  1534. // M - Emphasis
  1535. if (strlen($Header4Bytes) != 4) {
  1536. return false;
  1537. }
  1538. $MPEGrawHeader['synch'] = (getid3_lib::BigEndian2Int(substr($Header4Bytes, 0, 2)) & 0xFFE0) >> 4;
  1539. $MPEGrawHeader['version'] = (ord($Header4Bytes{1}) & 0x18) >> 3; // BB
  1540. $MPEGrawHeader['layer'] = (ord($Header4Bytes{1}) & 0x06) >> 1; // CC
  1541. $MPEGrawHeader['protection'] = (ord($Header4Bytes{1}) & 0x01); // D
  1542. $MPEGrawHeader['bitrate'] = (ord($Header4Bytes{2}) & 0xF0) >> 4; // EEEE
  1543. $MPEGrawHeader['sample_rate'] = (ord($Header4Bytes{2}) & 0x0C) >> 2; // FF
  1544. $MPEGrawHeader['padding'] = (ord($Header4Bytes{2}) & 0x02) >> 1; // G
  1545. $MPEGrawHeader['private'] = (ord($Header4Bytes{2}) & 0x01); // H
  1546. $MPEGrawHeader['channelmode'] = (ord($Header4Bytes{3}) & 0xC0) >> 6; // II
  1547. $MPEGrawHeader['modeextension'] = (ord($Header4Bytes{3}) & 0x30) >> 4; // JJ
  1548. $MPEGrawHeader['copyright'] = (ord($Header4Bytes{3}) & 0x08) >> 3; // K
  1549. $MPEGrawHeader['original'] = (ord($Header4Bytes{3}) & 0x04) >> 2; // L
  1550. $MPEGrawHeader['emphasis'] = (ord($Header4Bytes{3}) & 0x03); // MM
  1551. return $MPEGrawHeader;
  1552. }
  1553. public static function MPEGaudioFrameLength(&$bitrate, &$version, &$layer, $padding, &$samplerate) {
  1554. static $AudioFrameLengthCache = array();
  1555. if (!isset($AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate])) {
  1556. $AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate] = false;
  1557. if ($bitrate != 'free') {
  1558. if ($version == '1') {
  1559. if ($layer == '1') {
  1560. // For Layer I slot is 32 bits long
  1561. $FrameLengthCoefficient = 48;
  1562. $SlotLength = 4;
  1563. } else { // Layer 2 / 3
  1564. // for Layer 2 and Layer 3 slot is 8 bits long.
  1565. $FrameLengthCoefficient = 144;
  1566. $SlotLength = 1;
  1567. }
  1568. } else { // MPEG-2 / MPEG-2.5
  1569. if ($layer == '1') {
  1570. // For Layer I slot is 32 bits long
  1571. $FrameLengthCoefficient = 24;
  1572. $SlotLength = 4;
  1573. } elseif ($layer == '2') {
  1574. // for Layer 2 and Layer 3 slot is 8 bits long.
  1575. $FrameLengthCoefficient = 144;
  1576. $SlotLength = 1;
  1577. } else { // layer 3
  1578. // for Layer 2 and Layer 3 slot is 8 bits long.
  1579. $FrameLengthCoefficient = 72;
  1580. $SlotLength = 1;
  1581. }
  1582. }
  1583. // FrameLengthInBytes = ((Coefficient * BitRate) / SampleRate) + Padding
  1584. if ($samplerate > 0) {
  1585. $NewFramelength = ($FrameLengthCoefficient * $bitrate) / $samplerate;
  1586. $NewFramelength = floor($NewFramelength / $SlotLength) * $SlotLength; // round to next-lower multiple of SlotLength (1 byte for Layer 2/3, 4 bytes for Layer I)
  1587. if ($padding) {
  1588. $NewFramelength += $SlotLength;
  1589. }
  1590. $AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate] = (int) $NewFramelength;
  1591. }
  1592. }
  1593. }
  1594. return $AudioFrameLengthCache[$bitrate][$version][$layer][$padding][$samplerate];
  1595. }
  1596. public static function ClosestStandardMP3Bitrate($bit_rate) {
  1597. static $standard_bit_rates = array (320000, 256000, 224000, 192000, 160000, 128000, 112000, 96000, 80000, 64000, 56000, 48000, 40000, 32000, 24000, 16000, 8000);
  1598. static $bit_rate_table = array (0=>'-');
  1599. $round_bit_rate = intval(round($bit_rate, -3));
  1600. if (!isset($bit_rate_table[$round_bit_rate])) {
  1601. if ($round_bit_rate > max($standard_bit_rates)) {
  1602. $bit_rate_table[$round_bit_rate] = round($bit_rate, 2 - strlen($bit_rate));
  1603. } else {
  1604. $bit_rate_table[$round_bit_rate] = max($standard_bit_rates);
  1605. foreach ($standard_bit_rates as $standard_bit_rate) {
  1606. if ($round_bit_rate >= $standard_bit_rate + (($bit_rate_table[$round_bit_rate] - $standard_bit_rate) / 2)) {
  1607. break;
  1608. }
  1609. $bit_rate_table[$round_bit_rate] = $standard_bit_rate;
  1610. }
  1611. }
  1612. }
  1613. return $bit_rate_table[$round_bit_rate];
  1614. }
  1615. public static function XingVBRidOffset($version, $channelmode) {
  1616. static $XingVBRidOffsetCache = array();
  1617. if (empty($XingVBRidOffset)) {
  1618. $XingVBRidOffset = array (
  1619. '1' => array ('mono' => 0x15, // 4 + 17 = 21
  1620. 'stereo' => 0x24, // 4 + 32 = 36
  1621. 'joint stereo' => 0x24,
  1622. 'dual channel' => 0x24
  1623. ),
  1624. '2' => array ('mono' => 0x0D, // 4 + 9 = 13
  1625. 'stereo' => 0x15, // 4 + 17 = 21
  1626. 'joint stereo' => 0x15,
  1627. 'dual channel' => 0x15
  1628. ),
  1629. '2.5' => array ('mono' => 0x15,
  1630. 'stereo' => 0x15,
  1631. 'joint stereo' => 0x15,
  1632. 'dual channel' => 0x15
  1633. )
  1634. );
  1635. }
  1636. return $XingVBRidOffset[$version][$channelmode];
  1637. }
  1638. public static function LAMEvbrMethodLookup($VBRmethodID) {
  1639. static $LAMEvbrMethodLookup = array(
  1640. 0x00 => 'unknown',
  1641. 0x01 => 'cbr',
  1642. 0x02 => 'abr',
  1643. 0x03 => 'vbr-old / vbr-rh',
  1644. 0x04 => 'vbr-new / vbr-mtrh',
  1645. 0x05 => 'vbr-mt',
  1646. 0x06 => 'vbr (full vbr method 4)',
  1647. 0x08 => 'cbr (constant bitrate 2 pass)',
  1648. 0x09 => 'abr (2 pass)',
  1649. 0x0F => 'reserved'
  1650. );
  1651. return (isset($LAMEvbrMethodLookup[$VBRmethodID]) ? $LAMEvbrMethodLookup[$VBRmethodID] : '');
  1652. }
  1653. public static function LAMEmiscStereoModeLookup($StereoModeID) {
  1654. static $LAMEmiscStereoModeLookup = array(
  1655. 0 => 'mono',
  1656. 1 => 'stereo',
  1657. 2 => 'dual mono',
  1658. 3 => 'joint stereo',
  1659. 4 => 'forced stereo',
  1660. 5 => 'auto',
  1661. 6 => 'intensity stereo',
  1662. 7 => 'other'
  1663. );
  1664. return (isset($LAMEmiscStereoModeLookup[$StereoModeID]) ? $LAMEmiscStereoModeLookup[$StereoModeID] : '');
  1665. }
  1666. public static function LAMEmiscSourceSampleFrequencyLookup($SourceSampleFrequencyID) {
  1667. static $LAMEmiscSourceSampleFrequencyLookup = array(
  1668. 0 => '<= 32 kHz',
  1669. 1 => '44.1 kHz',
  1670. 2 => '48 kHz',
  1671. 3 => '> 48kHz'
  1672. );
  1673. return (isset($LAMEmiscSourceSampleFrequencyLookup[$SourceSampleFrequencyID]) ? $LAMEmiscSourceSampleFrequencyLookup[$SourceSampleFrequencyID] : '');
  1674. }
  1675. public static function LAMEsurroundInfoLookup($SurroundInfoID) {
  1676. static $LAMEsurroundInfoLookup = array(
  1677. 0 => 'no surround info',
  1678. 1 => 'DPL encoding',
  1679. 2 => 'DPL2 encoding',
  1680. 3 => 'Ambisonic encoding'
  1681. );
  1682. return (isset($LAMEsurroundInfoLookup[$SurroundInfoID]) ? $LAMEsurroundInfoLookup[$SurroundInfoID] : 'reserved');
  1683. }
  1684. public static function LAMEpresetUsedLookup($LAMEtag) {
  1685. if ($LAMEtag['preset_used_id'] == 0) {
  1686. // no preset used (LAME >=3.93)
  1687. // no preset recorded (LAME <3.93)
  1688. return '';
  1689. }
  1690. $LAMEpresetUsedLookup = array();
  1691. ///// THIS PART CANNOT BE STATIC .
  1692. for ($i = 8; $i <= 320; $i++) {
  1693. switch ($LAMEtag['vbr_method']) {
  1694. case 'cbr':
  1695. $LAMEpresetUsedLookup[$i] = '--alt-preset '.$LAMEtag['vbr_method'].' '.$i;
  1696. break;
  1697. case 'abr':
  1698. default: // other VBR modes shouldn't be here(?)
  1699. $LAMEpresetUsedLookup[$i] = '--alt-preset '.$i;
  1700. break;
  1701. }
  1702. }
  1703. // named old-style presets (studio, phone, voice, etc) are handled in GuessEncoderOptions()
  1704. // named alt-presets
  1705. $LAMEpresetUsedLookup[1000] = '--r3mix';
  1706. $LAMEpresetUsedLookup[1001] = '--alt-preset standard';
  1707. $LAMEpresetUsedLookup[1002] = '--alt-preset extreme';
  1708. $LAMEpresetUsedLookup[1003] = '--alt-preset insane';
  1709. $LAMEpresetUsedLookup[1004] = '--alt-preset fast standard';
  1710. $LAMEpresetUsedLookup[1005] = '--alt-preset fast extreme';
  1711. $LAMEpresetUsedLookup[1006] = '--alt-preset medium';
  1712. $LAMEpresetUsedLookup[1007] = '--alt-preset fast medium';
  1713. // LAME 3.94 additions/changes
  1714. $LAMEpresetUsedLookup[1010] = '--preset portable'; // 3.94a15 Oct 21 2003
  1715. $LAMEpresetUsedLookup[1015] = '--preset radio'; // 3.94a15 Oct 21 2003
  1716. $LAMEpresetUsedLookup[320] = '--preset insane'; // 3.94a15 Nov 12 2003
  1717. $LAMEpresetUsedLookup[410] = '-V9';
  1718. $LAMEpresetUsedLookup[420] = '-V8';
  1719. $LAMEpresetUsedLookup[440] = '-V6';
  1720. $LAMEpresetUsedLookup[430] = '--preset radio'; // 3.94a15 Nov 12 2003
  1721. $LAMEpresetUsedLookup[450] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'portable'; // 3.94a15 Nov 12 2003
  1722. $LAMEpresetUsedLookup[460] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'medium'; // 3.94a15 Nov 12 2003
  1723. $LAMEpresetUsedLookup[470] = '--r3mix'; // 3.94b1 Dec 18 2003
  1724. $LAMEpresetUsedLookup[480] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'standard'; // 3.94a15 Nov 12 2003
  1725. $LAMEpresetUsedLookup[490] = '-V1';
  1726. $LAMEpresetUsedLookup[500] = '--preset '.(($LAMEtag['raw']['vbr_method'] == 4) ? 'fast ' : '').'extreme'; // 3.94a15 Nov 12 2003
  1727. return (isset($LAMEpresetUsedLookup[$LAMEtag['preset_used_id']]) ? $LAMEpresetUsedLookup[$LAMEtag['preset_used_id']] : 'new/unknown preset: '.$LAMEtag['preset_used_id'].' - report to info@getid3.org');
  1728. }
  1729. }