v4l2-vp9.h 8.3 KB

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