AOMedia Codec SDK: lightfield_bitstream_parsing

AOMedia Codec SDK

lightfield_bitstream_parsing
1 /*
2 * Copyright (c) 2018, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
11
12 // Lightfield Bitstream Parsing
13 // ============================
14 //
15 // This is a lightfield bitstream parsing example. It takes an input file
16 // containing the whole compressed lightfield bitstream(ivf file) and a text
17 // file containing a stream of tiles to decode and then constructs and outputs
18 // a new bitstream that can be decoded by an AV1 decoder. The output bitstream
19 // contains reference frames(i.e. anchor frames), camera frame header, and
20 // tile list OBUs. num_references is the number of anchor frames coded at the
21 // beginning of the light field file. After running the lightfield encoder,
22 // run lightfield bitstream parsing:
23 // examples/lightfield_bitstream_parsing vase10x10.ivf vase_tile_list.ivf 4
24 // tile_list.txt
25 //
26 // The tile_list.txt is expected to be of the form:
27 // Frame <frame_index0>
28 // <image_index0> <anchor_index0> <tile_col0> <tile_row0>
29 // <image_index1> <anchor_index1> <tile_col1> <tile_row1>
30 // ...
31 // Frame <frame_index1)
32 // ...
33 //
34 // The "Frame" markers indicate a new render frame and thus a new tile list
35 // will be started and the old one flushed. The image_indexN, anchor_indexN,
36 // tile_colN, and tile_rowN identify an individual tile to be decoded and
37 // to use anchor_indexN anchor image for MCP.
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "aom/aom_decoder.h"
44 #include "aom/aom_encoder.h"
45 #include "aom/aom_integer.h"
46 #include "aom/aomdx.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
54 static const char *exec_name;
55
56 void 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
65 const int output_frame_width = 512;
66 const int output_frame_height = 512;
67
68 // Spec:
69 // typedef struct {
70 // uint8_t anchor_frame_idx;
71 // uint8_t tile_row;
72 // uint8_t tile_col;
73 // uint16_t coded_tile_data_size_minus_1;
74 // uint8_t *coded_tile_data;
75 // } TILE_LIST_ENTRY;
76
77 // Tile list entry provided by the application
78 typedef struct {
79 int image_idx;
80 int reference_idx;
81 int tile_col;
82 int tile_row;
83 } TILE_LIST_INFO;
84
85 static int get_image_bps(aom_img_fmt_t fmt) {
86 switch (fmt) {
87 case AOM_IMG_FMT_I420: return 12;
88 case AOM_IMG_FMT_I422: return 16;
89 case AOM_IMG_FMT_I444: return 24;
90 case AOM_IMG_FMT_I42016: return 24;
91 case AOM_IMG_FMT_I42216: return 32;
92 case AOM_IMG_FMT_I44416: return 48;
93 default: die("Invalid image format");
94 }
95 }
96
97 void process_tile_list(const TILE_LIST_INFO *tiles, int num_tiles,
98 aom_codec_pts_t tl_pts, unsigned char **frames,
99 const size_t *frame_sizes, aom_codec_ctx_t *codec,
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 // Write the tile list OBU header that is 1 byte long.
112 aom_wb_write_literal(&wb, 0, 1); // forbidden bit.
113 aom_wb_write_literal(&wb, 8, 4); // tile list OBU: "1000"
114 aom_wb_write_literal(&wb, 0, 1); // obu_extension = 0
115 aom_wb_write_literal(&wb, 1, 1); // obu_has_size_field
116 aom_wb_write_literal(&wb, 0, 1); // reserved
117 tl++;
118 tile_list_obu_header_size++;
119
120 // Write the OBU size using a fixed length_field_size of 4 bytes.
121 saved_obu_size_loc = tl;
122 // aom_wb_write_unsigned_literal(&wb, data, bits) requires that bits <= 32.
123 aom_wb_write_unsigned_literal(&wb, 0, 32);
124 tl += 4;
125 tile_list_obu_header_size += 4;
126
127 // write_tile_list_obu()
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 // Write each tile's data
135 for (i = 0; i <= num_tiles_minus_1; i++) {
136 aom_tile_data tile_data = { 0, NULL, 0 };
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 // Reset bit writer to the right location.
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
150 aom_codec_control_(codec, AV1_SET_DECODE_TILE_ROW, tr);
151 aom_codec_control_(codec, AV1_SET_DECODE_TILE_COL, tc);
152
153 aom_codec_err_t aom_status =
154 aom_codec_decode(codec, frame, frame_size, NULL);
155 if (aom_status) die_codec(codec, "Failed to decode tile.");
156
157 aom_codec_control_(codec, AV1D_GET_TILE_DATA, &tile_data);
158
159 // Copy over tile info.
160 // uint8_t anchor_frame_idx;
161 // uint8_t tile_row;
162 // uint8_t tile_col;
163 // uint16_t coded_tile_data_size_minus_1;
164 // uint8_t *coded_tile_data;
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);
169 aom_wb_write_literal(&wb, (int)tile_data.coded_tile_data_size - 1, 16);
170 tl += tile_info_bytes;
171
172 memcpy(tl, (uint8_t *)tile_data.coded_tile_data,
173 tile_data.coded_tile_data_size);
174 tl += tile_data.coded_tile_data_size;
175
176 tile_list_obu_size +=
177 tile_info_bytes + (uint32_t)tile_data.coded_tile_data_size;
178 }
179
180 // Write 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,
183 &bytes_written))
184 die_codec(codec, "Failed to encode the tile list obu size.");
185
186 // Copy the tile list.
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
193 int main(int argc, char **argv) {
194 aom_codec_ctx_t codec;
195 AvxVideoReader *reader = NULL;
196 AvxVideoWriter *writer = NULL;
197 const AvxInterface *decoder = NULL;
198 const AvxVideoInfo *info = NULL;
199 int num_references;
200 int i;
201 aom_codec_pts_t pts;
202 const char *tile_list_file = NULL;
203
204 exec_name = argv[0];
205 if (argc != 5) die("Invalid number of arguments.");
206
207 reader = aom_video_reader_open(argv[1]);
208 if (!reader) die("Failed to open %s for reading.", argv[1]);
209
210 num_references = (int)strtol(argv[3], NULL, 0);
211 info = aom_video_reader_get_info(reader);
212
213 // The writer to write out ivf file in tile list OBU, which can be decoded by
214 // AV1 decoder.
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
220 decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
221 if (!decoder) die("Unknown input codec.");
222 printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
223
224 if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
225 die_codec(&codec, "Failed to initialize decoder.");
226
227 // Decode anchor frames.
228 aom_codec_control_(&codec, AV1_SET_TILE_MODE, 0);
229
230 printf("Reading %d reference images.\n", num_references);
231 for (i = 0; i < num_references; ++i) {
232 aom_video_reader_read_frame(reader);
233
234 size_t frame_size = 0;
235 const unsigned char *frame =
236 aom_video_reader_get_frame(reader, &frame_size);
237 pts = (aom_codec_pts_t)aom_video_reader_get_frame_pts(reader);
238
239 // Copy references bitstream directly.
240 if (!aom_video_writer_write_frame(writer, frame, frame_size, pts))
241 die_codec(&codec, "Failed to copy compressed anchor frame.");
242
243 if (aom_codec_decode(&codec, frame, frame_size, NULL))
244 die_codec(&codec, "Failed to decode frame.");
245 }
246
247 // Decode camera frames.
248 aom_codec_control_(&codec, AV1_SET_TILE_MODE, 1);
249 aom_codec_control_(&codec, AV1D_EXT_TILE_DEBUG, 1);
250
251 FILE *infile = aom_video_reader_get_file(reader);
252 // Record the offset of the first camera image.
253 const FileOffset camera_frame_pos = ftello(infile);
254
255 printf("Loading compressed frames into memory.\n");
256
257 // Count the frames in the lightfield.
258 int num_frames = 0;
259 while (aom_video_reader_read_frame(reader)) {
260 ++num_frames;
261 }
262 if (num_frames < 1) die("Input light field has no frames.");
263
264 // Read all of the lightfield frames into memory.
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));
268 // Seek to the first camera image.
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;
278 }
279 printf("Read %d frames.\n", num_frames);
280
281 // Copy first camera frame for getting camera frame header. This is done
282 // only once.
283 {
284 size_t frame_size = frame_sizes[0];
285 const unsigned char *frame = frames[0];
286 pts = num_references;
287 aom_tile_data frame_header_info = { 0, NULL, 0 };
288
289 // Need to decode frame header to get camera frame header info. So, here
290 // decoding 1 tile is enough.
291 aom_codec_control_(&codec, AV1_SET_DECODE_TILE_ROW, 0);
292 aom_codec_control_(&codec, AV1_SET_DECODE_TILE_COL, 0);
293
294 aom_codec_err_t aom_status =
295 aom_codec_decode(&codec, frame, frame_size, NULL);
296 if (aom_status) die_codec(&codec, "Failed to decode tile.");
297
298 aom_codec_control_(&codec, AV1D_GET_FRAME_HEADER_INFO, &frame_header_info);
299
300 size_t obu_size_offset =
301 (uint8_t *)frame_header_info.coded_tile_data - frame;
302 size_t length_field_size = frame_header_info.coded_tile_data_size;
303 // Remove ext-tile tile info.
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;
307
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.");
311
312 memcpy(frame_hdr_buf, frame, bytes_to_copy);
313
314 // Update frame header OBU size.
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.");
320
321 // Copy camera frame header bitstream.
322 if (!aom_video_writer_write_frame(writer, frame_hdr_buf, bytes_to_copy,
323 pts))
324 die_codec(&codec, "Failed to copy compressed camera frame header.");
325 }
326
327 // Read out the image format.
328 aom_img_fmt_t ref_fmt = 0;
329 if (aom_codec_control(&codec, AV1D_GET_IMG_FORMAT, &ref_fmt))
330 die_codec(&codec, "Failed to get the image format");
331 const int bps = get_image_bps(ref_fmt);
332 // read out the tile size.
333 unsigned int tile_size = 0;
334 if (aom_codec_control(&codec, AV1D_GET_TILE_SIZE, &tile_size))
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;
338 // Allocate a buffer to store tile list bitstream.
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;
341
342 unsigned char *tl_buf = (unsigned char *)malloc(data_sz);
343 if (tl_buf == NULL) die_codec(&codec, "Failed to allocate tile list buffer.");
344
345 aom_codec_pts_t tl_pts = num_references;
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;
350
351 printf("Reading tile list from file.\n");
352 char line[1024];
353 FILE *tile_list_fptr = fopen(tile_list_file, "r");
354 int num_tiles = 0;
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) {
358 // Flush existing tile list and start another, either because we hit a
359 // new render frame or because we've hit our max number of tiles per list.
360 if (num_tiles > 0) {
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);
364 ++tl_pts;
365 }
366 num_tiles = 0;
367 }
368 if (line[0] == 'F') {
369 continue;
370 }
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);
377 }
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);
381 }
382 ++num_tiles;
383 }
384 }
385 if (num_tiles > 0) {
386 // Flush out the last tile list.
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);
390 ++tl_pts;
391 }
392
393 int num_tile_lists = tl_pts - pts;
394 printf("Finished processing tile lists. Num tile lists: %d.\n",
395 num_tile_lists);
396 free(tl_buf);
397 for (int f = 0; f < num_frames; ++f) {
398 free(frames[f]);
399 }
400 free(frame_sizes);
401 free(frames);
402 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
403 aom_video_writer_close(writer);
404 aom_video_reader_close(reader);
405
406 return EXIT_SUCCESS;
407 }
AV1D_GET_FRAME_HEADER_INFO
Definition: aomdx.h:184
aom_encoder.h
Describes the encoder algorithm interface to applications.
aom_img_fmt_t
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
aom_codec_pts_t
int64_t aom_codec_pts_t
Time Stamp Type.
Definition: aom_encoder.h:94
AV1D_GET_TILE_DATA
Definition: aomdx.h:188
AOM_IMG_FMT_I444
Definition: aom_image.h:50
aom_codec_ctx
Codec context structure.
Definition: aom_codec.h:204
AV1D_GET_TILE_SIZE
Definition: aomdx.h:126
aom_decoder.h
Describes the decoder algorithm interface to applications.
AOM_IMG_FMT_I422
Definition: aom_image.h:49
aom_tile_data
Structure to hold a tile's start address and size in the bitstream.
Definition: aomdx.h:67
AOM_IMG_FMT_I44416
Definition: aom_image.h:54
aom_codec_decode
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_control_
aom_codec_err_t aom_codec_control_(aom_codec_ctx_t *ctx, int ctrl_id,...)
Control algorithm.
aom_codec_dec_init
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition: aom_decoder.h:142
aom_codec_iface_name
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
AOM_IMG_FMT_I42216
Definition: aom_image.h:53
AV1D_GET_IMG_FORMAT
Definition: aomdx.h:123
AV1D_EXT_TILE_DEBUG
Definition: aomdx.h:197
aom_tile_data::extra_size
size_t extra_size
Definition: aomdx.h:73
aom_codec_control
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:423
AV1_SET_DECODE_TILE_ROW
Definition: aomdx.h:174
aom_tile_data::coded_tile_data_size
size_t coded_tile_data_size
Definition: aomdx.h:69
AV1_SET_TILE_MODE
Definition: aomdx.h:180
aom_tile_data::coded_tile_data
const void * coded_tile_data
Definition: aomdx.h:71
aom_codec_err_t
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
AOM_IMG_FMT_I42016
Definition: aom_image.h:52
aomdx.h
Provides definitions for using AOM or AV1 within the aom Decoder interface.
AOM_IMG_FMT_I420
Definition: aom_image.h:45