| 1 | /* |
| 2 | * ACM decoder. |
| 3 | * |
| 4 | * Copyright (c) 2004-2010, Marko Kreen |
| 5 | * |
| 6 | * Permission to use, copy, modify, and/or distribute this software for any |
| 7 | * purpose with or without fee is hereby granted, provided that the above |
| 8 | * copyright notice and this permission notice appear in all copies. |
| 9 | * |
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | */ |
| 18 | |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include "config.h" |
| 21 | #endif |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | #include "libacm.h" |
| 29 | |
| 30 | #define ACM_BUFLEN (64*1024) |
| 31 | |
| 32 | #define ACM_EXPECTED_EOF -99 |
| 33 | |
| 34 | typedef int (*filler_t)(ACMStream *acm, unsigned ind, unsigned col); |
| 35 | |
| 36 | /************************************** |
| 37 | * Stream processing |
| 38 | **************************************/ |
| 39 | |
| 40 | /* NB: bits <= 31! Thus less checks in code. */ |
| 41 | |
| 42 | static int load_buf(ACMStream *acm) |
| 43 | { |
| 44 | int res = 0; |
| 45 | |
| 46 | if (acm->file_eof) |
| 47 | return 0; |
| 48 | |
| 49 | acm->buf_start_ofs += acm->buf_size; |
| 50 | |
| 51 | if (acm->io.read_func != NULL) |
| 52 | res = acm->io.read_func(acm->buf, 1, acm->buf_max, |
| 53 | acm->io_arg); |
| 54 | |
| 55 | if (res < 0) |
| 56 | return ACM_ERR_READ_ERR; |
| 57 | |
| 58 | if (res == 0) { |
| 59 | acm->file_eof = 1; |
| 60 | /* add single zero byte */ |
| 61 | acm->buf[0] = 0; |
| 62 | acm->buf_size = 1; |
| 63 | } else { |
| 64 | acm->buf_size = res; |
| 65 | } |
| 66 | acm->buf_pos = 0; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int load_bits(ACMStream *acm) |
| 71 | { |
| 72 | int err; |
| 73 | unsigned data, got; |
| 74 | uint8_t *p = acm->buf + acm->buf_pos; |
| 75 | switch (acm->buf_size - acm->buf_pos) { |
| 76 | default: |
| 77 | data = 0; |
| 78 | got = 0; |
| 79 | break; |
| 80 | case 1: |
| 81 | data = p[0]; |
| 82 | got = 8; |
| 83 | break; |
| 84 | case 2: |
| 85 | data = p[0] + (p[1] << 8); |
| 86 | got = 16; |
| 87 | break; |
| 88 | case 3: |
| 89 | data = p[0] + (p[1] << 8) + (p[2] << 16); |
| 90 | got = 24; |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | if ((err = load_buf(acm)) < 0) |
| 95 | return err; |
| 96 | |
| 97 | while (got < 32) { |
| 98 | if (acm->buf_size - acm->buf_pos == 0) |
| 99 | break; |
| 100 | data |= acm->buf[acm->buf_pos] << got; |
| 101 | got += 8; |
| 102 | acm->buf_pos++; |
| 103 | } |
| 104 | acm->bit_data = data; |
| 105 | acm->bit_avail = got; |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | static int get_bits_reload(ACMStream *acm, unsigned bits) |
| 110 | { |
| 111 | int got, err; |
| 112 | unsigned data, b_data, b_avail; |
| 113 | |
| 114 | data = acm->bit_data; |
| 115 | got = acm->bit_avail; |
| 116 | bits -= got; |
| 117 | |
| 118 | if (acm->buf_size - acm->buf_pos >= 4) { |
| 119 | uint8_t *p = acm->buf + acm->buf_pos; |
| 120 | acm->buf_pos += 4; |
| 121 | b_data = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24); |
| 122 | b_avail = 32; |
| 123 | } else { |
| 124 | if ((err = load_bits(acm)) < 0) |
| 125 | return err; |
| 126 | if (acm->bit_avail < bits) |
| 127 | return ACM_ERR_UNEXPECTED_EOF; |
| 128 | b_data = acm->bit_data; |
| 129 | b_avail = acm->bit_avail; |
| 130 | } |
| 131 | |
| 132 | data |= (b_data & ((1 << bits) - 1)) << got; |
| 133 | acm->bit_data = b_data >> bits; |
| 134 | acm->bit_avail = b_avail - bits; |
| 135 | return data; |
| 136 | } |
| 137 | |
| 138 | #define GET_BITS_NOERR(tmpval, acm, bits) do { \ |
| 139 | if (acm->bit_avail >= bits) { \ |
| 140 | tmpval = acm->bit_data & ((1 << bits) - 1); \ |
| 141 | acm->bit_data >>= bits; \ |
| 142 | acm->bit_avail -= bits; \ |
| 143 | } else \ |
| 144 | tmpval = get_bits_reload(acm, bits); \ |
| 145 | } while (0) |
| 146 | |
| 147 | #define GET_BITS(res, acm, bits) do { \ |
| 148 | int tmpval; \ |
| 149 | GET_BITS_NOERR(tmpval, acm, bits); \ |
| 150 | if (tmpval < 0) \ |
| 151 | return tmpval; \ |
| 152 | res = tmpval; \ |
| 153 | } while (0) |
| 154 | |
| 155 | #define GET_BITS_EXPECT_EOF(res, acm, bits) do { \ |
| 156 | int tmpval; \ |
| 157 | GET_BITS_NOERR(tmpval, acm, bits); \ |
| 158 | if (tmpval < 0) { \ |
| 159 | if (tmpval == ACM_ERR_UNEXPECTED_EOF) \ |
| 160 | return ACM_EXPECTED_EOF; \ |
| 161 | return tmpval; \ |
| 162 | } \ |
| 163 | res = tmpval; \ |
| 164 | } while (0) |
| 165 | |
| 166 | /************************************************* |
| 167 | * Table filling |
| 168 | *************************************************/ |
| 169 | static const int map_1bit[] = { -1, +1 }; |
| 170 | static const int map_2bit_near[] = { -2, -1, +1, +2 }; |
| 171 | static const int map_2bit_far[] = { -3, -2, +2, +3 }; |
| 172 | static const int map_3bit[] = { -4, -3, -2, -1, +1, +2, +3, +4 }; |
| 173 | |
| 174 | /* IOW: (r * acm->subblock_len) + c */ |
| 175 | #define set_pos(acm, r, c, idx) do { \ |
| 176 | unsigned _pos = ((r) << acm->info.acm_level) + (c); \ |
| 177 | acm->block[_pos] = acm->midbuf[idx]; \ |
| 178 | } while (0) |
| 179 | |
| 180 | /************ Fillers **********/ |
| 181 | |
| 182 | static int f_zero(ACMStream *acm, unsigned ind, unsigned col) |
| 183 | { |
| 184 | unsigned i; |
| 185 | for (i = 0; i < acm->info.acm_rows; i++) |
| 186 | set_pos(acm, i, col, 0); |
| 187 | |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | static int f_bad(ACMStream *acm, unsigned ind, unsigned col) |
| 192 | { |
| 193 | /* corrupt block? */ |
| 194 | return ACM_ERR_CORRUPT; |
| 195 | } |
| 196 | |
| 197 | static int f_linear(ACMStream *acm, unsigned ind, unsigned col) |
| 198 | { |
| 199 | uint32_t i; |
| 200 | int b, middle = 1 << (ind - 1); |
| 201 | |
| 202 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 203 | GET_BITS(b, acm, ind); |
| 204 | set_pos(acm, i, col, b - middle); |
| 205 | } |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | static int f_k13(ACMStream *acm, unsigned ind, unsigned col) |
| 210 | { |
| 211 | unsigned i, b; |
| 212 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 213 | GET_BITS(b, acm, 1); |
| 214 | if (b == 0) { |
| 215 | /* 0 */ |
| 216 | set_pos(acm, i++, col, 0); |
| 217 | if (i >= acm->info.acm_rows) |
| 218 | break; |
| 219 | set_pos(acm, i, col, 0); |
| 220 | continue; |
| 221 | } |
| 222 | GET_BITS(b, acm, 1); |
| 223 | if (b == 0) { |
| 224 | /* 1, 0 */ |
| 225 | set_pos(acm, i, col, 0); |
| 226 | continue; |
| 227 | } |
| 228 | /* 1, 1, ? */ |
| 229 | GET_BITS(b, acm, 1); |
| 230 | set_pos(acm, i, col, map_1bit[b]); |
| 231 | } |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | static int f_k12(ACMStream *acm, unsigned ind, unsigned col) |
| 236 | { |
| 237 | unsigned i, b; |
| 238 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 239 | GET_BITS(b, acm, 1); |
| 240 | if (b == 0) { |
| 241 | /* 0 */ |
| 242 | set_pos(acm, i, col, 0); |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | /* 1, ? */ |
| 247 | GET_BITS(b, acm, 1); |
| 248 | set_pos(acm, i, col, map_1bit[b]); |
| 249 | } |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | static int f_k24(ACMStream *acm, unsigned ind, unsigned col) |
| 254 | { |
| 255 | unsigned i, b; |
| 256 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 257 | GET_BITS(b, acm, 1); |
| 258 | if (b == 0) { |
| 259 | /* 0 */ |
| 260 | set_pos(acm, i++, col, 0); |
| 261 | if (i >= acm->info.acm_rows) break; |
| 262 | set_pos(acm, i, col, 0); |
| 263 | continue; |
| 264 | } |
| 265 | |
| 266 | GET_BITS(b, acm, 1); |
| 267 | if (b == 0) { |
| 268 | /* 1, 0 */ |
| 269 | set_pos(acm, i, col, 0); |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | /* 1, 1, ?, ? */ |
| 274 | GET_BITS(b, acm, 2); |
| 275 | set_pos(acm, i, col, map_2bit_near[b]); |
| 276 | } |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | static int f_k23(ACMStream *acm, unsigned ind, unsigned col) |
| 281 | { |
| 282 | unsigned i, b; |
| 283 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 284 | GET_BITS(b, acm, 1); |
| 285 | if (b == 0) { |
| 286 | /* 0 */ |
| 287 | set_pos(acm, i, col, 0); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | /* 1, ?, ? */ |
| 292 | GET_BITS(b, acm, 2); |
| 293 | set_pos(acm, i, col, map_2bit_near[b]); |
| 294 | } |
| 295 | return 1; |
| 296 | } |
| 297 | |
| 298 | static int f_k35(ACMStream *acm, unsigned ind, unsigned col) |
| 299 | { |
| 300 | unsigned i, b; |
| 301 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 302 | GET_BITS(b, acm, 1); |
| 303 | if (b == 0) { |
| 304 | /* 0 */ |
| 305 | set_pos(acm, i++, col, 0); |
| 306 | if (i >= acm->info.acm_rows) |
| 307 | break; |
| 308 | set_pos(acm, i, col, 0); |
| 309 | continue; |
| 310 | } |
| 311 | |
| 312 | GET_BITS(b, acm, 1); |
| 313 | if (b == 0) { |
| 314 | /* 1, 0 */ |
| 315 | set_pos(acm, i, col, 0); |
| 316 | continue; |
| 317 | } |
| 318 | |
| 319 | GET_BITS(b, acm, 1); |
| 320 | if (b == 0) { |
| 321 | /* 1, 1, 0, ? */ |
| 322 | GET_BITS(b, acm, 1); |
| 323 | set_pos(acm, i, col, map_1bit[b]); |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | /* 1, 1, 1, ?, ? */ |
| 328 | GET_BITS(b, acm, 2); |
| 329 | set_pos(acm, i, col, map_2bit_far[b]); |
| 330 | } |
| 331 | return 1; |
| 332 | } |
| 333 | |
| 334 | static int f_k34(ACMStream *acm, unsigned ind, unsigned col) |
| 335 | { |
| 336 | unsigned i, b; |
| 337 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 338 | GET_BITS(b, acm, 1); |
| 339 | if (b == 0) { |
| 340 | /* 0 */ |
| 341 | set_pos(acm, i, col, 0); |
| 342 | continue; |
| 343 | } |
| 344 | |
| 345 | GET_BITS(b, acm, 1); |
| 346 | if (b == 0) { |
| 347 | /* 1, 0, ? */ |
| 348 | GET_BITS(b, acm, 1); |
| 349 | set_pos(acm, i, col, map_1bit[b]); |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | /* 1, 1, ?, ? */ |
| 354 | GET_BITS(b, acm, 2); |
| 355 | set_pos(acm, i, col, map_2bit_far[b]); |
| 356 | } |
| 357 | return 1; |
| 358 | } |
| 359 | |
| 360 | static int f_k45(ACMStream *acm, unsigned ind, unsigned col) |
| 361 | { |
| 362 | unsigned i, b; |
| 363 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 364 | GET_BITS(b, acm, 1); |
| 365 | if (b == 0) { |
| 366 | /* 0 */ |
| 367 | set_pos(acm, i, col, 0); i++; |
| 368 | if (i >= acm->info.acm_rows) |
| 369 | break; |
| 370 | set_pos(acm, i, col, 0); |
| 371 | continue; |
| 372 | } |
| 373 | |
| 374 | GET_BITS(b, acm, 1); |
| 375 | if (b == 0) { |
| 376 | /* 1, 0 */ |
| 377 | set_pos(acm, i, col, 0); |
| 378 | continue; |
| 379 | } |
| 380 | |
| 381 | /* 1, 1, ?, ?, ? */ |
| 382 | GET_BITS(b, acm, 3); |
| 383 | set_pos(acm, i, col, map_3bit[b]); |
| 384 | } |
| 385 | return 1; |
| 386 | } |
| 387 | |
| 388 | static int f_k44(ACMStream *acm, unsigned ind, unsigned col) |
| 389 | { |
| 390 | unsigned i, b; |
| 391 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 392 | GET_BITS(b, acm, 1); |
| 393 | if (b == 0) { |
| 394 | /* 0 */ |
| 395 | set_pos(acm, i, col, 0); |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | /* 1, ?, ?, ? */ |
| 400 | GET_BITS(b, acm, 3); |
| 401 | set_pos(acm, i, col, map_3bit[b]); |
| 402 | } |
| 403 | return 1; |
| 404 | } |
| 405 | |
| 406 | static int f_t15(ACMStream *acm, unsigned ind, unsigned col) |
| 407 | { |
| 408 | unsigned i, b; |
| 409 | int n1, n2, n3, tmp; |
| 410 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 411 | /* b = (x1) + (x2 * 3) + (x3 * 9) */ |
| 412 | GET_BITS(b, acm, 5); |
| 413 | if (b >= 3 * 3 * 3) |
| 414 | return ACM_ERR_CORRUPT; |
| 415 | |
| 416 | n1 = b % 3 - 1; |
| 417 | tmp = b / 3; |
| 418 | n2 = tmp % 3 - 1; |
| 419 | n3 = tmp / 3 - 1; |
| 420 | |
| 421 | set_pos(acm, i++, col, n1); |
| 422 | if (i >= acm->info.acm_rows) |
| 423 | break; |
| 424 | set_pos(acm, i++, col, n2); |
| 425 | if (i >= acm->info.acm_rows) |
| 426 | break; |
| 427 | set_pos(acm, i, col, n3); |
| 428 | } |
| 429 | return 1; |
| 430 | } |
| 431 | |
| 432 | static int f_t27(ACMStream *acm, unsigned ind, unsigned col) |
| 433 | { |
| 434 | unsigned i, b; |
| 435 | int n1, n2, n3, tmp; |
| 436 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 437 | /* b = (x1) + (x2 * 5) + (x3 * 25) */ |
| 438 | GET_BITS(b, acm, 7); |
| 439 | if (b >= 5 * 5 * 5) |
| 440 | return ACM_ERR_CORRUPT; |
| 441 | |
| 442 | n1 = b % 5 - 2; |
| 443 | tmp = b / 5; |
| 444 | n2 = tmp % 5 - 2; |
| 445 | n3 = tmp / 5 - 2; |
| 446 | |
| 447 | set_pos(acm, i++, col, n1); |
| 448 | if (i >= acm->info.acm_rows) |
| 449 | break; |
| 450 | set_pos(acm, i++, col, n2); |
| 451 | if (i >= acm->info.acm_rows) |
| 452 | break; |
| 453 | set_pos(acm, i, col, n3); |
| 454 | } |
| 455 | return 1; |
| 456 | } |
| 457 | |
| 458 | static int f_t37(ACMStream *acm, unsigned ind, unsigned col) |
| 459 | { |
| 460 | unsigned i, b; |
| 461 | int n1, n2; |
| 462 | for (i = 0; i < acm->info.acm_rows; i++) { |
| 463 | /* b = (x1) + (x2 * 11) */ |
| 464 | GET_BITS(b, acm, 7); |
| 465 | if (b >= 11 * 11) |
| 466 | return ACM_ERR_CORRUPT; |
| 467 | |
| 468 | n1 = b % 11 - 5; |
| 469 | n2 = b / 11 - 5; |
| 470 | |
| 471 | set_pos(acm, i++, col, n1); |
| 472 | if (i >= acm->info.acm_rows) |
| 473 | break; |
| 474 | set_pos(acm, i, col, n2); |
| 475 | } |
| 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | /****************/ |
| 480 | |
| 481 | static const filler_t filler_list[] = { |
| 482 | f_zero, f_bad, f_bad, f_linear, /* 0..3 */ |
| 483 | f_linear, f_linear, f_linear, f_linear, /* 4..7 */ |
| 484 | f_linear, f_linear, f_linear, f_linear, /* 8..11 */ |
| 485 | f_linear, f_linear, f_linear, f_linear, /* 12..15 */ |
| 486 | f_linear, f_k13, f_k12, f_t15, /* 16..19 */ |
| 487 | f_k24, f_k23, f_t27, f_k35, /* 20..23 */ |
| 488 | f_k34, f_bad, f_k45, f_k44, /* 24..27 */ |
| 489 | f_bad, f_t37, f_bad, f_bad /* 28..31 */ |
| 490 | }; |
| 491 | |
| 492 | static int fill_block(ACMStream *acm) |
| 493 | { |
| 494 | unsigned i, ind; |
| 495 | int err; |
| 496 | for (i = 0; i < acm->info.acm_cols; i++) { |
| 497 | GET_BITS_EXPECT_EOF(ind, acm, 5); |
| 498 | err = filler_list[ind](acm, ind, i); |
| 499 | if (err < 0) |
| 500 | return err; |
| 501 | } |
| 502 | return 1; |
| 503 | } |
| 504 | |
| 505 | /********************************************** |
| 506 | * Decompress code |
| 507 | **********************************************/ |
| 508 | |
| 509 | static void juggle(int *wrap_p, int *block_p, unsigned sub_len, unsigned sub_count) |
| 510 | { |
| 511 | uint32_t i, j; |
| 512 | int *p; |
| 513 | uint32_t r0, r1, r2, r3; |
| 514 | for (i = 0; i < sub_len; i++) { |
| 515 | p = block_p; |
| 516 | r0 = wrap_p[0]; |
| 517 | r1 = wrap_p[1]; |
| 518 | for (j = 0; j < sub_count/2; j++) { |
| 519 | r2 = *p; *p = r1*2 + (r0 + r2); p += sub_len; |
| 520 | r3 = *p; *p = r2*2 - (r1 + r3); p += sub_len; |
| 521 | r0 = r2; r1 = r3; |
| 522 | } |
| 523 | *wrap_p++ = r0; |
| 524 | *wrap_p++ = r1; |
| 525 | block_p++; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | static void juggle_block(ACMStream *acm) |
| 530 | { |
| 531 | unsigned sub_count, sub_len, todo_count, step_subcount, i; |
| 532 | int *wrap_p, *block_p, *p; |
| 533 | |
| 534 | /* juggle only if subblock_len > 1 */ |
| 535 | if (acm->info.acm_level == 0) |
| 536 | return; |
| 537 | |
| 538 | /* 2048 / subblock_len */ |
| 539 | if (acm->info.acm_level > 9) |
| 540 | step_subcount = 1; |
| 541 | else |
| 542 | step_subcount = (2048 >> acm->info.acm_level) - 2; |
| 543 | |
| 544 | /* Apply juggle() (rows)x(cols) |
| 545 | * from (step_subcount * 2) x (subblock_len/2) |
| 546 | * to (step_subcount * subblock_len) x (1) |
| 547 | */ |
| 548 | todo_count = acm->info.acm_rows; |
| 549 | block_p = acm->block; |
| 550 | while (1) { |
| 551 | wrap_p = acm->wrapbuf; |
| 552 | sub_count = step_subcount; |
| 553 | if (sub_count > todo_count) |
| 554 | sub_count = todo_count; |
| 555 | |
| 556 | sub_len = acm->info.acm_cols / 2; |
| 557 | sub_count *= 2; |
| 558 | |
| 559 | juggle(wrap_p, block_p, sub_len, sub_count); |
| 560 | wrap_p += sub_len*2; |
| 561 | |
| 562 | for (i = 0, p = block_p; i < sub_count; i++) { |
| 563 | p[0]++; |
| 564 | p += sub_len; |
| 565 | } |
| 566 | |
| 567 | while (sub_len > 1) { |
| 568 | sub_len /= 2; |
| 569 | sub_count *= 2; |
| 570 | juggle(wrap_p, block_p, sub_len, sub_count); |
| 571 | wrap_p += sub_len*2; |
| 572 | } |
| 573 | if (todo_count <= step_subcount) |
| 574 | break; |
| 575 | todo_count -= step_subcount; |
| 576 | block_p += step_subcount << acm->info.acm_level; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /***************************************************************/ |
| 581 | static int decode_block(ACMStream *acm) |
| 582 | { |
| 583 | int pwr, count, val, i, x, err; |
| 584 | |
| 585 | acm->block_ready = 0; |
| 586 | acm->block_pos = 0; |
| 587 | |
| 588 | /* read header */ |
| 589 | GET_BITS_EXPECT_EOF(pwr, acm, 4); |
| 590 | GET_BITS_EXPECT_EOF(val, acm, 16); |
| 591 | |
| 592 | /* generate tables */ |
| 593 | count = 1 << pwr; |
| 594 | for (i = 0, x = 0; i < count; i++) { |
| 595 | acm->midbuf[i] = x; |
| 596 | x += val; |
| 597 | } |
| 598 | for (i = 1, x = -val; i <= count; i++) { |
| 599 | acm->midbuf[-i] = (unsigned)x; |
| 600 | x -= val; |
| 601 | } |
| 602 | |
| 603 | /* to_check? */ |
| 604 | if ((err = fill_block(acm)) <= 0) |
| 605 | return err; |
| 606 | |
| 607 | juggle_block(acm); |
| 608 | |
| 609 | acm->block_ready = 1; |
| 610 | |
| 611 | return 1; |
| 612 | } |
| 613 | |
| 614 | /****************************** |
| 615 | * Output formats |
| 616 | ******************************/ |
| 617 | |
| 618 | static uint8_t *out_s16le(int *src, uint8_t *dst, unsigned n, unsigned shift) |
| 619 | { |
| 620 | while (n--) { |
| 621 | int val = *src++ >> shift; |
| 622 | *dst++ = val & 0xFF; |
| 623 | *dst++ = (val >> 8) & 0xFF; |
| 624 | } |
| 625 | return dst; |
| 626 | } |
| 627 | |
| 628 | static uint8_t *out_s16be(int *src, uint8_t *dst, unsigned n, unsigned shift) |
| 629 | { |
| 630 | while (n--) { |
| 631 | int val = *src++ >> shift; |
| 632 | *dst++ = (val >> 8) & 0xFF; |
| 633 | *dst++ = val & 0xFF; |
| 634 | } |
| 635 | return dst; |
| 636 | } |
| 637 | |
| 638 | static uint8_t *out_u16le(int *src, uint8_t *dst, unsigned n, unsigned shift) |
| 639 | { |
| 640 | while (n--) { |
| 641 | int val = (*src++ >> shift) + 0x8000; |
| 642 | *dst++ = val & 0xFF; |
| 643 | *dst++ = (val >> 8) & 0xFF; |
| 644 | } |
| 645 | return dst; |
| 646 | } |
| 647 | |
| 648 | static uint8_t *out_u16be(int *src, uint8_t *dst, unsigned n, unsigned shift) |
| 649 | { |
| 650 | while (n--) { |
| 651 | int val = (*src++ >> shift) + 0x8000; |
| 652 | *dst++ = (val >> 8) & 0xFF; |
| 653 | *dst++ = val & 0xFF; |
| 654 | } |
| 655 | return dst; |
| 656 | } |
| 657 | |
| 658 | static int output_values(int *src, uint8_t *dst, int n, |
| 659 | int acm_level, int bigendianp, int wordlen, int sgned) |
| 660 | { |
| 661 | uint8_t *res = NULL; |
| 662 | if (wordlen == 2) { |
| 663 | if (bigendianp == 0) { |
| 664 | if (sgned) |
| 665 | res = out_s16le(src, dst, n, acm_level); |
| 666 | else |
| 667 | res = out_u16le(src, dst, n, acm_level); |
| 668 | } else { |
| 669 | if (sgned) |
| 670 | res = out_s16be(src, dst, n, acm_level); |
| 671 | else |
| 672 | res = out_u16be(src, dst, n, acm_level); |
| 673 | } |
| 674 | } |
| 675 | if (res != NULL) |
| 676 | return res - dst; |
| 677 | return ACM_ERR_BADFMT; |
| 678 | } |
| 679 | |
| 680 | /* |
| 681 | * WAVC (compressed WAV) files are ACM files with additional header. |
| 682 | * |
| 683 | * 'WAVC' + 'V1.00' + uncompr(4b) + compr(4b) + 12b |
| 684 | */ |
| 685 | |
| 686 | #define WAVC_ID 0x564157 /* 'WAV' */ |
| 687 | |
| 688 | static int read_wavc_header(ACMStream *acm) |
| 689 | { |
| 690 | static const uint16_t expect[12] = { |
| 691 | /* 'V1.0', raw_size, acm_size */ |
| 692 | 0x3156, 0x302E, 0,0, 0,0, |
| 693 | /* hdrlen?, chans?, bits?, hz */ |
| 694 | 28,0, 1, 16, 22050, 0 |
| 695 | }; |
| 696 | uint16_t i, buf[12]; |
| 697 | |
| 698 | for (i = 0; i < 12; i++) |
| 699 | GET_BITS(buf[i], acm, 16); |
| 700 | if (memcmp(buf, expect, 4) != 0) |
| 701 | return -1; |
| 702 | /* full comparision is too strict */ |
| 703 | if (0 && memcmp(buf + 6, expect + 6, 12) != 0) |
| 704 | return -1; |
| 705 | /* just make sure the magic 28 is there */ |
| 706 | if (expect[6] != buf[6]) |
| 707 | return -1; |
| 708 | |
| 709 | acm->wavc_file = 1; |
| 710 | return 0; |
| 711 | } |
| 712 | |
| 713 | static int read_header(ACMStream *acm) |
| 714 | { |
| 715 | uint32_t tmp; |
| 716 | |
| 717 | /* read header */ |
| 718 | |
| 719 | GET_BITS(tmp, acm, 24); |
| 720 | if (tmp == WAVC_ID) { |
| 721 | GET_BITS(tmp, acm, 8); |
| 722 | if (tmp != 'C') |
| 723 | return ACM_ERR_NOT_ACM; |
| 724 | if (read_wavc_header(acm) < 0) |
| 725 | return ACM_ERR_NOT_ACM; |
| 726 | GET_BITS(tmp, acm, 24); |
| 727 | } |
| 728 | if (tmp != ACM_ID) |
| 729 | return ACM_ERR_NOT_ACM; |
| 730 | acm->info.acm_id = tmp; |
| 731 | |
| 732 | GET_BITS(acm->info.acm_version, acm, 8); |
| 733 | if (acm->info.acm_version != 1) |
| 734 | return ACM_ERR_NOT_ACM; |
| 735 | GET_BITS(acm->total_values, acm, 16); |
| 736 | GET_BITS(tmp, acm, 16); |
| 737 | acm->total_values += tmp << 16; |
| 738 | if (acm->total_values == 0) |
| 739 | return ACM_ERR_NOT_ACM; |
| 740 | GET_BITS(acm->info.channels, acm, 16); |
| 741 | if (acm->info.channels < 1 || acm->info.channels > 2) |
| 742 | return ACM_ERR_NOT_ACM; |
| 743 | acm->info.acm_channels = acm->info.channels; |
| 744 | GET_BITS(acm->info.rate, acm, 16); |
| 745 | if (acm->info.rate < 4096) |
| 746 | return ACM_ERR_NOT_ACM; |
| 747 | |
| 748 | GET_BITS(acm->info.acm_level, acm, 4); |
| 749 | GET_BITS(acm->info.acm_rows, acm, 12); |
| 750 | if (!acm->info.acm_rows) |
| 751 | return ACM_ERR_NOT_ACM; |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | /*********************************************** |
| 756 | * Public functions |
| 757 | ***********************************************/ |
| 758 | |
| 759 | int acm_open_decoder(ACMStream **res, void *arg, struct acm_io_callbacks io_cb, int force_chans) |
| 760 | { |
| 761 | int err = ACM_ERR_OTHER; |
| 762 | ACMStream *acm; |
| 763 | |
| 764 | acm = malloc(sizeof(*acm)); |
| 765 | if (!acm) |
| 766 | return err; |
| 767 | memset(acm, 0, sizeof(*acm)); |
| 768 | |
| 769 | acm->io_arg = arg; |
| 770 | acm->io = io_cb; |
| 771 | |
| 772 | if (acm->io.get_length_func) { |
| 773 | acm->data_len = acm->io.get_length_func(acm->io_arg); |
| 774 | } else { |
| 775 | acm->data_len = 0; |
| 776 | } |
| 777 | |
| 778 | acm->buf_max = ACM_BUFLEN; |
| 779 | acm->buf = malloc(acm->buf_max); |
| 780 | if (!acm->buf) |
| 781 | goto err_out; |
| 782 | |
| 783 | /* read header data */ |
| 784 | err = ACM_ERR_NOT_ACM; |
| 785 | if (read_header(acm) < 0) |
| 786 | goto err_out; |
| 787 | |
| 788 | /* |
| 789 | * Overwrite channel info if requested, if force_chans == 0 |
| 790 | * use channel count from the header. |
| 791 | * For force_chans == -1, assume that "plain" ACM files are always stereo |
| 792 | * (there are many plain ACM files in the wild that are really stereo |
| 793 | * even though the header specifies 1 channel), but still trust the |
| 794 | * header of WAVC ACM files, as those seem to be correct. |
| 795 | */ |
| 796 | if (force_chans > 0) |
| 797 | acm->info.channels = force_chans; |
| 798 | else if (force_chans == -1 && !acm->wavc_file && acm->info.channels < 2) |
| 799 | acm->info.channels = 2; |
| 800 | /* else if force_chans == 0, trust the file's header */ |
| 801 | |
| 802 | /* calculate blocks */ |
| 803 | acm->info.acm_cols = 1 << acm->info.acm_level; |
| 804 | acm->wrapbuf_len = 2 * acm->info.acm_cols - 2; |
| 805 | acm->block_len = acm->info.acm_rows * acm->info.acm_cols; |
| 806 | |
| 807 | /* allocate */ |
| 808 | acm->block = malloc(acm->block_len * sizeof(int)); |
| 809 | acm->wrapbuf = malloc(acm->wrapbuf_len * sizeof(int)); |
| 810 | acm->ampbuf = malloc(0x10000 * sizeof(int)); |
| 811 | acm->midbuf = acm->ampbuf + 0x8000; |
| 812 | |
| 813 | memset(acm->wrapbuf, 0, acm->wrapbuf_len * sizeof(int)); |
| 814 | |
| 815 | *res = acm; |
| 816 | return ACM_OK; |
| 817 | |
| 818 | err_out: |
| 819 | /* disable callbacks */ |
| 820 | memset(&acm->io, 0, sizeof(acm->io)); |
| 821 | acm->io_arg = NULL; |
| 822 | |
| 823 | acm_close(acm); |
| 824 | return err; |
| 825 | } |
| 826 | |
| 827 | int acm_read(ACMStream *acm, void *dst, unsigned numbytes, |
| 828 | int bigendianp, int wordlen, int sgned) |
| 829 | { |
| 830 | int avail, gotbytes = 0, err; |
| 831 | int *src, numwords; |
| 832 | |
| 833 | if (wordlen == 2) |
| 834 | numwords = numbytes / 2; |
| 835 | else |
| 836 | return ACM_ERR_BADFMT; |
| 837 | |
| 838 | if (acm->stream_pos >= acm->total_values) |
| 839 | return 0; |
| 840 | |
| 841 | if (!acm->block_ready) { |
| 842 | err = decode_block(acm); |
| 843 | if (err == ACM_EXPECTED_EOF) |
| 844 | return 0; |
| 845 | if (err < 0) |
| 846 | return err; |
| 847 | } |
| 848 | |
| 849 | /* check how many words can be read */ |
| 850 | avail = acm->block_len - acm->block_pos; |
| 851 | if (avail < numwords) |
| 852 | numwords = avail; |
| 853 | |
| 854 | if (acm->stream_pos + numwords > acm->total_values) |
| 855 | numwords = acm->total_values - acm->stream_pos; |
| 856 | |
| 857 | if (acm->info.channels > 1) |
| 858 | numwords -= numwords % acm->info.channels; |
| 859 | |
| 860 | /* convert, but if dst == NULL, simulate */ |
| 861 | if (dst != NULL) { |
| 862 | src = acm->block + acm->block_pos; |
| 863 | gotbytes = output_values(src, dst, numwords, |
| 864 | acm->info.acm_level, |
| 865 | bigendianp, wordlen, sgned); |
| 866 | } else |
| 867 | gotbytes = numwords * wordlen; |
| 868 | |
| 869 | if (gotbytes >= 0) { |
| 870 | acm->stream_pos += numwords; |
| 871 | acm->block_pos += numwords; |
| 872 | if (acm->block_pos == acm->block_len) |
| 873 | acm->block_ready = 0; |
| 874 | } |
| 875 | |
| 876 | return gotbytes; |
| 877 | } |
| 878 | |
| 879 | void acm_close(ACMStream *acm) |
| 880 | { |
| 881 | if (acm == NULL) |
| 882 | return; |
| 883 | if (acm->io.close_func) |
| 884 | acm->io.close_func(acm->io_arg); |
| 885 | if (acm->buf) |
| 886 | free(acm->buf); |
| 887 | if (acm->block) |
| 888 | free(acm->block); |
| 889 | if (acm->wrapbuf) |
| 890 | free(acm->wrapbuf); |
| 891 | if (acm->ampbuf) |
| 892 | free(acm->ampbuf); |
| 893 | free(acm); |
| 894 | } |
| 895 |