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