xref: /aosp_15_r20/external/libaom/aom_dsp/grain_table.h (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2018, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker /*!\file
13*77c1e3ccSAndroid Build Coastguard Worker  * \brief A table mapping from time to corresponding film grain parameters.
14*77c1e3ccSAndroid Build Coastguard Worker  *
15*77c1e3ccSAndroid Build Coastguard Worker  * In order to apply grain synthesis in the decoder, the film grain parameters
16*77c1e3ccSAndroid Build Coastguard Worker  * need to be signalled in the encoder. The film grain parameters are time
17*77c1e3ccSAndroid Build Coastguard Worker  * varying, and for two-pass encoding (and denoiser implementation flexibility)
18*77c1e3ccSAndroid Build Coastguard Worker  * it is common to denoise the video and do parameter estimation before encoding
19*77c1e3ccSAndroid Build Coastguard Worker  * the denoised video.
20*77c1e3ccSAndroid Build Coastguard Worker  *
21*77c1e3ccSAndroid Build Coastguard Worker  * The film grain table is used to provide this flexibility and is used as a
22*77c1e3ccSAndroid Build Coastguard Worker  * parameter that is passed to the encoder.
23*77c1e3ccSAndroid Build Coastguard Worker  *
24*77c1e3ccSAndroid Build Coastguard Worker  * Further, if regraining is to be done in say a single pass mode, or in two
25*77c1e3ccSAndroid Build Coastguard Worker  * pass within the encoder (before frames are added to the lookahead buffer),
26*77c1e3ccSAndroid Build Coastguard Worker  * this data structure can be used to keep track of on-the-fly estimated grain
27*77c1e3ccSAndroid Build Coastguard Worker  * parameters, that are then extracted from the table before the encoded frame
28*77c1e3ccSAndroid Build Coastguard Worker  * is written.
29*77c1e3ccSAndroid Build Coastguard Worker  */
30*77c1e3ccSAndroid Build Coastguard Worker #ifndef AOM_AOM_DSP_GRAIN_TABLE_H_
31*77c1e3ccSAndroid Build Coastguard Worker #define AOM_AOM_DSP_GRAIN_TABLE_H_
32*77c1e3ccSAndroid Build Coastguard Worker 
33*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
34*77c1e3ccSAndroid Build Coastguard Worker extern "C" {
35*77c1e3ccSAndroid Build Coastguard Worker #endif
36*77c1e3ccSAndroid Build Coastguard Worker 
37*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/grain_params.h"
38*77c1e3ccSAndroid Build Coastguard Worker #include "aom/internal/aom_codec_internal.h"
39*77c1e3ccSAndroid Build Coastguard Worker 
40*77c1e3ccSAndroid Build Coastguard Worker typedef struct aom_film_grain_table_entry_t {
41*77c1e3ccSAndroid Build Coastguard Worker   aom_film_grain_t params;
42*77c1e3ccSAndroid Build Coastguard Worker   int64_t start_time;
43*77c1e3ccSAndroid Build Coastguard Worker   int64_t end_time;
44*77c1e3ccSAndroid Build Coastguard Worker   struct aom_film_grain_table_entry_t *next;
45*77c1e3ccSAndroid Build Coastguard Worker } aom_film_grain_table_entry_t;
46*77c1e3ccSAndroid Build Coastguard Worker 
47*77c1e3ccSAndroid Build Coastguard Worker typedef struct {
48*77c1e3ccSAndroid Build Coastguard Worker   aom_film_grain_table_entry_t *head;
49*77c1e3ccSAndroid Build Coastguard Worker   aom_film_grain_table_entry_t *tail;
50*77c1e3ccSAndroid Build Coastguard Worker } aom_film_grain_table_t;
51*77c1e3ccSAndroid Build Coastguard Worker 
52*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Add a mapping from [time_stamp, end_time) to the given grain
53*77c1e3ccSAndroid Build Coastguard Worker  * parameters
54*77c1e3ccSAndroid Build Coastguard Worker  *
55*77c1e3ccSAndroid Build Coastguard Worker  * \param[in,out] table      The grain table
56*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]     time_stamp The start time stamp
57*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]     end_stamp  The end time_stamp
58*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]     grain      The grain parameters
59*77c1e3ccSAndroid Build Coastguard Worker  */
60*77c1e3ccSAndroid Build Coastguard Worker void aom_film_grain_table_append(aom_film_grain_table_t *table,
61*77c1e3ccSAndroid Build Coastguard Worker                                  int64_t time_stamp, int64_t end_time,
62*77c1e3ccSAndroid Build Coastguard Worker                                  const aom_film_grain_t *grain);
63*77c1e3ccSAndroid Build Coastguard Worker 
64*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Look-up (and optionally erase) the grain parameters for the given time
65*77c1e3ccSAndroid Build Coastguard Worker  *
66*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]  table      The grain table
67*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]  time_stamp The start time stamp
68*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]  end_stamp  The end time_stamp
69*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]  erase      Whether the time segment can be deleted
70*77c1e3ccSAndroid Build Coastguard Worker  * \param[out] grain      The output grain parameters
71*77c1e3ccSAndroid Build Coastguard Worker  */
72*77c1e3ccSAndroid Build Coastguard Worker int aom_film_grain_table_lookup(aom_film_grain_table_t *t, int64_t time_stamp,
73*77c1e3ccSAndroid Build Coastguard Worker                                 int64_t end_time, int erase,
74*77c1e3ccSAndroid Build Coastguard Worker                                 aom_film_grain_t *grain);
75*77c1e3ccSAndroid Build Coastguard Worker 
76*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Reads the grain table from a file.
77*77c1e3ccSAndroid Build Coastguard Worker  *
78*77c1e3ccSAndroid Build Coastguard Worker  * \param[out]  table       The grain table
79*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]   filename    The file to read from
80*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]   error_info  Error info for tracking errors
81*77c1e3ccSAndroid Build Coastguard Worker  */
82*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_film_grain_table_read(
83*77c1e3ccSAndroid Build Coastguard Worker     aom_film_grain_table_t *table, const char *filename,
84*77c1e3ccSAndroid Build Coastguard Worker     struct aom_internal_error_info *error_info);
85*77c1e3ccSAndroid Build Coastguard Worker 
86*77c1e3ccSAndroid Build Coastguard Worker /*!\brief Writes the grain table from a file.
87*77c1e3ccSAndroid Build Coastguard Worker  *
88*77c1e3ccSAndroid Build Coastguard Worker  * \param[out]  table       The grain table
89*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]   filename    The file to read from
90*77c1e3ccSAndroid Build Coastguard Worker  * \param[in]   error_info  Error info for tracking errors
91*77c1e3ccSAndroid Build Coastguard Worker  */
92*77c1e3ccSAndroid Build Coastguard Worker aom_codec_err_t aom_film_grain_table_write(
93*77c1e3ccSAndroid Build Coastguard Worker     const aom_film_grain_table_t *t, const char *filename,
94*77c1e3ccSAndroid Build Coastguard Worker     struct aom_internal_error_info *error_info);
95*77c1e3ccSAndroid Build Coastguard Worker 
96*77c1e3ccSAndroid Build Coastguard Worker void aom_film_grain_table_free(aom_film_grain_table_t *t);
97*77c1e3ccSAndroid Build Coastguard Worker 
98*77c1e3ccSAndroid Build Coastguard Worker #ifdef __cplusplus
99*77c1e3ccSAndroid Build Coastguard Worker }
100*77c1e3ccSAndroid Build Coastguard Worker #endif
101*77c1e3ccSAndroid Build Coastguard Worker 
102*77c1e3ccSAndroid Build Coastguard Worker #endif  // AOM_AOM_DSP_GRAIN_TABLE_H_
103