45 #include "aom/aom_integer.h"
47 #include "aom_dsp/bitwriter_buffer.h"
48 #include "common/tools_common.h"
49 #include "common/video_reader.h"
50 #include "common/video_writer.h"
54 static const char *exec_name;
56 void usage_exit(
void) {
57 fprintf(stderr,
"Usage: %s <infile> <outfile> <num_references> <tile_list>\n",
62 #define ALIGN_POWER_OF_TWO(value, n) \
63 (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
65 const int output_frame_width = 512;
66 const int output_frame_height = 512;
93 default: die(
"Invalid image format");
98 void process_tile_list(
const TILE_LIST_INFO *tiles,
int num_tiles,
101 unsigned char *tl_buf, AvxVideoWriter *writer,
102 uint8_t output_frame_width_in_tiles_minus_1,
103 uint8_t output_frame_height_in_tiles_minus_1) {
104 unsigned char *tl = tl_buf;
105 struct aom_write_bit_buffer wb = { tl, 0 };
106 unsigned char *saved_obu_size_loc = NULL;
107 uint32_t tile_list_obu_header_size = 0;
108 uint32_t tile_list_obu_size = 0;
109 int num_tiles_minus_1 = num_tiles - 1;
113 aom_wb_write_literal(&wb, 0, 1);
114 aom_wb_write_literal(&wb, 8, 4);
115 aom_wb_write_literal(&wb, 0, 1);
116 aom_wb_write_literal(&wb, 1, 1);
117 aom_wb_write_literal(&wb, 0, 1);
119 tile_list_obu_header_size++;
122 saved_obu_size_loc = tl;
124 aom_wb_write_unsigned_literal(&wb, 0, 32);
126 tile_list_obu_header_size += 4;
129 aom_wb_write_literal(&wb, output_frame_width_in_tiles_minus_1, 8);
130 aom_wb_write_literal(&wb, output_frame_height_in_tiles_minus_1, 8);
131 aom_wb_write_literal(&wb, num_tiles_minus_1, 16);
133 tile_list_obu_size += 4;
136 for (i = 0; i <= num_tiles_minus_1; i++) {
139 int image_idx = tiles[i].image_idx;
140 int ref_idx = tiles[i].reference_idx;
141 int tc = tiles[i].tile_col;
142 int tr = tiles[i].tile_row;
148 size_t frame_size = frame_sizes[image_idx];
149 const unsigned char *frame = frames[image_idx];
156 if (aom_status) die_codec(codec,
"Failed to decode tile.");
166 uint32_t tile_info_bytes = 5;
167 aom_wb_write_literal(&wb, ref_idx, 8);
168 aom_wb_write_literal(&wb, tr, 8);
169 aom_wb_write_literal(&wb, tc, 8);
171 tl += tile_info_bytes;
177 tile_list_obu_size +=
182 size_t bytes_written = 0;
183 if (aom_uleb_encode_fixed_size(tile_list_obu_size, 4, 4, saved_obu_size_loc,
185 die_codec(codec,
"Failed to encode the tile list obu size.");
188 if (!aom_video_writer_write_frame(
189 writer, tl_buf, tile_list_obu_header_size + tile_list_obu_size,
191 die_codec(codec,
"Failed to copy compressed tile list.");
194 int main(
int argc,
char **argv) {
196 AvxVideoReader *reader = NULL;
197 AvxVideoWriter *writer = NULL;
198 const AvxInterface *decoder = NULL;
199 const AvxVideoInfo *info = NULL;
203 const char *tile_list_file = NULL;
206 if (argc != 5) die(
"Invalid number of arguments.");
208 reader = aom_video_reader_open(argv[1]);
209 if (!reader) die(
"Failed to open %s for reading.", argv[1]);
211 num_references = (int)strtol(argv[3], NULL, 0);
212 info = aom_video_reader_get_info(reader);
214 aom_video_reader_set_fourcc(reader, AV1_FOURCC);
218 writer = aom_video_writer_open(argv[2], kContainerIVF, info);
219 if (!writer) die(
"Failed to open %s for writing", argv[2]);
221 tile_list_file = argv[4];
223 decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
224 if (!decoder) die(
"Unknown input codec.");
228 die_codec(&codec,
"Failed to initialize decoder.");
233 printf(
"Reading %d reference images.\n", num_references);
234 for (i = 0; i < num_references; ++i) {
235 aom_video_reader_read_frame(reader);
237 size_t frame_size = 0;
238 const unsigned char *frame =
239 aom_video_reader_get_frame(reader, &frame_size);
243 if (!aom_video_writer_write_frame(writer, frame, frame_size, pts))
244 die_codec(&codec,
"Failed to copy compressed anchor frame.");
247 die_codec(&codec,
"Failed to decode frame.");
254 FILE *infile = aom_video_reader_get_file(reader);
256 const FileOffset camera_frame_pos = ftello(infile);
258 printf(
"Loading compressed frames into memory.\n");
262 while (aom_video_reader_read_frame(reader)) {
265 if (num_frames < 1) die(
"Input light field has no frames.");
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));
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;
282 printf(
"Read %d frames.\n", num_frames);
287 size_t frame_size = frame_sizes[0];
288 const unsigned char *frame = frames[0];
289 pts = num_references;
299 if (aom_status) die_codec(&codec,
"Failed to decode tile.");
303 size_t obu_size_offset =
307 uint32_t frame_header_size = (uint32_t)frame_header_info.
extra_size - 1;
308 size_t bytes_to_copy =
309 obu_size_offset + length_field_size + frame_header_size;
311 unsigned char *frame_hdr_buf = (
unsigned char *)malloc(bytes_to_copy);
312 if (frame_hdr_buf == NULL)
313 die_codec(&codec,
"Failed to allocate frame header buffer.");
315 memcpy(frame_hdr_buf, frame, bytes_to_copy);
318 size_t bytes_written = 0;
319 if (aom_uleb_encode_fixed_size(
320 frame_header_size, length_field_size, length_field_size,
321 frame_hdr_buf + obu_size_offset, &bytes_written))
322 die_codec(&codec,
"Failed to encode the tile list obu size.");
325 if (!aom_video_writer_write_frame(writer, frame_hdr_buf, bytes_to_copy,
327 die_codec(&codec,
"Failed to copy compressed camera frame header.");
334 die_codec(&codec,
"Failed to get the image format");
335 const int bps = get_image_bps(ref_fmt);
336 if (!bps) die_codec(&codec,
"Invalid image format.");
338 unsigned int tile_size = 0;
340 die_codec(&codec,
"Failed to get the tile size");
341 const unsigned int tile_width = tile_size >> 16;
342 const unsigned int tile_height = tile_size & 65535;
344 const size_t data_sz = MAX_TILES * ALIGN_POWER_OF_TWO(tile_width, 5) *
345 ALIGN_POWER_OF_TWO(tile_height, 5) * bps / 8;
347 unsigned char *tl_buf = (
unsigned char *)malloc(data_sz);
348 if (tl_buf == NULL) die_codec(&codec,
"Failed to allocate tile list buffer.");
351 const uint8_t output_frame_width_in_tiles_minus_1 =
352 output_frame_width / tile_width - 1;
353 const uint8_t output_frame_height_in_tiles_minus_1 =
354 output_frame_height / tile_height - 1;
356 printf(
"Reading tile list from file.\n");
358 FILE *tile_list_fptr = fopen(tile_list_file,
"r");
359 if (!tile_list_fptr) die_codec(&codec,
"Failed to open tile list file.");
361 TILE_LIST_INFO tiles[MAX_TILES];
362 while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
363 if (line[0] ==
'F' || num_tiles >= MAX_TILES) {
367 process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec,
368 tl_buf, writer, output_frame_width_in_tiles_minus_1,
369 output_frame_height_in_tiles_minus_1);
374 if (line[0] ==
'F') {
377 if (sscanf(line,
"%d %d %d %d", &tiles[num_tiles].image_idx,
378 &tiles[num_tiles].reference_idx, &tiles[num_tiles].tile_col,
379 &tiles[num_tiles].tile_row) == 4) {
380 if (tiles[num_tiles].image_idx >= num_frames) {
381 die(
"Tile list image_idx out of bounds: %d >= %d.",
382 tiles[num_tiles].image_idx, num_frames);
384 if (tiles[num_tiles].reference_idx >= num_references) {
385 die(
"Tile list reference_idx out of bounds: %d >= %d.",
386 tiles[num_tiles].reference_idx, num_references);
393 process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec,
394 tl_buf, writer, output_frame_width_in_tiles_minus_1,
395 output_frame_height_in_tiles_minus_1);
399 const int num_tile_lists = (int)(tl_pts - pts);
400 printf(
"Finished processing tile lists. Num tile lists: %d.\n",
403 for (
int f = 0; f < num_frames; ++f) {
409 aom_video_writer_close(writer);
410 aom_video_reader_close(reader);