ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
module.audio.flac.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.flac.php //
11 // module for analyzing FLAC and OggFLAC audio files //
12 // dependencies: module.audio.ogg.php //
13 // ///
15 
16 
17 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ogg.php', __FILE__, true);
18 
20 {
21 
22  function getid3_flac(&$fd, &$ThisFileInfo) {
23  // http://flac.sourceforge.net/format.html
24 
25  fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
26  $StreamMarker = fread($fd, 4);
27  if ($StreamMarker != 'fLaC') {
28  $ThisFileInfo['error'][] = 'Expecting "fLaC" at offset '.$ThisFileInfo['avdataoffset'].', found "'.$StreamMarker.'"';
29  return false;
30  }
31  $ThisFileInfo['fileformat'] = 'flac';
32  $ThisFileInfo['audio']['dataformat'] = 'flac';
33  $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
34  $ThisFileInfo['audio']['lossless'] = true;
35 
36  return getid3_flac::FLACparseMETAdata($fd, $ThisFileInfo);
37  }
38 
39 
40  function FLACparseMETAdata(&$fd, &$ThisFileInfo) {
41 
42  do {
43  $METAdataBlockOffset = ftell($fd);
44  $METAdataBlockHeader = fread($fd, 4);
45  $METAdataLastBlockFlag = (bool) (getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 0, 1)) & 0x80);
46  $METAdataBlockType = getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 0, 1)) & 0x7F;
47  $METAdataBlockLength = getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 1, 3));
48  $METAdataBlockTypeText = getid3_flac::FLACmetaBlockTypeLookup($METAdataBlockType);
49 
50  if ($METAdataBlockLength < 0) {
51  $ThisFileInfo['error'][] = 'corrupt or invalid METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$METAdataBlockType.') at offset '.$METAdataBlockOffset;
52  break;
53  }
54 
55  $ThisFileInfo['flac'][$METAdataBlockTypeText]['raw'] = array();
56  $ThisFileInfo_flac_METAdataBlockTypeText_raw = &$ThisFileInfo['flac'][$METAdataBlockTypeText]['raw'];
57 
58  $ThisFileInfo_flac_METAdataBlockTypeText_raw['offset'] = $METAdataBlockOffset;
59  $ThisFileInfo_flac_METAdataBlockTypeText_raw['last_meta_block'] = $METAdataLastBlockFlag;
60  $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_type'] = $METAdataBlockType;
61  $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_type_text'] = $METAdataBlockTypeText;
62  $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_length'] = $METAdataBlockLength;
63  $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'] = @fread($fd, $METAdataBlockLength);
64  $ThisFileInfo['avdataoffset'] = ftell($fd);
65 
66  switch ($METAdataBlockTypeText) {
67 
68  case 'STREAMINFO':
69  if (!getid3_flac::FLACparseSTREAMINFO($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'], $ThisFileInfo)) {
70  return false;
71  }
72  break;
73 
74  case 'PADDING':
75  // ignore
76  break;
77 
78  case 'APPLICATION':
79  if (!getid3_flac::FLACparseAPPLICATION($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'], $ThisFileInfo)) {
80  return false;
81  }
82  break;
83 
84  case 'SEEKTABLE':
85  if (!getid3_flac::FLACparseSEEKTABLE($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'], $ThisFileInfo)) {
86  return false;
87  }
88  break;
89 
90  case 'VORBIS_COMMENT':
91  $OldOffset = ftell($fd);
92  fseek($fd, 0 - $METAdataBlockLength, SEEK_CUR);
94  fseek($fd, $OldOffset, SEEK_SET);
95  break;
96 
97  case 'CUESHEET':
98  if (!getid3_flac::FLACparseCUESHEET($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'], $ThisFileInfo)) {
99  return false;
100  }
101  break;
102 
103  default:
104  $ThisFileInfo['warning'][] = 'Unhandled METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$METAdataBlockType.') at offset '.$METAdataBlockOffset;
105  break;
106  }
107 
108  } while ($METAdataLastBlockFlag === false);
109 
110 
111  if (isset($ThisFileInfo['flac']['STREAMINFO'])) {
112  $ThisFileInfo['flac']['compressed_audio_bytes'] = $ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset'];
113  $ThisFileInfo['flac']['uncompressed_audio_bytes'] = $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] * $ThisFileInfo['flac']['STREAMINFO']['channels'] * ($ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'] / 8);
114  if ($ThisFileInfo['flac']['uncompressed_audio_bytes'] == 0) {
115  $ThisFileInfo['error'][] = 'Corrupt FLAC file: uncompressed_audio_bytes == zero';
116  return false;
117  }
118  $ThisFileInfo['flac']['compression_ratio'] = $ThisFileInfo['flac']['compressed_audio_bytes'] / $ThisFileInfo['flac']['uncompressed_audio_bytes'];
119  }
120 
121  // set md5_data_source - built into flac 0.5+
122  if (isset($ThisFileInfo['flac']['STREAMINFO']['audio_signature'])) {
123 
124  if ($ThisFileInfo['flac']['STREAMINFO']['audio_signature'] === str_repeat("\x00", 16)) {
125 
126  $ThisFileInfo['warning'][] = 'FLAC STREAMINFO.audio_signature is null (known issue with libOggFLAC)';
127 
128  } else {
129 
130  $ThisFileInfo['md5_data_source'] = '';
131  $md5 = $ThisFileInfo['flac']['STREAMINFO']['audio_signature'];
132  for ($i = 0; $i < strlen($md5); $i++) {
133  $ThisFileInfo['md5_data_source'] .= str_pad(dechex(ord($md5{$i})), 2, '00', STR_PAD_LEFT);
134  }
135  if (!preg_match('/^[0-9a-f]{32}$/', $ThisFileInfo['md5_data_source'])) {
136  unset($ThisFileInfo['md5_data_source']);
137  }
138 
139  }
140 
141  }
142 
143  $ThisFileInfo['audio']['bits_per_sample'] = $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'];
144  if ($ThisFileInfo['audio']['bits_per_sample'] == 8) {
145  // special case
146  // must invert sign bit on all data bytes before MD5'ing to match FLAC's calculated value
147  // MD5sum calculates on unsigned bytes, but FLAC calculated MD5 on 8-bit audio data as signed
148  $ThisFileInfo['warning'][] = 'FLAC calculates MD5 data strangely on 8-bit audio, so the stored md5_data_source value will not match the decoded WAV file';
149  }
150  if (!empty($ThisFileInfo['ogg']['vendor'])) {
151  $ThisFileInfo['audio']['encoder'] = $ThisFileInfo['ogg']['vendor'];
152  }
153 
154  return true;
155  }
156 
157  function FLACmetaBlockTypeLookup($blocktype) {
158  static $FLACmetaBlockTypeLookup = array();
159  if (empty($FLACmetaBlockTypeLookup)) {
160  $FLACmetaBlockTypeLookup[0] = 'STREAMINFO';
161  $FLACmetaBlockTypeLookup[1] = 'PADDING';
162  $FLACmetaBlockTypeLookup[2] = 'APPLICATION';
163  $FLACmetaBlockTypeLookup[3] = 'SEEKTABLE';
164  $FLACmetaBlockTypeLookup[4] = 'VORBIS_COMMENT';
165  $FLACmetaBlockTypeLookup[5] = 'CUESHEET';
166  }
167  return (isset($FLACmetaBlockTypeLookup[$blocktype]) ? $FLACmetaBlockTypeLookup[$blocktype] : 'reserved');
168  }
169 
170  function FLACapplicationIDLookup($applicationid) {
171  static $FLACapplicationIDLookup = array();
172  if (empty($FLACapplicationIDLookup)) {
173  // http://flac.sourceforge.net/id.html
174  $FLACapplicationIDLookup[0x46746F6C] = 'flac-tools'; // 'Ftol'
175  $FLACapplicationIDLookup[0x46746F6C] = 'Sound Font FLAC'; // 'SFFL'
176  }
177  return (isset($FLACapplicationIDLookup[$applicationid]) ? $FLACapplicationIDLookup[$applicationid] : 'reserved');
178  }
179 
180  function FLACparseSTREAMINFO($METAdataBlockData, &$ThisFileInfo) {
181  $offset = 0;
182  $ThisFileInfo['flac']['STREAMINFO']['min_block_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 2));
183  $offset += 2;
184  $ThisFileInfo['flac']['STREAMINFO']['max_block_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 2));
185  $offset += 2;
186  $ThisFileInfo['flac']['STREAMINFO']['min_frame_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 3));
187  $offset += 3;
188  $ThisFileInfo['flac']['STREAMINFO']['max_frame_size'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 3));
189  $offset += 3;
190 
191  $SampleRateChannelsSampleBitsStreamSamples = getid3_lib::BigEndian2Bin(substr($METAdataBlockData, $offset, 8));
192  $ThisFileInfo['flac']['STREAMINFO']['sample_rate'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 0, 20));
193  $ThisFileInfo['flac']['STREAMINFO']['channels'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 20, 3)) + 1;
194  $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 23, 5)) + 1;
195  $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] = getid3_lib::Bin2Dec(substr($SampleRateChannelsSampleBitsStreamSamples, 28, 36));
196  $offset += 8;
197 
198  $ThisFileInfo['flac']['STREAMINFO']['audio_signature'] = substr($METAdataBlockData, $offset, 16);
199  $offset += 16;
200 
201  if (!empty($ThisFileInfo['flac']['STREAMINFO']['sample_rate'])) {
202 
203  $ThisFileInfo['audio']['bitrate_mode'] = 'vbr';
204  $ThisFileInfo['audio']['sample_rate'] = $ThisFileInfo['flac']['STREAMINFO']['sample_rate'];
205  $ThisFileInfo['audio']['channels'] = $ThisFileInfo['flac']['STREAMINFO']['channels'];
206  $ThisFileInfo['audio']['bits_per_sample'] = $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'];
207  $ThisFileInfo['playtime_seconds'] = $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] / $ThisFileInfo['flac']['STREAMINFO']['sample_rate'];
208  $ThisFileInfo['audio']['bitrate'] = (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8) / $ThisFileInfo['playtime_seconds'];
209 
210  } else {
211 
212  $ThisFileInfo['error'][] = 'Corrupt METAdata block: STREAMINFO';
213  return false;
214 
215  }
216  return true;
217  }
218 
219 
220  function FLACparseAPPLICATION($METAdataBlockData, &$ThisFileInfo) {
221  $offset = 0;
222  $ApplicationID = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 4));
223  $offset += 4;
224  $ThisFileInfo['flac']['APPLICATION'][$ApplicationID]['name'] = getid3_flac::FLACapplicationIDLookup($ApplicationID);
225  $ThisFileInfo['flac']['APPLICATION'][$ApplicationID]['data'] = substr($METAdataBlockData, $offset);
226  $offset = $METAdataBlockLength;
227 
228  return true;
229  }
230 
231 
232  function FLACparseSEEKTABLE($METAdataBlockData, &$ThisFileInfo) {
233  $offset = 0;
234  $METAdataBlockLength = strlen($METAdataBlockData);
235  $placeholderpattern = str_repeat("\xFF", 8);
236  while ($offset < $METAdataBlockLength) {
237  $SampleNumberString = substr($METAdataBlockData, $offset, 8);
238  $offset += 8;
239  if ($SampleNumberString == $placeholderpattern) {
240 
241  // placeholder point
242  @$ThisFileInfo['flac']['SEEKTABLE']['placeholders']++;
243  $offset += 10;
244 
245  } else {
246 
247  $SampleNumber = getid3_lib::BigEndian2Int($SampleNumberString);
248  $ThisFileInfo['flac']['SEEKTABLE'][$SampleNumber]['offset'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
249  $offset += 8;
250  $ThisFileInfo['flac']['SEEKTABLE'][$SampleNumber]['samples'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 2));
251  $offset += 2;
252 
253  }
254  }
255  return true;
256  }
257 
258  function FLACparseCUESHEET($METAdataBlockData, &$ThisFileInfo) {
259  $offset = 0;
260  $ThisFileInfo['flac']['CUESHEET']['media_catalog_number'] = trim(substr($METAdataBlockData, $offset, 128), "\0");
261  $offset += 128;
262  $ThisFileInfo['flac']['CUESHEET']['lead_in_samples'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
263  $offset += 8;
264  $ThisFileInfo['flac']['CUESHEET']['flags']['is_cd'] = (bool) (getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1)) & 0x80);
265  $offset += 1;
266 
267  $offset += 258; // reserved
268 
269  $ThisFileInfo['flac']['CUESHEET']['number_tracks'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
270  $offset += 1;
271 
272  for ($track = 0; $track < $ThisFileInfo['flac']['CUESHEET']['number_tracks']; $track++) {
273  $TrackSampleOffset = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
274  $offset += 8;
275  $TrackNumber = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
276  $offset += 1;
277 
278  $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['sample_offset'] = $TrackSampleOffset;
279 
280  $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['isrc'] = substr($METAdataBlockData, $offset, 12);
281  $offset += 12;
282 
283  $TrackFlagsRaw = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
284  $offset += 1;
285  $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['is_audio'] = (bool) ($TrackFlagsRaw & 0x80);
286  $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['pre_emphasis'] = (bool) ($TrackFlagsRaw & 0x40);
287 
288  $offset += 13; // reserved
289 
290  $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points'] = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
291  $offset += 1;
292 
293  for ($index = 0; $index < $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points']; $index++) {
294  $IndexSampleOffset = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 8));
295  $offset += 8;
296  $IndexNumber = getid3_lib::BigEndian2Int(substr($METAdataBlockData, $offset, 1));
297  $offset += 1;
298 
299  $offset += 3; // reserved
300 
301  $ThisFileInfo['flac']['CUESHEET']['tracks'][$TrackNumber]['indexes'][$IndexNumber] = $IndexSampleOffset;
302  }
303  }
304  return true;
305  }
306 
307 }
308 
309 ?>