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
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.
46 /******************* static variables and structures *******************/
48 static unsigned char isobuffers
[ISO_BUFFERS_SIZE
+ 4] IBSS_ATTR
;
49 static unsigned char * const iso_buffers_end ICONST_ATTR
= isobuffers
+ ISO_BUFFERS_SIZE
;
50 static unsigned int pcm_buffer_size IBSS_ATTR
;
52 static decoder tta
[MAX_NCH
] IBSS_ATTR
; /* decoder state */
53 /* Rockbox speciffic: cache is defined in get_samples() (non static value) */
54 /* static int cache[MAX_NCH]; // decoder cache */
56 static tta_info
*ttainfo IBSS_ATTR
; /* currently playing file info */
58 static unsigned int fframes IBSS_ATTR
; /* number of frames in file */
59 static unsigned int framelen IBSS_ATTR
; /* the frame length in samples */
60 static unsigned int lastlen IBSS_ATTR
; /* the length of the last frame in samples */
61 static unsigned int data_pos IBSS_ATTR
; /* currently playing frame index */
62 static unsigned int data_cur IBSS_ATTR
; /* the playing position in frame */
64 static int maxvalue IBSS_ATTR
; /* output data max value */
66 /* Rockbox speciffic: seek_table is static size */
67 static unsigned int seek_table
[MAX_SEEK_TABLE_SIZE
]; /* the playing position table */
68 static unsigned int st_state
; /* seek table status */
70 static unsigned int frame_crc32 IBSS_ATTR
;
71 static unsigned int bit_count IBSS_ATTR
;
72 static unsigned int bit_cache IBSS_ATTR
;
73 static unsigned char *bitpos IBSS_ATTR
;
75 /* Rockbox speciffic: deletes read_id3_tags(). */
76 /* static int read_id3_tags (tta_info *info); */
78 /********************* rockbox helper functions *************************/
80 /* emulate stdio functions */
81 static size_t tta_fread(void *ptr
, size_t size
, size_t nobj
)
84 unsigned char *buffer
= ci
->request_buffer(&read_size
, size
* nobj
);
88 ci
->memcpy(ptr
, buffer
, read_size
);
89 ci
->advance_buffer(read_size
);
94 static int tta_fseek(long offset
, int origin
)
99 ci
->advance_buffer(offset
);
102 ci
->seek_buffer(offset
);
105 ci
->seek_buffer(offset
+ ci
->id3
->filesize
);
113 /************************* crc32 functions *****************************/
115 #define UPDATE_CRC32(x, crc) crc = \
116 (((crc>>8) & 0x00FFFFFF) ^ crc32_table[(crc^x) & 0xFF])
119 crc32 (unsigned char *buffer
, unsigned int len
) {
121 unsigned int crc
= 0xFFFFFFFF;
123 for (i
= 0; i
< len
; i
++) UPDATE_CRC32(buffer
[i
], crc
);
125 return (crc
^ 0xFFFFFFFF);
128 /************************* bit operations ******************************/
130 #define GET_BINARY(value, bits) \
131 while (bit_count < bits) { \
132 if (bitpos == iso_buffers_end) { \
133 if (!tta_fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
134 ttainfo->STATE = READ_ERROR; \
137 bitpos = isobuffers; \
139 UPDATE_CRC32(*bitpos, frame_crc32); \
140 bit_cache |= *bitpos << bit_count; \
144 value = bit_cache & bit_mask[bits]; \
145 bit_cache >>= bits; \
147 bit_cache &= bit_mask[bit_count];
149 #define GET_UNARY(value) \
151 while (!(bit_cache ^ bit_mask[bit_count])) { \
152 if (bitpos == iso_buffers_end) { \
153 if (!tta_fread(isobuffers, 1, ISO_BUFFERS_SIZE)) { \
154 ttainfo->STATE = READ_ERROR; \
157 bitpos = isobuffers; \
159 value += bit_count; \
160 bit_cache = *bitpos++; \
161 UPDATE_CRC32(bit_cache, frame_crc32); \
164 while (bit_cache & 1) { \
172 /************************* rice operations ******************************/
174 static inline int update_rice(int value
, adapt
*rice
, int depth
,
175 const unsigned int *shift_table
)
179 rice
->sum1
+= value
- (rice
->sum1
>> 4);
180 if (rice
->k1
> 0 && rice
->sum1
< shift_table
[rice
->k1
])
182 else if (rice
->sum1
> shift_table
[rice
->k1
+ 1])
184 value
+= *(shift_table
+ rice
->k0
- 4);
186 rice
->sum0
+= value
- (rice
->sum0
>> 4);
187 if (rice
->k0
> 0 && rice
->sum0
< shift_table
[rice
->k0
])
189 else if (rice
->sum0
> shift_table
[rice
->k0
+ 1])
195 /************************* buffer functions ******************************/
197 static void init_buffer_read(void) {
198 frame_crc32
= 0xFFFFFFFFUL
;
199 bit_count
= bit_cache
= 0;
200 bitpos
= iso_buffers_end
;
203 static int done_buffer_read(void) {
204 unsigned int crc32
, rbytes
;
206 frame_crc32
^= 0xFFFFFFFFUL
;
207 rbytes
= iso_buffers_end
- bitpos
;
209 if (rbytes
< sizeof(int)) {
210 ci
->memcpy(isobuffers
, bitpos
, 4);
211 if (!tta_fread(isobuffers
+ rbytes
, 1, ISO_BUFFERS_SIZE
- rbytes
))
216 ci
->memcpy(&crc32
, bitpos
, 4);
217 crc32
= ENDSWAP_INT32(crc32
);
218 bitpos
+= sizeof(int);
220 if (crc32
!= frame_crc32
) return -1;
222 bit_cache
= bit_count
= 0;
223 frame_crc32
= 0xFFFFFFFFUL
;
228 /************************* decoder functions ****************************/
231 const char *get_error_str (int error) {
233 case NO_ERROR: return "No errors found";
234 case OPEN_ERROR: return "Can't open file";
235 case FORMAT_ERROR: return "Not supported file format";
236 case FILE_ERROR: return "File is corrupted";
237 case READ_ERROR: return "Can't read from file";
238 case MEMORY_ERROR: return "Insufficient memory available";
239 default: return "Unknown error code";
243 int set_tta_info (tta_info
*info
)
245 unsigned int checksum
;
246 unsigned int datasize
;
247 unsigned int origsize
;
250 /* clear the memory */
251 ci
->memset (info
, 0, sizeof(tta_info
));
253 /* skip id3v2 tags */
254 tta_fseek(ci
->id3
->id3v2len
, SEEK_SET
);
256 /* read TTA header */
257 if (tta_fread (&ttahdr
, 1, sizeof (ttahdr
)) == 0) {
258 info
->STATE
= READ_ERROR
;
262 /* check for TTA3 signature */
263 if (ENDSWAP_INT32(ttahdr
.TTAid
) != TTA1_SIGN
) {
264 DEBUGF("ID error: %x\n", ENDSWAP_INT32(ttahdr
.TTAid
));
265 info
->STATE
= FORMAT_ERROR
;
269 ttahdr
.CRC32
= ENDSWAP_INT32(ttahdr
.CRC32
);
270 checksum
= crc32((unsigned char *) &ttahdr
,
271 sizeof(tta_hdr
) - sizeof(int));
272 if (checksum
!= ttahdr
.CRC32
) {
273 DEBUGF("CRC error: %x != %x\n", ttahdr
.CRC32
, checksum
);
274 info
->STATE
= FILE_ERROR
;
278 ttahdr
.AudioFormat
= ENDSWAP_INT16(ttahdr
.AudioFormat
);
279 ttahdr
.NumChannels
= ENDSWAP_INT16(ttahdr
.NumChannels
);
280 ttahdr
.BitsPerSample
= ENDSWAP_INT16(ttahdr
.BitsPerSample
);
281 ttahdr
.SampleRate
= ENDSWAP_INT32(ttahdr
.SampleRate
);
282 ttahdr
.DataLength
= ENDSWAP_INT32(ttahdr
.DataLength
);
284 /* check for player supported formats */
285 if (ttahdr
.AudioFormat
!= WAVE_FORMAT_PCM
||
286 ttahdr
.NumChannels
> MAX_NCH
||
287 ttahdr
.BitsPerSample
> MAX_BPS
||(
288 ttahdr
.SampleRate
!= 16000 &&
289 ttahdr
.SampleRate
!= 22050 &&
290 ttahdr
.SampleRate
!= 24000 &&
291 ttahdr
.SampleRate
!= 32000 &&
292 ttahdr
.SampleRate
!= 44100 &&
293 ttahdr
.SampleRate
!= 48000 &&
294 ttahdr
.SampleRate
!= 64000 &&
295 ttahdr
.SampleRate
!= 88200 &&
296 ttahdr
.SampleRate
!= 96000)) {
297 info
->STATE
= FORMAT_ERROR
;
298 DEBUGF("illegal audio format: %d channels: %d samplerate: %d\n",
299 ttahdr
.AudioFormat
, ttahdr
.NumChannels
, ttahdr
.SampleRate
);
303 /* fill the File Info */
304 info
->NCH
= ttahdr
.NumChannels
;
305 info
->BPS
= ttahdr
.BitsPerSample
;
306 info
->BSIZE
= (ttahdr
.BitsPerSample
+ 7)/8;
307 info
->FORMAT
= ttahdr
.AudioFormat
;
308 info
->SAMPLERATE
= ttahdr
.SampleRate
;
309 info
->DATALENGTH
= ttahdr
.DataLength
;
310 info
->FRAMELEN
= (int) MULTIPLY_FRAME_TIME(ttahdr
.SampleRate
);
311 info
->LENGTH
= ttahdr
.DataLength
/ ttahdr
.SampleRate
;
312 info
->DATAPOS
= ci
->id3
->id3v2len
;
313 info
->FILESIZE
= ci
->id3
->filesize
;
315 datasize
= info
->FILESIZE
- info
->DATAPOS
;
316 origsize
= info
->DATALENGTH
* info
->BSIZE
* info
->NCH
;
318 /* info->COMPRESS = (double) datasize / origsize; */
319 info
->BITRATE
= (int) ((uint64_t) datasize
* info
->SAMPLERATE
* info
->NCH
* info
->BPS
320 / (origsize
* 1000LL));
325 static void rice_init(adapt
*rice
, unsigned int k0
, unsigned int k1
) {
328 rice
->sum0
= shift_16
[k0
];
329 rice
->sum1
= shift_16
[k1
];
332 static void decoder_init(decoder
*tta
, int nch
, int byte_size
) {
333 int shift
= flt_set
[byte_size
- 1];
336 for (i
= 0; i
< nch
; i
++) {
337 filter_init(&tta
[i
].fst
, shift
);
338 rice_init(&tta
[i
].rice
, 10, 10);
343 static void seek_table_init (unsigned int *seek_table
,
344 unsigned int len
, unsigned int data_offset
) {
345 unsigned int *st
, frame_len
;
347 for (st
= seek_table
; st
< (seek_table
+ len
); st
++) {
348 frame_len
= ENDSWAP_INT32(*st
);
350 data_offset
+= frame_len
;
354 int set_position (unsigned int pos
, enum tta_seek_type type
)
357 unsigned int seek_pos
;
359 if (type
== TTA_SEEK_TIME
)
366 pos
-= ttainfo
->DATAPOS
;
367 for (i
= 1; i
< fframes
; i
++)
369 if (seek_table
[i
] > pos
)
375 ttainfo
->STATE
= FILE_ERROR
;
378 seek_pos
= ttainfo
->DATAPOS
+ seek_table
[data_pos
= pos
];
379 if (tta_fseek(seek_pos
, SEEK_SET
) < 0) {
380 ttainfo
->STATE
= READ_ERROR
;
387 /* init bit reader */
389 return data_pos
* ttainfo
->FRAMELEN
;
392 int player_init (tta_info
*info
) {
393 unsigned int checksum
;
394 unsigned int data_offset
;
395 unsigned int st_size
;
398 coldfire_set_macsr(0); /* signed integer mode */
407 lastlen
= ttainfo
->DATALENGTH
% ttainfo
->FRAMELEN
;
408 fframes
= ttainfo
->DATALENGTH
/ ttainfo
->FRAMELEN
+ (lastlen
? 1 : 0);
409 st_size
= (fframes
+ 1) * sizeof(int);
413 * playable tta file is to MAX_SEEK_TABLE_SIZE frames
414 * about 1:08:15 (frequency 44.1 kHz)
416 if (fframes
> MAX_SEEK_TABLE_SIZE
)
418 LOGF("frame is too many: %d > %d", fframes
, MAX_SEEK_TABLE_SIZE
);
422 /* read seek table */
423 if (!tta_fread(seek_table
, st_size
, 1)) {
424 ttainfo
->STATE
= READ_ERROR
;
428 checksum
= crc32((unsigned char *) seek_table
, st_size
- sizeof(int));
429 st_state
= (checksum
== ENDSWAP_INT32(seek_table
[fframes
]));
430 data_offset
= sizeof(tta_hdr
) + st_size
;
432 /* init seek table */
433 seek_table_init(seek_table
, fframes
, data_offset
);
435 /* init bit reader */
440 * because pcm data is int32_t, does not multiply ttainfo->BSIZE.
442 pcm_buffer_size
= PCM_BUFFER_LENGTH
* ttainfo
->NCH
;
443 maxvalue
= (1UL << ttainfo
->BPS
) - 1;
450 * because the seek table is static size buffer, player_stop() is nooperation function.
452 void player_stop (void) {
463 * with the optimization, the decoding logic is modify a little.
465 int get_samples (int32_t *buffer
) {
466 unsigned int k
, depth
, unary
, binary
;
470 int cur_pos
= pcm_buffer_size
;
471 int pcm_shift_bits
= TTA_OUTPUT_DEPTH
- ttainfo
->BPS
;
472 int pr_bits
= (ttainfo
->BSIZE
== 1)? 4 : 5;
473 int cache
= 0; /* decoder cache */
478 for (res
= 0; --cur_pos
>= 0;) {
482 if (data_cur
== framelen
) {
483 if (data_pos
== fframes
) break;
484 if (framelen
&& done_buffer_read()) {
485 if (set_position(data_pos
, TTA_SEEK_TIME
) < 0) return -1;
489 if (data_pos
== fframes
- 1 && lastlen
)
491 else framelen
= ttainfo
->FRAMELEN
;
493 decoder_init(tta
, ttainfo
->NCH
, ttainfo
->BSIZE
);
494 data_pos
++; data_cur
= 0;
497 /* decode Rice unsigned */
501 case 0: depth
= 0; k
= rice
->k0
; break;
503 depth
= 1; k
= rice
->k1
;
508 GET_BINARY(binary
, k
);
509 value
= (unary
<< k
) + binary
;
510 } else value
= unary
;
512 value
= update_rice(value
, rice
, depth
, shift_16
);
514 /* Rockbox specific: the following logic move to update_rice() */
518 rice
->sum1
+= value
- (rice
->sum1
>> 4);
519 if (rice
->k1
> 0 && rice
->sum1
< shift_16
[rice
->k1
])
521 else if (rice
->sum1
> shift_16
[rice
->k1
+ 1])
523 value
+= bit_shift
[rice
->k0
];
525 rice
->sum0
+= value
- (rice
->sum0
>> 4);
526 if (rice
->k0
> 0 && rice
->sum0
< shift_16
[rice
->k0
])
528 else if (rice
->sum0
> shift_16
[rice
->k0
+ 1])
534 /* decompress stage 1: adaptive hybrid filter */
535 hybrid_filter(fst
, &value
);
537 /* decompress stage 2: fixed order 1 prediction */
538 value
+= PREDICTOR1(dec
->last
, pr_bits
);
541 /* check for errors */
542 if (abs(value
) > maxvalue
) {
544 pcm_buffer_size
/ (ttainfo
->BSIZE
* ttainfo
->NCH
) - res
;
545 ci
->memset(buffer
, 0, pcm_buffer_size
* sizeof(int32_t));
546 data_cur
+= tail
; res
+= tail
;
550 /* Rockbox speciffic: Rockbox supports max 2channels */
551 if (ttainfo
->NCH
== 1)
553 *p
++ = value
<< pcm_shift_bits
;
567 cache
= value
- cache
;
569 *p
++ = cache
<< pcm_shift_bits
;
570 *p
++ = value
<< pcm_shift_bits
;
580 /* Rockbox speciffic: id3 tags functions delete. */