summaryrefslogtreecommitdiff
path: root/getid3/module.audio.la.php
blob: c1d4f52d14feecf7e77568622d2086b0dfd3cf68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/////////////////////////////////////////////////////////////////
/// getID3() by James Heinrich <info@getid3.org>               //
//  available at http://getid3.sourceforge.net                 //
//            or http://www.getid3.org                         //
/////////////////////////////////////////////////////////////////
// See readme.txt for more details                             //
/////////////////////////////////////////////////////////////////
//                                                             //
// module.audio.la.php                                         //
// module for analyzing LA audio files                         //
// dependencies: module.audio.riff.php                         //
//                                                            ///
/////////////////////////////////////////////////////////////////

getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio-video.riff.php', __FILE__, true);

class getid3_la
{

	function getid3_la(&$fd, &$ThisFileInfo) {
		$offset = 0;
		fseek($fd, $ThisFileInfo['avdataoffset'], SEEK_SET);
		$rawdata = fread($fd, GETID3_FREAD_BUFFER_SIZE);

		switch (substr($rawdata, $offset, 4)) {
			case 'LA02':
			case 'LA03':
			case 'LA04':
				$ThisFileInfo['fileformat']          = 'la';
				$ThisFileInfo['audio']['dataformat'] = 'la';
				$ThisFileInfo['audio']['lossless']   = true;

				$ThisFileInfo['la']['version_major'] = (int) substr($rawdata, $offset + 2, 1);
				$ThisFileInfo['la']['version_minor'] = (int) substr($rawdata, $offset + 3, 1);
				$ThisFileInfo['la']['version']       = (float) $ThisFileInfo['la']['version_major'] + ($ThisFileInfo['la']['version_minor'] / 10);
				$offset += 4;

				$ThisFileInfo['la']['uncompressed_size'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
				$offset += 4;
				if ($ThisFileInfo['la']['uncompressed_size'] == 0) {
					$ThisFileInfo['error'][] = 'Corrupt LA file: uncompressed_size == zero';
					return false;
				}

				$WAVEchunk = substr($rawdata, $offset, 4);
				if ($WAVEchunk !== 'WAVE') {
					$ThisFileInfo['error'][] = 'Expected "WAVE" ('.getid3_lib::PrintHexBytes('WAVE').') at offset '.$offset.', found "'.$WAVEchunk.'" ('.getid3_lib::PrintHexBytes($WAVEchunk).') instead.';
					return false;
				}
				$offset += 4;

				$ThisFileInfo['la']['fmt_size'] = 24;
				if ($ThisFileInfo['la']['version'] >= 0.3) {

					$ThisFileInfo['la']['fmt_size']    = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
					$ThisFileInfo['la']['header_size'] = 49 + $ThisFileInfo['la']['fmt_size'] - 24;
					$offset += 4;

				} else {

					// version 0.2 didn't support additional data blocks
					$ThisFileInfo['la']['header_size'] = 41;

				}

				$fmt_chunk = substr($rawdata, $offset, 4);
				if ($fmt_chunk !== 'fmt ') {
					$ThisFileInfo['error'][] = 'Expected "fmt " ('.getid3_lib::PrintHexBytes('fmt ').') at offset '.$offset.', found "'.$fmt_chunk.'" ('.getid3_lib::PrintHexBytes($fmt_chunk).') instead.';
					return false;
				}
				$offset += 4;
				$fmt_size = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
				$offset += 4;

				$ThisFileInfo['la']['raw']['format']  = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
				$offset += 2;

				$ThisFileInfo['la']['channels']       = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
				$offset += 2;
				if ($ThisFileInfo['la']['channels'] == 0) {
					$ThisFileInfo['error'][] = 'Corrupt LA file: channels == zero';
						return false;
				}

				$ThisFileInfo['la']['sample_rate'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
				$offset += 4;
				if ($ThisFileInfo['la']['sample_rate'] == 0) {
					$ThisFileInfo['error'][] = 'Corrupt LA file: sample_rate == zero';
						return false;
				}

				$ThisFileInfo['la']['bytes_per_second']     = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
				$offset += 4;
				$ThisFileInfo['la']['bytes_per_sample']     = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
				$offset += 2;
				$ThisFileInfo['la']['bits_per_sample']      = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 2));
				$offset += 2;

				$ThisFileInfo['la']['samples']              = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
				$offset += 4;

				$ThisFileInfo['la']['raw']['flags']         = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 1));
				$offset += 1;
				$ThisFileInfo['la']['flags']['seekable']             = (bool) ($ThisFileInfo['la']['raw']['flags'] & 0x01);
				if ($ThisFileInfo['la']['version'] >= 0.4) {
					$ThisFileInfo['la']['flags']['high_compression'] = (bool) ($ThisFileInfo['la']['raw']['flags'] & 0x02);
				}

				$ThisFileInfo['la']['original_crc']         = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
				$offset += 4;

				// mikeØbevin*de
				// Basically, the blocksize/seekevery are 61440/19 in La0.4 and 73728/16
				// in earlier versions. A seekpoint is added every blocksize * seekevery
				// samples, so 4 * int(totalSamples / (blockSize * seekEvery)) should
				// give the number of bytes used for the seekpoints. Of course, if seeking
				// is disabled, there are no seekpoints stored.
				if ($ThisFileInfo['la']['version'] >= 0.4) {
					$ThisFileInfo['la']['blocksize'] = 61440;
					$ThisFileInfo['la']['seekevery'] = 19;
				} else {
					$ThisFileInfo['la']['blocksize'] = 73728;
					$ThisFileInfo['la']['seekevery'] = 16;
				}

				$ThisFileInfo['la']['seekpoint_count'] = 0;
				if ($ThisFileInfo['la']['flags']['seekable']) {
					$ThisFileInfo['la']['seekpoint_count'] = floor($ThisFileInfo['la']['samples'] / ($ThisFileInfo['la']['blocksize'] * $ThisFileInfo['la']['seekevery']));

					for ($i = 0; $i < $ThisFileInfo['la']['seekpoint_count']; $i++) {
						$ThisFileInfo['la']['seekpoints'][] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
						$offset += 4;
					}
				}

				if ($ThisFileInfo['la']['version'] >= 0.3) {

					// Following the main header information, the program outputs all of the
					// seekpoints. Following these is what I called the 'footer start',
					// i.e. the position immediately after the La audio data is finished.
					$ThisFileInfo['la']['footerstart'] = getid3_lib::LittleEndian2Int(substr($rawdata, $offset, 4));
					$offset += 4;

					if ($ThisFileInfo['la']['footerstart'] > $ThisFileInfo['filesize']) {
						$ThisFileInfo['warning'][] = 'FooterStart value points to offset '.$ThisFileInfo['la']['footerstart'].' which is beyond end-of-file ('.$ThisFileInfo['filesize'].')';
						$ThisFileInfo['la']['footerstart'] = $ThisFileInfo['filesize'];
					}

				} else {

					// La v0.2 didn't have FooterStart value
					$ThisFileInfo['la']['footerstart'] = $ThisFileInfo['avdataend'];

				}

				if ($ThisFileInfo['la']['footerstart'] < $ThisFileInfo['avdataend']) {
					if ($RIFFtempfilename = tempnam('*', 'id3')) {
						if ($RIFF_fp = fopen($RIFFtempfilename, 'w+b')) {
							$RIFFdata = 'WAVE';
							if ($ThisFileInfo['la']['version'] == 0.2) {
								$RIFFdata .= substr($rawdata, 12, 24);
							} else {
								$RIFFdata .= substr($rawdata, 16, 24);
							}
							if ($ThisFileInfo['la']['footerstart'] < $ThisFileInfo['avdataend']) {
								fseek($fd, $ThisFileInfo['la']['footerstart'], SEEK_SET);
								$RIFFdata .= fread($fd, $ThisFileInfo['avdataend'] - $ThisFileInfo['la']['footerstart']);
							}
							$RIFFdata = 'RIFF'.getid3_lib::LittleEndian2String(strlen($RIFFdata), 4, false).$RIFFdata;
							fwrite($RIFF_fp, $RIFFdata, strlen($RIFFdata));
							$dummy = $ThisFileInfo;
							$dummy['filesize']     = strlen($RIFFdata);
							$dummy['avdataoffset'] = 0;
							$dummy['avdataend']    = $dummy['filesize'];

							$riff = new getid3_riff($RIFF_fp, $dummy);
							if (empty($dummy['error'])) {
								$ThisFileInfo['riff'] = $dummy['riff'];
							} else {
								$ThisFileInfo['warning'][] = 'Error parsing RIFF portion of La file: '.implode($dummy['error']);
							}
							unset($riff);
							unset($dummy);
							fclose($RIFF_fp);
						}
						unlink($RIFFtempfilename);
					}
				}

				// $ThisFileInfo['avdataoffset'] should be zero to begin with, but just in case it's not, include the addition anyway
				$ThisFileInfo['avdataend']    = $ThisFileInfo['avdataoffset'] + $ThisFileInfo['la']['footerstart'];
				$ThisFileInfo['avdataoffset'] = $ThisFileInfo['avdataoffset'] + $offset;

				//$ThisFileInfo['la']['codec']                = RIFFwFormatTagLookup($ThisFileInfo['la']['raw']['format']);
				$ThisFileInfo['la']['compression_ratio']    = (float) (($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) / $ThisFileInfo['la']['uncompressed_size']);
				$ThisFileInfo['playtime_seconds']           = (float) ($ThisFileInfo['la']['samples'] / $ThisFileInfo['la']['sample_rate']) / $ThisFileInfo['la']['channels'];
				if ($ThisFileInfo['playtime_seconds'] == 0) {
					$ThisFileInfo['error'][] = 'Corrupt LA file: playtime_seconds == zero';
					return false;
				}

				$ThisFileInfo['audio']['bitrate']            = ($ThisFileInfo['avdataend'] - $ThisFileInfo['avdataoffset']) * 8 / $ThisFileInfo['playtime_seconds'];
				//$ThisFileInfo['audio']['codec']              = $ThisFileInfo['la']['codec'];
				$ThisFileInfo['audio']['bits_per_sample']    = $ThisFileInfo['la']['bits_per_sample'];
				break;

			default:
				if (substr($rawdata, $offset, 2) == 'LA') {
					$ThisFileInfo['error'][] = 'This version of getID3() (v'.GETID3_VERSION.') doesn\'t 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.';
				} else {
					$ThisFileInfo['error'][] = 'Not a LA (Lossless-Audio) file';
				}
				return false;
				break;
		}

		$ThisFileInfo['audio']['channels']    = $ThisFileInfo['la']['channels'];
		$ThisFileInfo['audio']['sample_rate'] = (int) $ThisFileInfo['la']['sample_rate'];
		$ThisFileInfo['audio']['encoder']     = 'LA v'.$ThisFileInfo['la']['version'];

		return true;
	}

}


?>