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