Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 1 | /* |
Krishna Rapaka | 7319db5 | 2021-09-28 20:35:29 -0700 | [diff] [blame] | 2 | * Copyright (c) 2021, Alliance for Open Media. All rights reserved |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 3 | * |
Vibhoothi | 41c6dd7 | 2021-10-12 18:48:26 +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/. |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | // Lightfield Bitstream Parsing |
| 14 | // ============================ |
| 15 | // |
Yunqing Wang | ce28545 | 2018-06-25 14:51:51 -0700 | [diff] [blame] | 16 | // This is a lightfield bitstream parsing example. It takes an input file |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 17 | // containing the whole compressed lightfield bitstream(ivf file) and a text |
| 18 | // file containing a stream of tiles to decode and then constructs and outputs |
| 19 | // a new bitstream that can be decoded by an AV1 decoder. The output bitstream |
| 20 | // contains reference frames(i.e. anchor frames), camera frame header, and |
| 21 | // tile list OBUs. num_references is the number of anchor frames coded at the |
| 22 | // beginning of the light field file. After running the lightfield encoder, |
| 23 | // run lightfield bitstream parsing: |
Yunqing Wang | ce28545 | 2018-06-25 14:51:51 -0700 | [diff] [blame] | 24 | // examples/lightfield_bitstream_parsing vase10x10.ivf vase_tile_list.ivf 4 |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 25 | // tile_list.txt |
| 26 | // |
| 27 | // The tile_list.txt is expected to be of the form: |
| 28 | // Frame <frame_index0> |
| 29 | // <image_index0> <anchor_index0> <tile_col0> <tile_row0> |
| 30 | // <image_index1> <anchor_index1> <tile_col1> <tile_row1> |
| 31 | // ... |
| 32 | // Frame <frame_index1) |
| 33 | // ... |
| 34 | // |
| 35 | // The "Frame" markers indicate a new render frame and thus a new tile list |
| 36 | // will be started and the old one flushed. The image_indexN, anchor_indexN, |
| 37 | // tile_colN, and tile_rowN identify an individual tile to be decoded and |
| 38 | // to use anchor_indexN anchor image for MCP. |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 43 | |
| 44 | #include "aom/aom_decoder.h" |
| 45 | #include "aom/aom_encoder.h" |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 46 | #include "aom/aom_integer.h" |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 47 | #include "aom/aomdx.h" |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 48 | #include "aom_dsp/bitwriter_buffer.h" |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 49 | #include "common/tools_common.h" |
| 50 | #include "common/video_reader.h" |
| 51 | #include "common/video_writer.h" |
| 52 | |
Yunqing Wang | d74f568 | 2018-07-12 14:34:56 -0700 | [diff] [blame] | 53 | #define MAX_TILES 512 |
| 54 | |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 55 | static const char *exec_name; |
| 56 | |
| 57 | void usage_exit(void) { |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 58 | fprintf(stderr, "Usage: %s <infile> <outfile> <num_references> <tile_list>\n", |
Ryan Overbeck | 49a8217 | 2018-06-21 15:24:36 -0700 | [diff] [blame] | 59 | exec_name); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 60 | exit(EXIT_FAILURE); |
| 61 | } |
| 62 | |
| 63 | #define ALIGN_POWER_OF_TWO(value, n) \ |
| 64 | (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1)) |
| 65 | |
Yunqing Wang | 6ff4809 | 2018-11-13 14:10:48 -0800 | [diff] [blame] | 66 | const int output_frame_width = 512; |
| 67 | const int output_frame_height = 512; |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 68 | |
| 69 | // Spec: |
| 70 | // typedef struct { |
| 71 | // uint8_t anchor_frame_idx; |
| 72 | // uint8_t tile_row; |
| 73 | // uint8_t tile_col; |
| 74 | // uint16_t coded_tile_data_size_minus_1; |
| 75 | // uint8_t *coded_tile_data; |
| 76 | // } TILE_LIST_ENTRY; |
| 77 | |
| 78 | // Tile list entry provided by the application |
| 79 | typedef struct { |
| 80 | int image_idx; |
| 81 | int reference_idx; |
| 82 | int tile_col; |
| 83 | int tile_row; |
| 84 | } TILE_LIST_INFO; |
| 85 | |
Yunqing Wang | d74f568 | 2018-07-12 14:34:56 -0700 | [diff] [blame] | 86 | static int get_image_bps(aom_img_fmt_t fmt) { |
| 87 | switch (fmt) { |
| 88 | case AOM_IMG_FMT_I420: return 12; |
| 89 | case AOM_IMG_FMT_I422: return 16; |
| 90 | case AOM_IMG_FMT_I444: return 24; |
| 91 | case AOM_IMG_FMT_I42016: return 24; |
| 92 | case AOM_IMG_FMT_I42216: return 32; |
| 93 | case AOM_IMG_FMT_I44416: return 48; |
| 94 | default: die("Invalid image format"); |
| 95 | } |
Yaowu Xu | 0415f2e | 2018-07-23 11:48:53 -0700 | [diff] [blame] | 96 | return 0; |
Yunqing Wang | d74f568 | 2018-07-12 14:34:56 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 99 | void process_tile_list(const TILE_LIST_INFO *tiles, int num_tiles, |
| 100 | aom_codec_pts_t tl_pts, unsigned char **frames, |
| 101 | const size_t *frame_sizes, aom_codec_ctx_t *codec, |
Yunqing Wang | 6ff4809 | 2018-11-13 14:10:48 -0800 | [diff] [blame] | 102 | unsigned char *tl_buf, AvxVideoWriter *writer, |
| 103 | uint8_t output_frame_width_in_tiles_minus_1, |
| 104 | uint8_t output_frame_height_in_tiles_minus_1) { |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 105 | unsigned char *tl = tl_buf; |
| 106 | struct aom_write_bit_buffer wb = { tl, 0 }; |
| 107 | unsigned char *saved_obu_size_loc = NULL; |
| 108 | uint32_t tile_list_obu_header_size = 0; |
| 109 | uint32_t tile_list_obu_size = 0; |
| 110 | int num_tiles_minus_1 = num_tiles - 1; |
| 111 | int i; |
| 112 | |
| 113 | // Write the tile list OBU header that is 1 byte long. |
| 114 | aom_wb_write_literal(&wb, 0, 1); // forbidden bit. |
| 115 | aom_wb_write_literal(&wb, 8, 4); // tile list OBU: "1000" |
| 116 | aom_wb_write_literal(&wb, 0, 1); // obu_extension = 0 |
| 117 | aom_wb_write_literal(&wb, 1, 1); // obu_has_size_field |
| 118 | aom_wb_write_literal(&wb, 0, 1); // reserved |
| 119 | tl++; |
| 120 | tile_list_obu_header_size++; |
| 121 | |
| 122 | // Write the OBU size using a fixed length_field_size of 4 bytes. |
| 123 | saved_obu_size_loc = tl; |
| 124 | // aom_wb_write_unsigned_literal(&wb, data, bits) requires that bits <= 32. |
| 125 | aom_wb_write_unsigned_literal(&wb, 0, 32); |
| 126 | tl += 4; |
| 127 | tile_list_obu_header_size += 4; |
| 128 | |
| 129 | // write_tile_list_obu() |
| 130 | aom_wb_write_literal(&wb, output_frame_width_in_tiles_minus_1, 8); |
| 131 | aom_wb_write_literal(&wb, output_frame_height_in_tiles_minus_1, 8); |
| 132 | aom_wb_write_literal(&wb, num_tiles_minus_1, 16); |
| 133 | tl += 4; |
| 134 | tile_list_obu_size += 4; |
| 135 | |
| 136 | // Write each tile's data |
| 137 | for (i = 0; i <= num_tiles_minus_1; i++) { |
| 138 | aom_tile_data tile_data = { 0, NULL, 0 }; |
| 139 | |
| 140 | int image_idx = tiles[i].image_idx; |
| 141 | int ref_idx = tiles[i].reference_idx; |
| 142 | int tc = tiles[i].tile_col; |
| 143 | int tr = tiles[i].tile_row; |
| 144 | |
| 145 | // Reset bit writer to the right location. |
| 146 | wb.bit_buffer = tl; |
| 147 | wb.bit_offset = 0; |
| 148 | |
| 149 | size_t frame_size = frame_sizes[image_idx]; |
| 150 | const unsigned char *frame = frames[image_idx]; |
| 151 | |
Elliott Karpilovsky | 60dd0f9 | 2020-04-30 19:19:27 -0700 | [diff] [blame] | 152 | AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1_SET_DECODE_TILE_ROW, tr); |
| 153 | AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1_SET_DECODE_TILE_COL, tc); |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 154 | |
| 155 | aom_codec_err_t aom_status = |
| 156 | aom_codec_decode(codec, frame, frame_size, NULL); |
| 157 | if (aom_status) die_codec(codec, "Failed to decode tile."); |
| 158 | |
Elliott Karpilovsky | 60dd0f9 | 2020-04-30 19:19:27 -0700 | [diff] [blame] | 159 | AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1D_GET_TILE_DATA, &tile_data); |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 160 | |
| 161 | // Copy over tile info. |
| 162 | // uint8_t anchor_frame_idx; |
| 163 | // uint8_t tile_row; |
| 164 | // uint8_t tile_col; |
| 165 | // uint16_t coded_tile_data_size_minus_1; |
| 166 | // uint8_t *coded_tile_data; |
| 167 | uint32_t tile_info_bytes = 5; |
| 168 | aom_wb_write_literal(&wb, ref_idx, 8); |
| 169 | aom_wb_write_literal(&wb, tr, 8); |
| 170 | aom_wb_write_literal(&wb, tc, 8); |
| 171 | aom_wb_write_literal(&wb, (int)tile_data.coded_tile_data_size - 1, 16); |
| 172 | tl += tile_info_bytes; |
| 173 | |
| 174 | memcpy(tl, (uint8_t *)tile_data.coded_tile_data, |
| 175 | tile_data.coded_tile_data_size); |
| 176 | tl += tile_data.coded_tile_data_size; |
| 177 | |
| 178 | tile_list_obu_size += |
| 179 | tile_info_bytes + (uint32_t)tile_data.coded_tile_data_size; |
| 180 | } |
| 181 | |
| 182 | // Write tile list OBU size. |
| 183 | size_t bytes_written = 0; |
| 184 | if (aom_uleb_encode_fixed_size(tile_list_obu_size, 4, 4, saved_obu_size_loc, |
| 185 | &bytes_written)) |
| 186 | die_codec(codec, "Failed to encode the tile list obu size."); |
| 187 | |
| 188 | // Copy the tile list. |
| 189 | if (!aom_video_writer_write_frame( |
| 190 | writer, tl_buf, tile_list_obu_header_size + tile_list_obu_size, |
| 191 | tl_pts)) |
| 192 | die_codec(codec, "Failed to copy compressed tile list."); |
| 193 | } |
| 194 | |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 195 | int main(int argc, char **argv) { |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 196 | AvxVideoReader *reader = NULL; |
| 197 | AvxVideoWriter *writer = NULL; |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 198 | const AvxVideoInfo *info = NULL; |
Ryan Overbeck | 49a8217 | 2018-06-21 15:24:36 -0700 | [diff] [blame] | 199 | int num_references; |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 200 | int i; |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 201 | aom_codec_pts_t pts; |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 202 | const char *tile_list_file = NULL; |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 203 | |
| 204 | exec_name = argv[0]; |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 205 | if (argc != 5) die("Invalid number of arguments."); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 206 | |
| 207 | reader = aom_video_reader_open(argv[1]); |
| 208 | if (!reader) die("Failed to open %s for reading.", argv[1]); |
| 209 | |
Ryan Overbeck | 49a8217 | 2018-06-21 15:24:36 -0700 | [diff] [blame] | 210 | num_references = (int)strtol(argv[3], NULL, 0); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 211 | info = aom_video_reader_get_info(reader); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 212 | |
Yunqing Wang | 3ae70c9 | 2019-01-25 17:34:07 -0800 | [diff] [blame] | 213 | aom_video_reader_set_fourcc(reader, AV1_FOURCC); |
| 214 | |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 215 | // The writer to write out ivf file in tile list OBU, which can be decoded by |
| 216 | // AV1 decoder. |
| 217 | writer = aom_video_writer_open(argv[2], kContainerIVF, info); |
| 218 | if (!writer) die("Failed to open %s for writing", argv[2]); |
| 219 | |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 220 | tile_list_file = argv[4]; |
| 221 | |
Elliott Karpilovsky | cbe219b | 2020-04-22 16:21:06 -0700 | [diff] [blame] | 222 | aom_codec_iface_t *decoder = get_aom_decoder_by_fourcc(info->codec_fourcc); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 223 | if (!decoder) die("Unknown input codec."); |
Elliott Karpilovsky | cbe219b | 2020-04-22 16:21:06 -0700 | [diff] [blame] | 224 | printf("Using %s\n", aom_codec_iface_name(decoder)); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 225 | |
Elliott Karpilovsky | cbe219b | 2020-04-22 16:21:06 -0700 | [diff] [blame] | 226 | aom_codec_ctx_t codec; |
| 227 | if (aom_codec_dec_init(&codec, decoder, NULL, 0)) |
James Zern | dad374f | 2020-05-06 19:13:19 -0700 | [diff] [blame] | 228 | die("Failed to initialize decoder."); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 229 | |
| 230 | // Decode anchor frames. |
Elliott Karpilovsky | 60dd0f9 | 2020-04-30 19:19:27 -0700 | [diff] [blame] | 231 | AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1_SET_TILE_MODE, 0); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 232 | |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 233 | printf("Reading %d reference images.\n", num_references); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 234 | for (i = 0; i < num_references; ++i) { |
| 235 | aom_video_reader_read_frame(reader); |
| 236 | |
| 237 | size_t frame_size = 0; |
| 238 | const unsigned char *frame = |
| 239 | aom_video_reader_get_frame(reader, &frame_size); |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 240 | pts = (aom_codec_pts_t)aom_video_reader_get_frame_pts(reader); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 241 | |
| 242 | // Copy references bitstream directly. |
| 243 | if (!aom_video_writer_write_frame(writer, frame, frame_size, pts)) |
| 244 | die_codec(&codec, "Failed to copy compressed anchor frame."); |
| 245 | |
| 246 | if (aom_codec_decode(&codec, frame, frame_size, NULL)) |
| 247 | die_codec(&codec, "Failed to decode frame."); |
| 248 | } |
| 249 | |
| 250 | // Decode camera frames. |
Elliott Karpilovsky | 60dd0f9 | 2020-04-30 19:19:27 -0700 | [diff] [blame] | 251 | AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1_SET_TILE_MODE, 1); |
| 252 | AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_EXT_TILE_DEBUG, 1); |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 253 | |
| 254 | FILE *infile = aom_video_reader_get_file(reader); |
| 255 | // Record the offset of the first camera image. |
| 256 | const FileOffset camera_frame_pos = ftello(infile); |
| 257 | |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 258 | printf("Loading compressed frames into memory.\n"); |
| 259 | |
| 260 | // Count the frames in the lightfield. |
| 261 | int num_frames = 0; |
| 262 | while (aom_video_reader_read_frame(reader)) { |
| 263 | ++num_frames; |
| 264 | } |
| 265 | if (num_frames < 1) die("Input light field has no frames."); |
| 266 | |
| 267 | // Read all of the lightfield frames into memory. |
| 268 | unsigned char **frames = |
| 269 | (unsigned char **)malloc(num_frames * sizeof(unsigned char *)); |
| 270 | size_t *frame_sizes = (size_t *)malloc(num_frames * sizeof(size_t)); |
| 271 | // Seek to the first camera image. |
| 272 | fseeko(infile, camera_frame_pos, SEEK_SET); |
| 273 | for (int f = 0; f < num_frames; ++f) { |
| 274 | aom_video_reader_read_frame(reader); |
| 275 | size_t frame_size = 0; |
| 276 | const unsigned char *frame = |
| 277 | aom_video_reader_get_frame(reader, &frame_size); |
| 278 | frames[f] = (unsigned char *)malloc(frame_size * sizeof(unsigned char)); |
| 279 | memcpy(frames[f], frame, frame_size); |
| 280 | frame_sizes[f] = frame_size; |
| 281 | } |
| 282 | printf("Read %d frames.\n", num_frames); |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 283 | |
| 284 | // Copy first camera frame for getting camera frame header. This is done |
| 285 | // only once. |
| 286 | { |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 287 | size_t frame_size = frame_sizes[0]; |
| 288 | const unsigned char *frame = frames[0]; |
| 289 | pts = num_references; |
Yunqing Wang | 3f9c1e2 | 2018-06-16 16:48:15 -0700 | [diff] [blame] | 290 | aom_tile_data frame_header_info = { 0, NULL, 0 }; |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 291 | |
Yunqing Wang | 3f9c1e2 | 2018-06-16 16:48:15 -0700 | [diff] [blame] | 292 | // Need to decode frame header to get camera frame header info. So, here |
| 293 | // decoding 1 tile is enough. |
Elliott Karpilovsky | 60dd0f9 | 2020-04-30 19:19:27 -0700 | [diff] [blame] | 294 | AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1_SET_DECODE_TILE_ROW, 0); |
| 295 | AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1_SET_DECODE_TILE_COL, 0); |
Yunqing Wang | 3f9c1e2 | 2018-06-16 16:48:15 -0700 | [diff] [blame] | 296 | |
| 297 | aom_codec_err_t aom_status = |
| 298 | aom_codec_decode(&codec, frame, frame_size, NULL); |
| 299 | if (aom_status) die_codec(&codec, "Failed to decode tile."); |
| 300 | |
Elliott Karpilovsky | 60dd0f9 | 2020-04-30 19:19:27 -0700 | [diff] [blame] | 301 | AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_GET_FRAME_HEADER_INFO, |
| 302 | &frame_header_info); |
Yunqing Wang | 3f9c1e2 | 2018-06-16 16:48:15 -0700 | [diff] [blame] | 303 | |
| 304 | size_t obu_size_offset = |
| 305 | (uint8_t *)frame_header_info.coded_tile_data - frame; |
| 306 | size_t length_field_size = frame_header_info.coded_tile_data_size; |
Yaowu Xu | 6fbfae3 | 2018-06-18 10:58:11 -0700 | [diff] [blame] | 307 | // Remove ext-tile tile info. |
| 308 | uint32_t frame_header_size = (uint32_t)frame_header_info.extra_size - 1; |
Yunqing Wang | 3f9c1e2 | 2018-06-16 16:48:15 -0700 | [diff] [blame] | 309 | size_t bytes_to_copy = |
| 310 | obu_size_offset + length_field_size + frame_header_size; |
| 311 | |
| 312 | unsigned char *frame_hdr_buf = (unsigned char *)malloc(bytes_to_copy); |
| 313 | if (frame_hdr_buf == NULL) |
| 314 | die_codec(&codec, "Failed to allocate frame header buffer."); |
| 315 | |
| 316 | memcpy(frame_hdr_buf, frame, bytes_to_copy); |
| 317 | |
| 318 | // Update frame header OBU size. |
| 319 | size_t bytes_written = 0; |
| 320 | if (aom_uleb_encode_fixed_size( |
| 321 | frame_header_size, length_field_size, length_field_size, |
| 322 | frame_hdr_buf + obu_size_offset, &bytes_written)) |
| 323 | die_codec(&codec, "Failed to encode the tile list obu size."); |
| 324 | |
| 325 | // Copy camera frame header bitstream. |
| 326 | if (!aom_video_writer_write_frame(writer, frame_hdr_buf, bytes_to_copy, |
| 327 | pts)) |
| 328 | die_codec(&codec, "Failed to copy compressed camera frame header."); |
Yunqing Wang | aecdda8 | 2018-07-26 12:41:56 -0700 | [diff] [blame] | 329 | free(frame_hdr_buf); |
Yunqing Wang | 946ec2b | 2018-06-06 12:34:23 -0700 | [diff] [blame] | 330 | } |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 331 | |
Yunqing Wang | d74f568 | 2018-07-12 14:34:56 -0700 | [diff] [blame] | 332 | // Read out the image format. |
| 333 | aom_img_fmt_t ref_fmt = 0; |
Elliott Karpilovsky | 60dd0f9 | 2020-04-30 19:19:27 -0700 | [diff] [blame] | 334 | if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_GET_IMG_FORMAT, &ref_fmt)) |
Yunqing Wang | d74f568 | 2018-07-12 14:34:56 -0700 | [diff] [blame] | 335 | die_codec(&codec, "Failed to get the image format"); |
| 336 | const int bps = get_image_bps(ref_fmt); |
Yunqing Wang | aecdda8 | 2018-07-26 12:41:56 -0700 | [diff] [blame] | 337 | if (!bps) die_codec(&codec, "Invalid image format."); |
Yunqing Wang | d74f568 | 2018-07-12 14:34:56 -0700 | [diff] [blame] | 338 | // read out the tile size. |
| 339 | unsigned int tile_size = 0; |
Elliott Karpilovsky | 60dd0f9 | 2020-04-30 19:19:27 -0700 | [diff] [blame] | 340 | if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_GET_TILE_SIZE, &tile_size)) |
Yunqing Wang | d74f568 | 2018-07-12 14:34:56 -0700 | [diff] [blame] | 341 | die_codec(&codec, "Failed to get the tile size"); |
| 342 | const unsigned int tile_width = tile_size >> 16; |
| 343 | const unsigned int tile_height = tile_size & 65535; |
| 344 | // Allocate a buffer to store tile list bitstream. |
| 345 | const size_t data_sz = MAX_TILES * ALIGN_POWER_OF_TWO(tile_width, 5) * |
| 346 | ALIGN_POWER_OF_TWO(tile_height, 5) * bps / 8; |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 347 | |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 348 | unsigned char *tl_buf = (unsigned char *)malloc(data_sz); |
| 349 | if (tl_buf == NULL) die_codec(&codec, "Failed to allocate tile list buffer."); |
| 350 | |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 351 | aom_codec_pts_t tl_pts = num_references; |
Yunqing Wang | 6ff4809 | 2018-11-13 14:10:48 -0800 | [diff] [blame] | 352 | const uint8_t output_frame_width_in_tiles_minus_1 = |
| 353 | output_frame_width / tile_width - 1; |
| 354 | const uint8_t output_frame_height_in_tiles_minus_1 = |
| 355 | output_frame_height / tile_height - 1; |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 356 | |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 357 | printf("Reading tile list from file.\n"); |
| 358 | char line[1024]; |
| 359 | FILE *tile_list_fptr = fopen(tile_list_file, "r"); |
David Turner | 69c98e4 | 2018-11-09 14:17:20 +0000 | [diff] [blame] | 360 | if (!tile_list_fptr) die_codec(&codec, "Failed to open tile list file."); |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 361 | int num_tiles = 0; |
| 362 | TILE_LIST_INFO tiles[MAX_TILES]; |
| 363 | while ((fgets(line, 1024, tile_list_fptr)) != NULL) { |
| 364 | if (line[0] == 'F' || num_tiles >= MAX_TILES) { |
| 365 | // Flush existing tile list and start another, either because we hit a |
| 366 | // new render frame or because we've hit our max number of tiles per list. |
| 367 | if (num_tiles > 0) { |
| 368 | process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec, |
Yunqing Wang | 6ff4809 | 2018-11-13 14:10:48 -0800 | [diff] [blame] | 369 | tl_buf, writer, output_frame_width_in_tiles_minus_1, |
| 370 | output_frame_height_in_tiles_minus_1); |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 371 | ++tl_pts; |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 372 | } |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 373 | num_tiles = 0; |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 374 | } |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 375 | if (line[0] == 'F') { |
| 376 | continue; |
| 377 | } |
| 378 | if (sscanf(line, "%d %d %d %d", &tiles[num_tiles].image_idx, |
| 379 | &tiles[num_tiles].reference_idx, &tiles[num_tiles].tile_col, |
| 380 | &tiles[num_tiles].tile_row) == 4) { |
| 381 | if (tiles[num_tiles].image_idx >= num_frames) { |
| 382 | die("Tile list image_idx out of bounds: %d >= %d.", |
| 383 | tiles[num_tiles].image_idx, num_frames); |
| 384 | } |
| 385 | if (tiles[num_tiles].reference_idx >= num_references) { |
| 386 | die("Tile list reference_idx out of bounds: %d >= %d.", |
| 387 | tiles[num_tiles].reference_idx, num_references); |
| 388 | } |
| 389 | ++num_tiles; |
| 390 | } |
| 391 | } |
| 392 | if (num_tiles > 0) { |
| 393 | // Flush out the last tile list. |
| 394 | process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec, |
Yunqing Wang | 6ff4809 | 2018-11-13 14:10:48 -0800 | [diff] [blame] | 395 | tl_buf, writer, output_frame_width_in_tiles_minus_1, |
| 396 | output_frame_height_in_tiles_minus_1); |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 397 | ++tl_pts; |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Zoe Liu | 81738a2 | 2018-09-27 11:40:08 -0700 | [diff] [blame] | 400 | const int num_tile_lists = (int)(tl_pts - pts); |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 401 | printf("Finished processing tile lists. Num tile lists: %d.\n", |
| 402 | num_tile_lists); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 403 | free(tl_buf); |
Ryan Overbeck | 1852139 | 2018-09-21 09:43:24 -0700 | [diff] [blame] | 404 | for (int f = 0; f < num_frames; ++f) { |
| 405 | free(frames[f]); |
| 406 | } |
| 407 | free(frame_sizes); |
| 408 | free(frames); |
Yunqing Wang | 40bde4d | 2018-06-01 16:48:03 -0700 | [diff] [blame] | 409 | if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); |
| 410 | aom_video_writer_close(writer); |
| 411 | aom_video_reader_close(reader); |
| 412 | |
| 413 | return EXIT_SUCCESS; |
| 414 | } |