290
|
1 /*
|
|
2 * ttadec.c
|
|
3 *
|
|
4 * Description: TTAv1 decoder library for HW players.
|
|
5 * Developed by: Alexander Djourik <sasha@iszf.irk.ru>
|
|
6 * Pavel Zhilin <pzh@iszf.irk.ru>
|
|
7 *
|
|
8 * Copyright (c) 1999-2004 Alexander Djourik. All rights reserved.
|
|
9 *
|
|
10 */
|
|
11
|
|
12 /*
|
|
13 * This library is free software; you can redistribute it and/or
|
|
14 * modify it under the terms of the GNU Lesser General Public
|
|
15 * License as published by the Free Software Foundation; either
|
|
16 * version 2.1 of the License, or (at your option) any later version.
|
|
17 *
|
|
18 * This library is distributed in the hope that it will be useful,
|
|
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 * Lesser General Public License for more details.
|
|
22 *
|
|
23 * You should have received a copy of the GNU Lesser General Public
|
|
24 * License along with this library; if not, write to the Free Software
|
|
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
26 *
|
|
27 * Please see the file COPYING in this directory for full copyright
|
|
28 * information.
|
|
29 */
|
|
30
|
|
31 #include <stdlib.h>
|
|
32 #include <stdio.h>
|
|
33 #include <string.h>
|
|
34
|
|
35 #include "ttalib.h"
|
|
36 #include "ttadec.h"
|
|
37 #include "crc32.h"
|
|
38 #include "filters.h"
|
|
39
|
|
40 /******************* static variables and structures *******************/
|
|
41
|
|
42 static unsigned char isobuffers[ISO_BUFFERS_SIZE + 4];
|
|
43 static unsigned char *iso_buffers_end = isobuffers + ISO_BUFFERS_SIZE;
|
|
44 static unsigned long pcm_buffer_size;
|
|
45
|
|
46 static decoder tta[MAX_NCH]; // decoder state
|
|
47 static long cache[MAX_NCH]; // decoder cache
|
|
48
|
|
49 tta_info *ttainfo; // currently playing file info
|
|
50
|
|
51 static unsigned long fframes; // number of frames in file
|
|
52 static unsigned long framelen; // the frame length in samples
|
|
53 static unsigned long lastlen; // the length of the last frame in samples
|
|
54 static unsigned long data_pos; // currently playing frame index
|
|
55 static unsigned long data_cur; // the playing position in frame
|
|
56
|
|
57 static long maxvalue; // output data max value
|
|
58 static unsigned long *seek_table; // the playing position table
|
|
59 static unsigned long st_state; //seek table status
|
|
60
|
|
61 static unsigned long frame_crc32;
|
|
62 static unsigned long bit_count;
|
|
63 static unsigned long bit_cache;
|
|
64 static unsigned char *bitpos;
|
|
65 static unsigned long bitrate;
|
|
66
|
|
67 void get_id3v1_tag (tta_info *ttainfo);
|
|
68 int get_id3v2_tag (tta_info *ttainfo);
|
|
69
|
|
70 /************************* bit operations ******************************/
|
|
71
|
|
72 static void init_buffer_read() {
|
|
73 frame_crc32 = 0xFFFFFFFFUL;
|
|
74 bit_count = bit_cache = 0;
|
|
75 bitpos = iso_buffers_end;
|
|
76 }
|
|
77
|
|
78 __inline void get_binary(unsigned long *value, unsigned long bits) {
|
|
79 while (bit_count < bits) {
|
|
80 if (bitpos == iso_buffers_end) {
|
|
81 long res = fread(isobuffers, 1,
|
|
82 ISO_BUFFERS_SIZE, ttainfo->HANDLE);
|
|
83 if (!res) {
|
|
84 ttainfo->STATE = READ_ERROR;
|
|
85 return;
|
|
86 }
|
|
87 bitpos = isobuffers;
|
|
88 }
|
|
89
|
|
90 UPDATE_CRC32(*bitpos, frame_crc32);
|
|
91 bit_cache |= *bitpos << bit_count;
|
|
92 bit_count += 8;
|
|
93 bitpos++;
|
|
94 }
|
|
95
|
|
96 *value = bit_cache & bit_mask[bits];
|
|
97 bit_cache >>= bits;
|
|
98 bit_count -= bits;
|
|
99 bit_cache &= bit_mask[bit_count];
|
|
100 }
|
|
101
|
|
102 __inline void get_unary(unsigned long *value) {
|
|
103 *value = 0;
|
|
104
|
|
105 while (!(bit_cache ^ bit_mask[bit_count])) {
|
|
106 if (bitpos == iso_buffers_end) {
|
|
107 long res = fread(isobuffers, 1,
|
|
108 ISO_BUFFERS_SIZE, ttainfo->HANDLE);
|
|
109 if (!res) {
|
|
110 ttainfo->STATE = READ_ERROR;
|
|
111 return;
|
|
112 }
|
|
113 bitpos = isobuffers;
|
|
114 }
|
|
115
|
|
116 *value += bit_count;
|
|
117 bit_cache = *bitpos++;
|
|
118 UPDATE_CRC32(bit_cache, frame_crc32);
|
|
119 bit_count = 8;
|
|
120 }
|
|
121
|
|
122 while (bit_cache & 1) {
|
|
123 (*value)++;
|
|
124 bit_cache >>= 1;
|
|
125 bit_count--;
|
|
126 }
|
|
127
|
|
128 bit_cache >>= 1;
|
|
129 bit_count--;
|
|
130 }
|
|
131
|
|
132 static long done_buffer_read() {
|
|
133 unsigned long crc32, rbytes, res;
|
|
134 frame_crc32 ^= 0xFFFFFFFFUL;
|
|
135
|
|
136 rbytes = iso_buffers_end - bitpos;
|
|
137 if (rbytes < sizeof(long)) {
|
|
138 memcpy(isobuffers, bitpos, 4);
|
|
139 res = fread(isobuffers + rbytes, 1,
|
|
140 ISO_BUFFERS_SIZE - rbytes, ttainfo->HANDLE);
|
|
141 if (!res) {
|
|
142 ttainfo->STATE = READ_ERROR;
|
|
143 return 0;
|
|
144 }
|
|
145 bitpos = isobuffers;
|
|
146 }
|
|
147
|
|
148 memcpy(&crc32, bitpos, 4);
|
|
149 crc32 = ENDSWAP_INT32(crc32);
|
|
150 bitpos += sizeof(long);
|
|
151 res = (crc32 != frame_crc32);
|
|
152
|
|
153 bit_cache = bit_count = 0;
|
|
154 frame_crc32 = 0xFFFFFFFFUL;
|
|
155
|
|
156 // calculate dynamic bitrate
|
|
157 if (data_pos < fframes) {
|
|
158 rbytes = seek_table[data_pos] -
|
|
159 seek_table[data_pos - 1];
|
|
160 bitrate = (rbytes << 3) / 1070;
|
|
161 }
|
|
162
|
|
163 return res;
|
|
164 }
|
|
165
|
|
166 /************************* decoder functions ****************************/
|
|
167
|
|
168 static long skip_id3v2_header (FILE *infile) {
|
|
169 struct {
|
|
170 unsigned char id[3];
|
|
171 unsigned short version;
|
|
172 unsigned char flags;
|
|
173 unsigned char size[4];
|
|
174 } __ATTRIBUTE_PACKED__ id3v2;
|
|
175 unsigned long len = 0;
|
|
176
|
|
177 // read ID3V2 header
|
|
178 if (fread (&id3v2, sizeof(id3v2), 1, infile) == 0) {
|
|
179 fclose (infile);
|
|
180 ttainfo->STATE = READ_ERROR;
|
|
181 return -1;
|
|
182 }
|
|
183
|
|
184 // skip ID3V2 header
|
|
185 if (!memcmp (id3v2.id, "ID3", 3)) {
|
|
186 if (id3v2.size[0] & 0x80) {
|
|
187 fclose (infile);
|
|
188 ttainfo->STATE = FILE_ERROR;
|
|
189 return FILE_ERROR;
|
|
190 }
|
|
191 len = (id3v2.size[0] & 0x7f);
|
|
192 len = (len << 7) | (id3v2.size[1] & 0x7f);
|
|
193 len = (len << 7) | (id3v2.size[2] & 0x7f);
|
|
194 len = (len << 7) | (id3v2.size[3] & 0x7f);
|
|
195 len += 10;
|
|
196 if (id3v2.flags & (1 << 4)) len += 10;
|
|
197 fseek (infile, len, SEEK_SET);
|
|
198 } else fseek (infile, 0, SEEK_SET);
|
|
199
|
|
200 return len;
|
|
201 }
|
|
202
|
|
203 long open_tta_file (const char *filename, tta_info *info, unsigned long data_offset) {
|
|
204 FILE *infile;
|
|
205 tta_hdr ttahdr;
|
|
206 unsigned long checksum;
|
|
207
|
|
208 // clear the memory
|
|
209 memset (info, 0, sizeof(tta_info));
|
|
210
|
|
211 // printf("0: open_tta_file\n");
|
|
212 info->HANDLE = infile = fopen(filename, "rb");
|
|
213 if (!infile) return OPEN_ERROR;
|
|
214
|
|
215 // printf("1: data_offset %ld\n", data_offset);
|
|
216 // read id3v2 header
|
|
217 if (!data_offset) {
|
|
218 // data_offset = skip_id3v2_header(infile);
|
|
219 // data_offset = get_id3v2_tag(info);
|
|
220 data_offset = skip_v2_header(info);
|
|
221 // printf("2: data_offset %ld\n", data_offset);
|
|
222 // get_id3v1_tag (info);
|
|
223 if (data_offset < 0) return -1;
|
|
224 } else fseek (infile, data_offset, SEEK_SET);
|
|
225
|
|
226 get_id3_tags (filename, info);
|
|
227
|
|
228 // read TTA header
|
|
229 if (fread (&ttahdr, 1, sizeof (ttahdr), infile) == 0) {
|
|
230 fclose (infile);
|
|
231 info->STATE = READ_ERROR;
|
|
232 return -1;
|
|
233 }
|
|
234
|
|
235 // check for TTA3 signature
|
|
236 if (ENDSWAP_INT32(ttahdr.TTAid) != TTA1_SIGN) {
|
|
237 fclose (infile);
|
|
238 info->STATE = FORMAT_ERROR;
|
|
239 return -1;
|
|
240 }
|
|
241
|
|
242 ttahdr.CRC32 = ENDSWAP_INT32(ttahdr.CRC32);
|
|
243 checksum = crc32((unsigned char *) &ttahdr,
|
|
244 sizeof(tta_hdr) - sizeof(long));
|
|
245 if (checksum != ttahdr.CRC32) {
|
|
246 fclose (infile);
|
|
247 info->STATE = FILE_ERROR;
|
|
248 return -1;
|
|
249 }
|
|
250
|
|
251 ttahdr.AudioFormat = ENDSWAP_INT16(ttahdr.AudioFormat);
|
|
252 ttahdr.NumChannels = ENDSWAP_INT16(ttahdr.NumChannels);
|
|
253 ttahdr.BitsPerSample = ENDSWAP_INT16(ttahdr.BitsPerSample);
|
|
254 ttahdr.SampleRate = ENDSWAP_INT32(ttahdr.SampleRate);
|
|
255 ttahdr.DataLength = ENDSWAP_INT32(ttahdr.DataLength);
|
|
256
|
|
257 // check for player supported formats
|
|
258 if (ttahdr.AudioFormat != WAVE_FORMAT_PCM ||
|
|
259 ttahdr.NumChannels > MAX_NCH ||
|
|
260 ttahdr.BitsPerSample > MAX_BPS ||(
|
|
261 ttahdr.SampleRate != 16000 &&
|
|
262 ttahdr.SampleRate != 22050 &&
|
|
263 ttahdr.SampleRate != 24000 &&
|
|
264 ttahdr.SampleRate != 32000 &&
|
|
265 ttahdr.SampleRate != 44100 &&
|
|
266 ttahdr.SampleRate != 48000 &&
|
|
267 ttahdr.SampleRate != 64000 &&
|
|
268 ttahdr.SampleRate != 88200 &&
|
|
269 ttahdr.SampleRate != 96000)) {
|
|
270 fclose (infile);
|
|
271 info->STATE = FORMAT_ERROR;
|
|
272 return FORMAT_ERROR;
|
|
273 }
|
|
274
|
|
275 // fill the File Info
|
|
276 info->HANDLE = infile;
|
|
277 info->NCH = ttahdr.NumChannels;
|
|
278 info->BPS = ttahdr.BitsPerSample;
|
|
279 info->BSIZE = (ttahdr.BitsPerSample + 7)/8;
|
|
280 info->FORMAT = ttahdr.AudioFormat;
|
|
281 info->SAMPLERATE = ttahdr.SampleRate;
|
|
282 info->DATALENGTH = ttahdr.DataLength;
|
|
283 info->FRAMELEN = (long) (FRAME_TIME * ttahdr.SampleRate);
|
|
284 info->LENGTH = ttahdr.DataLength / ttahdr.SampleRate;
|
|
285 info->DATAPOS = data_offset;
|
|
286
|
|
287
|
|
288 return 0;
|
|
289 }
|
|
290
|
|
291 static void rice_init(adapt *rice, unsigned long k0, unsigned long k1) {
|
|
292 rice->k0 = k0;
|
|
293 rice->k1 = k1;
|
|
294 rice->sum0 = shift_16[k0];
|
|
295 rice->sum1 = shift_16[k1];
|
|
296 }
|
|
297
|
|
298 static void decoder_init(decoder *tta, long nch, long byte_size) {
|
|
299 long shift = flt_set[byte_size - 1];
|
|
300 long i;
|
|
301
|
|
302 for (i = 0; i < nch; i++) {
|
|
303 filter_init(&tta[i].fst, shift);
|
|
304 rice_init(&tta[i].rice, 10, 10);
|
|
305 tta[i].last = 0;
|
|
306 }
|
|
307 }
|
|
308
|
|
309 static void seek_table_init (unsigned long *seek_table,
|
|
310 unsigned long len, unsigned long data_offset) {
|
|
311 unsigned long *st, frame_len;
|
|
312
|
|
313 for (st = seek_table; st < (seek_table + len); st++) {
|
|
314 frame_len = ENDSWAP_INT32(*st);
|
|
315 *st = data_offset;
|
|
316 data_offset += frame_len;
|
|
317 }
|
|
318 }
|
|
319
|
|
320 long set_position (unsigned long pos) {
|
|
321 unsigned long seek_pos;
|
|
322
|
|
323 if (pos >= fframes) return 0;
|
|
324 if (!st_state) {
|
|
325 ttainfo->STATE = FILE_ERROR;
|
|
326 return -1;
|
|
327 }
|
|
328
|
|
329 seek_pos = ttainfo->DATAPOS + seek_table[data_pos = pos];
|
|
330 fseek(ttainfo->HANDLE, seek_pos, SEEK_SET);
|
|
331
|
|
332 data_cur = 0;
|
|
333 framelen = 0;
|
|
334
|
|
335 // init bit reader
|
|
336 init_buffer_read();
|
|
337
|
|
338 return 0;
|
|
339 }
|
|
340
|
|
341 long player_init (tta_info *info) {
|
|
342 unsigned long checksum;
|
|
343 unsigned long data_offset;
|
|
344 unsigned long st_size;
|
|
345
|
|
346 ttainfo = info;
|
|
347
|
|
348 framelen = 0;
|
|
349 data_pos = 0;
|
|
350 data_cur = 0;
|
|
351 bitrate = 0;
|
|
352
|
|
353 lastlen = ttainfo->DATALENGTH % ttainfo->FRAMELEN;
|
|
354 fframes = ttainfo->DATALENGTH / ttainfo->FRAMELEN + (lastlen ? 1:0);
|
|
355 st_size = (fframes + 1) * sizeof(long);
|
|
356
|
|
357 seek_table = (unsigned long *) malloc(st_size);
|
|
358 if (!seek_table) {
|
|
359 ttainfo->STATE = MEMORY_ERROR;
|
|
360 return -1;
|
|
361 }
|
|
362
|
|
363 // read seek table
|
|
364 if (!fread(seek_table, st_size, 1, ttainfo->HANDLE)) {
|
|
365 ttainfo->STATE = READ_ERROR;
|
|
366 return -1;
|
|
367 }
|
|
368
|
|
369 checksum = crc32((unsigned char *) seek_table, st_size - sizeof(long));
|
|
370 st_state = (checksum == ENDSWAP_INT32(seek_table[fframes]));
|
|
371 data_offset = sizeof(tta_hdr) + st_size;
|
|
372
|
|
373 // init seek table
|
|
374 seek_table_init(seek_table, fframes, data_offset);
|
|
375
|
|
376 // init bit reader
|
|
377 init_buffer_read();
|
|
378
|
|
379 pcm_buffer_size = PCM_BUFFER_LENGTH * ttainfo->BSIZE * ttainfo->NCH;
|
|
380 maxvalue = (1UL << ttainfo->BPS) - 1;
|
|
381
|
|
382 return 0;
|
|
383 }
|
|
384
|
|
385 void close_tta_file (tta_info *info) {
|
|
386 if (info->HANDLE) {
|
|
387 fclose (info->HANDLE);
|
|
388 info->HANDLE = NULL;
|
|
389 }
|
|
390 }
|
|
391
|
|
392 void player_stop () {
|
|
393 if (seek_table) {
|
|
394 free(seek_table);
|
|
395 seek_table = NULL;
|
|
396 }
|
|
397 }
|
|
398
|
|
399 long get_bitrate () {
|
|
400 return bitrate;
|
|
401 }
|
|
402
|
|
403 long get_samples (byte *buffer) {
|
|
404 unsigned long k, depth, unary, binary;
|
|
405 byte *p = buffer;
|
|
406 decoder *dec = tta;
|
|
407 long *prev = cache;
|
|
408 long value, res;
|
|
409
|
|
410 for (res = 0; p < buffer + pcm_buffer_size;) {
|
|
411 fltst *fst = &dec->fst;
|
|
412 adapt *rice = &dec->rice;
|
|
413 long *last = &dec->last;
|
|
414
|
|
415 if (data_cur == framelen) {
|
|
416 if (data_pos == fframes) break;
|
|
417 if (framelen && done_buffer_read()) {
|
|
418 if (set_position(data_pos) < 0)
|
|
419 return -1;
|
|
420 if (res) break;
|
|
421 }
|
|
422
|
|
423 if (data_pos == fframes - 1 && lastlen)
|
|
424 framelen = lastlen;
|
|
425 else framelen = ttainfo->FRAMELEN;
|
|
426
|
|
427 decoder_init(tta, ttainfo->NCH, ttainfo->BSIZE);
|
|
428 data_pos++; data_cur = 0;
|
|
429 }
|
|
430
|
|
431 // decode Rice unsigned
|
|
432 get_unary(&unary);
|
|
433
|
|
434 switch (unary) {
|
|
435 case 0: depth = 0; k = rice->k0; break;
|
|
436 default:
|
|
437 depth = 1; k = rice->k1;
|
|
438 unary--;
|
|
439 }
|
|
440
|
|
441 if (k) {
|
|
442 get_binary(&binary, k);
|
|
443 value = (unary << k) + binary;
|
|
444 } else value = unary;
|
|
445
|
|
446 switch (depth) {
|
|
447 case 1:
|
|
448 rice->sum1 += value - (rice->sum1 >> 4);
|
|
449 if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
|
|
450 rice->k1--;
|
|
451 else if (rice->sum1 > shift_16[rice->k1 + 1])
|
|
452 rice->k1++;
|
|
453 value += bit_shift[rice->k0];
|
|
454 default:
|
|
455 rice->sum0 += value - (rice->sum0 >> 4);
|
|
456 if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
|
|
457 rice->k0--;
|
|
458 else if (rice->sum0 > shift_16[rice->k0 + 1])
|
|
459 rice->k0++;
|
|
460 }
|
|
461
|
|
462 value = DEC(value);
|
|
463
|
|
464 // decompress stage 1: adaptive hybrid filter
|
|
465 hybrid_filter(fst, &value);
|
|
466
|
|
467 // decompress stage 2: fixed order 1 prediction
|
|
468 switch (ttainfo->BSIZE) {
|
|
469 case 1: value += PREDICTOR1(*last, 4); break; // bps 8
|
|
470 case 2: value += PREDICTOR1(*last, 5); break; // bps 16
|
|
471 case 3: value += PREDICTOR1(*last, 5); break; // bps 24
|
|
472 case 4: value += *last; break; // bps 32
|
|
473 } *last = value;
|
|
474
|
|
475 // check for errors
|
|
476 if (abs(value) > maxvalue) {
|
|
477 unsigned long tail =
|
|
478 pcm_buffer_size / (ttainfo->BSIZE * ttainfo->NCH) - res;
|
|
479 memset(buffer, 0, pcm_buffer_size);
|
|
480 data_cur += tail; res += tail;
|
|
481 break;
|
|
482 }
|
|
483
|
|
484 if (dec < tta + (ttainfo->NCH - 1)) {
|
|
485 *prev++ = value; dec++;
|
|
486 } else {
|
|
487 *prev = value;
|
|
488 if (ttainfo->NCH > 1) {
|
|
489 long *r = prev - 1;
|
|
490 for (*prev += *r/2; r >= cache; r--)
|
|
491 *r = *(r + 1) - *r;
|
|
492 for (r = cache; r < prev; r++)
|
|
493 WRITE_BUFFER(r, ttainfo->BSIZE, p)
|
|
494 }
|
|
495 WRITE_BUFFER(prev, ttainfo->BSIZE, p)
|
|
496 prev = cache;
|
|
497 data_cur++; res++;
|
|
498 dec = tta;
|
|
499 }
|
|
500 }
|
|
501
|
|
502 return res;
|
|
503 }
|
|
504
|
|
505 /* end */
|
|
506
|