File indexing completed on 2024-12-22 05:33:12
0001 <?php 0002 ///////////////////////////////////////////////////////////////// 0003 /// getID3() by James Heinrich <info@getid3.org> // 0004 // available at http://getid3.sourceforge.net // 0005 // or http://www.getid3.org // 0006 // also https://github.com/JamesHeinrich/getID3 // 0007 ///////////////////////////////////////////////////////////////// 0008 // See readme.txt for more details // 0009 ///////////////////////////////////////////////////////////////// 0010 // // 0011 // module.audio.flac.php // 0012 // module for analyzing FLAC and OggFLAC audio files // 0013 // dependencies: module.audio.ogg.php // 0014 // /// 0015 ///////////////////////////////////////////////////////////////// 0016 0017 0018 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ogg.php', __FILE__, true); 0019 0020 /** 0021 * @tutorial http://flac.sourceforge.net/format.html 0022 */ 0023 class getid3_flac extends getid3_handler 0024 { 0025 const syncword = 'fLaC'; 0026 0027 public function Analyze() { 0028 $info = &$this->getid3->info; 0029 0030 $this->fseek($info['avdataoffset']); 0031 $StreamMarker = $this->fread(4); 0032 if ($StreamMarker != self::syncword) { 0033 return $this->error('Expecting "'.getid3_lib::PrintHexBytes(self::syncword).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($StreamMarker).'"'); 0034 } 0035 $info['fileformat'] = 'flac'; 0036 $info['audio']['dataformat'] = 'flac'; 0037 $info['audio']['bitrate_mode'] = 'vbr'; 0038 $info['audio']['lossless'] = true; 0039 0040 // parse flac container 0041 return $this->parseMETAdata(); 0042 } 0043 0044 public function parseMETAdata() { 0045 $info = &$this->getid3->info; 0046 do { 0047 $BlockOffset = $this->ftell(); 0048 $BlockHeader = $this->fread(4); 0049 $LBFBT = getid3_lib::BigEndian2Int(substr($BlockHeader, 0, 1)); 0050 $LastBlockFlag = (bool) ($LBFBT & 0x80); 0051 $BlockType = ($LBFBT & 0x7F); 0052 $BlockLength = getid3_lib::BigEndian2Int(substr($BlockHeader, 1, 3)); 0053 $BlockTypeText = self::metaBlockTypeLookup($BlockType); 0054 0055 if (($BlockOffset + 4 + $BlockLength) > $info['avdataend']) { 0056 $this->error('METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$BlockTypeText.') at offset '.$BlockOffset.' extends beyond end of file'); 0057 break; 0058 } 0059 if ($BlockLength < 1) { 0060 $this->error('METADATA_BLOCK_HEADER.BLOCK_LENGTH ('.$BlockLength.') at offset '.$BlockOffset.' is invalid'); 0061 break; 0062 } 0063 0064 $info['flac'][$BlockTypeText]['raw'] = array(); 0065 $BlockTypeText_raw = &$info['flac'][$BlockTypeText]['raw']; 0066 0067 $BlockTypeText_raw['offset'] = $BlockOffset; 0068 $BlockTypeText_raw['last_meta_block'] = $LastBlockFlag; 0069 $BlockTypeText_raw['block_type'] = $BlockType; 0070 $BlockTypeText_raw['block_type_text'] = $BlockTypeText; 0071 $BlockTypeText_raw['block_length'] = $BlockLength; 0072 if ($BlockTypeText_raw['block_type'] != 0x06) { // do not read attachment data automatically 0073 $BlockTypeText_raw['block_data'] = $this->fread($BlockLength); 0074 } 0075 0076 switch ($BlockTypeText) { 0077 case 'STREAMINFO': // 0x00 0078 if (!$this->parseSTREAMINFO($BlockTypeText_raw['block_data'])) { 0079 return false; 0080 } 0081 break; 0082 0083 case 'PADDING': // 0x01 0084 unset($info['flac']['PADDING']); // ignore 0085 break; 0086 0087 case 'APPLICATION': // 0x02 0088 if (!$this->parseAPPLICATION($BlockTypeText_raw['block_data'])) { 0089 return false; 0090 } 0091 break; 0092 0093 case 'SEEKTABLE': // 0x03 0094 if (!$this->parseSEEKTABLE($BlockTypeText_raw['block_data'])) { 0095 return false; 0096 } 0097 break; 0098 0099 case 'VORBIS_COMMENT': // 0x04 0100 if (!$this->parseVORBIS_COMMENT($BlockTypeText_raw['block_data'])) { 0101 return false; 0102 } 0103 break; 0104 0105 case 'CUESHEET': // 0x05 0106 if (!$this->parseCUESHEET($BlockTypeText_raw['block_data'])) { 0107 return false; 0108 } 0109 break; 0110 0111 case 'PICTURE': // 0x06 0112 if (!$this->parsePICTURE()) { 0113 return false; 0114 } 0115 break; 0116 0117 default: 0118 $this->warning('Unhandled METADATA_BLOCK_HEADER.BLOCK_TYPE ('.$BlockType.') at offset '.$BlockOffset); 0119 } 0120 0121 unset($info['flac'][$BlockTypeText]['raw']); 0122 $info['avdataoffset'] = $this->ftell(); 0123 } 0124 while ($LastBlockFlag === false); 0125 0126 // handle tags 0127 if (!empty($info['flac']['VORBIS_COMMENT']['comments'])) { 0128 $info['flac']['comments'] = $info['flac']['VORBIS_COMMENT']['comments']; 0129 } 0130 if (!empty($info['flac']['VORBIS_COMMENT']['vendor'])) { 0131 $info['audio']['encoder'] = str_replace('reference ', '', $info['flac']['VORBIS_COMMENT']['vendor']); 0132 } 0133 0134 // copy attachments to 'comments' array if nesesary 0135 if (isset($info['flac']['PICTURE']) && ($this->getid3->option_save_attachments !== getID3::ATTACHMENTS_NONE)) { 0136 foreach ($info['flac']['PICTURE'] as $entry) { 0137 if (!empty($entry['data'])) { 0138 $info['flac']['comments']['picture'][] = array('image_mime'=>$entry['image_mime'], 'data'=>$entry['data']); 0139 } 0140 } 0141 } 0142 0143 if (isset($info['flac']['STREAMINFO'])) { 0144 if (!$this->isDependencyFor('matroska')) { 0145 $info['flac']['compressed_audio_bytes'] = $info['avdataend'] - $info['avdataoffset']; 0146 } 0147 $info['flac']['uncompressed_audio_bytes'] = $info['flac']['STREAMINFO']['samples_stream'] * $info['flac']['STREAMINFO']['channels'] * ($info['flac']['STREAMINFO']['bits_per_sample'] / 8); 0148 if ($info['flac']['uncompressed_audio_bytes'] == 0) { 0149 return $this->error('Corrupt FLAC file: uncompressed_audio_bytes == zero'); 0150 } 0151 if (!empty($info['flac']['compressed_audio_bytes'])) { 0152 $info['flac']['compression_ratio'] = $info['flac']['compressed_audio_bytes'] / $info['flac']['uncompressed_audio_bytes']; 0153 } 0154 } 0155 0156 // set md5_data_source - built into flac 0.5+ 0157 if (isset($info['flac']['STREAMINFO']['audio_signature'])) { 0158 0159 if ($info['flac']['STREAMINFO']['audio_signature'] === str_repeat("\x00", 16)) { 0160 $this->warning('FLAC STREAMINFO.audio_signature is null (known issue with libOggFLAC)'); 0161 } 0162 else { 0163 $info['md5_data_source'] = ''; 0164 $md5 = $info['flac']['STREAMINFO']['audio_signature']; 0165 for ($i = 0; $i < strlen($md5); $i++) { 0166 $info['md5_data_source'] .= str_pad(dechex(ord($md5[$i])), 2, '00', STR_PAD_LEFT); 0167 } 0168 if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) { 0169 unset($info['md5_data_source']); 0170 } 0171 } 0172 } 0173 0174 if (isset($info['flac']['STREAMINFO']['bits_per_sample'])) { 0175 $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample']; 0176 if ($info['audio']['bits_per_sample'] == 8) { 0177 // special case 0178 // must invert sign bit on all data bytes before MD5'ing to match FLAC's calculated value 0179 // MD5sum calculates on unsigned bytes, but FLAC calculated MD5 on 8-bit audio data as signed 0180 $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'); 0181 } 0182 } 0183 0184 return true; 0185 } 0186 0187 private function parseSTREAMINFO($BlockData) { 0188 $info = &$this->getid3->info; 0189 0190 $info['flac']['STREAMINFO'] = array(); 0191 $streaminfo = &$info['flac']['STREAMINFO']; 0192 0193 $streaminfo['min_block_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 0, 2)); 0194 $streaminfo['max_block_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 2, 2)); 0195 $streaminfo['min_frame_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 4, 3)); 0196 $streaminfo['max_frame_size'] = getid3_lib::BigEndian2Int(substr($BlockData, 7, 3)); 0197 0198 $SRCSBSS = getid3_lib::BigEndian2Bin(substr($BlockData, 10, 8)); 0199 $streaminfo['sample_rate'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 0, 20)); 0200 $streaminfo['channels'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 20, 3)) + 1; 0201 $streaminfo['bits_per_sample'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 23, 5)) + 1; 0202 $streaminfo['samples_stream'] = getid3_lib::Bin2Dec(substr($SRCSBSS, 28, 36)); 0203 0204 $streaminfo['audio_signature'] = substr($BlockData, 18, 16); 0205 0206 if (!empty($streaminfo['sample_rate'])) { 0207 0208 $info['audio']['bitrate_mode'] = 'vbr'; 0209 $info['audio']['sample_rate'] = $streaminfo['sample_rate']; 0210 $info['audio']['channels'] = $streaminfo['channels']; 0211 $info['audio']['bits_per_sample'] = $streaminfo['bits_per_sample']; 0212 $info['playtime_seconds'] = $streaminfo['samples_stream'] / $streaminfo['sample_rate']; 0213 if ($info['playtime_seconds'] > 0) { 0214 if (!$this->isDependencyFor('matroska')) { 0215 $info['audio']['bitrate'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['playtime_seconds']; 0216 } 0217 else { 0218 $this->warning('Cannot determine audio bitrate because total stream size is unknown'); 0219 } 0220 } 0221 0222 } else { 0223 return $this->error('Corrupt METAdata block: STREAMINFO'); 0224 } 0225 0226 return true; 0227 } 0228 0229 private function parseAPPLICATION($BlockData) { 0230 $info = &$this->getid3->info; 0231 0232 $ApplicationID = getid3_lib::BigEndian2Int(substr($BlockData, 0, 4)); 0233 $info['flac']['APPLICATION'][$ApplicationID]['name'] = self::applicationIDLookup($ApplicationID); 0234 $info['flac']['APPLICATION'][$ApplicationID]['data'] = substr($BlockData, 4); 0235 0236 return true; 0237 } 0238 0239 private function parseSEEKTABLE($BlockData) { 0240 $info = &$this->getid3->info; 0241 0242 $offset = 0; 0243 $BlockLength = strlen($BlockData); 0244 $placeholderpattern = str_repeat("\xFF", 8); 0245 while ($offset < $BlockLength) { 0246 $SampleNumberString = substr($BlockData, $offset, 8); 0247 $offset += 8; 0248 if ($SampleNumberString == $placeholderpattern) { 0249 0250 // placeholder point 0251 getid3_lib::safe_inc($info['flac']['SEEKTABLE']['placeholders'], 1); 0252 $offset += 10; 0253 0254 } else { 0255 0256 $SampleNumber = getid3_lib::BigEndian2Int($SampleNumberString); 0257 $info['flac']['SEEKTABLE'][$SampleNumber]['offset'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8)); 0258 $offset += 8; 0259 $info['flac']['SEEKTABLE'][$SampleNumber]['samples'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 2)); 0260 $offset += 2; 0261 0262 } 0263 } 0264 0265 return true; 0266 } 0267 0268 private function parseVORBIS_COMMENT($BlockData) { 0269 $info = &$this->getid3->info; 0270 0271 $getid3_ogg = new getid3_ogg($this->getid3); 0272 if ($this->isDependencyFor('matroska')) { 0273 $getid3_ogg->setStringMode($this->data_string); 0274 } 0275 $getid3_ogg->ParseVorbisComments(); 0276 if (isset($info['ogg'])) { 0277 unset($info['ogg']['comments_raw']); 0278 $info['flac']['VORBIS_COMMENT'] = $info['ogg']; 0279 unset($info['ogg']); 0280 } 0281 0282 unset($getid3_ogg); 0283 0284 return true; 0285 } 0286 0287 private function parseCUESHEET($BlockData) { 0288 $info = &$this->getid3->info; 0289 $offset = 0; 0290 $info['flac']['CUESHEET']['media_catalog_number'] = trim(substr($BlockData, $offset, 128), "\0"); 0291 $offset += 128; 0292 $info['flac']['CUESHEET']['lead_in_samples'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8)); 0293 $offset += 8; 0294 $info['flac']['CUESHEET']['flags']['is_cd'] = (bool) (getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)) & 0x80); 0295 $offset += 1; 0296 0297 $offset += 258; // reserved 0298 0299 $info['flac']['CUESHEET']['number_tracks'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); 0300 $offset += 1; 0301 0302 for ($track = 0; $track < $info['flac']['CUESHEET']['number_tracks']; $track++) { 0303 $TrackSampleOffset = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8)); 0304 $offset += 8; 0305 $TrackNumber = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); 0306 $offset += 1; 0307 0308 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['sample_offset'] = $TrackSampleOffset; 0309 0310 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['isrc'] = substr($BlockData, $offset, 12); 0311 $offset += 12; 0312 0313 $TrackFlagsRaw = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); 0314 $offset += 1; 0315 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['is_audio'] = (bool) ($TrackFlagsRaw & 0x80); 0316 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['flags']['pre_emphasis'] = (bool) ($TrackFlagsRaw & 0x40); 0317 0318 $offset += 13; // reserved 0319 0320 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points'] = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); 0321 $offset += 1; 0322 0323 for ($index = 0; $index < $info['flac']['CUESHEET']['tracks'][$TrackNumber]['index_points']; $index++) { 0324 $IndexSampleOffset = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 8)); 0325 $offset += 8; 0326 $IndexNumber = getid3_lib::BigEndian2Int(substr($BlockData, $offset, 1)); 0327 $offset += 1; 0328 0329 $offset += 3; // reserved 0330 0331 $info['flac']['CUESHEET']['tracks'][$TrackNumber]['indexes'][$IndexNumber] = $IndexSampleOffset; 0332 } 0333 } 0334 0335 return true; 0336 } 0337 0338 /** 0339 * Parse METADATA_BLOCK_PICTURE flac structure and extract attachment 0340 * External usage: audio.ogg 0341 */ 0342 public function parsePICTURE() { 0343 $info = &$this->getid3->info; 0344 0345 $picture['typeid'] = getid3_lib::BigEndian2Int($this->fread(4)); 0346 $picture['type'] = self::pictureTypeLookup($picture['typeid']); 0347 $picture['image_mime'] = $this->fread(getid3_lib::BigEndian2Int($this->fread(4))); 0348 $descr_length = getid3_lib::BigEndian2Int($this->fread(4)); 0349 if ($descr_length) { 0350 $picture['description'] = $this->fread($descr_length); 0351 } 0352 $picture['width'] = getid3_lib::BigEndian2Int($this->fread(4)); 0353 $picture['height'] = getid3_lib::BigEndian2Int($this->fread(4)); 0354 $picture['color_depth'] = getid3_lib::BigEndian2Int($this->fread(4)); 0355 $picture['colors_indexed'] = getid3_lib::BigEndian2Int($this->fread(4)); 0356 $data_length = getid3_lib::BigEndian2Int($this->fread(4)); 0357 0358 if ($picture['image_mime'] == '-->') { 0359 $picture['data'] = $this->fread($data_length); 0360 } else { 0361 $picture['data'] = $this->saveAttachment( 0362 str_replace('/', '_', $picture['type']).'_'.$this->ftell(), 0363 $this->ftell(), 0364 $data_length, 0365 $picture['image_mime']); 0366 } 0367 0368 $info['flac']['PICTURE'][] = $picture; 0369 0370 return true; 0371 } 0372 0373 public static function metaBlockTypeLookup($blocktype) { 0374 static $lookup = array( 0375 0 => 'STREAMINFO', 0376 1 => 'PADDING', 0377 2 => 'APPLICATION', 0378 3 => 'SEEKTABLE', 0379 4 => 'VORBIS_COMMENT', 0380 5 => 'CUESHEET', 0381 6 => 'PICTURE', 0382 ); 0383 return (isset($lookup[$blocktype]) ? $lookup[$blocktype] : 'reserved'); 0384 } 0385 0386 public static function applicationIDLookup($applicationid) { 0387 // http://flac.sourceforge.net/id.html 0388 static $lookup = array( 0389 0x41544348 => 'FlacFile', // "ATCH" 0390 0x42534F4C => 'beSolo', // "BSOL" 0391 0x42554753 => 'Bugs Player', // "BUGS" 0392 0x43756573 => 'GoldWave cue points (specification)', // "Cues" 0393 0x46696361 => 'CUE Splitter', // "Fica" 0394 0x46746F6C => 'flac-tools', // "Ftol" 0395 0x4D4F5442 => 'MOTB MetaCzar', // "MOTB" 0396 0x4D505345 => 'MP3 Stream Editor', // "MPSE" 0397 0x4D754D4C => 'MusicML: Music Metadata Language', // "MuML" 0398 0x52494646 => 'Sound Devices RIFF chunk storage', // "RIFF" 0399 0x5346464C => 'Sound Font FLAC', // "SFFL" 0400 0x534F4E59 => 'Sony Creative Software', // "SONY" 0401 0x5351455A => 'flacsqueeze', // "SQEZ" 0402 0x54745776 => 'TwistedWave', // "TtWv" 0403 0x55495453 => 'UITS Embedding tools', // "UITS" 0404 0x61696666 => 'FLAC AIFF chunk storage', // "aiff" 0405 0x696D6167 => 'flac-image application for storing arbitrary files in APPLICATION metadata blocks', // "imag" 0406 0x7065656D => 'Parseable Embedded Extensible Metadata (specification)', // "peem" 0407 0x71667374 => 'QFLAC Studio', // "qfst" 0408 0x72696666 => 'FLAC RIFF chunk storage', // "riff" 0409 0x74756E65 => 'TagTuner', // "tune" 0410 0x78626174 => 'XBAT', // "xbat" 0411 0x786D6364 => 'xmcd', // "xmcd" 0412 ); 0413 return (isset($lookup[$applicationid]) ? $lookup[$applicationid] : 'reserved'); 0414 } 0415 0416 public static function pictureTypeLookup($type_id) { 0417 static $lookup = array ( 0418 0 => 'Other', 0419 1 => '32x32 pixels \'file icon\' (PNG only)', 0420 2 => 'Other file icon', 0421 3 => 'Cover (front)', 0422 4 => 'Cover (back)', 0423 5 => 'Leaflet page', 0424 6 => 'Media (e.g. label side of CD)', 0425 7 => 'Lead artist/lead performer/soloist', 0426 8 => 'Artist/performer', 0427 9 => 'Conductor', 0428 10 => 'Band/Orchestra', 0429 11 => 'Composer', 0430 12 => 'Lyricist/text writer', 0431 13 => 'Recording Location', 0432 14 => 'During recording', 0433 15 => 'During performance', 0434 16 => 'Movie/video screen capture', 0435 17 => 'A bright coloured fish', 0436 18 => 'Illustration', 0437 19 => 'Band/artist logotype', 0438 20 => 'Publisher/Studio logotype', 0439 ); 0440 return (isset($lookup[$type_id]) ? $lookup[$type_id] : 'reserved'); 0441 } 0442 0443 }