module.audio.wavpack.php 19 KB

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