blob: 136cdf223f5caf81095735291d256617de7c467f [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu2ab7ff02016-09-02 12:04:54 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <assert.h>
13#include <math.h>
14#include <stdio.h>
15#include <string.h>
16
Yaowu Xuf883b422016-08-30 14:01:10 -070017#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070018
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"
Debargha Mukherjeeceebb702016-10-11 05:26:50 -070026#include "av1/encoder/rdopt.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/encoder/tokenize.h"
28
29static const TOKENVALUE dct_cat_lt_10_value_tokens[] = {
30 { 9, 63 }, { 9, 61 }, { 9, 59 }, { 9, 57 }, { 9, 55 }, { 9, 53 }, { 9, 51 },
31 { 9, 49 }, { 9, 47 }, { 9, 45 }, { 9, 43 }, { 9, 41 }, { 9, 39 }, { 9, 37 },
32 { 9, 35 }, { 9, 33 }, { 9, 31 }, { 9, 29 }, { 9, 27 }, { 9, 25 }, { 9, 23 },
33 { 9, 21 }, { 9, 19 }, { 9, 17 }, { 9, 15 }, { 9, 13 }, { 9, 11 }, { 9, 9 },
34 { 9, 7 }, { 9, 5 }, { 9, 3 }, { 9, 1 }, { 8, 31 }, { 8, 29 }, { 8, 27 },
35 { 8, 25 }, { 8, 23 }, { 8, 21 }, { 8, 19 }, { 8, 17 }, { 8, 15 }, { 8, 13 },
36 { 8, 11 }, { 8, 9 }, { 8, 7 }, { 8, 5 }, { 8, 3 }, { 8, 1 }, { 7, 15 },
37 { 7, 13 }, { 7, 11 }, { 7, 9 }, { 7, 7 }, { 7, 5 }, { 7, 3 }, { 7, 1 },
38 { 6, 7 }, { 6, 5 }, { 6, 3 }, { 6, 1 }, { 5, 3 }, { 5, 1 }, { 4, 1 },
39 { 3, 1 }, { 2, 1 }, { 1, 1 }, { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
40 { 4, 0 }, { 5, 0 }, { 5, 2 }, { 6, 0 }, { 6, 2 }, { 6, 4 }, { 6, 6 },
41 { 7, 0 }, { 7, 2 }, { 7, 4 }, { 7, 6 }, { 7, 8 }, { 7, 10 }, { 7, 12 },
42 { 7, 14 }, { 8, 0 }, { 8, 2 }, { 8, 4 }, { 8, 6 }, { 8, 8 }, { 8, 10 },
43 { 8, 12 }, { 8, 14 }, { 8, 16 }, { 8, 18 }, { 8, 20 }, { 8, 22 }, { 8, 24 },
44 { 8, 26 }, { 8, 28 }, { 8, 30 }, { 9, 0 }, { 9, 2 }, { 9, 4 }, { 9, 6 },
45 { 9, 8 }, { 9, 10 }, { 9, 12 }, { 9, 14 }, { 9, 16 }, { 9, 18 }, { 9, 20 },
46 { 9, 22 }, { 9, 24 }, { 9, 26 }, { 9, 28 }, { 9, 30 }, { 9, 32 }, { 9, 34 },
47 { 9, 36 }, { 9, 38 }, { 9, 40 }, { 9, 42 }, { 9, 44 }, { 9, 46 }, { 9, 48 },
48 { 9, 50 }, { 9, 52 }, { 9, 54 }, { 9, 56 }, { 9, 58 }, { 9, 60 }, { 9, 62 }
49};
Yaowu Xuf883b422016-08-30 14:01:10 -070050const TOKENVALUE *av1_dct_cat_lt_10_value_tokens =
Yaowu Xuc27fc142016-08-22 16:08:15 -070051 dct_cat_lt_10_value_tokens +
52 (sizeof(dct_cat_lt_10_value_tokens) / sizeof(*dct_cat_lt_10_value_tokens)) /
53 2;
54// The corresponding costs of the extrabits for the tokens in the above table
55// are stored in the table below. The values are obtained from looking up the
56// entry for the specified extrabits in the table corresponding to the token
Yaowu Xuf883b422016-08-30 14:01:10 -070057// (as defined in cost element av1_extra_bits)
Yaowu Xuc27fc142016-08-22 16:08:15 -070058// e.g. {9, 63} maps to cat5_cost[63 >> 1], {1, 1} maps to sign_cost[1 >> 1]
59static const int dct_cat_lt_10_value_cost[] = {
60 3773, 3750, 3704, 3681, 3623, 3600, 3554, 3531, 3432, 3409, 3363, 3340, 3282,
61 3259, 3213, 3190, 3136, 3113, 3067, 3044, 2986, 2963, 2917, 2894, 2795, 2772,
62 2726, 2703, 2645, 2622, 2576, 2553, 3197, 3116, 3058, 2977, 2881, 2800, 2742,
63 2661, 2615, 2534, 2476, 2395, 2299, 2218, 2160, 2079, 2566, 2427, 2334, 2195,
64 2023, 1884, 1791, 1652, 1893, 1696, 1453, 1256, 1229, 864, 512, 512, 512,
65 512, 0, 512, 512, 512, 512, 864, 1229, 1256, 1453, 1696, 1893, 1652,
66 1791, 1884, 2023, 2195, 2334, 2427, 2566, 2079, 2160, 2218, 2299, 2395, 2476,
67 2534, 2615, 2661, 2742, 2800, 2881, 2977, 3058, 3116, 3197, 2553, 2576, 2622,
68 2645, 2703, 2726, 2772, 2795, 2894, 2917, 2963, 2986, 3044, 3067, 3113, 3136,
69 3190, 3213, 3259, 3282, 3340, 3363, 3409, 3432, 3531, 3554, 3600, 3623, 3681,
70 3704, 3750, 3773,
71};
Yaowu Xuf883b422016-08-30 14:01:10 -070072const int *av1_dct_cat_lt_10_value_cost =
Yaowu Xuc27fc142016-08-22 16:08:15 -070073 dct_cat_lt_10_value_cost +
74 (sizeof(dct_cat_lt_10_value_cost) / sizeof(*dct_cat_lt_10_value_cost)) / 2;
75
76// Array indices are identical to previously-existing CONTEXT_NODE indices
77/* clang-format off */
Yaowu Xuf883b422016-08-30 14:01:10 -070078const aom_tree_index av1_coef_tree[TREE_SIZE(ENTROPY_TOKENS)] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -070079 -EOB_TOKEN, 2, // 0 = EOB
80 -ZERO_TOKEN, 4, // 1 = ZERO
81 -ONE_TOKEN, 6, // 2 = ONE
82 8, 12, // 3 = LOW_VAL
83 -TWO_TOKEN, 10, // 4 = TWO
84 -THREE_TOKEN, -FOUR_TOKEN, // 5 = THREE
85 14, 16, // 6 = HIGH_LOW
86 -CATEGORY1_TOKEN, -CATEGORY2_TOKEN, // 7 = CAT_ONE
87 18, 20, // 8 = CAT_THREEFOUR
88 -CATEGORY3_TOKEN, -CATEGORY4_TOKEN, // 9 = CAT_THREE
89 -CATEGORY5_TOKEN, -CATEGORY6_TOKEN // 10 = CAT_FIVE
90};
91/* clang-format on */
92
Yaowu Xuc27fc142016-08-22 16:08:15 -070093static const int16_t zero_cost[] = { 0 };
94static const int16_t sign_cost[1] = { 512 };
95static const int16_t cat1_cost[1 << 1] = { 864, 1229 };
96static const int16_t cat2_cost[1 << 2] = { 1256, 1453, 1696, 1893 };
97static const int16_t cat3_cost[1 << 3] = { 1652, 1791, 1884, 2023,
98 2195, 2334, 2427, 2566 };
99static const int16_t cat4_cost[1 << 4] = { 2079, 2160, 2218, 2299, 2395, 2476,
100 2534, 2615, 2661, 2742, 2800, 2881,
101 2977, 3058, 3116, 3197 };
102static const int16_t cat5_cost[1 << 5] = {
103 2553, 2576, 2622, 2645, 2703, 2726, 2772, 2795, 2894, 2917, 2963,
104 2986, 3044, 3067, 3113, 3136, 3190, 3213, 3259, 3282, 3340, 3363,
105 3409, 3432, 3531, 3554, 3600, 3623, 3681, 3704, 3750, 3773
106};
Yaowu Xuf883b422016-08-30 14:01:10 -0700107const int16_t av1_cat6_low_cost[256] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700108 3378, 3390, 3401, 3413, 3435, 3447, 3458, 3470, 3517, 3529, 3540, 3552, 3574,
109 3586, 3597, 3609, 3671, 3683, 3694, 3706, 3728, 3740, 3751, 3763, 3810, 3822,
110 3833, 3845, 3867, 3879, 3890, 3902, 3973, 3985, 3996, 4008, 4030, 4042, 4053,
111 4065, 4112, 4124, 4135, 4147, 4169, 4181, 4192, 4204, 4266, 4278, 4289, 4301,
112 4323, 4335, 4346, 4358, 4405, 4417, 4428, 4440, 4462, 4474, 4485, 4497, 4253,
113 4265, 4276, 4288, 4310, 4322, 4333, 4345, 4392, 4404, 4415, 4427, 4449, 4461,
114 4472, 4484, 4546, 4558, 4569, 4581, 4603, 4615, 4626, 4638, 4685, 4697, 4708,
115 4720, 4742, 4754, 4765, 4777, 4848, 4860, 4871, 4883, 4905, 4917, 4928, 4940,
116 4987, 4999, 5010, 5022, 5044, 5056, 5067, 5079, 5141, 5153, 5164, 5176, 5198,
117 5210, 5221, 5233, 5280, 5292, 5303, 5315, 5337, 5349, 5360, 5372, 4988, 5000,
118 5011, 5023, 5045, 5057, 5068, 5080, 5127, 5139, 5150, 5162, 5184, 5196, 5207,
119 5219, 5281, 5293, 5304, 5316, 5338, 5350, 5361, 5373, 5420, 5432, 5443, 5455,
120 5477, 5489, 5500, 5512, 5583, 5595, 5606, 5618, 5640, 5652, 5663, 5675, 5722,
121 5734, 5745, 5757, 5779, 5791, 5802, 5814, 5876, 5888, 5899, 5911, 5933, 5945,
122 5956, 5968, 6015, 6027, 6038, 6050, 6072, 6084, 6095, 6107, 5863, 5875, 5886,
123 5898, 5920, 5932, 5943, 5955, 6002, 6014, 6025, 6037, 6059, 6071, 6082, 6094,
124 6156, 6168, 6179, 6191, 6213, 6225, 6236, 6248, 6295, 6307, 6318, 6330, 6352,
125 6364, 6375, 6387, 6458, 6470, 6481, 6493, 6515, 6527, 6538, 6550, 6597, 6609,
126 6620, 6632, 6654, 6666, 6677, 6689, 6751, 6763, 6774, 6786, 6808, 6820, 6831,
127 6843, 6890, 6902, 6913, 6925, 6947, 6959, 6970, 6982
128};
Yaowu Xuf883b422016-08-30 14:01:10 -0700129const int av1_cat6_high_cost[64] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130 88, 2251, 2727, 4890, 3148, 5311, 5787, 7950, 3666, 5829, 6305,
131 8468, 6726, 8889, 9365, 11528, 3666, 5829, 6305, 8468, 6726, 8889,
132 9365, 11528, 7244, 9407, 9883, 12046, 10304, 12467, 12943, 15106, 3666,
133 5829, 6305, 8468, 6726, 8889, 9365, 11528, 7244, 9407, 9883, 12046,
134 10304, 12467, 12943, 15106, 7244, 9407, 9883, 12046, 10304, 12467, 12943,
135 15106, 10822, 12985, 13461, 15624, 13882, 16045, 16521, 18684
136};
137
Yaowu Xuf883b422016-08-30 14:01:10 -0700138#if CONFIG_AOM_HIGHBITDEPTH
139const int av1_cat6_high10_high_cost[256] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140 94, 2257, 2733, 4896, 3154, 5317, 5793, 7956, 3672, 5835, 6311,
141 8474, 6732, 8895, 9371, 11534, 3672, 5835, 6311, 8474, 6732, 8895,
142 9371, 11534, 7250, 9413, 9889, 12052, 10310, 12473, 12949, 15112, 3672,
143 5835, 6311, 8474, 6732, 8895, 9371, 11534, 7250, 9413, 9889, 12052,
144 10310, 12473, 12949, 15112, 7250, 9413, 9889, 12052, 10310, 12473, 12949,
145 15112, 10828, 12991, 13467, 15630, 13888, 16051, 16527, 18690, 4187, 6350,
146 6826, 8989, 7247, 9410, 9886, 12049, 7765, 9928, 10404, 12567, 10825,
147 12988, 13464, 15627, 7765, 9928, 10404, 12567, 10825, 12988, 13464, 15627,
148 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 7765, 9928, 10404,
149 12567, 10825, 12988, 13464, 15627, 11343, 13506, 13982, 16145, 14403, 16566,
150 17042, 19205, 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 14921,
151 17084, 17560, 19723, 17981, 20144, 20620, 22783, 4187, 6350, 6826, 8989,
152 7247, 9410, 9886, 12049, 7765, 9928, 10404, 12567, 10825, 12988, 13464,
153 15627, 7765, 9928, 10404, 12567, 10825, 12988, 13464, 15627, 11343, 13506,
154 13982, 16145, 14403, 16566, 17042, 19205, 7765, 9928, 10404, 12567, 10825,
155 12988, 13464, 15627, 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205,
156 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 14921, 17084, 17560,
157 19723, 17981, 20144, 20620, 22783, 8280, 10443, 10919, 13082, 11340, 13503,
158 13979, 16142, 11858, 14021, 14497, 16660, 14918, 17081, 17557, 19720, 11858,
159 14021, 14497, 16660, 14918, 17081, 17557, 19720, 15436, 17599, 18075, 20238,
160 18496, 20659, 21135, 23298, 11858, 14021, 14497, 16660, 14918, 17081, 17557,
161 19720, 15436, 17599, 18075, 20238, 18496, 20659, 21135, 23298, 15436, 17599,
162 18075, 20238, 18496, 20659, 21135, 23298, 19014, 21177, 21653, 23816, 22074,
163 24237, 24713, 26876
164};
Yaowu Xuf883b422016-08-30 14:01:10 -0700165const int av1_cat6_high12_high_cost[1024] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700166 100, 2263, 2739, 4902, 3160, 5323, 5799, 7962, 3678, 5841, 6317,
167 8480, 6738, 8901, 9377, 11540, 3678, 5841, 6317, 8480, 6738, 8901,
168 9377, 11540, 7256, 9419, 9895, 12058, 10316, 12479, 12955, 15118, 3678,
169 5841, 6317, 8480, 6738, 8901, 9377, 11540, 7256, 9419, 9895, 12058,
170 10316, 12479, 12955, 15118, 7256, 9419, 9895, 12058, 10316, 12479, 12955,
171 15118, 10834, 12997, 13473, 15636, 13894, 16057, 16533, 18696, 4193, 6356,
172 6832, 8995, 7253, 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831,
173 12994, 13470, 15633, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633,
174 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410,
175 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572,
176 17048, 19211, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927,
177 17090, 17566, 19729, 17987, 20150, 20626, 22789, 4193, 6356, 6832, 8995,
178 7253, 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831, 12994, 13470,
179 15633, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512,
180 13988, 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410, 12573, 10831,
181 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211,
182 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566,
183 19729, 17987, 20150, 20626, 22789, 8286, 10449, 10925, 13088, 11346, 13509,
184 13985, 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864,
185 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244,
186 18502, 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
187 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605,
188 18081, 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080,
189 24243, 24719, 26882, 4193, 6356, 6832, 8995, 7253, 9416, 9892, 12055,
190 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 7771, 9934, 10410,
191 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572,
192 17048, 19211, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349,
193 13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349, 13512, 13988, 16151,
194 14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729, 17987, 20150, 20626,
195 22789, 8286, 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027,
196 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924,
197 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
198 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081,
199 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665,
200 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 8286,
201 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666,
202 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
203 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027,
204 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502,
205 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
206 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542, 15018,
207 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180,
208 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535,
209 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759,
210 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
211 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276,
212 25752, 27915, 26173, 28336, 28812, 30975, 4193, 6356, 6832, 8995, 7253,
213 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633,
214 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988,
215 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410, 12573, 10831, 12994,
216 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349,
217 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729,
218 17987, 20150, 20626, 22789, 8286, 10449, 10925, 13088, 11346, 13509, 13985,
219 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027,
220 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502,
221 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
222 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081,
223 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243,
224 24719, 26882, 8286, 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864,
225 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666,
226 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141,
227 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605,
228 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502,
229 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882,
230 12379, 14542, 15018, 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596,
231 20759, 19017, 21180, 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180,
232 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957,
233 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337,
234 22595, 24758, 25234, 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
235 27397, 23113, 25276, 25752, 27915, 26173, 28336, 28812, 30975, 8286, 10449,
236 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666, 14924,
237 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
238 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027, 14503,
239 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665,
240 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 19020,
241 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542, 15018, 17181,
242 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
243 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698,
244 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759, 19017,
245 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397,
246 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276, 25752,
247 27915, 26173, 28336, 28812, 30975, 12379, 14542, 15018, 17181, 15439, 17602,
248 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 15957,
249 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337,
250 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
251 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 19535, 21698,
252 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276, 25752, 27915, 26173,
253 28336, 28812, 30975, 16472, 18635, 19111, 21274, 19532, 21695, 22171, 24334,
254 20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 20050, 22213, 22689,
255 24852, 23110, 25273, 25749, 27912, 23628, 25791, 26267, 28430, 26688, 28851,
256 29327, 31490, 20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 23628,
257 25791, 26267, 28430, 26688, 28851, 29327, 31490, 23628, 25791, 26267, 28430,
258 26688, 28851, 29327, 31490, 27206, 29369, 29845, 32008, 30266, 32429, 32905,
259 35068
260};
261#endif
262
Yaowu Xuf883b422016-08-30 14:01:10 -0700263const av1_extra_bit av1_extra_bits[ENTROPY_TOKENS] = {
Alex Converse81fd8902016-07-26 15:35:42 -0700264 { 0, 0, 0, zero_cost }, // ZERO_TOKEN
265 { 0, 0, 1, sign_cost }, // ONE_TOKEN
266 { 0, 0, 2, sign_cost }, // TWO_TOKEN
267 { 0, 0, 3, sign_cost }, // THREE_TOKEN
268 { 0, 0, 4, sign_cost }, // FOUR_TOKEN
269 { av1_cat1_prob, 1, CAT1_MIN_VAL, cat1_cost }, // CATEGORY1_TOKEN
270 { av1_cat2_prob, 2, CAT2_MIN_VAL, cat2_cost }, // CATEGORY2_TOKEN
271 { av1_cat3_prob, 3, CAT3_MIN_VAL, cat3_cost }, // CATEGORY3_TOKEN
272 { av1_cat4_prob, 4, CAT4_MIN_VAL, cat4_cost }, // CATEGORY4_TOKEN
273 { av1_cat5_prob, 5, CAT5_MIN_VAL, cat5_cost }, // CATEGORY5_TOKEN
274 { av1_cat6_prob, 14, CAT6_MIN_VAL, 0 }, // CATEGORY6_TOKEN
275 { 0, 0, 0, zero_cost } // EOB_TOKEN
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276};
277
Yaowu Xuf883b422016-08-30 14:01:10 -0700278#if CONFIG_AOM_HIGHBITDEPTH
279const av1_extra_bit av1_extra_bits_high10[ENTROPY_TOKENS] = {
Alex Converse81fd8902016-07-26 15:35:42 -0700280 { 0, 0, 0, zero_cost }, // ZERO
281 { 0, 0, 1, sign_cost }, // ONE
282 { 0, 0, 2, sign_cost }, // TWO
283 { 0, 0, 3, sign_cost }, // THREE
284 { 0, 0, 4, sign_cost }, // FOUR
285 { av1_cat1_prob_high10, 1, CAT1_MIN_VAL, cat1_cost }, // CAT1
286 { av1_cat2_prob_high10, 2, CAT2_MIN_VAL, cat2_cost }, // CAT2
287 { av1_cat3_prob_high10, 3, CAT3_MIN_VAL, cat3_cost }, // CAT3
288 { av1_cat4_prob_high10, 4, CAT4_MIN_VAL, cat4_cost }, // CAT4
289 { av1_cat5_prob_high10, 5, CAT5_MIN_VAL, cat5_cost }, // CAT5
290 { av1_cat6_prob_high10, 16, CAT6_MIN_VAL, 0 }, // CAT6
291 { 0, 0, 0, zero_cost } // EOB
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292};
Yaowu Xuf883b422016-08-30 14:01:10 -0700293const av1_extra_bit av1_extra_bits_high12[ENTROPY_TOKENS] = {
Alex Converse81fd8902016-07-26 15:35:42 -0700294 { 0, 0, 0, zero_cost }, // ZERO
295 { 0, 0, 1, sign_cost }, // ONE
296 { 0, 0, 2, sign_cost }, // TWO
297 { 0, 0, 3, sign_cost }, // THREE
298 { 0, 0, 4, sign_cost }, // FOUR
299 { av1_cat1_prob_high12, 1, CAT1_MIN_VAL, cat1_cost }, // CAT1
300 { av1_cat2_prob_high12, 2, CAT2_MIN_VAL, cat2_cost }, // CAT2
301 { av1_cat3_prob_high12, 3, CAT3_MIN_VAL, cat3_cost }, // CAT3
302 { av1_cat4_prob_high12, 4, CAT4_MIN_VAL, cat4_cost }, // CAT4
303 { av1_cat5_prob_high12, 5, CAT5_MIN_VAL, cat5_cost }, // CAT5
304 { av1_cat6_prob_high12, 18, CAT6_MIN_VAL, 0 }, // CAT6
305 { 0, 0, 0, zero_cost } // EOB
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306};
307#endif
308
Alex Converse1e4e29f2016-11-08 14:12:14 -0800309#if !CONFIG_EC_MULTISYMBOL
Yaowu Xuf883b422016-08-30 14:01:10 -0700310const struct av1_token av1_coef_encodings[ENTROPY_TOKENS] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700311 { 2, 2 }, { 6, 3 }, { 28, 5 }, { 58, 6 }, { 59, 6 }, { 60, 6 },
312 { 61, 6 }, { 124, 7 }, { 125, 7 }, { 126, 7 }, { 127, 7 }, { 0, 1 }
313};
Alex Converse1e4e29f2016-11-08 14:12:14 -0800314#endif // !CONFIG_EC_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315
316struct tokenize_b_args {
Urvang Joshi52648442016-10-13 17:27:51 -0700317 const AV1_COMP *cpi;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318 ThreadData *td;
319 TOKENEXTRA **tp;
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700320 int this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700321};
322
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800323#if !CONFIG_PVQ
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700324static void cost_coeffs_b(int plane, int block, int blk_row, int blk_col,
325 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
326 struct tokenize_b_args *const args = arg;
Angie Chiang22ba7512016-10-20 17:10:33 -0700327 const AV1_COMMON *cm = &args->cpi->common;
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700328 ThreadData *const td = args->td;
329 MACROBLOCK *const x = &td->mb;
330 MACROBLOCKD *const xd = &x->e_mbd;
331 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
332 struct macroblock_plane *p = &x->plane[plane];
333 struct macroblockd_plane *pd = &xd->plane[plane];
334 const PLANE_TYPE type = pd->plane_type;
335 const int ref = is_inter_block(mbmi);
336 const TX_TYPE tx_type = get_tx_type(type, xd, block, tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700337 const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, ref);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700338 int pt = get_entropy_context(tx_size, pd->above_context + blk_col,
339 pd->left_context + blk_row);
Angie Chiang22ba7512016-10-20 17:10:33 -0700340 int rate = av1_cost_coeffs(cm, x, plane, block, pt, tx_size, scan_order->scan,
Urvang Joshi03f6fdc2016-10-14 15:53:39 -0700341 scan_order->neighbors, 0);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700342 args->this_rate += rate;
Jingning Hana6923f72016-07-15 08:50:14 -0700343 (void)plane_bsize;
344 av1_set_contexts(xd, pd, tx_size, p->eobs[block] > 0, blk_col, blk_row);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700345}
346
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347static void set_entropy_context_b(int plane, int block, int blk_row,
348 int blk_col, BLOCK_SIZE plane_bsize,
349 TX_SIZE tx_size, void *arg) {
350 struct tokenize_b_args *const args = arg;
351 ThreadData *const td = args->td;
352 MACROBLOCK *const x = &td->mb;
353 MACROBLOCKD *const xd = &x->e_mbd;
354 struct macroblock_plane *p = &x->plane[plane];
355 struct macroblockd_plane *pd = &xd->plane[plane];
Jingning Hana6923f72016-07-15 08:50:14 -0700356 (void)plane_bsize;
357 av1_set_contexts(xd, pd, tx_size, p->eobs[block] > 0, blk_col, blk_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700358}
359
Yaowu Xuf883b422016-08-30 14:01:10 -0700360static INLINE void add_token(TOKENEXTRA **t, const aom_prob *context_tree,
Alex Converseaca9feb2016-10-10 11:08:10 -0700361#if CONFIG_EC_MULTISYMBOL
Thomas9ac55082016-09-23 18:04:17 +0100362 aom_cdf_prob (*token_cdf)[ENTROPY_TOKENS],
Alex Converseaca9feb2016-10-10 11:08:10 -0700363#endif // CONFIG_EC_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364 int32_t extra, uint8_t token,
365 uint8_t skip_eob_node, unsigned int *counts) {
366 (*t)->token = token;
367 (*t)->extra = extra;
368 (*t)->context_tree = context_tree;
Alex Converseaca9feb2016-10-10 11:08:10 -0700369#if CONFIG_EC_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700370 (*t)->token_cdf = token_cdf;
Alex Converseaca9feb2016-10-10 11:08:10 -0700371#endif // CONFIG_EC_MULTISYMBOL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700372 (*t)->skip_eob_node = skip_eob_node;
373 (*t)++;
374 ++counts[token];
375}
376
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377static INLINE int get_tx_eob(const struct segmentation *seg, int segment_id,
378 TX_SIZE tx_size) {
Jingning Hana8939362016-10-25 11:57:46 -0700379 const int eob_max = tx_size_2d[tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700380 return segfeature_active(seg, segment_id, SEG_LVL_SKIP) ? 0 : eob_max;
381}
382
Urvang Joshib100db72016-10-12 16:28:56 -0700383#if CONFIG_PALETTE
Urvang Joshi967ff392016-09-07 14:57:49 -0700384void av1_tokenize_palette_sb(const AV1_COMP *cpi,
385 const struct ThreadData *const td, int plane,
386 TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
387 int *rate) {
388 const MACROBLOCK *const x = &td->mb;
389 const MACROBLOCKD *const xd = &x->e_mbd;
390 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
391 const uint8_t *const color_map = xd->plane[plane != 0].color_index_map;
392 const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
393 const int n = pmi->palette_size[plane != 0];
394 int i, j;
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700395 int this_rate = 0;
Urvang Joshi967ff392016-09-07 14:57:49 -0700396 uint8_t color_order[PALETTE_MAX_SIZE];
Jingning Hanae5cfde2016-11-30 12:01:44 -0800397 const int rows =
398 block_size_high[bsize] >> (xd->plane[plane != 0].subsampling_y);
399 const int cols =
400 block_size_wide[bsize] >> (xd->plane[plane != 0].subsampling_x);
clang-format67948d32016-09-07 22:40:40 -0700401 const aom_prob(*const probs)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
Yaowu Xuf883b422016-08-30 14:01:10 -0700402 plane == 0 ? av1_default_palette_y_color_prob
403 : av1_default_palette_uv_color_prob;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404
405 for (i = 0; i < rows; ++i) {
406 for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
Urvang Joshi967ff392016-09-07 14:57:49 -0700407 int color_new_idx;
408 const int color_ctx = av1_get_palette_color_context(
409 color_map, cols, i, j, n, color_order, &color_new_idx);
410 assert(color_new_idx >= 0 && color_new_idx < n);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700411 if (dry_run == DRY_RUN_COSTCOEFFS)
Urvang Joshi967ff392016-09-07 14:57:49 -0700412 this_rate += cpi->palette_y_color_cost[n - 2][color_ctx][color_new_idx];
413 (*t)->token = color_new_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700414 (*t)->context_tree = probs[n - 2][color_ctx];
415 (*t)->skip_eob_node = 0;
416 ++(*t);
417 }
418 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700419 if (rate) *rate += this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700420}
Urvang Joshib100db72016-10-12 16:28:56 -0700421#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700422
423static void tokenize_b(int plane, int block, int blk_row, int blk_col,
424 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
425 struct tokenize_b_args *const args = arg;
Urvang Joshi52648442016-10-13 17:27:51 -0700426 const AV1_COMP *cpi = args->cpi;
Angie Chiangff6d8902016-10-21 11:02:09 -0700427 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700428 ThreadData *const td = args->td;
429 MACROBLOCK *const x = &td->mb;
430 MACROBLOCKD *const xd = &x->e_mbd;
431 TOKENEXTRA **tp = args->tp;
432 uint8_t token_cache[MAX_TX_SQUARE];
433 struct macroblock_plane *p = &x->plane[plane];
434 struct macroblockd_plane *pd = &xd->plane[plane];
435 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
436 int pt; /* near block/prev token context index */
437 int c;
438 TOKENEXTRA *t = *tp; /* store tokens starting here */
439 int eob = p->eobs[block];
440 const PLANE_TYPE type = pd->plane_type;
441 const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
442#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700443 const int segment_id = AOMMIN(mbmi->segment_id, mbmi->segment_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700444#else
445 const int segment_id = mbmi->segment_id;
446#endif // CONFIG_SUEPRTX
447 const int16_t *scan, *nb;
448 const TX_TYPE tx_type = get_tx_type(type, xd, block, tx_size);
Urvang Joshi03f6fdc2016-10-14 15:53:39 -0700449 const SCAN_ORDER *const scan_order =
Angie Chiangff6d8902016-10-21 11:02:09 -0700450 get_scan(cm, tx_size, tx_type, is_inter_block(mbmi));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700451 const int ref = is_inter_block(mbmi);
clang-format67948d32016-09-07 22:40:40 -0700452 unsigned int(*const counts)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700453 td->rd_counts.coef_counts[txsize_sqr_map[tx_size]][type][ref];
454#if CONFIG_ENTROPY
Urvang Joshi52648442016-10-13 17:27:51 -0700455 const aom_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700456 cpi->subframe_stats.coef_probs_buf[cpi->common.coef_probs_update_idx]
457 [txsize_sqr_map[tx_size]][type][ref];
458#else
clang-format67948d32016-09-07 22:40:40 -0700459 aom_prob(*const coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700460 cpi->common.fc->coef_probs[txsize_sqr_map[tx_size]][type][ref];
461#endif // CONFIG_ENTROPY
Alex Converseaca9feb2016-10-10 11:08:10 -0700462#if CONFIG_EC_MULTISYMBOL
Alex Converse9ed1a2f2016-09-04 13:30:43 +0200463 aom_cdf_prob(*const coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
464 cpi->common.fc->coef_cdfs[tx_size][type][ref];
Alex Converseec6fb642016-10-19 11:31:48 -0700465#endif
clang-format67948d32016-09-07 22:40:40 -0700466 unsigned int(*const eob_branch)[COEFF_CONTEXTS] =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700467 td->counts->eob_branch[txsize_sqr_map[tx_size]][type][ref];
468 const uint8_t *const band = get_band_translate(tx_size);
469 const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
470 int skip_eob = 0;
471 int16_t token;
472 EXTRABIT extra;
Jingning Hana6923f72016-07-15 08:50:14 -0700473 (void)plane_bsize;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700474 pt = get_entropy_context(tx_size, pd->above_context + blk_col,
475 pd->left_context + blk_row);
Urvang Joshi03f6fdc2016-10-14 15:53:39 -0700476 scan = scan_order->scan;
477 nb = scan_order->neighbors;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700478 c = 0;
479
480 while (c < eob) {
481 const int v = qcoeff[scan[c]];
482 eob_branch[band[c]][pt] += !skip_eob;
483
Yaowu Xuf883b422016-08-30 14:01:10 -0700484 av1_get_token_extra(v, &token, &extra);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700485
486 add_token(&t, coef_probs[band[c]][pt],
Alex Converseaca9feb2016-10-10 11:08:10 -0700487#if CONFIG_EC_MULTISYMBOL
Thomas9ac55082016-09-23 18:04:17 +0100488 &coef_cdfs[band[c]][pt],
Alex Converseec6fb642016-10-19 11:31:48 -0700489#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700490 extra, (uint8_t)token, (uint8_t)skip_eob, counts[band[c]][pt]);
491
Yaowu Xuf883b422016-08-30 14:01:10 -0700492 token_cache[scan[c]] = av1_pt_energy_class[token];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700493 ++c;
494 pt = get_coef_context(nb, token_cache, c);
495 skip_eob = (token == ZERO_TOKEN);
496 }
497 if (c < seg_eob) {
Alex Converse4ce69de2016-09-04 11:16:34 +0200498 add_token(&t, coef_probs[band[c]][pt],
Alex Converseaca9feb2016-10-10 11:08:10 -0700499#if CONFIG_EC_MULTISYMBOL
Alex Converse4ce69de2016-09-04 11:16:34 +0200500 NULL,
501#endif
502 0, EOB_TOKEN, 0, counts[band[c]][pt]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700503 ++eob_branch[band[c]][pt];
504 }
505
iole moccagattaf25a4cf2016-11-11 23:57:57 -0800506#if CONFIG_COEF_INTERLEAVE
507 t->token = EOSB_TOKEN;
508 t++;
509#endif
510
Yaowu Xuc27fc142016-08-22 16:08:15 -0700511 *tp = t;
512
Angie Chianged8cd9a2016-10-21 16:44:47 -0700513#if CONFIG_ADAPT_SCAN
514 // Since dqcoeff is not available here, we pass qcoeff into
515 // av1_update_scan_count_facade(). The update behavior should be the same
516 // because av1_update_scan_count_facade() only cares if coefficients are zero
517 // or not.
518 av1_update_scan_count_facade((AV1_COMMON *)cm, tx_size, tx_type, qcoeff, c);
519#endif
520
Jingning Hana6923f72016-07-15 08:50:14 -0700521 av1_set_contexts(xd, pd, tx_size, c > 0, blk_col, blk_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522}
Yushin Cho77bba8d2016-11-04 16:36:56 -0700523#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700524
525struct is_skippable_args {
526 uint16_t *eobs;
527 int *skippable;
528};
529static void is_skippable(int plane, int block, int blk_row, int blk_col,
530 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *argv) {
531 struct is_skippable_args *args = argv;
532 (void)plane;
533 (void)plane_bsize;
534 (void)tx_size;
535 (void)blk_row;
536 (void)blk_col;
537 args->skippable[0] &= (!args->eobs[block]);
538}
539
540// TODO(yaowu): rewrite and optimize this function to remove the usage of
Yaowu Xuf883b422016-08-30 14:01:10 -0700541// av1_foreach_transform_block() and simplify is_skippable().
542int av1_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543 int result = 1;
544 struct is_skippable_args args = { x->plane[plane].eobs, &result };
Yaowu Xuf883b422016-08-30 14:01:10 -0700545 av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane, is_skippable,
546 &args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547 return result;
548}
549
550static void has_high_freq_coeff(int plane, int block, int blk_row, int blk_col,
551 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
552 void *argv) {
553 struct is_skippable_args *args = argv;
554 int eobs = (tx_size == TX_4X4) ? 3 : 10;
555 (void)plane;
556 (void)plane_bsize;
557 (void)blk_row;
558 (void)blk_col;
559
560 *(args->skippable) |= (args->eobs[block] > eobs);
561}
562
Yaowu Xuf883b422016-08-30 14:01:10 -0700563int av1_has_high_freq_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564 int result = 0;
565 struct is_skippable_args args = { x->plane[plane].eobs, &result };
Yaowu Xuf883b422016-08-30 14:01:10 -0700566 av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane,
567 has_high_freq_coeff, &args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568 return result;
569}
Yushin Cho77bba8d2016-11-04 16:36:56 -0700570#if CONFIG_PVQ
571void add_pvq_block(AV1_COMMON *const cm, MACROBLOCK *const x, PVQ_INFO *pvq) {
572 PVQ_QUEUE *q = x->pvq_q;
573 if (q->curr_pos >= q->buf_len) {
574 q->buf_len = 2 * q->buf_len + 1;
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800575 CHECK_MEM_ERROR(cm, q->buf,
576 aom_realloc(q->buf, q->buf_len * sizeof(PVQ_INFO)));
Yushin Cho77bba8d2016-11-04 16:36:56 -0700577 }
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800578 // memcpy(q->buf + q->curr_pos, pvq, sizeof(PVQ_INFO));
Yushin Cho77bba8d2016-11-04 16:36:56 -0700579 OD_COPY(q->buf + q->curr_pos, pvq, 1);
580 ++q->curr_pos;
581}
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582
Yushin Cho77bba8d2016-11-04 16:36:56 -0700583// NOTE: This does not actually generate tokens, instead we store the encoding
584// decisions made for PVQ in a queue that we will read from when
585// actually writing the bitstream in write_modes_b
586static void tokenize_pvq(int plane, int block, int blk_row, int blk_col,
587 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
588 struct tokenize_b_args *const args = arg;
589 const AV1_COMP *cpi = args->cpi;
590 const AV1_COMMON *const cm = &cpi->common;
591 ThreadData *const td = args->td;
592 MACROBLOCK *const x = &td->mb;
593 PVQ_INFO *pvq_info;
594
595 (void)block;
596 (void)blk_row;
597 (void)blk_col;
598 (void)plane_bsize;
599 (void)tx_size;
600
601 assert(block < MAX_PVQ_BLOCKS_IN_SB);
602 pvq_info = &x->pvq[block][plane];
Yaowu Xud6ea71c2016-11-07 10:24:14 -0800603 add_pvq_block((AV1_COMMON * const)cm, x, pvq_info);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700604}
605#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606#if CONFIG_VAR_TX
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700607void tokenize_vartx(ThreadData *td, TOKENEXTRA **t, RUN_TYPE dry_run,
Debargha Mukherjee3c42c092016-09-29 09:17:36 -0700608 TX_SIZE tx_size, BLOCK_SIZE plane_bsize, int blk_row,
609 int blk_col, int block, int plane, void *arg) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610 MACROBLOCK *const x = &td->mb;
611 MACROBLOCKD *const xd = &x->e_mbd;
612 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
613 const struct macroblockd_plane *const pd = &xd->plane[plane];
614 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
615 const int tx_row = blk_row >> (1 - pd->subsampling_y);
616 const int tx_col = blk_col >> (1 - pd->subsampling_x);
Jingning Hana9336322016-11-02 15:45:07 -0700617 const int max_blocks_high = max_block_high(xd, plane_bsize, plane);
618 const int max_blocks_wide = max_block_wide(xd, plane_bsize, plane);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619 TX_SIZE plane_tx_size;
620
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
622
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700623 plane_tx_size =
624 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
625 : mbmi->inter_tx_size[tx_row][tx_col];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626
627 if (tx_size == plane_tx_size) {
Urvang Joshi368fbc92016-10-17 16:31:34 -0700628 plane_bsize = get_plane_block_size(mbmi->sb_type, pd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629 if (!dry_run)
630 tokenize_b(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700631 else if (dry_run == DRY_RUN_NORMAL)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632 set_entropy_context_b(plane, block, blk_row, blk_col, plane_bsize,
633 tx_size, arg);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700634 else if (dry_run == DRY_RUN_COSTCOEFFS)
635 cost_coeffs_b(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636 } else {
Jingning Hana8939362016-10-25 11:57:46 -0700637 // Half the block size in transform block unit.
Jingning Hana9336322016-11-02 15:45:07 -0700638 const TX_SIZE sub_txs = sub_tx_size_map[tx_size];
639 const int bsl = tx_size_wide_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700640 int i;
641
642 assert(bsl > 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700643
644 for (i = 0; i < 4; ++i) {
Jingning Hana8939362016-10-25 11:57:46 -0700645 const int offsetr = blk_row + ((i >> 1) * bsl);
646 const int offsetc = blk_col + ((i & 0x01) * bsl);
647
Jingning Hana8939362016-10-25 11:57:46 -0700648 int step = tx_size_wide_unit[sub_txs] * tx_size_high_unit[sub_txs];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700649
650 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
651
Jingning Hana8939362016-10-25 11:57:46 -0700652 tokenize_vartx(td, t, dry_run, sub_txs, plane_bsize, offsetr, offsetc,
Jingning Han98d6a1f2016-11-03 12:47:47 -0700653 block, plane, arg);
654 block += step;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655 }
656 }
657}
658
Urvang Joshi52648442016-10-13 17:27:51 -0700659void av1_tokenize_sb_vartx(const AV1_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700660 RUN_TYPE dry_run, int mi_row, int mi_col,
661 BLOCK_SIZE bsize, int *rate) {
Urvang Joshi52648442016-10-13 17:27:51 -0700662 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700663 MACROBLOCK *const x = &td->mb;
664 MACROBLOCKD *const xd = &x->e_mbd;
665 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
666 TOKENEXTRA *t_backup = *t;
Yaowu Xuf883b422016-08-30 14:01:10 -0700667 const int ctx = av1_get_skip_context(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668 const int skip_inc =
669 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700670 struct tokenize_b_args arg = { cpi, td, t, 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700671 int plane;
672 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
673
674 if (mbmi->skip) {
675 if (!dry_run) td->counts->skip[ctx][1] += skip_inc;
676 reset_skip_context(xd, bsize);
677 if (dry_run) *t = t_backup;
678 return;
679 }
680
681 if (!dry_run)
682 td->counts->skip[ctx][0] += skip_inc;
683 else
684 *t = t_backup;
685
686 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
687 const struct macroblockd_plane *const pd = &xd->plane[plane];
688 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
Jingning Hana8939362016-10-25 11:57:46 -0700689 const int mi_width = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
690 const int mi_height = block_size_high[plane_bsize] >> tx_size_wide_log2[0];
Jingning Han70e5f3f2016-11-09 17:03:07 -0800691 const TX_SIZE max_tx_size = max_txsize_rect_lookup[plane_bsize];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700692 const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
Jingning Hana8939362016-10-25 11:57:46 -0700693 int bw = block_size_wide[txb_size] >> tx_size_wide_log2[0];
694 int bh = block_size_high[txb_size] >> tx_size_wide_log2[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700695 int idx, idy;
696 int block = 0;
Jingning Hana8939362016-10-25 11:57:46 -0700697 int step = tx_size_wide_unit[max_tx_size] * tx_size_high_unit[max_tx_size];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698 for (idy = 0; idy < mi_height; idy += bh) {
Jingning Hana8939362016-10-25 11:57:46 -0700699 for (idx = 0; idx < mi_width; idx += bw) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -0700700 tokenize_vartx(td, t, dry_run, max_tx_size, plane_bsize, idy, idx,
701 block, plane, &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 block += step;
703 }
704 }
705
706 if (!dry_run) {
707 (*t)->token = EOSB_TOKEN;
708 (*t)++;
709 }
710 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700711 if (rate) *rate += arg.this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700712}
713#endif // CONFIG_VAR_TX
714
Urvang Joshi52648442016-10-13 17:27:51 -0700715void av1_tokenize_sb(const AV1_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700716 RUN_TYPE dry_run, BLOCK_SIZE bsize, int *rate) {
Urvang Joshi52648442016-10-13 17:27:51 -0700717 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718 MACROBLOCK *const x = &td->mb;
719 MACROBLOCKD *const xd = &x->e_mbd;
720 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700721 const int ctx = av1_get_skip_context(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722 const int skip_inc =
723 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700724 struct tokenize_b_args arg = { cpi, td, t, 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700725 if (mbmi->skip) {
726 if (!dry_run) td->counts->skip[ctx][1] += skip_inc;
727 reset_skip_context(xd, bsize);
728 return;
729 }
730
Yushin Cho77bba8d2016-11-04 16:36:56 -0700731#if !CONFIG_PVQ
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 if (!dry_run) {
iole moccagattaf25a4cf2016-11-11 23:57:57 -0800733#if CONFIG_COEF_INTERLEAVE
734 td->counts->skip[ctx][0] += skip_inc;
735 av1_foreach_transformed_block_interleave(xd, bsize, tokenize_b, &arg);
736#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737 int plane;
738
739 td->counts->skip[ctx][0] += skip_inc;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700741 av1_foreach_transformed_block_in_plane(xd, bsize, plane, tokenize_b,
742 &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 (*t)->token = EOSB_TOKEN;
744 (*t)++;
745 }
iole moccagattaf25a4cf2016-11-11 23:57:57 -0800746#endif
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700747 } else if (dry_run == DRY_RUN_NORMAL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700748 av1_foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700749 } else if (dry_run == DRY_RUN_COSTCOEFFS) {
750 av1_foreach_transformed_block(xd, bsize, cost_coeffs_b, &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700751 }
Yushin Cho77bba8d2016-11-04 16:36:56 -0700752#else
753 if (!dry_run) {
754 int plane;
755
756 td->counts->skip[ctx][0] += skip_inc;
757
758 for (plane = 0; plane < MAX_MB_PLANE; ++plane)
759 av1_foreach_transformed_block_in_plane(xd, bsize, plane, tokenize_pvq,
760 &arg);
761 }
762#endif
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700763 if (rate) *rate += arg.this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700764}
765
766#if CONFIG_SUPERTX
Urvang Joshi52648442016-10-13 17:27:51 -0700767void av1_tokenize_sb_supertx(const AV1_COMP *cpi, ThreadData *td,
768 TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
769 int *rate) {
770 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771 MACROBLOCKD *const xd = &td->mb.e_mbd;
772 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
773 TOKENEXTRA *t_backup = *t;
Yaowu Xuf883b422016-08-30 14:01:10 -0700774 const int ctx = av1_get_skip_context(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700775 const int skip_inc =
776 !segfeature_active(&cm->seg, mbmi->segment_id_supertx, SEG_LVL_SKIP);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700777 struct tokenize_b_args arg = { cpi, td, t, 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 if (mbmi->skip) {
779 if (!dry_run) td->counts->skip[ctx][1] += skip_inc;
780 reset_skip_context(xd, bsize);
781 if (dry_run) *t = t_backup;
782 return;
783 }
784
785 if (!dry_run) {
786 int plane;
787 td->counts->skip[ctx][0] += skip_inc;
788
789 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700790 av1_foreach_transformed_block_in_plane(xd, bsize, plane, tokenize_b,
791 &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700792 (*t)->token = EOSB_TOKEN;
793 (*t)++;
794 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700795 } else if (dry_run == DRY_RUN_NORMAL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700796 av1_foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797 *t = t_backup;
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700798 } else if (dry_run == DRY_RUN_COSTCOEFFS) {
799 av1_foreach_transformed_block(xd, bsize, cost_coeffs_b, &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700801 if (rate) *rate += arg.this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802}
803#endif // CONFIG_SUPERTX