module.audio.optimfrog.php 16 KB

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