module.audio.optimfrog.php 16 KB

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