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 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 ****************************/
230 const char *get_error_str (int error
) {
232 case NO_ERROR
: return "No errors found";
233 case OPEN_ERROR
: return "Can't open file";
234 case FORMAT_ERROR
: return "Not supported file format";
235 case FILE_ERROR
: return "File is corrupted";
236 case READ_ERROR
: return "Can't read from file";
237 case MEMORY_ERROR
: return "Insufficient memory available";
238 default: return "Unknown error code";
242 int set_tta_info (tta_info
*info
)
244 unsigned int checksum
;
245 unsigned int datasize
;
246 unsigned int origsize
;
249 /* clear the memory */
250 ci
->memset (info
, 0, sizeof(tta_info
));
252 /* skip id3v2 tags */
253 tta_fseek(ci
->id3
->id3v2len
, SEEK_SET
);
255 /* read TTA header */
256 if (tta_fread (&ttahdr
, 1, sizeof (ttahdr
)) == 0) {
257 info
->STATE
= READ_ERROR
;
261 /* check for TTA3 signature */
262 if (ENDSWAP_INT32(ttahdr
.TTAid
) != TTA1_SIGN
) {
263 DEBUGF("ID error: %x\n", ENDSWAP_INT32(ttahdr
.TTAid
));
264 info
->STATE
= FORMAT_ERROR
;
268 ttahdr
.CRC32
= ENDSWAP_INT32(ttahdr
.CRC32
);
269 checksum
= crc32((unsigned char *) &ttahdr
,
270 sizeof(tta_hdr
) - sizeof(int));
271 if (checksum
!= ttahdr
.CRC32
) {
272 DEBUGF("CRC error: %x != %x\n", ttahdr
.CRC32
, checksum
);
273 info
->STATE
= FILE_ERROR
;
277 ttahdr
.AudioFormat
= ENDSWAP_INT16(ttahdr
.AudioFormat
);
278 ttahdr
.NumChannels
= ENDSWAP_INT16(ttahdr
.NumChannels
);
279 ttahdr
.BitsPerSample
= ENDSWAP_INT16(ttahdr
.BitsPerSample
);
280 ttahdr
.SampleRate
= ENDSWAP_INT32(ttahdr
.SampleRate
);
281 ttahdr
.DataLength
= ENDSWAP_INT32(ttahdr
.DataLength
);
283 /* check for player supported formats */
284 if (ttahdr
.AudioFormat
!= WAVE_FORMAT_PCM
||
285 ttahdr
.NumChannels
> MAX_NCH
||
286 ttahdr
.BitsPerSample
> MAX_BPS
||(
287 ttahdr
.SampleRate
!= 16000 &&
288 ttahdr
.SampleRate
!= 22050 &&
289 ttahdr
.SampleRate
!= 24000 &&
290 ttahdr
.SampleRate
!= 32000 &&
291 ttahdr
.SampleRate
!= 44100 &&
292 ttahdr
.SampleRate
!= 48000 &&
293 ttahdr
.SampleRate
!= 64000 &&
294 ttahdr
.SampleRate
!= 88200 &&
295 ttahdr
.SampleRate
!= 96000)) {
296 info
->STATE
= FORMAT_ERROR
;
297 DEBUGF("illegal audio format: %d channels: %d samplerate: %d\n",
298 ttahdr
.AudioFormat
, ttahdr
.NumChannels
, ttahdr
.SampleRate
);
302 /* fill the File Info */
303 info
->NCH
= ttahdr
.NumChannels
;
304 info
->BPS
= ttahdr
.BitsPerSample
;
305 info
->BSIZE
= (ttahdr
.BitsPerSample
+ 7)/8;
306 info
->FORMAT
= ttahdr
.AudioFormat
;
307 info
->SAMPLERATE
= ttahdr
.SampleRate
;
308 info
->DATALENGTH
= ttahdr
.DataLength
;
309 info
->FRAMELEN
= (int) MULTIPLY_FRAME_TIME(ttahdr
.SampleRate
);
310 info
->LENGTH
= ttahdr
.DataLength
/ ttahdr
.SampleRate
;
311 info
->DATAPOS
= ci
->id3
->id3v2len
;
312 info
->FILESIZE
= ci
->id3
->filesize
;
314 datasize
= info
->FILESIZE
- info
->DATAPOS
;
315 origsize
= info
->DATALENGTH
* info
->BSIZE
* info
->NCH
;
317 /* info->COMPRESS = (double) datasize / origsize; */
318 info
->BITRATE
= (int) ((uint64_t) datasize
* info
->SAMPLERATE
* info
->NCH
* info
->BPS
319 / (origsize
* 1000LL));
324 static void rice_init(adapt
*rice
, unsigned int k0
, unsigned int k1
) {
327 rice
->sum0
= shift_16
[k0
];
328 rice
->sum1
= shift_16
[k1
];
331 static void decoder_init(decoder
*tta
, int nch
, int byte_size
) {
332 int shift
= flt_set
[byte_size
- 1];
335 for (i
= 0; i
< nch
; i
++) {
336 filter_init(&tta
[i
].fst
, shift
);
337 rice_init(&tta
[i
].rice
, 10, 10);
342 static void seek_table_init (unsigned int *seek_table
,
343 unsigned int len
, unsigned int data_offset
) {
344 unsigned int *st
, frame_len
;
346 for (st
= seek_table
; st
< (seek_table
+ len
); st
++) {
347 frame_len
= ENDSWAP_INT32(*st
);
349 data_offset
+= frame_len
;
353 int set_position (unsigned int pos
, enum tta_seek_type type
)
356 unsigned int seek_pos
;
358 if (type
== TTA_SEEK_TIME
)
365 pos
-= ttainfo
->DATAPOS
;
366 for (i
= 1; i
< fframes
; i
++)
368 if (seek_table
[i
] > pos
)
374 ttainfo
->STATE
= FILE_ERROR
;
377 seek_pos
= ttainfo
->DATAPOS
+ seek_table
[data_pos
= pos
];
378 if (tta_fseek(seek_pos
, SEEK_SET
) < 0) {
379 ttainfo
->STATE
= READ_ERROR
;
386 /* init bit reader */
388 return data_pos
* ttainfo
->FRAMELEN
;
391 int player_init (tta_info
*info
) {
392 unsigned int checksum
;
393 unsigned int data_offset
;
394 unsigned int st_size
;
397 coldfire_set_macsr(0); /* signed integer mode */
406 lastlen
= ttainfo
->DATALENGTH
% ttainfo
->FRAMELEN
;
407 fframes
= ttainfo
->DATALENGTH
/ ttainfo
->FRAMELEN
+ (lastlen
? 1 : 0);
408 st_size
= (fframes
+ 1) * sizeof(int);
412 * playable tta file is to MAX_SEEK_TABLE_SIZE frames
413 * about 1:08:15 (frequency 44.1 kHz)
415 if (fframes
> MAX_SEEK_TABLE_SIZE
)
417 LOGF("frame is too many: %d > %d", fframes
, MAX_SEEK_TABLE_SIZE
);
421 /* read seek table */
422 if (!tta_fread(seek_table
, st_size
, 1)) {
423 ttainfo
->STATE
= READ_ERROR
;
427 checksum
= crc32((unsigned char *) seek_table
, st_size
- sizeof(int));
428 st_state
= (checksum
== ENDSWAP_INT32(seek_table
[fframes
]));
429 data_offset
= sizeof(tta_hdr
) + st_size
;
431 /* init seek table */
432 seek_table_init(seek_table
, fframes
, data_offset
);
434 /* init bit reader */
439 * because pcm data is int32_t, does not multiply ttainfo->BSIZE.
441 pcm_buffer_size
= PCM_BUFFER_LENGTH
* ttainfo
->NCH
;
442 maxvalue
= (1UL << ttainfo
->BPS
) - 1;
449 * because the seek table is static size buffer, player_stop() is nooperation function.
451 void player_stop (void) {
462 * with the optimization, the decoding logic is modify a little.
464 int get_samples (int32_t *buffer
) {
465 unsigned int k
, depth
, unary
, binary
;
469 int cur_pos
= pcm_buffer_size
;
470 int pcm_shift_bits
= TTA_OUTPUT_DEPTH
- ttainfo
->BPS
;
471 int pr_bits
= (ttainfo
->BSIZE
== 1)? 4 : 5;
472 int cache
= 0; /* decoder cache */
477 for (res
= 0; --cur_pos
>= 0;) {
481 if (data_cur
== framelen
) {
482 if (data_pos
== fframes
) break;
483 if (framelen
&& done_buffer_read()) {
484 if (set_position(data_pos
, TTA_SEEK_TIME
) < 0) return -1;
488 if (data_pos
== fframes
- 1 && lastlen
)
490 else framelen
= ttainfo
->FRAMELEN
;
492 decoder_init(tta
, ttainfo
->NCH
, ttainfo
->BSIZE
);
493 data_pos
++; data_cur
= 0;
496 /* decode Rice unsigned */
500 case 0: depth
= 0; k
= rice
->k0
; break;
502 depth
= 1; k
= rice
->k1
;
507 GET_BINARY(binary
, k
);
508 value
= (unary
<< k
) + binary
;
509 } else value
= unary
;
511 value
= update_rice(value
, rice
, depth
, shift_16
);
513 /* Rockbox specific: the following logic move to update_rice() */
517 rice
->sum1
+= value
- (rice
->sum1
>> 4);
518 if (rice
->k1
> 0 && rice
->sum1
< shift_16
[rice
->k1
])
520 else if (rice
->sum1
> shift_16
[rice
->k1
+ 1])
522 value
+= bit_shift
[rice
->k0
];
524 rice
->sum0
+= value
- (rice
->sum0
>> 4);
525 if (rice
->k0
> 0 && rice
->sum0
< shift_16
[rice
->k0
])
527 else if (rice
->sum0
> shift_16
[rice
->k0
+ 1])
533 /* decompress stage 1: adaptive hybrid filter */
534 hybrid_filter(fst
, &value
);
536 /* decompress stage 2: fixed order 1 prediction */
537 value
+= PREDICTOR1(dec
->last
, pr_bits
);
540 /* check for errors */
541 if (abs(value
) > maxvalue
) {
543 pcm_buffer_size
/ (ttainfo
->BSIZE
* ttainfo
->NCH
) - res
;
544 ci
->memset(buffer
, 0, pcm_buffer_size
* sizeof(int32_t));
545 data_cur
+= tail
; res
+= tail
;
549 /* Rockbox speciffic: Rockbox supports max 2channels */
550 if (ttainfo
->NCH
== 1)
552 *p
++ = value
<< pcm_shift_bits
;
566 cache
= value
- cache
;
568 *p
++ = cache
<< pcm_shift_bits
;
569 *p
++ = value
<< pcm_shift_bits
;
579 /* Rockbox speciffic: id3 tags functions delete. */