AOMedia Codec SDK
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.
18 // After running the lightfield encoder, run lightfield decoder to decode a
19 // batch of tiles:
20 // examples/lightfield_decoder vase10x10.ivf vase_reference.yuv 4
21 
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "aom/aom_decoder.h"
27 #include "aom/aomdx.h"
28 #include "aom_scale/yv12config.h"
29 #include "av1/common/enums.h"
30 #include "common/tools_common.h"
31 #include "common/video_reader.h"
32 
33 static const char *exec_name;
34 
35 void usage_exit(void) {
36  fprintf(stderr, "Usage: %s <infile> <outfile> <num_references>\n", exec_name);
37  exit(EXIT_FAILURE);
38 }
39 
40 // Tile list entry provided by the application
41 typedef struct {
42  int image_idx;
43  int reference_idx;
44  int tile_col;
45  int tile_row;
46 } TILE_LIST_INFO;
47 
48 // M references: 0 - M-1; N images(including references): 0 - N-1;
49 // Note: order the image index incrementally, so that we only go through the
50 // bitstream once to construct the tile list.
51 const int num_tile_lists = 2;
52 const uint16_t tile_count_minus_1 = 9 - 1;
53 const TILE_LIST_INFO tile_list[2][9] = {
54  { { 16, 0, 4, 5 },
55  { 83, 3, 13, 2 },
56  { 57, 2, 2, 6 },
57  { 31, 1, 11, 5 },
58  { 2, 0, 7, 4 },
59  { 77, 3, 9, 9 },
60  { 49, 1, 0, 1 },
61  { 6, 0, 3, 10 },
62  { 63, 2, 5, 8 } },
63  { { 65, 2, 11, 1 },
64  { 42, 1, 3, 7 },
65  { 88, 3, 8, 4 },
66  { 76, 3, 1, 15 },
67  { 1, 0, 2, 2 },
68  { 19, 0, 5, 6 },
69  { 60, 2, 4, 0 },
70  { 25, 1, 11, 15 },
71  { 50, 2, 5, 4 } },
72 };
73 
74 int main(int argc, char **argv) {
75  FILE *outfile = NULL;
76  aom_codec_ctx_t codec;
77  AvxVideoReader *reader = NULL;
78  const AvxInterface *decoder = NULL;
79  const AvxVideoInfo *info = NULL;
80  int num_references;
81  aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
82  size_t frame_size = 0;
83  const unsigned char *frame = NULL;
84  int n, i, j;
85  exec_name = argv[0];
86 
87  if (argc != 4) die("Invalid number of arguments.");
88 
89  reader = aom_video_reader_open(argv[1]);
90  if (!reader) die("Failed to open %s for reading.", argv[1]);
91 
92  if (!(outfile = fopen(argv[2], "wb")))
93  die("Failed to open %s for writing.", argv[2]);
94 
95  num_references = (int)strtol(argv[3], NULL, 0);
96 
97  info = aom_video_reader_get_info(reader);
98 
99  decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
100  if (!decoder) die("Unknown input codec.");
101  printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
102 
103  if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
104  die_codec(&codec, "Failed to initialize decoder.");
105 
106  if (aom_codec_control(&codec, AV1D_SET_IS_ANNEXB, info->is_annexb)) {
107  die("Failed to set annex b status");
108  }
109 
110  // Decode anchor frames.
112  for (i = 0; i < num_references; ++i) {
113  aom_video_reader_read_frame(reader);
114  frame = aom_video_reader_get_frame(reader, &frame_size);
115  if (aom_codec_decode(&codec, frame, frame_size, NULL))
116  die_codec(&codec, "Failed to decode frame.");
117 
118  if (i == 0) {
119  aom_img_fmt_t ref_fmt = 0;
120  if (aom_codec_control(&codec, AV1D_GET_IMG_FORMAT, &ref_fmt))
121  die_codec(&codec, "Failed to get the image format");
122 
123  int frame_res[2];
124  if (aom_codec_control(&codec, AV1D_GET_FRAME_SIZE, frame_res))
125  die_codec(&codec, "Failed to get the image frame size");
126 
127  // Allocate memory to store decoded references. Allocate memory with the
128  // border so that it can be used as a reference.
129  for (j = 0; j < num_references; j++) {
130  unsigned int border = AOM_BORDER_IN_PIXELS;
131  if (!aom_img_alloc_with_border(&reference_images[j], ref_fmt,
132  frame_res[0], frame_res[1], 32, 8,
133  border)) {
134  die("Failed to allocate references.");
135  }
136  }
137  }
138 
140  &reference_images[i]))
141  die_codec(&codec, "Failed to copy decoded reference frame");
142 
143  aom_codec_iter_t iter = NULL;
144  aom_image_t *img = NULL;
145  while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
146  char name[1024];
147  snprintf(name, sizeof(name), "ref_%d.yuv", i);
148  printf("writing ref image to %s, %d, %d\n", name, img->d_w, img->d_h);
149  FILE *ref_file = fopen(name, "wb");
150  aom_img_write(img, ref_file);
151  fclose(ref_file);
152  }
153  }
154 
155  FILE *infile = aom_video_reader_get_file(reader);
156  // Record the offset of the first camera image.
157  const FileOffset camera_frame_pos = ftello(infile);
158 
159  // Process 1 tile.
160  for (n = 0; n < num_tile_lists; n++) {
161  for (i = 0; i <= tile_count_minus_1; i++) {
162  int image_idx = tile_list[n][i].image_idx;
163  int ref_idx = tile_list[n][i].reference_idx;
164  int tc = tile_list[n][i].tile_col;
165  int tr = tile_list[n][i].tile_row;
166  int frame_cnt = -1;
167 
168  // Seek to the first camera image.
169  fseeko(infile, camera_frame_pos, SEEK_SET);
170 
171  // Read out the camera image
172  while (frame_cnt != image_idx) {
173  aom_video_reader_read_frame(reader);
174  frame_cnt++;
175  }
176 
177  frame = aom_video_reader_get_frame(reader, &frame_size);
178 
182  aom_codec_control_(&codec, AV1_SET_DECODE_TILE_COL, tc);
183 
184  av1_ref_frame_t ref;
185  ref.idx = 0;
186  ref.use_external_ref = 1;
187  ref.img = reference_images[ref_idx];
188  if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) {
189  die_codec(&codec, "Failed to set reference frame.");
190  }
191 
192  aom_codec_err_t aom_status =
193  aom_codec_decode(&codec, frame, frame_size, NULL);
194  if (aom_status) die_codec(&codec, "Failed to decode tile.");
195 
196  aom_codec_iter_t iter = NULL;
197  aom_image_t *img = aom_codec_get_frame(&codec, &iter);
198  aom_img_write(img, outfile);
199  }
200  }
201 
202  for (i = 0; i < num_references; i++) aom_img_free(&reference_images[i]);
203  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
204  aom_video_reader_close(reader);
205  fclose(outfile);
206 
207  return EXIT_SUCCESS;
208 }
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.
int idx
Definition: aom.h:109
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: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
Definition: aom.h:60
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
Definition: aomdx.h:194
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
AV1 specific reference frame data struct.
Definition: aom.h:108
Definition: aom.h:66
Definition: aomdx.h:171
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
Definition: aomdx.h:177
int use_external_ref
Definition: aom.h:110
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
Provides definitions for using AOM or AV1 within the aom Decoder interface.
aom_image_t img
Definition: aom.h:111
unsigned int d_w
Definition: aom_image.h:156