aboutsummaryrefslogtreecommitdiff
path: root/libraries/phpvideotoolkit/adapters/ffmpeg-php/php-reader/src/ISO14496/Box/ILST.php
blob: 9b4a2096ba4e7be4f1b9e011bca9bf045ddd87f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/**
 * PHP Reader Library
 *
 * Copyright (c) 2008 The PHP Reader Project Workgroup. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the project workgroup nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * @package    php-reader
 * @subpackage ISO 14496
 * @copyright  Copyright (c) 2008 The PHP Reader Project Workgroup
 * @license    http://code.google.com/p/php-reader/wiki/License New BSD License
 * @version    $Id: ILST.php 101 2008-05-13 20:28:13Z svollbehr $
 */

/**#@+ @ignore */
require_once("ISO14496/Box.php");
/**#@-*/

/**
 * A container box for all the iTunes/iPod specific boxes. A list of well known
 * boxes is provided in the following table. The value for each box is contained
 * in a nested {@link ISO14496_Box_DATA Data Box}.
 * 
 * <ul>
 * <li><b>_nam</b> -- <i>Name of the track</i></li>
 * <li><b>_ART</b> -- <i>Name of the artist</i></li>
 * <li><b>aART</b> -- <i>Name of the album artist</i></li>
 * <li><b>_alb</b> -- <i>Name of the album</i></li>
 * <li><b>_grp</b> -- <i>Grouping</i></li>
 * <li><b>_day</b> -- <i>Year of publication</i></li>
 * <li><b>trkn</b> -- <i>Track number (number/total)</i></li>
 * <li><b>disk</b> -- <i>Disk number (number/total)</i></li>
 * <li><b>tmpo</b> -- <i>BPM tempo</i></li>
 * <li><b>_wrt</b> -- <i>Name of the composer</i></li>
 * <li><b>_cmt</b> -- <i>Comments</i></li>
 * <li><b>_gen</b> -- <i>Genre as string</i></li>
 * <li><b>gnre</b> -- <i>Genre as an ID3v1 code, added by one</i></li>
 * <li><b>cpil</b> -- <i>Part of a compilation (0/1)</i></li>
 * <li><b>tvsh</b> -- <i>Name of the (television) show</i></li>
 * <li><b>sonm</b> -- <i>Sort name of the track</i></li>
 * <li><b>soar</b> -- <i>Sort name of the artist</i></li>
 * <li><b>soaa</b> -- <i>Sort name of the album artist</i></li>
 * <li><b>soal</b> -- <i>Sort name of the album</i></li>
 * <li><b>soco</b> -- <i>Sort name of the composer</i></li>
 * <li><b>sosn</b> -- <i>Sort name of the show</i></li>
 * <li><b>_lyr</b> -- <i>Lyrics</i></li>
 * <li><b>covr</b> -- <i>Cover (or other) artwork binary data</i></li>
 * <li><b>_too</b> -- <i>Information about the software</i></li>
 * </ul>
 *
 * @package    php-reader
 * @subpackage ISO 14496
 * @author     Sven Vollbehr <svollbehr@gmail.com>
 * @copyright  Copyright (c) 2008 The PHP Reader Project Workgroup
 * @license    http://code.google.com/p/php-reader/wiki/License New BSD License
 * @version    $Rev: 101 $
 * @since      iTunes/iPod specific
 */
final class ISO14496_Box_ILST extends ISO14496_Box
{
  /**
   * Constructs the class with given parameters and reads box related data from
   * the ISO Base Media file.
   *
   * @param Reader $reader The reader object.
   */
  public function __construct($reader = null, &$options = array())
  {
    parent::__construct($reader, $options);
    $this->setContainer(true);
    
    if ($reader === null)
      return;
    
    $this->constructBoxes("ISO14496_Box_ILST_Container");
  }
  
  /**
   * Override magic function so that $obj->value on a box will return the data
   * box instead of the data container box.
   *
   * @param string $name The box or field name.
   * @return mixed
   */
  public function __get($name)
  {
    if (strlen($name) == 3)
      $name = "\xa9" . $name;
    if ($name[0] == "_")
      $name = "\xa9" . substr($name, 1, 3);
    if ($this->hasBox($name)) {
      $boxes = $this->getBoxesByIdentifier($name);
      return $boxes[0]->data;
    }
    if (method_exists($this, "get" . ucfirst($name)))
      return call_user_func(array($this, "get" . ucfirst($name)));
    return $this->addBox(new ISO14496_Box_ILST_Container($name))->data;
  }
}

/**
 * Generic iTunes/iPod DATA Box container.
 *
 * @package    php-reader
 * @subpackage ISO 14496
 * @author     Sven Vollbehr <svollbehr@gmail.com>
 * @copyright  Copyright (c) 2008 The PHP Reader Project Workgroup
 * @license    http://code.google.com/p/php-reader/wiki/License New BSD License
 * @version    $Rev: 101 $
 * @since      iTunes/iPod specific
 * @ignore
 */
final class ISO14496_Box_ILST_Container extends ISO14496_Box
{
  public function __construct($reader = null, &$options = array())
  {
    parent::__construct(is_string($reader) ? null : $reader, $options);
    $this->setContainer(true);
    
    if (is_string($reader)) {
      $this->setType($reader);
      $this->addBox(new ISO14496_Box_DATA());
    } else
      $this->constructBoxes();
  }
}

/**#@+ @ignore */
require_once("ISO14496/Box/Full.php");
/**#@-*/

/**
 * A box that contains data for iTunes/iPod specific boxes.
 *
 * @package    php-reader
 * @subpackage ISO 14496
 * @author     Sven Vollbehr <svollbehr@gmail.com>
 * @copyright  Copyright (c) 2008 The PHP Reader Project Workgroup
 * @license    http://code.google.com/p/php-reader/wiki/License New BSD License
 * @version    $Rev: 101 $
 * @since      iTunes/iPod specific
 */
final class ISO14496_Box_DATA extends ISO14496_Box_Full
{
  /** @var mixed */
  private $_value;
  
  /** A flag to indicate that the data is an unsigned 8-bit integer. */
  const INTEGER = 0x0;
  
  /**
   * A flag to indicate that the data is an unsigned 8-bit integer. Different
   * value used in old versions of iTunes.
   */
  const INTEGER_OLD_STYLE = 0x15;
  
  /** A flag to indicate that the data is a string. */
  const STRING = 0x1;
  
  /** A flag to indicate that the data is the contents of an JPEG image. */
  const JPEG = 0xd;
  
  /** A flag to indicate that the data is the contents of a PNG image. */
  const PNG = 0xe;
  
  /**
   * Constructs the class with given parameters and reads box related data from
   * the ISO Base Media file.
   *
   * @param Reader $reader The reader object.
   */
  public function __construct($reader = null, &$options = array())
  {
    parent::__construct($reader, $options);
    
    if ($reader === null)
      return;
    
    $this->_reader->skip(4);
    $data = $this->_reader->read
      ($this->getOffset() + $this->getSize() - $this->_reader->getOffset());
    switch ($this->getFlags()) {
    case self::INTEGER:
    case self::INTEGER_OLD_STYLE:
      for ($i = 0;  $i < strlen($data); $i++)
        $this->_value .= Transform::fromInt8($data[$i]);
      break;
    case self::STRING:
    default:
      $this->_value = $data;
    }
  }
  
  /**
   * Returns the value this box contains.
   * 
   * @return mixed
   */
  public function getValue() { return $this->_value; }
  
  /**
   * Sets the value this box contains.
   * 
   * @return mixed
   */
  public function setValue($value, $type = false)
  {
    $this->_value = (string)$value;
    if ($type === false && is_string($value))
      $this->_flags = self::STRING;
    if ($type === false && is_int($value))
      $this->_flags = self::INTEGER;
    if ($type !== false)
      $this->_flags = $type;
  }
  
  /**
   * Override magic function so that $obj->data will return the current box
   * instead of an error. For other values the method will attempt to call a
   * getter method.
   *
   * If there are no getter methods with given name, the method will yield an
   * exception.
   *
   * @param string $name The box or field name.
   * @return mixed
   */
  public function __get($name)
  {
    if ($name == "data")
      return $this;
    if (method_exists($this, "get" . ucfirst($name)))
      return call_user_func(array($this, "get" . ucfirst($name)));
    throw new ISO14496_Exception("Unknown box/field: " . $name);
  }
  
  /**
   * Returns the box raw data.
   *
   * @return string
   */
  public function __toString($data = "")
  {
    switch ($this->getFlags()) {
    case self::INTEGER:
    case self::INTEGER_OLD_STYLE:
      $data = "";
      for ($i = 0;  $i < strlen($this->_value); $i++)
        $data .= Transform::toInt8($this->_value[$i]);
      break;
    case self::STRING:
    default:
      $data = $this->_value;
    }
    return parent::__toString("\0\0\0\0" . $data);
  }
}