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