Mercurial > audlegacy-plugins
comparison src/tta/ttadec.c @ 551:c0f69d57483b trunk
[svn] - change some references for long to int, reported by and patch by
Kazuki Oikawa, closes #769.
author | nenolod |
---|---|
date | Thu, 25 Jan 2007 22:52:52 -0800 |
parents | fbd06b4aa776 |
children | 5bd17596c7e9 |
comparison
equal
deleted
inserted
replaced
550:6e0751b8b2a1 | 551:c0f69d57483b |
---|---|
39 | 39 |
40 /******************* static variables and structures *******************/ | 40 /******************* static variables and structures *******************/ |
41 | 41 |
42 static unsigned char isobuffers[ISO_BUFFERS_SIZE + 4]; | 42 static unsigned char isobuffers[ISO_BUFFERS_SIZE + 4]; |
43 static unsigned char *iso_buffers_end = isobuffers + ISO_BUFFERS_SIZE; | 43 static unsigned char *iso_buffers_end = isobuffers + ISO_BUFFERS_SIZE; |
44 static unsigned long pcm_buffer_size; | 44 static unsigned int pcm_buffer_size; |
45 | 45 |
46 static decoder tta[MAX_NCH]; // decoder state | 46 static decoder tta[MAX_NCH]; // decoder state |
47 static long cache[MAX_NCH]; // decoder cache | 47 static int cache[MAX_NCH]; // decoder cache |
48 | 48 |
49 tta_info *ttainfo; // currently playing file info | 49 tta_info *ttainfo; // currently playing file info |
50 | 50 |
51 static unsigned long fframes; // number of frames in file | 51 static unsigned int fframes; // number of frames in file |
52 static unsigned long framelen; // the frame length in samples | 52 static unsigned int framelen; // the frame length in samples |
53 static unsigned long lastlen; // the length of the last frame in samples | 53 static unsigned int lastlen; // the length of the last frame in samples |
54 static unsigned long data_pos; // currently playing frame index | 54 static unsigned int data_pos; // currently playing frame index |
55 static unsigned long data_cur; // the playing position in frame | 55 static unsigned int data_cur; // the playing position in frame |
56 | 56 |
57 static long maxvalue; // output data max value | 57 static int maxvalue; // output data max value |
58 static unsigned long *seek_table; // the playing position table | 58 static unsigned int *seek_table; // the playing position table |
59 static unsigned long st_state; //seek table status | 59 static unsigned int st_state; //seek table status |
60 | 60 |
61 static unsigned long frame_crc32; | 61 static unsigned int frame_crc32; |
62 static unsigned long bit_count; | 62 static unsigned int bit_count; |
63 static unsigned long bit_cache; | 63 static unsigned int bit_cache; |
64 static unsigned char *bitpos; | 64 static unsigned char *bitpos; |
65 static unsigned long bitrate; | 65 static unsigned int bitrate; |
66 | 66 |
67 void get_id3v1_tag (tta_info *ttainfo); | 67 void get_id3v1_tag (tta_info *ttainfo); |
68 int get_id3v2_tag (tta_info *ttainfo); | 68 int get_id3v2_tag (tta_info *ttainfo); |
69 | 69 |
70 /************************* bit operations ******************************/ | 70 /************************* bit operations ******************************/ |
73 frame_crc32 = 0xFFFFFFFFUL; | 73 frame_crc32 = 0xFFFFFFFFUL; |
74 bit_count = bit_cache = 0; | 74 bit_count = bit_cache = 0; |
75 bitpos = iso_buffers_end; | 75 bitpos = iso_buffers_end; |
76 } | 76 } |
77 | 77 |
78 __inline void get_binary(unsigned long *value, unsigned long bits) { | 78 __inline void get_binary(unsigned int *value, unsigned int bits) { |
79 while (bit_count < bits) { | 79 while (bit_count < bits) { |
80 if (bitpos == iso_buffers_end) { | 80 if (bitpos == iso_buffers_end) { |
81 long res = fread(isobuffers, 1, | 81 int res = fread(isobuffers, 1, |
82 ISO_BUFFERS_SIZE, ttainfo->HANDLE); | 82 ISO_BUFFERS_SIZE, ttainfo->HANDLE); |
83 if (!res) { | 83 if (!res) { |
84 ttainfo->STATE = READ_ERROR; | 84 ttainfo->STATE = READ_ERROR; |
85 return; | 85 return; |
86 } | 86 } |
97 bit_cache >>= bits; | 97 bit_cache >>= bits; |
98 bit_count -= bits; | 98 bit_count -= bits; |
99 bit_cache &= bit_mask[bit_count]; | 99 bit_cache &= bit_mask[bit_count]; |
100 } | 100 } |
101 | 101 |
102 __inline void get_unary(unsigned long *value) { | 102 __inline void get_unary(unsigned int *value) { |
103 *value = 0; | 103 *value = 0; |
104 | 104 |
105 while (!(bit_cache ^ bit_mask[bit_count])) { | 105 while (!(bit_cache ^ bit_mask[bit_count])) { |
106 if (bitpos == iso_buffers_end) { | 106 if (bitpos == iso_buffers_end) { |
107 long res = fread(isobuffers, 1, | 107 int res = fread(isobuffers, 1, |
108 ISO_BUFFERS_SIZE, ttainfo->HANDLE); | 108 ISO_BUFFERS_SIZE, ttainfo->HANDLE); |
109 if (!res) { | 109 if (!res) { |
110 ttainfo->STATE = READ_ERROR; | 110 ttainfo->STATE = READ_ERROR; |
111 return; | 111 return; |
112 } | 112 } |
127 | 127 |
128 bit_cache >>= 1; | 128 bit_cache >>= 1; |
129 bit_count--; | 129 bit_count--; |
130 } | 130 } |
131 | 131 |
132 static long done_buffer_read() { | 132 static int done_buffer_read() { |
133 unsigned long crc32, rbytes, res; | 133 unsigned int crc32, rbytes, res; |
134 frame_crc32 ^= 0xFFFFFFFFUL; | 134 frame_crc32 ^= 0xFFFFFFFFUL; |
135 | 135 |
136 rbytes = iso_buffers_end - bitpos; | 136 rbytes = iso_buffers_end - bitpos; |
137 if (rbytes < sizeof(long)) { | 137 if (rbytes < sizeof(int)) { |
138 memcpy(isobuffers, bitpos, 4); | 138 memcpy(isobuffers, bitpos, 4); |
139 res = fread(isobuffers + rbytes, 1, | 139 res = fread(isobuffers + rbytes, 1, |
140 ISO_BUFFERS_SIZE - rbytes, ttainfo->HANDLE); | 140 ISO_BUFFERS_SIZE - rbytes, ttainfo->HANDLE); |
141 if (!res) { | 141 if (!res) { |
142 ttainfo->STATE = READ_ERROR; | 142 ttainfo->STATE = READ_ERROR; |
145 bitpos = isobuffers; | 145 bitpos = isobuffers; |
146 } | 146 } |
147 | 147 |
148 memcpy(&crc32, bitpos, 4); | 148 memcpy(&crc32, bitpos, 4); |
149 crc32 = ENDSWAP_INT32(crc32); | 149 crc32 = ENDSWAP_INT32(crc32); |
150 bitpos += sizeof(long); | 150 bitpos += sizeof(int); |
151 res = (crc32 != frame_crc32); | 151 res = (crc32 != frame_crc32); |
152 | 152 |
153 bit_cache = bit_count = 0; | 153 bit_cache = bit_count = 0; |
154 frame_crc32 = 0xFFFFFFFFUL; | 154 frame_crc32 = 0xFFFFFFFFUL; |
155 | 155 |
163 return res; | 163 return res; |
164 } | 164 } |
165 | 165 |
166 /************************* decoder functions ****************************/ | 166 /************************* decoder functions ****************************/ |
167 | 167 |
168 static long skip_id3v2_header (FILE *infile) { | 168 static int skip_id3v2_header (FILE *infile) { |
169 struct { | 169 struct { |
170 unsigned char id[3]; | 170 unsigned char id[3]; |
171 unsigned short version; | 171 unsigned short version; |
172 unsigned char flags; | 172 unsigned char flags; |
173 unsigned char size[4]; | 173 unsigned char size[4]; |
174 } __ATTRIBUTE_PACKED__ id3v2; | 174 } __ATTRIBUTE_PACKED__ id3v2; |
175 unsigned long len = 0; | 175 unsigned int len = 0; |
176 | 176 |
177 // read ID3V2 header | 177 // read ID3V2 header |
178 if (fread (&id3v2, sizeof(id3v2), 1, infile) == 0) { | 178 if (fread (&id3v2, sizeof(id3v2), 1, infile) == 0) { |
179 fclose (infile); | 179 fclose (infile); |
180 ttainfo->STATE = READ_ERROR; | 180 ttainfo->STATE = READ_ERROR; |
198 } else fseek (infile, 0, SEEK_SET); | 198 } else fseek (infile, 0, SEEK_SET); |
199 | 199 |
200 return len; | 200 return len; |
201 } | 201 } |
202 | 202 |
203 long open_tta_file (const char *filename, tta_info *info, unsigned long data_offset) { | 203 int open_tta_file (const char *filename, tta_info *info, unsigned int data_offset) { |
204 FILE *infile; | 204 FILE *infile; |
205 tta_hdr ttahdr; | 205 tta_hdr ttahdr; |
206 unsigned long checksum; | 206 unsigned int checksum; |
207 | 207 |
208 // clear the memory | 208 // clear the memory |
209 memset (info, 0, sizeof(tta_info)); | 209 memset (info, 0, sizeof(tta_info)); |
210 | 210 |
211 // printf("0: open_tta_file\n"); | 211 // printf("0: open_tta_file\n"); |
239 return -1; | 239 return -1; |
240 } | 240 } |
241 | 241 |
242 ttahdr.CRC32 = ENDSWAP_INT32(ttahdr.CRC32); | 242 ttahdr.CRC32 = ENDSWAP_INT32(ttahdr.CRC32); |
243 checksum = crc32((unsigned char *) &ttahdr, | 243 checksum = crc32((unsigned char *) &ttahdr, |
244 sizeof(tta_hdr) - sizeof(long)); | 244 sizeof(tta_hdr) - sizeof(int)); |
245 if (checksum != ttahdr.CRC32) { | 245 if (checksum != ttahdr.CRC32) { |
246 fclose (infile); | 246 fclose (infile); |
247 info->STATE = FILE_ERROR; | 247 info->STATE = FILE_ERROR; |
248 return -1; | 248 return -1; |
249 } | 249 } |
278 info->BPS = ttahdr.BitsPerSample; | 278 info->BPS = ttahdr.BitsPerSample; |
279 info->BSIZE = (ttahdr.BitsPerSample + 7)/8; | 279 info->BSIZE = (ttahdr.BitsPerSample + 7)/8; |
280 info->FORMAT = ttahdr.AudioFormat; | 280 info->FORMAT = ttahdr.AudioFormat; |
281 info->SAMPLERATE = ttahdr.SampleRate; | 281 info->SAMPLERATE = ttahdr.SampleRate; |
282 info->DATALENGTH = ttahdr.DataLength; | 282 info->DATALENGTH = ttahdr.DataLength; |
283 info->FRAMELEN = (long) (FRAME_TIME * ttahdr.SampleRate); | 283 info->FRAMELEN = (int) (FRAME_TIME * ttahdr.SampleRate); |
284 info->LENGTH = ttahdr.DataLength / ttahdr.SampleRate; | 284 info->LENGTH = ttahdr.DataLength / ttahdr.SampleRate; |
285 info->DATAPOS = data_offset; | 285 info->DATAPOS = data_offset; |
286 | 286 |
287 | 287 |
288 return 0; | 288 return 0; |
289 } | 289 } |
290 | 290 |
291 static void rice_init(adapt *rice, unsigned long k0, unsigned long k1) { | 291 static void rice_init(adapt *rice, unsigned int k0, unsigned int k1) { |
292 rice->k0 = k0; | 292 rice->k0 = k0; |
293 rice->k1 = k1; | 293 rice->k1 = k1; |
294 rice->sum0 = shift_16[k0]; | 294 rice->sum0 = shift_16[k0]; |
295 rice->sum1 = shift_16[k1]; | 295 rice->sum1 = shift_16[k1]; |
296 } | 296 } |
297 | 297 |
298 static void decoder_init(decoder *tta, long nch, long byte_size) { | 298 static void decoder_init(decoder *tta, int nch, int byte_size) { |
299 long shift = flt_set[byte_size - 1]; | 299 int shift = flt_set[byte_size - 1]; |
300 long i; | 300 int i; |
301 | 301 |
302 for (i = 0; i < nch; i++) { | 302 for (i = 0; i < nch; i++) { |
303 filter_init(&tta[i].fst, shift); | 303 filter_init(&tta[i].fst, shift); |
304 rice_init(&tta[i].rice, 10, 10); | 304 rice_init(&tta[i].rice, 10, 10); |
305 tta[i].last = 0; | 305 tta[i].last = 0; |
306 } | 306 } |
307 } | 307 } |
308 | 308 |
309 static void seek_table_init (unsigned long *seek_table, | 309 static void seek_table_init (unsigned int *seek_table, |
310 unsigned long len, unsigned long data_offset) { | 310 unsigned int len, unsigned int data_offset) { |
311 unsigned long *st, frame_len; | 311 unsigned int *st, frame_len; |
312 | 312 |
313 for (st = seek_table; st < (seek_table + len); st++) { | 313 for (st = seek_table; st < (seek_table + len); st++) { |
314 frame_len = ENDSWAP_INT32(*st); | 314 frame_len = ENDSWAP_INT32(*st); |
315 *st = data_offset; | 315 *st = data_offset; |
316 data_offset += frame_len; | 316 data_offset += frame_len; |
317 } | 317 } |
318 } | 318 } |
319 | 319 |
320 long set_position (unsigned long pos) { | 320 int set_position (unsigned int pos) { |
321 unsigned long seek_pos; | 321 unsigned int seek_pos; |
322 | 322 |
323 if (pos >= fframes) return 0; | 323 if (pos >= fframes) return 0; |
324 if (!st_state) { | 324 if (!st_state) { |
325 ttainfo->STATE = FILE_ERROR; | 325 ttainfo->STATE = FILE_ERROR; |
326 return -1; | 326 return -1; |
336 init_buffer_read(); | 336 init_buffer_read(); |
337 | 337 |
338 return 0; | 338 return 0; |
339 } | 339 } |
340 | 340 |
341 long player_init (tta_info *info) { | 341 int player_init (tta_info *info) { |
342 unsigned long checksum; | 342 unsigned int checksum; |
343 unsigned long data_offset; | 343 unsigned int data_offset; |
344 unsigned long st_size; | 344 unsigned int st_size; |
345 | 345 |
346 ttainfo = info; | 346 ttainfo = info; |
347 | 347 |
348 framelen = 0; | 348 framelen = 0; |
349 data_pos = 0; | 349 data_pos = 0; |
350 data_cur = 0; | 350 data_cur = 0; |
351 bitrate = 0; | 351 bitrate = 0; |
352 | 352 |
353 lastlen = ttainfo->DATALENGTH % ttainfo->FRAMELEN; | 353 lastlen = ttainfo->DATALENGTH % ttainfo->FRAMELEN; |
354 fframes = ttainfo->DATALENGTH / ttainfo->FRAMELEN + (lastlen ? 1:0); | 354 fframes = ttainfo->DATALENGTH / ttainfo->FRAMELEN + (lastlen ? 1:0); |
355 st_size = (fframes + 1) * sizeof(long); | 355 st_size = (fframes + 1) * sizeof(int); |
356 | 356 |
357 seek_table = (unsigned long *) malloc(st_size); | 357 seek_table = (unsigned int *) malloc(st_size); |
358 if (!seek_table) { | 358 if (!seek_table) { |
359 ttainfo->STATE = MEMORY_ERROR; | 359 ttainfo->STATE = MEMORY_ERROR; |
360 return -1; | 360 return -1; |
361 } | 361 } |
362 | 362 |
364 if (!fread(seek_table, st_size, 1, ttainfo->HANDLE)) { | 364 if (!fread(seek_table, st_size, 1, ttainfo->HANDLE)) { |
365 ttainfo->STATE = READ_ERROR; | 365 ttainfo->STATE = READ_ERROR; |
366 return -1; | 366 return -1; |
367 } | 367 } |
368 | 368 |
369 checksum = crc32((unsigned char *) seek_table, st_size - sizeof(long)); | 369 checksum = crc32((unsigned char *) seek_table, st_size - sizeof(int)); |
370 st_state = (checksum == ENDSWAP_INT32(seek_table[fframes])); | 370 st_state = (checksum == ENDSWAP_INT32(seek_table[fframes])); |
371 data_offset = sizeof(tta_hdr) + st_size; | 371 data_offset = sizeof(tta_hdr) + st_size; |
372 | 372 |
373 // init seek table | 373 // init seek table |
374 seek_table_init(seek_table, fframes, data_offset); | 374 seek_table_init(seek_table, fframes, data_offset); |
394 free(seek_table); | 394 free(seek_table); |
395 seek_table = NULL; | 395 seek_table = NULL; |
396 } | 396 } |
397 } | 397 } |
398 | 398 |
399 long get_bitrate () { | 399 int get_bitrate () { |
400 return bitrate; | 400 return bitrate; |
401 } | 401 } |
402 | 402 |
403 long get_samples (byte *buffer) { | 403 int get_samples (byte *buffer) { |
404 unsigned long k, depth, unary, binary; | 404 unsigned int k, depth, unary, binary; |
405 byte *p = buffer; | 405 byte *p = buffer; |
406 decoder *dec = tta; | 406 decoder *dec = tta; |
407 long *prev = cache; | 407 int *prev = cache; |
408 long value, res; | 408 int value, res; |
409 | 409 |
410 for (res = 0; p < buffer + pcm_buffer_size;) { | 410 for (res = 0; p < buffer + pcm_buffer_size;) { |
411 fltst *fst = &dec->fst; | 411 fltst *fst = &dec->fst; |
412 adapt *rice = &dec->rice; | 412 adapt *rice = &dec->rice; |
413 long *last = &dec->last; | 413 int *last = &dec->last; |
414 | 414 |
415 if (data_cur == framelen) { | 415 if (data_cur == framelen) { |
416 if (data_pos == fframes) break; | 416 if (data_pos == fframes) break; |
417 if (framelen && done_buffer_read()) { | 417 if (framelen && done_buffer_read()) { |
418 if (set_position(data_pos) < 0) | 418 if (set_position(data_pos) < 0) |
472 case 4: value += *last; break; // bps 32 | 472 case 4: value += *last; break; // bps 32 |
473 } *last = value; | 473 } *last = value; |
474 | 474 |
475 // check for errors | 475 // check for errors |
476 if (abs(value) > maxvalue) { | 476 if (abs(value) > maxvalue) { |
477 unsigned long tail = | 477 unsigned int tail = |
478 pcm_buffer_size / (ttainfo->BSIZE * ttainfo->NCH) - res; | 478 pcm_buffer_size / (ttainfo->BSIZE * ttainfo->NCH) - res; |
479 memset(buffer, 0, pcm_buffer_size); | 479 memset(buffer, 0, pcm_buffer_size); |
480 data_cur += tail; res += tail; | 480 data_cur += tail; res += tail; |
481 break; | 481 break; |
482 } | 482 } |
484 if (dec < tta + (ttainfo->NCH - 1)) { | 484 if (dec < tta + (ttainfo->NCH - 1)) { |
485 *prev++ = value; dec++; | 485 *prev++ = value; dec++; |
486 } else { | 486 } else { |
487 *prev = value; | 487 *prev = value; |
488 if (ttainfo->NCH > 1) { | 488 if (ttainfo->NCH > 1) { |
489 long *r = prev - 1; | 489 int *r = prev - 1; |
490 for (*prev += *r/2; r >= cache; r--) | 490 for (*prev += *r/2; r >= cache; r--) |
491 *r = *(r + 1) - *r; | 491 *r = *(r + 1) - *r; |
492 for (r = cache; r < prev; r++) | 492 for (r = cache; r < prev; r++) |
493 WRITE_BUFFER(r, ttainfo->BSIZE, p) | 493 WRITE_BUFFER(r, ttainfo->BSIZE, p) |
494 } | 494 } |