summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg')
-rw-r--r--extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Bitstream.php125
-rw-r--r--extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Flac.php133
-rw-r--r--extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Media.php262
-rw-r--r--extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Opus.php126
-rw-r--r--extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Speex.php122
-rw-r--r--extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Theora.php240
-rw-r--r--extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Vorbis.php790
7 files changed, 1798 insertions, 0 deletions
diff --git a/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Bitstream.php b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Bitstream.php
new file mode 100644
index 00000000..1a462232
--- /dev/null
+++ b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Bitstream.php
@@ -0,0 +1,125 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------------+
+// | File_Ogg PEAR Package for Accessing Ogg Bitstreams |
+// | Copyright (c) 2005-2007 |
+// | David Grant <david@grant.org.uk> |
+// | Tim Starling <tstarling@wikimedia.org> |
+// +----------------------------------------------------------------------------+
+// | This library is free software; you can redistribute it and/or |
+// | modify it under the terms of the GNU Lesser General Public |
+// | License as published by the Free Software Foundation; either |
+// | version 2.1 of the License, or (at your option) any later version. |
+// | |
+// | This library is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+// | Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public |
+// | License along with this library; if not, write to the Free Software |
+// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
+// +----------------------------------------------------------------------------+
+
+
+/**
+ * @author David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @category File
+ * @copyright David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
+ * @link http://pear.php.net/package/File_Ogg
+ * @package File_Ogg
+ * @version CVS: $Id: Bitstream.php,v 1.3 2005/11/08 19:36:18 djg Exp $
+ */
+class File_Ogg_Bitstream
+{
+ /**
+ * The serial number of this logical stream.
+ *
+ * @var int
+ * @access private
+ */
+ var $_streamSerial;
+ /**
+ * @access private
+ */
+ var $_streamData;
+ /**
+ * @access private
+ */
+ var $_filePointer;
+ /**
+ * The number of bits used in this stream.
+ *
+ * @var int
+ * @access private
+ */
+ var $_streamSize;
+
+ /**
+ * The last granule position in the stream
+ * @var int
+ * @access private
+ */
+ var $_lastGranulePos;
+
+ /**
+ * Constructor for a generic logical stream.
+ *
+ * @param int $streamSerial Serial number of the logical stream.
+ * @param array $streamData Data for the requested logical stream.
+ * @param string $filePath Location of a file on the filesystem.
+ * @param pointer $filePointer File pointer for the current physical stream.
+ * @access private
+ */
+ function __construct($streamSerial, $streamData, $filePointer)
+ {
+ $this->_streamSerial = $streamSerial;
+ $this->_streamData = $streamData;
+ $this->_filePointer = $filePointer;
+ $this->_firstGranulePos = $streamData['first_granule_pos'];
+ $this->_lastGranulePos = $streamData['last_granule_pos'];
+ $this->_streamSize = $streamData['data_length'];
+ $this->_group = $streamData['pages'][0]['group'];
+ }
+
+ /**
+ * Gives the serial number of this stream.
+ *
+ * The stream serial number is of fairly academic importance, as it makes little
+ * difference to the end user. The serial number is used by the Ogg physical
+ * stream to distinguish between concurrent logical streams.
+ *
+ * @return int
+ * @access public
+ */
+ function getSerial()
+ {
+ return ($this->_streamSerial);
+ }
+
+ /**
+ * Gives the size (in bits) of this stream.
+ *
+ * This function returns the size of the Vorbis stream within the Ogg
+ * physical stream.
+ *
+ * @return int
+ * @access public
+ */
+ function getSize()
+ {
+ return ($this->_streamSize);
+ }
+
+ /**
+ * Get the multiplexed group ID
+ */
+ function getGroup()
+ {
+ return $this->_group;
+ }
+
+}
+
+?>
diff --git a/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Flac.php b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Flac.php
new file mode 100644
index 00000000..6838ba68
--- /dev/null
+++ b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Flac.php
@@ -0,0 +1,133 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------------+
+// | File_Ogg PEAR Package for Accessing Ogg Bitstreams |
+// | Copyright (c) 2005-2007 |
+// | David Grant <david@grant.org.uk> |
+// | Tim Starling <tstarling@wikimedia.org> |
+// +----------------------------------------------------------------------------+
+// | This library is free software; you can redistribute it and/or |
+// | modify it under the terms of the GNU Lesser General Public |
+// | License as published by the Free Software Foundation; either |
+// | version 2.1 of the License, or (at your option) any later version. |
+// | |
+// | This library is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+// | Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public |
+// | License along with this library; if not, write to the Free Software |
+// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
+// +----------------------------------------------------------------------------+
+
+
+/**
+ * @author David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @category File
+ * @copyright David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
+ * @link http://pear.php.net/package/File_Ogg
+ * @link http://flac.sourceforge.net/documentation.html
+ * @package File_Ogg
+ * @version CVS: $Id: Flac.php,v 1.9 2005/11/16 20:43:27 djg Exp $
+ */
+class File_Ogg_Flac extends File_Ogg_Media
+{
+ /**
+ * @access private
+ */
+ function __construct($streamSerial, $streamData, $filePointer)
+ {
+ parent::__construct($streamSerial, $streamData, $filePointer);
+ $this->_decodeHeader();
+ $this->_decodeCommentsHeader();
+ $this->_streamLength = $this->_streamInfo['total_samples']
+ / $this->_streamInfo['sample_rate'];
+ }
+
+ /**
+ * Get a short string describing the type of the stream
+ * @return string
+ */
+ function getType() {
+ return 'FLAC';
+ }
+
+ /**
+ * @access private
+ * @param int $packetType
+ * @param int $pageOffset
+ */
+ function _decodeHeader()
+ {
+ fseek($this->_filePointer, $this->_streamData['pages'][0]['body_offset'], SEEK_SET);
+ // Check if this is the correct header.
+ $packet = unpack("Cdata", fread($this->_filePointer, 1));
+ if ($packet['data'] != 0x7f)
+ throw new OggException("Stream Undecodable", OGG_ERROR_UNDECODABLE);
+
+ // The following four characters should be "FLAC".
+ if (fread($this->_filePointer, 4) != 'FLAC')
+ throw new OggException("Stream is undecodable due to a malformed header.", OGG_ERROR_UNDECODABLE);
+
+ $version = unpack("Cmajor/Cminor", fread($this->_filePointer, 2));
+ $this->_version = "{$version['major']}.{$version['minor']}";
+ if ($version['major'] > 1) {
+ throw new OggException("Cannot decode a version {$version['major']} FLAC stream", OGG_ERROR_UNDECODABLE);
+ }
+ $h = File_Ogg::_readBigEndian( $this->_filePointer,
+ array(
+ // Ogg-specific
+ 'num_headers' => 16,
+ 'flac_native_sig' => 32,
+ // METADATA_BLOCK_HEADER
+ 'is_last' => 1,
+ 'type' => 7,
+ 'length' => 24,
+ ));
+
+ // METADATA_BLOCK_STREAMINFO
+ // The variable names are canonical, and come from the FLAC source (format.h)
+ $this->_streamInfo = File_Ogg::_readBigEndian( $this->_filePointer,
+ array(
+ 'min_blocksize' => 16,
+ 'max_blocksize' => 16,
+ 'min_framesize' => 24,
+ 'max_framesize' => 24,
+ 'sample_rate' => 20,
+ 'channels' => 3,
+ 'bits_per_sample' => 5,
+ 'total_samples' => 36,
+ ));
+ $this->_streamInfo['md5sum'] = bin2hex(fread($this->_filePointer, 16));
+ }
+
+ /**
+ * Get an associative array containing header information about the stream
+ * @access public
+ * @return array
+ */
+ function getHeader()
+ {
+ return $this->_streamInfo;
+ }
+
+ function _decodeCommentsHeader()
+ {
+ fseek($this->_filePointer, $this->_streamData['pages'][1]['body_offset'], SEEK_SET);
+ $blockHeader = File_Ogg::_readBigEndian( $this->_filePointer,
+ array(
+ 'last_block' => 1,
+ 'block_type' => 7,
+ 'length' => 24
+ )
+ );
+ if ($blockHeader['block_type'] != 4) {
+ throw new OggException("Stream Undecodable", OGG_ERROR_UNDECODABLE);
+ }
+
+ $this->_decodeBareCommentsHeader();
+ }
+}
+?>
diff --git a/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Media.php b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Media.php
new file mode 100644
index 00000000..67ddaece
--- /dev/null
+++ b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Media.php
@@ -0,0 +1,262 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------------+
+// | File_Ogg PEAR Package for Accessing Ogg Bitstreams |
+// | Copyright (c) 2005-2007 |
+// | David Grant <david@grant.org.uk> |
+// | Tim Starling <tstarling@wikimedia.org> |
+// +----------------------------------------------------------------------------+
+// | This library is free software; you can redistribute it and/or |
+// | modify it under the terms of the GNU Lesser General Public |
+// | License as published by the Free Software Foundation; either |
+// | version 2.1 of the License, or (at your option) any later version. |
+// | |
+// | This library is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+// | Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public |
+// | License along with this library; if not, write to the Free Software |
+// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
+// +----------------------------------------------------------------------------+
+
+
+/**
+ * Parent class for media bitstreams
+ * Contains some functions common to various media formats
+ */
+abstract class File_Ogg_Media extends File_Ogg_Bitstream
+{
+ /**
+ * Maximum size of header comment to parse.
+ * Set to 1 MB by default. Make sure this is less than your PHP memory_limit.
+ */
+ const COMMENT_MAX_SIZE = 1000000;
+
+ /**
+ * Array to hold each of the comments.
+ *
+ * @access private
+ * @var array
+ */
+ var $_comments = array();
+
+ /**
+ * Vendor string for the stream.
+ *
+ * @access private
+ * @var string
+ */
+ var $_vendor;
+
+ /**
+ * Length of the stream in seconds
+ */
+ var $_streamLength;
+
+ /* Start offset of the stream in seconds */
+ var $_startOffset = 0;
+
+ /**
+ * Get a short string describing the type of the stream
+ * @return string
+ */
+ abstract function getType();
+
+ /**
+ * Get the 6-byte identification string expected in the common header
+ * @return string
+ */
+ function getIdentificationString()
+ {
+ return '';
+ }
+
+ /**
+ * @access private
+ * @param int $packetType
+ * @param int $pageOffset
+ */
+ function _decodeCommonHeader($packetType, $pageOffset)
+ {
+ fseek($this->_filePointer, $this->_streamData['pages'][$pageOffset]['body_offset'], SEEK_SET);
+ if ($packetType !== false) {
+ // Check if this is the correct header.
+ $packet = unpack("Cdata", fread($this->_filePointer, 1));
+ if ($packet['data'] != $packetType)
+ throw new OggException("Stream Undecodable", OGG_ERROR_UNDECODABLE);
+
+ // The following six characters should be equal to getIdentificationString()
+ $id = $this->getIdentificationString();
+ if ($id !== '' && fread($this->_filePointer, strlen($id)) !== $id)
+ throw new OggException("Stream is undecodable due to a malformed header.", OGG_ERROR_UNDECODABLE);
+ } // else seek only, no common header
+ }
+
+ /**
+ * Parse a Vorbis-style comments header.
+ *
+ * This function parses the comments header. The comments header contains a series of
+ * UTF-8 comments related to the audio encoded in the stream. This header also contains
+ * a string to identify the encoding software. More details on the comments header can
+ * be found at the following location: http://xiph.org/vorbis/doc/v-comment.html
+ *
+ * @access private
+ */
+ function _decodeBareCommentsHeader()
+ {
+ // Decode the vendor string length as a 32-bit unsigned integer.
+ $vendor_len = unpack("Vdata", fread($this->_filePointer, 4));
+ if ( $vendor_len['data'] > 0 ) {
+ // Retrieve the vendor string from the stream.
+ $this->_vendor = fread($this->_filePointer, $vendor_len['data']);
+ } else {
+ $this->_vendor = '';
+ }
+ // Decode the size of the comments list as a 32-bit unsigned integer.
+ $comment_list_length = unpack("Vdata", fread($this->_filePointer, 4));
+ // Iterate through the comments list.
+ for ($i = 0; $i < $comment_list_length['data']; ++$i) {
+ // Unpack the length of this comment.
+ $comment_length = unpack("Vdata", fread($this->_filePointer, 4));
+
+ // If the comment length is greater than specified limit, skip it.
+ if ( $comment_length['data'] > self::COMMENT_MAX_SIZE ) {
+ continue;
+ }
+
+ // Comments are in the format 'ARTIST=Super Furry Animals', so split it on the equals character.
+ // NOTE: Equals characters are strictly prohibited in either the COMMENT or DATA parts.
+ $comment = explode("=", fread($this->_filePointer, $comment_length['data']));
+ $comment_title = (string) $comment[0];
+ $comment_value = (string) $comment[1];
+
+ // Check if the comment type (e.g. ARTIST) already exists. If it does,
+ // take the new value, and the existing value (or array) and insert it
+ // into a new array. This is important, since each comment type may have
+ // multiple instances (e.g. ARTIST for a collaboration) and we should not
+ // overwrite the previous value.
+ if (isset($this->_comments[$comment_title])) {
+ if (is_array($this->_comments[$comment_title]))
+ $this->_comments[$comment_title][] = $comment_value;
+ else
+ $this->_comments[$comment_title] = array($this->_comments[$comment_title], $comment_value);
+ } else
+ $this->_comments[$comment_title] = $comment_value;
+ }
+ }
+
+ /**
+ * Number of channels used in this stream
+ *
+ * This function returns the number of channels used in this stream. This
+ * can range from 1 to 255, but will likely be 2 (stereo) or 1 (mono).
+ *
+ * @access public
+ * @return int
+ * @see File_Ogg_Vorbis::isMono()
+ * @see File_Ogg_Vorbis::isStereo()
+ * @see File_Ogg_Vorbis::isQuadrophonic()
+ */
+ function getChannels()
+ {
+ return ($this->_channels);
+ }
+
+ /**
+ * Provides a list of the comments extracted from the Vorbis stream.
+ *
+ * It is recommended that the user fully inspect the array returned by this function
+ * rather than blindly requesting a comment in false belief that it will always
+ * be present. Whilst the Vorbis specification dictates a number of popular
+ * comments (e.g. TITLE, ARTIST, etc.) for use in Vorbis streams, they are not
+ * guaranteed to appear.
+ *
+ * @access public
+ * @return array
+ */
+ function getCommentList()
+ {
+ return (array_keys($this->_comments));
+ }
+
+ /**
+ * Provides an interface to the numerous comments located with a Vorbis stream.
+ *
+ * A Vorbis stream may contain one or more instances of each comment, so the user
+ * should check the variable type before printing out the result of this method.
+ * The situation in which multiple instances of a comment occurring are not as
+ * rare as one might think, since they are conceivable at least for ARTIST comments
+ * in the situation where a track is a duet.
+ *
+ * @access public
+ * @param string $commentTitle Comment title to search for, e.g. TITLE.
+ * @param string $separator String to separate multiple values.
+ * @return string
+ */
+ function getField($commentTitle, $separator = ", ")
+ {
+ if (isset($this->_comments[$commentTitle])) {
+ if (is_array($this->_comments[$commentTitle]))
+ return (implode($separator, $this->_comments[$commentTitle]));
+ else
+ return ($this->_comments[$commentTitle]);
+ } else
+ // The comment doesn't exist in this file. The user should've called getCommentList first.
+ return ("");
+ }
+
+ /**
+ * Get the entire comments array.
+ * May return an empty array if the bitstream does not support comments.
+ *
+ * @access public
+ * @return array
+ */
+ function getComments() {
+ return $this->_comments;
+ }
+
+ /**
+ * Vendor of software used to encode this stream.
+ *
+ * Gives the vendor string for the software used to encode this stream.
+ * It is common to find libVorbis here. The majority of encoders appear
+ * to use libvorbis from Xiph.org.
+ *
+ * @access public
+ * @return string
+ */
+ function getVendor()
+ {
+ return ($this->_vendor);
+ }
+
+ /**
+ * Get an associative array containing header information about the stream
+ * @access public
+ * @return array
+ */
+ function getHeader()
+ {
+ return array();
+ }
+
+ /**
+ * Get the length of the stream in seconds
+ * @return float
+ */
+ function getLength()
+ {
+ return $this->_streamLength;
+ }
+ /**
+ * Get the start offset of the stream in seconds
+ *
+ * @return float
+ */
+ function getStartOffset(){
+ return $this->_startOffset;
+ }
+}
diff --git a/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Opus.php b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Opus.php
new file mode 100644
index 00000000..2c23d928
--- /dev/null
+++ b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Opus.php
@@ -0,0 +1,126 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------------+
+// | File_Ogg PEAR Package for Accessing Ogg Bitstreams |
+// | Copyright (c) 2013 |
+// | Jan Gerber <jgerber@wikimedia.org> |
+// +----------------------------------------------------------------------------+
+// | This library is free software; you can redistribute it and/or |
+// | modify it under the terms of the GNU Lesser General Public |
+// | License as published by the Free Software Foundation; either |
+// | version 2.1 of the License, or (at your option) any later version. |
+// | |
+// | This library is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+// | Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public |
+// | License along with this library; if not, write to the Free Software |
+// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
+// +----------------------------------------------------------------------------+
+
+
+define( 'OGG_OPUS_COMMENTS_PAGE_OFFSET', 1 );
+
+/**
+ * @author Jan Gerber <jgerber@wikimedia.org>
+ * @category File
+ * @copyright Jan Gerber <jgerber@wikimedia.org>
+ * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
+ * @link http://pear.php.net/package/File_Ogg
+ * @link http://www.opus-codec.org/
+ * @package File_Ogg
+ * @version 1
+ */
+class File_Ogg_Opus extends File_Ogg_Media
+{
+ /**
+ * @access private
+ */
+ function __construct($streamSerial, $streamData, $filePointer)
+ {
+ parent::__construct($streamSerial, $streamData, $filePointer);
+ $this->_decodeHeader();
+ $this->_decodeCommentsHeader();
+
+ $endSec = $this->getSecondsFromGranulePos( $this->_lastGranulePos );
+ $startSec = $this->getSecondsFromGranulePos( $this->_firstGranulePos );
+
+ if( $startSec > 1){
+ $this->_streamLength = $endSec - $startSec;
+ $this->_startOffset = $startSec;
+ }else{
+ $this->_streamLength = $endSec;
+ }
+ $this->_avgBitrate = $this->_streamLength ? ($this->_streamSize * 8) / $this->_streamLength : 0;
+ }
+
+ function getSecondsFromGranulePos( $granulePos ){
+ return (( '0x' . substr( $granulePos, 0, 8 ) ) * pow(2, 32)
+ + ( '0x' . substr( $granulePos, 8, 8 ) )
+ - $this->_header['pre_skip'])
+ / 48000;
+ }
+
+ /**
+ * Get a short string describing the type of the stream
+ * @return string
+ */
+ function getType()
+ {
+ return 'Opus';
+ }
+
+ /**
+ * Decode the stream header
+ * @access private
+ */
+ function _decodeHeader()
+ {
+ fseek($this->_filePointer, $this->_streamData['pages'][0]['body_offset'], SEEK_SET);
+ // The first 8 characters should be "OpusHead".
+ if (fread($this->_filePointer, 8) != 'OpusHead')
+ throw new OggException("Stream is undecodable due to a malformed header.", OGG_ERROR_UNDECODABLE);
+
+ $this->_header = File_Ogg::_readLittleEndian($this->_filePointer, array(
+ 'opus_version' => 8,
+ 'nb_channels' => 8,
+ 'pre_skip' => 16,
+ 'audio_sample_rate' => 32,
+ 'output_gain' => 16,
+ 'channel_mapping_family'=> 8,
+ ));
+ $this->_channels = $this->_header['nb_channels'];
+ }
+
+ /**
+ * Get an associative array containing header information about the stream
+ * @access public
+ * @return array
+ */
+ function getHeader() {
+ return $this->_header;
+ }
+
+ function getSampleRate()
+ {
+ //Opus always outputs 48kHz, the header only lists
+ //the samplerate of the source as reference
+ return 48000;
+ }
+
+ /**
+ * Decode the comments header
+ * @access private
+ */
+ function _decodeCommentsHeader()
+ {
+ $id = 'OpusTags';
+ $this->_decodeCommonHeader(false, OGG_OPUS_COMMENTS_PAGE_OFFSET);
+ if(fread($this->_filePointer, strlen($id)) !== $id)
+ throw new OggException("Stream is undecodable due to a malformed header.", OGG_ERROR_UNDECODABLE);
+ $this->_decodeBareCommentsHeader();
+ }
+}
+?>
diff --git a/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Speex.php b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Speex.php
new file mode 100644
index 00000000..42f9b0eb
--- /dev/null
+++ b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Speex.php
@@ -0,0 +1,122 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------------+
+// | File_Ogg PEAR Package for Accessing Ogg Bitstreams |
+// | Copyright (c) 2005-2007 |
+// | David Grant <david@grant.org.uk> |
+// | Tim Starling <tstarling@wikimedia.org> |
+// +----------------------------------------------------------------------------+
+// | This library is free software; you can redistribute it and/or |
+// | modify it under the terms of the GNU Lesser General Public |
+// | License as published by the Free Software Foundation; either |
+// | version 2.1 of the License, or (at your option) any later version. |
+// | |
+// | This library is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+// | Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public |
+// | License along with this library; if not, write to the Free Software |
+// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
+// +----------------------------------------------------------------------------+
+
+
+/**
+ * @author David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @category File
+ * @copyright David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
+ * @link http://pear.php.net/package/File_Ogg
+ * @link http://www.speex.org/docs.html
+ * @package File_Ogg
+ * @version CVS: $Id: Speex.php,v 1.10 2005/11/16 20:43:27 djg Exp $
+ */
+class File_Ogg_Speex extends File_Ogg_Media
+{
+ /**
+ * @access private
+ */
+ function __construct($streamSerial, $streamData, $filePointer)
+ {
+ parent::__construct($streamSerial, $streamData, $filePointer);
+ $this->_decodeHeader();
+ $this->_decodeCommentsHeader();
+ $endSec =
+ (( '0x' . substr( $this->_lastGranulePos, 0, 8 ) ) * pow(2, 32)
+ + ( '0x' . substr( $this->_lastGranulePos, 8, 8 ) ))
+ / $this->_header['rate'];
+
+ $startSec =
+ (( '0x' . substr( $this->_firstGranulePos, 0, 8 ) ) * pow(2, 32)
+ + ( '0x' . substr( $this->_firstGranulePos, 8, 8 ) ))
+ / $this->_header['rate'];
+
+ //make sure the offset is worth taking into account oggz_chop related hack
+ if( $startSec > 1){
+ $this->_streamLength = $endSec - $startSec;
+ $this->_startOffset = $startSec;
+ }else{
+ $this->_streamLength = $endSec;
+ }
+ }
+
+ /**
+ * Get a short string describing the type of the stream
+ * @return string
+ */
+ function getType()
+ {
+ return 'Speex';
+ }
+
+ /**
+ * Decode the stream header
+ * @access private
+ */
+ function _decodeHeader()
+ {
+ fseek($this->_filePointer, $this->_streamData['pages'][0]['body_offset'], SEEK_SET);
+ // The first 8 characters should be "Speex ".
+ if (fread($this->_filePointer, 8) != 'Speex ')
+ throw new OggException("Stream is undecodable due to a malformed header.", OGG_ERROR_UNDECODABLE);
+
+ $this->_version = fread($this->_filePointer, 20);
+ $this->_header = File_Ogg::_readLittleEndian($this->_filePointer, array(
+ 'speex_version_id' => 32,
+ 'header_size' => 32,
+ 'rate' => 32,
+ 'mode' => 32,
+ 'mode_bitstream_version'=> 32,
+ 'nb_channels' => 32,
+ 'bitrate' => 32,
+ 'frame_size' => 32,
+ 'vbr' => 32,
+ 'frames_per_packet' => 32,
+ 'extra_headers' => 32,
+ 'reserved1' => 32,
+ 'reserved2' => 32
+ ));
+ $this->_header['speex_version'] = $this->_version;
+ }
+
+ /**
+ * Get an associative array containing header information about the stream
+ * @access public
+ * @return array
+ */
+ function getHeader() {
+ return $this->_header;
+ }
+
+ /**
+ * Decode the comments header
+ * @access private
+ */
+ function _decodeCommentsHeader()
+ {
+ fseek($this->_filePointer, $this->_streamData['pages'][1]['body_offset'], SEEK_SET);
+ $this->_decodeBareCommentsHeader();
+ }
+}
+?>
diff --git a/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Theora.php b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Theora.php
new file mode 100644
index 00000000..b801ea26
--- /dev/null
+++ b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Theora.php
@@ -0,0 +1,240 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------------+
+// | File_Ogg PEAR Package for Accessing Ogg Bitstreams |
+// | Copyright (c) 2005-2007 |
+// | David Grant <david@grant.org.uk> |
+// | Tim Starling <tstarling@wikimedia.org> |
+// +----------------------------------------------------------------------------+
+// | This library is free software; you can redistribute it and/or |
+// | modify it under the terms of the GNU Lesser General Public |
+// | License as published by the Free Software Foundation; either |
+// | version 2.1 of the License, or (at your option) any later version. |
+// | |
+// | This library is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+// | Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public |
+// | License along with this library; if not, write to the Free Software |
+// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
+// +----------------------------------------------------------------------------+
+
+
+define( 'OGG_THEORA_IDENTIFICATION_HEADER', 0x80 );
+define( 'OGG_THEORA_COMMENTS_HEADER', 0x81 );
+define( 'OGG_THEORA_IDENTIFICATION_PAGE_OFFSET', 0 );
+define( 'OGG_THEORA_COMMENTS_PAGE_OFFSET', 1 );
+
+
+/**
+ * @author David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @category File
+ * @copyright David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
+ * @link http://pear.php.net/package/File_Ogg
+ * @link http://www.xiph.org/theora/
+ * @package File_Ogg
+ * @version CVS: $Id: Theora.php,v 1.9 2005/11/16 20:43:27 djg Exp $
+ */
+class File_Ogg_Theora extends File_Ogg_Media
+{
+ /**
+ * @access private
+ */
+ function __construct($streamSerial, $streamData, $filePointer)
+ {
+ parent::__construct($streamSerial, $streamData, $filePointer);
+ $this->_decodeIdentificationHeader();
+ $this->_decodeCommentsHeader();
+ $endSec = $this->getSecondsFromGranulePos( $this->_lastGranulePos );
+
+ $startSec = $this->getSecondsFromGranulePos( $this->_firstGranulePos );
+
+ //make sure the offset is worth taking into account oggz_chop related hack
+ if( $startSec > 1){
+ $this->_streamLength = $endSec - $startSec;
+ $this->_startOffset = $startSec;
+ }else{
+ $this->_streamLength = $endSec;
+ }
+
+ $this->_avgBitrate = $this->_streamLength ? ($this->_streamSize * 8) / $this->_streamLength : 0;
+ }
+ function getSecondsFromGranulePos($granulePos){
+ // Calculate GranulePos seconds
+ // First make some "numeric strings"
+ // These might not fit into PHP's integer type, but they will fit into
+ // the 53-bit mantissa of a double-precision number
+ $topWord = floatval( base_convert( substr( $granulePos, 0, 8 ), 16, 10 ) );
+ $bottomWord = floatval( base_convert( substr( $granulePos, 8, 8 ), 16, 10 ) );
+ // Calculate the keyframe position by shifting right by KFGSHIFT
+ // We don't use PHP's shift operators because they're terribly broken
+ // This is made slightly simpler by the fact that KFGSHIFT < 32
+ $keyFramePos = $topWord / pow(2, $this->_kfgShift - 32) +
+ floor( $bottomWord / pow(2, $this->_kfgShift) );
+ // Calculate the frame offset by masking off the top 64-KFGSHIFT bits
+ // This requires a bit of floating point trickery
+ $offset = fmod( $bottomWord, pow(2, $this->_kfgShift) );
+ // They didn't teach you that one at school did they?
+ // Now put it together with the frame rate to calculate time in seconds
+ return ( $keyFramePos + $offset ) / $this->_frameRate;
+ }
+ /**
+ * Get the 6-byte identification string expected in the common header
+ */
+ function getIdentificationString()
+ {
+ return OGG_STREAM_CAPTURE_THEORA;
+ }
+
+ /**
+ * Parse the identification header in a Theora stream.
+ * @access private
+ */
+ function _decodeIdentificationHeader()
+ {
+ $this->_decodeCommonHeader(OGG_THEORA_IDENTIFICATION_HEADER, OGG_THEORA_IDENTIFICATION_PAGE_OFFSET);
+ $h = File_Ogg::_readBigEndian( $this->_filePointer, array(
+ 'VMAJ' => 8,
+ 'VMIN' => 8,
+ 'VREV' => 8,
+ 'FMBW' => 16,
+ 'FMBH' => 16,
+ 'PICW' => 24,
+ 'PICH' => 24,
+ 'PICX' => 8,
+ 'PICY' => 8,
+ 'FRN' => 32,
+ 'FRD' => 32,
+ 'PARN' => 24,
+ 'PARD' => 24,
+ 'CS' => 8,
+ 'NOMBR' => 24,
+ 'QUAL' => 6,
+ 'KFGSHIFT' => 5,
+ 'PF' => 2));
+ if ( !$h ) {
+ throw new OggException("Stream is undecodable due to a truncated header.", OGG_ERROR_UNDECODABLE);
+ }
+
+ // Theora version
+ // Seems overly strict but this is what the spec says
+ // VREV is for backwards-compatible changes, apparently
+ if ( $h['VMAJ'] != 3 || $h['VMIN'] != 2 ) {
+ throw new OggException("Stream is undecodable due to an invalid theora version.", OGG_ERROR_UNDECODABLE);
+ }
+ $this->_theoraVersion = "{$h['VMAJ']}.{$h['VMIN']}.{$h['VREV']}";
+
+ // Frame height/width
+ if ( !$h['FMBW'] || !$h['FMBH'] ) {
+ throw new OggException("Stream is undecodable because it has frame size of zero.", OGG_ERROR_UNDECODABLE);
+ }
+ $this->_frameWidth = $h['FMBW'] * 16;
+ $this->_frameHeight = $h['FMBH'] * 16;
+
+ // Picture height/width
+ if ( $h['PICW'] > $this->_frameWidth || $h['PICH'] > $this->_frameHeight ) {
+ throw new OggException("Stream is undecodable because the picture width is greater than the frame width.", OGG_ERROR_UNDECODABLE);
+ }
+ $this->_pictureWidth = $h['PICW'];
+ $this->_pictureHeight = $h['PICH'];
+
+ // Picture offset
+ $this->_offsetX = $h['PICX'];
+ $this->_offsetY = $h['PICY'];
+ // Frame rate
+ $this->_frameRate = $h['FRD'] == 0 ? 0 : $h['FRN'] / $h['FRD'];
+ // Physical aspect ratio
+ if ( !$h['PARN'] || !$h['PARD'] ) {
+ $this->_physicalAspectRatio = 1;
+ } else {
+ $this->_physicalAspectRatio = $h['PARN'] / $h['PARD'];
+ }
+
+ // Color space
+ $colorSpaces = array(
+ 0 => 'Undefined',
+ 1 => 'Rec. 470M',
+ 2 => 'Rec. 470BG',
+ );
+ if ( isset( $colorSpaces[$h['CS']] ) ) {
+ $this->_colorSpace = $colorSpaces[$h['CS']];
+ } else {
+ $this->_colorSpace = 'Unknown (reserved)';
+ }
+
+ $this->_nomBitrate = $h['NOMBR'];
+
+ $this->_quality = $h['QUAL'];
+ $this->_kfgShift = $h['KFGSHIFT'];
+
+ $pixelFormats = array(
+ 0 => '4:2:0',
+ 1 => 'Unknown (reserved)',
+ 2 => '4:2:2',
+ 3 => '4:4:4',
+ );
+ $this->_pixelFormat = $pixelFormats[$h['PF']];
+
+ switch ( $h['PF'] ) {
+ case 0:
+ $h['NSBS'] =
+ floor( ($h['FMBW'] + 1) / 2 ) *
+ floor( ($h['FMBH'] + 1) / 2 ) + 2 *
+ floor( ($h['FMBW'] + 3) / 4 ) *
+ floor( ($h['FMBH'] + 3) / 4 );
+ $h['NBS'] = 6 * $h['FMBW'] * $h['FMBH'];
+ break;
+ case 2:
+ $h['NSBS'] =
+ floor( ($h['FMBW'] + 1) / 2 ) *
+ floor( ($h['FMBH'] + 1) / 2 ) + 2 *
+ floor( ($h['FMBW'] + 3) / 4 ) *
+ floor( ($h['FMBH'] + 1) / 2 );
+ $h['NBS'] = 8 * $h['FMBW'] * $h['FMBH'];
+ break;
+ case 3:
+ $h['NSBS'] =
+ 3 * floor( ($h['FMBW'] + 1) / 2 ) *
+ floor( ($h['FMBH'] + 1) / 2 );
+ $h['NBS'] = 12 * $h['FMBW'] * $h['FMBH'];
+ break;
+ default:
+ $h['NSBS'] = $h['NBS'] = 0;
+ }
+ $h['NMBS'] = $h['FMBW'] * $h['FMBH'];
+
+ $this->_idHeader = $h;
+ }
+
+ /**
+ * Get an associative array containing header information about the stream
+ * @access public
+ * @return array
+ */
+ function getHeader() {
+ return $this->_idHeader;
+ }
+
+ /**
+ * Get a short string describing the type of the stream
+ * @return string
+ */
+ function getType() {
+ return 'Theora';
+ }
+
+ /**
+ * Decode the comments header
+ * @access private
+ */
+ function _decodeCommentsHeader()
+ {
+ $this->_decodeCommonHeader(OGG_THEORA_COMMENTS_HEADER, OGG_THEORA_COMMENTS_PAGE_OFFSET);
+ $this->_decodeBareCommentsHeader();
+ }
+
+}
+?>
diff --git a/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Vorbis.php b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Vorbis.php
new file mode 100644
index 00000000..06c2c180
--- /dev/null
+++ b/extensions/TimedMediaHandler/handlers/OggHandler/File_Ogg/File/Ogg/Vorbis.php
@@ -0,0 +1,790 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------------+
+// | File_Ogg PEAR Package for Accessing Ogg Bitstreams |
+// | Copyright (c) 2005-2007 |
+// | David Grant <david@grant.org.uk> |
+// | Tim Starling <tstarling@wikimedia.org> |
+// +----------------------------------------------------------------------------+
+// | This library is free software; you can redistribute it and/or |
+// | modify it under the terms of the GNU Lesser General Public |
+// | License as published by the Free Software Foundation; either |
+// | version 2.1 of the License, or (at your option) any later version. |
+// | |
+// | This library is distributed in the hope that it will be useful, |
+// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
+// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
+// | Lesser General Public License for more details. |
+// | |
+// | You should have received a copy of the GNU Lesser General Public |
+// | License along with this library; if not, write to the Free Software |
+// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
+// +----------------------------------------------------------------------------+
+
+
+/**
+ * Check number for the first header in a Vorbis stream.
+ *
+ * @access private
+ */
+define("OGG_VORBIS_IDENTIFICATION_HEADER", 1);
+/**
+ * Check number for the second header in a Vorbis stream.
+ *
+ * @access private
+ */
+define("OGG_VORBIS_COMMENTS_HEADER", 3);
+/**
+ * Check number for the third header in a Vorbis stream.
+ *
+ * @access private
+ */
+define("OGG_VORBIS_SETUP_HEADER", 5);
+/**
+ * Error thrown if the stream appears to be corrupted.
+ *
+ * @access private
+ */
+define("OGG_VORBIS_ERROR_UNDECODABLE", OGG_ERROR_UNDECODABLE);
+/**
+ * Error thrown if the user attempts to extract a comment using a comment key
+ * that does not exist.
+ *
+ * @access private
+ */
+define("OGG_VORBIS_ERROR_INVALID_COMMENT", 2);
+
+define("OGG_VORBIS_IDENTIFICATION_PAGE_OFFSET", 0);
+define("OGG_VORBIS_COMMENTS_PAGE_OFFSET", 1);
+
+/**
+ * Error thrown if the user attempts to write a comment containing an illegal
+ * character
+ *
+ * @access private
+ */
+define("OGG_VORBIS_ERROR_ILLEGAL_COMMENT", 3);
+
+/**
+ * Extract the contents of a Vorbis logical stream.
+ *
+ * This class provides an interface to a Vorbis logical stream found within
+ * a Ogg stream. A variety of information may be extracted, including comment
+ * tags, running time, and bitrate. For more information, please see the following
+ * links.
+ *
+ * @author David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @category File
+ * @copyright David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
+ * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
+ * @link http://pear.php.net/package/File_Ogg
+ * @link http://www.xiph.org/vorbis/doc/
+ * @package File_Ogg
+ * @version CVS: $Id: Vorbis.php,v 1.13 2005/11/19 09:06:32 djg Exp $
+ */
+class File_Ogg_Vorbis extends File_Ogg_Media
+{
+
+ /**
+ * Version of vorbis specification used.
+ *
+ * @access private
+ * @var int
+ */
+ var $_version;
+
+ /**
+ * Number of channels in the vorbis stream.
+ *
+ * @access private
+ * @var int
+ */
+ var $_channels;
+
+ /**
+ * Number of samples per second in the vorbis stream.
+ *
+ * @access private
+ * @var int
+ */
+ var $_sampleRate;
+
+ /**
+ * Minimum bitrate for the vorbis stream.
+ *
+ * @access private
+ * @var int
+ */
+ var $_minBitrate;
+
+ /**
+ * Maximum bitrate for the vorbis stream.
+ *
+ * @access private
+ * @var int
+ */
+ var $_maxBitrate;
+
+ /**
+ * Nominal bitrate for the vorbis stream.
+ *
+ * @access private
+ * @var int
+ */
+ var $_nomBitrate;
+
+ /**
+ * Average bitrate for the vorbis stream.
+ *
+ * @access private
+ * @var float
+ */
+ var $_avgBitrate;
+
+ /**
+ * The length of this stream in seconds.
+ *
+ * @access private
+ * @var int
+ */
+ var $_streamLength;
+
+ /**
+ * the start offset of this stream in seconds
+ */
+ var $_startOffset;
+ /**
+ * Constructor for accessing a Vorbis logical stream.
+ *
+ * This method is the constructor for the native-PHP interface to a Vorbis logical
+ * stream, embedded within an Ogg physical stream.
+ *
+ * @param int $streamSerial Serial number of the logical stream.
+ * @param array $streamData Data for the requested logical stream.
+ * @param string $filePath Location of a file on the filesystem.
+ * @param pointer $filePointer File pointer for the current physical stream.
+ * @access private
+ */
+ function __construct($streamSerial, $streamData, $filePointer)
+ {
+ parent::__construct($streamSerial, $streamData, $filePointer);
+ $this->_decodeIdentificationHeader();
+ $this->_decodeCommentsHeader(OGG_VORBIS_COMMENTS_HEADER, OGG_VORBIS_COMMENTS_PAGE_OFFSET);
+
+ $endSec = $this->getSecondsFromGranulePos( $this->_lastGranulePos );
+ $startSec = $this->getSecondsFromGranulePos( $this->_firstGranulePos );
+
+ //make sure the offset is worth taking into account oggz_chop related hack
+ if( $startSec > 1){
+ $this->_streamLength = $endSec - $startSec;
+ $this->_startOffset = $startSec;
+ }else{
+ $this->_streamLength = $endSec;
+ }
+
+ $this->_avgBitrate = $this->_streamLength ? ($this->_streamSize * 8) / $this->_streamLength : 0;
+ }
+ function getSecondsFromGranulePos( $granulePos ){
+ return (( '0x' . substr( $granulePos, 0, 8 ) ) * pow(2, 32)
+ + ( '0x' . substr( $granulePos, 8, 8 ) ))
+ / $this->_idHeader['audio_sample_rate'];
+ }
+ /**
+ * Get a short string describing the type of the stream
+ */
+ function getType()
+ {
+ return 'Vorbis';
+ }
+
+ /**
+ * Parse the identification header (the first of three headers) in a Vorbis stream.
+ *
+ * This function parses the identification header. The identification header
+ * contains simple audio characteristics, such as sample rate and number of
+ * channels. There are a number of error-checking provisions laid down in the Vorbis
+ * specification to ensure the stream is pure.
+ *
+ * @access private
+ */
+ function _decodeIdentificationHeader()
+ {
+ $this->_decodeCommonHeader(OGG_VORBIS_IDENTIFICATION_HEADER, OGG_VORBIS_IDENTIFICATION_PAGE_OFFSET);
+
+ $h = File_Ogg::_readLittleEndian($this->_filePointer, array(
+ 'vorbis_version' => 32,
+ 'audio_channels' => 8,
+ 'audio_sample_rate' => 32,
+ 'bitrate_maximum' => 32,
+ 'bitrate_nominal' => 32,
+ 'bitrate_minimum' => 32,
+ 'blocksize_0' => 4,
+ 'blocksize_1' => 4,
+ 'framing_flag' => 1
+ ));
+
+ // The Vorbis stream version must be 0.
+ if ($h['vorbis_version'] == 0)
+ $this->_version = $h['vorbis_version'];
+ else
+ throw new OggException("Stream is undecodable due to an invalid vorbis stream version.", OGG_VORBIS_ERROR_UNDECODABLE);
+
+ // The number of channels MUST be greater than 0.
+ if ($h['audio_channels'] == 0)
+ throw new OggException("Stream is undecodable due to zero channels.", OGG_VORBIS_ERROR_UNDECODABLE);
+ else
+ $this->_channels = $h['audio_channels'];
+
+ // The sample rate MUST be greater than 0.
+ if ($h['audio_sample_rate'] == 0)
+ throw new OggException("Stream is undecodable due to a zero sample rate.", OGG_VORBIS_ERROR_UNDECODABLE);
+ else
+ $this->_sampleRate = $h['audio_sample_rate'];
+
+ // Extract the various bitrates
+ $this->_maxBitrate = $h['bitrate_maximum'];
+ $this->_nomBitrate = $h['bitrate_nominal'];
+ $this->_minBitrate = $h['bitrate_minimum'];
+
+ // Powers of two between 6 and 13 inclusive.
+ $valid_block_sizes = array(64, 128, 256, 512, 1024, 2048, 4096, 8192);
+
+ // blocksize_0 MUST be a valid blocksize.
+ $blocksize_0 = pow(2, $h['blocksize_0']);
+ if (FALSE == in_array($blocksize_0, $valid_block_sizes))
+ throw new OggException("Stream is undecodable because blocksize_0 is $blocksize_0, which is not a valid size.", OGG_VORBIS_ERROR_UNDECODABLE);
+
+ // Extract bits 5 to 8 from the character data.
+ // blocksize_1 MUST be a valid blocksize.
+ $blocksize_1 = pow(2, $h['blocksize_1']);
+ if (FALSE == in_array($blocksize_1, $valid_block_sizes))
+ throw new OggException("Stream is undecodable because blocksize_1 is not a valid size.", OGG_VORBIS_ERROR_UNDECODABLE);
+
+ // blocksize 0 MUST be less than or equal to blocksize 1.
+ if ($blocksize_0 > $blocksize_1)
+ throw new OggException("Stream is undecodable because blocksize_0 is not less than or equal to blocksize_1.", OGG_VORBIS_ERROR_UNDECODABLE);
+
+ // The framing bit MUST be set to mark the end of the identification header.
+ // Some encoders are broken though -- TS
+ /*
+ if ($h['framing_flag'] == 0)
+ throw new OggException("Stream in undecodable because the framing bit is not non-zero.", OGG_VORBIS_ERROR_UNDECODABLE);
+ */
+
+ $this->_idHeader = $h;
+ }
+
+ /**
+ * Decode the comments header
+ * @access private
+ * @param int $packetType
+ * @param int $pageOffset
+ */
+ function _decodeCommentsHeader($packetType, $pageOffset)
+ {
+ $this->_decodeCommonHeader($packetType, $pageOffset);
+ $this->_decodeBareCommentsHeader();
+ // The framing bit MUST be set to mark the end of the comments header.
+ $framing_bit = unpack("Cdata", fread($this->_filePointer, 1));
+ if ($framing_bit['data'] != 1)
+ throw new OggException("Stream Undecodable", OGG_VORBIS_ERROR_UNDECODABLE);
+ }
+
+ /**
+ * Get the 6-byte identification string expected in the common header
+ */
+ function getIdentificationString() {
+ return OGG_STREAM_CAPTURE_VORBIS;
+ }
+
+ /**
+ * Version of the Vorbis specification referred to in the encoding of this stream.
+ *
+ * This method returns the version of the Vorbis specification (currently 0 (ZERO))
+ * referred to by the encoder of this stream. The Vorbis specification is well-
+ * defined, and thus one does not expect this value to change on a frequent basis.
+ *
+ * @access public
+ * @return int
+ */
+ function getEncoderVersion()
+ {
+ return ($this->_version);
+ }
+
+ /**
+ * Samples per second.
+ *
+ * This function returns the number of samples used per second in this
+ * recording. Probably the most common value here is 44,100.
+ *
+ * @return int
+ * @access public
+ */
+ function getSampleRate()
+ {
+ return ($this->_sampleRate);
+ }
+
+ /**
+ * Various bitrate measurements
+ *
+ * Gives an array of the values of four different types of bitrates for this
+ * stream. The nominal, maximum and minimum values are found within the file,
+ * whereas the average value is computed.
+ *
+ * @access public
+ * @return array
+ */
+ function getBitrates()
+ {
+ return (array("nom" => $this->_nomBitrate, "max" => $this->_maxBitrate, "min" => $this->_minBitrate, "avg" => $this->_avgBitrate));
+ }
+
+ /**
+ * Gives the most accurate bitrate measurement from this stream.
+ *
+ * This function returns the most accurate bitrate measurement for this
+ * recording, depending on values set in the stream header.
+ *
+ * @access public
+ * @return float
+ */
+ function getBitrate()
+ {
+ if ($this->_avgBitrate != 0)
+ return ($this->_avgBitrate);
+ elseif ($this->_nomBitrate != 0)
+ return ($this->_nomBitrate);
+ else
+ return (($this->_minBitrate + $this->_maxBitrate) / 2);
+ }
+
+ /**
+ * Gives the length (in seconds) of this stream.
+ *
+ * @access public
+ * @return int
+ */
+ function getLength()
+ {
+ return ($this->_streamLength);
+ }
+ /**
+ * Get the start offset of the stream in seconds
+ * @access public
+ * @return int
+ */
+ function getStartOffset(){
+ return ($this->_startOffset);
+ }
+ /**
+ * States whether this logical stream was encoded in mono.
+ *
+ * @access public
+ * @return boolean
+ */
+ function isMono()
+ {
+ return ($this->_channels == 1);
+ }
+
+ /**
+ * States whether this logical stream was encoded in stereo.
+ *
+ * @access public
+ * @return boolean
+ */
+ function isStereo()
+ {
+ return ($this->_channels == 2);
+ }
+
+ /**
+ * States whether this logical stream was encoded in quadrophonic sound.
+ *
+ * @access public
+ * @return boolean
+ */
+ function isQuadrophonic()
+ {
+ return ($this->_channels == 4);
+ }
+
+ /**
+ * The title of this track, e.g. "What's Up Pussycat?".
+ *
+ * @access public
+ * @return string
+ */
+ function getTitle()
+ {
+ return ($this->getField("TITLE"));
+ }
+
+ /**
+ * Set the title of this track.
+ *
+ * @access public
+ * @param string $title
+ * @param boolean $replace
+ */
+ function setTitle($title, $replace = true)
+ {
+ $this->setField("TITLE", $title, $replace);
+ }
+
+ /**
+ * The version of the track, such as a remix.
+ *
+ * @access public
+ * @return string
+ */
+ function getVersion()
+ {
+ return $this->getField("VERSION");
+ }
+
+ /**
+ * Set the version of this track.
+ *
+ * @access public
+ * @param string $version
+ * @param boolean $replace
+ */
+ function setVersion($version, $replace = true)
+ {
+ $this->setField("VERSION", $version, $replace);
+ }
+
+ /**
+ * The album or collection from which this track comes.
+ *
+ * @access public
+ * @return string
+ */
+ function getAlbum()
+ {
+ return ($this->getField("ALBUM"));
+ }
+
+ /**
+ * Set the album or collection for this track.
+ *
+ * @access public
+ * @param string $album
+ * @param boolean $replace
+ */
+ function setAlbum($album, $replace = true)
+ {
+ $this->setField("ALBUM", $album, $replace);
+ }
+
+ /**
+ * The number of this track if it is part of a larger collection.
+ *
+ * @access public
+ * @return string
+ */
+ function getTrackNumber()
+ {
+ return ($this->getField("TRACKNUMBER"));
+ }
+
+ /**
+ * Set the number of this relative to the collection.
+ *
+ * @access public
+ * @param int $number
+ * @param boolean $replace
+ */
+ function setTrackNumber($number, $replace = true)
+ {
+ $this->setField("TRACKNUMBER", $number, $replace);
+ }
+
+ /**
+ * The artist responsible for this track.
+ *
+ * This function returns the name of the artist responsible for this
+ * recording, which may be either a solo-artist, duet or group.
+ *
+ * @access public
+ * @return string
+ */
+ function getArtist()
+ {
+ return ($this->getField("ARTIST"));
+ }
+
+ /**
+ * Set the artist of this track.
+ *
+ * @access public
+ * @param string $artist
+ * @param boolean $replace
+ */
+ function setArtist($artist, $replace = true)
+ {
+ $this->setField("ARTIST", $artist, $replace = true);
+ }
+
+ /**
+ * The performer of this track, such as an orchestra
+ *
+ * @access public
+ * @return string
+ */
+ function getPerformer()
+ {
+ return ($this->getField("PERFORMER"));
+ }
+
+ /**
+ * Set the performer of this track.
+ *
+ * @access public
+ * @param string $performer
+ * @param boolean $replace
+ */
+ function setPerformer($performer, $replace = true)
+ {
+ $this->setField("PERFORMER", $performer, $replace);
+ }
+
+ /**
+ * The copyright attribution for this track.
+ *
+ * @access public
+ * @return string
+ */
+ function getCopyright()
+ {
+ return ($this->getField("COPYRIGHT"));
+ }
+
+ /**
+ * Set the copyright attribution for this track.
+ *
+ * @access public
+ * @param string $copyright
+ * @param boolean $replace
+ */
+ function setCopyright($copyright, $replace = true)
+ {
+ $this->setField("COPYRIGHT", $copyright, $replace);
+ }
+
+ /**
+ * The rights of distribution for this track.
+ *
+ * This funtion returns the license for this track, and may include
+ * copyright information, or a creative commons statement.
+ *
+ * @access public
+ * @return string
+ */
+ function getLicense()
+ {
+ return ($this->getField("LICENSE"));
+ }
+
+ /**
+ * Set the distribution rights for this track.
+ *
+ * @access public
+ * @param string $license
+ * @param boolean $replace
+ */
+ function setLicense($license, $replace = true)
+ {
+ $this->setField("LICENSE", $license, $replace);
+ }
+
+ /**
+ * The organisation responsible for this track.
+ *
+ * This function returns the name of the organisation responsible for
+ * the production of this track, such as the record label.
+ *
+ * @access public
+ * @return string
+ */
+ function getOrganization()
+ {
+ return ($this->getField("ORGANIZATION"));
+ }
+
+ /**
+ * Set the organisation responsible for this track.
+ *
+ * @access public
+ * @param string $organization
+ * @param boolean $replace
+ */
+ function setOrganziation($organization, $replace = true)
+ {
+ $this->setField("ORGANIZATION", $organization, $replace);
+ }
+
+ /**
+ * A short description of the contents of this track.
+ *
+ * This function returns a short description of this track, which might
+ * contain extra information that doesn't fit anywhere else.
+ *
+ * @access public
+ * @return string
+ */
+ function getDescription()
+ {
+ return ($this->getField("DESCRIPTION"));
+ }
+
+ /**
+ * Set the description of this track.
+ *
+ * @access public
+ * @param string $description
+ * @param boolean $replace
+ */
+ function setDescription($description, $replace = true)
+ {
+ $this->setField("DESCRIPTION", $replace);
+ }
+
+ /**
+ * The genre of this recording (e.g. Rock)
+ *
+ * This function returns the genre of this recording. There are no pre-
+ * defined genres, so this is completely up to the tagging software.
+ *
+ * @access public
+ * @return string
+ */
+ function getGenre()
+ {
+ return ($this->getField("GENRE"));
+ }
+
+ /**
+ * Set the genre of this track.
+ *
+ * @access public
+ * @param string $genre
+ * @param boolean $replace
+ */
+ function setGenre($genre, $replace = true)
+ {
+ $this->setField("GENRE", $genre, $replace);
+ }
+
+ /**
+ * The date of the recording of this track.
+ *
+ * This function returns the date on which this recording was made. There
+ * is no specification for the format of this date.
+ *
+ * @access public
+ * @return string
+ */
+ function getDate()
+ {
+ return ($this->getField("DATE"));
+ }
+
+ /**
+ * Set the date of recording for this track.
+ *
+ * @access public
+ * @param string $date
+ * @param boolean $replace
+ */
+ function setDate($date, $replace = true)
+ {
+ $this->setField("DATE", $date, $replace);
+ }
+
+ /**
+ * Where this recording was made.
+ *
+ * This function returns where this recording was made, such as a recording
+ * studio, or concert venue.
+ *
+ * @access public
+ * @return string
+ */
+ function getLocation()
+ {
+ return ($this->getField("LOCATION"));
+ }
+
+ /**
+ * Set the location of the recording of this track.
+ *
+ * @access public
+ * @param string $location
+ * @param boolean $replace
+ */
+ function setLocation($location, $replace = true)
+ {
+ $this->setField("LOCATION", $location, $replace);
+ }
+
+ /**
+ * @access public
+ * @return string
+ */
+ function getContact()
+ {
+ return ($this->getField("CONTACT"));
+ }
+
+ /**
+ * Set the contact information for this track.
+ *
+ * @access public
+ * @param string $contact
+ * @param boolean $replace
+ */
+ function setContact($contact, $replace = true)
+ {
+ $this->setField("CONTACT", $contact, $replace);
+ }
+
+ /**
+ * International Standard Recording Code.
+ *
+ * Returns the International Standard Recording Code. This code can be
+ * validated using the Validate_ISPN package.
+ *
+ * @access public
+ * @return string
+ */
+ function getIsrc()
+ {
+ return ($this->getField("ISRC"));
+ }
+
+ /**
+ * Set the ISRC for this track.
+ *
+ * @access public
+ * @param string $isrc
+ * @param boolean $replace
+ */
+ function setIsrc($isrc, $replace = true)
+ {
+ $this->setField("ISRC", $isrc, $replace);
+ }
+
+ /**
+ * Get an associative array containing header information about the stream
+ * @access public
+ * @return array
+ */
+ function getHeader() {
+ return $this->_idHeader;
+ }
+}
+?>