/src/Zend/Media/Iso14496/Box/Iloc.php

http://php-reader.googlecode.com/ · PHP · 245 lines · 124 code · 11 blank · 110 comment · 35 complexity · 8eae80ae9690e4527e01348990d8d22a MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * @category Zend
  28. * @package Zend_Media
  29. * @subpackage ISO14496
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @version $Id: Iloc.php 177 2010-03-09 13:13:34Z svollbehr $
  33. */
  34. /**#@+ @ignore */
  35. require_once 'Zend/Media/Iso14496/Box.php';
  36. /**#@-*/
  37. /**
  38. * The <i>The Item Location Box</i> provides a directory of resources in this or
  39. * other files, by locating their containing file, their offset within that
  40. * file, and their length. Placing this in binary format enables common handling
  41. * of this data, even by systems which do not understand the particular metadata
  42. * system (handler) used. For example, a system might integrate all the
  43. * externally referenced metadata resources into one file, re-adjusting file
  44. * offsets and file references accordingly.
  45. *
  46. * Items may be stored fragmented into extents, e.g. to enable interleaving. An
  47. * extent is a contiguous subset of the bytes of the resource; the resource is
  48. * formed by concatenating the extents. If only one extent is used then either
  49. * or both of the offset and length may be implied:
  50. *
  51. * o If the offset is not identified (the field has a length of zero), then
  52. * the beginning of the file (offset 0) is implied.
  53. * o If the length is not specified, or specified as zero, then the entire
  54. * file length is implied. References into the same file as this metadata,
  55. * or items divided into more than one extent, should have an explicit
  56. * offset and length, or use a MIME type requiring a different
  57. * interpretation of the file, to avoid infinite recursion.
  58. *
  59. * The size of the item is the sum of the extentLengths. Note: extents may be
  60. * interleaved with the chunks defined by the sample tables of tracks.
  61. *
  62. * The dataReferenceIndex may take the value 0, indicating a reference into the
  63. * same file as this metadata, or an index into the dataReference table.
  64. *
  65. * Some referenced data may itself use offset/length techniques to address
  66. * resources within it (e.g. an MP4 file might be included in this way).
  67. * Normally such offsets are relative to the beginning of the containing file.
  68. * The field base offset provides an additional offset for offset calculations
  69. * within that contained data. For example, if an MP4 file is included within a
  70. * file formatted to this specification, then normally data-offsets within that
  71. * MP4 section are relative to the beginning of file; baseOffset adds to those
  72. * offsets.
  73. *
  74. * @category Zend
  75. * @package Zend_Media
  76. * @subpackage ISO14496
  77. * @author Sven Vollbehr <sven@vollbehr.eu>
  78. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  79. * @license http://framework.zend.com/license/new-bsd New BSD License
  80. * @version $Id: Iloc.php 177 2010-03-09 13:13:34Z svollbehr $
  81. */
  82. final class Zend_Media_Iso14496_Box_Iloc extends Zend_Media_Iso14496_Box
  83. {
  84. /** @var Array */
  85. private $_items = array();
  86. /**
  87. * Constructs the class with given parameters and reads box related data
  88. * from the ISO Base Media file.
  89. *
  90. * @param Zend_Io_Reader $reader The reader object.
  91. * @param Array $options The options array.
  92. */
  93. public function __construct($reader, &$options = array())
  94. {
  95. parent::__construct($reader, $options);
  96. $offsetSize = (($tmp = $this->_reader->readUInt16BE()) >> 12) & 0xf;
  97. $lengthSize = ($tmp >> 8) & 0xf;
  98. $baseOffsetSize = ($tmp >> 4) & 0xf;
  99. $itemCount = $this->_reader->readUInt16BE();
  100. for ($i = 0; $i < $itemCount; $i++) {
  101. $item = array();
  102. $item['itemId'] = $this->_reader->readUInt16BE();
  103. $item['dataReferenceIndex'] = $this->_reader->readUInt16BE();
  104. $item['baseOffset'] =
  105. ($baseOffsetSize == 4 ? $this->_reader->readUInt32BE() :
  106. ($baseOffsetSize == 8 ? $this->_reader->readInt64BE() : 0));
  107. $extentCount = $this->_reader->readUInt16BE();
  108. $item['extents'] = array();
  109. for ($j = 0; $j < $extentCount; $j++) {
  110. $extent = array();
  111. $extent['offset'] =
  112. ($offsetSize == 4 ? $this->_reader->readUInt32BE() :
  113. ($offsetSize == 8 ? $this->_reader->readInt64BE() : 0));
  114. $extent['length'] =
  115. ($lengthSize == 4 ? $this->_reader->readUInt32BE() :
  116. ($lengthSize == 8 ? $this->_reader->readInt64BE() : 0));
  117. $item['extents'][] = $extent;
  118. }
  119. $this->_items[] = $item;
  120. }
  121. }
  122. /**
  123. * Returns the array of items. Each entry has the following keys set:
  124. * itemId, dataReferenceIndex, baseOffset, and extents.
  125. *
  126. * @return Array
  127. */
  128. public function getItems()
  129. {
  130. return $this->_items;
  131. }
  132. /**
  133. * Sets the array of items. Each entry must have the following keys set:
  134. * itemId, dataReferenceIndex, baseOffset, and extents.
  135. *
  136. * @return Array
  137. */
  138. public function setItems($items)
  139. {
  140. $this->_items = $items;
  141. }
  142. /**
  143. * Returns the box heap size in bytes.
  144. *
  145. * @return integer
  146. */
  147. public function getHeapSize()
  148. {
  149. $totalSize = 4;
  150. for ($i = 0; $i < count($this->_itemId); $i++) {
  151. $totalSize += 6;
  152. if ($this->_itemId[$i]['baseOffset'] > 0xffffffff) {
  153. $totalSize += 8;
  154. } else {
  155. $totalSize += 4;
  156. }
  157. $extentCount = count($this->_itemId[$i]['extents']);
  158. for ($j = 0; $j < $extentCount; $j++) {
  159. if ($this->_itemId[$i]['extents'][$j]['offset'] > 0xffffffff) {
  160. $totalSize += 8 * $extentCount;
  161. } else {
  162. $totalSize += 4 * $extentCount;
  163. }
  164. if ($this->_itemId[$i]['extents'][$j]['length'] > 0xffffffff) {
  165. $totalSize += 8 * $extentCount;
  166. } else {
  167. $totalSize += 4 * $extentCount;
  168. }
  169. }
  170. }
  171. return parent::getHeapSize() + $totalSize;
  172. }
  173. /**
  174. * Writes the box data.
  175. *
  176. * @param Zend_Io_Writer $writer The writer object.
  177. * @return void
  178. */
  179. protected function _writeData($writer)
  180. {
  181. parent::_writeData($writer);
  182. $offsetSize = 4;
  183. $lengthSize = 4;
  184. $baseOffsetSize = 4;
  185. $itemCount = count($this->_itemId);
  186. for ($i = 0; $i < $itemCount; $i++) {
  187. if ($this->_itemId[$i]['baseOffset'] > 0xffffffff) {
  188. $baseOffsetSize = 8;
  189. }
  190. for ($j = 0; $j < count($this->_itemId[$i]['extents']); $j++) {
  191. if ($this->_itemId[$i]['extents'][$j]['offset'] > 0xffffffff) {
  192. $offsetSize = 8;
  193. }
  194. if ($this->_itemId[$i]['extents'][$j]['length'] > 0xffffffff) {
  195. $lengthSize = 8;
  196. }
  197. }
  198. }
  199. $writer->writeUInt16BE
  200. ((($offsetSize & 0xf) << 12) | (($lengthSize & 0xf) << 8) |
  201. (($baseOffsetSize & 0xf) << 4))
  202. ->writeUInt16BE($itemCount);
  203. for ($i = 0; $i < $itemCount; $i++) {
  204. $writer->writeUInt16BE($this->_itemId[$i]['itemId'])
  205. ->writeUInt16BE($this->_itemId[$i]['dataReferenceIndex']);
  206. if ($baseOffsetSize == 4) {
  207. $writer->writeUInt32BE($this->_itemId[$i]['baseOffset']);
  208. }
  209. if ($baseOffsetSize == 8) {
  210. $writer->writeInt64BE($this->_itemId[$i]['baseOffset']);
  211. }
  212. $writer->writeUInt16BE
  213. ($extentCount = count($this->_itemId[$i]['extents']));
  214. for ($j = 0; $j < $extentCount; $j++) {
  215. if ($offsetSize == 4) {
  216. $writer->writeUInt32BE
  217. ($this->_itemId[$i]['extents'][$j]['offset']);
  218. }
  219. if ($offsetSize == 8) {
  220. $writer->writeInt64BE
  221. ($this->_itemId[$i]['extents'][$j]['offset']);
  222. }
  223. if ($offsetSize == 4) {
  224. $writer->writeUInt32BE
  225. ($this->_itemId[$i]['extents'][$j]['length']);
  226. }
  227. if ($offsetSize == 8) {
  228. $writer->writeInt64BE
  229. ($this->_itemId[$i]['extents'][$j]['length']);
  230. }
  231. }
  232. }
  233. }
  234. }