diff options
author | trav90 <travawine@palemoon.org> | 2018-10-18 21:53:44 -0500 |
---|---|---|
committer | trav90 <travawine@palemoon.org> | 2018-10-18 21:53:44 -0500 |
commit | ec910d81405c736a4490383a250299a7837c2e64 (patch) | |
tree | 4f27cc226f93a863121aef6c56313e4153a69b3e /third_party/aom/examples | |
parent | 01eb57073ba97b2d6cbf20f745dfcc508197adc3 (diff) | |
download | UXP-ec910d81405c736a4490383a250299a7837c2e64.tar UXP-ec910d81405c736a4490383a250299a7837c2e64.tar.gz UXP-ec910d81405c736a4490383a250299a7837c2e64.tar.lz UXP-ec910d81405c736a4490383a250299a7837c2e64.tar.xz UXP-ec910d81405c736a4490383a250299a7837c2e64.zip |
Update aom to commit id e87fb2378f01103d5d6e477a4ef6892dc714e614
Diffstat (limited to 'third_party/aom/examples')
-rw-r--r-- | third_party/aom/examples/aom_cx_set_ref.c | 8 | ||||
-rw-r--r-- | third_party/aom/examples/inspect.c | 73 | ||||
-rw-r--r-- | third_party/aom/examples/lightfield_decoder.c | 188 | ||||
-rw-r--r-- | third_party/aom/examples/lightfield_encoder.c | 480 |
4 files changed, 705 insertions, 44 deletions
diff --git a/third_party/aom/examples/aom_cx_set_ref.c b/third_party/aom/examples/aom_cx_set_ref.c index ff24fa14a..456e81300 100644 --- a/third_party/aom/examples/aom_cx_set_ref.c +++ b/third_party/aom/examples/aom_cx_set_ref.c @@ -270,18 +270,18 @@ int main(int argc, char **argv) { while (aom_img_read(&raw, infile)) { if (limit && frame_in >= limit) break; if (update_frame_num > 1 && frame_out + 1 == update_frame_num) { - aom_ref_frame_t ref; - ref.frame_type = AOM_LAST_FRAME; + av1_ref_frame_t ref; + ref.idx = 0; ref.img = raw; // Set reference frame in encoder. - if (aom_codec_control(&ecodec, AOM_SET_REFERENCE, &ref)) + if (aom_codec_control(&ecodec, AV1_SET_REFERENCE, &ref)) die_codec(&ecodec, "Failed to set reference frame"); printf(" <SET_REF>"); // If set_reference in decoder is commented out, the enc/dec mismatch // would be seen. if (test_decode) { - if (aom_codec_control(&dcodec, AOM_SET_REFERENCE, &ref)) + if (aom_codec_control(&dcodec, AV1_SET_REFERENCE, &ref)) die_codec(&dcodec, "Failed to set reference frame"); } } diff --git a/third_party/aom/examples/inspect.c b/third_party/aom/examples/inspect.c index e5c28711f..74e770b84 100644 --- a/third_party/aom/examples/inspect.c +++ b/third_party/aom/examples/inspect.c @@ -149,6 +149,11 @@ const map_entry block_size_map[] = { #if CONFIG_EXT_PARTITION ENUM(BLOCK_64X128), ENUM(BLOCK_128X64), ENUM(BLOCK_128X128), #endif + ENUM(BLOCK_4X16), ENUM(BLOCK_16X4), ENUM(BLOCK_8X32), + ENUM(BLOCK_32X8), ENUM(BLOCK_16X64), ENUM(BLOCK_64X16), +#if CONFIG_EXT_PARTITION + ENUM(BLOCK_32X128), ENUM(BLOCK_128X32), +#endif LAST_ENUM }; @@ -161,8 +166,12 @@ const map_entry tx_size_map[] = { ENUM(TX_64X64), #endif ENUM(TX_4X8), ENUM(TX_8X4), ENUM(TX_8X16), ENUM(TX_16X8), - ENUM(TX_16X32), ENUM(TX_32X16), ENUM(TX_4X16), ENUM(TX_16X4), - ENUM(TX_8X32), ENUM(TX_32X8), LAST_ENUM + ENUM(TX_16X32), ENUM(TX_32X16), +#if CONFIG_TX64X64 + ENUM(TX_32X64), ENUM(TX_64X32), +#endif // CONFIG_TX64X64 + ENUM(TX_4X16), ENUM(TX_16X4), ENUM(TX_8X32), ENUM(TX_32X8), + LAST_ENUM }; const map_entry tx_type_map[] = { ENUM(DCT_DCT), @@ -185,52 +194,36 @@ const map_entry tx_type_map[] = { ENUM(DCT_DCT), #endif LAST_ENUM }; -const map_entry prediction_mode_map[] = { ENUM(DC_PRED), - ENUM(V_PRED), - ENUM(H_PRED), - ENUM(D45_PRED), - ENUM(D135_PRED), - ENUM(D117_PRED), - ENUM(D153_PRED), - ENUM(D207_PRED), - ENUM(D63_PRED), -#if CONFIG_ALT_INTRA - ENUM(SMOOTH_PRED), +const map_entry prediction_mode_map[] = { + ENUM(DC_PRED), ENUM(V_PRED), ENUM(H_PRED), + ENUM(D45_PRED), ENUM(D135_PRED), ENUM(D117_PRED), + ENUM(D153_PRED), ENUM(D207_PRED), ENUM(D63_PRED), + ENUM(SMOOTH_PRED), #if CONFIG_SMOOTH_HV - ENUM(SMOOTH_V_PRED), - ENUM(SMOOTH_H_PRED), + ENUM(SMOOTH_V_PRED), ENUM(SMOOTH_H_PRED), #endif // CONFIG_SMOOTH_HV -#endif // CONFIG_ALT_INTRA - ENUM(TM_PRED), - ENUM(NEARESTMV), - ENUM(NEARMV), - ENUM(ZEROMV), - ENUM(NEWMV), -#if CONFIG_EXT_INTER - ENUM(NEAREST_NEARESTMV), - ENUM(NEAR_NEARMV), - ENUM(NEAREST_NEWMV), - ENUM(NEW_NEARESTMV), - ENUM(NEAR_NEWMV), - ENUM(NEW_NEARMV), - ENUM(ZERO_ZEROMV), - ENUM(NEW_NEWMV), -#endif - ENUM(INTRA_INVALID), - LAST_ENUM }; + ENUM(TM_PRED), ENUM(NEARESTMV), ENUM(NEARMV), + ENUM(ZEROMV), ENUM(NEWMV), ENUM(NEAREST_NEARESTMV), + ENUM(NEAR_NEARMV), ENUM(NEAREST_NEWMV), ENUM(NEW_NEARESTMV), + ENUM(NEAR_NEWMV), ENUM(NEW_NEARMV), ENUM(ZERO_ZEROMV), + ENUM(NEW_NEWMV), ENUM(INTRA_INVALID), LAST_ENUM +}; #if CONFIG_CFL const map_entry uv_prediction_mode_map[] = { - ENUM(UV_DC_PRED), ENUM(UV_V_PRED), ENUM(UV_H_PRED), - ENUM(UV_D45_PRED), ENUM(UV_D135_PRED), ENUM(UV_D117_PRED), - ENUM(UV_D153_PRED), ENUM(UV_D207_PRED), ENUM(UV_D63_PRED), -#if CONFIG_ALT_INTRA - ENUM(UV_SMOOTH_PRED), + ENUM(UV_DC_PRED), ENUM(UV_V_PRED), + ENUM(UV_H_PRED), ENUM(UV_D45_PRED), + ENUM(UV_D135_PRED), ENUM(UV_D117_PRED), + ENUM(UV_D153_PRED), ENUM(UV_D207_PRED), + ENUM(UV_D63_PRED), ENUM(UV_SMOOTH_PRED), #if CONFIG_SMOOTH_HV ENUM(UV_SMOOTH_V_PRED), ENUM(UV_SMOOTH_H_PRED), #endif // CONFIG_SMOOTH_HV -#endif // CONFIG_ALT_INTRA - ENUM(UV_TM_PRED), ENUM(UV_MODE_INVALID), LAST_ENUM + ENUM(UV_TM_PRED), +#if CONFIG_CFL + ENUM(UV_CFL_PRED), +#endif + ENUM(UV_MODE_INVALID), LAST_ENUM }; #else #define uv_prediction_mode_map prediction_mode_map diff --git a/third_party/aom/examples/lightfield_decoder.c b/third_party/aom/examples/lightfield_decoder.c new file mode 100644 index 000000000..8743df9bc --- /dev/null +++ b/third_party/aom/examples/lightfield_decoder.c @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2017, Alliance for Open Media. All rights reserved + * + * This source code is subject to the terms of the BSD 2 Clause License and + * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License + * was not distributed with this source code in the LICENSE file, you can + * obtain it at www.aomedia.org/license/software. If the Alliance for Open + * Media Patent License 1.0 was not distributed with this source code in the + * PATENTS file, you can obtain it at www.aomedia.org/license/patent. + */ + +// Lightfield Decoder +// ================== +// +// This is an example of a simple lightfield decoder. It builds upon the +// simple_decoder.c example. It takes an input file containing the compressed +// data (in webm format), treating it as a lightfield instead of a video and +// will decode a single lightfield tile. The lf_width and lf_height arguments +// are the number of lightfield images in each dimension. The tile to decode +// is specified by the tile_u, tile_v, tile_s, tile_t arguments. The tile_u, +// tile_v specify the image and tile_s, tile_t specify the tile in the image. +// After running the lightfield encoder, run lightfield decoder to decode a +// single tile: +// examples/lightfield_decoder vase10x10.webm vase_tile.yuv 10 10 3 4 5 10 5 + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "aom/aom_decoder.h" +#include "aom/aomdx.h" + +#include "../tools_common.h" +#include "../video_reader.h" +#include "./aom_config.h" + +static const char *exec_name; + +void usage_exit(void) { + fprintf(stderr, + "Usage: %s <infile> <outfile> <lf_width> <lf_height> <tlie_u>" + " <tile_v> <tile_s> <tile_t> <lf_blocksize>\n", + exec_name); + exit(EXIT_FAILURE); +} + +aom_image_t *aom_img_copy(aom_image_t *src, aom_image_t *dst) { + dst = aom_img_alloc(dst, src->fmt, src->d_w, src->d_h, 16); + + int plane; + + for (plane = 0; plane < 3; ++plane) { + uint8_t *src_buf = src->planes[plane]; + const int src_stride = src->stride[plane]; + const int src_w = plane == 0 ? src->d_w : src->d_w >> 1; + const int src_h = plane == 0 ? src->d_h : src->d_h >> 1; + + uint8_t *dst_buf = dst->planes[plane]; + const int dst_stride = dst->stride[plane]; + int y; + + for (y = 0; y < src_h; ++y) { + memcpy(dst_buf, src_buf, src_w); + src_buf += src_stride; + dst_buf += dst_stride; + } + } + return dst; +} + +int main(int argc, char **argv) { + int frame_cnt = 0; + FILE *outfile = NULL; + aom_codec_ctx_t codec; + AvxVideoReader *reader = NULL; + const AvxInterface *decoder = NULL; + const AvxVideoInfo *info = NULL; + const char *lf_width_arg; + const char *lf_height_arg; + const char *tile_u_arg; + const char *tile_v_arg; + const char *tile_s_arg; + const char *tile_t_arg; + const char *lf_blocksize_arg; + int lf_width, lf_height; + int tile_u, tile_v, tile_s, tile_t; + int lf_blocksize; + int u_blocks; + int v_blocks; + + exec_name = argv[0]; + + if (argc != 10) die("Invalid number of arguments."); + + reader = aom_video_reader_open(argv[1]); + if (!reader) die("Failed to open %s for reading.", argv[1]); + + if (!(outfile = fopen(argv[2], "wb"))) + die("Failed to open %s for writing.", argv[2]); + + lf_width_arg = argv[3]; + lf_height_arg = argv[4]; + tile_u_arg = argv[5]; + tile_v_arg = argv[6]; + tile_s_arg = argv[7]; + tile_t_arg = argv[8]; + lf_blocksize_arg = argv[9]; + lf_width = (int)strtol(lf_width_arg, NULL, 0); + lf_height = (int)strtol(lf_height_arg, NULL, 0); + tile_u = (int)strtol(tile_u_arg, NULL, 0); + tile_v = (int)strtol(tile_v_arg, NULL, 0); + tile_s = (int)strtol(tile_s_arg, NULL, 0); + tile_t = (int)strtol(tile_t_arg, NULL, 0); + lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0); + + info = aom_video_reader_get_info(reader); + + decoder = get_aom_decoder_by_fourcc(info->codec_fourcc); + if (!decoder) die("Unknown input codec."); + + printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface())); + + if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0)) + die_codec(&codec, "Failed to initialize decoder."); + + // How many reference images we need to encode. + u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize; + v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize; + aom_image_t *reference_images = + (aom_image_t *)malloc(u_blocks * v_blocks * sizeof(aom_image_t)); + for (int bv = 0; bv < v_blocks; ++bv) { + for (int bu = 0; bu < u_blocks; ++bu) { + aom_video_reader_read_frame(reader); + aom_codec_iter_t iter = NULL; + aom_image_t *img = NULL; + size_t frame_size = 0; + const unsigned char *frame = + aom_video_reader_get_frame(reader, &frame_size); + if (aom_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0)) + die_codec(&codec, "Failed to decode frame."); + + while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) { + aom_img_copy(img, &reference_images[bu + bv * u_blocks]); + char name[1024]; + snprintf(name, sizeof(name), "ref_%d_%d.yuv", bu, bv); + printf("writing ref image to %s, %d, %d\n", name, img->d_w, img->d_h); + FILE *ref_file = fopen(name, "wb"); + aom_img_write(img, ref_file); + fclose(ref_file); + ++frame_cnt; + } + } + } + + int decode_frame_index = tile_v * lf_width + tile_u; + do { + aom_video_reader_read_frame(reader); + } while (frame_cnt++ != decode_frame_index); + size_t frame_size = 0; + const unsigned char *frame = aom_video_reader_get_frame(reader, &frame_size); + + int ref_bu = tile_u / lf_blocksize; + int ref_bv = tile_v / lf_blocksize; + int ref_bi = ref_bu + ref_bv * u_blocks; + av1_ref_frame_t ref; + ref.idx = 0; + ref.img = reference_images[ref_bi]; + // This is too slow for real lightfield rendering. This copies the + // reference image bytes. We need a way to just set a pointer + // in order to make this fast enough. + if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) { + die_codec(&codec, "Failed to set reference image."); + } + aom_codec_control_(&codec, AV1_SET_DECODE_TILE_ROW, tile_t); + aom_codec_control_(&codec, AV1_SET_DECODE_TILE_COL, tile_s); + aom_codec_err_t aom_status = + aom_codec_decode(&codec, frame, frame_size, NULL, 0); + if (aom_status) die_codec(&codec, "Failed to decode tile."); + aom_codec_iter_t iter = NULL; + aom_image_t *img = aom_codec_get_frame(&codec, &iter); + aom_img_write(img, outfile); + + if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); + aom_video_reader_close(reader); + fclose(outfile); + + return EXIT_SUCCESS; +} diff --git a/third_party/aom/examples/lightfield_encoder.c b/third_party/aom/examples/lightfield_encoder.c new file mode 100644 index 000000000..0a424db8c --- /dev/null +++ b/third_party/aom/examples/lightfield_encoder.c @@ -0,0 +1,480 @@ +/* + * Copyright (c) 2017, Alliance for Open Media. All rights reserved + * + * This source code is subject to the terms of the BSD 2 Clause License and + * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License + * was not distributed with this source code in the LICENSE file, you can + * obtain it at www.aomedia.org/license/software. If the Alliance for Open + * Media Patent License 1.0 was not distributed with this source code in the + * PATENTS file, you can obtain it at www.aomedia.org/license/patent. + */ + +// Lightfield Encoder +// ================== +// +// This is an example of a simple lightfield encoder. It builds upon the +// twopass_encoder.c example. It takes an input file in YV12 format, +// treating it as a planar lightfield instead of a video. The img_width +// and img_height arguments are the dimensions of the lightfield images, +// while the lf_width and lf_height arguments are the number of +// lightfield images in each dimension. The lf_blocksize determines the +// number of reference images used for MCP. For example, 5 means that there +// is a reference image for every 5x5 lightfield image block. All images +// within a block will use the center image in that block as the reference +// image for MCP. +// Run "make test" to download lightfield test data: vase10x10.yuv. +// Run lightfield encoder to encode whole lightfield: +// examples/lightfield_encoder 1024 1024 vase10x10.yuv vase10x10.webm 10 10 5 + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "aom/aom_encoder.h" +#include "aom/aomcx.h" +#include "av1/common/enums.h" + +#include "../tools_common.h" +#include "../video_writer.h" + +static const char *exec_name; +static const unsigned int deadline = AOM_DL_GOOD_QUALITY; + +void usage_exit(void) { + fprintf(stderr, + "Usage: %s <img_width> <img_height> <infile> <outfile> " + "<lf_width> <lf_height> <lf_blocksize>\n", + exec_name); + exit(EXIT_FAILURE); +} + +static aom_image_t *aom_img_copy(aom_image_t *src, aom_image_t *dst) { + dst = aom_img_alloc(dst, src->fmt, src->d_w, src->d_h, 16); + + int plane; + + for (plane = 0; plane < 3; ++plane) { + unsigned char *src_buf = src->planes[plane]; + const int src_stride = src->stride[plane]; + const int src_w = plane == 0 ? src->d_w : src->d_w >> 1; + const int src_h = plane == 0 ? src->d_h : src->d_h >> 1; + + unsigned char *dst_buf = dst->planes[plane]; + const int dst_stride = dst->stride[plane]; + int y; + + for (y = 0; y < src_h; ++y) { + memcpy(dst_buf, src_buf, src_w); + src_buf += src_stride; + dst_buf += dst_stride; + } + } + return dst; +} + +static int aom_img_size_bytes(aom_image_t *img) { + int image_size_bytes = 0; + int plane; + for (plane = 0; plane < 3; ++plane) { + const int stride = img->stride[plane]; + const int w = aom_img_plane_width(img, plane) * + ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1); + const int h = aom_img_plane_height(img, plane); + image_size_bytes += (w + stride) * h; + } + return image_size_bytes; +} + +static int get_frame_stats(aom_codec_ctx_t *ctx, const aom_image_t *img, + aom_codec_pts_t pts, unsigned int duration, + aom_enc_frame_flags_t flags, unsigned int dl, + aom_fixed_buf_t *stats) { + int got_pkts = 0; + aom_codec_iter_t iter = NULL; + const aom_codec_cx_pkt_t *pkt = NULL; + const aom_codec_err_t res = + aom_codec_encode(ctx, img, pts, duration, flags, dl); + if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to get frame stats."); + + while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) { + got_pkts = 1; + + if (pkt->kind == AOM_CODEC_STATS_PKT) { + const uint8_t *const pkt_buf = pkt->data.twopass_stats.buf; + const size_t pkt_size = pkt->data.twopass_stats.sz; + stats->buf = realloc(stats->buf, stats->sz + pkt_size); + memcpy((uint8_t *)stats->buf + stats->sz, pkt_buf, pkt_size); + stats->sz += pkt_size; + } + } + + return got_pkts; +} + +static int encode_frame(aom_codec_ctx_t *ctx, const aom_image_t *img, + aom_codec_pts_t pts, unsigned int duration, + aom_enc_frame_flags_t flags, unsigned int dl, + AvxVideoWriter *writer) { + int got_pkts = 0; + aom_codec_iter_t iter = NULL; + const aom_codec_cx_pkt_t *pkt = NULL; + const aom_codec_err_t res = + aom_codec_encode(ctx, img, pts, duration, flags, dl); + if (res != AOM_CODEC_OK) die_codec(ctx, "Failed to encode frame."); + + while ((pkt = aom_codec_get_cx_data(ctx, &iter)) != NULL) { + got_pkts = 1; + if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) { + const int keyframe = (pkt->data.frame.flags & AOM_FRAME_IS_KEY) != 0; + + if (!aom_video_writer_write_frame(writer, pkt->data.frame.buf, + pkt->data.frame.sz, + pkt->data.frame.pts)) + die_codec(ctx, "Failed to write compressed frame."); + printf(keyframe ? "K" : "."); + fflush(stdout); + } + } + + return got_pkts; +} + +static aom_fixed_buf_t pass0(aom_image_t *raw, FILE *infile, + const AvxInterface *encoder, + const aom_codec_enc_cfg_t *cfg, int lf_width, + int lf_height, int lf_blocksize) { + aom_codec_ctx_t codec; + int frame_count = 0; + int image_size_bytes = 0; + int u_blocks, v_blocks; + int bu, bv; + aom_fixed_buf_t stats = { NULL, 0 }; + + if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0)) + die_codec(&codec, "Failed to initialize encoder"); + if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 1)) + die_codec(&codec, "Failed to set frame parallel decoding"); + if (aom_codec_control(&codec, AOME_SET_ENABLEAUTOALTREF, 0)) + die_codec(&codec, "Failed to turn off auto altref"); + if (aom_codec_control(&codec, AV1E_SET_SINGLE_TILE_DECODING, 1)) + die_codec(&codec, "Failed to turn on single tile decoding"); + + image_size_bytes = aom_img_size_bytes(raw); + + // How many reference images we need to encode. + u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize; + v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize; + aom_image_t *reference_images = + (aom_image_t *)malloc(u_blocks * v_blocks * sizeof(aom_image_t)); + for (bv = 0; bv < v_blocks; ++bv) { + for (bu = 0; bu < u_blocks; ++bu) { + const int block_u_min = bu * lf_blocksize; + const int block_v_min = bv * lf_blocksize; + int block_u_end = (bu + 1) * lf_blocksize; + int block_v_end = (bv + 1) * lf_blocksize; + int u_block_size, v_block_size; + int block_ref_u, block_ref_v; + struct av1_ref_frame ref_frame; + + block_u_end = block_u_end < lf_width ? block_u_end : lf_width; + block_v_end = block_v_end < lf_height ? block_v_end : lf_height; + u_block_size = block_u_end - block_u_min; + v_block_size = block_v_end - block_v_min; + block_ref_u = block_u_min + u_block_size / 2; + block_ref_v = block_v_min + v_block_size / 2; + fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes, + SEEK_SET); + aom_img_read(raw, infile); + if (aom_codec_control(&codec, AOME_USE_REFERENCE, + AOM_LAST_FLAG | AOM_GOLD_FLAG | AOM_ALT_FLAG)) + die_codec(&codec, "Failed to set reference flags"); + // Reference frames can be encoded encoded without tiles. + ++frame_count; + get_frame_stats(&codec, raw, frame_count, 1, + AOM_EFLAG_FORCE_GF | AOM_EFLAG_NO_UPD_ENTROPY, deadline, + &stats); + ref_frame.idx = 0; + aom_codec_control(&codec, AV1_GET_REFERENCE, &ref_frame); + aom_img_copy(&ref_frame.img, &reference_images[frame_count - 1]); + } + } + for (bv = 0; bv < v_blocks; ++bv) { + for (bu = 0; bu < u_blocks; ++bu) { + const int block_u_min = bu * lf_blocksize; + const int block_v_min = bv * lf_blocksize; + int block_u_end = (bu + 1) * lf_blocksize; + int block_v_end = (bv + 1) * lf_blocksize; + int u, v; + block_u_end = block_u_end < lf_width ? block_u_end : lf_width; + block_v_end = block_v_end < lf_height ? block_v_end : lf_height; + for (v = block_v_min; v < block_v_end; ++v) { + for (u = block_u_min; u < block_u_end; ++u) { + // This was a work around for a bug in libvpx. I'm not sure if this + // same bug exists in current version of av1. Need to call this, + // otherwise the default is to not use any reference frames. Then + // if you don't have at least one AOM_EFLAG_NO_REF_* flag, all frames + // will be intra encoded. I'm not sure why the default is not to use + // any reference frames. It looks like there is something about the + // way I encode the reference frames above that sets that as + // default... + if (aom_codec_control(&codec, AOME_USE_REFERENCE, + AOM_LAST_FLAG | AOM_GOLD_FLAG | AOM_ALT_FLAG)) + die_codec(&codec, "Failed to set reference flags"); + + // Set tile size to 64 pixels. The tile_columns and + // tile_rows in the tile coding are overloaded to represent + // tile_width and tile_height, that range from 1 to 64, in the unit + // of 64 pixels. + if (aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS, 1)) + die_codec(&codec, "Failed to set tile width"); + if (aom_codec_control(&codec, AV1E_SET_TILE_ROWS, 1)) + die_codec(&codec, "Failed to set tile height"); + + av1_ref_frame_t ref; + ref.idx = 0; + ref.img = reference_images[bv * u_blocks + bu]; + if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) + die_codec(&codec, "Failed to set reference frame"); + + fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET); + aom_img_read(raw, infile); + ++frame_count; + get_frame_stats(&codec, raw, frame_count, 1, + AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | + AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY | + AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF, + deadline, &stats); + } + } + } + } + // Flush encoder. + while (get_frame_stats(&codec, NULL, frame_count, 1, 0, deadline, &stats)) { + } + + printf("Pass 0 complete. Processed %d frames.\n", frame_count); + if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec."); + + return stats; +} + +static void pass1(aom_image_t *raw, FILE *infile, const char *outfile_name, + const AvxInterface *encoder, const aom_codec_enc_cfg_t *cfg, + int lf_width, int lf_height, int lf_blocksize) { + AvxVideoInfo info = { encoder->fourcc, + cfg->g_w, + cfg->g_h, + { cfg->g_timebase.num, cfg->g_timebase.den } }; + AvxVideoWriter *writer = NULL; + aom_codec_ctx_t codec; + int frame_count = 0; + int image_size_bytes; + int bu, bv; + int u_blocks, v_blocks; + + writer = aom_video_writer_open(outfile_name, kContainerIVF, &info); + if (!writer) die("Failed to open %s for writing", outfile_name); + + if (aom_codec_enc_init(&codec, encoder->codec_interface(), cfg, 0)) + die_codec(&codec, "Failed to initialize encoder"); + if (aom_codec_control(&codec, AV1E_SET_FRAME_PARALLEL_DECODING, 1)) + die_codec(&codec, "Failed to set frame parallel decoding"); + if (aom_codec_control(&codec, AOME_SET_ENABLEAUTOALTREF, 0)) + die_codec(&codec, "Failed to turn off auto altref"); + if (aom_codec_control(&codec, AV1E_SET_SINGLE_TILE_DECODING, 1)) + die_codec(&codec, "Failed to turn on single tile decoding"); + + image_size_bytes = aom_img_size_bytes(raw); + u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize; + v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize; + aom_image_t *reference_images = + (aom_image_t *)malloc(u_blocks * v_blocks * sizeof(aom_image_t)); + // Encode reference images first. + printf("Encoding Reference Images\n"); + for (bv = 0; bv < v_blocks; ++bv) { + for (bu = 0; bu < u_blocks; ++bu) { + const int block_u_min = bu * lf_blocksize; + const int block_v_min = bv * lf_blocksize; + int block_u_end = (bu + 1) * lf_blocksize; + int block_v_end = (bv + 1) * lf_blocksize; + int u_block_size, v_block_size; + int block_ref_u, block_ref_v; + struct av1_ref_frame ref_frame; + + block_u_end = block_u_end < lf_width ? block_u_end : lf_width; + block_v_end = block_v_end < lf_height ? block_v_end : lf_height; + u_block_size = block_u_end - block_u_min; + v_block_size = block_v_end - block_v_min; + block_ref_u = block_u_min + u_block_size / 2; + block_ref_v = block_v_min + v_block_size / 2; + fseek(infile, (block_ref_u + block_ref_v * lf_width) * image_size_bytes, + SEEK_SET); + aom_img_read(raw, infile); + if (aom_codec_control(&codec, AOME_USE_REFERENCE, + AOM_LAST_FLAG | AOM_GOLD_FLAG | AOM_ALT_FLAG)) + die_codec(&codec, "Failed to set reference flags"); + // Reference frames may be encoded without tiles. + ++frame_count; + printf("Encoding reference image %d of %d\n", bv * u_blocks + bu, + u_blocks * v_blocks); + encode_frame(&codec, raw, frame_count, 1, + AOM_EFLAG_FORCE_GF | AOM_EFLAG_NO_UPD_ENTROPY, deadline, + writer); + ref_frame.idx = 0; + aom_codec_control(&codec, AV1_GET_REFERENCE, &ref_frame); + aom_img_copy(&ref_frame.img, &reference_images[frame_count - 1]); + } + } + + for (bv = 0; bv < v_blocks; ++bv) { + for (bu = 0; bu < u_blocks; ++bu) { + const int block_u_min = bu * lf_blocksize; + const int block_v_min = bv * lf_blocksize; + int block_u_end = (bu + 1) * lf_blocksize; + int block_v_end = (bv + 1) * lf_blocksize; + int u, v; + block_u_end = block_u_end < lf_width ? block_u_end : lf_width; + block_v_end = block_v_end < lf_height ? block_v_end : lf_height; + for (v = block_v_min; v < block_v_end; ++v) { + for (u = block_u_min; u < block_u_end; ++u) { + // This was a work around for a bug in libvpx. I'm not sure if this + // same bug exists in current version of av1. Need to call this, + // otherwise the default is to not use any reference frames. Then + // if you don't have at least one AOM_EFLAG_NO_REF_* flag, all frames + // will be intra encoded. I'm not sure why the default is not to use + // any reference frames. It looks like there is something about the + // way I encode the reference frames above that sets that as + // default... + if (aom_codec_control(&codec, AOME_USE_REFERENCE, + AOM_LAST_FLAG | AOM_GOLD_FLAG | AOM_ALT_FLAG)) + die_codec(&codec, "Failed to set reference flags"); + + // Set tile size to 64 pixels. The tile_columns and + // tile_rows in the tile coding are overloaded to represent tile_width + // and tile_height, that range from 1 to 64, in the unit of 64 pixels. + if (aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS, 1)) + die_codec(&codec, "Failed to set tile width"); + if (aom_codec_control(&codec, AV1E_SET_TILE_ROWS, 1)) + die_codec(&codec, "Failed to set tile height"); + + av1_ref_frame_t ref; + ref.idx = 0; + ref.img = reference_images[bv * u_blocks + bu]; + if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) + die_codec(&codec, "Failed to set reference frame"); + fseek(infile, (u + v * lf_width) * image_size_bytes, SEEK_SET); + aom_img_read(raw, infile); + ++frame_count; + + printf("Encoding image %d of %d\n", + frame_count - (u_blocks * v_blocks), lf_width * lf_height); + encode_frame(&codec, raw, frame_count, 1, + AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | + AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY | + AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF, + deadline, writer); + } + } + } + } + + // Flush encoder. + while (encode_frame(&codec, NULL, -1, 1, 0, deadline, writer)) { + } + + if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec."); + + aom_video_writer_close(writer); + + printf("Pass 1 complete. Processed %d frames.\n", frame_count); +} + +int main(int argc, char **argv) { + FILE *infile = NULL; + int w, h; + // The number of lightfield images in the u and v dimensions. + int lf_width, lf_height; + // Defines how many images refer to the same reference image for MCP. + // lf_blocksize X lf_blocksize images will all use the reference image + // in the middle of the block of images. + int lf_blocksize; + aom_codec_ctx_t codec; + aom_codec_enc_cfg_t cfg; + aom_image_t raw; + aom_codec_err_t res; + aom_fixed_buf_t stats; + + const AvxInterface *encoder = NULL; + const int fps = 30; + const int bitrate = 200; // kbit/s + const char *const width_arg = argv[1]; + const char *const height_arg = argv[2]; + const char *const infile_arg = argv[3]; + const char *const outfile_arg = argv[4]; + const char *const lf_width_arg = argv[5]; + const char *const lf_height_arg = argv[6]; + const char *lf_blocksize_arg = argv[7]; + exec_name = argv[0]; + + if (argc < 8) die("Invalid number of arguments"); + + encoder = get_aom_encoder_by_name("av1"); + if (!encoder) die("Unsupported codec."); + + w = (int)strtol(width_arg, NULL, 0); + h = (int)strtol(height_arg, NULL, 0); + lf_width = (int)strtol(lf_width_arg, NULL, 0); + lf_height = (int)strtol(lf_height_arg, NULL, 0); + lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0); + lf_blocksize = lf_blocksize < lf_width ? lf_blocksize : lf_width; + lf_blocksize = lf_blocksize < lf_height ? lf_blocksize : lf_height; + + if (w <= 0 || h <= 0 || (w % 2) != 0 || (h % 2) != 0) + die("Invalid frame size: %dx%d", w, h); + if (lf_width <= 0 || lf_height <= 0) + die("Invalid lf_width and/or lf_height: %dx%d", lf_width, lf_height); + if (lf_blocksize <= 0) die("Invalid lf_blocksize: %d", lf_blocksize); + + if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, w, h, 1)) + die("Failed to allocate image", w, h); + + printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface())); + + // Configuration + res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0); + + if (res) die_codec(&codec, "Failed to get default codec config."); + + cfg.g_w = w; + cfg.g_h = h; + cfg.g_timebase.num = 1; + cfg.g_timebase.den = fps; + cfg.rc_target_bitrate = bitrate; + cfg.g_error_resilient = AOM_ERROR_RESILIENT_DEFAULT; + // Need to set lag_in_frames to 1 or 0. Otherwise the frame flags get + // overridden after the first frame in encode_frame_to_data_rate() (see where + // get_frame_flags() is called). + cfg.g_lag_in_frames = 0; + cfg.kf_mode = AOM_KF_DISABLED; + cfg.large_scale_tile = 1; + + if (!(infile = fopen(infile_arg, "rb"))) + die("Failed to open %s for reading", infile_arg); + + // Pass 0 + cfg.g_pass = AOM_RC_FIRST_PASS; + stats = pass0(&raw, infile, encoder, &cfg, lf_width, lf_height, lf_blocksize); + + // Pass 1 + rewind(infile); + cfg.g_pass = AOM_RC_LAST_PASS; + cfg.rc_twopass_stats_in = stats; + pass1(&raw, infile, outfile_arg, encoder, &cfg, lf_width, lf_height, + lf_blocksize); + free(stats.buf); + + aom_img_free(&raw); + fclose(infile); + + return EXIT_SUCCESS; +} |