xref: /aosp_15_r20/external/libaom/examples/lightfield_decoder.c (revision 77c1e3ccc04c968bd2bc212e87364f250e820521)
1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker  * Copyright (c) 2017, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker  *
4*77c1e3ccSAndroid Build Coastguard Worker  * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker  * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker  * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker  */
11*77c1e3ccSAndroid Build Coastguard Worker 
12*77c1e3ccSAndroid Build Coastguard Worker // Lightfield Decoder
13*77c1e3ccSAndroid Build Coastguard Worker // ==================
14*77c1e3ccSAndroid Build Coastguard Worker //
15*77c1e3ccSAndroid Build Coastguard Worker // This is an example of a simple lightfield decoder. It builds upon the
16*77c1e3ccSAndroid Build Coastguard Worker // simple_decoder.c example.  It takes an input file containing the compressed
17*77c1e3ccSAndroid Build Coastguard Worker // data (in ivf format), treating it as a lightfield instead of a video; and a
18*77c1e3ccSAndroid Build Coastguard Worker // text file with a list of tiles to decode. There is an optional parameter
19*77c1e3ccSAndroid Build Coastguard Worker // allowing to choose the output format, and the supported formats are
20*77c1e3ccSAndroid Build Coastguard Worker // YUV1D(default), YUV, and NV12.
21*77c1e3ccSAndroid Build Coastguard Worker // After running the lightfield encoder, run lightfield decoder to decode a
22*77c1e3ccSAndroid Build Coastguard Worker // batch of tiles:
23*77c1e3ccSAndroid Build Coastguard Worker // examples/lightfield_decoder vase10x10.ivf vase_reference.yuv 4 tile_list.txt
24*77c1e3ccSAndroid Build Coastguard Worker // 0(optional)
25*77c1e3ccSAndroid Build Coastguard Worker // The tile_list.txt is expected to be of the form:
26*77c1e3ccSAndroid Build Coastguard Worker // Frame <frame_index0>
27*77c1e3ccSAndroid Build Coastguard Worker // <image_index0> <anchor_index0> <tile_col0> <tile_row0>
28*77c1e3ccSAndroid Build Coastguard Worker // <image_index1> <anchor_index1> <tile_col1> <tile_row1>
29*77c1e3ccSAndroid Build Coastguard Worker // ...
30*77c1e3ccSAndroid Build Coastguard Worker // Frame <frame_index1)
31*77c1e3ccSAndroid Build Coastguard Worker // ...
32*77c1e3ccSAndroid Build Coastguard Worker //
33*77c1e3ccSAndroid Build Coastguard Worker // The "Frame" markers indicate a new render frame and thus a new tile list
34*77c1e3ccSAndroid Build Coastguard Worker // will be started and the old one flushed.  The image_indexN, anchor_indexN,
35*77c1e3ccSAndroid Build Coastguard Worker // tile_colN, and tile_rowN identify an individual tile to be decoded and
36*77c1e3ccSAndroid Build Coastguard Worker // to use anchor_indexN anchor image for MCP.
37*77c1e3ccSAndroid Build Coastguard Worker 
38*77c1e3ccSAndroid Build Coastguard Worker #include <stdio.h>
39*77c1e3ccSAndroid Build Coastguard Worker #include <stdlib.h>
40*77c1e3ccSAndroid Build Coastguard Worker #include <string.h>
41*77c1e3ccSAndroid Build Coastguard Worker 
42*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aom_decoder.h"
43*77c1e3ccSAndroid Build Coastguard Worker #include "aom/aomdx.h"
44*77c1e3ccSAndroid Build Coastguard Worker #include "aom_scale/yv12config.h"
45*77c1e3ccSAndroid Build Coastguard Worker #include "av1/common/enums.h"
46*77c1e3ccSAndroid Build Coastguard Worker #include "common/tools_common.h"
47*77c1e3ccSAndroid Build Coastguard Worker #include "common/video_reader.h"
48*77c1e3ccSAndroid Build Coastguard Worker 
49*77c1e3ccSAndroid Build Coastguard Worker enum {
50*77c1e3ccSAndroid Build Coastguard Worker   YUV1D,  // 1D tile output for conformance test.
51*77c1e3ccSAndroid Build Coastguard Worker   YUV,    // Tile output in YUV format.
52*77c1e3ccSAndroid Build Coastguard Worker   NV12,   // Tile output in NV12 format.
53*77c1e3ccSAndroid Build Coastguard Worker } UENUM1BYTE(OUTPUT_FORMAT);
54*77c1e3ccSAndroid Build Coastguard Worker 
55*77c1e3ccSAndroid Build Coastguard Worker static const char *exec_name;
56*77c1e3ccSAndroid Build Coastguard Worker 
usage_exit(void)57*77c1e3ccSAndroid Build Coastguard Worker void usage_exit(void) {
58*77c1e3ccSAndroid Build Coastguard Worker   fprintf(stderr,
59*77c1e3ccSAndroid Build Coastguard Worker           "Usage: %s <infile> <outfile> <num_references> <tile_list> <output "
60*77c1e3ccSAndroid Build Coastguard Worker           "format(optional)>\n",
61*77c1e3ccSAndroid Build Coastguard Worker           exec_name);
62*77c1e3ccSAndroid Build Coastguard Worker   exit(EXIT_FAILURE);
63*77c1e3ccSAndroid Build Coastguard Worker }
64*77c1e3ccSAndroid Build Coastguard Worker 
65*77c1e3ccSAndroid Build Coastguard Worker // Output frame size
66*77c1e3ccSAndroid Build Coastguard Worker static const int output_frame_width = 512;
67*77c1e3ccSAndroid Build Coastguard Worker static const int output_frame_height = 512;
68*77c1e3ccSAndroid Build Coastguard Worker 
aom_img_copy_tile(const aom_image_t * src,const aom_image_t * dst,int dst_row_offset,int dst_col_offset)69*77c1e3ccSAndroid Build Coastguard Worker static void aom_img_copy_tile(const aom_image_t *src, const aom_image_t *dst,
70*77c1e3ccSAndroid Build Coastguard Worker                               int dst_row_offset, int dst_col_offset) {
71*77c1e3ccSAndroid Build Coastguard Worker   const int shift = (src->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 1 : 0;
72*77c1e3ccSAndroid Build Coastguard Worker   int plane;
73*77c1e3ccSAndroid Build Coastguard Worker 
74*77c1e3ccSAndroid Build Coastguard Worker   for (plane = 0; plane < 3; ++plane) {
75*77c1e3ccSAndroid Build Coastguard Worker     const unsigned char *src_buf = src->planes[plane];
76*77c1e3ccSAndroid Build Coastguard Worker     const int src_stride = src->stride[plane];
77*77c1e3ccSAndroid Build Coastguard Worker     unsigned char *dst_buf = dst->planes[plane];
78*77c1e3ccSAndroid Build Coastguard Worker     const int dst_stride = dst->stride[plane];
79*77c1e3ccSAndroid Build Coastguard Worker     const int roffset =
80*77c1e3ccSAndroid Build Coastguard Worker         (plane > 0) ? dst_row_offset >> dst->y_chroma_shift : dst_row_offset;
81*77c1e3ccSAndroid Build Coastguard Worker     const int coffset =
82*77c1e3ccSAndroid Build Coastguard Worker         (plane > 0) ? dst_col_offset >> dst->x_chroma_shift : dst_col_offset;
83*77c1e3ccSAndroid Build Coastguard Worker 
84*77c1e3ccSAndroid Build Coastguard Worker     // col offset needs to be adjusted for HBD.
85*77c1e3ccSAndroid Build Coastguard Worker     dst_buf += roffset * dst_stride + (coffset << shift);
86*77c1e3ccSAndroid Build Coastguard Worker 
87*77c1e3ccSAndroid Build Coastguard Worker     const int w = (aom_img_plane_width(src, plane) << shift);
88*77c1e3ccSAndroid Build Coastguard Worker     const int h = aom_img_plane_height(src, plane);
89*77c1e3ccSAndroid Build Coastguard Worker     int y;
90*77c1e3ccSAndroid Build Coastguard Worker 
91*77c1e3ccSAndroid Build Coastguard Worker     for (y = 0; y < h; ++y) {
92*77c1e3ccSAndroid Build Coastguard Worker       memcpy(dst_buf, src_buf, w);
93*77c1e3ccSAndroid Build Coastguard Worker       src_buf += src_stride;
94*77c1e3ccSAndroid Build Coastguard Worker       dst_buf += dst_stride;
95*77c1e3ccSAndroid Build Coastguard Worker     }
96*77c1e3ccSAndroid Build Coastguard Worker   }
97*77c1e3ccSAndroid Build Coastguard Worker }
98*77c1e3ccSAndroid Build Coastguard Worker 
decode_tile(aom_codec_ctx_t * codec,const unsigned char * frame,size_t frame_size,int tr,int tc,int ref_idx,aom_image_t * reference_images,aom_image_t * output,int * tile_idx,unsigned int * output_bit_depth,aom_image_t ** img_ptr,int output_format)99*77c1e3ccSAndroid Build Coastguard Worker static void decode_tile(aom_codec_ctx_t *codec, const unsigned char *frame,
100*77c1e3ccSAndroid Build Coastguard Worker                         size_t frame_size, int tr, int tc, int ref_idx,
101*77c1e3ccSAndroid Build Coastguard Worker                         aom_image_t *reference_images, aom_image_t *output,
102*77c1e3ccSAndroid Build Coastguard Worker                         int *tile_idx, unsigned int *output_bit_depth,
103*77c1e3ccSAndroid Build Coastguard Worker                         aom_image_t **img_ptr, int output_format) {
104*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1_SET_TILE_MODE, 1);
105*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1D_EXT_TILE_DEBUG, 1);
106*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1_SET_DECODE_TILE_ROW, tr);
107*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1_SET_DECODE_TILE_COL, tc);
108*77c1e3ccSAndroid Build Coastguard Worker 
109*77c1e3ccSAndroid Build Coastguard Worker   av1_ref_frame_t ref;
110*77c1e3ccSAndroid Build Coastguard Worker   ref.idx = 0;
111*77c1e3ccSAndroid Build Coastguard Worker   ref.use_external_ref = 1;
112*77c1e3ccSAndroid Build Coastguard Worker   ref.img = reference_images[ref_idx];
113*77c1e3ccSAndroid Build Coastguard Worker   if (AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1_SET_REFERENCE, &ref)) {
114*77c1e3ccSAndroid Build Coastguard Worker     die_codec(codec, "Failed to set reference frame.");
115*77c1e3ccSAndroid Build Coastguard Worker   }
116*77c1e3ccSAndroid Build Coastguard Worker 
117*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_err_t aom_status = aom_codec_decode(codec, frame, frame_size, NULL);
118*77c1e3ccSAndroid Build Coastguard Worker   if (aom_status) die_codec(codec, "Failed to decode tile.");
119*77c1e3ccSAndroid Build Coastguard Worker 
120*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_iter_t iter = NULL;
121*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t *img = aom_codec_get_frame(codec, &iter);
122*77c1e3ccSAndroid Build Coastguard Worker   if (!img) die_codec(codec, "Failed to get frame.");
123*77c1e3ccSAndroid Build Coastguard Worker   *img_ptr = img;
124*77c1e3ccSAndroid Build Coastguard Worker 
125*77c1e3ccSAndroid Build Coastguard Worker   // aom_img_alloc() sets bit_depth as follows:
126*77c1e3ccSAndroid Build Coastguard Worker   // output->bit_depth = (fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 16 : 8;
127*77c1e3ccSAndroid Build Coastguard Worker   // Use img->bit_depth(read from bitstream), so that aom_shift_img()
128*77c1e3ccSAndroid Build Coastguard Worker   // works as expected.
129*77c1e3ccSAndroid Build Coastguard Worker   output->bit_depth = img->bit_depth;
130*77c1e3ccSAndroid Build Coastguard Worker   *output_bit_depth = img->bit_depth;
131*77c1e3ccSAndroid Build Coastguard Worker 
132*77c1e3ccSAndroid Build Coastguard Worker   if (output_format != YUV1D) {
133*77c1e3ccSAndroid Build Coastguard Worker     // read out the tile size.
134*77c1e3ccSAndroid Build Coastguard Worker     unsigned int tile_size = 0;
135*77c1e3ccSAndroid Build Coastguard Worker     if (AOM_CODEC_CONTROL_TYPECHECKED(codec, AV1D_GET_TILE_SIZE, &tile_size))
136*77c1e3ccSAndroid Build Coastguard Worker       die_codec(codec, "Failed to get the tile size");
137*77c1e3ccSAndroid Build Coastguard Worker     const unsigned int tile_width = tile_size >> 16;
138*77c1e3ccSAndroid Build Coastguard Worker     const unsigned int tile_height = tile_size & 65535;
139*77c1e3ccSAndroid Build Coastguard Worker     const uint32_t output_frame_width_in_tiles =
140*77c1e3ccSAndroid Build Coastguard Worker         output_frame_width / tile_width;
141*77c1e3ccSAndroid Build Coastguard Worker 
142*77c1e3ccSAndroid Build Coastguard Worker     // Copy the tile to the output frame.
143*77c1e3ccSAndroid Build Coastguard Worker     const int row_offset =
144*77c1e3ccSAndroid Build Coastguard Worker         (*tile_idx / output_frame_width_in_tiles) * tile_height;
145*77c1e3ccSAndroid Build Coastguard Worker     const int col_offset =
146*77c1e3ccSAndroid Build Coastguard Worker         (*tile_idx % output_frame_width_in_tiles) * tile_width;
147*77c1e3ccSAndroid Build Coastguard Worker 
148*77c1e3ccSAndroid Build Coastguard Worker     aom_img_copy_tile(img, output, row_offset, col_offset);
149*77c1e3ccSAndroid Build Coastguard Worker     (*tile_idx)++;
150*77c1e3ccSAndroid Build Coastguard Worker   }
151*77c1e3ccSAndroid Build Coastguard Worker }
152*77c1e3ccSAndroid Build Coastguard Worker 
img_write_to_file(const aom_image_t * img,FILE * file,int output_format)153*77c1e3ccSAndroid Build Coastguard Worker static void img_write_to_file(const aom_image_t *img, FILE *file,
154*77c1e3ccSAndroid Build Coastguard Worker                               int output_format) {
155*77c1e3ccSAndroid Build Coastguard Worker   if (output_format == YUV)
156*77c1e3ccSAndroid Build Coastguard Worker     aom_img_write(img, file);
157*77c1e3ccSAndroid Build Coastguard Worker   else if (output_format == NV12)
158*77c1e3ccSAndroid Build Coastguard Worker     aom_img_write_nv12(img, file);
159*77c1e3ccSAndroid Build Coastguard Worker   else
160*77c1e3ccSAndroid Build Coastguard Worker     die("Invalid output format");
161*77c1e3ccSAndroid Build Coastguard Worker }
162*77c1e3ccSAndroid Build Coastguard Worker 
main(int argc,char ** argv)163*77c1e3ccSAndroid Build Coastguard Worker int main(int argc, char **argv) {
164*77c1e3ccSAndroid Build Coastguard Worker   FILE *outfile = NULL;
165*77c1e3ccSAndroid Build Coastguard Worker   AvxVideoReader *reader = NULL;
166*77c1e3ccSAndroid Build Coastguard Worker   const AvxVideoInfo *info = NULL;
167*77c1e3ccSAndroid Build Coastguard Worker   int num_references;
168*77c1e3ccSAndroid Build Coastguard Worker   aom_img_fmt_t ref_fmt = 0;
169*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
170*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t output;
171*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t *output_shifted = NULL;
172*77c1e3ccSAndroid Build Coastguard Worker   size_t frame_size = 0;
173*77c1e3ccSAndroid Build Coastguard Worker   const unsigned char *frame = NULL;
174*77c1e3ccSAndroid Build Coastguard Worker   int i, j;
175*77c1e3ccSAndroid Build Coastguard Worker   const char *tile_list_file = NULL;
176*77c1e3ccSAndroid Build Coastguard Worker   int output_format = YUV1D;
177*77c1e3ccSAndroid Build Coastguard Worker   exec_name = argv[0];
178*77c1e3ccSAndroid Build Coastguard Worker 
179*77c1e3ccSAndroid Build Coastguard Worker   if (argc < 5) die("Invalid number of arguments.");
180*77c1e3ccSAndroid Build Coastguard Worker 
181*77c1e3ccSAndroid Build Coastguard Worker   reader = aom_video_reader_open(argv[1]);
182*77c1e3ccSAndroid Build Coastguard Worker   if (!reader) die("Failed to open %s for reading.", argv[1]);
183*77c1e3ccSAndroid Build Coastguard Worker 
184*77c1e3ccSAndroid Build Coastguard Worker   if (!(outfile = fopen(argv[2], "wb")))
185*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to open %s for writing.", argv[2]);
186*77c1e3ccSAndroid Build Coastguard Worker 
187*77c1e3ccSAndroid Build Coastguard Worker   num_references = (int)strtol(argv[3], NULL, 0);
188*77c1e3ccSAndroid Build Coastguard Worker   tile_list_file = argv[4];
189*77c1e3ccSAndroid Build Coastguard Worker 
190*77c1e3ccSAndroid Build Coastguard Worker   if (argc > 5) output_format = (int)strtol(argv[5], NULL, 0);
191*77c1e3ccSAndroid Build Coastguard Worker   if (output_format < YUV1D || output_format > NV12)
192*77c1e3ccSAndroid Build Coastguard Worker     die("Output format out of range [0, 2]");
193*77c1e3ccSAndroid Build Coastguard Worker 
194*77c1e3ccSAndroid Build Coastguard Worker   info = aom_video_reader_get_info(reader);
195*77c1e3ccSAndroid Build Coastguard Worker 
196*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_iface_t *decoder;
197*77c1e3ccSAndroid Build Coastguard Worker   if (info->codec_fourcc == LST_FOURCC)
198*77c1e3ccSAndroid Build Coastguard Worker     decoder = get_aom_decoder_by_fourcc(AV1_FOURCC);
199*77c1e3ccSAndroid Build Coastguard Worker   else
200*77c1e3ccSAndroid Build Coastguard Worker     die("Unknown input codec.");
201*77c1e3ccSAndroid Build Coastguard Worker   printf("Using %s\n", aom_codec_iface_name(decoder));
202*77c1e3ccSAndroid Build Coastguard Worker 
203*77c1e3ccSAndroid Build Coastguard Worker   aom_codec_ctx_t codec;
204*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_dec_init(&codec, decoder, NULL, 0))
205*77c1e3ccSAndroid Build Coastguard Worker     die_codec(&codec, "Failed to initialize decoder.");
206*77c1e3ccSAndroid Build Coastguard Worker 
207*77c1e3ccSAndroid Build Coastguard Worker   if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_SET_IS_ANNEXB,
208*77c1e3ccSAndroid Build Coastguard Worker                                     info->is_annexb)) {
209*77c1e3ccSAndroid Build Coastguard Worker     die("Failed to set annex b status");
210*77c1e3ccSAndroid Build Coastguard Worker   }
211*77c1e3ccSAndroid Build Coastguard Worker 
212*77c1e3ccSAndroid Build Coastguard Worker   // Decode anchor frames.
213*77c1e3ccSAndroid Build Coastguard Worker   AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1_SET_TILE_MODE, 0);
214*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < num_references; ++i) {
215*77c1e3ccSAndroid Build Coastguard Worker     aom_video_reader_read_frame(reader);
216*77c1e3ccSAndroid Build Coastguard Worker     frame = aom_video_reader_get_frame(reader, &frame_size);
217*77c1e3ccSAndroid Build Coastguard Worker     if (aom_codec_decode(&codec, frame, frame_size, NULL))
218*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&codec, "Failed to decode frame.");
219*77c1e3ccSAndroid Build Coastguard Worker 
220*77c1e3ccSAndroid Build Coastguard Worker     if (i == 0) {
221*77c1e3ccSAndroid Build Coastguard Worker       if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_GET_IMG_FORMAT, &ref_fmt))
222*77c1e3ccSAndroid Build Coastguard Worker         die_codec(&codec, "Failed to get the image format");
223*77c1e3ccSAndroid Build Coastguard Worker 
224*77c1e3ccSAndroid Build Coastguard Worker       int frame_res[2];
225*77c1e3ccSAndroid Build Coastguard Worker       if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1D_GET_FRAME_SIZE, frame_res))
226*77c1e3ccSAndroid Build Coastguard Worker         die_codec(&codec, "Failed to get the image frame size");
227*77c1e3ccSAndroid Build Coastguard Worker 
228*77c1e3ccSAndroid Build Coastguard Worker       // Allocate memory to store decoded references. Allocate memory with the
229*77c1e3ccSAndroid Build Coastguard Worker       // border so that it can be used as a reference.
230*77c1e3ccSAndroid Build Coastguard Worker       for (j = 0; j < num_references; j++) {
231*77c1e3ccSAndroid Build Coastguard Worker         unsigned int border = AOM_DEC_BORDER_IN_PIXELS;
232*77c1e3ccSAndroid Build Coastguard Worker         if (!aom_img_alloc_with_border(&reference_images[j], ref_fmt,
233*77c1e3ccSAndroid Build Coastguard Worker                                        frame_res[0], frame_res[1], 32, 8,
234*77c1e3ccSAndroid Build Coastguard Worker                                        border)) {
235*77c1e3ccSAndroid Build Coastguard Worker           die("Failed to allocate references.");
236*77c1e3ccSAndroid Build Coastguard Worker         }
237*77c1e3ccSAndroid Build Coastguard Worker       }
238*77c1e3ccSAndroid Build Coastguard Worker     }
239*77c1e3ccSAndroid Build Coastguard Worker 
240*77c1e3ccSAndroid Build Coastguard Worker     if (AOM_CODEC_CONTROL_TYPECHECKED(&codec, AV1_COPY_NEW_FRAME_IMAGE,
241*77c1e3ccSAndroid Build Coastguard Worker                                       &reference_images[i]))
242*77c1e3ccSAndroid Build Coastguard Worker       die_codec(&codec, "Failed to copy decoded reference frame");
243*77c1e3ccSAndroid Build Coastguard Worker 
244*77c1e3ccSAndroid Build Coastguard Worker     aom_codec_iter_t iter = NULL;
245*77c1e3ccSAndroid Build Coastguard Worker     aom_image_t *img = NULL;
246*77c1e3ccSAndroid Build Coastguard Worker     while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
247*77c1e3ccSAndroid Build Coastguard Worker       char name[1024];
248*77c1e3ccSAndroid Build Coastguard Worker       snprintf(name, sizeof(name), "ref_%d.yuv", i);
249*77c1e3ccSAndroid Build Coastguard Worker       printf("writing ref image to %s, %u, %u\n", name, img->d_w, img->d_h);
250*77c1e3ccSAndroid Build Coastguard Worker       FILE *ref_file = fopen(name, "wb");
251*77c1e3ccSAndroid Build Coastguard Worker       aom_img_write(img, ref_file);
252*77c1e3ccSAndroid Build Coastguard Worker       fclose(ref_file);
253*77c1e3ccSAndroid Build Coastguard Worker     }
254*77c1e3ccSAndroid Build Coastguard Worker   }
255*77c1e3ccSAndroid Build Coastguard Worker 
256*77c1e3ccSAndroid Build Coastguard Worker   FILE *infile = aom_video_reader_get_file(reader);
257*77c1e3ccSAndroid Build Coastguard Worker   // Record the offset of the first camera image.
258*77c1e3ccSAndroid Build Coastguard Worker   const FileOffset camera_frame_pos = ftello(infile);
259*77c1e3ccSAndroid Build Coastguard Worker 
260*77c1e3ccSAndroid Build Coastguard Worker   printf("Loading compressed frames into memory.\n");
261*77c1e3ccSAndroid Build Coastguard Worker 
262*77c1e3ccSAndroid Build Coastguard Worker   // Count the frames in the lightfield.
263*77c1e3ccSAndroid Build Coastguard Worker   int num_frames = 0;
264*77c1e3ccSAndroid Build Coastguard Worker   while (aom_video_reader_read_frame(reader)) {
265*77c1e3ccSAndroid Build Coastguard Worker     ++num_frames;
266*77c1e3ccSAndroid Build Coastguard Worker   }
267*77c1e3ccSAndroid Build Coastguard Worker   if (num_frames < 1) die("Input light field has no frames.");
268*77c1e3ccSAndroid Build Coastguard Worker 
269*77c1e3ccSAndroid Build Coastguard Worker   // Read all of the lightfield frames into memory.
270*77c1e3ccSAndroid Build Coastguard Worker   unsigned char **frames =
271*77c1e3ccSAndroid Build Coastguard Worker       (unsigned char **)malloc(num_frames * sizeof(unsigned char *));
272*77c1e3ccSAndroid Build Coastguard Worker   size_t *frame_sizes = (size_t *)malloc(num_frames * sizeof(size_t));
273*77c1e3ccSAndroid Build Coastguard Worker   if (!(frames && frame_sizes)) die("Failed to allocate frame data.");
274*77c1e3ccSAndroid Build Coastguard Worker   // Seek to the first camera image.
275*77c1e3ccSAndroid Build Coastguard Worker   fseeko(infile, camera_frame_pos, SEEK_SET);
276*77c1e3ccSAndroid Build Coastguard Worker   for (int f = 0; f < num_frames; ++f) {
277*77c1e3ccSAndroid Build Coastguard Worker     aom_video_reader_read_frame(reader);
278*77c1e3ccSAndroid Build Coastguard Worker     frame = aom_video_reader_get_frame(reader, &frame_size);
279*77c1e3ccSAndroid Build Coastguard Worker     frames[f] = (unsigned char *)malloc(frame_size * sizeof(unsigned char));
280*77c1e3ccSAndroid Build Coastguard Worker     if (!frames[f]) die("Failed to allocate frame data.");
281*77c1e3ccSAndroid Build Coastguard Worker     memcpy(frames[f], frame, frame_size);
282*77c1e3ccSAndroid Build Coastguard Worker     frame_sizes[f] = frame_size;
283*77c1e3ccSAndroid Build Coastguard Worker   }
284*77c1e3ccSAndroid Build Coastguard Worker   printf("Read %d frames.\n", num_frames);
285*77c1e3ccSAndroid Build Coastguard Worker 
286*77c1e3ccSAndroid Build Coastguard Worker   if (output_format != YUV1D) {
287*77c1e3ccSAndroid Build Coastguard Worker     // Allocate the output frame.
288*77c1e3ccSAndroid Build Coastguard Worker     aom_img_fmt_t out_fmt = ref_fmt;
289*77c1e3ccSAndroid Build Coastguard Worker     if (FORCE_HIGHBITDEPTH_DECODING) out_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
290*77c1e3ccSAndroid Build Coastguard Worker     if (!aom_img_alloc(&output, out_fmt, output_frame_width,
291*77c1e3ccSAndroid Build Coastguard Worker                        output_frame_height, 32))
292*77c1e3ccSAndroid Build Coastguard Worker       die("Failed to allocate output image.");
293*77c1e3ccSAndroid Build Coastguard Worker   }
294*77c1e3ccSAndroid Build Coastguard Worker 
295*77c1e3ccSAndroid Build Coastguard Worker   printf("Decoding tile list from file.\n");
296*77c1e3ccSAndroid Build Coastguard Worker   char line[1024];
297*77c1e3ccSAndroid Build Coastguard Worker   FILE *tile_list_fptr = fopen(tile_list_file, "r");
298*77c1e3ccSAndroid Build Coastguard Worker   if (!tile_list_fptr) die_codec(&codec, "Failed to open tile list file.");
299*77c1e3ccSAndroid Build Coastguard Worker   int tile_list_cnt = 0;
300*77c1e3ccSAndroid Build Coastguard Worker   int tile_list_writes = 0;
301*77c1e3ccSAndroid Build Coastguard Worker   int tile_idx = 0;
302*77c1e3ccSAndroid Build Coastguard Worker   aom_image_t *out = NULL;
303*77c1e3ccSAndroid Build Coastguard Worker   unsigned int output_bit_depth = 0;
304*77c1e3ccSAndroid Build Coastguard Worker 
305*77c1e3ccSAndroid Build Coastguard Worker   while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
306*77c1e3ccSAndroid Build Coastguard Worker     if (line[0] == 'F') {
307*77c1e3ccSAndroid Build Coastguard Worker       if (output_format != YUV1D) {
308*77c1e3ccSAndroid Build Coastguard Worker         // Write out the tile list.
309*77c1e3ccSAndroid Build Coastguard Worker         if (tile_list_cnt) {
310*77c1e3ccSAndroid Build Coastguard Worker           out = &output;
311*77c1e3ccSAndroid Build Coastguard Worker           if (output_bit_depth != 0) {
312*77c1e3ccSAndroid Build Coastguard Worker             if (!aom_shift_img(output_bit_depth, &out, &output_shifted)) {
313*77c1e3ccSAndroid Build Coastguard Worker               die("Error allocating image");
314*77c1e3ccSAndroid Build Coastguard Worker             }
315*77c1e3ccSAndroid Build Coastguard Worker           }
316*77c1e3ccSAndroid Build Coastguard Worker           img_write_to_file(out, outfile, output_format);
317*77c1e3ccSAndroid Build Coastguard Worker           tile_list_writes++;
318*77c1e3ccSAndroid Build Coastguard Worker         }
319*77c1e3ccSAndroid Build Coastguard Worker 
320*77c1e3ccSAndroid Build Coastguard Worker         tile_list_cnt++;
321*77c1e3ccSAndroid Build Coastguard Worker         tile_idx = 0;
322*77c1e3ccSAndroid Build Coastguard Worker         // Then memset the frame.
323*77c1e3ccSAndroid Build Coastguard Worker         memset(output.img_data, 0, output.sz);
324*77c1e3ccSAndroid Build Coastguard Worker       }
325*77c1e3ccSAndroid Build Coastguard Worker       continue;
326*77c1e3ccSAndroid Build Coastguard Worker     }
327*77c1e3ccSAndroid Build Coastguard Worker 
328*77c1e3ccSAndroid Build Coastguard Worker     int image_idx, ref_idx, tc, tr;
329*77c1e3ccSAndroid Build Coastguard Worker     sscanf(line, "%d %d %d %d", &image_idx, &ref_idx, &tc, &tr);
330*77c1e3ccSAndroid Build Coastguard Worker     if (image_idx >= num_frames) {
331*77c1e3ccSAndroid Build Coastguard Worker       die("Tile list image_idx out of bounds: %d >= %d.", image_idx,
332*77c1e3ccSAndroid Build Coastguard Worker           num_frames);
333*77c1e3ccSAndroid Build Coastguard Worker     }
334*77c1e3ccSAndroid Build Coastguard Worker     if (ref_idx >= num_references) {
335*77c1e3ccSAndroid Build Coastguard Worker       die("Tile list ref_idx out of bounds: %d >= %d.", ref_idx,
336*77c1e3ccSAndroid Build Coastguard Worker           num_references);
337*77c1e3ccSAndroid Build Coastguard Worker     }
338*77c1e3ccSAndroid Build Coastguard Worker     frame = frames[image_idx];
339*77c1e3ccSAndroid Build Coastguard Worker     frame_size = frame_sizes[image_idx];
340*77c1e3ccSAndroid Build Coastguard Worker 
341*77c1e3ccSAndroid Build Coastguard Worker     aom_image_t *img = NULL;
342*77c1e3ccSAndroid Build Coastguard Worker     decode_tile(&codec, frame, frame_size, tr, tc, ref_idx, reference_images,
343*77c1e3ccSAndroid Build Coastguard Worker                 &output, &tile_idx, &output_bit_depth, &img, output_format);
344*77c1e3ccSAndroid Build Coastguard Worker     if (output_format == YUV1D) {
345*77c1e3ccSAndroid Build Coastguard Worker       out = img;
346*77c1e3ccSAndroid Build Coastguard Worker       if (output_bit_depth != 0) {
347*77c1e3ccSAndroid Build Coastguard Worker         if (!aom_shift_img(output_bit_depth, &out, &output_shifted)) {
348*77c1e3ccSAndroid Build Coastguard Worker           die("Error allocating image");
349*77c1e3ccSAndroid Build Coastguard Worker         }
350*77c1e3ccSAndroid Build Coastguard Worker       }
351*77c1e3ccSAndroid Build Coastguard Worker       aom_img_write(out, outfile);
352*77c1e3ccSAndroid Build Coastguard Worker     }
353*77c1e3ccSAndroid Build Coastguard Worker   }
354*77c1e3ccSAndroid Build Coastguard Worker 
355*77c1e3ccSAndroid Build Coastguard Worker   if (output_format != YUV1D) {
356*77c1e3ccSAndroid Build Coastguard Worker     // Write out the last tile list.
357*77c1e3ccSAndroid Build Coastguard Worker     if (tile_list_writes < tile_list_cnt) {
358*77c1e3ccSAndroid Build Coastguard Worker       out = &output;
359*77c1e3ccSAndroid Build Coastguard Worker       if (output_bit_depth != 0) {
360*77c1e3ccSAndroid Build Coastguard Worker         if (!aom_shift_img(output_bit_depth, &out, &output_shifted)) {
361*77c1e3ccSAndroid Build Coastguard Worker           die("Error allocating image");
362*77c1e3ccSAndroid Build Coastguard Worker         }
363*77c1e3ccSAndroid Build Coastguard Worker       }
364*77c1e3ccSAndroid Build Coastguard Worker       img_write_to_file(out, outfile, output_format);
365*77c1e3ccSAndroid Build Coastguard Worker     }
366*77c1e3ccSAndroid Build Coastguard Worker   }
367*77c1e3ccSAndroid Build Coastguard Worker 
368*77c1e3ccSAndroid Build Coastguard Worker   if (output_shifted) aom_img_free(output_shifted);
369*77c1e3ccSAndroid Build Coastguard Worker   if (output_format != YUV1D) aom_img_free(&output);
370*77c1e3ccSAndroid Build Coastguard Worker   for (i = 0; i < num_references; i++) aom_img_free(&reference_images[i]);
371*77c1e3ccSAndroid Build Coastguard Worker   for (int f = 0; f < num_frames; ++f) {
372*77c1e3ccSAndroid Build Coastguard Worker     free(frames[f]);
373*77c1e3ccSAndroid Build Coastguard Worker   }
374*77c1e3ccSAndroid Build Coastguard Worker   free(frame_sizes);
375*77c1e3ccSAndroid Build Coastguard Worker   free(frames);
376*77c1e3ccSAndroid Build Coastguard Worker   if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
377*77c1e3ccSAndroid Build Coastguard Worker   aom_video_reader_close(reader);
378*77c1e3ccSAndroid Build Coastguard Worker   fclose(outfile);
379*77c1e3ccSAndroid Build Coastguard Worker 
380*77c1e3ccSAndroid Build Coastguard Worker   return EXIT_SUCCESS;
381*77c1e3ccSAndroid Build Coastguard Worker }
382