[SPIP] v3.2.1-->v3.2.3
[lhc/web/www.git] / www / plugins-dist / medias / lib / getid3 / module.audio.la.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 LA (LosslessAudio) audio files //
13 // dependencies: module.audio.riff.php //
14 // ///
15 /////////////////////////////////////////////////////////////////
16
17 getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);
18
19 class getid3_la extends getid3_handler
20 {
21 /**
22 * @return bool
23 */
24 public function Analyze() {
25 $info = &$this->getid3->info;
26
27 $offset = 0;
28 $this->fseek($info['avdataoffset']);
29 $rawdata = $this->fread($this->getid3->fread_buffer_size());
30
31 switch (substr($rawdata, $offset, 4)) {
32 case 'LA02':
33 case 'LA03':
34 case 'LA04':
35 $info['fileformat'] = 'la';
36 $info['audio']['dataformat'] = 'la';
37 $info['audio']['lossless'] = true;
38
39 $info['la']['version_major'] = (int) substr($rawdata, $offset + 2, 1);
40 $info['la']['version_minor'] = (int) substr($rawdata, $offset + 3, 1);
41 $info['la']['version'] = (float) $info['la']['version_major'] + ($info['la']['version_minor'] / 10);
42 $offset += 4;
43
44 $info['la']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
45 $offset += 4;
46 if ($info['la']['uncompressed_size'] == 0) {
47 $this->error('Corrupt LA file: uncompressed_size == zero');
48 return false;
49 }
50
51 $WAVEchunk = substr($rawdata, $offset, 4);
52 if ($WAVEchunk !== 'WAVE') {
53 $this->error('Expected "WAVE" ('.getid3_lib::PrintHexBytes('WAVE').') at offset '.$offset.', found "'.$WAVEchunk.'" ('.getid3_lib::PrintHexBytes($WAVEchunk).') instead.');
54 return false;
55 }
56 $offset += 4;
57
58 $info['la']['fmt_size'] = 24;
59 if ($info['la']['version'] >= 0.3) {
60
61 $info['la']['fmt_size'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
62 $info['la']['header_size'] = 49 + $info['la']['fmt_size'] - 24;
63 $offset += 4;
64
65 } else {
66
67 // version 0.2 didn't support additional data blocks
68 $info['la']['header_size'] = 41;
69
70 }
71
72 $fmt_chunk = substr($rawdata, $offset, 4);
73 if ($fmt_chunk !== 'fmt ') {
74 $this->error('Expected "fmt " ('.getid3_lib::PrintHexBytes('fmt ').') at offset '.$offset.', found "'.$fmt_chunk.'" ('.getid3_lib::PrintHexBytes($fmt_chunk).') instead.');
75 return false;
76 }
77 $offset += 4;
78 $fmt_size = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
79 $offset += 4;
80
81 $info['la']['raw']['format'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
82 $offset += 2;
83
84 $info['la']['channels'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
85 $offset += 2;
86 if ($info['la']['channels'] == 0) {
87 $this->error('Corrupt LA file: channels == zero');
88 return false;
89 }
90
91 $info['la']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
92 $offset += 4;
93 if ($info['la']['sample_rate'] == 0) {
94 $this->error('Corrupt LA file: sample_rate == zero');
95 return false;
96 }
97
98 $info['la']['bytes_per_second'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
99 $offset += 4;
100 $info['la']['bytes_per_sample'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
101 $offset += 2;
102 $info['la']['bits_per_sample'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
103 $offset += 2;
104
105 $info['la']['samples'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
106 $offset += 4;
107
108 $info['la']['raw']['flags'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 1));
109 $offset += 1;
110 $info['la']['flags']['seekable'] = (bool) ($info['la']['raw']['flags'] & 0x01);
111 if ($info['la']['version'] >= 0.4) {
112 $info['la']['flags']['high_compression'] = (bool) ($info['la']['raw']['flags'] & 0x02);
113 }
114
115 $info['la']['original_crc'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
116 $offset += 4;
117
118 // mikeƘbevin*de
119 // Basically, the blocksize/seekevery are 61440/19 in La0.4 and 73728/16
120 // in earlier versions. A seekpoint is added every blocksize * seekevery
121 // samples, so 4 * int(totalSamples / (blockSize * seekEvery)) should
122 // give the number of bytes used for the seekpoints. Of course, if seeking
123 // is disabled, there are no seekpoints stored.
124 if ($info['la']['version'] >= 0.4) {
125 $info['la']['blocksize'] = 61440;
126 $info['la']['seekevery'] = 19;
127 } else {
128 $info['la']['blocksize'] = 73728;
129 $info['la']['seekevery'] = 16;
130 }
131
132 $info['la']['seekpoint_count'] = 0;
133 if ($info['la']['flags']['seekable']) {
134 $info['la']['seekpoint_count'] = floor($info['la']['samples'] / ($info['la']['blocksize'] * $info['la']['seekevery']));
135
136 for ($i = 0; $i < $info['la']['seekpoint_count']; $i++) {
137 $info['la']['seekpoints'][] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
138 $offset += 4;
139 }
140 }
141
142 if ($info['la']['version'] >= 0.3) {
143
144 // Following the main header information, the program outputs all of the
145 // seekpoints. Following these is what I called the 'footer start',
146 // i.e. the position immediately after the La audio data is finished.
147 $info['la']['footerstart'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
148 $offset += 4;
149
150 if ($info['la']['footerstart'] > $info['filesize']) {
151 $this->warning('FooterStart value points to offset '.$info['la']['footerstart'].' which is beyond end-of-file ('.$info['filesize'].')');
152 $info['la']['footerstart'] = $info['filesize'];
153 }
154
155 } else {
156
157 // La v0.2 didn't have FooterStart value
158 $info['la']['footerstart'] = $info['avdataend'];
159
160 }
161
162 if ($info['la']['footerstart'] < $info['avdataend']) {
163 if ($RIFFtempfilename = tempnam(GETID3_TEMP_DIR, 'id3')) {
164 if ($RIFF_fp = fopen($RIFFtempfilename, 'w+b')) {
165 $RIFFdata = 'WAVE';
166 if ($info['la']['version'] == 0.2) {
167 $RIFFdata .= substr($rawdata, 12, 24);
168 } else {
169 $RIFFdata .= substr($rawdata, 16, 24);
170 }
171 if ($info['la']['footerstart'] < $info['avdataend']) {
172 $this->fseek($info['la']['footerstart']);
173 $RIFFdata .= $this->fread($info['avdataend'] - $info['la']['footerstart']);
174 }
175 $RIFFdata = 'RIFF'.getid3_lib::LittleEndian2String(strlen($RIFFdata), 4, false).$RIFFdata;
176 fwrite($RIFF_fp, $RIFFdata, strlen($RIFFdata));
177 fclose($RIFF_fp);
178
179 $getid3_temp = new getID3();
180 $getid3_temp->openfile($RIFFtempfilename);
181 $getid3_riff = new getid3_riff($getid3_temp);
182 $getid3_riff->Analyze();
183
184 if (empty($getid3_temp->info['error'])) {
185 $info['riff'] = $getid3_temp->info['riff'];
186 } else {
187 $this->warning('Error parsing RIFF portion of La file: '.implode($getid3_temp->info['error']));
188 }
189 unset($getid3_temp, $getid3_riff);
190 }
191 unlink($RIFFtempfilename);
192 }
193 }
194
195 // $info['avdataoffset'] should be zero to begin with, but just in case it's not, include the addition anyway
196 $info['avdataend'] = $info['avdataoffset'] + $info['la']['footerstart'];
197 $info['avdataoffset'] = $info['avdataoffset'] + $offset;
198
199 $info['la']['compression_ratio'] = (float) (($info['avdataend'] - $info['avdataoffset']) / $info['la']['uncompressed_size']);
200 $info['playtime_seconds'] = (float) ($info['la']['samples'] / $info['la']['sample_rate']) / $info['la']['channels'];
201 if ($info['playtime_seconds'] == 0) {
202 $this->error('Corrupt LA file: playtime_seconds == zero');
203 return false;
204 }
205
206 $info['audio']['bitrate'] = ($info['avdataend'] - $info['avdataoffset']) * 8 / $info['playtime_seconds'];
207 //$info['audio']['codec'] = $info['la']['codec'];
208 $info['audio']['bits_per_sample'] = $info['la']['bits_per_sample'];
209 break;
210
211 default:
212 if (substr($rawdata, $offset, 2) == 'LA') {
213 $this->error('This version of getID3() ['.$this->getid3->version().'] does not support LA version '.substr($rawdata, $offset + 2, 1).'.'.substr($rawdata, $offset + 3, 1).' which this appears to be - check http://getid3.sourceforge.net for updates.');
214 } else {
215 $this->error('Not a LA (Lossless-Audio) file');
216 }
217 return false;
218 break;
219 }
220
221 $info['audio']['channels'] = $info['la']['channels'];
222 $info['audio']['sample_rate'] = (int) $info['la']['sample_rate'];
223 $info['audio']['encoder'] = 'LA v'.$info['la']['version'];
224
225 return true;
226 }
227
228 }