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