1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Helper functions for vp9 codecs.
5 * Copyright (c) 2021 Collabora, Ltd.
7 * Author: Andrzej Pietrasiewicz <andrzej.p@collabora.com>
10 #ifndef _MEDIA_V4L2_VP9_H
11 #define _MEDIA_V4L2_VP9_H
13 #include <media/v4l2-ctrls.h>
16 * struct v4l2_vp9_frame_mv_context - motion vector-related probabilities
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.
28 * A member of v4l2_vp9_frame_context.
30 struct v4l2_vp9_frame_mv_context {
36 u8 class0_fr[2][2][3];
43 * struct v4l2_vp9_frame_context - frame probabilities, including motion-vector related
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.
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.
67 struct v4l2_vp9_frame_context {
71 u8 coef[4][2][2][6][6][3];
74 u8 interp_filter[4][2];
83 struct v4l2_vp9_frame_mv_context mv;
87 * struct v4l2_vp9_frame_symbol_counts - pointers to arrays of symbol counts
89 * @partition: partition counts.
91 * @intra_inter: is inter-block counts.
92 * @tx32p: TX32 counts.
93 * @tx16p: TX16 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.
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.
119 struct v4l2_vp9_frame_symbol_counts {
120 u32 (*partition)[16][4];
122 u32 (*intra_inter)[4][2];
126 u32 (*y_mode)[4][10];
127 u32 (*uv_mode)[10][10];
129 u32 (*comp_ref)[5][2];
130 u32 (*single_ref)[5][2][2];
131 u32 (*mv_mode)[7][4];
135 u32 (*classes)[2][11];
137 u32 (*bits)[2][10][2];
138 u32 (*class0_fp)[2][2][4];
140 u32 (*class0_hp)[2][2];
142 u32 (*coeff[4][2][2][6][6])[3];
143 u32 *eob[4][2][2][6][6][2];
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 */
152 * v4l2_vp9_fw_update_probs() - Perform forward update of vp9 probabilities
154 * @probs: current probabilities values
155 * @deltas: delta values from compressed header
156 * @dec_params: vp9 frame decoding parameters
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.
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);
168 * v4l2_vp9_reset_frame_ctx() - Reset appropriate frame context
170 * @dec_params: vp9 frame decoding parameters
171 * @frame_context: array of the 4 frame contexts
173 * This function resets appropriate frame contexts, based on what's in dec_params.
175 * Returns the frame context index after the update, which might be reset to zero if
176 * mandated by the spec.
178 u8 v4l2_vp9_reset_frame_ctx(const struct v4l2_ctrl_vp9_frame *dec_params,
179 struct v4l2_vp9_frame_context *frame_context);
182 * v4l2_vp9_adapt_coef_probs() - Perform backward update of vp9 coefficients probabilities
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
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.
193 void v4l2_vp9_adapt_coef_probs(struct v4l2_vp9_frame_context *probs,
194 struct v4l2_vp9_frame_symbol_counts *counts,
196 bool frame_is_intra);
199 * v4l2_vp9_adapt_noncoef_probs() - Perform backward update of vp9 non-coefficients probabilities
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
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.
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,
220 * v4l2_vp9_seg_feat_enabled() - Check if a segmentation feature is enabled
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
226 * This function returns true if a given feature is active in a given segment.
229 v4l2_vp9_seg_feat_enabled(const u8 *feature_enabled,
230 unsigned int feature,
233 #endif /* _MEDIA_V4L2_VP9_H */