Fix inter-frame film grain params When using film grain for a shown inter-frame, there is the option to re-use the parameters from a reference frame. Logically, the encoder should indicate a reference frame (using film_grain_params_ref_idx) with the same film grain parameters as the current frame. Previously, the encoder had a bug where it would actually choose a reference frame where the film grain parameters did *not* match. Furthermore, a simple use of memcmp to compare the film grain parameters has two issues: structure packing values do not have a guaranteed value unless the structure is initialised using memset, and two attributes of the structure are overwritten and so their equality does not matter (update_parameters and random_seed). This patch replaces the use of memcmp with an equivalence checking function for film grain parameter structures. Change-Id: I3d97213ab8b2d77a0ac5b842100785590800a01c
diff --git a/aom_dsp/grain_synthesis.h b/aom_dsp/grain_synthesis.h index 7aee6f6..9155b39 100644 --- a/aom_dsp/grain_synthesis.h +++ b/aom_dsp/grain_synthesis.h
@@ -20,6 +20,8 @@ extern "C" { #endif +#include <string.h> + #include "aom_dsp/aom_dsp_common.h" #include "aom/aom_image.h" @@ -28,6 +30,9 @@ * This structure contains input parameters for film grain synthesis */ typedef struct { + // This structure is compared element-by-element in the function + // av1_check_grain_params_equiv: this function must be updated if any changes + // are made to this structure. int apply_grain; int update_parameters; @@ -79,8 +84,73 @@ int grain_scale_shift; uint16_t random_seed; + // This structure is compared element-by-element in the function + // av1_check_grain_params_equiv: this function must be updated if any changes + // are made to this structure. } aom_film_grain_t; +/*!\brief Check if two film grain parameters structs are equivalent + * + * Check if two film grain parameters are equal, except for the + * update_parameters and random_seed elements which are ignored. + * + * \param[in] pa The first set of parameters to compare + * \param[in] pb The second set of parameters to compare + * \return Returns 1 if the params are equivalent, 0 otherwise + */ +static INLINE int av1_check_grain_params_equiv( + const aom_film_grain_t *const pa, const aom_film_grain_t *const pb) { + if (pa->apply_grain != pb->apply_grain) return 0; + // Don't compare update_parameters + + if (pa->num_y_points != pb->num_y_points) return 0; + if (memcmp(pa->scaling_points_y, pb->scaling_points_y, + pa->num_y_points * 2 * sizeof(*pa->scaling_points_y)) != 0) + return 0; + + if (pa->num_cb_points != pb->num_cb_points) return 0; + if (memcmp(pa->scaling_points_cb, pb->scaling_points_cb, + pa->num_cb_points * 2 * sizeof(*pa->scaling_points_cb)) != 0) + return 0; + + if (pa->num_cr_points != pb->num_cr_points) return 0; + if (memcmp(pa->scaling_points_cr, pb->scaling_points_cr, + pa->num_cr_points * 2 * sizeof(*pa->scaling_points_cr)) != 0) + return 0; + + if (pa->scaling_shift != pb->scaling_shift) return 0; + if (pa->ar_coeff_lag != pb->ar_coeff_lag) return 0; + + const int num_pos = 2 * pa->ar_coeff_lag * (pa->ar_coeff_lag + 1); + if (memcmp(pa->ar_coeffs_y, pb->ar_coeffs_y, + num_pos * sizeof(*pa->ar_coeffs_y)) != 0) + return 0; + if (memcmp(pa->ar_coeffs_cb, pb->ar_coeffs_cb, + num_pos * sizeof(*pa->ar_coeffs_cb)) != 0) + return 0; + if (memcmp(pa->ar_coeffs_cr, pb->ar_coeffs_cr, + num_pos * sizeof(*pa->ar_coeffs_cr)) != 0) + return 0; + + if (pa->ar_coeff_shift != pb->ar_coeff_shift) return 0; + + if (pa->cb_mult != pb->cb_mult) return 0; + if (pa->cb_luma_mult != pb->cb_luma_mult) return 0; + if (pa->cb_offset != pb->cb_offset) return 0; + + if (pa->cr_mult != pb->cr_mult) return 0; + if (pa->cr_luma_mult != pb->cr_luma_mult) return 0; + if (pa->cr_offset != pb->cr_offset) return 0; + + if (pa->overlap_flag != pb->overlap_flag) return 0; + if (pa->clip_to_restricted_range != pb->clip_to_restricted_range) return 0; + if (pa->bit_depth != pb->bit_depth) return 0; + if (pa->chroma_scaling_from_luma != pb->chroma_scaling_from_luma) return 0; + if (pa->grain_scale_shift != pb->grain_scale_shift) return 0; + + return 1; +} + /*!\brief Add film grain * * Add film grain to an image
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c index 5a893a6..6fe8fdb 100644 --- a/av1/encoder/bitstream.c +++ b/av1/encoder/bitstream.c
@@ -2491,7 +2491,7 @@ assert(ref_idx != INVALID_IDX); const RefCntBuffer *const buf = cm->ref_frame_map[ref_idx]; if (buf->film_grain_params_present && - memcmp(pars, &buf->film_grain_params, sizeof(*pars))) { + av1_check_grain_params_equiv(pars, &buf->film_grain_params)) { break; } }