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 "aom_scale/yv12config.h"
34 #include "av1/common/enums.h"
35 #include "common/tools_common.h"
36 #include "common/video_reader.h"
37 
38 static const char *exec_name;
39 
40 void usage_exit(void) {
41  fprintf(stderr,
42  "Usage: %s <infile> <outfile> <num_references> <num_tile_lists>\n",
43  exec_name);
44  exit(EXIT_FAILURE);
45 }
46 
47 int main(int argc, char **argv) {
48  FILE *outfile = NULL;
49  aom_codec_ctx_t codec;
50  AvxVideoReader *reader = NULL;
51  const AvxInterface *decoder = NULL;
52  const AvxVideoInfo *info = NULL;
53  int num_references;
54  int num_tile_lists;
55  aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
56  size_t frame_size = 0;
57  const unsigned char *frame = NULL;
58  int i, j, n;
59 
60  exec_name = argv[0];
61 
62  if (argc != 5) die("Invalid number of arguments.");
63 
64  reader = aom_video_reader_open(argv[1]);
65  if (!reader) die("Failed to open %s for reading.", argv[1]);
66 
67  if (!(outfile = fopen(argv[2], "wb")))
68  die("Failed to open %s for writing.", argv[2]);
69 
70  num_references = (int)strtol(argv[3], NULL, 0);
71  num_tile_lists = (int)strtol(argv[4], NULL, 0);
72 
73  info = aom_video_reader_get_info(reader);
74 
75  decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
76  if (!decoder) die("Unknown input codec.");
77  printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
78 
79  if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
80  die_codec(&codec, "Failed to initialize decoder.");
81 
82  if (aom_codec_control(&codec, AV1D_SET_IS_ANNEXB, info->is_annexb)) {
83  die("Failed to set annex b status");
84  }
85 
86  // Decode anchor frames.
88  for (i = 0; i < num_references; ++i) {
89  aom_video_reader_read_frame(reader);
90  frame = aom_video_reader_get_frame(reader, &frame_size);
91  if (aom_codec_decode(&codec, frame, frame_size, NULL))
92  die_codec(&codec, "Failed to decode frame.");
93 
94  if (i == 0) {
95  aom_img_fmt_t ref_fmt = 0;
96  if (aom_codec_control(&codec, AV1D_GET_IMG_FORMAT, &ref_fmt))
97  die_codec(&codec, "Failed to get the image format");
98 
99  int frame_res[2];
100  if (aom_codec_control(&codec, AV1D_GET_FRAME_SIZE, frame_res))
101  die_codec(&codec, "Failed to get the image frame size");
102 
103  // Allocate memory to store decoded references. Allocate memory with the
104  // border so that it can be used as a reference.
105  for (j = 0; j < num_references; j++) {
106  unsigned int border = AOM_BORDER_IN_PIXELS;
107  if (!aom_img_alloc_with_border(&reference_images[j], ref_fmt,
108  frame_res[0], frame_res[1], 32, 8,
109  border)) {
110  die("Failed to allocate references.");
111  }
112  }
113  }
114 
116  &reference_images[i]))
117  die_codec(&codec, "Failed to copy decoded reference frame");
118 
119  aom_codec_iter_t iter = NULL;
120  aom_image_t *img = NULL;
121  while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
122  char name[1024];
123  snprintf(name, sizeof(name), "ref_%d.yuv", i);
124  printf("writing ref image to %s, %d, %d\n", name, img->d_w, img->d_h);
125  FILE *ref_file = fopen(name, "wb");
126  aom_img_write(img, ref_file);
127  fclose(ref_file);
128  }
129  }
130 
131  // Decode the lightfield.
133 
134  // Set external references.
135  av1_ext_ref_frame_t set_ext_ref = { &reference_images[0], num_references };
136  aom_codec_control_(&codec, AV1D_SET_EXT_REF_PTR, &set_ext_ref);
137  // Must decode the camera frame header first.
138  aom_video_reader_read_frame(reader);
139  frame = aom_video_reader_get_frame(reader, &frame_size);
140  if (aom_codec_decode(&codec, frame, frame_size, NULL))
141  die_codec(&codec, "Failed to decode the frame.");
142  // Decode tile lists one by one.
143  for (n = 0; n < num_tile_lists; n++) {
144  aom_video_reader_read_frame(reader);
145  frame = aom_video_reader_get_frame(reader, &frame_size);
146 
147  if (aom_codec_decode(&codec, frame, frame_size, NULL))
148  die_codec(&codec, "Failed to decode the tile list.");
149  aom_codec_iter_t iter = NULL;
150  aom_image_t *img;
151  while ((img = aom_codec_get_frame(&codec, &iter)))
152  fwrite(img->img_data, 1, img->sz, outfile);
153  }
154 
155  for (i = 0; i < num_references; i++) aom_img_free(&reference_images[i]);
156  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
157  aom_video_reader_close(reader);
158  fclose(outfile);
159 
160  return EXIT_SUCCESS;
161 }
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.
Definition: aomdx.h:203
Codec context structure.
Definition: aom_codec.h:204
Describes the decoder algorithm interface to applications.
Definition: aomdx.h:190
Definition: aomdx.h:112
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.
Definition: aomdx.h:123
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:414
Definition: aom.h:66
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
Definition: aomdx.h:177
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.
unsigned int d_w
Definition: aom_image.h:156