function ParseVorbisCommentsFilepointer(&$fd, &$ThisFileInfo) { $OriginalOffset = ftell($fd); $CommentStartOffset = $OriginalOffset; $commentdataoffset = 0; $VorbisCommentPage = 1; switch ($ThisFileInfo['audio']['dataformat']) { case 'vorbis': $CommentStartOffset = $ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage]['page_start_offset']; // Second Ogg page, after header block fseek($fd, $CommentStartOffset, SEEK_SET); $commentdataoffset = 27 + $ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage]['page_segments']; $commentdata = fread($fd, OggPageSegmentLength($ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage], 1) + $commentdataoffset); $commentdataoffset += strlen('vorbis') + 1; break; case 'flac': fseek($fd, $ThisFileInfo['flac']['VORBIS_COMMENT']['raw']['offset'] + 4, SEEK_SET); $commentdata = fread($fd, $ThisFileInfo['flac']['VORBIS_COMMENT']['raw']['block_length']); break; case 'speex': $CommentStartOffset = $ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage]['page_start_offset']; // Second Ogg page, after header block fseek($fd, $CommentStartOffset, SEEK_SET); $commentdataoffset = 27 + $ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage]['page_segments']; $commentdata = fread($fd, OggPageSegmentLength($ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage], 1) + $commentdataoffset); break; default: return false; break; } $VendorSize = LittleEndian2Int(substr($commentdata, $commentdataoffset, 4)); $commentdataoffset += 4; $ThisFileInfo['ogg']['vendor'] = substr($commentdata, $commentdataoffset, $VendorSize); $commentdataoffset += $VendorSize; $CommentsCount = LittleEndian2Int(substr($commentdata, $commentdataoffset, 4)); $commentdataoffset += 4; $ThisFileInfo['avdataoffset'] = $CommentStartOffset + $commentdataoffset; $basicfields = array('TITLE', 'ARTIST', 'ALBUM', 'TRACKNUMBER', 'GENRE', 'DATE', 'DESCRIPTION', 'COMMENT'); for ($i = 0; $i < $CommentsCount; $i++) { $ThisFileInfo['ogg']['comments_raw'][$i]['dataoffset'] = $CommentStartOffset + $commentdataoffset; if (ftell($fd) < $ThisFileInfo['ogg']['comments_raw'][$i]['dataoffset'] + 4) { $VorbisCommentPage++; $oggpageinfo = ParseOggPageHeader($fd); $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo; // First, save what we haven't read yet $AsYetUnusedData = substr($commentdata, $commentdataoffset); // Then take that data off the end $commentdata = substr($commentdata, 0, $commentdataoffset); // Add [headerlength] bytes of dummy data for the Ogg Page Header, just to keep absolute offsets correct $commentdata .= str_repeat(chr(0), 27 + $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']); $commentdataoffset += 27 + $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']; // Finally, stick the unused data back on the end $commentdata .= $AsYetUnusedData; //$commentdata .= fread($fd, $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']); $commentdata .= fread($fd, OggPageSegmentLength($ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage], 1)); } $ThisFileInfo['ogg']['comments_raw'][$i]['size'] = LittleEndian2Int(substr($commentdata, $commentdataoffset, 4)); // replace avdataoffset with position just after the last vorbiscomment $ThisFileInfo['avdataoffset'] = $ThisFileInfo['ogg']['comments_raw'][$i]['dataoffset'] + $ThisFileInfo['ogg']['comments_raw'][$i]['size'] + 4; $commentdataoffset += 4; while (strlen($commentdata) - $commentdataoffset < $ThisFileInfo['ogg']['comments_raw'][$i]['size']) { if ($ThisFileInfo['ogg']['comments_raw'][$i]['size'] > $ThisFileInfo['avdataend'] || $ThisFileInfo['ogg']['comments_raw'][$i]['size'] < 0) { $ThisFileInfo['error'] .= "\n" . 'Invalid Ogg comment size (comment #' . $i . ', claims to be ' . number_format($ThisFileInfo['ogg']['comments_raw'][$i]['size']) . ' bytes) - aborting reading comments'; break 2; } $VorbisCommentPage++; $oggpageinfo = ParseOggPageHeader($fd); $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo; // First, save what we haven't read yet $AsYetUnusedData = substr($commentdata, $commentdataoffset); // Then take that data off the end $commentdata = substr($commentdata, 0, $commentdataoffset); // Add [headerlength] bytes of dummy data for the Ogg Page Header, just to keep absolute offsets correct $commentdata .= str_repeat(chr(0), 27 + $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']); $commentdataoffset += 27 + $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']; // Finally, stick the unused data back on the end $commentdata .= $AsYetUnusedData; //$commentdata .= fread($fd, $ThisFileInfo['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']); $commentdata .= fread($fd, OggPageSegmentLength($ThisFileInfo['ogg']['pageheader'][$VorbisCommentPage], 1)); //$filebaseoffset += $oggpageinfo['header_end_offset'] - $oggpageinfo['page_start_offset']; } $commentstring = substr($commentdata, $commentdataoffset, $ThisFileInfo['ogg']['comments_raw'][$i]['size']); $commentdataoffset += $ThisFileInfo['ogg']['comments_raw'][$i]['size']; if (!$commentstring) { // no comment? $ThisFileInfo['warning'] .= "\n" . 'Blank Ogg comment [' . $i . ']'; } elseif (strstr($commentstring, '=')) { $commentexploded = explode('=', $commentstring, 2); $ThisFileInfo['ogg']['comments_raw'][$i]['key'] = strtoupper($commentexploded[0]); $ThisFileInfo['ogg']['comments_raw'][$i]['value'] = $commentexploded[1] ? utf8_decode($commentexploded[1]) : ''; $ThisFileInfo['ogg']['comments_raw'][$i]['data'] = base64_decode($ThisFileInfo['ogg']['comments_raw'][$i]['value']); $ThisFileInfo['ogg']['comments'][strtolower($ThisFileInfo['ogg']['comments_raw'][$i]['key'])][] = $ThisFileInfo['ogg']['comments_raw'][$i]['value']; require_once GETID3_INCLUDEPATH . 'getid3.getimagesize.php'; $imagechunkcheck = GetDataImageSize($ThisFileInfo['ogg']['comments_raw'][$i]['data']); $ThisFileInfo['ogg']['comments_raw'][$i]['image_mime'] = image_type_to_mime_type($imagechunkcheck[2]); if (!$ThisFileInfo['ogg']['comments_raw'][$i]['image_mime'] || $ThisFileInfo['ogg']['comments_raw'][$i]['image_mime'] == 'application/octet-stream') { unset($ThisFileInfo['ogg']['comments_raw'][$i]['image_mime']); unset($ThisFileInfo['ogg']['comments_raw'][$i]['data']); } } else { $ThisFileInfo['warning'] .= "\n" . '[known problem with CDex >= v1.40, < v1.50b7] Invalid Ogg comment name/value pair [' . $i . ']: ' . $commentstring; } } // Check for presence of vorbiscomments if (isset($ThisFileInfo['ogg']['comments'])) { $ThisFileInfo['tags'][] = 'vorbiscomment'; // Yank other comments - vorbiscomments has highest preference if (isset($ThisFileInfo['ogg']['comments'])) { CopyFormatCommentsToRootComments($ThisFileInfo['ogg']['comments'], $ThisFileInfo, true, true, true); } } // Replay Gain Adjustment // http://privatewww.essex.ac.uk/~djmrob/replaygain/ if (isset($ThisFileInfo['ogg']['comments']) && is_array($ThisFileInfo['ogg']['comments'])) { foreach ($ThisFileInfo['ogg']['comments'] as $index => $keyvaluepair) { if (isset($keyvaluepair['key'])) { switch ($keyvaluepair['key']) { case 'RG_AUDIOPHILE': case 'REPLAYGAIN_ALBUM_GAIN': $ThisFileInfo['replay_gain']['audiophile']['adjustment'] = (double) $keyvaluepair['value']; break; case 'RG_RADIO': case 'REPLAYGAIN_TRACK_GAIN': $ThisFileInfo['replay_gain']['radio']['adjustment'] = (double) $keyvaluepair['value']; break; case 'REPLAYGAIN_ALBUM_PEAK': $ThisFileInfo['replay_gain']['audiophile']['peak'] = (double) $keyvaluepair['value']; break; case 'RG_PEAK': case 'REPLAYGAIN_TRACK_PEAK': $ThisFileInfo['replay_gain']['radio']['peak'] = (double) $keyvaluepair['value']; break; default: // do nothing break; } } } } fseek($fd, $OriginalOffset, SEEK_SET); return true; }
function getOggHeaderFilepointer(&$fd, &$MP3fileInfo) { if (!$fd) { $MP3fileInfo['error'] = "\n" . 'Could not open file'; return FALSE; } else { // Page 1 - Stream Header rewind($fd); $MP3fileInfo['ogg']['pageheader'][0] = ParseOggPageHeader($fd); if (ftell($fd) >= 10000) { $MP3fileInfo['error'] = "\n" . 'Could not find start of Ogg page in the first 10,000 bytes (this might not be an Ogg-Vorbis file?)'; unset($MP3fileInfo['fileformat']); unset($MP3fileInfo['ogg']); return FALSE; } $filedata = fread($fd, 23); $filedataoffset = 0; $MP3fileInfo['ogg']['bitstreamversion'] = LittleEndian2Int(substr($filedata, 0, 4)); $MP3fileInfo['ogg']['numberofchannels'] = LittleEndian2Int(substr($filedata, 4, 1)); $MP3fileInfo['ogg']['samplerate'] = LittleEndian2Int(substr($filedata, 5, 4)); $MP3fileInfo['ogg']['samples'] = 0; // filled in later $MP3fileInfo['ogg']['bitrate_average'] = 0; // filled in later if (substr($filedata, 9, 4) !== chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff)) { $MP3fileInfo['ogg']['bitrate_max'] = LittleEndian2Int(substr($filedata, 9, 4)); } if (substr($filedata, 13, 4) !== chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff)) { $MP3fileInfo['ogg']['bitrate_nominal'] = LittleEndian2Int(substr($filedata, 13, 4)); } if (substr($filedata, 17, 4) !== chr(0xff) . chr(0xff) . chr(0xff) . chr(0xff)) { $MP3fileInfo['ogg']['bitrate_min'] = LittleEndian2Int(substr($filedata, 17, 4)); } $MP3fileInfo['ogg']['blocksize_small'] = pow(2, LittleEndian2Int(substr($filedata, 21, 1)) & 0xf); $MP3fileInfo['ogg']['blocksize_large'] = pow(2, (LittleEndian2Int(substr($filedata, 21, 1)) & 0xf0) >> 4); $MP3fileInfo['ogg']['stop_bit'] = ord(substr($filedata, 22, 1)); // must be 1, marks end of packet // Page 2 - Comment Header $MP3fileInfo['ogg']['pageheader'][1] = ParseOggPageHeader($fd); $filedata = fread($fd, FREAD_BUFFER_SIZE); $filedataoffset = 0; $vendorsize = LittleEndian2Int(substr($filedata, $filedataoffset, 4)); $filedataoffset += 4; $MP3fileInfo['ogg']['vendor'] = substr($filedata, $filedataoffset, $vendorsize); $filedataoffset += $vendorsize; $basicfields = array('TITLE', 'ARTIST', 'ALBUM', 'TRACKNUMBER', 'GENRE', 'DATE', 'DESCRIPTION', 'COMMENT'); $totalcomments = LittleEndian2Int(substr($filedata, $filedataoffset, 4)); $filedataoffset += 4; for ($i = 0; $i < $totalcomments; $i++) { $commentsize = LittleEndian2Int(substr($filedata, $filedataoffset, 4)); $filedataoffset += 4; $commentstring = substr($filedata, $filedataoffset, $commentsize); $filedataoffset += $commentsize; $commentexploded = explode('=', $commentstring, 2); $MP3fileInfo['ogg']['comments']["{$i}"]['key'] = strtoupper($commentexploded[0]); $MP3fileInfo['ogg']['comments']["{$i}"]['value'] = $commentexploded[1] ? $commentexploded[1] : ''; if (in_array($MP3fileInfo['ogg']['comments']["{$i}"]['key'], $basicfields)) { $MP3fileInfo['ogg'][strtolower($MP3fileInfo['ogg']['comments']["{$i}"]['key'])] = $MP3fileInfo['ogg']['comments']["{$i}"]['value']; } } $MP3fileInfo['ogg']['comments_offset_end'] = $filedataoffset; // Last Page - Number of Samples fseek($fd, max($MP3fileInfo['filesize'] - FREAD_BUFFER_SIZE, 0), SEEK_SET); $LastChunkOfOgg = strrev(fread($fd, FREAD_BUFFER_SIZE)); if ($LastOggSpostion = strpos($LastChunkOfOgg, 'SggO')) { fseek($fd, 0 - ($LastOggSpostion + strlen('SggO')), SEEK_END); $MP3fileInfo['ogg']['pageheader']['eos'] = ParseOggPageHeader($fd); $MP3fileInfo['ogg']['samples'] = $MP3fileInfo['ogg']['pageheader']['eos']['pcm_abs_position']; $MP3fileInfo['ogg']['bitrate_average'] = $MP3fileInfo['filesize'] * 8 / ($MP3fileInfo['ogg']['samples'] / $MP3fileInfo['ogg']['samplerate']); } if (isset($MP3fileInfo['ogg']['bitrate_average']) && $MP3fileInfo['ogg']['bitrate_average'] > 0) { $MP3fileInfo['bitrate'] = $MP3fileInfo['ogg']['bitrate_average']; } else { if (isset($MP3fileInfo['ogg']['bitrate_nominal']) && $MP3fileInfo['ogg']['bitrate_nominal'] > 0) { $MP3fileInfo['bitrate'] = $MP3fileInfo['ogg']['bitrate_nominal']; } else { if (isset($MP3fileInfo['ogg']['bitrate_min']) && isset($MP3fileInfo['ogg']['bitrate_max'])) { $MP3fileInfo['bitrate'] = ($MP3fileInfo['ogg']['bitrate_min'] + $MP3fileInfo['ogg']['bitrate_max']) / 2; } } } if (isset($MP3fileInfo['bitrate']) && !isset($MP3fileInfo['playtime_seconds'])) { $MP3fileInfo['playtime_seconds'] = (double) ($MP3fileInfo['filesize'] * 8 / $MP3fileInfo['bitrate']); } } return TRUE; }