module.audio.wavpack.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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.wavpack.php //
  11. // module for analyzing WavPack v4.0+ Audio files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. class getid3_wavpack extends getid3_handler
  16. {
  17. function Analyze() {
  18. $info = &$this->getid3->info;
  19. fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
  20. while (true) {
  21. $wavpackheader = fread($this->getid3->fp, 32);
  22. if (ftell($this->getid3->fp) >= $info['avdataend']) {
  23. break;
  24. } elseif (feof($this->getid3->fp)) {
  25. break;
  26. } elseif (
  27. isset($info['wavpack']['blockheader']['total_samples']) &&
  28. isset($info['wavpack']['blockheader']['block_samples']) &&
  29. ($info['wavpack']['blockheader']['total_samples'] > 0) &&
  30. ($info['wavpack']['blockheader']['block_samples'] > 0) &&
  31. (!isset($info['wavpack']['riff_trailer_size']) || ($info['wavpack']['riff_trailer_size'] <= 0)) &&
  32. ((isset($info['wavpack']['config_flags']['md5_checksum']) && ($info['wavpack']['config_flags']['md5_checksum'] === false)) || !empty($info['md5_data_source']))) {
  33. break;
  34. }
  35. $blockheader_offset = ftell($this->getid3->fp) - 32;
  36. $blockheader_magic = substr($wavpackheader, 0, 4);
  37. $blockheader_size = getid3_lib::LittleEndian2Int(substr($wavpackheader, 4, 4));
  38. $magic = 'wvpk';
  39. if ($blockheader_magic != $magic) {
  40. $info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$blockheader_offset.', found "'.getid3_lib::PrintHexBytes($blockheader_magic).'"';
  41. switch (isset($info['audio']['dataformat']) ? $info['audio']['dataformat'] : '') {
  42. case 'wavpack':
  43. case 'wvc':
  44. break;
  45. default:
  46. unset($info['fileformat']);
  47. unset($info['audio']);
  48. unset($info['wavpack']);
  49. break;
  50. }
  51. return false;
  52. }
  53. if (empty($info['wavpack']['blockheader']['block_samples']) ||
  54. empty($info['wavpack']['blockheader']['total_samples']) ||
  55. ($info['wavpack']['blockheader']['block_samples'] <= 0) ||
  56. ($info['wavpack']['blockheader']['total_samples'] <= 0)) {
  57. // Also, it is possible that the first block might not have
  58. // any samples (block_samples == 0) and in this case you should skip blocks
  59. // until you find one with samples because the other information (like
  60. // total_samples) are not guaranteed to be correct until (block_samples > 0)
  61. // Finally, I have defined a format for files in which the length is not known
  62. // (for example when raw files are created using pipes). In these cases
  63. // total_samples will be -1 and you must seek to the final block to determine
  64. // the total number of samples.
  65. $info['audio']['dataformat'] = 'wavpack';
  66. $info['fileformat'] = 'wavpack';
  67. $info['audio']['lossless'] = true;
  68. $info['audio']['bitrate_mode'] = 'vbr';
  69. $info['wavpack']['blockheader']['offset'] = $blockheader_offset;
  70. $info['wavpack']['blockheader']['magic'] = $blockheader_magic;
  71. $info['wavpack']['blockheader']['size'] = $blockheader_size;
  72. if ($info['wavpack']['blockheader']['size'] >= 0x100000) {
  73. $info['error'][] = 'Expecting WavPack block size less than "0x100000", found "'.$info['wavpack']['blockheader']['size'].'" at offset '.$info['wavpack']['blockheader']['offset'];
  74. switch (isset($info['audio']['dataformat']) ? $info['audio']['dataformat'] : '') {
  75. case 'wavpack':
  76. case 'wvc':
  77. break;
  78. default:
  79. unset($info['fileformat']);
  80. unset($info['audio']);
  81. unset($info['wavpack']);
  82. break;
  83. }
  84. return false;
  85. }
  86. $info['wavpack']['blockheader']['minor_version'] = ord($wavpackheader{8});
  87. $info['wavpack']['blockheader']['major_version'] = ord($wavpackheader{9});
  88. if (($info['wavpack']['blockheader']['major_version'] != 4) ||
  89. (($info['wavpack']['blockheader']['minor_version'] < 4) &&
  90. ($info['wavpack']['blockheader']['minor_version'] > 16))) {
  91. $info['error'][] = 'Expecting WavPack version between "4.2" and "4.16", found version "'.$info['wavpack']['blockheader']['major_version'].'.'.$info['wavpack']['blockheader']['minor_version'].'" at offset '.$info['wavpack']['blockheader']['offset'];
  92. switch (isset($info['audio']['dataformat']) ? $info['audio']['dataformat'] : '') {
  93. case 'wavpack':
  94. case 'wvc':
  95. break;
  96. default:
  97. unset($info['fileformat']);
  98. unset($info['audio']);
  99. unset($info['wavpack']);
  100. break;
  101. }
  102. return false;
  103. }
  104. $info['wavpack']['blockheader']['track_number'] = ord($wavpackheader{10}); // unused
  105. $info['wavpack']['blockheader']['index_number'] = ord($wavpackheader{11}); // unused
  106. $info['wavpack']['blockheader']['total_samples'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 12, 4));
  107. $info['wavpack']['blockheader']['block_index'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 16, 4));
  108. $info['wavpack']['blockheader']['block_samples'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 20, 4));
  109. $info['wavpack']['blockheader']['flags_raw'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 24, 4));
  110. $info['wavpack']['blockheader']['crc'] = getid3_lib::LittleEndian2Int(substr($wavpackheader, 28, 4));
  111. $info['wavpack']['blockheader']['flags']['bytes_per_sample'] = 1 + ($info['wavpack']['blockheader']['flags_raw'] & 0x00000003);
  112. $info['wavpack']['blockheader']['flags']['mono'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000004);
  113. $info['wavpack']['blockheader']['flags']['hybrid'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000008);
  114. $info['wavpack']['blockheader']['flags']['joint_stereo'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000010);
  115. $info['wavpack']['blockheader']['flags']['cross_decorrelation'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000020);
  116. $info['wavpack']['blockheader']['flags']['hybrid_noiseshape'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000040);
  117. $info['wavpack']['blockheader']['flags']['ieee_32bit_float'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000080);
  118. $info['wavpack']['blockheader']['flags']['int_32bit'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000100);
  119. $info['wavpack']['blockheader']['flags']['hybrid_bitrate_noise'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000200);
  120. $info['wavpack']['blockheader']['flags']['hybrid_balance_noise'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000400);
  121. $info['wavpack']['blockheader']['flags']['multichannel_initial'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00000800);
  122. $info['wavpack']['blockheader']['flags']['multichannel_final'] = (bool) ($info['wavpack']['blockheader']['flags_raw'] & 0x00001000);
  123. $info['audio']['lossless'] = !$info['wavpack']['blockheader']['flags']['hybrid'];
  124. }
  125. while (!feof($this->getid3->fp) && (ftell($this->getid3->fp) < ($blockheader_offset + $blockheader_size + 8))) {
  126. $metablock = array('offset'=>ftell($this->getid3->fp));
  127. $metablockheader = fread($this->getid3->fp, 2);
  128. if (feof($this->getid3->fp)) {
  129. break;
  130. }
  131. $metablock['id'] = ord($metablockheader{0});
  132. $metablock['function_id'] = ($metablock['id'] & 0x3F);
  133. $metablock['function_name'] = $this->WavPackMetablockNameLookup($metablock['function_id']);
  134. // The 0x20 bit in the id of the meta subblocks (which is defined as
  135. // ID_OPTIONAL_DATA) is a permanent part of the id. The idea is that
  136. // if a decoder encounters an id that it does not know about, it uses
  137. // that "ID_OPTIONAL_DATA" flag to determine what to do. If it is set
  138. // then the decoder simply ignores the metadata, but if it is zero
  139. // then the decoder should quit because it means that an understanding
  140. // of the metadata is required to correctly decode the audio.
  141. $metablock['non_decoder'] = (bool) ($metablock['id'] & 0x20);
  142. $metablock['padded_data'] = (bool) ($metablock['id'] & 0x40);
  143. $metablock['large_block'] = (bool) ($metablock['id'] & 0x80);
  144. if ($metablock['large_block']) {
  145. $metablockheader .= fread($this->getid3->fp, 2);
  146. }
  147. $metablock['size'] = getid3_lib::LittleEndian2Int(substr($metablockheader, 1)) * 2; // size is stored in words
  148. $metablock['data'] = null;
  149. if ($metablock['size'] > 0) {
  150. switch ($metablock['function_id']) {
  151. case 0x21: // ID_RIFF_HEADER
  152. case 0x22: // ID_RIFF_TRAILER
  153. case 0x23: // ID_REPLAY_GAIN
  154. case 0x24: // ID_CUESHEET
  155. case 0x25: // ID_CONFIG_BLOCK
  156. case 0x26: // ID_MD5_CHECKSUM
  157. $metablock['data'] = fread($this->getid3->fp, $metablock['size']);
  158. if ($metablock['padded_data']) {
  159. // padded to the nearest even byte
  160. $metablock['size']--;
  161. $metablock['data'] = substr($metablock['data'], 0, -1);
  162. }
  163. break;
  164. case 0x00: // ID_DUMMY
  165. case 0x01: // ID_ENCODER_INFO
  166. case 0x02: // ID_DECORR_TERMS
  167. case 0x03: // ID_DECORR_WEIGHTS
  168. case 0x04: // ID_DECORR_SAMPLES
  169. case 0x05: // ID_ENTROPY_VARS
  170. case 0x06: // ID_HYBRID_PROFILE
  171. case 0x07: // ID_SHAPING_WEIGHTS
  172. case 0x08: // ID_FLOAT_INFO
  173. case 0x09: // ID_INT32_INFO
  174. case 0x0A: // ID_WV_BITSTREAM
  175. case 0x0B: // ID_WVC_BITSTREAM
  176. case 0x0C: // ID_WVX_BITSTREAM
  177. case 0x0D: // ID_CHANNEL_INFO
  178. fseek($this->getid3->fp, $metablock['offset'] + ($metablock['large_block'] ? 4 : 2) + $metablock['size'], SEEK_SET);
  179. break;
  180. default:
  181. $info['warning'][] = 'Unexpected metablock type "0x'.str_pad(dechex($metablock['function_id']), 2, '0', STR_PAD_LEFT).'" at offset '.$metablock['offset'];
  182. fseek($this->getid3->fp, $metablock['offset'] + ($metablock['large_block'] ? 4 : 2) + $metablock['size'], SEEK_SET);
  183. break;
  184. }
  185. switch ($metablock['function_id']) {
  186. case 0x21: // ID_RIFF_HEADER
  187. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
  188. $original_wav_filesize = getid3_lib::LittleEndian2Int(substr($metablock['data'], 4, 4));
  189. $getid3_temp = new getID3();
  190. $getid3_temp->openfile($this->getid3->filename);
  191. $getid3_riff = new getid3_riff($getid3_temp);
  192. $getid3_riff->ParseRIFFdata($metablock['data']);
  193. $metablock['riff'] = $getid3_temp->info['riff'];
  194. $info['audio']['sample_rate'] = $getid3_temp->info['riff']['raw']['fmt ']['nSamplesPerSec'];
  195. unset($getid3_riff, $getid3_temp);
  196. $metablock['riff']['original_filesize'] = $original_wav_filesize;
  197. $info['wavpack']['riff_trailer_size'] = $original_wav_filesize - $metablock['riff']['WAVE']['data'][0]['size'] - $metablock['riff']['header_size'];
  198. $info['playtime_seconds'] = $info['wavpack']['blockheader']['total_samples'] / $info['audio']['sample_rate'];
  199. // Safe RIFF header in case there's a RIFF footer later
  200. $metablockRIFFheader = $metablock['data'];
  201. break;
  202. case 0x22: // ID_RIFF_TRAILER
  203. $metablockRIFFfooter = $metablockRIFFheader.$metablock['data'];
  204. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
  205. $startoffset = $metablock['offset'] + ($metablock['large_block'] ? 4 : 2);
  206. $getid3_temp = new getID3();
  207. $getid3_temp->openfile($this->getid3->filename);
  208. $getid3_temp->info['avdataend'] = $info['avdataend'];
  209. $getid3_temp->info['fileformat'] = 'riff';
  210. $getid3_riff = new getid3_riff($getid3_temp);
  211. $metablock['riff'] = $getid3_riff->ParseRIFF($startoffset, $startoffset + $metablock['size']);
  212. if (!empty($metablock['riff']['INFO'])) {
  213. $getid3_riff->RIFFcommentsParse($metablock['riff']['INFO'], $metablock['comments']);
  214. $info['tags']['riff'] = $metablock['comments'];
  215. }
  216. unset($getid3_temp, $getid3_riff);
  217. break;
  218. case 0x23: // ID_REPLAY_GAIN
  219. $info['warning'][] = 'WavPack "Replay Gain" contents not yet handled by getID3() in metablock at offset '.$metablock['offset'];
  220. break;
  221. case 0x24: // ID_CUESHEET
  222. $info['warning'][] = 'WavPack "Cuesheet" contents not yet handled by getID3() in metablock at offset '.$metablock['offset'];
  223. break;
  224. case 0x25: // ID_CONFIG_BLOCK
  225. $metablock['flags_raw'] = getid3_lib::LittleEndian2Int(substr($metablock['data'], 0, 3));
  226. $metablock['flags']['adobe_mode'] = (bool) ($metablock['flags_raw'] & 0x000001); // "adobe" mode for 32-bit floats
  227. $metablock['flags']['fast_flag'] = (bool) ($metablock['flags_raw'] & 0x000002); // fast mode
  228. $metablock['flags']['very_fast_flag'] = (bool) ($metablock['flags_raw'] & 0x000004); // double fast
  229. $metablock['flags']['high_flag'] = (bool) ($metablock['flags_raw'] & 0x000008); // high quality mode
  230. $metablock['flags']['very_high_flag'] = (bool) ($metablock['flags_raw'] & 0x000010); // double high (not used yet)
  231. $metablock['flags']['bitrate_kbps'] = (bool) ($metablock['flags_raw'] & 0x000020); // bitrate is kbps, not bits / sample
  232. $metablock['flags']['auto_shaping'] = (bool) ($metablock['flags_raw'] & 0x000040); // automatic noise shaping
  233. $metablock['flags']['shape_override'] = (bool) ($metablock['flags_raw'] & 0x000080); // shaping mode specified
  234. $metablock['flags']['joint_override'] = (bool) ($metablock['flags_raw'] & 0x000100); // joint-stereo mode specified
  235. $metablock['flags']['copy_time'] = (bool) ($metablock['flags_raw'] & 0x000200); // copy file-time from source
  236. $metablock['flags']['create_exe'] = (bool) ($metablock['flags_raw'] & 0x000400); // create executable
  237. $metablock['flags']['create_wvc'] = (bool) ($metablock['flags_raw'] & 0x000800); // create correction file
  238. $metablock['flags']['optimize_wvc'] = (bool) ($metablock['flags_raw'] & 0x001000); // maximize bybrid compression
  239. $metablock['flags']['quality_mode'] = (bool) ($metablock['flags_raw'] & 0x002000); // psychoacoustic quality mode
  240. $metablock['flags']['raw_flag'] = (bool) ($metablock['flags_raw'] & 0x004000); // raw mode (not implemented yet)
  241. $metablock['flags']['calc_noise'] = (bool) ($metablock['flags_raw'] & 0x008000); // calc noise in hybrid mode
  242. $metablock['flags']['lossy_mode'] = (bool) ($metablock['flags_raw'] & 0x010000); // obsolete (for information)
  243. $metablock['flags']['extra_mode'] = (bool) ($metablock['flags_raw'] & 0x020000); // extra processing mode
  244. $metablock['flags']['skip_wvx'] = (bool) ($metablock['flags_raw'] & 0x040000); // no wvx stream w/ floats & big ints
  245. $metablock['flags']['md5_checksum'] = (bool) ($metablock['flags_raw'] & 0x080000); // compute & store MD5 signature
  246. $metablock['flags']['quiet_mode'] = (bool) ($metablock['flags_raw'] & 0x100000); // don't report progress %
  247. $info['wavpack']['config_flags'] = $metablock['flags'];
  248. $info['audio']['encoder_options'] = '';
  249. if ($info['wavpack']['blockheader']['flags']['hybrid']) {
  250. $info['audio']['encoder_options'] .= ' -b???';
  251. }
  252. $info['audio']['encoder_options'] .= ($metablock['flags']['adobe_mode'] ? ' -a' : '');
  253. $info['audio']['encoder_options'] .= ($metablock['flags']['optimize_wvc'] ? ' -cc' : '');
  254. $info['audio']['encoder_options'] .= ($metablock['flags']['create_exe'] ? ' -e' : '');
  255. $info['audio']['encoder_options'] .= ($metablock['flags']['fast_flag'] ? ' -f' : '');
  256. $info['audio']['encoder_options'] .= ($metablock['flags']['joint_override'] ? ' -j?' : '');
  257. $info['audio']['encoder_options'] .= ($metablock['flags']['high_flag'] ? ' -h' : '');
  258. $info['audio']['encoder_options'] .= ($metablock['flags']['md5_checksum'] ? ' -m' : '');
  259. $info['audio']['encoder_options'] .= ($metablock['flags']['calc_noise'] ? ' -n' : '');
  260. $info['audio']['encoder_options'] .= ($metablock['flags']['shape_override'] ? ' -s?' : '');
  261. $info['audio']['encoder_options'] .= ($metablock['flags']['extra_mode'] ? ' -x?' : '');
  262. if (!empty($info['audio']['encoder_options'])) {
  263. $info['audio']['encoder_options'] = trim($info['audio']['encoder_options']);
  264. } elseif (isset($info['audio']['encoder_options'])) {
  265. unset($info['audio']['encoder_options']);
  266. }
  267. break;
  268. case 0x26: // ID_MD5_CHECKSUM
  269. if (strlen($metablock['data']) == 16) {
  270. $info['md5_data_source'] = strtolower(getid3_lib::PrintHexBytes($metablock['data'], true, false, false));
  271. } else {
  272. $info['warning'][] = 'Expecting 16 bytes of WavPack "MD5 Checksum" in metablock at offset '.$metablock['offset'].', but found '.strlen($metablock['data']).' bytes';
  273. }
  274. break;
  275. case 0x00: // ID_DUMMY
  276. case 0x01: // ID_ENCODER_INFO
  277. case 0x02: // ID_DECORR_TERMS
  278. case 0x03: // ID_DECORR_WEIGHTS
  279. case 0x04: // ID_DECORR_SAMPLES
  280. case 0x05: // ID_ENTROPY_VARS
  281. case 0x06: // ID_HYBRID_PROFILE
  282. case 0x07: // ID_SHAPING_WEIGHTS
  283. case 0x08: // ID_FLOAT_INFO
  284. case 0x09: // ID_INT32_INFO
  285. case 0x0A: // ID_WV_BITSTREAM
  286. case 0x0B: // ID_WVC_BITSTREAM
  287. case 0x0C: // ID_WVX_BITSTREAM
  288. case 0x0D: // ID_CHANNEL_INFO
  289. unset($metablock);
  290. break;
  291. }
  292. }
  293. if (!empty($metablock)) {
  294. $info['wavpack']['metablocks'][] = $metablock;
  295. }
  296. }
  297. }
  298. $info['audio']['encoder'] = 'WavPack v'.$info['wavpack']['blockheader']['major_version'].'.'.str_pad($info['wavpack']['blockheader']['minor_version'], 2, '0', STR_PAD_LEFT);
  299. $info['audio']['bits_per_sample'] = $info['wavpack']['blockheader']['flags']['bytes_per_sample'] * 8;
  300. $info['audio']['channels'] = ($info['wavpack']['blockheader']['flags']['mono'] ? 1 : 2);
  301. if (!empty($info['playtime_seconds'])) {
  302. $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
  303. } else {
  304. $info['audio']['dataformat'] = 'wvc';
  305. }
  306. return true;
  307. }
  308. function WavPackMetablockNameLookup(&$id) {
  309. static $WavPackMetablockNameLookup = array(
  310. 0x00 => 'Dummy',
  311. 0x01 => 'Encoder Info',
  312. 0x02 => 'Decorrelation Terms',
  313. 0x03 => 'Decorrelation Weights',
  314. 0x04 => 'Decorrelation Samples',
  315. 0x05 => 'Entropy Variables',
  316. 0x06 => 'Hybrid Profile',
  317. 0x07 => 'Shaping Weights',
  318. 0x08 => 'Float Info',
  319. 0x09 => 'Int32 Info',
  320. 0x0A => 'WV Bitstream',
  321. 0x0B => 'WVC Bitstream',
  322. 0x0C => 'WVX Bitstream',
  323. 0x0D => 'Channel Info',
  324. 0x21 => 'RIFF header',
  325. 0x22 => 'RIFF trailer',
  326. 0x23 => 'Replay Gain',
  327. 0x24 => 'Cuesheet',
  328. 0x25 => 'Config Block',
  329. 0x26 => 'MD5 Checksum',
  330. );
  331. return (isset($WavPackMetablockNameLookup[$id]) ? $WavPackMetablockNameLookup[$id] : '');
  332. }
  333. }
  334. ?>