add True Audio (TTA) codec
[kugel-rb.git] / apps / codecs / libtta / ttadec.c
blob027b78a17ed81969449625d9744a6d981be2d959
1 /*
2 * ttadec.c
4 * Description: TTAv1 decoder library for HW players
5 * Developed by: Alexander Djourik <ald@true-audio.com>
6 * Pavel Zhilin <pzh@true-audio.com>
8 * Copyright (c) 2004 True Audio Software. All rights reserved.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the True Audio Software nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include "codeclib.h"
41 #include "ttalib.h"
42 #include "ttadec.h"
43 #include "filter.h"
45 /******************* static variables and structures *******************/
47 static unsigned char isobuffers[ISO_BUFFERS_SIZE + 4] IBSS_ATTR;
48 static unsigned char *iso_buffers_end = isobuffers + ISO_BUFFERS_SIZE;
49 static unsigned int pcm_buffer_size;
51 static decoder tta[MAX_NCH]; /* decoder state */
52 /* Rockbox speciffic: cache is defined in get_samples() (non static value) */
53 /* static int cache[MAX_NCH]; // decoder cache */
55 tta_info *ttainfo; /* currently playing file info */
57 static unsigned int fframes; /* number of frames in file */
58 static unsigned int framelen; /* the frame length in samples */
59 static unsigned int lastlen; /* the length of the last frame in samples */
60 static unsigned int data_pos; /* currently playing frame index */
61 static unsigned int data_cur; /* the playing position in frame */
63 static int maxvalue; /* output data max value */
65 /* Rockbox speciffic: seek_table is static size */
66 static unsigned int seek_table[MAX_SEEK_TABLE_SIZE]; /* the playing position table */
67 static unsigned int st_state; /* seek table status */
69 static unsigned int frame_crc32;
70 static unsigned int bit_count;
71 static unsigned int bit_cache;
72 static unsigned char *bitpos;
74 /* Rockbox speciffic: deletes read_id3_tags(). */
75 /* static int read_id3_tags (tta_info *info); */
77 /********************* rockbox helper functions *************************/
79 /* emulate stdio functions */
80 static int fread(void *ptr, size_t size, size_t nobj)
82 size_t read_size;
83 unsigned char *buffer = ci->request_buffer(&read_size, size * nobj);
85 if (read_size > 0)
87 ci->memcpy(ptr, buffer, read_size);
88 ci->advance_buffer(read_size);
90 return read_size;
93 static int fseek(long offset, int origin)
95 switch (origin)
97 case SEEK_CUR:
98 ci->advance_buffer(offset);
99 break;
100 case SEEK_SET:
101 ci->seek_buffer(offset);
102 break;
103 case SEEK_END:
104 ci->seek_buffer(offset + ci->id3->filesize);
105 break;
106 default:
107 return -1;
109 return 0;
112 /************************* crc32 functions *****************************/
114 #define UPDATE_CRC32(x, crc) crc = \
115 (((crc>>8) & 0x00FFFFFF) ^ crc32_table[(crc^x) & 0xFF])
117 static unsigned int
118 crc32 (unsigned char *buffer, unsigned int len) {
119 unsigned int i;
120 unsigned int crc = 0xFFFFFFFF;
122 for (i = 0; i < len; i++) UPDATE_CRC32(buffer[i], crc);
124 return (crc ^ 0xFFFFFFFF);
127 /************************* bit operations ******************************/
129 #define GET_BINARY(value, bits) \
130 while (bit_count < bits) { \
131 if (bitpos == iso_buffers_end) { \
132 if (!fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
133 ttainfo->STATE = READ_ERROR; \
134 return -1; \
136 bitpos = isobuffers; \
138 UPDATE_CRC32(*bitpos, frame_crc32); \
139 bit_cache |= *bitpos << bit_count; \
140 bit_count += 8; \
141 bitpos++; \
143 value = bit_cache & bit_mask[bits]; \
144 bit_cache >>= bits; \
145 bit_count -= bits; \
146 bit_cache &= bit_mask[bit_count];
148 #define GET_UNARY(value) \
149 value = 0; \
150 while (!(bit_cache ^ bit_mask[bit_count])) { \
151 if (bitpos == iso_buffers_end) { \
152 if (!fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
153 ttainfo->STATE = READ_ERROR; \
154 return -1; \
156 bitpos = isobuffers; \
158 value += bit_count; \
159 bit_cache = *bitpos++; \
160 UPDATE_CRC32(bit_cache, frame_crc32); \
161 bit_count = 8; \
163 while (bit_cache & 1) { \
164 value++; \
165 bit_cache >>= 1; \
166 bit_count--; \
168 bit_cache >>= 1; \
169 bit_count--;
171 /************************* rice operations ******************************/
173 static inline int update_rice(int value, adapt *rice, int depth,
174 const unsigned int *shift_table)
176 if (depth > 0)
178 rice->sum1 += value - (rice->sum1 >> 4);
179 if (rice->k1 > 0 && rice->sum1 < shift_table[rice->k1])
180 rice->k1--;
181 else if (rice->sum1 > shift_table[rice->k1 + 1])
182 rice->k1++;
183 value += *(shift_table + rice->k0 - 4);
185 rice->sum0 += value - (rice->sum0 >> 4);
186 if (rice->k0 > 0 && rice->sum0 < shift_table[rice->k0])
187 rice->k0--;
188 else if (rice->sum0 > shift_table[rice->k0 + 1])
189 rice->k0++;
191 return DEC(value);
194 /************************* buffer functions ******************************/
196 static void init_buffer_read(void) {
197 frame_crc32 = 0xFFFFFFFFUL;
198 bit_count = bit_cache = 0;
199 bitpos = iso_buffers_end;
202 static int done_buffer_read(void) {
203 unsigned int crc32, rbytes;
205 frame_crc32 ^= 0xFFFFFFFFUL;
206 rbytes = iso_buffers_end - bitpos;
208 if (rbytes < sizeof(int)) {
209 ci->memcpy(isobuffers, bitpos, 4);
210 if (!fread(isobuffers + rbytes, 1, ISO_BUFFERS_SIZE - rbytes))
211 return -1;
212 bitpos = isobuffers;
215 ci->memcpy(&crc32, bitpos, 4);
216 crc32 = ENDSWAP_INT32(crc32);
217 bitpos += sizeof(int);
219 if (crc32 != frame_crc32) return -1;
221 bit_cache = bit_count = 0;
222 frame_crc32 = 0xFFFFFFFFUL;
224 return 0;
227 /************************* decoder functions ****************************/
229 const char *get_error_str (int error) {
230 switch (error) {
231 case NO_ERROR: return "No errors found";
232 case OPEN_ERROR: return "Can't open file";
233 case FORMAT_ERROR: return "Not supported file format";
234 case FILE_ERROR: return "File is corrupted";
235 case READ_ERROR: return "Can't read from file";
236 case MEMORY_ERROR: return "Insufficient memory available";
237 default: return "Unknown error code";
241 int set_tta_info (tta_info *info)
243 unsigned int checksum;
244 unsigned int datasize;
245 unsigned int origsize;
246 tta_hdr ttahdr;
248 /* clear the memory */
249 ci->memset (info, 0, sizeof(tta_info));
251 /* skip id3v2 tags */
252 fseek(ci->id3->id3v2len, SEEK_SET);
254 /* read TTA header */
255 if (fread (&ttahdr, 1, sizeof (ttahdr)) == 0) {
256 info->STATE = READ_ERROR;
257 return -1;
260 /* check for TTA3 signature */
261 if (ENDSWAP_INT32(ttahdr.TTAid) != TTA1_SIGN) {
262 DEBUGF("ID error: %x\n", ENDSWAP_INT32(ttahdr.TTAid));
263 info->STATE = FORMAT_ERROR;
264 return -1;
267 ttahdr.CRC32 = ENDSWAP_INT32(ttahdr.CRC32);
268 checksum = crc32((unsigned char *) &ttahdr,
269 sizeof(tta_hdr) - sizeof(int));
270 if (checksum != ttahdr.CRC32) {
271 DEBUGF("CRC error: %x != %x\n", ttahdr.CRC32, checksum);
272 info->STATE = FILE_ERROR;
273 return -1;
276 ttahdr.AudioFormat = ENDSWAP_INT16(ttahdr.AudioFormat);
277 ttahdr.NumChannels = ENDSWAP_INT16(ttahdr.NumChannels);
278 ttahdr.BitsPerSample = ENDSWAP_INT16(ttahdr.BitsPerSample);
279 ttahdr.SampleRate = ENDSWAP_INT32(ttahdr.SampleRate);
280 ttahdr.DataLength = ENDSWAP_INT32(ttahdr.DataLength);
282 /* check for player supported formats */
283 if (ttahdr.AudioFormat != WAVE_FORMAT_PCM ||
284 ttahdr.NumChannels > MAX_NCH ||
285 ttahdr.BitsPerSample > MAX_BPS ||(
286 ttahdr.SampleRate != 16000 &&
287 ttahdr.SampleRate != 22050 &&
288 ttahdr.SampleRate != 24000 &&
289 ttahdr.SampleRate != 32000 &&
290 ttahdr.SampleRate != 44100 &&
291 ttahdr.SampleRate != 48000 &&
292 ttahdr.SampleRate != 64000 &&
293 ttahdr.SampleRate != 88200 &&
294 ttahdr.SampleRate != 96000)) {
295 info->STATE = FORMAT_ERROR;
296 DEBUGF("illegal audio format: %d channels: %d samplerate: %d\n",
297 ttahdr.AudioFormat, ttahdr.NumChannels, ttahdr.SampleRate);
298 return -1;
301 /* fill the File Info */
302 info->NCH = ttahdr.NumChannels;
303 info->BPS = ttahdr.BitsPerSample;
304 info->BSIZE = (ttahdr.BitsPerSample + 7)/8;
305 info->FORMAT = ttahdr.AudioFormat;
306 info->SAMPLERATE = ttahdr.SampleRate;
307 info->DATALENGTH = ttahdr.DataLength;
308 info->FRAMELEN = (int) MULTIPLY_FRAME_TIME(ttahdr.SampleRate);
309 info->LENGTH = ttahdr.DataLength / ttahdr.SampleRate;
310 info->DATAPOS = ci->id3->id3v2len;
311 info->FILESIZE = ci->id3->filesize;
313 datasize = info->FILESIZE - info->DATAPOS;
314 origsize = info->DATALENGTH * info->BSIZE * info->NCH;
316 /* info->COMPRESS = (double) datasize / origsize; */
317 info->BITRATE = (int) ((uint64_t) datasize * info->SAMPLERATE * info->NCH * info->BPS
318 / (origsize * 1000LL));
320 return 0;
323 static void rice_init(adapt *rice, unsigned int k0, unsigned int k1) {
324 rice->k0 = k0;
325 rice->k1 = k1;
326 rice->sum0 = shift_16[k0];
327 rice->sum1 = shift_16[k1];
330 static void decoder_init(decoder *tta, int nch, int byte_size) {
331 int shift = flt_set[byte_size - 1];
332 int i;
334 for (i = 0; i < nch; i++) {
335 filter_init(&tta[i].fst, shift);
336 rice_init(&tta[i].rice, 10, 10);
337 tta[i].last = 0;
341 static void seek_table_init (unsigned int *seek_table,
342 unsigned int len, unsigned int data_offset) {
343 unsigned int *st, frame_len;
345 for (st = seek_table; st < (seek_table + len); st++) {
346 frame_len = ENDSWAP_INT32(*st);
347 *st = data_offset;
348 data_offset += frame_len;
352 int set_position (unsigned int pos, enum tta_seek_type type)
354 unsigned int i;
355 unsigned int seek_pos;
357 if (type == TTA_SEEK_TIME)
359 if (pos >= fframes)
360 pos = fframes -1;
362 else
364 pos -= ttainfo->DATAPOS;
365 for (i = 1; i < fframes; i++)
367 if (seek_table[i] > pos)
368 break;
370 pos = i - 1;
372 if (!st_state) {
373 ttainfo->STATE = FILE_ERROR;
374 return -1;
376 seek_pos = ttainfo->DATAPOS + seek_table[data_pos = pos];
377 if (fseek(seek_pos, SEEK_SET) < 0) {
378 ttainfo->STATE = READ_ERROR;
379 return -1;
382 data_cur = 0;
383 framelen = 0;
385 /* init bit reader */
386 init_buffer_read();
387 return data_pos * ttainfo->FRAMELEN;
390 int player_init (tta_info *info) {
391 unsigned int checksum;
392 unsigned int data_offset;
393 unsigned int st_size;
395 ttainfo = info;
397 framelen = 0;
398 data_pos = 0;
399 data_cur = 0;
401 lastlen = ttainfo->DATALENGTH % ttainfo->FRAMELEN;
402 fframes = ttainfo->DATALENGTH / ttainfo->FRAMELEN + (lastlen ? 1 : 0);
403 st_size = (fframes + 1) * sizeof(int);
406 * Rockbox speciffic
407 * playable tta file is to MAX_SEEK_TABLE_SIZE frames
408 * about 1:08:15 (frequency 44.1 kHz)
410 if (fframes > MAX_SEEK_TABLE_SIZE)
412 LOGF("frame is too many: %d > %d", fframes, MAX_SEEK_TABLE_SIZE);
413 return -1;
416 /* read seek table */
417 if (!fread(seek_table, st_size, 1)) {
418 ttainfo->STATE = READ_ERROR;
419 return -1;
422 checksum = crc32((unsigned char *) seek_table, st_size - sizeof(int));
423 st_state = (checksum == ENDSWAP_INT32(seek_table[fframes]));
424 data_offset = sizeof(tta_hdr) + st_size;
426 /* init seek table */
427 seek_table_init(seek_table, fframes, data_offset);
429 /* init bit reader */
430 init_buffer_read();
433 * Rockbox speciffic
434 * because pcm data is int32_t, does not multiply ttainfo->BSIZE.
436 pcm_buffer_size = PCM_BUFFER_LENGTH * ttainfo->NCH;
437 maxvalue = (1UL << ttainfo->BPS) - 1;
439 return 0;
443 * Rockbox specffic
444 * because the seek table is static size buffer, player_stop() is nooperation function.
446 void player_stop (void) {
448 if (seek_table) {
449 free(seek_table);
450 seek_table = NULL;
456 * Rockbox speciffic
457 * with the optimization, the decoding logic is modify a little.
459 int get_samples (int32_t *buffer) {
460 unsigned int k, depth, unary, binary;
461 int32_t *p = buffer;
462 decoder *dec = tta;
463 int value, res;
464 int cur_pos = pcm_buffer_size;
465 int pcm_shift_bits = TTA_OUTPUT_DEPTH - ttainfo->BPS;
466 int pr_bits = (ttainfo->BSIZE == 1)? 4 : 5;
467 int cache = 0; /* decoder cache */
469 fltst *fst;
470 adapt *rice;
472 for (res = 0; --cur_pos >= 0;) {
473 fst = &dec->fst;
474 rice = &dec->rice;
476 if (data_cur == framelen) {
477 if (data_pos == fframes) break;
478 if (framelen && done_buffer_read()) {
479 if (set_position(data_pos, TTA_SEEK_TIME) < 0) return -1;
480 if (res) break;
483 if (data_pos == fframes - 1 && lastlen)
484 framelen = lastlen;
485 else framelen = ttainfo->FRAMELEN;
487 decoder_init(tta, ttainfo->NCH, ttainfo->BSIZE);
488 data_pos++; data_cur = 0;
491 /* decode Rice unsigned */
492 GET_UNARY(unary);
494 switch (unary) {
495 case 0: depth = 0; k = rice->k0; break;
496 default:
497 depth = 1; k = rice->k1;
498 unary--;
501 if (k) {
502 GET_BINARY(binary, k);
503 value = (unary << k) + binary;
504 } else value = unary;
506 value = update_rice(value, rice, depth, shift_16);
508 /* Rockbox specific: the following logic move to update_rice() */
509 #if 0
510 if (depth > 0)
512 rice->sum1 += value - (rice->sum1 >> 4);
513 if (rice->k1 > 0 && rice->sum1 < shift_16[rice->k1])
514 rice->k1--;
515 else if (rice->sum1 > shift_16[rice->k1 + 1])
516 rice->k1++;
517 value += bit_shift[rice->k0];
519 rice->sum0 += value - (rice->sum0 >> 4);
520 if (rice->k0 > 0 && rice->sum0 < shift_16[rice->k0])
521 rice->k0--;
522 else if (rice->sum0 > shift_16[rice->k0 + 1])
523 rice->k0++;
525 value = DEC(value);
526 #endif
528 /* decompress stage 1: adaptive hybrid filter */
529 hybrid_filter(fst, &value);
531 /* decompress stage 2: fixed order 1 prediction */
532 value += PREDICTOR1(dec->last, pr_bits);
533 dec->last = value;
535 /* check for errors */
536 if (abs(value) > maxvalue) {
537 unsigned int tail =
538 pcm_buffer_size / (ttainfo->BSIZE * ttainfo->NCH) - res;
539 ci->memset(buffer, 0, pcm_buffer_size * sizeof(int32_t));
540 data_cur += tail; res += tail;
541 break;
544 /* Rockbox speciffic: Rockbox supports max 2channels */
545 if (ttainfo->NCH == 1)
547 *p++ = value << pcm_shift_bits;
548 data_cur++;
549 res++;
551 else
553 if (dec == tta)
555 cache = value;
556 dec++;
558 else
560 value += cache / 2;
561 cache = value - cache;
562 dec = tta;
563 *p++ = cache << pcm_shift_bits;
564 *p++ = value << pcm_shift_bits;
565 data_cur++;
566 res++;
571 return res;
574 /* Rockbox speciffic: id3 tags functions delete. */
576 /* eof */