aom_cx_set_ref.c 14.2 KB
Newer Older
1
/*
Yaowu Xu's avatar
Yaowu Xu committed
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
Yaowu Xu's avatar
Yaowu Xu committed
4
5
6
7
8
9
 * 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.
10
11
 */

Yaowu Xu's avatar
Yaowu Xu committed
12
// AV1 Set Reference Frame
13
14
// ============================
//
Yaowu Xu's avatar
Yaowu Xu committed
15
// This is an example demonstrating how to overwrite the AV1 encoder's
16
17
18
19
20
21
22
23
24
25
26
27
// internal reference frame. In the sample we set the last frame to the
// current frame. This technique could be used to bounce between two cameras.
//
// The decoder would also have to set the reference frame to the same value
// on the same frame, or the video will become corrupt. The 'test_decode'
// variable is set to 1 in this example that tests if the encoder and decoder
// results are matching.
//
// Usage
// -----
// This example encodes a raw video. And the last argument passed in specifies
// the frame number to update the reference frame on. For example, run
Yaowu Xu's avatar
Yaowu Xu committed
28
// examples/aom_cx_set_ref av1 352 288 in.yuv out.ivf 4 30
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
// The parameter is parsed as follows:
//
//
// Extra Variables
// ---------------
// This example maintains the frame number passed on the command line
// in the `update_frame_num` variable.
//
//
// Configuration
// -------------
//
// The reference frame is updated on the frame specified on the command
// line.
//
// Observing The Effects
// ---------------------
// The encoder and decoder results should be matching when the same reference
// frame setting operation is done in both encoder and decoder. Otherwise,
// the encoder/decoder mismatch would be seen.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Yaowu Xu's avatar
Yaowu Xu committed
54
55
56
#include "aom/aomcx.h"
#include "aom/aom_decoder.h"
#include "aom/aom_encoder.h"
57
58
59
60
61
62
63

#include "./tools_common.h"
#include "./video_writer.h"

static const char *exec_name;

void usage_exit() {
clang-format's avatar
clang-format committed
64
65
  fprintf(stderr,
          "Usage: %s <codec> <width> <height> <infile> <outfile> "
66
67
68
69
70
          "<frame> <limit(optional)>\n",
          exec_name);
  exit(EXIT_FAILURE);
}

Yaowu Xu's avatar
Yaowu Xu committed
71
72
static int compare_img(const aom_image_t *const img1,
                       const aom_image_t *const img2) {
73
  uint32_t l_w = img1->d_w;
clang-format's avatar
clang-format committed
74
  uint32_t c_w = (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
75
76
77
78
79
80
81
82
83
84
  const uint32_t c_h =
      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
  uint32_t i;
  int match = 1;

  match &= (img1->fmt == img2->fmt);
  match &= (img1->d_w == img2->d_w);
  match &= (img1->d_h == img2->d_h);

  for (i = 0; i < img1->d_h; ++i)
Yaowu Xu's avatar
Yaowu Xu committed
85
86
    match &= (memcmp(img1->planes[AOM_PLANE_Y] + i * img1->stride[AOM_PLANE_Y],
                     img2->planes[AOM_PLANE_Y] + i * img2->stride[AOM_PLANE_Y],
87
88
89
                     l_w) == 0);

  for (i = 0; i < c_h; ++i)
Yaowu Xu's avatar
Yaowu Xu committed
90
91
    match &= (memcmp(img1->planes[AOM_PLANE_U] + i * img1->stride[AOM_PLANE_U],
                     img2->planes[AOM_PLANE_U] + i * img2->stride[AOM_PLANE_U],
92
93
94
                     c_w) == 0);

  for (i = 0; i < c_h; ++i)
Yaowu Xu's avatar
Yaowu Xu committed
95
96
    match &= (memcmp(img1->planes[AOM_PLANE_V] + i * img1->stride[AOM_PLANE_V],
                     img2->planes[AOM_PLANE_V] + i * img2->stride[AOM_PLANE_V],
97
98
99
100
101
                     c_w) == 0);

  return match;
}

clang-format's avatar
clang-format committed
102
#define mmin(a, b) ((a) < (b) ? (a) : (b))
Yaowu Xu's avatar
Yaowu Xu committed
103
104
static void find_mismatch(const aom_image_t *const img1,
                          const aom_image_t *const img2, int yloc[4],
clang-format's avatar
clang-format committed
105
                          int uloc[4], int vloc[4]) {
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  const uint32_t bsize = 64;
  const uint32_t bsizey = bsize >> img1->y_chroma_shift;
  const uint32_t bsizex = bsize >> img1->x_chroma_shift;
  const uint32_t c_w =
      (img1->d_w + img1->x_chroma_shift) >> img1->x_chroma_shift;
  const uint32_t c_h =
      (img1->d_h + img1->y_chroma_shift) >> img1->y_chroma_shift;
  int match = 1;
  uint32_t i, j;
  yloc[0] = yloc[1] = yloc[2] = yloc[3] = -1;
  for (i = 0, match = 1; match && i < img1->d_h; i += bsize) {
    for (j = 0; match && j < img1->d_w; j += bsize) {
      int k, l;
      const int si = mmin(i + bsize, img1->d_h) - i;
      const int sj = mmin(j + bsize, img1->d_w) - j;
      for (k = 0; match && k < si; ++k) {
        for (l = 0; match && l < sj; ++l) {
Yaowu Xu's avatar
Yaowu Xu committed
123
124
125
126
          if (*(img1->planes[AOM_PLANE_Y] +
                (i + k) * img1->stride[AOM_PLANE_Y] + j + l) !=
              *(img2->planes[AOM_PLANE_Y] +
                (i + k) * img2->stride[AOM_PLANE_Y] + j + l)) {
127
128
            yloc[0] = i + k;
            yloc[1] = j + l;
Yaowu Xu's avatar
Yaowu Xu committed
129
130
131
132
            yloc[2] = *(img1->planes[AOM_PLANE_Y] +
                        (i + k) * img1->stride[AOM_PLANE_Y] + j + l);
            yloc[3] = *(img2->planes[AOM_PLANE_Y] +
                        (i + k) * img2->stride[AOM_PLANE_Y] + j + l);
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
            match = 0;
            break;
          }
        }
      }
    }
  }

  uloc[0] = uloc[1] = uloc[2] = uloc[3] = -1;
  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
    for (j = 0; match && j < c_w; j += bsizex) {
      int k, l;
      const int si = mmin(i + bsizey, c_h - i);
      const int sj = mmin(j + bsizex, c_w - j);
      for (k = 0; match && k < si; ++k) {
        for (l = 0; match && l < sj; ++l) {
Yaowu Xu's avatar
Yaowu Xu committed
149
150
151
152
          if (*(img1->planes[AOM_PLANE_U] +
                (i + k) * img1->stride[AOM_PLANE_U] + j + l) !=
              *(img2->planes[AOM_PLANE_U] +
                (i + k) * img2->stride[AOM_PLANE_U] + j + l)) {
153
154
            uloc[0] = i + k;
            uloc[1] = j + l;
Yaowu Xu's avatar
Yaowu Xu committed
155
156
157
158
            uloc[2] = *(img1->planes[AOM_PLANE_U] +
                        (i + k) * img1->stride[AOM_PLANE_U] + j + l);
            uloc[3] = *(img2->planes[AOM_PLANE_U] +
                        (i + k) * img2->stride[AOM_PLANE_U] + j + l);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
            match = 0;
            break;
          }
        }
      }
    }
  }
  vloc[0] = vloc[1] = vloc[2] = vloc[3] = -1;
  for (i = 0, match = 1; match && i < c_h; i += bsizey) {
    for (j = 0; match && j < c_w; j += bsizex) {
      int k, l;
      const int si = mmin(i + bsizey, c_h - i);
      const int sj = mmin(j + bsizex, c_w - j);
      for (k = 0; match && k < si; ++k) {
        for (l = 0; match && l < sj; ++l) {
Yaowu Xu's avatar
Yaowu Xu committed
174
175
176
177
          if (*(img1->planes[AOM_PLANE_V] +
                (i + k) * img1->stride[AOM_PLANE_V] + j + l) !=
              *(img2->planes[AOM_PLANE_V] +
                (i + k) * img2->stride[AOM_PLANE_V] + j + l)) {
178
179
            vloc[0] = i + k;
            vloc[1] = j + l;
Yaowu Xu's avatar
Yaowu Xu committed
180
181
182
183
            vloc[2] = *(img1->planes[AOM_PLANE_V] +
                        (i + k) * img1->stride[AOM_PLANE_V] + j + l);
            vloc[3] = *(img2->planes[AOM_PLANE_V] +
                        (i + k) * img2->stride[AOM_PLANE_V] + j + l);
184
185
186
187
188
189
190
191
192
            match = 0;
            break;
          }
        }
      }
    }
  }
}

Yaowu Xu's avatar
Yaowu Xu committed
193
194
static void testing_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
                           aom_codec_enc_cfg_t *cfg, unsigned int frame_out,
195
                           int *mismatch_seen) {
Yaowu Xu's avatar
Yaowu Xu committed
196
197
  aom_image_t enc_img, dec_img;
  struct av1_ref_frame ref_enc, ref_dec;
198

clang-format's avatar
clang-format committed
199
  if (*mismatch_seen) return;
200
201
202

  ref_enc.idx = 0;
  ref_dec.idx = 0;
Yaowu Xu's avatar
Yaowu Xu committed
203
  if (aom_codec_control(encoder, AV1_GET_REFERENCE, &ref_enc))
clang-format's avatar
clang-format committed
204
    die_codec(encoder, "Failed to get encoder reference frame");
205
  enc_img = ref_enc.img;
Yaowu Xu's avatar
Yaowu Xu committed
206
  if (aom_codec_control(decoder, AV1_GET_REFERENCE, &ref_dec))
207
208
209
210
211
212
213
214
215
    die_codec(decoder, "Failed to get decoder reference frame");
  dec_img = ref_dec.img;

  if (!compare_img(&enc_img, &dec_img)) {
    int y[4], u[4], v[4];

    *mismatch_seen = 1;

    find_mismatch(&enc_img, &dec_img, y, u, v);
clang-format's avatar
clang-format committed
216
217
218
219
220
221
222
    printf(
        "Encode/decode mismatch on frame %d at"
        " Y[%d, %d] {%d/%d},"
        " U[%d, %d] {%d/%d},"
        " V[%d, %d] {%d/%d}",
        frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1],
        v[2], v[3]);
223
224
  }

Yaowu Xu's avatar
Yaowu Xu committed
225
226
  aom_img_free(&enc_img);
  aom_img_free(&dec_img);
227
228
}

Yaowu Xu's avatar
Yaowu Xu committed
229
230
231
232
static int encode_frame(aom_codec_ctx_t *ecodec, aom_codec_enc_cfg_t *cfg,
                        aom_image_t *img, unsigned int frame_in,
                        AvxVideoWriter *writer, int test_decode,
                        aom_codec_ctx_t *dcodec, unsigned int *frame_out,
233
234
                        int *mismatch_seen) {
  int got_pkts = 0;
Yaowu Xu's avatar
Yaowu Xu committed
235
236
  aom_codec_iter_t iter = NULL;
  const aom_codec_cx_pkt_t *pkt = NULL;
237
  int got_data;
Yaowu Xu's avatar
Yaowu Xu committed
238
239
240
  const aom_codec_err_t res =
      aom_codec_encode(ecodec, img, frame_in, 1, 0, AOM_DL_GOOD_QUALITY);
  if (res != AOM_CODEC_OK) die_codec(ecodec, "Failed to encode frame");
241
242
243

  got_data = 0;

Yaowu Xu's avatar
Yaowu Xu committed
244
  while ((pkt = aom_codec_get_cx_data(ecodec, &iter)) != NULL) {
245
246
    got_pkts = 1;

Yaowu Xu's avatar
Yaowu Xu committed
247
248
    if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
      const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
249

Yaowu Xu's avatar
Yaowu Xu committed
250
      if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) {
clang-format's avatar
clang-format committed
251
252
        *frame_out += 1;
      }
253

Yaowu Xu's avatar
Yaowu Xu committed
254
      if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
255
256
257
258
259
260
261
262
263
264
                                        pkt->data.frame.sz,
                                        pkt->data.frame.pts)) {
        die_codec(ecodec, "Failed to write compressed frame");
      }
      printf(keyframe ? "K" : ".");
      fflush(stdout);
      got_data = 1;

      // Decode 1 frame.
      if (test_decode) {
Yaowu Xu's avatar
Yaowu Xu committed
265
        if (aom_codec_decode(dcodec, pkt->data.frame.buf,
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
                             (unsigned int)pkt->data.frame.sz, NULL, 0))
          die_codec(dcodec, "Failed to decode frame.");
      }
    }
  }

  // Mismatch checking
  if (got_data && test_decode) {
    testing_decode(ecodec, dcodec, cfg, *frame_out, mismatch_seen);
  }

  return got_pkts;
}

int main(int argc, char **argv) {
  FILE *infile = NULL;
  // Encoder
Yaowu Xu's avatar
Yaowu Xu committed
283
284
  aom_codec_ctx_t ecodec = { 0 };
  aom_codec_enc_cfg_t cfg = { 0 };
285
  unsigned int frame_in = 0;
Yaowu Xu's avatar
Yaowu Xu committed
286
287
288
289
290
  aom_image_t raw;
  aom_codec_err_t res;
  AvxVideoInfo info = { 0 };
  AvxVideoWriter *writer = NULL;
  const AvxInterface *encoder = NULL;
291
292
293
294

  // Test encoder/decoder mismatch.
  int test_decode = 1;
  // Decoder
Yaowu Xu's avatar
Yaowu Xu committed
295
  aom_codec_ctx_t dcodec;
296
297
298
  unsigned int frame_out = 0;

  // The frame number to set reference frame on
299
  unsigned int update_frame_num = 0;
300
301
302
303
304
305
306
307
308
309
  int mismatch_seen = 0;

  const int fps = 30;
  const int bitrate = 500;

  const char *codec_arg = NULL;
  const char *width_arg = NULL;
  const char *height_arg = NULL;
  const char *infile_arg = NULL;
  const char *outfile_arg = NULL;
310
  unsigned int limit = 0;
311
312
  exec_name = argv[0];

clang-format's avatar
clang-format committed
313
  if (argc < 7) die("Invalid number of arguments");
314
315
316
317
318
319
320

  codec_arg = argv[1];
  width_arg = argv[2];
  height_arg = argv[3];
  infile_arg = argv[4];
  outfile_arg = argv[5];

Yaowu Xu's avatar
Yaowu Xu committed
321
  encoder = get_aom_encoder_by_name(codec_arg);
clang-format's avatar
clang-format committed
322
  if (!encoder) die("Unsupported codec.");
323
324

  update_frame_num = atoi(argv[6]);
Yaowu Xu's avatar
Yaowu Xu committed
325
326
  // In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are
  // allocated while calling aom_codec_encode(), thus, setting reference for
327
  // 1st frame isn't supported.
clang-format's avatar
clang-format committed
328
  if (update_frame_num <= 1) die("Couldn't parse frame number '%s'\n", argv[6]);
329
330
331
332
333
334
335
336
337
338
339
340
341

  if (argc > 7) {
    limit = atoi(argv[7]);
    if (update_frame_num > limit)
      die("Update frame number couldn't larger than limit\n");
  }

  info.codec_fourcc = encoder->fourcc;
  info.frame_width = strtol(width_arg, NULL, 0);
  info.frame_height = strtol(height_arg, NULL, 0);
  info.time_base.numerator = 1;
  info.time_base.denominator = fps;

clang-format's avatar
clang-format committed
342
343
  if (info.frame_width <= 0 || info.frame_height <= 0 ||
      (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) {
344
345
346
    die("Invalid frame size: %dx%d", info.frame_width, info.frame_height);
  }

Yaowu Xu's avatar
Yaowu Xu committed
347
  if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width,
clang-format's avatar
clang-format committed
348
                     info.frame_height, 1)) {
349
350
351
    die("Failed to allocate image.");
  }

Yaowu Xu's avatar
Yaowu Xu committed
352
  printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
353

Yaowu Xu's avatar
Yaowu Xu committed
354
  res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
clang-format's avatar
clang-format committed
355
  if (res) die_codec(&ecodec, "Failed to get default codec config.");
356
357
358
359
360
361
362
363

  cfg.g_w = info.frame_width;
  cfg.g_h = info.frame_height;
  cfg.g_timebase.num = info.time_base.numerator;
  cfg.g_timebase.den = info.time_base.denominator;
  cfg.rc_target_bitrate = bitrate;
  cfg.g_lag_in_frames = 3;

Yaowu Xu's avatar
Yaowu Xu committed
364
  writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info);
clang-format's avatar
clang-format committed
365
  if (!writer) die("Failed to open %s for writing.", outfile_arg);
366
367
368
369

  if (!(infile = fopen(infile_arg, "rb")))
    die("Failed to open %s for reading.", infile_arg);

Yaowu Xu's avatar
Yaowu Xu committed
370
  if (aom_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, 0))
371
372
373
    die_codec(&ecodec, "Failed to initialize encoder");

  // Disable alt_ref.
Yaowu Xu's avatar
Yaowu Xu committed
374
  if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0))
375
376
377
    die_codec(&ecodec, "Failed to set enable auto alt ref");

  if (test_decode) {
Yaowu Xu's avatar
Yaowu Xu committed
378
379
    const AvxInterface *decoder = get_aom_decoder_by_name(codec_arg);
    if (aom_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0))
clang-format's avatar
clang-format committed
380
      die_codec(&dcodec, "Failed to initialize decoder.");
381
382
383
  }

  // Encode frames.
Yaowu Xu's avatar
Yaowu Xu committed
384
  while (aom_img_read(&raw, infile)) {
clang-format's avatar
clang-format committed
385
    if (limit && frame_in >= limit) break;
386
    if (update_frame_num > 1 && frame_out + 1 == update_frame_num) {
Yaowu Xu's avatar
Yaowu Xu committed
387
388
      aom_ref_frame_t ref;
      ref.frame_type = AOM_LAST_FRAME;
389
390
      ref.img = raw;
      // Set reference frame in encoder.
Yaowu Xu's avatar
Yaowu Xu committed
391
      if (aom_codec_control(&ecodec, AOM_SET_REFERENCE, &ref))
392
393
394
395
396
397
        die_codec(&ecodec, "Failed to set reference frame");
      printf(" <SET_REF>");

      // If set_reference in decoder is commented out, the enc/dec mismatch
      // would be seen.
      if (test_decode) {
Yaowu Xu's avatar
Yaowu Xu committed
398
        if (aom_codec_control(&dcodec, AOM_SET_REFERENCE, &ref))
399
400
401
402
          die_codec(&dcodec, "Failed to set reference frame");
      }
    }

clang-format's avatar
clang-format committed
403
404
    encode_frame(&ecodec, &cfg, &raw, frame_in, writer, test_decode, &dcodec,
                 &frame_out, &mismatch_seen);
405
    frame_in++;
clang-format's avatar
clang-format committed
406
    if (mismatch_seen) break;
407
408
409
410
411
  }

  // Flush encoder.
  if (!mismatch_seen)
    while (encode_frame(&ecodec, &cfg, NULL, frame_in, writer, test_decode,
clang-format's avatar
clang-format committed
412
413
                        &dcodec, &frame_out, &mismatch_seen)) {
    }
414
415
416
417
418
419
420
421
422
423
424
425
426

  printf("\n");
  fclose(infile);
  printf("Processed %d frames.\n", frame_out);

  if (test_decode) {
    if (!mismatch_seen)
      printf("Encoder/decoder results are matching.\n");
    else
      printf("Encoder/decoder results are NOT matching.\n");
  }

  if (test_decode)
Yaowu Xu's avatar
Yaowu Xu committed
427
    if (aom_codec_destroy(&dcodec))
428
429
      die_codec(&dcodec, "Failed to destroy decoder");

Yaowu Xu's avatar
Yaowu Xu committed
430
431
  aom_img_free(&raw);
  if (aom_codec_destroy(&ecodec))
432
433
    die_codec(&ecodec, "Failed to destroy encoder.");

Yaowu Xu's avatar
Yaowu Xu committed
434
  aom_video_writer_close(writer);
435
436
437

  return EXIT_SUCCESS;
}