[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.audio.optimfrog.php
1 <?php
2
3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
10 // //
11 // module.audio.optimfrog.php //
12 // module for analyzing OptimFROG audio files //
13 // dependencies: module.audio.riff.php //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
18
19 class getid3_optimfrog extends getid3_handler
20 {
21 /**
22 * @return bool
23 */
24 public function Analyze() {
25 $info = &$this->getid3->info;
26
27 $info['fileformat'] = 'ofr';
28 $info['audio']['dataformat'] = 'ofr';
29 $info['audio']['bitrate_mode'] = 'vbr';
30 $info['audio']['lossless'] = true;
31
32 $this->fseek($info['avdataoffset']);
33 $OFRheader = $this->fread(8);
34 if (substr($OFRheader, 0, 5) == '*RIFF') {
35
36 return $this->ParseOptimFROGheader42();
37
38 } elseif (substr($OFRheader, 0, 3) == 'OFR') {
39
40 return $this->ParseOptimFROGheader45();
41
42 }
43
44 $this->error('Expecting "*RIFF" or "OFR " at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($OFRheader).'"');
45 unset($info['fileformat']);
46 return false;
47 }
48
49 /**
50 * @return bool
51 */
52 public function ParseOptimFROGheader42() {
53 // for fileformat of v4.21 and older
54
55 $info = &$this->getid3->info;
56 $this->fseek($info['avdataoffset']);
57 $OptimFROGheaderData = $this->fread(45);
58 $info['avdataoffset'] = 45;
59
60 $OptimFROGencoderVersion_raw = getid3_lib::LittleEndian2Int(substr($OptimFROGheaderData, 0, 1));
61 $OptimFROGencoderVersion_major = floor($OptimFROGencoderVersion_raw / 10);
62 $OptimFROGencoderVersion_minor = $OptimFROGencoderVersion_raw - ($OptimFROGencoderVersion_major * 10);
63 $RIFFdata = substr($OptimFROGheaderData, 1, 44);
64 $OrignalRIFFheaderSize = getid3_lib::LittleEndian2Int(substr($RIFFdata, 4, 4)) + 8;
65 $OrignalRIFFdataSize = getid3_lib::LittleEndian2Int(substr($RIFFdata, 40, 4)) + 44;
66
67 if ($OrignalRIFFheaderSize > $OrignalRIFFdataSize) {
68 $info['avdataend'] -= ($OrignalRIFFheaderSize - $OrignalRIFFdataSize);
69 $this->fseek($info['avdataend']);
70 $RIFFdata .= $this->fread($OrignalRIFFheaderSize - $OrignalRIFFdataSize);
71 }
72
73 // move the data chunk after all other chunks (if any)
74 // so that the RIFF parser doesn't see EOF when trying
75 // to skip over the data chunk
76 $RIFFdata = substr($RIFFdata, 0, 36).substr($RIFFdata, 44).substr($RIFFdata, 36, 8);
77
78 $getid3_temp = new getID3();
79 $getid3_temp->openfile($this->getid3->filename);
80 $getid3_temp->info['avdataoffset'] = $info['avdataoffset'];
81 $getid3_temp->info['avdataend'] = $info['avdataend'];
82 $getid3_riff = new getid3_riff($getid3_temp);
83 $getid3_riff->ParseRIFFdata($RIFFdata);
84 $info['riff'] = $getid3_temp->info['riff'];
85
86 $info['audio']['encoder'] = 'OptimFROG '.$OptimFROGencoderVersion_major.'.'.$OptimFROGencoderVersion_minor;
87 $info['audio']['channels'] = $info['riff']['audio'][0]['channels'];
88 $info['audio']['sample_rate'] = $info['riff']['audio'][0]['sample_rate'];
89 $info['audio']['bits_per_sample'] = $info['riff']['audio'][0]['bits_per_sample'];
90 $info['playtime_seconds'] = $OrignalRIFFdataSize / ($info['audio']['channels'] * $info['audio']['sample_rate'] * ($info['audio']['bits_per_sample'] / 8));
91 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
92
93 unset($getid3_riff, $getid3_temp, $RIFFdata);
94
95 return true;
96 }
97
98 /**
99 * @return bool
100 */
101 public function ParseOptimFROGheader45() {
102 // for fileformat of v4.50a and higher
103
104 $info = &$this->getid3->info;
105 $RIFFdata = '';
106 $this->fseek($info['avdataoffset']);
107 while (!feof($this->getid3->fp) && ($this->ftell() < $info['avdataend'])) {
108 $BlockOffset = $this->ftell();
109 $BlockData = $this->fread(8);
110 $offset = 8;
111 $BlockName = substr($BlockData, 0, 4);
112 $BlockSize = getid3_lib::LittleEndian2Int(substr($BlockData, 4, 4));
113
114 if ($BlockName == 'OFRX') {
115 $BlockName = 'OFR ';
116 }
117 if (!isset($info['ofr'][$BlockName])) {
118 $info['ofr'][$BlockName] = array();
119 }
120 $thisfile_ofr_thisblock = &$info['ofr'][$BlockName];
121
122 switch ($BlockName) {
123 case 'OFR ':
124
125 // shortcut
126 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
127 $thisfile_ofr_thisblock['size'] = $BlockSize;
128
129 $info['audio']['encoder'] = 'OptimFROG 4.50 alpha';
130 switch ($BlockSize) {
131 case 12:
132 case 15:
133 // good
134 break;
135
136 default:
137 $this->warning('"'.$BlockName.'" contains more data than expected (expected 12 or 15 bytes, found '.$BlockSize.' bytes)');
138 break;
139 }
140 $BlockData .= $this->fread($BlockSize);
141
142 $thisfile_ofr_thisblock['total_samples'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 6));
143 $offset += 6;
144 $thisfile_ofr_thisblock['raw']['sample_type'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 1));
145 $thisfile_ofr_thisblock['sample_type'] = $this->OptimFROGsampleTypeLookup($thisfile_ofr_thisblock['raw']['sample_type']);
146 $offset += 1;
147 $thisfile_ofr_thisblock['channel_config'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 1));
148 $thisfile_ofr_thisblock['channels'] = $thisfile_ofr_thisblock['channel_config'];
149 $offset += 1;
150 $thisfile_ofr_thisblock['sample_rate'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 4));
151 $offset += 4;
152
153 if ($BlockSize > 12) {
154
155 // OFR 4.504b or higher
156 $thisfile_ofr_thisblock['channels'] = $this->OptimFROGchannelConfigNumChannelsLookup($thisfile_ofr_thisblock['channel_config']);
157 $thisfile_ofr_thisblock['raw']['encoder_id'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 2));
158 $thisfile_ofr_thisblock['encoder'] = $this->OptimFROGencoderNameLookup($thisfile_ofr_thisblock['raw']['encoder_id']);
159 $offset += 2;
160 $thisfile_ofr_thisblock['raw']['compression'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 1));
161 $thisfile_ofr_thisblock['compression'] = $this->OptimFROGcompressionLookup($thisfile_ofr_thisblock['raw']['compression']);
162 $thisfile_ofr_thisblock['speedup'] = $this->OptimFROGspeedupLookup($thisfile_ofr_thisblock['raw']['compression']);
163 $offset += 1;
164
165 $info['audio']['encoder'] = 'OptimFROG '.$thisfile_ofr_thisblock['encoder'];
166 $info['audio']['encoder_options'] = '--mode '.$thisfile_ofr_thisblock['compression'];
167
168 if ((($thisfile_ofr_thisblock['raw']['encoder_id'] & 0xF0) >> 4) == 7) { // v4.507
169 if (strtolower(getid3_lib::fileextension($info['filename'])) == 'ofs') {
170 // OptimFROG DualStream format is lossy, but as of v4.507 there is no way to tell the difference
171 // between lossless and lossy other than the file extension.
172 $info['audio']['dataformat'] = 'ofs';
173 $info['audio']['lossless'] = true;
174 }
175 }
176
177 }
178
179 $info['audio']['channels'] = $thisfile_ofr_thisblock['channels'];
180 $info['audio']['sample_rate'] = $thisfile_ofr_thisblock['sample_rate'];
181 $info['audio']['bits_per_sample'] = $this->OptimFROGbitsPerSampleTypeLookup($thisfile_ofr_thisblock['raw']['sample_type']);
182 break;
183
184
185 case 'COMP':
186 // unlike other block types, there CAN be multiple COMP blocks
187
188 $COMPdata['offset'] = $BlockOffset;
189 $COMPdata['size'] = $BlockSize;
190
191 if ($info['avdataoffset'] == 0) {
192 $info['avdataoffset'] = $BlockOffset;
193 }
194
195 // Only interested in first 14 bytes (only first 12 needed for v4.50 alpha), not actual audio data
196 $BlockData .= $this->fread(14);
197 $this->fseek($BlockSize - 14, SEEK_CUR);
198
199 $COMPdata['crc_32'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 4));
200 $offset += 4;
201 $COMPdata['sample_count'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 4));
202 $offset += 4;
203 $COMPdata['raw']['sample_type'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 1));
204 $COMPdata['sample_type'] = $this->OptimFROGsampleTypeLookup($COMPdata['raw']['sample_type']);
205 $offset += 1;
206 $COMPdata['raw']['channel_configuration'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 1));
207 $COMPdata['channel_configuration'] = $this->OptimFROGchannelConfigurationLookup($COMPdata['raw']['channel_configuration']);
208 $offset += 1;
209 $COMPdata['raw']['algorithm_id'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 2));
210 //$COMPdata['algorithm'] = OptimFROGalgorithmNameLookup($COMPdata['raw']['algorithm_id']);
211 $offset += 2;
212
213 if ($info['ofr']['OFR ']['size'] > 12) {
214
215 // OFR 4.504b or higher
216 $COMPdata['raw']['encoder_id'] = getid3_lib::LittleEndian2Int(substr($BlockData, $offset, 2));
217 $COMPdata['encoder'] = $this->OptimFROGencoderNameLookup($COMPdata['raw']['encoder_id']);
218 $offset += 2;
219
220 }
221
222 if ($COMPdata['crc_32'] == 0x454E4F4E) {
223 // ASCII value of 'NONE' - placeholder value in v4.50a
224 $COMPdata['crc_32'] = false;
225 }
226
227 $thisfile_ofr_thisblock[] = $COMPdata;
228 break;
229
230 case 'HEAD':
231 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
232 $thisfile_ofr_thisblock['size'] = $BlockSize;
233
234 $RIFFdata .= $this->fread($BlockSize);
235 break;
236
237 case 'TAIL':
238 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
239 $thisfile_ofr_thisblock['size'] = $BlockSize;
240
241 if ($BlockSize > 0) {
242 $RIFFdata .= $this->fread($BlockSize);
243 }
244 break;
245
246 case 'RECV':
247 // block contains no useful meta data - simply note and skip
248
249 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
250 $thisfile_ofr_thisblock['size'] = $BlockSize;
251
252 $this->fseek($BlockSize, SEEK_CUR);
253 break;
254
255
256 case 'APET':
257 // APEtag v2
258
259 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
260 $thisfile_ofr_thisblock['size'] = $BlockSize;
261 $this->warning('APEtag processing inside OptimFROG not supported in this version ('.$this->getid3->version().') of getID3()');
262
263 $this->fseek($BlockSize, SEEK_CUR);
264 break;
265
266
267 case 'MD5 ':
268 // APEtag v2
269
270 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
271 $thisfile_ofr_thisblock['size'] = $BlockSize;
272
273 if ($BlockSize == 16) {
274
275 $thisfile_ofr_thisblock['md5_binary'] = $this->fread($BlockSize);
276 $thisfile_ofr_thisblock['md5_string'] = getid3_lib::PrintHexBytes($thisfile_ofr_thisblock['md5_binary'], true, false, false);
277 $info['md5_data_source'] = $thisfile_ofr_thisblock['md5_string'];
278
279 } else {
280
281 $this->warning('Expecting block size of 16 in "MD5 " chunk, found '.$BlockSize.' instead');
282 $this->fseek($BlockSize, SEEK_CUR);
283
284 }
285 break;
286
287
288 default:
289 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
290 $thisfile_ofr_thisblock['size'] = $BlockSize;
291
292 $this->warning('Unhandled OptimFROG block type "'.$BlockName.'" at offset '.$thisfile_ofr_thisblock['offset']);
293 $this->fseek($BlockSize, SEEK_CUR);
294 break;
295 }
296 }
297 if (isset($info['ofr']['TAIL']['offset'])) {
298 $info['avdataend'] = $info['ofr']['TAIL']['offset'];
299 }
300
301 $info['playtime_seconds'] = (float) $info['ofr']['OFR ']['total_samples'] / ($info['audio']['channels'] * $info['audio']['sample_rate']);
302 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
303
304 // move the data chunk after all other chunks (if any)
305 // so that the RIFF parser doesn't see EOF when trying
306 // to skip over the data chunk
307 $RIFFdata = substr($RIFFdata, 0, 36).substr($RIFFdata, 44).substr($RIFFdata, 36, 8);
308
309 $getid3_temp = new getID3();
310 $getid3_temp->openfile($this->getid3->filename);
311 $getid3_temp->info['avdataoffset'] = $info['avdataoffset'];
312 $getid3_temp->info['avdataend'] = $info['avdataend'];
313 $getid3_riff = new getid3_riff($getid3_temp);
314 $getid3_riff->ParseRIFFdata($RIFFdata);
315 $info['riff'] = $getid3_temp->info['riff'];
316
317 unset($getid3_riff, $getid3_temp, $RIFFdata);
318
319 return true;
320 }
321
322 /**
323 * @param int $SampleType
324 *
325 * @return string|false
326 */
327 public static function OptimFROGsampleTypeLookup($SampleType) {
328 static $OptimFROGsampleTypeLookup = array(
329 0 => 'unsigned int (8-bit)',
330 1 => 'signed int (8-bit)',
331 2 => 'unsigned int (16-bit)',
332 3 => 'signed int (16-bit)',
333 4 => 'unsigned int (24-bit)',
334 5 => 'signed int (24-bit)',
335 6 => 'unsigned int (32-bit)',
336 7 => 'signed int (32-bit)',
337 8 => 'float 0.24 (32-bit)',
338 9 => 'float 16.8 (32-bit)',
339 10 => 'float 24.0 (32-bit)'
340 );
341 return (isset($OptimFROGsampleTypeLookup[$SampleType]) ? $OptimFROGsampleTypeLookup[$SampleType] : false);
342 }
343
344 /**
345 * @param int $SampleType
346 *
347 * @return int|false
348 */
349 public static function OptimFROGbitsPerSampleTypeLookup($SampleType) {
350 static $OptimFROGbitsPerSampleTypeLookup = array(
351 0 => 8,
352 1 => 8,
353 2 => 16,
354 3 => 16,
355 4 => 24,
356 5 => 24,
357 6 => 32,
358 7 => 32,
359 8 => 32,
360 9 => 32,
361 10 => 32
362 );
363 return (isset($OptimFROGbitsPerSampleTypeLookup[$SampleType]) ? $OptimFROGbitsPerSampleTypeLookup[$SampleType] : false);
364 }
365
366 /**
367 * @param int $ChannelConfiguration
368 *
369 * @return string|false
370 */
371 public static function OptimFROGchannelConfigurationLookup($ChannelConfiguration) {
372 static $OptimFROGchannelConfigurationLookup = array(
373 0 => 'mono',
374 1 => 'stereo'
375 );
376 return (isset($OptimFROGchannelConfigurationLookup[$ChannelConfiguration]) ? $OptimFROGchannelConfigurationLookup[$ChannelConfiguration] : false);
377 }
378
379 /**
380 * @param int $ChannelConfiguration
381 *
382 * @return int|false
383 */
384 public static function OptimFROGchannelConfigNumChannelsLookup($ChannelConfiguration) {
385 static $OptimFROGchannelConfigNumChannelsLookup = array(
386 0 => 1,
387 1 => 2
388 );
389 return (isset($OptimFROGchannelConfigNumChannelsLookup[$ChannelConfiguration]) ? $OptimFROGchannelConfigNumChannelsLookup[$ChannelConfiguration] : false);
390 }
391
392
393 // static function OptimFROGalgorithmNameLookup($AlgorithID) {
394 // static $OptimFROGalgorithmNameLookup = array();
395 // return (isset($OptimFROGalgorithmNameLookup[$AlgorithID]) ? $OptimFROGalgorithmNameLookup[$AlgorithID] : false);
396 // }
397
398
399 /**
400 * @param int $EncoderID
401 *
402 * @return string
403 */
404 public static function OptimFROGencoderNameLookup($EncoderID) {
405 // version = (encoderID >> 4) + 4500
406 // system = encoderID & 0xF
407
408 $EncoderVersion = number_format(((($EncoderID & 0xF0) >> 4) + 4500) / 1000, 3);
409 $EncoderSystemID = ($EncoderID & 0x0F);
410
411 static $OptimFROGencoderSystemLookup = array(
412 0x00 => 'Windows console',
413 0x01 => 'Linux console',
414 0x0F => 'unknown'
415 );
416 return $EncoderVersion.' ('.(isset($OptimFROGencoderSystemLookup[$EncoderSystemID]) ? $OptimFROGencoderSystemLookup[$EncoderSystemID] : 'undefined encoder type (0x'.dechex($EncoderSystemID).')').')';
417 }
418
419 /**
420 * @param int $CompressionID
421 *
422 * @return string
423 */
424 public static function OptimFROGcompressionLookup($CompressionID) {
425 // mode = compression >> 3
426 // speedup = compression & 0x07
427
428 $CompressionModeID = ($CompressionID & 0xF8) >> 3;
429 //$CompressionSpeedupID = ($CompressionID & 0x07);
430
431 static $OptimFROGencoderModeLookup = array(
432 0x00 => 'fast',
433 0x01 => 'normal',
434 0x02 => 'high',
435 0x03 => 'extra', // extranew (some versions)
436 0x04 => 'best', // bestnew (some versions)
437 0x05 => 'ultra',
438 0x06 => 'insane',
439 0x07 => 'highnew',
440 0x08 => 'extranew',
441 0x09 => 'bestnew'
442 );
443 return (isset($OptimFROGencoderModeLookup[$CompressionModeID]) ? $OptimFROGencoderModeLookup[$CompressionModeID] : 'undefined mode (0x'.str_pad(dechex($CompressionModeID), 2, '0', STR_PAD_LEFT).')');
444 }
445
446 /**
447 * @param int $CompressionID
448 *
449 * @return string
450 */
451 public static function OptimFROGspeedupLookup($CompressionID) {
452 // mode = compression >> 3
453 // speedup = compression & 0x07
454
455 //$CompressionModeID = ($CompressionID & 0xF8) >> 3;
456 $CompressionSpeedupID = ($CompressionID & 0x07);
457
458 static $OptimFROGencoderSpeedupLookup = array(
459 0x00 => '1x',
460 0x01 => '2x',
461 0x02 => '4x'
462 );
463 return (isset($OptimFROGencoderSpeedupLookup[$CompressionSpeedupID]) ? $OptimFROGencoderSpeedupLookup[$CompressionSpeedupID] : 'undefined mode (0x'.dechex($CompressionSpeedupID));
464 }
465
466 }