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