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");
97 void process_tile_list(
const TILE_LIST_INFO *tiles,
int num_tiles,
100 unsigned char *tl_buf, AvxVideoWriter *writer,
101 uint8_t output_frame_width_in_tiles_minus_1,
102 uint8_t output_frame_height_in_tiles_minus_1) {
103 unsigned char *tl = tl_buf;
104 struct aom_write_bit_buffer wb = { tl, 0 };
105 unsigned char *saved_obu_size_loc = NULL;
106 uint32_t tile_list_obu_header_size = 0;
107 uint32_t tile_list_obu_size = 0;
108 int num_tiles_minus_1 = num_tiles - 1;
112 aom_wb_write_literal(&wb, 0, 1);
113 aom_wb_write_literal(&wb, 8, 4);
114 aom_wb_write_literal(&wb, 0, 1);
115 aom_wb_write_literal(&wb, 1, 1);
116 aom_wb_write_literal(&wb, 0, 1);
118 tile_list_obu_header_size++;
121 saved_obu_size_loc = tl;
123 aom_wb_write_unsigned_literal(&wb, 0, 32);
125 tile_list_obu_header_size += 4;
128 aom_wb_write_literal(&wb, output_frame_width_in_tiles_minus_1, 8);
129 aom_wb_write_literal(&wb, output_frame_height_in_tiles_minus_1, 8);
130 aom_wb_write_literal(&wb, num_tiles_minus_1, 16);
132 tile_list_obu_size += 4;
135 for (i = 0; i <= num_tiles_minus_1; i++) {
138 int image_idx = tiles[i].image_idx;
139 int ref_idx = tiles[i].reference_idx;
140 int tc = tiles[i].tile_col;
141 int tr = tiles[i].tile_row;
147 size_t frame_size = frame_sizes[image_idx];
148 const unsigned char *frame = frames[image_idx];
155 if (aom_status) die_codec(codec,
"Failed to decode tile.");
165 uint32_t tile_info_bytes = 5;
166 aom_wb_write_literal(&wb, ref_idx, 8);
167 aom_wb_write_literal(&wb, tr, 8);
168 aom_wb_write_literal(&wb, tc, 8);
170 tl += tile_info_bytes;
176 tile_list_obu_size +=
181 size_t bytes_written = 0;
182 if (aom_uleb_encode_fixed_size(tile_list_obu_size, 4, 4, saved_obu_size_loc,
184 die_codec(codec,
"Failed to encode the tile list obu size.");
187 if (!aom_video_writer_write_frame(
188 writer, tl_buf, tile_list_obu_header_size + tile_list_obu_size,
190 die_codec(codec,
"Failed to copy compressed tile list.");
193 int main(
int argc,
char **argv) {
195 AvxVideoReader *reader = NULL;
196 AvxVideoWriter *writer = NULL;
197 const AvxInterface *decoder = NULL;
198 const AvxVideoInfo *info = NULL;
202 const char *tile_list_file = NULL;
205 if (argc != 5) die(
"Invalid number of arguments.");
207 reader = aom_video_reader_open(argv[1]);
208 if (!reader) die(
"Failed to open %s for reading.", argv[1]);
210 num_references = (int)strtol(argv[3], NULL, 0);
211 info = aom_video_reader_get_info(reader);
215 writer = aom_video_writer_open(argv[2], kContainerIVF, info);
216 if (!writer) die(
"Failed to open %s for writing", argv[2]);
218 tile_list_file = argv[4];
220 decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
221 if (!decoder) die(
"Unknown input codec.");
225 die_codec(&codec,
"Failed to initialize decoder.");
230 printf(
"Reading %d reference images.\n", num_references);
231 for (i = 0; i < num_references; ++i) {
232 aom_video_reader_read_frame(reader);
234 size_t frame_size = 0;
235 const unsigned char *frame =
236 aom_video_reader_get_frame(reader, &frame_size);
240 if (!aom_video_writer_write_frame(writer, frame, frame_size, pts))
241 die_codec(&codec,
"Failed to copy compressed anchor frame.");
244 die_codec(&codec,
"Failed to decode frame.");
251 FILE *infile = aom_video_reader_get_file(reader);
253 const FileOffset camera_frame_pos = ftello(infile);
255 printf(
"Loading compressed frames into memory.\n");
259 while (aom_video_reader_read_frame(reader)) {
262 if (num_frames < 1) die(
"Input light field has no frames.");
265 unsigned char **frames =
266 (
unsigned char **)malloc(num_frames *
sizeof(
unsigned char *));
267 size_t *frame_sizes = (
size_t *)malloc(num_frames *
sizeof(
size_t));
269 fseeko(infile, camera_frame_pos, SEEK_SET);
270 for (
int f = 0; f < num_frames; ++f) {
271 aom_video_reader_read_frame(reader);
272 size_t frame_size = 0;
273 const unsigned char *frame =
274 aom_video_reader_get_frame(reader, &frame_size);
275 frames[f] = (
unsigned char *)malloc(frame_size *
sizeof(
unsigned char));
276 memcpy(frames[f], frame, frame_size);
277 frame_sizes[f] = frame_size;
279 printf(
"Read %d frames.\n", num_frames);
284 size_t frame_size = frame_sizes[0];
285 const unsigned char *frame = frames[0];
286 pts = num_references;
296 if (aom_status) die_codec(&codec,
"Failed to decode tile.");
300 size_t obu_size_offset =
304 uint32_t frame_header_size = (uint32_t)frame_header_info.
extra_size - 1;
305 size_t bytes_to_copy =
306 obu_size_offset + length_field_size + frame_header_size;
308 unsigned char *frame_hdr_buf = (
unsigned char *)malloc(bytes_to_copy);
309 if (frame_hdr_buf == NULL)
310 die_codec(&codec,
"Failed to allocate frame header buffer.");
312 memcpy(frame_hdr_buf, frame, bytes_to_copy);
315 size_t bytes_written = 0;
316 if (aom_uleb_encode_fixed_size(
317 frame_header_size, length_field_size, length_field_size,
318 frame_hdr_buf + obu_size_offset, &bytes_written))
319 die_codec(&codec,
"Failed to encode the tile list obu size.");
322 if (!aom_video_writer_write_frame(writer, frame_hdr_buf, bytes_to_copy,
324 die_codec(&codec,
"Failed to copy compressed camera frame header.");
330 die_codec(&codec,
"Failed to get the image format");
331 const int bps = get_image_bps(ref_fmt);
333 unsigned int tile_size = 0;
335 die_codec(&codec,
"Failed to get the tile size");
336 const unsigned int tile_width = tile_size >> 16;
337 const unsigned int tile_height = tile_size & 65535;
339 const size_t data_sz = MAX_TILES * ALIGN_POWER_OF_TWO(tile_width, 5) *
340 ALIGN_POWER_OF_TWO(tile_height, 5) * bps / 8;
342 unsigned char *tl_buf = (
unsigned char *)malloc(data_sz);
343 if (tl_buf == NULL) die_codec(&codec,
"Failed to allocate tile list buffer.");
346 const uint8_t output_frame_width_in_tiles_minus_1 =
347 output_frame_width / tile_width - 1;
348 const uint8_t output_frame_height_in_tiles_minus_1 =
349 output_frame_height / tile_height - 1;
351 printf(
"Reading tile list from file.\n");
353 FILE *tile_list_fptr = fopen(tile_list_file,
"r");
355 TILE_LIST_INFO tiles[MAX_TILES];
356 while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
357 if (line[0] ==
'F' || num_tiles >= MAX_TILES) {
361 process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec,
362 tl_buf, writer, output_frame_width_in_tiles_minus_1,
363 output_frame_height_in_tiles_minus_1);
368 if (line[0] ==
'F') {
371 if (sscanf(line,
"%d %d %d %d", &tiles[num_tiles].image_idx,
372 &tiles[num_tiles].reference_idx, &tiles[num_tiles].tile_col,
373 &tiles[num_tiles].tile_row) == 4) {
374 if (tiles[num_tiles].image_idx >= num_frames) {
375 die(
"Tile list image_idx out of bounds: %d >= %d.",
376 tiles[num_tiles].image_idx, num_frames);
378 if (tiles[num_tiles].reference_idx >= num_references) {
379 die(
"Tile list reference_idx out of bounds: %d >= %d.",
380 tiles[num_tiles].reference_idx, num_references);
387 process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec,
388 tl_buf, writer, output_frame_width_in_tiles_minus_1,
389 output_frame_height_in_tiles_minus_1);
393 int num_tile_lists = tl_pts - pts;
394 printf(
"Finished processing tile lists. Num tile lists: %d.\n",
397 for (
int f = 0; f < num_frames; ++f) {
403 aom_video_writer_close(writer);
404 aom_video_reader_close(reader);
Describes the encoder algorithm interface to applications.
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
int64_t aom_codec_pts_t
Time Stamp Type.
Definition: aom_encoder.h:94
Definition: aom_image.h:50
Codec context structure.
Definition: aom_codec.h:204
Describes the decoder algorithm interface to applications.
Definition: aom_image.h:49
Structure to hold a tile's start address and size in the bitstream.
Definition: aomdx.h:67
Definition: aom_image.h:54
aom_codec_err_t aom_codec_decode(aom_codec_ctx_t *ctx, const uint8_t *data, size_t data_sz, void *user_priv)
Decode data.
aom_codec_err_t aom_codec_control_(aom_codec_ctx_t *ctx, int ctrl_id,...)
Control algorithm.
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition: aom_decoder.h:142
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
Definition: aom_image.h:53
size_t extra_size
Definition: aomdx.h:73
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:423
size_t coded_tile_data_size
Definition: aomdx.h:69
const void * coded_tile_data
Definition: aomdx.h:71
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
Definition: aom_image.h:52
Provides definitions for using AOM or AV1 within the aom Decoder interface.
Definition: aom_image.h:45