[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.audio.bonk.php
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.la.php //
12 // module for analyzing BONK audio files //
13 // dependencies: module.tag.id3v2.php (optional) //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17
18 class getid3_bonk extends getid3_handler
19 {
20 /**
21 * @return bool
22 */
23 public function Analyze() {
24 $info = &$this->getid3->info;
25
26 // shortcut
27 $info['bonk'] = array();
28 $thisfile_bonk = &$info['bonk'];
29
30 $thisfile_bonk['dataoffset'] = $info['avdataoffset'];
31 $thisfile_bonk['dataend'] = $info['avdataend'];
32
33 if (!getid3_lib::intValueSupported($thisfile_bonk['dataend'])) {
34
35 $this->warning('Unable to parse BONK file from end (v0.6+ preferred method) because PHP filesystem functions only support up to '.round(PHP_INT_MAX / 1073741824).'GB');
36
37 } else {
38
39 // scan-from-end method, for v0.6 and higher
40 $this->fseek($thisfile_bonk['dataend'] - 8);
41 $PossibleBonkTag = $this->fread(8);
42 while ($this->BonkIsValidTagName(substr($PossibleBonkTag, 4, 4), true)) {
43 $BonkTagSize = getid3_lib::LittleEndian2Int(substr($PossibleBonkTag, 0, 4));
44 $this->fseek(0 - $BonkTagSize, SEEK_CUR);
45 $BonkTagOffset = $this->ftell();
46 $TagHeaderTest = $this->fread(5);
47 if (($TagHeaderTest{0} != "\x00") || (substr($PossibleBonkTag, 4, 4) != strtolower(substr($PossibleBonkTag, 4, 4)))) {
48 $this->error('Expecting "'.getid3_lib::PrintHexBytes("\x00".strtoupper(substr($PossibleBonkTag, 4, 4))).'" at offset '.$BonkTagOffset.', found "'.getid3_lib::PrintHexBytes($TagHeaderTest).'"');
49 return false;
50 }
51 $BonkTagName = substr($TagHeaderTest, 1, 4);
52
53 $thisfile_bonk[$BonkTagName]['size'] = $BonkTagSize;
54 $thisfile_bonk[$BonkTagName]['offset'] = $BonkTagOffset;
55 $this->HandleBonkTags($BonkTagName);
56 $NextTagEndOffset = $BonkTagOffset - 8;
57 if ($NextTagEndOffset < $thisfile_bonk['dataoffset']) {
58 if (empty($info['audio']['encoder'])) {
59 $info['audio']['encoder'] = 'Extended BONK v0.9+';
60 }
61 return true;
62 }
63 $this->fseek($NextTagEndOffset);
64 $PossibleBonkTag = $this->fread(8);
65 }
66
67 }
68
69 // seek-from-beginning method for v0.4 and v0.5
70 if (empty($thisfile_bonk['BONK'])) {
71 $this->fseek($thisfile_bonk['dataoffset']);
72 do {
73 $TagHeaderTest = $this->fread(5);
74 switch ($TagHeaderTest) {
75 case "\x00".'BONK':
76 if (empty($info['audio']['encoder'])) {
77 $info['audio']['encoder'] = 'BONK v0.4';
78 }
79 break;
80
81 case "\x00".'INFO':
82 $info['audio']['encoder'] = 'Extended BONK v0.5';
83 break;
84
85 default:
86 break 2;
87 }
88 $BonkTagName = substr($TagHeaderTest, 1, 4);
89 $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
90 $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
91 $this->HandleBonkTags($BonkTagName);
92
93 } while (true);
94 }
95
96 // parse META block for v0.6 - v0.8
97 if (empty($thisfile_bonk['INFO']) && isset($thisfile_bonk['META']['tags']['info'])) {
98 $this->fseek($thisfile_bonk['META']['tags']['info']);
99 $TagHeaderTest = $this->fread(5);
100 if ($TagHeaderTest == "\x00".'INFO') {
101 $info['audio']['encoder'] = 'Extended BONK v0.6 - v0.8';
102
103 $BonkTagName = substr($TagHeaderTest, 1, 4);
104 $thisfile_bonk[$BonkTagName]['size'] = $thisfile_bonk['dataend'] - $thisfile_bonk['dataoffset'];
105 $thisfile_bonk[$BonkTagName]['offset'] = $thisfile_bonk['dataoffset'];
106 $this->HandleBonkTags($BonkTagName);
107 }
108 }
109
110 if (empty($info['audio']['encoder'])) {
111 $info['audio']['encoder'] = 'Extended BONK v0.9+';
112 }
113 if (empty($thisfile_bonk['BONK'])) {
114 unset($info['bonk']);
115 }
116 return true;
117
118 }
119
120 /**
121 * @param string $BonkTagName
122 */
123 public function HandleBonkTags($BonkTagName) {
124 $info = &$this->getid3->info;
125 switch ($BonkTagName) {
126 case 'BONK':
127 // shortcut
128 $thisfile_bonk_BONK = &$info['bonk']['BONK'];
129
130 $BonkData = "\x00".'BONK'.$this->fread(17);
131 $thisfile_bonk_BONK['version'] = getid3_lib::LittleEndian2Int(substr($BonkData, 5, 1));
132 $thisfile_bonk_BONK['number_samples'] = getid3_lib::LittleEndian2Int(substr($BonkData, 6, 4));
133 $thisfile_bonk_BONK['sample_rate'] = getid3_lib::LittleEndian2Int(substr($BonkData, 10, 4));
134
135 $thisfile_bonk_BONK['channels'] = getid3_lib::LittleEndian2Int(substr($BonkData, 14, 1));
136 $thisfile_bonk_BONK['lossless'] = (bool) getid3_lib::LittleEndian2Int(substr($BonkData, 15, 1));
137 $thisfile_bonk_BONK['joint_stereo'] = (bool) getid3_lib::LittleEndian2Int(substr($BonkData, 16, 1));
138 $thisfile_bonk_BONK['number_taps'] = getid3_lib::LittleEndian2Int(substr($BonkData, 17, 2));
139 $thisfile_bonk_BONK['downsampling_ratio'] = getid3_lib::LittleEndian2Int(substr($BonkData, 19, 1));
140 $thisfile_bonk_BONK['samples_per_packet'] = getid3_lib::LittleEndian2Int(substr($BonkData, 20, 2));
141
142 $info['avdataoffset'] = $thisfile_bonk_BONK['offset'] + 5 + 17;
143 $info['avdataend'] = $thisfile_bonk_BONK['offset'] + $thisfile_bonk_BONK['size'];
144
145 $info['fileformat'] = 'bonk';
146 $info['audio']['dataformat'] = 'bonk';
147 $info['audio']['bitrate_mode'] = 'vbr'; // assumed
148 $info['audio']['channels'] = $thisfile_bonk_BONK['channels'];
149 $info['audio']['sample_rate'] = $thisfile_bonk_BONK['sample_rate'];
150 $info['audio']['channelmode'] = ($thisfile_bonk_BONK['joint_stereo'] ? 'joint stereo' : 'stereo');
151 $info['audio']['lossless'] = $thisfile_bonk_BONK['lossless'];
152 $info['audio']['codec'] = 'bonk';
153
154 $info['playtime_seconds'] = $thisfile_bonk_BONK['number_samples'] / ($thisfile_bonk_BONK['sample_rate'] * $thisfile_bonk_BONK['channels']);
155 if ($info['playtime_seconds'] > 0) {
156 $info['audio']['bitrate'] = (($info['bonk']['dataend'] - $info['bonk']['dataoffset']) * 8) / $info['playtime_seconds'];
157 }
158 break;
159
160 case 'INFO':
161 // shortcut
162 $thisfile_bonk_INFO = &$info['bonk']['INFO'];
163
164 $thisfile_bonk_INFO['version'] = getid3_lib::LittleEndian2Int($this->fread(1));
165 $thisfile_bonk_INFO['entries_count'] = 0;
166 $NextInfoDataPair = $this->fread(5);
167 if (!$this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
168 while (!feof($this->getid3->fp)) {
169 //$CurrentSeekInfo['offset'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 0, 4));
170 //$CurrentSeekInfo['nextbit'] = getid3_lib::LittleEndian2Int(substr($NextInfoDataPair, 4, 1));
171 //$thisfile_bonk_INFO[] = $CurrentSeekInfo;
172
173 $NextInfoDataPair = $this->fread(5);
174 if ($this->BonkIsValidTagName(substr($NextInfoDataPair, 1, 4))) {
175 $this->fseek(-5, SEEK_CUR);
176 break;
177 }
178 $thisfile_bonk_INFO['entries_count']++;
179 }
180 }
181 break;
182
183 case 'META':
184 $BonkData = "\x00".'META'.$this->fread($info['bonk']['META']['size'] - 5);
185 $info['bonk']['META']['version'] = getid3_lib::LittleEndian2Int(substr($BonkData, 5, 1));
186
187 $MetaTagEntries = floor(((strlen($BonkData) - 8) - 6) / 8); // BonkData - xxxxmeta - ØMETA
188 $offset = 6;
189 for ($i = 0; $i < $MetaTagEntries; $i++) {
190 $MetaEntryTagName = substr($BonkData, $offset, 4);
191 $offset += 4;
192 $MetaEntryTagOffset = getid3_lib::LittleEndian2Int(substr($BonkData, $offset, 4));
193 $offset += 4;
194 $info['bonk']['META']['tags'][$MetaEntryTagName] = $MetaEntryTagOffset;
195 }
196 break;
197
198 case ' ID3':
199 $info['audio']['encoder'] = 'Extended BONK v0.9+';
200
201 // ID3v2 checking is optional
202 if (class_exists('getid3_id3v2')) {
203 $getid3_temp = new getID3();
204 $getid3_temp->openfile($this->getid3->filename);
205 $getid3_id3v2 = new getid3_id3v2($getid3_temp);
206 $getid3_id3v2->StartingOffset = $info['bonk'][' ID3']['offset'] + 2;
207 $info['bonk'][' ID3']['valid'] = $getid3_id3v2->Analyze();
208 if ($info['bonk'][' ID3']['valid']) {
209 $info['id3v2'] = $getid3_temp->info['id3v2'];
210 }
211 unset($getid3_temp, $getid3_id3v2);
212 }
213 break;
214
215 default:
216 $this->warning('Unexpected Bonk tag "'.$BonkTagName.'" at offset '.$info['bonk'][$BonkTagName]['offset']);
217 break;
218
219 }
220 }
221
222 /**
223 * @param string $PossibleBonkTag
224 * @param bool $ignorecase
225 *
226 * @return bool
227 */
228 public static function BonkIsValidTagName($PossibleBonkTag, $ignorecase=false) {
229 static $BonkIsValidTagName = array('BONK', 'INFO', ' ID3', 'META');
230 foreach ($BonkIsValidTagName as $validtagname) {
231 if ($validtagname == $PossibleBonkTag) {
232 return true;
233 } elseif ($ignorecase && (strtolower($validtagname) == strtolower($PossibleBonkTag))) {
234 return true;
235 }
236 }
237 return false;
238 }
239
240 }