getid3_mp3

The WordPress Core getid3 mp3 class.

Defined (1)

The class is defined in the following location(s).

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