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