Blame view

sources/3rdparty/james-heinrich/getid3/getid3/module.audio.vqf.php 5.88 KB
03e52840d   Kload   Init
1
2
3
4
5
  <?php
  /////////////////////////////////////////////////////////////////
  /// getID3() by James Heinrich <info@getid3.org>               //
  //  available at http://getid3.sourceforge.net                 //
  //            or http://www.getid3.org                         //
6d9380f96   Cédric Dupont   Update sources OC...
6
  //          also https://github.com/JamesHeinrich/getID3       //
03e52840d   Kload   Init
7
8
9
10
11
12
13
14
15
16
17
18
19
  /////////////////////////////////////////////////////////////////
  // See readme.txt for more details                             //
  /////////////////////////////////////////////////////////////////
  //                                                             //
  // module.audio.vqf.php                                        //
  // module for analyzing VQF audio files                        //
  // dependencies: NONE                                          //
  //                                                            ///
  /////////////////////////////////////////////////////////////////
  
  
  class getid3_vqf extends getid3_handler
  {
31b7f2792   Kload   Upgrade to ownclo...
20
  	public function Analyze() {
03e52840d   Kload   Init
21
  		$info = &$this->getid3->info;
6d9380f96   Cédric Dupont   Update sources OC...
22
  		// based loosely on code from TTwinVQ by Jurgen Faul <jfaulØgmx*de>
03e52840d   Kload   Init
23
24
25
26
27
28
29
30
31
32
33
  		// http://jfaul.de/atl  or  http://j-faul.virtualave.net/atl/atl.html
  
  		$info['fileformat']            = 'vqf';
  		$info['audio']['dataformat']   = 'vqf';
  		$info['audio']['bitrate_mode'] = 'cbr';
  		$info['audio']['lossless']     = false;
  
  		// shortcut
  		$info['vqf']['raw'] = array();
  		$thisfile_vqf               = &$info['vqf'];
  		$thisfile_vqf_raw           = &$thisfile_vqf['raw'];
6d9380f96   Cédric Dupont   Update sources OC...
34
35
  		$this->fseek($info['avdataoffset']);
  		$VQFheaderData = $this->fread(16);
03e52840d   Kload   Init
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  
  		$offset = 0;
  		$thisfile_vqf_raw['header_tag'] = substr($VQFheaderData, $offset, 4);
  		$magic = 'TWIN';
  		if ($thisfile_vqf_raw['header_tag'] != $magic) {
  			$info['error'][] = 'Expecting "'.getid3_lib::PrintHexBytes($magic).'" at offset '.$info['avdataoffset'].', found "'.getid3_lib::PrintHexBytes($thisfile_vqf_raw['header_tag']).'"';
  			unset($info['vqf']);
  			unset($info['fileformat']);
  			return false;
  		}
  		$offset += 4;
  		$thisfile_vqf_raw['version'] =                           substr($VQFheaderData, $offset, 8);
  		$offset += 8;
  		$thisfile_vqf_raw['size']    = getid3_lib::BigEndian2Int(substr($VQFheaderData, $offset, 4));
  		$offset += 4;
6d9380f96   Cédric Dupont   Update sources OC...
51
  		while ($this->ftell() < $info['avdataend']) {
03e52840d   Kload   Init
52

6d9380f96   Cédric Dupont   Update sources OC...
53
  			$ChunkBaseOffset = $this->ftell();
03e52840d   Kload   Init
54
  			$chunkoffset = 0;
6d9380f96   Cédric Dupont   Update sources OC...
55
  			$ChunkData = $this->fread(8);
03e52840d   Kload   Init
56
57
58
59
60
61
62
63
  			$ChunkName = substr($ChunkData, $chunkoffset, 4);
  			if ($ChunkName == 'DATA') {
  				$info['avdataoffset'] = $ChunkBaseOffset;
  				break;
  			}
  			$chunkoffset += 4;
  			$ChunkSize = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  			$chunkoffset += 4;
6d9380f96   Cédric Dupont   Update sources OC...
64
  			if ($ChunkSize > ($info['avdataend'] - $this->ftell())) {
03e52840d   Kload   Init
65
66
67
68
  				$info['error'][] = 'Invalid chunk size ('.$ChunkSize.') for chunk "'.$ChunkName.'" at offset '.$ChunkBaseOffset;
  				break;
  			}
  			if ($ChunkSize > 0) {
6d9380f96   Cédric Dupont   Update sources OC...
69
  				$ChunkData .= $this->fread($ChunkSize);
03e52840d   Kload   Init
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  			}
  
  			switch ($ChunkName) {
  				case 'COMM':
  					// shortcut
  					$thisfile_vqf['COMM'] = array();
  					$thisfile_vqf_COMM    = &$thisfile_vqf['COMM'];
  
  					$thisfile_vqf_COMM['channel_mode']   = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  					$chunkoffset += 4;
  					$thisfile_vqf_COMM['bitrate']        = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  					$chunkoffset += 4;
  					$thisfile_vqf_COMM['sample_rate']    = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  					$chunkoffset += 4;
  					$thisfile_vqf_COMM['security_level'] = getid3_lib::BigEndian2Int(substr($ChunkData, $chunkoffset, 4));
  					$chunkoffset += 4;
  
  					$info['audio']['channels']        = $thisfile_vqf_COMM['channel_mode'] + 1;
  					$info['audio']['sample_rate']     = $this->VQFchannelFrequencyLookup($thisfile_vqf_COMM['sample_rate']);
  					$info['audio']['bitrate']         = $thisfile_vqf_COMM['bitrate'] * 1000;
  					$info['audio']['encoder_options'] = 'CBR' . ceil($info['audio']['bitrate']/1000);
  
  					if ($info['audio']['bitrate'] == 0) {
  						$info['error'][] = 'Corrupt VQF file: bitrate_audio == zero';
  						return false;
  					}
  					break;
  
  				case 'NAME':
  				case 'AUTH':
  				case '(c) ':
  				case 'FILE':
  				case 'COMT':
  				case 'ALBM':
  					$thisfile_vqf['comments'][$this->VQFcommentNiceNameLookup($ChunkName)][] = trim(substr($ChunkData, 8));
  					break;
  
  				case 'DSIZ':
  					$thisfile_vqf['DSIZ'] = getid3_lib::BigEndian2Int(substr($ChunkData, 8, 4));
  					break;
  
  				default:
  					$info['warning'][] = 'Unhandled chunk type "'.$ChunkName.'" at offset '.$ChunkBaseOffset;
  					break;
  			}
  		}
  
  		$info['playtime_seconds'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate'];
  
  		if (isset($thisfile_vqf['DSIZ']) && (($thisfile_vqf['DSIZ'] != ($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'))))) {
  			switch ($thisfile_vqf['DSIZ']) {
  				case 0:
  				case 1:
  					$info['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';
  					$info['audio']['encoder'] = 'Ahead Nero';
  					break;
  
  				default:
  					$info['warning'][] = 'Probable corrupted file - should be '.$thisfile_vqf['DSIZ'].' bytes, actually '.($info['avdataend'] - $info['avdataoffset'] - strlen('DATA'));
  					break;
  			}
  		}
  
  		return true;
  	}
31b7f2792   Kload   Upgrade to ownclo...
135
  	public function VQFchannelFrequencyLookup($frequencyid) {
03e52840d   Kload   Init
136
137
138
139
140
141
142
  		static $VQFchannelFrequencyLookup = array(
  			11 => 11025,
  			22 => 22050,
  			44 => 44100
  		);
  		return (isset($VQFchannelFrequencyLookup[$frequencyid]) ? $VQFchannelFrequencyLookup[$frequencyid] : $frequencyid * 1000);
  	}
31b7f2792   Kload   Upgrade to ownclo...
143
  	public function VQFcommentNiceNameLookup($shortname) {
03e52840d   Kload   Init
144
145
146
147
148
149
150
151
152
153
154
155
  		static $VQFcommentNiceNameLookup = array(
  			'NAME' => 'title',
  			'AUTH' => 'artist',
  			'(c) ' => 'copyright',
  			'FILE' => 'filename',
  			'COMT' => 'comment',
  			'ALBM' => 'album'
  		);
  		return (isset($VQFcommentNiceNameLookup[$shortname]) ? $VQFcommentNiceNameLookup[$shortname] : $shortname);
  	}
  
  }