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 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.audio-video.riff.php', __FILE__
, true);
19 class getid3_optimfrog
extends getid3_handler
24 public function Analyze() {
25 $info = &$this->getid3
->info
;
27 $info['fileformat'] = 'ofr';
28 $info['audio']['dataformat'] = 'ofr';
29 $info['audio']['bitrate_mode'] = 'vbr';
30 $info['audio']['lossless'] = true;
32 $this->fseek($info['avdataoffset']);
33 $OFRheader = $this->fread(8);
34 if (substr($OFRheader, 0, 5) == '*RIFF') {
36 return $this->ParseOptimFROGheader42();
38 } elseif (substr($OFRheader, 0, 3) == 'OFR') {
40 return $this->ParseOptimFROGheader45();
44 $this->error('Expecting "*RIFF" or "OFR " at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($OFRheader).'"');
45 unset($info['fileformat']);
52 public function ParseOptimFROGheader42() {
53 // for fileformat of v4.21 and older
55 $info = &$this->getid3
->info
;
56 $this->fseek($info['avdataoffset']);
57 $OptimFROGheaderData = $this->fread(45);
58 $info['avdataoffset'] = 45;
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;
67 if ($OrignalRIFFheaderSize > $OrignalRIFFdataSize) {
68 $info['avdataend'] -= ($OrignalRIFFheaderSize - $OrignalRIFFdataSize);
69 $this->fseek($info['avdataend']);
70 $RIFFdata .= $this->fread($OrignalRIFFheaderSize - $OrignalRIFFdataSize);
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);
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'];
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'];
93 unset($getid3_riff, $getid3_temp, $RIFFdata);
101 public function ParseOptimFROGheader45() {
102 // for fileformat of v4.50a and higher
104 $info = &$this->getid3
->info
;
106 $this->fseek($info['avdataoffset']);
107 while (!feof($this->getid3
->fp
) && ($this->ftell() < $info['avdataend'])) {
108 $BlockOffset = $this->ftell();
109 $BlockData = $this->fread(8);
111 $BlockName = substr($BlockData, 0, 4);
112 $BlockSize = getid3_lib
::LittleEndian2Int(substr($BlockData, 4, 4));
114 if ($BlockName == 'OFRX') {
117 if (!isset($info['ofr'][$BlockName])) {
118 $info['ofr'][$BlockName] = array();
120 $thisfile_ofr_thisblock = &$info['ofr'][$BlockName];
122 switch ($BlockName) {
126 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
127 $thisfile_ofr_thisblock['size'] = $BlockSize;
129 $info['audio']['encoder'] = 'OptimFROG 4.50 alpha';
130 switch ($BlockSize) {
137 $this->warning('"'.$BlockName.'" contains more data than expected (expected 12 or 15 bytes, found '.$BlockSize.' bytes)');
140 $BlockData .= $this->fread($BlockSize);
142 $thisfile_ofr_thisblock['total_samples'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $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']);
147 $thisfile_ofr_thisblock['channel_config'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 1));
148 $thisfile_ofr_thisblock['channels'] = $thisfile_ofr_thisblock['channel_config'];
150 $thisfile_ofr_thisblock['sample_rate'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 4));
153 if ($BlockSize > 12) {
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']);
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']);
165 $info['audio']['encoder'] = 'OptimFROG '.$thisfile_ofr_thisblock['encoder'];
166 $info['audio']['encoder_options'] = '--mode '.$thisfile_ofr_thisblock['compression'];
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;
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']);
186 // unlike other block types, there CAN be multiple COMP blocks
188 $COMPdata['offset'] = $BlockOffset;
189 $COMPdata['size'] = $BlockSize;
191 if ($info['avdataoffset'] == 0) {
192 $info['avdataoffset'] = $BlockOffset;
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
);
199 $COMPdata['crc_32'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 4));
201 $COMPdata['sample_count'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 4));
203 $COMPdata['raw']['sample_type'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 1));
204 $COMPdata['sample_type'] = $this->OptimFROGsampleTypeLookup($COMPdata['raw']['sample_type']);
206 $COMPdata['raw']['channel_configuration'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 1));
207 $COMPdata['channel_configuration'] = $this->OptimFROGchannelConfigurationLookup($COMPdata['raw']['channel_configuration']);
209 $COMPdata['raw']['algorithm_id'] = getid3_lib
::LittleEndian2Int(substr($BlockData, $offset, 2));
210 //$COMPdata['algorithm'] = OptimFROGalgorithmNameLookup($COMPdata['raw']['algorithm_id']);
213 if ($info['ofr']['OFR ']['size'] > 12) {
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']);
222 if ($COMPdata['crc_32'] == 0x454E4F4E) {
223 // ASCII value of 'NONE' - placeholder value in v4.50a
224 $COMPdata['crc_32'] = false;
227 $thisfile_ofr_thisblock[] = $COMPdata;
231 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
232 $thisfile_ofr_thisblock['size'] = $BlockSize;
234 $RIFFdata .= $this->fread($BlockSize);
238 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
239 $thisfile_ofr_thisblock['size'] = $BlockSize;
241 if ($BlockSize > 0) {
242 $RIFFdata .= $this->fread($BlockSize);
247 // block contains no useful meta data - simply note and skip
249 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
250 $thisfile_ofr_thisblock['size'] = $BlockSize;
252 $this->fseek($BlockSize, SEEK_CUR
);
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()');
263 $this->fseek($BlockSize, SEEK_CUR
);
270 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
271 $thisfile_ofr_thisblock['size'] = $BlockSize;
273 if ($BlockSize == 16) {
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'];
281 $this->warning('Expecting block size of 16 in "MD5 " chunk, found '.$BlockSize.' instead');
282 $this->fseek($BlockSize, SEEK_CUR
);
289 $thisfile_ofr_thisblock['offset'] = $BlockOffset;
290 $thisfile_ofr_thisblock['size'] = $BlockSize;
292 $this->warning('Unhandled OptimFROG block type "'.$BlockName.'" at offset '.$thisfile_ofr_thisblock['offset']);
293 $this->fseek($BlockSize, SEEK_CUR
);
297 if (isset($info['ofr']['TAIL']['offset'])) {
298 $info['avdataend'] = $info['ofr']['TAIL']['offset'];
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'];
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);
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'];
317 unset($getid3_riff, $getid3_temp, $RIFFdata);
323 * @param int $SampleType
325 * @return string|false
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)'
341 return (isset($OptimFROGsampleTypeLookup[$SampleType]) ?
$OptimFROGsampleTypeLookup[$SampleType] : false);
345 * @param int $SampleType
349 public static function OptimFROGbitsPerSampleTypeLookup($SampleType) {
350 static $OptimFROGbitsPerSampleTypeLookup = array(
363 return (isset($OptimFROGbitsPerSampleTypeLookup[$SampleType]) ?
$OptimFROGbitsPerSampleTypeLookup[$SampleType] : false);
367 * @param int $ChannelConfiguration
369 * @return string|false
371 public static function OptimFROGchannelConfigurationLookup($ChannelConfiguration) {
372 static $OptimFROGchannelConfigurationLookup = array(
376 return (isset($OptimFROGchannelConfigurationLookup[$ChannelConfiguration]) ?
$OptimFROGchannelConfigurationLookup[$ChannelConfiguration] : false);
380 * @param int $ChannelConfiguration
384 public static function OptimFROGchannelConfigNumChannelsLookup($ChannelConfiguration) {
385 static $OptimFROGchannelConfigNumChannelsLookup = array(
389 return (isset($OptimFROGchannelConfigNumChannelsLookup[$ChannelConfiguration]) ?
$OptimFROGchannelConfigNumChannelsLookup[$ChannelConfiguration] : false);
393 // static function OptimFROGalgorithmNameLookup($AlgorithID) {
394 // static $OptimFROGalgorithmNameLookup = array();
395 // return (isset($OptimFROGalgorithmNameLookup[$AlgorithID]) ? $OptimFROGalgorithmNameLookup[$AlgorithID] : false);
400 * @param int $EncoderID
404 public static function OptimFROGencoderNameLookup($EncoderID) {
405 // version = (encoderID >> 4) + 4500
406 // system = encoderID & 0xF
408 $EncoderVersion = number_format(((($EncoderID & 0xF0) >> 4) +
4500) / 1000, 3);
409 $EncoderSystemID = ($EncoderID & 0x0F);
411 static $OptimFROGencoderSystemLookup = array(
412 0x00 => 'Windows console',
413 0x01 => 'Linux console',
416 return $EncoderVersion.' ('.(isset($OptimFROGencoderSystemLookup[$EncoderSystemID]) ?
$OptimFROGencoderSystemLookup[$EncoderSystemID] : 'undefined encoder type (0x'.dechex($EncoderSystemID).')').')';
420 * @param int $CompressionID
424 public static function OptimFROGcompressionLookup($CompressionID) {
425 // mode = compression >> 3
426 // speedup = compression & 0x07
428 $CompressionModeID = ($CompressionID & 0xF8) >> 3;
429 //$CompressionSpeedupID = ($CompressionID & 0x07);
431 static $OptimFROGencoderModeLookup = array(
435 0x03 => 'extra', // extranew (some versions)
436 0x04 => 'best', // bestnew (some versions)
443 return (isset($OptimFROGencoderModeLookup[$CompressionModeID]) ?
$OptimFROGencoderModeLookup[$CompressionModeID] : 'undefined mode (0x'.str_pad(dechex($CompressionModeID), 2, '0', STR_PAD_LEFT
).')');
447 * @param int $CompressionID
451 public static function OptimFROGspeedupLookup($CompressionID) {
452 // mode = compression >> 3
453 // speedup = compression & 0x07
455 //$CompressionModeID = ($CompressionID & 0xF8) >> 3;
456 $CompressionSpeedupID = ($CompressionID & 0x07);
458 static $OptimFROGencoderSpeedupLookup = array(
463 return (isset($OptimFROGencoderSpeedupLookup[$CompressionSpeedupID]) ?
$OptimFROGencoderSpeedupLookup[$CompressionSpeedupID] : 'undefined mode (0x'.dechex($CompressionSpeedupID));