xref: /linux/drivers/staging/media/rkvdec/rkvdec-h264.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Rockchip Video Decoder H264 backend
4  *
5  * Copyright (C) 2019 Collabora, Ltd.
6  *	Boris Brezillon <boris.brezillon@collabora.com>
7  *
8  * Copyright (C) 2016 Rockchip Electronics Co., Ltd.
9  *	Jeffy Chen <jeffy.chen@rock-chips.com>
10  */
11 
12 #include <media/v4l2-h264.h>
13 #include <media/v4l2-mem2mem.h>
14 
15 #include "rkvdec.h"
16 #include "rkvdec-regs.h"
17 
18 /* Size with u32 units. */
19 #define RKV_CABAC_INIT_BUFFER_SIZE	(3680 + 128)
20 #define RKV_RPS_SIZE			((128 + 128) / 4)
21 #define RKV_ERROR_INFO_SIZE		(256 * 144 * 4)
22 
23 #define RKVDEC_NUM_REFLIST		3
24 
25 struct rkvdec_h264_scaling_list {
26 	u8 scaling_list_4x4[6][16];
27 	u8 scaling_list_8x8[6][64];
28 	u8 padding[128];
29 };
30 
31 struct rkvdec_sps_pps_packet {
32 	u32 info[8];
33 };
34 
35 struct rkvdec_ps_field {
36 	u16 offset;
37 	u8 len;
38 };
39 
40 #define PS_FIELD(_offset, _len) \
41 	((struct rkvdec_ps_field){ _offset, _len })
42 
43 #define SEQ_PARAMETER_SET_ID				PS_FIELD(0, 4)
44 #define PROFILE_IDC					PS_FIELD(4, 8)
45 #define CONSTRAINT_SET3_FLAG				PS_FIELD(12, 1)
46 #define CHROMA_FORMAT_IDC				PS_FIELD(13, 2)
47 #define BIT_DEPTH_LUMA					PS_FIELD(15, 3)
48 #define BIT_DEPTH_CHROMA				PS_FIELD(18, 3)
49 #define QPPRIME_Y_ZERO_TRANSFORM_BYPASS_FLAG		PS_FIELD(21, 1)
50 #define LOG2_MAX_FRAME_NUM_MINUS4			PS_FIELD(22, 4)
51 #define MAX_NUM_REF_FRAMES				PS_FIELD(26, 5)
52 #define PIC_ORDER_CNT_TYPE				PS_FIELD(31, 2)
53 #define LOG2_MAX_PIC_ORDER_CNT_LSB_MINUS4		PS_FIELD(33, 4)
54 #define DELTA_PIC_ORDER_ALWAYS_ZERO_FLAG		PS_FIELD(37, 1)
55 #define PIC_WIDTH_IN_MBS				PS_FIELD(38, 9)
56 #define PIC_HEIGHT_IN_MBS				PS_FIELD(47, 9)
57 #define FRAME_MBS_ONLY_FLAG				PS_FIELD(56, 1)
58 #define MB_ADAPTIVE_FRAME_FIELD_FLAG			PS_FIELD(57, 1)
59 #define DIRECT_8X8_INFERENCE_FLAG			PS_FIELD(58, 1)
60 #define MVC_EXTENSION_ENABLE				PS_FIELD(59, 1)
61 #define NUM_VIEWS					PS_FIELD(60, 2)
62 #define VIEW_ID(i)					PS_FIELD(62 + ((i) * 10), 10)
63 #define NUM_ANCHOR_REFS_L(i)				PS_FIELD(82 + ((i) * 11), 1)
64 #define ANCHOR_REF_L(i)				PS_FIELD(83 + ((i) * 11), 10)
65 #define NUM_NON_ANCHOR_REFS_L(i)			PS_FIELD(104 + ((i) * 11), 1)
66 #define NON_ANCHOR_REFS_L(i)				PS_FIELD(105 + ((i) * 11), 10)
67 #define PIC_PARAMETER_SET_ID				PS_FIELD(128, 8)
68 #define PPS_SEQ_PARAMETER_SET_ID			PS_FIELD(136, 5)
69 #define ENTROPY_CODING_MODE_FLAG			PS_FIELD(141, 1)
70 #define BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT_FLAG	PS_FIELD(142, 1)
71 #define NUM_REF_IDX_L_DEFAULT_ACTIVE_MINUS1(i)		PS_FIELD(143 + ((i) * 5), 5)
72 #define WEIGHTED_PRED_FLAG				PS_FIELD(153, 1)
73 #define WEIGHTED_BIPRED_IDC				PS_FIELD(154, 2)
74 #define PIC_INIT_QP_MINUS26				PS_FIELD(156, 7)
75 #define PIC_INIT_QS_MINUS26				PS_FIELD(163, 6)
76 #define CHROMA_QP_INDEX_OFFSET				PS_FIELD(169, 5)
77 #define DEBLOCKING_FILTER_CONTROL_PRESENT_FLAG		PS_FIELD(174, 1)
78 #define CONSTRAINED_INTRA_PRED_FLAG			PS_FIELD(175, 1)
79 #define REDUNDANT_PIC_CNT_PRESENT			PS_FIELD(176, 1)
80 #define TRANSFORM_8X8_MODE_FLAG			PS_FIELD(177, 1)
81 #define SECOND_CHROMA_QP_INDEX_OFFSET			PS_FIELD(178, 5)
82 #define SCALING_LIST_ENABLE_FLAG			PS_FIELD(183, 1)
83 #define SCALING_LIST_ADDRESS				PS_FIELD(184, 32)
84 #define IS_LONG_TERM(i)				PS_FIELD(216 + (i), 1)
85 
86 #define DPB_OFFS(i, j)					(288 + ((j) * 32 * 7) + ((i) * 7))
87 #define DPB_INFO(i, j)					PS_FIELD(DPB_OFFS(i, j), 5)
88 #define BOTTOM_FLAG(i, j)				PS_FIELD(DPB_OFFS(i, j) + 5, 1)
89 #define VIEW_INDEX_OFF(i, j)				PS_FIELD(DPB_OFFS(i, j) + 6, 1)
90 
91 /* Data structure describing auxiliary buffer format. */
92 struct rkvdec_h264_priv_tbl {
93 	s8 cabac_table[4][464][2];
94 	struct rkvdec_h264_scaling_list scaling_list;
95 	u32 rps[RKV_RPS_SIZE];
96 	struct rkvdec_sps_pps_packet param_set[256];
97 	u8 err_info[RKV_ERROR_INFO_SIZE];
98 };
99 
100 struct rkvdec_h264_reflists {
101 	struct v4l2_h264_reference p[V4L2_H264_REF_LIST_LEN];
102 	struct v4l2_h264_reference b0[V4L2_H264_REF_LIST_LEN];
103 	struct v4l2_h264_reference b1[V4L2_H264_REF_LIST_LEN];
104 };
105 
106 struct rkvdec_h264_run {
107 	struct rkvdec_run base;
108 	const struct v4l2_ctrl_h264_decode_params *decode_params;
109 	const struct v4l2_ctrl_h264_sps *sps;
110 	const struct v4l2_ctrl_h264_pps *pps;
111 	const struct v4l2_ctrl_h264_scaling_matrix *scaling_matrix;
112 	struct vb2_buffer *ref_buf[V4L2_H264_NUM_DPB_ENTRIES];
113 };
114 
115 struct rkvdec_h264_ctx {
116 	struct rkvdec_aux_buf priv_tbl;
117 	struct rkvdec_h264_reflists reflists;
118 };
119 
120 #define CABAC_ENTRY(ctxidx, idc0_m, idc0_n, idc1_m, idc1_n,		\
121 	    idc2_m, idc2_n, intra_m, intra_n)			\
122 	[0][(ctxidx)] = {idc0_m, idc0_n},			\
123 	[1][(ctxidx)] = {idc1_m, idc1_n},			\
124 	[2][(ctxidx)] = {idc2_m, idc2_n},			\
125 	[3][(ctxidx)] = {intra_m, intra_n}
126 
127 /*
128  * Constant CABAC table.
129  * Built from the tables described in section '9.3.1.1 Initialisation process
130  * for context variables' of the H264 spec.
131  */
132 static const s8 rkvdec_h264_cabac_table[4][464][2] = {
133 	/* Table 9-12 – Values of variables m and n for ctxIdx from 0 to 10 */
134 	CABAC_ENTRY(0, 20, -15, 20, -15, 20, -15, 20, -15),
135 	CABAC_ENTRY(1, 2, 54, 2, 54, 2, 54, 2, 54),
136 	CABAC_ENTRY(2, 3, 74, 3, 74, 3, 74, 3, 74),
137 	CABAC_ENTRY(3, 20, -15, 20, -15, 20, -15, 20, -15),
138 	CABAC_ENTRY(4, 2, 54, 2, 54, 2, 54, 2, 54),
139 	CABAC_ENTRY(5, 3, 74, 3, 74, 3, 74, 3, 74),
140 	CABAC_ENTRY(6, -28, 127, -28, 127, -28, 127, -28, 127),
141 	CABAC_ENTRY(7, -23, 104, -23, 104, -23, 104, -23, 104),
142 	CABAC_ENTRY(8, -6, 53, -6, 53, -6, 53, -6, 53),
143 	CABAC_ENTRY(9, -1, 54, -1, 54, -1, 54, -1, 54),
144 	CABAC_ENTRY(10, 7, 51, 7, 51, 7, 51, 7, 51),
145 
146 	/* Table 9-13 – Values of variables m and n for ctxIdx from 11 to 23 */
147 	CABAC_ENTRY(11, 23, 33, 22, 25, 29, 16, 0, 0),
148 	CABAC_ENTRY(12, 23, 2, 34, 0, 25, 0, 0, 0),
149 	CABAC_ENTRY(13, 21, 0, 16, 0, 14, 0, 0, 0),
150 	CABAC_ENTRY(14, 1, 9, -2, 9, -10, 51, 0, 0),
151 	CABAC_ENTRY(15, 0, 49, 4, 41, -3, 62, 0, 0),
152 	CABAC_ENTRY(16, -37, 118, -29, 118, -27, 99, 0, 0),
153 	CABAC_ENTRY(17, 5, 57, 2, 65, 26, 16, 0, 0),
154 	CABAC_ENTRY(18, -13, 78, -6, 71, -4, 85, 0, 0),
155 	CABAC_ENTRY(19, -11, 65, -13, 79, -24, 102, 0, 0),
156 	CABAC_ENTRY(20, 1, 62, 5, 52, 5, 57, 0, 0),
157 	CABAC_ENTRY(21, 12, 49, 9, 50, 6, 57, 0, 0),
158 	CABAC_ENTRY(22, -4, 73, -3, 70, -17, 73, 0, 0),
159 	CABAC_ENTRY(23, 17, 50, 10, 54, 14, 57, 0, 0),
160 
161 	/* Table 9-14 – Values of variables m and n for ctxIdx from 24 to 39 */
162 	CABAC_ENTRY(24, 18, 64, 26, 34, 20, 40, 0, 0),
163 	CABAC_ENTRY(25, 9, 43, 19, 22, 20, 10, 0, 0),
164 	CABAC_ENTRY(26, 29, 0, 40, 0, 29, 0, 0, 0),
165 	CABAC_ENTRY(27, 26, 67, 57, 2, 54, 0, 0, 0),
166 	CABAC_ENTRY(28, 16, 90, 41, 36, 37, 42, 0, 0),
167 	CABAC_ENTRY(29, 9, 104, 26, 69, 12, 97, 0, 0),
168 	CABAC_ENTRY(30, -46, 127, -45, 127, -32, 127, 0, 0),
169 	CABAC_ENTRY(31, -20, 104, -15, 101, -22, 117, 0, 0),
170 	CABAC_ENTRY(32, 1, 67, -4, 76, -2, 74, 0, 0),
171 	CABAC_ENTRY(33, -13, 78, -6, 71, -4, 85, 0, 0),
172 	CABAC_ENTRY(34, -11, 65, -13, 79, -24, 102, 0, 0),
173 	CABAC_ENTRY(35, 1, 62, 5, 52, 5, 57, 0, 0),
174 	CABAC_ENTRY(36, -6, 86, 6, 69, -6, 93, 0, 0),
175 	CABAC_ENTRY(37, -17, 95, -13, 90, -14, 88, 0, 0),
176 	CABAC_ENTRY(38, -6, 61, 0, 52, -6, 44, 0, 0),
177 	CABAC_ENTRY(39, 9, 45, 8, 43, 4, 55, 0, 0),
178 
179 	/* Table 9-15 – Values of variables m and n for ctxIdx from 40 to 53 */
180 	CABAC_ENTRY(40, -3, 69, -2, 69, -11, 89, 0, 0),
181 	CABAC_ENTRY(41, -6, 81, -5, 82, -15, 103, 0, 0),
182 	CABAC_ENTRY(42, -11, 96, -10, 96, -21, 116, 0, 0),
183 	CABAC_ENTRY(43, 6, 55, 2, 59, 19, 57, 0, 0),
184 	CABAC_ENTRY(44, 7, 67, 2, 75, 20, 58, 0, 0),
185 	CABAC_ENTRY(45, -5, 86, -3, 87, 4, 84, 0, 0),
186 	CABAC_ENTRY(46, 2, 88, -3, 100, 6, 96, 0, 0),
187 	CABAC_ENTRY(47, 0, 58, 1, 56, 1, 63, 0, 0),
188 	CABAC_ENTRY(48, -3, 76, -3, 74, -5, 85, 0, 0),
189 	CABAC_ENTRY(49, -10, 94, -6, 85, -13, 106, 0, 0),
190 	CABAC_ENTRY(50, 5, 54, 0, 59, 5, 63, 0, 0),
191 	CABAC_ENTRY(51, 4, 69, -3, 81, 6, 75, 0, 0),
192 	CABAC_ENTRY(52, -3, 81, -7, 86, -3, 90, 0, 0),
193 	CABAC_ENTRY(53, 0, 88, -5, 95, -1, 101, 0, 0),
194 
195 	/* Table 9-16 – Values of variables m and n for ctxIdx from 54 to 59 */
196 	CABAC_ENTRY(54, -7, 67, -1, 66, 3, 55, 0, 0),
197 	CABAC_ENTRY(55, -5, 74, -1, 77, -4, 79, 0, 0),
198 	CABAC_ENTRY(56, -4, 74, 1, 70, -2, 75, 0, 0),
199 	CABAC_ENTRY(57, -5, 80, -2, 86, -12, 97, 0, 0),
200 	CABAC_ENTRY(58, -7, 72, -5, 72, -7, 50, 0, 0),
201 	CABAC_ENTRY(59, 1, 58, 0, 61, 1, 60, 0, 0),
202 
203 	/* Table 9-17 – Values of variables m and n for ctxIdx from 60 to 69 */
204 	CABAC_ENTRY(60, 0, 41, 0, 41, 0, 41, 0, 41),
205 	CABAC_ENTRY(61, 0, 63, 0, 63, 0, 63, 0, 63),
206 	CABAC_ENTRY(62, 0, 63, 0, 63, 0, 63, 0, 63),
207 	CABAC_ENTRY(63, 0, 63, 0, 63, 0, 63, 0, 63),
208 	CABAC_ENTRY(64, -9, 83, -9, 83, -9, 83, -9, 83),
209 	CABAC_ENTRY(65, 4, 86, 4, 86, 4, 86, 4, 86),
210 	CABAC_ENTRY(66, 0, 97, 0, 97, 0, 97, 0, 97),
211 	CABAC_ENTRY(67, -7, 72, -7, 72, -7, 72, -7, 72),
212 	CABAC_ENTRY(68, 13, 41, 13, 41, 13, 41, 13, 41),
213 	CABAC_ENTRY(69, 3, 62, 3, 62, 3, 62, 3, 62),
214 
215 	/* Table 9-18 – Values of variables m and n for ctxIdx from 70 to 104 */
216 	CABAC_ENTRY(70, 0, 45, 13, 15, 7, 34, 0, 11),
217 	CABAC_ENTRY(71, -4, 78, 7, 51, -9, 88, 1, 55),
218 	CABAC_ENTRY(72, -3, 96, 2, 80, -20, 127, 0, 69),
219 	CABAC_ENTRY(73, -27, 126, -39, 127, -36, 127, -17, 127),
220 	CABAC_ENTRY(74, -28, 98, -18, 91, -17, 91, -13, 102),
221 	CABAC_ENTRY(75, -25, 101, -17, 96, -14, 95, 0, 82),
222 	CABAC_ENTRY(76, -23, 67, -26, 81, -25, 84, -7, 74),
223 	CABAC_ENTRY(77, -28, 82, -35, 98, -25, 86, -21, 107),
224 	CABAC_ENTRY(78, -20, 94, -24, 102, -12, 89, -27, 127),
225 	CABAC_ENTRY(79, -16, 83, -23, 97, -17, 91, -31, 127),
226 	CABAC_ENTRY(80, -22, 110, -27, 119, -31, 127, -24, 127),
227 	CABAC_ENTRY(81, -21, 91, -24, 99, -14, 76, -18, 95),
228 	CABAC_ENTRY(82, -18, 102, -21, 110, -18, 103, -27, 127),
229 	CABAC_ENTRY(83, -13, 93, -18, 102, -13, 90, -21, 114),
230 	CABAC_ENTRY(84, -29, 127, -36, 127, -37, 127, -30, 127),
231 	CABAC_ENTRY(85, -7, 92, 0, 80, 11, 80, -17, 123),
232 	CABAC_ENTRY(86, -5, 89, -5, 89, 5, 76, -12, 115),
233 	CABAC_ENTRY(87, -7, 96, -7, 94, 2, 84, -16, 122),
234 	CABAC_ENTRY(88, -13, 108, -4, 92, 5, 78, -11, 115),
235 	CABAC_ENTRY(89, -3, 46, 0, 39, -6, 55, -12, 63),
236 	CABAC_ENTRY(90, -1, 65, 0, 65, 4, 61, -2, 68),
237 	CABAC_ENTRY(91, -1, 57, -15, 84, -14, 83, -15, 84),
238 	CABAC_ENTRY(92, -9, 93, -35, 127, -37, 127, -13, 104),
239 	CABAC_ENTRY(93, -3, 74, -2, 73, -5, 79, -3, 70),
240 	CABAC_ENTRY(94, -9, 92, -12, 104, -11, 104, -8, 93),
241 	CABAC_ENTRY(95, -8, 87, -9, 91, -11, 91, -10, 90),
242 	CABAC_ENTRY(96, -23, 126, -31, 127, -30, 127, -30, 127),
243 	CABAC_ENTRY(97, 5, 54, 3, 55, 0, 65, -1, 74),
244 	CABAC_ENTRY(98, 6, 60, 7, 56, -2, 79, -6, 97),
245 	CABAC_ENTRY(99, 6, 59, 7, 55, 0, 72, -7, 91),
246 	CABAC_ENTRY(100, 6, 69, 8, 61, -4, 92, -20, 127),
247 	CABAC_ENTRY(101, -1, 48, -3, 53, -6, 56, -4, 56),
248 	CABAC_ENTRY(102, 0, 68, 0, 68, 3, 68, -5, 82),
249 	CABAC_ENTRY(103, -4, 69, -7, 74, -8, 71, -7, 76),
250 	CABAC_ENTRY(104, -8, 88, -9, 88, -13, 98, -22, 125),
251 
252 	/* Table 9-19 – Values of variables m and n for ctxIdx from 105 to 165 */
253 	CABAC_ENTRY(105, -2, 85, -13, 103, -4, 86, -7, 93),
254 	CABAC_ENTRY(106, -6, 78, -13, 91, -12, 88, -11, 87),
255 	CABAC_ENTRY(107, -1, 75, -9, 89, -5, 82, -3, 77),
256 	CABAC_ENTRY(108, -7, 77, -14, 92, -3, 72, -5, 71),
257 	CABAC_ENTRY(109, 2, 54, -8, 76, -4, 67, -4, 63),
258 	CABAC_ENTRY(110, 5, 50, -12, 87, -8, 72, -4, 68),
259 	CABAC_ENTRY(111, -3, 68, -23, 110, -16, 89, -12, 84),
260 	CABAC_ENTRY(112, 1, 50, -24, 105, -9, 69, -7, 62),
261 	CABAC_ENTRY(113, 6, 42, -10, 78, -1, 59, -7, 65),
262 	CABAC_ENTRY(114, -4, 81, -20, 112, 5, 66, 8, 61),
263 	CABAC_ENTRY(115, 1, 63, -17, 99, 4, 57, 5, 56),
264 	CABAC_ENTRY(116, -4, 70, -78, 127, -4, 71, -2, 66),
265 	CABAC_ENTRY(117, 0, 67, -70, 127, -2, 71, 1, 64),
266 	CABAC_ENTRY(118, 2, 57, -50, 127, 2, 58, 0, 61),
267 	CABAC_ENTRY(119, -2, 76, -46, 127, -1, 74, -2, 78),
268 	CABAC_ENTRY(120, 11, 35, -4, 66, -4, 44, 1, 50),
269 	CABAC_ENTRY(121, 4, 64, -5, 78, -1, 69, 7, 52),
270 	CABAC_ENTRY(122, 1, 61, -4, 71, 0, 62, 10, 35),
271 	CABAC_ENTRY(123, 11, 35, -8, 72, -7, 51, 0, 44),
272 	CABAC_ENTRY(124, 18, 25, 2, 59, -4, 47, 11, 38),
273 	CABAC_ENTRY(125, 12, 24, -1, 55, -6, 42, 1, 45),
274 	CABAC_ENTRY(126, 13, 29, -7, 70, -3, 41, 0, 46),
275 	CABAC_ENTRY(127, 13, 36, -6, 75, -6, 53, 5, 44),
276 	CABAC_ENTRY(128, -10, 93, -8, 89, 8, 76, 31, 17),
277 	CABAC_ENTRY(129, -7, 73, -34, 119, -9, 78, 1, 51),
278 	CABAC_ENTRY(130, -2, 73, -3, 75, -11, 83, 7, 50),
279 	CABAC_ENTRY(131, 13, 46, 32, 20, 9, 52, 28, 19),
280 	CABAC_ENTRY(132, 9, 49, 30, 22, 0, 67, 16, 33),
281 	CABAC_ENTRY(133, -7, 100, -44, 127, -5, 90, 14, 62),
282 	CABAC_ENTRY(134, 9, 53, 0, 54, 1, 67, -13, 108),
283 	CABAC_ENTRY(135, 2, 53, -5, 61, -15, 72, -15, 100),
284 	CABAC_ENTRY(136, 5, 53, 0, 58, -5, 75, -13, 101),
285 	CABAC_ENTRY(137, -2, 61, -1, 60, -8, 80, -13, 91),
286 	CABAC_ENTRY(138, 0, 56, -3, 61, -21, 83, -12, 94),
287 	CABAC_ENTRY(139, 0, 56, -8, 67, -21, 64, -10, 88),
288 	CABAC_ENTRY(140, -13, 63, -25, 84, -13, 31, -16, 84),
289 	CABAC_ENTRY(141, -5, 60, -14, 74, -25, 64, -10, 86),
290 	CABAC_ENTRY(142, -1, 62, -5, 65, -29, 94, -7, 83),
291 	CABAC_ENTRY(143, 4, 57, 5, 52, 9, 75, -13, 87),
292 	CABAC_ENTRY(144, -6, 69, 2, 57, 17, 63, -19, 94),
293 	CABAC_ENTRY(145, 4, 57, 0, 61, -8, 74, 1, 70),
294 	CABAC_ENTRY(146, 14, 39, -9, 69, -5, 35, 0, 72),
295 	CABAC_ENTRY(147, 4, 51, -11, 70, -2, 27, -5, 74),
296 	CABAC_ENTRY(148, 13, 68, 18, 55, 13, 91, 18, 59),
297 	CABAC_ENTRY(149, 3, 64, -4, 71, 3, 65, -8, 102),
298 	CABAC_ENTRY(150, 1, 61, 0, 58, -7, 69, -15, 100),
299 	CABAC_ENTRY(151, 9, 63, 7, 61, 8, 77, 0, 95),
300 	CABAC_ENTRY(152, 7, 50, 9, 41, -10, 66, -4, 75),
301 	CABAC_ENTRY(153, 16, 39, 18, 25, 3, 62, 2, 72),
302 	CABAC_ENTRY(154, 5, 44, 9, 32, -3, 68, -11, 75),
303 	CABAC_ENTRY(155, 4, 52, 5, 43, -20, 81, -3, 71),
304 	CABAC_ENTRY(156, 11, 48, 9, 47, 0, 30, 15, 46),
305 	CABAC_ENTRY(157, -5, 60, 0, 44, 1, 7, -13, 69),
306 	CABAC_ENTRY(158, -1, 59, 0, 51, -3, 23, 0, 62),
307 	CABAC_ENTRY(159, 0, 59, 2, 46, -21, 74, 0, 65),
308 	CABAC_ENTRY(160, 22, 33, 19, 38, 16, 66, 21, 37),
309 	CABAC_ENTRY(161, 5, 44, -4, 66, -23, 124, -15, 72),
310 	CABAC_ENTRY(162, 14, 43, 15, 38, 17, 37, 9, 57),
311 	CABAC_ENTRY(163, -1, 78, 12, 42, 44, -18, 16, 54),
312 	CABAC_ENTRY(164, 0, 60, 9, 34, 50, -34, 0, 62),
313 	CABAC_ENTRY(165, 9, 69, 0, 89, -22, 127, 12, 72),
314 
315 	/* Table 9-20 – Values of variables m and n for ctxIdx from 166 to 226 */
316 	CABAC_ENTRY(166, 11, 28, 4, 45, 4, 39, 24, 0),
317 	CABAC_ENTRY(167, 2, 40, 10, 28, 0, 42, 15, 9),
318 	CABAC_ENTRY(168, 3, 44, 10, 31, 7, 34, 8, 25),
319 	CABAC_ENTRY(169, 0, 49, 33, -11, 11, 29, 13, 18),
320 	CABAC_ENTRY(170, 0, 46, 52, -43, 8, 31, 15, 9),
321 	CABAC_ENTRY(171, 2, 44, 18, 15, 6, 37, 13, 19),
322 	CABAC_ENTRY(172, 2, 51, 28, 0, 7, 42, 10, 37),
323 	CABAC_ENTRY(173, 0, 47, 35, -22, 3, 40, 12, 18),
324 	CABAC_ENTRY(174, 4, 39, 38, -25, 8, 33, 6, 29),
325 	CABAC_ENTRY(175, 2, 62, 34, 0, 13, 43, 20, 33),
326 	CABAC_ENTRY(176, 6, 46, 39, -18, 13, 36, 15, 30),
327 	CABAC_ENTRY(177, 0, 54, 32, -12, 4, 47, 4, 45),
328 	CABAC_ENTRY(178, 3, 54, 102, -94, 3, 55, 1, 58),
329 	CABAC_ENTRY(179, 2, 58, 0, 0, 2, 58, 0, 62),
330 	CABAC_ENTRY(180, 4, 63, 56, -15, 6, 60, 7, 61),
331 	CABAC_ENTRY(181, 6, 51, 33, -4, 8, 44, 12, 38),
332 	CABAC_ENTRY(182, 6, 57, 29, 10, 11, 44, 11, 45),
333 	CABAC_ENTRY(183, 7, 53, 37, -5, 14, 42, 15, 39),
334 	CABAC_ENTRY(184, 6, 52, 51, -29, 7, 48, 11, 42),
335 	CABAC_ENTRY(185, 6, 55, 39, -9, 4, 56, 13, 44),
336 	CABAC_ENTRY(186, 11, 45, 52, -34, 4, 52, 16, 45),
337 	CABAC_ENTRY(187, 14, 36, 69, -58, 13, 37, 12, 41),
338 	CABAC_ENTRY(188, 8, 53, 67, -63, 9, 49, 10, 49),
339 	CABAC_ENTRY(189, -1, 82, 44, -5, 19, 58, 30, 34),
340 	CABAC_ENTRY(190, 7, 55, 32, 7, 10, 48, 18, 42),
341 	CABAC_ENTRY(191, -3, 78, 55, -29, 12, 45, 10, 55),
342 	CABAC_ENTRY(192, 15, 46, 32, 1, 0, 69, 17, 51),
343 	CABAC_ENTRY(193, 22, 31, 0, 0, 20, 33, 17, 46),
344 	CABAC_ENTRY(194, -1, 84, 27, 36, 8, 63, 0, 89),
345 	CABAC_ENTRY(195, 25, 7, 33, -25, 35, -18, 26, -19),
346 	CABAC_ENTRY(196, 30, -7, 34, -30, 33, -25, 22, -17),
347 	CABAC_ENTRY(197, 28, 3, 36, -28, 28, -3, 26, -17),
348 	CABAC_ENTRY(198, 28, 4, 38, -28, 24, 10, 30, -25),
349 	CABAC_ENTRY(199, 32, 0, 38, -27, 27, 0, 28, -20),
350 	CABAC_ENTRY(200, 34, -1, 34, -18, 34, -14, 33, -23),
351 	CABAC_ENTRY(201, 30, 6, 35, -16, 52, -44, 37, -27),
352 	CABAC_ENTRY(202, 30, 6, 34, -14, 39, -24, 33, -23),
353 	CABAC_ENTRY(203, 32, 9, 32, -8, 19, 17, 40, -28),
354 	CABAC_ENTRY(204, 31, 19, 37, -6, 31, 25, 38, -17),
355 	CABAC_ENTRY(205, 26, 27, 35, 0, 36, 29, 33, -11),
356 	CABAC_ENTRY(206, 26, 30, 30, 10, 24, 33, 40, -15),
357 	CABAC_ENTRY(207, 37, 20, 28, 18, 34, 15, 41, -6),
358 	CABAC_ENTRY(208, 28, 34, 26, 25, 30, 20, 38, 1),
359 	CABAC_ENTRY(209, 17, 70, 29, 41, 22, 73, 41, 17),
360 	CABAC_ENTRY(210, 1, 67, 0, 75, 20, 34, 30, -6),
361 	CABAC_ENTRY(211, 5, 59, 2, 72, 19, 31, 27, 3),
362 	CABAC_ENTRY(212, 9, 67, 8, 77, 27, 44, 26, 22),
363 	CABAC_ENTRY(213, 16, 30, 14, 35, 19, 16, 37, -16),
364 	CABAC_ENTRY(214, 18, 32, 18, 31, 15, 36, 35, -4),
365 	CABAC_ENTRY(215, 18, 35, 17, 35, 15, 36, 38, -8),
366 	CABAC_ENTRY(216, 22, 29, 21, 30, 21, 28, 38, -3),
367 	CABAC_ENTRY(217, 24, 31, 17, 45, 25, 21, 37, 3),
368 	CABAC_ENTRY(218, 23, 38, 20, 42, 30, 20, 38, 5),
369 	CABAC_ENTRY(219, 18, 43, 18, 45, 31, 12, 42, 0),
370 	CABAC_ENTRY(220, 20, 41, 27, 26, 27, 16, 35, 16),
371 	CABAC_ENTRY(221, 11, 63, 16, 54, 24, 42, 39, 22),
372 	CABAC_ENTRY(222, 9, 59, 7, 66, 0, 93, 14, 48),
373 	CABAC_ENTRY(223, 9, 64, 16, 56, 14, 56, 27, 37),
374 	CABAC_ENTRY(224, -1, 94, 11, 73, 15, 57, 21, 60),
375 	CABAC_ENTRY(225, -2, 89, 10, 67, 26, 38, 12, 68),
376 	CABAC_ENTRY(226, -9, 108, -10, 116, -24, 127, 2, 97),
377 
378 	/* Table 9-21 – Values of variables m and n for ctxIdx from 227 to 275 */
379 	CABAC_ENTRY(227, -6, 76, -23, 112, -24, 115, -3, 71),
380 	CABAC_ENTRY(228, -2, 44, -15, 71, -22, 82, -6, 42),
381 	CABAC_ENTRY(229, 0, 45, -7, 61, -9, 62, -5, 50),
382 	CABAC_ENTRY(230, 0, 52, 0, 53, 0, 53, -3, 54),
383 	CABAC_ENTRY(231, -3, 64, -5, 66, 0, 59, -2, 62),
384 	CABAC_ENTRY(232, -2, 59, -11, 77, -14, 85, 0, 58),
385 	CABAC_ENTRY(233, -4, 70, -9, 80, -13, 89, 1, 63),
386 	CABAC_ENTRY(234, -4, 75, -9, 84, -13, 94, -2, 72),
387 	CABAC_ENTRY(235, -8, 82, -10, 87, -11, 92, -1, 74),
388 	CABAC_ENTRY(236, -17, 102, -34, 127, -29, 127, -9, 91),
389 	CABAC_ENTRY(237, -9, 77, -21, 101, -21, 100, -5, 67),
390 	CABAC_ENTRY(238, 3, 24, -3, 39, -14, 57, -5, 27),
391 	CABAC_ENTRY(239, 0, 42, -5, 53, -12, 67, -3, 39),
392 	CABAC_ENTRY(240, 0, 48, -7, 61, -11, 71, -2, 44),
393 	CABAC_ENTRY(241, 0, 55, -11, 75, -10, 77, 0, 46),
394 	CABAC_ENTRY(242, -6, 59, -15, 77, -21, 85, -16, 64),
395 	CABAC_ENTRY(243, -7, 71, -17, 91, -16, 88, -8, 68),
396 	CABAC_ENTRY(244, -12, 83, -25, 107, -23, 104, -10, 78),
397 	CABAC_ENTRY(245, -11, 87, -25, 111, -15, 98, -6, 77),
398 	CABAC_ENTRY(246, -30, 119, -28, 122, -37, 127, -10, 86),
399 	CABAC_ENTRY(247, 1, 58, -11, 76, -10, 82, -12, 92),
400 	CABAC_ENTRY(248, -3, 29, -10, 44, -8, 48, -15, 55),
401 	CABAC_ENTRY(249, -1, 36, -10, 52, -8, 61, -10, 60),
402 	CABAC_ENTRY(250, 1, 38, -10, 57, -8, 66, -6, 62),
403 	CABAC_ENTRY(251, 2, 43, -9, 58, -7, 70, -4, 65),
404 	CABAC_ENTRY(252, -6, 55, -16, 72, -14, 75, -12, 73),
405 	CABAC_ENTRY(253, 0, 58, -7, 69, -10, 79, -8, 76),
406 	CABAC_ENTRY(254, 0, 64, -4, 69, -9, 83, -7, 80),
407 	CABAC_ENTRY(255, -3, 74, -5, 74, -12, 92, -9, 88),
408 	CABAC_ENTRY(256, -10, 90, -9, 86, -18, 108, -17, 110),
409 	CABAC_ENTRY(257, 0, 70, 2, 66, -4, 79, -11, 97),
410 	CABAC_ENTRY(258, -4, 29, -9, 34, -22, 69, -20, 84),
411 	CABAC_ENTRY(259, 5, 31, 1, 32, -16, 75, -11, 79),
412 	CABAC_ENTRY(260, 7, 42, 11, 31, -2, 58, -6, 73),
413 	CABAC_ENTRY(261, 1, 59, 5, 52, 1, 58, -4, 74),
414 	CABAC_ENTRY(262, -2, 58, -2, 55, -13, 78, -13, 86),
415 	CABAC_ENTRY(263, -3, 72, -2, 67, -9, 83, -13, 96),
416 	CABAC_ENTRY(264, -3, 81, 0, 73, -4, 81, -11, 97),
417 	CABAC_ENTRY(265, -11, 97, -8, 89, -13, 99, -19, 117),
418 	CABAC_ENTRY(266, 0, 58, 3, 52, -13, 81, -8, 78),
419 	CABAC_ENTRY(267, 8, 5, 7, 4, -6, 38, -5, 33),
420 	CABAC_ENTRY(268, 10, 14, 10, 8, -13, 62, -4, 48),
421 	CABAC_ENTRY(269, 14, 18, 17, 8, -6, 58, -2, 53),
422 	CABAC_ENTRY(270, 13, 27, 16, 19, -2, 59, -3, 62),
423 	CABAC_ENTRY(271, 2, 40, 3, 37, -16, 73, -13, 71),
424 	CABAC_ENTRY(272, 0, 58, -1, 61, -10, 76, -10, 79),
425 	CABAC_ENTRY(273, -3, 70, -5, 73, -13, 86, -12, 86),
426 	CABAC_ENTRY(274, -6, 79, -1, 70, -9, 83, -13, 90),
427 	CABAC_ENTRY(275, -8, 85, -4, 78, -10, 87, -14, 97),
428 
429 	/* Table 9-22 – Values of variables m and n for ctxIdx from 277 to 337 */
430 	CABAC_ENTRY(277, -13, 106, -21, 126, -22, 127, -6, 93),
431 	CABAC_ENTRY(278, -16, 106, -23, 124, -25, 127, -6, 84),
432 	CABAC_ENTRY(279, -10, 87, -20, 110, -25, 120, -8, 79),
433 	CABAC_ENTRY(280, -21, 114, -26, 126, -27, 127, 0, 66),
434 	CABAC_ENTRY(281, -18, 110, -25, 124, -19, 114, -1, 71),
435 	CABAC_ENTRY(282, -14, 98, -17, 105, -23, 117, 0, 62),
436 	CABAC_ENTRY(283, -22, 110, -27, 121, -25, 118, -2, 60),
437 	CABAC_ENTRY(284, -21, 106, -27, 117, -26, 117, -2, 59),
438 	CABAC_ENTRY(285, -18, 103, -17, 102, -24, 113, -5, 75),
439 	CABAC_ENTRY(286, -21, 107, -26, 117, -28, 118, -3, 62),
440 	CABAC_ENTRY(287, -23, 108, -27, 116, -31, 120, -4, 58),
441 	CABAC_ENTRY(288, -26, 112, -33, 122, -37, 124, -9, 66),
442 	CABAC_ENTRY(289, -10, 96, -10, 95, -10, 94, -1, 79),
443 	CABAC_ENTRY(290, -12, 95, -14, 100, -15, 102, 0, 71),
444 	CABAC_ENTRY(291, -5, 91, -8, 95, -10, 99, 3, 68),
445 	CABAC_ENTRY(292, -9, 93, -17, 111, -13, 106, 10, 44),
446 	CABAC_ENTRY(293, -22, 94, -28, 114, -50, 127, -7, 62),
447 	CABAC_ENTRY(294, -5, 86, -6, 89, -5, 92, 15, 36),
448 	CABAC_ENTRY(295, 9, 67, -2, 80, 17, 57, 14, 40),
449 	CABAC_ENTRY(296, -4, 80, -4, 82, -5, 86, 16, 27),
450 	CABAC_ENTRY(297, -10, 85, -9, 85, -13, 94, 12, 29),
451 	CABAC_ENTRY(298, -1, 70, -8, 81, -12, 91, 1, 44),
452 	CABAC_ENTRY(299, 7, 60, -1, 72, -2, 77, 20, 36),
453 	CABAC_ENTRY(300, 9, 58, 5, 64, 0, 71, 18, 32),
454 	CABAC_ENTRY(301, 5, 61, 1, 67, -1, 73, 5, 42),
455 	CABAC_ENTRY(302, 12, 50, 9, 56, 4, 64, 1, 48),
456 	CABAC_ENTRY(303, 15, 50, 0, 69, -7, 81, 10, 62),
457 	CABAC_ENTRY(304, 18, 49, 1, 69, 5, 64, 17, 46),
458 	CABAC_ENTRY(305, 17, 54, 7, 69, 15, 57, 9, 64),
459 	CABAC_ENTRY(306, 10, 41, -7, 69, 1, 67, -12, 104),
460 	CABAC_ENTRY(307, 7, 46, -6, 67, 0, 68, -11, 97),
461 	CABAC_ENTRY(308, -1, 51, -16, 77, -10, 67, -16, 96),
462 	CABAC_ENTRY(309, 7, 49, -2, 64, 1, 68, -7, 88),
463 	CABAC_ENTRY(310, 8, 52, 2, 61, 0, 77, -8, 85),
464 	CABAC_ENTRY(311, 9, 41, -6, 67, 2, 64, -7, 85),
465 	CABAC_ENTRY(312, 6, 47, -3, 64, 0, 68, -9, 85),
466 	CABAC_ENTRY(313, 2, 55, 2, 57, -5, 78, -13, 88),
467 	CABAC_ENTRY(314, 13, 41, -3, 65, 7, 55, 4, 66),
468 	CABAC_ENTRY(315, 10, 44, -3, 66, 5, 59, -3, 77),
469 	CABAC_ENTRY(316, 6, 50, 0, 62, 2, 65, -3, 76),
470 	CABAC_ENTRY(317, 5, 53, 9, 51, 14, 54, -6, 76),
471 	CABAC_ENTRY(318, 13, 49, -1, 66, 15, 44, 10, 58),
472 	CABAC_ENTRY(319, 4, 63, -2, 71, 5, 60, -1, 76),
473 	CABAC_ENTRY(320, 6, 64, -2, 75, 2, 70, -1, 83),
474 	CABAC_ENTRY(321, -2, 69, -1, 70, -2, 76, -7, 99),
475 	CABAC_ENTRY(322, -2, 59, -9, 72, -18, 86, -14, 95),
476 	CABAC_ENTRY(323, 6, 70, 14, 60, 12, 70, 2, 95),
477 	CABAC_ENTRY(324, 10, 44, 16, 37, 5, 64, 0, 76),
478 	CABAC_ENTRY(325, 9, 31, 0, 47, -12, 70, -5, 74),
479 	CABAC_ENTRY(326, 12, 43, 18, 35, 11, 55, 0, 70),
480 	CABAC_ENTRY(327, 3, 53, 11, 37, 5, 56, -11, 75),
481 	CABAC_ENTRY(328, 14, 34, 12, 41, 0, 69, 1, 68),
482 	CABAC_ENTRY(329, 10, 38, 10, 41, 2, 65, 0, 65),
483 	CABAC_ENTRY(330, -3, 52, 2, 48, -6, 74, -14, 73),
484 	CABAC_ENTRY(331, 13, 40, 12, 41, 5, 54, 3, 62),
485 	CABAC_ENTRY(332, 17, 32, 13, 41, 7, 54, 4, 62),
486 	CABAC_ENTRY(333, 7, 44, 0, 59, -6, 76, -1, 68),
487 	CABAC_ENTRY(334, 7, 38, 3, 50, -11, 82, -13, 75),
488 	CABAC_ENTRY(335, 13, 50, 19, 40, -2, 77, 11, 55),
489 	CABAC_ENTRY(336, 10, 57, 3, 66, -2, 77, 5, 64),
490 	CABAC_ENTRY(337, 26, 43, 18, 50, 25, 42, 12, 70),
491 
492 	/* Table 9-23 – Values of variables m and n for ctxIdx from 338 to 398 */
493 	CABAC_ENTRY(338, 14, 11, 19, -6, 17, -13, 15, 6),
494 	CABAC_ENTRY(339, 11, 14, 18, -6, 16, -9, 6, 19),
495 	CABAC_ENTRY(340, 9, 11, 14, 0, 17, -12, 7, 16),
496 	CABAC_ENTRY(341, 18, 11, 26, -12, 27, -21, 12, 14),
497 	CABAC_ENTRY(342, 21, 9, 31, -16, 37, -30, 18, 13),
498 	CABAC_ENTRY(343, 23, -2, 33, -25, 41, -40, 13, 11),
499 	CABAC_ENTRY(344, 32, -15, 33, -22, 42, -41, 13, 15),
500 	CABAC_ENTRY(345, 32, -15, 37, -28, 48, -47, 15, 16),
501 	CABAC_ENTRY(346, 34, -21, 39, -30, 39, -32, 12, 23),
502 	CABAC_ENTRY(347, 39, -23, 42, -30, 46, -40, 13, 23),
503 	CABAC_ENTRY(348, 42, -33, 47, -42, 52, -51, 15, 20),
504 	CABAC_ENTRY(349, 41, -31, 45, -36, 46, -41, 14, 26),
505 	CABAC_ENTRY(350, 46, -28, 49, -34, 52, -39, 14, 44),
506 	CABAC_ENTRY(351, 38, -12, 41, -17, 43, -19, 17, 40),
507 	CABAC_ENTRY(352, 21, 29, 32, 9, 32, 11, 17, 47),
508 	CABAC_ENTRY(353, 45, -24, 69, -71, 61, -55, 24, 17),
509 	CABAC_ENTRY(354, 53, -45, 63, -63, 56, -46, 21, 21),
510 	CABAC_ENTRY(355, 48, -26, 66, -64, 62, -50, 25, 22),
511 	CABAC_ENTRY(356, 65, -43, 77, -74, 81, -67, 31, 27),
512 	CABAC_ENTRY(357, 43, -19, 54, -39, 45, -20, 22, 29),
513 	CABAC_ENTRY(358, 39, -10, 52, -35, 35, -2, 19, 35),
514 	CABAC_ENTRY(359, 30, 9, 41, -10, 28, 15, 14, 50),
515 	CABAC_ENTRY(360, 18, 26, 36, 0, 34, 1, 10, 57),
516 	CABAC_ENTRY(361, 20, 27, 40, -1, 39, 1, 7, 63),
517 	CABAC_ENTRY(362, 0, 57, 30, 14, 30, 17, -2, 77),
518 	CABAC_ENTRY(363, -14, 82, 28, 26, 20, 38, -4, 82),
519 	CABAC_ENTRY(364, -5, 75, 23, 37, 18, 45, -3, 94),
520 	CABAC_ENTRY(365, -19, 97, 12, 55, 15, 54, 9, 69),
521 	CABAC_ENTRY(366, -35, 125, 11, 65, 0, 79, -12, 109),
522 	CABAC_ENTRY(367, 27, 0, 37, -33, 36, -16, 36, -35),
523 	CABAC_ENTRY(368, 28, 0, 39, -36, 37, -14, 36, -34),
524 	CABAC_ENTRY(369, 31, -4, 40, -37, 37, -17, 32, -26),
525 	CABAC_ENTRY(370, 27, 6, 38, -30, 32, 1, 37, -30),
526 	CABAC_ENTRY(371, 34, 8, 46, -33, 34, 15, 44, -32),
527 	CABAC_ENTRY(372, 30, 10, 42, -30, 29, 15, 34, -18),
528 	CABAC_ENTRY(373, 24, 22, 40, -24, 24, 25, 34, -15),
529 	CABAC_ENTRY(374, 33, 19, 49, -29, 34, 22, 40, -15),
530 	CABAC_ENTRY(375, 22, 32, 38, -12, 31, 16, 33, -7),
531 	CABAC_ENTRY(376, 26, 31, 40, -10, 35, 18, 35, -5),
532 	CABAC_ENTRY(377, 21, 41, 38, -3, 31, 28, 33, 0),
533 	CABAC_ENTRY(378, 26, 44, 46, -5, 33, 41, 38, 2),
534 	CABAC_ENTRY(379, 23, 47, 31, 20, 36, 28, 33, 13),
535 	CABAC_ENTRY(380, 16, 65, 29, 30, 27, 47, 23, 35),
536 	CABAC_ENTRY(381, 14, 71, 25, 44, 21, 62, 13, 58),
537 	CABAC_ENTRY(382, 8, 60, 12, 48, 18, 31, 29, -3),
538 	CABAC_ENTRY(383, 6, 63, 11, 49, 19, 26, 26, 0),
539 	CABAC_ENTRY(384, 17, 65, 26, 45, 36, 24, 22, 30),
540 	CABAC_ENTRY(385, 21, 24, 22, 22, 24, 23, 31, -7),
541 	CABAC_ENTRY(386, 23, 20, 23, 22, 27, 16, 35, -15),
542 	CABAC_ENTRY(387, 26, 23, 27, 21, 24, 30, 34, -3),
543 	CABAC_ENTRY(388, 27, 32, 33, 20, 31, 29, 34, 3),
544 	CABAC_ENTRY(389, 28, 23, 26, 28, 22, 41, 36, -1),
545 	CABAC_ENTRY(390, 28, 24, 30, 24, 22, 42, 34, 5),
546 	CABAC_ENTRY(391, 23, 40, 27, 34, 16, 60, 32, 11),
547 	CABAC_ENTRY(392, 24, 32, 18, 42, 15, 52, 35, 5),
548 	CABAC_ENTRY(393, 28, 29, 25, 39, 14, 60, 34, 12),
549 	CABAC_ENTRY(394, 23, 42, 18, 50, 3, 78, 39, 11),
550 	CABAC_ENTRY(395, 19, 57, 12, 70, -16, 123, 30, 29),
551 	CABAC_ENTRY(396, 22, 53, 21, 54, 21, 53, 34, 26),
552 	CABAC_ENTRY(397, 22, 61, 14, 71, 22, 56, 29, 39),
553 	CABAC_ENTRY(398, 11, 86, 11, 83, 25, 61, 19, 66),
554 
555 	/* Values of variables m and n for ctxIdx from 399 to 463 (not documented) */
556 	CABAC_ENTRY(399, 12, 40, 25, 32, 21, 33, 31, 21),
557 	CABAC_ENTRY(400, 11, 51, 21, 49, 19, 50, 31, 31),
558 	CABAC_ENTRY(401, 14, 59, 21, 54, 17, 61, 25, 50),
559 	CABAC_ENTRY(402, -4, 79, -5, 85, -3, 78, -17, 120),
560 	CABAC_ENTRY(403, -7, 71, -6, 81, -8, 74, -20, 112),
561 	CABAC_ENTRY(404, -5, 69, -10, 77, -9, 72, -18, 114),
562 	CABAC_ENTRY(405, -9, 70, -7, 81, -10, 72, -11, 85),
563 	CABAC_ENTRY(406, -8, 66, -17, 80, -18, 75, -15, 92),
564 	CABAC_ENTRY(407, -10, 68, -18, 73, -12, 71, -14, 89),
565 	CABAC_ENTRY(408, -19, 73, -4, 74, -11, 63, -26, 71),
566 	CABAC_ENTRY(409, -12, 69, -10, 83, -5, 70, -15, 81),
567 	CABAC_ENTRY(410, -16, 70, -9, 71, -17, 75, -14, 80),
568 	CABAC_ENTRY(411, -15, 67, -9, 67, -14, 72, 0, 68),
569 	CABAC_ENTRY(412, -20, 62, -1, 61, -16, 67, -14, 70),
570 	CABAC_ENTRY(413, -19, 70, -8, 66, -8, 53, -24, 56),
571 	CABAC_ENTRY(414, -16, 66, -14, 66, -14, 59, -23, 68),
572 	CABAC_ENTRY(415, -22, 65, 0, 59, -9, 52, -24, 50),
573 	CABAC_ENTRY(416, -20, 63, 2, 59, -11, 68, -11, 74),
574 	CABAC_ENTRY(417, 9, -2, 17, -10, 9, -2, 23, -13),
575 	CABAC_ENTRY(418, 26, -9, 32, -13, 30, -10, 26, -13),
576 	CABAC_ENTRY(419, 33, -9, 42, -9, 31, -4, 40, -15),
577 	CABAC_ENTRY(420, 39, -7, 49, -5, 33, -1, 49, -14),
578 	CABAC_ENTRY(421, 41, -2, 53, 0, 33, 7, 44, 3),
579 	CABAC_ENTRY(422, 45, 3, 64, 3, 31, 12, 45, 6),
580 	CABAC_ENTRY(423, 49, 9, 68, 10, 37, 23, 44, 34),
581 	CABAC_ENTRY(424, 45, 27, 66, 27, 31, 38, 33, 54),
582 	CABAC_ENTRY(425, 36, 59, 47, 57, 20, 64, 19, 82),
583 	CABAC_ENTRY(426, -6, 66, -5, 71, -9, 71, -3, 75),
584 	CABAC_ENTRY(427, -7, 35, 0, 24, -7, 37, -1, 23),
585 	CABAC_ENTRY(428, -7, 42, -1, 36, -8, 44, 1, 34),
586 	CABAC_ENTRY(429, -8, 45, -2, 42, -11, 49, 1, 43),
587 	CABAC_ENTRY(430, -5, 48, -2, 52, -10, 56, 0, 54),
588 	CABAC_ENTRY(431, -12, 56, -9, 57, -12, 59, -2, 55),
589 	CABAC_ENTRY(432, -6, 60, -6, 63, -8, 63, 0, 61),
590 	CABAC_ENTRY(433, -5, 62, -4, 65, -9, 67, 1, 64),
591 	CABAC_ENTRY(434, -8, 66, -4, 67, -6, 68, 0, 68),
592 	CABAC_ENTRY(435, -8, 76, -7, 82, -10, 79, -9, 92),
593 	CABAC_ENTRY(436, -5, 85, -3, 81, -3, 78, -14, 106),
594 	CABAC_ENTRY(437, -6, 81, -3, 76, -8, 74, -13, 97),
595 	CABAC_ENTRY(438, -10, 77, -7, 72, -9, 72, -15, 90),
596 	CABAC_ENTRY(439, -7, 81, -6, 78, -10, 72, -12, 90),
597 	CABAC_ENTRY(440, -17, 80, -12, 72, -18, 75, -18, 88),
598 	CABAC_ENTRY(441, -18, 73, -14, 68, -12, 71, -10, 73),
599 	CABAC_ENTRY(442, -4, 74, -3, 70, -11, 63, -9, 79),
600 	CABAC_ENTRY(443, -10, 83, -6, 76, -5, 70, -14, 86),
601 	CABAC_ENTRY(444, -9, 71, -5, 66, -17, 75, -10, 73),
602 	CABAC_ENTRY(445, -9, 67, -5, 62, -14, 72, -10, 70),
603 	CABAC_ENTRY(446, -1, 61, 0, 57, -16, 67, -10, 69),
604 	CABAC_ENTRY(447, -8, 66, -4, 61, -8, 53, -5, 66),
605 	CABAC_ENTRY(448, -14, 66, -9, 60, -14, 59, -9, 64),
606 	CABAC_ENTRY(449, 0, 59, 1, 54, -9, 52, -5, 58),
607 	CABAC_ENTRY(450, 2, 59, 2, 58, -11, 68, 2, 59),
608 	CABAC_ENTRY(451, 21, -13, 17, -10, 9, -2, 21, -10),
609 	CABAC_ENTRY(452, 33, -14, 32, -13, 30, -10, 24, -11),
610 	CABAC_ENTRY(453, 39, -7, 42, -9, 31, -4, 28, -8),
611 	CABAC_ENTRY(454, 46, -2, 49, -5, 33, -1, 28, -1),
612 	CABAC_ENTRY(455, 51, 2, 53, 0, 33, 7, 29, 3),
613 	CABAC_ENTRY(456, 60, 6, 64, 3, 31, 12, 29, 9),
614 	CABAC_ENTRY(457, 61, 17, 68, 10, 37, 23, 35, 20),
615 	CABAC_ENTRY(458, 55, 34, 66, 27, 31, 38, 29, 36),
616 	CABAC_ENTRY(459, 42, 62, 47, 57, 20, 64, 14, 67),
617 };
618 
set_ps_field(u32 * buf,struct rkvdec_ps_field field,u32 value)619 static void set_ps_field(u32 *buf, struct rkvdec_ps_field field, u32 value)
620 {
621 	u8 bit = field.offset % 32, word = field.offset / 32;
622 	u64 mask = GENMASK_ULL(bit + field.len - 1, bit);
623 	u64 val = ((u64)value << bit) & mask;
624 
625 	buf[word] &= ~mask;
626 	buf[word] |= val;
627 	if (bit + field.len > 32) {
628 		buf[word + 1] &= ~(mask >> 32);
629 		buf[word + 1] |= val >> 32;
630 	}
631 }
632 
assemble_hw_pps(struct rkvdec_ctx * ctx,struct rkvdec_h264_run * run)633 static void assemble_hw_pps(struct rkvdec_ctx *ctx,
634 			    struct rkvdec_h264_run *run)
635 {
636 	struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
637 	const struct v4l2_ctrl_h264_sps *sps = run->sps;
638 	const struct v4l2_ctrl_h264_pps *pps = run->pps;
639 	const struct v4l2_ctrl_h264_decode_params *dec_params = run->decode_params;
640 	const struct v4l2_h264_dpb_entry *dpb = dec_params->dpb;
641 	struct rkvdec_h264_priv_tbl *priv_tbl = h264_ctx->priv_tbl.cpu;
642 	struct rkvdec_sps_pps_packet *hw_ps;
643 	dma_addr_t scaling_list_address;
644 	u32 scaling_distance;
645 	u32 i;
646 
647 	/*
648 	 * HW read the SPS/PPS information from PPS packet index by PPS id.
649 	 * offset from the base can be calculated by PPS_id * 32 (size per PPS
650 	 * packet unit). so the driver copy SPS/PPS information to the exact PPS
651 	 * packet unit for HW accessing.
652 	 */
653 	hw_ps = &priv_tbl->param_set[pps->pic_parameter_set_id];
654 	memset(hw_ps, 0, sizeof(*hw_ps));
655 
656 #define WRITE_PPS(value, field) set_ps_field(hw_ps->info, field, value)
657 	/* write sps */
658 	WRITE_PPS(0xf, SEQ_PARAMETER_SET_ID);
659 	WRITE_PPS(0xff, PROFILE_IDC);
660 	WRITE_PPS(1, CONSTRAINT_SET3_FLAG);
661 	WRITE_PPS(sps->chroma_format_idc, CHROMA_FORMAT_IDC);
662 	WRITE_PPS(sps->bit_depth_luma_minus8, BIT_DEPTH_LUMA);
663 	WRITE_PPS(sps->bit_depth_chroma_minus8, BIT_DEPTH_CHROMA);
664 	WRITE_PPS(0, QPPRIME_Y_ZERO_TRANSFORM_BYPASS_FLAG);
665 	WRITE_PPS(sps->log2_max_frame_num_minus4, LOG2_MAX_FRAME_NUM_MINUS4);
666 	WRITE_PPS(sps->max_num_ref_frames, MAX_NUM_REF_FRAMES);
667 	WRITE_PPS(sps->pic_order_cnt_type, PIC_ORDER_CNT_TYPE);
668 	WRITE_PPS(sps->log2_max_pic_order_cnt_lsb_minus4,
669 		  LOG2_MAX_PIC_ORDER_CNT_LSB_MINUS4);
670 	WRITE_PPS(!!(sps->flags & V4L2_H264_SPS_FLAG_DELTA_PIC_ORDER_ALWAYS_ZERO),
671 		  DELTA_PIC_ORDER_ALWAYS_ZERO_FLAG);
672 
673 	/*
674 	 * Use the SPS values since they are already in macroblocks
675 	 * dimensions, height can be field height (halved) if
676 	 * V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY is not set and also it allows
677 	 * decoding smaller images into larger allocation which can be used
678 	 * to implementing SVC spatial layer support.
679 	 */
680 	WRITE_PPS(sps->pic_width_in_mbs_minus1 + 1, PIC_WIDTH_IN_MBS);
681 	WRITE_PPS(sps->pic_height_in_map_units_minus1 + 1, PIC_HEIGHT_IN_MBS);
682 
683 	WRITE_PPS(!!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY),
684 		  FRAME_MBS_ONLY_FLAG);
685 	WRITE_PPS(!!(sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD),
686 		  MB_ADAPTIVE_FRAME_FIELD_FLAG);
687 	WRITE_PPS(!!(sps->flags & V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE),
688 		  DIRECT_8X8_INFERENCE_FLAG);
689 
690 	/* write pps */
691 	WRITE_PPS(0xff, PIC_PARAMETER_SET_ID);
692 	WRITE_PPS(0x1f, PPS_SEQ_PARAMETER_SET_ID);
693 	WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE),
694 		  ENTROPY_CODING_MODE_FLAG);
695 	WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT),
696 		  BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT_FLAG);
697 	WRITE_PPS(pps->num_ref_idx_l0_default_active_minus1,
698 		  NUM_REF_IDX_L_DEFAULT_ACTIVE_MINUS1(0));
699 	WRITE_PPS(pps->num_ref_idx_l1_default_active_minus1,
700 		  NUM_REF_IDX_L_DEFAULT_ACTIVE_MINUS1(1));
701 	WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_WEIGHTED_PRED),
702 		  WEIGHTED_PRED_FLAG);
703 	WRITE_PPS(pps->weighted_bipred_idc, WEIGHTED_BIPRED_IDC);
704 	WRITE_PPS(pps->pic_init_qp_minus26, PIC_INIT_QP_MINUS26);
705 	WRITE_PPS(pps->pic_init_qs_minus26, PIC_INIT_QS_MINUS26);
706 	WRITE_PPS(pps->chroma_qp_index_offset, CHROMA_QP_INDEX_OFFSET);
707 	WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT),
708 		  DEBLOCKING_FILTER_CONTROL_PRESENT_FLAG);
709 	WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED),
710 		  CONSTRAINED_INTRA_PRED_FLAG);
711 	WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT),
712 		  REDUNDANT_PIC_CNT_PRESENT);
713 	WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE),
714 		  TRANSFORM_8X8_MODE_FLAG);
715 	WRITE_PPS(pps->second_chroma_qp_index_offset,
716 		  SECOND_CHROMA_QP_INDEX_OFFSET);
717 
718 	WRITE_PPS(!!(pps->flags & V4L2_H264_PPS_FLAG_SCALING_MATRIX_PRESENT),
719 		  SCALING_LIST_ENABLE_FLAG);
720 	/* To be on the safe side, program the scaling matrix address */
721 	scaling_distance = offsetof(struct rkvdec_h264_priv_tbl, scaling_list);
722 	scaling_list_address = h264_ctx->priv_tbl.dma + scaling_distance;
723 	WRITE_PPS(scaling_list_address, SCALING_LIST_ADDRESS);
724 
725 	for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
726 		u32 is_longterm = 0;
727 
728 		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM)
729 			is_longterm = 1;
730 
731 		WRITE_PPS(is_longterm, IS_LONG_TERM(i));
732 	}
733 }
734 
lookup_ref_buf_idx(struct rkvdec_ctx * ctx,struct rkvdec_h264_run * run)735 static void lookup_ref_buf_idx(struct rkvdec_ctx *ctx,
736 			       struct rkvdec_h264_run *run)
737 {
738 	const struct v4l2_ctrl_h264_decode_params *dec_params = run->decode_params;
739 	u32 i;
740 
741 	for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
742 		struct v4l2_m2m_ctx *m2m_ctx = ctx->fh.m2m_ctx;
743 		const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb;
744 		struct vb2_queue *cap_q = &m2m_ctx->cap_q_ctx.q;
745 		struct vb2_buffer *buf = NULL;
746 
747 		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE) {
748 			buf = vb2_find_buffer(cap_q, dpb[i].reference_ts);
749 			if (!buf)
750 				pr_debug("No buffer for reference_ts %llu",
751 					 dpb[i].reference_ts);
752 		}
753 
754 		run->ref_buf[i] = buf;
755 	}
756 }
757 
assemble_hw_rps(struct rkvdec_ctx * ctx,struct v4l2_h264_reflist_builder * builder,struct rkvdec_h264_run * run)758 static void assemble_hw_rps(struct rkvdec_ctx *ctx,
759 			    struct v4l2_h264_reflist_builder *builder,
760 			    struct rkvdec_h264_run *run)
761 {
762 	const struct v4l2_ctrl_h264_decode_params *dec_params = run->decode_params;
763 	const struct v4l2_h264_dpb_entry *dpb = dec_params->dpb;
764 	struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
765 	struct rkvdec_h264_priv_tbl *priv_tbl = h264_ctx->priv_tbl.cpu;
766 
767 	u32 *hw_rps = priv_tbl->rps;
768 	u32 i, j;
769 	u16 *p = (u16 *)hw_rps;
770 
771 	memset(hw_rps, 0, sizeof(priv_tbl->rps));
772 
773 	/*
774 	 * Assign an invalid pic_num if DPB entry at that position is inactive.
775 	 * If we assign 0 in that position hardware will treat that as a real
776 	 * reference picture with pic_num 0, triggering output picture
777 	 * corruption.
778 	 */
779 	for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
780 		if (!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE))
781 			continue;
782 
783 		p[i] = builder->refs[i].frame_num;
784 	}
785 
786 	for (j = 0; j < RKVDEC_NUM_REFLIST; j++) {
787 		for (i = 0; i < builder->num_valid; i++) {
788 			struct v4l2_h264_reference *ref;
789 			bool dpb_valid;
790 			bool bottom;
791 
792 			switch (j) {
793 			case 0:
794 				ref = &h264_ctx->reflists.p[i];
795 				break;
796 			case 1:
797 				ref = &h264_ctx->reflists.b0[i];
798 				break;
799 			case 2:
800 				ref = &h264_ctx->reflists.b1[i];
801 				break;
802 			}
803 
804 			if (WARN_ON(ref->index >= ARRAY_SIZE(dec_params->dpb)))
805 				continue;
806 
807 			dpb_valid = run->ref_buf[ref->index] != NULL;
808 			bottom = ref->fields == V4L2_H264_BOTTOM_FIELD_REF;
809 
810 			set_ps_field(hw_rps, DPB_INFO(i, j),
811 				     ref->index | dpb_valid << 4);
812 			set_ps_field(hw_rps, BOTTOM_FLAG(i, j), bottom);
813 		}
814 	}
815 }
816 
assemble_hw_scaling_list(struct rkvdec_ctx * ctx,struct rkvdec_h264_run * run)817 static void assemble_hw_scaling_list(struct rkvdec_ctx *ctx,
818 				     struct rkvdec_h264_run *run)
819 {
820 	const struct v4l2_ctrl_h264_scaling_matrix *scaling = run->scaling_matrix;
821 	const struct v4l2_ctrl_h264_pps *pps = run->pps;
822 	struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
823 	struct rkvdec_h264_priv_tbl *tbl = h264_ctx->priv_tbl.cpu;
824 
825 	if (!(pps->flags & V4L2_H264_PPS_FLAG_SCALING_MATRIX_PRESENT))
826 		return;
827 
828 	BUILD_BUG_ON(sizeof(tbl->scaling_list.scaling_list_4x4) !=
829 		     sizeof(scaling->scaling_list_4x4));
830 	BUILD_BUG_ON(sizeof(tbl->scaling_list.scaling_list_8x8) !=
831 		     sizeof(scaling->scaling_list_8x8));
832 
833 	memcpy(tbl->scaling_list.scaling_list_4x4,
834 	       scaling->scaling_list_4x4,
835 	       sizeof(scaling->scaling_list_4x4));
836 
837 	memcpy(tbl->scaling_list.scaling_list_8x8,
838 	       scaling->scaling_list_8x8,
839 	       sizeof(scaling->scaling_list_8x8));
840 }
841 
842 /*
843  * dpb poc related registers table
844  */
845 static const u32 poc_reg_tbl_top_field[16] = {
846 	RKVDEC_REG_H264_POC_REFER0(0),
847 	RKVDEC_REG_H264_POC_REFER0(2),
848 	RKVDEC_REG_H264_POC_REFER0(4),
849 	RKVDEC_REG_H264_POC_REFER0(6),
850 	RKVDEC_REG_H264_POC_REFER0(8),
851 	RKVDEC_REG_H264_POC_REFER0(10),
852 	RKVDEC_REG_H264_POC_REFER0(12),
853 	RKVDEC_REG_H264_POC_REFER0(14),
854 	RKVDEC_REG_H264_POC_REFER1(1),
855 	RKVDEC_REG_H264_POC_REFER1(3),
856 	RKVDEC_REG_H264_POC_REFER1(5),
857 	RKVDEC_REG_H264_POC_REFER1(7),
858 	RKVDEC_REG_H264_POC_REFER1(9),
859 	RKVDEC_REG_H264_POC_REFER1(11),
860 	RKVDEC_REG_H264_POC_REFER1(13),
861 	RKVDEC_REG_H264_POC_REFER2(0)
862 };
863 
864 static const u32 poc_reg_tbl_bottom_field[16] = {
865 	RKVDEC_REG_H264_POC_REFER0(1),
866 	RKVDEC_REG_H264_POC_REFER0(3),
867 	RKVDEC_REG_H264_POC_REFER0(5),
868 	RKVDEC_REG_H264_POC_REFER0(7),
869 	RKVDEC_REG_H264_POC_REFER0(9),
870 	RKVDEC_REG_H264_POC_REFER0(11),
871 	RKVDEC_REG_H264_POC_REFER0(13),
872 	RKVDEC_REG_H264_POC_REFER1(0),
873 	RKVDEC_REG_H264_POC_REFER1(2),
874 	RKVDEC_REG_H264_POC_REFER1(4),
875 	RKVDEC_REG_H264_POC_REFER1(6),
876 	RKVDEC_REG_H264_POC_REFER1(8),
877 	RKVDEC_REG_H264_POC_REFER1(10),
878 	RKVDEC_REG_H264_POC_REFER1(12),
879 	RKVDEC_REG_H264_POC_REFER1(14),
880 	RKVDEC_REG_H264_POC_REFER2(1)
881 };
882 
config_registers(struct rkvdec_ctx * ctx,struct rkvdec_h264_run * run)883 static void config_registers(struct rkvdec_ctx *ctx,
884 			     struct rkvdec_h264_run *run)
885 {
886 	struct rkvdec_dev *rkvdec = ctx->dev;
887 	const struct v4l2_ctrl_h264_decode_params *dec_params = run->decode_params;
888 	const struct v4l2_ctrl_h264_sps *sps = run->sps;
889 	const struct v4l2_h264_dpb_entry *dpb = dec_params->dpb;
890 	struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
891 	dma_addr_t priv_start_addr = h264_ctx->priv_tbl.dma;
892 	const struct v4l2_pix_format_mplane *dst_fmt;
893 	struct vb2_v4l2_buffer *src_buf = run->base.bufs.src;
894 	struct vb2_v4l2_buffer *dst_buf = run->base.bufs.dst;
895 	const struct v4l2_format *f;
896 	dma_addr_t rlc_addr;
897 	dma_addr_t refer_addr;
898 	u32 rlc_len;
899 	u32 hor_virstride = 0;
900 	u32 ver_virstride = 0;
901 	u32 y_virstride = 0;
902 	u32 yuv_virstride = 0;
903 	u32 offset;
904 	dma_addr_t dst_addr;
905 	u32 reg, i;
906 
907 	reg = RKVDEC_MODE(RKVDEC_MODE_H264);
908 	writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_SYSCTRL);
909 
910 	f = &ctx->decoded_fmt;
911 	dst_fmt = &f->fmt.pix_mp;
912 	hor_virstride = (sps->bit_depth_luma_minus8 + 8) * dst_fmt->width / 8;
913 	ver_virstride = round_up(dst_fmt->height, 16);
914 	y_virstride = hor_virstride * ver_virstride;
915 
916 	if (sps->chroma_format_idc == 0)
917 		yuv_virstride = y_virstride;
918 	else if (sps->chroma_format_idc == 1)
919 		yuv_virstride += y_virstride + y_virstride / 2;
920 	else if (sps->chroma_format_idc == 2)
921 		yuv_virstride += 2 * y_virstride;
922 
923 	reg = RKVDEC_Y_HOR_VIRSTRIDE(hor_virstride / 16) |
924 	      RKVDEC_UV_HOR_VIRSTRIDE(hor_virstride / 16) |
925 	      RKVDEC_SLICE_NUM_HIGHBIT |
926 	      RKVDEC_SLICE_NUM_LOWBITS(0x7ff);
927 	writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_PICPAR);
928 
929 	/* config rlc base address */
930 	rlc_addr = vb2_dma_contig_plane_dma_addr(&src_buf->vb2_buf, 0);
931 	writel_relaxed(rlc_addr, rkvdec->regs + RKVDEC_REG_STRM_RLC_BASE);
932 	writel_relaxed(rlc_addr, rkvdec->regs + RKVDEC_REG_RLCWRITE_BASE);
933 
934 	rlc_len = vb2_get_plane_payload(&src_buf->vb2_buf, 0);
935 	reg = RKVDEC_STRM_LEN(rlc_len);
936 	writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_STRM_LEN);
937 
938 	/* config cabac table */
939 	offset = offsetof(struct rkvdec_h264_priv_tbl, cabac_table);
940 	writel_relaxed(priv_start_addr + offset,
941 		       rkvdec->regs + RKVDEC_REG_CABACTBL_PROB_BASE);
942 
943 	/* config output base address */
944 	dst_addr = vb2_dma_contig_plane_dma_addr(&dst_buf->vb2_buf, 0);
945 	writel_relaxed(dst_addr, rkvdec->regs + RKVDEC_REG_DECOUT_BASE);
946 
947 	reg = RKVDEC_Y_VIRSTRIDE(y_virstride / 16);
948 	writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_Y_VIRSTRIDE);
949 
950 	reg = RKVDEC_YUV_VIRSTRIDE(yuv_virstride / 16);
951 	writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_YUV_VIRSTRIDE);
952 
953 	/* config ref pic address & poc */
954 	for (i = 0; i < ARRAY_SIZE(dec_params->dpb); i++) {
955 		struct vb2_buffer *vb_buf = run->ref_buf[i];
956 
957 		/*
958 		 * If a DPB entry is unused or invalid, address of current destination
959 		 * buffer is returned.
960 		 */
961 		if (!vb_buf)
962 			vb_buf = &dst_buf->vb2_buf;
963 		refer_addr = vb2_dma_contig_plane_dma_addr(vb_buf, 0);
964 
965 		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE)
966 			refer_addr |= RKVDEC_COLMV_USED_FLAG_REF;
967 		if (dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_FIELD)
968 			refer_addr |= RKVDEC_FIELD_REF;
969 
970 		if (dpb[i].fields & V4L2_H264_TOP_FIELD_REF)
971 			refer_addr |= RKVDEC_TOPFIELD_USED_REF;
972 		if (dpb[i].fields & V4L2_H264_BOTTOM_FIELD_REF)
973 			refer_addr |= RKVDEC_BOTFIELD_USED_REF;
974 
975 		writel_relaxed(dpb[i].top_field_order_cnt,
976 			       rkvdec->regs +  poc_reg_tbl_top_field[i]);
977 		writel_relaxed(dpb[i].bottom_field_order_cnt,
978 			       rkvdec->regs + poc_reg_tbl_bottom_field[i]);
979 
980 		if (i < V4L2_H264_NUM_DPB_ENTRIES - 1)
981 			writel_relaxed(refer_addr,
982 				       rkvdec->regs + RKVDEC_REG_H264_BASE_REFER(i));
983 		else
984 			writel_relaxed(refer_addr,
985 				       rkvdec->regs + RKVDEC_REG_H264_BASE_REFER15);
986 	}
987 
988 	reg = RKVDEC_CUR_POC(dec_params->top_field_order_cnt);
989 	writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_CUR_POC0);
990 
991 	reg = RKVDEC_CUR_POC(dec_params->bottom_field_order_cnt);
992 	writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_CUR_POC1);
993 
994 	/* config hw pps address */
995 	offset = offsetof(struct rkvdec_h264_priv_tbl, param_set);
996 	writel_relaxed(priv_start_addr + offset,
997 		       rkvdec->regs + RKVDEC_REG_PPS_BASE);
998 
999 	/* config hw rps address */
1000 	offset = offsetof(struct rkvdec_h264_priv_tbl, rps);
1001 	writel_relaxed(priv_start_addr + offset,
1002 		       rkvdec->regs + RKVDEC_REG_RPS_BASE);
1003 
1004 	reg = RKVDEC_AXI_DDR_RDATA(0);
1005 	writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_AXI_DDR_RDATA);
1006 
1007 	reg = RKVDEC_AXI_DDR_WDATA(0);
1008 	writel_relaxed(reg, rkvdec->regs + RKVDEC_REG_AXI_DDR_WDATA);
1009 
1010 	offset = offsetof(struct rkvdec_h264_priv_tbl, err_info);
1011 	writel_relaxed(priv_start_addr + offset,
1012 		       rkvdec->regs + RKVDEC_REG_H264_ERRINFO_BASE);
1013 }
1014 
1015 #define RKVDEC_H264_MAX_DEPTH_IN_BYTES		2
1016 
rkvdec_h264_adjust_fmt(struct rkvdec_ctx * ctx,struct v4l2_format * f)1017 static int rkvdec_h264_adjust_fmt(struct rkvdec_ctx *ctx,
1018 				  struct v4l2_format *f)
1019 {
1020 	struct v4l2_pix_format_mplane *fmt = &f->fmt.pix_mp;
1021 
1022 	fmt->num_planes = 1;
1023 	if (!fmt->plane_fmt[0].sizeimage)
1024 		fmt->plane_fmt[0].sizeimage = fmt->width * fmt->height *
1025 					      RKVDEC_H264_MAX_DEPTH_IN_BYTES;
1026 	return 0;
1027 }
1028 
rkvdec_h264_validate_sps(struct rkvdec_ctx * ctx,const struct v4l2_ctrl_h264_sps * sps)1029 static int rkvdec_h264_validate_sps(struct rkvdec_ctx *ctx,
1030 				    const struct v4l2_ctrl_h264_sps *sps)
1031 {
1032 	unsigned int width, height;
1033 
1034 	/*
1035 	 * TODO: The hardware supports 10-bit and 4:2:2 profiles,
1036 	 * but it's currently broken in the driver.
1037 	 * Reject them for now, until it's fixed.
1038 	 */
1039 	if (sps->chroma_format_idc > 1)
1040 		/* Only 4:0:0 and 4:2:0 are supported */
1041 		return -EINVAL;
1042 	if (sps->bit_depth_luma_minus8 != sps->bit_depth_chroma_minus8)
1043 		/* Luma and chroma bit depth mismatch */
1044 		return -EINVAL;
1045 	if (sps->bit_depth_luma_minus8 != 0)
1046 		/* Only 8-bit is supported */
1047 		return -EINVAL;
1048 
1049 	width = (sps->pic_width_in_mbs_minus1 + 1) * 16;
1050 	height = (sps->pic_height_in_map_units_minus1 + 1) * 16;
1051 
1052 	/*
1053 	 * When frame_mbs_only_flag is not set, this is field height,
1054 	 * which is half the final height (see (7-18) in the
1055 	 * specification)
1056 	 */
1057 	if (!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY))
1058 		height *= 2;
1059 
1060 	if (width > ctx->coded_fmt.fmt.pix_mp.width ||
1061 	    height > ctx->coded_fmt.fmt.pix_mp.height)
1062 		return -EINVAL;
1063 
1064 	return 0;
1065 }
1066 
rkvdec_h264_start(struct rkvdec_ctx * ctx)1067 static int rkvdec_h264_start(struct rkvdec_ctx *ctx)
1068 {
1069 	struct rkvdec_dev *rkvdec = ctx->dev;
1070 	struct rkvdec_h264_priv_tbl *priv_tbl;
1071 	struct rkvdec_h264_ctx *h264_ctx;
1072 	struct v4l2_ctrl *ctrl;
1073 	int ret;
1074 
1075 	ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
1076 			      V4L2_CID_STATELESS_H264_SPS);
1077 	if (!ctrl)
1078 		return -EINVAL;
1079 
1080 	ret = rkvdec_h264_validate_sps(ctx, ctrl->p_new.p_h264_sps);
1081 	if (ret)
1082 		return ret;
1083 
1084 	h264_ctx = kzalloc(sizeof(*h264_ctx), GFP_KERNEL);
1085 	if (!h264_ctx)
1086 		return -ENOMEM;
1087 
1088 	priv_tbl = dma_alloc_coherent(rkvdec->dev, sizeof(*priv_tbl),
1089 				      &h264_ctx->priv_tbl.dma, GFP_KERNEL);
1090 	if (!priv_tbl) {
1091 		ret = -ENOMEM;
1092 		goto err_free_ctx;
1093 	}
1094 
1095 	h264_ctx->priv_tbl.size = sizeof(*priv_tbl);
1096 	h264_ctx->priv_tbl.cpu = priv_tbl;
1097 	memcpy(priv_tbl->cabac_table, rkvdec_h264_cabac_table,
1098 	       sizeof(rkvdec_h264_cabac_table));
1099 
1100 	ctx->priv = h264_ctx;
1101 	return 0;
1102 
1103 err_free_ctx:
1104 	kfree(h264_ctx);
1105 	return ret;
1106 }
1107 
rkvdec_h264_stop(struct rkvdec_ctx * ctx)1108 static void rkvdec_h264_stop(struct rkvdec_ctx *ctx)
1109 {
1110 	struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
1111 	struct rkvdec_dev *rkvdec = ctx->dev;
1112 
1113 	dma_free_coherent(rkvdec->dev, h264_ctx->priv_tbl.size,
1114 			  h264_ctx->priv_tbl.cpu, h264_ctx->priv_tbl.dma);
1115 	kfree(h264_ctx);
1116 }
1117 
rkvdec_h264_run_preamble(struct rkvdec_ctx * ctx,struct rkvdec_h264_run * run)1118 static void rkvdec_h264_run_preamble(struct rkvdec_ctx *ctx,
1119 				     struct rkvdec_h264_run *run)
1120 {
1121 	struct v4l2_ctrl *ctrl;
1122 
1123 	ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
1124 			      V4L2_CID_STATELESS_H264_DECODE_PARAMS);
1125 	run->decode_params = ctrl ? ctrl->p_cur.p : NULL;
1126 	ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
1127 			      V4L2_CID_STATELESS_H264_SPS);
1128 	run->sps = ctrl ? ctrl->p_cur.p : NULL;
1129 	ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
1130 			      V4L2_CID_STATELESS_H264_PPS);
1131 	run->pps = ctrl ? ctrl->p_cur.p : NULL;
1132 	ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
1133 			      V4L2_CID_STATELESS_H264_SCALING_MATRIX);
1134 	run->scaling_matrix = ctrl ? ctrl->p_cur.p : NULL;
1135 
1136 	rkvdec_run_preamble(ctx, &run->base);
1137 }
1138 
rkvdec_h264_run(struct rkvdec_ctx * ctx)1139 static int rkvdec_h264_run(struct rkvdec_ctx *ctx)
1140 {
1141 	struct v4l2_h264_reflist_builder reflist_builder;
1142 	struct rkvdec_dev *rkvdec = ctx->dev;
1143 	struct rkvdec_h264_ctx *h264_ctx = ctx->priv;
1144 	struct rkvdec_h264_run run;
1145 
1146 	rkvdec_h264_run_preamble(ctx, &run);
1147 
1148 	/* Build the P/B{0,1} ref lists. */
1149 	v4l2_h264_init_reflist_builder(&reflist_builder, run.decode_params,
1150 				       run.sps, run.decode_params->dpb);
1151 	v4l2_h264_build_p_ref_list(&reflist_builder, h264_ctx->reflists.p);
1152 	v4l2_h264_build_b_ref_lists(&reflist_builder, h264_ctx->reflists.b0,
1153 				    h264_ctx->reflists.b1);
1154 
1155 	assemble_hw_scaling_list(ctx, &run);
1156 	assemble_hw_pps(ctx, &run);
1157 	lookup_ref_buf_idx(ctx, &run);
1158 	assemble_hw_rps(ctx, &reflist_builder, &run);
1159 	config_registers(ctx, &run);
1160 
1161 	rkvdec_run_postamble(ctx, &run.base);
1162 
1163 	schedule_delayed_work(&rkvdec->watchdog_work, msecs_to_jiffies(2000));
1164 
1165 	writel(0, rkvdec->regs + RKVDEC_REG_STRMD_ERR_EN);
1166 	writel(0, rkvdec->regs + RKVDEC_REG_H264_ERR_E);
1167 	writel(1, rkvdec->regs + RKVDEC_REG_PREF_LUMA_CACHE_COMMAND);
1168 	writel(1, rkvdec->regs + RKVDEC_REG_PREF_CHR_CACHE_COMMAND);
1169 
1170 	/* Start decoding! */
1171 	writel(RKVDEC_INTERRUPT_DEC_E | RKVDEC_CONFIG_DEC_CLK_GATE_E |
1172 	       RKVDEC_TIMEOUT_E | RKVDEC_BUF_EMPTY_E,
1173 	       rkvdec->regs + RKVDEC_REG_INTERRUPT);
1174 
1175 	return 0;
1176 }
1177 
rkvdec_h264_try_ctrl(struct rkvdec_ctx * ctx,struct v4l2_ctrl * ctrl)1178 static int rkvdec_h264_try_ctrl(struct rkvdec_ctx *ctx, struct v4l2_ctrl *ctrl)
1179 {
1180 	if (ctrl->id == V4L2_CID_STATELESS_H264_SPS)
1181 		return rkvdec_h264_validate_sps(ctx, ctrl->p_new.p_h264_sps);
1182 
1183 	return 0;
1184 }
1185 
1186 const struct rkvdec_coded_fmt_ops rkvdec_h264_fmt_ops = {
1187 	.adjust_fmt = rkvdec_h264_adjust_fmt,
1188 	.start = rkvdec_h264_start,
1189 	.stop = rkvdec_h264_stop,
1190 	.run = rkvdec_h264_run,
1191 	.try_ctrl = rkvdec_h264_try_ctrl,
1192 };
1193