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
18 // will decode a single lightfield tile. The lf_width and lf_height arguments
19 // are the number of lightfield images in each dimension. The tile to decode
20 // is specified by the tile_u, tile_v, tile_s, tile_t arguments. The tile_u,
21 // tile_v specify the image and tile_s, tile_t specify the tile in the image.
22 // After running the lightfield encoder, run lightfield decoder to decode a
23 // single tile:
24 // examples/lightfield_decoder vase10x10.ivf vase_reference.yuv 10 10 5
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "aom/aom_decoder.h"
31 #include "aom/aomdx.h"
32 #include "common/tools_common.h"
33 #include "common/video_reader.h"
34
35 #define MAX_EXTERNAL_REFERENCES 128
36 #define AOM_BORDER_IN_PIXELS 288
37
38 static const char *exec_name;
39
40 void usage_exit(void) {
41 fprintf(
42 stderr,
43 "Usage: %s <infile> <outfile> <lf_width> <lf_height> <lf_blocksize>\n",
44 exec_name);
45 exit(EXIT_FAILURE);
46 }
47
48 // Tile list entry provided by the application
49 typedef struct {
50 int image_idx;
51 int reference_idx;
52 int tile_col;
53 int tile_row;
54 } TILE_LIST_INFO;
55
56 // M references: 0 - M-1; N images(including references): 0 - N-1;
57 // Note: order the image index incrementally, so that we only go through the
58 // bitstream once to construct the tile list.
59 const int num_tile_lists = 2;
60 const uint16_t tile_count_minus_1 = 9 - 1;
61 const TILE_LIST_INFO tile_list[2][9] = {
62 { { 16, 0, 4, 5 },
63 { 83, 3, 13, 2 },
64 { 57, 2, 2, 6 },
65 { 31, 1, 11, 5 },
66 { 2, 0, 7, 4 },
67 { 77, 3, 9, 9 },
68 { 49, 1, 0, 1 },
69 { 6, 0, 3, 10 },
70 { 63, 2, 5, 8 } },
71 { { 65, 2, 11, 1 },
72 { 42, 1, 3, 7 },
73 { 88, 3, 8, 4 },
74 { 76, 3, 1, 15 },
75 { 1, 0, 2, 2 },
76 { 19, 0, 5, 6 },
77 { 60, 2, 4, 0 },
78 { 25, 1, 11, 15 },
79 { 50, 2, 5, 4 } },
80 };
81
82 int main(int argc, char **argv) {
83 FILE *outfile = NULL;
84 aom_codec_ctx_t codec;
85 AvxVideoReader *reader = NULL;
86 const AvxInterface *decoder = NULL;
87 const AvxVideoInfo *info = NULL;
88 const char *lf_width_arg;
89 const char *lf_height_arg;
90 const char *lf_blocksize_arg;
91 int width, height;
92 int lf_width, lf_height;
93 int lf_blocksize;
94 int u_blocks;
95 int v_blocks;
96 aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
97 size_t frame_size = 0;
98 const unsigned char *frame = NULL;
99 int n, i;
100
101 exec_name = argv[0];
102
103 if (argc != 6) die("Invalid number of arguments.");
104
105 reader = aom_video_reader_open(argv[1]);
106 if (!reader) die("Failed to open %s for reading.", argv[1]);
107
108 if (!(outfile = fopen(argv[2], "wb")))
109 die("Failed to open %s for writing.", argv[2]);
110
111 lf_width_arg = argv[3];
112 lf_height_arg = argv[4];
113 lf_blocksize_arg = argv[5];
114 lf_width = (int)strtol(lf_width_arg, NULL, 0);
115 lf_height = (int)strtol(lf_height_arg, NULL, 0);
116 lf_blocksize = (int)strtol(lf_blocksize_arg, NULL, 0);
117
118 info = aom_video_reader_get_info(reader);
119 width = info->frame_width;
120 height = info->frame_height;
121
122 decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
123 if (!decoder) die("Unknown input codec.");
124 printf("Using %s\n", aom_codec_iface_name(decoder->codec_interface()));
125
126 if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
127 die_codec(&codec, "Failed to initialize decoder.");
128
129 // How many anchor frames we have.
130 u_blocks = (lf_width + lf_blocksize - 1) / lf_blocksize;
131 v_blocks = (lf_height + lf_blocksize - 1) / lf_blocksize;
132
133 int num_references = v_blocks * u_blocks;
134
135 // Allocate memory to store decoded references.
136 aom_img_fmt_t ref_fmt = AOM_IMG_FMT_I420;
137 if (!CONFIG_LOWBITDEPTH) ref_fmt |= AOM_IMG_FMT_HIGHBITDEPTH;
138 // Allocate memory with the border so that it can be used as a reference.
139 for (i = 0; i < num_references; i++) {
140 unsigned int border = AOM_BORDER_IN_PIXELS;
141 if (!aom_img_alloc_with_border(&reference_images[i], ref_fmt, width, height,
142 32, 8, border)) {
143 die("Failed to allocate references.");
144 }
145 }
146
147 // Decode anchor frames.
148 aom_codec_control_(&codec, AV1_SET_TILE_MODE, 0);
149
150 for (i = 0; i < num_references; ++i) {
151 aom_video_reader_read_frame(reader);
152 frame = aom_video_reader_get_frame(reader, &frame_size);
153 if (aom_codec_decode(&codec, frame, frame_size, NULL))
154 die_codec(&codec, "Failed to decode frame.");
155
156 if (aom_codec_control(&codec, AV1_COPY_NEW_FRAME_IMAGE,
157 &reference_images[i]))
158 die_codec(&codec, "Failed to copy decoded reference frame");
159
160 aom_codec_iter_t iter = NULL;
161 aom_image_t *img = NULL;
162 while ((img = aom_codec_get_frame(&codec, &iter)) != NULL) {
163 char name[1024];
164 snprintf(name, sizeof(name), "ref_%d.yuv", i);
165 printf("writing ref image to %s, %d, %d\n", name, img->d_w, img->d_h);
166 FILE *ref_file = fopen(name, "wb");
167 aom_img_write(img, ref_file);
168 fclose(ref_file);
169 }
170 }
171
172 FILE *infile = aom_video_reader_get_file(reader);
173 // Record the offset of the first camera image.
174 const FileOffset camera_frame_pos = ftello(infile);
175
176 // Process 1 tile.
177 for (n = 0; n < num_tile_lists; n++) {
178 for (i = 0; i <= tile_count_minus_1; i++) {
179 int image_idx = tile_list[n][i].image_idx;
180 int ref_idx = tile_list[n][i].reference_idx;
181 int tc = tile_list[n][i].tile_col;
182 int tr = tile_list[n][i].tile_row;
183 int frame_cnt = -1;
184
185 // Seek to the first camera image.
186 fseeko(infile, camera_frame_pos, SEEK_SET);
187
188 // Read out the camera image
189 while (frame_cnt != image_idx) {
190 aom_video_reader_read_frame(reader);
191 frame_cnt++;
192 }
193
194 frame = aom_video_reader_get_frame(reader, &frame_size);
195
196 aom_codec_control_(&codec, AV1_SET_TILE_MODE, 1);
197 aom_codec_control_(&codec, AV1D_EXT_TILE_DEBUG, 1);
198 aom_codec_control_(&codec, AV1_SET_DECODE_TILE_ROW, tr);
199 aom_codec_control_(&codec, AV1_SET_DECODE_TILE_COL, tc);
200
201 av1_ref_frame_t ref;
202 ref.idx = 0;
203 ref.use_external_ref = 1;
204 ref.img = reference_images[ref_idx];
205 if (aom_codec_control(&codec, AV1_SET_REFERENCE, &ref)) {
206 die_codec(&codec, "Failed to set reference frame.");
207 }
208
209 aom_codec_err_t aom_status =
210 aom_codec_decode(&codec, frame, frame_size, NULL);
211 if (aom_status) die_codec(&codec, "Failed to decode tile.");
212
213 aom_codec_iter_t iter = NULL;
214 aom_image_t *img = aom_codec_get_frame(&codec, &iter);
215 aom_img_write(img, outfile);
216 }
217 }
218
219 for (i = 0; i < num_references; i++) aom_img_free(&reference_images[i]);
220 if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
221 aom_video_reader_close(reader);
222 fclose(outfile);
223
224 return EXIT_SUCCESS;
225 }
aom_image::d_h
unsigned int d_h
Definition: aom_image.h:157
aom_codec_get_frame
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
av1_ref_frame::idx
int idx
Definition: aom.h:109
aom_img_fmt_t
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
aom_codec_ctx
Codec context structure.
Definition: aom_codec.h:204
AOM_IMG_FMT_HIGHBITDEPTH
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
aom_decoder.h
Describes the decoder algorithm interface to applications.
aom_image
Image Descriptor.
Definition: aom_image.h:141
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_codec_control_
aom_codec_err_t aom_codec_control_(aom_codec_ctx_t *ctx, int ctrl_id,...)
Control algorithm.
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
AV1_SET_REFERENCE
Definition: aom.h:60
aom_codec_iface_name
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
aom_codec_destroy
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
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.
AV1D_EXT_TILE_DEBUG
Definition: aomdx.h:188
aom_img_free
void aom_img_free(aom_image_t *img)
Close an image descriptor.
aom_codec_control
#define aom_codec_control(ctx, id, data)
aom_codec_control wrapper macro
Definition: aom_codec.h:423
av1_ref_frame
AV1 specific reference frame data struct.
Definition: aom.h:108
AV1_COPY_NEW_FRAME_IMAGE
Definition: aom.h:66
AV1_SET_DECODE_TILE_ROW
Definition: aomdx.h:165
aom_codec_iter_t
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
AV1_SET_TILE_MODE
Definition: aomdx.h:171
av1_ref_frame::use_external_ref
int use_external_ref
Definition: aom.h:110
aom_codec_err_t
aom_codec_err_t
Algorithm return codes.
Definition: aom_codec.h:101
aomdx.h
Provides definitions for using AOM or AV1 within the aom Decoder interface.
av1_ref_frame::img
aom_image_t img
Definition: aom.h:111
AOM_IMG_FMT_I420
Definition: aom_image.h:45
aom_image::d_w
unsigned int d_w
Definition: aom_image.h:156