1cea4364c2a0e8d7a4195466ac793a8be875caf8
3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.audio.flac.php //
12 // module for analyzing FLAC and OggFLAC audio files //
13 // dependencies: module.audio.ogg.php //
15 /////////////////////////////////////////////////////////////////
17 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
20 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.audio.ogg.php', __FILE__
, true);
23 * @tutorial http://flac.sourceforge.net/format.html
25 class getid3_flac
extends getid3_handler
27 const syncword
= 'fLaC';
32 public function Analyze() {
33 $info = &$this->getid3
->info
;
35 $this->fseek($info['avdataoffset']);
36 $StreamMarker = $this->fread(4);
37 if ($StreamMarker != self
::syncword
) {
38 return $this->error('Expecting "'.getid3_lib
::PrintHexBytes(self
::syncword
).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($StreamMarker).'"');
40 $info['fileformat'] = 'flac';
41 $info['audio']['dataformat'] = 'flac';
42 $info['audio']['bitrate_mode'] = 'vbr';
43 $info['audio']['lossless'] = true;
45 // parse flac container
46 return $this->parseMETAdata();
52 public function parseMETAdata() {
53 $info = &$this->getid3
->info
;
55 $BlockOffset = $this->ftell();
56 $BlockHeader = $this->fread(4);
57 $LBFBT = getid3_lib
::BigEndian2Int(substr($BlockHeader, 0, 1)); // LBFBT = LastBlockFlag + BlockType
58 $LastBlockFlag = (bool) ($LBFBT & 0x80);
59 $BlockType = ($LBFBT & 0x7F);
60 $BlockLength = getid3_lib
::BigEndian2Int(substr($BlockHeader, 1, 3));
61 $BlockTypeText = self
::metaBlockTypeLookup($BlockType);
63 if (($BlockOffset +
4 +
$BlockLength) > $info['avdataend']) {
64 $this->warning('METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$BlockTypeText.') at offset '.$BlockOffset.' extends beyond end of file');
67 if ($BlockLength < 1) {
68 if ($BlockTypeText != 'reserved') {
69 // probably supposed to be zero-length
70 $this->warning('METADATA_BLOCK_HEADER.BLOCK_LENGTH ('.$BlockTypeText.') at offset '.$BlockOffset.' is zero bytes');
73 $this->error('METADATA_BLOCK_HEADER.BLOCK_LENGTH ('.$BlockLength.') at offset '.$BlockOffset.' is invalid');
77 $info['flac'][$BlockTypeText]['raw'] = array();
78 $BlockTypeText_raw = &$info['flac'][$BlockTypeText]['raw'];
80 $BlockTypeText_raw['offset'] = $BlockOffset;
81 $BlockTypeText_raw['last_meta_block'] = $LastBlockFlag;
82 $BlockTypeText_raw['block_type'] = $BlockType;
83 $BlockTypeText_raw['block_type_text'] = $BlockTypeText;
84 $BlockTypeText_raw['block_length'] = $BlockLength;
85 if ($BlockTypeText_raw['block_type'] != 0x06) { // do not read attachment data automatically
86 $BlockTypeText_raw['block_data'] = $this->fread($BlockLength);
89 switch ($BlockTypeText) {
90 case 'STREAMINFO': // 0x00
91 if (!$this->parseSTREAMINFO($BlockTypeText_raw['block_data'])) {
96 case 'PADDING': // 0x01
97 unset($info['flac']['PADDING']); // ignore
100 case 'APPLICATION': // 0x02
101 if (!$this->parseAPPLICATION($BlockTypeText_raw['block_data'])) {
106 case 'SEEKTABLE': // 0x03
107 if (!$this->parseSEEKTABLE($BlockTypeText_raw['block_data'])) {
112 case 'VORBIS_COMMENT': // 0x04
113 if (!$this->parseVORBIS_COMMENT($BlockTypeText_raw['block_data'])) {
118 case 'CUESHEET': // 0x05
119 if (!$this->parseCUESHEET($BlockTypeText_raw['block_data'])) {
124 case 'PICTURE': // 0x06
125 if (!$this->parsePICTURE()) {
131 $this->warning('Unhandled METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$BlockType.') at offset '.$BlockOffset);
134 unset($info['flac'][$BlockTypeText]['raw']);
135 $info['avdataoffset'] = $this->ftell();
137 while ($LastBlockFlag === false);
140 if (!empty($info['flac']['VORBIS_COMMENT']['comments'])) {
141 $info['flac']['comments'] = $info['flac']['VORBIS_COMMENT']['comments'];
143 if (!empty($info['flac']['VORBIS_COMMENT']['vendor'])) {
144 $info['audio']['encoder'] = str_replace('reference ', '', $info['flac']['VORBIS_COMMENT']['vendor']);
147 // copy attachments to 'comments' array if nesesary
148 if (isset($info['flac']['PICTURE']) && ($this->getid3
->option_save_attachments
!== getID3
::ATTACHMENTS_NONE
)) {
149 foreach ($info['flac']['PICTURE'] as $entry) {
150 if (!empty($entry['data'])) {
151 if (!isset($info['flac']['comments']['picture'])) {
152 $info['flac']['comments']['picture'] = array();
154 $comments_picture_data = array();
155 foreach (array('data', 'image_mime', 'image_width', 'image_height', 'imagetype', 'picturetype', 'description', 'datalength') as $picture_key) {
156 if (isset($entry[$picture_key])) {
157 $comments_picture_data[$picture_key] = $entry[$picture_key];
160 $info['flac']['comments']['picture'][] = $comments_picture_data;
161 unset($comments_picture_data);
166 if (isset($info['flac']['STREAMINFO'])) {
167 if (!$this->isDependencyFor('matroska')) {
168 $info['flac']['compressed_audio_bytes'] = $info['avdataend'] - $info['avdataoffset'];
170 $info['flac']['uncompressed_audio_bytes'] = $info['flac']['STREAMINFO']['samples_stream'] * $info['flac']['STREAMINFO']['channels'] * ($info['flac']['STREAMINFO']['bits_per_sample'] / 8);
171 if ($info['flac']['uncompressed_audio_bytes'] == 0) {
172 return $this->error('Corrupt FLAC file: uncompressed_audio_bytes == zero');
174 if (!empty($info['flac']['compressed_audio_bytes'])) {
175 $info['flac']['compression_ratio'] = $info['flac']['compressed_audio_bytes'] / $info['flac']['uncompressed_audio_bytes'];
179 // set md5_data_source - built into flac 0.5+
180 if (isset($info['flac']['STREAMINFO']['audio_signature'])) {
182 if ($info['flac']['STREAMINFO']['audio_signature'] === str_repeat("\x00", 16)) {
183 $this->warning('FLAC STREAMINFO.audio_signature is null (known issue with libOggFLAC)');
186 $info['md5_data_source'] = '';
187 $md5 = $info['flac']['STREAMINFO']['audio_signature'];
188 for ($i = 0; $i < strlen($md5); $i++
) {
189 $info['md5_data_source'] .= str_pad(dechex(ord($md5[$i])), 2, '00', STR_PAD_LEFT
);
191 if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) {
192 unset($info['md5_data_source']);
197 if (isset($info['flac']['STREAMINFO']['bits_per_sample'])) {
198 $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample'];
199 if ($info['audio']['bits_per_sample'] == 8) {
201 // must invert sign bit on all data bytes before MD5'ing to match FLAC's calculated value
202 // MD5sum calculates on unsigned bytes, but FLAC calculated MD5 on 8-bit audio data as signed
203 $this->warning('FLAC calculates MD5 data strangely on 8-bit audio, so the stored md5_data_source value will not match the decoded WAV file');
212 * @param string $BlockData
216 public static function parseSTREAMINFOdata($BlockData) {
217 $streaminfo = array();
218 $streaminfo['min_block_size'] = getid3_lib
::BigEndian2Int(substr($BlockData, 0, 2));
219 $streaminfo['max_block_size'] = getid3_lib
::BigEndian2Int(substr($BlockData, 2, 2));
220 $streaminfo['min_frame_size'] = getid3_lib
::BigEndian2Int(substr($BlockData, 4, 3));
221 $streaminfo['max_frame_size'] = getid3_lib
::BigEndian2Int(substr($BlockData, 7, 3));
223 $SRCSBSS = getid3_lib
::BigEndian2Bin(substr($BlockData, 10, 8));
224 $streaminfo['sample_rate'] = getid3_lib
::Bin2Dec(substr($SRCSBSS, 0, 20));
225 $streaminfo['channels'] = getid3_lib
::Bin2Dec(substr($SRCSBSS, 20, 3)) +
1;
226 $streaminfo['bits_per_sample'] = getid3_lib
::Bin2Dec(substr($SRCSBSS, 23, 5)) +
1;
227 $streaminfo['samples_stream'] = getid3_lib
::Bin2Dec(substr($SRCSBSS, 28, 36));
229 $streaminfo['audio_signature'] = substr($BlockData, 18, 16);
235 * @param string $BlockData
239 private function parseSTREAMINFO($BlockData) {
240 $info = &$this->getid3
->info
;
242 $info['flac']['STREAMINFO'] = self
::parseSTREAMINFOdata($BlockData);
244 if (!empty($info['flac']['STREAMINFO']['sample_rate'])) {
246 $info['audio']['bitrate_mode'] = 'vbr';
247 $info['audio']['sample_rate'] = $info['flac']['STREAMINFO']['sample_rate'];
248 $info['audio']['channels'] = $info['flac']['STREAMINFO']['channels'];
249 $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample'];
250 $info['playtime_seconds'] = $info['flac']['STREAMINFO']['samples_stream'] / $info['flac']['STREAMINFO']['sample_rate'];
251 if ($info['playtime_seconds'] > 0) {
252 if (!$this->isDependencyFor('matroska')) {
253 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds'];
256 $this->warning('Cannot determine audio bitrate because total stream size is unknown');
261 return $this->error('Corrupt METAdata block: STREAMINFO');
268 * @param string $BlockData
272 private function parseAPPLICATION($BlockData) {
273 $info = &$this->getid3
->info
;
275 $ApplicationID = getid3_lib
::BigEndian2Int(substr($BlockData, 0, 4));
276 $info['flac']['APPLICATION'][$ApplicationID]['name'] = self
::applicationIDLookup($ApplicationID);
277 $info['flac']['APPLICATION'][$ApplicationID]['data'] = substr($BlockData, 4);
283 * @param string $BlockData
287 private function parseSEEKTABLE($BlockData) {
288 $info = &$this->getid3
->info
;
291 $BlockLength = strlen($BlockData);
292 $placeholderpattern = str_repeat("\xFF", 8);
293 while ($offset < $BlockLength) {
294 $SampleNumberString = substr($BlockData, $offset, 8);
296 if ($SampleNumberString == $placeholderpattern) {
299 getid3_lib
::safe_inc($info['flac']['SEEKTABLE']['placeholders'], 1);
304 $SampleNumber = getid3_lib
::BigEndian2Int($SampleNumberString);
305 $info['flac']['SEEKTABLE'][$SampleNumber]['offset'] = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 8));
307 $info['flac']['SEEKTABLE'][$SampleNumber]['samples'] = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 2));
317 * @param string $BlockData
321 private function parseVORBIS_COMMENT($BlockData) {
322 $info = &$this->getid3
->info
;
324 $getid3_ogg = new getid3_ogg($this->getid3
);
325 if ($this->isDependencyFor('matroska')) {
326 $getid3_ogg->setStringMode($this->data_string
);
328 $getid3_ogg->ParseVorbisComments();
329 if (isset($info['ogg'])) {
330 unset($info['ogg']['comments_raw']);
331 $info['flac']['VORBIS_COMMENT'] = $info['ogg'];
341 * @param string $BlockData
345 private function parseCUESHEET($BlockData) {
346 $info = &$this->getid3
->info
;
348 $info['flac']['CUESHEET']['media_catalog_number'] = trim(substr($BlockData, $offset, 128), "\0");
350 $info['flac']['CUESHEET']['lead_in_samples'] = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 8));
352 $info['flac']['CUESHEET']['flags']['is_cd'] = (bool) (getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 1)) & 0x80);
355 $offset +
= 258; // reserved
357 $info['flac']['CUESHEET']['number_tracks'] = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 1));
360 for ($track = 0; $track < $info['flac']['CUESHEET']['number_tracks']; $track++
) {
361 $TrackSampleOffset = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 8));
363 $TrackNumber = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 1));
366 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['sample_offset'] = $TrackSampleOffset;
368 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['isrc'] = substr($BlockData, $offset, 12);
371 $TrackFlagsRaw = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 1));
373 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['is_audio'] = (bool) ($TrackFlagsRaw & 0x80);
374 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['pre_emphasis'] = (bool) ($TrackFlagsRaw & 0x40);
376 $offset +
= 13; // reserved
378 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points'] = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 1));
381 for ($index = 0; $index < $info['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points']; $index++
) {
382 $IndexSampleOffset = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 8));
384 $IndexNumber = getid3_lib
::BigEndian2Int(substr($BlockData, $offset, 1));
387 $offset +
= 3; // reserved
389 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['indexes'][$IndexNumber] = $IndexSampleOffset;
397 * Parse METADATA_BLOCK_PICTURE flac structure and extract attachment
398 * External usage: audio.ogg
402 public function parsePICTURE() {
403 $info = &$this->getid3
->info
;
405 $picture['typeid'] = getid3_lib
::BigEndian2Int($this->fread(4));
406 $picture['picturetype'] = self
::pictureTypeLookup($picture['typeid']);
407 $picture['image_mime'] = $this->fread(getid3_lib
::BigEndian2Int($this->fread(4)));
408 $descr_length = getid3_lib
::BigEndian2Int($this->fread(4));
410 $picture['description'] = $this->fread($descr_length);
412 $picture['image_width'] = getid3_lib
::BigEndian2Int($this->fread(4));
413 $picture['image_height'] = getid3_lib
::BigEndian2Int($this->fread(4));
414 $picture['color_depth'] = getid3_lib
::BigEndian2Int($this->fread(4));
415 $picture['colors_indexed'] = getid3_lib
::BigEndian2Int($this->fread(4));
416 $picture['datalength'] = getid3_lib
::BigEndian2Int($this->fread(4));
418 if ($picture['image_mime'] == '-->') {
419 $picture['data'] = $this->fread($picture['datalength']);
421 $picture['data'] = $this->saveAttachment(
422 str_replace('/', '_', $picture['picturetype']).'_'.$this->ftell(),
424 $picture['datalength'],
425 $picture['image_mime']);
428 $info['flac']['PICTURE'][] = $picture;
434 * @param int $blocktype
438 public static function metaBlockTypeLookup($blocktype) {
439 static $lookup = array(
444 4 => 'VORBIS_COMMENT',
448 return (isset($lookup[$blocktype]) ?
$lookup[$blocktype] : 'reserved');
452 * @param int $applicationid
456 public static function applicationIDLookup($applicationid) {
457 // http://flac.sourceforge.net/id.html
458 static $lookup = array(
459 0x41544348 => 'FlacFile', // "ATCH"
460 0x42534F4C => 'beSolo', // "BSOL"
461 0x42554753 => 'Bugs Player', // "BUGS"
462 0x43756573 => 'GoldWave cue points (specification)', // "Cues"
463 0x46696361 => 'CUE Splitter', // "Fica"
464 0x46746F6C => 'flac-tools', // "Ftol"
465 0x4D4F5442 => 'MOTB MetaCzar', // "MOTB"
466 0x4D505345 => 'MP3 Stream Editor', // "MPSE"
467 0x4D754D4C => 'MusicML: Music Metadata Language', // "MuML"
468 0x52494646 => 'Sound Devices RIFF chunk storage', // "RIFF"
469 0x5346464C => 'Sound Font FLAC', // "SFFL"
470 0x534F4E59 => 'Sony Creative Software', // "SONY"
471 0x5351455A => 'flacsqueeze', // "SQEZ"
472 0x54745776 => 'TwistedWave', // "TtWv"
473 0x55495453 => 'UITS Embedding tools', // "UITS"
474 0x61696666 => 'FLAC AIFF chunk storage', // "aiff"
475 0x696D6167 => 'flac-image application for storing arbitrary files in APPLICATION metadata blocks', // "imag"
476 0x7065656D => 'Parseable Embedded Extensible Metadata (specification)', // "peem"
477 0x71667374 => 'QFLAC Studio', // "qfst"
478 0x72696666 => 'FLAC RIFF chunk storage', // "riff"
479 0x74756E65 => 'TagTuner', // "tune"
480 0x78626174 => 'XBAT', // "xbat"
481 0x786D6364 => 'xmcd', // "xmcd"
483 return (isset($lookup[$applicationid]) ?
$lookup[$applicationid] : 'reserved');
487 * @param int $type_id
491 public static function pictureTypeLookup($type_id) {
492 static $lookup = array (
494 1 => '32x32 pixels \'file icon\' (PNG only)',
495 2 => 'Other file icon',
496 3 => 'Cover (front)',
499 6 => 'Media (e.g. label side of CD)',
500 7 => 'Lead artist/lead performer/soloist',
501 8 => 'Artist/performer',
503 10 => 'Band/Orchestra',
505 12 => 'Lyricist/text writer',
506 13 => 'Recording Location',
507 14 => 'During recording',
508 15 => 'During performance',
509 16 => 'Movie/video screen capture',
510 17 => 'A bright coloured fish',
511 18 => 'Illustration',
512 19 => 'Band/artist logotype',
513 20 => 'Publisher/Studio logotype',
515 return (isset($lookup[$type_id]) ?
$lookup[$type_id] : 'reserved');