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