1<?php
2
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/////////////////////////////////////////////////////////////////
10//                                                             //
11// module.audio.vqf.php                                        //
12// module for analyzing VQF audio files                        //
13// dependencies: NONE                                          //
14//                                                            ///
15/////////////////////////////////////////////////////////////////
16
17if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
18	exit;
19}
20
21class getid3_vqf extends getid3_handler
22{
23	/**
24	 * @return bool
25	 */
26	public function Analyze() {
27		$info = &$this->getid3->info;
28
29		// based loosely on code from TTwinVQ by Jurgen Faul <jfaulØgmx*de>
30		// http://jfaul.de/atl  or  http://j-faul.virtualave.net/atl/atl.html
31
32		$info['fileformat']            = 'vqf';
33		$info['audio']['dataformat']   = 'vqf';
34		$info['audio']['bitrate_mode'] = 'cbr';
35		$info['audio']['lossless']     = false;
36
37		// shortcut
38		$info['vqf']['raw'] = array();
39		$thisfile_vqf               = &$info['vqf'];
40		$thisfile_vqf_raw           = &$thisfile_vqf['raw'];
41
42		$this->fseek($info['avdataoffset']);
43		$VQFheaderData = $this->fread(16);
44
45		$offset = 0;
46		$thisfile_vqf_raw['header_tag'] = substr($VQFheaderData, $offset, 4);
47		$magic = 'TWIN';
48		if ($thisfile_vqf_raw['header_tag'] != $magic) {
49			$this->error('Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_vqf_raw['header_tag']).'"');
50			unset($info['vqf']);
51			unset($info['fileformat']);
52			return false;
53		}
54		$offset += 4;
55		$thisfile_vqf_raw['version'] =                           substr($VQFheaderData, $offset, 8);
56		$offset += 8;
57		$thisfile_vqf_raw['size']    = getid3_lib::BigEndian2Int(substr($VQFheaderData, $offset, 4));
58		$offset += 4;
59
60		while ($this->ftell() < $info['avdataend']) {
61
62			$ChunkBaseOffset = $this->ftell();
63			$chunkoffset = 0;
64			$ChunkData = $this->fread(8);
65			$ChunkName = substr($ChunkData, $chunkoffset, 4);
66			if ($ChunkName == 'DATA') {
67				$info['avdataoffset'] = $ChunkBaseOffset;
68				break;
69			}
70			$chunkoffset += 4;
71			$ChunkSize = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
72			$chunkoffset += 4;
73			if ($ChunkSize > ($info['avdataend'] - $this->ftell())) {
74				$this->error('Invalid chunk size ('.$ChunkSize.') for chunk "'.$ChunkName.'" at offset '.$ChunkBaseOffset);
75				break;
76			}
77			if ($ChunkSize > 0) {
78				$ChunkData .= $this->fread($ChunkSize);
79			}
80
81			switch ($ChunkName) {
82				case 'COMM':
83					// shortcut
84					$thisfile_vqf['COMM'] = array();
85					$thisfile_vqf_COMM    = &$thisfile_vqf['COMM'];
86
87					$thisfile_vqf_COMM['channel_mode']   = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
88					$chunkoffset += 4;
89					$thisfile_vqf_COMM['bitrate']        = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
90					$chunkoffset += 4;
91					$thisfile_vqf_COMM['sample_rate']    = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
92					$chunkoffset += 4;
93					$thisfile_vqf_COMM['security_level'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
94					$chunkoffset += 4;
95
96					$info['audio']['channels']        = $thisfile_vqf_COMM['channel_mode'] + 1;
97					$info['audio']['sample_rate']     = $this->VQFchannelFrequencyLookup($thisfile_vqf_COMM['sample_rate']);
98					$info['audio']['bitrate']         = $thisfile_vqf_COMM['bitrate'] * 1000;
99					$info['audio']['encoder_options'] = 'CBR' . ceil($info['audio']['bitrate']/1000);
100
101					if ($info['audio']['bitrate'] == 0) {
102						$this->error('Corrupt VQF file: bitrate_audio == zero');
103						return false;
104					}
105					break;
106
107				case 'NAME':
108				case 'AUTH':
109				case '(c) ':
110				case 'FILE':
111				case 'COMT':
112				case 'ALBM':
113					$thisfile_vqf['comments'][$this->VQFcommentNiceNameLookup($ChunkName)][] = trim(substr($ChunkData, 8));
114					break;
115
116				case 'DSIZ':
117					$thisfile_vqf['DSIZ'] = getid3_lib::BigEndian2Int(substr($ChunkData, 8, 4));
118					break;
119
120				default:
121					$this->warning('Unhandled chunk type "'.$ChunkName.'" at offset '.$ChunkBaseOffset);
122					break;
123			}
124		}
125
126		$info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate'];
127
128		if (isset($thisfile_vqf['DSIZ']) && (($thisfile_vqf['DSIZ'] != ($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'))))) {
129			switch ($thisfile_vqf['DSIZ']) {
130				case 0:
131				case 1:
132					$this->warning('Invalid DSIZ value "'.$thisfile_vqf['DSIZ'].'". This is known to happen with VQF files encoded by Ahead Nero, and seems to be its way of saying this is TwinVQF v'.($thisfile_vqf['DSIZ'] + 1).'.0');
133					$info['audio']['encoder'] = 'Ahead Nero';
134					break;
135
136				default:
137					$this->warning('Probable corrupted file - should be '.$thisfile_vqf['DSIZ'].' bytes, actually '.($info['avdataend'] - $info['avdataoffset'] - strlen('DATA')));
138					break;
139			}
140		}
141
142		return true;
143	}
144
145	/**
146	 * @param int $frequencyid
147	 *
148	 * @return int
149	 */
150	public function VQFchannelFrequencyLookup($frequencyid) {
151		static $VQFchannelFrequencyLookup = array(
152			11 => 11025,
153			22 => 22050,
154			44 => 44100
155		);
156		return (isset($VQFchannelFrequencyLookup[$frequencyid]) ? $VQFchannelFrequencyLookup[$frequencyid] : $frequencyid * 1000);
157	}
158
159	/**
160	 * @param string $shortname
161	 *
162	 * @return string
163	 */
164	public function VQFcommentNiceNameLookup($shortname) {
165		static $VQFcommentNiceNameLookup = array(
166			'NAME' => 'title',
167			'AUTH' => 'artist',
168			'(c) ' => 'copyright',
169			'FILE' => 'filename',
170			'COMT' => 'comment',
171			'ALBM' => 'album'
172		);
173		return (isset($VQFcommentNiceNameLookup[$shortname]) ? $VQFcommentNiceNameLookup[$shortname] : $shortname);
174	}
175
176}
177