treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / amd / display / dc / dsc / rc_calc.c
blob03ae15946c6d883015ddf80209fd53674efc6e86
2 /*
3 * Copyright 2017 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * Authors: AMD
27 #include "os_types.h"
28 #include "rc_calc.h"
29 #include "qp_tables.h"
31 #define table_hash(mode, bpc, max_min) ((mode << 16) | (bpc << 8) | max_min)
33 #define MODE_SELECT(val444, val422, val420) \
34 (cm == CM_444 || cm == CM_RGB) ? (val444) : (cm == CM_422 ? (val422) : (val420))
37 #define TABLE_CASE(mode, bpc, max) case (table_hash(mode, BPC_##bpc, max)): \
38 table = qp_table_##mode##_##bpc##bpc_##max; \
39 table_size = sizeof(qp_table_##mode##_##bpc##bpc_##max)/sizeof(*qp_table_##mode##_##bpc##bpc_##max); \
40 break
43 void get_qp_set(qp_set qps, enum colour_mode cm, enum bits_per_comp bpc, enum max_min max_min, float bpp)
45 int mode = MODE_SELECT(444, 422, 420);
46 int sel = table_hash(mode, bpc, max_min);
47 int table_size = 0;
48 int index;
49 const struct qp_entry *table = 0L;
51 // alias enum
52 enum { min = MM_MIN, max = MM_MAX };
53 switch (sel) {
54 TABLE_CASE(444, 8, max);
55 TABLE_CASE(444, 8, min);
56 TABLE_CASE(444, 10, max);
57 TABLE_CASE(444, 10, min);
58 TABLE_CASE(444, 12, max);
59 TABLE_CASE(444, 12, min);
60 TABLE_CASE(422, 8, max);
61 TABLE_CASE(422, 8, min);
62 TABLE_CASE(422, 10, max);
63 TABLE_CASE(422, 10, min);
64 TABLE_CASE(422, 12, max);
65 TABLE_CASE(422, 12, min);
66 TABLE_CASE(420, 8, max);
67 TABLE_CASE(420, 8, min);
68 TABLE_CASE(420, 10, max);
69 TABLE_CASE(420, 10, min);
70 TABLE_CASE(420, 12, max);
71 TABLE_CASE(420, 12, min);
74 if (table == 0)
75 return;
77 index = (bpp - table[0].bpp) * 2;
79 /* requested size is bigger than the table */
80 if (index >= table_size) {
81 dm_error("ERROR: Requested rc_calc to find a bpp entry that exceeds the table size\n");
82 return;
85 memcpy(qps, table[index].qps, sizeof(qp_set));
88 double dsc_roundf(double num)
90 if (num < 0.0)
91 num = num - 0.5;
92 else
93 num = num + 0.5;
95 return (int)(num);
98 double dsc_ceil(double num)
100 double retval = (int)num;
102 if (retval != num && num > 0)
103 retval = num + 1;
105 return (int)retval;
108 void get_ofs_set(qp_set ofs, enum colour_mode mode, float bpp)
110 int *p = ofs;
112 if (mode == CM_444 || mode == CM_RGB) {
113 *p++ = (bpp <= 6) ? (0) : ((((bpp >= 8) && (bpp <= 12))) ? (2) : ((bpp >= 15) ? (10) : ((((bpp > 6) && (bpp < 8))) ? (0 + dsc_roundf((bpp - 6) * (2 / 2.0))) : (2 + dsc_roundf((bpp - 12) * (8 / 3.0))))));
114 *p++ = (bpp <= 6) ? (-2) : ((((bpp >= 8) && (bpp <= 12))) ? (0) : ((bpp >= 15) ? (8) : ((((bpp > 6) && (bpp < 8))) ? (-2 + dsc_roundf((bpp - 6) * (2 / 2.0))) : (0 + dsc_roundf((bpp - 12) * (8 / 3.0))))));
115 *p++ = (bpp <= 6) ? (-2) : ((((bpp >= 8) && (bpp <= 12))) ? (0) : ((bpp >= 15) ? (6) : ((((bpp > 6) && (bpp < 8))) ? (-2 + dsc_roundf((bpp - 6) * (2 / 2.0))) : (0 + dsc_roundf((bpp - 12) * (6 / 3.0))))));
116 *p++ = (bpp <= 6) ? (-4) : ((((bpp >= 8) && (bpp <= 12))) ? (-2) : ((bpp >= 15) ? (4) : ((((bpp > 6) && (bpp < 8))) ? (-4 + dsc_roundf((bpp - 6) * (2 / 2.0))) : (-2 + dsc_roundf((bpp - 12) * (6 / 3.0))))));
117 *p++ = (bpp <= 6) ? (-6) : ((((bpp >= 8) && (bpp <= 12))) ? (-4) : ((bpp >= 15) ? (2) : ((((bpp > 6) && (bpp < 8))) ? (-6 + dsc_roundf((bpp - 6) * (2 / 2.0))) : (-4 + dsc_roundf((bpp - 12) * (6 / 3.0))))));
118 *p++ = (bpp <= 12) ? (-6) : ((bpp >= 15) ? (0) : (-6 + dsc_roundf((bpp - 12) * (6 / 3.0))));
119 *p++ = (bpp <= 12) ? (-8) : ((bpp >= 15) ? (-2) : (-8 + dsc_roundf((bpp - 12) * (6 / 3.0))));
120 *p++ = (bpp <= 12) ? (-8) : ((bpp >= 15) ? (-4) : (-8 + dsc_roundf((bpp - 12) * (4 / 3.0))));
121 *p++ = (bpp <= 12) ? (-8) : ((bpp >= 15) ? (-6) : (-8 + dsc_roundf((bpp - 12) * (2 / 3.0))));
122 *p++ = (bpp <= 12) ? (-10) : ((bpp >= 15) ? (-8) : (-10 + dsc_roundf((bpp - 12) * (2 / 3.0))));
123 *p++ = -10;
124 *p++ = (bpp <= 6) ? (-12) : ((bpp >= 8) ? (-10) : (-12 + dsc_roundf((bpp - 6) * (2 / 2.0))));
125 *p++ = -12;
126 *p++ = -12;
127 *p++ = -12;
128 } else if (mode == CM_422) {
129 *p++ = (bpp <= 8) ? (2) : ((bpp >= 10) ? (10) : (2 + dsc_roundf((bpp - 8) * (8 / 2.0))));
130 *p++ = (bpp <= 8) ? (0) : ((bpp >= 10) ? (8) : (0 + dsc_roundf((bpp - 8) * (8 / 2.0))));
131 *p++ = (bpp <= 8) ? (0) : ((bpp >= 10) ? (6) : (0 + dsc_roundf((bpp - 8) * (6 / 2.0))));
132 *p++ = (bpp <= 8) ? (-2) : ((bpp >= 10) ? (4) : (-2 + dsc_roundf((bpp - 8) * (6 / 2.0))));
133 *p++ = (bpp <= 8) ? (-4) : ((bpp >= 10) ? (2) : (-4 + dsc_roundf((bpp - 8) * (6 / 2.0))));
134 *p++ = (bpp <= 8) ? (-6) : ((bpp >= 10) ? (0) : (-6 + dsc_roundf((bpp - 8) * (6 / 2.0))));
135 *p++ = (bpp <= 8) ? (-8) : ((bpp >= 10) ? (-2) : (-8 + dsc_roundf((bpp - 8) * (6 / 2.0))));
136 *p++ = (bpp <= 8) ? (-8) : ((bpp >= 10) ? (-4) : (-8 + dsc_roundf((bpp - 8) * (4 / 2.0))));
137 *p++ = (bpp <= 8) ? (-8) : ((bpp >= 10) ? (-6) : (-8 + dsc_roundf((bpp - 8) * (2 / 2.0))));
138 *p++ = (bpp <= 8) ? (-10) : ((bpp >= 10) ? (-8) : (-10 + dsc_roundf((bpp - 8) * (2 / 2.0))));
139 *p++ = -10;
140 *p++ = (bpp <= 6) ? (-12) : ((bpp >= 7) ? (-10) : (-12 + dsc_roundf((bpp - 6) * (2.0 / 1))));
141 *p++ = -12;
142 *p++ = -12;
143 *p++ = -12;
144 } else {
145 *p++ = (bpp <= 6) ? (2) : ((bpp >= 8) ? (10) : (2 + dsc_roundf((bpp - 6) * (8 / 2.0))));
146 *p++ = (bpp <= 6) ? (0) : ((bpp >= 8) ? (8) : (0 + dsc_roundf((bpp - 6) * (8 / 2.0))));
147 *p++ = (bpp <= 6) ? (0) : ((bpp >= 8) ? (6) : (0 + dsc_roundf((bpp - 6) * (6 / 2.0))));
148 *p++ = (bpp <= 6) ? (-2) : ((bpp >= 8) ? (4) : (-2 + dsc_roundf((bpp - 6) * (6 / 2.0))));
149 *p++ = (bpp <= 6) ? (-4) : ((bpp >= 8) ? (2) : (-4 + dsc_roundf((bpp - 6) * (6 / 2.0))));
150 *p++ = (bpp <= 6) ? (-6) : ((bpp >= 8) ? (0) : (-6 + dsc_roundf((bpp - 6) * (6 / 2.0))));
151 *p++ = (bpp <= 6) ? (-8) : ((bpp >= 8) ? (-2) : (-8 + dsc_roundf((bpp - 6) * (6 / 2.0))));
152 *p++ = (bpp <= 6) ? (-8) : ((bpp >= 8) ? (-4) : (-8 + dsc_roundf((bpp - 6) * (4 / 2.0))));
153 *p++ = (bpp <= 6) ? (-8) : ((bpp >= 8) ? (-6) : (-8 + dsc_roundf((bpp - 6) * (2 / 2.0))));
154 *p++ = (bpp <= 6) ? (-10) : ((bpp >= 8) ? (-8) : (-10 + dsc_roundf((bpp - 6) * (2 / 2.0))));
155 *p++ = -10;
156 *p++ = (bpp <= 4) ? (-12) : ((bpp >= 5) ? (-10) : (-12 + dsc_roundf((bpp - 4) * (2 / 1.0))));
157 *p++ = -12;
158 *p++ = -12;
159 *p++ = -12;
163 int median3(int a, int b, int c)
165 if (a > b)
166 swap(a, b);
167 if (b > c)
168 swap(b, c);
169 if (a > b)
170 swap(b, c);
172 return b;
175 void calc_rc_params(struct rc_params *rc, enum colour_mode cm, enum bits_per_comp bpc, float bpp, int slice_width, int slice_height, int minor_version)
177 float bpp_group;
178 float initial_xmit_delay_factor;
179 int padding_pixels;
180 int i;
182 rc->rc_quant_incr_limit0 = ((bpc == BPC_8) ? 11 : (bpc == BPC_10 ? 15 : 19)) - ((minor_version == 1 && cm == CM_444) ? 1 : 0);
183 rc->rc_quant_incr_limit1 = ((bpc == BPC_8) ? 11 : (bpc == BPC_10 ? 15 : 19)) - ((minor_version == 1 && cm == CM_444) ? 1 : 0);
185 bpp_group = MODE_SELECT(bpp, bpp * 2.0, bpp * 2.0);
187 switch (cm) {
188 case CM_420:
189 rc->initial_fullness_offset = (bpp >= 6) ? (2048) : ((bpp <= 4) ? (6144) : ((((bpp > 4) && (bpp <= 5))) ? (6144 - dsc_roundf((bpp - 4) * (512))) : (5632 - dsc_roundf((bpp - 5) * (3584)))));
190 rc->first_line_bpg_offset = median3(0, (12 + (int) (0.09 * min(34, slice_height - 8))), (int)((3 * bpc * 3) - (3 * bpp_group)));
191 rc->second_line_bpg_offset = median3(0, 12, (int)((3 * bpc * 3) - (3 * bpp_group)));
192 break;
193 case CM_422:
194 rc->initial_fullness_offset = (bpp >= 8) ? (2048) : ((bpp <= 7) ? (5632) : (5632 - dsc_roundf((bpp - 7) * (3584))));
195 rc->first_line_bpg_offset = median3(0, (12 + (int) (0.09 * min(34, slice_height - 8))), (int)((3 * bpc * 4) - (3 * bpp_group)));
196 rc->second_line_bpg_offset = 0;
197 break;
198 case CM_444:
199 case CM_RGB:
200 rc->initial_fullness_offset = (bpp >= 12) ? (2048) : ((bpp <= 8) ? (6144) : ((((bpp > 8) && (bpp <= 10))) ? (6144 - dsc_roundf((bpp - 8) * (512 / 2))) : (5632 - dsc_roundf((bpp - 10) * (3584 / 2)))));
201 rc->first_line_bpg_offset = median3(0, (12 + (int) (0.09 * min(34, slice_height - 8))), (int)(((3 * bpc + (cm == CM_444 ? 0 : 2)) * 3) - (3 * bpp_group)));
202 rc->second_line_bpg_offset = 0;
203 break;
206 initial_xmit_delay_factor = (cm == CM_444 || cm == CM_RGB) ? 1.0 : 2.0;
207 rc->initial_xmit_delay = dsc_roundf(8192.0/2.0/bpp/initial_xmit_delay_factor);
209 if (cm == CM_422 || cm == CM_420)
210 slice_width /= 2;
212 padding_pixels = ((slice_width % 3) != 0) ? (3 - (slice_width % 3)) * (rc->initial_xmit_delay / slice_width) : 0;
213 if (3 * bpp_group >= (((rc->initial_xmit_delay + 2) / 3) * (3 + (cm == CM_422)))) {
214 if ((rc->initial_xmit_delay + padding_pixels) % 3 == 1)
215 rc->initial_xmit_delay++;
218 rc->flatness_min_qp = ((bpc == BPC_8) ? (3) : ((bpc == BPC_10) ? (7) : (11))) - ((minor_version == 1 && cm == CM_444) ? 1 : 0);
219 rc->flatness_max_qp = ((bpc == BPC_8) ? (12) : ((bpc == BPC_10) ? (16) : (20))) - ((minor_version == 1 && cm == CM_444) ? 1 : 0);
220 rc->flatness_det_thresh = 2 << (bpc - 8);
222 get_qp_set(rc->qp_min, cm, bpc, MM_MIN, bpp);
223 get_qp_set(rc->qp_max, cm, bpc, MM_MAX, bpp);
224 if (cm == CM_444 && minor_version == 1) {
225 for (i = 0; i < QP_SET_SIZE; ++i) {
226 rc->qp_min[i] = rc->qp_min[i] > 0 ? rc->qp_min[i] - 1 : 0;
227 rc->qp_max[i] = rc->qp_max[i] > 0 ? rc->qp_max[i] - 1 : 0;
230 get_ofs_set(rc->ofs, cm, bpp);
232 /* fixed parameters */
233 rc->rc_model_size = 8192;
234 rc->rc_edge_factor = 6;
235 rc->rc_tgt_offset_hi = 3;
236 rc->rc_tgt_offset_lo = 3;
238 rc->rc_buf_thresh[0] = 896;
239 rc->rc_buf_thresh[1] = 1792;
240 rc->rc_buf_thresh[2] = 2688;
241 rc->rc_buf_thresh[3] = 3584;
242 rc->rc_buf_thresh[4] = 4480;
243 rc->rc_buf_thresh[5] = 5376;
244 rc->rc_buf_thresh[6] = 6272;
245 rc->rc_buf_thresh[7] = 6720;
246 rc->rc_buf_thresh[8] = 7168;
247 rc->rc_buf_thresh[9] = 7616;
248 rc->rc_buf_thresh[10] = 7744;
249 rc->rc_buf_thresh[11] = 7872;
250 rc->rc_buf_thresh[12] = 8000;
251 rc->rc_buf_thresh[13] = 8064;