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