function getOnlyMPEGaudioInfoBruteForce($fd, &$ThisFileInfo) { $MPEGaudioHeaderDecodeCache = array(); $MPEGaudioHeaderValidCache = array(); $MPEGaudioHeaderLengthCache = array(); $MPEGaudioVersionLookup = getid3_mp3::MPEGaudioVersionArray(); $MPEGaudioLayerLookup = getid3_mp3::MPEGaudioLayerArray(); $MPEGaudioBitrateLookup = getid3_mp3::MPEGaudioBitrateArray(); $MPEGaudioFrequencyLookup = getid3_mp3::MPEGaudioFrequencyArray(); $MPEGaudioChannelModeLookup = getid3_mp3::MPEGaudioChannelModeArray(); $MPEGaudioModeExtensionLookup = getid3_mp3::MPEGaudioModeExtensionArray(); $MPEGaudioEmphasisLookup = getid3_mp3::MPEGaudioEmphasisArray(); $LongMPEGversionLookup = array(); $LongMPEGlayerLookup = array(); $LongMPEGbitrateLookup = array(); $LongMPEGpaddingLookup = array(); $LongMPEGfrequencyLookup = array(); $Distribution['bitrate'] = array(); $Distribution['frequency'] = array(); $Distribution['layer'] = array(); $Distribution['version'] = array(); $Distribution['padding'] = array(); fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET); $max_frames_scan = 5000; $frames_scanned = 0; $previousvalidframe = $ThisFileInfo['avdataoffset']; while (ftell($fd) < $ThisFileInfo['avdataend']) { set_time_limit(30); $head4 = fread($fd, 4); if (strlen($head4) < 4) { break; } if ($head4[0] != "ÿ") { for ($i = 1; $i < 4; $i++) { if ($head4[$i] == "ÿ") { fseek($fd, $i - 4, SEEK_CUR); continue 2; } } continue; } if (!isset($MPEGaudioHeaderDecodeCache[$head4])) { $MPEGaudioHeaderDecodeCache[$head4] = getid3_mp3::MPEGaudioHeaderDecode($head4); } if (!isset($MPEGaudioHeaderValidCache[$head4])) { $MPEGaudioHeaderValidCache[$head4] = getid3_mp3::MPEGaudioHeaderValid($MPEGaudioHeaderDecodeCache[$head4], false, false); } if ($MPEGaudioHeaderValidCache[$head4]) { if (!isset($MPEGaudioHeaderLengthCache[$head4])) { $LongMPEGversionLookup[$head4] = $MPEGaudioVersionLookup[$MPEGaudioHeaderDecodeCache[$head4]['version']]; $LongMPEGlayerLookup[$head4] = $MPEGaudioLayerLookup[$MPEGaudioHeaderDecodeCache[$head4]['layer']]; $LongMPEGbitrateLookup[$head4] = $MPEGaudioBitrateLookup[$LongMPEGversionLookup[$head4]][$LongMPEGlayerLookup[$head4]][$MPEGaudioHeaderDecodeCache[$head4]['bitrate']]; $LongMPEGpaddingLookup[$head4] = (bool) $MPEGaudioHeaderDecodeCache[$head4]['padding']; $LongMPEGfrequencyLookup[$head4] = $MPEGaudioFrequencyLookup[$LongMPEGversionLookup[$head4]][$MPEGaudioHeaderDecodeCache[$head4]['sample_rate']]; $MPEGaudioHeaderLengthCache[$head4] = getid3_mp3::MPEGaudioFrameLength($LongMPEGbitrateLookup[$head4], $LongMPEGversionLookup[$head4], $LongMPEGlayerLookup[$head4], $LongMPEGpaddingLookup[$head4], $LongMPEGfrequencyLookup[$head4]); } if ($MPEGaudioHeaderLengthCache[$head4] > 4) { $WhereWeWere = ftell($fd); fseek($fd, $MPEGaudioHeaderLengthCache[$head4] - 4, SEEK_CUR); $next4 = fread($fd, 4); if ($next4[0] == "ÿ") { if (!isset($MPEGaudioHeaderDecodeCache[$next4])) { $MPEGaudioHeaderDecodeCache[$next4] = getid3_mp3::MPEGaudioHeaderDecode($next4); } if (!isset($MPEGaudioHeaderValidCache[$next4])) { $MPEGaudioHeaderValidCache[$next4] = getid3_mp3::MPEGaudioHeaderValid($MPEGaudioHeaderDecodeCache[$next4], false, false); } if ($MPEGaudioHeaderValidCache[$next4]) { fseek($fd, -4, SEEK_CUR); @$Distribution['bitrate'][$LongMPEGbitrateLookup[$head4]]++; @$Distribution['layer'][$LongMPEGlayerLookup[$head4]]++; @$Distribution['version'][$LongMPEGversionLookup[$head4]]++; @$Distribution['padding'][intval($LongMPEGpaddingLookup[$head4])]++; @$Distribution['frequency'][$LongMPEGfrequencyLookup[$head4]]++; if ($max_frames_scan && ++$frames_scanned >= $max_frames_scan) { $pct_data_scanned = (ftell($fd) - $ThisFileInfo['avdataoffset']) / ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']); $ThisFileInfo['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.'; foreach ($Distribution as $key1 => $value1) { foreach ($value1 as $key2 => $value2) { $Distribution[$key1][$key2] = round($value2 / $pct_data_scanned); } } break; } continue; } } unset($next4); fseek($fd, $WhereWeWere - 3, SEEK_SET); } } } foreach ($Distribution as $key => $value) { ksort($Distribution[$key], SORT_NUMERIC); } ksort($Distribution['version'], SORT_STRING); $ThisFileInfo['mpeg']['audio']['bitrate_distribution'] = $Distribution['bitrate']; $ThisFileInfo['mpeg']['audio']['frequency_distribution'] = $Distribution['frequency']; $ThisFileInfo['mpeg']['audio']['layer_distribution'] = $Distribution['layer']; $ThisFileInfo['mpeg']['audio']['version_distribution'] = $Distribution['version']; $ThisFileInfo['mpeg']['audio']['padding_distribution'] = $Distribution['padding']; if (count($Distribution['version']) > 1) { $ThisFileInfo['error'][] = 'Corrupt file - more than one MPEG version detected'; } if (count($Distribution['layer']) > 1) { $ThisFileInfo['error'][] = 'Corrupt file - more than one MPEG layer detected'; } if (count($Distribution['frequency']) > 1) { $ThisFileInfo['error'][] = 'Corrupt file - more than one MPEG sample rate detected'; } $bittotal = 0; foreach ($Distribution['bitrate'] as $bitratevalue => $bitratecount) { if ($bitratevalue != 'free') { $bittotal += $bitratevalue * $bitratecount; } } $ThisFileInfo['mpeg']['audio']['frame_count'] = array_sum($Distribution['bitrate']); if ($ThisFileInfo['mpeg']['audio']['frame_count'] == 0) { $ThisFileInfo['error'][] = 'no MPEG audio frames found'; return false; } $ThisFileInfo['mpeg']['audio']['bitrate'] = $bittotal / $ThisFileInfo['mpeg']['audio']['frame_count']; $ThisFileInfo['mpeg']['audio']['bitrate_mode'] = count($Distribution['bitrate']) > 0 ? 'vbr' : 'cbr'; $ThisFileInfo['mpeg']['audio']['sample_rate'] = getid3_lib::array_max($Distribution['frequency'], true); $ThisFileInfo['audio']['bitrate'] = $ThisFileInfo['mpeg']['audio']['bitrate']; $ThisFileInfo['audio']['bitrate_mode'] = $ThisFileInfo['mpeg']['audio']['bitrate_mode']; $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['mpeg']['audio']['sample_rate']; $ThisFileInfo['audio']['dataformat'] = 'mp' . getid3_lib::array_max($Distribution['layer'], true); $ThisFileInfo['fileformat'] = $ThisFileInfo['audio']['dataformat']; return true; }
public function decodeMPEGaudioHeader($fd, $offset, &$info, $recursive_search = true, $scan_as_cbr = false, $fast_mpeg_header_scan = false) { static $mpeg_audio_version_lookup; static $mpeg_audio_layer_lookup; static $mpeg_audio_bitrate_lookup; static $mpeg_audio_frequency_lookup; static $mpeg_audio_channel_mode_lookup; static $mpeg_audio_mode_extension_lookup; static $mpeg_audio_emphasis_lookup; if (empty($mpeg_audio_version_lookup)) { $mpeg_audio_version_lookup = getid3_mp3::MPEGaudioVersionarray(); $mpeg_audio_layer_lookup = getid3_mp3::MPEGaudioLayerarray(); $mpeg_audio_bitrate_lookup = getid3_mp3::MPEGaudioBitratearray(); $mpeg_audio_frequency_lookup = getid3_mp3::MPEGaudioFrequencyarray(); $mpeg_audio_channel_mode_lookup = getid3_mp3::MPEGaudioChannelModearray(); $mpeg_audio_mode_extension_lookup = getid3_mp3::MPEGaudioModeExtensionarray(); $mpeg_audio_emphasis_lookup = getid3_mp3::MPEGaudioEmphasisarray(); } if ($offset >= $info['avdataend']) { // non-fatal error: 'end of file encounter looking for MPEG synch' return; } fseek($fd, $offset, SEEK_SET); $header_string = fread($fd, 226); // LAME header at offset 36 + 190 bytes of Xing/LAME data // MP3 audio frame structure: // $aa $aa $aa $aa [$bb $bb] $cc... // where $aa..$aa is the four-byte mpeg-audio header (below) // $bb $bb is the optional 2-byte CRC // and $cc... is the audio data $head4 = substr($header_string, 0, 4); if (isset($mpeg_audio_header_decode_cache[$head4])) { $mpeg_header_raw_array = $mpeg_audio_header_decode_cache[$head4]; } else { $mpeg_header_raw_array = getid3_mp3::MPEGaudioHeaderDecode($head4); $mpeg_audio_header_decode_cache[$head4] = $mpeg_header_raw_array; } // Not in cache if (!isset($mpeg_audio_header_valid_cache[$head4])) { $mpeg_audio_header_valid_cache[$head4] = getid3_mp3::MPEGaudioHeaderValid($mpeg_header_raw_array, false, false); } // shortcut if (!isset($info['mpeg']['audio'])) { $info['mpeg']['audio'] = array(); } $thisfile_mpeg_audio =& $info['mpeg']['audio']; if ($mpeg_audio_header_valid_cache[$head4]) { $thisfile_mpeg_audio['raw'] = $mpeg_header_raw_array; } else { // non-fatal error: Invalid MPEG audio header at offset $offset return; } if (!$fast_mpeg_header_scan) { $thisfile_mpeg_audio['version'] = $mpeg_audio_version_lookup[$thisfile_mpeg_audio['raw']['version']]; $thisfile_mpeg_audio['layer'] = $mpeg_audio_layer_lookup[$thisfile_mpeg_audio['raw']['layer']]; $thisfile_mpeg_audio['channelmode'] = $mpeg_audio_channel_mode_lookup[$thisfile_mpeg_audio['raw']['channelmode']]; $thisfile_mpeg_audio['channels'] = $thisfile_mpeg_audio['channelmode'] == 'mono' ? 1 : 2; $thisfile_mpeg_audio['sample_rate'] = $mpeg_audio_frequency_lookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['raw']['sample_rate']]; $thisfile_mpeg_audio['protection'] = !$thisfile_mpeg_audio['raw']['protection']; $thisfile_mpeg_audio['private'] = (bool) $thisfile_mpeg_audio['raw']['private']; $thisfile_mpeg_audio['modeextension'] = $mpeg_audio_mode_extension_lookup[$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['modeextension']]; $thisfile_mpeg_audio['copyright'] = (bool) $thisfile_mpeg_audio['raw']['copyright']; $thisfile_mpeg_audio['original'] = (bool) $thisfile_mpeg_audio['raw']['original']; $thisfile_mpeg_audio['emphasis'] = $mpeg_audio_emphasis_lookup[$thisfile_mpeg_audio['raw']['emphasis']]; $info['audio']['channels'] = $thisfile_mpeg_audio['channels']; $info['audio']['sample_rate'] = $thisfile_mpeg_audio['sample_rate']; if ($thisfile_mpeg_audio['protection']) { $thisfile_mpeg_audio['crc'] = getid3_lib::BigEndian2Int(substr($header_string, 4, 2)); } } if ($thisfile_mpeg_audio['raw']['bitrate'] == 15) { // http://www.hydrogenaudio.org/?act=ST&f=16&t=9682&st=0 $this->getid3->warning('Invalid bitrate index (15), this is a known bug in free-format MP3s encoded by LAME v3.90 - 3.93.1'); $thisfile_mpeg_audio['raw']['bitrate'] = 0; } $thisfile_mpeg_audio['padding'] = (bool) $thisfile_mpeg_audio['raw']['padding']; $thisfile_mpeg_audio['bitrate'] = $mpeg_audio_bitrate_lookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['bitrate']]; if ($thisfile_mpeg_audio['bitrate'] == 'free' && $offset == $info['avdataoffset']) { // only skip multiple frame check if free-format bitstream found at beginning of file // otherwise is quite possibly simply corrupted data $recursive_search = false; } // For Layer 2 there are some combinations of bitrate and mode which are not allowed. if (!$fast_mpeg_header_scan && $thisfile_mpeg_audio['layer'] == '2') { $info['audio']['dataformat'] = 'mp2'; switch ($thisfile_mpeg_audio['channelmode']) { case 'mono': if ($thisfile_mpeg_audio['bitrate'] == 'free' || $thisfile_mpeg_audio['bitrate'] <= 192000) { // these are ok } else { // non-fatal error: bitrate not allowed in Layer 2/mono return; } break; case 'stereo': case 'joint stereo': case 'dual channel': if ($thisfile_mpeg_audio['bitrate'] == 'free' || $thisfile_mpeg_audio['bitrate'] == 64000 || $thisfile_mpeg_audio['bitrate'] >= 96000) { // these are ok } else { // non-fatal error: bitrate not allowed in Layer 2/stereo/joint stereo/dual channel return; } break; } } if ($info['audio']['sample_rate'] > 0) { $thisfile_mpeg_audio['framelength'] = getid3_mp3::MPEGaudioFrameLength($thisfile_mpeg_audio['bitrate'], $thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['layer'], (int) $thisfile_mpeg_audio['padding'], $info['audio']['sample_rate']); } $next_frame_test_offset = $offset + 1; if ($thisfile_mpeg_audio['bitrate'] != 'free') { $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate']; if (isset($thisfile_mpeg_audio['framelength'])) { $next_frame_test_offset = $offset + $thisfile_mpeg_audio['framelength']; } else { // non-fatal error: Frame at offset('.$offset.') is has an invalid frame length. return; } } $expected_number_of_audio_bytes = 0; //////////////////////////////////////////////////////////////////////////////////// // Variable-bitrate headers if (substr($header_string, 4 + 32, 4) == 'VBRI') { // Fraunhofer VBR header is hardcoded 'VBRI' at offset 0x24 (36) // specs taken from http://minnie.tuhs.org/pipermail/mp3encoder/2001-January/001800.html $thisfile_mpeg_audio['bitrate_mode'] = 'vbr'; $thisfile_mpeg_audio['VBR_method'] = 'Fraunhofer'; $info['audio']['codec'] = 'Fraunhofer'; $side_info_data = substr($header_string, 4 + 2, 32); $fraunhofer_vbr_offset = 36; $thisfile_mpeg_audio['VBR_encoder_version'] = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 4, 2)); // VbriVersion $thisfile_mpeg_audio['VBR_encoder_delay'] = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 6, 2)); // VbriDelay $thisfile_mpeg_audio['VBR_quality'] = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 8, 2)); // VbriQuality $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 10, 4)); // VbriStreamBytes $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 14, 4)); // VbriStreamFrames $thisfile_mpeg_audio['VBR_seek_offsets'] = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 18, 2)); // VbriTableSize $thisfile_mpeg_audio['VBR_seek_scale'] = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 20, 2)); // VbriTableScale $thisfile_mpeg_audio['VBR_entry_bytes'] = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 22, 2)); // VbriEntryBytes $thisfile_mpeg_audio['VBR_entry_frames'] = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 24, 2)); // VbriEntryFrames $expected_number_of_audio_bytes = $thisfile_mpeg_audio['VBR_bytes']; $previous_byte_offset = $offset; for ($i = 0; $i < $thisfile_mpeg_audio['VBR_seek_offsets']; $i++) { $fraunhofer_offset_n = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset, $thisfile_mpeg_audio['VBR_entry_bytes'])); $fraunhofer_vbr_offset += $thisfile_mpeg_audio['VBR_entry_bytes']; $thisfile_mpeg_audio['VBR_offsets_relative'][$i] = $fraunhofer_offset_n * $thisfile_mpeg_audio['VBR_seek_scale']; $thisfile_mpeg_audio['VBR_offsets_absolute'][$i] = $fraunhofer_offset_n * $thisfile_mpeg_audio['VBR_seek_scale'] + $previous_byte_offset; $previous_byte_offset += $fraunhofer_offset_n; } } else { // Xing VBR header is hardcoded 'Xing' at a offset 0x0D (13), 0x15 (21) or 0x24 (36) // depending on MPEG layer and number of channels $vbr_id_offset = getid3_mp3::XingVBRidOffset($thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['channelmode']); $side_info_data = substr($header_string, 4 + 2, $vbr_id_offset - 4); if (substr($header_string, $vbr_id_offset, strlen('Xing')) == 'Xing' || substr($header_string, $vbr_id_offset, strlen('Info')) == 'Info') { // 'Xing' is traditional Xing VBR frame // 'Info' is LAME-encoded CBR (This was done to avoid CBR files to be recognized as traditional Xing VBR files by some decoders.) // 'Info' *can* legally be used to specify a VBR file as well, however. // http://www.multiweb.cz/twoinches/MP3inside.htm //00..03 = "Xing" or "Info" //04..07 = Flags: // 0x01 Frames Flag set if value for number of frames in file is stored // 0x02 Bytes Flag set if value for filesize in bytes is stored // 0x04 TOC Flag set if values for TOC are stored // 0x08 VBR Scale Flag set if values for VBR scale is stored //08..11 Frames: Number of frames in file (including the first Xing/Info one) //12..15 Bytes: File length in Bytes //16..115 TOC (Table of Contents): // Contains of 100 indexes (one Byte length) for easier lookup in file. Approximately solves problem with moving inside file. // Each Byte has a value according this formula: // (TOC[i] / 256) * fileLenInBytes // 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: // TOC[(60/240)*100] = TOC[25] // and corresponding Byte in file is then approximately at: // (TOC[25]/256) * 5000000 //116..119 VBR Scale // should be safe to leave this at 'vbr' and let it be overriden to 'cbr' if a CBR preset/mode is used by LAME $thisfile_mpeg_audio['bitrate_mode'] = 'vbr'; $thisfile_mpeg_audio['VBR_method'] = 'Xing'; $thisfile_mpeg_audio['xing_flags_raw'] = getid3_lib::BigEndian2Int(substr($header_string, $vbr_id_offset + 4, 4)); $thisfile_mpeg_audio['xing_flags']['frames'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x1); $thisfile_mpeg_audio['xing_flags']['bytes'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x2); $thisfile_mpeg_audio['xing_flags']['toc'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x4); $thisfile_mpeg_audio['xing_flags']['vbr_scale'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x8); if ($thisfile_mpeg_audio['xing_flags']['frames']) { $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($header_string, $vbr_id_offset + 8, 4)); } if ($thisfile_mpeg_audio['xing_flags']['bytes']) { $thisfile_mpeg_audio['VBR_bytes'] = getid3_lib::BigEndian2Int(substr($header_string, $vbr_id_offset + 12, 4)); } if (!empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) { $frame_lengthfloat = $thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames']; if ($thisfile_mpeg_audio['layer'] == '1') { // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12 $info['audio']['bitrate'] = $frame_lengthfloat / 4 * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 12; } else { // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144 $info['audio']['bitrate'] = $frame_lengthfloat * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 144; } $thisfile_mpeg_audio['framelength'] = floor($frame_lengthfloat); } if ($thisfile_mpeg_audio['xing_flags']['toc']) { $lame_toc_data = substr($header_string, $vbr_id_offset + 16, 100); for ($i = 0; $i < 100; $i++) { $thisfile_mpeg_audio['toc'][$i] = ord($lame_toc_data[$i]); } } if ($thisfile_mpeg_audio['xing_flags']['vbr_scale']) { $thisfile_mpeg_audio['VBR_scale'] = getid3_lib::BigEndian2Int(substr($header_string, $vbr_id_offset + 116, 4)); } // http://gabriel.mp3-tech.org/mp3infotag.html if (substr($header_string, $vbr_id_offset + 120, 4) == 'LAME') { // shortcut $thisfile_mpeg_audio['LAME'] = array(); $thisfile_mpeg_audio_lame =& $thisfile_mpeg_audio['LAME']; $thisfile_mpeg_audio_lame['long_version'] = substr($header_string, $vbr_id_offset + 120, 20); $thisfile_mpeg_audio_lame['short_version'] = substr($thisfile_mpeg_audio_lame['long_version'], 0, 9); if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.90') { // extra 11 chars are not part of version string when LAMEtag present unset($thisfile_mpeg_audio_lame['long_version']); // It the LAME tag was only introduced in LAME v3.90 // http://www.hydrogenaudio.org/?act=ST&f=15&t=9933 // Offsets of various bytes in http://gabriel.mp3-tech.org/mp3infotag.html // are assuming a 'Xing' identifier offset of 0x24, which is the case for // MPEG-1 non-mono, but not for other combinations $lame_tag_offset_contant = $vbr_id_offset - 0x24; // shortcuts $thisfile_mpeg_audio_lame['RGAD'] = array('track' => array(), 'album' => array()); $thisfile_mpeg_audio_lame_rgad =& $thisfile_mpeg_audio_lame['RGAD']; $thisfile_mpeg_audio_lame_rgad_track =& $thisfile_mpeg_audio_lame_rgad['track']; $thisfile_mpeg_audio_lame_rgad_album =& $thisfile_mpeg_audio_lame_rgad['album']; $thisfile_mpeg_audio_lame['raw'] = array(); $thisfile_mpeg_audio_lame_raw =& $thisfile_mpeg_audio_lame['raw']; // byte $9B VBR Quality // This field is there to indicate a quality level, although the scale was not precised in the original Xing specifications. // Actually overwrites original Xing bytes unset($thisfile_mpeg_audio['VBR_scale']); $thisfile_mpeg_audio_lame['vbr_quality'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0x9b, 1)); // bytes $9C-$A4 Encoder short VersionString $thisfile_mpeg_audio_lame['short_version'] = substr($header_string, $lame_tag_offset_contant + 0x9c, 9); // byte $A5 Info Tag revision + VBR method $lame_tagRevisionVBRmethod = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xa5, 1)); $thisfile_mpeg_audio_lame['tag_revision'] = ($lame_tagRevisionVBRmethod & 0xf0) >> 4; $thisfile_mpeg_audio_lame_raw['vbr_method'] = $lame_tagRevisionVBRmethod & 0xf; $thisfile_mpeg_audio_lame['vbr_method'] = getid3_mp3::LAMEvbrMethodLookup($thisfile_mpeg_audio_lame_raw['vbr_method']); $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' // byte $A6 Lowpass filter value $thisfile_mpeg_audio_lame['lowpass_frequency'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xa6, 1)) * 100; // bytes $A7-$AE Replay Gain // http://privatewww.essex.ac.uk/~djmrob/replaygain/rg_data_format.html // bytes $A7-$AA : 32 bit floating point "Peak signal amplitude" if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.94b') { // LAME 3.94a16 and later - 9.23 fixed point // ie 0x0059E2EE / (2^23) = 5890798 / 8388608 = 0.7022378444671630859375 $thisfile_mpeg_audio_lame_rgad['peak_amplitude'] = (double) (getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xa7, 4)) / 8388608); } else { // LAME 3.94a15 and earlier - 32-bit floating point // Actually 3.94a16 will fall in here too and be WRONG, but is hard to detect 3.94a16 vs 3.94a15 $thisfile_mpeg_audio_lame_rgad['peak_amplitude'] = getid3_lib::LittleEndian2Float(substr($header_string, $lame_tag_offset_contant + 0xa7, 4)); } if ($thisfile_mpeg_audio_lame_rgad['peak_amplitude'] == 0) { unset($thisfile_mpeg_audio_lame_rgad['peak_amplitude']); } else { $thisfile_mpeg_audio_lame_rgad['peak_db'] = 20 * log10($thisfile_mpeg_audio_lame_rgad['peak_amplitude']); } $thisfile_mpeg_audio_lame_raw['RGAD_track'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xab, 2)); $thisfile_mpeg_audio_lame_raw['RGAD_album'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xad, 2)); if ($thisfile_mpeg_audio_lame_raw['RGAD_track'] != 0) { $thisfile_mpeg_audio_lame_rgad_track['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0xe000) >> 13; $thisfile_mpeg_audio_lame_rgad_track['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x1c00) >> 10; $thisfile_mpeg_audio_lame_rgad_track['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x200) >> 9; $thisfile_mpeg_audio_lame_rgad_track['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x1ff; $thisfile_mpeg_audio_lame_rgad_track['name'] = getid3_lib_replaygain::NameLookup($thisfile_mpeg_audio_lame_rgad_track['raw']['name']); $thisfile_mpeg_audio_lame_rgad_track['originator'] = getid3_lib_replaygain::OriginatorLookup($thisfile_mpeg_audio_lame_rgad_track['raw']['originator']); $thisfile_mpeg_audio_lame_rgad_track['gain_db'] = getid3_lib_replaygain::AdjustmentLookup($thisfile_mpeg_audio_lame_rgad_track['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_rgad_track['raw']['sign_bit']); if (!empty($thisfile_mpeg_audio_lame_rgad['peak_amplitude'])) { $info['replay_gain']['track']['peak'] = $thisfile_mpeg_audio_lame_rgad['peak_amplitude']; } $info['replay_gain']['track']['originator'] = $thisfile_mpeg_audio_lame_rgad_track['originator']; $info['replay_gain']['track']['adjustment'] = $thisfile_mpeg_audio_lame_rgad_track['gain_db']; } else { unset($thisfile_mpeg_audio_lame_rgad['track']); } if ($thisfile_mpeg_audio_lame_raw['RGAD_album'] != 0) { $thisfile_mpeg_audio_lame_rgad_album['raw']['name'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0xe000) >> 13; $thisfile_mpeg_audio_lame_rgad_album['raw']['originator'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x1c00) >> 10; $thisfile_mpeg_audio_lame_rgad_album['raw']['sign_bit'] = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x200) >> 9; $thisfile_mpeg_audio_lame_rgad_album['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x1ff; $thisfile_mpeg_audio_lame_rgad_album['name'] = getid3_lib_replaygain::NameLookup($thisfile_mpeg_audio_lame_rgad_album['raw']['name']); $thisfile_mpeg_audio_lame_rgad_album['originator'] = getid3_lib_replaygain::OriginatorLookup($thisfile_mpeg_audio_lame_rgad_album['raw']['originator']); $thisfile_mpeg_audio_lame_rgad_album['gain_db'] = getid3_lib_replaygain::AdjustmentLookup($thisfile_mpeg_audio_lame_rgad_album['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_rgad_album['raw']['sign_bit']); if (!empty($thisfile_mpeg_audio_lame_rgad['peak_amplitude'])) { $info['replay_gain']['album']['peak'] = $thisfile_mpeg_audio_lame_rgad['peak_amplitude']; } $info['replay_gain']['album']['originator'] = $thisfile_mpeg_audio_lame_rgad_album['originator']; $info['replay_gain']['album']['adjustment'] = $thisfile_mpeg_audio_lame_rgad_album['gain_db']; } else { unset($thisfile_mpeg_audio_lame_rgad['album']); } if (empty($thisfile_mpeg_audio_lame_rgad)) { unset($thisfile_mpeg_audio_lame['RGAD']); } // byte $AF Encoding flags + ATH Type $encoding_flags_ath_type = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xaf, 1)); $thisfile_mpeg_audio_lame['encoding_flags']['nspsytune'] = (bool) ($encoding_flags_ath_type & 0x10); $thisfile_mpeg_audio_lame['encoding_flags']['nssafejoint'] = (bool) ($encoding_flags_ath_type & 0x20); $thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'] = (bool) ($encoding_flags_ath_type & 0x40); $thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev'] = (bool) ($encoding_flags_ath_type & 0x80); $thisfile_mpeg_audio_lame['ath_type'] = $encoding_flags_ath_type & 0xf; // byte $B0 if ABR {specified bitrate} else {minimal bitrate} $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xb0, 1)); if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 2) { // Average BitRate (ABR) $thisfile_mpeg_audio_lame['bitrate_abr'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']; } elseif ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { // Constant BitRate (CBR) // ignore } elseif ($thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] > 0) { // Variable BitRate (VBR) - minimum bitrate $thisfile_mpeg_audio_lame['bitrate_min'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']; } // bytes $B1-$B3 Encoder delays $encoder_delays = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xb1, 3)); $thisfile_mpeg_audio_lame['encoder_delay'] = ($encoder_delays & 0xfff000) >> 12; $thisfile_mpeg_audio_lame['end_padding'] = $encoder_delays & 0xfff; // byte $B4 Misc $misc_byte = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xb4, 1)); $thisfile_mpeg_audio_lame_raw['noise_shaping'] = $misc_byte & 0x3; $thisfile_mpeg_audio_lame_raw['stereo_mode'] = ($misc_byte & 0x1c) >> 2; $thisfile_mpeg_audio_lame_raw['not_optimal_quality'] = ($misc_byte & 0x20) >> 5; $thisfile_mpeg_audio_lame_raw['source_sample_freq'] = ($misc_byte & 0xc0) >> 6; $thisfile_mpeg_audio_lame['noise_shaping'] = $thisfile_mpeg_audio_lame_raw['noise_shaping']; $thisfile_mpeg_audio_lame['stereo_mode'] = getid3_mp3::LAMEmiscStereoModeLookup($thisfile_mpeg_audio_lame_raw['stereo_mode']); $thisfile_mpeg_audio_lame['not_optimal_quality'] = (bool) $thisfile_mpeg_audio_lame_raw['not_optimal_quality']; $thisfile_mpeg_audio_lame['source_sample_freq'] = getid3_mp3::LAMEmiscSourceSampleFrequencyLookup($thisfile_mpeg_audio_lame_raw['source_sample_freq']); // byte $B5 MP3 Gain $thisfile_mpeg_audio_lame_raw['mp3_gain'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xb5, 1), false, true); $thisfile_mpeg_audio_lame['mp3_gain_db'] = 20 * log10(2) / 4 * $thisfile_mpeg_audio_lame_raw['mp3_gain']; $thisfile_mpeg_audio_lame['mp3_gain_factor'] = pow(2, $thisfile_mpeg_audio_lame['mp3_gain_db'] / 6); // bytes $B6-$B7 Preset and surround info $PresetSurroundBytes = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xb6, 2)); // Reserved = ($PresetSurroundBytes & 0xC000); $thisfile_mpeg_audio_lame_raw['surround_info'] = $PresetSurroundBytes & 0x3800; $thisfile_mpeg_audio_lame['surround_info'] = getid3_mp3::LAMEsurroundInfoLookup($thisfile_mpeg_audio_lame_raw['surround_info']); $thisfile_mpeg_audio_lame['preset_used_id'] = $PresetSurroundBytes & 0x7ff; $thisfile_mpeg_audio_lame['preset_used'] = getid3_mp3::LAMEpresetUsedLookup($thisfile_mpeg_audio_lame); if (!empty($thisfile_mpeg_audio_lame['preset_used_id']) && empty($thisfile_mpeg_audio_lame['preset_used'])) { $this->getid3->warning('Unknown LAME preset used (' . $thisfile_mpeg_audio_lame['preset_used_id'] . ') - please report to info@getid3.org'); } if ($thisfile_mpeg_audio_lame['short_version'] == 'LAME3.90.' && !empty($thisfile_mpeg_audio_lame['preset_used_id'])) { // this may change if 3.90.4 ever comes out $thisfile_mpeg_audio_lame['short_version'] = 'LAME3.90.3'; } // bytes $B8-$BB MusicLength $thisfile_mpeg_audio_lame['audio_bytes'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xb8, 4)); $expected_number_of_audio_bytes = $thisfile_mpeg_audio_lame['audio_bytes'] > 0 ? $thisfile_mpeg_audio_lame['audio_bytes'] : $thisfile_mpeg_audio['VBR_bytes']; // bytes $BC-$BD MusicCRC $thisfile_mpeg_audio_lame['music_crc'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xbc, 2)); // bytes $BE-$BF CRC-16 of Info Tag $thisfile_mpeg_audio_lame['lame_tag_crc'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xbe, 2)); // LAME CBR if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { $thisfile_mpeg_audio['bitrate_mode'] = 'cbr'; $thisfile_mpeg_audio['bitrate'] = getid3_mp3::ClosestStandardMP3Bitrate($thisfile_mpeg_audio['bitrate']); $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate']; } } } } else { // not Fraunhofer or Xing VBR methods, most likely CBR (but could be VBR with no header) $thisfile_mpeg_audio['bitrate_mode'] = 'cbr'; if ($recursive_search) { $thisfile_mpeg_audio['bitrate_mode'] = 'vbr'; if (getid3_mp3::RecursiveFrameScanning($fd, $info, $offset, $next_frame_test_offset, true)) { $recursive_search = false; $thisfile_mpeg_audio['bitrate_mode'] = 'cbr'; } if ($thisfile_mpeg_audio['bitrate_mode'] == 'vbr') { $this->getid3->warning('VBR file with no VBR header. Bitrate values calculated from actual frame bitrates.'); } } } } if ($expected_number_of_audio_bytes > 0 && $expected_number_of_audio_bytes != $info['avdataend'] - $info['avdataoffset']) { if ($expected_number_of_audio_bytes > $info['avdataend'] - $info['avdataoffset']) { if ($expected_number_of_audio_bytes - ($info['avdataend'] - $info['avdataoffset']) == 1) { $this->getid3->warning('Last byte of data truncated (this is a known bug in Meracl ID3 Tag Writer before v1.3.5)'); } else { $this->getid3->warning('Probable truncated file: expecting ' . $expected_number_of_audio_bytes . ' bytes of audio data, only found ' . ($info['avdataend'] - $info['avdataoffset']) . ' (short by ' . ($expected_number_of_audio_bytes - ($info['avdataend'] - $info['avdataoffset'])) . ' bytes)'); } } else { if ($info['avdataend'] - $info['avdataoffset'] - $expected_number_of_audio_bytes == 1) { $info['avdataend']--; } else { $this->getid3->warning('Too much data in file: expecting ' . $expected_number_of_audio_bytes . ' bytes of audio data, found ' . ($info['avdataend'] - $info['avdataoffset']) . ' (' . ($info['avdataend'] - $info['avdataoffset'] - $expected_number_of_audio_bytes) . ' bytes too many)'); } } } if ($thisfile_mpeg_audio['bitrate'] == 'free' && empty($info['audio']['bitrate'])) { if ($offset == $info['avdataoffset'] && empty($thisfile_mpeg_audio['VBR_frames'])) { $frame_byte_length = getid3_mp3::FreeFormatFrameLength($fd, $offset, $info, true); if ($frame_byte_length > 0) { $thisfile_mpeg_audio['framelength'] = $frame_byte_length; if ($thisfile_mpeg_audio['layer'] == '1') { // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12 $info['audio']['bitrate'] = ($frame_byte_length / 4 - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate'] / 12; } else { // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144 $info['audio']['bitrate'] = ($frame_byte_length - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate'] / 144; } } else { // non-fatal error: Error calculating frame length of free-format MP3 without Xing/LAME header. return; } } } if (@$thisfile_mpeg_audio['VBR_frames']) { switch ($thisfile_mpeg_audio['bitrate_mode']) { case 'vbr': case 'abr': if ($thisfile_mpeg_audio['version'] == '1' && $thisfile_mpeg_audio['layer'] == 1) { $thisfile_mpeg_audio['VBR_bitrate'] = @$thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames'] * 8 * ($info['audio']['sample_rate'] / 384); } elseif (($thisfile_mpeg_audio['version'] == '2' || $thisfile_mpeg_audio['version'] == '2.5') && $thisfile_mpeg_audio['layer'] == 3) { $thisfile_mpeg_audio['VBR_bitrate'] = @$thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames'] * 8 * ($info['audio']['sample_rate'] / 576); } else { $thisfile_mpeg_audio['VBR_bitrate'] = @$thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames'] * 8 * ($info['audio']['sample_rate'] / 1152); } if ($thisfile_mpeg_audio['VBR_bitrate'] > 0) { $info['audio']['bitrate'] = $thisfile_mpeg_audio['VBR_bitrate']; $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio['VBR_bitrate']; // to avoid confusion } break; } } // End variable-bitrate headers //////////////////////////////////////////////////////////////////////////////////// if ($recursive_search) { if (!getid3_mp3::RecursiveFrameScanning($fd, $info, $offset, $next_frame_test_offset, $scan_as_cbr)) { return false; } } return true; }