Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 1 | /* |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 3 | * |
Yaowu Xu | 9c01aa1 | 2016-09-01 14:32:49 -0700 | [diff] [blame] | 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. |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 12 | // AV1 Set Reference Frame |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 13 | // ============================ |
| 14 | // |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 15 | // This is an example demonstrating how to overwrite the AV1 encoder's |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 16 | // internal reference frame. In the sample we set the last frame to the |
| 17 | // current frame. This technique could be used to bounce between two cameras. |
| 18 | // |
| 19 | // The decoder would also have to set the reference frame to the same value |
| 20 | // on the same frame, or the video will become corrupt. The 'test_decode' |
| 21 | // variable is set to 1 in this example that tests if the encoder and decoder |
| 22 | // results are matching. |
| 23 | // |
| 24 | // Usage |
| 25 | // ----- |
| 26 | // This example encodes a raw video. And the last argument passed in specifies |
| 27 | // the frame number to update the reference frame on. For example, run |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 28 | // examples/aom_cx_set_ref av1 352 288 in.yuv out.ivf 4 30 |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 29 | // The parameter is parsed as follows: |
| 30 | // |
| 31 | // |
| 32 | // Extra Variables |
| 33 | // --------------- |
| 34 | // This example maintains the frame number passed on the command line |
| 35 | // in the `update_frame_num` variable. |
| 36 | // |
| 37 | // |
| 38 | // Configuration |
| 39 | // ------------- |
| 40 | // |
| 41 | // The reference frame is updated on the frame specified on the command |
| 42 | // line. |
| 43 | // |
| 44 | // Observing The Effects |
| 45 | // --------------------- |
| 46 | // The encoder and decoder results should be matching when the same reference |
| 47 | // frame setting operation is done in both encoder and decoder. Otherwise, |
| 48 | // the encoder/decoder mismatch would be seen. |
| 49 | |
| 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 54 | #include "aom/aomcx.h" |
| 55 | #include "aom/aom_decoder.h" |
| 56 | #include "aom/aom_encoder.h" |
Urvang Joshi | 09c293e | 2017-04-20 17:56:27 -0700 | [diff] [blame] | 57 | #include "examples/encoder_util.h" |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 58 | #include "./tools_common.h" |
| 59 | #include "./video_writer.h" |
| 60 | |
| 61 | static const char *exec_name; |
| 62 | |
| 63 | void usage_exit() { |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 64 | fprintf(stderr, |
| 65 | "Usage: %s <codec> <width> <height> <infile> <outfile> " |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 66 | "<frame> <limit(optional)>\n", |
| 67 | exec_name); |
| 68 | exit(EXIT_FAILURE); |
| 69 | } |
| 70 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 71 | static void testing_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder, |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame^] | 72 | unsigned int frame_out, int *mismatch_seen) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 73 | aom_image_t enc_img, dec_img; |
| 74 | struct av1_ref_frame ref_enc, ref_dec; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 75 | |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 76 | if (*mismatch_seen) return; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 77 | |
| 78 | ref_enc.idx = 0; |
| 79 | ref_dec.idx = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 80 | if (aom_codec_control(encoder, AV1_GET_REFERENCE, &ref_enc)) |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 81 | die_codec(encoder, "Failed to get encoder reference frame"); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 82 | enc_img = ref_enc.img; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 83 | if (aom_codec_control(decoder, AV1_GET_REFERENCE, &ref_dec)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 84 | die_codec(decoder, "Failed to get decoder reference frame"); |
| 85 | dec_img = ref_dec.img; |
| 86 | |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame^] | 87 | if (!aom_compare_img(&enc_img, &dec_img)) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 88 | int y[4], u[4], v[4]; |
| 89 | |
| 90 | *mismatch_seen = 1; |
| 91 | |
Urvang Joshi | 09c293e | 2017-04-20 17:56:27 -0700 | [diff] [blame] | 92 | aom_find_mismatch(&enc_img, &dec_img, y, u, v); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 93 | printf( |
| 94 | "Encode/decode mismatch on frame %d at" |
| 95 | " Y[%d, %d] {%d/%d}," |
| 96 | " U[%d, %d] {%d/%d}," |
| 97 | " V[%d, %d] {%d/%d}", |
| 98 | frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1], |
| 99 | v[2], v[3]); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 102 | aom_img_free(&enc_img); |
| 103 | aom_img_free(&dec_img); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 106 | static int encode_frame(aom_codec_ctx_t *ecodec, aom_image_t *img, |
| 107 | unsigned int frame_in, AvxVideoWriter *writer, |
| 108 | int test_decode, aom_codec_ctx_t *dcodec, |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame^] | 109 | unsigned int *frame_out, int *mismatch_seen) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 110 | int got_pkts = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 111 | aom_codec_iter_t iter = NULL; |
| 112 | const aom_codec_cx_pkt_t *pkt = NULL; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 113 | int got_data; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 114 | const aom_codec_err_t res = |
| 115 | aom_codec_encode(ecodec, img, frame_in, 1, 0, AOM_DL_GOOD_QUALITY); |
| 116 | if (res != AOM_CODEC_OK) die_codec(ecodec, "Failed to encode frame"); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 117 | |
| 118 | got_data = 0; |
| 119 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 120 | while ((pkt = aom_codec_get_cx_data(ecodec, &iter)) != NULL) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 121 | got_pkts = 1; |
| 122 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 123 | if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { |
| 124 | const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 125 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 126 | if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) { |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 127 | *frame_out += 1; |
| 128 | } |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 129 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 130 | if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf, |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 131 | pkt->data.frame.sz, |
| 132 | pkt->data.frame.pts)) { |
| 133 | die_codec(ecodec, "Failed to write compressed frame"); |
| 134 | } |
| 135 | printf(keyframe ? "K" : "."); |
| 136 | fflush(stdout); |
| 137 | got_data = 1; |
| 138 | |
| 139 | // Decode 1 frame. |
| 140 | if (test_decode) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 141 | if (aom_codec_decode(dcodec, pkt->data.frame.buf, |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 142 | (unsigned int)pkt->data.frame.sz, NULL, 0)) |
| 143 | die_codec(dcodec, "Failed to decode frame."); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Mismatch checking |
| 149 | if (got_data && test_decode) { |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame^] | 150 | testing_decode(ecodec, dcodec, *frame_out, mismatch_seen); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return got_pkts; |
| 154 | } |
| 155 | |
| 156 | int main(int argc, char **argv) { |
| 157 | FILE *infile = NULL; |
| 158 | // Encoder |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 159 | aom_codec_ctx_t ecodec; |
| 160 | aom_codec_enc_cfg_t cfg; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 161 | unsigned int frame_in = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 162 | aom_image_t raw; |
| 163 | aom_codec_err_t res; |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 164 | AvxVideoInfo info; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 165 | AvxVideoWriter *writer = NULL; |
| 166 | const AvxInterface *encoder = NULL; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 167 | |
| 168 | // Test encoder/decoder mismatch. |
| 169 | int test_decode = 1; |
| 170 | // Decoder |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 171 | aom_codec_ctx_t dcodec; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 172 | unsigned int frame_out = 0; |
| 173 | |
| 174 | // The frame number to set reference frame on |
Yunqing Wang | 2a5a3f6 | 2016-07-22 17:14:22 -0700 | [diff] [blame] | 175 | unsigned int update_frame_num = 0; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 176 | int mismatch_seen = 0; |
| 177 | |
| 178 | const int fps = 30; |
| 179 | const int bitrate = 500; |
| 180 | |
| 181 | const char *codec_arg = NULL; |
| 182 | const char *width_arg = NULL; |
| 183 | const char *height_arg = NULL; |
| 184 | const char *infile_arg = NULL; |
| 185 | const char *outfile_arg = NULL; |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 186 | const char *update_frame_num_arg = NULL; |
Yunqing Wang | 2a5a3f6 | 2016-07-22 17:14:22 -0700 | [diff] [blame] | 187 | unsigned int limit = 0; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 188 | exec_name = argv[0]; |
| 189 | |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 190 | // Clear explicitly, as simply assigning "{ 0 }" generates |
| 191 | // "missing-field-initializers" warning in some compilers. |
| 192 | memset(&ecodec, 0, sizeof(ecodec)); |
| 193 | memset(&cfg, 0, sizeof(cfg)); |
| 194 | memset(&info, 0, sizeof(info)); |
| 195 | |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 196 | if (argc < 7) die("Invalid number of arguments"); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 197 | |
| 198 | codec_arg = argv[1]; |
| 199 | width_arg = argv[2]; |
| 200 | height_arg = argv[3]; |
| 201 | infile_arg = argv[4]; |
| 202 | outfile_arg = argv[5]; |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 203 | update_frame_num_arg = argv[6]; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 204 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 205 | encoder = get_aom_encoder_by_name(codec_arg); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 206 | if (!encoder) die("Unsupported codec."); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 207 | |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 208 | update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 209 | // In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are |
| 210 | // allocated while calling aom_codec_encode(), thus, setting reference for |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 211 | // 1st frame isn't supported. |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 212 | if (update_frame_num <= 1) { |
| 213 | die("Couldn't parse frame number '%s'\n", update_frame_num_arg); |
| 214 | } |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 215 | |
| 216 | if (argc > 7) { |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 217 | limit = (unsigned int)strtoul(argv[7], NULL, 0); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 218 | if (update_frame_num > limit) |
| 219 | die("Update frame number couldn't larger than limit\n"); |
| 220 | } |
| 221 | |
| 222 | info.codec_fourcc = encoder->fourcc; |
James Zern | 097fef9 | 2017-03-23 00:25:55 -0700 | [diff] [blame] | 223 | info.frame_width = (int)strtol(width_arg, NULL, 0); |
| 224 | info.frame_height = (int)strtol(height_arg, NULL, 0); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 225 | info.time_base.numerator = 1; |
| 226 | info.time_base.denominator = fps; |
| 227 | |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 228 | if (info.frame_width <= 0 || info.frame_height <= 0 || |
| 229 | (info.frame_width % 2) != 0 || (info.frame_height % 2) != 0) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 230 | die("Invalid frame size: %dx%d", info.frame_width, info.frame_height); |
| 231 | } |
| 232 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 233 | if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, info.frame_width, |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 234 | info.frame_height, 1)) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 235 | die("Failed to allocate image."); |
| 236 | } |
| 237 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 238 | printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface())); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 239 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 240 | res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 241 | if (res) die_codec(&ecodec, "Failed to get default codec config."); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 242 | |
| 243 | cfg.g_w = info.frame_width; |
| 244 | cfg.g_h = info.frame_height; |
| 245 | cfg.g_timebase.num = info.time_base.numerator; |
| 246 | cfg.g_timebase.den = info.time_base.denominator; |
| 247 | cfg.rc_target_bitrate = bitrate; |
| 248 | cfg.g_lag_in_frames = 3; |
| 249 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 250 | writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 251 | if (!writer) die("Failed to open %s for writing.", outfile_arg); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 252 | |
| 253 | if (!(infile = fopen(infile_arg, "rb"))) |
| 254 | die("Failed to open %s for reading.", infile_arg); |
| 255 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 256 | if (aom_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, 0)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 257 | die_codec(&ecodec, "Failed to initialize encoder"); |
| 258 | |
| 259 | // Disable alt_ref. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 260 | if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 261 | die_codec(&ecodec, "Failed to set enable auto alt ref"); |
| 262 | |
| 263 | if (test_decode) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 264 | const AvxInterface *decoder = get_aom_decoder_by_name(codec_arg); |
| 265 | if (aom_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0)) |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 266 | die_codec(&dcodec, "Failed to initialize decoder."); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Encode frames. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 270 | while (aom_img_read(&raw, infile)) { |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 271 | if (limit && frame_in >= limit) break; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 272 | if (update_frame_num > 1 && frame_out + 1 == update_frame_num) { |
Thomas Daede | 497d195 | 2017-08-08 17:33:06 -0700 | [diff] [blame] | 273 | av1_ref_frame_t ref; |
| 274 | ref.idx = 0; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 275 | ref.img = raw; |
| 276 | // Set reference frame in encoder. |
Thomas Daede | 497d195 | 2017-08-08 17:33:06 -0700 | [diff] [blame] | 277 | if (aom_codec_control(&ecodec, AV1_SET_REFERENCE, &ref)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 278 | die_codec(&ecodec, "Failed to set reference frame"); |
| 279 | printf(" <SET_REF>"); |
| 280 | |
| 281 | // If set_reference in decoder is commented out, the enc/dec mismatch |
| 282 | // would be seen. |
| 283 | if (test_decode) { |
Thomas Daede | 497d195 | 2017-08-08 17:33:06 -0700 | [diff] [blame] | 284 | if (aom_codec_control(&dcodec, AV1_SET_REFERENCE, &ref)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 285 | die_codec(&dcodec, "Failed to set reference frame"); |
| 286 | } |
| 287 | } |
| 288 | |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 289 | encode_frame(&ecodec, &raw, frame_in, writer, test_decode, &dcodec, |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame^] | 290 | &frame_out, &mismatch_seen); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 291 | frame_in++; |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 292 | if (mismatch_seen) break; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | // Flush encoder. |
| 296 | if (!mismatch_seen) |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 297 | while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec, |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame^] | 298 | &frame_out, &mismatch_seen)) { |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 299 | } |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 300 | |
| 301 | printf("\n"); |
| 302 | fclose(infile); |
| 303 | printf("Processed %d frames.\n", frame_out); |
| 304 | |
| 305 | if (test_decode) { |
| 306 | if (!mismatch_seen) |
| 307 | printf("Encoder/decoder results are matching.\n"); |
| 308 | else |
| 309 | printf("Encoder/decoder results are NOT matching.\n"); |
| 310 | } |
| 311 | |
| 312 | if (test_decode) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 313 | if (aom_codec_destroy(&dcodec)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 314 | die_codec(&dcodec, "Failed to destroy decoder"); |
| 315 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 316 | aom_img_free(&raw); |
| 317 | if (aom_codec_destroy(&ecodec)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 318 | die_codec(&ecodec, "Failed to destroy encoder."); |
| 319 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 320 | aom_video_writer_close(writer); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 321 | |
| 322 | return EXIT_SUCCESS; |
| 323 | } |