1 /* 2 * Copyright (c) 1995 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Permission to use, copy, modify, and distribute this software and its 6 * documentation for any purpose, without fee, and without written agreement is 7 * hereby granted, provided that the above copyright notice and the following 8 * two paragraphs appear in all copies of this software. 9 * 10 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR 11 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 12 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF 13 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 14 * 15 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 17 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 18 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO 19 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 20 */ 21 /* 22 * decoders.h 23 * 24 * This file contains the declarations of structures required for Huffman 25 * decoding 26 * 27 */ 28 29 /* Include util.h for bit i/o parsing macros. */ 30 31 #include "util.h" 32 33 /* Code for unbound values in decoding tables */ 34 #define ERROR (-1) 35 #define DCT_ERROR 63 36 37 #define MACRO_BLOCK_STUFFING 34 38 #define MACRO_BLOCK_ESCAPE 35 39 40 /* Two types of DCT Coefficients */ 41 #define DCT_COEFF_FIRST 0 42 #define DCT_COEFF_NEXT 1 43 44 /* Special values for DCT Coefficients */ 45 #define END_OF_BLOCK 62 46 #define ESCAPE 61 47 48 /* Structure for an entry in the decoding table of 49 * macroblock_address_increment */ 50 typedef struct { 51 int value; /* value for macroblock_address_increment */ 52 int num_bits; /* length of the Huffman code */ 53 } mb_addr_inc_entry; 54 55 /* Decoding table for macroblock_address_increment */ 56 extern mb_addr_inc_entry mb_addr_inc[2048]; 57 58 59 /* Structure for an entry in the decoding table of macroblock_type */ 60 typedef struct { 61 unsigned int mb_quant; /* macroblock_quant */ 62 unsigned int mb_motion_forward; /* macroblock_motion_forward */ 63 unsigned int mb_motion_backward; /* macroblock_motion_backward */ 64 unsigned int mb_pattern; /* macroblock_pattern */ 65 unsigned int mb_intra; /* macroblock_intra */ 66 int num_bits; /* length of the Huffman code */ 67 } mb_type_entry; 68 69 /* Decoding table for macroblock_type in predictive-coded pictures */ 70 extern mb_type_entry mb_type_P[64]; 71 72 /* Decoding table for macroblock_type in bidirectionally-coded pictures */ 73 extern mb_type_entry mb_type_B[64]; 74 75 76 /* Structures for an entry in the decoding table of coded_block_pattern */ 77 typedef struct { 78 unsigned int cbp; /* coded_block_pattern */ 79 int num_bits; /* length of the Huffman code */ 80 } coded_block_pattern_entry; 81 82 /* External declaration of coded block pattern table. */ 83 84 extern const coded_block_pattern_entry coded_block_pattern[512]; 85 86 87 88 /* Structure for an entry in the decoding table of motion vectors */ 89 typedef struct { 90 int code; /* value for motion_horizontal_forward_code, 91 * motion_vertical_forward_code, 92 * motion_horizontal_backward_code, or 93 * motion_vertical_backward_code. 94 */ 95 int num_bits; /* length of the Huffman code */ 96 } motion_vectors_entry; 97 98 99 /* Decoding table for motion vectors */ 100 extern motion_vectors_entry motion_vectors[2048]; 101 102 103 /* Structure for an entry in the decoding table of dct_dc_size */ 104 typedef struct { 105 unsigned int value; /* value of dct_dc_size (luminance or chrominance) */ 106 int num_bits; /* length of the Huffman code */ 107 } dct_dc_size_entry; 108 109 /* External declaration of dct dc size lumiance table. */ 110 111 extern const dct_dc_size_entry dct_dc_size_luminance[32]; 112 extern const dct_dc_size_entry dct_dc_size_luminance1[16]; 113 114 /* External declaration of dct dc size chrom table. */ 115 116 extern const dct_dc_size_entry dct_dc_size_chrominance[32]; 117 extern const dct_dc_size_entry dct_dc_size_chrominance1[32]; 118 119 120 /* DCT coeff tables. */ 121 122 #define RUN_MASK 0xfc00 123 #define LEVEL_MASK 0x03f0 124 #define NUM_MASK 0x000f 125 #define RUN_SHIFT 10 126 #define LEVEL_SHIFT 4 127 128 /* External declaration of dct coeff tables. */ 129 130 extern const unsigned short int dct_coeff_tbl_0[256]; 131 extern const unsigned short int dct_coeff_tbl_1[16]; 132 extern const unsigned short int dct_coeff_tbl_2[4]; 133 extern const unsigned short int dct_coeff_tbl_3[4]; 134 extern const unsigned short int dct_coeff_next[256]; 135 extern const unsigned short int dct_coeff_first[256]; 136 137 #define DecodeDCTDCSizeLum(macro_val) \ 138 { \ 139 unsigned int index; \ 140 \ 141 show_bits5(index); \ 142 \ 143 if (index < 31) { \ 144 macro_val = dct_dc_size_luminance[index].value; \ 145 flush_bits(dct_dc_size_luminance[index].num_bits); \ 146 } \ 147 else { \ 148 show_bits9(index); \ 149 index -= 0x1f0; \ 150 macro_val = dct_dc_size_luminance1[index].value; \ 151 flush_bits(dct_dc_size_luminance1[index].num_bits); \ 152 } \ 153 } 154 155 #define DecodeDCTDCSizeChrom(macro_val) \ 156 { \ 157 unsigned int index; \ 158 \ 159 show_bits5(index); \ 160 \ 161 if (index < 31) { \ 162 macro_val = dct_dc_size_chrominance[index].value; \ 163 flush_bits(dct_dc_size_chrominance[index].num_bits); \ 164 } \ 165 else { \ 166 show_bits10(index); \ 167 index -= 0x3e0; \ 168 macro_val = dct_dc_size_chrominance1[index].value; \ 169 flush_bits(dct_dc_size_chrominance1[index].num_bits); \ 170 } \ 171 } 172 173 #ifdef NO_GRIFF_MODS 174 #define DecodeDCTCoeff(dct_coeff_tbl, run, level) \ 175 { \ 176 unsigned int temp, index; \ 177 unsigned int value, next32bits, flushed; \ 178 \ 179 /* \ 180 * Grab the next 32 bits and use it to improve performance of \ 181 * getting the bits to parse. Thus, calls are translated as: \ 182 * \ 183 * show_bitsX <--> next32bits >> (32-X) \ 184 * get_bitsX <--> val = next32bits >> (32-flushed-X); \ 185 * flushed += X; \ 186 * next32bits &= bitMask[flushed]; \ 187 * flush_bitsX <--> flushed += X; \ 188 * next32bits &= bitMask[flushed]; \ 189 * \ 190 * I've streamlined the code a lot, so that we don't have to mask \ 191 * out the low order bits and a few of the extra adds are removed. \ 192 */ \ 193 show_bits32(next32bits); \ 194 \ 195 /* show_bits8(index); */ \ 196 index = next32bits >> 24; \ 197 \ 198 if (index > 3) { \ 199 value = dct_coeff_tbl[index]; \ 200 run = value >> RUN_SHIFT; \ 201 if (run != END_OF_BLOCK) { \ 202 /* num_bits = (value & NUM_MASK) + 1; */ \ 203 /* flush_bits(num_bits); */ \ 204 if (run != ESCAPE) { \ 205 /* get_bits1(value); */ \ 206 /* if (value) level = -level; */ \ 207 flushed = (value & NUM_MASK) + 2; \ 208 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \ 209 value = next32bits >> (32-flushed); \ 210 value &= 0x1; \ 211 if (value) level = -level; \ 212 /* next32bits &= ((~0) >> flushed); last op before update */ \ 213 } \ 214 else { /* run == ESCAPE */ \ 215 /* Get the next six into run, and next 8 into temp */ \ 216 /* get_bits14(temp); */ \ 217 flushed = (value & NUM_MASK) + 1; \ 218 temp = next32bits >> (18-flushed); \ 219 /* Normally, we'd ad 14 to flushed, but I've saved a few \ 220 * instr by moving the add below */ \ 221 temp &= 0x3fff; \ 222 run = temp >> 8; \ 223 temp &= 0xff; \ 224 if (temp == 0) { \ 225 /* get_bits8(level); */ \ 226 level = next32bits >> (10-flushed); \ 227 level &= 0xff; \ 228 flushed += 22; \ 229 assert(level >= 128); \ 230 } else if (temp != 128) { \ 231 /* Grab sign bit */ \ 232 flushed += 14; \ 233 level = ((int) (temp << 24)) >> 24; \ 234 } else { \ 235 /* get_bits8(level); */ \ 236 level = next32bits >> (10-flushed); \ 237 level &= 0xff; \ 238 flushed += 22; \ 239 level = level - 256; \ 240 assert(level <= -128 && level >= -255); \ 241 } \ 242 } \ 243 /* Update bitstream... */ \ 244 flush_bits(flushed); \ 245 assert (flushed <= 32); \ 246 } \ 247 } \ 248 else { \ 249 switch (index) { \ 250 case 2: { \ 251 /* show_bits10(index); */ \ 252 index = next32bits >> 22; \ 253 value = dct_coeff_tbl_2[index & 3]; \ 254 break; \ 255 } \ 256 case 3: { \ 257 /* show_bits10(index); */ \ 258 index = next32bits >> 22; \ 259 value = dct_coeff_tbl_3[index & 3]; \ 260 break; \ 261 } \ 262 case 1: { \ 263 /* show_bits12(index); */ \ 264 index = next32bits >> 20; \ 265 value = dct_coeff_tbl_1[index & 15]; \ 266 break; \ 267 } \ 268 default: { /* index == 0 */ \ 269 /* show_bits16(index); */ \ 270 index = next32bits >> 16; \ 271 value = dct_coeff_tbl_0[index & 255]; \ 272 }} \ 273 run = value >> RUN_SHIFT; \ 274 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \ 275 \ 276 /* \ 277 * Fold these operations together to make it fast... \ 278 */ \ 279 /* num_bits = (value & NUM_MASK) + 1; */ \ 280 /* flush_bits(num_bits); */ \ 281 /* get_bits1(value); */ \ 282 /* if (value) level = -level; */ \ 283 \ 284 flushed = (value & NUM_MASK) + 2; \ 285 value = next32bits >> (32-flushed); \ 286 value &= 0x1; \ 287 if (value) level = -level; \ 288 \ 289 /* Update bitstream ... */ \ 290 flush_bits(flushed); \ 291 assert (flushed <= 32); \ 292 } \ 293 } 294 #else /* NO_GRIFF_MODS */ 295 #define DecodeDCTCoeff(dct_coeff_tbl, run, level) \ 296 { \ 297 unsigned int temp, index; \ 298 unsigned int value, next32bits, flushed; \ 299 \ 300 /* \ 301 * Grab the next 32 bits and use it to improve performance of \ 302 * getting the bits to parse. Thus, calls are translated as: \ 303 * \ 304 * show_bitsX <--> next32bits >> (32-X) \ 305 * get_bitsX <--> val = next32bits >> (32-flushed-X); \ 306 * flushed += X; \ 307 * next32bits &= bitMask[flushed]; \ 308 * flush_bitsX <--> flushed += X; \ 309 * next32bits &= bitMask[flushed]; \ 310 * \ 311 * I've streamlined the code a lot, so that we don't have to mask \ 312 * out the low order bits and a few of the extra adds are removed. \ 313 */ \ 314 show_bits32(next32bits); \ 315 \ 316 /* show_bits8(index); */ \ 317 index = next32bits >> 24; \ 318 \ 319 if (index > 3) { \ 320 value = dct_coeff_tbl[index]; \ 321 run = value >> RUN_SHIFT; \ 322 if (run != END_OF_BLOCK) { \ 323 /* num_bits = (value & NUM_MASK) + 1; */ \ 324 /* flush_bits(num_bits); */ \ 325 if (run != ESCAPE) { \ 326 /* get_bits1(value); */ \ 327 /* if (value) level = -level; */ \ 328 flushed = (value & NUM_MASK) + 2; \ 329 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \ 330 value = next32bits >> (32-flushed); \ 331 value &= 0x1; \ 332 if (value) level = -level; \ 333 /* next32bits &= ((~0) >> flushed); last op before update */ \ 334 \ 335 /* Update bitstream... */ \ 336 flush_bits(flushed); \ 337 assert (flushed <= 32); \ 338 } \ 339 else { /* run == ESCAPE */ \ 340 /* Get the next six into run, and next 8 into temp */ \ 341 /* get_bits14(temp); */ \ 342 flushed = (value & NUM_MASK) + 1; \ 343 temp = next32bits >> (18-flushed); \ 344 /* Normally, we'd ad 14 to flushed, but I've saved a few \ 345 * instr by moving the add below */ \ 346 temp &= 0x3fff; \ 347 run = temp >> 8; \ 348 temp &= 0xff; \ 349 if (temp == 0) { \ 350 /* get_bits8(level); */ \ 351 level = next32bits >> (10-flushed); \ 352 level &= 0xff; \ 353 flushed += 22; \ 354 /* CG: 12jul2000 - assert(level >= 128); */ \ 355 if (level >= 128) { \ 356 /* Update bitstream... */ \ 357 flush_bits(flushed); \ 358 assert (flushed <= 32); \ 359 } else { \ 360 run = END_OF_BLOCK; \ 361 level = END_OF_BLOCK; \ 362 } \ 363 } else if (temp != 128) { \ 364 /* Grab sign bit */ \ 365 flushed += 14; \ 366 level = ((int) (temp << 24)) >> 24; \ 367 /* Update bitstream... */ \ 368 flush_bits(flushed); \ 369 assert (flushed <= 32); \ 370 } else { \ 371 /* get_bits8(level); */ \ 372 level = next32bits >> (10-flushed); \ 373 level &= 0xff; \ 374 flushed += 22; \ 375 level = level - 256; \ 376 /* CG: 12jul2000 - assert(level <= -128 && level >= -255); */ \ 377 if ( level <= -128 && level >= -255) { \ 378 /* Update bitstream... */ \ 379 flush_bits(flushed); \ 380 assert (flushed <= 32); \ 381 } else { \ 382 run = END_OF_BLOCK; \ 383 level = END_OF_BLOCK; \ 384 } \ 385 } \ 386 } \ 387 } \ 388 } \ 389 else { \ 390 switch (index) { \ 391 case 2: { \ 392 /* show_bits10(index); */ \ 393 index = next32bits >> 22; \ 394 value = dct_coeff_tbl_2[index & 3]; \ 395 break; \ 396 } \ 397 case 3: { \ 398 /* show_bits10(index); */ \ 399 index = next32bits >> 22; \ 400 value = dct_coeff_tbl_3[index & 3]; \ 401 break; \ 402 } \ 403 case 1: { \ 404 /* show_bits12(index); */ \ 405 index = next32bits >> 20; \ 406 value = dct_coeff_tbl_1[index & 15]; \ 407 break; \ 408 } \ 409 default: { /* index == 0 */ \ 410 /* show_bits16(index); */ \ 411 index = next32bits >> 16; \ 412 value = dct_coeff_tbl_0[index & 255]; \ 413 }} \ 414 run = value >> RUN_SHIFT; \ 415 level = (value & LEVEL_MASK) >> LEVEL_SHIFT; \ 416 \ 417 /* \ 418 * Fold these operations together to make it fast... \ 419 */ \ 420 /* num_bits = (value & NUM_MASK) + 1; */ \ 421 /* flush_bits(num_bits); */ \ 422 /* get_bits1(value); */ \ 423 /* if (value) level = -level; */ \ 424 \ 425 flushed = (value & NUM_MASK) + 2; \ 426 value = next32bits >> (32-flushed); \ 427 value &= 0x1; \ 428 if (value) level = -level; \ 429 \ 430 /* Update bitstream ... */ \ 431 flush_bits(flushed); \ 432 assert (flushed <= 32); \ 433 } \ 434 } 435 #endif /* NO_GRIFF_MODS */ 436 437 #define DecodeDCTCoeffFirst(runval, levelval) \ 438 { \ 439 DecodeDCTCoeff(dct_coeff_first, runval, levelval); \ 440 } 441 442 #define DecodeDCTCoeffNext(runval, levelval) \ 443 { \ 444 DecodeDCTCoeff(dct_coeff_next, runval, levelval); \ 445 } 446 447 /* 448 *-------------------------------------------------------------- 449 * 450 * DecodeMBAddrInc -- 451 * 452 * Huffman Decoder for macro_block_address_increment; the location 453 * in which the result will be placed is being passed as argument. 454 * The decoded value is obtained by doing a table lookup on 455 * mb_addr_inc. 456 * 457 * Results: 458 * The decoded value for macro_block_address_increment or ERROR 459 * for unbound values will be placed in the location specified. 460 * 461 * Side effects: 462 * Bit stream is irreversibly parsed. 463 * 464 *-------------------------------------------------------------- 465 */ 466 #define DecodeMBAddrInc(val) \ 467 { \ 468 unsigned int index; \ 469 show_bits11(index); \ 470 val = mb_addr_inc[index].value; \ 471 flush_bits(mb_addr_inc[index].num_bits); \ 472 } 473 474 /* 475 *-------------------------------------------------------------- 476 * 477 * DecodeMotionVectors -- 478 * 479 * Huffman Decoder for the various motion vectors, including 480 * motion_horizontal_forward_code, motion_vertical_forward_code, 481 * motion_horizontal_backward_code, motion_vertical_backward_code. 482 * Location where the decoded result will be placed is being passed 483 * as argument. The decoded values are obtained by doing a table 484 * lookup on motion_vectors. 485 * 486 * Results: 487 * The decoded value for the motion vector or ERROR for unbound 488 * values will be placed in the location specified. 489 * 490 * Side effects: 491 * Bit stream is irreversibly parsed. 492 * 493 *-------------------------------------------------------------- 494 */ 495 496 #define DecodeMotionVectors(value) \ 497 { \ 498 unsigned int index; \ 499 show_bits11(index); \ 500 value = motion_vectors[index].code; \ 501 flush_bits(motion_vectors[index].num_bits); \ 502 } 503 /* 504 *-------------------------------------------------------------- 505 * 506 * DecodeMBTypeB -- 507 * 508 * Huffman Decoder for macro_block_type in bidirectionally-coded 509 * pictures;locations in which the decoded results: macroblock_quant, 510 * macroblock_motion_forward, macro_block_motion_backward, 511 * macroblock_pattern, macro_block_intra, will be placed are 512 * being passed as argument. The decoded values are obtained by 513 * doing a table lookup on mb_type_B. 514 * 515 * Results: 516 * The various decoded values for macro_block_type in 517 * bidirectionally-coded pictures or ERROR for unbound values will 518 * be placed in the locations specified. 519 * 520 * Side effects: 521 * Bit stream is irreversibly parsed. 522 * 523 *-------------------------------------------------------------- 524 */ 525 #define DecodeMBTypeB(quant, motion_fwd, motion_bwd, pat, intra) \ 526 { \ 527 unsigned int index; \ 528 \ 529 show_bits6(index); \ 530 \ 531 quant = mb_type_B[index].mb_quant; \ 532 motion_fwd = mb_type_B[index].mb_motion_forward; \ 533 motion_bwd = mb_type_B[index].mb_motion_backward; \ 534 pat = mb_type_B[index].mb_pattern; \ 535 intra = mb_type_B[index].mb_intra; \ 536 flush_bits(mb_type_B[index].num_bits); \ 537 } 538 /* 539 *-------------------------------------------------------------- 540 * 541 * DecodeMBTypeI -- 542 * 543 * Huffman Decoder for macro_block_type in intra-coded pictures; 544 * locations in which the decoded results: macroblock_quant, 545 * macroblock_motion_forward, macro_block_motion_backward, 546 * macroblock_pattern, macro_block_intra, will be placed are 547 * being passed as argument. 548 * 549 * Results: 550 * The various decoded values for macro_block_type in intra-coded 551 * pictures or ERROR for unbound values will be placed in the 552 * locations specified. 553 * 554 * Side effects: 555 * Bit stream is irreversibly parsed. 556 * 557 *-------------------------------------------------------------- 558 */ 559 #define DecodeMBTypeI(quant, motion_fwd, motion_bwd, pat, intra) \ 560 { \ 561 unsigned int index; \ 562 static int quantTbl[4] = {ERROR, 1, 0, 0}; \ 563 \ 564 show_bits2(index); \ 565 \ 566 motion_fwd = 0; \ 567 motion_bwd = 0; \ 568 pat = 0; \ 569 intra = 1; \ 570 quant = quantTbl[index]; \ 571 if (index) { \ 572 flush_bits (1 + quant); \ 573 } \ 574 } 575 /* 576 *-------------------------------------------------------------- 577 * 578 * DecodeMBTypeP -- 579 * 580 * Huffman Decoder for macro_block_type in predictive-coded pictures; 581 * locations in which the decoded results: macroblock_quant, 582 * macroblock_motion_forward, macro_block_motion_backward, 583 * macroblock_pattern, macro_block_intra, will be placed are 584 * being passed as argument. The decoded values are obtained by 585 * doing a table lookup on mb_type_P. 586 * 587 * Results: 588 * The various decoded values for macro_block_type in 589 * predictive-coded pictures or ERROR for unbound values will be 590 * placed in the locations specified. 591 * 592 * Side effects: 593 * Bit stream is irreversibly parsed. 594 * 595 *-------------------------------------------------------------- 596 */ 597 #define DecodeMBTypeP(quant, motion_fwd, motion_bwd, pat, intra) \ 598 { \ 599 unsigned int index; \ 600 \ 601 show_bits6(index); \ 602 \ 603 quant = mb_type_P[index].mb_quant; \ 604 motion_fwd = mb_type_P[index].mb_motion_forward; \ 605 motion_bwd = mb_type_P[index].mb_motion_backward; \ 606 pat = mb_type_P[index].mb_pattern; \ 607 intra = mb_type_P[index].mb_intra; \ 608 \ 609 flush_bits(mb_type_P[index].num_bits); \ 610 } 611 /* 612 *-------------------------------------------------------------- 613 * 614 * DecodeCBP -- 615 * 616 * Huffman Decoder for coded_block_pattern; location in which the 617 * decoded result will be placed is being passed as argument. The 618 * decoded values are obtained by doing a table lookup on 619 * coded_block_pattern. 620 * 621 * Results: 622 * The decoded value for coded_block_pattern or ERROR for unbound 623 * values will be placed in the location specified. 624 * 625 * Side effects: 626 * Bit stream is irreversibly parsed. 627 * 628 *-------------------------------------------------------------- 629 */ 630 #define DecodeCBP(coded_bp) \ 631 { \ 632 unsigned int index; \ 633 \ 634 show_bits9(index); \ 635 coded_bp = coded_block_pattern[index].cbp; \ 636 flush_bits(coded_block_pattern[index].num_bits); \ 637 } 638