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