Coverage Report

Created: 2026-04-01 07:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/aom_dsp/grain_table.c
Line
Count
Source
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 This file has the implementation details of the grain table.
14
 *
15
 * The file format is an ascii representation for readability and
16
 * editability. Array parameters are separated from the non-array
17
 * parameters and prefixed with a few characters to make for easy
18
 * localization with a parameter set. Each entry is prefixed with "E"
19
 * and the other parameters are only specified if "update-parms" is
20
 * non-zero.
21
 *
22
 * filmgrn1
23
 * E <start-time> <end-time> <apply-grain> <random-seed> <update-parms>
24
 *  p <ar_coeff_lag> <ar_coeff_shift> <grain_scale_shift> ...
25
 *  sY <num_y_points> <point_0_x> <point_0_y> ...
26
 *  sCb <num_cb_points> <point_0_x> <point_0_y> ...
27
 *  sCr <num_cr_points> <point_0_x> <point_0_y> ...
28
 *  cY <ar_coeff_y_0> ....
29
 *  cCb <ar_coeff_cb_0> ....
30
 *  cCr <ar_coeff_cr_0> ....
31
 * E <start-time> ...
32
 */
33
#include <inttypes.h>
34
#include <string.h>
35
#include <stdio.h>
36
#include "aom_dsp/aom_dsp_common.h"
37
#include "aom_dsp/grain_table.h"
38
#include "aom_mem/aom_mem.h"
39
40
static const char kFileMagic[8] = { 'f', 'i', 'l', 'm', 'g', 'r', 'n', '1' };
41
42
static void grain_table_entry_read(FILE *file,
43
                                   struct aom_internal_error_info *error_info,
44
0
                                   aom_film_grain_table_entry_t *entry) {
45
0
  aom_film_grain_t *pars = &entry->params;
46
0
  int num_read =
47
0
      fscanf(file, "E %" PRId64 " %" PRId64 " %d %hd %d\n", &entry->start_time,
48
0
             &entry->end_time, &pars->apply_grain, &pars->random_seed,
49
0
             &pars->update_parameters);
50
0
  if (num_read == 0 && feof(file)) return;
51
0
  if (num_read != 5) {
52
0
    aom_internal_error(error_info, AOM_CODEC_ERROR,
53
0
                       "Unable to read entry header. Read %d != 5", num_read);
54
0
  }
55
0
  if (pars->update_parameters) {
56
0
    num_read = fscanf(file, "p %d %d %d %d %d %d %d %d %d %d %d %d\n",
57
0
                      &pars->ar_coeff_lag, &pars->ar_coeff_shift,
58
0
                      &pars->grain_scale_shift, &pars->scaling_shift,
59
0
                      &pars->chroma_scaling_from_luma, &pars->overlap_flag,
60
0
                      &pars->cb_mult, &pars->cb_luma_mult, &pars->cb_offset,
61
0
                      &pars->cr_mult, &pars->cr_luma_mult, &pars->cr_offset);
62
0
    if (num_read != 12) {
63
0
      aom_internal_error(error_info, AOM_CODEC_ERROR,
64
0
                         "Unable to read entry params. Read %d != 12",
65
0
                         num_read);
66
0
    }
67
0
    if (!fscanf(file, "\tsY %d ", &pars->num_y_points)) {
68
0
      aom_internal_error(error_info, AOM_CODEC_ERROR,
69
0
                         "Unable to read num y points");
70
0
    }
71
0
    for (int i = 0; i < pars->num_y_points; ++i) {
72
0
      if (2 != fscanf(file, "%d %d", &pars->scaling_points_y[i][0],
73
0
                      &pars->scaling_points_y[i][1])) {
74
0
        aom_internal_error(error_info, AOM_CODEC_ERROR,
75
0
                           "Unable to read y scaling points");
76
0
      }
77
0
    }
78
0
    if (!fscanf(file, "\n\tsCb %d", &pars->num_cb_points)) {
79
0
      aom_internal_error(error_info, AOM_CODEC_ERROR,
80
0
                         "Unable to read num cb points");
81
0
    }
82
0
    for (int i = 0; i < pars->num_cb_points; ++i) {
83
0
      if (2 != fscanf(file, "%d %d", &pars->scaling_points_cb[i][0],
84
0
                      &pars->scaling_points_cb[i][1])) {
85
0
        aom_internal_error(error_info, AOM_CODEC_ERROR,
86
0
                           "Unable to read cb scaling points");
87
0
      }
88
0
    }
89
0
    if (!fscanf(file, "\n\tsCr %d", &pars->num_cr_points)) {
90
0
      aom_internal_error(error_info, AOM_CODEC_ERROR,
91
0
                         "Unable to read num cr points");
92
0
    }
93
0
    for (int i = 0; i < pars->num_cr_points; ++i) {
94
0
      if (2 != fscanf(file, "%d %d", &pars->scaling_points_cr[i][0],
95
0
                      &pars->scaling_points_cr[i][1])) {
96
0
        aom_internal_error(error_info, AOM_CODEC_ERROR,
97
0
                           "Unable to read cr scaling points");
98
0
      }
99
0
    }
100
101
0
    if (fscanf(file, "\n\tcY")) {
102
0
      aom_internal_error(error_info, AOM_CODEC_ERROR,
103
0
                         "Unable to read Y coeffs header (cY)");
104
0
    }
105
0
    const int n = 2 * pars->ar_coeff_lag * (pars->ar_coeff_lag + 1);
106
0
    for (int i = 0; i < n; ++i) {
107
0
      if (1 != fscanf(file, "%d", &pars->ar_coeffs_y[i])) {
108
0
        aom_internal_error(error_info, AOM_CODEC_ERROR,
109
0
                           "Unable to read Y coeffs");
110
0
      }
111
0
    }
112
0
    if (fscanf(file, "\n\tcCb")) {
113
0
      aom_internal_error(error_info, AOM_CODEC_ERROR,
114
0
                         "Unable to read Cb coeffs header (cCb)");
115
0
    }
116
0
    for (int i = 0; i <= n; ++i) {
117
0
      if (1 != fscanf(file, "%d", &pars->ar_coeffs_cb[i])) {
118
0
        aom_internal_error(error_info, AOM_CODEC_ERROR,
119
0
                           "Unable to read Cb coeffs");
120
0
      }
121
0
    }
122
0
    if (fscanf(file, "\n\tcCr")) {
123
0
      aom_internal_error(error_info, AOM_CODEC_ERROR,
124
0
                         "Unable read to Cr coeffs header (cCr)");
125
0
    }
126
0
    for (int i = 0; i <= n; ++i) {
127
0
      if (1 != fscanf(file, "%d", &pars->ar_coeffs_cr[i])) {
128
0
        aom_internal_error(error_info, AOM_CODEC_ERROR,
129
0
                           "Unable to read Cr coeffs");
130
0
      }
131
0
    }
132
0
    (void)fscanf(file, "\n");
133
0
  }
134
0
}
135
136
static void grain_table_entry_write(FILE *file,
137
0
                                    aom_film_grain_table_entry_t *entry) {
138
0
  const aom_film_grain_t *pars = &entry->params;
139
0
  fprintf(file, "E %" PRId64 " %" PRId64 " %d %d %d\n", entry->start_time,
140
0
          entry->end_time, pars->apply_grain, pars->random_seed,
141
0
          pars->update_parameters);
142
0
  if (pars->update_parameters) {
143
0
    fprintf(file, "\tp %d %d %d %d %d %d %d %d %d %d %d %d\n",
144
0
            pars->ar_coeff_lag, pars->ar_coeff_shift, pars->grain_scale_shift,
145
0
            pars->scaling_shift, pars->chroma_scaling_from_luma,
146
0
            pars->overlap_flag, pars->cb_mult, pars->cb_luma_mult,
147
0
            pars->cb_offset, pars->cr_mult, pars->cr_luma_mult,
148
0
            pars->cr_offset);
149
0
    fprintf(file, "\tsY %d ", pars->num_y_points);
150
0
    for (int i = 0; i < pars->num_y_points; ++i) {
151
0
      fprintf(file, " %d %d", pars->scaling_points_y[i][0],
152
0
              pars->scaling_points_y[i][1]);
153
0
    }
154
0
    fprintf(file, "\n\tsCb %d", pars->num_cb_points);
155
0
    for (int i = 0; i < pars->num_cb_points; ++i) {
156
0
      fprintf(file, " %d %d", pars->scaling_points_cb[i][0],
157
0
              pars->scaling_points_cb[i][1]);
158
0
    }
159
0
    fprintf(file, "\n\tsCr %d", pars->num_cr_points);
160
0
    for (int i = 0; i < pars->num_cr_points; ++i) {
161
0
      fprintf(file, " %d %d", pars->scaling_points_cr[i][0],
162
0
              pars->scaling_points_cr[i][1]);
163
0
    }
164
0
    fprintf(file, "\n\tcY");
165
0
    const int n = 2 * pars->ar_coeff_lag * (pars->ar_coeff_lag + 1);
166
0
    for (int i = 0; i < n; ++i) {
167
0
      fprintf(file, " %d", pars->ar_coeffs_y[i]);
168
0
    }
169
0
    fprintf(file, "\n\tcCb");
170
0
    for (int i = 0; i <= n; ++i) {
171
0
      fprintf(file, " %d", pars->ar_coeffs_cb[i]);
172
0
    }
173
0
    fprintf(file, "\n\tcCr");
174
0
    for (int i = 0; i <= n; ++i) {
175
0
      fprintf(file, " %d", pars->ar_coeffs_cr[i]);
176
0
    }
177
0
    fprintf(file, "\n");
178
0
  }
179
0
}
180
181
// TODO(https://crbug.com/aomedia/3228): Update this function to return an
182
// integer status.
183
void aom_film_grain_table_append(aom_film_grain_table_t *t, int64_t time_stamp,
184
                                 int64_t end_time,
185
0
                                 const aom_film_grain_t *grain) {
186
0
  if (!t->tail || memcmp(grain, &t->tail->params, sizeof(*grain))) {
187
0
    aom_film_grain_table_entry_t *new_tail = aom_malloc(sizeof(*new_tail));
188
0
    if (!new_tail) return;
189
0
    memset(new_tail, 0, sizeof(*new_tail));
190
0
    if (t->tail) t->tail->next = new_tail;
191
0
    if (!t->head) t->head = new_tail;
192
0
    t->tail = new_tail;
193
194
0
    new_tail->start_time = time_stamp;
195
0
    new_tail->end_time = end_time;
196
0
    new_tail->params = *grain;
197
0
  } else {
198
0
    t->tail->end_time = AOMMAX(t->tail->end_time, end_time);
199
0
    t->tail->start_time = AOMMIN(t->tail->start_time, time_stamp);
200
0
  }
201
0
}
202
203
int aom_film_grain_table_lookup(aom_film_grain_table_t *t, int64_t time_stamp,
204
                                int64_t end_time, int erase,
205
0
                                aom_film_grain_t *grain) {
206
0
  aom_film_grain_table_entry_t *entry = t->head;
207
0
  aom_film_grain_table_entry_t *prev_entry = NULL;
208
0
  uint16_t random_seed = grain ? grain->random_seed : 0;
209
0
  if (grain) memset(grain, 0, sizeof(*grain));
210
211
0
  while (entry) {
212
0
    aom_film_grain_table_entry_t *next = entry->next;
213
0
    if (time_stamp >= entry->start_time && time_stamp < entry->end_time) {
214
0
      if (grain) {
215
0
        *grain = entry->params;
216
0
        if (time_stamp != 0) grain->random_seed = random_seed;
217
0
      }
218
0
      if (!erase) return 1;
219
220
0
      const int64_t entry_end_time = entry->end_time;
221
0
      if (time_stamp <= entry->start_time && end_time >= entry->end_time) {
222
0
        if (t->tail == entry) t->tail = prev_entry;
223
0
        if (prev_entry) {
224
0
          prev_entry->next = entry->next;
225
0
        } else {
226
0
          t->head = entry->next;
227
0
        }
228
0
        aom_free(entry);
229
0
      } else if (time_stamp <= entry->start_time &&
230
0
                 end_time < entry->end_time) {
231
0
        entry->start_time = end_time;
232
0
      } else if (time_stamp > entry->start_time &&
233
0
                 end_time >= entry->end_time) {
234
0
        entry->end_time = time_stamp;
235
0
      } else {
236
0
        aom_film_grain_table_entry_t *new_entry =
237
0
            aom_malloc(sizeof(*new_entry));
238
0
        if (!new_entry) return 0;
239
0
        new_entry->next = entry->next;
240
0
        new_entry->start_time = end_time;
241
0
        new_entry->end_time = entry->end_time;
242
0
        new_entry->params = entry->params;
243
0
        entry->next = new_entry;
244
0
        entry->end_time = time_stamp;
245
0
        if (t->tail == entry) t->tail = new_entry;
246
0
      }
247
      // If segments aren't aligned, delete from the beginning of subsequent
248
      // segments
249
0
      if (end_time > entry_end_time) {
250
        // Ignoring the return value here is safe since we're erasing from the
251
        // beginning of subsequent entries.
252
0
        aom_film_grain_table_lookup(t, entry_end_time, end_time, /*erase=*/1,
253
0
                                    NULL);
254
0
      }
255
0
      return 1;
256
0
    }
257
0
    prev_entry = entry;
258
0
    entry = next;
259
0
  }
260
0
  return 0;
261
0
}
262
263
aom_codec_err_t aom_film_grain_table_read(
264
    aom_film_grain_table_t *t, const char *filename,
265
0
    struct aom_internal_error_info *error_info) {
266
0
  FILE *const file = fopen(filename, "rb");
267
0
  error_info->error_code = AOM_CODEC_OK;
268
269
0
  if (!file) {
270
0
    error_info->error_code = AOM_CODEC_ERROR;
271
0
    return AOM_CODEC_ERROR;
272
0
  }
273
274
0
  error_info->setjmp = 1;
275
0
  if (setjmp(error_info->jmp) == 0) {
276
    // Read in one extra character as there should be white space after
277
    // the header.
278
0
    char magic[9];
279
0
    if (!fread(magic, 9, 1, file) || memcmp(magic, kFileMagic, 8)) {
280
0
      aom_internal_error(error_info, AOM_CODEC_ERROR,
281
0
                         "Unable to read (or invalid) file magic");
282
0
    }
283
284
0
    aom_film_grain_table_entry_t *prev_entry = NULL;
285
0
    for (;;) {
286
      // Check for end of file before attempting to read.
287
0
      int c = fgetc(file);
288
0
      if (c == EOF) {
289
0
        break;
290
0
      }
291
0
      ungetc(c, file);
292
293
0
      aom_film_grain_table_entry_t *entry = aom_malloc(sizeof(*entry));
294
0
      if (!entry) {
295
0
        aom_internal_error(error_info, AOM_CODEC_MEM_ERROR,
296
0
                           "Unable to allocate grain table entry");
297
0
      }
298
0
      memset(entry, 0, sizeof(*entry));
299
0
      grain_table_entry_read(file, error_info, entry);
300
0
      entry->next = NULL;
301
302
0
      if (prev_entry) prev_entry->next = entry;
303
0
      if (!t->head) t->head = entry;
304
0
      t->tail = entry;
305
0
      prev_entry = entry;
306
0
    }
307
0
  }
308
309
0
  fclose(file);
310
0
  error_info->setjmp = 0;
311
312
0
  return error_info->error_code;
313
0
}
314
315
aom_codec_err_t aom_film_grain_table_write(
316
    const aom_film_grain_table_t *t, const char *filename,
317
0
    struct aom_internal_error_info *error_info) {
318
0
  FILE *const file = fopen(filename, "wb");
319
0
  error_info->error_code = AOM_CODEC_OK;
320
321
0
  if (!file) {
322
0
    error_info->error_code = AOM_CODEC_ERROR;
323
0
    return AOM_CODEC_ERROR;
324
0
  }
325
326
0
  error_info->setjmp = 1;
327
0
  if (setjmp(error_info->jmp) == 0) {
328
0
    if (!fwrite(kFileMagic, 8, 1, file)) {
329
0
      aom_internal_error(error_info, AOM_CODEC_ERROR,
330
0
                         "Unable to write file magic");
331
0
    }
332
333
0
    fprintf(file, "\n");
334
0
    aom_film_grain_table_entry_t *entry = t->head;
335
0
    while (entry) {
336
0
      grain_table_entry_write(file, entry);
337
0
      entry = entry->next;
338
0
    }
339
0
  }
340
341
0
  fclose(file);
342
0
  error_info->setjmp = 0;
343
344
0
  return error_info->error_code;
345
0
}
346
347
0
void aom_film_grain_table_free(aom_film_grain_table_t *t) {
348
0
  aom_film_grain_table_entry_t *entry = t->head;
349
0
  while (entry) {
350
0
    aom_film_grain_table_entry_t *next = entry->next;
351
0
    aom_free(entry);
352
0
    entry = next;
353
0
  }
354
0
  memset(t, 0, sizeof(*t));
355
0
}