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 /*!\file
13 * \brief Describes film grain parameters and film grain synthesis
14 *
15 */
16 #ifndef AOM_AOM_DSP_GRAIN_SYNTHESIS_H_
17 #define AOM_AOM_DSP_GRAIN_SYNTHESIS_H_
18
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22
23 #include <string.h>
24
25 #include "aom_dsp/aom_dsp_common.h"
26 #include "aom/aom_image.h"
27
28 /*!\brief Structure containing film grain synthesis parameters for a frame
29 *
30 * This structure contains input parameters for film grain synthesis
31 */
32 typedef struct {
33 // This structure is compared element-by-element in the function
34 // av1_check_grain_params_equiv: this function must be updated if any changes
35 // are made to this structure.
36 int apply_grain;
37
38 int update_parameters;
39
40 // 8 bit values
41 int scaling_points_y[14][2];
42 int num_y_points; // value: 0..14
43
44 // 8 bit values
45 int scaling_points_cb[10][2];
46 int num_cb_points; // value: 0..10
47
48 // 8 bit values
49 int scaling_points_cr[10][2];
50 int num_cr_points; // value: 0..10
51
52 int scaling_shift; // values : 8..11
53
54 int ar_coeff_lag; // values: 0..3
55
56 // 8 bit values
57 int ar_coeffs_y[24];
58 int ar_coeffs_cb[25];
59 int ar_coeffs_cr[25];
60
61 // Shift value: AR coeffs range
62 // 6: [-2, 2)
63 // 7: [-1, 1)
64 // 8: [-0.5, 0.5)
65 // 9: [-0.25, 0.25)
66 int ar_coeff_shift; // values : 6..9
67
68 int cb_mult; // 8 bits
69 int cb_luma_mult; // 8 bits
70 int cb_offset; // 9 bits
71
72 int cr_mult; // 8 bits
73 int cr_luma_mult; // 8 bits
74 int cr_offset; // 9 bits
75
76 int overlap_flag;
77
78 int clip_to_restricted_range;
79
80 unsigned int bit_depth; // video bit depth
81
82 int chroma_scaling_from_luma;
83
84 int grain_scale_shift;
85
86 uint16_t random_seed;
87 // This structure is compared element-by-element in the function
88 // av1_check_grain_params_equiv: this function must be updated if any changes
89 // are made to this structure.
90 } aom_film_grain_t;
91
92 /*!\brief Check if two film grain parameters structs are equivalent
93 *
94 * Check if two film grain parameters are equal, except for the
95 * update_parameters and random_seed elements which are ignored.
96 *
97 * \param[in] pa The first set of parameters to compare
98 * \param[in] pb The second set of parameters to compare
99 * \return Returns 1 if the params are equivalent, 0 otherwise
100 */
av1_check_grain_params_equiv(const aom_film_grain_t * const pa,const aom_film_grain_t * const pb)101 static INLINE int av1_check_grain_params_equiv(
102 const aom_film_grain_t *const pa, const aom_film_grain_t *const pb) {
103 if (pa->apply_grain != pb->apply_grain) return 0;
104 // Don't compare update_parameters
105
106 if (pa->num_y_points != pb->num_y_points) return 0;
107 if (memcmp(pa->scaling_points_y, pb->scaling_points_y,
108 pa->num_y_points * 2 * sizeof(*pa->scaling_points_y)) != 0)
109 return 0;
110
111 if (pa->num_cb_points != pb->num_cb_points) return 0;
112 if (memcmp(pa->scaling_points_cb, pb->scaling_points_cb,
113 pa->num_cb_points * 2 * sizeof(*pa->scaling_points_cb)) != 0)
114 return 0;
115
116 if (pa->num_cr_points != pb->num_cr_points) return 0;
117 if (memcmp(pa->scaling_points_cr, pb->scaling_points_cr,
118 pa->num_cr_points * 2 * sizeof(*pa->scaling_points_cr)) != 0)
119 return 0;
120
121 if (pa->scaling_shift != pb->scaling_shift) return 0;
122 if (pa->ar_coeff_lag != pb->ar_coeff_lag) return 0;
123
124 const int num_pos = 2 * pa->ar_coeff_lag * (pa->ar_coeff_lag + 1);
125 if (memcmp(pa->ar_coeffs_y, pb->ar_coeffs_y,
126 num_pos * sizeof(*pa->ar_coeffs_y)) != 0)
127 return 0;
128 if (memcmp(pa->ar_coeffs_cb, pb->ar_coeffs_cb,
129 num_pos * sizeof(*pa->ar_coeffs_cb)) != 0)
130 return 0;
131 if (memcmp(pa->ar_coeffs_cr, pb->ar_coeffs_cr,
132 num_pos * sizeof(*pa->ar_coeffs_cr)) != 0)
133 return 0;
134
135 if (pa->ar_coeff_shift != pb->ar_coeff_shift) return 0;
136
137 if (pa->cb_mult != pb->cb_mult) return 0;
138 if (pa->cb_luma_mult != pb->cb_luma_mult) return 0;
139 if (pa->cb_offset != pb->cb_offset) return 0;
140
141 if (pa->cr_mult != pb->cr_mult) return 0;
142 if (pa->cr_luma_mult != pb->cr_luma_mult) return 0;
143 if (pa->cr_offset != pb->cr_offset) return 0;
144
145 if (pa->overlap_flag != pb->overlap_flag) return 0;
146 if (pa->clip_to_restricted_range != pb->clip_to_restricted_range) return 0;
147 if (pa->bit_depth != pb->bit_depth) return 0;
148 if (pa->chroma_scaling_from_luma != pb->chroma_scaling_from_luma) return 0;
149 if (pa->grain_scale_shift != pb->grain_scale_shift) return 0;
150
151 return 1;
152 }
153
154 /*!\brief Add film grain
155 *
156 * Add film grain to an image
157 *
158 * Returns 0 for success, -1 for failure
159 *
160 * \param[in] grain_params Grain parameters
161 * \param[in] luma luma plane
162 * \param[in] cb cb plane
163 * \param[in] cr cr plane
164 * \param[in] height luma plane height
165 * \param[in] width luma plane width
166 * \param[in] luma_stride luma plane stride
167 * \param[in] chroma_stride chroma plane stride
168 */
169 int av1_add_film_grain_run(const aom_film_grain_t *grain_params, uint8_t *luma,
170 uint8_t *cb, uint8_t *cr, int height, int width,
171 int luma_stride, int chroma_stride,
172 int use_high_bit_depth, int chroma_subsamp_y,
173 int chroma_subsamp_x, int mc_identity);
174
175 /*!\brief Add film grain
176 *
177 * Add film grain to an image
178 *
179 * Returns 0 for success, -1 for failure
180 *
181 * \param[in] grain_params Grain parameters
182 * \param[in] src Source image
183 * \param[out] dst Resulting image with grain
184 */
185 int av1_add_film_grain(const aom_film_grain_t *grain_params,
186 const aom_image_t *src, aom_image_t *dst);
187
188 #ifdef __cplusplus
189 } // extern "C"
190 #endif
191
192 #endif // AOM_AOM_DSP_GRAIN_SYNTHESIS_H_
193