ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Nsv.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
10 // available at http://getid3.sourceforge.net //
11 // or http://www.getid3.org //
13 // See readme.txt for more details //
15 // //
16 // module.audio.nsv.php //
17 // module for analyzing Nullsoft NSV files //
18 // dependencies: NONE //
19 // ///
21 
29 class Nsv extends BaseHandler
30 {
31 
36  public function analyze()
37  {
38  $info = &$this->getid3->info;
39 
40  fseek($this->getid3->fp, $info['avdataoffset'], SEEK_SET);
41  $NSVheader = fread($this->getid3->fp, 4);
42 
43  switch ($NSVheader) {
44  case 'NSVs':
45  if ($this->getNSVsHeaderFilepointer(0)) {
46  $info['fileformat'] = 'nsv';
47  $info['audio']['dataformat'] = 'nsv';
48  $info['video']['dataformat'] = 'nsv';
49  $info['audio']['lossless'] = false;
50  $info['video']['lossless'] = false;
51  }
52  break;
53 
54  case 'NSVf':
55  if ($this->getNSVfHeaderFilepointer(0)) {
56  $info['fileformat'] = 'nsv';
57  $info['audio']['dataformat'] = 'nsv';
58  $info['video']['dataformat'] = 'nsv';
59  $info['audio']['lossless'] = false;
60  $info['video']['lossless'] = false;
61  $this->getNSVsHeaderFilepointer($info['nsv']['NSVf']['header_length']);
62  }
63  break;
64 
65  default:
66  $info['error'][] = 'Expecting "NSVs" or "NSVf" at offset '.$info['avdataoffset'].', found "'.Helper::PrintHexBytes($NSVheader).'"';
67 
68  return false;
69  break;
70  }
71 
72  if (!isset($info['nsv']['NSVf'])) {
73  $info['warning'][] = 'NSVf header not present - cannot calculate playtime or bitrate';
74  }
75 
76  return true;
77  }
78 
84  public function getNSVsHeaderFilepointer($fileoffset)
85  {
86  $info = &$this->getid3->info;
87  fseek($this->getid3->fp, $fileoffset, SEEK_SET);
88  $NSVsheader = fread($this->getid3->fp, 28);
89  $offset = 0;
90 
91  $info['nsv']['NSVs']['identifier'] = substr($NSVsheader, $offset, 4);
92  $offset += 4;
93 
94  if ($info['nsv']['NSVs']['identifier'] != 'NSVs') {
95  $info['error'][] = 'expected "NSVs" at offset ('.$fileoffset.'), found "'.$info['nsv']['NSVs']['identifier'].'" instead';
96  unset($info['nsv']['NSVs']);
97 
98  return false;
99  }
100 
101  $info['nsv']['NSVs']['offset'] = $fileoffset;
102 
103  $info['nsv']['NSVs']['video_codec'] = substr($NSVsheader, $offset, 4);
104  $offset += 4;
105  $info['nsv']['NSVs']['audio_codec'] = substr($NSVsheader, $offset, 4);
106  $offset += 4;
107  $info['nsv']['NSVs']['resolution_x'] = Helper::LittleEndian2Int(substr($NSVsheader, $offset, 2));
108  $offset += 2;
109  $info['nsv']['NSVs']['resolution_y'] = Helper::LittleEndian2Int(substr($NSVsheader, $offset, 2));
110  $offset += 2;
111 
112  $info['nsv']['NSVs']['framerate_index'] = Helper::LittleEndian2Int(substr($NSVsheader, $offset, 1));
113  $offset += 1;
114  //$info['nsv']['NSVs']['unknown1b'] = GetId3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
115  $offset += 1;
116  //$info['nsv']['NSVs']['unknown1c'] = GetId3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
117  $offset += 1;
118  //$info['nsv']['NSVs']['unknown1d'] = GetId3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
119  $offset += 1;
120  //$info['nsv']['NSVs']['unknown2a'] = GetId3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
121  $offset += 1;
122  //$info['nsv']['NSVs']['unknown2b'] = GetId3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
123  $offset += 1;
124  //$info['nsv']['NSVs']['unknown2c'] = GetId3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
125  $offset += 1;
126  //$info['nsv']['NSVs']['unknown2d'] = GetId3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
127  $offset += 1;
128 
129  switch ($info['nsv']['NSVs']['audio_codec']) {
130  case 'PCM ':
131  $info['nsv']['NSVs']['bits_channel'] = Helper::LittleEndian2Int(substr($NSVsheader, $offset, 1));
132  $offset += 1;
133  $info['nsv']['NSVs']['channels'] = Helper::LittleEndian2Int(substr($NSVsheader, $offset, 1));
134  $offset += 1;
135  $info['nsv']['NSVs']['sample_rate'] = Helper::LittleEndian2Int(substr($NSVsheader, $offset, 2));
136  $offset += 2;
137 
138  $info['audio']['sample_rate'] = $info['nsv']['NSVs']['sample_rate'];
139  break;
140 
141  case 'MP3 ':
142  case 'NONE':
143  default:
144  //$info['nsv']['NSVs']['unknown3'] = GetId3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 4));
145  $offset += 4;
146  break;
147  }
148 
149  $info['video']['resolution_x'] = $info['nsv']['NSVs']['resolution_x'];
150  $info['video']['resolution_y'] = $info['nsv']['NSVs']['resolution_y'];
151  $info['nsv']['NSVs']['frame_rate'] = $this->NSVframerateLookup($info['nsv']['NSVs']['framerate_index']);
152  $info['video']['frame_rate'] = $info['nsv']['NSVs']['frame_rate'];
153  $info['video']['bits_per_sample'] = 24;
154  $info['video']['pixel_aspect_ratio'] = (float) 1;
155 
156  return true;
157  }
158 
165  public function getNSVfHeaderFilepointer($fileoffset, $getTOCoffsets=false)
166  {
167  $info = &$this->getid3->info;
168  fseek($this->getid3->fp, $fileoffset, SEEK_SET);
169  $NSVfheader = fread($this->getid3->fp, 28);
170  $offset = 0;
171 
172  $info['nsv']['NSVf']['identifier'] = substr($NSVfheader, $offset, 4);
173  $offset += 4;
174 
175  if ($info['nsv']['NSVf']['identifier'] != 'NSVf') {
176  $info['error'][] = 'expected "NSVf" at offset ('.$fileoffset.'), found "'.$info['nsv']['NSVf']['identifier'].'" instead';
177  unset($info['nsv']['NSVf']);
178 
179  return false;
180  }
181 
182  $info['nsv']['NSVs']['offset'] = $fileoffset;
183 
184  $info['nsv']['NSVf']['header_length'] = Helper::LittleEndian2Int(substr($NSVfheader, $offset, 4));
185  $offset += 4;
186  $info['nsv']['NSVf']['file_size'] = Helper::LittleEndian2Int(substr($NSVfheader, $offset, 4));
187  $offset += 4;
188 
189  if ($info['nsv']['NSVf']['file_size'] > $info['avdataend']) {
190  $info['warning'][] = 'truncated file - NSVf header indicates '.$info['nsv']['NSVf']['file_size'].' bytes, file actually '.$info['avdataend'].' bytes';
191  }
192 
193  $info['nsv']['NSVf']['playtime_ms'] = Helper::LittleEndian2Int(substr($NSVfheader, $offset, 4));
194  $offset += 4;
195  $info['nsv']['NSVf']['meta_size'] = Helper::LittleEndian2Int(substr($NSVfheader, $offset, 4));
196  $offset += 4;
197  $info['nsv']['NSVf']['TOC_entries_1'] = Helper::LittleEndian2Int(substr($NSVfheader, $offset, 4));
198  $offset += 4;
199  $info['nsv']['NSVf']['TOC_entries_2'] = Helper::LittleEndian2Int(substr($NSVfheader, $offset, 4));
200  $offset += 4;
201 
202  if ($info['nsv']['NSVf']['playtime_ms'] == 0) {
203  $info['error'][] = 'Corrupt NSV file: NSVf.playtime_ms == zero';
204 
205  return false;
206  }
207 
208  $NSVfheader .= fread($this->getid3->fp, $info['nsv']['NSVf']['meta_size'] + (4 * $info['nsv']['NSVf']['TOC_entries_1']) + (4 * $info['nsv']['NSVf']['TOC_entries_2']));
209  $NSVfheaderlength = strlen($NSVfheader);
210  $info['nsv']['NSVf']['metadata'] = substr($NSVfheader, $offset, $info['nsv']['NSVf']['meta_size']);
211  $offset += $info['nsv']['NSVf']['meta_size'];
212 
213  if ($getTOCoffsets) {
214  $TOCcounter = 0;
215  while ($TOCcounter < $info['nsv']['NSVf']['TOC_entries_1']) {
216  if ($TOCcounter < $info['nsv']['NSVf']['TOC_entries_1']) {
217  $info['nsv']['NSVf']['TOC_1'][$TOCcounter] = Helper::LittleEndian2Int(substr($NSVfheader, $offset, 4));
218  $offset += 4;
219  $TOCcounter++;
220  }
221  }
222  }
223 
224  if (trim($info['nsv']['NSVf']['metadata']) != '') {
225  $info['nsv']['NSVf']['metadata'] = str_replace('`', "\x01", $info['nsv']['NSVf']['metadata']);
226  $CommentPairArray = explode("\x01".' ', $info['nsv']['NSVf']['metadata']);
227  foreach ($CommentPairArray as $CommentPair) {
228  if (strstr($CommentPair, '='."\x01")) {
229  list($key, $value) = explode('='."\x01", $CommentPair, 2);
230  $info['nsv']['comments'][strtolower($key)][] = trim(str_replace("\x01", '', $value));
231  }
232  }
233  }
234 
235  $info['playtime_seconds'] = $info['nsv']['NSVf']['playtime_ms'] / 1000;
236  $info['bitrate'] = ($info['nsv']['NSVf']['file_size'] * 8) / $info['playtime_seconds'];
237 
238  return true;
239  }
240 
247  public static function NSVframerateLookup($framerateindex)
248  {
249  if ($framerateindex <= 127) {
250  return (float) $framerateindex;
251  }
252  static $NSVframerateLookup = array();
253  if (empty($NSVframerateLookup)) {
254  $NSVframerateLookup[129] = (float) 29.970;
255  $NSVframerateLookup[131] = (float) 23.976;
256  $NSVframerateLookup[133] = (float) 14.985;
257  $NSVframerateLookup[197] = (float) 59.940;
258  $NSVframerateLookup[199] = (float) 47.952;
259  }
260 
261  return (isset($NSVframerateLookup[$framerateindex]) ? $NSVframerateLookup[$framerateindex] : false);
262  }
263 }
static PrintHexBytes($string, $hex=true, $spaces=true, $htmlencoding='UTF-8')
Definition: Helper.php:36
GetId3() by James Heinrich info@getid3.org //.
Definition: BaseHandler.php:25
fseek($bytes, $whence=SEEK_SET)
$info
Definition: example_052.php:80
getNSVsHeaderFilepointer($fileoffset)
Definition: Nsv.php:84
static NSVframerateLookup($framerateindex)
array $NSVframerateLookup
Definition: Nsv.php:247
Create styles array
The data for the language used.
GetId3() by James Heinrich info@getid3.org //.
Definition: Nsv.php:29
static LittleEndian2Int($byteword, $signed=false)
Definition: Helper.php:413
getNSVfHeaderFilepointer($fileoffset, $getTOCoffsets=false)
Definition: Nsv.php:165