From 14ac1b562db32b68d684fefdec7d3e17b83ed745 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Sat, 30 Nov 2013 02:07:05 -0800 Subject: [PATCH] clean up frame extraction code --- matroska.php | 34 +++++++++++++++++++- videodata.php | 89 +++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 105 insertions(+), 18 deletions(-) diff --git a/matroska.php b/matroska.php index aad18c08..ff6ce03d 100644 --- a/matroska.php +++ b/matroska.php @@ -9,6 +9,7 @@ class EBMLElementType { // Information needed to parse all possible element types in a document class EBMLElementTypeList { private $_els; + private $_ids; public function __construct($filename) { $lines = file($filename); @@ -27,6 +28,7 @@ class EBMLElementTypeList { } } $this->_els[$id] = $t; + $this->_ids[strtoupper($t->name)] = $id; } } @@ -39,6 +41,12 @@ class EBMLElementTypeList { return $this->_els[$id]->name; } + public function id($name) { + $name = strtoupper($name); + if (!isset($this->_ids[$name])) return NULL; + return $this->_ids[$name]; + } + public function datatype($id) { if ($id == 'root') return 'container'; if (!isset($this->_els[$id])) return 'binary'; @@ -379,7 +387,7 @@ class EBMLElementList extends EBMLElement implements Iterator { foreach ($this as $element) { $iElement++; if ($iElement > self::$MAX_ELEMENTS) throw new Exception('not supported: too many elements'); - if ($element->name() == $name) { + if (strtoupper($element->name()) == strtoupper($name)) { return $element->value(); } } @@ -488,3 +496,27 @@ function readMatroska($fileHandle) { } return $root; } + +function encodeVarInt($n) { + $data = ''; + $flag = 0x80; + while ($n >= $flag) { + if ($flag == 0) { + throw new Exception('not supported: number too large'); + } + $data = chr($n & 0xFF) . $data; + $n = $n >> 8; + $flag = $flag >> 1; + } + $data = chr($n | $flag) . $data; + return $data; +} + +function encodeElementName($name) { + global $EBML_ELEMENTS; + return encodeVarInt($EBML_ELEMENTS->id($name)); +} + +function encodeElement($name, $content) { + return encodeElementName($name) . encodeVarInt(strlen($content)) . $content; +} diff --git a/videodata.php b/videodata.php index 3fe9eeee..ec302fdc 100644 --- a/videodata.php +++ b/videodata.php @@ -1,22 +1,77 @@ size(), $pixelWidth, $pixelHeight, $codecID) . $frame->readAll(); + $data['frame'] = muxVPxFrame($pixelWidth, $pixelHeight, $codecID, $frame->readAll()); } catch (Exception $e) { error_log($e->getMessage());