AOMedia AV1 Codec
lightfield_decoder
1 /*
2  * Copyright (c) 2017, 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 Decoder
13 // ==================
14 //
15 // This is an example of a simple lightfield decoder. It builds upon the
16 // simple_decoder.c example. It takes an input file containing the compressed
17 // data (in ivf format), treating it as a lightfield instead of a video; and a
18 // text file with a list of tiles to decode. There is an optional parameter
19 // allowing to choose the output format, and the supported formats are
20 // YUV1D(default), YUV, and NV12.
21 // After running the lightfield encoder, run lightfield decoder to decode a
22 // batch of tiles:
23 // examples/lightfield_decoder vase10x10.ivf vase_reference.yuv 4 tile_list.txt
24 // 0(optional)
25 // The tile_list.txt is expected to be of the form:
26 // Frame <frame_index0>
27 // <image_index0> <anchor_index0> <tile_col0> <tile_row0>
28 // <image_index1> <anchor_index1> <tile_col1> <tile_row1>
29 // ...
30 // Frame <frame_index1)
31 // ...
32 //
33 // The "Frame" markers indicate a new render frame and thus a new tile list
34 // will be started and the old one flushed. The image_indexN, anchor_indexN,
35 // tile_colN, and tile_rowN identify an individual tile to be decoded and
36 // to use anchor_indexN anchor image for MCP.
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include "aom/aom_decoder.h"
43 #include "aom/aomdx.h"
44 #include "aom_scale/yv12config.h"
45 #include "av1/common/enums.h"
46 #include "common/tools_common.h"
47 #include "common/video_reader.h"
48 
49 static const char *exec_name;
50 
51 void usage_exit(void) {
52  fprintf(stderr,
53  "Usage: %s <infile> <outfile> <num_references> <tile_list> <output "
54  "format(optional)>\n",
55  exec_name);
56  exit(EXIT_FAILURE);
57 }
58 
59 // Output frame size
60 const int output_frame_width = 512;
61 const int output_frame_height = 512;
62 
63 static void aom_img_copy_tile(const aom_image_t *src, const aom_image_t *dst,
64  int dst_row_offset, int dst_col_offset) {
65  const int shift = (src->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 1 : 0;
66  int plane;
67 
68  for (plane = 0; plane < 3; ++plane) {
69  const unsigned char *src_buf = src->planes[plane];
70  const int src_stride = src->stride[plane];
71  unsigned char *dst_buf = dst->planes[plane];
72  const int dst_stride = dst->stride[plane];
73  const int roffset =
74  (plane > 0) ? dst_row_offset >> dst->y_chroma_shift : dst_row_offset;
75  const int coffset =
76  (plane > 0) ? dst_col_offset >> dst->x_chroma_shift : dst_col_offset;
77 
78  // col offset needs to be adjusted for HBD.
79  dst_buf += roffset * dst_stride + (coffset << shift);
80 
81  const int w = (aom_img_plane_width(src, plane) << shift);
82  const int h = aom_img_plane_height(src, plane);
83  int y;
84 
85  for (y = 0; y < h; ++y) {
86  memcpy(dst_buf, src_buf, w);
87  src_buf += src_stride;
88  dst_buf += dst_stride;
89  }
90  }
91 }
92 
93 void decode_tile(aom_codec_ctx_t *codec, const unsigned char *frame,
94  size_t frame_size, int tr, int tc, int ref_idx,
95  aom_image_t *reference_images, aom_image_t *output,
96  int *tile_idx, unsigned int *output_bit_depth,
97  aom_image_t **img_ptr, int output_format) {
101  AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1_SET_DECODE_TILE_COL, tc);
102 
103  av1_ref_frame_t ref;
104  ref.idx = 0;
105  ref.use_external_ref = 1;
106  ref.img = reference_images[ref_idx];
108  die_codec(codec, "Failed to set reference frame.");
109  }
110 
111  aom_codec_err_t aom_status = aom_codec_decode(codec, frame, frame_size, NULL);
112  if (aom_status) die_codec(codec, "Failed to decode tile.");
113 
114  aom_codec_iter_t iter = NULL;
115  aom_image_t *img = aom_codec_get_frame(codec, &iter);
116  if (!img) die_codec(codec, "Failed to get frame.");
117  *img_ptr = img;
118 
119  // aom_img_alloc() sets bit_depth as follows:
120  // output->bit_depth = (fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
121  // Use img->bit_depth(read from bitstream), so that aom_shift_img()
122  // works as expected.
123  output->bit_depth = img->bit_depth;
124  *output_bit_depth = img->bit_depth;
125 
126  if (output_format != YUV1D) {
127  // read out the tile size.
128  unsigned int tile_size = 0;
129  if (AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1D_GET_TILE_SIZE, &tile_size))
130  die_codec(codec, "Failed to get the tile size");
131  const unsigned int tile_width = tile_size >> 16;
132  const unsigned int tile_height = tile_size & 65535;
133  const uint32_t output_frame_width_in_tiles =
134  output_frame_width / tile_width;
135 
136  // Copy the tile to the output frame.
137  const int row_offset =
138  (*tile_idx / output_frame_width_in_tiles) * tile_height;
139  const int col_offset =
140  (*tile_idx % output_frame_width_in_tiles) * tile_width;
141 
142  aom_img_copy_tile(img, output, row_offset, col_offset);
143  (*tile_idx)++;
144  }
145 }
146 
147 static void img_write_to_file(const aom_image_t *img, FILE *file,
148  int output_format) {
149  if (output_format == YUV)
150  aom_img_write(img, file);
151  else if (output_format == NV12)
152  aom_img_write_nv12(img, file);
153  else
154  die("Invalid output format");
155 }
156 
157 int main(int argc, char **argv) {
158  FILE *outfile = NULL;
159  AvxVideoReader *reader = NULL;
160  const AvxVideoInfo *info = NULL;
161  int num_references;
162  aom_img_fmt_t ref_fmt = 0;
163  aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
164  aom_image_t output;
165  aom_image_t *output_shifted = NULL;
166  size_t frame_size = 0;
167  const unsigned char *frame = NULL;
168  int i, j;
169  const char *tile_list_file = NULL;
170  int output_format = YUV1D;
171  exec_name = argv[0];
172 
173  if (argc < 5) die("Invalid number of arguments.");
174 
175  reader = aom_video_reader_open(argv[1]);
176  if (!reader) die("Failed to open %s for reading.", argv[1]);
177 
178  if (!(outfile = fopen(argv[2], "wb")))
179  die("Failed to open %s for writing.", argv[2]);
180 
181  num_references = (int)strtol(argv[3], NULL, 0);
182  tile_list_file = argv[4];
183 
184  if (argc > 5) output_format = (int)strtol(argv[5], NULL, 0);
185  if (output_format < YUV1D || output_format > NV12)
186  die("Output format out of range [0, 2]");
187 
188  info = aom_video_reader_get_info(reader);
189 
190  aom_codec_iface_t *decoder;
191  if (info->codec_fourcc == LST_FOURCC)
192  decoder = get_aom_decoder_by_fourcc(AV1_FOURCC);
193  else
194  die("Unknown input codec.");
195  printf("Using %s\n", aom_codec_iface_name(decoder));
196 
197  aom_codec_ctx_t codec;
198  if (aom_codec_dec_init(&codec, decoder, NULL, 0))
199  die_codec(&codec, "Failed to initialize decoder.");
200 
202  info->is_annexb)) {
203  die("Failed to set annex b status");
204  }
205 
206  // Decode anchor frames.
208  for (i = 0; i < num_references; ++i) {
209  aom_video_reader_read_frame(reader);
210  frame = aom_video_reader_get_frame(reader, &frame_size);
211  if (aom_codec_decode(&codec, frame, frame_size, NULL))
212  die_codec(&codec, "Failed to decode frame.");
213 
214  if (i == 0) {
215  if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_GET_IMG_FORMAT, &ref_fmt))
216  die_codec(&codec, "Failed to get the image format");
217 
218  int frame_res[2];
219  if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_GET_FRAME_SIZE, frame_res))
220  die_codec(&codec, "Failed to get the image frame size");
221 
222  // Allocate memory to store decoded references. Allocate memory with the
223  // border so that it can be used as a reference.
224  for (j = 0; j < num_references; j++) {
225  unsigned int border = AOM_DEC_BORDER_IN_PIXELS;
226  if (!aom_img_alloc_with_border(&reference_images[j], ref_fmt,
227  frame_res[0], frame_res[1], 32, 8,
228  border)) {
229  die("Failed to allocate references.");
230  }
231  }
232  }
233 
235  &reference_images[i]))
236  die_codec(&codec, "Failed to copy decoded reference frame");
237 
238  aom_codec_iter_t iter = NULL;
239  aom_image_t *img = NULL;
240  while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
241  char name[1024];
242  snprintf(name, sizeof(name), "ref_%d.yuv", i);
243  printf("writing ref image to %s, %u, %u\n", name, img->d_w, img->d_h);
244  FILE *ref_file = fopen(name, "wb");
245  aom_img_write(img, ref_file);
246  fclose(ref_file);
247  }
248  }
249 
250  FILE *infile = aom_video_reader_get_file(reader);
251  // Record the offset of the first camera image.
252  const FileOffset camera_frame_pos = ftello(infile);
253 
254  printf("Loading compressed frames into memory.\n");
255 
256  // Count the frames in the lightfield.
257  int num_frames = 0;
258  while (aom_video_reader_read_frame(reader)) {
259  ++num_frames;
260  }
261  if (num_frames < 1) die("Input light field has no frames.");
262 
263  // Read all of the lightfield frames into memory.
264  unsigned char **frames =
265  (unsigned char **)malloc(num_frames * sizeof(unsigned char *));
266  size_t *frame_sizes = (size_t *)malloc(num_frames * sizeof(size_t));
267  // Seek to the first camera image.
268  fseeko(infile, camera_frame_pos, SEEK_SET);
269  for (int f = 0; f < num_frames; ++f) {
270  aom_video_reader_read_frame(reader);
271  frame = aom_video_reader_get_frame(reader, &frame_size);
272  frames[f] = (unsigned char *)malloc(frame_size * sizeof(unsigned char));
273  memcpy(frames[f], frame, frame_size);
274  frame_sizes[f] = frame_size;
275  }
276  printf("Read %d frames.\n", num_frames);
277 
278  if (output_format != YUV1D) {
279  // Allocate the output frame.
280  aom_img_fmt_t out_fmt = ref_fmt;
281  if (FORCE_HIGHBITDEPTH_DECODING) out_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
282  if (!aom_img_alloc(&output, out_fmt, output_frame_width,
283  output_frame_height, 32))
284  die("Failed to allocate output image.");
285  }
286 
287  printf("Decoding tile list from file.\n");
288  char line[1024];
289  FILE *tile_list_fptr = fopen(tile_list_file, "r");
290  if (!tile_list_fptr) die_codec(&codec, "Failed to open tile list file.");
291  int tile_list_cnt = 0;
292  int tile_list_writes = 0;
293  int tile_idx = 0;
294  aom_image_t *out = NULL;
295  unsigned int output_bit_depth = 0;
296 
297  while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
298  if (line[0] == 'F') {
299  if (output_format != YUV1D) {
300  // Write out the tile list.
301  if (tile_list_cnt) {
302  out = &output;
303  if (output_bit_depth != 0)
304  aom_shift_img(output_bit_depth, &out, &output_shifted);
305  img_write_to_file(out, outfile, output_format);
306  tile_list_writes++;
307  }
308 
309  tile_list_cnt++;
310  tile_idx = 0;
311  // Then memset the frame.
312  memset(output.img_data, 0, output.sz);
313  }
314  continue;
315  }
316 
317  int image_idx, ref_idx, tc, tr;
318  sscanf(line, "%d %d %d %d", &image_idx, &ref_idx, &tc, &tr);
319  if (image_idx >= num_frames) {
320  die("Tile list image_idx out of bounds: %d >= %d.", image_idx,
321  num_frames);
322  }
323  if (ref_idx >= num_references) {
324  die("Tile list ref_idx out of bounds: %d >= %d.", ref_idx,
325  num_references);
326  }
327  frame = frames[image_idx];
328  frame_size = frame_sizes[image_idx];
329 
330  aom_image_t *img = NULL;
331  decode_tile(&codec, frame, frame_size, tr, tc, ref_idx, reference_images,
332  &output, &tile_idx, &output_bit_depth, &img, output_format);
333  if (output_format == YUV1D) {
334  out = img;
335  if (output_bit_depth != 0)
336  aom_shift_img(output_bit_depth, &out, &output_shifted);
337  aom_img_write(out, outfile);
338  }
339  }
340 
341  if (output_format != YUV1D) {
342  // Write out the last tile list.
343  if (tile_list_writes < tile_list_cnt) {
344  out = &output;
345  if (output_bit_depth != 0)
346  aom_shift_img(output_bit_depth, &out, &output_shifted);
347  img_write_to_file(out, outfile, output_format);
348  }
349  }
350 
351  if (output_shifted) aom_img_free(output_shifted);
352  if (output_format != YUV1D) aom_img_free(&output);
353  for (i = 0; i < num_references; i++) aom_img_free(&reference_images[i]);
354  for (int f = 0; f < num_frames; ++f) {
355  free(frames[f]);
356  }
357  free(frame_sizes);
358  free(frames);
359  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
360  aom_video_reader_close(reader);
361  fclose(outfile);
362 
363  return EXIT_SUCCESS;
364 }
unsigned char * img_data
Definition: aom_image.h:217
unsigned int d_h
Definition: aom_image.h:187
#define AOM_CODEC_CONTROL_TYPECHECKED(ctx, id, data)
aom_codec_control wrapper macro (adds type-checking, less flexible)
Definition: aom_codec.h:520
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
int idx
Definition: aom.h:90
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
unsigned char * planes[3]
Definition: aom_image.h:202
Codec control function to indicate whether bitstream is in Annex-B format, unsigned int parameter...
Definition: aomdx.h:345
Codec context structure.
Definition: aom_codec.h:298
Codec control function to get the size of the tile, unsigned int parameter.
Definition: aomdx.h:237
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
unsigned int y_chroma_shift
Definition: aom_image.h:195
Describes the decoder algorithm interface to applications.
Codec control function to get the dimensions that the current frame is decoded at, int* parameter. This may be different to the intended display size for the frame as specified in the wrapper or frame header (see AV1D_GET_DISPLAY_SIZE).
Definition: aomdx.h:216
Image Descriptor.
Definition: aom_image.h:171
aom_image_t * aom_img_alloc(aom_image_t *img, aom_img_fmt_t fmt, unsigned int d_w, unsigned int d_h, unsigned int align)
Open a descriptor, allocating storage for the underlying image.
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.
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition: aom_codec.h:254
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition: aom_decoder.h:129
Codec control function to write a frame into a reference buffer.
Definition: aom.h:57
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.
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.
Codec control function to get the image format of the stream, aom_img_fmt_t* parameter.
Definition: aomdx.h:233
Codec control function to enable the ext-tile software debug and testing code in the decoder...
Definition: aomdx.h:332
void aom_img_free(aom_image_t *img)
Close an image descriptor.
int aom_img_plane_width(const aom_image_t *img, int plane)
Get the width of a plane.
AV1 specific reference frame data struct.
Definition: aom.h:89
Codec control function to copy the new frame to an external buffer.
Definition: aom.h:76
int aom_img_plane_height(const aom_image_t *img, int plane)
Get the height of a plane.
Codec control function to set the range of tile decoding, int parameter.
Definition: aomdx.h:301
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:288
unsigned int x_chroma_shift
Definition: aom_image.h:194
Codec control function to set the tile coding mode, int parameter.
Definition: aomdx.h:309
size_t sz
Definition: aom_image.h:204
int use_external_ref
Definition: aom.h:91
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:155
Provides definitions for using AOM or AV1 within the aom Decoder interface.
int stride[3]
Definition: aom_image.h:203
aom_image_t img
Definition: aom.h:92
unsigned int bit_depth
Definition: aom_image.h:183
unsigned int d_w
Definition: aom_image.h:186
aom_img_fmt_t fmt
Definition: aom_image.h:172