1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42
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"
51
52#define MAX_TILES 512
53
54static const char *exec_name;
55
56void usage_exit(void) {
57 fprintf(stderr, "Usage: %s <infile> <outfile> <num_references> <tile_list>\n",
58 exec_name);
59 exit(EXIT_FAILURE);
60}
61
62#define ALIGN_POWER_OF_TWO(value, n) \
63 (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
64
65const int output_frame_width = 512;
66const int output_frame_height = 512;
67
68
69
70
71
72
73
74
75
76
77
78typedef struct {
79 int image_idx;
80 int reference_idx;
81 int tile_col;
82 int tile_row;
83} TILE_LIST_INFO;
84
86 switch (fmt) {
93 default: die("Invalid image format");
94 }
95}
96
97static 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;
109 int i;
110
111
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);
117 tl++;
118 tile_list_obu_header_size++;
119
120
121 saved_obu_size_loc = tl;
122
123 aom_wb_write_unsigned_literal(&wb, 0, 32);
124 tl += 4;
125 tile_list_obu_header_size += 4;
126
127
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);
131 tl += 4;
132 tile_list_obu_size += 4;
133
134
135 for (i = 0; i <= num_tiles_minus_1; i++) {
137
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;
142
143
144 wb.bit_buffer = tl;
145 wb.bit_offset = 0;
146
147 size_t frame_size = frame_sizes[image_idx];
148 const unsigned char *frame = frames[image_idx];
149
152
155 if (aom_status) die_codec(codec, "Failed to decode tile.");
156
158
159
160
161
162
163
164
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;
171
175
176 tile_list_obu_size +=
178 }
179
180
181 size_t bytes_written = 0;
182 if (aom_uleb_encode_fixed_size(tile_list_obu_size, 4, 4, saved_obu_size_loc,
183 &bytes_written))
184 die_codec(codec, "Failed to encode the tile list obu size.");
185
186
187 if (!aom_video_writer_write_frame(
188 writer, tl_buf, tile_list_obu_header_size + tile_list_obu_size,
189 tl_pts))
190 die_codec(codec, "Failed to copy compressed tile list.");
191}
192
193int main(int argc, char **argv) {
194 AvxVideoReader *reader = NULL;
195 AvxVideoWriter *writer = NULL;
196 const AvxVideoInfo *info = NULL;
197 int num_references;
198 int i;
200 const char *tile_list_file = NULL;
201
202 exec_name = argv[0];
203 if (argc != 5) die("Invalid number of arguments.");
204
205 reader = aom_video_reader_open(argv[1]);
206 if (!reader) die("Failed to open %s for reading.", argv[1]);
207
208 num_references = (int)strtol(argv[3], NULL, 0);
209 info = aom_video_reader_get_info(reader);
210
211 aom_video_reader_set_fourcc(reader, AV1_FOURCC);
212
213
214
215 writer = aom_video_writer_open(argv[2], kContainerIVF, info);
216 if (!writer) die("Failed to open %s for writing", argv[2]);
217
218 tile_list_file = argv[4];
219
221 if (!decoder) die("Unknown input codec.");
223
226 die("Failed to initialize decoder.");
227
228
230
231 printf("Reading %d reference images.\n", num_references);
232 for (i = 0; i < num_references; ++i) {
233 aom_video_reader_read_frame(reader);
234
235 size_t frame_size = 0;
236 const unsigned char *frame =
237 aom_video_reader_get_frame(reader, &frame_size);
239
240
241 if (!aom_video_writer_write_frame(writer, frame, frame_size, pts))
242 die_codec(&codec, "Failed to copy compressed anchor frame.");
243
245 die_codec(&codec, "Failed to decode frame.");
246 }
247
248
251
252 FILE *infile = aom_video_reader_get_file(reader);
253
254 const FileOffset camera_frame_pos = ftello(infile);
255
256 printf("Loading compressed frames into memory.\n");
257
258
259 int num_frames = 0;
260 while (aom_video_reader_read_frame(reader)) {
261 ++num_frames;
262 }
263 if (num_frames < 1) die("Input light field has no frames.");
264
265
266 unsigned char **frames =
267 (unsigned char **)malloc(num_frames * sizeof(unsigned char *));
268 size_t *frame_sizes = (size_t *)malloc(num_frames * sizeof(size_t));
269 if (!(frames && frame_sizes)) die("Failed to allocate frame data.");
270
271
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 if (!frames[f]) die("Failed to allocate frame data.");
280 memcpy(frames[f], frame, frame_size);
281 frame_sizes[f] = frame_size;
282 }
283 printf("Read %d frames.\n", num_frames);
284
285
286
287 {
288 size_t frame_size = frame_sizes[0];
289 const unsigned char *frame = frames[0];
290 pts = num_references;
292
293
294
297
300 if (aom_status) die_codec(&codec, "Failed to decode tile.");
301
303 &frame_header_info);
304
305 size_t obu_size_offset =
308
309 uint32_t frame_header_size = (uint32_t)frame_header_info.
extra_size - 1;
310 size_t bytes_to_copy =
311 obu_size_offset + length_field_size + frame_header_size;
312
313 unsigned char *frame_hdr_buf = (unsigned char *)malloc(bytes_to_copy);
314 if (frame_hdr_buf == NULL)
315 die_codec(&codec, "Failed to allocate frame header buffer.");
316
317 memcpy(frame_hdr_buf, frame, bytes_to_copy);
318
319
320 size_t bytes_written = 0;
321 if (aom_uleb_encode_fixed_size(
322 frame_header_size, length_field_size, length_field_size,
323 frame_hdr_buf + obu_size_offset, &bytes_written))
324 die_codec(&codec, "Failed to encode the tile list obu size.");
325
326
327 if (!aom_video_writer_write_frame(writer, frame_hdr_buf, bytes_to_copy,
328 pts))
329 die_codec(&codec, "Failed to copy compressed camera frame header.");
330 free(frame_hdr_buf);
331 }
332
333
336 die_codec(&codec, "Failed to get the image format");
337 const int bps = get_image_bps(ref_fmt);
338 if (!bps) die_codec(&codec, "Invalid image format.");
339
340 unsigned int tile_size = 0;
342 die_codec(&codec, "Failed to get the tile size");
343 const unsigned int tile_width = tile_size >> 16;
344 const unsigned int tile_height = tile_size & 65535;
345
346 const size_t data_sz = MAX_TILES * ALIGN_POWER_OF_TWO(tile_width, 5) *
347 ALIGN_POWER_OF_TWO(tile_height, 5) * bps / 8;
348
349 unsigned char *tl_buf = (unsigned char *)malloc(data_sz);
350 if (tl_buf == NULL) die_codec(&codec, "Failed to allocate tile list buffer.");
351
353 const uint8_t output_frame_width_in_tiles_minus_1 =
354 output_frame_width / tile_width - 1;
355 const uint8_t output_frame_height_in_tiles_minus_1 =
356 output_frame_height / tile_height - 1;
357
358 printf("Reading tile list from file.\n");
359 char line[1024];
360 FILE *tile_list_fptr = fopen(tile_list_file, "r");
361 if (!tile_list_fptr) die_codec(&codec, "Failed to open tile list file.");
362 int num_tiles = 0;
363 TILE_LIST_INFO tiles[MAX_TILES];
364 while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
365 if (line[0] == 'F' || num_tiles >= MAX_TILES) {
366
367
368 if (num_tiles > 0) {
369 process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec,
370 tl_buf, writer, output_frame_width_in_tiles_minus_1,
371 output_frame_height_in_tiles_minus_1);
372 ++tl_pts;
373 }
374 num_tiles = 0;
375 }
376 if (line[0] == 'F') {
377 continue;
378 }
379 if (sscanf(line, "%d %d %d %d", &tiles[num_tiles].image_idx,
380 &tiles[num_tiles].reference_idx, &tiles[num_tiles].tile_col,
381 &tiles[num_tiles].tile_row) == 4) {
382 if (tiles[num_tiles].image_idx >= num_frames) {
383 die("Tile list image_idx out of bounds: %d >= %d.",
384 tiles[num_tiles].image_idx, num_frames);
385 }
386 if (tiles[num_tiles].reference_idx >= num_references) {
387 die("Tile list reference_idx out of bounds: %d >= %d.",
388 tiles[num_tiles].reference_idx, num_references);
389 }
390 ++num_tiles;
391 }
392 }
393 if (num_tiles > 0) {
394
395 process_tile_list(tiles, num_tiles, tl_pts, frames, frame_sizes, &codec,
396 tl_buf, writer, output_frame_width_in_tiles_minus_1,
397 output_frame_height_in_tiles_minus_1);
398 ++tl_pts;
399 }
400
401 const int num_tile_lists = (int)(tl_pts - pts);
402 printf("Finished processing tile lists. Num tile lists: %d.\n",
403 num_tile_lists);
404 free(tl_buf);
405 for (int f = 0; f < num_frames; ++f) {
406 free(frames[f]);
407 }
408 free(frame_sizes);
409 free(frames);
411 aom_video_writer_close(writer);
412 aom_video_reader_close(reader);
413
414 return EXIT_SUCCESS;
415}
Describes the decoder algorithm interface to applications.
Describes the encoder algorithm interface to applications.
@ AOM_IMG_FMT_I42216
Definition aom_image.h:58
@ AOM_IMG_FMT_I42016
Definition aom_image.h:56
@ AOM_IMG_FMT_I444
Definition aom_image.h:50
@ AOM_IMG_FMT_I422
Definition aom_image.h:49
@ AOM_IMG_FMT_I44416
Definition aom_image.h:59
@ AOM_IMG_FMT_I420
Definition aom_image.h:45
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
Provides definitions for using AOM or AV1 within the aom Decoder interface.
@ AV1_SET_TILE_MODE
Codec control function to set the tile coding mode, unsigned int parameter.
Definition aomdx.h:313
@ AV1D_GET_TILE_SIZE
Codec control function to get the size of the tile, unsigned int* parameter.
Definition aomdx.h:240
@ AV1_SET_DECODE_TILE_ROW
Codec control function to set the range of tile decoding, int parameter.
Definition aomdx.h:304
@ AV1D_GET_IMG_FORMAT
Codec control function to get the image format of the stream, aom_img_fmt_t* parameter.
Definition aomdx.h:235
@ AV1D_GET_TILE_DATA
Codec control function to get the start address and size of a tile in the coded bitstream,...
Definition aomdx.h:323
@ AV1D_GET_FRAME_HEADER_INFO
Codec control function to get the frame header information of an encoded frame, aom_tile_data* parame...
Definition aomdx.h:318
@ AV1D_EXT_TILE_DEBUG
Codec control function to enable the ext-tile software debug and testing code in the decoder,...
Definition aomdx.h:336
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
struct aom_codec_ctx aom_codec_ctx_t
Codec context structure.
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition aom_codec.h:254
int64_t aom_codec_pts_t
Time Stamp Type.
Definition aom_codec.h:235
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_codec_err_t
Algorithm return codes.
Definition aom_codec.h:155
#define AOM_CODEC_CONTROL_TYPECHECKED(ctx, id, data)
aom_codec_control wrapper macro (adds type-checking, less flexible)
Definition aom_codec.h:525
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.
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition aom_decoder.h:129
Structure to hold a tile's start address and size in the bitstream.
Definition aomdx.h:91
const void * coded_tile_data
Definition aomdx.h:95
size_t coded_tile_data_size
Definition aomdx.h:93
size_t extra_size
Definition aomdx.h:97