rlm@3: <?php
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | PHP version 5                                                        |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | Copyright (c) 2002-2006 James Heinrich, Allan Hansen                 |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | This source file is subject to version 2 of the GPL license,         |
rlm@3: // | that is bundled with this package in the file license.txt and is     |
rlm@3: // | available through the world-wide-web at the following url:           |
rlm@3: // | http://www.gnu.org/copyleft/gpl.html                                 |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | getID3() - http://getid3.sourceforge.net or http://www.getid3.org    |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | Authors: James Heinrich <infoØgetid3*org>                            |
rlm@3: // |          Allan Hansen <ahØartemis*dk>                                |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: // | module.audio.mp3.php                                                 |
rlm@3: // | Module for analyzing MPEG Audio Layer 1,2,3 files.                   |
rlm@3: // | dependencies: none                                                   |
rlm@3: // +----------------------------------------------------------------------+
rlm@3: //
rlm@3: // $Id: module.audio.mp3.php,v 1.10 2006/11/16 22:57:57 ah Exp $
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3: class getid3_mp3 extends getid3_handler
rlm@3: {
rlm@3:     // Number of frames to scan to determine if MPEG-audio sequence is valid.
rlm@3:     // Lower this number to 5-20 for faster scanning
rlm@3:     // Increase this number to 50+ for most accurate detection of valid VBR/CBR mpeg-audio streams
rlm@3:     const VALID_CHECK_FRAMES = 35;
rlm@3:     
rlm@3: 
rlm@3:     public function Analyze() {
rlm@3: 
rlm@3:         $this->getAllMPEGInfo($this->getid3->fp, $this->getid3->info);
rlm@3: 
rlm@3:         return true;   
rlm@3:     }
rlm@3:     
rlm@3:     
rlm@3:     public function AnalyzeMPEGaudioInfo() {
rlm@3:         
rlm@3:         $this->getOnlyMPEGaudioInfo($this->getid3->fp, $this->getid3->info, $this->getid3->info['avdataoffset'], false);
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3:     public function getAllMPEGInfo(&$fd, &$info) {
rlm@3: 
rlm@3:         $this->getOnlyMPEGaudioInfo($fd, $info, 0 + $info['avdataoffset']);
rlm@3: 
rlm@3:         if (isset($info['mpeg']['audio']['bitrate_mode'])) {
rlm@3:             $info['audio']['bitrate_mode'] = strtolower($info['mpeg']['audio']['bitrate_mode']);
rlm@3:         }
rlm@3: 
rlm@3:         if (((isset($info['id3v2']['headerlength']) && ($info['avdataoffset'] > $info['id3v2']['headerlength'])) || (!isset($info['id3v2']) && ($info['avdataoffset'] > 0)))) {
rlm@3: 
rlm@3:             $synch_offset_warning = 'Unknown data before synch ';
rlm@3:             if (isset($info['id3v2']['headerlength'])) {
rlm@3:                 $synch_offset_warning .= '(ID3v2 header ends at '.$info['id3v2']['headerlength'].', then '.($info['avdataoffset'] - $info['id3v2']['headerlength']).' bytes garbage, ';
rlm@3:             } else {
rlm@3:                 $synch_offset_warning .= '(should be at beginning of file, ';
rlm@3:             }
rlm@3:             $synch_offset_warning .= 'synch detected at '.$info['avdataoffset'].')';
rlm@3:             if ($info['audio']['bitrate_mode'] == 'cbr') {
rlm@3: 
rlm@3:                 if (!empty($info['id3v2']['headerlength']) && (($info['avdataoffset'] - $info['id3v2']['headerlength']) == $info['mpeg']['audio']['framelength'])) {
rlm@3: 
rlm@3:                     $synch_offset_warning .= '. This is a known problem with some versions of LAME (3.90-3.92) DLL in CBR mode.';
rlm@3:                     $info['audio']['codec'] = 'LAME';
rlm@3:                     $current_data_lame_version_string = 'LAME3.';
rlm@3: 
rlm@3:                 } elseif (empty($info['id3v2']['headerlength']) && ($info['avdataoffset'] == $info['mpeg']['audio']['framelength'])) {
rlm@3: 
rlm@3:                     $synch_offset_warning .= '. This is a known problem with some versions of LAME (3.90 - 3.92) DLL in CBR mode.';
rlm@3:                     $info['audio']['codec'] = 'LAME';
rlm@3:                     $current_data_lame_version_string = 'LAME3.';
rlm@3: 
rlm@3:                 }
rlm@3: 
rlm@3:             }
rlm@3:             $this->getid3->warning($synch_offset_warning);
rlm@3: 
rlm@3:         }
rlm@3: 
rlm@3:         if (isset($info['mpeg']['audio']['LAME'])) {
rlm@3:             $info['audio']['codec'] = 'LAME';
rlm@3:             if (!empty($info['mpeg']['audio']['LAME']['long_version'])) {
rlm@3:                 $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['long_version'], "\x00");
rlm@3:             } elseif (!empty($info['mpeg']['audio']['LAME']['short_version'])) {
rlm@3:                 $info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['short_version'], "\x00");
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         $current_data_lame_version_string = (!empty($current_data_lame_version_string) ? $current_data_lame_version_string : @$info['audio']['encoder']);
rlm@3:         if (!empty($current_data_lame_version_string) && (substr($current_data_lame_version_string, 0, 6) == 'LAME3.') && !preg_match('[0-9\)]', substr($current_data_lame_version_string, -1))) {
rlm@3:             // a version number of LAME that does not end with a number like "LAME3.92"
rlm@3:             // or with a closing parenthesis like "LAME3.88 (alpha)"
rlm@3:             // or a version of LAME with the LAMEtag-not-filled-in-DLL-mode bug (3.90-3.92)
rlm@3: 
rlm@3:             // not sure what the actual last frame length will be, but will be less than or equal to 1441
rlm@3:             $possibly_longer_lame_version_frame_length = 1441;
rlm@3: 
rlm@3:             // Not sure what version of LAME this is - look in padding of last frame for longer version string
rlm@3:             $possible_lame_version_string_offset = $info['avdataend'] - $possibly_longer_lame_version_frame_length;
rlm@3:             fseek($fd, $possible_lame_version_string_offset);
rlm@3:             $possibly_longer_lame_version_data = fread($fd, $possibly_longer_lame_version_frame_length);
rlm@3:             switch (substr($current_data_lame_version_string, -1)) {
rlm@3:                 case 'a':
rlm@3:                 case 'b':
rlm@3:                     // "LAME3.94a" will have a longer version string of "LAME3.94 (alpha)" for example
rlm@3:                     // need to trim off "a" to match longer string
rlm@3:                     $current_data_lame_version_string = substr($current_data_lame_version_string, 0, -1);
rlm@3:                     break;
rlm@3:             }
rlm@3:             if (($possibly_longer_lame_version_string = strstr($possibly_longer_lame_version_data, $current_data_lame_version_string)) !== false) {
rlm@3:                 if (substr($possibly_longer_lame_version_string, 0, strlen($current_data_lame_version_string)) == $current_data_lame_version_string) {
rlm@3:                     $possibly_longer_lame_version_new_string = substr($possibly_longer_lame_version_string, 0, strspn($possibly_longer_lame_version_string, 'LAME0123456789., (abcdefghijklmnopqrstuvwxyzJFSOND)')); //"LAME3.90.3"  "LAME3.87 (beta 1, Sep 27 2000)" "LAME3.88 (beta)"
rlm@3:                     if (strlen($possibly_longer_lame_version_new_string) > strlen(@$info['audio']['encoder'])) {
rlm@3:                         $info['audio']['encoder'] = $possibly_longer_lame_version_new_string;
rlm@3:                     }
rlm@3:                 }
rlm@3:             }
rlm@3:         }
rlm@3:         if (!empty($info['audio']['encoder'])) {
rlm@3:             $info['audio']['encoder'] = rtrim($info['audio']['encoder'], "\x00 ");
rlm@3:         }
rlm@3: 
rlm@3:         switch (@$info['mpeg']['audio']['layer']) {
rlm@3:             case 1:
rlm@3:             case 2:
rlm@3:                 $info['audio']['dataformat'] = 'mp'.$info['mpeg']['audio']['layer'];
rlm@3:                 break;
rlm@3:         }
rlm@3:         if (@$info['fileformat'] == 'mp3') {
rlm@3:             switch ($info['audio']['dataformat']) {
rlm@3:                 case 'mp1':
rlm@3:                 case 'mp2':
rlm@3:                 case 'mp3':
rlm@3:                     $info['fileformat'] = $info['audio']['dataformat'];
rlm@3:                     break;
rlm@3: 
rlm@3:                 default:
rlm@3:                     $this->getid3->warning('Expecting [audio][dataformat] to be mp1/mp2/mp3 when fileformat == mp3, [audio][dataformat] actually "'.$info['audio']['dataformat'].'"');
rlm@3:                     break;
rlm@3:             }
rlm@3:         }
rlm@3:         
rlm@3:         $info['mime_type']         = 'audio/mpeg';
rlm@3:         $info['audio']['lossless'] = false;
rlm@3: 
rlm@3:         // Calculate playtime
rlm@3:         if (!isset($info['playtime_seconds']) && isset($info['audio']['bitrate']) && ($info['audio']['bitrate'] > 0)) {
rlm@3:             $info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['audio']['bitrate'];
rlm@3:         }
rlm@3: 
rlm@3:         $info['audio']['encoder_options'] = getid3_mp3::GuessEncoderOptions($info);
rlm@3: 
rlm@3:         return true;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function GuessEncoderOptions(&$info) {
rlm@3:         // shortcuts
rlm@3:         if (!empty($info['mpeg']['audio'])) {
rlm@3:             $thisfile_mpeg_audio = &$info['mpeg']['audio'];
rlm@3:             if (!empty($thisfile_mpeg_audio['LAME'])) {
rlm@3:                 $thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         $encoder_options = '';
rlm@3:         static $named_preset_bitrates = array (16, 24, 40, 56, 112, 128, 160, 192, 256);
rlm@3: 
rlm@3:         if ((@$thisfile_mpeg_audio['VBR_method'] == 'Fraunhofer') && !empty($thisfile_mpeg_audio['VBR_quality'])) {
rlm@3: 
rlm@3:             $encoder_options = 'VBR q'.$thisfile_mpeg_audio['VBR_quality'];
rlm@3: 
rlm@3:         } elseif (!empty($thisfile_mpeg_audio_lame['preset_used']) && (!in_array($thisfile_mpeg_audio_lame['preset_used_id'], $named_preset_bitrates))) {
rlm@3: 
rlm@3:             $encoder_options = $thisfile_mpeg_audio_lame['preset_used'];
rlm@3: 
rlm@3:         } elseif (!empty($thisfile_mpeg_audio_lame['vbr_quality'])) {
rlm@3: 
rlm@3:             static $known_encoder_values = array ();
rlm@3:             if (empty($known_encoder_values)) {
rlm@3: 
rlm@3:                 //$known_encoder_values[abrbitrate_minbitrate][vbr_quality][raw_vbr_method][raw_noise_shaping][raw_stereo_mode][ath_type][lowpass_frequency] = 'preset name';
rlm@3:                 $known_encoder_values[0xFF][58][1][1][3][2][20500] = '--alt-preset insane';        // 3.90,   3.90.1, 3.92
rlm@3:                 $known_encoder_values[0xFF][58][1][1][3][2][20600] = '--alt-preset insane';        // 3.90.2, 3.90.3, 3.91
rlm@3:                 $known_encoder_values[0xFF][57][1][1][3][4][20500] = '--alt-preset insane';        // 3.94,   3.95
rlm@3:                 $known_encoder_values['**'][78][3][2][3][2][19500] = '--alt-preset extreme';       // 3.90,   3.90.1, 3.92
rlm@3:                 $known_encoder_values['**'][78][3][2][3][2][19600] = '--alt-preset extreme';       // 3.90.2, 3.91
rlm@3:                 $known_encoder_values['**'][78][3][1][3][2][19600] = '--alt-preset extreme';       // 3.90.3
rlm@3:                 $known_encoder_values['**'][78][4][2][3][2][19500] = '--alt-preset fast extreme';  // 3.90,   3.90.1, 3.92
rlm@3:                 $known_encoder_values['**'][78][4][2][3][2][19600] = '--alt-preset fast extreme';  // 3.90.2, 3.90.3, 3.91
rlm@3:                 $known_encoder_values['**'][78][3][2][3][4][19000] = '--alt-preset standard';      // 3.90,   3.90.1, 3.90.2, 3.91, 3.92
rlm@3:                 $known_encoder_values['**'][78][3][1][3][4][19000] = '--alt-preset standard';      // 3.90.3
rlm@3:                 $known_encoder_values['**'][78][4][2][3][4][19000] = '--alt-preset fast standard'; // 3.90,   3.90.1, 3.90.2, 3.91, 3.92
rlm@3:                 $known_encoder_values['**'][78][4][1][3][4][19000] = '--alt-preset fast standard'; // 3.90.3
rlm@3:                 $known_encoder_values['**'][88][4][1][3][3][19500] = '--r3mix';                    // 3.90,   3.90.1, 3.92
rlm@3:                 $known_encoder_values['**'][88][4][1][3][3][19600] = '--r3mix';                    // 3.90.2, 3.90.3, 3.91
rlm@3:                 $known_encoder_values['**'][67][4][1][3][4][18000] = '--r3mix';                    // 3.94,   3.95
rlm@3:                 $known_encoder_values['**'][68][3][2][3][4][18000] = '--alt-preset medium';        // 3.90.3
rlm@3:                 $known_encoder_values['**'][68][4][2][3][4][18000] = '--alt-preset fast medium';   // 3.90.3
rlm@3: 
rlm@3:                 $known_encoder_values[0xFF][99][1][1][1][2][0]     = '--preset studio';            // 3.90,   3.90.1, 3.90.2, 3.91, 3.92
rlm@3:                 $known_encoder_values[0xFF][58][2][1][3][2][20600] = '--preset studio';            // 3.90.3, 3.93.1
rlm@3:                 $known_encoder_values[0xFF][58][2][1][3][2][20500] = '--preset studio';            // 3.93
rlm@3:                 $known_encoder_values[0xFF][57][2][1][3][4][20500] = '--preset studio';            // 3.94,   3.95
rlm@3:                 $known_encoder_values[0xC0][88][1][1][1][2][0]     = '--preset cd';                // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
rlm@3:                 $known_encoder_values[0xC0][58][2][2][3][2][19600] = '--preset cd';                // 3.90.3, 3.93.1
rlm@3:                 $known_encoder_values[0xC0][58][2][2][3][2][19500] = '--preset cd';                // 3.93
rlm@3:                 $known_encoder_values[0xC0][57][2][1][3][4][19500] = '--preset cd';                // 3.94,   3.95
rlm@3:                 $known_encoder_values[0xA0][78][1][1][3][2][18000] = '--preset hifi';              // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
rlm@3:                 $known_encoder_values[0xA0][58][2][2][3][2][18000] = '--preset hifi';              // 3.90.3, 3.93,   3.93.1
rlm@3:                 $known_encoder_values[0xA0][57][2][1][3][4][18000] = '--preset hifi';              // 3.94,   3.95
rlm@3:                 $known_encoder_values[0x80][67][1][1][3][2][18000] = '--preset tape';              // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
rlm@3:                 $known_encoder_values[0x80][67][1][1][3][2][15000] = '--preset radio';             // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
rlm@3:                 $known_encoder_values[0x70][67][1][1][3][2][15000] = '--preset fm';                // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
rlm@3:                 $known_encoder_values[0x70][58][2][2][3][2][16000] = '--preset tape/radio/fm';     // 3.90.3, 3.93,   3.93.1
rlm@3:                 $known_encoder_values[0x70][57][2][1][3][4][16000] = '--preset tape/radio/fm';     // 3.94,   3.95
rlm@3:                 $known_encoder_values[0x38][58][2][2][0][2][10000] = '--preset voice';             // 3.90.3, 3.93,   3.93.1
rlm@3:                 $known_encoder_values[0x38][57][2][1][0][4][15000] = '--preset voice';             // 3.94,   3.95
rlm@3:                 $known_encoder_values[0x38][57][2][1][0][4][16000] = '--preset voice';             // 3.94a14
rlm@3:                 $known_encoder_values[0x28][65][1][1][0][2][7500]  = '--preset mw-us';             // 3.90,   3.90.1, 3.92
rlm@3:                 $known_encoder_values[0x28][65][1][1][0][2][7600]  = '--preset mw-us';             // 3.90.2, 3.91
rlm@3:                 $known_encoder_values[0x28][58][2][2][0][2][7000]  = '--preset mw-us';             // 3.90.3, 3.93,   3.93.1
rlm@3:                 $known_encoder_values[0x28][57][2][1][0][4][10500] = '--preset mw-us';             // 3.94,   3.95
rlm@3:                 $known_encoder_values[0x28][57][2][1][0][4][11200] = '--preset mw-us';             // 3.94a14
rlm@3:                 $known_encoder_values[0x28][57][2][1][0][4][8800]  = '--preset mw-us';             // 3.94a15
rlm@3:                 $known_encoder_values[0x18][58][2][2][0][2][4000]  = '--preset phon+/lw/mw-eu/sw'; // 3.90.3, 3.93.1
rlm@3:                 $known_encoder_values[0x18][58][2][2][0][2][3900]  = '--preset phon+/lw/mw-eu/sw'; // 3.93
rlm@3:                 $known_encoder_values[0x18][57][2][1][0][4][5900]  = '--preset phon+/lw/mw-eu/sw'; // 3.94,   3.95
rlm@3:                 $known_encoder_values[0x18][57][2][1][0][4][6200]  = '--preset phon+/lw/mw-eu/sw'; // 3.94a14
rlm@3:                 $known_encoder_values[0x18][57][2][1][0][4][3200]  = '--preset phon+/lw/mw-eu/sw'; // 3.94a15
rlm@3:                 $known_encoder_values[0x10][58][2][2][0][2][3800]  = '--preset phone';             // 3.90.3, 3.93.1
rlm@3:                 $known_encoder_values[0x10][58][2][2][0][2][3700]  = '--preset phone';             // 3.93
rlm@3:                 $known_encoder_values[0x10][57][2][1][0][4][5600]  = '--preset phone';             // 3.94,   3.95
rlm@3:             }
rlm@3: 
rlm@3:             if (isset($known_encoder_values[$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']])) {
rlm@3: 
rlm@3:                 $encoder_options = $known_encoder_values[$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']];
rlm@3: 
rlm@3:             } elseif (isset($known_encoder_values['**'][$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']])) {
rlm@3: 
rlm@3:                 $encoder_options = $known_encoder_values['**'][$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']];
rlm@3: 
rlm@3:             } elseif ($info['audio']['bitrate_mode'] == 'vbr') {
rlm@3: 
rlm@3:                 // http://gabriel.mp3-tech.org/mp3infotag.html
rlm@3:                 // int    Quality = (100 - 10 * gfp->VBR_q - gfp->quality)h
rlm@3: 
rlm@3: 
rlm@3:                 $lame_v_value = 10 - ceil($thisfile_mpeg_audio_lame['vbr_quality'] / 10);
rlm@3:                 $lame_q_value = 100 - $thisfile_mpeg_audio_lame['vbr_quality'] - ($lame_v_value * 10);
rlm@3:                 $encoder_options = '-V'.$lame_v_value.' -q'.$lame_q_value;
rlm@3: 
rlm@3:             } elseif ($info['audio']['bitrate_mode'] == 'cbr') {
rlm@3: 
rlm@3:                 $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
rlm@3: 
rlm@3:             } else {
rlm@3: 
rlm@3:                 $encoder_options = strtoupper($info['audio']['bitrate_mode']);
rlm@3: 
rlm@3:             }
rlm@3: 
rlm@3:         } elseif (!empty($thisfile_mpeg_audio_lame['bitrate_abr'])) {
rlm@3: 
rlm@3:             $encoder_options = 'ABR'.$thisfile_mpeg_audio_lame['bitrate_abr'];
rlm@3: 
rlm@3:         } elseif (!empty($info['audio']['bitrate'])) {
rlm@3: 
rlm@3:             if ($info['audio']['bitrate_mode'] == 'cbr') {
rlm@3:                 $encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
rlm@3:             } else {
rlm@3:                 $encoder_options = strtoupper($info['audio']['bitrate_mode']);
rlm@3:             }
rlm@3: 
rlm@3:         }
rlm@3:         if (!empty($thisfile_mpeg_audio_lame['bitrate_min'])) {
rlm@3:             $encoder_options .= ' -b'.$thisfile_mpeg_audio_lame['bitrate_min'];
rlm@3:         }
rlm@3: 
rlm@3:         if (@$thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev'] || @$thisfile_mpeg_audio_lame['encoding_flags']['nogap_next']) {
rlm@3:             $encoder_options .= ' --nogap';
rlm@3:         }
rlm@3: 
rlm@3:         if (!empty($thisfile_mpeg_audio_lame['lowpass_frequency'])) {
rlm@3:             $exploded_options = explode(' ', $encoder_options, 4);
rlm@3:             if ($exploded_options[0] == '--r3mix') {
rlm@3:                 $exploded_options[1] = 'r3mix';
rlm@3:             }
rlm@3:             switch ($exploded_options[0]) {
rlm@3:                 case '--preset':
rlm@3:                 case '--alt-preset':
rlm@3:                 case '--r3mix':
rlm@3:                     if ($exploded_options[1] == 'fast') {
rlm@3:                         $exploded_options[1] .= ' '.$exploded_options[2];
rlm@3:                     }
rlm@3:                     switch ($exploded_options[1]) {
rlm@3:                         case 'portable':
rlm@3:                         case 'medium':
rlm@3:                         case 'standard':
rlm@3:                         case 'extreme':
rlm@3:                         case 'insane':
rlm@3:                         case 'fast portable':
rlm@3:                         case 'fast medium':
rlm@3:                         case 'fast standard':
rlm@3:                         case 'fast extreme':
rlm@3:                         case 'fast insane':
rlm@3:                         case 'r3mix':
rlm@3:                             static $expected_lowpass = array (
rlm@3:                                     'insane|20500'        => 20500,
rlm@3:                                     'insane|20600'        => 20600,  // 3.90.2, 3.90.3, 3.91
rlm@3:                                     'medium|18000'        => 18000,
rlm@3:                                     'fast medium|18000'   => 18000,
rlm@3:                                     'extreme|19500'       => 19500,  // 3.90,   3.90.1, 3.92, 3.95
rlm@3:                                     'extreme|19600'       => 19600,  // 3.90.2, 3.90.3, 3.91, 3.93.1
rlm@3:                                     'fast extreme|19500'  => 19500,  // 3.90,   3.90.1, 3.92, 3.95
rlm@3:                                     'fast extreme|19600'  => 19600,  // 3.90.2, 3.90.3, 3.91, 3.93.1
rlm@3:                                     'standard|19000'      => 19000,
rlm@3:                                     'fast standard|19000' => 19000,
rlm@3:                                     'r3mix|19500'         => 19500,  // 3.90,   3.90.1, 3.92
rlm@3:                                     'r3mix|19600'         => 19600,  // 3.90.2, 3.90.3, 3.91
rlm@3:                                     'r3mix|18000'         => 18000,  // 3.94,   3.95
rlm@3:                                 );
rlm@3:                             if (!isset($expected_lowpass[$exploded_options[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))) {
rlm@3:                                 $encoder_options .= ' --lowpass '.$thisfile_mpeg_audio_lame['lowpass_frequency'];
rlm@3:                             }
rlm@3:                             break;
rlm@3: 
rlm@3:                         default:
rlm@3:                             break;
rlm@3:                     }
rlm@3:                     break;
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         if (isset($thisfile_mpeg_audio_lame['raw']['source_sample_freq'])) {
rlm@3:             if (($thisfile_mpeg_audio['sample_rate'] == 44100) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 1)) {
rlm@3:                 $encoder_options .= ' --resample 44100';
rlm@3:             } elseif (($thisfile_mpeg_audio['sample_rate'] == 48000) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 2)) {
rlm@3:                 $encoder_options .= ' --resample 48000';
rlm@3:             } elseif ($thisfile_mpeg_audio['sample_rate'] < 44100) {
rlm@3:                 switch ($thisfile_mpeg_audio_lame['raw']['source_sample_freq']) {
rlm@3:                     case 0: // <= 32000
rlm@3:                         // may or may not be same as source frequency - ignore
rlm@3:                         break;
rlm@3:                     case 1: // 44100
rlm@3:                     case 2: // 48000
rlm@3:                     case 3: // 48000+
rlm@3:                         $exploded_options = explode(' ', $encoder_options, 4);
rlm@3:                         switch ($exploded_options[0]) {
rlm@3:                             case '--preset':
rlm@3:                             case '--alt-preset':
rlm@3:                                 switch ($exploded_options[1]) {
rlm@3:                                     case 'fast':
rlm@3:                                     case 'portable':
rlm@3:                                     case 'medium':
rlm@3:                                     case 'standard':
rlm@3:                                     case 'extreme':
rlm@3:                                     case 'insane':
rlm@3:                                         $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
rlm@3:                                         break;
rlm@3: 
rlm@3:                                     default:
rlm@3:                                         static $expected_resampled_rate = array (
rlm@3:                                                 'phon+/lw/mw-eu/sw|16000' => 16000,
rlm@3:                                                 'mw-us|24000'             => 24000, // 3.95
rlm@3:                                                 'mw-us|32000'             => 32000, // 3.93
rlm@3:                                                 'mw-us|16000'             => 16000, // 3.92
rlm@3:                                                 'phone|16000'             => 16000,
rlm@3:                                                 'phone|11025'             => 11025, // 3.94a15
rlm@3:                                                 'radio|32000'             => 32000, // 3.94a15
rlm@3:                                                 'fm/radio|32000'          => 32000, // 3.92
rlm@3:                                                 'fm|32000'                => 32000, // 3.90
rlm@3:                                                 'voice|32000'             => 32000);
rlm@3:                                         if (!isset($expected_resampled_rate[$exploded_options[1].'|'.$thisfile_mpeg_audio['sample_rate']])) {
rlm@3:                                             $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
rlm@3:                                         }
rlm@3:                                         break;
rlm@3:                                 }
rlm@3:                                 break;
rlm@3: 
rlm@3:                             case '--r3mix':
rlm@3:                             default:
rlm@3:                                 $encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
rlm@3:                                 break;
rlm@3:                         }
rlm@3:                         break;
rlm@3:                 }
rlm@3:             }
rlm@3:         }
rlm@3:         if (empty($encoder_options) && !empty($info['audio']['bitrate']) && !empty($info['audio']['bitrate_mode'])) {
rlm@3:             //$encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
rlm@3:             $encoder_options = strtoupper($info['audio']['bitrate_mode']);
rlm@3:         }
rlm@3: 
rlm@3:         return $encoder_options;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3:     
rlm@3:     public function decodeMPEGaudioHeader($fd, $offset, &$info, $recursive_search=true, $scan_as_cbr=false, $fast_mpeg_header_scan=false) {
rlm@3: 
rlm@3:         static $mpeg_audio_version_lookup;
rlm@3:         static $mpeg_audio_layer_lookup;
rlm@3:         static $mpeg_audio_bitrate_lookup;
rlm@3:         static $mpeg_audio_frequency_lookup;
rlm@3:         static $mpeg_audio_channel_mode_lookup;
rlm@3:         static $mpeg_audio_mode_extension_lookup;
rlm@3:         static $mpeg_audio_emphasis_lookup;
rlm@3:         if (empty($mpeg_audio_version_lookup)) {
rlm@3:             $mpeg_audio_version_lookup        = getid3_mp3::MPEGaudioVersionarray();
rlm@3:             $mpeg_audio_layer_lookup          = getid3_mp3::MPEGaudioLayerarray();
rlm@3:             $mpeg_audio_bitrate_lookup        = getid3_mp3::MPEGaudioBitratearray();
rlm@3:             $mpeg_audio_frequency_lookup      = getid3_mp3::MPEGaudioFrequencyarray();
rlm@3:             $mpeg_audio_channel_mode_lookup   = getid3_mp3::MPEGaudioChannelModearray();
rlm@3:             $mpeg_audio_mode_extension_lookup = getid3_mp3::MPEGaudioModeExtensionarray();
rlm@3:             $mpeg_audio_emphasis_lookup       = getid3_mp3::MPEGaudioEmphasisarray();
rlm@3:         }
rlm@3: 
rlm@3:         if ($offset >= $info['avdataend']) {
rlm@3: 
rlm@3:             // non-fatal error: 'end of file encounter looking for MPEG synch'
rlm@3:             return;
rlm@3:             
rlm@3:         }
rlm@3:         fseek($fd, $offset, SEEK_SET);
rlm@3:         $header_string = fread($fd, 226); // LAME header at offset 36 + 190 bytes of Xing/LAME data
rlm@3: 
rlm@3:         // MP3 audio frame structure:
rlm@3:         // $aa $aa $aa $aa [$bb $bb] $cc...
rlm@3:         // where $aa..$aa is the four-byte mpeg-audio header (below)
rlm@3:         // $bb $bb is the optional 2-byte CRC
rlm@3:         // and $cc... is the audio data
rlm@3: 
rlm@3:         $head4 = substr($header_string, 0, 4);
rlm@3: 
rlm@3:         if (isset($mpeg_audio_header_decode_cache[$head4])) {
rlm@3:             $mpeg_header_raw_array= $mpeg_audio_header_decode_cache[$head4];
rlm@3:         } else {
rlm@3:             $mpeg_header_raw_array = getid3_mp3::MPEGaudioHeaderDecode($head4);
rlm@3:             $mpeg_audio_header_decode_cache[$head4] = $mpeg_header_raw_array;
rlm@3:         }
rlm@3: 
rlm@3:         // Not in cache
rlm@3:         if (!isset($mpeg_audio_header_valid_cache[$head4])) {
rlm@3:             $mpeg_audio_header_valid_cache[$head4] = getid3_mp3::MPEGaudioHeaderValid($mpeg_header_raw_array, false, false);
rlm@3:         }
rlm@3: 
rlm@3:         // shortcut
rlm@3:         if (!isset($info['mpeg']['audio'])) {
rlm@3:             $info['mpeg']['audio'] = array ();
rlm@3:         }
rlm@3:         $thisfile_mpeg_audio = &$info['mpeg']['audio'];
rlm@3: 
rlm@3: 
rlm@3:         if ($mpeg_audio_header_valid_cache[$head4]) {
rlm@3:             $thisfile_mpeg_audio['raw'] = $mpeg_header_raw_array;
rlm@3:         } else {
rlm@3:             
rlm@3:             // non-fatal error: Invalid MPEG audio header at offset $offset
rlm@3:             return;
rlm@3:         }
rlm@3: 
rlm@3:         if (!$fast_mpeg_header_scan) {
rlm@3: 
rlm@3:             $thisfile_mpeg_audio['version']       = $mpeg_audio_version_lookup[$thisfile_mpeg_audio['raw']['version']];
rlm@3:             $thisfile_mpeg_audio['layer']         = $mpeg_audio_layer_lookup[$thisfile_mpeg_audio['raw']['layer']];
rlm@3: 
rlm@3:             $thisfile_mpeg_audio['channelmode']   = $mpeg_audio_channel_mode_lookup[$thisfile_mpeg_audio['raw']['channelmode']];
rlm@3:             $thisfile_mpeg_audio['channels']      = (($thisfile_mpeg_audio['channelmode'] == 'mono') ? 1 : 2);
rlm@3:             $thisfile_mpeg_audio['sample_rate']   = $mpeg_audio_frequency_lookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['raw']['sample_rate']];
rlm@3:             $thisfile_mpeg_audio['protection']    = !$thisfile_mpeg_audio['raw']['protection'];
rlm@3:             $thisfile_mpeg_audio['private']       = (bool) $thisfile_mpeg_audio['raw']['private'];
rlm@3:             $thisfile_mpeg_audio['modeextension'] = $mpeg_audio_mode_extension_lookup[$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['modeextension']];
rlm@3:             $thisfile_mpeg_audio['copyright']     = (bool) $thisfile_mpeg_audio['raw']['copyright'];
rlm@3:             $thisfile_mpeg_audio['original']      = (bool) $thisfile_mpeg_audio['raw']['original'];
rlm@3:             $thisfile_mpeg_audio['emphasis']      = $mpeg_audio_emphasis_lookup[$thisfile_mpeg_audio['raw']['emphasis']];
rlm@3: 
rlm@3:             $info['audio']['channels']    = $thisfile_mpeg_audio['channels'];
rlm@3:             $info['audio']['sample_rate'] = $thisfile_mpeg_audio['sample_rate'];
rlm@3: 
rlm@3:             if ($thisfile_mpeg_audio['protection']) {
rlm@3:                 $thisfile_mpeg_audio['crc'] = getid3_lib::BigEndian2Int(substr($header_string, 4, 2));
rlm@3:             }
rlm@3: 
rlm@3:         }
rlm@3: 
rlm@3:         if ($thisfile_mpeg_audio['raw']['bitrate'] == 15) {
rlm@3:             // http://www.hydrogenaudio.org/?act=ST&f=16&t=9682&st=0
rlm@3:             $this->getid3->warning('Invalid bitrate index (15), this is a known bug in free-format MP3s encoded by LAME v3.90 - 3.93.1');
rlm@3:             $thisfile_mpeg_audio['raw']['bitrate'] = 0;
rlm@3:         }
rlm@3:         $thisfile_mpeg_audio['padding'] = (bool) $thisfile_mpeg_audio['raw']['padding'];
rlm@3:         $thisfile_mpeg_audio['bitrate'] = $mpeg_audio_bitrate_lookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['bitrate']];
rlm@3: 
rlm@3:         if (($thisfile_mpeg_audio['bitrate'] == 'free') && ($offset == $info['avdataoffset'])) {
rlm@3:             // only skip multiple frame check if free-format bitstream found at beginning of file
rlm@3:             // otherwise is quite possibly simply corrupted data
rlm@3:             $recursive_search = false;
rlm@3:         }
rlm@3: 
rlm@3:         // For Layer 2 there are some combinations of bitrate and mode which are not allowed.
rlm@3:         if (!$fast_mpeg_header_scan && ($thisfile_mpeg_audio['layer'] == '2')) {
rlm@3: 
rlm@3:             $info['audio']['dataformat'] = 'mp2';
rlm@3:             switch ($thisfile_mpeg_audio['channelmode']) {
rlm@3: 
rlm@3:                 case 'mono':
rlm@3:                     if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] <= 192000)) {
rlm@3:                         // these are ok
rlm@3:                     } else {
rlm@3:                         
rlm@3:                         // non-fatal error: bitrate not allowed in Layer 2/mono
rlm@3:                         return;
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3:                 case 'stereo':
rlm@3:                 case 'joint stereo':
rlm@3:                 case 'dual channel':
rlm@3:                     if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] == 64000) || ($thisfile_mpeg_audio['bitrate'] >= 96000)) {
rlm@3:                         // these are ok
rlm@3:                     } else {
rlm@3:                         
rlm@3:                         // non-fatal error: bitrate not allowed in Layer 2/stereo/joint stereo/dual channel
rlm@3:                         return;
rlm@3:                     }
rlm@3:                     break;
rlm@3: 
rlm@3:             }
rlm@3: 
rlm@3:         }
rlm@3: 
rlm@3: 
rlm@3:         if ($info['audio']['sample_rate'] > 0) {
rlm@3:             $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']);
rlm@3:         }
rlm@3: 
rlm@3:         $next_frame_test_offset = $offset + 1;
rlm@3:         if ($thisfile_mpeg_audio['bitrate'] != 'free') {
rlm@3: 
rlm@3:             $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
rlm@3: 
rlm@3:             if (isset($thisfile_mpeg_audio['framelength'])) {
rlm@3:                 $next_frame_test_offset = $offset + $thisfile_mpeg_audio['framelength'];
rlm@3:             } else {
rlm@3: 
rlm@3:                 // non-fatal error: Frame at offset('.$offset.') is has an invalid frame length.
rlm@3:                 return;
rlm@3:             }
rlm@3: 
rlm@3:         }
rlm@3: 
rlm@3:         $expected_number_of_audio_bytes = 0;
rlm@3: 
rlm@3:         ////////////////////////////////////////////////////////////////////////////////////
rlm@3:         // Variable-bitrate headers
rlm@3: 
rlm@3:         if (substr($header_string, 4 + 32, 4) == 'VBRI') {
rlm@3:             // Fraunhofer VBR header is hardcoded 'VBRI' at offset 0x24 (36)
rlm@3:             // specs taken from http://minnie.tuhs.org/pipermail/mp3encoder/2001-January/001800.html
rlm@3: 
rlm@3:             $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
rlm@3:             $thisfile_mpeg_audio['VBR_method']   = 'Fraunhofer';
rlm@3:             $info['audio']['codec']                = 'Fraunhofer';
rlm@3: 
rlm@3:             $side_info_data = substr($header_string, 4 + 2, 32);
rlm@3: 
rlm@3:             $fraunhofer_vbr_offset = 36;
rlm@3: 
rlm@3:             $thisfile_mpeg_audio['VBR_encoder_version']     = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset +  4, 2)); // VbriVersion
rlm@3:             $thisfile_mpeg_audio['VBR_encoder_delay']       = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset +  6, 2)); // VbriDelay
rlm@3:             $thisfile_mpeg_audio['VBR_quality']             = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset +  8, 2)); // VbriQuality
rlm@3:             $thisfile_mpeg_audio['VBR_bytes']               = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 10, 4)); // VbriStreamBytes
rlm@3:             $thisfile_mpeg_audio['VBR_frames']              = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 14, 4)); // VbriStreamFrames
rlm@3:             $thisfile_mpeg_audio['VBR_seek_offsets']        = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 18, 2)); // VbriTableSize
rlm@3:             $thisfile_mpeg_audio['VBR_seek_scale']          = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 20, 2)); // VbriTableScale
rlm@3:             $thisfile_mpeg_audio['VBR_entry_bytes']         = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 22, 2)); // VbriEntryBytes
rlm@3:             $thisfile_mpeg_audio['VBR_entry_frames']        = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset + 24, 2)); // VbriEntryFrames
rlm@3: 
rlm@3:             $expected_number_of_audio_bytes = $thisfile_mpeg_audio['VBR_bytes'];
rlm@3: 
rlm@3:             $previous_byte_offset = $offset;
rlm@3:             for ($i = 0; $i < $thisfile_mpeg_audio['VBR_seek_offsets']; $i++) {
rlm@3:                 $fraunhofer_offset_n = getid3_lib::BigEndian2Int(substr($header_string, $fraunhofer_vbr_offset, $thisfile_mpeg_audio['VBR_entry_bytes']));
rlm@3:                 $fraunhofer_vbr_offset += $thisfile_mpeg_audio['VBR_entry_bytes'];
rlm@3:                 $thisfile_mpeg_audio['VBR_offsets_relative'][$i] = ($fraunhofer_offset_n * $thisfile_mpeg_audio['VBR_seek_scale']);
rlm@3:                 $thisfile_mpeg_audio['VBR_offsets_absolute'][$i] = ($fraunhofer_offset_n * $thisfile_mpeg_audio['VBR_seek_scale']) + $previous_byte_offset;
rlm@3:                 $previous_byte_offset += $fraunhofer_offset_n;
rlm@3:             }
rlm@3: 
rlm@3: 
rlm@3:         } else {
rlm@3: 
rlm@3:             // Xing VBR header is hardcoded 'Xing' at a offset 0x0D (13), 0x15 (21) or 0x24 (36)
rlm@3:             // depending on MPEG layer and number of channels
rlm@3: 
rlm@3:             $vbr_id_offset = getid3_mp3::XingVBRidOffset($thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['channelmode']);
rlm@3:             $side_info_data = substr($header_string, 4 + 2, $vbr_id_offset - 4);
rlm@3: 
rlm@3:             if ((substr($header_string, $vbr_id_offset, strlen('Xing')) == 'Xing') || (substr($header_string, $vbr_id_offset, strlen('Info')) == 'Info')) {
rlm@3:                 // 'Xing' is traditional Xing VBR frame
rlm@3:                 // 'Info' is LAME-encoded CBR (This was done to avoid CBR files to be recognized as traditional Xing VBR files by some decoders.)
rlm@3:                 // 'Info' *can* legally be used to specify a VBR file as well, however.
rlm@3: 
rlm@3:                 // http://www.multiweb.cz/twoinches/MP3inside.htm
rlm@3:                 //00..03 = "Xing" or "Info"
rlm@3:                 //04..07 = Flags:
rlm@3:                 //  0x01  Frames Flag     set if value for number of frames in file is stored
rlm@3:                 //  0x02  Bytes Flag      set if value for filesize in bytes is stored
rlm@3:                 //  0x04  TOC Flag        set if values for TOC are stored
rlm@3:                 //  0x08  VBR Scale Flag  set if values for VBR scale is stored
rlm@3:                 //08..11  Frames: Number of frames in file (including the first Xing/Info one)
rlm@3:                 //12..15  Bytes:  File length in Bytes
rlm@3:                 //16..115  TOC (Table of Contents):
rlm@3:                 //  Contains of 100 indexes (one Byte length) for easier lookup in file. Approximately solves problem with moving inside file.
rlm@3:                 //  Each Byte has a value according this formula:
rlm@3:                 //  (TOC[i] / 256) * fileLenInBytes
rlm@3:                 //  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:
rlm@3:                 //  TOC[(60/240)*100] = TOC[25]
rlm@3:                 //  and corresponding Byte in file is then approximately at:
rlm@3:                 //  (TOC[25]/256) * 5000000
rlm@3:                 //116..119  VBR Scale
rlm@3: 
rlm@3: 
rlm@3:                 // should be safe to leave this at 'vbr' and let it be overriden to 'cbr' if a CBR preset/mode is used by LAME
rlm@3:                 $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
rlm@3:                 $thisfile_mpeg_audio['VBR_method']   = 'Xing';
rlm@3: 
rlm@3:                 $thisfile_mpeg_audio['xing_flags_raw'] = getid3_lib::BigEndian2Int(substr($header_string, $vbr_id_offset + 4, 4));
rlm@3: 
rlm@3:                 $thisfile_mpeg_audio['xing_flags']['frames']    = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000001);
rlm@3:                 $thisfile_mpeg_audio['xing_flags']['bytes']     = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000002);
rlm@3:                 $thisfile_mpeg_audio['xing_flags']['toc']       = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000004);
rlm@3:                 $thisfile_mpeg_audio['xing_flags']['vbr_scale'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000008);
rlm@3: 
rlm@3:                 if ($thisfile_mpeg_audio['xing_flags']['frames']) {
rlm@3:                     $thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($header_string, $vbr_id_offset +  8, 4));
rlm@3:                 }
rlm@3:                 if ($thisfile_mpeg_audio['xing_flags']['bytes']) {
rlm@3:                     $thisfile_mpeg_audio['VBR_bytes']  = getid3_lib::BigEndian2Int(substr($header_string, $vbr_id_offset + 12, 4));
rlm@3:                 }
rlm@3: 
rlm@3:                 if (!empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
rlm@3: 
rlm@3:                     $frame_lengthfloat = $thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames'];
rlm@3: 
rlm@3:                     if ($thisfile_mpeg_audio['layer'] == '1') {
rlm@3:                         // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12
rlm@3:                         $info['audio']['bitrate'] = ($frame_lengthfloat / 4) * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 12;
rlm@3:                     } else {
rlm@3:                         // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144
rlm@3:                         $info['audio']['bitrate'] = $frame_lengthfloat * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 144;
rlm@3:                     }
rlm@3:                     $thisfile_mpeg_audio['framelength'] = floor($frame_lengthfloat);
rlm@3:                 }
rlm@3: 
rlm@3:                 if ($thisfile_mpeg_audio['xing_flags']['toc']) {
rlm@3:                     $lame_toc_data = substr($header_string, $vbr_id_offset + 16, 100);
rlm@3:                     for ($i = 0; $i < 100; $i++) {
rlm@3:                         $thisfile_mpeg_audio['toc'][$i] = ord($lame_toc_data{$i});
rlm@3:                     }
rlm@3:                 }
rlm@3:                 if ($thisfile_mpeg_audio['xing_flags']['vbr_scale']) {
rlm@3:                     $thisfile_mpeg_audio['VBR_scale'] = getid3_lib::BigEndian2Int(substr($header_string, $vbr_id_offset + 116, 4));
rlm@3:                 }
rlm@3: 
rlm@3: 
rlm@3:                 // http://gabriel.mp3-tech.org/mp3infotag.html
rlm@3:                 if (substr($header_string, $vbr_id_offset + 120, 4) == 'LAME') {
rlm@3: 
rlm@3:                     // shortcut
rlm@3:                     $thisfile_mpeg_audio['LAME'] = array ();
rlm@3:                     $thisfile_mpeg_audio_lame    = &$thisfile_mpeg_audio['LAME'];
rlm@3: 
rlm@3: 
rlm@3:                     $thisfile_mpeg_audio_lame['long_version']  = substr($header_string, $vbr_id_offset + 120, 20);
rlm@3:                     $thisfile_mpeg_audio_lame['short_version'] = substr($thisfile_mpeg_audio_lame['long_version'], 0, 9);
rlm@3: 
rlm@3:                     if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.90') {
rlm@3: 
rlm@3:                         // extra 11 chars are not part of version string when LAMEtag present
rlm@3:                         unset($thisfile_mpeg_audio_lame['long_version']);
rlm@3: 
rlm@3:                         // It the LAME tag was only introduced in LAME v3.90
rlm@3:                         // http://www.hydrogenaudio.org/?act=ST&f=15&t=9933
rlm@3: 
rlm@3:                         // Offsets of various bytes in http://gabriel.mp3-tech.org/mp3infotag.html
rlm@3:                         // are assuming a 'Xing' identifier offset of 0x24, which is the case for
rlm@3:                         // MPEG-1 non-mono, but not for other combinations
rlm@3:                         $lame_tag_offset_contant = $vbr_id_offset - 0x24;
rlm@3: 
rlm@3:                         // shortcuts
rlm@3:                         $thisfile_mpeg_audio_lame['RGAD']    = array ('track'=>array(), 'album'=>array());
rlm@3:                         $thisfile_mpeg_audio_lame_rgad       = &$thisfile_mpeg_audio_lame['RGAD'];
rlm@3:                         $thisfile_mpeg_audio_lame_rgad_track = &$thisfile_mpeg_audio_lame_rgad['track'];
rlm@3:                         $thisfile_mpeg_audio_lame_rgad_album = &$thisfile_mpeg_audio_lame_rgad['album'];
rlm@3:                         $thisfile_mpeg_audio_lame['raw']     = array ();
rlm@3:                         $thisfile_mpeg_audio_lame_raw        = &$thisfile_mpeg_audio_lame['raw'];
rlm@3: 
rlm@3:                         // byte $9B  VBR Quality
rlm@3:                         // This field is there to indicate a quality level, although the scale was not precised in the original Xing specifications.
rlm@3:                         // Actually overwrites original Xing bytes
rlm@3:                         unset($thisfile_mpeg_audio['VBR_scale']);
rlm@3:                         $thisfile_mpeg_audio_lame['vbr_quality'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0x9B, 1));
rlm@3: 
rlm@3:                         // bytes $9C-$A4  Encoder short VersionString
rlm@3:                         $thisfile_mpeg_audio_lame['short_version'] = substr($header_string, $lame_tag_offset_contant + 0x9C, 9);
rlm@3: 
rlm@3:                         // byte $A5  Info Tag revision + VBR method
rlm@3:                         $lame_tagRevisionVBRmethod = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xA5, 1));
rlm@3: 
rlm@3:                         $thisfile_mpeg_audio_lame['tag_revision']      = ($lame_tagRevisionVBRmethod & 0xF0) >> 4;
rlm@3:                         $thisfile_mpeg_audio_lame_raw['vbr_method'] =  $lame_tagRevisionVBRmethod & 0x0F;
rlm@3:                         $thisfile_mpeg_audio_lame['vbr_method']        = getid3_mp3::LAMEvbrMethodLookup($thisfile_mpeg_audio_lame_raw['vbr_method']);
rlm@3:                         $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'
rlm@3: 
rlm@3:                         // byte $A6  Lowpass filter value
rlm@3:                         $thisfile_mpeg_audio_lame['lowpass_frequency'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xA6, 1)) * 100;
rlm@3: 
rlm@3:                         // bytes $A7-$AE  Replay Gain
rlm@3:                         // http://privatewww.essex.ac.uk/~djmrob/replaygain/rg_data_format.html
rlm@3:                         // bytes $A7-$AA : 32 bit floating point "Peak signal amplitude"
rlm@3:                         if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.94b') {
rlm@3:                             // LAME 3.94a16 and later - 9.23 fixed point
rlm@3:                             // ie 0x0059E2EE / (2^23) = 5890798 / 8388608 = 0.7022378444671630859375
rlm@3:                             $thisfile_mpeg_audio_lame_rgad['peak_amplitude'] = (float) ((getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xA7, 4))) / 8388608);
rlm@3:                         } else {
rlm@3:                             // LAME 3.94a15 and earlier - 32-bit floating point
rlm@3:                             // Actually 3.94a16 will fall in here too and be WRONG, but is hard to detect 3.94a16 vs 3.94a15
rlm@3:                             $thisfile_mpeg_audio_lame_rgad['peak_amplitude'] = getid3_lib::LittleEndian2Float(substr($header_string, $lame_tag_offset_contant + 0xA7, 4));
rlm@3:                         }
rlm@3:                         if ($thisfile_mpeg_audio_lame_rgad['peak_amplitude'] == 0) {
rlm@3:                             unset($thisfile_mpeg_audio_lame_rgad['peak_amplitude']);
rlm@3:                         } else {
rlm@3:                             $thisfile_mpeg_audio_lame_rgad['peak_db'] = 20 * log10($thisfile_mpeg_audio_lame_rgad['peak_amplitude']);
rlm@3:                         }
rlm@3: 
rlm@3:                         $thisfile_mpeg_audio_lame_raw['RGAD_track']      =   getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xAB, 2));
rlm@3:                         $thisfile_mpeg_audio_lame_raw['RGAD_album']      =   getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xAD, 2));
rlm@3: 
rlm@3: 
rlm@3:                         if ($thisfile_mpeg_audio_lame_raw['RGAD_track'] != 0) {
rlm@3: 
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_track['raw']['name']        = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0xE000) >> 13;
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_track['raw']['originator']  = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x1C00) >> 10;
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_track['raw']['sign_bit']    = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x0200) >> 9;
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_track['raw']['gain_adjust'] =  $thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x01FF;
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_track['name']       = getid3_lib_replaygain::NameLookup($thisfile_mpeg_audio_lame_rgad_track['raw']['name']);
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_track['originator'] = getid3_lib_replaygain::OriginatorLookup($thisfile_mpeg_audio_lame_rgad_track['raw']['originator']);
rlm@3:                             $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']);
rlm@3: 
rlm@3:                             if (!empty($thisfile_mpeg_audio_lame_rgad['peak_amplitude'])) {
rlm@3:                                 $info['replay_gain']['track']['peak']   = $thisfile_mpeg_audio_lame_rgad['peak_amplitude'];
rlm@3:                             }
rlm@3:                             $info['replay_gain']['track']['originator'] = $thisfile_mpeg_audio_lame_rgad_track['originator'];
rlm@3:                             $info['replay_gain']['track']['adjustment'] = $thisfile_mpeg_audio_lame_rgad_track['gain_db'];
rlm@3:                         } else {
rlm@3:                             unset($thisfile_mpeg_audio_lame_rgad['track']);
rlm@3:                         }
rlm@3:                         if ($thisfile_mpeg_audio_lame_raw['RGAD_album'] != 0) {
rlm@3: 
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_album['raw']['name']        = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0xE000) >> 13;
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_album['raw']['originator']  = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x1C00) >> 10;
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_album['raw']['sign_bit']    = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x0200) >> 9;
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_album['raw']['gain_adjust'] =  $thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x01FF;
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_album['name']       = getid3_lib_replaygain::NameLookup($thisfile_mpeg_audio_lame_rgad_album['raw']['name']);
rlm@3:                             $thisfile_mpeg_audio_lame_rgad_album['originator'] = getid3_lib_replaygain::OriginatorLookup($thisfile_mpeg_audio_lame_rgad_album['raw']['originator']);
rlm@3:                             $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']);
rlm@3: 
rlm@3:                             if (!empty($thisfile_mpeg_audio_lame_rgad['peak_amplitude'])) {
rlm@3:                                 $info['replay_gain']['album']['peak']   = $thisfile_mpeg_audio_lame_rgad['peak_amplitude'];
rlm@3:                             }
rlm@3:                             $info['replay_gain']['album']['originator'] = $thisfile_mpeg_audio_lame_rgad_album['originator'];
rlm@3:                             $info['replay_gain']['album']['adjustment'] = $thisfile_mpeg_audio_lame_rgad_album['gain_db'];
rlm@3:                         } else {
rlm@3:                             unset($thisfile_mpeg_audio_lame_rgad['album']);
rlm@3:                         }
rlm@3:                         if (empty($thisfile_mpeg_audio_lame_rgad)) {
rlm@3:                             unset($thisfile_mpeg_audio_lame['RGAD']);
rlm@3:                         }
rlm@3: 
rlm@3: 
rlm@3:                         // byte $AF  Encoding flags + ATH Type
rlm@3:                         $encoding_flags_ath_type = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xAF, 1));
rlm@3:                         $thisfile_mpeg_audio_lame['encoding_flags']['nspsytune']   = (bool) ($encoding_flags_ath_type & 0x10);
rlm@3:                         $thisfile_mpeg_audio_lame['encoding_flags']['nssafejoint'] = (bool) ($encoding_flags_ath_type & 0x20);
rlm@3:                         $thisfile_mpeg_audio_lame['encoding_flags']['nogap_next']  = (bool) ($encoding_flags_ath_type & 0x40);
rlm@3:                         $thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev']  = (bool) ($encoding_flags_ath_type & 0x80);
rlm@3:                         $thisfile_mpeg_audio_lame['ath_type']                      =         $encoding_flags_ath_type & 0x0F;
rlm@3: 
rlm@3:                         // byte $B0  if ABR {specified bitrate} else {minimal bitrate}
rlm@3:                         $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xB0, 1));
rlm@3:                         if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 2) { // Average BitRate (ABR)
rlm@3:                             $thisfile_mpeg_audio_lame['bitrate_abr'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
rlm@3:                         } elseif ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { // Constant BitRate (CBR)
rlm@3:                             // ignore
rlm@3:                         } elseif ($thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] > 0) { // Variable BitRate (VBR) - minimum bitrate
rlm@3:                             $thisfile_mpeg_audio_lame['bitrate_min'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
rlm@3:                         }
rlm@3: 
rlm@3:                         // bytes $B1-$B3  Encoder delays
rlm@3:                         $encoder_delays = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xB1, 3));
rlm@3:                         $thisfile_mpeg_audio_lame['encoder_delay'] = ($encoder_delays & 0xFFF000) >> 12;
rlm@3:                         $thisfile_mpeg_audio_lame['end_padding']   =  $encoder_delays & 0x000FFF;
rlm@3: 
rlm@3:                         // byte $B4  Misc
rlm@3:                         $misc_byte = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xB4, 1));
rlm@3:                         $thisfile_mpeg_audio_lame_raw['noise_shaping']       = ($misc_byte & 0x03);
rlm@3:                         $thisfile_mpeg_audio_lame_raw['stereo_mode']         = ($misc_byte & 0x1C) >> 2;
rlm@3:                         $thisfile_mpeg_audio_lame_raw['not_optimal_quality'] = ($misc_byte & 0x20) >> 5;
rlm@3:                         $thisfile_mpeg_audio_lame_raw['source_sample_freq']  = ($misc_byte & 0xC0) >> 6;
rlm@3:                         $thisfile_mpeg_audio_lame['noise_shaping']           = $thisfile_mpeg_audio_lame_raw['noise_shaping'];
rlm@3:                         $thisfile_mpeg_audio_lame['stereo_mode']             = getid3_mp3::LAMEmiscStereoModeLookup($thisfile_mpeg_audio_lame_raw['stereo_mode']);
rlm@3:                         $thisfile_mpeg_audio_lame['not_optimal_quality']     = (bool) $thisfile_mpeg_audio_lame_raw['not_optimal_quality'];
rlm@3:                         $thisfile_mpeg_audio_lame['source_sample_freq']      = getid3_mp3::LAMEmiscSourceSampleFrequencyLookup($thisfile_mpeg_audio_lame_raw['source_sample_freq']);
rlm@3: 
rlm@3:                         // byte $B5  MP3 Gain
rlm@3:                         $thisfile_mpeg_audio_lame_raw['mp3_gain']   = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xB5, 1), false, true);
rlm@3:                         $thisfile_mpeg_audio_lame['mp3_gain_db']     = (20 * log10(2) / 4) * $thisfile_mpeg_audio_lame_raw['mp3_gain'];
rlm@3:                         $thisfile_mpeg_audio_lame['mp3_gain_factor'] = pow(2, ($thisfile_mpeg_audio_lame['mp3_gain_db'] / 6));
rlm@3: 
rlm@3:                         // bytes $B6-$B7  Preset and surround info
rlm@3:                         $PresetSurroundBytes = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xB6, 2));
rlm@3:                         // Reserved                                                    = ($PresetSurroundBytes & 0xC000);
rlm@3:                         $thisfile_mpeg_audio_lame_raw['surround_info'] = ($PresetSurroundBytes & 0x3800);
rlm@3:                         $thisfile_mpeg_audio_lame['surround_info']     = getid3_mp3::LAMEsurroundInfoLookup($thisfile_mpeg_audio_lame_raw['surround_info']);
rlm@3:                         $thisfile_mpeg_audio_lame['preset_used_id']    = ($PresetSurroundBytes & 0x07FF);
rlm@3:                         $thisfile_mpeg_audio_lame['preset_used']       = getid3_mp3::LAMEpresetUsedLookup($thisfile_mpeg_audio_lame);
rlm@3:                         if (!empty($thisfile_mpeg_audio_lame['preset_used_id']) && empty($thisfile_mpeg_audio_lame['preset_used'])) {
rlm@3:                             $this->getid3->warning('Unknown LAME preset used ('.$thisfile_mpeg_audio_lame['preset_used_id'].') - please report to info@getid3.org');
rlm@3:                         }
rlm@3:                         if (($thisfile_mpeg_audio_lame['short_version'] == 'LAME3.90.') && !empty($thisfile_mpeg_audio_lame['preset_used_id'])) {
rlm@3:                             // this may change if 3.90.4 ever comes out
rlm@3:                             $thisfile_mpeg_audio_lame['short_version'] = 'LAME3.90.3';
rlm@3:                         }
rlm@3: 
rlm@3:                         // bytes $B8-$BB  MusicLength
rlm@3:                         $thisfile_mpeg_audio_lame['audio_bytes'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xB8, 4));
rlm@3:                         $expected_number_of_audio_bytes = (($thisfile_mpeg_audio_lame['audio_bytes'] > 0) ? $thisfile_mpeg_audio_lame['audio_bytes'] : $thisfile_mpeg_audio['VBR_bytes']);
rlm@3: 
rlm@3:                         // bytes $BC-$BD  MusicCRC
rlm@3:                         $thisfile_mpeg_audio_lame['music_crc']    = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xBC, 2));
rlm@3: 
rlm@3:                         // bytes $BE-$BF  CRC-16 of Info Tag
rlm@3:                         $thisfile_mpeg_audio_lame['lame_tag_crc'] = getid3_lib::BigEndian2Int(substr($header_string, $lame_tag_offset_contant + 0xBE, 2));
rlm@3: 
rlm@3: 
rlm@3:                         // LAME CBR
rlm@3:                         if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) {
rlm@3: 
rlm@3:                             $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
rlm@3:                             $thisfile_mpeg_audio['bitrate'] = getid3_mp3::ClosestStandardMP3Bitrate($thisfile_mpeg_audio['bitrate']);
rlm@3:                             $info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
rlm@3: 
rlm@3:                         }
rlm@3: 
rlm@3:                     }
rlm@3:                 }
rlm@3: 
rlm@3:             } else {
rlm@3: 
rlm@3:                 // not Fraunhofer or Xing VBR methods, most likely CBR (but could be VBR with no header)
rlm@3:                 $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
rlm@3:                 if ($recursive_search) {
rlm@3:                     $thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
rlm@3:                     if (getid3_mp3::RecursiveFrameScanning($fd, $info, $offset, $next_frame_test_offset, true)) {
rlm@3:                         $recursive_search = false;
rlm@3:                         $thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
rlm@3:                     }
rlm@3:                     if ($thisfile_mpeg_audio['bitrate_mode'] == 'vbr') {
rlm@3:                         $this->getid3->warning('VBR file with no VBR header. Bitrate values calculated from actual frame bitrates.');
rlm@3:                     }
rlm@3:                 }
rlm@3: 
rlm@3:             }
rlm@3: 
rlm@3:         }
rlm@3: 
rlm@3:         if (($expected_number_of_audio_bytes > 0) && ($expected_number_of_audio_bytes != ($info['avdataend'] - $info['avdataoffset']))) {
rlm@3:             if ($expected_number_of_audio_bytes > ($info['avdataend'] - $info['avdataoffset'])) {
rlm@3:                 if (($expected_number_of_audio_bytes - ($info['avdataend'] - $info['avdataoffset'])) == 1) {
rlm@3:                     $this->getid3->warning('Last byte of data truncated (this is a known bug in Meracl ID3 Tag Writer before v1.3.5)');
rlm@3:                 } else {
rlm@3:                     $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)');
rlm@3:                 }
rlm@3:             } else {
rlm@3:                 if ((($info['avdataend'] - $info['avdataoffset']) - $expected_number_of_audio_bytes) == 1) {
rlm@3:                         $info['avdataend']--;
rlm@3:                 } else {
rlm@3:                     $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)');
rlm@3:                 }
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         if (($thisfile_mpeg_audio['bitrate'] == 'free') && empty($info['audio']['bitrate'])) {
rlm@3:             if (($offset == $info['avdataoffset']) && empty($thisfile_mpeg_audio['VBR_frames'])) {
rlm@3:                 $frame_byte_length = getid3_mp3::FreeFormatFrameLength($fd, $offset, $info, true);
rlm@3:                 if ($frame_byte_length > 0) {
rlm@3:                     $thisfile_mpeg_audio['framelength'] = $frame_byte_length;
rlm@3:                     if ($thisfile_mpeg_audio['layer'] == '1') {
rlm@3:                         // BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12
rlm@3:                         $info['audio']['bitrate'] = ((($frame_byte_length / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12;
rlm@3:                     } else {
rlm@3:                         // Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144
rlm@3:                         $info['audio']['bitrate'] = (($frame_byte_length - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144;
rlm@3:                     }
rlm@3:                 } else {
rlm@3:                     
rlm@3:                     // non-fatal error: Error calculating frame length of free-format MP3 without Xing/LAME header.
rlm@3:                     return;
rlm@3:                 }
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         if (@$thisfile_mpeg_audio['VBR_frames']) {
rlm@3:             switch ($thisfile_mpeg_audio['bitrate_mode']) {
rlm@3:                 case 'vbr':
rlm@3:                 case 'abr':
rlm@3:                     if (($thisfile_mpeg_audio['version'] == '1') && ($thisfile_mpeg_audio['layer'] == 1)) {
rlm@3:                         $thisfile_mpeg_audio['VBR_bitrate'] = ((@$thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames']) * 8) * ($info['audio']['sample_rate'] / 384);
rlm@3:                     } elseif ((($thisfile_mpeg_audio['version'] == '2') || ($thisfile_mpeg_audio['version'] == '2.5')) && ($thisfile_mpeg_audio['layer'] == 3)) {
rlm@3:                         $thisfile_mpeg_audio['VBR_bitrate'] = ((@$thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames']) * 8) * ($info['audio']['sample_rate'] / 576);
rlm@3:                     } else {
rlm@3:                         $thisfile_mpeg_audio['VBR_bitrate'] = ((@$thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames']) * 8) * ($info['audio']['sample_rate'] / 1152);
rlm@3:                     }
rlm@3:                     if ($thisfile_mpeg_audio['VBR_bitrate'] > 0) {
rlm@3:                         $info['audio']['bitrate']         = $thisfile_mpeg_audio['VBR_bitrate'];
rlm@3:                         $thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio['VBR_bitrate']; // to avoid confusion
rlm@3:                     }
rlm@3:                     break;
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         // End variable-bitrate headers
rlm@3:         ////////////////////////////////////////////////////////////////////////////////////
rlm@3: 
rlm@3:         if ($recursive_search) {
rlm@3: 
rlm@3:             if (!getid3_mp3::RecursiveFrameScanning($fd, $info, $offset, $next_frame_test_offset, $scan_as_cbr)) {
rlm@3:                 return false;
rlm@3:             }
rlm@3: 
rlm@3:         }
rlm@3: 
rlm@3:         return true;
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     
rlm@3:     public function RecursiveFrameScanning(&$fd, &$info, &$offset, &$next_frame_test_offset, $scan_as_cbr) {
rlm@3:         for ($i = 0; $i < getid3_mp3::VALID_CHECK_FRAMES; $i++) {
rlm@3:             // check next getid3_mp3::VALID_CHECK_FRAMES frames for validity, to make sure we haven't run across a false synch
rlm@3:             if (($next_frame_test_offset + 4) >= $info['avdataend']) {
rlm@3:                 // end of file
rlm@3:                 return true;
rlm@3:             }
rlm@3: 
rlm@3:             $next_frame_test_array = array ('avdataend' => $info['avdataend'], 'avdataoffset' => $info['avdataoffset']);
rlm@3:             if ($this->decodeMPEGaudioHeader($fd, $next_frame_test_offset, $next_frame_test_array, false)) {
rlm@3:                 if ($scan_as_cbr) {
rlm@3:                     // force CBR mode, used for trying to pick out invalid audio streams with
rlm@3:                     // valid(?) VBR headers, or VBR streams with no VBR header
rlm@3:                     if (!isset($next_frame_test_array['mpeg']['audio']['bitrate']) || !isset($info['mpeg']['audio']['bitrate']) || ($next_frame_test_array['mpeg']['audio']['bitrate'] != $info['mpeg']['audio']['bitrate'])) {
rlm@3:                         return false;
rlm@3:                     }
rlm@3:                 }
rlm@3: 
rlm@3: 
rlm@3:                 // next frame is OK, get ready to check the one after that
rlm@3:                 if (isset($next_frame_test_array['mpeg']['audio']['framelength']) && ($next_frame_test_array['mpeg']['audio']['framelength'] > 0)) {
rlm@3:                     $next_frame_test_offset += $next_frame_test_array['mpeg']['audio']['framelength'];
rlm@3:                 } else {
rlm@3:                     
rlm@3:                     // non-fatal error: Frame at offset $offset has an invalid frame length.
rlm@3:                     return;
rlm@3:                 }
rlm@3: 
rlm@3:             } else {
rlm@3: 
rlm@3:                 // non-fatal error: Next frame is not valid.
rlm@3:                 return;
rlm@3:             }
rlm@3:         }
rlm@3:         return true;
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3:     
rlm@3:     public function FreeFormatFrameLength($fd, $offset, &$info, $deep_scan=false) {
rlm@3:         fseek($fd, $offset, SEEK_SET);
rlm@3:         $mpeg_audio_data = fread($fd, 32768);
rlm@3: 
rlm@3:         $sync_pattern1 = substr($mpeg_audio_data, 0, 4);
rlm@3:         // may be different pattern due to padding
rlm@3:         $sync_pattern2 = $sync_pattern1{0}.$sync_pattern1{1}.chr(ord($sync_pattern1{2}) | 0x02).$sync_pattern1{3};
rlm@3:         if ($sync_pattern2 === $sync_pattern1) {
rlm@3:             $sync_pattern2 = $sync_pattern1{0}.$sync_pattern1{1}.chr(ord($sync_pattern1{2}) & 0xFD).$sync_pattern1{3};
rlm@3:         }
rlm@3: 
rlm@3:         $frame_length = false;
rlm@3:         $frame_length1 = strpos($mpeg_audio_data, $sync_pattern1, 4);
rlm@3:         $frame_length2 = strpos($mpeg_audio_data, $sync_pattern2, 4);
rlm@3:         if ($frame_length1 > 4) {
rlm@3:             $frame_length = $frame_length1;
rlm@3:         }
rlm@3:         if (($frame_length2 > 4) && ($frame_length2 < $frame_length1)) {
rlm@3:             $frame_length = $frame_length2;
rlm@3:         }
rlm@3:         if (!$frame_length) {
rlm@3: 
rlm@3:             // LAME 3.88 has a different value for modeextension on the first frame vs the rest
rlm@3:             $frame_length1 = strpos($mpeg_audio_data, substr($sync_pattern1, 0, 3), 4);
rlm@3:             $frame_length2 = strpos($mpeg_audio_data, substr($sync_pattern2, 0, 3), 4);
rlm@3: 
rlm@3:             if ($frame_length1 > 4) {
rlm@3:                 $frame_length = $frame_length1;
rlm@3:             }
rlm@3:             if (($frame_length2 > 4) && ($frame_length2 < $frame_length1)) {
rlm@3:                 $frame_length = $frame_length2;
rlm@3:             }
rlm@3:             if (!$frame_length) {
rlm@3:                 throw new getid3_exception('Cannot find next free-format synch pattern ('.getid3_lib::PrintHexBytes($sync_pattern1).' or '.getid3_lib::PrintHexBytes($sync_pattern2).') after offset '.$offset);
rlm@3:             } else {
rlm@3:                 $this->getid3->warning('ModeExtension varies between first frame and other frames (known free-format issue in LAME 3.88)');
rlm@3:                 $info['audio']['codec']   = 'LAME';
rlm@3:                 $info['audio']['encoder'] = 'LAME3.88';
rlm@3:                 $sync_pattern1 = substr($sync_pattern1, 0, 3);
rlm@3:                 $sync_pattern2 = substr($sync_pattern2, 0, 3);
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         if ($deep_scan) {
rlm@3: 
rlm@3:             $actual_frame_length_values = array ();
rlm@3:             $next_offset = $offset + $frame_length;
rlm@3:             while ($next_offset < ($info['avdataend'] - 6)) {
rlm@3:                 fseek($fd, $next_offset - 1, SEEK_SET);
rlm@3:                 $NextSyncPattern = fread($fd, 6);
rlm@3:                 if ((substr($NextSyncPattern, 1, strlen($sync_pattern1)) == $sync_pattern1) || (substr($NextSyncPattern, 1, strlen($sync_pattern2)) == $sync_pattern2)) {
rlm@3:                     // good - found where expected
rlm@3:                     $actual_frame_length_values[] = $frame_length;
rlm@3:                 } elseif ((substr($NextSyncPattern, 0, strlen($sync_pattern1)) == $sync_pattern1) || (substr($NextSyncPattern, 0, strlen($sync_pattern2)) == $sync_pattern2)) {
rlm@3:                     // ok - found one byte earlier than expected (last frame wasn't padded, first frame was)
rlm@3:                     $actual_frame_length_values[] = ($frame_length - 1);
rlm@3:                     $next_offset--;
rlm@3:                 } elseif ((substr($NextSyncPattern, 2, strlen($sync_pattern1)) == $sync_pattern1) || (substr($NextSyncPattern, 2, strlen($sync_pattern2)) == $sync_pattern2)) {
rlm@3:                     // ok - found one byte later than expected (last frame was padded, first frame wasn't)
rlm@3:                     $actual_frame_length_values[] = ($frame_length + 1);
rlm@3:                     $next_offset++;
rlm@3:                 } else {
rlm@3:                     throw new getid3_exception('Did not find expected free-format sync pattern at offset '.$next_offset);
rlm@3:                 }
rlm@3:                 $next_offset += $frame_length;
rlm@3:             }
rlm@3:             if (count($actual_frame_length_values) > 0) {
rlm@3:                 $frame_length = intval(round(array_sum($actual_frame_length_values) / count($actual_frame_length_values)));
rlm@3:             }
rlm@3:         }
rlm@3:         return $frame_length;
rlm@3:     }
rlm@3: 
rlm@3:     
rlm@3: 
rlm@3:     public function getOnlyMPEGaudioInfo($fd, &$info, $avdata_offset, $bit_rate_histogram=false) {
rlm@3: 
rlm@3:         // looks for synch, decodes MPEG audio header
rlm@3:      
rlm@3:         fseek($fd, $avdata_offset, SEEK_SET);
rlm@3:         
rlm@3:         $sync_seek_buffer_size = min(128 * 1024, $info['avdataend'] - $avdata_offset);
rlm@3:         $header = fread($fd, $sync_seek_buffer_size);
rlm@3:         $sync_seek_buffer_size = strlen($header);
rlm@3:         $synch_seek_offset = 0;
rlm@3:         
rlm@3:         static $mpeg_audio_version_lookup;
rlm@3:         static $mpeg_audio_layer_lookup;
rlm@3:         static $mpeg_audio_bitrate_lookup;
rlm@3:         if (empty($mpeg_audio_version_lookup)) {
rlm@3:             $mpeg_audio_version_lookup = getid3_mp3::MPEGaudioVersionarray();
rlm@3:             $mpeg_audio_layer_lookup   = getid3_mp3::MPEGaudioLayerarray();
rlm@3:             $mpeg_audio_bitrate_lookup = getid3_mp3::MPEGaudioBitratearray();
rlm@3: 
rlm@3:         }
rlm@3: 
rlm@3:         while ($synch_seek_offset < $sync_seek_buffer_size) {
rlm@3:      
rlm@3:             if ((($avdata_offset + $synch_seek_offset)  < $info['avdataend']) && !feof($fd)) {
rlm@3:      
rlm@3:                 // if a synch's not found within the first 128k bytes, then give up               
rlm@3:                 if ($synch_seek_offset > $sync_seek_buffer_size) {
rlm@3:                     throw new getid3_exception('Could not find valid MPEG audio synch within the first '.round($sync_seek_buffer_size / 1024).'kB');
rlm@3:                 } 
rlm@3:               
rlm@3:                 if (feof($fd)) {
rlm@3:                     throw new getid3_exception('Could not find valid MPEG audio synch before end of file');
rlm@3:                 }
rlm@3:             }
rlm@3:      
rlm@3:            if (($synch_seek_offset + 1) >= strlen($header)) {
rlm@3:                 throw new getid3_exception('Could not find valid MPEG synch before end of file');
rlm@3:            }
rlm@3:      
rlm@3:            if (($header{$synch_seek_offset} == "\xFF") && ($header{($synch_seek_offset + 1)} > "\xE0")) { // synch detected 
rlm@3: 
rlm@3:                 if (!isset($first_frame_info) && !isset($info['mpeg']['audio'])) {
rlm@3:                     $first_frame_info = $info;
rlm@3:                     $first_frame_avdata_offset = $avdata_offset + $synch_seek_offset;
rlm@3:                     if (!getid3_mp3::decodeMPEGaudioHeader($fd, $avdata_offset + $synch_seek_offset, $first_frame_info, false)) {
rlm@3:                         // if this is the first valid MPEG-audio frame, save it in case it's a VBR header frame and there's
rlm@3:                         // garbage between this frame and a valid sequence of MPEG-audio frames, to be restored below
rlm@3:                         unset($first_frame_info);
rlm@3:                     }
rlm@3:                 }
rlm@3: 
rlm@3:                 $dummy = $info; // only overwrite real data if valid header found
rlm@3:                 if (getid3_mp3::decodeMPEGaudioHeader($fd, $avdata_offset + $synch_seek_offset, $dummy, true)) {
rlm@3:                     $info = $dummy;
rlm@3:                     $info['avdataoffset'] = $avdata_offset + $synch_seek_offset;
rlm@3:                     
rlm@3:                     switch (@$info['fileformat']) {
rlm@3:                         case '':
rlm@3:                         case 'mp3':
rlm@3:                             $info['fileformat']          = 'mp3';
rlm@3:                             $info['audio']['dataformat'] = 'mp3';
rlm@3:                             break;
rlm@3:                     }
rlm@3:                     if (isset($first_frame_info['mpeg']['audio']['bitrate_mode']) && ($first_frame_info['mpeg']['audio']['bitrate_mode'] == 'vbr')) {
rlm@3:                         if (!(abs($info['audio']['bitrate'] - $first_frame_info['audio']['bitrate']) <= 1)) {
rlm@3:                             // If there is garbage data between a valid VBR header frame and a sequence
rlm@3:                             // of valid MPEG-audio frames the VBR data is no longer discarded.
rlm@3:                             $info = $first_frame_info;
rlm@3:                             $info['avdataoffset']        = $first_frame_avdata_offset;
rlm@3:                             $info['fileformat']          = 'mp3';
rlm@3:                             $info['audio']['dataformat'] = 'mp3';
rlm@3:                             $dummy                               = $info;
rlm@3:                             unset($dummy['mpeg']['audio']);
rlm@3:                             $GarbageOffsetStart = $first_frame_avdata_offset + $first_frame_info['mpeg']['audio']['framelength'];
rlm@3:                             $GarbageOffsetEnd   = $avdata_offset + $synch_seek_offset;
rlm@3:                             if (getid3_mp3::decodeMPEGaudioHeader($fd, $GarbageOffsetEnd, $dummy, true, true)) {
rlm@3: 
rlm@3:                                 $info = $dummy;
rlm@3:                                 $info['avdataoffset'] = $GarbageOffsetEnd;
rlm@3:                                 $this->getid3->warning('apparently-valid VBR header not used because could not find '.getid3_mp3::VALID_CHECK_FRAMES.' consecutive MPEG-audio frames immediately after VBR header (garbage data for '.($GarbageOffsetEnd - $GarbageOffsetStart).' bytes between '.$GarbageOffsetStart.' and '.$GarbageOffsetEnd.'), but did find valid CBR stream starting at '.$GarbageOffsetEnd);
rlm@3: 
rlm@3:                             } else {
rlm@3: 
rlm@3:                                 $this->getid3->warning('using data from VBR header even though could not find '.getid3_mp3::VALID_CHECK_FRAMES.' consecutive MPEG-audio frames immediately after VBR header (garbage data for '.($GarbageOffsetEnd - $GarbageOffsetStart).' bytes between '.$GarbageOffsetStart.' and '.$GarbageOffsetEnd.')');
rlm@3: 
rlm@3:                             }
rlm@3:                         }
rlm@3:                     }
rlm@3:                     if (isset($info['mpeg']['audio']['bitrate_mode']) && ($info['mpeg']['audio']['bitrate_mode'] == 'vbr') && !isset($info['mpeg']['audio']['VBR_method'])) {
rlm@3:                         // VBR file with no VBR header
rlm@3:                         $bit_rate_histogram = true;
rlm@3:                     }
rlm@3: 
rlm@3:                     if ($bit_rate_histogram) {
rlm@3: 
rlm@3:                         $info['mpeg']['audio']['stereo_distribution']  = array ('stereo'=>0, 'joint stereo'=>0, 'dual channel'=>0, 'mono'=>0);
rlm@3:                         $info['mpeg']['audio']['version_distribution'] = array ('1'=>0, '2'=>0, '2.5'=>0);
rlm@3: 
rlm@3:                         if ($info['mpeg']['audio']['version'] == '1') {
rlm@3:                             if ($info['mpeg']['audio']['layer'] == 3) {
rlm@3:                                 $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);
rlm@3:                             } elseif ($info['mpeg']['audio']['layer'] == 2) {
rlm@3:                                 $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);
rlm@3:                             } elseif ($info['mpeg']['audio']['layer'] == 1) {
rlm@3:                                 $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);
rlm@3:                             }
rlm@3:                         } elseif ($info['mpeg']['audio']['layer'] == 1) {
rlm@3:                             $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);
rlm@3:                         } else {
rlm@3:                             $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);
rlm@3:                         }
rlm@3: 
rlm@3:                         $dummy = array ('avdataend' => $info['avdataend'], 'avdataoffset' => $info['avdataoffset']);
rlm@3:                         $synch_start_offset = $info['avdataoffset'];
rlm@3: 
rlm@3:                         $fast_mode = false;
rlm@3:                         $synch_errors_found = 0;
rlm@3:                         while ($this->decodeMPEGaudioHeader($fd, $synch_start_offset, $dummy, false, false, $fast_mode)) {
rlm@3:                             $fast_mode = true;
rlm@3:                             $thisframebitrate = $mpeg_audio_bitrate_lookup[$mpeg_audio_version_lookup[$dummy['mpeg']['audio']['raw']['version']]][$mpeg_audio_layer_lookup[$dummy['mpeg']['audio']['raw']['layer']]][$dummy['mpeg']['audio']['raw']['bitrate']];
rlm@3: 
rlm@3:                             if (empty($dummy['mpeg']['audio']['framelength'])) {
rlm@3:                                 $synch_errors_found++;
rlm@3:                             } 
rlm@3:                             else {
rlm@3:                                 @$info['mpeg']['audio']['bitrate_distribution'][$thisframebitrate]++;
rlm@3:                                 @$info['mpeg']['audio']['stereo_distribution'][$dummy['mpeg']['audio']['channelmode']]++;
rlm@3:                                 @$info['mpeg']['audio']['version_distribution'][$dummy['mpeg']['audio']['version']]++;
rlm@3: 
rlm@3:                                 $synch_start_offset += $dummy['mpeg']['audio']['framelength'];
rlm@3:                             }
rlm@3:                         }
rlm@3:                         if ($synch_errors_found > 0) {
rlm@3:                             $this->getid3->warning('Found '.$synch_errors_found.' synch errors in histogram analysis');
rlm@3:                         }
rlm@3: 
rlm@3:                         $bit_total     = 0;
rlm@3:                         $frame_counter = 0;
rlm@3:                         foreach ($info['mpeg']['audio']['bitrate_distribution'] as $bit_rate_value => $bit_rate_count) {
rlm@3:                             $frame_counter += $bit_rate_count;
rlm@3:                             if ($bit_rate_value != 'free') {
rlm@3:                                 $bit_total += ($bit_rate_value * $bit_rate_count);
rlm@3:                             }
rlm@3:                         }
rlm@3:                         if ($frame_counter == 0) {
rlm@3:                             throw new getid3_exception('Corrupt MP3 file: framecounter == zero');
rlm@3:                         }
rlm@3:                         $info['mpeg']['audio']['frame_count'] = $frame_counter;
rlm@3:                         $info['mpeg']['audio']['bitrate']     = ($bit_total / $frame_counter);
rlm@3: 
rlm@3:                         $info['audio']['bitrate'] = $info['mpeg']['audio']['bitrate'];
rlm@3: 
rlm@3: 
rlm@3:                         // Definitively set VBR vs CBR, even if the Xing/LAME/VBRI header says differently
rlm@3:                         $distinct_bit_rates = 0;
rlm@3:                         foreach ($info['mpeg']['audio']['bitrate_distribution'] as $bit_rate_value => $bit_rate_count) {
rlm@3:                             if ($bit_rate_count > 0) {
rlm@3:                                 $distinct_bit_rates++;
rlm@3:                             }
rlm@3:                         }
rlm@3:                         if ($distinct_bit_rates > 1) {
rlm@3:                             $info['mpeg']['audio']['bitrate_mode'] = 'vbr';
rlm@3:                         } else {
rlm@3:                             $info['mpeg']['audio']['bitrate_mode'] = 'cbr';
rlm@3:                         }
rlm@3:                         $info['audio']['bitrate_mode'] = $info['mpeg']['audio']['bitrate_mode'];
rlm@3: 
rlm@3:                     }
rlm@3: 
rlm@3:                     break; // exit while()
rlm@3:                 }
rlm@3:             }
rlm@3: 
rlm@3:             $synch_seek_offset++;
rlm@3:             if (($avdata_offset + $synch_seek_offset) >= $info['avdataend']) {
rlm@3:                 // end of file/data
rlm@3: 
rlm@3:                 if (empty($info['mpeg']['audio'])) {
rlm@3: 
rlm@3:                     throw new getid3_exception('could not find valid MPEG synch before end of file');
rlm@3:                 }
rlm@3:                 break;
rlm@3:             }
rlm@3: 
rlm@3:         }
rlm@3:         
rlm@3:         $info['audio']['channels']        = $info['mpeg']['audio']['channels'];
rlm@3:         $info['audio']['channelmode']     = $info['mpeg']['audio']['channelmode'];
rlm@3:         $info['audio']['sample_rate']     = $info['mpeg']['audio']['sample_rate'];
rlm@3:         return true;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioVersionarray() {
rlm@3:         
rlm@3:         static $array = array ('2.5', false, '2', '1');
rlm@3:         return $array;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioLayerarray() {
rlm@3:         
rlm@3:         static $array = array (false, 3, 2, 1);
rlm@3:         return $array;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioBitratearray() {
rlm@3:         
rlm@3:         static $array;
rlm@3:         if (empty($array)) {
rlm@3:             $array = array (
rlm@3:                 '1'  =>  array (1 => array ('free', 32000, 64000, 96000, 128000, 160000, 192000, 224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000),
rlm@3:                                 2 => array ('free', 32000, 48000, 56000,  64000,  80000,  96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000),
rlm@3:                                 3 => array ('free', 32000, 40000, 48000,  56000,  64000,  80000,  96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000)
rlm@3:                                ),
rlm@3: 
rlm@3:                 '2'  =>  array (1 => array ('free', 32000, 48000, 56000,  64000,  80000,  96000, 112000, 128000, 144000, 160000, 176000, 192000, 224000, 256000),
rlm@3:                                 2 => array ('free',  8000, 16000, 24000,  32000,  40000,  48000,  56000,  64000,  80000,  96000, 112000, 128000, 144000, 160000),
rlm@3:                                )
rlm@3:             );
rlm@3:             $array['2'][3] = $array['2'][2];
rlm@3:             $array['2.5']  = $array['2'];
rlm@3:         }
rlm@3:         return $array;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioFrequencyarray() {
rlm@3:         
rlm@3:         static $array = array (
rlm@3:                 '1'   => array (44100, 48000, 32000),
rlm@3:                 '2'   => array (22050, 24000, 16000),
rlm@3:                 '2.5' => array (11025, 12000,  8000)
rlm@3:         );
rlm@3:         return $array;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioChannelModearray() {
rlm@3:         
rlm@3:         static $array = array ('stereo', 'joint stereo', 'dual channel', 'mono');
rlm@3:         return $array;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioModeExtensionarray() {
rlm@3:         
rlm@3:         static $array = array (
rlm@3:                 1 => array ('4-31', '8-31', '12-31', '16-31'),
rlm@3:                 2 => array ('4-31', '8-31', '12-31', '16-31'),
rlm@3:                 3 => array ('', 'IS', 'MS', 'IS+MS')
rlm@3:         );
rlm@3:         return $array;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioEmphasisarray() {
rlm@3:         
rlm@3:         static $array = array ('none', '50/15ms', false, 'CCIT J.17');
rlm@3:         return $array;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioHeaderBytesValid($head4, $allow_bitrate_15=false) {
rlm@3:         
rlm@3:         return getid3_mp3::MPEGaudioHeaderValid(getid3_mp3::MPEGaudioHeaderDecode($head4), false, $allow_bitrate_15);
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioHeaderValid($raw_array, $echo_errors=false, $allow_bitrate_15=false) {
rlm@3:         
rlm@3:         if (($raw_array['synch'] & 0x0FFE) != 0x0FFE) {
rlm@3:             return false;
rlm@3:         }
rlm@3: 
rlm@3:         static $mpeg_audio_version_lookup;
rlm@3:         static $mpeg_audio_layer_lookup;
rlm@3:         static $mpeg_audio_bitrate_lookup;
rlm@3:         static $mpeg_audio_frequency_lookup;
rlm@3:         static $mpeg_audio_channel_mode_lookup;
rlm@3:         static $mpeg_audio_mode_extension_lookup;
rlm@3:         static $mpeg_audio_emphasis_lookup;
rlm@3:         if (empty($mpeg_audio_version_lookup)) {
rlm@3:             $mpeg_audio_version_lookup        = getid3_mp3::MPEGaudioVersionarray();
rlm@3:             $mpeg_audio_layer_lookup          = getid3_mp3::MPEGaudioLayerarray();
rlm@3:             $mpeg_audio_bitrate_lookup        = getid3_mp3::MPEGaudioBitratearray();
rlm@3:             $mpeg_audio_frequency_lookup      = getid3_mp3::MPEGaudioFrequencyarray();
rlm@3:             $mpeg_audio_channel_mode_lookup   = getid3_mp3::MPEGaudioChannelModearray();
rlm@3:             $mpeg_audio_mode_extension_lookup = getid3_mp3::MPEGaudioModeExtensionarray();
rlm@3:             $mpeg_audio_emphasis_lookup       = getid3_mp3::MPEGaudioEmphasisarray();
rlm@3:         }
rlm@3: 
rlm@3:         if (isset($mpeg_audio_version_lookup[$raw_array['version']])) {
rlm@3:             $decodedVersion = $mpeg_audio_version_lookup[$raw_array['version']];
rlm@3:         } else {
rlm@3:             echo ($echo_errors ? "\n".'invalid Version ('.$raw_array['version'].')' : '');
rlm@3:             return false;
rlm@3:         }
rlm@3:         if (isset($mpeg_audio_layer_lookup[$raw_array['layer']])) {
rlm@3:             $decodedLayer = $mpeg_audio_layer_lookup[$raw_array['layer']];
rlm@3:         } else {
rlm@3:             echo ($echo_errors ? "\n".'invalid Layer ('.$raw_array['layer'].')' : '');
rlm@3:             return false;
rlm@3:         }
rlm@3:         if (!isset($mpeg_audio_bitrate_lookup[$decodedVersion][$decodedLayer][$raw_array['bitrate']])) {
rlm@3:             echo ($echo_errors ? "\n".'invalid Bitrate ('.$raw_array['bitrate'].')' : '');
rlm@3:             if ($raw_array['bitrate'] == 15) {
rlm@3:                 // known issue in LAME 3.90 - 3.93.1 where free-format has bitrate ID of 15 instead of 0
rlm@3:                 // let it go through here otherwise file will not be identified
rlm@3:                 if (!$allow_bitrate_15) {
rlm@3:                     return false;
rlm@3:                 }
rlm@3:             } else {
rlm@3:                 return false;
rlm@3:             }
rlm@3:         }
rlm@3:         if (!isset($mpeg_audio_frequency_lookup[$decodedVersion][$raw_array['sample_rate']])) {
rlm@3:             echo ($echo_errors ? "\n".'invalid Frequency ('.$raw_array['sample_rate'].')' : '');
rlm@3:             return false;
rlm@3:         }
rlm@3:         if (!isset($mpeg_audio_channel_mode_lookup[$raw_array['channelmode']])) {
rlm@3:             echo ($echo_errors ? "\n".'invalid ChannelMode ('.$raw_array['channelmode'].')' : '');
rlm@3:             return false;
rlm@3:         }
rlm@3:         if (!isset($mpeg_audio_mode_extension_lookup[$decodedLayer][$raw_array['modeextension']])) {
rlm@3:             echo ($echo_errors ? "\n".'invalid Mode Extension ('.$raw_array['modeextension'].')' : '');
rlm@3:             return false;
rlm@3:         }
rlm@3:         if (!isset($mpeg_audio_emphasis_lookup[$raw_array['emphasis']])) {
rlm@3:             echo ($echo_errors ? "\n".'invalid Emphasis ('.$raw_array['emphasis'].')' : '');
rlm@3:             return false;
rlm@3:         }
rlm@3:         // These are just either set or not set, you can't mess that up :)
rlm@3:         // $raw_array['protection'];
rlm@3:         // $raw_array['padding'];
rlm@3:         // $raw_array['private'];
rlm@3:         // $raw_array['copyright'];
rlm@3:         // $raw_array['original'];
rlm@3: 
rlm@3:         return true;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioHeaderDecode($header_four_bytes) {
rlm@3:         // AAAA AAAA  AAAB BCCD  EEEE FFGH  IIJJ KLMM
rlm@3:         // A - Frame sync (all bits set)
rlm@3:         // B - MPEG Audio version ID
rlm@3:         // C - Layer description
rlm@3:         // D - Protection bit
rlm@3:         // E - Bitrate index
rlm@3:         // F - Sampling rate frequency index
rlm@3:         // G - Padding bit
rlm@3:         // H - Private bit
rlm@3:         // I - Channel Mode
rlm@3:         // J - Mode extension (Only if Joint stereo)
rlm@3:         // K - Copyright
rlm@3:         // L - Original
rlm@3:         // M - Emphasis
rlm@3: 
rlm@3:         if (strlen($header_four_bytes) != 4) {
rlm@3:             return false;
rlm@3:         }
rlm@3: 
rlm@3:         $mpeg_raw_header['synch']         = (getid3_lib::BigEndian2Int(substr($header_four_bytes, 0, 2)) & 0xFFE0) >> 4;
rlm@3:         $mpeg_raw_header['version']       = (ord($header_four_bytes{1}) & 0x18) >> 3; //    BB
rlm@3:         $mpeg_raw_header['layer']         = (ord($header_four_bytes{1}) & 0x06) >> 1; //      CC
rlm@3:         $mpeg_raw_header['protection']    = (ord($header_four_bytes{1}) & 0x01);      //        D
rlm@3:         $mpeg_raw_header['bitrate']       = (ord($header_four_bytes{2}) & 0xF0) >> 4; // EEEE
rlm@3:         $mpeg_raw_header['sample_rate']   = (ord($header_four_bytes{2}) & 0x0C) >> 2; //     FF
rlm@3:         $mpeg_raw_header['padding']       = (ord($header_four_bytes{2}) & 0x02) >> 1; //       G
rlm@3:         $mpeg_raw_header['private']       = (ord($header_four_bytes{2}) & 0x01);      //        H
rlm@3:         $mpeg_raw_header['channelmode']   = (ord($header_four_bytes{3}) & 0xC0) >> 6; // II
rlm@3:         $mpeg_raw_header['modeextension'] = (ord($header_four_bytes{3}) & 0x30) >> 4; //   JJ
rlm@3:         $mpeg_raw_header['copyright']     = (ord($header_four_bytes{3}) & 0x08) >> 3; //     K
rlm@3:         $mpeg_raw_header['original']      = (ord($header_four_bytes{3}) & 0x04) >> 2; //      L
rlm@3:         $mpeg_raw_header['emphasis']      = (ord($header_four_bytes{3}) & 0x03);      //       MM
rlm@3: 
rlm@3:         return $mpeg_raw_header;
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function MPEGaudioFrameLength(&$bit_rate, &$version, &$layer, $padding, &$sample_rate) {
rlm@3:         
rlm@3:         if (!isset($cache[$bit_rate][$version][$layer][$padding][$sample_rate])) {
rlm@3:             $cache[$bit_rate][$version][$layer][$padding][$sample_rate] = false;
rlm@3:             if ($bit_rate != 'free') {
rlm@3: 
rlm@3:                 if ($version == '1') {
rlm@3: 
rlm@3:                     if ($layer == '1') {
rlm@3: 
rlm@3:                         // For Layer I slot is 32 bits long
rlm@3:                         $frame_length_coefficient = 48;
rlm@3:                         $slot_length = 4;
rlm@3: 
rlm@3:                     } else { // Layer 2 / 3
rlm@3: 
rlm@3:                         // for Layer 2 and Layer 3 slot is 8 bits long.
rlm@3:                         $frame_length_coefficient = 144;
rlm@3:                         $slot_length = 1;
rlm@3: 
rlm@3:                     }
rlm@3: 
rlm@3:                 } else { // MPEG-2 / MPEG-2.5
rlm@3: 
rlm@3:                     if ($layer == '1') {
rlm@3: 
rlm@3:                         // For Layer I slot is 32 bits long
rlm@3:                         $frame_length_coefficient = 24;
rlm@3:                         $slot_length = 4;
rlm@3: 
rlm@3:                     } elseif ($layer == '2') {
rlm@3: 
rlm@3:                         // for Layer 2 and Layer 3 slot is 8 bits long.
rlm@3:                         $frame_length_coefficient = 144;
rlm@3:                         $slot_length = 1;
rlm@3: 
rlm@3:                     } else { // layer 3
rlm@3: 
rlm@3:                         // for Layer 2 and Layer 3 slot is 8 bits long.
rlm@3:                         $frame_length_coefficient = 72;
rlm@3:                         $slot_length = 1;
rlm@3: 
rlm@3:                     }
rlm@3: 
rlm@3:                 }
rlm@3: 
rlm@3:                 // FrameLengthInBytes = ((Coefficient * BitRate) / SampleRate) + Padding
rlm@3:                 if ($sample_rate > 0) {
rlm@3:                     $new_frame_length  = ($frame_length_coefficient * $bit_rate) / $sample_rate;
rlm@3:                     $new_frame_length  = floor($new_frame_length / $slot_length) * $slot_length; // round to next-lower multiple of SlotLength (1 byte for Layer 2/3, 4 bytes for Layer I)
rlm@3:                     if ($padding) {
rlm@3:                         $new_frame_length += $slot_length;
rlm@3:                     }
rlm@3:                     $cache[$bit_rate][$version][$layer][$padding][$sample_rate] = (int) $new_frame_length;
rlm@3:                 }
rlm@3:             }
rlm@3:         }
rlm@3:         return $cache[$bit_rate][$version][$layer][$padding][$sample_rate];
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function ClosestStandardMP3Bitrate($bit_rate) {
rlm@3:         
rlm@3:         static $standard_bit_rates = array (320000, 256000, 224000, 192000, 160000, 128000, 112000, 96000, 80000, 64000, 56000, 48000, 40000, 32000, 24000, 16000, 8000);
rlm@3:         static $bit_rate_table = array (0=>'-');
rlm@3:         $round_bit_rate = intval(round($bit_rate, -3));
rlm@3:         if (!isset($bit_rate_table[$round_bit_rate])) {
rlm@3:             if ($round_bit_rate > 320000) {
rlm@3:                 $bit_rate_table[$round_bit_rate] = round($bit_rate, -4);
rlm@3:             } else {
rlm@3:                 $last_bit_rate = 320000;
rlm@3:                 foreach ($standard_bit_rates as $standard_bit_rate) {
rlm@3:                     $bit_rate_table[$round_bit_rate] = $standard_bit_rate;
rlm@3:                     if ($round_bit_rate >= $standard_bit_rate - (($last_bit_rate - $standard_bit_rate) / 2)) {
rlm@3:                         break;
rlm@3:                     }
rlm@3:                     $last_bit_rate = $standard_bit_rate;
rlm@3:                 }
rlm@3:             }
rlm@3:         }
rlm@3:         return $bit_rate_table[$round_bit_rate];
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function XingVBRidOffset($version, $channel_mode) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:                 '1'   => array ('mono'          => 0x15, // 4 + 17 = 21
rlm@3:                                 'stereo'        => 0x24, // 4 + 32 = 36
rlm@3:                                 'joint stereo'  => 0x24,
rlm@3:                                 'dual channel'  => 0x24
rlm@3:                                ),
rlm@3: 
rlm@3:                 '2'   => array ('mono'          => 0x0D, // 4 +  9 = 13
rlm@3:                                 'stereo'        => 0x15, // 4 + 17 = 21
rlm@3:                                 'joint stereo'  => 0x15,
rlm@3:                                 'dual channel'  => 0x15
rlm@3:                                ),
rlm@3: 
rlm@3:                 '2.5' => array ('mono'          => 0x15,
rlm@3:                                 'stereo'        => 0x15,
rlm@3:                                 'joint stereo'  => 0x15,
rlm@3:                                 'dual channel'  => 0x15
rlm@3:                                )
rlm@3:         );
rlm@3:         
rlm@3:         return $lookup[$version][$channel_mode];
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function LAMEvbrMethodLookup($vbr_method_id) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:             0x00 => 'unknown',
rlm@3:             0x01 => 'cbr',
rlm@3:             0x02 => 'abr',
rlm@3:             0x03 => 'vbr-old / vbr-rh',
rlm@3:             0x04 => 'vbr-new / vbr-mtrh',
rlm@3:             0x05 => 'vbr-mt',
rlm@3:             0x06 => 'Full VBR Method 4',
rlm@3:             0x08 => 'constant bitrate 2 pass',
rlm@3:             0x09 => 'abr 2 pass',
rlm@3:             0x0F => 'reserved'
rlm@3:         );
rlm@3:         return (isset($lookup[$vbr_method_id]) ? $lookup[$vbr_method_id] : '');
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function LAMEmiscStereoModeLookup($stereo_mode_id) {
rlm@3:     
rlm@3:         static $lookup = array (
rlm@3:             0 => 'mono',
rlm@3:             1 => 'stereo',
rlm@3:             2 => 'dual mono',
rlm@3:             3 => 'joint stereo',
rlm@3:             4 => 'forced stereo',
rlm@3:             5 => 'auto',
rlm@3:             6 => 'intensity stereo',
rlm@3:             7 => 'other'
rlm@3:         );
rlm@3:         return (isset($lookup[$stereo_mode_id]) ? $lookup[$stereo_mode_id] : '');
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function LAMEmiscSourceSampleFrequencyLookup($source_sample_frequency_id) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:             0 => '<= 32 kHz',
rlm@3:             1 => '44.1 kHz',
rlm@3:             2 => '48 kHz',
rlm@3:             3 => '> 48kHz'
rlm@3:         );
rlm@3:         return (isset($lookup[$source_sample_frequency_id]) ? $lookup[$source_sample_frequency_id] : '');
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function LAMEsurroundInfoLookup($surround_info_id) {
rlm@3:         
rlm@3:         static $lookup = array (
rlm@3:             0 => 'no surround info',
rlm@3:             1 => 'DPL encoding',
rlm@3:             2 => 'DPL2 encoding',
rlm@3:             3 => 'Ambisonic encoding'
rlm@3:         );
rlm@3:         return (isset($lookup[$surround_info_id]) ? $lookup[$surround_info_id] : 'reserved');
rlm@3:     }
rlm@3: 
rlm@3: 
rlm@3: 
rlm@3:     public static function LAMEpresetUsedLookup($lame_tag) {
rlm@3:         
rlm@3:         if ($lame_tag['preset_used_id'] == 0) {
rlm@3:             // no preset used (LAME >=3.93)
rlm@3:             // no preset recorded (LAME <3.93)
rlm@3:             return '';
rlm@3:         }
rlm@3:         
rlm@3:         $lame_preset_used_lookup = array ();
rlm@3:         
rlm@3:         for ($i = 8; $i <= 320; $i++) {
rlm@3:             switch ($lame_tag['vbr_method']) {
rlm@3:                 case 'cbr':
rlm@3:                     $lame_preset_used_lookup[$i] = '--alt-preset '.$lame_tag['vbr_method'].' '.$i;
rlm@3:                     break;
rlm@3:                 case 'abr':
rlm@3:                 default: // other VBR modes shouldn't be here(?)
rlm@3:                     $lame_preset_used_lookup[$i] = '--alt-preset '.$i;
rlm@3:                     break;
rlm@3:             }
rlm@3:         }
rlm@3: 
rlm@3:         // named old-style presets (studio, phone, voice, etc) are handled in GuessEncoderOptions()
rlm@3: 
rlm@3:         // named alt-presets
rlm@3:         $lame_preset_used_lookup[1000] = '--r3mix';
rlm@3:         $lame_preset_used_lookup[1001] = '--alt-preset standard';
rlm@3:         $lame_preset_used_lookup[1002] = '--alt-preset extreme';
rlm@3:         $lame_preset_used_lookup[1003] = '--alt-preset insane';
rlm@3:         $lame_preset_used_lookup[1004] = '--alt-preset fast standard';
rlm@3:         $lame_preset_used_lookup[1005] = '--alt-preset fast extreme';
rlm@3:         $lame_preset_used_lookup[1006] = '--alt-preset medium';
rlm@3:         $lame_preset_used_lookup[1007] = '--alt-preset fast medium';
rlm@3: 
rlm@3:         // LAME 3.94 additions/changes
rlm@3:         $lame_preset_used_lookup[1010] = '--preset portable';                                                            // 3.94a15 Oct 21 2003
rlm@3:         $lame_preset_used_lookup[1015] = '--preset radio';                                                               // 3.94a15 Oct 21 2003
rlm@3: 
rlm@3:         $lame_preset_used_lookup[320]  = '--preset insane';                                                              // 3.94a15 Nov 12 2003
rlm@3:         $lame_preset_used_lookup[410]  = '-V9';
rlm@3:         $lame_preset_used_lookup[420]  = '-V8';
rlm@3:         $lame_preset_used_lookup[430]  = '--preset radio';                                                               // 3.94a15 Nov 12 2003
rlm@3:         $lame_preset_used_lookup[440]  = '-V6';
rlm@3:         $lame_preset_used_lookup[450]  = '--preset '.(($lame_tag['raw']['vbr_method'] == 4) ? 'fast ' : '').'portable';  // 3.94a15 Nov 12 2003
rlm@3:         $lame_preset_used_lookup[460]  = '--preset '.(($lame_tag['raw']['vbr_method'] == 4) ? 'fast ' : '').'medium';    // 3.94a15 Nov 12 2003
rlm@3:         $lame_preset_used_lookup[470]  = '--r3mix';                                                                      // 3.94b1  Dec 18 2003
rlm@3:         $lame_preset_used_lookup[480]  = '--preset '.(($lame_tag['raw']['vbr_method'] == 4) ? 'fast ' : '').'standard';  // 3.94a15 Nov 12 2003
rlm@3:         $lame_preset_used_lookup[490]  = '-V1';
rlm@3:         $lame_preset_used_lookup[500]  = '--preset '.(($lame_tag['raw']['vbr_method'] == 4) ? 'fast ' : '').'extreme';   // 3.94a15 Nov 12 2003
rlm@3:         
rlm@3:         return (isset($lame_preset_used_lookup[$lame_tag['preset_used_id']]) ? $lame_preset_used_lookup[$lame_tag['preset_used_id']] : 'new/unknown preset: '.$lame_tag['preset_used_id'].' - report to info@getid3.org');
rlm@3:     }
rlm@3: 	
rlm@3: 	
rlm@3: }
rlm@3: 
rlm@3: ?>