module.audio.optimfrog.php 16 KB

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