Line data Source code
1 : /*
2 : * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3 : *
4 : * This source code is subject to the terms of the BSD 2 Clause License and
5 : * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 : * was not distributed with this source code in the LICENSE file, you can
7 : * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 : * Media Patent License 1.0 was not distributed with this source code in the
9 : * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 : */
11 :
12 : #include <assert.h>
13 : #include <math.h>
14 : #include <stdio.h>
15 : #include <string.h>
16 :
17 : #include "aom_mem/aom_mem.h"
18 :
19 : #include "av1/common/entropy.h"
20 : #include "av1/common/pred_common.h"
21 : #include "av1/common/scan.h"
22 : #include "av1/common/seg_common.h"
23 :
24 : #include "av1/encoder/cost.h"
25 : #include "av1/encoder/encoder.h"
26 : #if CONFIG_LV_MAP
27 : #include "av1/encoder/encodetxb.c"
28 : #endif
29 : #include "av1/encoder/rdopt.h"
30 : #include "av1/encoder/tokenize.h"
31 :
32 : static const TOKENVALUE dct_cat_lt_10_value_tokens[] = {
33 : { 9, 63 }, { 9, 61 }, { 9, 59 }, { 9, 57 }, { 9, 55 }, { 9, 53 }, { 9, 51 },
34 : { 9, 49 }, { 9, 47 }, { 9, 45 }, { 9, 43 }, { 9, 41 }, { 9, 39 }, { 9, 37 },
35 : { 9, 35 }, { 9, 33 }, { 9, 31 }, { 9, 29 }, { 9, 27 }, { 9, 25 }, { 9, 23 },
36 : { 9, 21 }, { 9, 19 }, { 9, 17 }, { 9, 15 }, { 9, 13 }, { 9, 11 }, { 9, 9 },
37 : { 9, 7 }, { 9, 5 }, { 9, 3 }, { 9, 1 }, { 8, 31 }, { 8, 29 }, { 8, 27 },
38 : { 8, 25 }, { 8, 23 }, { 8, 21 }, { 8, 19 }, { 8, 17 }, { 8, 15 }, { 8, 13 },
39 : { 8, 11 }, { 8, 9 }, { 8, 7 }, { 8, 5 }, { 8, 3 }, { 8, 1 }, { 7, 15 },
40 : { 7, 13 }, { 7, 11 }, { 7, 9 }, { 7, 7 }, { 7, 5 }, { 7, 3 }, { 7, 1 },
41 : { 6, 7 }, { 6, 5 }, { 6, 3 }, { 6, 1 }, { 5, 3 }, { 5, 1 }, { 4, 1 },
42 : { 3, 1 }, { 2, 1 }, { 1, 1 }, { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
43 : { 4, 0 }, { 5, 0 }, { 5, 2 }, { 6, 0 }, { 6, 2 }, { 6, 4 }, { 6, 6 },
44 : { 7, 0 }, { 7, 2 }, { 7, 4 }, { 7, 6 }, { 7, 8 }, { 7, 10 }, { 7, 12 },
45 : { 7, 14 }, { 8, 0 }, { 8, 2 }, { 8, 4 }, { 8, 6 }, { 8, 8 }, { 8, 10 },
46 : { 8, 12 }, { 8, 14 }, { 8, 16 }, { 8, 18 }, { 8, 20 }, { 8, 22 }, { 8, 24 },
47 : { 8, 26 }, { 8, 28 }, { 8, 30 }, { 9, 0 }, { 9, 2 }, { 9, 4 }, { 9, 6 },
48 : { 9, 8 }, { 9, 10 }, { 9, 12 }, { 9, 14 }, { 9, 16 }, { 9, 18 }, { 9, 20 },
49 : { 9, 22 }, { 9, 24 }, { 9, 26 }, { 9, 28 }, { 9, 30 }, { 9, 32 }, { 9, 34 },
50 : { 9, 36 }, { 9, 38 }, { 9, 40 }, { 9, 42 }, { 9, 44 }, { 9, 46 }, { 9, 48 },
51 : { 9, 50 }, { 9, 52 }, { 9, 54 }, { 9, 56 }, { 9, 58 }, { 9, 60 }, { 9, 62 }
52 : };
53 : const TOKENVALUE *av1_dct_cat_lt_10_value_tokens =
54 : dct_cat_lt_10_value_tokens +
55 : (sizeof(dct_cat_lt_10_value_tokens) / sizeof(*dct_cat_lt_10_value_tokens)) /
56 : 2;
57 : // The corresponding costs of the extrabits for the tokens in the above table
58 : // are stored in the table below. The values are obtained from looking up the
59 : // entry for the specified extrabits in the table corresponding to the token
60 : // (as defined in cost element av1_extra_bits)
61 : // e.g. {9, 63} maps to cat5_cost[63 >> 1], {1, 1} maps to sign_cost[1 >> 1]
62 : static const int dct_cat_lt_10_value_cost[] = {
63 : 3773, 3750, 3704, 3681, 3623, 3600, 3554, 3531, 3432, 3409, 3363, 3340, 3282,
64 : 3259, 3213, 3190, 3136, 3113, 3067, 3044, 2986, 2963, 2917, 2894, 2795, 2772,
65 : 2726, 2703, 2645, 2622, 2576, 2553, 3197, 3116, 3058, 2977, 2881, 2800, 2742,
66 : 2661, 2615, 2534, 2476, 2395, 2299, 2218, 2160, 2079, 2566, 2427, 2334, 2195,
67 : 2023, 1884, 1791, 1652, 1893, 1696, 1453, 1256, 1229, 864, 512, 512, 512,
68 : 512, 0, 512, 512, 512, 512, 864, 1229, 1256, 1453, 1696, 1893, 1652,
69 : 1791, 1884, 2023, 2195, 2334, 2427, 2566, 2079, 2160, 2218, 2299, 2395, 2476,
70 : 2534, 2615, 2661, 2742, 2800, 2881, 2977, 3058, 3116, 3197, 2553, 2576, 2622,
71 : 2645, 2703, 2726, 2772, 2795, 2894, 2917, 2963, 2986, 3044, 3067, 3113, 3136,
72 : 3190, 3213, 3259, 3282, 3340, 3363, 3409, 3432, 3531, 3554, 3600, 3623, 3681,
73 : 3704, 3750, 3773,
74 : };
75 : const int *av1_dct_cat_lt_10_value_cost =
76 : dct_cat_lt_10_value_cost +
77 : (sizeof(dct_cat_lt_10_value_cost) / sizeof(*dct_cat_lt_10_value_cost)) / 2;
78 :
79 : // Array indices are identical to previously-existing CONTEXT_NODE indices
80 : /* clang-format off */
81 : const aom_tree_index av1_coef_tree[TREE_SIZE(ENTROPY_TOKENS)] = {
82 : -EOB_TOKEN, 2, // 0 = EOB
83 : -ZERO_TOKEN, 4, // 1 = ZERO
84 : -ONE_TOKEN, 6, // 2 = ONE
85 : 8, 12, // 3 = LOW_VAL
86 : -TWO_TOKEN, 10, // 4 = TWO
87 : -THREE_TOKEN, -FOUR_TOKEN, // 5 = THREE
88 : 14, 16, // 6 = HIGH_LOW
89 : -CATEGORY1_TOKEN, -CATEGORY2_TOKEN, // 7 = CAT_ONE
90 : 18, 20, // 8 = CAT_THREEFOUR
91 : -CATEGORY3_TOKEN, -CATEGORY4_TOKEN, // 9 = CAT_THREE
92 : -CATEGORY5_TOKEN, -CATEGORY6_TOKEN // 10 = CAT_FIVE
93 : };
94 : /* clang-format on */
95 :
96 : static const int16_t zero_cost[] = { 0 };
97 : static const int16_t sign_cost[1] = { 512 };
98 : static const int16_t cat1_cost[1 << 1] = { 864, 1229 };
99 : static const int16_t cat2_cost[1 << 2] = { 1256, 1453, 1696, 1893 };
100 : static const int16_t cat3_cost[1 << 3] = { 1652, 1791, 1884, 2023,
101 : 2195, 2334, 2427, 2566 };
102 : static const int16_t cat4_cost[1 << 4] = { 2079, 2160, 2218, 2299, 2395, 2476,
103 : 2534, 2615, 2661, 2742, 2800, 2881,
104 : 2977, 3058, 3116, 3197 };
105 : static const int16_t cat5_cost[1 << 5] = {
106 : 2553, 2576, 2622, 2645, 2703, 2726, 2772, 2795, 2894, 2917, 2963,
107 : 2986, 3044, 3067, 3113, 3136, 3190, 3213, 3259, 3282, 3340, 3363,
108 : 3409, 3432, 3531, 3554, 3600, 3623, 3681, 3704, 3750, 3773
109 : };
110 : const int16_t av1_cat6_low_cost[256] = {
111 : 3378, 3390, 3401, 3413, 3435, 3447, 3458, 3470, 3517, 3529, 3540, 3552, 3574,
112 : 3586, 3597, 3609, 3671, 3683, 3694, 3706, 3728, 3740, 3751, 3763, 3810, 3822,
113 : 3833, 3845, 3867, 3879, 3890, 3902, 3973, 3985, 3996, 4008, 4030, 4042, 4053,
114 : 4065, 4112, 4124, 4135, 4147, 4169, 4181, 4192, 4204, 4266, 4278, 4289, 4301,
115 : 4323, 4335, 4346, 4358, 4405, 4417, 4428, 4440, 4462, 4474, 4485, 4497, 4253,
116 : 4265, 4276, 4288, 4310, 4322, 4333, 4345, 4392, 4404, 4415, 4427, 4449, 4461,
117 : 4472, 4484, 4546, 4558, 4569, 4581, 4603, 4615, 4626, 4638, 4685, 4697, 4708,
118 : 4720, 4742, 4754, 4765, 4777, 4848, 4860, 4871, 4883, 4905, 4917, 4928, 4940,
119 : 4987, 4999, 5010, 5022, 5044, 5056, 5067, 5079, 5141, 5153, 5164, 5176, 5198,
120 : 5210, 5221, 5233, 5280, 5292, 5303, 5315, 5337, 5349, 5360, 5372, 4988, 5000,
121 : 5011, 5023, 5045, 5057, 5068, 5080, 5127, 5139, 5150, 5162, 5184, 5196, 5207,
122 : 5219, 5281, 5293, 5304, 5316, 5338, 5350, 5361, 5373, 5420, 5432, 5443, 5455,
123 : 5477, 5489, 5500, 5512, 5583, 5595, 5606, 5618, 5640, 5652, 5663, 5675, 5722,
124 : 5734, 5745, 5757, 5779, 5791, 5802, 5814, 5876, 5888, 5899, 5911, 5933, 5945,
125 : 5956, 5968, 6015, 6027, 6038, 6050, 6072, 6084, 6095, 6107, 5863, 5875, 5886,
126 : 5898, 5920, 5932, 5943, 5955, 6002, 6014, 6025, 6037, 6059, 6071, 6082, 6094,
127 : 6156, 6168, 6179, 6191, 6213, 6225, 6236, 6248, 6295, 6307, 6318, 6330, 6352,
128 : 6364, 6375, 6387, 6458, 6470, 6481, 6493, 6515, 6527, 6538, 6550, 6597, 6609,
129 : 6620, 6632, 6654, 6666, 6677, 6689, 6751, 6763, 6774, 6786, 6808, 6820, 6831,
130 : 6843, 6890, 6902, 6913, 6925, 6947, 6959, 6970, 6982
131 : };
132 : const int av1_cat6_high_cost[CAT6_HIGH_COST_ENTRIES] = {
133 : 100, 2263, 2739, 4902, 3160, 5323, 5799, 7962, 3678, 5841, 6317,
134 : 8480, 6738, 8901, 9377, 11540, 3678, 5841, 6317, 8480, 6738, 8901,
135 : 9377, 11540, 7256, 9419, 9895, 12058, 10316, 12479, 12955, 15118, 3678,
136 : 5841, 6317, 8480, 6738, 8901, 9377, 11540, 7256, 9419, 9895, 12058,
137 : 10316, 12479, 12955, 15118, 7256, 9419, 9895, 12058, 10316, 12479, 12955,
138 : 15118, 10834, 12997, 13473, 15636, 13894, 16057, 16533, 18696,
139 : #if CONFIG_HIGHBITDEPTH
140 : 4193, 6356, 6832, 8995, 7253, 9416, 9892, 12055, 7771, 9934, 10410,
141 : 12573, 10831, 12994, 13470, 15633, 7771, 9934, 10410, 12573, 10831, 12994,
142 : 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 7771,
143 : 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151,
144 : 14409, 16572, 17048, 19211, 11349, 13512, 13988, 16151, 14409, 16572, 17048,
145 : 19211, 14927, 17090, 17566, 19729, 17987, 20150, 20626, 22789, 4193, 6356,
146 : 6832, 8995, 7253, 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831,
147 : 12994, 13470, 15633, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633,
148 : 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410,
149 : 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572,
150 : 17048, 19211, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927,
151 : 17090, 17566, 19729, 17987, 20150, 20626, 22789, 8286, 10449, 10925, 13088,
152 : 11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
153 : 19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605,
154 : 18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924,
155 : 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
156 : 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659,
157 : 23822, 22080, 24243, 24719, 26882, 4193, 6356, 6832, 8995, 7253, 9416,
158 : 9892, 12055, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 7771,
159 : 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151,
160 : 14409, 16572, 17048, 19211, 7771, 9934, 10410, 12573, 10831, 12994, 13470,
161 : 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349, 13512,
162 : 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729, 17987,
163 : 20150, 20626, 22789, 8286, 10449, 10925, 13088, 11346, 13509, 13985, 16148,
164 : 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503,
165 : 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665,
166 : 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442,
167 : 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244,
168 : 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719,
169 : 26882, 8286, 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027,
170 : 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924,
171 : 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
172 : 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081,
173 : 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665,
174 : 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379,
175 : 14542, 15018, 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759,
176 : 19017, 21180, 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
177 : 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120,
178 : 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595,
179 : 24758, 25234, 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397,
180 : 23113, 25276, 25752, 27915, 26173, 28336, 28812, 30975, 4193, 6356, 6832,
181 : 8995, 7253, 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831, 12994,
182 : 13470, 15633, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349,
183 : 13512, 13988, 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410, 12573,
184 : 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048,
185 : 19211, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090,
186 : 17566, 19729, 17987, 20150, 20626, 22789, 8286, 10449, 10925, 13088, 11346,
187 : 13509, 13985, 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
188 : 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081,
189 : 20244, 18502, 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087,
190 : 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442,
191 : 17605, 18081, 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822,
192 : 22080, 24243, 24719, 26882, 8286, 10449, 10925, 13088, 11346, 13509, 13985,
193 : 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027,
194 : 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502,
195 : 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
196 : 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081,
197 : 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243,
198 : 24719, 26882, 12379, 14542, 15018, 17181, 15439, 17602, 18078, 20241, 15957,
199 : 18120, 18596, 20759, 19017, 21180, 21656, 23819, 15957, 18120, 18596, 20759,
200 : 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
201 : 27397, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698,
202 : 22174, 24337, 22595, 24758, 25234, 27397, 19535, 21698, 22174, 24337, 22595,
203 : 24758, 25234, 27397, 23113, 25276, 25752, 27915, 26173, 28336, 28812, 30975,
204 : 8286, 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503,
205 : 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087,
206 : 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864,
207 : 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244,
208 : 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141,
209 : 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542,
210 : 15018, 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017,
211 : 21180, 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819,
212 : 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596,
213 : 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758,
214 : 25234, 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113,
215 : 25276, 25752, 27915, 26173, 28336, 28812, 30975, 12379, 14542, 15018, 17181,
216 : 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
217 : 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698,
218 : 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759, 19017,
219 : 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397,
220 : 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276, 25752,
221 : 27915, 26173, 28336, 28812, 30975, 16472, 18635, 19111, 21274, 19532, 21695,
222 : 22171, 24334, 20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 20050,
223 : 22213, 22689, 24852, 23110, 25273, 25749, 27912, 23628, 25791, 26267, 28430,
224 : 26688, 28851, 29327, 31490, 20050, 22213, 22689, 24852, 23110, 25273, 25749,
225 : 27912, 23628, 25791, 26267, 28430, 26688, 28851, 29327, 31490, 23628, 25791,
226 : 26267, 28430, 26688, 28851, 29327, 31490, 27206, 29369, 29845, 32008, 30266,
227 : 32429, 32905, 35068
228 : #endif
229 : };
230 :
231 : const uint8_t av1_cat6_skipped_bits_discount[8] = {
232 : 0, 3, 6, 9, 12, 18, 24, 30
233 : };
234 :
235 : #if CONFIG_NEW_MULTISYMBOL
236 : const av1_extra_bit av1_extra_bits[ENTROPY_TOKENS] = {
237 : { 0, 0, 0, zero_cost }, // ZERO_TOKEN
238 : { 0, 0, 1, sign_cost }, // ONE_TOKEN
239 : { 0, 0, 2, sign_cost }, // TWO_TOKEN
240 : { 0, 0, 3, sign_cost }, // THREE_TOKEN
241 : { 0, 0, 4, sign_cost }, // FOUR_TOKEN
242 : { av1_cat1_cdf, 1, CAT1_MIN_VAL, cat1_cost }, // CATEGORY1_TOKEN
243 : { av1_cat2_cdf, 2, CAT2_MIN_VAL, cat2_cost }, // CATEGORY2_TOKEN
244 : { av1_cat3_cdf, 3, CAT3_MIN_VAL, cat3_cost }, // CATEGORY3_TOKEN
245 : { av1_cat4_cdf, 4, CAT4_MIN_VAL, cat4_cost }, // CATEGORY4_TOKEN
246 : { av1_cat5_cdf, 5, CAT5_MIN_VAL, cat5_cost }, // CATEGORY5_TOKEN
247 : { av1_cat6_cdf, 18, CAT6_MIN_VAL, 0 }, // CATEGORY6_TOKEN
248 : { 0, 0, 0, zero_cost } // EOB_TOKEN
249 : };
250 : #else
251 : const av1_extra_bit av1_extra_bits[ENTROPY_TOKENS] = {
252 : { 0, 0, 0, zero_cost }, // ZERO_TOKEN
253 : { 0, 0, 1, sign_cost }, // ONE_TOKEN
254 : { 0, 0, 2, sign_cost }, // TWO_TOKEN
255 : { 0, 0, 3, sign_cost }, // THREE_TOKEN
256 : { 0, 0, 4, sign_cost }, // FOUR_TOKEN
257 : { av1_cat1_prob, 1, CAT1_MIN_VAL, cat1_cost }, // CATEGORY1_TOKEN
258 : { av1_cat2_prob, 2, CAT2_MIN_VAL, cat2_cost }, // CATEGORY2_TOKEN
259 : { av1_cat3_prob, 3, CAT3_MIN_VAL, cat3_cost }, // CATEGORY3_TOKEN
260 : { av1_cat4_prob, 4, CAT4_MIN_VAL, cat4_cost }, // CATEGORY4_TOKEN
261 : { av1_cat5_prob, 5, CAT5_MIN_VAL, cat5_cost }, // CATEGORY5_TOKEN
262 : { av1_cat6_prob, 18, CAT6_MIN_VAL, 0 }, // CATEGORY6_TOKEN
263 : { 0, 0, 0, zero_cost } // EOB_TOKEN
264 : };
265 : #endif
266 :
267 : #if !CONFIG_PVQ || CONFIG_VAR_TX
268 0 : static void cost_coeffs_b(int plane, int block, int blk_row, int blk_col,
269 : BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
270 0 : struct tokenize_b_args *const args = arg;
271 0 : const AV1_COMP *const cpi = args->cpi;
272 0 : const AV1_COMMON *cm = &cpi->common;
273 0 : ThreadData *const td = args->td;
274 0 : MACROBLOCK *const x = &td->mb;
275 0 : MACROBLOCKD *const xd = &x->e_mbd;
276 0 : MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
277 0 : struct macroblock_plane *p = &x->plane[plane];
278 0 : struct macroblockd_plane *pd = &xd->plane[plane];
279 0 : const PLANE_TYPE type = pd->plane_type;
280 0 : const int ref = is_inter_block(mbmi);
281 0 : const TX_TYPE tx_type = get_tx_type(type, xd, block, tx_size);
282 0 : const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, ref);
283 0 : const int rate = av1_cost_coeffs(cpi, x, plane, block, tx_size, scan_order,
284 0 : pd->above_context + blk_col,
285 0 : pd->left_context + blk_row, 0);
286 0 : args->this_rate += rate;
287 : (void)plane_bsize;
288 0 : av1_set_contexts(xd, pd, plane, tx_size, p->eobs[block] > 0, blk_col,
289 : blk_row);
290 0 : }
291 :
292 0 : static void set_entropy_context_b(int plane, int block, int blk_row,
293 : int blk_col, BLOCK_SIZE plane_bsize,
294 : TX_SIZE tx_size, void *arg) {
295 0 : struct tokenize_b_args *const args = arg;
296 0 : ThreadData *const td = args->td;
297 0 : MACROBLOCK *const x = &td->mb;
298 0 : MACROBLOCKD *const xd = &x->e_mbd;
299 0 : struct macroblock_plane *p = &x->plane[plane];
300 0 : struct macroblockd_plane *pd = &xd->plane[plane];
301 : (void)plane_bsize;
302 0 : av1_set_contexts(xd, pd, plane, tx_size, p->eobs[block] > 0, blk_col,
303 : blk_row);
304 0 : }
305 :
306 0 : static INLINE void add_token(TOKENEXTRA **t,
307 : aom_cdf_prob (*tail_cdf)[CDF_SIZE(ENTROPY_TOKENS)],
308 : aom_cdf_prob (*head_cdf)[CDF_SIZE(ENTROPY_TOKENS)],
309 : int eob_val, int first_val, int32_t extra,
310 : uint8_t token) {
311 0 : (*t)->token = token;
312 0 : (*t)->extra = extra;
313 0 : (*t)->tail_cdf = tail_cdf;
314 0 : (*t)->head_cdf = head_cdf;
315 0 : (*t)->eob_val = eob_val;
316 0 : (*t)->first_val = first_val;
317 0 : (*t)++;
318 0 : }
319 : #endif // !CONFIG_PVQ || CONFIG_VAR_TX
320 :
321 : #if CONFIG_PALETTE
322 0 : void av1_tokenize_palette_sb(const AV1_COMP *cpi,
323 : const struct ThreadData *const td, int plane,
324 : TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
325 : int *rate) {
326 0 : const MACROBLOCK *const x = &td->mb;
327 0 : const MACROBLOCKD *const xd = &x->e_mbd;
328 0 : const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
329 0 : const uint8_t *const color_map = xd->plane[plane].color_index_map;
330 0 : const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
331 0 : const int n = pmi->palette_size[plane];
332 : int i, j;
333 0 : int this_rate = 0;
334 : uint8_t color_order[PALETTE_MAX_SIZE];
335 : const aom_prob(
336 0 : *const probs)[PALETTE_COLOR_INDEX_CONTEXTS][PALETTE_COLORS - 1] =
337 : plane == 0 ? av1_default_palette_y_color_index_prob
338 0 : : av1_default_palette_uv_color_index_prob;
339 : int plane_block_width, rows, cols;
340 0 : av1_get_block_dimensions(bsize, plane, xd, &plane_block_width, NULL, &rows,
341 : &cols);
342 0 : assert(plane == 0 || plane == 1);
343 :
344 : #if CONFIG_PALETTE_THROUGHPUT
345 : int k;
346 0 : for (k = 1; k < rows + cols - 1; ++k) {
347 0 : for (j = AOMMIN(k, cols - 1); j >= AOMMAX(0, k - rows + 1); --j) {
348 0 : i = k - j;
349 : #else
350 : for (i = 0; i < rows; ++i) {
351 : for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
352 : #endif // CONFIG_PALETTE_THROUGHPUT
353 : int color_new_idx;
354 0 : const int color_ctx = av1_get_palette_color_index_context(
355 : color_map, plane_block_width, i, j, n, color_order, &color_new_idx);
356 0 : assert(color_new_idx >= 0 && color_new_idx < n);
357 0 : if (dry_run == DRY_RUN_COSTCOEFFS)
358 0 : this_rate += cpi->palette_y_color_cost[n - PALETTE_MIN_SIZE][color_ctx]
359 0 : [color_new_idx];
360 0 : (*t)->token = color_new_idx;
361 0 : (*t)->context_tree = probs[n - PALETTE_MIN_SIZE][color_ctx];
362 0 : (*t)->skip_eob_node = 0;
363 0 : ++(*t);
364 : }
365 : }
366 0 : if (rate) *rate += this_rate;
367 0 : }
368 : #endif // CONFIG_PALETTE
369 :
370 : #if CONFIG_PVQ
371 : static void add_pvq_block(AV1_COMMON *const cm, MACROBLOCK *const x,
372 : PVQ_INFO *pvq) {
373 : PVQ_QUEUE *q = x->pvq_q;
374 : if (q->curr_pos >= q->buf_len) {
375 : int new_buf_len = 2 * q->buf_len + 1;
376 : PVQ_INFO *new_buf;
377 : CHECK_MEM_ERROR(cm, new_buf, aom_malloc(new_buf_len * sizeof(PVQ_INFO)));
378 : memcpy(new_buf, q->buf, q->buf_len * sizeof(PVQ_INFO));
379 : aom_free(q->buf);
380 : q->buf = new_buf;
381 : q->buf_len = new_buf_len;
382 : }
383 : OD_COPY(q->buf + q->curr_pos, pvq, 1);
384 : ++q->curr_pos;
385 : }
386 :
387 : // NOTE: This does not actually generate tokens, instead we store the encoding
388 : // decisions made for PVQ in a queue that we will read from when
389 : // actually writing the bitstream in write_modes_b
390 : static void tokenize_pvq(int plane, int block, int blk_row, int blk_col,
391 : BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
392 : struct tokenize_b_args *const args = arg;
393 : const AV1_COMP *cpi = args->cpi;
394 : const AV1_COMMON *const cm = &cpi->common;
395 : ThreadData *const td = args->td;
396 : MACROBLOCK *const x = &td->mb;
397 : PVQ_INFO *pvq_info;
398 :
399 : (void)block;
400 : (void)blk_row;
401 : (void)blk_col;
402 : (void)plane_bsize;
403 : (void)tx_size;
404 :
405 : assert(block < MAX_PVQ_BLOCKS_IN_SB);
406 : pvq_info = &x->pvq[block][plane];
407 : add_pvq_block((AV1_COMMON * const)cm, x, pvq_info);
408 : }
409 : #endif // CONFIG_PVQ
410 :
411 0 : static void tokenize_b(int plane, int block, int blk_row, int blk_col,
412 : BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
413 : #if !CONFIG_PVQ
414 0 : struct tokenize_b_args *const args = arg;
415 0 : const AV1_COMP *cpi = args->cpi;
416 0 : const AV1_COMMON *const cm = &cpi->common;
417 0 : ThreadData *const td = args->td;
418 0 : MACROBLOCK *const x = &td->mb;
419 0 : MACROBLOCKD *const xd = &x->e_mbd;
420 0 : TOKENEXTRA **tp = args->tp;
421 : uint8_t token_cache[MAX_TX_SQUARE];
422 0 : struct macroblock_plane *p = &x->plane[plane];
423 0 : struct macroblockd_plane *pd = &xd->plane[plane];
424 0 : MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
425 : int pt; /* near block/prev token context index */
426 : int c;
427 0 : TOKENEXTRA *t = *tp; /* store tokens starting here */
428 0 : const int eob = p->eobs[block];
429 0 : const PLANE_TYPE type = pd->plane_type;
430 0 : const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
431 : #if CONFIG_SUPERTX
432 : const int segment_id = AOMMIN(mbmi->segment_id, mbmi->segment_id_supertx);
433 : #else
434 0 : const int segment_id = mbmi->segment_id;
435 : #endif // CONFIG_SUEPRTX
436 : const int16_t *scan, *nb;
437 0 : const TX_TYPE tx_type = get_tx_type(type, xd, block, tx_size);
438 0 : const SCAN_ORDER *const scan_order =
439 0 : get_scan(cm, tx_size, tx_type, is_inter_block(mbmi));
440 0 : const int ref = is_inter_block(mbmi);
441 0 : unsigned int(*const counts)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
442 0 : td->rd_counts.coef_counts[txsize_sqr_map[tx_size]][type][ref];
443 : #if CONFIG_EC_ADAPT
444 0 : FRAME_CONTEXT *ec_ctx = xd->tile_ctx;
445 : #else
446 : FRAME_CONTEXT *ec_ctx = cpi->common.fc;
447 : #endif
448 : aom_cdf_prob(
449 0 : *const coef_head_cdfs)[COEFF_CONTEXTS][CDF_SIZE(ENTROPY_TOKENS)] =
450 0 : ec_ctx->coef_head_cdfs[txsize_sqr_map[tx_size]][type][ref];
451 : aom_cdf_prob(
452 0 : *const coef_tail_cdfs)[COEFF_CONTEXTS][CDF_SIZE(ENTROPY_TOKENS)] =
453 0 : ec_ctx->coef_tail_cdfs[txsize_sqr_map[tx_size]][type][ref];
454 0 : unsigned int(*const blockz_count)[2] =
455 0 : td->counts->blockz_count[txsize_sqr_map[tx_size]][type][ref];
456 : int eob_val;
457 0 : int first_val = 1;
458 0 : const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
459 0 : unsigned int(*const eob_branch)[COEFF_CONTEXTS] =
460 0 : td->counts->eob_branch[txsize_sqr_map[tx_size]][type][ref];
461 0 : const uint8_t *const band = get_band_translate(tx_size);
462 : int16_t token;
463 : EXTRABIT extra;
464 : (void)plane_bsize;
465 0 : pt = get_entropy_context(tx_size, pd->above_context + blk_col,
466 0 : pd->left_context + blk_row);
467 0 : scan = scan_order->scan;
468 0 : nb = scan_order->neighbors;
469 0 : c = 0;
470 :
471 0 : if (eob == 0)
472 0 : add_token(&t, &coef_tail_cdfs[band[c]][pt], &coef_head_cdfs[band[c]][pt], 1,
473 : 1, 0, BLOCK_Z_TOKEN);
474 :
475 0 : ++blockz_count[pt][eob != 0];
476 :
477 0 : while (c < eob) {
478 0 : int v = qcoeff[scan[c]];
479 0 : first_val = (c == 0);
480 :
481 0 : if (!v) {
482 0 : add_token(&t, &coef_tail_cdfs[band[c]][pt], &coef_head_cdfs[band[c]][pt],
483 : 0, first_val, 0, ZERO_TOKEN);
484 0 : ++counts[band[c]][pt][ZERO_TOKEN];
485 0 : token_cache[scan[c]] = 0;
486 : } else {
487 0 : eob_val =
488 0 : (c + 1 == eob) ? (c + 1 == seg_eob ? LAST_EOB : EARLY_EOB) : NO_EOB;
489 :
490 0 : av1_get_token_extra(v, &token, &extra);
491 :
492 0 : add_token(&t, &coef_tail_cdfs[band[c]][pt], &coef_head_cdfs[band[c]][pt],
493 0 : eob_val, first_val, extra, (uint8_t)token);
494 :
495 0 : if (eob_val != LAST_EOB) {
496 0 : ++counts[band[c]][pt][token];
497 0 : ++eob_branch[band[c]][pt];
498 0 : counts[band[c]][pt][EOB_TOKEN] += eob_val != NO_EOB;
499 : }
500 :
501 0 : token_cache[scan[c]] = av1_pt_energy_class[token];
502 : }
503 0 : ++c;
504 0 : pt = get_coef_context(nb, token_cache, AOMMIN(c, eob - 1));
505 : }
506 :
507 : #if CONFIG_COEF_INTERLEAVE
508 : t->token = EOSB_TOKEN;
509 : t++;
510 : #endif
511 :
512 0 : *tp = t;
513 :
514 : #if CONFIG_ADAPT_SCAN
515 : // Since dqcoeff is not available here, we pass qcoeff into
516 : // av1_update_scan_count_facade(). The update behavior should be the same
517 : // because av1_update_scan_count_facade() only cares if coefficients are zero
518 : // or not.
519 : av1_update_scan_count_facade((AV1_COMMON *)cm, td->counts, tx_size, tx_type,
520 : qcoeff, c);
521 : #endif
522 :
523 0 : av1_set_contexts(xd, pd, plane, tx_size, c > 0, blk_col, blk_row);
524 : #else // !CONFIG_PVQ
525 : tokenize_pvq(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
526 : #endif // !CONFIG_PVQ
527 0 : }
528 :
529 : struct is_skippable_args {
530 : uint16_t *eobs;
531 : int *skippable;
532 : };
533 0 : static void is_skippable(int plane, int block, int blk_row, int blk_col,
534 : BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *argv) {
535 0 : struct is_skippable_args *args = argv;
536 : (void)plane;
537 : (void)plane_bsize;
538 : (void)tx_size;
539 : (void)blk_row;
540 : (void)blk_col;
541 0 : args->skippable[0] &= (!args->eobs[block]);
542 0 : }
543 :
544 : // TODO(yaowu): rewrite and optimize this function to remove the usage of
545 : // av1_foreach_transform_block() and simplify is_skippable().
546 0 : int av1_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
547 0 : int result = 1;
548 0 : struct is_skippable_args args = { x->plane[plane].eobs, &result };
549 0 : av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane, is_skippable,
550 : &args);
551 0 : return result;
552 : }
553 :
554 : #if CONFIG_VAR_TX
555 0 : void tokenize_vartx(ThreadData *td, TOKENEXTRA **t, RUN_TYPE dry_run,
556 : TX_SIZE tx_size, BLOCK_SIZE plane_bsize, int blk_row,
557 : int blk_col, int block, int plane, void *arg) {
558 0 : MACROBLOCK *const x = &td->mb;
559 0 : MACROBLOCKD *const xd = &x->e_mbd;
560 0 : MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
561 0 : const struct macroblockd_plane *const pd = &xd->plane[plane];
562 0 : const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
563 0 : const int tx_row = blk_row >> (1 - pd->subsampling_y);
564 0 : const int tx_col = blk_col >> (1 - pd->subsampling_x);
565 0 : const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
566 0 : const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
567 : TX_SIZE plane_tx_size;
568 :
569 0 : if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
570 :
571 0 : plane_tx_size =
572 0 : plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
573 0 : : mbmi->inter_tx_size[tx_row][tx_col];
574 :
575 0 : if (tx_size == plane_tx_size) {
576 0 : plane_bsize = get_plane_block_size(mbmi->sb_type, pd);
577 : #if CONFIG_LV_MAP
578 : if (!dry_run) {
579 : av1_update_and_record_txb_context(plane, block, blk_row, blk_col,
580 : plane_bsize, tx_size, arg);
581 : } else if (dry_run == DRY_RUN_NORMAL) {
582 : av1_update_txb_context_b(plane, block, blk_row, blk_col, plane_bsize,
583 : tx_size, arg);
584 : } else {
585 : printf("DRY_RUN_COSTCOEFFS is not supported yet\n");
586 : assert(0);
587 : }
588 : #else
589 0 : if (!dry_run)
590 0 : tokenize_b(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
591 0 : else if (dry_run == DRY_RUN_NORMAL)
592 0 : set_entropy_context_b(plane, block, blk_row, blk_col, plane_bsize,
593 : tx_size, arg);
594 0 : else if (dry_run == DRY_RUN_COSTCOEFFS)
595 0 : cost_coeffs_b(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
596 : #endif
597 : } else {
598 : // Half the block size in transform block unit.
599 0 : const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
600 0 : const int bsl = tx_size_wide_unit[sub_txs];
601 : int i;
602 :
603 0 : assert(bsl > 0);
604 :
605 0 : for (i = 0; i < 4; ++i) {
606 0 : const int offsetr = blk_row + ((i >> 1) * bsl);
607 0 : const int offsetc = blk_col + ((i & 0x01) * bsl);
608 :
609 0 : int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
610 :
611 0 : if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
612 :
613 0 : tokenize_vartx(td, t, dry_run, sub_txs, plane_bsize, offsetr, offsetc,
614 : block, plane, arg);
615 0 : block += step;
616 : }
617 : }
618 : }
619 :
620 0 : void av1_tokenize_sb_vartx(const AV1_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
621 : RUN_TYPE dry_run, int mi_row, int mi_col,
622 : BLOCK_SIZE bsize, int *rate) {
623 0 : const AV1_COMMON *const cm = &cpi->common;
624 0 : MACROBLOCK *const x = &td->mb;
625 0 : MACROBLOCKD *const xd = &x->e_mbd;
626 0 : MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
627 : #if CONFIG_LV_MAP
628 : (void)t;
629 : #else
630 0 : TOKENEXTRA *t_backup = *t;
631 : #endif
632 0 : const int ctx = av1_get_skip_context(xd);
633 0 : const int skip_inc =
634 0 : !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP);
635 0 : struct tokenize_b_args arg = { cpi, td, t, 0 };
636 : int plane;
637 0 : if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
638 :
639 0 : if (mbmi->skip) {
640 0 : if (!dry_run) td->counts->skip[ctx][1] += skip_inc;
641 0 : av1_reset_skip_context(xd, mi_row, mi_col, bsize);
642 : #if !CONFIG_LV_MAP
643 0 : if (dry_run) *t = t_backup;
644 : #endif
645 0 : return;
646 : }
647 :
648 0 : if (!dry_run) td->counts->skip[ctx][0] += skip_inc;
649 : #if !CONFIG_LV_MAP
650 : else
651 0 : *t = t_backup;
652 : #endif
653 :
654 0 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
655 : #if CONFIG_CB4X4
656 0 : if (!is_chroma_reference(mi_row, mi_col, bsize,
657 : xd->plane[plane].subsampling_x,
658 : xd->plane[plane].subsampling_y)) {
659 : #if !CONFIG_PVQ || !CONFIG_LV_MAP
660 0 : if (!dry_run) {
661 0 : (*t)->token = EOSB_TOKEN;
662 0 : (*t)++;
663 : }
664 : #endif
665 0 : continue;
666 : }
667 : #endif
668 0 : const struct macroblockd_plane *const pd = &xd->plane[plane];
669 : #if CONFIG_CB4X4 && !CONFIG_CHROMA_2X2
670 0 : const BLOCK_SIZE plane_bsize =
671 0 : AOMMAX(BLOCK_4X4, get_plane_block_size(bsize, pd));
672 : #else
673 : const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
674 : #endif
675 0 : const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
676 0 : const int mi_height = block_size_high[plane_bsize] >> tx_size_wide_log2[0];
677 0 : const TX_SIZE max_tx_size = get_vartx_max_txsize(mbmi, plane_bsize);
678 0 : const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
679 0 : int bw = block_size_wide[txb_size] >> tx_size_wide_log2[0];
680 0 : int bh = block_size_high[txb_size] >> tx_size_wide_log2[0];
681 : int idx, idy;
682 0 : int block = 0;
683 0 : int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
684 0 : for (idy = 0; idy < mi_height; idy += bh) {
685 0 : for (idx = 0; idx < mi_width; idx += bw) {
686 0 : tokenize_vartx(td, t, dry_run, max_tx_size, plane_bsize, idy, idx,
687 : block, plane, &arg);
688 0 : block += step;
689 : }
690 : }
691 :
692 : #if !CONFIG_LV_MAP
693 0 : if (!dry_run) {
694 0 : (*t)->token = EOSB_TOKEN;
695 0 : (*t)++;
696 : }
697 : #endif
698 : }
699 0 : if (rate) *rate += arg.this_rate;
700 : }
701 : #endif // CONFIG_VAR_TX
702 :
703 0 : void av1_tokenize_sb(const AV1_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
704 : RUN_TYPE dry_run, BLOCK_SIZE bsize, int *rate,
705 : const int mi_row, const int mi_col) {
706 0 : const AV1_COMMON *const cm = &cpi->common;
707 0 : MACROBLOCK *const x = &td->mb;
708 0 : MACROBLOCKD *const xd = &x->e_mbd;
709 0 : MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
710 0 : const int ctx = av1_get_skip_context(xd);
711 0 : const int skip_inc =
712 0 : !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP);
713 0 : struct tokenize_b_args arg = { cpi, td, t, 0 };
714 0 : if (mbmi->skip) {
715 0 : if (!dry_run) td->counts->skip[ctx][1] += skip_inc;
716 0 : av1_reset_skip_context(xd, mi_row, mi_col, bsize);
717 0 : return;
718 : }
719 :
720 0 : if (!dry_run) {
721 : #if CONFIG_COEF_INTERLEAVE
722 : td->counts->skip[ctx][0] += skip_inc;
723 : av1_foreach_transformed_block_interleave(xd, bsize, tokenize_b, &arg);
724 : #else
725 : int plane;
726 :
727 0 : td->counts->skip[ctx][0] += skip_inc;
728 0 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
729 : #if CONFIG_CB4X4
730 0 : if (!is_chroma_reference(mi_row, mi_col, bsize,
731 : xd->plane[plane].subsampling_x,
732 : xd->plane[plane].subsampling_y)) {
733 : #if !CONFIG_PVQ
734 0 : (*t)->token = EOSB_TOKEN;
735 0 : (*t)++;
736 : #endif
737 0 : continue;
738 : }
739 : #else
740 : (void)mi_row;
741 : (void)mi_col;
742 : #endif
743 0 : av1_foreach_transformed_block_in_plane(xd, bsize, plane, tokenize_b,
744 : &arg);
745 : #if !CONFIG_PVQ
746 0 : (*t)->token = EOSB_TOKEN;
747 0 : (*t)++;
748 : #endif // !CONFIG_PVQ
749 : }
750 : #endif
751 : }
752 : #if !CONFIG_PVQ
753 0 : else if (dry_run == DRY_RUN_NORMAL) {
754 : int plane;
755 0 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
756 : #if CONFIG_CB4X4
757 0 : if (!is_chroma_reference(mi_row, mi_col, bsize,
758 : xd->plane[plane].subsampling_x,
759 : xd->plane[plane].subsampling_y))
760 0 : continue;
761 : #else
762 : (void)mi_row;
763 : (void)mi_col;
764 : #endif
765 0 : av1_foreach_transformed_block_in_plane(xd, bsize, plane,
766 : set_entropy_context_b, &arg);
767 : }
768 0 : } else if (dry_run == DRY_RUN_COSTCOEFFS) {
769 : int plane;
770 0 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
771 : #if CONFIG_CB4X4
772 0 : if (!is_chroma_reference(mi_row, mi_col, bsize,
773 : xd->plane[plane].subsampling_x,
774 : xd->plane[plane].subsampling_y))
775 0 : continue;
776 : #else
777 : (void)mi_row;
778 : (void)mi_col;
779 : #endif
780 0 : av1_foreach_transformed_block_in_plane(xd, bsize, plane, cost_coeffs_b,
781 : &arg);
782 : }
783 : }
784 : #endif // !CONFIG_PVQ
785 :
786 0 : if (rate) *rate += arg.this_rate;
787 : }
788 :
789 : #if CONFIG_SUPERTX
790 : void av1_tokenize_sb_supertx(const AV1_COMP *cpi, ThreadData *td,
791 : TOKENEXTRA **t, RUN_TYPE dry_run, int mi_row,
792 : int mi_col, BLOCK_SIZE bsize, int *rate) {
793 : const AV1_COMMON *const cm = &cpi->common;
794 : MACROBLOCKD *const xd = &td->mb.e_mbd;
795 : MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
796 : TOKENEXTRA *t_backup = *t;
797 : const int ctx = av1_get_skip_context(xd);
798 : const int skip_inc =
799 : !segfeature_active(&cm->seg, mbmi->segment_id_supertx, SEG_LVL_SKIP);
800 : struct tokenize_b_args arg = { cpi, td, t, 0 };
801 : if (mbmi->skip) {
802 : if (!dry_run) td->counts->skip[ctx][1] += skip_inc;
803 : av1_reset_skip_context(xd, mi_row, mi_col, bsize);
804 : if (dry_run) *t = t_backup;
805 : return;
806 : }
807 :
808 : if (!dry_run) {
809 : int plane;
810 : td->counts->skip[ctx][0] += skip_inc;
811 :
812 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
813 : av1_foreach_transformed_block_in_plane(xd, bsize, plane, tokenize_b,
814 : &arg);
815 : (*t)->token = EOSB_TOKEN;
816 : (*t)++;
817 : }
818 : } else if (dry_run == DRY_RUN_NORMAL) {
819 : int plane;
820 : for (plane = 0; plane < MAX_MB_PLANE; ++plane)
821 : av1_foreach_transformed_block_in_plane(xd, bsize, plane,
822 : set_entropy_context_b, &arg);
823 : *t = t_backup;
824 : } else if (dry_run == DRY_RUN_COSTCOEFFS) {
825 : int plane;
826 : for (plane = 0; plane < MAX_MB_PLANE; ++plane)
827 : av1_foreach_transformed_block_in_plane(xd, bsize, plane, cost_coeffs_b,
828 : &arg);
829 : }
830 : if (rate) *rate += arg.this_rate;
831 : }
832 : #endif // CONFIG_SUPERTX
|