eab601b779cb8f1099fdfccbba3a749e23f97696
2 /////////////////////////////////////////////////////////////////
3 /// getID3() by James Heinrich <info@getid3.org> //
4 // available at http://getid3.sourceforge.net //
5 // or http://www.getid3.org //
6 // also https://github.com/JamesHeinrich/getID3 //
7 /////////////////////////////////////////////////////////////////
8 // See readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.audio.nsv.php //
12 // module for analyzing Nullsoft NSV files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
18 class getid3_nsv
extends getid3_handler
21 public function Analyze() {
22 $info = &$this->getid3
->info
;
24 $this->fseek($info['avdataoffset']);
25 $NSVheader = $this->fread(4);
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;
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']);
50 $this->error('Expecting "NSVs" or "NSVf" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($NSVheader).'"');
55 if (!isset($info['nsv']['NSVf'])) {
56 $this->warning('NSVf header not present - cannot calculate playtime or bitrate');
62 public function getNSVsHeaderFilepointer($fileoffset) {
63 $info = &$this->getid3
->info
;
64 $this->fseek($fileoffset);
65 $NSVsheader = $this->fread(28);
68 $info['nsv']['NSVs']['identifier'] = substr($NSVsheader, $offset, 4);
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']);
77 $info['nsv']['NSVs']['offset'] = $fileoffset;
79 $info['nsv']['NSVs']['video_codec'] = substr($NSVsheader, $offset, 4);
81 $info['nsv']['NSVs']['audio_codec'] = substr($NSVsheader, $offset, 4);
83 $info['nsv']['NSVs']['resolution_x'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 2));
85 $info['nsv']['NSVs']['resolution_y'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 2));
88 $info['nsv']['NSVs']['framerate_index'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 1));
90 //$info['nsv']['NSVs']['unknown1b'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
92 //$info['nsv']['NSVs']['unknown1c'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
94 //$info['nsv']['NSVs']['unknown1d'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
96 //$info['nsv']['NSVs']['unknown2a'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
98 //$info['nsv']['NSVs']['unknown2b'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
100 //$info['nsv']['NSVs']['unknown2c'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
102 //$info['nsv']['NSVs']['unknown2d'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
105 switch ($info['nsv']['NSVs']['audio_codec']) {
107 $info['nsv']['NSVs']['bits_channel'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 1));
109 $info['nsv']['NSVs']['channels'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 1));
111 $info['nsv']['NSVs']['sample_rate'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 2));
114 $info['audio']['sample_rate'] = $info['nsv']['NSVs']['sample_rate'];
120 //$info['nsv']['NSVs']['unknown3'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 4));
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;
135 public function getNSVfHeaderFilepointer($fileoffset, $getTOCoffsets=false) {
136 $info = &$this->getid3
->info
;
137 $this->fseek($fileoffset);
138 $NSVfheader = $this->fread(28);
141 $info['nsv']['NSVf']['identifier'] = substr($NSVfheader, $offset, 4);
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']);
150 $info['nsv']['NSVs']['offset'] = $fileoffset;
152 $info['nsv']['NSVf']['header_length'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
154 $info['nsv']['NSVf']['file_size'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
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');
161 $info['nsv']['NSVf']['playtime_ms'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
163 $info['nsv']['NSVf']['meta_size'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
165 $info['nsv']['NSVf']['TOC_entries_1'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
167 $info['nsv']['NSVf']['TOC_entries_2'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
170 if ($info['nsv']['NSVf']['playtime_ms'] == 0) {
171 $this->error('Corrupt NSV file: NSVf.playtime_ms == zero');
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'];
180 if ($getTOCoffsets) {
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));
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));
202 $info['playtime_seconds'] = $info['nsv']['NSVf']['playtime_ms'] / 1000;
203 $info['bitrate'] = ($info['nsv']['NSVf']['file_size'] * 8) / $info['playtime_seconds'];
209 public static function NSVframerateLookup($framerateindex) {
210 if ($framerateindex <= 127) {
211 return (float) $framerateindex;
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;
221 return (isset($NSVframerateLookup[$framerateindex]) ?
$NSVframerateLookup[$framerateindex] : false);