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