blob: 143928b4a4395c49f22dc5acad311c9abba10251 [file] [log] [blame]
Ryan Overbecka5fefa72017-09-19 11:39:10 -07001/*
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:
Yunqing Wangd417bb52018-06-03 14:13:38 -070027// examples/lightfield_encoder 1024 1024 vase10x10.yuv vase10x10.ivf 10 10 5
Ryan Overbecka5fefa72017-09-19 11:39:10 -070028
Yunqing Wangb041d8a2017-11-15 12:31:18 -080029// 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 Overbecka5fefa72017-09-19 11:39:10 -070034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37
38#include "aom/aom_encoder.h"
39#include "aom/aomcx.h"
Urvang Joshi6077b182018-08-21 11:49:38 -070040#include "aom_scale/yv12config.h"
Ryan Overbecka5fefa72017-09-19 11:39:10 -070041#include "av1/common/enums.h"
Tom Finegan77902132018-05-21 10:19:15 -070042#include "common/tools_common.h"
43#include "common/video_writer.h"
Ryan Overbecka5fefa72017-09-19 11:39:10 -070044
45static const char *exec_name;
Ryan Overbecka5fefa72017-09-19 11:39:10 -070046
47void 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
Yaowu Xu413566e2019-05-17 09:36:48 -070055static int img_size_bytes(aom_image_t *img) {
Ryan Overbecka5fefa72017-09-19 11:39:10 -070056 int image_size_bytes = 0;
57 int plane;
58 for (plane = 0; plane < 3; ++plane) {
Ryan Overbecka5fefa72017-09-19 11:39:10 -070059 const int w = aom_img_plane_width(img, plane) *
60 ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
61 const int h = aom_img_plane_height(img, plane);
Yunqing Wang17d9d432018-05-27 15:08:04 -070062 image_size_bytes += w * h;
Ryan Overbecka5fefa72017-09-19 11:39:10 -070063 }
64 return image_size_bytes;
65}
66
67static int get_frame_stats(aom_codec_ctx_t *ctx, const aom_image_t *img,
68 aom_codec_pts_t pts, unsigned int duration,
Sean DuBois47cc2552018-01-23 07:44:16 +000069 aom_enc_frame_flags_t flags,
Ryan Overbecka5fefa72017-09-19 11:39:10 -070070 aom_fixed_buf_t *stats) {
71 int got_pkts = 0;
72 aom_codec_iter_t iter = NULL;
73 const aom_codec_cx_pkt_t *pkt = NULL;
Sean DuBois47cc2552018-01-23 07:44:16 +000074 const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
Ryan Overbecka5fefa72017-09-19 11:39:10 -070075 if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to get frame stats.");
76
77 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
78 got_pkts = 1;
79
80 if (pkt->kind == AOM_CODEC_STATS_PKT) {
81 const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf;
82 const size_t pkt_size = pkt->data.twopass_stats.sz;
83 stats->buf = realloc(stats->buf, stats->sz + pkt_size);
84 memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size);
85 stats->sz += pkt_size;
86 }
87 }
88
89 return got_pkts;
90}
91
92static int encode_frame(aom_codec_ctx_t *ctx, const aom_image_t *img,
93 aom_codec_pts_t pts, unsigned int duration,
Yunqing Wangb97cfae2018-05-11 14:19:05 -070094 aom_enc_frame_flags_t flags, AvxVideoWriter *writer) {
Ryan Overbecka5fefa72017-09-19 11:39:10 -070095 int got_pkts = 0;
96 aom_codec_iter_t iter = NULL;
97 const aom_codec_cx_pkt_t *pkt = NULL;
Sean DuBois47cc2552018-01-23 07:44:16 +000098 const aom_codec_err_t res = aom_codec_encode(ctx, img, pts, duration, flags);
Ryan Overbecka5fefa72017-09-19 11:39:10 -070099 if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to encode frame.");
100
101 while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) {
102 got_pkts = 1;
103 if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
104 const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0;
105
106 if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf,
107 pkt->data.frame.sz,
108 pkt->data.frame.pts))
109 die_codec(ctx, "Failed to write compressed frame.");
110 printf(keyframe ? "K" : ".");
111 fflush(stdout);
112 }
113 }
114
115 return got_pkts;
116}
117
Yunqing Wang17d9d432018-05-27 15:08:04 -0700118static void get_raw_image(aom_image_t **frame_to_encode, aom_image_t *raw,
119 aom_image_t *raw_shift) {
120 if (!CONFIG_LOWBITDEPTH) {
121 // Need to allocate larger buffer to use hbd internal.
122 int input_shift = 0;
123 aom_img_upshift(raw_shift, raw, input_shift);
124 *frame_to_encode = raw_shift;
125 } else {
126 *frame_to_encode = raw;
127 }
128}
129
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700130static aom_fixed_buf_t pass0(aom_image_t *raw, FILE *infile,
131 const AvxInterface *encoder,
132 const aom_codec_enc_cfg_t *cfg, int lf_width,
Yunqing Wang17d9d432018-05-27 15:08:04 -0700133 int lf_height, int lf_blocksize, int flags,
134 aom_image_t *raw_shift) {
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700135 aom_codec_ctx_t codec;
136 int frame_count = 0;
Yaowu Xu413566e2019-05-17 09:36:48 -0700137 int image_size_bytes = img_size_bytes(raw);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700138 int u_blocks, v_blocks;
139 int bu, bv;
140 aom_fixed_buf_t stats = { NULL, 0 };
Yunqing Wang17d9d432018-05-27 15:08:04 -0700141 aom_image_t *frame_to_encode;
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700142
Yunqing Wang17d9d432018-05-27 15:08:04 -0700143 if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, flags))
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700144 die_codec(&codec, "Failed to initialize encoder");
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700145 if (aom_codec_control(&codec, AOME_SET_ENABLEAUTOALTREF, 0))
146 die_codec(&codec, "Failed to turn off auto altref");
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800147 if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 0))
148 die_codec(&codec, "Failed to set frame parallel decoding");
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700149
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700150 // How many reference images we need to encode.
151 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
152 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700153
154 printf("\n First pass: ");
155
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700156 for (bv = 0; bv < v_blocks; ++bv) {
157 for (bu = 0; bu < u_blocks; ++bu) {
158 const int block_u_min = bu * lf_blocksize;
159 const int block_v_min = bv * lf_blocksize;
160 int block_u_end = (bu + 1) * lf_blocksize;
161 int block_v_end = (bv + 1) * lf_blocksize;
162 int u_block_size, v_block_size;
163 int block_ref_u, block_ref_v;
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700164
165 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
166 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
167 u_block_size = block_u_end - block_u_min;
168 v_block_size = block_v_end - block_v_min;
169 block_ref_u = block_u_min + u_block_size / 2;
170 block_ref_v = block_v_min + v_block_size / 2;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700171
172 printf("A%d, ", (block_ref_u + block_ref_v * lf_width));
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700173 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
174 SEEK_SET);
175 aom_img_read(raw, infile);
Yunqing Wang17d9d432018-05-27 15:08:04 -0700176 get_raw_image(&frame_to_encode, raw, raw_shift);
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800177
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700178 // Reference frames can be encoded encoded without tiles.
179 ++frame_count;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700180 get_frame_stats(&codec, frame_to_encode, frame_count, 1,
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800181 AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
182 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
183 AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 |
184 AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF |
185 AOM_EFLAG_NO_UPD_ARF,
Sean DuBois47cc2552018-01-23 07:44:16 +0000186 &stats);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700187 }
188 }
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800189
190 if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 1))
191 die_codec(&codec, "Failed to set frame parallel decoding");
192
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700193 for (bv = 0; bv < v_blocks; ++bv) {
194 for (bu = 0; bu < u_blocks; ++bu) {
195 const int block_u_min = bu * lf_blocksize;
196 const int block_v_min = bv * lf_blocksize;
197 int block_u_end = (bu + 1) * lf_blocksize;
198 int block_v_end = (bv + 1) * lf_blocksize;
199 int u, v;
200 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
201 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
202 for (v = block_v_min; v < block_v_end; ++v) {
203 for (u = block_u_min; u < block_u_end; ++u) {
Yunqing Wang17d9d432018-05-27 15:08:04 -0700204 printf("C%d, ", (u + v * lf_width));
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700205 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
206 aom_img_read(raw, infile);
Yunqing Wang17d9d432018-05-27 15:08:04 -0700207 get_raw_image(&frame_to_encode, raw, raw_shift);
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800208
Yunqing Wang17d9d432018-05-27 15:08:04 -0700209 ++frame_count;
210 get_frame_stats(&codec, frame_to_encode, frame_count, 1,
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800211 AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
212 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
213 AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 |
214 AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF |
215 AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY,
Sean DuBois47cc2552018-01-23 07:44:16 +0000216 &stats);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700217 }
218 }
219 }
220 }
221 // Flush encoder.
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800222 // No ARF, this should not be needed.
Sean DuBois47cc2552018-01-23 07:44:16 +0000223 while (get_frame_stats(&codec, NULL, frame_count, 1, 0, &stats)) {
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700224 }
225
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700226 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
227
Yunqing Wang17d9d432018-05-27 15:08:04 -0700228 printf("\nFirst pass complete. Processed %d frames.\n", frame_count);
229
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700230 return stats;
231}
232
233static void pass1(aom_image_t *raw, FILE *infile, const char *outfile_name,
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800234 const AvxInterface *encoder, aom_codec_enc_cfg_t *cfg,
Yunqing Wang17d9d432018-05-27 15:08:04 -0700235 int lf_width, int lf_height, int lf_blocksize, int flags,
236 aom_image_t *raw_shift) {
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700237 AvxVideoInfo info = { encoder->fourcc,
238 cfg->g_w,
239 cfg->g_h,
Jonathan Matthewsa6e4f962018-07-13 16:01:28 +0100240 { cfg->g_timebase.num, cfg->g_timebase.den },
241 0 };
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700242 AvxVideoWriter *writer = NULL;
243 aom_codec_ctx_t codec;
244 int frame_count = 0;
Yaowu Xu413566e2019-05-17 09:36:48 -0700245 int image_size_bytes = img_size_bytes(raw);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700246 int bu, bv;
247 int u_blocks, v_blocks;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700248 aom_image_t *frame_to_encode;
Yunqing Wang93b18f32018-06-08 21:08:29 -0700249 aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
Yunqing Wang17d9d432018-05-27 15:08:04 -0700250 int reference_image_num = 0;
251 int i;
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700252
253 writer = aom_video_writer_open(outfile_name, kContainerIVF, &info);
254 if (!writer) die("Failed to open %s for writing", outfile_name);
255
Yunqing Wang17d9d432018-05-27 15:08:04 -0700256 if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, flags))
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700257 die_codec(&codec, "Failed to initialize encoder");
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700258 if (aom_codec_control(&codec, AOME_SET_ENABLEAUTOALTREF, 0))
259 die_codec(&codec, "Failed to turn off auto altref");
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800260 if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 0))
261 die_codec(&codec, "Failed to set frame parallel decoding");
Yunqing Wange3de2d82018-05-15 13:43:41 -0700262 // Note: The superblock is a sequence parameter and has to be the same for 1
263 // sequence. In lightfield application, must choose the superblock size(either
264 // 64x64 or 128x128) before the encoding starts. Otherwise, the default is
265 // AOM_SUPERBLOCK_SIZE_DYNAMIC, and the superblock size will be set to 64x64
266 // internally.
267 if (aom_codec_control(&codec, AV1E_SET_SUPERBLOCK_SIZE,
268 AOM_SUPERBLOCK_SIZE_64X64))
269 die_codec(&codec, "Failed to set SB size");
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700270
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700271 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
272 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700273
274 reference_image_num = u_blocks * v_blocks;
Sarah Parker9feb1582019-04-25 12:29:52 -0700275 // Set the max gf group length so the references are guaranteed to be in
276 // a different gf group than any of the regular frames. This avoids using
Sarah Parker48b46b42019-04-23 20:43:02 -0700277 // both vbr and constant quality mode in a single group. The number of
278 // references now cannot surpass 17 because of the enforced MAX_GF_INTERVAL of
279 // 16. If it is necessary to exceed this reference frame limit, one will have
280 // to do some additional handling to ensure references are in separate gf
281 // groups from the regular frames.
282 if (aom_codec_control(&codec, AV1E_SET_MAX_GF_INTERVAL,
283 reference_image_num - 1))
284 die_codec(&codec, "Failed to set max gf interval");
Yunqing Wang17d9d432018-05-27 15:08:04 -0700285 aom_img_fmt_t ref_fmt = AOM_IMG_FMT_I420;
286 if (!CONFIG_LOWBITDEPTH) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
287 // Allocate memory with the border so that it can be used as a reference.
Satish Kumar Suman29909962019-01-09 10:31:21 +0530288 int border_in_pixels =
289 (codec.config.enc->rc_resize_mode || codec.config.enc->rc_superres_mode)
290 ? AOM_BORDER_IN_PIXELS
291 : AOM_ENC_NO_SCALE_BORDER;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700292 for (i = 0; i < reference_image_num; i++) {
293 if (!aom_img_alloc_with_border(&reference_images[i], ref_fmt, cfg->g_w,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530294 cfg->g_h, 32, 8, border_in_pixels)) {
Yunqing Wang17d9d432018-05-27 15:08:04 -0700295 die("Failed to allocate image.");
296 }
297 }
298
299 printf("\n Second pass: ");
300
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700301 // Encode reference images first.
302 printf("Encoding Reference Images\n");
303 for (bv = 0; bv < v_blocks; ++bv) {
304 for (bu = 0; bu < u_blocks; ++bu) {
305 const int block_u_min = bu * lf_blocksize;
306 const int block_v_min = bv * lf_blocksize;
307 int block_u_end = (bu + 1) * lf_blocksize;
308 int block_v_end = (bv + 1) * lf_blocksize;
309 int u_block_size, v_block_size;
310 int block_ref_u, block_ref_v;
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700311
312 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
313 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
314 u_block_size = block_u_end - block_u_min;
315 v_block_size = block_v_end - block_v_min;
316 block_ref_u = block_u_min + u_block_size / 2;
317 block_ref_v = block_v_min + v_block_size / 2;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700318
319 printf("A%d, ", (block_ref_u + block_ref_v * lf_width));
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700320 fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes,
321 SEEK_SET);
322 aom_img_read(raw, infile);
Yunqing Wang17d9d432018-05-27 15:08:04 -0700323
324 get_raw_image(&frame_to_encode, raw, raw_shift);
325
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700326 // Reference frames may be encoded without tiles.
327 ++frame_count;
328 printf("Encoding reference image %d of %d\n", bv * u_blocks + bu,
329 u_blocks * v_blocks);
Yunqing Wang17d9d432018-05-27 15:08:04 -0700330 encode_frame(&codec, frame_to_encode, frame_count, 1,
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800331 AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
332 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
333 AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 |
334 AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF |
335 AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY,
Sean DuBois47cc2552018-01-23 07:44:16 +0000336 writer);
Yunqing Wang17d9d432018-05-27 15:08:04 -0700337
Yunqing Wang946ec2b2018-06-06 12:34:23 -0700338 if (aom_codec_control(&codec, AV1_COPY_NEW_FRAME_IMAGE,
Yunqing Wang17d9d432018-05-27 15:08:04 -0700339 &reference_images[frame_count - 1]))
Yunqing Wang946ec2b2018-06-06 12:34:23 -0700340 die_codec(&codec, "Failed to copy decoder reference frame");
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700341 }
342 }
343
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800344 cfg->large_scale_tile = 1;
345 // Fixed q encoding for camera frames.
346 cfg->rc_end_usage = AOM_Q;
347 if (aom_codec_enc_config_set(&codec, cfg))
348 die_codec(&codec, "Failed to configure encoder");
349
350 // The fixed q value used in encoding.
351 if (aom_codec_control(&codec, AOME_SET_CQ_LEVEL, 36))
352 die_codec(&codec, "Failed to set cq level");
353 if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 1))
354 die_codec(&codec, "Failed to set frame parallel decoding");
355 if (aom_codec_control(&codec, AV1E_SET_SINGLE_TILE_DECODING, 1))
356 die_codec(&codec, "Failed to turn on single tile decoding");
Yunqing Wang75e20e82018-06-16 12:10:48 -0700357 // Set tile_columns and tile_rows to MAX values, which guarantees the tile
358 // size of 64 x 64 pixels(i.e. 1 SB) for <= 4k resolution.
359 if (aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS, 6))
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800360 die_codec(&codec, "Failed to set tile width");
Yunqing Wang75e20e82018-06-16 12:10:48 -0700361 if (aom_codec_control(&codec, AV1E_SET_TILE_ROWS, 6))
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800362 die_codec(&codec, "Failed to set tile height");
363
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700364 for (bv = 0; bv < v_blocks; ++bv) {
365 for (bu = 0; bu < u_blocks; ++bu) {
366 const int block_u_min = bu * lf_blocksize;
367 const int block_v_min = bv * lf_blocksize;
368 int block_u_end = (bu + 1) * lf_blocksize;
369 int block_v_end = (bv + 1) * lf_blocksize;
370 int u, v;
371 block_u_end = block_u_end < lf_width ? block_u_end : lf_width;
372 block_v_end = block_v_end < lf_height ? block_v_end : lf_height;
373 for (v = block_v_min; v < block_v_end; ++v) {
374 for (u = block_u_min; u < block_u_end; ++u) {
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700375 av1_ref_frame_t ref;
376 ref.idx = 0;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700377 ref.use_external_ref = 1;
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700378 ref.img = reference_images[bv * u_blocks + bu];
379 if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref))
380 die_codec(&codec, "Failed to set reference frame");
Yunqing Wang17d9d432018-05-27 15:08:04 -0700381
382 printf("C%d, ", (u + v * lf_width));
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700383 fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET);
384 aom_img_read(raw, infile);
Yunqing Wang17d9d432018-05-27 15:08:04 -0700385 get_raw_image(&frame_to_encode, raw, raw_shift);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700386
Yunqing Wang17d9d432018-05-27 15:08:04 -0700387 ++frame_count;
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700388 printf("Encoding image %d of %d\n",
389 frame_count - (u_blocks * v_blocks), lf_width * lf_height);
Yunqing Wang17d9d432018-05-27 15:08:04 -0700390 encode_frame(&codec, frame_to_encode, frame_count, 1,
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800391 AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
392 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
393 AOM_EFLAG_NO_REF_BWD | AOM_EFLAG_NO_REF_ARF2 |
394 AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF |
395 AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY,
Sean DuBois47cc2552018-01-23 07:44:16 +0000396 writer);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700397 }
398 }
399 }
400 }
401
402 // Flush encoder.
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800403 // No ARF, this should not be needed.
Sean DuBois47cc2552018-01-23 07:44:16 +0000404 while (encode_frame(&codec, NULL, -1, 1, 0, writer)) {
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700405 }
406
Yunqing Wang17d9d432018-05-27 15:08:04 -0700407 for (i = 0; i < reference_image_num; i++) aom_img_free(&reference_images[i]);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700408
Yunqing Wang17d9d432018-05-27 15:08:04 -0700409 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec.");
Yunqing Wang3ae70c92019-01-25 17:34:07 -0800410
411 // Modify large_scale_file fourcc.
412 if (cfg->large_scale_tile == 1)
413 aom_video_writer_set_fourcc(writer, LST_FOURCC);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700414 aom_video_writer_close(writer);
415
Yunqing Wang17d9d432018-05-27 15:08:04 -0700416 printf("\nSecond pass complete. Processed %d frames.\n", frame_count);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700417}
418
419int main(int argc, char **argv) {
420 FILE *infile = NULL;
421 int w, h;
422 // The number of lightfield images in the u and v dimensions.
423 int lf_width, lf_height;
424 // Defines how many images refer to the same reference image for MCP.
425 // lf_blocksize X lf_blocksize images will all use the reference image
426 // in the middle of the block of images.
427 int lf_blocksize;
428 aom_codec_ctx_t codec;
429 aom_codec_enc_cfg_t cfg;
430 aom_image_t raw;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700431 aom_image_t raw_shift;
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700432 aom_codec_err_t res;
433 aom_fixed_buf_t stats;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700434 int flags = 0;
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700435
436 const AvxInterface *encoder = NULL;
437 const int fps = 30;
438 const int bitrate = 200; // kbit/s
439 const char *const width_arg = argv[1];
440 const char *const height_arg = argv[2];
441 const char *const infile_arg = argv[3];
442 const char *const outfile_arg = argv[4];
443 const char *const lf_width_arg = argv[5];
444 const char *const lf_height_arg = argv[6];
445 const char *lf_blocksize_arg = argv[7];
446 exec_name = argv[0];
447
448 if (argc < 8) die("Invalid number of arguments");
449
450 encoder = get_aom_encoder_by_name("av1");
451 if (!encoder) die("Unsupported codec.");
452
453 w = (int)strtol(width_arg, NULL, 0);
454 h = (int)strtol(height_arg, NULL, 0);
455 lf_width = (int)strtol(lf_width_arg, NULL, 0);
456 lf_height = (int)strtol(lf_height_arg, NULL, 0);
457 lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0);
458 lf_blocksize = lf_blocksize < lf_width ? lf_blocksize : lf_width;
459 lf_blocksize = lf_blocksize < lf_height ? lf_blocksize : lf_height;
460
461 if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0)
462 die("Invalid frame size: %dx%d", w, h);
463 if (lf_width <= 0 || lf_height <= 0)
464 die("Invalid lf_width and/or lf_height: %dx%d", lf_width, lf_height);
465 if (lf_blocksize <= 0) die("Invalid lf_blocksize: %d", lf_blocksize);
466
Yunqing Wang17d9d432018-05-27 15:08:04 -0700467 if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, w, h, 32)) {
468 die("Failed to allocate image.");
469 }
470 if (!CONFIG_LOWBITDEPTH) {
471 // Need to allocate larger buffer to use hbd internal.
472 aom_img_alloc(&raw_shift, AOM_IMG_FMT_I420 | AOM_IMG_FMT_HIGHBITDEPTH, w, h,
473 32);
474 }
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700475
476 printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
477
478 // Configuration
479 res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700480 if (res) die_codec(&codec, "Failed to get default codec config.");
481
482 cfg.g_w = w;
483 cfg.g_h = h;
484 cfg.g_timebase.num = 1;
485 cfg.g_timebase.den = fps;
486 cfg.rc_target_bitrate = bitrate;
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800487 cfg.g_error_resilient = 0; // This is required.
488 cfg.g_lag_in_frames = 0; // need to set this since default is 19.
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700489 cfg.kf_mode = AOM_KF_DISABLED;
Yunqing Wangb041d8a2017-11-15 12:31:18 -0800490 cfg.large_scale_tile = 0; // Only set it to 1 for camera frame encoding.
Yunqing Wang17d9d432018-05-27 15:08:04 -0700491 cfg.g_bit_depth = AOM_BITS_8;
492 flags |= (cfg.g_bit_depth > AOM_BITS_8 || !CONFIG_LOWBITDEPTH)
493 ? AOM_CODEC_USE_HIGHBITDEPTH
494 : 0;
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700495
496 if (!(infile = fopen(infile_arg, "rb")))
497 die("Failed to open %s for reading", infile_arg);
498
499 // Pass 0
500 cfg.g_pass = AOM_RC_FIRST_PASS;
Yunqing Wang17d9d432018-05-27 15:08:04 -0700501 stats = pass0(&raw, infile, encoder, &cfg, lf_width, lf_height, lf_blocksize,
502 flags, &raw_shift);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700503
504 // Pass 1
505 rewind(infile);
506 cfg.g_pass = AOM_RC_LAST_PASS;
507 cfg.rc_twopass_stats_in = stats;
508 pass1(&raw, infile, outfile_arg, encoder, &cfg, lf_width, lf_height,
Yunqing Wang17d9d432018-05-27 15:08:04 -0700509 lf_blocksize, flags, &raw_shift);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700510 free(stats.buf);
511
Yunqing Wang17d9d432018-05-27 15:08:04 -0700512 if (!CONFIG_LOWBITDEPTH) aom_img_free(&raw_shift);
Ryan Overbecka5fefa72017-09-19 11:39:10 -0700513 aom_img_free(&raw);
514 fclose(infile);
515
516 return EXIT_SUCCESS;
517}