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 | |
Yunqing Wang | ff07a12 | 2018-04-17 16:02:34 -0700 | [diff] [blame] | 61 | #define AOM_BORDER_IN_PIXELS 288 |
| 62 | |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 63 | static const char *exec_name; |
| 64 | |
| 65 | void usage_exit() { |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 66 | fprintf(stderr, |
| 67 | "Usage: %s <codec> <width> <height> <infile> <outfile> " |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 68 | "<frame> <limit(optional)>\n", |
| 69 | exec_name); |
| 70 | exit(EXIT_FAILURE); |
| 71 | } |
| 72 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 73 | 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] | 74 | unsigned int frame_out, int *mismatch_seen) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 75 | aom_image_t enc_img, dec_img; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 76 | |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 77 | if (*mismatch_seen) return; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 78 | |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 79 | /* Get the internal reference frame */ |
| 80 | if (aom_codec_control(encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img)) |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 81 | die_codec(encoder, "Failed to get encoder reference frame"); |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 82 | if (aom_codec_control(decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 83 | die_codec(decoder, "Failed to get decoder reference frame"); |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 84 | |
| 85 | if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) != |
| 86 | (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) { |
| 87 | if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) { |
| 88 | aom_image_t enc_hbd_img; |
| 89 | aom_img_alloc(&enc_hbd_img, enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH, |
| 90 | enc_img.d_w, enc_img.d_h, 16); |
| 91 | aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img); |
| 92 | enc_img = enc_hbd_img; |
| 93 | } |
| 94 | if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) { |
| 95 | aom_image_t dec_hbd_img; |
| 96 | aom_img_alloc(&dec_hbd_img, dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH, |
| 97 | dec_img.d_w, dec_img.d_h, 16); |
| 98 | aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img); |
| 99 | dec_img = dec_hbd_img; |
| 100 | } |
| 101 | } |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 102 | |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame] | 103 | if (!aom_compare_img(&enc_img, &dec_img)) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 104 | int y[4], u[4], v[4]; |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 105 | if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) { |
| 106 | aom_find_mismatch_high(&enc_img, &dec_img, y, u, v); |
| 107 | } else { |
| 108 | aom_find_mismatch(&enc_img, &dec_img, y, u, v); |
| 109 | } |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 110 | |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 111 | printf( |
| 112 | "Encode/decode mismatch on frame %d at" |
| 113 | " Y[%d, %d] {%d/%d}," |
| 114 | " U[%d, %d] {%d/%d}," |
| 115 | " V[%d, %d] {%d/%d}", |
| 116 | frame_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0], v[1], |
| 117 | v[2], v[3]); |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 118 | *mismatch_seen = 1; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 121 | aom_img_free(&enc_img); |
| 122 | aom_img_free(&dec_img); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 125 | static int encode_frame(aom_codec_ctx_t *ecodec, aom_image_t *img, |
| 126 | unsigned int frame_in, AvxVideoWriter *writer, |
| 127 | int test_decode, aom_codec_ctx_t *dcodec, |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame] | 128 | unsigned int *frame_out, int *mismatch_seen) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 129 | int got_pkts = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 130 | aom_codec_iter_t iter = NULL; |
| 131 | const aom_codec_cx_pkt_t *pkt = NULL; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 132 | int got_data; |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 133 | const aom_codec_err_t res = aom_codec_encode(ecodec, img, frame_in, 1, 0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 134 | if (res != AOM_CODEC_OK) die_codec(ecodec, "Failed to encode frame"); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 135 | |
| 136 | got_data = 0; |
| 137 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 138 | while ((pkt = aom_codec_get_cx_data(ecodec, &iter)) != NULL) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 139 | got_pkts = 1; |
| 140 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 141 | if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { |
| 142 | const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 143 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 144 | if (!(pkt->data.frame.flags & AOM_FRAME_IS_FRAGMENT)) { |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 145 | *frame_out += 1; |
| 146 | } |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 147 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 148 | if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf, |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 149 | pkt->data.frame.sz, |
| 150 | pkt->data.frame.pts)) { |
| 151 | die_codec(ecodec, "Failed to write compressed frame"); |
| 152 | } |
| 153 | printf(keyframe ? "K" : "."); |
| 154 | fflush(stdout); |
| 155 | got_data = 1; |
| 156 | |
| 157 | // Decode 1 frame. |
| 158 | if (test_decode) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 159 | if (aom_codec_decode(dcodec, pkt->data.frame.buf, |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 160 | (unsigned int)pkt->data.frame.sz, NULL)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 161 | die_codec(dcodec, "Failed to decode frame."); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Mismatch checking |
| 167 | if (got_data && test_decode) { |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame] | 168 | testing_decode(ecodec, dcodec, *frame_out, mismatch_seen); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return got_pkts; |
| 172 | } |
| 173 | |
| 174 | int main(int argc, char **argv) { |
| 175 | FILE *infile = NULL; |
| 176 | // Encoder |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 177 | aom_codec_ctx_t ecodec; |
| 178 | aom_codec_enc_cfg_t cfg; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 179 | unsigned int frame_in = 0; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 180 | aom_image_t raw; |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 181 | aom_image_t ext_ref; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 182 | aom_codec_err_t res; |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 183 | AvxVideoInfo info; |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 184 | AvxVideoWriter *writer = NULL; |
| 185 | const AvxInterface *encoder = NULL; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 186 | |
| 187 | // Test encoder/decoder mismatch. |
| 188 | int test_decode = 1; |
| 189 | // Decoder |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 190 | aom_codec_ctx_t dcodec; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 191 | unsigned int frame_out = 0; |
| 192 | |
| 193 | // The frame number to set reference frame on |
Yunqing Wang | 2a5a3f6 | 2016-07-22 17:14:22 -0700 | [diff] [blame] | 194 | unsigned int update_frame_num = 0; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 195 | int mismatch_seen = 0; |
| 196 | |
| 197 | const int fps = 30; |
| 198 | const int bitrate = 500; |
| 199 | |
| 200 | const char *codec_arg = NULL; |
| 201 | const char *width_arg = NULL; |
| 202 | const char *height_arg = NULL; |
| 203 | const char *infile_arg = NULL; |
| 204 | const char *outfile_arg = NULL; |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 205 | const char *update_frame_num_arg = NULL; |
Yunqing Wang | 2a5a3f6 | 2016-07-22 17:14:22 -0700 | [diff] [blame] | 206 | unsigned int limit = 0; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 207 | exec_name = argv[0]; |
| 208 | |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 209 | // Clear explicitly, as simply assigning "{ 0 }" generates |
| 210 | // "missing-field-initializers" warning in some compilers. |
| 211 | memset(&ecodec, 0, sizeof(ecodec)); |
| 212 | memset(&cfg, 0, sizeof(cfg)); |
| 213 | memset(&info, 0, sizeof(info)); |
| 214 | |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 215 | if (argc < 7) die("Invalid number of arguments"); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 216 | |
| 217 | codec_arg = argv[1]; |
| 218 | width_arg = argv[2]; |
| 219 | height_arg = argv[3]; |
| 220 | infile_arg = argv[4]; |
| 221 | outfile_arg = argv[5]; |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 222 | update_frame_num_arg = argv[6]; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 223 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 224 | encoder = get_aom_encoder_by_name(codec_arg); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 225 | if (!encoder) die("Unsupported codec."); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 226 | |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 227 | update_frame_num = (unsigned int)strtoul(update_frame_num_arg, NULL, 0); |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 228 | // In AV1, the reference buffers (cm->buffer_pool->frame_bufs[i].buf) are |
| 229 | // allocated while calling aom_codec_encode(), thus, setting reference for |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 230 | // 1st frame isn't supported. |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 231 | if (update_frame_num <= 1) { |
| 232 | die("Couldn't parse frame number '%s'\n", update_frame_num_arg); |
| 233 | } |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 234 | |
| 235 | if (argc > 7) { |
Urvang Joshi | d3a7576 | 2016-07-08 16:09:36 -0700 | [diff] [blame] | 236 | limit = (unsigned int)strtoul(argv[7], NULL, 0); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 237 | if (update_frame_num > limit) |
| 238 | die("Update frame number couldn't larger than limit\n"); |
| 239 | } |
| 240 | |
| 241 | info.codec_fourcc = encoder->fourcc; |
James Zern | 097fef9 | 2017-03-23 00:25:55 -0700 | [diff] [blame] | 242 | info.frame_width = (int)strtol(width_arg, NULL, 0); |
| 243 | info.frame_height = (int)strtol(height_arg, NULL, 0); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 244 | info.time_base.numerator = 1; |
| 245 | info.time_base.denominator = fps; |
| 246 | |
Yunqing Wang | ff07a12 | 2018-04-17 16:02:34 -0700 | [diff] [blame] | 247 | if (info.frame_width <= 0 || info.frame_height <= 0) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 248 | die("Invalid frame size: %dx%d", info.frame_width, info.frame_height); |
| 249 | } |
| 250 | |
Yunqing Wang | ff07a12 | 2018-04-17 16:02:34 -0700 | [diff] [blame] | 251 | if (!aom_img_alloc_with_border(&raw, AOM_IMG_FMT_I420, info.frame_width, |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 252 | info.frame_height, 1, 8, 0)) { |
| 253 | die("Failed to allocate image."); |
| 254 | } |
| 255 | // Allocate memory with the border so that it can be used as a reference. |
| 256 | if (!aom_img_alloc_with_border(&ext_ref, AOM_IMG_FMT_I420, info.frame_width, |
Yunqing Wang | ff07a12 | 2018-04-17 16:02:34 -0700 | [diff] [blame] | 257 | info.frame_height, 32, 8, |
| 258 | AOM_BORDER_IN_PIXELS)) { |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 259 | die("Failed to allocate image."); |
| 260 | } |
| 261 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 262 | printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface())); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 263 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 264 | res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 265 | if (res) die_codec(&ecodec, "Failed to get default codec config."); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 266 | |
| 267 | cfg.g_w = info.frame_width; |
| 268 | cfg.g_h = info.frame_height; |
| 269 | cfg.g_timebase.num = info.time_base.numerator; |
| 270 | cfg.g_timebase.den = info.time_base.denominator; |
| 271 | cfg.rc_target_bitrate = bitrate; |
| 272 | cfg.g_lag_in_frames = 3; |
| 273 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 274 | writer = aom_video_writer_open(outfile_arg, kContainerIVF, &info); |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 275 | if (!writer) die("Failed to open %s for writing.", outfile_arg); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 276 | |
| 277 | if (!(infile = fopen(infile_arg, "rb"))) |
| 278 | die("Failed to open %s for reading.", infile_arg); |
| 279 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 280 | if (aom_codec_enc_init(&ecodec, encoder->codec_interface(), &cfg, 0)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 281 | die_codec(&ecodec, "Failed to initialize encoder"); |
| 282 | |
| 283 | // Disable alt_ref. |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 284 | if (aom_codec_control(&ecodec, AOME_SET_ENABLEAUTOALTREF, 0)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 285 | die_codec(&ecodec, "Failed to set enable auto alt ref"); |
| 286 | |
| 287 | if (test_decode) { |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 288 | const AvxInterface *decoder = get_aom_decoder_by_name(codec_arg); |
| 289 | if (aom_codec_dec_init(&dcodec, decoder->codec_interface(), NULL, 0)) |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 290 | die_codec(&dcodec, "Failed to initialize decoder."); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | // Encode frames. |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 294 | aom_image_t *img_frm = &ext_ref; |
| 295 | while (aom_img_read(img_frm, infile)) { |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 296 | if (limit && frame_in >= limit) break; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 297 | if (update_frame_num > 1 && frame_out + 1 == update_frame_num) { |
Thomas Daede | 497d195 | 2017-08-08 17:33:06 -0700 | [diff] [blame] | 298 | av1_ref_frame_t ref; |
| 299 | ref.idx = 0; |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 300 | ref.use_external_ref = 0; |
| 301 | ref.img = *img_frm; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 302 | // Set reference frame in encoder. |
Thomas Daede | 497d195 | 2017-08-08 17:33:06 -0700 | [diff] [blame] | 303 | if (aom_codec_control(&ecodec, AV1_SET_REFERENCE, &ref)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 304 | die_codec(&ecodec, "Failed to set reference frame"); |
| 305 | printf(" <SET_REF>"); |
| 306 | |
| 307 | // If set_reference in decoder is commented out, the enc/dec mismatch |
| 308 | // would be seen. |
| 309 | if (test_decode) { |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 310 | ref.use_external_ref = 1; |
Thomas Daede | 497d195 | 2017-08-08 17:33:06 -0700 | [diff] [blame] | 311 | if (aom_codec_control(&dcodec, AV1_SET_REFERENCE, &ref)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 312 | die_codec(&dcodec, "Failed to set reference frame"); |
| 313 | } |
| 314 | } |
| 315 | |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 316 | encode_frame(&ecodec, img_frm, frame_in, writer, test_decode, &dcodec, |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame] | 317 | &frame_out, &mismatch_seen); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 318 | frame_in++; |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 319 | if (mismatch_seen) break; |
Yunqing Wang | 4450c76 | 2018-04-24 13:49:25 -0700 | [diff] [blame^] | 320 | if (frame_out >= update_frame_num) img_frm = &raw; |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Flush encoder. |
| 324 | if (!mismatch_seen) |
Urvang Joshi | d71a231 | 2016-07-14 12:33:48 -0700 | [diff] [blame] | 325 | while (encode_frame(&ecodec, NULL, frame_in, writer, test_decode, &dcodec, |
Imdad Sardharwalla | b5def02 | 2017-12-14 11:20:24 +0000 | [diff] [blame] | 326 | &frame_out, &mismatch_seen)) { |
clang-format | 397d964 | 2016-08-08 18:51:16 -0700 | [diff] [blame] | 327 | } |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 328 | |
| 329 | printf("\n"); |
| 330 | fclose(infile); |
| 331 | printf("Processed %d frames.\n", frame_out); |
| 332 | |
| 333 | if (test_decode) { |
| 334 | if (!mismatch_seen) |
| 335 | printf("Encoder/decoder results are matching.\n"); |
| 336 | else |
| 337 | printf("Encoder/decoder results are NOT matching.\n"); |
| 338 | } |
| 339 | |
| 340 | if (test_decode) |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 341 | if (aom_codec_destroy(&dcodec)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 342 | die_codec(&dcodec, "Failed to destroy decoder"); |
| 343 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 344 | aom_img_free(&raw); |
| 345 | if (aom_codec_destroy(&ecodec)) |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 346 | die_codec(&ecodec, "Failed to destroy encoder."); |
| 347 | |
Yaowu Xu | f883b42 | 2016-08-30 14:01:10 -0700 | [diff] [blame] | 348 | aom_video_writer_close(writer); |
Yunqing Wang | 9aaa3c9 | 2016-03-25 11:57:20 -0700 | [diff] [blame] | 349 | |
| 350 | return EXIT_SUCCESS; |
| 351 | } |