module.audio.optimfrog.php 17 KB

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