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 parses it
17 // and constructs and outputs a new bitstream that can be decoded by an AV1
18 // decoder. The output bitstream contains reference frames(i.e. anchor frames),
19 // camera frame header, and tile list OBUs. num_references is the number of
20 // anchor frames coded at the beginning of the light field file.
21 // After running the lightfield encoder, run lightfield bitstream parsing:
22 // examples/lightfield_bitstream_parsing vase10x10.ivf vase_tile_list.ivf 4
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "aom/aom_decoder.h"
29 #include "aom/aom_encoder.h"
30 #include "aom/aom_integer.h"
31 #include "aom/aomdx.h"
32 #include "aom_dsp/bitwriter_buffer.h"
33 #include "common/tools_common.h"
34 #include "common/video_reader.h"
35 #include "common/video_writer.h"
36 
37 #define MAX_TILES 512
38 
39 static const char *exec_name;
40 
41 void usage_exit(void) {
42  fprintf(stderr, "Usage: %s <infile> <outfile> <num_references> \n",
43  exec_name);
44  exit(EXIT_FAILURE);
45 }
46 
47 #define ALIGN_POWER_OF_TWO(value, n) \
48  (((value) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
49 
50 // SB size: 64x64
51 const uint8_t output_frame_width_in_tiles_minus_1 = 512 / 64 - 1;
52 const uint8_t output_frame_height_in_tiles_minus_1 = 512 / 64 - 1;
53 
54 // Spec:
55 // typedef struct {
56 // uint8_t anchor_frame_idx;
57 // uint8_t tile_row;
58 // uint8_t tile_col;
59 // uint16_t coded_tile_data_size_minus_1;
60 // uint8_t *coded_tile_data;
61 // } TILE_LIST_ENTRY;
62 
63 // Tile list entry provided by the application
64 typedef struct {
65  int image_idx;
66  int reference_idx;
67  int tile_col;
68  int tile_row;
69 } TILE_LIST_INFO;
70 
71 // M references: 0 - M-1; N images(including references): 0 - N-1;
72 // Note: order the image index incrementally, so that we only go through the
73 // bitstream once to construct the tile list.
74 const int num_tile_lists = 2;
75 const uint16_t tile_count_minus_1 = 9 - 1;
76 const TILE_LIST_INFO tile_list[2][9] = {
77  { { 16, 0, 4, 5 },
78  { 83, 3, 13, 2 },
79  { 57, 2, 2, 6 },
80  { 31, 1, 11, 5 },
81  { 2, 0, 7, 4 },
82  { 77, 3, 9, 9 },
83  { 49, 1, 0, 1 },
84  { 6, 0, 3, 10 },
85  { 63, 2, 5, 8 } },
86  { { 65, 2, 11, 1 },
87  { 42, 1, 3, 7 },
88  { 88, 3, 8, 4 },
89  { 76, 3, 1, 15 },
90  { 1, 0, 2, 2 },
91  { 19, 0, 5, 6 },
92  { 60, 2, 4, 0 },
93  { 25, 1, 11, 15 },
94  { 50, 2, 5, 4 } },
95 };
96 
97 static int get_image_bps(aom_img_fmt_t fmt) {
98  switch (fmt) {
99  case AOM_IMG_FMT_I420: return 12;
100  case AOM_IMG_FMT_I422: return 16;
101  case AOM_IMG_FMT_I444: return 24;
102  case AOM_IMG_FMT_I42016: return 24;
103  case AOM_IMG_FMT_I42216: return 32;
104  case AOM_IMG_FMT_I44416: return 48;
105  default: die("Invalid image format");
106  }
107  return 0;
108 }
109 
110 int main(int argc, char **argv) {
111  aom_codec_ctx_t codec;
112  AvxVideoReader *reader = NULL;
113  AvxVideoWriter *writer = NULL;
114  const AvxInterface *decoder = NULL;
115  const AvxVideoInfo *info = NULL;
116  int num_references;
117  int n, i;
118  aom_codec_pts_t pts;
119 
120  exec_name = argv[0];
121  if (argc != 4) die("Invalid number of arguments.");
122 
123  reader = aom_video_reader_open(argv[1]);
124  if (!reader) die("Failed to open %s for reading.", argv[1]);
125 
126  num_references = (int)strtol(argv[3], NULL, 0);
127  info = aom_video_reader_get_info(reader);
128 
129  // The writer to write out ivf file in tile list OBU, which can be decoded by
130  // AV1 decoder.
131  writer = aom_video_writer_open(argv[2], kContainerIVF, info);
132  if (!writer) die("Failed to open %s for writing", argv[2]);
133 
134  decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
135  if (!decoder) die("Unknown input codec.");
136  printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
137 
138  if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
139  die_codec(&codec, "Failed to initialize decoder.");
140 
141  // Decode anchor frames.
143 
144  for (i = 0; i < num_references; ++i) {
145  aom_video_reader_read_frame(reader);
146 
147  size_t frame_size = 0;
148  const unsigned char *frame =
149  aom_video_reader_get_frame(reader, &frame_size);
150  pts = (aom_codec_pts_t)aom_video_reader_get_frame_pts(reader);
151 
152  // Copy references bitstream directly.
153  if (!aom_video_writer_write_frame(writer, frame, frame_size, pts))
154  die_codec(&codec, "Failed to copy compressed anchor frame.");
155 
156  if (aom_codec_decode(&codec, frame, frame_size, NULL))
157  die_codec(&codec, "Failed to decode frame.");
158  }
159 
160  // Decode camera frames.
163 
164  FILE *infile = aom_video_reader_get_file(reader);
165  // Record the offset of the first camera image.
166  const FileOffset camera_frame_pos = ftello(infile);
167 
168  // Read out the first camera frame.
169  aom_video_reader_read_frame(reader);
170 
171  // Copy first camera frame for getting camera frame header. This is done
172  // only once.
173  {
174  size_t frame_size = 0;
175  const unsigned char *frame =
176  aom_video_reader_get_frame(reader, &frame_size);
177  pts = (aom_codec_pts_t)aom_video_reader_get_frame_pts(reader);
178  aom_tile_data frame_header_info = { 0, NULL, 0 };
179 
180  // Need to decode frame header to get camera frame header info. So, here
181  // decoding 1 tile is enough.
183  aom_codec_control_(&codec, AV1_SET_DECODE_TILE_COL, 0);
184 
185  aom_codec_err_t aom_status =
186  aom_codec_decode(&codec, frame, frame_size, NULL);
187  if (aom_status) die_codec(&codec, "Failed to decode tile.");
188 
189  aom_codec_control_(&codec, AV1D_GET_FRAME_HEADER_INFO, &frame_header_info);
190 
191  size_t obu_size_offset =
192  (uint8_t *)frame_header_info.coded_tile_data - frame;
193  size_t length_field_size = frame_header_info.coded_tile_data_size;
194  // Remove ext-tile tile info.
195  uint32_t frame_header_size = (uint32_t)frame_header_info.extra_size - 1;
196  size_t bytes_to_copy =
197  obu_size_offset + length_field_size + frame_header_size;
198 
199  unsigned char *frame_hdr_buf = (unsigned char *)malloc(bytes_to_copy);
200  if (frame_hdr_buf == NULL)
201  die_codec(&codec, "Failed to allocate frame header buffer.");
202 
203  memcpy(frame_hdr_buf, frame, bytes_to_copy);
204 
205  // Update frame header OBU size.
206  size_t bytes_written = 0;
207  if (aom_uleb_encode_fixed_size(
208  frame_header_size, length_field_size, length_field_size,
209  frame_hdr_buf + obu_size_offset, &bytes_written))
210  die_codec(&codec, "Failed to encode the tile list obu size.");
211 
212  // Copy camera frame header bitstream.
213  if (!aom_video_writer_write_frame(writer, frame_hdr_buf, bytes_to_copy,
214  pts))
215  die_codec(&codec, "Failed to copy compressed camera frame header.");
216  free(frame_hdr_buf);
217  }
218 
219  // Read out the image format.
220  aom_img_fmt_t ref_fmt = 0;
221  if (aom_codec_control(&codec, AV1D_GET_IMG_FORMAT, &ref_fmt))
222  die_codec(&codec, "Failed to get the image format");
223  const int bps = get_image_bps(ref_fmt);
224  if (!bps) die_codec(&codec, "Invalid image format.");
225  // read out the tile size.
226  unsigned int tile_size = 0;
227  if (aom_codec_control(&codec, AV1D_GET_TILE_SIZE, &tile_size))
228  die_codec(&codec, "Failed to get the tile size");
229  const unsigned int tile_width = tile_size >> 16;
230  const unsigned int tile_height = tile_size & 65535;
231  // Allocate a buffer to store tile list bitstream.
232  const size_t data_sz = MAX_TILES * ALIGN_POWER_OF_TWO(tile_width, 5) *
233  ALIGN_POWER_OF_TWO(tile_height, 5) * bps / 8;
234  unsigned char *tl_buf = (unsigned char *)malloc(data_sz);
235  if (tl_buf == NULL) die_codec(&codec, "Failed to allocate tile list buffer.");
236 
237  aom_codec_pts_t tl_pts = pts;
238 
239  // Process 1 tile list.
240  for (n = 0; n < num_tile_lists; n++) {
241  unsigned char *tl = tl_buf;
242  struct aom_write_bit_buffer wb = { tl, 0 };
243  unsigned char *saved_obu_size_loc = NULL;
244  uint32_t tile_list_obu_header_size = 0;
245  uint32_t tile_list_obu_size = 0;
246 
247  // Write the tile list OBU header that is 1 byte long.
248  aom_wb_write_literal(&wb, 0, 1); // forbidden bit.
249  aom_wb_write_literal(&wb, 8, 4); // tile list OBU: "1000"
250  aom_wb_write_literal(&wb, 0, 1); // obu_extension = 0
251  aom_wb_write_literal(&wb, 1, 1); // obu_has_size_field
252  aom_wb_write_literal(&wb, 0, 1); // reserved
253  tl++;
254  tile_list_obu_header_size++;
255 
256  // Write the OBU size using a fixed length_field_size of 4 bytes.
257  saved_obu_size_loc = tl;
258  // aom_wb_write_unsigned_literal(&wb, data, bits) requires that bits <= 32.
259  aom_wb_write_unsigned_literal(&wb, 0, 32);
260  tl += 4;
261  tile_list_obu_header_size += 4;
262 
263  // write_tile_list_obu()
264  aom_wb_write_literal(&wb, output_frame_width_in_tiles_minus_1, 8);
265  aom_wb_write_literal(&wb, output_frame_height_in_tiles_minus_1, 8);
266  aom_wb_write_literal(&wb, tile_count_minus_1, 16);
267  tl += 4;
268  tile_list_obu_size += 4;
269 
270  // Write each tile's data
271  for (i = 0; i <= tile_count_minus_1; i++) {
272  aom_tile_data tile_data = { 0, NULL, 0 };
273 
274  int image_idx = tile_list[n][i].image_idx;
275  int ref_idx = tile_list[n][i].reference_idx;
276  int tc = tile_list[n][i].tile_col;
277  int tr = tile_list[n][i].tile_row;
278  int frame_cnt = -1;
279 
280  // Reset bit writer to the right location.
281  wb.bit_buffer = tl;
282  wb.bit_offset = 0;
283 
284  // Seek to the first camera image.
285  fseeko(infile, camera_frame_pos, SEEK_SET);
286 
287  // Read out the camera image
288  while (frame_cnt != image_idx) {
289  aom_video_reader_read_frame(reader);
290  frame_cnt++;
291  }
292 
293  size_t frame_size = 0;
294  const unsigned char *frame =
295  aom_video_reader_get_frame(reader, &frame_size);
296 
298  aom_codec_control_(&codec, AV1_SET_DECODE_TILE_COL, tc);
299 
300  aom_codec_err_t aom_status =
301  aom_codec_decode(&codec, frame, frame_size, NULL);
302  if (aom_status) die_codec(&codec, "Failed to decode tile.");
303 
304  aom_codec_control_(&codec, AV1D_GET_TILE_DATA, &tile_data);
305 
306  // Copy over tile info.
307  // uint8_t anchor_frame_idx;
308  // uint8_t tile_row;
309  // uint8_t tile_col;
310  // uint16_t coded_tile_data_size_minus_1;
311  // uint8_t *coded_tile_data;
312  uint32_t tile_info_bytes = 5;
313  aom_wb_write_literal(&wb, ref_idx, 8);
314  aom_wb_write_literal(&wb, tr, 8);
315  aom_wb_write_literal(&wb, tc, 8);
316  aom_wb_write_literal(&wb, (int)tile_data.coded_tile_data_size - 1, 16);
317  tl += tile_info_bytes;
318 
319  memcpy(tl, (uint8_t *)tile_data.coded_tile_data,
320  tile_data.coded_tile_data_size);
321  tl += tile_data.coded_tile_data_size;
322 
323  tile_list_obu_size +=
324  tile_info_bytes + (uint32_t)tile_data.coded_tile_data_size;
325  }
326 
327  // Write tile list OBU size.
328  size_t bytes_written = 0;
329  if (aom_uleb_encode_fixed_size(tile_list_obu_size, 4, 4, saved_obu_size_loc,
330  &bytes_written))
331  die_codec(&codec, "Failed to encode the tile list obu size.");
332 
333  // Copy the tile list.
334  if (!aom_video_writer_write_frame(
335  writer, tl_buf, tile_list_obu_header_size + tile_list_obu_size,
336  tl_pts))
337  die_codec(&codec, "Failed to copy compressed tile list.");
338 
339  tl_pts++;
340  }
341 
342  free(tl_buf);
343  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
344  aom_video_writer_close(writer);
345  aom_video_reader_close(reader);
346 
347  return EXIT_SUCCESS;
348 }
Definition: aomdx.h:181
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:86
Definition: aomdx.h:185
Definition: aom_image.h:50
Codec context structure.
Definition: aom_codec.h:204
Definition: aomdx.h:126
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
Definition: aomdx.h:123
Definition: aomdx.h:194
size_t extra_size
Definition: aomdx.h:73
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:414
Definition: aomdx.h:171
size_t coded_tile_data_size
Definition: aomdx.h:69
Definition: aomdx.h:177
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