Coverage Report

Created: 2024-09-06 07:53

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