3 /////////////////////////////////////////////////////////////////
4 /// getID3() by James Heinrich <info@getid3.org> //
5 // available at https://github.com/JamesHeinrich/getID3 //
6 // or https://www.getid3.org //
7 // or http://getid3.sourceforge.net //
8 // see readme.txt for more details //
9 /////////////////////////////////////////////////////////////////
11 // module.audio.nsv.php //
12 // module for analyzing Nullsoft NSV files //
13 // dependencies: NONE //
15 /////////////////////////////////////////////////////////////////
17 if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
21 class getid3_nsv
extends getid3_handler
26 public function Analyze() {
27 $info = &$this->getid3
->info
;
29 $this->fseek($info['avdataoffset']);
30 $NSVheader = $this->fread(4);
34 if ($this->getNSVsHeaderFilepointer(0)) {
35 $info['fileformat'] = 'nsv';
36 $info['audio']['dataformat'] = 'nsv';
37 $info['video']['dataformat'] = 'nsv';
38 $info['audio']['lossless'] = false;
39 $info['video']['lossless'] = false;
44 if ($this->getNSVfHeaderFilepointer(0)) {
45 $info['fileformat'] = 'nsv';
46 $info['audio']['dataformat'] = 'nsv';
47 $info['video']['dataformat'] = 'nsv';
48 $info['audio']['lossless'] = false;
49 $info['video']['lossless'] = false;
50 $this->getNSVsHeaderFilepointer($info['nsv']['NSVf']['header_length']);
55 $this->error('Expecting "NSVs" or "NSVf" at offset '.$info['avdataoffset'].', found "'.getid3_lib
::PrintHexBytes($NSVheader).'"');
59 if (!isset($info['nsv']['NSVf'])) {
60 $this->warning('NSVf header not present - cannot calculate playtime or bitrate');
67 * @param int $fileoffset
71 public function getNSVsHeaderFilepointer($fileoffset) {
72 $info = &$this->getid3
->info
;
73 $this->fseek($fileoffset);
74 $NSVsheader = $this->fread(28);
77 $info['nsv']['NSVs']['identifier'] = substr($NSVsheader, $offset, 4);
80 if ($info['nsv']['NSVs']['identifier'] != 'NSVs') {
81 $this->error('expected "NSVs" at offset ('.$fileoffset.'), found "'.$info['nsv']['NSVs']['identifier'].'" instead');
82 unset($info['nsv']['NSVs']);
86 $info['nsv']['NSVs']['offset'] = $fileoffset;
88 $info['nsv']['NSVs']['video_codec'] = substr($NSVsheader, $offset, 4);
90 $info['nsv']['NSVs']['audio_codec'] = substr($NSVsheader, $offset, 4);
92 $info['nsv']['NSVs']['resolution_x'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 2));
94 $info['nsv']['NSVs']['resolution_y'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 2));
97 $info['nsv']['NSVs']['framerate_index'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 1));
99 //$info['nsv']['NSVs']['unknown1b'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
101 //$info['nsv']['NSVs']['unknown1c'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
103 //$info['nsv']['NSVs']['unknown1d'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
105 //$info['nsv']['NSVs']['unknown2a'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
107 //$info['nsv']['NSVs']['unknown2b'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
109 //$info['nsv']['NSVs']['unknown2c'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
111 //$info['nsv']['NSVs']['unknown2d'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 1));
114 switch ($info['nsv']['NSVs']['audio_codec']) {
116 $info['nsv']['NSVs']['bits_channel'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 1));
118 $info['nsv']['NSVs']['channels'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 1));
120 $info['nsv']['NSVs']['sample_rate'] = getid3_lib
::LittleEndian2Int(substr($NSVsheader, $offset, 2));
123 $info['audio']['sample_rate'] = $info['nsv']['NSVs']['sample_rate'];
129 //$info['nsv']['NSVs']['unknown3'] = getid3_lib::LittleEndian2Int(substr($NSVsheader, $offset, 4));
134 $info['video']['resolution_x'] = $info['nsv']['NSVs']['resolution_x'];
135 $info['video']['resolution_y'] = $info['nsv']['NSVs']['resolution_y'];
136 $info['nsv']['NSVs']['frame_rate'] = $this->NSVframerateLookup($info['nsv']['NSVs']['framerate_index']);
137 $info['video']['frame_rate'] = $info['nsv']['NSVs']['frame_rate'];
138 $info['video']['bits_per_sample'] = 24;
139 $info['video']['pixel_aspect_ratio'] = (float) 1;
145 * @param int $fileoffset
146 * @param bool $getTOCoffsets
150 public function getNSVfHeaderFilepointer($fileoffset, $getTOCoffsets=false) {
151 $info = &$this->getid3
->info
;
152 $this->fseek($fileoffset);
153 $NSVfheader = $this->fread(28);
156 $info['nsv']['NSVf']['identifier'] = substr($NSVfheader, $offset, 4);
159 if ($info['nsv']['NSVf']['identifier'] != 'NSVf') {
160 $this->error('expected "NSVf" at offset ('.$fileoffset.'), found "'.$info['nsv']['NSVf']['identifier'].'" instead');
161 unset($info['nsv']['NSVf']);
165 $info['nsv']['NSVs']['offset'] = $fileoffset;
167 $info['nsv']['NSVf']['header_length'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
169 $info['nsv']['NSVf']['file_size'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
172 if ($info['nsv']['NSVf']['file_size'] > $info['avdataend']) {
173 $this->warning('truncated file - NSVf header indicates '.$info['nsv']['NSVf']['file_size'].' bytes, file actually '.$info['avdataend'].' bytes');
176 $info['nsv']['NSVf']['playtime_ms'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
178 $info['nsv']['NSVf']['meta_size'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
180 $info['nsv']['NSVf']['TOC_entries_1'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
182 $info['nsv']['NSVf']['TOC_entries_2'] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
185 if ($info['nsv']['NSVf']['playtime_ms'] == 0) {
186 $this->error('Corrupt NSV file: NSVf.playtime_ms == zero');
190 $NSVfheader .= $this->fread($info['nsv']['NSVf']['meta_size'] +
(4 * $info['nsv']['NSVf']['TOC_entries_1']) +
(4 * $info['nsv']['NSVf']['TOC_entries_2']));
191 $NSVfheaderlength = strlen($NSVfheader);
192 $info['nsv']['NSVf']['metadata'] = substr($NSVfheader, $offset, $info['nsv']['NSVf']['meta_size']);
193 $offset +
= $info['nsv']['NSVf']['meta_size'];
195 if ($getTOCoffsets) {
197 while ($TOCcounter < $info['nsv']['NSVf']['TOC_entries_1']) {
198 if ($TOCcounter < $info['nsv']['NSVf']['TOC_entries_1']) {
199 $info['nsv']['NSVf']['TOC_1'][$TOCcounter] = getid3_lib
::LittleEndian2Int(substr($NSVfheader, $offset, 4));
206 if (trim($info['nsv']['NSVf']['metadata']) != '') {
207 $info['nsv']['NSVf']['metadata'] = str_replace('`', "\x01", $info['nsv']['NSVf']['metadata']);
208 $CommentPairArray = explode("\x01".' ', $info['nsv']['NSVf']['metadata']);
209 foreach ($CommentPairArray as $CommentPair) {
210 if (strstr($CommentPair, '='."\x01")) {
211 list($key, $value) = explode('='."\x01", $CommentPair, 2);
212 $info['nsv']['comments'][strtolower($key)][] = trim(str_replace("\x01", '', $value));
217 $info['playtime_seconds'] = $info['nsv']['NSVf']['playtime_ms'] / 1000;
218 $info['bitrate'] = ($info['nsv']['NSVf']['file_size'] * 8) / $info['playtime_seconds'];
224 * @param int $framerateindex
226 * @return float|false
228 public static function NSVframerateLookup($framerateindex) {
229 if ($framerateindex <= 127) {
230 return (float) $framerateindex;
232 static $NSVframerateLookup = array();
233 if (empty($NSVframerateLookup)) {
234 $NSVframerateLookup[129] = 29.970;
235 $NSVframerateLookup[131] = 23.976;
236 $NSVframerateLookup[133] = 14.985;
237 $NSVframerateLookup[197] = 59.940;
238 $NSVframerateLookup[199] = 47.952;
240 return (isset($NSVframerateLookup[$framerateindex]) ?
$NSVframerateLookup[$framerateindex] : false);