blob: 7707e8f55ab3c26dddfd1df2fb77b855cbdf9e02 [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 Xuf883b422016-08-30 14:01:10 -070093static const aom_tree_index cat1[2] = { 0, 0 };
94static const aom_tree_index cat2[4] = { 2, 2, 0, 0 };
95static const aom_tree_index cat3[6] = { 2, 2, 4, 4, 0, 0 };
96static const aom_tree_index cat4[8] = { 2, 2, 4, 4, 6, 6, 0, 0 };
97static const aom_tree_index cat5[10] = { 2, 2, 4, 4, 6, 6, 8, 8, 0, 0 };
98static const aom_tree_index cat6[28] = { 2, 2, 4, 4, 6, 6, 8, 8, 10, 10,
Yaowu Xuc27fc142016-08-22 16:08:15 -070099 12, 12, 14, 14, 16, 16, 18, 18, 20, 20,
100 22, 22, 24, 24, 26, 26, 0, 0 };
101
102static const int16_t zero_cost[] = { 0 };
103static const int16_t sign_cost[1] = { 512 };
104static const int16_t cat1_cost[1 << 1] = { 864, 1229 };
105static const int16_t cat2_cost[1 << 2] = { 1256, 1453, 1696, 1893 };
106static const int16_t cat3_cost[1 << 3] = { 1652, 1791, 1884, 2023,
107 2195, 2334, 2427, 2566 };
108static const int16_t cat4_cost[1 << 4] = { 2079, 2160, 2218, 2299, 2395, 2476,
109 2534, 2615, 2661, 2742, 2800, 2881,
110 2977, 3058, 3116, 3197 };
111static const int16_t cat5_cost[1 << 5] = {
112 2553, 2576, 2622, 2645, 2703, 2726, 2772, 2795, 2894, 2917, 2963,
113 2986, 3044, 3067, 3113, 3136, 3190, 3213, 3259, 3282, 3340, 3363,
114 3409, 3432, 3531, 3554, 3600, 3623, 3681, 3704, 3750, 3773
115};
Yaowu Xuf883b422016-08-30 14:01:10 -0700116const int16_t av1_cat6_low_cost[256] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700117 3378, 3390, 3401, 3413, 3435, 3447, 3458, 3470, 3517, 3529, 3540, 3552, 3574,
118 3586, 3597, 3609, 3671, 3683, 3694, 3706, 3728, 3740, 3751, 3763, 3810, 3822,
119 3833, 3845, 3867, 3879, 3890, 3902, 3973, 3985, 3996, 4008, 4030, 4042, 4053,
120 4065, 4112, 4124, 4135, 4147, 4169, 4181, 4192, 4204, 4266, 4278, 4289, 4301,
121 4323, 4335, 4346, 4358, 4405, 4417, 4428, 4440, 4462, 4474, 4485, 4497, 4253,
122 4265, 4276, 4288, 4310, 4322, 4333, 4345, 4392, 4404, 4415, 4427, 4449, 4461,
123 4472, 4484, 4546, 4558, 4569, 4581, 4603, 4615, 4626, 4638, 4685, 4697, 4708,
124 4720, 4742, 4754, 4765, 4777, 4848, 4860, 4871, 4883, 4905, 4917, 4928, 4940,
125 4987, 4999, 5010, 5022, 5044, 5056, 5067, 5079, 5141, 5153, 5164, 5176, 5198,
126 5210, 5221, 5233, 5280, 5292, 5303, 5315, 5337, 5349, 5360, 5372, 4988, 5000,
127 5011, 5023, 5045, 5057, 5068, 5080, 5127, 5139, 5150, 5162, 5184, 5196, 5207,
128 5219, 5281, 5293, 5304, 5316, 5338, 5350, 5361, 5373, 5420, 5432, 5443, 5455,
129 5477, 5489, 5500, 5512, 5583, 5595, 5606, 5618, 5640, 5652, 5663, 5675, 5722,
130 5734, 5745, 5757, 5779, 5791, 5802, 5814, 5876, 5888, 5899, 5911, 5933, 5945,
131 5956, 5968, 6015, 6027, 6038, 6050, 6072, 6084, 6095, 6107, 5863, 5875, 5886,
132 5898, 5920, 5932, 5943, 5955, 6002, 6014, 6025, 6037, 6059, 6071, 6082, 6094,
133 6156, 6168, 6179, 6191, 6213, 6225, 6236, 6248, 6295, 6307, 6318, 6330, 6352,
134 6364, 6375, 6387, 6458, 6470, 6481, 6493, 6515, 6527, 6538, 6550, 6597, 6609,
135 6620, 6632, 6654, 6666, 6677, 6689, 6751, 6763, 6774, 6786, 6808, 6820, 6831,
136 6843, 6890, 6902, 6913, 6925, 6947, 6959, 6970, 6982
137};
Yaowu Xuf883b422016-08-30 14:01:10 -0700138const int av1_cat6_high_cost[64] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700139 88, 2251, 2727, 4890, 3148, 5311, 5787, 7950, 3666, 5829, 6305,
140 8468, 6726, 8889, 9365, 11528, 3666, 5829, 6305, 8468, 6726, 8889,
141 9365, 11528, 7244, 9407, 9883, 12046, 10304, 12467, 12943, 15106, 3666,
142 5829, 6305, 8468, 6726, 8889, 9365, 11528, 7244, 9407, 9883, 12046,
143 10304, 12467, 12943, 15106, 7244, 9407, 9883, 12046, 10304, 12467, 12943,
144 15106, 10822, 12985, 13461, 15624, 13882, 16045, 16521, 18684
145};
146
Yaowu Xuf883b422016-08-30 14:01:10 -0700147#if CONFIG_AOM_HIGHBITDEPTH
148const int av1_cat6_high10_high_cost[256] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700149 94, 2257, 2733, 4896, 3154, 5317, 5793, 7956, 3672, 5835, 6311,
150 8474, 6732, 8895, 9371, 11534, 3672, 5835, 6311, 8474, 6732, 8895,
151 9371, 11534, 7250, 9413, 9889, 12052, 10310, 12473, 12949, 15112, 3672,
152 5835, 6311, 8474, 6732, 8895, 9371, 11534, 7250, 9413, 9889, 12052,
153 10310, 12473, 12949, 15112, 7250, 9413, 9889, 12052, 10310, 12473, 12949,
154 15112, 10828, 12991, 13467, 15630, 13888, 16051, 16527, 18690, 4187, 6350,
155 6826, 8989, 7247, 9410, 9886, 12049, 7765, 9928, 10404, 12567, 10825,
156 12988, 13464, 15627, 7765, 9928, 10404, 12567, 10825, 12988, 13464, 15627,
157 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 7765, 9928, 10404,
158 12567, 10825, 12988, 13464, 15627, 11343, 13506, 13982, 16145, 14403, 16566,
159 17042, 19205, 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 14921,
160 17084, 17560, 19723, 17981, 20144, 20620, 22783, 4187, 6350, 6826, 8989,
161 7247, 9410, 9886, 12049, 7765, 9928, 10404, 12567, 10825, 12988, 13464,
162 15627, 7765, 9928, 10404, 12567, 10825, 12988, 13464, 15627, 11343, 13506,
163 13982, 16145, 14403, 16566, 17042, 19205, 7765, 9928, 10404, 12567, 10825,
164 12988, 13464, 15627, 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205,
165 11343, 13506, 13982, 16145, 14403, 16566, 17042, 19205, 14921, 17084, 17560,
166 19723, 17981, 20144, 20620, 22783, 8280, 10443, 10919, 13082, 11340, 13503,
167 13979, 16142, 11858, 14021, 14497, 16660, 14918, 17081, 17557, 19720, 11858,
168 14021, 14497, 16660, 14918, 17081, 17557, 19720, 15436, 17599, 18075, 20238,
169 18496, 20659, 21135, 23298, 11858, 14021, 14497, 16660, 14918, 17081, 17557,
170 19720, 15436, 17599, 18075, 20238, 18496, 20659, 21135, 23298, 15436, 17599,
171 18075, 20238, 18496, 20659, 21135, 23298, 19014, 21177, 21653, 23816, 22074,
172 24237, 24713, 26876
173};
Yaowu Xuf883b422016-08-30 14:01:10 -0700174const int av1_cat6_high12_high_cost[1024] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175 100, 2263, 2739, 4902, 3160, 5323, 5799, 7962, 3678, 5841, 6317,
176 8480, 6738, 8901, 9377, 11540, 3678, 5841, 6317, 8480, 6738, 8901,
177 9377, 11540, 7256, 9419, 9895, 12058, 10316, 12479, 12955, 15118, 3678,
178 5841, 6317, 8480, 6738, 8901, 9377, 11540, 7256, 9419, 9895, 12058,
179 10316, 12479, 12955, 15118, 7256, 9419, 9895, 12058, 10316, 12479, 12955,
180 15118, 10834, 12997, 13473, 15636, 13894, 16057, 16533, 18696, 4193, 6356,
181 6832, 8995, 7253, 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831,
182 12994, 13470, 15633, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633,
183 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410,
184 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572,
185 17048, 19211, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927,
186 17090, 17566, 19729, 17987, 20150, 20626, 22789, 4193, 6356, 6832, 8995,
187 7253, 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831, 12994, 13470,
188 15633, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512,
189 13988, 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410, 12573, 10831,
190 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211,
191 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566,
192 19729, 17987, 20150, 20626, 22789, 8286, 10449, 10925, 13088, 11346, 13509,
193 13985, 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864,
194 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244,
195 18502, 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
196 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605,
197 18081, 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080,
198 24243, 24719, 26882, 4193, 6356, 6832, 8995, 7253, 9416, 9892, 12055,
199 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 7771, 9934, 10410,
200 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572,
201 17048, 19211, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349,
202 13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349, 13512, 13988, 16151,
203 14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729, 17987, 20150, 20626,
204 22789, 8286, 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027,
205 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924,
206 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
207 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081,
208 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665,
209 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 8286,
210 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666,
211 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563,
212 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027,
213 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502,
214 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304,
215 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542, 15018,
216 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180,
217 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535,
218 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759,
219 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
220 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276,
221 25752, 27915, 26173, 28336, 28812, 30975, 4193, 6356, 6832, 8995, 7253,
222 9416, 9892, 12055, 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633,
223 7771, 9934, 10410, 12573, 10831, 12994, 13470, 15633, 11349, 13512, 13988,
224 16151, 14409, 16572, 17048, 19211, 7771, 9934, 10410, 12573, 10831, 12994,
225 13470, 15633, 11349, 13512, 13988, 16151, 14409, 16572, 17048, 19211, 11349,
226 13512, 13988, 16151, 14409, 16572, 17048, 19211, 14927, 17090, 17566, 19729,
227 17987, 20150, 20626, 22789, 8286, 10449, 10925, 13088, 11346, 13509, 13985,
228 16148, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027,
229 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502,
230 20665, 21141, 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
231 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081,
232 20244, 18502, 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243,
233 24719, 26882, 8286, 10449, 10925, 13088, 11346, 13509, 13985, 16148, 11864,
234 14027, 14503, 16666, 14924, 17087, 17563, 19726, 11864, 14027, 14503, 16666,
235 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665, 21141,
236 23304, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726, 15442, 17605,
237 18081, 20244, 18502, 20665, 21141, 23304, 15442, 17605, 18081, 20244, 18502,
238 20665, 21141, 23304, 19020, 21183, 21659, 23822, 22080, 24243, 24719, 26882,
239 12379, 14542, 15018, 17181, 15439, 17602, 18078, 20241, 15957, 18120, 18596,
240 20759, 19017, 21180, 21656, 23819, 15957, 18120, 18596, 20759, 19017, 21180,
241 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 15957,
242 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337,
243 22595, 24758, 25234, 27397, 19535, 21698, 22174, 24337, 22595, 24758, 25234,
244 27397, 23113, 25276, 25752, 27915, 26173, 28336, 28812, 30975, 8286, 10449,
245 10925, 13088, 11346, 13509, 13985, 16148, 11864, 14027, 14503, 16666, 14924,
246 17087, 17563, 19726, 11864, 14027, 14503, 16666, 14924, 17087, 17563, 19726,
247 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 11864, 14027, 14503,
248 16666, 14924, 17087, 17563, 19726, 15442, 17605, 18081, 20244, 18502, 20665,
249 21141, 23304, 15442, 17605, 18081, 20244, 18502, 20665, 21141, 23304, 19020,
250 21183, 21659, 23822, 22080, 24243, 24719, 26882, 12379, 14542, 15018, 17181,
251 15439, 17602, 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
252 23819, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698,
253 22174, 24337, 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759, 19017,
254 21180, 21656, 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397,
255 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276, 25752,
256 27915, 26173, 28336, 28812, 30975, 12379, 14542, 15018, 17181, 15439, 17602,
257 18078, 20241, 15957, 18120, 18596, 20759, 19017, 21180, 21656, 23819, 15957,
258 18120, 18596, 20759, 19017, 21180, 21656, 23819, 19535, 21698, 22174, 24337,
259 22595, 24758, 25234, 27397, 15957, 18120, 18596, 20759, 19017, 21180, 21656,
260 23819, 19535, 21698, 22174, 24337, 22595, 24758, 25234, 27397, 19535, 21698,
261 22174, 24337, 22595, 24758, 25234, 27397, 23113, 25276, 25752, 27915, 26173,
262 28336, 28812, 30975, 16472, 18635, 19111, 21274, 19532, 21695, 22171, 24334,
263 20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 20050, 22213, 22689,
264 24852, 23110, 25273, 25749, 27912, 23628, 25791, 26267, 28430, 26688, 28851,
265 29327, 31490, 20050, 22213, 22689, 24852, 23110, 25273, 25749, 27912, 23628,
266 25791, 26267, 28430, 26688, 28851, 29327, 31490, 23628, 25791, 26267, 28430,
267 26688, 28851, 29327, 31490, 27206, 29369, 29845, 32008, 30266, 32429, 32905,
268 35068
269};
270#endif
271
Yaowu Xuf883b422016-08-30 14:01:10 -0700272#if CONFIG_AOM_HIGHBITDEPTH
273static const aom_tree_index cat1_high10[2] = { 0, 0 };
274static const aom_tree_index cat2_high10[4] = { 2, 2, 0, 0 };
275static const aom_tree_index cat3_high10[6] = { 2, 2, 4, 4, 0, 0 };
276static const aom_tree_index cat4_high10[8] = { 2, 2, 4, 4, 6, 6, 0, 0 };
277static const aom_tree_index cat5_high10[10] = { 2, 2, 4, 4, 6, 6, 8, 8, 0, 0 };
278static const aom_tree_index cat6_high10[32] = { 2, 2, 4, 4, 6, 6, 8, 8,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279 10, 10, 12, 12, 14, 14, 16, 16,
280 18, 18, 20, 20, 22, 22, 24, 24,
281 26, 26, 28, 28, 30, 30, 0, 0 };
Yaowu Xuf883b422016-08-30 14:01:10 -0700282static const aom_tree_index cat1_high12[2] = { 0, 0 };
283static const aom_tree_index cat2_high12[4] = { 2, 2, 0, 0 };
284static const aom_tree_index cat3_high12[6] = { 2, 2, 4, 4, 0, 0 };
285static const aom_tree_index cat4_high12[8] = { 2, 2, 4, 4, 6, 6, 0, 0 };
286static const aom_tree_index cat5_high12[10] = { 2, 2, 4, 4, 6, 6, 8, 8, 0, 0 };
287static const aom_tree_index cat6_high12[36] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700288 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14, 16, 16, 18, 18,
289 20, 20, 22, 22, 24, 24, 26, 26, 28, 28, 30, 30, 32, 32, 34, 34, 0, 0
290};
291#endif
292
Yaowu Xuf883b422016-08-30 14:01:10 -0700293const av1_extra_bit av1_extra_bits[ENTROPY_TOKENS] = {
294 { 0, 0, 0, 0, zero_cost }, // ZERO_TOKEN
295 { 0, 0, 0, 1, sign_cost }, // ONE_TOKEN
296 { 0, 0, 0, 2, sign_cost }, // TWO_TOKEN
297 { 0, 0, 0, 3, sign_cost }, // THREE_TOKEN
298 { 0, 0, 0, 4, sign_cost }, // FOUR_TOKEN
299 { cat1, av1_cat1_prob, 1, CAT1_MIN_VAL, cat1_cost }, // CATEGORY1_TOKEN
300 { cat2, av1_cat2_prob, 2, CAT2_MIN_VAL, cat2_cost }, // CATEGORY2_TOKEN
301 { cat3, av1_cat3_prob, 3, CAT3_MIN_VAL, cat3_cost }, // CATEGORY3_TOKEN
302 { cat4, av1_cat4_prob, 4, CAT4_MIN_VAL, cat4_cost }, // CATEGORY4_TOKEN
303 { cat5, av1_cat5_prob, 5, CAT5_MIN_VAL, cat5_cost }, // CATEGORY5_TOKEN
304 { cat6, av1_cat6_prob, 14, CAT6_MIN_VAL, 0 }, // CATEGORY6_TOKEN
305 { 0, 0, 0, 0, zero_cost } // EOB_TOKEN
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306};
307
Yaowu Xuf883b422016-08-30 14:01:10 -0700308#if CONFIG_AOM_HIGHBITDEPTH
309const av1_extra_bit av1_extra_bits_high10[ENTROPY_TOKENS] = {
310 { 0, 0, 0, 0, zero_cost }, // ZERO
311 { 0, 0, 0, 1, sign_cost }, // ONE
312 { 0, 0, 0, 2, sign_cost }, // TWO
313 { 0, 0, 0, 3, sign_cost }, // THREE
314 { 0, 0, 0, 4, sign_cost }, // FOUR
315 { cat1_high10, av1_cat1_prob_high10, 1, CAT1_MIN_VAL, cat1_cost }, // CAT1
316 { cat2_high10, av1_cat2_prob_high10, 2, CAT2_MIN_VAL, cat2_cost }, // CAT2
317 { cat3_high10, av1_cat3_prob_high10, 3, CAT3_MIN_VAL, cat3_cost }, // CAT3
318 { cat4_high10, av1_cat4_prob_high10, 4, CAT4_MIN_VAL, cat4_cost }, // CAT4
319 { cat5_high10, av1_cat5_prob_high10, 5, CAT5_MIN_VAL, cat5_cost }, // CAT5
320 { cat6_high10, av1_cat6_prob_high10, 16, CAT6_MIN_VAL, 0 }, // CAT6
321 { 0, 0, 0, 0, zero_cost } // EOB
Yaowu Xuc27fc142016-08-22 16:08:15 -0700322};
Yaowu Xuf883b422016-08-30 14:01:10 -0700323const av1_extra_bit av1_extra_bits_high12[ENTROPY_TOKENS] = {
324 { 0, 0, 0, 0, zero_cost }, // ZERO
325 { 0, 0, 0, 1, sign_cost }, // ONE
326 { 0, 0, 0, 2, sign_cost }, // TWO
327 { 0, 0, 0, 3, sign_cost }, // THREE
328 { 0, 0, 0, 4, sign_cost }, // FOUR
329 { cat1_high12, av1_cat1_prob_high12, 1, CAT1_MIN_VAL, cat1_cost }, // CAT1
330 { cat2_high12, av1_cat2_prob_high12, 2, CAT2_MIN_VAL, cat2_cost }, // CAT2
331 { cat3_high12, av1_cat3_prob_high12, 3, CAT3_MIN_VAL, cat3_cost }, // CAT3
332 { cat4_high12, av1_cat4_prob_high12, 4, CAT4_MIN_VAL, cat4_cost }, // CAT4
333 { cat5_high12, av1_cat5_prob_high12, 5, CAT5_MIN_VAL, cat5_cost }, // CAT5
334 { cat6_high12, av1_cat6_prob_high12, 18, CAT6_MIN_VAL, 0 }, // CAT6
335 { 0, 0, 0, 0, zero_cost } // EOB
Yaowu Xuc27fc142016-08-22 16:08:15 -0700336};
337#endif
338
Alex Converseec6fb642016-10-19 11:31:48 -0700339#if !CONFIG_RANS
Yaowu Xuf883b422016-08-30 14:01:10 -0700340const struct av1_token av1_coef_encodings[ENTROPY_TOKENS] = {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341 { 2, 2 }, { 6, 3 }, { 28, 5 }, { 58, 6 }, { 59, 6 }, { 60, 6 },
342 { 61, 6 }, { 124, 7 }, { 125, 7 }, { 126, 7 }, { 127, 7 }, { 0, 1 }
343};
Alex Converseec6fb642016-10-19 11:31:48 -0700344#endif // !CONFIG_RANS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345
346struct tokenize_b_args {
Urvang Joshi52648442016-10-13 17:27:51 -0700347 const AV1_COMP *cpi;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700348 ThreadData *td;
349 TOKENEXTRA **tp;
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700350 int this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700351};
352
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700353static void cost_coeffs_b(int plane, int block, int blk_row, int blk_col,
354 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
355 struct tokenize_b_args *const args = arg;
Angie Chiang22ba7512016-10-20 17:10:33 -0700356 const AV1_COMMON *cm = &args->cpi->common;
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700357 ThreadData *const td = args->td;
358 MACROBLOCK *const x = &td->mb;
359 MACROBLOCKD *const xd = &x->e_mbd;
360 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
361 struct macroblock_plane *p = &x->plane[plane];
362 struct macroblockd_plane *pd = &xd->plane[plane];
363 const PLANE_TYPE type = pd->plane_type;
364 const int ref = is_inter_block(mbmi);
365 const TX_TYPE tx_type = get_tx_type(type, xd, block, tx_size);
Angie Chiangff6d8902016-10-21 11:02:09 -0700366 const SCAN_ORDER *const scan_order = get_scan(cm, tx_size, tx_type, ref);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700367 int pt = get_entropy_context(tx_size, pd->above_context + blk_col,
368 pd->left_context + blk_row);
Angie Chiang22ba7512016-10-20 17:10:33 -0700369 int rate = av1_cost_coeffs(cm, x, plane, block, pt, tx_size, scan_order->scan,
Urvang Joshi03f6fdc2016-10-14 15:53:39 -0700370 scan_order->neighbors, 0);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700371 args->this_rate += rate;
Jingning Hana6923f72016-07-15 08:50:14 -0700372 (void)plane_bsize;
373 av1_set_contexts(xd, pd, tx_size, p->eobs[block] > 0, blk_col, blk_row);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700374}
375
Yaowu Xuc27fc142016-08-22 16:08:15 -0700376static void set_entropy_context_b(int plane, int block, int blk_row,
377 int blk_col, BLOCK_SIZE plane_bsize,
378 TX_SIZE tx_size, void *arg) {
379 struct tokenize_b_args *const args = arg;
380 ThreadData *const td = args->td;
381 MACROBLOCK *const x = &td->mb;
382 MACROBLOCKD *const xd = &x->e_mbd;
383 struct macroblock_plane *p = &x->plane[plane];
384 struct macroblockd_plane *pd = &xd->plane[plane];
Jingning Hana6923f72016-07-15 08:50:14 -0700385 (void)plane_bsize;
386 av1_set_contexts(xd, pd, tx_size, p->eobs[block] > 0, blk_col, blk_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700387}
388
Yaowu Xuf883b422016-08-30 14:01:10 -0700389static INLINE void add_token(TOKENEXTRA **t, const aom_prob *context_tree,
Alex Converseec6fb642016-10-19 11:31:48 -0700390#if CONFIG_RANS || CONFIG_DAALA_EC
Alex Converse9ed1a2f2016-09-04 13:30:43 +0200391 const aom_cdf_prob (*token_cdf)[ENTROPY_TOKENS],
Alex Converseec6fb642016-10-19 11:31:48 -0700392#endif // CONFIG_RANS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700393 int32_t extra, uint8_t token,
394 uint8_t skip_eob_node, unsigned int *counts) {
395 (*t)->token = token;
396 (*t)->extra = extra;
397 (*t)->context_tree = context_tree;
Alex Converseec6fb642016-10-19 11:31:48 -0700398#if CONFIG_RANS || CONFIG_DAALA_EC
Yaowu Xuc27fc142016-08-22 16:08:15 -0700399 (*t)->token_cdf = token_cdf;
Alex Converseec6fb642016-10-19 11:31:48 -0700400#endif // CONFIG_RANS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700401 (*t)->skip_eob_node = skip_eob_node;
402 (*t)++;
403 ++counts[token];
404}
405
Yaowu Xuc27fc142016-08-22 16:08:15 -0700406static INLINE int get_tx_eob(const struct segmentation *seg, int segment_id,
407 TX_SIZE tx_size) {
408 const int eob_max = num_4x4_blocks_txsize_lookup[tx_size] << 4;
409 return segfeature_active(seg, segment_id, SEG_LVL_SKIP) ? 0 : eob_max;
410}
411
Urvang Joshib100db72016-10-12 16:28:56 -0700412#if CONFIG_PALETTE
Urvang Joshi967ff392016-09-07 14:57:49 -0700413void av1_tokenize_palette_sb(const AV1_COMP *cpi,
414 const struct ThreadData *const td, int plane,
415 TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
416 int *rate) {
417 const MACROBLOCK *const x = &td->mb;
418 const MACROBLOCKD *const xd = &x->e_mbd;
419 const MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
420 const uint8_t *const color_map = xd->plane[plane != 0].color_index_map;
421 const PALETTE_MODE_INFO *const pmi = &mbmi->palette_mode_info;
422 const int n = pmi->palette_size[plane != 0];
423 int i, j;
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700424 int this_rate = 0;
Urvang Joshi967ff392016-09-07 14:57:49 -0700425 uint8_t color_order[PALETTE_MAX_SIZE];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700426 const int rows = (4 * num_4x4_blocks_high_lookup[bsize]) >>
427 (xd->plane[plane != 0].subsampling_y);
428 const int cols = (4 * num_4x4_blocks_wide_lookup[bsize]) >>
429 (xd->plane[plane != 0].subsampling_x);
clang-format67948d32016-09-07 22:40:40 -0700430 const aom_prob(*const probs)[PALETTE_COLOR_CONTEXTS][PALETTE_COLORS - 1] =
Yaowu Xuf883b422016-08-30 14:01:10 -0700431 plane == 0 ? av1_default_palette_y_color_prob
432 : av1_default_palette_uv_color_prob;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700433
434 for (i = 0; i < rows; ++i) {
435 for (j = (i == 0 ? 1 : 0); j < cols; ++j) {
Urvang Joshi967ff392016-09-07 14:57:49 -0700436 int color_new_idx;
437 const int color_ctx = av1_get_palette_color_context(
438 color_map, cols, i, j, n, color_order, &color_new_idx);
439 assert(color_new_idx >= 0 && color_new_idx < n);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700440 if (dry_run == DRY_RUN_COSTCOEFFS)
Urvang Joshi967ff392016-09-07 14:57:49 -0700441 this_rate += cpi->palette_y_color_cost[n - 2][color_ctx][color_new_idx];
442 (*t)->token = color_new_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700443 (*t)->context_tree = probs[n - 2][color_ctx];
444 (*t)->skip_eob_node = 0;
445 ++(*t);
446 }
447 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700448 if (rate) *rate += this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700449}
Urvang Joshib100db72016-10-12 16:28:56 -0700450#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700451
452static void tokenize_b(int plane, int block, int blk_row, int blk_col,
453 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *arg) {
454 struct tokenize_b_args *const args = arg;
Urvang Joshi52648442016-10-13 17:27:51 -0700455 const AV1_COMP *cpi = args->cpi;
Angie Chiangff6d8902016-10-21 11:02:09 -0700456 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700457 ThreadData *const td = args->td;
458 MACROBLOCK *const x = &td->mb;
459 MACROBLOCKD *const xd = &x->e_mbd;
460 TOKENEXTRA **tp = args->tp;
461 uint8_t token_cache[MAX_TX_SQUARE];
462 struct macroblock_plane *p = &x->plane[plane];
463 struct macroblockd_plane *pd = &xd->plane[plane];
464 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi;
465 int pt; /* near block/prev token context index */
466 int c;
467 TOKENEXTRA *t = *tp; /* store tokens starting here */
468 int eob = p->eobs[block];
469 const PLANE_TYPE type = pd->plane_type;
470 const tran_low_t *qcoeff = BLOCK_OFFSET(p->qcoeff, block);
471#if CONFIG_SUPERTX
Yaowu Xuf883b422016-08-30 14:01:10 -0700472 const int segment_id = AOMMIN(mbmi->segment_id, mbmi->segment_id_supertx);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700473#else
474 const int segment_id = mbmi->segment_id;
475#endif // CONFIG_SUEPRTX
476 const int16_t *scan, *nb;
477 const TX_TYPE tx_type = get_tx_type(type, xd, block, tx_size);
Urvang Joshi03f6fdc2016-10-14 15:53:39 -0700478 const SCAN_ORDER *const scan_order =
Angie Chiangff6d8902016-10-21 11:02:09 -0700479 get_scan(cm, tx_size, tx_type, is_inter_block(mbmi));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700480 const int ref = is_inter_block(mbmi);
clang-format67948d32016-09-07 22:40:40 -0700481 unsigned int(*const counts)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700482 td->rd_counts.coef_counts[txsize_sqr_map[tx_size]][type][ref];
483#if CONFIG_ENTROPY
Urvang Joshi52648442016-10-13 17:27:51 -0700484 const aom_prob(*coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700485 cpi->subframe_stats.coef_probs_buf[cpi->common.coef_probs_update_idx]
486 [txsize_sqr_map[tx_size]][type][ref];
487#else
clang-format67948d32016-09-07 22:40:40 -0700488 aom_prob(*const coef_probs)[COEFF_CONTEXTS][UNCONSTRAINED_NODES] =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700489 cpi->common.fc->coef_probs[txsize_sqr_map[tx_size]][type][ref];
490#endif // CONFIG_ENTROPY
Alex Converseec6fb642016-10-19 11:31:48 -0700491#if CONFIG_RANS || CONFIG_DAALA_EC
Alex Converse9ed1a2f2016-09-04 13:30:43 +0200492 aom_cdf_prob(*const coef_cdfs)[COEFF_CONTEXTS][ENTROPY_TOKENS] =
493 cpi->common.fc->coef_cdfs[tx_size][type][ref];
Alex Converseec6fb642016-10-19 11:31:48 -0700494#endif
clang-format67948d32016-09-07 22:40:40 -0700495 unsigned int(*const eob_branch)[COEFF_CONTEXTS] =
Yaowu Xuc27fc142016-08-22 16:08:15 -0700496 td->counts->eob_branch[txsize_sqr_map[tx_size]][type][ref];
497 const uint8_t *const band = get_band_translate(tx_size);
498 const int seg_eob = get_tx_eob(&cpi->common.seg, segment_id, tx_size);
499 int skip_eob = 0;
500 int16_t token;
501 EXTRABIT extra;
Jingning Hana6923f72016-07-15 08:50:14 -0700502 (void)plane_bsize;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700503 pt = get_entropy_context(tx_size, pd->above_context + blk_col,
504 pd->left_context + blk_row);
Urvang Joshi03f6fdc2016-10-14 15:53:39 -0700505 scan = scan_order->scan;
506 nb = scan_order->neighbors;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700507 c = 0;
508
509 while (c < eob) {
510 const int v = qcoeff[scan[c]];
511 eob_branch[band[c]][pt] += !skip_eob;
512
Yaowu Xuf883b422016-08-30 14:01:10 -0700513 av1_get_token_extra(v, &token, &extra);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700514
515 add_token(&t, coef_probs[band[c]][pt],
Alex Converseec6fb642016-10-19 11:31:48 -0700516#if CONFIG_RANS || CONFIG_DAALA_EC
Alex Converse9ed1a2f2016-09-04 13:30:43 +0200517 (const aom_cdf_prob(*)[ENTROPY_TOKENS]) & coef_cdfs[band[c]][pt],
Alex Converseec6fb642016-10-19 11:31:48 -0700518#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700519 extra, (uint8_t)token, (uint8_t)skip_eob, counts[band[c]][pt]);
520
Yaowu Xuf883b422016-08-30 14:01:10 -0700521 token_cache[scan[c]] = av1_pt_energy_class[token];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522 ++c;
523 pt = get_coef_context(nb, token_cache, c);
524 skip_eob = (token == ZERO_TOKEN);
525 }
526 if (c < seg_eob) {
Alex Converse4ce69de2016-09-04 11:16:34 +0200527 add_token(&t, coef_probs[band[c]][pt],
Alex Converseec6fb642016-10-19 11:31:48 -0700528#if CONFIG_RANS || CONFIG_DAALA_EC
Alex Converse4ce69de2016-09-04 11:16:34 +0200529 NULL,
530#endif
531 0, EOB_TOKEN, 0, counts[band[c]][pt]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700532 ++eob_branch[band[c]][pt];
533 }
534
535 *tp = t;
536
Jingning Hana6923f72016-07-15 08:50:14 -0700537 av1_set_contexts(xd, pd, tx_size, c > 0, blk_col, blk_row);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538}
539
540struct is_skippable_args {
541 uint16_t *eobs;
542 int *skippable;
543};
544static void is_skippable(int plane, int block, int blk_row, int blk_col,
545 BLOCK_SIZE plane_bsize, TX_SIZE tx_size, void *argv) {
546 struct is_skippable_args *args = argv;
547 (void)plane;
548 (void)plane_bsize;
549 (void)tx_size;
550 (void)blk_row;
551 (void)blk_col;
552 args->skippable[0] &= (!args->eobs[block]);
553}
554
555// TODO(yaowu): rewrite and optimize this function to remove the usage of
Yaowu Xuf883b422016-08-30 14:01:10 -0700556// av1_foreach_transform_block() and simplify is_skippable().
557int av1_is_skippable_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700558 int result = 1;
559 struct is_skippable_args args = { x->plane[plane].eobs, &result };
Yaowu Xuf883b422016-08-30 14:01:10 -0700560 av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane, is_skippable,
561 &args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 return result;
563}
564
565static void has_high_freq_coeff(int plane, int block, int blk_row, int blk_col,
566 BLOCK_SIZE plane_bsize, TX_SIZE tx_size,
567 void *argv) {
568 struct is_skippable_args *args = argv;
569 int eobs = (tx_size == TX_4X4) ? 3 : 10;
570 (void)plane;
571 (void)plane_bsize;
572 (void)blk_row;
573 (void)blk_col;
574
575 *(args->skippable) |= (args->eobs[block] > eobs);
576}
577
Yaowu Xuf883b422016-08-30 14:01:10 -0700578int av1_has_high_freq_in_plane(MACROBLOCK *x, BLOCK_SIZE bsize, int plane) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579 int result = 0;
580 struct is_skippable_args args = { x->plane[plane].eobs, &result };
Yaowu Xuf883b422016-08-30 14:01:10 -0700581 av1_foreach_transformed_block_in_plane(&x->e_mbd, bsize, plane,
582 has_high_freq_coeff, &args);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700583 return result;
584}
585
586#if CONFIG_VAR_TX
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700587void tokenize_vartx(ThreadData *td, TOKENEXTRA **t, RUN_TYPE dry_run,
Debargha Mukherjee3c42c092016-09-29 09:17:36 -0700588 TX_SIZE tx_size, BLOCK_SIZE plane_bsize, int blk_row,
589 int blk_col, int block, int plane, void *arg) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590 MACROBLOCK *const x = &td->mb;
591 MACROBLOCKD *const xd = &x->e_mbd;
592 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
593 const struct macroblockd_plane *const pd = &xd->plane[plane];
594 const BLOCK_SIZE bsize = txsize_to_bsize[tx_size];
595 const int tx_row = blk_row >> (1 - pd->subsampling_y);
596 const int tx_col = blk_col >> (1 - pd->subsampling_x);
597 TX_SIZE plane_tx_size;
598
599 int max_blocks_high = num_4x4_blocks_high_lookup[plane_bsize];
600 int max_blocks_wide = num_4x4_blocks_wide_lookup[plane_bsize];
601
602 assert(tx_size < TX_SIZES);
603
604 if (xd->mb_to_bottom_edge < 0)
605 max_blocks_high += xd->mb_to_bottom_edge >> (5 + pd->subsampling_y);
606 if (xd->mb_to_right_edge < 0)
607 max_blocks_wide += xd->mb_to_right_edge >> (5 + pd->subsampling_x);
608
609 if (blk_row >= max_blocks_high || blk_col >= max_blocks_wide) return;
610
Debargha Mukherjee2f123402016-08-30 17:43:38 -0700611 plane_tx_size =
612 plane ? uv_txsize_lookup[bsize][mbmi->inter_tx_size[tx_row][tx_col]][0][0]
613 : mbmi->inter_tx_size[tx_row][tx_col];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700614
615 if (tx_size == plane_tx_size) {
Urvang Joshi368fbc92016-10-17 16:31:34 -0700616 plane_bsize = get_plane_block_size(mbmi->sb_type, pd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700617 if (!dry_run)
618 tokenize_b(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700619 else if (dry_run == DRY_RUN_NORMAL)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700620 set_entropy_context_b(plane, block, blk_row, blk_col, plane_bsize,
621 tx_size, arg);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700622 else if (dry_run == DRY_RUN_COSTCOEFFS)
623 cost_coeffs_b(plane, block, blk_row, blk_col, plane_bsize, tx_size, arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700624 } else {
625 int bsl = b_width_log2_lookup[bsize];
626 int i;
627
628 assert(bsl > 0);
629 --bsl;
630
631 for (i = 0; i < 4; ++i) {
632 const int offsetr = blk_row + ((i >> 1) << bsl);
633 const int offsetc = blk_col + ((i & 0x01) << bsl);
634 int step = num_4x4_blocks_txsize_lookup[tx_size - 1];
635
636 if (offsetr >= max_blocks_high || offsetc >= max_blocks_wide) continue;
637
Debargha Mukherjee3c42c092016-09-29 09:17:36 -0700638 tokenize_vartx(td, t, dry_run, tx_size - 1, plane_bsize, offsetr, offsetc,
639 block + i * step, plane, arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700640 }
641 }
642}
643
Urvang Joshi52648442016-10-13 17:27:51 -0700644void av1_tokenize_sb_vartx(const AV1_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700645 RUN_TYPE dry_run, int mi_row, int mi_col,
646 BLOCK_SIZE bsize, int *rate) {
Urvang Joshi52648442016-10-13 17:27:51 -0700647 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700648 MACROBLOCK *const x = &td->mb;
649 MACROBLOCKD *const xd = &x->e_mbd;
650 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
651 TOKENEXTRA *t_backup = *t;
Yaowu Xuf883b422016-08-30 14:01:10 -0700652 const int ctx = av1_get_skip_context(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700653 const int skip_inc =
654 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700655 struct tokenize_b_args arg = { cpi, td, t, 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700656 int plane;
657 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
658
659 if (mbmi->skip) {
660 if (!dry_run) td->counts->skip[ctx][1] += skip_inc;
661 reset_skip_context(xd, bsize);
662 if (dry_run) *t = t_backup;
663 return;
664 }
665
666 if (!dry_run)
667 td->counts->skip[ctx][0] += skip_inc;
668 else
669 *t = t_backup;
670
671 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
672 const struct macroblockd_plane *const pd = &xd->plane[plane];
673 const BLOCK_SIZE plane_bsize = get_plane_block_size(bsize, pd);
674 const int mi_width = num_4x4_blocks_wide_lookup[plane_bsize];
675 const int mi_height = num_4x4_blocks_high_lookup[plane_bsize];
676 const TX_SIZE max_tx_size = max_txsize_lookup[plane_bsize];
677 const BLOCK_SIZE txb_size = txsize_to_bsize[max_tx_size];
678 int bh = num_4x4_blocks_wide_lookup[txb_size];
679 int idx, idy;
680 int block = 0;
681 int step = num_4x4_blocks_txsize_lookup[max_tx_size];
682 for (idy = 0; idy < mi_height; idy += bh) {
683 for (idx = 0; idx < mi_width; idx += bh) {
Debargha Mukherjee3c42c092016-09-29 09:17:36 -0700684 tokenize_vartx(td, t, dry_run, max_tx_size, plane_bsize, idy, idx,
685 block, plane, &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686 block += step;
687 }
688 }
689
690 if (!dry_run) {
691 (*t)->token = EOSB_TOKEN;
692 (*t)++;
693 }
694 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700695 if (rate) *rate += arg.this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696}
697#endif // CONFIG_VAR_TX
698
Urvang Joshi52648442016-10-13 17:27:51 -0700699void av1_tokenize_sb(const AV1_COMP *cpi, ThreadData *td, TOKENEXTRA **t,
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700700 RUN_TYPE dry_run, BLOCK_SIZE bsize, int *rate) {
Urvang Joshi52648442016-10-13 17:27:51 -0700701 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 MACROBLOCK *const x = &td->mb;
703 MACROBLOCKD *const xd = &x->e_mbd;
704 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
Yaowu Xuf883b422016-08-30 14:01:10 -0700705 const int ctx = av1_get_skip_context(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706 const int skip_inc =
707 !segfeature_active(&cm->seg, mbmi->segment_id, SEG_LVL_SKIP);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700708 struct tokenize_b_args arg = { cpi, td, t, 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700709 if (mbmi->skip) {
710 if (!dry_run) td->counts->skip[ctx][1] += skip_inc;
711 reset_skip_context(xd, bsize);
712 return;
713 }
714
715 if (!dry_run) {
716 int plane;
717
718 td->counts->skip[ctx][0] += skip_inc;
719
720 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700721 av1_foreach_transformed_block_in_plane(xd, bsize, plane, tokenize_b,
722 &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723 (*t)->token = EOSB_TOKEN;
724 (*t)++;
725 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700726 } else if (dry_run == DRY_RUN_NORMAL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700727 av1_foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700728 } else if (dry_run == DRY_RUN_COSTCOEFFS) {
729 av1_foreach_transformed_block(xd, bsize, cost_coeffs_b, &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700731 if (rate) *rate += arg.this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732}
733
734#if CONFIG_SUPERTX
Urvang Joshi52648442016-10-13 17:27:51 -0700735void av1_tokenize_sb_supertx(const AV1_COMP *cpi, ThreadData *td,
736 TOKENEXTRA **t, RUN_TYPE dry_run, BLOCK_SIZE bsize,
737 int *rate) {
738 const AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739 MACROBLOCKD *const xd = &td->mb.e_mbd;
740 MB_MODE_INFO *const mbmi = &xd->mi[0]->mbmi;
741 TOKENEXTRA *t_backup = *t;
Yaowu Xuf883b422016-08-30 14:01:10 -0700742 const int ctx = av1_get_skip_context(xd);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 const int skip_inc =
744 !segfeature_active(&cm->seg, mbmi->segment_id_supertx, SEG_LVL_SKIP);
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700745 struct tokenize_b_args arg = { cpi, td, t, 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -0700746 if (mbmi->skip) {
747 if (!dry_run) td->counts->skip[ctx][1] += skip_inc;
748 reset_skip_context(xd, bsize);
749 if (dry_run) *t = t_backup;
750 return;
751 }
752
753 if (!dry_run) {
754 int plane;
755 td->counts->skip[ctx][0] += skip_inc;
756
757 for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700758 av1_foreach_transformed_block_in_plane(xd, bsize, plane, tokenize_b,
759 &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760 (*t)->token = EOSB_TOKEN;
761 (*t)++;
762 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700763 } else if (dry_run == DRY_RUN_NORMAL) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700764 av1_foreach_transformed_block(xd, bsize, set_entropy_context_b, &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 *t = t_backup;
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700766 } else if (dry_run == DRY_RUN_COSTCOEFFS) {
767 av1_foreach_transformed_block(xd, bsize, cost_coeffs_b, &arg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768 }
Debargha Mukherjeeceebb702016-10-11 05:26:50 -0700769 if (rate) *rate += arg.this_rate;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770}
771#endif // CONFIG_SUPERTX