PageRenderTime 300ms CodeModel.GetById 100ms app.highlight 132ms RepoModel.GetById 29ms app.codeStats 9ms

/Vendor/getid3/module.audio.mp3.php

https://bitbucket.org/nova-atlantis/simple-server-media-player
PHP | 2012 lines | 1432 code | 283 blank | 297 comment | 398 complexity | eb87257b2f0507f4ac5591d4226dfc86 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1<?php
  2/////////////////////////////////////////////////////////////////
  3/// getID3() by James Heinrich <info@getid3.org>               //
  4//  available at http://getid3.sourceforge.net                 //
  5//            or http://www.getid3.org                         //
  6//          also https://github.com/JamesHeinrich/getID3       //
  7/////////////////////////////////////////////////////////////////
  8// See readme.txt for more details                             //
  9/////////////////////////////////////////////////////////////////
 10//                                                             //
 11// module.audio.mp3.php                                        //
 12// module for analyzing MP3 files                              //
 13// dependencies: NONE                                          //
 14//                                                            ///
 15/////////////////////////////////////////////////////////////////
 16
 17
 18// number of frames to scan to determine if MPEG-audio sequence is valid
 19// Lower this number to 5-20 for faster scanning
 20// Increase this number to 50+ for most accurate detection of valid VBR/CBR
 21// mpeg-audio streams
 22define('GETID3_MP3_VALID_CHECK_FRAMES', 35);
 23
 24
 25class getid3_mp3 extends getid3_handler
 26{
 27
 28	public $allow_bruteforce = false; // forces getID3() to scan the file byte-by-byte and log all the valid audio frame headers - extremely slow, unrecommended, but may provide data from otherwise-unusuable files
 29
 30	public function Analyze() {
 31		$info = &$this->getid3->info;
 32
 33		$initialOffset = $info['avdataoffset'];
 34
 35		if (!$this->getOnlyMPEGaudioInfo($info['avdataoffset'])) {
 36			if ($this->allow_bruteforce) {
 37				$info['error'][] = 'Rescanning file in BruteForce mode';
 38				$this->getOnlyMPEGaudioInfoBruteForce($this->getid3->fp, $info);
 39			}
 40		}
 41
 42
 43		if (isset($info['mpeg']['audio']['bitrate_mode'])) {
 44			$info['audio']['bitrate_mode'] = strtolower($info['mpeg']['audio']['bitrate_mode']);
 45		}
 46
 47		if (((isset($info['id3v2']['headerlength']) && ($info['avdataoffset'] > $info['id3v2']['headerlength'])) || (!isset($info['id3v2']) && ($info['avdataoffset'] > 0) && ($info['avdataoffset'] != $initialOffset)))) {
 48
 49			$synchoffsetwarning = 'Unknown data before synch ';
 50			if (isset($info['id3v2']['headerlength'])) {
 51				$synchoffsetwarning .= '(ID3v2 header ends at '.$info['id3v2']['headerlength'].', then '.($info['avdataoffset'] - $info['id3v2']['headerlength']).' bytes garbage, ';
 52			} elseif ($initialOffset > 0) {
 53				$synchoffsetwarning .= '(should be at '.$initialOffset.', ';
 54			} else {
 55				$synchoffsetwarning .= '(should be at beginning of file, ';
 56			}
 57			$synchoffsetwarning .= 'synch detected at '.$info['avdataoffset'].')';
 58			if (isset($info['audio']['bitrate_mode']) && ($info['audio']['bitrate_mode'] == 'cbr')) {
 59
 60				if (!empty($info['id3v2']['headerlength']) && (($info['avdataoffset'] - $info['id3v2']['headerlength']) == $info['mpeg']['audio']['framelength'])) {
 61
 62					$synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90-3.92) DLL in CBR mode.';
 63					$info['audio']['codec'] = 'LAME';
 64					$CurrentDataLAMEversionString = 'LAME3.';
 65
 66				} elseif (empty($info['id3v2']['headerlength']) && ($info['avdataoffset'] == $info['mpeg']['audio']['framelength'])) {
 67
 68					$synchoffsetwarning .= '. This is a known problem with some versions of LAME (3.90 - 3.92) DLL in CBR mode.';
 69					$info['audio']['codec'] = 'LAME';
 70					$CurrentDataLAMEversionString = 'LAME3.';
 71
 72				}
 73
 74			}
 75			$info['warning'][] = $synchoffsetwarning;
 76
 77		}
 78
 79		if (isset($info['mpeg']['audio']['LAME'])) {
 80			$info['audio']['codec'] = 'LAME';
 81			if (!empty($info['mpeg']['audio']['LAME']['long_version'])) {
 82				$info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['long_version'], "\x00");
 83			} elseif (!empty($info['mpeg']['audio']['LAME']['short_version'])) {
 84				$info['audio']['encoder'] = rtrim($info['mpeg']['audio']['LAME']['short_version'], "\x00");
 85			}
 86		}
 87
 88		$CurrentDataLAMEversionString = (!empty($CurrentDataLAMEversionString) ? $CurrentDataLAMEversionString : (isset($info['audio']['encoder']) ? $info['audio']['encoder'] : ''));
 89		if (!empty($CurrentDataLAMEversionString) && (substr($CurrentDataLAMEversionString, 0, 6) == 'LAME3.') && !preg_match('[0-9\)]', substr($CurrentDataLAMEversionString, -1))) {
 90			// a version number of LAME that does not end with a number like "LAME3.92"
 91			// or with a closing parenthesis like "LAME3.88 (alpha)"
 92			// or a version of LAME with the LAMEtag-not-filled-in-DLL-mode bug (3.90-3.92)
 93
 94			// not sure what the actual last frame length will be, but will be less than or equal to 1441
 95			$PossiblyLongerLAMEversion_FrameLength = 1441;
 96
 97			// Not sure what version of LAME this is - look in padding of last frame for longer version string
 98			$PossibleLAMEversionStringOffset = $info['avdataend'] - $PossiblyLongerLAMEversion_FrameLength;
 99			$this->fseek($PossibleLAMEversionStringOffset);
100			$PossiblyLongerLAMEversion_Data = $this->fread($PossiblyLongerLAMEversion_FrameLength);
101			switch (substr($CurrentDataLAMEversionString, -1)) {
102				case 'a':
103				case 'b':
104					// "LAME3.94a" will have a longer version string of "LAME3.94 (alpha)" for example
105					// need to trim off "a" to match longer string
106					$CurrentDataLAMEversionString = substr($CurrentDataLAMEversionString, 0, -1);
107					break;
108			}
109			if (($PossiblyLongerLAMEversion_String = strstr($PossiblyLongerLAMEversion_Data, $CurrentDataLAMEversionString)) !== false) {
110				if (substr($PossiblyLongerLAMEversion_String, 0, strlen($CurrentDataLAMEversionString)) == $CurrentDataLAMEversionString) {
111					$PossiblyLongerLAMEversion_NewString = substr($PossiblyLongerLAMEversion_String, 0, strspn($PossiblyLongerLAMEversion_String, 'LAME0123456789., (abcdefghijklmnopqrstuvwxyzJFSOND)')); //"LAME3.90.3"  "LAME3.87 (beta 1, Sep 27 2000)" "LAME3.88 (beta)"
112					if (empty($info['audio']['encoder']) || (strlen($PossiblyLongerLAMEversion_NewString) > strlen($info['audio']['encoder']))) {
113						$info['audio']['encoder'] = $PossiblyLongerLAMEversion_NewString;
114					}
115				}
116			}
117		}
118		if (!empty($info['audio']['encoder'])) {
119			$info['audio']['encoder'] = rtrim($info['audio']['encoder'], "\x00 ");
120		}
121
122		switch (isset($info['mpeg']['audio']['layer']) ? $info['mpeg']['audio']['layer'] : '') {
123			case 1:
124			case 2:
125				$info['audio']['dataformat'] = 'mp'.$info['mpeg']['audio']['layer'];
126				break;
127		}
128		if (isset($info['fileformat']) && ($info['fileformat'] == 'mp3')) {
129			switch ($info['audio']['dataformat']) {
130				case 'mp1':
131				case 'mp2':
132				case 'mp3':
133					$info['fileformat'] = $info['audio']['dataformat'];
134					break;
135
136				default:
137					$info['warning'][] = 'Expecting [audio][dataformat] to be mp1/mp2/mp3 when fileformat == mp3, [audio][dataformat] actually "'.$info['audio']['dataformat'].'"';
138					break;
139			}
140		}
141
142		if (empty($info['fileformat'])) {
143			unset($info['fileformat']);
144			unset($info['audio']['bitrate_mode']);
145			unset($info['avdataoffset']);
146			unset($info['avdataend']);
147			return false;
148		}
149
150		$info['mime_type']         = 'audio/mpeg';
151		$info['audio']['lossless'] = false;
152
153		// Calculate playtime
154		if (!isset($info['playtime_seconds']) && isset($info['audio']['bitrate']) && ($info['audio']['bitrate'] > 0)) {
155			$info['playtime_seconds'] = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['audio']['bitrate'];
156		}
157
158		$info['audio']['encoder_options'] = $this->GuessEncoderOptions();
159
160		return true;
161	}
162
163
164	public function GuessEncoderOptions() {
165		// shortcuts
166		$info = &$this->getid3->info;
167		if (!empty($info['mpeg']['audio'])) {
168			$thisfile_mpeg_audio = &$info['mpeg']['audio'];
169			if (!empty($thisfile_mpeg_audio['LAME'])) {
170				$thisfile_mpeg_audio_lame = &$thisfile_mpeg_audio['LAME'];
171			}
172		}
173
174		$encoder_options = '';
175		static $NamedPresetBitrates = array(16, 24, 40, 56, 112, 128, 160, 192, 256);
176
177		if (isset($thisfile_mpeg_audio['VBR_method']) && ($thisfile_mpeg_audio['VBR_method'] == 'Fraunhofer') && !empty($thisfile_mpeg_audio['VBR_quality'])) {
178
179			$encoder_options = 'VBR q'.$thisfile_mpeg_audio['VBR_quality'];
180
181		} elseif (!empty($thisfile_mpeg_audio_lame['preset_used']) && (!in_array($thisfile_mpeg_audio_lame['preset_used_id'], $NamedPresetBitrates))) {
182
183			$encoder_options = $thisfile_mpeg_audio_lame['preset_used'];
184
185		} elseif (!empty($thisfile_mpeg_audio_lame['vbr_quality'])) {
186
187			static $KnownEncoderValues = array();
188			if (empty($KnownEncoderValues)) {
189
190				//$KnownEncoderValues[abrbitrate_minbitrate][vbr_quality][raw_vbr_method][raw_noise_shaping][raw_stereo_mode][ath_type][lowpass_frequency] = 'preset name';
191				$KnownEncoderValues[0xFF][58][1][1][3][2][20500] = '--alt-preset insane';        // 3.90,   3.90.1, 3.92
192				$KnownEncoderValues[0xFF][58][1][1][3][2][20600] = '--alt-preset insane';        // 3.90.2, 3.90.3, 3.91
193				$KnownEncoderValues[0xFF][57][1][1][3][4][20500] = '--alt-preset insane';        // 3.94,   3.95
194				$KnownEncoderValues['**'][78][3][2][3][2][19500] = '--alt-preset extreme';       // 3.90,   3.90.1, 3.92
195				$KnownEncoderValues['**'][78][3][2][3][2][19600] = '--alt-preset extreme';       // 3.90.2, 3.91
196				$KnownEncoderValues['**'][78][3][1][3][2][19600] = '--alt-preset extreme';       // 3.90.3
197				$KnownEncoderValues['**'][78][4][2][3][2][19500] = '--alt-preset fast extreme';  // 3.90,   3.90.1, 3.92
198				$KnownEncoderValues['**'][78][4][2][3][2][19600] = '--alt-preset fast extreme';  // 3.90.2, 3.90.3, 3.91
199				$KnownEncoderValues['**'][78][3][2][3][4][19000] = '--alt-preset standard';      // 3.90,   3.90.1, 3.90.2, 3.91, 3.92
200				$KnownEncoderValues['**'][78][3][1][3][4][19000] = '--alt-preset standard';      // 3.90.3
201				$KnownEncoderValues['**'][78][4][2][3][4][19000] = '--alt-preset fast standard'; // 3.90,   3.90.1, 3.90.2, 3.91, 3.92
202				$KnownEncoderValues['**'][78][4][1][3][4][19000] = '--alt-preset fast standard'; // 3.90.3
203				$KnownEncoderValues['**'][88][4][1][3][3][19500] = '--r3mix';                    // 3.90,   3.90.1, 3.92
204				$KnownEncoderValues['**'][88][4][1][3][3][19600] = '--r3mix';                    // 3.90.2, 3.90.3, 3.91
205				$KnownEncoderValues['**'][67][4][1][3][4][18000] = '--r3mix';                    // 3.94,   3.95
206				$KnownEncoderValues['**'][68][3][2][3][4][18000] = '--alt-preset medium';        // 3.90.3
207				$KnownEncoderValues['**'][68][4][2][3][4][18000] = '--alt-preset fast medium';   // 3.90.3
208
209				$KnownEncoderValues[0xFF][99][1][1][1][2][0]     = '--preset studio';            // 3.90,   3.90.1, 3.90.2, 3.91, 3.92
210				$KnownEncoderValues[0xFF][58][2][1][3][2][20600] = '--preset studio';            // 3.90.3, 3.93.1
211				$KnownEncoderValues[0xFF][58][2][1][3][2][20500] = '--preset studio';            // 3.93
212				$KnownEncoderValues[0xFF][57][2][1][3][4][20500] = '--preset studio';            // 3.94,   3.95
213				$KnownEncoderValues[0xC0][88][1][1][1][2][0]     = '--preset cd';                // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
214				$KnownEncoderValues[0xC0][58][2][2][3][2][19600] = '--preset cd';                // 3.90.3, 3.93.1
215				$KnownEncoderValues[0xC0][58][2][2][3][2][19500] = '--preset cd';                // 3.93
216				$KnownEncoderValues[0xC0][57][2][1][3][4][19500] = '--preset cd';                // 3.94,   3.95
217				$KnownEncoderValues[0xA0][78][1][1][3][2][18000] = '--preset hifi';              // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
218				$KnownEncoderValues[0xA0][58][2][2][3][2][18000] = '--preset hifi';              // 3.90.3, 3.93,   3.93.1
219				$KnownEncoderValues[0xA0][57][2][1][3][4][18000] = '--preset hifi';              // 3.94,   3.95
220				$KnownEncoderValues[0x80][67][1][1][3][2][18000] = '--preset tape';              // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
221				$KnownEncoderValues[0x80][67][1][1][3][2][15000] = '--preset radio';             // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
222				$KnownEncoderValues[0x70][67][1][1][3][2][15000] = '--preset fm';                // 3.90,   3.90.1, 3.90.2,   3.91, 3.92
223				$KnownEncoderValues[0x70][58][2][2][3][2][16000] = '--preset tape/radio/fm';     // 3.90.3, 3.93,   3.93.1
224				$KnownEncoderValues[0x70][57][2][1][3][4][16000] = '--preset tape/radio/fm';     // 3.94,   3.95
225				$KnownEncoderValues[0x38][58][2][2][0][2][10000] = '--preset voice';             // 3.90.3, 3.93,   3.93.1
226				$KnownEncoderValues[0x38][57][2][1][0][4][15000] = '--preset voice';             // 3.94,   3.95
227				$KnownEncoderValues[0x38][57][2][1][0][4][16000] = '--preset voice';             // 3.94a14
228				$KnownEncoderValues[0x28][65][1][1][0][2][7500]  = '--preset mw-us';             // 3.90,   3.90.1, 3.92
229				$KnownEncoderValues[0x28][65][1][1][0][2][7600]  = '--preset mw-us';             // 3.90.2, 3.91
230				$KnownEncoderValues[0x28][58][2][2][0][2][7000]  = '--preset mw-us';             // 3.90.3, 3.93,   3.93.1
231				$KnownEncoderValues[0x28][57][2][1][0][4][10500] = '--preset mw-us';             // 3.94,   3.95
232				$KnownEncoderValues[0x28][57][2][1][0][4][11200] = '--preset mw-us';             // 3.94a14
233				$KnownEncoderValues[0x28][57][2][1][0][4][8800]  = '--preset mw-us';             // 3.94a15
234				$KnownEncoderValues[0x18][58][2][2][0][2][4000]  = '--preset phon+/lw/mw-eu/sw'; // 3.90.3, 3.93.1
235				$KnownEncoderValues[0x18][58][2][2][0][2][3900]  = '--preset phon+/lw/mw-eu/sw'; // 3.93
236				$KnownEncoderValues[0x18][57][2][1][0][4][5900]  = '--preset phon+/lw/mw-eu/sw'; // 3.94,   3.95
237				$KnownEncoderValues[0x18][57][2][1][0][4][6200]  = '--preset phon+/lw/mw-eu/sw'; // 3.94a14
238				$KnownEncoderValues[0x18][57][2][1][0][4][3200]  = '--preset phon+/lw/mw-eu/sw'; // 3.94a15
239				$KnownEncoderValues[0x10][58][2][2][0][2][3800]  = '--preset phone';             // 3.90.3, 3.93.1
240				$KnownEncoderValues[0x10][58][2][2][0][2][3700]  = '--preset phone';             // 3.93
241				$KnownEncoderValues[0x10][57][2][1][0][4][5600]  = '--preset phone';             // 3.94,   3.95
242			}
243
244			if (isset($KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) {
245
246				$encoder_options = $KnownEncoderValues[$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate']][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']];
247
248			} elseif (isset($KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']])) {
249
250				$encoder_options = $KnownEncoderValues['**'][$thisfile_mpeg_audio_lame['vbr_quality']][$thisfile_mpeg_audio_lame['raw']['vbr_method']][$thisfile_mpeg_audio_lame['raw']['noise_shaping']][$thisfile_mpeg_audio_lame['raw']['stereo_mode']][$thisfile_mpeg_audio_lame['ath_type']][$thisfile_mpeg_audio_lame['lowpass_frequency']];
251
252			} elseif ($info['audio']['bitrate_mode'] == 'vbr') {
253
254				// http://gabriel.mp3-tech.org/mp3infotag.html
255				// int    Quality = (100 - 10 * gfp->VBR_q - gfp->quality)h
256
257
258				$LAME_V_value = 10 - ceil($thisfile_mpeg_audio_lame['vbr_quality'] / 10);
259				$LAME_q_value = 100 - $thisfile_mpeg_audio_lame['vbr_quality'] - ($LAME_V_value * 10);
260				$encoder_options = '-V'.$LAME_V_value.' -q'.$LAME_q_value;
261
262			} elseif ($info['audio']['bitrate_mode'] == 'cbr') {
263
264				$encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
265
266			} else {
267
268				$encoder_options = strtoupper($info['audio']['bitrate_mode']);
269
270			}
271
272		} elseif (!empty($thisfile_mpeg_audio_lame['bitrate_abr'])) {
273
274			$encoder_options = 'ABR'.$thisfile_mpeg_audio_lame['bitrate_abr'];
275
276		} elseif (!empty($info['audio']['bitrate'])) {
277
278			if ($info['audio']['bitrate_mode'] == 'cbr') {
279				$encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
280			} else {
281				$encoder_options = strtoupper($info['audio']['bitrate_mode']);
282			}
283
284		}
285		if (!empty($thisfile_mpeg_audio_lame['bitrate_min'])) {
286			$encoder_options .= ' -b'.$thisfile_mpeg_audio_lame['bitrate_min'];
287		}
288
289		if (!empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev']) || !empty($thisfile_mpeg_audio_lame['encoding_flags']['nogap_next'])) {
290			$encoder_options .= ' --nogap';
291		}
292
293		if (!empty($thisfile_mpeg_audio_lame['lowpass_frequency'])) {
294			$ExplodedOptions = explode(' ', $encoder_options, 4);
295			if ($ExplodedOptions[0] == '--r3mix') {
296				$ExplodedOptions[1] = 'r3mix';
297			}
298			switch ($ExplodedOptions[0]) {
299				case '--preset':
300				case '--alt-preset':
301				case '--r3mix':
302					if ($ExplodedOptions[1] == 'fast') {
303						$ExplodedOptions[1] .= ' '.$ExplodedOptions[2];
304					}
305					switch ($ExplodedOptions[1]) {
306						case 'portable':
307						case 'medium':
308						case 'standard':
309						case 'extreme':
310						case 'insane':
311						case 'fast portable':
312						case 'fast medium':
313						case 'fast standard':
314						case 'fast extreme':
315						case 'fast insane':
316						case 'r3mix':
317							static $ExpectedLowpass = array(
318									'insane|20500'        => 20500,
319									'insane|20600'        => 20600,  // 3.90.2, 3.90.3, 3.91
320									'medium|18000'        => 18000,
321									'fast medium|18000'   => 18000,
322									'extreme|19500'       => 19500,  // 3.90,   3.90.1, 3.92, 3.95
323									'extreme|19600'       => 19600,  // 3.90.2, 3.90.3, 3.91, 3.93.1
324									'fast extreme|19500'  => 19500,  // 3.90,   3.90.1, 3.92, 3.95
325									'fast extreme|19600'  => 19600,  // 3.90.2, 3.90.3, 3.91, 3.93.1
326									'standard|19000'      => 19000,
327									'fast standard|19000' => 19000,
328									'r3mix|19500'         => 19500,  // 3.90,   3.90.1, 3.92
329									'r3mix|19600'         => 19600,  // 3.90.2, 3.90.3, 3.91
330									'r3mix|18000'         => 18000,  // 3.94,   3.95
331								);
332							if (!isset($ExpectedLowpass[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio_lame['lowpass_frequency']]) && ($thisfile_mpeg_audio_lame['lowpass_frequency'] < 22050) && (round($thisfile_mpeg_audio_lame['lowpass_frequency'] / 1000) < round($thisfile_mpeg_audio['sample_rate'] / 2000))) {
333								$encoder_options .= ' --lowpass '.$thisfile_mpeg_audio_lame['lowpass_frequency'];
334							}
335							break;
336
337						default:
338							break;
339					}
340					break;
341			}
342		}
343
344		if (isset($thisfile_mpeg_audio_lame['raw']['source_sample_freq'])) {
345			if (($thisfile_mpeg_audio['sample_rate'] == 44100) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 1)) {
346				$encoder_options .= ' --resample 44100';
347			} elseif (($thisfile_mpeg_audio['sample_rate'] == 48000) && ($thisfile_mpeg_audio_lame['raw']['source_sample_freq'] != 2)) {
348				$encoder_options .= ' --resample 48000';
349			} elseif ($thisfile_mpeg_audio['sample_rate'] < 44100) {
350				switch ($thisfile_mpeg_audio_lame['raw']['source_sample_freq']) {
351					case 0: // <= 32000
352						// may or may not be same as source frequency - ignore
353						break;
354					case 1: // 44100
355					case 2: // 48000
356					case 3: // 48000+
357						$ExplodedOptions = explode(' ', $encoder_options, 4);
358						switch ($ExplodedOptions[0]) {
359							case '--preset':
360							case '--alt-preset':
361								switch ($ExplodedOptions[1]) {
362									case 'fast':
363									case 'portable':
364									case 'medium':
365									case 'standard':
366									case 'extreme':
367									case 'insane':
368										$encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
369										break;
370
371									default:
372										static $ExpectedResampledRate = array(
373												'phon+/lw/mw-eu/sw|16000' => 16000,
374												'mw-us|24000'             => 24000, // 3.95
375												'mw-us|32000'             => 32000, // 3.93
376												'mw-us|16000'             => 16000, // 3.92
377												'phone|16000'             => 16000,
378												'phone|11025'             => 11025, // 3.94a15
379												'radio|32000'             => 32000, // 3.94a15
380												'fm/radio|32000'          => 32000, // 3.92
381												'fm|32000'                => 32000, // 3.90
382												'voice|32000'             => 32000);
383										if (!isset($ExpectedResampledRate[$ExplodedOptions[1].'|'.$thisfile_mpeg_audio['sample_rate']])) {
384											$encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
385										}
386										break;
387								}
388								break;
389
390							case '--r3mix':
391							default:
392								$encoder_options .= ' --resample '.$thisfile_mpeg_audio['sample_rate'];
393								break;
394						}
395						break;
396				}
397			}
398		}
399		if (empty($encoder_options) && !empty($info['audio']['bitrate']) && !empty($info['audio']['bitrate_mode'])) {
400			//$encoder_options = strtoupper($info['audio']['bitrate_mode']).ceil($info['audio']['bitrate'] / 1000);
401			$encoder_options = strtoupper($info['audio']['bitrate_mode']);
402		}
403
404		return $encoder_options;
405	}
406
407
408	public function decodeMPEGaudioHeader($offset, &$info, $recursivesearch=true, $ScanAsCBR=false, $FastMPEGheaderScan=false) {
409		static $MPEGaudioVersionLookup;
410		static $MPEGaudioLayerLookup;
411		static $MPEGaudioBitrateLookup;
412		static $MPEGaudioFrequencyLookup;
413		static $MPEGaudioChannelModeLookup;
414		static $MPEGaudioModeExtensionLookup;
415		static $MPEGaudioEmphasisLookup;
416		if (empty($MPEGaudioVersionLookup)) {
417			$MPEGaudioVersionLookup       = self::MPEGaudioVersionArray();
418			$MPEGaudioLayerLookup         = self::MPEGaudioLayerArray();
419			$MPEGaudioBitrateLookup       = self::MPEGaudioBitrateArray();
420			$MPEGaudioFrequencyLookup     = self::MPEGaudioFrequencyArray();
421			$MPEGaudioChannelModeLookup   = self::MPEGaudioChannelModeArray();
422			$MPEGaudioModeExtensionLookup = self::MPEGaudioModeExtensionArray();
423			$MPEGaudioEmphasisLookup      = self::MPEGaudioEmphasisArray();
424		}
425
426		if ($this->fseek($offset) != 0) {
427			$info['error'][] = 'decodeMPEGaudioHeader() failed to seek to next offset at '.$offset;
428			return false;
429		}
430		//$headerstring = $this->fread(1441); // worst-case max length = 32kHz @ 320kbps layer 3 = 1441 bytes/frame
431		$headerstring = $this->fread(226); // LAME header at offset 36 + 190 bytes of Xing/LAME data
432
433		// MP3 audio frame structure:
434		// $aa $aa $aa $aa [$bb $bb] $cc...
435		// where $aa..$aa is the four-byte mpeg-audio header (below)
436		// $bb $bb is the optional 2-byte CRC
437		// and $cc... is the audio data
438
439		$head4 = substr($headerstring, 0, 4);
440
441		static $MPEGaudioHeaderDecodeCache = array();
442		if (isset($MPEGaudioHeaderDecodeCache[$head4])) {
443			$MPEGheaderRawArray = $MPEGaudioHeaderDecodeCache[$head4];
444		} else {
445			$MPEGheaderRawArray = self::MPEGaudioHeaderDecode($head4);
446			$MPEGaudioHeaderDecodeCache[$head4] = $MPEGheaderRawArray;
447		}
448
449		static $MPEGaudioHeaderValidCache = array();
450		if (!isset($MPEGaudioHeaderValidCache[$head4])) { // Not in cache
451			//$MPEGaudioHeaderValidCache[$head4] = self::MPEGaudioHeaderValid($MPEGheaderRawArray, false, true);  // allow badly-formatted freeformat (from LAME 3.90 - 3.93.1)
452			$MPEGaudioHeaderValidCache[$head4] = self::MPEGaudioHeaderValid($MPEGheaderRawArray, false, false);
453		}
454
455		// shortcut
456		if (!isset($info['mpeg']['audio'])) {
457			$info['mpeg']['audio'] = array();
458		}
459		$thisfile_mpeg_audio = &$info['mpeg']['audio'];
460
461
462		if ($MPEGaudioHeaderValidCache[$head4]) {
463			$thisfile_mpeg_audio['raw'] = $MPEGheaderRawArray;
464		} else {
465			$info['error'][] = 'Invalid MPEG audio header ('.getid3_lib::PrintHexBytes($head4).') at offset '.$offset;
466			return false;
467		}
468
469		if (!$FastMPEGheaderScan) {
470			$thisfile_mpeg_audio['version']       = $MPEGaudioVersionLookup[$thisfile_mpeg_audio['raw']['version']];
471			$thisfile_mpeg_audio['layer']         = $MPEGaudioLayerLookup[$thisfile_mpeg_audio['raw']['layer']];
472
473			$thisfile_mpeg_audio['channelmode']   = $MPEGaudioChannelModeLookup[$thisfile_mpeg_audio['raw']['channelmode']];
474			$thisfile_mpeg_audio['channels']      = (($thisfile_mpeg_audio['channelmode'] == 'mono') ? 1 : 2);
475			$thisfile_mpeg_audio['sample_rate']   = $MPEGaudioFrequencyLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['raw']['sample_rate']];
476			$thisfile_mpeg_audio['protection']    = !$thisfile_mpeg_audio['raw']['protection'];
477			$thisfile_mpeg_audio['private']       = (bool) $thisfile_mpeg_audio['raw']['private'];
478			$thisfile_mpeg_audio['modeextension'] = $MPEGaudioModeExtensionLookup[$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['modeextension']];
479			$thisfile_mpeg_audio['copyright']     = (bool) $thisfile_mpeg_audio['raw']['copyright'];
480			$thisfile_mpeg_audio['original']      = (bool) $thisfile_mpeg_audio['raw']['original'];
481			$thisfile_mpeg_audio['emphasis']      = $MPEGaudioEmphasisLookup[$thisfile_mpeg_audio['raw']['emphasis']];
482
483			$info['audio']['channels']    = $thisfile_mpeg_audio['channels'];
484			$info['audio']['sample_rate'] = $thisfile_mpeg_audio['sample_rate'];
485
486			if ($thisfile_mpeg_audio['protection']) {
487				$thisfile_mpeg_audio['crc'] = getid3_lib::BigEndian2Int(substr($headerstring, 4, 2));
488			}
489		}
490
491		if ($thisfile_mpeg_audio['raw']['bitrate'] == 15) {
492			// http://www.hydrogenaudio.org/?act=ST&f=16&t=9682&st=0
493			$info['warning'][] = 'Invalid bitrate index (15), this is a known bug in free-format MP3s encoded by LAME v3.90 - 3.93.1';
494			$thisfile_mpeg_audio['raw']['bitrate'] = 0;
495		}
496		$thisfile_mpeg_audio['padding'] = (bool) $thisfile_mpeg_audio['raw']['padding'];
497		$thisfile_mpeg_audio['bitrate'] = $MPEGaudioBitrateLookup[$thisfile_mpeg_audio['version']][$thisfile_mpeg_audio['layer']][$thisfile_mpeg_audio['raw']['bitrate']];
498
499		if (($thisfile_mpeg_audio['bitrate'] == 'free') && ($offset == $info['avdataoffset'])) {
500			// only skip multiple frame check if free-format bitstream found at beginning of file
501			// otherwise is quite possibly simply corrupted data
502			$recursivesearch = false;
503		}
504
505		// For Layer 2 there are some combinations of bitrate and mode which are not allowed.
506		if (!$FastMPEGheaderScan && ($thisfile_mpeg_audio['layer'] == '2')) {
507
508			$info['audio']['dataformat'] = 'mp2';
509			switch ($thisfile_mpeg_audio['channelmode']) {
510
511				case 'mono':
512					if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] <= 192000)) {
513						// these are ok
514					} else {
515						$info['error'][] = $thisfile_mpeg_audio['bitrate'].'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
516						return false;
517					}
518					break;
519
520				case 'stereo':
521				case 'joint stereo':
522				case 'dual channel':
523					if (($thisfile_mpeg_audio['bitrate'] == 'free') || ($thisfile_mpeg_audio['bitrate'] == 64000) || ($thisfile_mpeg_audio['bitrate'] >= 96000)) {
524						// these are ok
525					} else {
526						$info['error'][] = intval(round($thisfile_mpeg_audio['bitrate'] / 1000)).'kbps not allowed in Layer 2, '.$thisfile_mpeg_audio['channelmode'].'.';
527						return false;
528					}
529					break;
530
531			}
532
533		}
534
535
536		if ($info['audio']['sample_rate'] > 0) {
537			$thisfile_mpeg_audio['framelength'] = self::MPEGaudioFrameLength($thisfile_mpeg_audio['bitrate'], $thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['layer'], (int) $thisfile_mpeg_audio['padding'], $info['audio']['sample_rate']);
538		}
539
540		$nextframetestoffset = $offset + 1;
541		if ($thisfile_mpeg_audio['bitrate'] != 'free') {
542
543			$info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
544
545			if (isset($thisfile_mpeg_audio['framelength'])) {
546				$nextframetestoffset = $offset + $thisfile_mpeg_audio['framelength'];
547			} else {
548				$info['error'][] = 'Frame at offset('.$offset.') is has an invalid frame length.';
549				return false;
550			}
551
552		}
553
554		$ExpectedNumberOfAudioBytes = 0;
555
556		////////////////////////////////////////////////////////////////////////////////////
557		// Variable-bitrate headers
558
559		if (substr($headerstring, 4 + 32, 4) == 'VBRI') {
560			// Fraunhofer VBR header is hardcoded 'VBRI' at offset 0x24 (36)
561			// specs taken from http://minnie.tuhs.org/pipermail/mp3encoder/2001-January/001800.html
562
563			$thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
564			$thisfile_mpeg_audio['VBR_method']   = 'Fraunhofer';
565			$info['audio']['codec']                = 'Fraunhofer';
566
567			$SideInfoData = substr($headerstring, 4 + 2, 32);
568
569			$FraunhoferVBROffset = 36;
570
571			$thisfile_mpeg_audio['VBR_encoder_version']     = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset +  4, 2)); // VbriVersion
572			$thisfile_mpeg_audio['VBR_encoder_delay']       = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset +  6, 2)); // VbriDelay
573			$thisfile_mpeg_audio['VBR_quality']             = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset +  8, 2)); // VbriQuality
574			$thisfile_mpeg_audio['VBR_bytes']               = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 10, 4)); // VbriStreamBytes
575			$thisfile_mpeg_audio['VBR_frames']              = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 14, 4)); // VbriStreamFrames
576			$thisfile_mpeg_audio['VBR_seek_offsets']        = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 18, 2)); // VbriTableSize
577			$thisfile_mpeg_audio['VBR_seek_scale']          = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 20, 2)); // VbriTableScale
578			$thisfile_mpeg_audio['VBR_entry_bytes']         = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 22, 2)); // VbriEntryBytes
579			$thisfile_mpeg_audio['VBR_entry_frames']        = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset + 24, 2)); // VbriEntryFrames
580
581			$ExpectedNumberOfAudioBytes = $thisfile_mpeg_audio['VBR_bytes'];
582
583			$previousbyteoffset = $offset;
584			for ($i = 0; $i < $thisfile_mpeg_audio['VBR_seek_offsets']; $i++) {
585				$Fraunhofer_OffsetN = getid3_lib::BigEndian2Int(substr($headerstring, $FraunhoferVBROffset, $thisfile_mpeg_audio['VBR_entry_bytes']));
586				$FraunhoferVBROffset += $thisfile_mpeg_audio['VBR_entry_bytes'];
587				$thisfile_mpeg_audio['VBR_offsets_relative'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']);
588				$thisfile_mpeg_audio['VBR_offsets_absolute'][$i] = ($Fraunhofer_OffsetN * $thisfile_mpeg_audio['VBR_seek_scale']) + $previousbyteoffset;
589				$previousbyteoffset += $Fraunhofer_OffsetN;
590			}
591
592
593		} else {
594
595			// Xing VBR header is hardcoded 'Xing' at a offset 0x0D (13), 0x15 (21) or 0x24 (36)
596			// depending on MPEG layer and number of channels
597
598			$VBRidOffset = self::XingVBRidOffset($thisfile_mpeg_audio['version'], $thisfile_mpeg_audio['channelmode']);
599			$SideInfoData = substr($headerstring, 4 + 2, $VBRidOffset - 4);
600
601			if ((substr($headerstring, $VBRidOffset, strlen('Xing')) == 'Xing') || (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Info')) {
602				// 'Xing' is traditional Xing VBR frame
603				// 'Info' is LAME-encoded CBR (This was done to avoid CBR files to be recognized as traditional Xing VBR files by some decoders.)
604				// 'Info' *can* legally be used to specify a VBR file as well, however.
605
606				// http://www.multiweb.cz/twoinches/MP3inside.htm
607				//00..03 = "Xing" or "Info"
608				//04..07 = Flags:
609				//  0x01  Frames Flag     set if value for number of frames in file is stored
610				//  0x02  Bytes Flag      set if value for filesize in bytes is stored
611				//  0x04  TOC Flag        set if values for TOC are stored
612				//  0x08  VBR Scale Flag  set if values for VBR scale is stored
613				//08..11  Frames: Number of frames in file (including the first Xing/Info one)
614				//12..15  Bytes:  File length in Bytes
615				//16..115  TOC (Table of Contents):
616				//  Contains of 100 indexes (one Byte length) for easier lookup in file. Approximately solves problem with moving inside file.
617				//  Each Byte has a value according this formula:
618				//  (TOC[i] / 256) * fileLenInBytes
619				//  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:
620				//  TOC[(60/240)*100] = TOC[25]
621				//  and corresponding Byte in file is then approximately at:
622				//  (TOC[25]/256) * 5000000
623				//116..119  VBR Scale
624
625
626				// should be safe to leave this at 'vbr' and let it be overriden to 'cbr' if a CBR preset/mode is used by LAME
627//				if (substr($headerstring, $VBRidOffset, strlen('Info')) == 'Xing') {
628					$thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
629					$thisfile_mpeg_audio['VBR_method']   = 'Xing';
630//				} else {
631//					$ScanAsCBR = true;
632//					$thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
633//				}
634
635				$thisfile_mpeg_audio['xing_flags_raw'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 4, 4));
636
637				$thisfile_mpeg_audio['xing_flags']['frames']    = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000001);
638				$thisfile_mpeg_audio['xing_flags']['bytes']     = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000002);
639				$thisfile_mpeg_audio['xing_flags']['toc']       = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000004);
640				$thisfile_mpeg_audio['xing_flags']['vbr_scale'] = (bool) ($thisfile_mpeg_audio['xing_flags_raw'] & 0x00000008);
641
642				if ($thisfile_mpeg_audio['xing_flags']['frames']) {
643					$thisfile_mpeg_audio['VBR_frames'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset +  8, 4));
644					//$thisfile_mpeg_audio['VBR_frames']--; // don't count header Xing/Info frame
645				}
646				if ($thisfile_mpeg_audio['xing_flags']['bytes']) {
647					$thisfile_mpeg_audio['VBR_bytes']  = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 12, 4));
648				}
649
650				//if (($thisfile_mpeg_audio['bitrate'] == 'free') && !empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
651				if (!empty($thisfile_mpeg_audio['VBR_frames']) && !empty($thisfile_mpeg_audio['VBR_bytes'])) {
652
653					$framelengthfloat = $thisfile_mpeg_audio['VBR_bytes'] / $thisfile_mpeg_audio['VBR_frames'];
654
655					if ($thisfile_mpeg_audio['layer'] == '1') {
656						// BitRate = (((FrameLengthInBytes / 4) - Padding) * SampleRate) / 12
657						//$info['audio']['bitrate'] = ((($framelengthfloat / 4) - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 12;
658						$info['audio']['bitrate'] = ($framelengthfloat / 4) * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 12;
659					} else {
660						// Bitrate = ((FrameLengthInBytes - Padding) * SampleRate) / 144
661						//$info['audio']['bitrate'] = (($framelengthfloat - intval($thisfile_mpeg_audio['padding'])) * $thisfile_mpeg_audio['sample_rate']) / 144;
662						$info['audio']['bitrate'] = $framelengthfloat * $thisfile_mpeg_audio['sample_rate'] * (2 / $info['audio']['channels']) / 144;
663					}
664					$thisfile_mpeg_audio['framelength'] = floor($framelengthfloat);
665				}
666
667				if ($thisfile_mpeg_audio['xing_flags']['toc']) {
668					$LAMEtocData = substr($headerstring, $VBRidOffset + 16, 100);
669					for ($i = 0; $i < 100; $i++) {
670						$thisfile_mpeg_audio['toc'][$i] = ord($LAMEtocData{$i});
671					}
672				}
673				if ($thisfile_mpeg_audio['xing_flags']['vbr_scale']) {
674					$thisfile_mpeg_audio['VBR_scale'] = getid3_lib::BigEndian2Int(substr($headerstring, $VBRidOffset + 116, 4));
675				}
676
677
678				// http://gabriel.mp3-tech.org/mp3infotag.html
679				if (substr($headerstring, $VBRidOffset + 120, 4) == 'LAME') {
680
681					// shortcut
682					$thisfile_mpeg_audio['LAME'] = array();
683					$thisfile_mpeg_audio_lame    = &$thisfile_mpeg_audio['LAME'];
684
685
686					$thisfile_mpeg_audio_lame['long_version']  = substr($headerstring, $VBRidOffset + 120, 20);
687					$thisfile_mpeg_audio_lame['short_version'] = substr($thisfile_mpeg_audio_lame['long_version'], 0, 9);
688
689					if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.90') {
690
691						// extra 11 chars are not part of version string when LAMEtag present
692						unset($thisfile_mpeg_audio_lame['long_version']);
693
694						// It the LAME tag was only introduced in LAME v3.90
695						// http://www.hydrogenaudio.org/?act=ST&f=15&t=9933
696
697						// Offsets of various bytes in http://gabriel.mp3-tech.org/mp3infotag.html
698						// are assuming a 'Xing' identifier offset of 0x24, which is the case for
699						// MPEG-1 non-mono, but not for other combinations
700						$LAMEtagOffsetContant = $VBRidOffset - 0x24;
701
702						// shortcuts
703						$thisfile_mpeg_audio_lame['RGAD']    = array('track'=>array(), 'album'=>array());
704						$thisfile_mpeg_audio_lame_RGAD       = &$thisfile_mpeg_audio_lame['RGAD'];
705						$thisfile_mpeg_audio_lame_RGAD_track = &$thisfile_mpeg_audio_lame_RGAD['track'];
706						$thisfile_mpeg_audio_lame_RGAD_album = &$thisfile_mpeg_audio_lame_RGAD['album'];
707						$thisfile_mpeg_audio_lame['raw'] = array();
708						$thisfile_mpeg_audio_lame_raw    = &$thisfile_mpeg_audio_lame['raw'];
709
710						// byte $9B  VBR Quality
711						// This field is there to indicate a quality level, although the scale was not precised in the original Xing specifications.
712						// Actually overwrites original Xing bytes
713						unset($thisfile_mpeg_audio['VBR_scale']);
714						$thisfile_mpeg_audio_lame['vbr_quality'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0x9B, 1));
715
716						// bytes $9C-$A4  Encoder short VersionString
717						$thisfile_mpeg_audio_lame['short_version'] = substr($headerstring, $LAMEtagOffsetContant + 0x9C, 9);
718
719						// byte $A5  Info Tag revision + VBR method
720						$LAMEtagRevisionVBRmethod = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA5, 1));
721
722						$thisfile_mpeg_audio_lame['tag_revision']   = ($LAMEtagRevisionVBRmethod & 0xF0) >> 4;
723						$thisfile_mpeg_audio_lame_raw['vbr_method'] =  $LAMEtagRevisionVBRmethod & 0x0F;
724						$thisfile_mpeg_audio_lame['vbr_method']     = self::LAMEvbrMethodLookup($thisfile_mpeg_audio_lame_raw['vbr_method']);
725						$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'
726
727						// byte $A6  Lowpass filter value
728						$thisfile_mpeg_audio_lame['lowpass_frequency'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA6, 1)) * 100;
729
730						// bytes $A7-$AE  Replay Gain
731						// http://privatewww.essex.ac.uk/~djmrob/replaygain/rg_data_format.html
732						// bytes $A7-$AA : 32 bit floating point "Peak signal amplitude"
733						if ($thisfile_mpeg_audio_lame['short_version'] >= 'LAME3.94b') {
734							// LAME 3.94a16 and later - 9.23 fixed point
735							// ie 0x0059E2EE / (2^23) = 5890798 / 8388608 = 0.7022378444671630859375
736							$thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = (float) ((getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4))) / 8388608);
737						} else {
738							// LAME 3.94a15 and earlier - 32-bit floating point
739							// Actually 3.94a16 will fall in here too and be WRONG, but is hard to detect 3.94a16 vs 3.94a15
740							$thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] = getid3_lib::LittleEndian2Float(substr($headerstring, $LAMEtagOffsetContant + 0xA7, 4));
741						}
742						if ($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'] == 0) {
743							unset($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
744						} else {
745							$thisfile_mpeg_audio_lame_RGAD['peak_db'] = getid3_lib::RGADamplitude2dB($thisfile_mpeg_audio_lame_RGAD['peak_amplitude']);
746						}
747
748						$thisfile_mpeg_audio_lame_raw['RGAD_track']      =   getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAB, 2));
749						$thisfile_mpeg_audio_lame_raw['RGAD_album']      =   getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAD, 2));
750
751
752						if ($thisfile_mpeg_audio_lame_raw['RGAD_track'] != 0) {
753
754							$thisfile_mpeg_audio_lame_RGAD_track['raw']['name']        = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0xE000) >> 13;
755							$thisfile_mpeg_audio_lame_RGAD_track['raw']['originator']  = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x1C00) >> 10;
756							$thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit']    = ($thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x0200) >> 9;
757							$thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'] =  $thisfile_mpeg_audio_lame_raw['RGAD_track'] & 0x01FF;
758							$thisfile_mpeg_audio_lame_RGAD_track['name']       = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['name']);
759							$thisfile_mpeg_audio_lame_RGAD_track['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['originator']);
760							$thisfile_mpeg_audio_lame_RGAD_track['gain_db']    = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_track['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_track['raw']['sign_bit']);
761
762							if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
763								$info['replay_gain']['track']['peak']   = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
764							}
765							$info['replay_gain']['track']['originator'] = $thisfile_mpeg_audio_lame_RGAD_track['originator'];
766							$info['replay_gain']['track']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_track['gain_db'];
767						} else {
768							unset($thisfile_mpeg_audio_lame_RGAD['track']);
769						}
770						if ($thisfile_mpeg_audio_lame_raw['RGAD_album'] != 0) {
771
772							$thisfile_mpeg_audio_lame_RGAD_album['raw']['name']        = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0xE000) >> 13;
773							$thisfile_mpeg_audio_lame_RGAD_album['raw']['originator']  = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x1C00) >> 10;
774							$thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit']    = ($thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x0200) >> 9;
775							$thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'] = $thisfile_mpeg_audio_lame_raw['RGAD_album'] & 0x01FF;
776							$thisfile_mpeg_audio_lame_RGAD_album['name']       = getid3_lib::RGADnameLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['name']);
777							$thisfile_mpeg_audio_lame_RGAD_album['originator'] = getid3_lib::RGADoriginatorLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['originator']);
778							$thisfile_mpeg_audio_lame_RGAD_album['gain_db']    = getid3_lib::RGADadjustmentLookup($thisfile_mpeg_audio_lame_RGAD_album['raw']['gain_adjust'], $thisfile_mpeg_audio_lame_RGAD_album['raw']['sign_bit']);
779
780							if (!empty($thisfile_mpeg_audio_lame_RGAD['peak_amplitude'])) {
781								$info['replay_gain']['album']['peak']   = $thisfile_mpeg_audio_lame_RGAD['peak_amplitude'];
782							}
783							$info['replay_gain']['album']['originator'] = $thisfile_mpeg_audio_lame_RGAD_album['originator'];
784							$info['replay_gain']['album']['adjustment'] = $thisfile_mpeg_audio_lame_RGAD_album['gain_db'];
785						} else {
786							unset($thisfile_mpeg_audio_lame_RGAD['album']);
787						}
788						if (empty($thisfile_mpeg_audio_lame_RGAD)) {
789							unset($thisfile_mpeg_audio_lame['RGAD']);
790						}
791
792
793						// byte $AF  Encoding flags + ATH Type
794						$EncodingFlagsATHtype = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xAF, 1));
795						$thisfile_mpeg_audio_lame['encoding_flags']['nspsytune']   = (bool) ($EncodingFlagsATHtype & 0x10);
796						$thisfile_mpeg_audio_lame['encoding_flags']['nssafejoint'] = (bool) ($EncodingFlagsATHtype & 0x20);
797						$thisfile_mpeg_audio_lame['encoding_flags']['nogap_next']  = (bool) ($EncodingFlagsATHtype & 0x40);
798						$thisfile_mpeg_audio_lame['encoding_flags']['nogap_prev']  = (bool) ($EncodingFlagsATHtype & 0x80);
799						$thisfile_mpeg_audio_lame['ath_type']                      =         $EncodingFlagsATHtype & 0x0F;
800
801						// byte $B0  if ABR {specified bitrate} else {minimal bitrate}
802						$thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB0, 1));
803						if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 2) { // Average BitRate (ABR)
804							$thisfile_mpeg_audio_lame['bitrate_abr'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
805						} elseif ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) { // Constant BitRate (CBR)
806							// ignore
807						} elseif ($thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'] > 0) { // Variable BitRate (VBR) - minimum bitrate
808							$thisfile_mpeg_audio_lame['bitrate_min'] = $thisfile_mpeg_audio_lame['raw']['abrbitrate_minbitrate'];
809						}
810
811						// bytes $B1-$B3  Encoder delays
812						$EncoderDelays = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB1, 3));
813						$thisfile_mpeg_audio_lame['encoder_delay'] = ($EncoderDelays & 0xFFF000) >> 12;
814						$thisfile_mpeg_audio_lame['end_padding']   =  $EncoderDelays & 0x000FFF;
815
816						// byte $B4  Misc
817						$MiscByte = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB4, 1));
818						$thisfile_mpeg_audio_lame_raw['noise_shaping']       = ($MiscByte & 0x03);
819						$thisfile_mpeg_audio_lame_raw['stereo_mode']         = ($MiscByte & 0x1C) >> 2;
820						$thisfile_mpeg_audio_lame_raw['not_optimal_quality'] = ($MiscByte & 0x20) >> 5;
821						$thisfile_mpeg_audio_lame_raw['source_sample_freq']  = ($MiscByte & 0xC0) >> 6;
822						$thisfile_mpeg_audio_lame['noise_shaping']       = $thisfile_mpeg_audio_lame_raw['noise_shaping'];
823						$thisfile_mpeg_audio_lame['stereo_mode']         = self::LAMEmiscStereoModeLookup($thisfile_mpeg_audio_lame_raw['stereo_mode']);
824						$thisfile_mpeg_audio_lame['not_optimal_quality'] = (bool) $thisfile_mpeg_audio_lame_raw['not_optimal_quality'];
825						$thisfile_mpeg_audio_lame['source_sample_freq']  = self::LAMEmiscSourceSampleFrequencyLookup($thisfile_mpeg_audio_lame_raw['source_sample_freq']);
826
827						// byte $B5  MP3 Gain
828						$thisfile_mpeg_audio_lame_raw['mp3_gain'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB5, 1), false, true);
829						$thisfile_mpeg_audio_lame['mp3_gain_db']     = (getid3_lib::RGADamplitude2dB(2) / 4) * $thisfile_mpeg_audio_lame_raw['mp3_gain'];
830						$thisfile_mpeg_audio_lame['mp3_gain_factor'] = pow(2, ($thisfile_mpeg_audio_lame['mp3_gain_db'] / 6));
831
832						// bytes $B6-$B7  Preset and surround info
833						$PresetSurroundBytes = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB6, 2));
834						// Reserved                                                    = ($PresetSurroundBytes & 0xC000);
835						$thisfile_mpeg_audio_lame_raw['surround_info'] = ($PresetSurroundBytes & 0x3800);
836						$thisfile_mpeg_audio_lame['surround_info']     = self::LAMEsurroundInfoLookup($thisfile_mpeg_audio_lame_raw['surround_info']);
837						$thisfile_mpeg_audio_lame['preset_used_id']    = ($PresetSurroundBytes & 0x07FF);
838						$thisfile_mpeg_audio_lame['preset_used']       = self::LAMEpresetUsedLookup($thisfile_mpeg_audio_lame);
839						if (!empty($thisfile_mpeg_audio_lame['preset_used_id']) && empty($thisfile_mpeg_audio_lame['preset_used'])) {
840							$info['warning'][] = 'Unknown LAME preset used ('.$thisfile_mpeg_audio_lame['preset_used_id'].') - please report to info@getid3.org';
841						}
842						if (($thisfile_mpeg_audio_lame['short_version'] == 'LAME3.90.') && !empty($thisfile_mpeg_audio_lame['preset_used_id'])) {
843							// this may change if 3.90.4 ever comes out
844							$thisfile_mpeg_audio_lame['short_version'] = 'LAME3.90.3';
845						}
846
847						// bytes $B8-$BB  MusicLength
848						$thisfile_mpeg_audio_lame['audio_bytes'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xB8, 4));
849						$ExpectedNumberOfAudioBytes = (($thisfile_mpeg_audio_lame['audio_bytes'] > 0) ? $thisfile_mpeg_audio_lame['audio_bytes'] : $thisfile_mpeg_audio['VBR_bytes']);
850
851						// bytes $BC-$BD  MusicCRC
852						$thisfile_mpeg_audio_lame['music_crc']    = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBC, 2));
853
854						// bytes $BE-$BF  CRC-16 of Info Tag
855						$thisfile_mpeg_audio_lame['lame_tag_crc'] = getid3_lib::BigEndian2Int(substr($headerstring, $LAMEtagOffsetContant + 0xBE, 2));
856
857
858						// LAME CBR
859						if ($thisfile_mpeg_audio_lame_raw['vbr_method'] == 1) {
860
861							$thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
862							$thisfile_mpeg_audio['bitrate'] = self::ClosestStandardMP3Bitrate($thisfile_mpeg_audio['bitrate']);
863							$info['audio']['bitrate'] = $thisfile_mpeg_audio['bitrate'];
864							//if (empty($thisfile_mpeg_audio['bitrate']) || (!empty($thisfile_mpeg_audio_lame['bitrate_min']) && ($thisfile_mpeg_audio_lame['bitrate_min'] != 255))) {
865							//	$thisfile_mpeg_audio['bitrate'] = $thisfile_mpeg_audio_lame['bitrate_min'];
866							//}
867
868						}
869
870					}
871				}
872
873			} else {
874
875				// not Fraunhofer or Xing VBR methods, most likely CBR (but could be VBR with no header)
876				$thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
877				if ($recursivesearch) {
878					$thisfile_mpeg_audio['bitrate_mode'] = 'vbr';
879					if ($this->RecursiveFrameScanning($offset, $nextframetestoffset, true)) {
880						$recursivesearch = false;
881						$thisfile_mpeg_audio['bitrate_mode'] = 'cbr';
882					}
883					if ($thisfile_mpeg_audio['bitrate_mode'] == 'vbr') {
884						$info['warning'][] = 'VBR file with no VBR header. Bitrate values calculated from actual frame bitrates.';
885					}
886				}
887
888			}
889
890		}
891
892		if (($ExpectedNumberOfAudioBytes > 0) && ($ExpectedNumberOfAudioBytes != ($info['avdataend'] - $info['avdataoffset']))) {
893			if ($ExpectedNumberOfAudioBytes > ($info['avdataend'] - $info['avdataoffset'])) {
894				if ($this->isDependencyFor('matroska') || $this->isDependencyFor('riff')) {
895					// ignore, audio data is broken into chunks so will always be data "missing"
896				}
897				elseif (($ExpectedNumberOfAudioBytes - ($info['avdataend'] - $info['avdataoffset'])) == 1) {
898					$this->warning('Last byte of data truncated (this is a known bug in Meracl ID3 Tag Writer before v1.3.5)');
899				}
900				else {
901					$this->warning('Probable truncated file: expecting '.$ExpectedNumberOfAudioBytes.' bytes of audio data, only found '.($info['avdataend'] - $info['avdataoffset']).' (short by '.($ExpectedNumberOfAudioBytes - ($info['avdataend'] - $info['avdataoffset'])).' bytes)');
902				}
903			} else {
904				if ((($info['avda…

Large files files are truncated, but you can click here to view the full file