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