function FLACparseMETAdata(&$fd, &$ThisFileInfo) { do { $METAdataBlockOffset = ftell($fd); $METAdataBlockHeader = fread($fd, 4); $METAdataLastBlockFlag = (bool) (getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 0, 1)) & 0x80); $METAdataBlockType = getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 0, 1)) & 0x7f; $METAdataBlockLength = getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 1, 3)); $METAdataBlockTypeText = getid3_flac::FLACmetaBlockTypeLookup($METAdataBlockType); $ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['offset'] = $METAdataBlockOffset; $ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['last_meta_block'] = $METAdataLastBlockFlag; $ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['block_type'] = $METAdataBlockType; $ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['block_type_text'] = $METAdataBlockTypeText; $ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['block_length'] = $METAdataBlockLength; $ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['block_data'] = fread($fd, $METAdataBlockLength); $ThisFileInfo['avdataoffset'] = ftell($fd); switch ($METAdataBlockTypeText) { case 'STREAMINFO': if (!getid3_flac::FLACparseSTREAMINFO($ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['block_data'], $ThisFileInfo)) { return false; } break; case 'PADDING': // ignore break; case 'APPLICATION': if (!getid3_flac::FLACparseAPPLICATION($ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['block_data'], $ThisFileInfo)) { return false; } break; case 'SEEKTABLE': if (!getid3_flac::FLACparseSEEKTABLE($ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['block_data'], $ThisFileInfo)) { return false; } break; case 'VORBIS_COMMENT': $OldOffset = ftell($fd); fseek($fd, 0 - $METAdataBlockLength, SEEK_CUR); getid3_ogg::ParseVorbisCommentsFilepointer($fd, $ThisFileInfo); fseek($fd, $OldOffset, SEEK_SET); break; case 'CUESHEET': if (!getid3_flac::FLACparseCUESHEET($ThisFileInfo['flac']["{$METAdataBlockTypeText}"]['raw']['block_data'], $ThisFileInfo)) { return false; } break; default: $ThisFileInfo['warning'][] = 'Unhandled METADATA_BLOCK_HEADER.BLOCK_TYPE (' . $METAdataBlockType . ') at offset ' . $METAdataBlockOffset; break; } } while ($METAdataLastBlockFlag === false); if (isset($ThisFileInfo['flac']['STREAMINFO'])) { $ThisFileInfo['flac']['compressed_audio_bytes'] = $ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']; $ThisFileInfo['flac']['uncompressed_audio_bytes'] = $ThisFileInfo['flac']['STREAMINFO']['samples_stream'] * $ThisFileInfo['flac']['STREAMINFO']['channels'] * ($ThisFileInfo['flac']['STREAMINFO']['bits_per_sample'] / 8); if ($ThisFileInfo['flac']['uncompressed_audio_bytes'] == 0) { $ThisFileInfo['error'][] = 'Corrupt FLAC file: uncompressed_audio_bytes == zero'; return false; } $ThisFileInfo['flac']['compression_ratio'] = $ThisFileInfo['flac']['compressed_audio_bytes'] / $ThisFileInfo['flac']['uncompressed_audio_bytes']; } // set md5_data_source - built into flac 0.5+ if (isset($ThisFileInfo['flac']['STREAMINFO']['audio_signature'])) { if ($ThisFileInfo['flac']['STREAMINFO']['audio_signature'] === str_repeat("", 16)) { $ThisFileInfo['warning'][] = 'FLAC STREAMINFO.audio_signature is null (known issue with libOggFLAC)'; } else { $ThisFileInfo['md5_data_source'] = ''; $md5 = $ThisFileInfo['flac']['STREAMINFO']['audio_signature']; for ($i = 0; $i < strlen($md5); $i++) { $ThisFileInfo['md5_data_source'] .= str_pad(dechex(ord($md5[$i])), 2, '00', STR_PAD_LEFT); } if (!preg_match('/^[0-9a-f]{32}$/', $ThisFileInfo['md5_data_source'])) { unset($ThisFileInfo['md5_data_source']); } } } $ThisFileInfo['audio']['bits_per_sample'] = $ThisFileInfo['flac']['STREAMINFO']['bits_per_sample']; if ($ThisFileInfo['audio']['bits_per_sample'] == 8) { // special case // must invert sign bit on all data bytes before MD5'ing to match FLAC's calculated value // MD5sum calculates on unsigned bytes, but FLAC calculated MD5 on 8-bit audio data as signed $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'; } if (!empty($ThisFileInfo['ogg']['vendor'])) { $ThisFileInfo['audio']['encoder'] = $ThisFileInfo['ogg']['vendor']; } return true; }
function FLACparseMETAdata() { $info =& $this->getid3->info; do { $METAdataBlockOffset = ftell($this->getid3->fp); $METAdataBlockHeader = fread($this->getid3->fp, 4); $METAdataLastBlockFlag = (bool) (getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 0, 1)) & 0x80); $METAdataBlockType = getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 0, 1)) & 0x7f; $METAdataBlockLength = getid3_lib::BigEndian2Int(substr($METAdataBlockHeader, 1, 3)); $METAdataBlockTypeText = getid3_flac::FLACmetaBlockTypeLookup($METAdataBlockType); if ($METAdataBlockLength < 0) { $info['error'][] = 'corrupt or invalid METADATA_BLOCK_HEADER.BLOCK_TYPE (' . $METAdataBlockType . ') at offset ' . $METAdataBlockOffset; break; } $info['flac'][$METAdataBlockTypeText]['raw'] = array(); $ThisFileInfo_flac_METAdataBlockTypeText_raw =& $info['flac'][$METAdataBlockTypeText]['raw']; $ThisFileInfo_flac_METAdataBlockTypeText_raw['offset'] = $METAdataBlockOffset; $ThisFileInfo_flac_METAdataBlockTypeText_raw['last_meta_block'] = $METAdataLastBlockFlag; $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_type'] = $METAdataBlockType; $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_type_text'] = $METAdataBlockTypeText; $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_length'] = $METAdataBlockLength; if ($METAdataBlockOffset + 4 + $METAdataBlockLength > $info['filesize']) { $info['error'][] = 'METADATA_BLOCK_HEADER.BLOCK_TYPE (' . $METAdataBlockType . ') at offset ' . $METAdataBlockOffset . ' extends beyond end of file'; break; } if ($METAdataBlockLength < 1) { $info['error'][] = 'METADATA_BLOCK_HEADER.BLOCK_LENGTH (' . $METAdataBlockLength . ') at offset ' . $METAdataBlockOffset . ' is invalid'; break; } $ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'] = fread($this->getid3->fp, $METAdataBlockLength); $info['avdataoffset'] = ftell($this->getid3->fp); switch ($METAdataBlockTypeText) { case 'STREAMINFO': // 0x00 if (!$this->FLACparseSTREAMINFO($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'])) { return false; } break; case 'PADDING': // 0x01 // ignore break; case 'APPLICATION': // 0x02 if (!$this->FLACparseAPPLICATION($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'])) { return false; } break; case 'SEEKTABLE': // 0x03 if (!$this->FLACparseSEEKTABLE($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'])) { return false; } break; case 'VORBIS_COMMENT': // 0x04 $getid3_temp = new getID3(); $getid3_temp->openfile($this->getid3->filename); $getid3_temp->info['avdataoffset'] = ftell($this->getid3->fp) - $METAdataBlockLength; $getid3_temp->info['audio']['dataformat'] = 'flac'; $getid3_temp->info['flac'] = $info['flac']; $getid3_ogg = new getid3_ogg($getid3_temp); $getid3_ogg->ParseVorbisCommentsFilepointer(); $maybe_copy_keys = array('vendor', 'comments_raw', 'comments', 'replay_gain'); foreach ($maybe_copy_keys as $maybe_copy_key) { if (!empty($getid3_temp->info['ogg'][$maybe_copy_key])) { $info['ogg'][$maybe_copy_key] = $getid3_temp->info['ogg'][$maybe_copy_key]; } } if (!empty($getid3_temp->info['replay_gain'])) { $info['replay_gain'] = $getid3_temp->info['replay_gain']; } unset($getid3_temp, $getid3_ogg); break; case 'CUESHEET': // 0x05 if (!getid3_flac::FLACparseCUESHEET($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'])) { return false; } break; case 'PICTURE': // 0x06 if (!getid3_flac::FLACparsePICTURE($ThisFileInfo_flac_METAdataBlockTypeText_raw['block_data'])) { return false; } break; default: $info['warning'][] = 'Unhandled METADATA_BLOCK_HEADER.BLOCK_TYPE (' . $METAdataBlockType . ') at offset ' . $METAdataBlockOffset; break; } } while ($METAdataLastBlockFlag === false); if (isset($info['flac']['PICTURE'])) { foreach ($info['flac']['PICTURE'] as $key => $valuearray) { if (!empty($valuearray['image_mime']) && !empty($valuearray['data'])) { $info['ogg']['comments']['picture'][] = array('image_mime' => $valuearray['image_mime'], 'data' => $valuearray['data']); } } } if (isset($info['flac']['STREAMINFO'])) { $info['flac']['compressed_audio_bytes'] = $info['avdataend'] - $info['avdataoffset']; $info['flac']['uncompressed_audio_bytes'] = $info['flac']['STREAMINFO']['samples_stream'] * $info['flac']['STREAMINFO']['channels'] * ($info['flac']['STREAMINFO']['bits_per_sample'] / 8); if ($info['flac']['uncompressed_audio_bytes'] == 0) { $info['error'][] = 'Corrupt FLAC file: uncompressed_audio_bytes == zero'; return false; } $info['flac']['compression_ratio'] = $info['flac']['compressed_audio_bytes'] / $info['flac']['uncompressed_audio_bytes']; } // set md5_data_source - built into flac 0.5+ if (isset($info['flac']['STREAMINFO']['audio_signature'])) { if ($info['flac']['STREAMINFO']['audio_signature'] === str_repeat("", 16)) { $info['warning'][] = 'FLAC STREAMINFO.audio_signature is null (known issue with libOggFLAC)'; } else { $info['md5_data_source'] = ''; $md5 = $info['flac']['STREAMINFO']['audio_signature']; for ($i = 0; $i < strlen($md5); $i++) { $info['md5_data_source'] .= str_pad(dechex(ord($md5[$i])), 2, '00', STR_PAD_LEFT); } if (!preg_match('/^[0-9a-f]{32}$/', $info['md5_data_source'])) { unset($info['md5_data_source']); } } } $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample']; if ($info['audio']['bits_per_sample'] == 8) { // special case // must invert sign bit on all data bytes before MD5'ing to match FLAC's calculated value // MD5sum calculates on unsigned bytes, but FLAC calculated MD5 on 8-bit audio data as signed $info['warning'][] = 'FLAC calculates MD5 data strangely on 8-bit audio, so the stored md5_data_source value will not match the decoded WAV file'; } if (!empty($info['ogg']['vendor'])) { $info['audio']['encoder'] = $info['ogg']['vendor']; } return true; }