Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 1 | /* |
Lester Lu | 6bc30d6 | 2021-12-16 19:13:21 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 3 | * |
Lester Lu | 6bc30d6 | 2021-12-16 19:13:21 +0000 | [diff] [blame^] | 4 | * This source code is subject to the terms of the BSD 3-Clause Clear License |
| 5 | * and the Alliance for Open Media Patent License 1.0. If the BSD 3-Clause Clear |
| 6 | * License was not distributed with this source code in the LICENSE file, you |
| 7 | * can obtain it at aomedia.org/license/software-license/bsd-3-c-c/. If the |
| 8 | * Alliance for Open Media Patent License 1.0 was not distributed with this |
| 9 | * source code in the PATENTS file, you can obtain it at |
| 10 | * aomedia.org/license/patent-license/. |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | // Lightfield Encoder |
| 14 | // ================== |
| 15 | // |
| 16 | // This is an example of a simple lightfield encoder. It builds upon the |
| 17 | // twopass_encoder.c example. It takes an input file in YV12 format, |
| 18 | // treating it as a planar lightfield instead of a video. The img_width |
| 19 | // and img_height arguments are the dimensions of the lightfield images, |
| 20 | // while the lf_width and lf_height arguments are the number of |
| 21 | // lightfield images in each dimension. The lf_blocksize determines the |
| 22 | // number of reference images used for MCP. For example, 5 means that there |
| 23 | // is a reference image for every 5x5 lightfield image block. All images |
| 24 | // within a block will use the center image in that block as the reference |
| 25 | // image for MCP. |
| 26 | // Run "make test" to download lightfield test data: vase10x10.yuv. |
| 27 | // Run lightfield encoder to encode whole lightfield: |
Yunqing Wang | d417bb5 | 2018-06-03 14:13:38 -0700 | [diff] [blame] | 28 | // examples/lightfield_encoder 1024 1024 vase10x10.yuv vase10x10.ivf 10 10 5 |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 29 | |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 30 | // Note: In bitstream.c and encoder.c, define EXT_TILE_DEBUG as 1 will print |
| 31 | // out the uncompressed header and the frame contexts, which can be used to |
| 32 | // test the bit exactness of the headers and the frame contexts for large scale |
| 33 | // tile coded frames. |
| 34 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 35 | #include <stdio.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <string.h> |
| 38 | |
| 39 | #include "aom/aom_encoder.h" |
| 40 | #include "aom/aomcx.h" |
Urvang Joshi | 6077b18 | 2018-08-21 11:49:38 -0700 | [diff] [blame] | 41 | #include "aom_scale/yv12config.h" |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 42 | #include "av1/common/enums.h" |
Tom Finegan | 7790213 | 2018-05-21 10:19:15 -0700 | [diff] [blame] | 43 | #include "common/tools_common.h" |
| 44 | #include "common/video_writer.h" |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 45 | |
| 46 | static const char *exec_name; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 47 | |
| 48 | void usage_exit(void) { |
| 49 | fprintf(stderr, |
| 50 | "Usage: %s <img_width> <img_height> <infile> <outfile> " |
| 51 | "<lf_width> <lf_height> <lf_blocksize>\n", |
| 52 | exec_name); |
| 53 | exit(EXIT_FAILURE); |
| 54 | } |
| 55 | |
Yaowu Xu | 413566e | 2019-05-17 09:36:48 -0700 | [diff] [blame] | 56 | static int img_size_bytes(aom_image_t *img) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 57 | int image_size_bytes = 0; |
| 58 | int plane; |
| 59 | for (plane = 0; plane < 3; ++plane) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 60 | const int w = aom_img_plane_width(img, plane) * |
| 61 | ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); |
| 62 | const int h = aom_img_plane_height(img, plane); |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 63 | image_size_bytes += w * h; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 64 | } |
| 65 | return image_size_bytes; |
| 66 | } |
| 67 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 68 | static int encode_frame(aom_codec_ctx_t *ctx, const aom_image_t *img, |
| 69 | aom_codec_pts_t pts, unsigned int duration, |
Yunqing Wang | b97cfae | 2018-05-11 14:19:05 -0700 | [diff] [blame] | 70 | aom_enc_frame_flags_t flags, AvxVideoWriter *writer) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 71 | int got_pkts = 0; |
| 72 | aom_codec_iter_t iter = NULL; |
| 73 | const aom_codec_cx_pkt_t *pkt = NULL; |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 74 | const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 75 | if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to encode frame."); |
| 76 | |
| 77 | while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) { |
| 78 | got_pkts = 1; |
| 79 | if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { |
| 80 | const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0; |
| 81 | |
| 82 | if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf, |
| 83 | pkt->data.frame.sz, |
| 84 | pkt->data.frame.pts)) |
| 85 | die_codec(ctx, "Failed to write compressed frame."); |
| 86 | printf(keyframe ? "K" : "."); |
| 87 | fflush(stdout); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return got_pkts; |
| 92 | } |
| 93 | |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 94 | static void get_raw_image(aom_image_t **frame_to_encode, aom_image_t *raw, |
| 95 | aom_image_t *raw_shift) { |
Ryan Lei | 2a67637 | 2020-09-28 16:58:52 -0700 | [diff] [blame] | 96 | // Need to allocate larger buffer to use hbd internal. |
| 97 | int input_shift = 0; |
| 98 | aom_img_upshift(raw_shift, raw, input_shift); |
| 99 | *frame_to_encode = raw_shift; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 102 | static void pass1(aom_image_t *raw, FILE *infile, const char *outfile_name, |
Elliott Karpilovsky | 67e84d4 | 2020-04-26 16:03:39 -0700 | [diff] [blame] | 103 | aom_codec_iface_t *encoder, aom_codec_enc_cfg_t *cfg, |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 104 | int lf_width, int lf_height, int lf_blocksize, int flags, |
| 105 | aom_image_t *raw_shift) { |
Elliott Karpilovsky | cbe219b | 2020-04-22 16:21:06 -0700 | [diff] [blame] | 106 | AvxVideoInfo info = { get_fourcc_by_aom_encoder(encoder), |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 107 | cfg->g_w, |
| 108 | cfg->g_h, |
Jonathan Matthews | a6e4f96 | 2018-07-13 16:01:28 +0100 | [diff] [blame] | 109 | { cfg->g_timebase.num, cfg->g_timebase.den }, |
| 110 | 0 }; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 111 | AvxVideoWriter *writer = NULL; |
| 112 | aom_codec_ctx_t codec; |
| 113 | int frame_count = 0; |
Yaowu Xu | 413566e | 2019-05-17 09:36:48 -0700 | [diff] [blame] | 114 | int image_size_bytes = img_size_bytes(raw); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 115 | int bu, bv; |
| 116 | int u_blocks, v_blocks; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 117 | aom_image_t *frame_to_encode; |
Yunqing Wang | 93b18f3 | 2018-06-08 21:08:29 -0700 | [diff] [blame] | 118 | aom_image_t reference_images[MAX_EXTERNAL_REFERENCES]; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 119 | int reference_image_num = 0; |
| 120 | int i; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 121 | |
| 122 | writer = aom_video_writer_open(outfile_name, kContainerIVF, &info); |
| 123 | if (!writer) die("Failed to open %s for writing", outfile_name); |
| 124 | |
Elliott Karpilovsky | cbe219b | 2020-04-22 16:21:06 -0700 | [diff] [blame] | 125 | if (aom_codec_enc_init(&codec, encoder, cfg, flags)) |
James Zern | dad374f | 2020-05-06 19:13:19 -0700 | [diff] [blame] | 126 | die("Failed to initialize encoder"); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 127 | if (aom_codec_control(&codec, AOME_SET_ENABLEAUTOALTREF, 0)) |
| 128 | die_codec(&codec, "Failed to turn off auto altref"); |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 129 | if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 0)) |
| 130 | die_codec(&codec, "Failed to set frame parallel decoding"); |
Yunqing Wang | d8fd9e7 | 2019-12-26 15:36:31 -0800 | [diff] [blame] | 131 | if (aom_codec_control(&codec, AV1E_ENABLE_EXT_TILE_DEBUG, 1)) |
| 132 | die_codec(&codec, "Failed to enable encoder ext_tile debug"); |
Yaowu Xu | 8d9fecb | 2020-04-27 15:21:02 -0700 | [diff] [blame] | 133 | if (aom_codec_control(&codec, AOME_SET_CPUUSED, 3)) |
Yunqing Wang | d8fd9e7 | 2019-12-26 15:36:31 -0800 | [diff] [blame] | 134 | die_codec(&codec, "Failed to set cpu-used"); |
Yunqing Wang | d0f0d3b | 2019-12-23 12:15:44 -0800 | [diff] [blame] | 135 | |
Yunqing Wang | e3de2d8 | 2018-05-15 13:43:41 -0700 | [diff] [blame] | 136 | // Note: The superblock is a sequence parameter and has to be the same for 1 |
| 137 | // sequence. In lightfield application, must choose the superblock size(either |
| 138 | // 64x64 or 128x128) before the encoding starts. Otherwise, the default is |
| 139 | // AOM_SUPERBLOCK_SIZE_DYNAMIC, and the superblock size will be set to 64x64 |
| 140 | // internally. |
| 141 | if (aom_codec_control(&codec, AV1E_SET_SUPERBLOCK_SIZE, |
| 142 | AOM_SUPERBLOCK_SIZE_64X64)) |
| 143 | die_codec(&codec, "Failed to set SB size"); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 144 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 145 | u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize; |
| 146 | v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 147 | |
| 148 | reference_image_num = u_blocks * v_blocks; |
Sarah Parker | 9feb158 | 2019-04-25 12:29:52 -0700 | [diff] [blame] | 149 | // Set the max gf group length so the references are guaranteed to be in |
| 150 | // a different gf group than any of the regular frames. This avoids using |
Sarah Parker | 48b46b4 | 2019-04-23 20:43:02 -0700 | [diff] [blame] | 151 | // both vbr and constant quality mode in a single group. The number of |
| 152 | // references now cannot surpass 17 because of the enforced MAX_GF_INTERVAL of |
| 153 | // 16. If it is necessary to exceed this reference frame limit, one will have |
| 154 | // to do some additional handling to ensure references are in separate gf |
| 155 | // groups from the regular frames. |
| 156 | if (aom_codec_control(&codec, AV1E_SET_MAX_GF_INTERVAL, |
| 157 | reference_image_num - 1)) |
| 158 | die_codec(&codec, "Failed to set max gf interval"); |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 159 | aom_img_fmt_t ref_fmt = AOM_IMG_FMT_I420; |
Ryan Lei | 2a67637 | 2020-09-28 16:58:52 -0700 | [diff] [blame] | 160 | ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 161 | // Allocate memory with the border so that it can be used as a reference. |
Satish Kumar Suman | 2990996 | 2019-01-09 10:31:21 +0530 | [diff] [blame] | 162 | int border_in_pixels = |
| 163 | (codec.config.enc->rc_resize_mode || codec.config.enc->rc_superres_mode) |
| 164 | ? AOM_BORDER_IN_PIXELS |
| 165 | : AOM_ENC_NO_SCALE_BORDER; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 166 | for (i = 0; i < reference_image_num; i++) { |
| 167 | if (!aom_img_alloc_with_border(&reference_images[i], ref_fmt, cfg->g_w, |
Satish Kumar Suman | 2990996 | 2019-01-09 10:31:21 +0530 | [diff] [blame] | 168 | cfg->g_h, 32, 8, border_in_pixels)) { |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 169 | die("Failed to allocate image."); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | printf("\n Second pass: "); |
| 174 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 175 | // Encode reference images first. |
| 176 | printf("Encoding Reference Images\n"); |
| 177 | for (bv = 0; bv < v_blocks; ++bv) { |
| 178 | for (bu = 0; bu < u_blocks; ++bu) { |
| 179 | const int block_u_min = bu * lf_blocksize; |
| 180 | const int block_v_min = bv * lf_blocksize; |
| 181 | int block_u_end = (bu + 1) * lf_blocksize; |
| 182 | int block_v_end = (bv + 1) * lf_blocksize; |
| 183 | int u_block_size, v_block_size; |
| 184 | int block_ref_u, block_ref_v; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 185 | |
| 186 | block_u_end = block_u_end < lf_width ? block_u_end : lf_width; |
| 187 | block_v_end = block_v_end < lf_height ? block_v_end : lf_height; |
| 188 | u_block_size = block_u_end - block_u_min; |
| 189 | v_block_size = block_v_end - block_v_min; |
| 190 | block_ref_u = block_u_min + u_block_size / 2; |
| 191 | block_ref_v = block_v_min + v_block_size / 2; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 192 | |
| 193 | printf("A%d, ", (block_ref_u + block_ref_v * lf_width)); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 194 | fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes, |
| 195 | SEEK_SET); |
| 196 | aom_img_read(raw, infile); |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 197 | |
| 198 | get_raw_image(&frame_to_encode, raw, raw_shift); |
| 199 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 200 | // Reference frames may be encoded without tiles. |
| 201 | ++frame_count; |
| 202 | printf("Encoding reference image %d of %d\n", bv * u_blocks + bu, |
| 203 | u_blocks * v_blocks); |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 204 | encode_frame(&codec, frame_to_encode, frame_count, 1, |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 205 | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | |
| 206 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | |
| 207 | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | |
| 208 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | |
| 209 | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY, |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 210 | writer); |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 211 | |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 212 | if (aom_codec_control(&codec, AV1_COPY_NEW_FRAME_IMAGE, |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 213 | &reference_images[frame_count - 1])) |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 214 | die_codec(&codec, "Failed to copy decoder reference frame"); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 218 | cfg->large_scale_tile = 1; |
| 219 | // Fixed q encoding for camera frames. |
| 220 | cfg->rc_end_usage = AOM_Q; |
| 221 | if (aom_codec_enc_config_set(&codec, cfg)) |
| 222 | die_codec(&codec, "Failed to configure encoder"); |
| 223 | |
| 224 | // The fixed q value used in encoding. |
Urvang Joshi | 3d3535b | 2020-07-14 11:01:30 -0700 | [diff] [blame] | 225 | if (aom_codec_control(&codec, AOME_SET_QP, 36)) |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 226 | die_codec(&codec, "Failed to set cq level"); |
| 227 | if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 1)) |
| 228 | die_codec(&codec, "Failed to set frame parallel decoding"); |
| 229 | if (aom_codec_control(&codec, AV1E_SET_SINGLE_TILE_DECODING, 1)) |
| 230 | die_codec(&codec, "Failed to turn on single tile decoding"); |
Yunqing Wang | 75e20e8 | 2018-06-16 12:10:48 -0700 | [diff] [blame] | 231 | // Set tile_columns and tile_rows to MAX values, which guarantees the tile |
| 232 | // size of 64 x 64 pixels(i.e. 1 SB) for <= 4k resolution. |
| 233 | if (aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS, 6)) |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 234 | die_codec(&codec, "Failed to set tile width"); |
Yunqing Wang | 75e20e8 | 2018-06-16 12:10:48 -0700 | [diff] [blame] | 235 | if (aom_codec_control(&codec, AV1E_SET_TILE_ROWS, 6)) |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 236 | die_codec(&codec, "Failed to set tile height"); |
| 237 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 238 | for (bv = 0; bv < v_blocks; ++bv) { |
| 239 | for (bu = 0; bu < u_blocks; ++bu) { |
| 240 | const int block_u_min = bu * lf_blocksize; |
| 241 | const int block_v_min = bv * lf_blocksize; |
| 242 | int block_u_end = (bu + 1) * lf_blocksize; |
| 243 | int block_v_end = (bv + 1) * lf_blocksize; |
| 244 | int u, v; |
| 245 | block_u_end = block_u_end < lf_width ? block_u_end : lf_width; |
| 246 | block_v_end = block_v_end < lf_height ? block_v_end : lf_height; |
| 247 | for (v = block_v_min; v < block_v_end; ++v) { |
| 248 | for (u = block_u_min; u < block_u_end; ++u) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 249 | av1_ref_frame_t ref; |
| 250 | ref.idx = 0; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 251 | ref.use_external_ref = 1; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 252 | ref.img = reference_images[bv * u_blocks + bu]; |
| 253 | if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) |
| 254 | die_codec(&codec, "Failed to set reference frame"); |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 255 | |
| 256 | printf("C%d, ", (u + v * lf_width)); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 257 | fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET); |
| 258 | aom_img_read(raw, infile); |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 259 | get_raw_image(&frame_to_encode, raw, raw_shift); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 260 | |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 261 | ++frame_count; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 262 | printf("Encoding image %d of %d\n", |
| 263 | frame_count - (u_blocks * v_blocks), lf_width * lf_height); |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 264 | encode_frame(&codec, frame_to_encode, frame_count, 1, |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 265 | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | |
| 266 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | |
| 267 | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | |
| 268 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | |
| 269 | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY, |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 270 | writer); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | // Flush encoder. |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 277 | // No ARF, this should not be needed. |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 278 | while (encode_frame(&codec, NULL, -1, 1, 0, writer)) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 281 | for (i = 0; i < reference_image_num; i++) aom_img_free(&reference_images[i]); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 282 | |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 283 | if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec."); |
Yunqing Wang | 3ae70c9 | 2019-01-25 17:34:07 -0800 | [diff] [blame] | 284 | |
| 285 | // Modify large_scale_file fourcc. |
| 286 | if (cfg->large_scale_tile == 1) |
| 287 | aom_video_writer_set_fourcc(writer, LST_FOURCC); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 288 | aom_video_writer_close(writer); |
| 289 | |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 290 | printf("\nSecond pass complete. Processed %d frames.\n", frame_count); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | int main(int argc, char **argv) { |
| 294 | FILE *infile = NULL; |
| 295 | int w, h; |
| 296 | // The number of lightfield images in the u and v dimensions. |
| 297 | int lf_width, lf_height; |
| 298 | // Defines how many images refer to the same reference image for MCP. |
| 299 | // lf_blocksize X lf_blocksize images will all use the reference image |
| 300 | // in the middle of the block of images. |
| 301 | int lf_blocksize; |
| 302 | aom_codec_ctx_t codec; |
| 303 | aom_codec_enc_cfg_t cfg; |
| 304 | aom_image_t raw; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 305 | aom_image_t raw_shift; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 306 | aom_codec_err_t res; |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 307 | int flags = 0; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 308 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 309 | const int fps = 30; |
| 310 | const int bitrate = 200; // kbit/s |
| 311 | const char *const width_arg = argv[1]; |
| 312 | const char *const height_arg = argv[2]; |
| 313 | const char *const infile_arg = argv[3]; |
| 314 | const char *const outfile_arg = argv[4]; |
| 315 | const char *const lf_width_arg = argv[5]; |
| 316 | const char *const lf_height_arg = argv[6]; |
| 317 | const char *lf_blocksize_arg = argv[7]; |
| 318 | exec_name = argv[0]; |
| 319 | |
| 320 | if (argc < 8) die("Invalid number of arguments"); |
| 321 | |
Elliott Karpilovsky | cbe219b | 2020-04-22 16:21:06 -0700 | [diff] [blame] | 322 | aom_codec_iface_t *encoder = get_aom_encoder_by_short_name("av1"); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 323 | if (!encoder) die("Unsupported codec."); |
| 324 | |
| 325 | w = (int)strtol(width_arg, NULL, 0); |
| 326 | h = (int)strtol(height_arg, NULL, 0); |
| 327 | lf_width = (int)strtol(lf_width_arg, NULL, 0); |
| 328 | lf_height = (int)strtol(lf_height_arg, NULL, 0); |
| 329 | lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0); |
| 330 | lf_blocksize = lf_blocksize < lf_width ? lf_blocksize : lf_width; |
| 331 | lf_blocksize = lf_blocksize < lf_height ? lf_blocksize : lf_height; |
| 332 | |
| 333 | if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0) |
| 334 | die("Invalid frame size: %dx%d", w, h); |
| 335 | if (lf_width <= 0 || lf_height <= 0) |
| 336 | die("Invalid lf_width and/or lf_height: %dx%d", lf_width, lf_height); |
| 337 | if (lf_blocksize <= 0) die("Invalid lf_blocksize: %d", lf_blocksize); |
| 338 | |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 339 | if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, w, h, 32)) { |
| 340 | die("Failed to allocate image."); |
| 341 | } |
Ryan Lei | 2a67637 | 2020-09-28 16:58:52 -0700 | [diff] [blame] | 342 | // Need to allocate larger buffer to use hbd internal. |
| 343 | aom_img_alloc(&raw_shift, AOM_IMG_FMT_I420 | AOM_IMG_FMT_HIGHBITDEPTH, w, h, |
| 344 | 32); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 345 | |
Elliott Karpilovsky | cbe219b | 2020-04-22 16:21:06 -0700 | [diff] [blame] | 346 | printf("Using %s\n", aom_codec_iface_name(encoder)); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 347 | |
| 348 | // Configuration |
Elliott Karpilovsky | cbe219b | 2020-04-22 16:21:06 -0700 | [diff] [blame] | 349 | res = aom_codec_enc_config_default(encoder, &cfg, 0); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 350 | if (res) die_codec(&codec, "Failed to get default codec config."); |
| 351 | |
| 352 | cfg.g_w = w; |
| 353 | cfg.g_h = h; |
| 354 | cfg.g_timebase.num = 1; |
| 355 | cfg.g_timebase.den = fps; |
| 356 | cfg.rc_target_bitrate = bitrate; |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 357 | cfg.g_error_resilient = 0; // This is required. |
| 358 | cfg.g_lag_in_frames = 0; // need to set this since default is 19. |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 359 | cfg.kf_mode = AOM_KF_DISABLED; |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 360 | cfg.large_scale_tile = 0; // Only set it to 1 for camera frame encoding. |
Yunqing Wang | 17d9d43 | 2018-05-27 15:08:04 -0700 | [diff] [blame] | 361 | cfg.g_bit_depth = AOM_BITS_8; |
Ryan Lei | 2a67637 | 2020-09-28 16:58:52 -0700 | [diff] [blame] | 362 | flags |= AOM_CODEC_USE_HIGHBITDEPTH; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 363 | |
| 364 | if (!(infile = fopen(infile_arg, "rb"))) |
| 365 | die("Failed to open %s for reading", infile_arg); |
| 366 | |
Debargha Mukherjee | 5d3f3d6 | 2020-07-23 12:52:54 -0700 | [diff] [blame] | 367 | // Pass 1 |
| 368 | cfg.g_pass = AOM_RC_ONE_PASS; |
| 369 | pass1(&raw, infile, outfile_arg, encoder, &cfg, lf_width, lf_height, |
| 370 | lf_blocksize, flags, &raw_shift); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 371 | |
Ryan Lei | 2a67637 | 2020-09-28 16:58:52 -0700 | [diff] [blame] | 372 | aom_img_free(&raw_shift); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 373 | aom_img_free(&raw); |
| 374 | fclose(infile); |
| 375 | |
| 376 | return EXIT_SUCCESS; |
| 377 | } |