44 #include "common/tools_common.h" 45 #include "common/video_reader.h" 47 #define MAX_EXTERNAL_REFERENCES 128 48 #define AOM_BORDER_IN_PIXELS 288 50 static const char *exec_name;
52 void usage_exit(
void) {
54 "Usage: %s <infile> <outfile> <num_references> <tile_list> <output " 55 "format(optional)>\n",
61 const int output_frame_width = 512;
62 const int output_frame_height = 512;
65 int dst_row_offset,
int dst_col_offset) {
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];
75 (plane > 0) ? dst_row_offset >> dst->
y_chroma_shift : dst_row_offset;
77 (plane > 0) ? dst_col_offset >> dst->
x_chroma_shift : dst_col_offset;
80 dst_buf += roffset * dst_stride + (coffset << shift);
86 for (y = 0; y < h; ++y) {
87 memcpy(dst_buf, src_buf, w);
88 src_buf += src_stride;
89 dst_buf += dst_stride;
95 size_t frame_size,
int tr,
int tc,
int ref_idx,
97 int *tile_idx,
unsigned int *output_bit_depth,
107 ref.
img = reference_images[ref_idx];
109 die_codec(codec,
"Failed to set reference frame.");
113 if (aom_status) die_codec(codec,
"Failed to decode tile.");
117 if (!img) die_codec(codec,
"Failed to get frame.");
127 if (output_format != YUV1D) {
129 unsigned int tile_size = 0;
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;
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;
142 aom_img_copy_tile(img, output, row_offset, col_offset);
147 static void img_write_to_file(
const aom_image_t *img, FILE *file,
149 if (output_format == YUV)
150 aom_img_write(img, file);
151 else if (output_format == NV12)
152 aom_img_write_nv12(img, file);
154 die(
"Invalid output format");
157 int main(
int argc,
char **argv) {
158 FILE *outfile = NULL;
160 AvxVideoReader *reader = NULL;
161 const AvxInterface *decoder = NULL;
162 const AvxVideoInfo *info = NULL;
165 aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
168 size_t frame_size = 0;
169 const unsigned char *frame = NULL;
171 const char *tile_list_file = NULL;
172 int output_format = YUV1D;
175 if (argc < 5) die(
"Invalid number of arguments.");
177 reader = aom_video_reader_open(argv[1]);
178 if (!reader) die(
"Failed to open %s for reading.", argv[1]);
180 if (!(outfile = fopen(argv[2],
"wb")))
181 die(
"Failed to open %s for writing.", argv[2]);
183 num_references = (int)strtol(argv[3], NULL, 0);
184 tile_list_file = argv[4];
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]");
190 info = aom_video_reader_get_info(reader);
192 decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
193 if (!decoder) die(
"Unknown input codec.");
197 die_codec(&codec,
"Failed to initialize decoder.");
200 die(
"Failed to set annex b status");
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);
209 die_codec(&codec,
"Failed to decode frame.");
213 die_codec(&codec,
"Failed to get the image format");
217 die_codec(&codec,
"Failed to get the image frame size");
221 for (j = 0; j < num_references; j++) {
222 unsigned int border = AOM_BORDER_IN_PIXELS;
224 frame_res[0], frame_res[1], 32, 8,
226 die(
"Failed to allocate references.");
232 &reference_images[i]))
233 die_codec(&codec,
"Failed to copy decoded reference frame");
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);
247 FILE *infile = aom_video_reader_get_file(reader);
249 const FileOffset camera_frame_pos = ftello(infile);
251 printf(
"Loading compressed frames into memory.\n");
255 while (aom_video_reader_read_frame(reader)) {
258 if (num_frames < 1) die(
"Input light field has no frames.");
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));
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;
273 printf(
"Read %d frames.\n", num_frames);
275 if (output_format != YUV1D) {
280 output_frame_height, 32))
281 die(
"Failed to allocate output image.");
284 printf(
"Decoding tile list from file.\n");
286 FILE *tile_list_fptr = fopen(tile_list_file,
"r");
287 int tile_list_cnt = 0;
288 int tile_list_writes = 0;
291 unsigned int output_bit_depth = 0;
293 while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
294 if (line[0] ==
'F') {
295 if (output_format != YUV1D) {
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);
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,
319 if (ref_idx >= num_references) {
320 die(
"Tile list ref_idx out of bounds: %d >= %d.", ref_idx,
323 frame = frames[image_idx];
324 frame_size = frame_sizes[image_idx];
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) {
331 if (output_bit_depth != 0)
332 aom_shift_img(output_bit_depth, &out, &output_shifted);
333 aom_img_write(out, outfile);
337 if (output_format != YUV1D) {
339 if (tile_list_writes < tile_list_cnt) {
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);
349 for (i = 0; i < num_references; i++)
aom_img_free(&reference_images[i]);
350 for (
int f = 0; f < num_frames; ++f) {
356 aom_video_reader_close(reader);
unsigned char * img_data
Definition: aom_image.h:188
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
int stride[4]
Definition: aom_image.h:174
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition: aom_image.h:38
unsigned int y_chroma_shift
Definition: aom_image.h:165
Describes the decoder algorithm interface to applications.
Image Descriptor.
Definition: aom_image.h:141
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_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
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.
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
int aom_img_plane_width(const aom_image_t *img, int plane)
Get the width of a plane.
AV1 specific reference frame data struct.
Definition: aom.h:108
int aom_img_plane_height(const aom_image_t *img, int plane)
Get the height of a plane.
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
unsigned int x_chroma_shift
Definition: aom_image.h:164
size_t sz
Definition: aom_image.h:175
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 bit_depth
Definition: aom_image.h:153
unsigned char * planes[4]
Definition: aom_image.h:173
unsigned int d_w
Definition: aom_image.h:156
aom_img_fmt_t fmt
Definition: aom_image.h:142