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 12 #ifndef AOM_AV1_ENCODER_PICKLPF_H_ 13 #define AOM_AV1_ENCODER_PICKLPF_H_ 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 #include "av1/encoder/encoder.h" 20 21 struct yv12_buffer_config; 22 struct AV1_COMP; 23 24 /*!\brief Algorithm for AV1 loop filter level selection. 25 * 26 * \ingroup in_loop_filter 27 * This function determines proper filter levels used for in-loop filter 28 * (deblock filter). 29 * 30 * \param[in] sd The pointer of frame buffer 31 * \param[in] cpi Top-level encoder structure 32 * \param[in] method The method used to select filter levels 33 * 34 * \par 35 * method includes: 36 * \arg \c LPF_PICK_FROM_FULL_IMAGE: Try the full image with different values. 37 * \arg \c LPF_PICK_FROM_FULL_IMAGE_NON_DUAL: Try the full image filter search 38 * with non-dual filter only. 39 * \arg \c LPF_PICK_FROM_SUBIMAGE: Try a small portion of the image with 40 * different values. 41 * \arg \c LPF_PICK_FROM_Q: Estimate the level based on quantizer and frame type 42 * \arg \c LPF_PICK_MINIMAL_LPF: Pick 0 to disable LPF if LPF was enabled last 43 * frame 44 * 45 * \remark Nothing is returned. Instead, filter levels below are stored in the 46 * "loopfilter" structure inside "cpi": 47 * \arg \c filter_level[0]: the vertical filter level for Y plane 48 * \arg \c filter_level[1]: the horizontal filter level for Y plane 49 * \arg \c filter_level_u: the filter level for U plane 50 * \arg \c filter_level_v: the filter level for V plane 51 * 52 * \n 53 * \b Overview 54 * \par 55 * The workflow of deblock filter is shown in Fig.1. \n 56 * Boundary pixels pass through a non-flatness check, followed by a step that 57 * determines smoothness and selects proper types of filters 58 * (4-, 6-, 8-, 14-tap filter). \n 59 * If non-flatness criteria is not satisfied, the encoder will not apply 60 * deblock filtering on these boundary pixels. 61 * \image html filter_flow.png "Fig.1. The workflow of deblock filter" width=70% 62 * 63 * \par 64 * The non-flatness is determined by the boundary pixels and thresholds as shown 65 * in Fig.2. \n 66 * Filtering is applied when \n 67 * \f$|p_0-p_1|<thr_1\f$ and \f$|q_0-q_1|<thr_1\f$ and 68 * \f$2*|p_0-q_0|+|p_1-q_1|/2<thr_2\f$ \n 69 * \image html filter_thr.png "Fig.2. Non-flatness of pixel boundary" height=40% 70 * 71 * \par 72 * Thresholds ("thr_1" and "thr_2") are determined by the filter level. \n 73 * In AV1, for each frame, we employ the four filter levels, based on these 74 * observations: \n 75 * Luma and chroma planes have different characteristics, including subsampling 76 * (different plane size), coding quality (chroma planes are better coded). \n 77 * Therefore chroma planes need less deblocking filtering than luma plane. \n 78 * In addition, content texture has different spatial characteristics: vertical 79 * and horizontal direction may need different level of filtering. \n 80 * The selection of these filter levels is described in the following section. 81 * 82 * \par 83 * \b Algorithm 84 * \par 85 * The encoder selects filter levels given the current frame buffer, and the 86 * method. \n 87 * By default, "LPF_PICK_FROM_FULL_IMAGE" is used, which should provide 88 * the most appropriate filter levels. \n 89 * For video on demand (VOD) mode, if speed setting is larger than 5, 90 * "LPF_PICK_FROM_FULL_IMAGE_NON_DUAL" is used. \n 91 * For real-time mode, if speed setting is larger than 5, "LPF_PICK_FROM_Q" is 92 * used. 93 * 94 * \par 95 * "LPF_PICK_FROM_FULL_IMAGE" method: determine filter levels sequentially 96 * by a filter level search procedure (function "search_filter_level"). \n 97 * The order is: \n 98 * First search and determine the filter level for Y plane. 99 * Let vertical filter level (filter_level[0]) and the horizontal filter level 100 * (filter_level[1]) be equal to it. \n 101 * Keep the horizontal filter level the same and search and determine the 102 * vertical filter level. \n 103 * Search and determine the horizontal filter level. \n 104 * Search and determine filter level for U plane. \n 105 * Search and determine filter level for V plane. 106 * 107 * \par 108 * Search and determine filter level is fulfilled by function 109 * "search_filter_level". \n 110 * It starts with a base filter level ("filt_mid") initialized by the 111 * corresponding last frame's filter level. \n 112 * A filter step ("filter_step") is determined as: 113 * filter_step = filt_mid < 16 ? 4 : filt_mid / 4. \n 114 * Then a modified binary search strategy is employed to find a proper 115 * filter level. \n 116 * In each iteration, set filt_low = filt_mid - filter_step, 117 * filt_high = filt_mid + filter_step. \n 118 * We now have three candidate levels, "filt_mid", "filt_low" and "filt_high". 119 * \n 120 * Deblock filtering is applied on the current frame with candidate filter 121 * levels and the sum of squared error (SSE) between source and filtered frame 122 * is computed. \n 123 * Set "filt_best" to the filter level of the smallest SSE. If "filter_best" 124 * equals to "filt_mid", halve the filter_step. Otherwise, set filt_mid = 125 * filt_best. \n 126 * Go to the next iteration until "filter_step" is 0. \n 127 * Note that in the comparison of SSEs between SSE[filt_low] and SSE[filt_mid], 128 * a "bias" is introduced to slightly raise the filter level. \n 129 * It is based on the observation that low filter levels tend to yield a smaller 130 * SSE and produce a higher PSNR for the current frame, \n 131 * while oversmoothing it and degradating the quality for prediction for future 132 * frames and leanding to a suboptimal performance overall. \n 133 * Function "try_filter_frame" is the referrence for applying deblock filtering 134 * with a given filter level and computatition of SSE. 135 * 136 * \par 137 * "LPF_PICK_FROM_FULL_IMAGE_NON_DUAL" method: almost the same as 138 * "LPF_PICK_FROM_FULL_IMAGE", \n 139 * just without separately searching for appropriate filter levels for vertical 140 * and horizontal filters. 141 * 142 * \par 143 * "LPF_PICK_FROM_Q" method: filter levels are determined by the 144 * quantization factor (q). \n 145 * For 8 bit: \n 146 * Keyframes: filt_guess = q * 0.06699 - 1.60817 \n 147 * Other frames: filt_guess = q * inter_frame_multiplier + 2.48225 \n 148 * inter_frame_multiplier = q > 700 ? 0.04590 : 0.02295 \n 149 * For 10 bit and 12 bit: \n 150 * filt_guess = q * 0.316206 + 3.87252 \n 151 * Then filter_level[0] = filter_level[1] = filter_level_u = filter_level_v = 152 * clamp(filt_guess, min_filter_level, max_filter_level) \n 153 * Where min_filter_level = 0, max_filter_level = 64 \n 154 * The equations were determined by linear fitting using filter levels 155 * generated by "LPF_PICK_FROM_FULL_IMAGE" method. 156 * 157 */ 158 void av1_pick_filter_level(const struct yv12_buffer_config *sd, 159 struct AV1_COMP *cpi, LPF_PICK_METHOD method); 160 #ifdef __cplusplus 161 } // extern "C" 162 #endif 163 164 #endif // AOM_AV1_ENCODER_PICKLPF_H_ 165