summaryrefslogtreecommitdiffstats
path: root/third_party/aom/examples/noise_model.c
blob: 5cc6003b6730df4713303f3ed71da42652f152d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Copyright (c) 2018, Alliance for Open Media. All rights reserved
 *
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
 */

/*!\file
 * \brief This is an sample binary to create noise params from input video.
 *
 * To allow for external denoising applications, this sample binary illustrates
 * how to create a film grain table (film grain params as a function of time)
 * from an input video and its corresponding denoised source.
 *
 * The --output-grain-table file can be passed as input to the encoder (in
 * aomenc this is done through the "--film-grain-table" parameter).
 *
 * As an example, where the input source is an 854x480 yuv420p 8-bit video
 * named "input.854_480.yuv" you would use steps similar to the following:
 *
 * # Run your denoiser (e.g, using hqdn3d filter):
 * ffmpeg -vcodec rawvideo -video_size 854x480 -i input.854_480.yuv \
 *    -vf hqdn3d=5:5:5:5 -vcodec rawvideo -an -f rawvideo \
 *    denoised.854_480.yuv
 *
 * # Model the noise between the denoised version and original source:
 * ./examples/noise_model --fps=25/1 --width=854 --height=480 --i420 \
 *    --input-denoised=denoised.854_480.yuv --input=original.854_480.yuv \
 *    --output-grain-table=film_grain.tbl
 *
 * # Encode with your favorite settings (including the grain table):
 * aomenc --limit=100  --cpu-used=4 --input-bit-depth=8                  \
 *    --i420 -w 854 -h 480 --end-usage=q --cq-level=25 --lag-in-frames=25 \
 *    --auto-alt-ref=2 --bit-depth=8 --film-grain-table=film_grain.tbl \
 *    -o denoised_with_grain_params.ivf denoised.854_480.yuv
 */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "aom/aom_encoder.h"
#include "aom_dsp/aom_dsp_common.h"

#if CONFIG_AV1_DECODER
#include "aom_dsp/grain_synthesis.h"
#endif

#include "aom_dsp/grain_table.h"
#include "aom_dsp/noise_model.h"
#include "aom_dsp/noise_util.h"
#include "aom_mem/aom_mem.h"
#include "common/args.h"
#include "common/tools_common.h"
#include "common/video_writer.h"

static const char *exec_name;

void usage_exit(void) {
  fprintf(stderr,
          "Usage: %s --input=<input> --input-denoised=<denoised> "
          "--output-grain-table=<outfile> "
          "See comments in noise_model.c for more information.\n",
          exec_name);
  exit(EXIT_FAILURE);
}

static const arg_def_t help =
    ARG_DEF(NULL, "help", 0, "Show usage options and exit");
static const arg_def_t width_arg =
    ARG_DEF("w", "width", 1, "Input width (if rawvideo)");
static const arg_def_t height_arg =
    ARG_DEF("h", "height", 1, "Input height (if rawvideo)");
static const arg_def_t skip_frames_arg =
    ARG_DEF("s", "skip-frames", 1, "Number of frames to skip (default = 1)");
static const arg_def_t fps_arg = ARG_DEF(NULL, "fps", 1, "Frame rate");
static const arg_def_t input_arg = ARG_DEF("-i", "input", 1, "Input filename");
static const arg_def_t output_grain_table_arg =
    ARG_DEF("n", "output-grain-table", 1, "Output noise file");
static const arg_def_t input_denoised_arg =
    ARG_DEF("d", "input-denoised", 1, "Input denoised filename (YUV) only");
static const arg_def_t flat_block_finder_arg =
    ARG_DEF("b", "flat-block-finder", 1, "Run the flat block finder");
static const arg_def_t block_size_arg =
    ARG_DEF("b", "block-size", 1, "Block size");
static const arg_def_t bit_depth_arg =
    ARG_DEF(NULL, "bit-depth", 1, "Bit depth of input");
static const arg_def_t use_i420 =
    ARG_DEF(NULL, "i420", 0, "Input file (and denoised) is I420 (default)");
static const arg_def_t use_i422 =
    ARG_DEF(NULL, "i422", 0, "Input file (and denoised) is I422");
static const arg_def_t use_i444 =
    ARG_DEF(NULL, "i444", 0, "Input file (and denoised) is I444");
static const arg_def_t debug_file_arg =
    ARG_DEF(NULL, "debug-file", 1, "File to output debug info");

typedef struct {
  int width;
  int height;
  struct aom_rational fps;
  const char *input;
  const char *input_denoised;
  const char *output_grain_table;
  int img_fmt;
  int block_size;
  int bit_depth;
  int run_flat_block_finder;
  int force_flat_psd;
  int skip_frames;
  const char *debug_file;
} noise_model_args_t;

void parse_args(noise_model_args_t *noise_args, int *argc, char **argv) {
  struct arg arg;
  static const arg_def_t *main_args[] = { &help,
                                          &input_arg,
                                          &fps_arg,
                                          &width_arg,
                                          &height_arg,
                                          &block_size_arg,
                                          &output_grain_table_arg,
                                          &input_denoised_arg,
                                          &use_i420,
                                          &use_i422,
                                          &use_i444,
                                          &debug_file_arg,
                                          NULL };
  for (int argi = *argc + 1; *argv; argi++, argv++) {
    if (arg_match(&arg, &help, argv)) {
      fprintf(stdout, "\nOptions:\n");
      arg_show_usage(stdout, main_args);
      exit(0);
    } else if (arg_match(&arg, &width_arg, argv)) {
      noise_args->width = atoi(arg.val);
    } else if (arg_match(&arg, &height_arg, argv)) {
      noise_args->height = atoi(arg.val);
    } else if (arg_match(&arg, &input_arg, argv)) {
      noise_args->input = arg.val;
    } else if (arg_match(&arg, &input_denoised_arg, argv)) {
      noise_args->input_denoised = arg.val;
    } else if (arg_match(&arg, &output_grain_table_arg, argv)) {
      noise_args->output_grain_table = arg.val;
    } else if (arg_match(&arg, &block_size_arg, argv)) {
      noise_args->block_size = atoi(arg.val);
    } else if (arg_match(&arg, &bit_depth_arg, argv)) {
      noise_args->bit_depth = atoi(arg.val);
    } else if (arg_match(&arg, &flat_block_finder_arg, argv)) {
      noise_args->run_flat_block_finder = atoi(arg.val);
    } else if (arg_match(&arg, &fps_arg, argv)) {
      noise_args->fps = arg_parse_rational(&arg);
    } else if (arg_match(&arg, &use_i420, argv)) {
      noise_args->img_fmt = AOM_IMG_FMT_I420;
    } else if (arg_match(&arg, &use_i422, argv)) {
      noise_args->img_fmt = AOM_IMG_FMT_I422;
    } else if (arg_match(&arg, &use_i444, argv)) {
      noise_args->img_fmt = AOM_IMG_FMT_I444;
    } else if (arg_match(&arg, &skip_frames_arg, argv)) {
      noise_args->skip_frames = atoi(arg.val);
    } else if (arg_match(&arg, &debug_file_arg, argv)) {
      noise_args->debug_file = arg.val;
    } else {
      fprintf(stdout, "Unknown arg: %s\n\nUsage:\n", *argv);
      arg_show_usage(stdout, main_args);
      exit(0);
    }
  }
  if (noise_args->bit_depth > 8) {
    noise_args->img_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
  }
}

#if CONFIG_AV1_DECODER
static void print_variance_y(FILE *debug_file, aom_image_t *raw,
                             aom_image_t *denoised, const uint8_t *flat_blocks,
                             int block_size, aom_film_grain_t *grain) {
  aom_image_t renoised;
  grain->apply_grain = 1;
  grain->random_seed = 7391;
  aom_img_alloc(&renoised, raw->fmt, raw->w, raw->h, 1);

  if (av1_add_film_grain(grain, denoised, &renoised)) {
    fprintf(stderr, "Internal failure in av1_add_film_grain().\n");
    aom_img_free(&renoised);
    return;
  }

  const int num_blocks_w = (raw->w + block_size - 1) / block_size;
  const int num_blocks_h = (raw->h + block_size - 1) / block_size;
  fprintf(debug_file, "x = [");
  for (int by = 0; by < num_blocks_h; by++) {
    for (int bx = 0; bx < num_blocks_w; bx++) {
      double block_mean = 0;
      double noise_std = 0, noise_mean = 0;
      double renoise_std = 0, renoise_mean = 0;
      for (int yi = 0; yi < block_size; ++yi) {
        const int y = by * block_size + yi;
        for (int xi = 0; xi < block_size; ++xi) {
          const int x = bx * block_size + xi;
          const double noise_v = (raw->planes[0][y * raw->stride[0] + x] -
                                  denoised->planes[0][y * raw->stride[0] + x]);
          noise_mean += noise_v;
          noise_std += noise_v * noise_v;

          block_mean += raw->planes[0][y * raw->stride[0] + x];

          const double renoise_v =
              (renoised.planes[0][y * raw->stride[0] + x] -
               denoised->planes[0][y * raw->stride[0] + x]);
          renoise_mean += renoise_v;
          renoise_std += renoise_v * renoise_v;
        }
      }
      int n = (block_size * block_size);
      block_mean /= n;
      noise_mean /= n;
      renoise_mean /= n;
      noise_std = sqrt(noise_std / n - noise_mean * noise_mean);
      renoise_std = sqrt(renoise_std / n - renoise_mean * renoise_mean);
      fprintf(debug_file, "%d %3.2lf %3.2lf %3.2lf  ",
              flat_blocks[by * num_blocks_w + bx], block_mean, noise_std,
              renoise_std);
    }
    fprintf(debug_file, "\n");
  }
  fprintf(debug_file, "];\n");

  if (raw->fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
    fprintf(stderr,
            "Detailed debug info not supported for high bit"
            "depth formats\n");
  } else {
    fprintf(debug_file, "figure(2); clf;\n");
    fprintf(debug_file,
            "scatter(x(:, 2:4:end), x(:, 3:4:end), 'r'); hold on;\n");
    fprintf(debug_file, "scatter(x(:, 2:4:end), x(:, 4:4:end), 'b');\n");
    fprintf(debug_file,
            "plot(linspace(0, 255, length(noise_strength_0)), "
            "noise_strength_0, 'b');\n");
    fprintf(debug_file,
            "title('Scatter plot of intensity vs noise strength');\n");
    fprintf(debug_file,
            "legend('Actual', 'Estimated', 'Estimated strength');\n");
    fprintf(debug_file, "figure(3); clf;\n");
    fprintf(debug_file, "scatter(x(:, 3:4:end), x(:, 4:4:end), 'k');\n");
    fprintf(debug_file, "title('Actual vs Estimated');\n");
    fprintf(debug_file, "pause(3);\n");
  }
  aom_img_free(&renoised);
}
#endif

static void print_debug_info(FILE *debug_file, aom_image_t *raw,
                             aom_image_t *denoised, uint8_t *flat_blocks,
                             int block_size, aom_noise_model_t *noise_model) {
  (void)raw;
  (void)denoised;
  (void)flat_blocks;
  (void)block_size;
  fprintf(debug_file, "figure(3); clf;\n");
  fprintf(debug_file, "figure(2); clf;\n");
  fprintf(debug_file, "figure(1); clf;\n");
  for (int c = 0; c < 3; ++c) {
    fprintf(debug_file, "noise_strength_%d = [\n", c);
    const aom_equation_system_t *eqns =
        &noise_model->combined_state[c].strength_solver.eqns;
    for (int k = 0; k < eqns->n; ++k) {
      fprintf(debug_file, "%lf ", eqns->x[k]);
    }
    fprintf(debug_file, "];\n");
    fprintf(debug_file, "plot(noise_strength_%d); hold on;\n", c);
  }
  fprintf(debug_file, "legend('Y', 'cb', 'cr');\n");
  fprintf(debug_file, "title('Noise strength function');\n");

#if CONFIG_AV1_DECODER
  aom_film_grain_t grain;
  aom_noise_model_get_grain_parameters(noise_model, &grain);
  print_variance_y(debug_file, raw, denoised, flat_blocks, block_size, &grain);
#endif
  fflush(debug_file);
}

int main(int argc, char *argv[]) {
  noise_model_args_t args = { 0,  0, { 25, 1 }, 0, 0, 0,   AOM_IMG_FMT_I420,
                              32, 8, 1,         0, 1, NULL };
  aom_image_t raw, denoised;
  FILE *infile = NULL;
  AvxVideoInfo info;

  memset(&info, 0, sizeof(info));

  exec_name = argv[0];
  parse_args(&args, &argc, argv + 1);

  info.frame_width = args.width;
  info.frame_height = args.height;
  info.time_base.numerator = args.fps.den;
  info.time_base.denominator = args.fps.num;

  if (info.frame_width <= 0 || info.frame_height <= 0 ||
      (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
    die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
  }
  if (!aom_img_alloc(&raw, args.img_fmt, info.frame_width, info.frame_height,
                     1)) {
    die("Failed to allocate image.");
  }
  if (!aom_img_alloc(&denoised, args.img_fmt, info.frame_width,
                     info.frame_height, 1)) {
    die("Failed to allocate image.");
  }
  infile = fopen(args.input, "r");
  if (!infile) {
    die("Failed to open input file:", args.input);
  }
  fprintf(stderr, "Bit depth: %d  stride:%d\n", args.bit_depth, raw.stride[0]);

  const int high_bd = args.bit_depth > 8;
  const int block_size = args.block_size;
  aom_flat_block_finder_t block_finder;
  aom_flat_block_finder_init(&block_finder, block_size, args.bit_depth,
                             high_bd);

  const int num_blocks_w = (info.frame_width + block_size - 1) / block_size;
  const int num_blocks_h = (info.frame_height + block_size - 1) / block_size;
  uint8_t *flat_blocks = (uint8_t *)aom_malloc(num_blocks_w * num_blocks_h);
  // Sets the random seed on the first entry in the output table
  int16_t random_seed = 7391;
  aom_noise_model_t noise_model;
  aom_noise_model_params_t params = { AOM_NOISE_SHAPE_SQUARE, 3, args.bit_depth,
                                      high_bd };
  aom_noise_model_init(&noise_model, params);

  FILE *denoised_file = 0;
  if (args.input_denoised) {
    denoised_file = fopen(args.input_denoised, "rb");
    if (!denoised_file)
      die("Unable to open input_denoised: %s", args.input_denoised);
  } else {
    die("--input-denoised file must be specified");
  }
  FILE *debug_file = 0;
  if (args.debug_file) {
    debug_file = fopen(args.debug_file, "w");
  }
  aom_film_grain_table_t grain_table = { 0, 0 };

  int64_t prev_timestamp = 0;
  int frame_count = 0;
  while (aom_img_read(&raw, infile)) {
    if (args.input_denoised) {
      if (!aom_img_read(&denoised, denoised_file)) {
        die("Unable to read input denoised file");
      }
    }
    if (frame_count % args.skip_frames == 0) {
      int num_flat_blocks = num_blocks_w * num_blocks_h;
      memset(flat_blocks, 1, num_flat_blocks);
      if (args.run_flat_block_finder) {
        memset(flat_blocks, 0, num_flat_blocks);
        num_flat_blocks = aom_flat_block_finder_run(
            &block_finder, raw.planes[0], info.frame_width, info.frame_height,
            info.frame_width, flat_blocks);
        fprintf(stdout, "Num flat blocks %d\n", num_flat_blocks);
      }

      const uint8_t *planes[3] = { raw.planes[0], raw.planes[1],
                                   raw.planes[2] };
      uint8_t *denoised_planes[3] = { denoised.planes[0], denoised.planes[1],
                                      denoised.planes[2] };
      int strides[3] = { raw.stride[0] >> high_bd, raw.stride[1] >> high_bd,
                         raw.stride[2] >> high_bd };
      int chroma_sub[3] = { raw.x_chroma_shift, raw.y_chroma_shift, 0 };

      fprintf(stdout, "Updating noise model...\n");
      aom_noise_status_t status = aom_noise_model_update(
          &noise_model, (const uint8_t *const *)planes,
          (const uint8_t *const *)denoised_planes, info.frame_width,
          info.frame_height, strides, chroma_sub, flat_blocks, block_size);

      int64_t cur_timestamp =
          frame_count * 10000000ULL * args.fps.den / args.fps.num;
      if (status == AOM_NOISE_STATUS_DIFFERENT_NOISE_TYPE) {
        fprintf(stdout,
                "Noise type is different, updating parameters for time "
                "[ %" PRId64 ", %" PRId64 ")\n",
                prev_timestamp, cur_timestamp);
        aom_film_grain_t grain;
        aom_noise_model_get_grain_parameters(&noise_model, &grain);
        grain.random_seed = random_seed;
        random_seed = 0;
        aom_film_grain_table_append(&grain_table, prev_timestamp, cur_timestamp,
                                    &grain);
        aom_noise_model_save_latest(&noise_model);
        prev_timestamp = cur_timestamp;
      }
      if (debug_file) {
        print_debug_info(debug_file, &raw, &denoised, flat_blocks, block_size,
                         &noise_model);
      }
      fprintf(stdout, "Done noise model update, status = %d\n", status);
    }
    frame_count++;
  }

  aom_film_grain_t grain;
  aom_noise_model_get_grain_parameters(&noise_model, &grain);
  grain.random_seed = random_seed;
  aom_film_grain_table_append(&grain_table, prev_timestamp, INT64_MAX, &grain);
  if (args.output_grain_table) {
    struct aom_internal_error_info error_info;
    if (AOM_CODEC_OK != aom_film_grain_table_write(&grain_table,
                                                   args.output_grain_table,
                                                   &error_info)) {
      die("Unable to write output film grain table");
    }
  }
  aom_film_grain_table_free(&grain_table);

  if (infile) fclose(infile);
  if (denoised_file) fclose(denoised_file);
  if (debug_file) fclose(debug_file);
  aom_img_free(&raw);
  aom_img_free(&denoised);

  return EXIT_SUCCESS;
}