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_PICKRST_H_
12 #define AOM_AV1_ENCODER_PICKRST_H_
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #include "av1/encoder/encoder.h"
19
20 struct yv12_buffer_config;
21 struct AV1_COMP;
22
23 // Enable extra debugging for loop restoration costing?
24 //
25 // If this is set to 1, then we record not just the selected LR parameters, but
26 // also the values which the search process thinks they should be delta-coded
27 // against. Then, when writing out the bitstream, we verify this information,
28 // to help ensure that the search code is costing things properly
29 #define DEBUG_LR_COSTING 0
30
31 #if DEBUG_LR_COSTING
32 #define MAX_LR_UNITS_W 64
33 #define MAX_LR_UNITS_H 64
34
35 // Storage for reference parameters.
36 //
37 // The storage size is determined by:
38 // * This is always written and then checked within the same frame encode pass,
39 // so we do not need to buffer multiple frames of data
40 // * The parameters can be different per plane within one frame
41 // * The relevant set of ref parameters can differ between the search where
42 // we set the frame restoration mode to RESTORE_WIENER, and the search where
43 // we set it to RESTORE_SWITCHABLE.
44 // So we need to store at least two sets of Wiener params and two sets of
45 // SGR params, and the easiest way to do this is to index by
46 // frame_restoration_type
47 extern RestorationUnitInfo lr_ref_params[RESTORE_TYPES][MAX_MB_PLANE]
48 [MAX_LR_UNITS_W * MAX_LR_UNITS_H];
49 #endif // DEBUG_LR_COSTING
50
51 static const uint8_t g_shuffle_stats_data[16] = {
52 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8,
53 };
54
55 static const uint8_t g_shuffle_stats_highbd_data[32] = {
56 0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8, 9,
57 0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 6, 7, 6, 7, 8, 9,
58 };
59
find_average(const uint8_t * src,int h_start,int h_end,int v_start,int v_end,int stride)60 static inline uint8_t find_average(const uint8_t *src, int h_start, int h_end,
61 int v_start, int v_end, int stride) {
62 uint64_t sum = 0;
63 for (int i = v_start; i < v_end; i++) {
64 for (int j = h_start; j < h_end; j++) {
65 sum += src[i * stride + j];
66 }
67 }
68 uint64_t avg = sum / ((v_end - v_start) * (h_end - h_start));
69 return (uint8_t)avg;
70 }
71
72 #if CONFIG_AV1_HIGHBITDEPTH
find_average_highbd(const uint16_t * src,int h_start,int h_end,int v_start,int v_end,int stride)73 static inline uint16_t find_average_highbd(const uint16_t *src, int h_start,
74 int h_end, int v_start, int v_end,
75 int stride) {
76 uint64_t sum = 0;
77 for (int i = v_start; i < v_end; i++) {
78 for (int j = h_start; j < h_end; j++) {
79 sum += src[i * stride + j];
80 }
81 }
82 uint64_t avg = sum / ((v_end - v_start) * (h_end - h_start));
83 return (uint16_t)avg;
84 }
85 #endif
86
87 /*!\brief Algorithm for AV1 loop restoration search and estimation.
88 *
89 * \ingroup in_loop_restoration
90 * This function determines proper restoration filter types and
91 * associated parameters for each restoration unit in a frame.
92 *
93 * \param[in] sd Source frame buffer
94 * \param[in,out] cpi Top-level encoder structure
95 *
96 * \remark Nothing is returned. Instead, chosen restoration filter
97 * types and parameters are stored per plane in the \c rst_info structure
98 * of type \ref RestorationInfo inside \c cpi->common:
99 * \arg \c rst_info[ \c 0 ]: Chosen parameters for Y plane
100 * \arg \c rst_info[ \c 1 ]: Chosen parameters for U plane if it exists
101 * \arg \c rst_info[ \c 2 ]: Chosen parameters for V plane if it exists
102 * \par
103 * The following fields in each \c rst_info[ \c p], \c p = 0, 1, 2
104 * are populated:
105 * \arg \c rst_info[ \c p ].\c frame_restoration_type
106 * \arg \c rst_info[ \c p ].\c unit_info[ \c u ],
107 * for each \c u in 0, 1, ..., \c n( \c p ) - 1,
108 * where \c n( \c p ) is the number of restoration units in plane \c p.
109 * \par
110 * The following fields in each \c rst_info[ \c p ].\c unit_info[ \c u ],
111 * \c p = 0, 1, 2 and \c u = 0, 1, ..., \c n( \c p ) - 1, of type
112 * \ref RestorationUnitInfo are populated:
113 * \arg \c rst_info[ \c p ].\c unit_info[ \c u ].\c restoration_type
114 * \arg \c rst_info[ \c p ].\c unit_info[ \c u ].\c wiener_info OR
115 * \c rst_info[ \c p ].\c unit_info[ \c u ].\c sgrproj_info OR
116 * neither, depending on
117 * \c rst_info[ \c p ].\c unit_info[ \c u ].\c restoration_type
118 *
119 */
120 void av1_pick_filter_restoration(const YV12_BUFFER_CONFIG *sd, AV1_COMP *cpi);
121
122 #ifdef __cplusplus
123 } // extern "C"
124 #endif
125
126 #endif // AOM_AV1_ENCODER_PICKRST_H_
127