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 /////////////////////////////////////////////////////////////////
11 // module.audio.la.php //
12 // module for analyzing LA (LosslessAudio) audio files //
13 // dependencies: module.audio.riff.php //
15 /////////////////////////////////////////////////////////////////
17 getid3_lib
::IncludeDependency(GETID3_INCLUDEPATH
.'module.audio-video.riff.php', __FILE__
, true);
19 class getid3_la
extends getid3_handler
24 public function Analyze() {
25 $info = &$this->getid3
->info
;
28 $this->fseek($info['avdataoffset']);
29 $rawdata = $this->fread($this->getid3
->fread_buffer_size());
31 switch (substr($rawdata, $offset, 4)) {
35 $info['fileformat'] = 'la';
36 $info['audio']['dataformat'] = 'la';
37 $info['audio']['lossless'] = true;
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);
44 $info['la']['uncompressed_size'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 4));
46 if ($info['la']['uncompressed_size'] == 0) {
47 $this->error('Corrupt LA file: uncompressed_size == zero');
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.');
58 $info['la']['fmt_size'] = 24;
59 if ($info['la']['version'] >= 0.3) {
61 $info['la']['fmt_size'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 4));
62 $info['la']['header_size'] = 49 +
$info['la']['fmt_size'] - 24;
67 // version 0.2 didn't support additional data blocks
68 $info['la']['header_size'] = 41;
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.');
78 $fmt_size = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 4));
81 $info['la']['raw']['format'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 2));
84 $info['la']['channels'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 2));
86 if ($info['la']['channels'] == 0) {
87 $this->error('Corrupt LA file: channels == zero');
91 $info['la']['sample_rate'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 4));
93 if ($info['la']['sample_rate'] == 0) {
94 $this->error('Corrupt LA file: sample_rate == zero');
98 $info['la']['bytes_per_second'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 4));
100 $info['la']['bytes_per_sample'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 2));
102 $info['la']['bits_per_sample'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 2));
105 $info['la']['samples'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 4));
108 $info['la']['raw']['flags'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $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);
115 $info['la']['original_crc'] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 4));
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;
128 $info['la']['blocksize'] = 73728;
129 $info['la']['seekevery'] = 16;
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']));
136 for ($i = 0; $i < $info['la']['seekpoint_count']; $i++
) {
137 $info['la']['seekpoints'][] = getid3_lib
::LittleEndian2Int(substr($rawdata, $offset, 4));
142 if ($info['la']['version'] >= 0.3) {
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));
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'];
157 // La v0.2 didn't have FooterStart value
158 $info['la']['footerstart'] = $info['avdataend'];
162 if ($info['la']['footerstart'] < $info['avdataend']) {
163 if ($RIFFtempfilename = tempnam(GETID3_TEMP_DIR
, 'id3')) {
164 if ($RIFF_fp = fopen($RIFFtempfilename, 'w+b')) {
166 if ($info['la']['version'] == 0.2) {
167 $RIFFdata .= substr($rawdata, 12, 24);
169 $RIFFdata .= substr($rawdata, 16, 24);
171 if ($info['la']['footerstart'] < $info['avdataend']) {
172 $this->fseek($info['la']['footerstart']);
173 $RIFFdata .= $this->fread($info['avdataend'] - $info['la']['footerstart']);
175 $RIFFdata = 'RIFF'.getid3_lib
::LittleEndian2String(strlen($RIFFdata), 4, false).$RIFFdata;
176 fwrite($RIFF_fp, $RIFFdata, strlen($RIFFdata));
179 $getid3_temp = new getID3();
180 $getid3_temp->openfile($RIFFtempfilename);
181 $getid3_riff = new getid3_riff($getid3_temp);
182 $getid3_riff->Analyze();
184 if (empty($getid3_temp->info
['error'])) {
185 $info['riff'] = $getid3_temp->info
['riff'];
187 $this->warning('Error parsing RIFF portion of La file: '.implode($getid3_temp->info
['error']));
189 unset($getid3_temp, $getid3_riff);
191 unlink($RIFFtempfilename);
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;
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');
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'];
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.');
215 $this->error('Not a LA (Lossless-Audio) file');
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'];