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.
22 // Run lightfield tile list decoder to decode an AV1 tile list file:
23 // examples/lightfield_tile_list_decoder vase_tile_list.ivf vase_tile_list.yuv
24 // 4 2
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <assert.h>
30 
31 #include "aom/aom_decoder.h"
32 #include "aom/aomdx.h"
33 #include "common/tools_common.h"
34 #include "common/video_reader.h"
35 
36 #define MAX_EXTERNAL_REFERENCES 128
37 #define AOM_BORDER_IN_PIXELS 288
38 
39 static const char *exec_name;
40 
41 void usage_exit(void) {
42  fprintf(stderr,
43  "Usage: %s <infile> <outfile> <num_references> <num_tile_lists>\n",
44  exec_name);
45  exit(EXIT_FAILURE);
46 }
47 
48 int main(int argc, char **argv) {
49  FILE *outfile = NULL;
50  aom_codec_ctx_t codec;
51  AvxVideoReader *reader = NULL;
52  const AvxInterface *decoder = NULL;
53  const AvxVideoInfo *info = NULL;
54  int width, height;
55  int num_references;
56  int num_tile_lists;
57  aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
58  size_t frame_size = 0;
59  const unsigned char *frame = NULL;
60  int i, n;
61 
62  exec_name = argv[0];
63 
64  if (argc != 5) die("Invalid number of arguments.");
65 
66  reader = aom_video_reader_open(argv[1]);
67  if (!reader) die("Failed to open %s for reading.", argv[1]);
68 
69  if (!(outfile = fopen(argv[2], "wb")))
70  die("Failed to open %s for writing.", argv[2]);
71 
72  num_references = (int)strtol(argv[3], NULL, 0);
73  num_tile_lists = (int)strtol(argv[4], NULL, 0);
74 
75  info = aom_video_reader_get_info(reader);
76  width = info->frame_width;
77  height = info->frame_height;
78 
79  decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
80  if (!decoder) die("Unknown input codec.");
81  printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
82 
83  if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
84  die_codec(&codec, "Failed to initialize decoder.");
85 
86  // Allocate memory to store decoded references.
88  if (!CONFIG_LOWBITDEPTH) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
89  // Allocate memory with the border so that it can be used as a reference.
90  for (i = 0; i < num_references; i++) {
91  unsigned int border = AOM_BORDER_IN_PIXELS;
92  if (!aom_img_alloc_with_border(&reference_images[i], ref_fmt, width, height,
93  32, 8, border)) {
94  die("Failed to allocate references.");
95  }
96  }
97 
98  // Decode anchor frames.
100 
101  for (i = 0; i < num_references; ++i) {
102  aom_video_reader_read_frame(reader);
103  frame = aom_video_reader_get_frame(reader, &frame_size);
104  if (aom_codec_decode(&codec, frame, frame_size, NULL))
105  die_codec(&codec, "Failed to decode frame.");
106 
108  &reference_images[i]))
109  die_codec(&codec, "Failed to copy decoded reference frame");
110 
111  aom_codec_iter_t iter = NULL;
112  aom_image_t *img = NULL;
113  while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
114  char name[1024];
115  snprintf(name, sizeof(name), "ref_%d.yuv", i);
116  printf("writing ref image to %s, %d, %d\n", name, img->d_w, img->d_h);
117  FILE *ref_file = fopen(name, "wb");
118  aom_img_write(img, ref_file);
119  fclose(ref_file);
120  }
121  }
122 
123  // Decode the lightfield.
125 
126  // Set external references.
127  av1_ext_ref_frame_t set_ext_ref = { &reference_images[0], num_references };
128  aom_codec_control_(&codec, AV1D_SET_EXT_REF_PTR, &set_ext_ref);
129 
130  // Must decode the camera frame header first.
131  aom_video_reader_read_frame(reader);
132  frame = aom_video_reader_get_frame(reader, &frame_size);
133  if (aom_codec_decode(&codec, frame, frame_size, NULL))
134  die_codec(&codec, "Failed to decode the frame.");
135 
136  // Decode tile lists one by one.
137  for (n = 0; n < num_tile_lists; n++) {
138  aom_video_reader_read_frame(reader);
139  frame = aom_video_reader_get_frame(reader, &frame_size);
140 
141  if (aom_codec_decode(&codec, frame, frame_size, NULL))
142  die_codec(&codec, "Failed to decode the tile list.");
143  aom_codec_iter_t iter = NULL;
144  aom_image_t *img;
145  while ((img = aom_codec_get_frame(&codec, &iter)))
146  fwrite(img->img_data, 1, img->sz, outfile);
147  }
148 
149  for (i = 0; i < num_references; i++) aom_img_free(&reference_images[i]);
150  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
151  aom_video_reader_close(reader);
152  fclose(outfile);
153 
154  return EXIT_SUCCESS;
155 }
unsigned char * img_data
Definition: aom_image.h:188
unsigned int d_h
Definition: aom_image.h:157
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
Codec context structure.
Definition: aom_codec.h:204
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
Describes the decoder algorithm interface to applications.
Definition: aomdx.h:184
Image Descriptor.
Definition: aom_image.h:141
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.
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.
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:423
Definition: aom.h:66
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
Definition: aomdx.h:171
size_t sz
Definition: aom_image.h:175
Structure to hold the external reference frame pointer.
Definition: aomdx.h:80
Provides definitions for using AOM or AV1 within the aom Decoder interface.
Definition: aom_image.h:45
unsigned int d_w
Definition: aom_image.h:156