Beispiel #1
0
 /**
  * Constructs the class with given file and options.
  *
  * @param string|resource|HausDesign_Io_Reader $filename The path to the file,
  *  file descriptor of an opened file, or a {@link HausDesign_Io_Reader} instance.
  * @param Array                          $options  The options array.
  */
 public function __construct($filename, $options = array())
 {
     if ($filename instanceof HausDesign_Io_Reader) {
         $this->_reader =& $filename;
     } else {
         require_once 'HausDesign/Io/FileReader.php';
         try {
             $this->_reader = new HausDesign_Io_FileReader($filename);
         } catch (HausDesign_Io_Exception $e) {
             $this->_reader = null;
             require_once 'HausDesign/Media/Mpeg/Exception.php';
             throw new HausDesign_Media_Mpeg_Exception($e->getMessage());
         }
     }
     $this->setOptions($options);
     $startCode = 0;
     $startTime = 0;
     $pictureCount = 0;
     $pictureRate = 0;
     $rates = array(0, 23.976, 24, 25, 29.97, 30, 50, 59.94, 60);
     $foundSeqHdr = false;
     $foundGOP = false;
     do {
         do {
             $startCode = $this->nextStartCode();
         } while ($startCode != 0x1b3 && $startCode != 0x1b8);
         if ($startCode == 0x1b3 && $pictureRate == 0) {
             $i1 = $this->_reader->readUInt32BE();
             $i2 = $this->_reader->readUInt32BE();
             if (!HausDesign_Bit_Twiddling::testAllBits($i2, 0x2000)) {
                 require_once 'HausDesign/Media/Mpeg/Exception.php';
                 throw new HausDesign_Media_Mpeg_Exception('File does not contain a valid MPEG Program Stream (Invalid mark)');
             }
             $pictureRate = $rates[HausDesign_Bit_Twiddling::getValue($i1, 4, 8)];
             $foundSeqHdr = true;
         }
         if ($startCode == 0x1b8) {
             $tmp = $this->_reader->readUInt32BE();
             $startTime = ($tmp >> 26 & 0x1f) * 60 * 60 * 1000 + ($tmp >> 20 & 0x3f) * 60 * 1000 + ($tmp >> 13 & 0x3f) * 1000 + (int) (1 / $pictureRate * ($tmp >> 7 & 0x3f) * 1000);
             $foundGOP = true;
         }
     } while (!$foundSeqHdr || !$foundGOP);
     $this->_reader->setOffset($this->_reader->getSize());
     do {
         if (($startCode = $this->prevStartCode()) == 0x100) {
             $pictureCount++;
         }
     } while ($startCode != 0x1b8);
     $this->_reader->skip(4);
     $tmp = $this->_reader->readUInt32BE();
     $this->_length = (($tmp >> 26 & 0x1f) * 60 * 60 * 1000 + ($tmp >> 20 & 0x3f) * 60 * 1000 + ($tmp >> 13 & 0x3f) * 1000 + (int) (1 / $pictureRate * ($tmp >> 7 & 0x3f) * 1000) - $startTime + (int) (1 / $pictureRate * $pictureCount * 1000)) / 1000;
 }
Beispiel #2
0
 /**
  * Constructs the class with given parameters and reads object related data
  * from the frame.
  *
  * @param HausDesign_Io_Reader $reader The reader object.
  * @param Array          $options Array of options.
  */
 public function __construct($reader, &$options = array())
 {
     parent::__construct($reader, $options);
     $this->_offset = $this->_reader->getOffset();
     $header = null;
     for ($i = 0; $i < 5775; $i++) {
         $header = $this->_reader->readUInt32BE();
         if (HausDesign_Bit_Twiddling::testAllBits(HausDesign_Bit_Twiddling::getValue($header, 21, 32), 0xffe)) {
             break;
         }
         $this->_reader->setOffset(++$this->_offset + 1);
         if ($this->_offset == $this->_reader->getSize() || $i == 5775 - 1) {
             require_once 'HausDesign/Media/Mpeg/Exception.php';
             throw new HausDesign_Media_Mpeg_Exception('File does not contain a valid MPEG Audio Bit Stream (Invalid frame sync and resynchronization failed)');
         }
     }
     $this->_version = HausDesign_Bit_Twiddling::getValue($header, 19, 20);
     $this->_frequencyType = HausDesign_Bit_Twiddling::testBit($header, 19);
     $this->_layer = HausDesign_Bit_Twiddling::getValue($header, 17, 18);
     $this->_redundancy = !HausDesign_Bit_Twiddling::testBit($header, 16);
     $this->_bitrate = isset(self::$bitrates[$this->_frequencyType][$this->_layer][$index = HausDesign_Bit_Twiddling::getValue($header, 12, 15)]) ? self::$bitrates[$this->_frequencyType][$this->_layer][$index] : false;
     $this->_samplingFrequency = isset(self::$samplingFrequencies[$this->_version][$index = HausDesign_Bit_Twiddling::getValue($header, 10, 11)]) ? self::$samplingFrequencies[$this->_version][$index] : false;
     $this->_padding = HausDesign_Bit_Twiddling::testBit($header, 9);
     $this->_mode = HausDesign_Bit_Twiddling::getValue($header, 6, 7);
     $this->_modeExtension = HausDesign_Bit_Twiddling::getValue($header, 4, 5);
     $this->_copyright = HausDesign_Bit_Twiddling::testBit($header, 3);
     $this->_original = HausDesign_Bit_Twiddling::testBit($header, 2);
     $this->_emphasis = HausDesign_Bit_Twiddling::getValue($header, 0, 1);
     $this->_length = (int) (self::$coefficients[$this->_frequencyType][$this->_layer] * ($this->_bitrate * 1000) / $this->_samplingFrequency + ($this->_padding ? 1 : 0)) * self::$slotsizes[$this->_layer];
     $this->_samples = self::$samples[$this->_frequencyType][$this->_layer];
     if ($this->getOption('readmode', 'lazy') == 'full') {
         $this->_readCrc();
         $this->_readData();
     }
     $this->_reader->skip($this->_length - 4);
 }