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; 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 "common/tools_common.h"
45 #include "common/video_reader.h"
46 
47 #define MAX_EXTERNAL_REFERENCES 128
48 #define AOM_BORDER_IN_PIXELS 288
49 
50 static const char *exec_name;
51 
52 void usage_exit(void) {
53  fprintf(stderr,
54  "Usage: %s <infile> <outfile> <num_references> <tile_list> <output "
55  "format(optional)>\n",
56  exec_name);
57  exit(EXIT_FAILURE);
58 }
59 
60 // Output frame size
61 const int output_frame_width = 512;
62 const int output_frame_height = 512;
63 
64 static void aom_img_copy_tile(const aom_image_t *src, const aom_image_t *dst,
65  int dst_row_offset, int dst_col_offset) {
66  const int shift = (src->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 1 : 0;
67  int plane;
68 
69  for (plane = 0; plane < 3; ++plane) {
70  const unsigned char *src_buf = src->planes[plane];
71  const int src_stride = src->stride[plane];
72  unsigned char *dst_buf = dst->planes[plane];
73  const int dst_stride = dst->stride[plane];
74  const int roffset =
75  (plane > 0) ? dst_row_offset >> dst->y_chroma_shift : dst_row_offset;
76  const int coffset =
77  (plane > 0) ? dst_col_offset >> dst->x_chroma_shift : dst_col_offset;
78 
79  // col offset needs to be adjusted for HBD.
80  dst_buf += roffset * dst_stride + (coffset << shift);
81 
82  const int w = (aom_img_plane_width(src, plane) << shift);
83  const int h = aom_img_plane_height(src, plane);
84  int y;
85 
86  for (y = 0; y < h; ++y) {
87  memcpy(dst_buf, src_buf, w);
88  src_buf += src_stride;
89  dst_buf += dst_stride;
90  }
91  }
92 }
93 
94 void decode_tile(aom_codec_ctx_t *codec, const unsigned char *frame,
95  size_t frame_size, int tr, int tc, int ref_idx,
96  aom_image_t *reference_images, aom_image_t *output,
97  int *tile_idx, unsigned int *output_bit_depth,
98  aom_image_t **img_ptr, int output_format) {
102  aom_codec_control_(codec, AV1_SET_DECODE_TILE_COL, tc);
103 
104  av1_ref_frame_t ref;
105  ref.idx = 0;
106  ref.use_external_ref = 1;
107  ref.img = reference_images[ref_idx];
108  if (aom_codec_control(codec, AV1_SET_REFERENCE, &ref)) {
109  die_codec(codec, "Failed to set reference frame.");
110  }
111 
112  aom_codec_err_t aom_status = aom_codec_decode(codec, frame, frame_size, NULL);
113  if (aom_status) die_codec(codec, "Failed to decode tile.");
114 
115  aom_codec_iter_t iter = NULL;
116  aom_image_t *img = aom_codec_get_frame(codec, &iter);
117  if (!img) die_codec(codec, "Failed to get frame.");
118  *img_ptr = img;
119 
120  // aom_img_alloc() sets bit_depth as follows:
121  // output->bit_depth = (fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
122  // Use img->bit_depth(read from bitstream), so that aom_shift_img()
123  // works as expected.
124  output->bit_depth = img->bit_depth;
125  *output_bit_depth = img->bit_depth;
126 
127  if (output_format != YUV1D) {
128  // read out the tile size.
129  unsigned int tile_size = 0;
130  if (aom_codec_control(codec, AV1D_GET_TILE_SIZE, &tile_size))
131  die_codec(codec, "Failed to get the tile size");
132  const unsigned int tile_width = tile_size >> 16;
133  const unsigned int tile_height = tile_size & 65535;
134  const uint8_t output_frame_width_in_tiles = 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  aom_codec_ctx_t codec;
160  AvxVideoReader *reader = NULL;
161  const AvxInterface *decoder = NULL;
162  const AvxVideoInfo *info = NULL;
163  int num_references;
164  aom_img_fmt_t ref_fmt = 0;
165  aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
166  aom_image_t output;
167  aom_image_t *output_shifted = NULL;
168  size_t frame_size = 0;
169  const unsigned char *frame = NULL;
170  int i, j;
171  const char *tile_list_file = NULL;
172  int output_format = YUV1D;
173  exec_name = argv[0];
174 
175  if (argc < 5) die("Invalid number of arguments.");
176 
177  reader = aom_video_reader_open(argv[1]);
178  if (!reader) die("Failed to open %s for reading.", argv[1]);
179 
180  if (!(outfile = fopen(argv[2], "wb")))
181  die("Failed to open %s for writing.", argv[2]);
182 
183  num_references = (int)strtol(argv[3], NULL, 0);
184  tile_list_file = argv[4];
185 
186  if (argc > 5) output_format = (int)strtol(argv[5], NULL, 0);
187  if (output_format < YUV1D || output_format > NV12)
188  die("Output format out of range [0, 2]");
189 
190  info = aom_video_reader_get_info(reader);
191 
192  decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
193  if (!decoder) die("Unknown input codec.");
194  printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
195 
196  if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
197  die_codec(&codec, "Failed to initialize decoder.");
198 
199  if (aom_codec_control(&codec, AV1D_SET_IS_ANNEXB, info->is_annexb)) {
200  die("Failed to set annex b status");
201  }
202 
203  // Decode anchor frames.
205  for (i = 0; i < num_references; ++i) {
206  aom_video_reader_read_frame(reader);
207  frame = aom_video_reader_get_frame(reader, &frame_size);
208  if (aom_codec_decode(&codec, frame, frame_size, NULL))
209  die_codec(&codec, "Failed to decode frame.");
210 
211  if (i == 0) {
212  if (aom_codec_control(&codec, AV1D_GET_IMG_FORMAT, &ref_fmt))
213  die_codec(&codec, "Failed to get the image format");
214 
215  int frame_res[2];
216  if (aom_codec_control(&codec, AV1D_GET_FRAME_SIZE, frame_res))
217  die_codec(&codec, "Failed to get the image frame size");
218 
219  // Allocate memory to store decoded references. Allocate memory with the
220  // border so that it can be used as a reference.
221  for (j = 0; j < num_references; j++) {
222  unsigned int border = AOM_BORDER_IN_PIXELS;
223  if (!aom_img_alloc_with_border(&reference_images[j], ref_fmt,
224  frame_res[0], frame_res[1], 32, 8,
225  border)) {
226  die("Failed to allocate references.");
227  }
228  }
229  }
230 
232  &reference_images[i]))
233  die_codec(&codec, "Failed to copy decoded reference frame");
234 
235  aom_codec_iter_t iter = NULL;
236  aom_image_t *img = NULL;
237  while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
238  char name[1024];
239  snprintf(name, sizeof(name), "ref_%d.yuv", i);
240  printf("writing ref image to %s, %d, %d\n", name, img->d_w, img->d_h);
241  FILE *ref_file = fopen(name, "wb");
242  aom_img_write(img, ref_file);
243  fclose(ref_file);
244  }
245  }
246 
247  FILE *infile = aom_video_reader_get_file(reader);
248  // Record the offset of the first camera image.
249  const FileOffset camera_frame_pos = ftello(infile);
250 
251  printf("Loading compressed frames into memory.\n");
252 
253  // Count the frames in the lightfield.
254  int num_frames = 0;
255  while (aom_video_reader_read_frame(reader)) {
256  ++num_frames;
257  }
258  if (num_frames < 1) die("Input light field has no frames.");
259 
260  // Read all of the lightfield frames into memory.
261  unsigned char **frames =
262  (unsigned char **)malloc(num_frames * sizeof(unsigned char *));
263  size_t *frame_sizes = (size_t *)malloc(num_frames * sizeof(size_t));
264  // Seek to the first camera image.
265  fseeko(infile, camera_frame_pos, SEEK_SET);
266  for (int f = 0; f < num_frames; ++f) {
267  aom_video_reader_read_frame(reader);
268  frame = aom_video_reader_get_frame(reader, &frame_size);
269  frames[f] = (unsigned char *)malloc(frame_size * sizeof(unsigned char));
270  memcpy(frames[f], frame, frame_size);
271  frame_sizes[f] = frame_size;
272  }
273  printf("Read %d frames.\n", num_frames);
274 
275  if (output_format != YUV1D) {
276  // Allocate the output frame.
277  aom_img_fmt_t out_fmt = ref_fmt;
278  if (!CONFIG_LOWBITDEPTH) out_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
279  if (!aom_img_alloc(&output, out_fmt, output_frame_width,
280  output_frame_height, 32))
281  die("Failed to allocate output image.");
282  }
283 
284  printf("Decoding tile list from file.\n");
285  char line[1024];
286  FILE *tile_list_fptr = fopen(tile_list_file, "r");
287  int tile_list_cnt = 0;
288  int tile_list_writes = 0;
289  int tile_idx = 0;
290  aom_image_t *out = NULL;
291  unsigned int output_bit_depth = 0;
292 
293  while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
294  if (line[0] == 'F') {
295  if (output_format != YUV1D) {
296  // Write out the tile list.
297  if (tile_list_cnt) {
298  out = &output;
299  if (output_bit_depth != 0)
300  aom_shift_img(output_bit_depth, &out, &output_shifted);
301  img_write_to_file(out, outfile, output_format);
302  tile_list_writes++;
303  }
304 
305  tile_list_cnt++;
306  tile_idx = 0;
307  // Then memset the frame.
308  memset(output.img_data, 0, output.sz);
309  }
310  continue;
311  }
312 
313  int image_idx, ref_idx, tc, tr;
314  sscanf(line, "%d %d %d %d", &image_idx, &ref_idx, &tc, &tr);
315  if (image_idx >= num_frames) {
316  die("Tile list image_idx out of bounds: %d >= %d.", image_idx,
317  num_frames);
318  }
319  if (ref_idx >= num_references) {
320  die("Tile list ref_idx out of bounds: %d >= %d.", ref_idx,
321  num_references);
322  }
323  frame = frames[image_idx];
324  frame_size = frame_sizes[image_idx];
325 
326  aom_image_t *img = NULL;
327  decode_tile(&codec, frame, frame_size, tr, tc, ref_idx, reference_images,
328  &output, &tile_idx, &output_bit_depth, &img, output_format);
329  if (output_format == YUV1D) {
330  out = img;
331  if (output_bit_depth != 0)
332  aom_shift_img(output_bit_depth, &out, &output_shifted);
333  aom_img_write(out, outfile);
334  }
335  }
336 
337  if (output_format != YUV1D) {
338  // Write out the last tile list.
339  if (tile_list_writes < tile_list_cnt) {
340  out = &output;
341  if (output_bit_depth != 0)
342  aom_shift_img(output_bit_depth, &out, &output_shifted);
343  img_write_to_file(out, outfile, output_format);
344  }
345  }
346 
347  if (output_shifted) aom_img_free(output_shifted);
348  if (output_format != YUV1D) aom_img_free(&output);
349  for (i = 0; i < num_references; i++) aom_img_free(&reference_images[i]);
350  for (int f = 0; f < num_frames; ++f) {
351  free(frames[f]);
352  }
353  free(frame_sizes);
354  free(frames);
355  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
356  aom_video_reader_close(reader);
357  fclose(outfile);
358 
359  return EXIT_SUCCESS;
360 }
aom_image::fmt
aom_img_fmt_t fmt
Definition: aom_image.h:142
aom_codec_decode
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_img_fmt_t
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
aom_image::d_h
unsigned int d_h
Definition: aom_image.h:157
av1_ref_frame::use_external_ref
int use_external_ref
Definition: aom.h:110
aom_codec_ctx
Codec context structure.
Definition: aom_codec.h:204
AV1_SET_TILE_MODE
@ AV1_SET_TILE_MODE
Definition: aomdx.h:180
av1_ref_frame
AV1 specific reference frame data struct.
Definition: aom.h:108
aom_codec_iter_t
const typedef void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
aom_image::stride
int stride[4]
Definition: aom_image.h:174
aom_img_free
void aom_img_free(aom_image_t *img)
Close an image descriptor.
aom_codec_control_
aom_codec_err_t aom_codec_control_(aom_codec_ctx_t *ctx, int ctrl_id,...)
Control algorithm.
aom_image::y_chroma_shift
unsigned int y_chroma_shift
Definition: aom_image.h:165
av1_ref_frame::img
aom_image_t img
Definition: aom.h:111
aom_codec_iface_name
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
AV1_SET_DECODE_TILE_ROW
@ AV1_SET_DECODE_TILE_ROW
Definition: aomdx.h:174
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
AV1_COPY_NEW_FRAME_IMAGE
@ AV1_COPY_NEW_FRAME_IMAGE
Definition: aom.h:66
aom_img_alloc_with_border
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.
aom_img_alloc
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_control
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:423
aom_img_plane_width
int aom_img_plane_width(const aom_image_t *img, int plane)
Get the width of a plane.
aom_codec_err_t
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
aom_decoder.h
Describes the decoder algorithm interface to applications.
aom_image::x_chroma_shift
unsigned int x_chroma_shift
Definition: aom_image.h:164
AV1D_SET_IS_ANNEXB
@ AV1D_SET_IS_ANNEXB
Definition: aomdx.h:200
av1_ref_frame::idx
int idx
Definition: aom.h:109
aom_codec_dec_init
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition: aom_decoder.h:142
AV1D_GET_IMG_FORMAT
@ AV1D_GET_IMG_FORMAT
Definition: aomdx.h:123
AV1D_EXT_TILE_DEBUG
@ AV1D_EXT_TILE_DEBUG
Definition: aomdx.h:197
aom_image::bit_depth
unsigned int bit_depth
Definition: aom_image.h:153
aom_codec_get_frame
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
AV1D_GET_FRAME_SIZE
@ AV1D_GET_FRAME_SIZE
Definition: aomdx.h:112
aom_image::d_w
unsigned int d_w
Definition: aom_image.h:156
aom_image::planes
unsigned char * planes[4]
Definition: aom_image.h:173
aomdx.h
Provides definitions for using AOM or AV1 within the aom Decoder interface.
aom_image::sz
size_t sz
Definition: aom_image.h:175
aom_image
Image Descriptor.
Definition: aom_image.h:141
AV1D_GET_TILE_SIZE
@ AV1D_GET_TILE_SIZE
Definition: aomdx.h:126
aom_img_plane_height
int aom_img_plane_height(const aom_image_t *img, int plane)
Get the height of a plane.
AOM_IMG_FMT_HIGHBITDEPTH
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
AV1_SET_REFERENCE
@ AV1_SET_REFERENCE
Definition: aom.h:60
aom_image::img_data
unsigned char * img_data
Definition: aom_image.h:188