xref: /openbmc/linux/include/media/v4l2-vp9.h (revision bbaf1ff0)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Helper functions for vp9 codecs.
4  *
5  * Copyright (c) 2021 Collabora, Ltd.
6  *
7  * Author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
8  */
9 
10 #ifndef _MEDIA_V4L2_VP9_H
11 #define _MEDIA_V4L2_VP9_H
12 
13 #include <media/v4l2-ctrls.h>
14 
15 /**
16  * struct v4l2_vp9_frame_mv_context - motion vector-related probabilities
17  *
18  * @joint: motion vector joint probabilities.
19  * @sign: motion vector sign probabilities.
20  * @classes: motion vector class probabilities.
21  * @class0_bit: motion vector class0 bit probabilities.
22  * @bits: motion vector bits probabilities.
23  * @class0_fr: motion vector class0 fractional bit probabilities.
24  * @fr: motion vector fractional bit probabilities.
25  * @class0_hp: motion vector class0 high precision fractional bit probabilities.
26  * @hp: motion vector high precision fractional bit probabilities.
27  *
28  * A member of v4l2_vp9_frame_context.
29  */
30 struct v4l2_vp9_frame_mv_context {
31 	u8 joint[3];
32 	u8 sign[2];
33 	u8 classes[2][10];
34 	u8 class0_bit[2];
35 	u8 bits[2][10];
36 	u8 class0_fr[2][2][3];
37 	u8 fr[2][3];
38 	u8 class0_hp[2];
39 	u8 hp[2];
40 };
41 
42 /**
43  * struct v4l2_vp9_frame_context - frame probabilities, including motion-vector related
44  *
45  * @tx8: TX 8x8 probabilities.
46  * @tx16: TX 16x16 probabilities.
47  * @tx32: TX 32x32 probabilities.
48  * @coef: coefficient probabilities.
49  * @skip: skip probabilities.
50  * @inter_mode: inter mode probabilities.
51  * @interp_filter: interpolation filter probabilities.
52  * @is_inter: is inter-block probabilities.
53  * @comp_mode: compound prediction mode probabilities.
54  * @single_ref: single ref probabilities.
55  * @comp_ref: compound ref probabilities.
56  * @y_mode: Y prediction mode probabilities.
57  * @uv_mode: UV prediction mode probabilities.
58  * @partition: partition probabilities.
59  * @mv: motion vector probabilities.
60  *
61  * Drivers which need to keep track of frame context(s) can use this struct.
62  * The members correspond to probability tables, which are specified only implicitly in the
63  * vp9 spec. Section 10.5 "Default probability tables" contains all the types of involved
64  * tables, i.e. the actual tables are of the same kind, and when they are reset (which is
65  * mandated by the spec sometimes) they are overwritten with values from the default tables.
66  */
67 struct v4l2_vp9_frame_context {
68 	u8 tx8[2][1];
69 	u8 tx16[2][2];
70 	u8 tx32[2][3];
71 	u8 coef[4][2][2][6][6][3];
72 	u8 skip[3];
73 	u8 inter_mode[7][3];
74 	u8 interp_filter[4][2];
75 	u8 is_inter[4];
76 	u8 comp_mode[5];
77 	u8 single_ref[5][2];
78 	u8 comp_ref[5];
79 	u8 y_mode[4][9];
80 	u8 uv_mode[10][9];
81 	u8 partition[16][3];
82 
83 	struct v4l2_vp9_frame_mv_context mv;
84 };
85 
86 /**
87  * struct v4l2_vp9_frame_symbol_counts - pointers to arrays of symbol counts
88  *
89  * @partition: partition counts.
90  * @skip: skip counts.
91  * @intra_inter: is inter-block counts.
92  * @tx32p: TX32 counts.
93  * @tx16p: TX16 counts.
94  * @tx8p: TX8 counts.
95  * @y_mode: Y prediction mode counts.
96  * @uv_mode: UV prediction mode counts.
97  * @comp: compound prediction mode counts.
98  * @comp_ref: compound ref counts.
99  * @single_ref: single ref counts.
100  * @mv_mode: inter mode counts.
101  * @filter: interpolation filter counts.
102  * @mv_joint: motion vector joint counts.
103  * @sign: motion vector sign counts.
104  * @classes: motion vector class counts.
105  * @class0: motion vector class0 bit counts.
106  * @bits: motion vector bits counts.
107  * @class0_fp: motion vector class0 fractional bit counts.
108  * @fp: motion vector fractional bit counts.
109  * @class0_hp: motion vector class0 high precision fractional bit counts.
110  * @hp: motion vector high precision fractional bit counts.
111  * @coeff: coefficient counts.
112  * @eob: eob counts
113  *
114  * The fields correspond to what is specified in section 8.3 "Clear counts process" of the spec.
115  * Different pieces of hardware can report the counts in different order, so we cannot rely on
116  * simply overlaying a struct on a relevant block of memory. Instead we provide pointers to
117  * arrays or array of pointers to arrays in case of coeff, or array of pointers for eob.
118  */
119 struct v4l2_vp9_frame_symbol_counts {
120 	u32 (*partition)[16][4];
121 	u32 (*skip)[3][2];
122 	u32 (*intra_inter)[4][2];
123 	u32 (*tx32p)[2][4];
124 	u32 (*tx16p)[2][4];
125 	u32 (*tx8p)[2][2];
126 	u32 (*y_mode)[4][10];
127 	u32 (*uv_mode)[10][10];
128 	u32 (*comp)[5][2];
129 	u32 (*comp_ref)[5][2];
130 	u32 (*single_ref)[5][2][2];
131 	u32 (*mv_mode)[7][4];
132 	u32 (*filter)[4][3];
133 	u32 (*mv_joint)[4];
134 	u32 (*sign)[2][2];
135 	u32 (*classes)[2][11];
136 	u32 (*class0)[2][2];
137 	u32 (*bits)[2][10][2];
138 	u32 (*class0_fp)[2][2][4];
139 	u32 (*fp)[2][4];
140 	u32 (*class0_hp)[2][2];
141 	u32 (*hp)[2][2];
142 	u32 (*coeff[4][2][2][6][6])[3];
143 	u32 *eob[4][2][2][6][6][2];
144 };
145 
146 extern const u8 v4l2_vp9_kf_y_mode_prob[10][10][9]; /* Section 10.4 of the spec */
147 extern const u8 v4l2_vp9_kf_partition_probs[16][3]; /* Section 10.4 of the spec */
148 extern const u8 v4l2_vp9_kf_uv_mode_prob[10][9]; /* Section 10.4 of the spec */
149 extern const struct v4l2_vp9_frame_context v4l2_vp9_default_probs; /* Section 10.5 of the spec */
150 
151 /**
152  * v4l2_vp9_fw_update_probs() - Perform forward update of vp9 probabilities
153  *
154  * @probs: current probabilities values
155  * @deltas: delta values from compressed header
156  * @dec_params: vp9 frame decoding parameters
157  *
158  * This function performs forward updates of probabilities for the vp9 boolean decoder.
159  * The frame header can contain a directive to update the probabilities (deltas), if so, then
160  * the deltas are provided in the header, too. The userspace parses those and passes the said
161  * deltas struct to the kernel.
162  */
163 void v4l2_vp9_fw_update_probs(struct v4l2_vp9_frame_context *probs,
164 			      const struct v4l2_ctrl_vp9_compressed_hdr *deltas,
165 			      const struct v4l2_ctrl_vp9_frame *dec_params);
166 
167 /**
168  * v4l2_vp9_reset_frame_ctx() - Reset appropriate frame context
169  *
170  * @dec_params: vp9 frame decoding parameters
171  * @frame_context: array of the 4 frame contexts
172  *
173  * This function resets appropriate frame contexts, based on what's in dec_params.
174  *
175  * Returns the frame context index after the update, which might be reset to zero if
176  * mandated by the spec.
177  */
178 u8 v4l2_vp9_reset_frame_ctx(const struct v4l2_ctrl_vp9_frame *dec_params,
179 			    struct v4l2_vp9_frame_context *frame_context);
180 
181 /**
182  * v4l2_vp9_adapt_coef_probs() - Perform backward update of vp9 coefficients probabilities
183  *
184  * @probs: current probabilities values
185  * @counts: values of symbol counts after the current frame has been decoded
186  * @use_128: flag to request that 128 is used as update factor if true, otherwise 112 is used
187  * @frame_is_intra: flag indicating that FrameIsIntra is true
188  *
189  * This function performs backward updates of coefficients probabilities for the vp9 boolean
190  * decoder. After a frame has been decoded the counts of how many times a given symbol has
191  * occurred are known and are used to update the probability of each symbol.
192  */
193 void v4l2_vp9_adapt_coef_probs(struct v4l2_vp9_frame_context *probs,
194 			       struct v4l2_vp9_frame_symbol_counts *counts,
195 			       bool use_128,
196 			       bool frame_is_intra);
197 
198 /**
199  * v4l2_vp9_adapt_noncoef_probs() - Perform backward update of vp9 non-coefficients probabilities
200  *
201  * @probs: current probabilities values
202  * @counts: values of symbol counts after the current frame has been decoded
203  * @reference_mode: specifies the type of inter prediction to be used. See
204  *	&v4l2_vp9_reference_mode for more details
205  * @interpolation_filter: specifies the filter selection used for performing inter prediction.
206  *	See &v4l2_vp9_interpolation_filter for more details
207  * @tx_mode: specifies the TX mode. See &v4l2_vp9_tx_mode for more details
208  * @flags: combination of V4L2_VP9_FRAME_FLAG_* flags
209  *
210  * This function performs backward updates of non-coefficients probabilities for the vp9 boolean
211  * decoder. After a frame has been decoded the counts of how many times a given symbol has
212  * occurred are known and are used to update the probability of each symbol.
213  */
214 void v4l2_vp9_adapt_noncoef_probs(struct v4l2_vp9_frame_context *probs,
215 				  struct v4l2_vp9_frame_symbol_counts *counts,
216 				  u8 reference_mode, u8 interpolation_filter, u8 tx_mode,
217 				  u32 flags);
218 
219 /**
220  * v4l2_vp9_seg_feat_enabled() - Check if a segmentation feature is enabled
221  *
222  * @feature_enabled: array of 8-bit flags (for all segments)
223  * @feature: id of the feature to check
224  * @segid: id of the segment to look up
225  *
226  * This function returns true if a given feature is active in a given segment.
227  */
228 bool
229 v4l2_vp9_seg_feat_enabled(const u8 *feature_enabled,
230 			  unsigned int feature,
231 			  unsigned int segid);
232 
233 #endif /* _MEDIA_V4L2_VP9_H */
234