AOMedia Codec SDK
lightfield_tile_list_decoder
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 Tile List Decoder
13 // ============================
14 //
15 // This is a lightfield tile list decoder example. It takes an input file that
16 // contains the anchor frames that are references of the coded tiles, the camera
17 // frame header, and tile list OBUs that include the tile information and the
18 // compressed tile data. This input file is reconstructed from the encoded
19 // lightfield ivf file, and is decodable by AV1 decoder. num_references is
20 // the number of anchor frames coded at the beginning of the light field file.
21 // num_tile_lists is the number of tile lists need to be decoded. There is an
22 // optional parameter allowing to choose the output format, and the supported
23 // formats are YUV1D(default), YUV, and NV12.
24 // Run lightfield tile list decoder to decode an AV1 tile list file:
25 // examples/lightfield_tile_list_decoder vase_tile_list.ivf vase_tile_list.yuv
26 // 4 2 0(optional)
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 
33 #include "aom/aom_decoder.h"
34 #include "aom/aomdx.h"
35 #include "common/tools_common.h"
36 #include "common/video_reader.h"
37 
38 #define MAX_EXTERNAL_REFERENCES 128
39 #define AOM_BORDER_IN_PIXELS 288
40 
41 static const char *exec_name;
42 
43 void usage_exit(void) {
44  fprintf(stderr,
45  "Usage: %s <infile> <outfile> <num_references> <num_tile_lists> "
46  "<output format(optional)>\n",
47  exec_name);
48  exit(EXIT_FAILURE);
49 }
50 
51 static void write_tile_yuv1d(aom_codec_ctx_t *codec, const aom_image_t *img,
52  FILE *file) {
53  // read out the tile size.
54  unsigned int tile_size = 0;
55  if (aom_codec_control(codec, AV1D_GET_TILE_SIZE, &tile_size))
56  die_codec(codec, "Failed to get the tile size");
57  const unsigned int tile_width = tile_size >> 16;
58  const unsigned int tile_height = tile_size & 65535;
59  const uint8_t output_frame_width_in_tiles = img->d_w / tile_width;
60 
61  unsigned int tile_count = 0;
62  if (aom_codec_control(codec, AV1D_GET_TILE_COUNT, &tile_count))
63  die_codec(codec, "Failed to get the tile size");
64 
65  // Write tile to file.
66  const int shift = (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 1 : 0;
67  unsigned int tile_idx;
68 
69  for (tile_idx = 0; tile_idx < tile_count; ++tile_idx) {
70  const int row_offset =
71  (tile_idx / output_frame_width_in_tiles) * tile_height;
72  const int col_offset =
73  (tile_idx % output_frame_width_in_tiles) * tile_width;
74  int plane;
75 
76  for (plane = 0; plane < 3; ++plane) {
77  const unsigned char *buf = img->planes[plane];
78  const int stride = img->stride[plane];
79  const int roffset =
80  (plane > 0) ? row_offset >> img->y_chroma_shift : row_offset;
81  const int coffset =
82  (plane > 0) ? col_offset >> img->x_chroma_shift : col_offset;
83  const int w = (plane > 0) ? ((tile_width >> img->x_chroma_shift) << shift)
84  : (tile_width << shift);
85  const int h =
86  (plane > 0) ? (tile_height >> img->y_chroma_shift) : tile_height;
87  int y;
88 
89  // col offset needs to be adjusted for HBD.
90  buf += roffset * stride + (coffset << shift);
91 
92  for (y = 0; y < h; ++y) {
93  fwrite(buf, 1, w, file);
94  buf += stride;
95  }
96  }
97  }
98 }
99 
100 int main(int argc, char **argv) {
101  FILE *outfile = NULL;
102  aom_codec_ctx_t codec;
103  AvxVideoReader *reader = NULL;
104  const AvxInterface *decoder = NULL;
105  const AvxVideoInfo *info = NULL;
106  int num_references;
107  int num_tile_lists;
108  aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
109  size_t frame_size = 0;
110  const unsigned char *frame = NULL;
111  int output_format = YUV1D;
112  int i, j, n;
113 
114  exec_name = argv[0];
115 
116  if (argc < 5) die("Invalid number of arguments.");
117 
118  reader = aom_video_reader_open(argv[1]);
119  if (!reader) die("Failed to open %s for reading.", argv[1]);
120 
121  if (!(outfile = fopen(argv[2], "wb")))
122  die("Failed to open %s for writing.", argv[2]);
123 
124  num_references = (int)strtol(argv[3], NULL, 0);
125  num_tile_lists = (int)strtol(argv[4], NULL, 0);
126 
127  if (argc > 5) output_format = (int)strtol(argv[5], NULL, 0);
128  if (output_format < YUV1D || output_format > NV12)
129  die("Output format out of range [0, 2]");
130 
131  info = aom_video_reader_get_info(reader);
132 
133  decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
134  if (!decoder) die("Unknown input codec.");
135  printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
136 
137  if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
138  die_codec(&codec, "Failed to initialize decoder.");
139 
140  if (aom_codec_control(&codec, AV1D_SET_IS_ANNEXB, info->is_annexb)) {
141  die("Failed to set annex b status");
142  }
143 
144  // Decode anchor frames.
146  for (i = 0; i < num_references; ++i) {
147  aom_video_reader_read_frame(reader);
148  frame = aom_video_reader_get_frame(reader, &frame_size);
149  if (aom_codec_decode(&codec, frame, frame_size, NULL))
150  die_codec(&codec, "Failed to decode frame.");
151 
152  if (i == 0) {
153  aom_img_fmt_t ref_fmt = 0;
154  if (aom_codec_control(&codec, AV1D_GET_IMG_FORMAT, &ref_fmt))
155  die_codec(&codec, "Failed to get the image format");
156 
157  int frame_res[2];
158  if (aom_codec_control(&codec, AV1D_GET_FRAME_SIZE, frame_res))
159  die_codec(&codec, "Failed to get the image frame size");
160 
161  // Allocate memory to store decoded references. Allocate memory with the
162  // border so that it can be used as a reference.
163  for (j = 0; j < num_references; j++) {
164  unsigned int border = AOM_BORDER_IN_PIXELS;
165  if (!aom_img_alloc_with_border(&reference_images[j], ref_fmt,
166  frame_res[0], frame_res[1], 32, 8,
167  border)) {
168  die("Failed to allocate references.");
169  }
170  }
171  }
172 
174  &reference_images[i]))
175  die_codec(&codec, "Failed to copy decoded reference frame");
176 
177  aom_codec_iter_t iter = NULL;
178  aom_image_t *img = NULL;
179  while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
180  char name[1024];
181  snprintf(name, sizeof(name), "ref_%d.yuv", i);
182  printf("writing ref image to %s, %d, %d\n", name, img->d_w, img->d_h);
183  FILE *ref_file = fopen(name, "wb");
184  aom_img_write(img, ref_file);
185  fclose(ref_file);
186  }
187  }
188 
189  // Decode the lightfield.
191 
192  // Set external references.
193  av1_ext_ref_frame_t set_ext_ref = { &reference_images[0], num_references };
194  aom_codec_control_(&codec, AV1D_SET_EXT_REF_PTR, &set_ext_ref);
195  // Must decode the camera frame header first.
196  aom_video_reader_read_frame(reader);
197  frame = aom_video_reader_get_frame(reader, &frame_size);
198  if (aom_codec_decode(&codec, frame, frame_size, NULL))
199  die_codec(&codec, "Failed to decode the frame.");
200  // Decode tile lists one by one.
201  for (n = 0; n < num_tile_lists; n++) {
202  aom_video_reader_read_frame(reader);
203  frame = aom_video_reader_get_frame(reader, &frame_size);
204 
205  if (aom_codec_decode(&codec, frame, frame_size, NULL))
206  die_codec(&codec, "Failed to decode the tile list.");
207  aom_codec_iter_t iter = NULL;
208  aom_image_t *img = aom_codec_get_frame(&codec, &iter);
209  if (!img) die_codec(&codec, "Failed to get frame.");
210 
211  if (output_format == YUV1D)
212  // write the tile to the output file in 1D format.
213  write_tile_yuv1d(&codec, img, outfile);
214  else if (output_format == YUV)
215  aom_img_write(img, outfile);
216  else
217  // NV12 output format
218  aom_img_write_nv12(img, outfile);
219  }
220 
221  for (i = 0; i < num_references; i++) aom_img_free(&reference_images[i]);
222  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
223  aom_video_reader_close(reader);
224  fclose(outfile);
225 
226  return EXIT_SUCCESS;
227 }
Describes the decoder algorithm interface to applications.
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
aom_image_t * aom_img_alloc_with_border(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align, unsigned int size_align, unsigned int border)
Open a descriptor, allocating storage for the underlying image with a border.
void aom_img_free(aom_image_t *img)
Close an image descriptor.
Provides definitions for using AOM or AV1 within the aom Decoder interface.
@ AV1_SET_TILE_MODE
Definition: aomdx.h:180
@ AV1D_GET_FRAME_SIZE
Definition: aomdx.h:112
@ AV1D_SET_IS_ANNEXB
Definition: aomdx.h:200
@ AV1D_GET_TILE_COUNT
Definition: aomdx.h:129
@ AV1D_GET_TILE_SIZE
Definition: aomdx.h:126
@ AV1D_GET_IMG_FORMAT
Definition: aomdx.h:123
@ AV1D_SET_EXT_REF_PTR
Definition: aomdx.h:193
@ AV1_COPY_NEW_FRAME_IMAGE
Definition: aom.h:66
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_err_t aom_codec_control_(aom_codec_ctx_t *ctx, int ctrl_id,...)
Control algorithm.
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:423
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
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:142
Codec context structure.
Definition: aom_codec.h:204
Image Descriptor.
Definition: aom_image.h:141
unsigned int y_chroma_shift
Definition: aom_image.h:165
aom_img_fmt_t fmt
Definition: aom_image.h:142
unsigned int x_chroma_shift
Definition: aom_image.h:164
unsigned int d_w
Definition: aom_image.h:156
int stride[4]
Definition: aom_image.h:174
unsigned int d_h
Definition: aom_image.h:157
unsigned char * planes[4]
Definition: aom_image.h:173
Structure to hold the external reference frame pointer.
Definition: aomdx.h:80