Permalink
Browse files

Merging from my fork at http://svn.wikimedia.org/svnroot/mediawiki/tr…

…unk/extensions/OggHandler/PEAR/File_Ogg

* Updated package.xml ready for release
* Added header parsing for Theora, Speex and Flac.
* Removed partially written save/encode code.
* Introduced some PHP 5 features, no longer supports PHP 4.
* Introduced convenience functions getHeader() and getComments() to retrieve metadata in associative array format.
* Fixed the error handling, using exceptions instead of inappropriate continuation.
* Added overall length calculation for multiplexed streams
* Fixed a page splitting bug


git-svn-id: http://svn.php.net/repository/pear/packages/File_Ogg/trunk@251848 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent f40c5bc commit c2abfe024f7837ca5e5dd07b7300a59d8e1c2d34 Tim Starling committed Jan 31, 2008
Showing with 1,143 additions and 510 deletions.
  1. +275 −83 File/Ogg.php
  2. +52 −6 File/Ogg/Bitstream.php
  3. +98 −7 File/Ogg/Flac.php
  4. +227 −0 File/Ogg/Media.php
  5. +74 −7 File/Ogg/Speex.php
  6. +192 −6 File/Ogg/Theora.php
  7. +77 −324 File/Ogg/Vorbis.php
  8. +148 −77 package.xml
View
Oops, something went wrong.
@@ -2,7 +2,9 @@
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------------+
// | File_Ogg PEAR Package for Accessing Ogg Bitstreams |
-// | Copyright (c) 2005 David Grant <david@grant.org.uk> |
+// | 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 |
@@ -19,10 +21,11 @@
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
// +----------------------------------------------------------------------------+
+
/**
- * @author David Grant <david@grant.org.uk>
+ * @author David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
* @category File
- * @copyright David Grant <david@grant.org.uk>
+ * @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
@@ -48,11 +51,44 @@ class File_Ogg_Bitstream
/**
* The number of bits used in this stream.
*
- * @var int
+ * @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 File_Ogg_Bitstream($streamSerial, $streamData, $filePointer)
+ {
+ $this->_streamSerial = $streamSerial;
+ $this->_streamList = $streamData;
+ $this->_filePointer = $filePointer;
+ $this->_lastGranulePos = 0;
+ // This gives an accuracy of approximately 99.7% to the streamsize of ogginfo.
+ foreach ( $streamData as $packet ) {
+ $this->_streamSize += $packet['data_length'];
+ # Reject -1 as a granule pos, that means no segment finished in the packet
+ if ( $packet['abs_granule_pos'] != 'ffffffffffffffff' ) {
+ $this->_lastGranulePos = max($this->_lastGranulePos, $packet['abs_granule_pos']);
+ }
+ }
+ $this->_group = $streamData[0]['group'];
+ }
+
/**
* Gives the serial number of this stream.
*
@@ -81,5 +117,15 @@ function getSize()
{
return ($this->_streamSize);
}
+
+ /**
+ * Get the multiplexed group ID
+ */
+ function getGroup()
+ {
+ return $this->_group;
+ }
+
}
-?>
+
+?>
View
@@ -2,7 +2,9 @@
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------------+
// | File_Ogg PEAR Package for Accessing Ogg Bitstreams |
-// | Copyright (c) 2005 David Grant <david@grant.org.uk> |
+// | 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 |
@@ -19,25 +21,114 @@
// | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
// +----------------------------------------------------------------------------+
-require_once('File/Ogg/Bitstream.php');
+require_once('File/Ogg/Media.php');
/**
- * @author David Grant <david@grant.org.uk>
+ * @author David Grant <david@grant.org.uk>, Tim Starling <tstarling@wikimedia.org>
* @category File
- * @copyright David Grant <david@grant.org.uk>
+ * @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$
*/
-class File_Ogg_Flac extends File_Ogg_Bitstream
+class File_Ogg_Flac extends File_Ogg_Media
{
/**
* @access private
*/
- function File_Ogg_Flac()
+ function File_Ogg_Flac($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->_streamList[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 PEAR_Exception("Stream Undecodable", OGG_ERROR_UNDECODABLE);
+
+ // The following four characters should be "FLAC".
+ if (fread($this->_filePointer, 4) != 'FLAC')
+ throw new PEAR_Exception("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 PEAR_Exception("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->_streamList[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 PEAR_Exception("Stream Undecodable", OGG_ERROR_UNDECODABLE);
+ }
+
+ $this->_decodeBareCommentsHeader();
}
}
-?>
+?>
Oops, something went wrong.

0 comments on commit c2abfe0

Please sign in to comment.