0.2.0-dev Download ZIP
1367 lines ·51.4 KB Raw
| 1 | /******************************************************************** |
| 2 | * * |
| 3 | * THIS FILE IS PART OF THE OggTheora SOFTWARE CODEC SOURCE CODE. * |
| 4 | * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS * |
| 5 | * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE * |
| 6 | * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. * |
| 7 | * * |
| 8 | * THE Theora SOURCE CODE IS COPYRIGHT (C) 2002-2009,2025 * |
| 9 | * by the Xiph.Org Foundation https://www.xiph.org/ * |
| 10 | * * |
| 11 | ******************************************************************** |
| 12 | |
| 13 | function: |
| 14 | |
| 15 | ********************************************************************/ |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 | #include "encint.h" |
| 19 | |
| 20 | |
| 21 | |
| 22 | static unsigned char OC_DCT_EOB_TOKEN[31]={ |
| 23 | 0,1,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5 |
| 24 | }; |
| 25 | |
| 26 | static int oc_make_eob_token(int _run_count){ |
| 27 | return _run_count<32?OC_DCT_EOB_TOKEN[_run_count-1]:OC_DCT_REPEAT_RUN3_TOKEN; |
| 28 | } |
| 29 | |
| 30 | static unsigned char OC_DCT_EOB_EB[31]={ |
| 31 | 0,0,0,0,1,2,3,0,1,2,3,4,5,6,7,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 |
| 32 | }; |
| 33 | |
| 34 | static int oc_make_eob_token_full(int _run_count,int *_eb){ |
| 35 | if(_run_count<32){ |
| 36 | *_eb=OC_DCT_EOB_EB[_run_count-1]; |
| 37 | return OC_DCT_EOB_TOKEN[_run_count-1]; |
| 38 | } |
| 39 | else{ |
| 40 | *_eb=_run_count; |
| 41 | return OC_DCT_REPEAT_RUN3_TOKEN; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /*Returns the number of blocks ended by an EOB token.*/ |
| 46 | static int oc_decode_eob_token(int _token,int _eb){ |
| 47 | return (0x20820C41U>>_token*5&0x1F)+_eb; |
| 48 | } |
| 49 | |
| 50 | /*Some tables for fast construction of value tokens.*/ |
| 51 | |
| 52 | static const unsigned char OC_DCT_VALUE_TOKEN[1161]={ |
| 53 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 54 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 55 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 56 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 57 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 58 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 59 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 60 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 61 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 62 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 63 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 64 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 65 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 66 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 67 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 68 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 69 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 70 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 71 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 72 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 73 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 74 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 75 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 76 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 77 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 78 | 22,22,22,22,22,22,22,22,22,22,22,22,21,21,21,21,21,21,21,21, |
| 79 | 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21, |
| 80 | 21,21,21,21,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20, |
| 81 | 19,19,19,19,19,19,19,19,18,18,18,18,17,17,16,15,14,13,12,10, |
| 82 | 7, |
| 83 | 9,11,13,14,15,16,17,17,18,18,18,18,19,19,19,19,19,19,19,19, |
| 84 | 20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,21,21,21,21, |
| 85 | 21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21,21, |
| 86 | 21,21,21,21,21,21,21,21,22,22,22,22,22,22,22,22,22,22,22,22, |
| 87 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 88 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 89 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 90 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 91 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 92 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 93 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 94 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 95 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 96 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 97 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 98 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 99 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 100 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 101 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 102 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 103 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 104 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 105 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 106 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 107 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 108 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 109 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 110 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22, |
| 111 | 22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22,22 |
| 112 | }; |
| 113 | |
| 114 | static const ogg_uint16_t OC_DCT_VALUE_EB[1161]={ |
| 115 | 1023,1022,1021,1020,1019,1018,1017,1016,1015,1014, |
| 116 | 1013,1012,1011,1010,1009,1008,1007,1006,1005,1004, |
| 117 | 1003,1002,1001,1000, 999, 998, 997, 996, 995, 994, |
| 118 | 993, 992, 991, 990, 989, 988, 987, 986, 985, 984, |
| 119 | 983, 982, 981, 980, 979, 978, 977, 976, 975, 974, |
| 120 | 973, 972, 971, 970, 969, 968, 967, 966, 965, 964, |
| 121 | 963, 962, 961, 960, 959, 958, 957, 956, 955, 954, |
| 122 | 953, 952, 951, 950, 949, 948, 947, 946, 945, 944, |
| 123 | 943, 942, 941, 940, 939, 938, 937, 936, 935, 934, |
| 124 | 933, 932, 931, 930, 929, 928, 927, 926, 925, 924, |
| 125 | 923, 922, 921, 920, 919, 918, 917, 916, 915, 914, |
| 126 | 913, 912, 911, 910, 909, 908, 907, 906, 905, 904, |
| 127 | 903, 902, 901, 900, 899, 898, 897, 896, 895, 894, |
| 128 | 893, 892, 891, 890, 889, 888, 887, 886, 885, 884, |
| 129 | 883, 882, 881, 880, 879, 878, 877, 876, 875, 874, |
| 130 | 873, 872, 871, 870, 869, 868, 867, 866, 865, 864, |
| 131 | 863, 862, 861, 860, 859, 858, 857, 856, 855, 854, |
| 132 | 853, 852, 851, 850, 849, 848, 847, 846, 845, 844, |
| 133 | 843, 842, 841, 840, 839, 838, 837, 836, 835, 834, |
| 134 | 833, 832, 831, 830, 829, 828, 827, 826, 825, 824, |
| 135 | 823, 822, 821, 820, 819, 818, 817, 816, 815, 814, |
| 136 | 813, 812, 811, 810, 809, 808, 807, 806, 805, 804, |
| 137 | 803, 802, 801, 800, 799, 798, 797, 796, 795, 794, |
| 138 | 793, 792, 791, 790, 789, 788, 787, 786, 785, 784, |
| 139 | 783, 782, 781, 780, 779, 778, 777, 776, 775, 774, |
| 140 | 773, 772, 771, 770, 769, 768, 767, 766, 765, 764, |
| 141 | 763, 762, 761, 760, 759, 758, 757, 756, 755, 754, |
| 142 | 753, 752, 751, 750, 749, 748, 747, 746, 745, 744, |
| 143 | 743, 742, 741, 740, 739, 738, 737, 736, 735, 734, |
| 144 | 733, 732, 731, 730, 729, 728, 727, 726, 725, 724, |
| 145 | 723, 722, 721, 720, 719, 718, 717, 716, 715, 714, |
| 146 | 713, 712, 711, 710, 709, 708, 707, 706, 705, 704, |
| 147 | 703, 702, 701, 700, 699, 698, 697, 696, 695, 694, |
| 148 | 693, 692, 691, 690, 689, 688, 687, 686, 685, 684, |
| 149 | 683, 682, 681, 680, 679, 678, 677, 676, 675, 674, |
| 150 | 673, 672, 671, 670, 669, 668, 667, 666, 665, 664, |
| 151 | 663, 662, 661, 660, 659, 658, 657, 656, 655, 654, |
| 152 | 653, 652, 651, 650, 649, 648, 647, 646, 645, 644, |
| 153 | 643, 642, 641, 640, 639, 638, 637, 636, 635, 634, |
| 154 | 633, 632, 631, 630, 629, 628, 627, 626, 625, 624, |
| 155 | 623, 622, 621, 620, 619, 618, 617, 616, 615, 614, |
| 156 | 613, 612, 611, 610, 609, 608, 607, 606, 605, 604, |
| 157 | 603, 602, 601, 600, 599, 598, 597, 596, 595, 594, |
| 158 | 593, 592, 591, 590, 589, 588, 587, 586, 585, 584, |
| 159 | 583, 582, 581, 580, 579, 578, 577, 576, 575, 574, |
| 160 | 573, 572, 571, 570, 569, 568, 567, 566, 565, 564, |
| 161 | 563, 562, 561, 560, 559, 558, 557, 556, 555, 554, |
| 162 | 553, 552, 551, 550, 549, 548, 547, 546, 545, 544, |
| 163 | 543, 542, 541, 540, 539, 538, 537, 536, 535, 534, |
| 164 | 533, 532, 531, 530, 529, 528, 527, 526, 525, 524, |
| 165 | 523, 522, 521, 520, 519, 518, 517, 516, 515, 514, |
| 166 | 513, 512, 63, 62, 61, 60, 59, 58, 57, 56, |
| 167 | 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, |
| 168 | 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, |
| 169 | 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, |
| 170 | 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, |
| 171 | 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, |
| 172 | 5, 4, 3, 2, 1, 1, 1, 1, 0, 0, |
| 173 | 0, |
| 174 | 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, |
| 175 | 2, 3, 0, 1, 2, 3, 4, 5, 6, 7, |
| 176 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 177 | 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, |
| 178 | 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, |
| 179 | 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, |
| 180 | 24, 25, 26, 27, 28, 29, 30, 31, 0, 1, |
| 181 | 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, |
| 182 | 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, |
| 183 | 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 184 | 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, |
| 185 | 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, |
| 186 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, |
| 187 | 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, |
| 188 | 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, |
| 189 | 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, |
| 190 | 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, |
| 191 | 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, |
| 192 | 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| 193 | 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, |
| 194 | 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, |
| 195 | 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, |
| 196 | 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, |
| 197 | 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, |
| 198 | 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, |
| 199 | 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, |
| 200 | 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, |
| 201 | 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, |
| 202 | 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, |
| 203 | 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, |
| 204 | 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, |
| 205 | 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, |
| 206 | 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, |
| 207 | 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, |
| 208 | 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, |
| 209 | 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, |
| 210 | 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, |
| 211 | 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, |
| 212 | 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, |
| 213 | 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, |
| 214 | 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, |
| 215 | 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, |
| 216 | 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, |
| 217 | 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, |
| 218 | 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, |
| 219 | 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, |
| 220 | 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, |
| 221 | 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, |
| 222 | 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, |
| 223 | 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, |
| 224 | 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, |
| 225 | 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, |
| 226 | 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, |
| 227 | 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, |
| 228 | 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, |
| 229 | 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, |
| 230 | 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, |
| 231 | 502, 503, 504, 505, 506, 507, 508, 509, 510, 511 |
| 232 | }; |
| 233 | |
| 234 | /*The first DCT coefficient that both has a smaller magnitude and gets coded |
| 235 | with a different token.*/ |
| 236 | static const ogg_int16_t OC_DCT_TRELLIS_ALT_VALUE[1161]={ |
| 237 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 238 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 239 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 240 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 241 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 242 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 243 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 244 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 245 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 246 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 247 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 248 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 249 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 250 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 251 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 252 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 253 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 254 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 255 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 256 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 257 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 258 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 259 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 260 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 261 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 262 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 263 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 264 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 265 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 266 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 267 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 268 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 269 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 270 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 271 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 272 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 273 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 274 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 275 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 276 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 277 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 278 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 279 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 280 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 281 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 282 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 283 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 284 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 285 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 286 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 287 | -68, -68, -68, -68, -68, -68, -68, -68, -68, -68, |
| 288 | -68, -68, -36, -36, -36, -36, -36, -36, -36, -36, |
| 289 | -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, |
| 290 | -36, -36, -36, -36, -36, -36, -36, -36, -36, -36, |
| 291 | -36, -36, -36, -36, -20, -20, -20, -20, -20, -20, |
| 292 | -20, -20, -20, -20, -20, -20, -20, -20, -20, -20, |
| 293 | -12, -12, -12, -12, -12, -12, -12, -12, -8, -8, |
| 294 | -8, -8, -6, -6, -5, -4, -3, -2, -1, 0, |
| 295 | 0, |
| 296 | 0, 1, 2, 3, 4, 5, 6, 6, 8, 8, |
| 297 | 8, 8, 12, 12, 12, 12, 12, 12, 12, 12, |
| 298 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, |
| 299 | 20, 20, 20, 20, 20, 20, 36, 36, 36, 36, |
| 300 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 301 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 302 | 36, 36, 36, 36, 36, 36, 36, 36, 68, 68, |
| 303 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 304 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 305 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 306 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 307 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 308 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 309 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 310 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 311 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 312 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 313 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 314 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 315 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 316 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 317 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 318 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 319 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 320 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 321 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 322 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 323 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 324 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 325 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 326 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 327 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 328 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 329 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 330 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 331 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 332 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 333 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 334 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 335 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 336 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 337 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 338 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 339 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 340 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 341 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 342 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 343 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 344 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 345 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 346 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 347 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 348 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 349 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 350 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 351 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 352 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, |
| 353 | 68, 68, 68, 68, 68, 68, 68, 68, 68, 68 |
| 354 | }; |
| 355 | |
| 356 | #define OC_DCT_VALUE_TOKEN_PTR (OC_DCT_VALUE_TOKEN+580) |
| 357 | #define OC_DCT_VALUE_EB_PTR (OC_DCT_VALUE_EB+580) |
| 358 | #define OC_DCT_TRELLIS_ALT_VALUE_PTR (OC_DCT_TRELLIS_ALT_VALUE+580) |
| 359 | |
| 360 | /*Some tables for fast construction of combo tokens.*/ |
| 361 | |
| 362 | static const unsigned char OC_DCT_RUN_CAT1_TOKEN[17]={ |
| 363 | 23,24,25,26,27,28,28,28,28,29,29,29,29,29,29,29,29 |
| 364 | }; |
| 365 | |
| 366 | static const unsigned char OC_DCT_RUN_CAT1_EB[17][2]={ |
| 367 | {0,1},{0,1},{0, 1},{0, 1},{0, 1},{0, 4},{1, 5},{2, 6},{3,7}, |
| 368 | {0,8},{1,9},{2,10},{3,11},{4,12},{5,13},{6,14},{7,15} |
| 369 | }; |
| 370 | |
| 371 | static const unsigned char OC_DCT_RUN_CAT2_EB[3][2][2]={ |
| 372 | { {0,1},{2,3} },{ {0,2},{4,6} },{ {1,3},{5,7} } |
| 373 | }; |
| 374 | |
| 375 | /*Token logging to allow a few fragments of efficient rollback. |
| 376 | Late SKIP analysis is tied up in the tokenization process, so we need to be |
| 377 | able to undo a fragment's tokens on a whim.*/ |
| 378 | |
| 379 | static const unsigned char OC_ZZI_HUFF_OFFSET[64]={ |
| 380 | 0,16,16,16,16,16,32,32, |
| 381 | 32,32,32,32,32,32,32,48, |
| 382 | 48,48,48,48,48,48,48,48, |
| 383 | 48,48,48,48,64,64,64,64, |
| 384 | 64,64,64,64,64,64,64,64, |
| 385 | 64,64,64,64,64,64,64,64, |
| 386 | 64,64,64,64,64,64,64,64 |
| 387 | }; |
| 388 | |
| 389 | static int oc_token_bits(oc_enc_ctx *_enc,int _huffi,int _zzi,int _token){ |
| 390 | return _enc->huff_codes[_huffi+OC_ZZI_HUFF_OFFSET[_zzi]][_token].nbits |
| 391 | +OC_DCT_TOKEN_EXTRA_BITS[_token]; |
| 392 | } |
| 393 | |
| 394 | static void oc_enc_tokenlog_checkpoint(oc_enc_ctx *_enc, |
| 395 | oc_token_checkpoint *_cp,int _pli,int _zzi){ |
| 396 | _cp->pli=_pli; |
| 397 | _cp->zzi=_zzi; |
| 398 | _cp->eob_run=_enc->eob_run[_pli][_zzi]; |
| 399 | _cp->ndct_tokens=_enc->ndct_tokens[_pli][_zzi]; |
| 400 | } |
| 401 | |
| 402 | void oc_enc_tokenlog_rollback(oc_enc_ctx *_enc, |
| 403 | const oc_token_checkpoint *_stack,int _n){ |
| 404 | int i; |
| 405 | for(i=_n;i-->0;){ |
| 406 | int pli; |
| 407 | int zzi; |
| 408 | pli=_stack[i].pli; |
| 409 | zzi=_stack[i].zzi; |
| 410 | _enc->eob_run[pli][zzi]=_stack[i].eob_run; |
| 411 | _enc->ndct_tokens[pli][zzi]=_stack[i].ndct_tokens; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | static void oc_enc_token_log(oc_enc_ctx *_enc, |
| 416 | int _pli,int _zzi,int _token,int _eb){ |
| 417 | ptrdiff_t ti; |
| 418 | ti=_enc->ndct_tokens[_pli][_zzi]++; |
| 419 | _enc->dct_tokens[_pli][_zzi][ti]=(unsigned char)_token; |
| 420 | _enc->extra_bits[_pli][_zzi][ti]=(ogg_uint16_t)_eb; |
| 421 | } |
| 422 | |
| 423 | static void oc_enc_eob_log(oc_enc_ctx *_enc, |
| 424 | int _pli,int _zzi,int _run_count){ |
| 425 | int token; |
| 426 | int eb; |
| 427 | token=oc_make_eob_token_full(_run_count,&eb); |
| 428 | oc_enc_token_log(_enc,_pli,_zzi,token,eb); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | void oc_enc_tokenize_start(oc_enc_ctx *_enc){ |
| 433 | memset(_enc->ndct_tokens,0,sizeof(_enc->ndct_tokens)); |
| 434 | memset(_enc->eob_run,0,sizeof(_enc->eob_run)); |
| 435 | memset(_enc->dct_token_offs,0,sizeof(_enc->dct_token_offs)); |
| 436 | memset(_enc->dc_pred_last,0,sizeof(_enc->dc_pred_last)); |
| 437 | } |
| 438 | |
| 439 | typedef struct oc_quant_token oc_quant_token; |
| 440 | |
| 441 | /*A single node in the Viterbi trellis. |
| 442 | We maintain up to 2 of these per coefficient: |
| 443 | - A token to code if the value is zero (EOB, zero run, or combo token). |
| 444 | - A token to code if the value is not zero (DCT value token).*/ |
| 445 | struct oc_quant_token{ |
| 446 | unsigned char next; |
| 447 | signed char token; |
| 448 | ogg_int16_t eb; |
| 449 | ogg_uint32_t cost; |
| 450 | int bits; |
| 451 | int qc; |
| 452 | }; |
| 453 | |
| 454 | /*Tokenizes the AC coefficients, possibly adjusting the quantization, and then |
| 455 | dequantizes and de-zig-zags the result. |
| 456 | The AC coefficients of _idct must be pre-initialized to zero.*/ |
| 457 | int oc_enc_tokenize_ac(oc_enc_ctx *_enc,int _pli,ptrdiff_t _fragi, |
| 458 | ogg_int16_t *_idct,const ogg_int16_t *_qdct, |
| 459 | const ogg_uint16_t *_dequant,const ogg_int16_t *_dct, |
| 460 | int _zzi,oc_token_checkpoint **_stack,int _lambda,int _acmin){ |
| 461 | oc_token_checkpoint *stack; |
| 462 | ogg_int64_t zflags; |
| 463 | ogg_int64_t nzflags; |
| 464 | ogg_int64_t best_flags; |
| 465 | ogg_uint32_t d2_accum[64]; |
| 466 | oc_quant_token tokens[64][2]; |
| 467 | ogg_uint16_t *eob_run; |
| 468 | const unsigned char *dct_fzig_zag; |
| 469 | ogg_uint32_t cost; |
| 470 | int bits; |
| 471 | int eob; |
| 472 | int token; |
| 473 | int eb; |
| 474 | int next; |
| 475 | int huffi; |
| 476 | int zzi; |
| 477 | int ti; |
| 478 | int zzj; |
| 479 | int qc; |
| 480 | huffi=_enc->huff_idxs[_enc->state.frame_type][1][_pli+1>>1]; |
| 481 | eob_run=_enc->eob_run[_pli]; |
| 482 | memset(tokens[0],0,sizeof(tokens[0])); |
| 483 | best_flags=nzflags=0; |
| 484 | zflags=1; |
| 485 | d2_accum[0]=0; |
| 486 | zzj=64; |
| 487 | for(zzi=OC_MINI(_zzi,63);zzi>0;zzi--){ |
| 488 | ogg_uint32_t best_cost; |
| 489 | int best_bits=INT_MAX; |
| 490 | int best_next=INT_MAX; |
| 491 | int best_token=INT_MAX; |
| 492 | int best_eb=INT_MAX; |
| 493 | int best_qc=INT_MAX; |
| 494 | ogg_uint32_t d2; |
| 495 | int dq; |
| 496 | int qc_m; |
| 497 | int e; |
| 498 | int c; |
| 499 | int s; |
| 500 | int tj; |
| 501 | qc=_qdct[zzi]; |
| 502 | s=-(qc<0); |
| 503 | qc_m=qc+s^s; |
| 504 | c=_dct[zzi]; |
| 505 | /*The hard case: try a zero run.*/ |
| 506 | if(qc_m<=1){ |
| 507 | ogg_uint32_t sum_d2; |
| 508 | int nzeros; |
| 509 | int dc_reserve; |
| 510 | if(!qc_m){ |
| 511 | /*Skip runs that are already quantized to zeros. |
| 512 | If we considered each zero coefficient in turn, we might |
| 513 | theoretically find a better way to partition long zero runs (e.g., |
| 514 | a run of > 17 zeros followed by a 1 might be better coded as a short |
| 515 | zero run followed by a combo token, rather than the longer zero |
| 516 | token followed by a 1 value token), but zeros are so common that |
| 517 | this becomes very computationally expensive (quadratic instead of |
| 518 | linear in the number of coefficients), for a marginal gain.*/ |
| 519 | while(zzi>1&&!_qdct[zzi-1])zzi--; |
| 520 | /*The distortion of coefficients originally quantized to zero is |
| 521 | treated as zero (since we'll never quantize them to anything else).*/ |
| 522 | d2=0; |
| 523 | } |
| 524 | else{ |
| 525 | d2=c*(ogg_int32_t)c; |
| 526 | c=c+s^s; |
| 527 | } |
| 528 | eob=eob_run[zzi]; |
| 529 | nzeros=zzj-zzi; |
| 530 | zzj&=63; |
| 531 | sum_d2=d2+d2_accum[zzj]; |
| 532 | d2_accum[zzi]=sum_d2; |
| 533 | /*We reserve 1 spot for combo run tokens that start in the 1st AC stack |
| 534 | to ensure they can be extended to include the DC coefficient if |
| 535 | necessary; this greatly simplifies stack-rewriting later on.*/ |
| 536 | dc_reserve=zzi+62>>6; |
| 537 | best_cost=0xFFFFFFFF; |
| 538 | for(;;){ |
| 539 | if(nzflags>>zzj&1){ |
| 540 | int val; |
| 541 | int val_s; |
| 542 | int zzk; |
| 543 | int tk; |
| 544 | next=tokens[zzj][1].next; |
| 545 | tk=next&1; |
| 546 | zzk=next>>1; |
| 547 | /*Try a pure zero run to this point.*/ |
| 548 | token=OC_DCT_SHORT_ZRL_TOKEN+(nzeros+55>>6); |
| 549 | bits=oc_token_bits(_enc,huffi,zzi,token); |
| 550 | d2=sum_d2-d2_accum[zzj]; |
| 551 | cost=d2+_lambda*bits+tokens[zzj][1].cost; |
| 552 | if(cost<=best_cost){ |
| 553 | best_next=(zzj<<1)+1; |
| 554 | best_token=token; |
| 555 | best_eb=nzeros-1; |
| 556 | best_cost=cost; |
| 557 | best_bits=bits+tokens[zzj][1].bits; |
| 558 | best_qc=0; |
| 559 | } |
| 560 | if(nzeros<17+dc_reserve){ |
| 561 | val=_qdct[zzj]; |
| 562 | val_s=-(val<0); |
| 563 | val=val+val_s^val_s; |
| 564 | if(val<=2){ |
| 565 | /*Try a +/- 1 combo token.*/ |
| 566 | token=OC_DCT_RUN_CAT1_TOKEN[nzeros-1]; |
| 567 | eb=OC_DCT_RUN_CAT1_EB[nzeros-1][-val_s]; |
| 568 | e=_dct[zzj]-(_dequant[zzj]+val_s^val_s); |
| 569 | d2=e*(ogg_int32_t)e+sum_d2-d2_accum[zzj]; |
| 570 | bits=oc_token_bits(_enc,huffi,zzi,token); |
| 571 | cost=d2+_lambda*bits+tokens[zzk][tk].cost; |
| 572 | if(cost<=best_cost){ |
| 573 | best_next=next; |
| 574 | best_token=token; |
| 575 | best_eb=eb; |
| 576 | best_cost=cost; |
| 577 | best_bits=bits+tokens[zzk][tk].bits; |
| 578 | best_qc=1+val_s^val_s; |
| 579 | } |
| 580 | } |
| 581 | if(nzeros<3+dc_reserve&&2<=val&&val<=4){ |
| 582 | int sval; |
| 583 | /*Try a +/- 2/3 combo token.*/ |
| 584 | token=OC_DCT_RUN_CAT2A+(nzeros>>1); |
| 585 | bits=oc_token_bits(_enc,huffi,zzi,token); |
| 586 | val=2+(val>2); |
| 587 | sval=val+val_s^val_s; |
| 588 | e=_dct[zzj]-_dequant[zzj]*sval; |
| 589 | d2=e*(ogg_int32_t)e+sum_d2-d2_accum[zzj]; |
| 590 | cost=d2+_lambda*bits+tokens[zzk][tk].cost; |
| 591 | if(cost<=best_cost){ |
| 592 | best_cost=cost; |
| 593 | best_bits=bits+tokens[zzk][tk].bits; |
| 594 | best_next=next; |
| 595 | best_token=token; |
| 596 | best_eb=OC_DCT_RUN_CAT2_EB[nzeros-1][-val_s][val-2]; |
| 597 | best_qc=sval; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | /*zzj can't be coded as a zero, so stop trying to extend the run.*/ |
| 602 | if(!(zflags>>zzj&1))break; |
| 603 | } |
| 604 | /*We could try to consider _all_ potentially non-zero coefficients, but |
| 605 | if we already found a bunch of them not worth coding, it's fairly |
| 606 | unlikely they would now be worth coding from this position; skipping |
| 607 | them saves a lot of work.*/ |
| 608 | zzj=(tokens[zzj][0].next>>1)-(tokens[zzj][0].qc!=0)&63; |
| 609 | if(zzj==0){ |
| 610 | /*We made it all the way to the end of the block; try an EOB token.*/ |
| 611 | if(eob<4095){ |
| 612 | bits=oc_token_bits(_enc,huffi,zzi,oc_make_eob_token(eob+1)) |
| 613 | -(eob>0?oc_token_bits(_enc,huffi,zzi,oc_make_eob_token(eob)):0); |
| 614 | } |
| 615 | else bits=oc_token_bits(_enc,huffi,zzi,OC_DCT_EOB1_TOKEN); |
| 616 | cost=sum_d2+bits*_lambda; |
| 617 | /*If the best route so far is still a pure zero run to the end of the |
| 618 | block, force coding it as an EOB. |
| 619 | Even if it's not optimal for this block, it has a good chance of |
| 620 | getting combined with an EOB token from subsequent blocks, saving |
| 621 | bits overall.*/ |
| 622 | if(cost<=best_cost||best_token<=OC_DCT_ZRL_TOKEN&&zzi+best_eb==63){ |
| 623 | best_next=0; |
| 624 | /*This token is just a marker; in reality we may not emit any |
| 625 | tokens, but update eob_run[] instead.*/ |
| 626 | best_token=OC_DCT_EOB1_TOKEN; |
| 627 | best_eb=0; |
| 628 | best_cost=cost; |
| 629 | best_bits=bits; |
| 630 | best_qc=0; |
| 631 | } |
| 632 | break; |
| 633 | } |
| 634 | nzeros=zzj-zzi; |
| 635 | } |
| 636 | tokens[zzi][0].next=(unsigned char)best_next; |
| 637 | tokens[zzi][0].token=(signed char)best_token; |
| 638 | tokens[zzi][0].eb=(ogg_int16_t)best_eb; |
| 639 | tokens[zzi][0].cost=best_cost; |
| 640 | tokens[zzi][0].bits=best_bits; |
| 641 | tokens[zzi][0].qc=best_qc; |
| 642 | zflags|=(ogg_int64_t)1<<zzi; |
| 643 | if(qc_m){ |
| 644 | dq=_dequant[zzi]; |
| 645 | if(zzi<_acmin)_lambda=0; |
| 646 | e=dq-c; |
| 647 | d2=e*(ogg_int32_t)e; |
| 648 | token=OC_ONE_TOKEN-s; |
| 649 | bits=oc_token_bits(_enc,huffi,zzi,token); |
| 650 | zzj=zzi+1&63; |
| 651 | tj=best_flags>>zzj&1; |
| 652 | next=(zzj<<1)+tj; |
| 653 | tokens[zzi][1].next=(unsigned char)next; |
| 654 | tokens[zzi][1].token=(signed char)token; |
| 655 | tokens[zzi][1].eb=0; |
| 656 | tokens[zzi][1].cost=d2+_lambda*bits+tokens[zzj][tj].cost; |
| 657 | tokens[zzi][1].bits=bits+tokens[zzj][tj].bits; |
| 658 | tokens[zzi][1].qc=1+s^s; |
| 659 | nzflags|=(ogg_int64_t)1<<zzi; |
| 660 | best_flags|= |
| 661 | (ogg_int64_t)(tokens[zzi][1].cost<tokens[zzi][0].cost)<<zzi; |
| 662 | } |
| 663 | } |
| 664 | else{ |
| 665 | int alt_qc; |
| 666 | eob=eob_run[zzi]; |
| 667 | if(zzi<_acmin)_lambda=0; |
| 668 | dq=_dequant[zzi]; |
| 669 | /*No zero run can extend past this point.*/ |
| 670 | d2_accum[zzi]=0; |
| 671 | e=qc*dq-c; |
| 672 | d2=e*(ogg_int32_t)e; |
| 673 | best_token=*(OC_DCT_VALUE_TOKEN_PTR+qc); |
| 674 | best_bits=oc_token_bits(_enc,huffi,zzi,best_token); |
| 675 | best_cost=d2+_lambda*best_bits; |
| 676 | alt_qc=*(OC_DCT_TRELLIS_ALT_VALUE_PTR+qc); |
| 677 | e=alt_qc*dq-c; |
| 678 | d2=e*(ogg_int32_t)e; |
| 679 | token=*(OC_DCT_VALUE_TOKEN_PTR+alt_qc); |
| 680 | bits=oc_token_bits(_enc,huffi,zzi,token); |
| 681 | cost=d2+_lambda*bits; |
| 682 | if(cost<best_cost){ |
| 683 | best_token=token; |
| 684 | best_bits=bits; |
| 685 | best_cost=cost; |
| 686 | qc=alt_qc; |
| 687 | } |
| 688 | zzj=zzi+1&63; |
| 689 | tj=best_flags>>zzj&1; |
| 690 | next=(zzj<<1)+tj; |
| 691 | tokens[zzi][1].next=(unsigned char)next; |
| 692 | tokens[zzi][1].token=(signed char)best_token; |
| 693 | tokens[zzi][1].eb=*(OC_DCT_VALUE_EB_PTR+qc); |
| 694 | tokens[zzi][1].cost=best_cost+tokens[zzj][tj].cost; |
| 695 | tokens[zzi][1].bits=best_bits+tokens[zzj][tj].bits; |
| 696 | tokens[zzi][1].qc=qc; |
| 697 | nzflags|=(ogg_int64_t)1<<zzi; |
| 698 | best_flags|=(ogg_int64_t)1<<zzi; |
| 699 | } |
| 700 | zzj=zzi; |
| 701 | } |
| 702 | /*Emit the tokens from the best path through the trellis.*/ |
| 703 | stack=*_stack; |
| 704 | dct_fzig_zag=_enc->state.opt_data.dct_fzig_zag; |
| 705 | zzi=1; |
| 706 | ti=best_flags>>1&1; |
| 707 | bits=tokens[zzi][ti].bits; |
| 708 | do{ |
| 709 | oc_enc_tokenlog_checkpoint(_enc,stack++,_pli,zzi); |
| 710 | eob=eob_run[zzi]; |
| 711 | if(tokens[zzi][ti].token<OC_NDCT_EOB_TOKEN_MAX){ |
| 712 | if(++eob>=4095){ |
| 713 | oc_enc_token_log(_enc,_pli,zzi,OC_DCT_REPEAT_RUN3_TOKEN,eob); |
| 714 | eob=0; |
| 715 | } |
| 716 | eob_run[zzi]=eob; |
| 717 | /*We don't include the actual EOB cost for this block in the return value. |
| 718 | It is very likely to eventually be spread over several blocks, and |
| 719 | including it more harshly penalizes the first few blocks in a long EOB |
| 720 | run. |
| 721 | Omitting it here gives a small PSNR and SSIM gain.*/ |
| 722 | bits-=tokens[zzi][ti].bits; |
| 723 | zzi=_zzi; |
| 724 | break; |
| 725 | } |
| 726 | /*Emit pending EOB run if any.*/ |
| 727 | if(eob>0){ |
| 728 | oc_enc_eob_log(_enc,_pli,zzi,eob); |
| 729 | eob_run[zzi]=0; |
| 730 | } |
| 731 | oc_enc_token_log(_enc,_pli,zzi,tokens[zzi][ti].token,tokens[zzi][ti].eb); |
| 732 | next=tokens[zzi][ti].next; |
| 733 | qc=tokens[zzi][ti].qc; |
| 734 | zzj=(next>>1)-1&63; |
| 735 | /*TODO: It may be worth saving the dequantized coefficient in the trellis |
| 736 | above; we had to compute it to measure the error anyway.*/ |
| 737 | _idct[dct_fzig_zag[zzj]]=(ogg_int16_t)(qc*(int)_dequant[zzj]); |
| 738 | zzi=next>>1; |
| 739 | ti=next&1; |
| 740 | } |
| 741 | while(zzi); |
| 742 | *_stack=stack; |
| 743 | return bits; |
| 744 | } |
| 745 | |
| 746 | /*Simplistic R/D tokenizer. |
| 747 | The AC coefficients of _idct must be pre-initialized to zero. |
| 748 | This could be made more accurate by using more sophisticated |
| 749 | rate predictions for zeros. |
| 750 | It could be made faster by switching from R/D decisions to static |
| 751 | lambda-derived rounding biases.*/ |
| 752 | int oc_enc_tokenize_ac_fast(oc_enc_ctx *_enc,int _pli,ptrdiff_t _fragi, |
| 753 | ogg_int16_t *_idct,const ogg_int16_t *_qdct, |
| 754 | const ogg_uint16_t *_dequant,const ogg_int16_t *_dct, |
| 755 | int _zzi,oc_token_checkpoint **_stack,int _lambda,int _acmin){ |
| 756 | const unsigned char *dct_fzig_zag; |
| 757 | ogg_uint16_t *eob_run; |
| 758 | oc_token_checkpoint *stack; |
| 759 | int huffi; |
| 760 | int zzi; |
| 761 | int zzj; |
| 762 | int zzk; |
| 763 | int total_bits; |
| 764 | int zr[4]; |
| 765 | stack=*_stack; |
| 766 | total_bits=0; |
| 767 | /*The apparent bit-cost of coding a zero from observing the trellis |
| 768 | quantizer is pre-combined with lambda. |
| 769 | Four predictive cases are considered: the last optimized value is zero (+2) |
| 770 | or non-zero and the non-optimized value is zero (+1) or non-zero.*/ |
| 771 | zr[0]=3*_lambda>>1; |
| 772 | zr[1]=_lambda; |
| 773 | zr[2]=4*_lambda; |
| 774 | zr[3]=7*_lambda>>1; |
| 775 | eob_run=_enc->eob_run[_pli]; |
| 776 | dct_fzig_zag=_enc->state.opt_data.dct_fzig_zag; |
| 777 | huffi=_enc->huff_idxs[_enc->state.frame_type][1][_pli+1>>1]; |
| 778 | for(zzj=zzi=1;zzj<_zzi&&!_qdct[zzj];zzj++); |
| 779 | while(zzj<_zzi){ |
| 780 | int v; |
| 781 | int d0; |
| 782 | int d1; |
| 783 | int sign; |
| 784 | int k; |
| 785 | int eob; |
| 786 | int dq0; |
| 787 | int dq1; |
| 788 | int dd0; |
| 789 | int dd1; |
| 790 | int next_zero; |
| 791 | int eob_bits; |
| 792 | int dct_fzig_zzj; |
| 793 | dct_fzig_zzj=dct_fzig_zag[zzj]; |
| 794 | v=_dct[zzj]; |
| 795 | d0=_qdct[zzj]; |
| 796 | eob=eob_run[zzi]; |
| 797 | for(zzk=zzj+1;zzk<_zzi&&!_qdct[zzk];zzk++); |
| 798 | next_zero=zzk-zzj+62>>6; |
| 799 | dq0=d0*_dequant[zzj]; |
| 800 | dd0=dq0-v; |
| 801 | dd0*=dd0; |
| 802 | sign=-(d0<0); |
| 803 | k=d0+sign^sign; |
| 804 | d1=(k-(zzj>_acmin))+sign^sign; |
| 805 | dq1=d1*_dequant[zzj]; |
| 806 | dd1=dq1-v; |
| 807 | dd1*=dd1; |
| 808 | /*The cost of ending an eob run is included when the alternative is to |
| 809 | extend this eob run. |
| 810 | A per qi/zzi weight would probably be useful. |
| 811 | Including it in the overall tokenization cost was not helpful. |
| 812 | The same is true at the far end of the zero run plus token case.*/ |
| 813 | if(eob>0&&d1==0&&zzk==_zzi){ |
| 814 | eob_bits=oc_token_bits(_enc,huffi,zzi,OC_DCT_EOB1_TOKEN); |
| 815 | } |
| 816 | else eob_bits=0; |
| 817 | if(zzj==zzi){ |
| 818 | /*No active zero run.*/ |
| 819 | int best_token; |
| 820 | int best_eb; |
| 821 | int token; |
| 822 | int best_bits; |
| 823 | int bits; |
| 824 | int cost; |
| 825 | best_token=*(OC_DCT_VALUE_TOKEN_PTR+d0); |
| 826 | best_bits=oc_token_bits(_enc,huffi,zzi,best_token); |
| 827 | if(d1!=0){ |
| 828 | token=*(OC_DCT_VALUE_TOKEN_PTR+d1); |
| 829 | bits=oc_token_bits(_enc,huffi,zzi,token); |
| 830 | cost=dd1+(bits+eob_bits)*_lambda; |
| 831 | } |
| 832 | else{ |
| 833 | token=bits=0; |
| 834 | cost=dd1+zr[next_zero]; |
| 835 | } |
| 836 | if((dd0+(best_bits+eob_bits)*_lambda)>cost){ |
| 837 | _idct[dct_fzig_zzj]=dq1; |
| 838 | if(d1==0){ |
| 839 | zzj=zzk; |
| 840 | continue; |
| 841 | } |
| 842 | best_bits=bits; |
| 843 | best_token=token; |
| 844 | best_eb=*(OC_DCT_VALUE_EB_PTR+d1); |
| 845 | } |
| 846 | else{ |
| 847 | best_eb=*(OC_DCT_VALUE_EB_PTR+d0); |
| 848 | _idct[dct_fzig_zzj]=dq0; |
| 849 | } |
| 850 | oc_enc_tokenlog_checkpoint(_enc,stack++,_pli,zzi); |
| 851 | if(eob>0){ |
| 852 | oc_enc_eob_log(_enc,_pli,zzi,eob); |
| 853 | eob_run[zzi]=0; |
| 854 | } |
| 855 | oc_enc_token_log(_enc,_pli,zzi,best_token,best_eb); |
| 856 | total_bits+=best_bits; |
| 857 | } |
| 858 | else{ |
| 859 | int d; |
| 860 | int dc_reserve; |
| 861 | int best_token; |
| 862 | int best_eb; |
| 863 | int best_bits; |
| 864 | int best_cost; |
| 865 | int best_bits1; |
| 866 | int best_token1; |
| 867 | int best_eb1; |
| 868 | int zr_bits; |
| 869 | int eob2; |
| 870 | int eob_bits2; |
| 871 | int bits; |
| 872 | int token; |
| 873 | int nzeros; |
| 874 | nzeros=zzj-zzi; |
| 875 | dc_reserve=zzi+62>>6; |
| 876 | /*A zero run, followed by the value alone.*/ |
| 877 | best_token=best_token1=OC_DCT_SHORT_ZRL_TOKEN+(nzeros+55>>6); |
| 878 | best_eb=best_eb1=nzeros-1; |
| 879 | eob2=eob_run[zzj]; |
| 880 | eob_bits2=eob2>0?oc_token_bits(_enc,huffi,zzj,OC_DCT_EOB1_TOKEN):0; |
| 881 | zr_bits=oc_token_bits(_enc,huffi,zzi,best_token)+eob_bits2; |
| 882 | best_bits=zr_bits |
| 883 | +oc_token_bits(_enc,huffi,zzj,*(OC_DCT_VALUE_TOKEN_PTR+d0)); |
| 884 | d=d0; |
| 885 | best_bits1=0; |
| 886 | if(d1!=0){ |
| 887 | best_bits1=zr_bits |
| 888 | +oc_token_bits(_enc,huffi,zzj,*(OC_DCT_VALUE_TOKEN_PTR+d1)); |
| 889 | } |
| 890 | if(nzeros<17+dc_reserve){ |
| 891 | if(k<=2){ |
| 892 | /*+/- 1 combo token.*/ |
| 893 | token=OC_DCT_RUN_CAT1_TOKEN[nzeros-1]; |
| 894 | bits=oc_token_bits(_enc,huffi,zzi,token); |
| 895 | if(k==2&&bits<=best_bits1){ |
| 896 | best_bits1=bits; |
| 897 | best_token1=token; |
| 898 | best_eb1=OC_DCT_RUN_CAT1_EB[nzeros-1][-sign]; |
| 899 | } |
| 900 | if(k==1&&bits<=best_bits){ |
| 901 | best_bits=bits; |
| 902 | best_token=token; |
| 903 | best_eb=OC_DCT_RUN_CAT1_EB[nzeros-1][-sign]; |
| 904 | } |
| 905 | } |
| 906 | if(nzeros<3+dc_reserve&&2<=k&&k<=4){ |
| 907 | /*+/- 2/3 combo token.*/ |
| 908 | token=OC_DCT_RUN_CAT2A+(nzeros>>1); |
| 909 | bits=oc_token_bits(_enc,huffi,zzi,token); |
| 910 | if(k==4&&bits<=best_bits1){ |
| 911 | best_bits1=bits; |
| 912 | best_token1=token; |
| 913 | best_eb1=OC_DCT_RUN_CAT2_EB[nzeros-1][-sign][1]; |
| 914 | } |
| 915 | if(k!=4&&bits<=best_bits){ |
| 916 | best_bits=bits; |
| 917 | best_token=token; |
| 918 | best_eb=OC_DCT_RUN_CAT2_EB[nzeros-1][-sign][k-2]; |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | best_cost=dd0+(best_bits+eob_bits)*_lambda; |
| 923 | if(d1==0&&(dd1+zr[2+next_zero])<=best_cost){ |
| 924 | zzj=zzk; |
| 925 | continue; |
| 926 | } |
| 927 | if(d1!=0&&dd1+(best_bits1+eob_bits)*_lambda<best_cost){ |
| 928 | best_bits=best_bits1; |
| 929 | best_token=best_token1; |
| 930 | best_eb=best_eb1; |
| 931 | d=d1; |
| 932 | _idct[dct_fzig_zzj]=dq1; |
| 933 | } |
| 934 | else _idct[dct_fzig_zzj]=dq0; |
| 935 | oc_enc_tokenlog_checkpoint(_enc,stack++,_pli,zzi); |
| 936 | if(eob){ |
| 937 | oc_enc_eob_log(_enc,_pli,zzi,eob); |
| 938 | eob_run[zzi]=0; |
| 939 | } |
| 940 | oc_enc_token_log(_enc,_pli,zzi,best_token,best_eb); |
| 941 | /*If a zero run won vs. the combo token we still need to code this |
| 942 | value.*/ |
| 943 | if(best_token<=OC_DCT_ZRL_TOKEN){ |
| 944 | oc_enc_tokenlog_checkpoint(_enc,stack++,_pli,zzj); |
| 945 | if(eob2){ |
| 946 | oc_enc_eob_log(_enc,_pli,zzj,eob2); |
| 947 | /*The cost of any EOB run we disrupted is ignored because doing so |
| 948 | improved PSNR/SSIM by a small amount.*/ |
| 949 | best_bits-=eob_bits2; |
| 950 | eob_run[zzj]=0; |
| 951 | } |
| 952 | oc_enc_token_log(_enc,_pli,zzj, |
| 953 | *(OC_DCT_VALUE_TOKEN_PTR+d),*(OC_DCT_VALUE_EB_PTR+d)); |
| 954 | } |
| 955 | total_bits+=best_bits; |
| 956 | } |
| 957 | zzi=zzj+1; |
| 958 | zzj=zzk; |
| 959 | } |
| 960 | /*Code an EOB run to complete this block. |
| 961 | The cost of the EOB run is not included in the total as explained in |
| 962 | in a comment in the trellis tokenizer above.*/ |
| 963 | if(zzi<64){ |
| 964 | int eob; |
| 965 | eob=eob_run[zzi]+1; |
| 966 | oc_enc_tokenlog_checkpoint(_enc,stack++,_pli,zzi); |
| 967 | if(eob>=4095){ |
| 968 | oc_enc_token_log(_enc,_pli,zzi,OC_DCT_REPEAT_RUN3_TOKEN,eob); |
| 969 | eob=0; |
| 970 | } |
| 971 | eob_run[zzi]=eob; |
| 972 | } |
| 973 | *_stack=stack; |
| 974 | return total_bits; |
| 975 | } |
| 976 | |
| 977 | void oc_enc_pred_dc_frag_rows(oc_enc_ctx *_enc, |
| 978 | int _pli,int _fragy0,int _frag_yend){ |
| 979 | const oc_fragment_plane *fplane; |
| 980 | const oc_fragment *frags; |
| 981 | ogg_int16_t *frag_dc; |
| 982 | ptrdiff_t fragi; |
| 983 | int *pred_last; |
| 984 | int nhfrags; |
| 985 | int fragx; |
| 986 | int fragy; |
| 987 | fplane=_enc->state.fplanes+_pli; |
| 988 | frags=_enc->state.frags; |
| 989 | frag_dc=_enc->frag_dc; |
| 990 | pred_last=_enc->dc_pred_last[_pli]; |
| 991 | nhfrags=fplane->nhfrags; |
| 992 | fragi=fplane->froffset+_fragy0*nhfrags; |
| 993 | for(fragy=_fragy0;fragy<_frag_yend;fragy++){ |
| 994 | if(fragy==0){ |
| 995 | /*For the first row, all of the cases reduce to just using the previous |
| 996 | predictor for the same reference frame.*/ |
| 997 | for(fragx=0;fragx<nhfrags;fragx++,fragi++){ |
| 998 | if(frags[fragi].coded){ |
| 999 | int refi; |
| 1000 | refi=frags[fragi].refi; |
| 1001 | frag_dc[fragi]=(ogg_int16_t)(frags[fragi].dc-pred_last[refi]); |
| 1002 | pred_last[refi]=frags[fragi].dc; |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | else{ |
| 1007 | const oc_fragment *u_frags; |
| 1008 | int l_ref; |
| 1009 | int ul_ref; |
| 1010 | int u_ref; |
| 1011 | u_frags=frags-nhfrags; |
| 1012 | l_ref=-1; |
| 1013 | ul_ref=-1; |
| 1014 | u_ref=u_frags[fragi].refi; |
| 1015 | for(fragx=0;fragx<nhfrags;fragx++,fragi++){ |
| 1016 | int ur_ref; |
| 1017 | if(fragx+1>=nhfrags)ur_ref=-1; |
| 1018 | else ur_ref=u_frags[fragi+1].refi; |
| 1019 | if(frags[fragi].coded){ |
| 1020 | int pred; |
| 1021 | int refi; |
| 1022 | refi=frags[fragi].refi; |
| 1023 | /*We break out a separate case based on which of our neighbors use |
| 1024 | the same reference frames. |
| 1025 | This is somewhat faster than trying to make a generic case which |
| 1026 | handles all of them, since it reduces lots of poorly predicted |
| 1027 | jumps to one switch statement, and also lets a number of the |
| 1028 | multiplications be optimized out by strength reduction.*/ |
| 1029 | switch((l_ref==refi)|(ul_ref==refi)<<1| |
| 1030 | (u_ref==refi)<<2|(ur_ref==refi)<<3){ |
| 1031 | default:pred=pred_last[refi];break; |
| 1032 | case 1: |
| 1033 | case 3:pred=frags[fragi-1].dc;break; |
| 1034 | case 2:pred=u_frags[fragi-1].dc;break; |
| 1035 | case 4: |
| 1036 | case 6: |
| 1037 | case 12:pred=u_frags[fragi].dc;break; |
| 1038 | case 5:pred=(frags[fragi-1].dc+u_frags[fragi].dc)/2;break; |
| 1039 | case 8:pred=u_frags[fragi+1].dc;break; |
| 1040 | case 9: |
| 1041 | case 11: |
| 1042 | case 13:{ |
| 1043 | pred=(75*frags[fragi-1].dc+53*u_frags[fragi+1].dc)/128; |
| 1044 | }break; |
| 1045 | case 10:pred=(u_frags[fragi-1].dc+u_frags[fragi+1].dc)/2;break; |
| 1046 | case 14:{ |
| 1047 | pred=(3*(u_frags[fragi-1].dc+u_frags[fragi+1].dc) |
| 1048 | +10*u_frags[fragi].dc)/16; |
| 1049 | }break; |
| 1050 | case 7: |
| 1051 | case 15:{ |
| 1052 | int p0; |
| 1053 | int p1; |
| 1054 | int p2; |
| 1055 | p0=frags[fragi-1].dc; |
| 1056 | p1=u_frags[fragi-1].dc; |
| 1057 | p2=u_frags[fragi].dc; |
| 1058 | pred=(29*(p0+p2)-26*p1)/32; |
| 1059 | if(abs(pred-p2)>128)pred=p2; |
| 1060 | else if(abs(pred-p0)>128)pred=p0; |
| 1061 | else if(abs(pred-p1)>128)pred=p1; |
| 1062 | }break; |
| 1063 | } |
| 1064 | frag_dc[fragi]=(ogg_int16_t)(frags[fragi].dc-pred); |
| 1065 | pred_last[refi]=frags[fragi].dc; |
| 1066 | l_ref=refi; |
| 1067 | } |
| 1068 | else l_ref=-1; |
| 1069 | ul_ref=u_ref; |
| 1070 | u_ref=ur_ref; |
| 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | void oc_enc_tokenize_dc_frag_list(oc_enc_ctx *_enc,int _pli, |
| 1077 | const ptrdiff_t *_coded_fragis,ptrdiff_t _ncoded_fragis, |
| 1078 | int _prev_ndct_tokens1,int _prev_eob_run1){ |
| 1079 | const ogg_int16_t *frag_dc; |
| 1080 | ptrdiff_t fragii; |
| 1081 | unsigned char *dct_tokens0; |
| 1082 | unsigned char *dct_tokens1; |
| 1083 | ogg_uint16_t *extra_bits0; |
| 1084 | ogg_uint16_t *extra_bits1; |
| 1085 | ptrdiff_t ti0; |
| 1086 | ptrdiff_t ti1r; |
| 1087 | ptrdiff_t ti1w; |
| 1088 | int eob_run0; |
| 1089 | int eob_run1; |
| 1090 | int neobs1; |
| 1091 | int token; |
| 1092 | int eb; |
| 1093 | int token1=OC_DCT_ZRL_TOKEN; |
| 1094 | int eb1=INT_MAX; |
| 1095 | /*Return immediately if there are no coded fragments; otherwise we'd flush |
| 1096 | any trailing EOB run into the AC 1 list and never read it back out.*/ |
| 1097 | if(_ncoded_fragis<=0)return; |
| 1098 | frag_dc=_enc->frag_dc; |
| 1099 | dct_tokens0=_enc->dct_tokens[_pli][0]; |
| 1100 | dct_tokens1=_enc->dct_tokens[_pli][1]; |
| 1101 | extra_bits0=_enc->extra_bits[_pli][0]; |
| 1102 | extra_bits1=_enc->extra_bits[_pli][1]; |
| 1103 | ti0=_enc->ndct_tokens[_pli][0]; |
| 1104 | ti1w=ti1r=_prev_ndct_tokens1; |
| 1105 | eob_run0=_enc->eob_run[_pli][0]; |
| 1106 | /*Flush any trailing EOB run for the 1st AC coefficient. |
| 1107 | This is needed to allow us to track tokens to the end of the list.*/ |
| 1108 | eob_run1=_enc->eob_run[_pli][1]; |
| 1109 | if(eob_run1>0)oc_enc_eob_log(_enc,_pli,1,eob_run1); |
| 1110 | /*If there was an active EOB run at the start of the 1st AC stack, read it |
| 1111 | in and decode it.*/ |
| 1112 | if(_prev_eob_run1>0){ |
| 1113 | token1=dct_tokens1[ti1r]; |
| 1114 | eb1=extra_bits1[ti1r]; |
| 1115 | ti1r++; |
| 1116 | eob_run1=oc_decode_eob_token(token1,eb1); |
| 1117 | /*Consume the portion of the run that came before these fragments.*/ |
| 1118 | neobs1=eob_run1-_prev_eob_run1; |
| 1119 | } |
| 1120 | else eob_run1=neobs1=0; |
| 1121 | for(fragii=0;fragii<_ncoded_fragis;fragii++){ |
| 1122 | int val; |
| 1123 | /*All tokens in the 1st AC coefficient stack are regenerated as the DC |
| 1124 | coefficients are produced. |
| 1125 | This can be done in-place; stack 1 cannot get larger.*/ |
| 1126 | if(!neobs1){ |
| 1127 | /*There's no active EOB run in stack 1; read the next token.*/ |
| 1128 | token1=dct_tokens1[ti1r]; |
| 1129 | eb1=extra_bits1[ti1r]; |
| 1130 | ti1r++; |
| 1131 | if(token1<OC_NDCT_EOB_TOKEN_MAX){ |
| 1132 | neobs1=oc_decode_eob_token(token1,eb1); |
| 1133 | /*It's an EOB run; add it to the current (inactive) one. |
| 1134 | Because we may have moved entries to stack 0, we may have an |
| 1135 | opportunity to merge two EOB runs in stack 1.*/ |
| 1136 | eob_run1+=neobs1; |
| 1137 | } |
| 1138 | } |
| 1139 | val=frag_dc[_coded_fragis[fragii]]; |
| 1140 | if(val){ |
| 1141 | /*There was a non-zero DC value, so there's no alteration to stack 1 |
| 1142 | for this fragment; just code the stack 0 token.*/ |
| 1143 | /*Flush any pending EOB run.*/ |
| 1144 | if(eob_run0>0){ |
| 1145 | token=oc_make_eob_token_full(eob_run0,&eb); |
| 1146 | dct_tokens0[ti0]=(unsigned char)token; |
| 1147 | extra_bits0[ti0]=(ogg_uint16_t)eb; |
| 1148 | ti0++; |
| 1149 | eob_run0=0; |
| 1150 | } |
| 1151 | dct_tokens0[ti0]=*(OC_DCT_VALUE_TOKEN_PTR+val); |
| 1152 | extra_bits0[ti0]=*(OC_DCT_VALUE_EB_PTR+val); |
| 1153 | ti0++; |
| 1154 | } |
| 1155 | else{ |
| 1156 | /*Zero DC value; that means the entry in stack 1 might need to be coded |
| 1157 | from stack 0. |
| 1158 | This requires a stack 1 fixup.*/ |
| 1159 | if(neobs1>0){ |
| 1160 | /*We're in the middle of an active EOB run in stack 1. |
| 1161 | Move it to stack 0.*/ |
| 1162 | if(++eob_run0>=4095){ |
| 1163 | dct_tokens0[ti0]=OC_DCT_REPEAT_RUN3_TOKEN; |
| 1164 | extra_bits0[ti0]=eob_run0; |
| 1165 | ti0++; |
| 1166 | eob_run0=0; |
| 1167 | } |
| 1168 | eob_run1--; |
| 1169 | } |
| 1170 | else{ |
| 1171 | /*No active EOB run in stack 1, so we can't extend one in stack 0. |
| 1172 | Flush it if we've got it.*/ |
| 1173 | if(eob_run0>0){ |
| 1174 | token=oc_make_eob_token_full(eob_run0,&eb); |
| 1175 | dct_tokens0[ti0]=(unsigned char)token; |
| 1176 | extra_bits0[ti0]=(ogg_uint16_t)eb; |
| 1177 | ti0++; |
| 1178 | eob_run0=0; |
| 1179 | } |
| 1180 | /*Stack 1 token is one of: a pure zero run token, a single |
| 1181 | coefficient token, or a zero run/coefficient combo token. |
| 1182 | A zero run token is expanded and moved to token stack 0, and the |
| 1183 | stack 1 entry dropped. |
| 1184 | A single coefficient value may be transformed into combo token that |
| 1185 | is moved to stack 0, or if it cannot be combined, it is left alone |
| 1186 | and a single length-1 zero run is emitted in stack 0. |
| 1187 | A combo token is extended and moved to stack 0. |
| 1188 | During AC coding, we restrict the run lengths on combo tokens for |
| 1189 | stack 1 to guarantee we can extend them.*/ |
| 1190 | switch(token1){ |
| 1191 | case OC_DCT_SHORT_ZRL_TOKEN:{ |
| 1192 | if(eb1<7){ |
| 1193 | dct_tokens0[ti0]=OC_DCT_SHORT_ZRL_TOKEN; |
| 1194 | extra_bits0[ti0]=(ogg_uint16_t)(eb1+1); |
| 1195 | ti0++; |
| 1196 | /*Don't write the AC coefficient back out.*/ |
| 1197 | continue; |
| 1198 | } |
| 1199 | /*Fall through.*/ |
| 1200 | } |
| 1201 | case OC_DCT_ZRL_TOKEN:{ |
| 1202 | dct_tokens0[ti0]=OC_DCT_ZRL_TOKEN; |
| 1203 | extra_bits0[ti0]=(ogg_uint16_t)(eb1+1); |
| 1204 | ti0++; |
| 1205 | /*Don't write the AC coefficient back out.*/ |
| 1206 | }continue; |
| 1207 | case OC_ONE_TOKEN: |
| 1208 | case OC_MINUS_ONE_TOKEN:{ |
| 1209 | dct_tokens0[ti0]=OC_DCT_RUN_CAT1A; |
| 1210 | extra_bits0[ti0]=(ogg_uint16_t)(token1-OC_ONE_TOKEN); |
| 1211 | ti0++; |
| 1212 | /*Don't write the AC coefficient back out.*/ |
| 1213 | }continue; |
| 1214 | case OC_TWO_TOKEN: |
| 1215 | case OC_MINUS_TWO_TOKEN:{ |
| 1216 | dct_tokens0[ti0]=OC_DCT_RUN_CAT2A; |
| 1217 | extra_bits0[ti0]=(ogg_uint16_t)(token1-OC_TWO_TOKEN<<1); |
| 1218 | ti0++; |
| 1219 | /*Don't write the AC coefficient back out.*/ |
| 1220 | }continue; |
| 1221 | case OC_DCT_VAL_CAT2:{ |
| 1222 | dct_tokens0[ti0]=OC_DCT_RUN_CAT2A; |
| 1223 | extra_bits0[ti0]=(ogg_uint16_t)((eb1<<1)+1); |
| 1224 | ti0++; |
| 1225 | /*Don't write the AC coefficient back out.*/ |
| 1226 | }continue; |
| 1227 | case OC_DCT_RUN_CAT1A: |
| 1228 | case OC_DCT_RUN_CAT1A+1: |
| 1229 | case OC_DCT_RUN_CAT1A+2: |
| 1230 | case OC_DCT_RUN_CAT1A+3:{ |
| 1231 | dct_tokens0[ti0]=(unsigned char)(token1+1); |
| 1232 | extra_bits0[ti0]=(ogg_uint16_t)eb1; |
| 1233 | ti0++; |
| 1234 | /*Don't write the AC coefficient back out.*/ |
| 1235 | }continue; |
| 1236 | case OC_DCT_RUN_CAT1A+4:{ |
| 1237 | dct_tokens0[ti0]=OC_DCT_RUN_CAT1B; |
| 1238 | extra_bits0[ti0]=(ogg_uint16_t)(eb1<<2); |
| 1239 | ti0++; |
| 1240 | /*Don't write the AC coefficient back out.*/ |
| 1241 | }continue; |
| 1242 | case OC_DCT_RUN_CAT1B:{ |
| 1243 | if((eb1&3)<3){ |
| 1244 | dct_tokens0[ti0]=OC_DCT_RUN_CAT1B; |
| 1245 | extra_bits0[ti0]=(ogg_uint16_t)(eb1+1); |
| 1246 | ti0++; |
| 1247 | /*Don't write the AC coefficient back out.*/ |
| 1248 | continue; |
| 1249 | } |
| 1250 | eb1=((eb1&4)<<1)-1; |
| 1251 | /*Fall through.*/ |
| 1252 | } |
| 1253 | case OC_DCT_RUN_CAT1C:{ |
| 1254 | dct_tokens0[ti0]=OC_DCT_RUN_CAT1C; |
| 1255 | extra_bits0[ti0]=(ogg_uint16_t)(eb1+1); |
| 1256 | ti0++; |
| 1257 | /*Don't write the AC coefficient back out.*/ |
| 1258 | }continue; |
| 1259 | case OC_DCT_RUN_CAT2A:{ |
| 1260 | eb1=(eb1<<1)-1; |
| 1261 | /*Fall through.*/ |
| 1262 | } |
| 1263 | case OC_DCT_RUN_CAT2B:{ |
| 1264 | dct_tokens0[ti0]=OC_DCT_RUN_CAT2B; |
| 1265 | extra_bits0[ti0]=(ogg_uint16_t)(eb1+1); |
| 1266 | ti0++; |
| 1267 | /*Don't write the AC coefficient back out.*/ |
| 1268 | }continue; |
| 1269 | } |
| 1270 | /*We can't merge tokens, write a short zero run and keep going.*/ |
| 1271 | dct_tokens0[ti0]=OC_DCT_SHORT_ZRL_TOKEN; |
| 1272 | extra_bits0[ti0]=0; |
| 1273 | ti0++; |
| 1274 | } |
| 1275 | } |
| 1276 | if(!neobs1){ |
| 1277 | /*Flush any (inactive) EOB run.*/ |
| 1278 | if(eob_run1>0){ |
| 1279 | token=oc_make_eob_token_full(eob_run1,&eb); |
| 1280 | dct_tokens1[ti1w]=(unsigned char)token; |
| 1281 | extra_bits1[ti1w]=(ogg_uint16_t)eb; |
| 1282 | ti1w++; |
| 1283 | eob_run1=0; |
| 1284 | } |
| 1285 | /*There's no active EOB run, so log the current token.*/ |
| 1286 | dct_tokens1[ti1w]=(unsigned char)token1; |
| 1287 | extra_bits1[ti1w]=(ogg_uint16_t)eb1; |
| 1288 | ti1w++; |
| 1289 | } |
| 1290 | else{ |
| 1291 | /*Otherwise consume one EOB from the current run.*/ |
| 1292 | neobs1--; |
| 1293 | /*If we have more than 4095 EOBs outstanding in stack1, flush the run.*/ |
| 1294 | if(eob_run1-neobs1>=4095){ |
| 1295 | dct_tokens1[ti1w]=OC_DCT_REPEAT_RUN3_TOKEN; |
| 1296 | extra_bits1[ti1w]=4095; |
| 1297 | ti1w++; |
| 1298 | eob_run1-=4095; |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | /*Save the current state.*/ |
| 1303 | _enc->ndct_tokens[_pli][0]=ti0; |
| 1304 | _enc->ndct_tokens[_pli][1]=ti1w; |
| 1305 | _enc->eob_run[_pli][0]=eob_run0; |
| 1306 | _enc->eob_run[_pli][1]=eob_run1; |
| 1307 | } |
| 1308 | |
| 1309 | /*Final EOB run welding.*/ |
| 1310 | void oc_enc_tokenize_finish(oc_enc_ctx *_enc){ |
| 1311 | int pli; |
| 1312 | int zzi; |
| 1313 | /*Emit final EOB runs.*/ |
| 1314 | for(pli=0;pli<3;pli++)for(zzi=0;zzi<64;zzi++){ |
| 1315 | int eob_run; |
| 1316 | eob_run=_enc->eob_run[pli][zzi]; |
| 1317 | if(eob_run>0)oc_enc_eob_log(_enc,pli,zzi,eob_run); |
| 1318 | } |
| 1319 | /*Merge the final EOB run of one token list with the start of the next, if |
| 1320 | possible.*/ |
| 1321 | for(zzi=0;zzi<64;zzi++)for(pli=0;pli<3;pli++){ |
| 1322 | int old_tok1; |
| 1323 | int old_tok2; |
| 1324 | int old_eb1; |
| 1325 | int old_eb2; |
| 1326 | int new_tok; |
| 1327 | int new_eb; |
| 1328 | int zzj; |
| 1329 | int plj; |
| 1330 | ptrdiff_t ti=-1; |
| 1331 | int run_count; |
| 1332 | /*Make sure this coefficient has tokens at all.*/ |
| 1333 | if(_enc->ndct_tokens[pli][zzi]<=0)continue; |
| 1334 | /*Ensure the first token is an EOB run.*/ |
| 1335 | old_tok2=_enc->dct_tokens[pli][zzi][0]; |
| 1336 | if(old_tok2>=OC_NDCT_EOB_TOKEN_MAX)continue; |
| 1337 | /*Search for a previous coefficient that has any tokens at all.*/ |
| 1338 | old_tok1=OC_NDCT_EOB_TOKEN_MAX; |
| 1339 | for(zzj=zzi,plj=pli;zzj>=0;zzj--){ |
| 1340 | while(plj-->0){ |
| 1341 | ti=_enc->ndct_tokens[plj][zzj]-1; |
| 1342 | if(ti>=_enc->dct_token_offs[plj][zzj]){ |
| 1343 | old_tok1=_enc->dct_tokens[plj][zzj][ti]; |
| 1344 | break; |
| 1345 | } |
| 1346 | } |
| 1347 | if(plj>=0)break; |
| 1348 | plj=3; |
| 1349 | } |
| 1350 | /*Ensure its last token was an EOB run.*/ |
| 1351 | if(old_tok1>=OC_NDCT_EOB_TOKEN_MAX)continue; |
| 1352 | /*Pull off the associated extra bits, if any, and decode the runs.*/ |
| 1353 | old_eb1=_enc->extra_bits[plj][zzj][ti]; |
| 1354 | old_eb2=_enc->extra_bits[pli][zzi][0]; |
| 1355 | run_count=oc_decode_eob_token(old_tok1,old_eb1) |
| 1356 | +oc_decode_eob_token(old_tok2,old_eb2); |
| 1357 | /*We can't possibly combine these into one run. |
| 1358 | It might be possible to split them more optimally, but we'll just leave |
| 1359 | them as-is.*/ |
| 1360 | if(run_count>=4096)continue; |
| 1361 | /*We CAN combine them into one run.*/ |
| 1362 | new_tok=oc_make_eob_token_full(run_count,&new_eb); |
| 1363 | _enc->dct_tokens[plj][zzj][ti]=(unsigned char)new_tok; |
| 1364 | _enc->extra_bits[plj][zzj][ti]=(ogg_uint16_t)new_eb; |
| 1365 | _enc->dct_token_offs[pli][zzi]++; |
| 1366 | } |
| 1367 | } |