• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 #ifndef AOM_AV1_ENCODER_PICKCDEF_H_
12 #define AOM_AV1_ENCODER_PICKCDEF_H_
13 
14 #include "av1/common/cdef.h"
15 #include "av1/encoder/speed_features.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /*!\cond */
22 struct MultiThreadInfo;
23 
24 #define REDUCED_PRI_STRENGTHS_LVL1 8
25 #define REDUCED_PRI_STRENGTHS_LVL2 5
26 #define REDUCED_SEC_STRENGTHS_LVL3 2
27 #define REDUCED_PRI_STRENGTHS_LVL4 2
28 
29 #define REDUCED_TOTAL_STRENGTHS_LVL1 \
30   (REDUCED_PRI_STRENGTHS_LVL1 * CDEF_SEC_STRENGTHS)
31 #define REDUCED_TOTAL_STRENGTHS_LVL2 \
32   (REDUCED_PRI_STRENGTHS_LVL2 * CDEF_SEC_STRENGTHS)
33 #define REDUCED_TOTAL_STRENGTHS_LVL3 \
34   (REDUCED_PRI_STRENGTHS_LVL2 * REDUCED_SEC_STRENGTHS_LVL3)
35 #define REDUCED_TOTAL_STRENGTHS_LVL4 \
36   (REDUCED_PRI_STRENGTHS_LVL4 * REDUCED_SEC_STRENGTHS_LVL3)
37 #define TOTAL_STRENGTHS (CDEF_PRI_STRENGTHS * CDEF_SEC_STRENGTHS)
38 
39 static const int priconv_lvl1[REDUCED_PRI_STRENGTHS_LVL1] = { 0, 1, 2,  3,
40                                                               5, 7, 10, 13 };
41 static const int priconv_lvl2[REDUCED_PRI_STRENGTHS_LVL2] = { 0, 2, 4, 8, 14 };
42 static const int priconv_lvl4[REDUCED_PRI_STRENGTHS_LVL4] = { 0, 11 };
43 static const int secconv_lvl3[REDUCED_SEC_STRENGTHS_LVL3] = { 0, 2 };
44 static const int nb_cdef_strengths[CDEF_PICK_METHODS] = {
45   TOTAL_STRENGTHS,
46   REDUCED_TOTAL_STRENGTHS_LVL1,
47   REDUCED_TOTAL_STRENGTHS_LVL2,
48   REDUCED_TOTAL_STRENGTHS_LVL3,
49   REDUCED_TOTAL_STRENGTHS_LVL4,
50   TOTAL_STRENGTHS
51 };
52 
53 typedef void (*copy_fn_t)(uint16_t *dst, int dstride, const void *src,
54                           int src_voffset, int src_hoffset, int sstride,
55                           int vsize, int hsize);
56 typedef uint64_t (*compute_cdef_dist_t)(void *dst, int dstride, uint16_t *src,
57                                         cdef_list *dlist, int cdef_count,
58                                         BLOCK_SIZE bsize, int coeff_shift,
59                                         int row, int col);
60 
61 /*! \brief CDEF search context.
62  */
63 typedef struct {
64   /*!
65    * Pointer to the frame buffer holding the source frame
66    */
67   const YV12_BUFFER_CONFIG *ref;
68   /*!
69    * Pointer to params related to MB_MODE_INFO arrays and related info
70    */
71   CommonModeInfoParams *mi_params;
72   /*!
73    * Info specific to each plane
74    */
75   struct macroblockd_plane plane[MAX_MB_PLANE];
76   /*!
77    * Function pointer of copy_fn
78    */
79   copy_fn_t copy_fn;
80   /*!
81    * Function pointer of compute_cdef_dist_fn
82    */
83   compute_cdef_dist_t compute_cdef_dist_fn;
84   /*!
85    *  Number of strenghts evaluated in CDEF filter search
86    */
87   int total_strengths;
88   /*!
89    * Bit-depth dependent shift
90    */
91   int coeff_shift;
92   /*!
93    * CDEF damping factor
94    */
95   int damping;
96   /*!
97    * Search method used to select CDEF parameters
98    */
99   int pick_method;
100   /*!
101    * Number of planes
102    */
103   int num_planes;
104   /*!
105    * Log2 of width of the MI unit in pixels. mi_wide_l2[i]
106    * indicates the width of the MI unit in pixels for the ith plane
107    */
108   int mi_wide_l2[MAX_MB_PLANE];
109   /*!
110    * Log2 of height of the MI unit in pixels. mi_high_l2[i]
111    * indicates the height of the MI unit in pixels for the ith plane
112    */
113   int mi_high_l2[MAX_MB_PLANE];
114   /*!
115    * Subsampling in x direction. xdec[i] indicates the subsampling
116    * for the ith plane
117    */
118   int xdec[MAX_MB_PLANE];
119   /*!
120    * Subsampling in y direction. ydec[i] indicates the subsampling
121    * for the ith plane
122    */
123   int ydec[MAX_MB_PLANE];
124   /*!
125    * bsize[i] indicates the block size of ith plane
126    */
127   int bsize[MAX_MB_PLANE];
128   /*!
129    * Number of 64x64 blocks in vertical direction of a frame
130    */
131   int nvfb;
132   /*!
133    * Number of 64x64 blocks in horizontal direction of a frame
134    */
135   int nhfb;
136   /*!
137    * Pointer to the mean squared error between the CDEF filtered block and the
138    * source block. mse[i][j][k] stores the MSE of the ith plane (i=0 corresponds
139    * to Y-plane, i=1 corresponds to U and V planes), jth block and kth strength
140    * index
141    */
142   uint64_t (*mse[2])[TOTAL_STRENGTHS];
143   /*!
144    * Holds the position (in units of mi's) of the cdef filtered
145    * block in raster scan order
146    */
147   int *sb_index;
148   /*!
149    * Holds the count of cdef filtered blocks
150    */
151   int sb_count;
152 } CdefSearchCtx;
153 
sb_all_skip(const CommonModeInfoParams * const mi_params,int mi_row,int mi_col)154 static INLINE int sb_all_skip(const CommonModeInfoParams *const mi_params,
155                               int mi_row, int mi_col) {
156   const int maxr = AOMMIN(mi_params->mi_rows - mi_row, MI_SIZE_64X64);
157   const int maxc = AOMMIN(mi_params->mi_cols - mi_col, MI_SIZE_64X64);
158   const int stride = mi_params->mi_stride;
159   MB_MODE_INFO **mbmi = mi_params->mi_grid_base + mi_row * stride + mi_col;
160   for (int r = 0; r < maxr; ++r, mbmi += stride) {
161     for (int c = 0; c < maxc; ++c) {
162       if (!mbmi[c]->skip_txfm) return 0;
163     }
164   }
165   return 1;
166 }
167 
168 // Checks if cdef processing can be skipped for particular sb.
169 // Inputs:
170 //   cdef_search_ctx: Pointer to the structure containing parameters related to
171 //   CDEF search context.
172 //   fbr: Row index in units of 64x64 block
173 //   fbc: Column index in units of 64x64 block
174 // Returns:
175 //   1/0 will be returned to indicate skip/don't skip cdef processing of sb
176 //   respectively.
cdef_sb_skip(const CommonModeInfoParams * const mi_params,int fbr,int fbc)177 static INLINE int cdef_sb_skip(const CommonModeInfoParams *const mi_params,
178                                int fbr, int fbc) {
179   const MB_MODE_INFO *const mbmi =
180       mi_params->mi_grid_base[MI_SIZE_64X64 * fbr * mi_params->mi_stride +
181                               MI_SIZE_64X64 * fbc];
182   // No filtering if the entire filter block is skipped.
183   if (sb_all_skip(mi_params, fbr * MI_SIZE_64X64, fbc * MI_SIZE_64X64))
184     return 1;
185   // Skip odd numbered 64x64 block rows(cols) when bsize is BLOCK_128X128,
186   // BLOCK_64X128(BLOCK_128X128, BLOCK_128X64) as for such blocks CDEF filtering
187   // is done at the corresponding block sizes.
188   if (((fbc & 1) &&
189        (mbmi->bsize == BLOCK_128X128 || mbmi->bsize == BLOCK_128X64)) ||
190       ((fbr & 1) &&
191        (mbmi->bsize == BLOCK_128X128 || mbmi->bsize == BLOCK_64X128)))
192     return 1;
193   return 0;
194 }
195 
196 void av1_cdef_mse_calc_block(CdefSearchCtx *cdef_search_ctx, int fbr, int fbc,
197                              int sb_count);
198 /*!\endcond */
199 
200 /*!\brief AV1 CDEF parameter search
201  *
202  * \ingroup in_loop_cdef
203  *
204  * Searches for optimal CDEF parameters for frame
205  *
206  * \param[in]      mt_info      Pointer to multi-threading parameters
207  * \param[in]      frame        Compressed frame buffer
208  * \param[in]      ref          Source frame buffer
209  * \param[in,out]  cm           Pointer to top level common structure
210  * \param[in]      xd           Pointer to common current coding block structure
211  * \param[in]      pick_method  The method used to select params
212  * \param[in]      rdmult       rd multiplier to use in making param choices
213  * \param[in]      skip_cdef_feature Speed feature to skip cdef
214  * \param[in]      frames_since_key Number of frames since key frame
215  *
216  * \return Nothing is returned. Instead, optimal CDEF parameters are stored
217  * in the \c cdef_info structure of type \ref CdefInfo inside \c cm:
218  * \arg \c cdef_bits: Bits of strength parameters
219  * \arg \c nb_cdef_strengths: Number of strength parameters
220  * \arg \c cdef_strengths: list of \c nb_cdef_strengths strength parameters
221  * for the luma plane.
222  * \arg \c uv_cdef_strengths: list of \c nb_cdef_strengths strength parameters
223  * for the chroma planes.
224  * \arg \c damping_factor: CDEF damping factor.
225  *
226  */
227 void av1_cdef_search(struct MultiThreadInfo *mt_info,
228                      const YV12_BUFFER_CONFIG *frame,
229                      const YV12_BUFFER_CONFIG *ref, AV1_COMMON *cm,
230                      MACROBLOCKD *xd, CDEF_PICK_METHOD pick_method, int rdmult,
231                      int skip_cdef_feature, int frames_since_key);
232 
233 #ifdef __cplusplus
234 }  // extern "C"
235 #endif
236 #endif  // AOM_AV1_ENCODER_PICKCDEF_H_
237