PageRenderTime 68ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/getid3/module.audio.mp3.php

https://bitbucket.org/holyfield/wpgetid
PHP | 2011 lines | 1431 code | 284 blank | 296 comment | 399 complexity | f6ce3db00d2abb944602deee9130d95a MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at http://getid3.sourceforge.net //
  5. // or http://www.getid3.org //
  6. /////////////////////////////////////////////////////////////////
  7. // See readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.mp3.php //
  11. // module for analyzing MP3 files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. // number of frames to scan to determine if MPEG-audio sequence is valid
  16. // Lower this number to 5-20 for faster scanning
  17. // Increase this number to 50+ for most accurate detection of valid VBR/CBR
  18. // mpeg-audio streams
  19. define('GETID3_MP3_VALID_CHECK_FRAMES', 35);
  20. class getid3_mp3 extends getid3_handler
  21. {
  22. var $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
  23. function Analyze() {
  24. $info = &$this->getid3->info;
  25. $initialOffset = $info['avdataoffset'];
  26. if (!$this->getOnlyMPEGaudioInfo($info['avdataoffset'])) {
  27. if ($this->allow_bruteforce) {
  28. $info['error'][] = 'Rescanning file in BruteForce mode';
  29. $this->getOnlyMPEGaudioInfoBruteForce($this->getid3->fp, $info);
  30. }
  31. }
  32. if (isset($info['mpeg']['audio']['bitrate_mode'])) {
  33. $info['audio']['bitrate_mode'] = strtolower($info['mpeg']['audio']['bitrate_mode']);
  34. }
  35. if (((isset($info['id3v2']['headerlength']) && ($info['avdataoffset'] > $info['id3v2']['headerlength'])) || (!isset($info['id3v2']) && ($info['avdataoffset'] > 0) && ($info['avdataoffset'] != $initialOffset)))) {
  36. $synchoffsetwarning = 'Unknown data before synch ';
  37. if (isset($info['id3v2']['headerlength'])) {
  38. $synchoffsetwarning .= '(ID3v2 header ends at '.$info['id3v2']['headerlength'].', then '.($info['avdataoffset'] - $info['id3v2']['headerlength']).' bytes garbage, ';
  39. } elseif ($initialOffset > 0) {
  40. $synchoffsetwarning .= '(should be at '.$initialOffset.', ';
  41. } else {
  42. $synchoffsetwarning .= '(should be at beginning of file, ';
  43. }
  44. $synchoffsetwarning .= 'synch detected at '.$info['avdataoffset'].')';
  45. if (isset($info['audio']['bitrate_mode']) && ($info['audio']['bitrate_mode'] == 'cbr')) {
  46. if (!empty($info['id3v2']['headerlength']) && (($info['avdataoffset'] - $info['id3v2']['headerlength']) == $info['mpeg']['audio']['framelength'])) {
  47. $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90-3.92) DLL in CBR mode.';
  48. $info['audio']['codec'] = 'LAME';
  49. $CurrentDataLAMEversionString = 'LAME3.';
  50. } elseif (empty($info['id3v2']['headerlength']) && ($info['avdataoffset'] == $info['mpeg']['audio']['framelength'])) {
  51. $synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90 - 3.92) DLL in CBR mode.';
  52. $info['audio']['codec'] = 'LAME';
  53. $CurrentDataLAMEversionString = 'LAME3.';
  54. }
  55. }
  56. $info['warning'][] = $synchoffsetwarning;
  57. }
  58. if (isset($info['mpeg']['audio']['LAME'])) {
  59. $info['audio']['codec'] = 'LAME';
  60. if (!empty($info['mpeg']['audio']['LAME']['long_version'])) {
  61. $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['long_version'], "\x00");
  62. } elseif (!empty($info['mpeg']['audio']['LAME']['short_version'])) {
  63. $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['short_version'], "\x00");
  64. }
  65. }
  66. $CurrentDataLAMEversionString = (!empty($CurrentDataLAMEversionString) ? $CurrentDataLAMEversionString : (isset($info['audio']['encoder']) ? $info['audio']['encoder'] : ''));
  67. if (!empty($CurrentDataLAMEversionString) && (substr($CurrentDataLAMEversionString, 0, 6) == 'LAME3.') && !preg_match('[0-9\)]', substr($CurrentDataLAMEversionString, -1))) {
  68. // a version number of LAME that does not end with a number like "LAME3.92"
  69. // or with a closing parenthesis like "LAME3.88 (alpha)"
  70. // or a version of LAME with the LAMEtag-not-filled-in-DLL-mode bug (3.90-3.92)
  71. // not sure what the actual last frame length will be, but will be less than or equal to 1441
  72. $PossiblyLongerLAMEversion_FrameLength = 1441;
  73. // Not sure what version of LAME this is - look in padding of last frame for longer version string
  74. $PossibleLAMEversionStringOffset = $info['avdataend'] - $PossiblyLongerLAMEversion_FrameLength;
  75. fseek($this->getid3->fp, $PossibleLAMEversionStringOffset);
  76. $PossiblyLongerLAMEversion_Data = fread($this->getid3->fp, $PossiblyLongerLAMEversion_FrameLength);
  77. switch (substr($CurrentDataLAMEversionString, -1)) {
  78. case 'a':
  79. case 'b':
  80. // "LAME3.94a" will have a longer version string of "LAME3.94 (alpha)" for example
  81. // need to trim off "a" to match longer string
  82. $CurrentDataLAMEversionString = substr($CurrentDataLAMEversionString, 0, -1);
  83. break;
  84. }
  85. if (($PossiblyLongerLAMEversion_String = strstr($PossiblyLongerLAMEversion_Data, $CurrentDataLAMEversionString)) !== false) {
  86. if (substr($PossiblyLongerLAMEversion_String, 0, strlen($CurrentDataLAMEversionString)) == $CurrentDataLAMEversionString) {
  87. $PossiblyLongerLAMEversion_NewString = substr($PossiblyLongerLAMEversion_String, 0, strspn($PossiblyLongerLAMEversion_String, 'LAME0123456789., (abcdefghijklmnopqrstuvwxyzJFSOND)')); //"LAME3.90.3" "LAME3.87 (beta 1, Sep 27 2000)" "LAME3.88 (beta)"
  88. if (empty($info['audio']['encoder']) || (strlen($PossiblyLongerLAMEversion_NewString) > strlen($info['audio']['encoder']))) {
  89. $info['audio']['encoder'] = $PossiblyLongerLAMEversion_NewString;
  90. }
  91. }
  92. }
  93. }
  94. if (!empty($info['audio']['encoder'])) {
  95. $info['audio']['encoder'] = rtrim($info['audio']['encoder'], "\x00 ");
  96. }
  97. switch (isset($info['mpeg']['audio']['layer']) ? $info['mpeg']['audio']['layer'] : '') {
  98. case 1:
  99. case 2:
  100. $info['audio']['dataformat'] = 'mp'.$info['mpeg']['audio']['layer'];
  101. break;
  102. }
  103. if (isset($info['fileformat']) && ($info['fileformat'] == 'mp3')) {
  104. switch ($info['audio']['dataformat']) {
  105. case 'mp1':
  106. case 'mp2':
  107. case 'mp3':
  108. $info['fileformat'] = $info['audio']['dataformat'];
  109. break;
  110. default:
  111. $info['warning'][] = 'Expecting [audio][dataformat] to be mp1/mp2/mp3 when fileformat == mp3, [audio][dataformat] actually "'.$info['audio']['dataformat'].'"';
  112. break;
  113. }
  114. }
  115. if (empty($info['fileformat'])) {
  116. unset($info['fileformat']);
  117. unset($info['audio']['bitrate_mode']);
  118. unset($info['avdataoffset']);
  119. unset($info['avdataend']);
  120. return false;
  121. }
  122. $info['mime_type'] = 'audio/mpeg';
  123. $info['audio']['lossless'] = false;
  124. // Calculate playtime
  125. if (!isset($info['playtime_seconds']) && isset($info['audio']['bitrate']) && ($info['audio']['bitrate'] > 0)) {
  126. $info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['audio']['bitrate'];
  127. }
  128. $info['audio']['encoder_options'] = $this->GuessEncoderOptions();
  129. return true;
  130. }
  131. function GuessEncoderOptions() {
  132. // shortcuts
  133. $info = &$this->getid3->info;
  134. if (!empty($info['mpeg']['audio'])) {
  135. $thisfile_mpeg_audio = &$info['mpeg']['audio'];
  136. if (!empty($thisfile_mpeg_audio['LAME'])) {
  137. $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
  138. }
  139. }
  140. $encoder_options = '';
  141. static $NamedPresetBitrates = array(16, 24, 40, 56, 112, 128, 160, 192, 256);
  142. if (isset($thisfile_mpeg_audio['VBR_method']) && ($thisfile_mpeg_audio['VBR_method'] == 'Fraunhofer') && !empty($thisfile_mpeg_audio['VBR_quality'])) {
  143. $encoder_options = 'VBR q'.$thisfile_mpeg_audio['VBR_quality'];
  144. } elseif (!empty($thisfile_mpeg_audio_lame['preset_used']) && (!in_array($thisfile_mpeg_audio_lame['preset_used_id'], $NamedPresetBitrates))) {
  145. $encoder_options = $thisfile_mpeg_audio_lame['preset_used'];
  146. } elseif (!empty($thisfile_mpeg_audio_lame['vbr_quality'])) {
  147. static $KnownEncoderValues = array();
  148. if (empty($KnownEncoderValues)) {
  149. //$KnownEncoderValues[abrbitrate_minbitrate][vbr_quality][raw_vbr_method][raw_noise_shaping][raw_stereo_mode][ath_type][lowpass_frequency] = 'preset name';
  150. $KnownEncoderValues[0xFF][58][1][1][3][2][20500] = '--alt-preset insane'; // 3.90, 3.90.1, 3.92
  151. $KnownEncoderValues[0xFF][58][1][1][3][2][20600] = '--alt-preset insane'; // 3.90.2, 3.90.3, 3.91
  152. $KnownEncoderValues[0xFF][57][1][1][3][4][20500] = '--alt-preset insane'; // 3.94, 3.95
  153. $KnownEncoderValues['**'][78][3][2][3][2][19500] = '--alt-preset extreme'; // 3.90, 3.90.1, 3.92
  154. $KnownEncoderValues['**'][78][3][2][3][2][19600] = '--alt-preset extreme'; // 3.90.2, 3.91
  155. $KnownEncoderValues['**'][78][3][1][3][2][19600] = '--alt-preset extreme'; // 3.90.3
  156. $KnownEncoderValues['**'][78][4][2][3][2][19500] = '--alt-preset fast extreme'; // 3.90, 3.90.1, 3.92
  157. $KnownEncoderValues['**'][78][4][2][3][2][19600] = '--alt-preset fast extreme'; // 3.90.2, 3.90.3, 3.91
  158. $KnownEncoderValues['**'][78][3][2][3][4][19000] = '--alt-preset standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  159. $KnownEncoderValues['**'][78][3][1][3][4][19000] = '--alt-preset standard'; // 3.90.3
  160. $KnownEncoderValues['**'][78][4][2][3][4][19000] = '--alt-preset fast standard'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  161. $KnownEncoderValues['**'][78][4][1][3][4][19000] = '--alt-preset fast standard'; // 3.90.3
  162. $KnownEncoderValues['**'][88][4][1][3][3][19500] = '--r3mix'; // 3.90, 3.90.1, 3.92
  163. $KnownEncoderValues['**'][88][4][1][3][3][19600] = '--r3mix'; // 3.90.2, 3.90.3, 3.91
  164. $KnownEncoderValues['**'][67][4][1][3][4][18000] = '--r3mix'; // 3.94, 3.95
  165. $KnownEncoderValues['**'][68][3][2][3][4][18000] = '--alt-preset medium'; // 3.90.3
  166. $KnownEncoderValues['**'][68][4][2][3][4][18000] = '--alt-preset fast medium'; // 3.90.3
  167. $KnownEncoderValues[0xFF][99][1][1][1][2][0] = '--preset studio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  168. $KnownEncoderValues[0xFF][58][2][1][3][2][20600] = '--preset studio'; // 3.90.3, 3.93.1
  169. $KnownEncoderValues[0xFF][58][2][1][3][2][20500] = '--preset studio'; // 3.93
  170. $KnownEncoderValues[0xFF][57][2][1][3][4][20500] = '--preset studio'; // 3.94, 3.95
  171. $KnownEncoderValues[0xC0][88][1][1][1][2][0] = '--preset cd'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  172. $KnownEncoderValues[0xC0][58][2][2][3][2][19600] = '--preset cd'; // 3.90.3, 3.93.1
  173. $KnownEncoderValues[0xC0][58][2][2][3][2][19500] = '--preset cd'; // 3.93
  174. $KnownEncoderValues[0xC0][57][2][1][3][4][19500] = '--preset cd'; // 3.94, 3.95
  175. $KnownEncoderValues[0xA0][78][1][1][3][2][18000] = '--preset hifi'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  176. $KnownEncoderValues[0xA0][58][2][2][3][2][18000] = '--preset hifi'; // 3.90.3, 3.93, 3.93.1
  177. $KnownEncoderValues[0xA0][57][2][1][3][4][18000] = '--preset hifi'; // 3.94, 3.95
  178. $KnownEncoderValues[0x80][67][1][1][3][2][18000] = '--preset tape'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  179. $KnownEncoderValues[0x80][67][1][1][3][2][15000] = '--preset radio'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  180. $KnownEncoderValues[0x70][67][1][1][3][2][15000] = '--preset fm'; // 3.90, 3.90.1, 3.90.2, 3.91, 3.92
  181. $KnownEncoderValues[0x70][58][2][2][3][2][16000] = '--preset tape/radio/fm'; // 3.90.3, 3.93, 3.93.1
  182. $KnownEncoderValues[0x70][57][2][1][3][4][16000] = '--preset tape/radio/fm'; // 3.94, 3.95
  183. $KnownEncoderValues[0x38][58][2][2][0][2][10000] = '--preset voice'; // 3.90.3, 3.93, 3.93.1
  184. $KnownEncoderValues[0x38][57][2][1][0][4][15000] = '--preset voice'; // 3.94, 3.95
  185. $KnownEncoderValues[0x38][57][2][1][0][4][16000] = '--preset voice'; // 3.94a14
  186. $KnownEncoderValues[0x28][65][1][1][0][2][7500] = '--preset mw-us'; // 3.90, 3.90.1, 3.92
  187. $KnownEncoderValues[0x28][65][1][1][0][2][7600] = '--preset mw-us'; // 3.90.2, 3.91
  188. $KnownEncoderValues[0x28][58][2][2][0][2][7000] = '--preset mw-us'; // 3.90.3, 3.93, 3.93.1
  189. $KnownEncoderValues[0x28][57][2][1][0][4][10500] = '--preset mw-us'; // 3.94, 3.95
  190. $KnownEncoderValues[0x28][57][2][1][0][4][11200] = '--preset mw-us'; // 3.94a14
  191. $KnownEncoderValues[0x28][57][2][1][0][4][8800] = '--preset mw-us'; // 3.94a15
  192. $KnownEncoderValues[0x18][58][2][2][0][2][4000] = '--preset phon+/lw/mw-eu/sw'; // 3.90.3, 3.93.1
  193. $KnownEncoderValues[0x18][58][2][2][0][2][3900] = '--preset phon+/lw/mw-eu/sw'; // 3.93
  194. $KnownEncoderValues[0x18][57][2][1][0][4][5900] = '--preset phon+/lw/mw-eu/sw'; // 3.94, 3.95
  195. $KnownEncoderValues[0x18][57][2][1][0][4][6200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a14
  196. $KnownEncoderValues[0x18][57][2][1][0][4][3200] = '--preset phon+/lw/mw-eu/sw'; // 3.94a15
  197. $KnownEncoderValues[0x10][58][2][2][0][2][3800] = '--preset phone'; // 3.90.3, 3.93.1
  198. $KnownEncoderValues[0x10][58][2][2][0][2][3700] = '--preset phone'; // 3.93
  199. $KnownEncoderValues[0x10][57][2][1][0][4][5600] = '--preset phone'; // 3.94, 3.95
  200. }
  201. 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']])) {
  202. $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']];
  203. } 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']])) {
  204. $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']];
  205. } elseif ($info['audio']['bitrate_mode'] == 'vbr') {
  206. // http://gabriel.mp3-tech.org/mp3infotag.html
  207. // int Quality = (100 - 10 * gfp->VBR_q - gfp->quality)h
  208. $LAME_V_value = 10 - ceil($thisfile_mpeg_audio_lame['vbr_quality'] / 10);
  209. $LAME_q_value = 100 - $thisfile_mpeg_audio_lame['vbr_quality'] - ($LAME_V_value * 10);
  210. $encoder_options = '-V'.$LAME_V_value.' -q'.$LAME_q_value;
  211. } elseif ($info['audio']['bitrate_mode'] == 'cbr') {
  212. $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
  213. } else {
  214. $encoder_options = strtoupper($info['audio']['bitrate_mode']);
  215. }
  216. } elseif (!empty($thisfile_mpeg_audio_lame['bitrate_abr'])) {
  217. $encoder_options = 'ABR'.$thisfile_mpeg_audio_lame['bitrate_abr'];
  218. } elseif (!empty($info['audio']['bitrate'])) {
  219. if ($info['audio']['bitrate_mode'] == 'cbr') {
  220. $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
  221. } else {
  222. $encoder_options = strtoupper($info['audio']['bitrate_mode']);
  223. }
  224. }
  225. if (!empty($thisfile_mpeg_audio_lame['bitrate_min'])) {
  226. $encoder_options .= ' -b'.$thisfile_mpeg_audio_lame['bitrate_min'];
  227. }
  228. if (!empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev']) || !empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'])) {
  229. $encoder_options .= ' --nogap';
  230. }
  231. if (!empty($thisfile_mpeg_audio_lame['lowpass_frequency'])) {
  232. $ExplodedOptions = explode(' ', $encoder_options, 4);
  233. if ($ExplodedOptions[0] == '--r3mix') {
  234. $ExplodedOptions[1] = 'r3mix';
  235. }
  236. switch ($ExplodedOptions[0]) {
  237. case '--preset':
  238. case '--alt-preset':
  239. case '--r3mix':
  240. if ($ExplodedOptions[1] == 'fast') {
  241. $ExplodedOptions[1] .= ' '.$ExplodedOptions[2];
  242. }
  243. switch ($ExplodedOptions[1]) {
  244. case 'portable':
  245. case 'medium':
  246. case 'standard':
  247. case 'extreme':
  248. case 'insane':
  249. case 'fast portable':
  250. case 'fast medium':
  251. case 'fast standard':
  252. case 'fast extreme':
  253. case 'fast insane':
  254. case 'r3mix':
  255. static $ExpectedLowpass = array(
  256. 'insane|20500' => 20500,
  257. 'insane|20600' => 20600, // 3.90.2, 3.90.3, 3.91
  258. 'medium|18000' => 18000,
  259. 'fast medium|18000' => 18000,
  260. 'extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95
  261. 'extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1
  262. 'fast extreme|19500' => 19500, // 3.90, 3.90.1, 3.92, 3.95
  263. 'fast extreme|19600' => 19600, // 3.90.2, 3.90.3, 3.91, 3.93.1
  264. 'standard|19000' => 19000,
  265. 'fast standard|19000' => 19000,
  266. 'r3mix|19500' => 19500, // 3.90, 3.90.1, 3.92
  267. 'r3mix|19600' => 19600, // 3.90.2, 3.90.3, 3.91
  268. 'r3mix|18000' => 18000, // 3.94, 3.95
  269. );
  270. 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))) {
  271. $encoder_options .= ' --lowpass '.$thisfile_mpeg_audio_lame['lowpass_frequency'];
  272. }
  273. break;
  274. default:
  275. break;
  276. }
  277. break;
  278. }
  279. }
  280. if (isset($thisfile_mpeg_audio_lame['raw']['source_sample_freq'])) {
  281. if (($thisfile_mpeg_audio['sample_rate'] == 44100) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 1)) {
  282. $encoder_options .= ' --resample 44100';
  283. } elseif (($thisfile_mpeg_audio['sample_rate'] == 48000) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 2)) {
  284. $encoder_options .= ' --resample 48000';
  285. } elseif ($thisfile_mpeg_audio['sample_rate'] < 44100) {
  286. switch ($thisfile_mpeg_audio_lame['raw']['source_sample_freq']) {
  287. case 0: // <= 32000
  288. // may or may not be same as source frequency - ignore
  289. break;
  290. case 1: // 44100
  291. case 2: // 48000
  292. case 3: // 48000+
  293. $ExplodedOptions = explode(' ', $encoder_options, 4);
  294. switch ($ExplodedOptions[0]) {
  295. case '--preset':
  296. case '--alt-preset':
  297. switch ($ExplodedOptions[1]) {
  298. case 'fast':
  299. case 'portable':
  300. case 'medium':
  301. case 'standard':
  302. case 'extreme':
  303. case 'insane':
  304. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  305. break;
  306. default:
  307. static $ExpectedResampledRate = array(
  308. 'phon+/lw/mw-eu/sw|16000' => 16000,
  309. 'mw-us|24000' => 24000, // 3.95
  310. 'mw-us|32000' => 32000, // 3.93
  311. 'mw-us|16000' => 16000, // 3.92
  312. 'phone|16000' => 16000,
  313. 'phone|11025' => 11025, // 3.94a15
  314. 'radio|32000' => 32000, // 3.94a15
  315. 'fm/radio|32000' => 32000, // 3.92
  316. 'fm|32000' => 32000, // 3.90
  317. 'voice|32000' => 32000);
  318. if (!isset($ExpectedResampledRate[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio['sample_rate']])) {
  319. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  320. }
  321. break;
  322. }
  323. break;
  324. case '--r3mix':
  325. default:
  326. $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
  327. break;
  328. }
  329. break;
  330. }
  331. }
  332. }
  333. if (empty($encoder_options) && !empty($info['audio']['bitrate']) && !empty($info['audio']['bitrate_mode'])) {
  334. //$encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
  335. $encoder_options = strtoupper($info['audio']['bitrate_mode']);
  336. }
  337. return $encoder_options;
  338. }
  339. function decodeMPEGaudioHeader($offset, &$info, $recursivesearch=true, $ScanAsCBR=false, $FastMPEGheaderScan=false) {
  340. static $MPEGaudioVersionLookup;
  341. static $MPEGaudioLayerLookup;
  342. static $MPEGaudioBitrateLookup;
  343. static $MPEGaudioFrequencyLookup;
  344. static $MPEGaudioChannelModeLookup;
  345. static $MPEGaudioModeExtensionLookup;
  346. static $MPEGaudioEmphasisLookup;
  347. if (empty($MPEGaudioVersionLookup)) {
  348. $MPEGaudioVersionLookup = getid3_mp3::MPEGaudioVersionArray();
  349. $MPEGaudioLayerLookup = getid3_mp3::MPEGaudioLayerArray();
  350. $MPEGaudioBitrateLookup = getid3_mp3::MPEGaudioBitrateArray();
  351. $MPEGaudioFrequencyLookup = getid3_mp3::MPEGaudioFrequencyArray();
  352. $MPEGaudioChannelModeLookup = getid3_mp3::MPEGaudioChannelModeArray();
  353. $MPEGaudioModeExtensionLookup = getid3_mp3::MPEGaudioModeExtensionArray();
  354. $MPEGaudioEmphasisLookup = getid3_mp3::MPEGaudioEmphasisArray();
  355. }
  356. if (fseek($this->getid3->fp, $offset, SEEK_SET) != 0) {
  357. $info['error'][] = 'decodeMPEGaudioHeader() failed to seek to next offset at '.$offset;
  358. return false;
  359. }
  360. //$headerstring = fread($this->getid3->fp, 1441); // worst-case max length = 32kHz @ 320kbps layer 3 = 1441 bytes/frame
  361. $headerstring = fread($this->getid3->fp, 226); // LAME header at offset 36 + 190 bytes of Xing/LAME data
  362. // MP3 audio frame structure:
  363. // $aa $aa $aa $aa [$bb $bb] $cc...
  364. // where $aa..$aa is the four-byte mpeg-audio header (below)
  365. // $bb $bb is the optional 2-byte CRC
  366. // and $cc... is the audio data
  367. $head4 = substr($headerstring, 0, 4);
  368. static $MPEGaudioHeaderDecodeCache = array();
  369. if (isset($MPEGaudioHeaderDecodeCache[$head4])) {
  370. $MPEGheaderRawArray = $MPEGaudioHeaderDecodeCache[$head4];
  371. } else {
  372. $MPEGheaderRawArray = getid3_mp3::MPEGaudioHeaderDecode($head4);
  373. $MPEGaudioHeaderDecodeCache[$head4] = $MPEGheaderRawArray;
  374. }
  375. static $MPEGaudioHeaderValidCache = array();
  376. if (!isset($MPEGaudioHeaderValidCache[$head4])) { // Not in cache
  377. //$MPEGaudioHeaderValidCache[$head4] = getid3_mp3::MPEGaudioHeaderValid($MPEGheaderRawArray, false, true); // allow badly-formatted freeformat (from LAME 3.90 - 3.93.1)
  378. $MPEGaudioHeaderValidCache[$head4] = getid3_mp3::MPEGaudioHeaderValid($MPEGheaderRawArray, false, false);
  379. }
  380. // shortcut
  381. if (!isset($info['mpeg']['audio'])) {
  382. $info['mpeg']['audio'] = array();
  383. }
  384. $thisfile_mpeg_audio = &$info['mpeg']['audio'];
  385. if ($MPEGaudioHeaderValidCache[$head4]) {
  386. $thisfile_mpeg_audio['raw'] = $MPEGheaderRawArray;
  387. } else {
  388. $info['error'][] = 'Invalid MPEG audio header ('.getid3_lib::PrintHexBytes($head4).') at offset '.$offset;
  389. return false;
  390. }
  391. if (!$FastMPEGheaderScan) {
  392. $thisfile_mpeg_audio['version'] = $MPEGaudioVersionLookup[$thisfile_mpeg_audio['raw']['version']];
  393. $thisfile_mpeg_audio['layer'] = $MPEGaudioLayerLookup[$thisfile_mpeg_audio['raw']['layer']];
  394. $thisfile_mpeg_audio['channelmode'] = $MPEGaudioChannelModeLookup[$thisfile_mpeg_audio['raw']['channelmode']];
  395. $thisfile_mpeg_audio['channels'] = (($thisfile_mpeg_audio['channelmode'] == 'mono') ? 1 : 2);
  396. $thisfile_mpeg_audio['sample_rate'] = $MPEGaudioFrequencyLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['raw']['sample_rate']];
  397. $thisfile_mpeg_audio['protection'] = !$thisfile_mpeg_audio['raw']['protection'];
  398. $thisfile_mpeg_audio['private'] = (bool) $thisfile_mpeg_audio['raw']['private'];
  399. $thisfile_mpeg_audio['modeextension'] = $MPEGaudioModeExtensionLookup[$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['modeextension']];
  400. $thisfile_mpeg_audio['copyright'] = (bool) $thisfile_mpeg_audio['raw']['copyright'];
  401. $thisfile_mpeg_audio['original'] = (bool) $thisfile_mpeg_audio['raw']['original'];
  402. $thisfile_mpeg_audio['emphasis'] = $MPEGaudioEmphasisLookup[$thisfile_mpeg_audio['raw']['emphasis']];
  403. $info['audio']['channels'] = $thisfile_mpeg_audio['channels'];
  404. $info['audio']['sample_rate'] = $thisfile_mpeg_audio['sample_rate'];
  405. if ($thisfile_mpeg_audio['protection']) {
  406. $thisfile_mpeg_audio['crc'] = getid3_lib::BigEndian2Int(substr($headerstring, 4, 2));
  407. }
  408. }
  409. if ($thisfile_mpeg_audio['raw']['bitrate'] == 15) {
  410. // http://www.hydrogenaudio.org/?act=ST&f=16&t=9682&st=0
  411. $info['warning'][] = 'Invalid bitrate index (15), this is a known bug in free-format MP3s encoded by LAME v3.90 - 3.93.1';
  412. $thisfile_mpeg_audio['raw']['bitrate'] = 0;
  413. }
  414. $thisfile_mpeg_audio['padding'] = (bool) $thisfile_mpeg_audio['raw']['padding'];
  415. $thisfile_mpeg_audio['bitrate'] = $MPEGaudioBitrateLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['bitrate']];
  416. if (($thisfile_mpeg_audio['bitrate'] == 'free') && ($offset == $info['avdataoffset'])) {
  417. // only skip multiple frame check if free-format bitstream found at beginning of file
  418. // otherwise is quite possibly simply corrupted data
  419. $recursivesearch = false;
  420. }
  421. // For Layer 2 there are some combinations of bitrate and mode which are not allowed.
  422. if (!$FastMPEGheaderScan && ($thisfile_mpeg_audio['layer'] == '2')) {
  423. $info['audio']['dataformat'] = 'mp2';
  424. switch ($thisfile_mpeg_audio['channelmode']) {
  425. case 'mono':
  426. if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] <= 192000)) {
  427. // these are ok
  428. } else {
  429. $info['error'][] = $thisfile_mpeg_audio['bitrate'].'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
  430. return false;
  431. }
  432. break;
  433. case 'stereo':
  434. case 'joint stereo':
  435. case 'dual channel':
  436. if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] == 64000) || ($thisfile_mpeg_audio['bitrate'] >= 96000)) {
  437. // these are ok
  438. } else {
  439. $info['error'][] = intval(round($thisfile_mpeg_audio['bitrate'] / 1000)).'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
  440. return false;
  441. }
  442. break;
  443. }
  444. }
  445. if ($info['audio']['sample_rate'] > 0) {
  446. $thisfile_mpeg_audio['framelength'] = getid3_mp3::MPEGaudioFrameLength($thisfile_mpeg_audio['bitrate'], $thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['layer'], (int) $thisfile_mpeg_audio['padding'], $info['audio']['sample_rate']);
  447. }
  448. $nextframetestoffset = $offset + 1;
  449. if ($thisfile_mpeg_audio['bitrate'] != 'free') {
  450. $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
  451. if (isset($thisfile_mpeg_audio['framelength'])) {
  452. $nextframetestoffset = $offset + $thisfile_mpeg_audio['framelength'];
  453. } else {
  454. $info['error'][] = 'Frame at offset('.$offset.') is has an invalid frame length.';
  455. return false;
  456. }
  457. }
  458. $ExpectedNumberOfAudioBytes = 0;
  459. ////////////////////////////////////////////////////////////////////////////////////
  460. // Variable-bitrate headers
  461. if (substr($headerstring, 4 + 32, 4) == 'VBRI') {
  462. // Fraunhofer VBR header is hardcoded 'VBRI' at offset 0x24 (36)
  463. // specs taken from http://minnie.tuhs.org/pipermail/mp3encoder/2001-January/001800.html
  464. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  465. $thisfile_mpeg_audio['VBR_method'] = 'Fraunhofer';
  466. $info['audio']['codec'] = 'Fraunhofer';
  467. $SideInfoData = substr($headerstring, 4 + 2, 32);
  468. $FraunhoferVBROffset = 36;
  469. $thisfile_mpeg_audio['VBR_encoder_version'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 4, 2)); // VbriVersion
  470. $thisfile_mpeg_audio['VBR_encoder_delay'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 6, 2)); // VbriDelay
  471. $thisfile_mpeg_audio['VBR_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 8, 2)); // VbriQuality
  472. $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 10, 4)); // VbriStreamBytes
  473. $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 14, 4)); // VbriStreamFrames
  474. $thisfile_mpeg_audio['VBR_seek_offsets'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 18, 2)); // VbriTableSize
  475. $thisfile_mpeg_audio['VBR_seek_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 20, 2)); // VbriTableScale
  476. $thisfile_mpeg_audio['VBR_entry_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 22, 2)); // VbriEntryBytes
  477. $thisfile_mpeg_audio['VBR_entry_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 24, 2)); // VbriEntryFrames
  478. $ExpectedNumberOfAudioBytes = $thisfile_mpeg_audio['VBR_bytes'];
  479. $previousbyteoffset = $offset;
  480. for ($i = 0; $i < $thisfile_mpeg_audio['VBR_seek_offsets']; $i++) {
  481. $Fraunhofer_OffsetN = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset, $thisfile_mpeg_audio['VBR_entry_bytes']));
  482. $FraunhoferVBROffset += $thisfile_mpeg_audio['VBR_entry_bytes'];
  483. $thisfile_mpeg_audio['VBR_offsets_relative'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']);
  484. $thisfile_mpeg_audio['VBR_offsets_absolute'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']) + $previousbyteoffset;
  485. $previousbyteoffset += $Fraunhofer_OffsetN;
  486. }
  487. } else {
  488. // Xing VBR header is hardcoded 'Xing' at a offset 0x0D (13), 0x15 (21) or 0x24 (36)
  489. // depending on MPEG layer and number of channels
  490. $VBRidOffset = getid3_mp3::XingVBRidOffset($thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['channelmode']);
  491. $SideInfoData = substr($headerstring, 4 + 2, $VBRidOffset - 4);
  492. if ((substr($headerstring, $VBRidOffset, strlen('Xing')) == 'Xing') || (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Info')) {
  493. // 'Xing' is traditional Xing VBR frame
  494. // 'Info' is LAME-encoded CBR (This was done to avoid CBR files to be recognized as traditional Xing VBR files by some decoders.)
  495. // 'Info' *can* legally be used to specify a VBR file as well, however.
  496. // http://www.multiweb.cz/twoinches/MP3inside.htm
  497. //00..03 = "Xing" or "Info"
  498. //04..07 = Flags:
  499. // 0x01 Frames Flag set if value for number of frames in file is stored
  500. // 0x02 Bytes Flag set if value for filesize in bytes is stored
  501. // 0x04 TOC Flag set if values for TOC are stored
  502. // 0x08 VBR Scale Flag set if values for VBR scale is stored
  503. //08..11 Frames: Number of frames in file (including the first Xing/Info one)
  504. //12..15 Bytes: File length in Bytes
  505. //16..115 TOC (Table of Contents):
  506. // Contains of 100 indexes (one Byte length) for easier lookup in file. Approximately solves problem with moving inside file.
  507. // Each Byte has a value according this formula:
  508. // (TOC[i] / 256) * fileLenInBytes
  509. // 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:
  510. // TOC[(60/240)*100] = TOC[25]
  511. // and corresponding Byte in file is then approximately at:
  512. // (TOC[25]/256) * 5000000
  513. //116..119 VBR Scale
  514. // should be safe to leave this at 'vbr' and let it be overriden to 'cbr' if a CBR preset/mode is used by LAME
  515. // if (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Xing') {
  516. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  517. $thisfile_mpeg_audio['VBR_method'] = 'Xing';
  518. // } else {
  519. // $ScanAsCBR = true;
  520. // $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  521. // }
  522. $thisfile_mpeg_audio['xing_flags_raw'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 4, 4));
  523. $thisfile_mpeg_audio['xing_flags']['frames'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000001);
  524. $thisfile_mpeg_audio['xing_flags']['bytes'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000002);
  525. $thisfile_mpeg_audio['xing_flags']['toc'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000004);
  526. $thisfile_mpeg_audio['xing_flags']['vbr_scale'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000008);
  527. if ($thisfile_mpeg_audio['xing_flags']['frames']) {
  528. $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 8, 4));
  529. //$thisfile_mpeg_audio['VBR_frames']--; // don't count header Xing/Info frame
  530. }
  531. if ($thisfile_mpeg_audio['xing_flags']['bytes']) {
  532. $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 12, 4));
  533. }
  534. //if (($thisfile_mpeg_audio['bitrate'] == 'free') && !empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
  535. if (!empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
  536. $framelengthfloat = $thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames'];
  537. if ($thisfile_mpeg_audio['layer'] == '1') {
  538. // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12
  539. //$info['audio']['bitrate'] = ((($framelengthfloat / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12;
  540. $info['audio']['bitrate'] = ($framelengthfloat / 4) * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 12;
  541. } else {
  542. // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144
  543. //$info['audio']['bitrate'] = (($framelengthfloat - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144;
  544. $info['audio']['bitrate'] = $framelengthfloat * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 144;
  545. }
  546. $thisfile_mpeg_audio['framelength'] = floor($framelengthfloat);
  547. }
  548. if ($thisfile_mpeg_audio['xing_flags']['toc']) {
  549. $LAMEtocData = substr($headerstring, $VBRidOffset + 16, 100);
  550. for ($i = 0; $i < 100; $i++) {
  551. $thisfile_mpeg_audio['toc'][$i] = ord($LAMEtocData{$i});
  552. }
  553. }
  554. if ($thisfile_mpeg_audio['xing_flags']['vbr_scale']) {
  555. $thisfile_mpeg_audio['VBR_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 116, 4));
  556. }
  557. // http://gabriel.mp3-tech.org/mp3infotag.html
  558. if (substr($headerstring, $VBRidOffset + 120, 4) == 'LAME') {
  559. // shortcut
  560. $thisfile_mpeg_audio['LAME'] = array();
  561. $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
  562. $thisfile_mpeg_audio_lame['long_version'] = substr($headerstring, $VBRidOffset + 120, 20);
  563. $thisfile_mpeg_audio_lame['short_version'] = substr($thisfile_mpeg_audio_lame['long_version'], 0, 9);
  564. if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.90') {
  565. // extra 11 chars are not part of version string when LAMEtag present
  566. unset($thisfile_mpeg_audio_lame['long_version']);
  567. // It the LAME tag was only introduced in LAME v3.90
  568. // http://www.hydrogenaudio.org/?act=ST&f=15&t=9933
  569. // Offsets of various bytes in http://gabriel.mp3-tech.org/mp3infotag.html
  570. // are assuming a 'Xing' identifier offset of 0x24, which is the case for
  571. // MPEG-1 non-mono, but not for other combinations
  572. $LAMEtagOffsetContant = $VBRidOffset - 0x24;
  573. // shortcuts
  574. $thisfile_mpeg_audio_lame['RGAD'] = array('track'=>array(), 'album'=>array());
  575. $thisfile_mpeg_audio_lame_RGAD = &$thisfile_mpeg_audio_lame['RGAD'];
  576. $thisfile_mpeg_audio_lame_RGAD_track = &$thisfile_mpeg_audio_lame_RGAD['track'];
  577. $thisfile_mpeg_audio_lame_RGAD_album = &$thisfile_mpeg_audio_lame_RGAD['album'];
  578. $thisfile_mpeg_audio_lame['raw'] = array();
  579. $thisfile_mpeg_audio_lame_raw = &$thisfile_mpeg_audio_lame['raw'];
  580. // byte $9B VBR Quality
  581. // This field is there to indicate a quality level, although the scale was not precised in the original Xing specifications.
  582. // Actually overwrites original Xing bytes
  583. unset($thisfile_mpeg_audio['VBR_scale']);
  584. $thisfile_mpeg_audio_lame['vbr_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0x9B, 1));
  585. // bytes $9C-$A4 Encoder short VersionString
  586. $thisfile_mpeg_audio_lame['short_version'] = substr($headerstring, $LAMEtagOffsetContant + 0x9C, 9);
  587. // byte $A5 Info Tag revision + VBR method
  588. $LAMEtagRevisionVBRmethod = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA5, 1));
  589. $thisfile_mpeg_audio_lame['tag_revision'] = ($LAMEtagRevisionVBRmethod & 0xF0) >> 4;
  590. $thisfile_mpeg_audio_lame_raw['vbr_method'] = $LAMEtagRevisionVBRmethod & 0x0F;
  591. $thisfile_mpeg_audio_lame['vbr_method'] = getid3_mp3::LAMEvbrMethodLookup($thisfile_mpeg_audio_lame_raw['vbr_method']);
  592. $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'
  593. // byte $A6 Lowpass filter value
  594. $thisfile_mpeg_audio_lame['lowpass_frequency'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA6, 1)) * 100;
  595. // bytes $A7-$AE Replay Gain
  596. // http://privatewww.essex.ac.uk/~djmrob/replaygain/rg_data_format.html
  597. // bytes $A7-$AA : 32 bit floating point "Peak signal amplitude"
  598. if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.94b') {
  599. // LAME 3.94a16 and later - 9.23 fixed point
  600. // ie 0x0059E2EE / (2^23) = 5890798 / 8388608 = 0.7022378444671630859375
  601. $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = (float) ((getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4))) / 8388608);
  602. } else {
  603. // LAME 3.94a15 and earlier - 32-bit floating point
  604. // Actually 3.94a16 will fall in here too and be WRONG, but is hard to detect 3.94a16 vs 3.94a15
  605. $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = getid3_lib::LittleEndian2Float(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4));
  606. }
  607. if ($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] == 0) {
  608. unset($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
  609. } else {
  610. $thisfile_mpeg_audio_lame_RGAD['peak_db'] = getid3_lib::RGADamplitude2dB($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
  611. }
  612. $thisfile_mpeg_audio_lame_raw['RGAD_track'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAB, 2));
  613. $thisfile_mpeg_audio_lame_raw['RGAD_album'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAD, 2));
  614. if ($thisfile_mpeg_audio_lame_raw['RGAD_track'] != 0) {
  615. $thisfile_mpeg_audio_lame_RGAD_track['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0xE000) >> 13;
  616. $thisfile_mpeg_audio_lame_RGAD_track['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x1C00) >> 10;
  617. $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x0200) >> 9;
  618. $thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x01FF;
  619. $thisfile_mpeg_audio_lame_RGAD_track['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['name']);
  620. $thisfile_mpeg_audio_lame_RGAD_track['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['originator']);
  621. $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']);
  622. if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
  623. $info['replay_gain']['track']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
  624. }
  625. $info['replay_gain']['track']['originator'] = $thisfile_mpeg_audio_lame_RGAD_track['originator'];
  626. $info['replay_gain']['track']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_track['gain_db'];
  627. } else {
  628. unset($thisfile_mpeg_audio_lame_RGAD['track']);
  629. }
  630. if ($thisfile_mpeg_audio_lame_raw['RGAD_album'] != 0) {
  631. $thisfile_mpeg_audio_lame_RGAD_album['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0xE000) >> 13;
  632. $thisfile_mpeg_audio_lame_RGAD_album['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x1C00) >> 10;
  633. $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x0200) >> 9;
  634. $thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x01FF;
  635. $thisfile_mpeg_audio_lame_RGAD_album['name'] = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['name']);
  636. $thisfile_mpeg_audio_lame_RGAD_album['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['originator']);
  637. $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']);
  638. if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
  639. $info['replay_gain']['album']['peak'] = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
  640. }
  641. $info['replay_gain']['album']['originator'] = $thisfile_mpeg_audio_lame_RGAD_album['originator'];
  642. $info['replay_gain']['album']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_album['gain_db'];
  643. } else {
  644. unset($thisfile_mpeg_audio_lame_RGAD['album']);
  645. }
  646. if (empty($thisfile_mpeg_audio_lame_RGAD)) {
  647. unset($thisfile_mpeg_audio_lame['RGAD']);
  648. }
  649. // byte $AF Encoding flags + ATH Type
  650. $EncodingFlagsATHtype = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAF, 1));
  651. $thisfile_mpeg_audio_lame['encoding_flags']['nspsytune'] = (bool) ($EncodingFlagsATHtype & 0x10);
  652. $thisfile_mpeg_audio_lame['encoding_flags']['nssafejoint'] = (bool) ($EncodingFlagsATHtype & 0x20);
  653. $thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'] = (bool) ($EncodingFlagsATHtype & 0x40);
  654. $thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev'] = (bool) ($EncodingFlagsATHtype & 0x80);
  655. $thisfile_mpeg_audio_lame['ath_type'] = $EncodingFlagsATHtype & 0x0F;
  656. // byte $B0 if ABR {specified bitrate} else {minimal bitrate}
  657. $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB0, 1));
  658. if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 2) { // Average BitRate (ABR)
  659. $thisfile_mpeg_audio_lame['bitrate_abr'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
  660. } elseif ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { // Constant BitRate (CBR)
  661. // ignore
  662. } elseif ($thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] > 0) { // Variable BitRate (VBR) - minimum bitrate
  663. $thisfile_mpeg_audio_lame['bitrate_min'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
  664. }
  665. // bytes $B1-$B3 Encoder delays
  666. $EncoderDelays = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB1, 3));
  667. $thisfile_mpeg_audio_lame['encoder_delay'] = ($EncoderDelays & 0xFFF000) >> 12;
  668. $thisfile_mpeg_audio_lame['end_padding'] = $EncoderDelays & 0x000FFF;
  669. // byte $B4 Misc
  670. $MiscByte = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB4, 1));
  671. $thisfile_mpeg_audio_lame_raw['noise_shaping'] = ($MiscByte & 0x03);
  672. $thisfile_mpeg_audio_lame_raw['stereo_mode'] = ($MiscByte & 0x1C) >> 2;
  673. $thisfile_mpeg_audio_lame_raw['not_optimal_quality'] = ($MiscByte & 0x20) >> 5;
  674. $thisfile_mpeg_audio_lame_raw['source_sample_freq'] = ($MiscByte & 0xC0) >> 6;
  675. $thisfile_mpeg_audio_lame['noise_shaping'] = $thisfile_mpeg_audio_lame_raw['noise_shaping'];
  676. $thisfile_mpeg_audio_lame['stereo_mode'] = getid3_mp3::LAMEmiscStereoModeLookup($thisfile_mpeg_audio_lame_raw['stereo_mode']);
  677. $thisfile_mpeg_audio_lame['not_optimal_quality'] = (bool) $thisfile_mpeg_audio_lame_raw['not_optimal_quality'];
  678. $thisfile_mpeg_audio_lame['source_sample_freq'] = getid3_mp3::LAMEmiscSourceSampleFrequencyLookup($thisfile_mpeg_audio_lame_raw['source_sample_freq']);
  679. // byte $B5 MP3 Gain
  680. $thisfile_mpeg_audio_lame_raw['mp3_gain'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB5, 1), false, true);
  681. $thisfile_mpeg_audio_lame['mp3_gain_db'] = (getid3_lib::RGADamplitude2dB(2) / 4) * $thisfile_mpeg_audio_lame_raw['mp3_gain'];
  682. $thisfile_mpeg_audio_lame['mp3_gain_factor'] = pow(2, ($thisfile_mpeg_audio_lame['mp3_gain_db'] / 6));
  683. // bytes $B6-$B7 Preset and surround info
  684. $PresetSurroundBytes = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB6, 2));
  685. // Reserved = ($PresetSurroundBytes & 0xC000);
  686. $thisfile_mpeg_audio_lame_raw['surround_info'] = ($PresetSurroundBytes & 0x3800);
  687. $thisfile_mpeg_audio_lame['surround_info'] = getid3_mp3::LAMEsurroundInfoLookup($thisfile_mpeg_audio_lame_raw['surround_info']);
  688. $thisfile_mpeg_audio_lame['preset_used_id'] = ($PresetSurroundBytes & 0x07FF);
  689. $thisfile_mpeg_audio_lame['preset_used'] = getid3_mp3::LAMEpresetUsedLookup($thisfile_mpeg_audio_lame);
  690. if (!empty($thisfile_mpeg_audio_lame['preset_used_id']) && empty($thisfile_mpeg_audio_lame['preset_used'])) {
  691. $info['warning'][] = 'Unknown LAME preset used ('.$thisfile_mpeg_audio_lame['preset_used_id'].') - please report to info@getid3.org';
  692. }
  693. if (($thisfile_mpeg_audio_lame['short_version'] == 'LAME3.90.') && !empty($thisfile_mpeg_audio_lame['preset_used_id'])) {
  694. // this may change if 3.90.4 ever comes out
  695. $thisfile_mpeg_audio_lame['short_version'] = 'LAME3.90.3';
  696. }
  697. // bytes $B8-$BB MusicLength
  698. $thisfile_mpeg_audio_lame['audio_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB8, 4));
  699. $ExpectedNumberOfAudioBytes = (($thisfile_mpeg_audio_lame['audio_bytes'] > 0) ? $thisfile_mpeg_audio_lame['audio_bytes'] : $thisfile_mpeg_audio['VBR_bytes']);
  700. // bytes $BC-$BD MusicCRC
  701. $thisfile_mpeg_audio_lame['music_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBC, 2));
  702. // bytes $BE-$BF CRC-16 of Info Tag
  703. $thisfile_mpeg_audio_lame['lame_tag_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBE, 2));
  704. // LAME CBR
  705. if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) {
  706. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  707. $thisfile_mpeg_audio['bitrate'] = getid3_mp3::ClosestStandardMP3Bitrate($thisfile_mpeg_audio['bitrate']);
  708. $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
  709. //if (empty($thisfile_mpeg_audio['bitrate']) || (!empty($thisfile_mpeg_audio_lame['bitrate_min']) && ($thisfile_mpeg_audio_lame['bitrate_min'] != 255))) {
  710. // $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio_lame['bitrate_min'];
  711. //}
  712. }
  713. }
  714. }
  715. } else {
  716. // not Fraunhofer or Xing VBR methods, most likely CBR (but could be VBR with no header)
  717. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  718. if ($recursivesearch) {
  719. $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
  720. if ($this->RecursiveFrameScanning($offset, $nextframetestoffset, true)) {
  721. $recursivesearch = false;
  722. $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
  723. }
  724. if ($thisfile_mpeg_audio['bitrate_mode'] == 'vbr') {
  725. $info['warning'][] = 'VBR file with no VBR header. Bitrate values calculated from actual frame bitrates.';
  726. }
  727. }
  728. }
  729. }
  730. if (($ExpectedNumberOfAudioBytes > 0) && ($ExpectedNumberOfAudioBytes != ($info['avdataend'] - $info['avdataoffset']))) {
  731. if ($ExpectedNumberOfAudioBytes > ($info['avdataend'] - $info['avdataoffset'])) {
  732. if (isset($info['fileformat']) && ($info['fileformat'] == 'riff')) {
  733. // ignore, audio data is broken into chunks so will always be data "missing"
  734. } elseif (($ExpectedNumberOfAudioBytes - ($info['avdataend'] - $info['avdataoffset'])) == 1) {
  735. $info['warning'][] = 'Last byte of data truncated (this is a known bug in Meracl ID3 Tag Writer before v1.3.5)';
  736. } else {
  737. $info['warning'][] = 'Probable truncated file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, only found '.($info['avdataend'] - $info['avdataoffset']).' (short by '.($ExpectedNumberOfAudioBytes - ($info['avdat…

Large files files are truncated, but you can click here to view the full file