xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/amd/display/dc/dsc/amdgpu_rc_calc_dpi.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: amdgpu_rc_calc_dpi.c,v 1.2 2021/12/18 23:45:04 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2012-17 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 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: amdgpu_rc_calc_dpi.c,v 1.2 2021/12/18 23:45:04 riastradh Exp $");
29 
30 #include "os_types.h"
31 #include <drm/drm_dsc.h>
32 #include "dscc_types.h"
33 #include "rc_calc.h"
34 
35 double dsc_ceil(double num);
36 
copy_pps_fields(struct drm_dsc_config * to,const struct drm_dsc_config * from)37 static void copy_pps_fields(struct drm_dsc_config *to, const struct drm_dsc_config *from)
38 {
39 	to->line_buf_depth           = from->line_buf_depth;
40 	to->bits_per_component       = from->bits_per_component;
41 	to->convert_rgb              = from->convert_rgb;
42 	to->slice_width              = from->slice_width;
43 	to->slice_height             = from->slice_height;
44 	to->simple_422               = from->simple_422;
45 	to->native_422               = from->native_422;
46 	to->native_420               = from->native_420;
47 	to->pic_width                = from->pic_width;
48 	to->pic_height               = from->pic_height;
49 	to->rc_tgt_offset_high       = from->rc_tgt_offset_high;
50 	to->rc_tgt_offset_low        = from->rc_tgt_offset_low;
51 	to->bits_per_pixel           = from->bits_per_pixel;
52 	to->rc_edge_factor           = from->rc_edge_factor;
53 	to->rc_quant_incr_limit1     = from->rc_quant_incr_limit1;
54 	to->rc_quant_incr_limit0     = from->rc_quant_incr_limit0;
55 	to->initial_xmit_delay       = from->initial_xmit_delay;
56 	to->initial_dec_delay        = from->initial_dec_delay;
57 	to->block_pred_enable        = from->block_pred_enable;
58 	to->first_line_bpg_offset    = from->first_line_bpg_offset;
59 	to->second_line_bpg_offset   = from->second_line_bpg_offset;
60 	to->initial_offset           = from->initial_offset;
61 	memcpy(&to->rc_buf_thresh, &from->rc_buf_thresh, sizeof(from->rc_buf_thresh));
62 	memcpy(&to->rc_range_params, &from->rc_range_params, sizeof(from->rc_range_params));
63 	to->rc_model_size            = from->rc_model_size;
64 	to->flatness_min_qp          = from->flatness_min_qp;
65 	to->flatness_max_qp          = from->flatness_max_qp;
66 	to->initial_scale_value      = from->initial_scale_value;
67 	to->scale_decrement_interval = from->scale_decrement_interval;
68 	to->scale_increment_interval = from->scale_increment_interval;
69 	to->nfl_bpg_offset           = from->nfl_bpg_offset;
70 	to->nsl_bpg_offset           = from->nsl_bpg_offset;
71 	to->slice_bpg_offset         = from->slice_bpg_offset;
72 	to->final_offset             = from->final_offset;
73 	to->vbr_enable               = from->vbr_enable;
74 	to->slice_chunk_size         = from->slice_chunk_size;
75 	to->second_line_offset_adj   = from->second_line_offset_adj;
76 	to->dsc_version_minor        = from->dsc_version_minor;
77 }
78 
copy_rc_to_cfg(struct drm_dsc_config * dsc_cfg,const struct rc_params * rc)79 static void copy_rc_to_cfg(struct drm_dsc_config *dsc_cfg, const struct rc_params *rc)
80 {
81 	int i;
82 
83 	dsc_cfg->rc_quant_incr_limit0   = rc->rc_quant_incr_limit0;
84 	dsc_cfg->rc_quant_incr_limit1   = rc->rc_quant_incr_limit1;
85 	dsc_cfg->initial_offset         = rc->initial_fullness_offset;
86 	dsc_cfg->initial_xmit_delay     = rc->initial_xmit_delay;
87 	dsc_cfg->first_line_bpg_offset  = rc->first_line_bpg_offset;
88 	dsc_cfg->second_line_bpg_offset = rc->second_line_bpg_offset;
89 	dsc_cfg->flatness_min_qp        = rc->flatness_min_qp;
90 	dsc_cfg->flatness_max_qp        = rc->flatness_max_qp;
91 	for (i = 0; i < QP_SET_SIZE; ++i) {
92 		dsc_cfg->rc_range_params[i].range_min_qp     = rc->qp_min[i];
93 		dsc_cfg->rc_range_params[i].range_max_qp     = rc->qp_max[i];
94 		/* Truncate 8-bit signed value to 6-bit signed value */
95 		dsc_cfg->rc_range_params[i].range_bpg_offset = 0x3f & rc->ofs[i];
96 	}
97 	dsc_cfg->rc_model_size    = rc->rc_model_size;
98 	dsc_cfg->rc_edge_factor   = rc->rc_edge_factor;
99 	dsc_cfg->rc_tgt_offset_high = rc->rc_tgt_offset_hi;
100 	dsc_cfg->rc_tgt_offset_low = rc->rc_tgt_offset_lo;
101 
102 	for (i = 0; i < QP_SET_SIZE - 1; ++i)
103 		dsc_cfg->rc_buf_thresh[i] = rc->rc_buf_thresh[i];
104 }
105 
dscc_compute_dsc_parameters(const struct drm_dsc_config * pps,struct dsc_parameters * dsc_params)106 int dscc_compute_dsc_parameters(const struct drm_dsc_config *pps, struct dsc_parameters *dsc_params)
107 {
108 	enum colour_mode  mode = pps->convert_rgb ? CM_RGB :
109 							(pps->simple_422  ? CM_444 :
110 							(pps->native_422  ? CM_422 :
111 							pps->native_420  ? CM_420 : CM_444));
112 	enum bits_per_comp bpc = (pps->bits_per_component == 8) ? BPC_8 :
113 							(pps->bits_per_component == 10) ? BPC_10 : BPC_12;
114 	float            bpp = ((float) pps->bits_per_pixel / 16.0);
115 	int              slice_width  = pps->slice_width;
116 	int              slice_height = pps->slice_height;
117 	int              ret;
118 	struct rc_params rc;
119 	struct drm_dsc_config   dsc_cfg;
120 
121 	double d_bytes_per_pixel = dsc_ceil(bpp * slice_width / 8.0) / slice_width;
122 
123 	// TODO: Make sure the formula for calculating this is precise (ceiling vs. floor, and at what point they should be applied)
124 	if (pps->native_422 || pps->native_420)
125 		d_bytes_per_pixel /= 2;
126 
127 	dsc_params->bytes_per_pixel = (uint32_t)dsc_ceil(d_bytes_per_pixel * 0x10000000);
128 
129 	/* in native_422 or native_420 modes, the bits_per_pixel is double the target bpp
130 	 * (the latter is what calc_rc_params expects)
131 	 */
132 	if (pps->native_422 || pps->native_420)
133 		bpp /= 2.0;
134 
135 	calc_rc_params(&rc, mode, bpc, bpp, slice_width, slice_height, pps->dsc_version_minor);
136 	dsc_params->pps = *pps;
137 	dsc_params->pps.initial_scale_value = 8 * rc.rc_model_size / (rc.rc_model_size - rc.initial_fullness_offset);
138 
139 	copy_pps_fields(&dsc_cfg, &dsc_params->pps);
140 	copy_rc_to_cfg(&dsc_cfg, &rc);
141 
142 	dsc_cfg.mux_word_size = dsc_params->pps.bits_per_component <= 10 ? 48 : 64;
143 
144 	ret = drm_dsc_compute_rc_parameters(&dsc_cfg);
145 
146 	copy_pps_fields(&dsc_params->pps, &dsc_cfg);
147 	dsc_params->rc_buffer_model_size = dsc_cfg.rc_bits;
148 	return ret;
149 }
150 
151