Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Alliance for Open Media. All rights reserved |
| 3 | * |
| 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. |
| 10 | */ |
| 11 | |
| 12 | // Lightfield Encoder |
| 13 | // ================== |
| 14 | // |
| 15 | // This is an example of a simple lightfield encoder. It builds upon the |
| 16 | // twopass_encoder.c example. It takes an input file in YV12 format, |
| 17 | // treating it as a planar lightfield instead of a video. The img_width |
| 18 | // and img_height arguments are the dimensions of the lightfield images, |
| 19 | // while the lf_width and lf_height arguments are the number of |
| 20 | // lightfield images in each dimension. The lf_blocksize determines the |
| 21 | // number of reference images used for MCP. For example, 5 means that there |
| 22 | // is a reference image for every 5x5 lightfield image block. All images |
| 23 | // within a block will use the center image in that block as the reference |
| 24 | // image for MCP. |
| 25 | // Run "make test" to download lightfield test data: vase10x10.yuv. |
| 26 | // Run lightfield encoder to encode whole lightfield: |
| 27 | // examples/lightfield_encoder 1024 1024 vase10x10.yuv vase10x10.webm 10 10 5 |
| 28 | |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 29 | // Note: In bitstream.c and encoder.c, define EXT_TILE_DEBUG as 1 will print |
| 30 | // out the uncompressed header and the frame contexts, which can be used to |
| 31 | // test the bit exactness of the headers and the frame contexts for large scale |
| 32 | // tile coded frames. |
| 33 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | #include "aom/aom_encoder.h" |
| 39 | #include "aom/aomcx.h" |
| 40 | #include "av1/common/enums.h" |
| 41 | |
| 42 | #include "../tools_common.h" |
| 43 | #include "../video_writer.h" |
| 44 | |
| 45 | static const char *exec_name; |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 46 | |
| 47 | void usage_exit(void) { |
| 48 | fprintf(stderr, |
| 49 | "Usage: %s <img_width> <img_height> <infile> <outfile> " |
| 50 | "<lf_width> <lf_height> <lf_blocksize>\n", |
| 51 | exec_name); |
| 52 | exit(EXIT_FAILURE); |
| 53 | } |
| 54 | |
| 55 | static aom_image_t *aom_img_copy(aom_image_t *src, aom_image_t *dst) { |
| 56 | dst = aom_img_alloc(dst, src->fmt, src->d_w, src->d_h, 16); |
| 57 | |
| 58 | int plane; |
| 59 | |
| 60 | for (plane = 0; plane < 3; ++plane) { |
| 61 | unsigned char *src_buf = src->planes[plane]; |
| 62 | const int src_stride = src->stride[plane]; |
| 63 | const int src_w = plane == 0 ? src->d_w : src->d_w >> 1; |
| 64 | const int src_h = plane == 0 ? src->d_h : src->d_h >> 1; |
| 65 | |
| 66 | unsigned char *dst_buf = dst->planes[plane]; |
| 67 | const int dst_stride = dst->stride[plane]; |
| 68 | int y; |
| 69 | |
| 70 | for (y = 0; y < src_h; ++y) { |
| 71 | memcpy(dst_buf, src_buf, src_w); |
| 72 | src_buf += src_stride; |
| 73 | dst_buf += dst_stride; |
| 74 | } |
| 75 | } |
| 76 | return dst; |
| 77 | } |
| 78 | |
| 79 | static int aom_img_size_bytes(aom_image_t *img) { |
| 80 | int image_size_bytes = 0; |
| 81 | int plane; |
| 82 | for (plane = 0; plane < 3; ++plane) { |
| 83 | const int stride = img->stride[plane]; |
| 84 | const int w = aom_img_plane_width(img, plane) * |
| 85 | ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); |
| 86 | const int h = aom_img_plane_height(img, plane); |
| 87 | image_size_bytes += (w + stride) * h; |
| 88 | } |
| 89 | return image_size_bytes; |
| 90 | } |
| 91 | |
| 92 | static int get_frame_stats(aom_codec_ctx_t *ctx, const aom_image_t *img, |
| 93 | aom_codec_pts_t pts, unsigned int duration, |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 94 | aom_enc_frame_flags_t flags, |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 95 | aom_fixed_buf_t *stats) { |
| 96 | int got_pkts = 0; |
| 97 | aom_codec_iter_t iter = NULL; |
| 98 | const aom_codec_cx_pkt_t *pkt = NULL; |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 99 | 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] | 100 | if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to get frame stats."); |
| 101 | |
| 102 | while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) { |
| 103 | got_pkts = 1; |
| 104 | |
| 105 | if (pkt->kind == AOM_CODEC_STATS_PKT) { |
| 106 | const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf; |
| 107 | const size_t pkt_size = pkt->data.twopass_stats.sz; |
| 108 | stats->buf = realloc(stats->buf, stats->sz + pkt_size); |
| 109 | memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size); |
| 110 | stats->sz += pkt_size; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return got_pkts; |
| 115 | } |
| 116 | |
| 117 | static int encode_frame(aom_codec_ctx_t *ctx, const aom_image_t *img, |
| 118 | aom_codec_pts_t pts, unsigned int duration, |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 119 | aom_enc_frame_flags_t flags AvxVideoWriter *writer) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 120 | int got_pkts = 0; |
| 121 | aom_codec_iter_t iter = NULL; |
| 122 | const aom_codec_cx_pkt_t *pkt = NULL; |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 123 | 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] | 124 | if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to encode frame."); |
| 125 | |
| 126 | while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) { |
| 127 | got_pkts = 1; |
| 128 | if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { |
| 129 | const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0; |
| 130 | |
| 131 | if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf, |
| 132 | pkt->data.frame.sz, |
| 133 | pkt->data.frame.pts)) |
| 134 | die_codec(ctx, "Failed to write compressed frame."); |
| 135 | printf(keyframe ? "K" : "."); |
| 136 | fflush(stdout); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return got_pkts; |
| 141 | } |
| 142 | |
| 143 | static aom_fixed_buf_t pass0(aom_image_t *raw, FILE *infile, |
| 144 | const AvxInterface *encoder, |
| 145 | const aom_codec_enc_cfg_t *cfg, int lf_width, |
| 146 | int lf_height, int lf_blocksize) { |
| 147 | aom_codec_ctx_t codec; |
| 148 | int frame_count = 0; |
| 149 | int image_size_bytes = 0; |
| 150 | int u_blocks, v_blocks; |
| 151 | int bu, bv; |
| 152 | aom_fixed_buf_t stats = { NULL, 0 }; |
| 153 | |
| 154 | if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0)) |
| 155 | die_codec(&codec, "Failed to initialize encoder"); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 156 | if (aom_codec_control(&codec, AOME_SET_ENABLEAUTOALTREF, 0)) |
| 157 | die_codec(&codec, "Failed to turn off auto altref"); |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 158 | if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 0)) |
| 159 | die_codec(&codec, "Failed to set frame parallel decoding"); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 160 | |
| 161 | image_size_bytes = aom_img_size_bytes(raw); |
| 162 | |
| 163 | // How many reference images we need to encode. |
| 164 | u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize; |
| 165 | v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize; |
| 166 | aom_image_t *reference_images = |
| 167 | (aom_image_t *)malloc(u_blocks * v_blocks * sizeof(aom_image_t)); |
| 168 | for (bv = 0; bv < v_blocks; ++bv) { |
| 169 | for (bu = 0; bu < u_blocks; ++bu) { |
| 170 | const int block_u_min = bu * lf_blocksize; |
| 171 | const int block_v_min = bv * lf_blocksize; |
| 172 | int block_u_end = (bu + 1) * lf_blocksize; |
| 173 | int block_v_end = (bv + 1) * lf_blocksize; |
| 174 | int u_block_size, v_block_size; |
| 175 | int block_ref_u, block_ref_v; |
| 176 | struct av1_ref_frame ref_frame; |
| 177 | |
| 178 | block_u_end = block_u_end < lf_width ? block_u_end : lf_width; |
| 179 | block_v_end = block_v_end < lf_height ? block_v_end : lf_height; |
| 180 | u_block_size = block_u_end - block_u_min; |
| 181 | v_block_size = block_v_end - block_v_min; |
| 182 | block_ref_u = block_u_min + u_block_size / 2; |
| 183 | block_ref_v = block_v_min + v_block_size / 2; |
| 184 | fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes, |
| 185 | SEEK_SET); |
| 186 | aom_img_read(raw, infile); |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 187 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 188 | // Reference frames can be encoded encoded without tiles. |
| 189 | ++frame_count; |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 190 | get_frame_stats(&codec, raw, frame_count, 1, |
| 191 | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | |
| 192 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | |
| 193 | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | |
| 194 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | |
| 195 | AOM_EFLAG_NO_UPD_ARF, |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 196 | &stats); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 197 | ref_frame.idx = 0; |
| 198 | aom_codec_control(&codec, AV1_GET_REFERENCE, &ref_frame); |
| 199 | aom_img_copy(&ref_frame.img, &reference_images[frame_count - 1]); |
| 200 | } |
| 201 | } |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 202 | |
| 203 | if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 1)) |
| 204 | die_codec(&codec, "Failed to set frame parallel decoding"); |
| 205 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 206 | for (bv = 0; bv < v_blocks; ++bv) { |
| 207 | for (bu = 0; bu < u_blocks; ++bu) { |
| 208 | const int block_u_min = bu * lf_blocksize; |
| 209 | const int block_v_min = bv * lf_blocksize; |
| 210 | int block_u_end = (bu + 1) * lf_blocksize; |
| 211 | int block_v_end = (bv + 1) * lf_blocksize; |
| 212 | int u, v; |
| 213 | block_u_end = block_u_end < lf_width ? block_u_end : lf_width; |
| 214 | block_v_end = block_v_end < lf_height ? block_v_end : lf_height; |
| 215 | for (v = block_v_min; v < block_v_end; ++v) { |
| 216 | for (u = block_u_min; u < block_u_end; ++u) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 217 | av1_ref_frame_t ref; |
| 218 | ref.idx = 0; |
| 219 | ref.img = reference_images[bv * u_blocks + bu]; |
| 220 | if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) |
| 221 | die_codec(&codec, "Failed to set reference frame"); |
| 222 | |
| 223 | fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET); |
| 224 | aom_img_read(raw, infile); |
| 225 | ++frame_count; |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 226 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 227 | get_frame_stats(&codec, raw, frame_count, 1, |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 228 | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | |
| 229 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | |
| 230 | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | |
| 231 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | |
| 232 | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY, |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 233 | &stats); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | // Flush encoder. |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 239 | // No ARF, this should not be needed. |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 240 | while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | printf("Pass 0 complete. Processed %d frames.\n", frame_count); |
| 244 | if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec."); |
| 245 | |
| 246 | return stats; |
| 247 | } |
| 248 | |
| 249 | static void pass1(aom_image_t *raw, FILE *infile, const char *outfile_name, |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 250 | const AvxInterface *encoder, aom_codec_enc_cfg_t *cfg, |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 251 | int lf_width, int lf_height, int lf_blocksize) { |
| 252 | AvxVideoInfo info = { encoder->fourcc, |
| 253 | cfg->g_w, |
| 254 | cfg->g_h, |
| 255 | { cfg->g_timebase.num, cfg->g_timebase.den } }; |
| 256 | AvxVideoWriter *writer = NULL; |
| 257 | aom_codec_ctx_t codec; |
| 258 | int frame_count = 0; |
| 259 | int image_size_bytes; |
| 260 | int bu, bv; |
| 261 | int u_blocks, v_blocks; |
| 262 | |
| 263 | writer = aom_video_writer_open(outfile_name, kContainerIVF, &info); |
| 264 | if (!writer) die("Failed to open %s for writing", outfile_name); |
| 265 | |
| 266 | if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0)) |
| 267 | die_codec(&codec, "Failed to initialize encoder"); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 268 | if (aom_codec_control(&codec, AOME_SET_ENABLEAUTOALTREF, 0)) |
| 269 | die_codec(&codec, "Failed to turn off auto altref"); |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 270 | if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 0)) |
| 271 | die_codec(&codec, "Failed to set frame parallel decoding"); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 272 | |
| 273 | image_size_bytes = aom_img_size_bytes(raw); |
| 274 | u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize; |
| 275 | v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize; |
| 276 | aom_image_t *reference_images = |
| 277 | (aom_image_t *)malloc(u_blocks * v_blocks * sizeof(aom_image_t)); |
| 278 | // Encode reference images first. |
| 279 | printf("Encoding Reference Images\n"); |
| 280 | for (bv = 0; bv < v_blocks; ++bv) { |
| 281 | for (bu = 0; bu < u_blocks; ++bu) { |
| 282 | const int block_u_min = bu * lf_blocksize; |
| 283 | const int block_v_min = bv * lf_blocksize; |
| 284 | int block_u_end = (bu + 1) * lf_blocksize; |
| 285 | int block_v_end = (bv + 1) * lf_blocksize; |
| 286 | int u_block_size, v_block_size; |
| 287 | int block_ref_u, block_ref_v; |
| 288 | struct av1_ref_frame ref_frame; |
| 289 | |
| 290 | block_u_end = block_u_end < lf_width ? block_u_end : lf_width; |
| 291 | block_v_end = block_v_end < lf_height ? block_v_end : lf_height; |
| 292 | u_block_size = block_u_end - block_u_min; |
| 293 | v_block_size = block_v_end - block_v_min; |
| 294 | block_ref_u = block_u_min + u_block_size / 2; |
| 295 | block_ref_v = block_v_min + v_block_size / 2; |
| 296 | fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes, |
| 297 | SEEK_SET); |
| 298 | aom_img_read(raw, infile); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 299 | // Reference frames may be encoded without tiles. |
| 300 | ++frame_count; |
| 301 | printf("Encoding reference image %d of %d\n", bv * u_blocks + bu, |
| 302 | u_blocks * v_blocks); |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 303 | encode_frame(&codec, raw, frame_count, 1, |
| 304 | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | |
| 305 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | |
| 306 | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | |
| 307 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | |
| 308 | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY, |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 309 | writer); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 310 | ref_frame.idx = 0; |
| 311 | aom_codec_control(&codec, AV1_GET_REFERENCE, &ref_frame); |
| 312 | aom_img_copy(&ref_frame.img, &reference_images[frame_count - 1]); |
| 313 | } |
| 314 | } |
| 315 | |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 316 | cfg->large_scale_tile = 1; |
| 317 | // Fixed q encoding for camera frames. |
| 318 | cfg->rc_end_usage = AOM_Q; |
| 319 | if (aom_codec_enc_config_set(&codec, cfg)) |
| 320 | die_codec(&codec, "Failed to configure encoder"); |
| 321 | |
| 322 | // The fixed q value used in encoding. |
| 323 | if (aom_codec_control(&codec, AOME_SET_CQ_LEVEL, 36)) |
| 324 | die_codec(&codec, "Failed to set cq level"); |
| 325 | if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 1)) |
| 326 | die_codec(&codec, "Failed to set frame parallel decoding"); |
| 327 | if (aom_codec_control(&codec, AV1E_SET_SINGLE_TILE_DECODING, 1)) |
| 328 | die_codec(&codec, "Failed to turn on single tile decoding"); |
| 329 | // Set tile size to 64 pixels. The tile_columns and |
| 330 | // tile_rows in the tile coding are overloaded to represent tile_width |
| 331 | // and tile_height, that range from 1 to 64, in the unit of 64 pixels. |
| 332 | if (aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS, 1)) |
| 333 | die_codec(&codec, "Failed to set tile width"); |
| 334 | if (aom_codec_control(&codec, AV1E_SET_TILE_ROWS, 1)) |
| 335 | die_codec(&codec, "Failed to set tile height"); |
| 336 | |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 337 | for (bv = 0; bv < v_blocks; ++bv) { |
| 338 | for (bu = 0; bu < u_blocks; ++bu) { |
| 339 | const int block_u_min = bu * lf_blocksize; |
| 340 | const int block_v_min = bv * lf_blocksize; |
| 341 | int block_u_end = (bu + 1) * lf_blocksize; |
| 342 | int block_v_end = (bv + 1) * lf_blocksize; |
| 343 | int u, v; |
| 344 | block_u_end = block_u_end < lf_width ? block_u_end : lf_width; |
| 345 | block_v_end = block_v_end < lf_height ? block_v_end : lf_height; |
| 346 | for (v = block_v_min; v < block_v_end; ++v) { |
| 347 | for (u = block_u_min; u < block_u_end; ++u) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 348 | av1_ref_frame_t ref; |
| 349 | ref.idx = 0; |
| 350 | ref.img = reference_images[bv * u_blocks + bu]; |
| 351 | if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) |
| 352 | die_codec(&codec, "Failed to set reference frame"); |
| 353 | fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET); |
| 354 | aom_img_read(raw, infile); |
| 355 | ++frame_count; |
| 356 | |
| 357 | printf("Encoding image %d of %d\n", |
| 358 | frame_count - (u_blocks * v_blocks), lf_width * lf_height); |
| 359 | encode_frame(&codec, raw, frame_count, 1, |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 360 | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | |
| 361 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | |
| 362 | AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 | |
| 363 | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | |
| 364 | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY, |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 365 | writer); |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // Flush encoder. |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 372 | // No ARF, this should not be needed. |
Sean DuBois | 47cc255 | 2018-01-23 07:44:16 +0000 | [diff] [blame] | 373 | while (encode_frame(&codec, NULL, -1, 1, 0, writer)) { |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec."); |
| 377 | |
| 378 | aom_video_writer_close(writer); |
| 379 | |
| 380 | printf("Pass 1 complete. Processed %d frames.\n", frame_count); |
| 381 | } |
| 382 | |
| 383 | int main(int argc, char **argv) { |
| 384 | FILE *infile = NULL; |
| 385 | int w, h; |
| 386 | // The number of lightfield images in the u and v dimensions. |
| 387 | int lf_width, lf_height; |
| 388 | // Defines how many images refer to the same reference image for MCP. |
| 389 | // lf_blocksize X lf_blocksize images will all use the reference image |
| 390 | // in the middle of the block of images. |
| 391 | int lf_blocksize; |
| 392 | aom_codec_ctx_t codec; |
| 393 | aom_codec_enc_cfg_t cfg; |
| 394 | aom_image_t raw; |
| 395 | aom_codec_err_t res; |
| 396 | aom_fixed_buf_t stats; |
| 397 | |
| 398 | const AvxInterface *encoder = NULL; |
| 399 | const int fps = 30; |
| 400 | const int bitrate = 200; // kbit/s |
| 401 | const char *const width_arg = argv[1]; |
| 402 | const char *const height_arg = argv[2]; |
| 403 | const char *const infile_arg = argv[3]; |
| 404 | const char *const outfile_arg = argv[4]; |
| 405 | const char *const lf_width_arg = argv[5]; |
| 406 | const char *const lf_height_arg = argv[6]; |
| 407 | const char *lf_blocksize_arg = argv[7]; |
| 408 | exec_name = argv[0]; |
| 409 | |
| 410 | if (argc < 8) die("Invalid number of arguments"); |
| 411 | |
| 412 | encoder = get_aom_encoder_by_name("av1"); |
| 413 | if (!encoder) die("Unsupported codec."); |
| 414 | |
| 415 | w = (int)strtol(width_arg, NULL, 0); |
| 416 | h = (int)strtol(height_arg, NULL, 0); |
| 417 | lf_width = (int)strtol(lf_width_arg, NULL, 0); |
| 418 | lf_height = (int)strtol(lf_height_arg, NULL, 0); |
| 419 | lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0); |
| 420 | lf_blocksize = lf_blocksize < lf_width ? lf_blocksize : lf_width; |
| 421 | lf_blocksize = lf_blocksize < lf_height ? lf_blocksize : lf_height; |
| 422 | |
| 423 | if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0) |
| 424 | die("Invalid frame size: %dx%d", w, h); |
| 425 | if (lf_width <= 0 || lf_height <= 0) |
| 426 | die("Invalid lf_width and/or lf_height: %dx%d", lf_width, lf_height); |
| 427 | if (lf_blocksize <= 0) die("Invalid lf_blocksize: %d", lf_blocksize); |
| 428 | |
| 429 | if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, w, h, 1)) |
| 430 | die("Failed to allocate image", w, h); |
| 431 | |
| 432 | printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface())); |
| 433 | |
| 434 | // Configuration |
| 435 | res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0); |
| 436 | |
| 437 | if (res) die_codec(&codec, "Failed to get default codec config."); |
| 438 | |
| 439 | cfg.g_w = w; |
| 440 | cfg.g_h = h; |
| 441 | cfg.g_timebase.num = 1; |
| 442 | cfg.g_timebase.den = fps; |
| 443 | cfg.rc_target_bitrate = bitrate; |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 444 | cfg.g_error_resilient = 0; // This is required. |
| 445 | 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] | 446 | cfg.kf_mode = AOM_KF_DISABLED; |
Yunqing Wang | b041d8a | 2017-11-15 12:31:18 -0800 | [diff] [blame] | 447 | cfg.large_scale_tile = 0; // Only set it to 1 for camera frame encoding. |
Ryan Overbeck | a5fefa7 | 2017-09-19 11:39:10 -0700 | [diff] [blame] | 448 | |
| 449 | if (!(infile = fopen(infile_arg, "rb"))) |
| 450 | die("Failed to open %s for reading", infile_arg); |
| 451 | |
| 452 | // Pass 0 |
| 453 | cfg.g_pass = AOM_RC_FIRST_PASS; |
| 454 | stats = pass0(&raw, infile, encoder, &cfg, lf_width, lf_height, lf_blocksize); |
| 455 | |
| 456 | // Pass 1 |
| 457 | rewind(infile); |
| 458 | cfg.g_pass = AOM_RC_LAST_PASS; |
| 459 | cfg.rc_twopass_stats_in = stats; |
| 460 | pass1(&raw, infile, outfile_arg, encoder, &cfg, lf_width, lf_height, |
| 461 | lf_blocksize); |
| 462 | free(stats.buf); |
| 463 | |
| 464 | aom_img_free(&raw); |
| 465 | fclose(infile); |
| 466 | |
| 467 | return EXIT_SUCCESS; |
| 468 | } |