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 /////////////////////////////////////////////////////////////////
11 // module.audio.optimfrog.php //
12 // module for analyzing OptimFROG audio files //
13 // dependencies: module.audio.riff.php //
15 /////////////////////////////////////////////////////////////////
17 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
20 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.audio-video.riff.php', __FILE__
, true);
22 class getid3_optimfrog
extends getid3_handler
27 public function Analyze() {
28 $info = &$this->getid3
->info
;
30 $info['fileformat'] = 'ofr';
31 $info['audio']['dataformat'] = 'ofr';
32 $info['audio']['bitrate_mode'] = 'vbr';
33 $info['audio']['lossless'] = true;
35 $this->fseek($info['avdataoffset']);
36 $OFRheader = $this->fread(8);
37 if (substr($OFRheader, 0, 5) == '*RIFF') {
39 return $this->ParseOptimFROGheader42();
41 } elseif (substr($OFRheader, 0, 3) == 'OFR') {
43 return $this->ParseOptimFROGheader45();
47 $this->error('Expecting "*RIFF" or "OFR " at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($OFRheader).'"');
48 unset($info['fileformat']);
55 public function ParseOptimFROGheader42() {
56 // for fileformat of v4.21 and older
58 $info = &$this->getid3
->info
;
59 $this->fseek($info['avdataoffset']);
60 $OptimFROGheaderData = $this->fread(45);
61 $info['avdataoffset'] = 45;
63 $OptimFROGencoderVersion_raw = getid3_lib
::LittleEndian2Int(substr($OptimFROGheaderData, 0, 1));
64 $OptimFROGencoderVersion_major = floor($OptimFROGencoderVersion_raw / 10);
65 $OptimFROGencoderVersion_minor = $OptimFROGencoderVersion_raw - ($OptimFROGencoderVersion_major * 10);
66 $RIFFdata = substr($OptimFROGheaderData, 1, 44);
67 $OrignalRIFFheaderSize = getid3_lib
::LittleEndian2Int(substr($RIFFdata, 4, 4)) +
8;
68 $OrignalRIFFdataSize = getid3_lib
::LittleEndian2Int(substr($RIFFdata, 40, 4)) +
44;
70 if ($OrignalRIFFheaderSize > $OrignalRIFFdataSize) {
71 $info['avdataend'] -= ($OrignalRIFFheaderSize - $OrignalRIFFdataSize);
72 $this->fseek($info['avdataend']);
73 $RIFFdata .= $this->fread($OrignalRIFFheaderSize - $OrignalRIFFdataSize);
76 // move the data chunk after all other chunks (if any)
77 // so that the RIFF parser doesn't see EOF when trying
78 // to skip over the data chunk
79 $RIFFdata = substr($RIFFdata, 0, 36).substr($RIFFdata, 44).substr($RIFFdata, 36, 8);
81 $getid3_temp = new getID3();
82 $getid3_temp->openfile($this->getid3
->filename
, $this->getid3
->info
['filesize'], $this->getid3
->fp
);
83 $getid3_temp->info
['avdataoffset'] = $info['avdataoffset'];
84 $getid3_temp->info
['avdataend'] = $info['avdataend'];
85 $getid3_riff = new getid3_riff($getid3_temp);
86 $getid3_riff->ParseRIFFdata($RIFFdata);
87 $info['riff'] = $getid3_temp->info
['riff'];
89 $info['audio']['encoder'] = 'OptimFROG '.$OptimFROGencoderVersion_major.'.'.$OptimFROGencoderVersion_minor;
90 $info['audio']['channels'] = $info['riff']['audio'][0]['channels'];
91 $info['audio']['sample_rate'] = $info['riff']['audio'][0]['sample_rate'];
92 $info['audio']['bits_per_sample'] = $info['riff']['audio'][0]['bits_per_sample'];
93 $info['playtime_seconds'] = $OrignalRIFFdataSize / ($info['audio']['channels'] * $info['audio']['sample_rate'] * ($info['audio']['bits_per_sample'] / 8));
94 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
96 unset($getid3_riff, $getid3_temp, $RIFFdata);
104 public function ParseOptimFROGheader45() {
105 // for fileformat of v4.50a and higher
107 $info = &$this->getid3
->info
;
109 $this->fseek($info['avdataoffset']);
110 while (!feof($this->getid3
->fp
) && ($this->ftell() < $info['avdataend'])) {
111 $BlockOffset = $this->ftell();
112 $BlockData = $this->fread(8);
114 $BlockName = substr($BlockData, 0, 4);
115 $BlockSize = getid3_lib
::LittleEndian2Int(substr($BlockData, 4, 4));
117 if ($BlockName == 'OFRX') {
120 if (!isset($info['ofr'][$BlockName])) {
121 $info['ofr'][$BlockName] = array();
123 $thisfile_ofr_thisblock = &$info['ofr'][$BlockName];
125 switch ($BlockName) {
129 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
130 $thisfile_ofr_thisblock['size'] = $BlockSize;
132 $info['audio']['encoder'] = 'OptimFROG 4.50 alpha';
133 switch ($BlockSize) {
140 $this->warning('"'.$BlockName.'" contains more data than expected (expected 12 or 15 bytes, found '.$BlockSize.' bytes)');
143 $BlockData .= $this->fread($BlockSize);
145 $thisfile_ofr_thisblock['total_samples'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 6));
147 $thisfile_ofr_thisblock['raw']['sample_type'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 1));
148 $thisfile_ofr_thisblock['sample_type'] = $this->OptimFROGsampleTypeLookup($thisfile_ofr_thisblock['raw']['sample_type']);
150 $thisfile_ofr_thisblock['channel_config'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 1));
151 $thisfile_ofr_thisblock['channels'] = $thisfile_ofr_thisblock['channel_config'];
153 $thisfile_ofr_thisblock['sample_rate'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 4));
156 if ($BlockSize > 12) {
158 // OFR 4.504b or higher
159 $thisfile_ofr_thisblock['channels'] = $this->OptimFROGchannelConfigNumChannelsLookup($thisfile_ofr_thisblock['channel_config']);
160 $thisfile_ofr_thisblock['raw']['encoder_id'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 2));
161 $thisfile_ofr_thisblock['encoder'] = $this->OptimFROGencoderNameLookup($thisfile_ofr_thisblock['raw']['encoder_id']);
163 $thisfile_ofr_thisblock['raw']['compression'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 1));
164 $thisfile_ofr_thisblock['compression'] = $this->OptimFROGcompressionLookup($thisfile_ofr_thisblock['raw']['compression']);
165 $thisfile_ofr_thisblock['speedup'] = $this->OptimFROGspeedupLookup($thisfile_ofr_thisblock['raw']['compression']);
168 $info['audio']['encoder'] = 'OptimFROG '.$thisfile_ofr_thisblock['encoder'];
169 $info['audio']['encoder_options'] = '--mode '.$thisfile_ofr_thisblock['compression'];
171 if ((($thisfile_ofr_thisblock['raw']['encoder_id'] & 0xF0) >> 4) == 7) { // v4.507
172 if (strtolower(getid3_lib
::fileextension($info['filename'])) == 'ofs') {
173 // OptimFROG DualStream format is lossy, but as of v4.507 there is no way to tell the difference
174 // between lossless and lossy other than the file extension.
175 $info['audio']['dataformat'] = 'ofs';
176 $info['audio']['lossless'] = true;
182 $info['audio']['channels'] = $thisfile_ofr_thisblock['channels'];
183 $info['audio']['sample_rate'] = $thisfile_ofr_thisblock['sample_rate'];
184 $info['audio']['bits_per_sample'] = $this->OptimFROGbitsPerSampleTypeLookup($thisfile_ofr_thisblock['raw']['sample_type']);
189 // unlike other block types, there CAN be multiple COMP blocks
191 $COMPdata['offset'] = $BlockOffset;
192 $COMPdata['size'] = $BlockSize;
194 if ($info['avdataoffset'] == 0) {
195 $info['avdataoffset'] = $BlockOffset;
198 // Only interested in first 14 bytes (only first 12 needed for v4.50 alpha), not actual audio data
199 $BlockData .= $this->fread(14);
200 $this->fseek($BlockSize - 14, SEEK_CUR
);
202 $COMPdata['crc_32'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 4));
204 $COMPdata['sample_count'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 4));
206 $COMPdata['raw']['sample_type'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 1));
207 $COMPdata['sample_type'] = $this->OptimFROGsampleTypeLookup($COMPdata['raw']['sample_type']);
209 $COMPdata['raw']['channel_configuration'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 1));
210 $COMPdata['channel_configuration'] = $this->OptimFROGchannelConfigurationLookup($COMPdata['raw']['channel_configuration']);
212 $COMPdata['raw']['algorithm_id'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 2));
213 //$COMPdata['algorithm'] = OptimFROGalgorithmNameLookup($COMPdata['raw']['algorithm_id']);
216 if ($info['ofr']['OFR ']['size'] > 12) {
218 // OFR 4.504b or higher
219 $COMPdata['raw']['encoder_id'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 2));
220 $COMPdata['encoder'] = $this->OptimFROGencoderNameLookup($COMPdata['raw']['encoder_id']);
225 if ($COMPdata['crc_32'] == 0x454E4F4E) {
226 // ASCII value of 'NONE' - placeholder value in v4.50a
227 $COMPdata['crc_32'] = false;
230 $thisfile_ofr_thisblock[] = $COMPdata;
234 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
235 $thisfile_ofr_thisblock['size'] = $BlockSize;
237 $RIFFdata .= $this->fread($BlockSize);
241 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
242 $thisfile_ofr_thisblock['size'] = $BlockSize;
244 if ($BlockSize > 0) {
245 $RIFFdata .= $this->fread($BlockSize);
250 // block contains no useful meta data - simply note and skip
252 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
253 $thisfile_ofr_thisblock['size'] = $BlockSize;
255 $this->fseek($BlockSize, SEEK_CUR
);
262 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
263 $thisfile_ofr_thisblock['size'] = $BlockSize;
264 $this->warning('APEtag processing inside OptimFROG not supported in this version ('.$this->getid3
->version().') of getID3()');
266 $this->fseek($BlockSize, SEEK_CUR
);
273 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
274 $thisfile_ofr_thisblock['size'] = $BlockSize;
276 if ($BlockSize == 16) {
278 $thisfile_ofr_thisblock['md5_binary'] = $this->fread($BlockSize);
279 $thisfile_ofr_thisblock['md5_string'] = getid3_lib
::PrintHexBytes($thisfile_ofr_thisblock['md5_binary'], true, false, false);
280 $info['md5_data_source'] = $thisfile_ofr_thisblock['md5_string'];
284 $this->warning('Expecting block size of 16 in "MD5 " chunk, found '.$BlockSize.' instead');
285 $this->fseek($BlockSize, SEEK_CUR
);
292 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
293 $thisfile_ofr_thisblock['size'] = $BlockSize;
295 $this->warning('Unhandled OptimFROG block type "'.$BlockName.'" at offset '.$thisfile_ofr_thisblock['offset']);
296 $this->fseek($BlockSize, SEEK_CUR
);
300 if (isset($info['ofr']['TAIL']['offset'])) {
301 $info['avdataend'] = $info['ofr']['TAIL']['offset'];
304 $info['playtime_seconds'] = (float) $info['ofr']['OFR ']['total_samples'] / ($info['audio']['channels'] * $info['audio']['sample_rate']);
305 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
307 // move the data chunk after all other chunks (if any)
308 // so that the RIFF parser doesn't see EOF when trying
309 // to skip over the data chunk
310 $RIFFdata = substr($RIFFdata, 0, 36).substr($RIFFdata, 44).substr($RIFFdata, 36, 8);
312 $getid3_temp = new getID3();
313 $getid3_temp->openfile($this->getid3
->filename
, $this->getid3
->info
['filesize'], $this->getid3
->fp
);
314 $getid3_temp->info
['avdataoffset'] = $info['avdataoffset'];
315 $getid3_temp->info
['avdataend'] = $info['avdataend'];
316 $getid3_riff = new getid3_riff($getid3_temp);
317 $getid3_riff->ParseRIFFdata($RIFFdata);
318 $info['riff'] = $getid3_temp->info
['riff'];
320 unset($getid3_riff, $getid3_temp, $RIFFdata);
326 * @param int $SampleType
328 * @return string|false
330 public static function OptimFROGsampleTypeLookup($SampleType) {
331 static $OptimFROGsampleTypeLookup = array(
332 0 => 'unsigned int (8-bit)',
333 1 => 'signed int (8-bit)',
334 2 => 'unsigned int (16-bit)',
335 3 => 'signed int (16-bit)',
336 4 => 'unsigned int (24-bit)',
337 5 => 'signed int (24-bit)',
338 6 => 'unsigned int (32-bit)',
339 7 => 'signed int (32-bit)',
340 8 => 'float 0.24 (32-bit)',
341 9 => 'float 16.8 (32-bit)',
342 10 => 'float 24.0 (32-bit)'
344 return (isset($OptimFROGsampleTypeLookup[$SampleType]) ?
$OptimFROGsampleTypeLookup[$SampleType] : false);
348 * @param int $SampleType
352 public static function OptimFROGbitsPerSampleTypeLookup($SampleType) {
353 static $OptimFROGbitsPerSampleTypeLookup = array(
366 return (isset($OptimFROGbitsPerSampleTypeLookup[$SampleType]) ?
$OptimFROGbitsPerSampleTypeLookup[$SampleType] : false);
370 * @param int $ChannelConfiguration
372 * @return string|false
374 public static function OptimFROGchannelConfigurationLookup($ChannelConfiguration) {
375 static $OptimFROGchannelConfigurationLookup = array(
379 return (isset($OptimFROGchannelConfigurationLookup[$ChannelConfiguration]) ?
$OptimFROGchannelConfigurationLookup[$ChannelConfiguration] : false);
383 * @param int $ChannelConfiguration
387 public static function OptimFROGchannelConfigNumChannelsLookup($ChannelConfiguration) {
388 static $OptimFROGchannelConfigNumChannelsLookup = array(
392 return (isset($OptimFROGchannelConfigNumChannelsLookup[$ChannelConfiguration]) ?
$OptimFROGchannelConfigNumChannelsLookup[$ChannelConfiguration] : false);
396 // static function OptimFROGalgorithmNameLookup($AlgorithID) {
397 // static $OptimFROGalgorithmNameLookup = array();
398 // return (isset($OptimFROGalgorithmNameLookup[$AlgorithID]) ? $OptimFROGalgorithmNameLookup[$AlgorithID] : false);
403 * @param int $EncoderID
407 public static function OptimFROGencoderNameLookup($EncoderID) {
408 // version = (encoderID >> 4) + 4500
409 // system = encoderID & 0xF
411 $EncoderVersion = number_format(((($EncoderID & 0xF0) >> 4) +
4500) / 1000, 3);
412 $EncoderSystemID = ($EncoderID & 0x0F);
414 static $OptimFROGencoderSystemLookup = array(
415 0x00 => 'Windows console',
416 0x01 => 'Linux console',
419 return $EncoderVersion.' ('.(isset($OptimFROGencoderSystemLookup[$EncoderSystemID]) ?
$OptimFROGencoderSystemLookup[$EncoderSystemID] : 'undefined encoder type (0x'.dechex($EncoderSystemID).')').')';
423 * @param int $CompressionID
427 public static function OptimFROGcompressionLookup($CompressionID) {
428 // mode = compression >> 3
429 // speedup = compression & 0x07
431 $CompressionModeID = ($CompressionID & 0xF8) >> 3;
432 //$CompressionSpeedupID = ($CompressionID & 0x07);
434 static $OptimFROGencoderModeLookup = array(
438 0x03 => 'extra', // extranew (some versions)
439 0x04 => 'best', // bestnew (some versions)
446 return (isset($OptimFROGencoderModeLookup[$CompressionModeID]) ?
$OptimFROGencoderModeLookup[$CompressionModeID] : 'undefined mode (0x'.str_pad(dechex($CompressionModeID), 2, '0', STR_PAD_LEFT
).')');
450 * @param int $CompressionID
454 public static function OptimFROGspeedupLookup($CompressionID) {
455 // mode = compression >> 3
456 // speedup = compression & 0x07
458 //$CompressionModeID = ($CompressionID & 0xF8) >> 3;
459 $CompressionSpeedupID = ($CompressionID & 0x07);
461 static $OptimFROGencoderSpeedupLookup = array(
466 return (isset($OptimFROGencoderSpeedupLookup[$CompressionSpeedupID]) ?
$OptimFROGencoderSpeedupLookup[$CompressionSpeedupID] : 'undefined mode (0x'.dechex($CompressionSpeedupID));