44#include "aom_scale/yv12config.h"
46#include "common/tools_common.h"
47#include "common/video_reader.h"
53} UENUM1BYTE(OUTPUT_FORMAT);
55static const char *exec_name;
57void usage_exit(
void) {
59 "Usage: %s <infile> <outfile> <num_references> <tile_list> <output "
60 "format(optional)>\n",
66static const int output_frame_width = 512;
67static const int output_frame_height = 512;
70 int dst_row_offset,
int dst_col_offset) {
74 for (plane = 0; plane < 3; ++plane) {
75 const unsigned char *src_buf = src->
planes[plane];
76 const int src_stride = src->
stride[plane];
77 unsigned char *dst_buf = dst->
planes[plane];
78 const int dst_stride = dst->
stride[plane];
80 (plane > 0) ? dst_row_offset >> dst->
y_chroma_shift : dst_row_offset;
82 (plane > 0) ? dst_col_offset >> dst->
x_chroma_shift : dst_col_offset;
85 dst_buf += roffset * dst_stride + (coffset << shift);
91 for (y = 0; y < h; ++y) {
92 memcpy(dst_buf, src_buf, w);
93 src_buf += src_stride;
94 dst_buf += dst_stride;
99static void decode_tile(
aom_codec_ctx_t *codec,
const unsigned char *frame,
100 size_t frame_size,
int tr,
int tc,
int ref_idx,
102 int *tile_idx,
unsigned int *output_bit_depth,
112 ref.
img = reference_images[ref_idx];
114 die_codec(codec,
"Failed to set reference frame.");
118 if (aom_status) die_codec(codec,
"Failed to decode tile.");
122 if (!img) die_codec(codec,
"Failed to get frame.");
132 if (output_format != YUV1D) {
134 unsigned int tile_size = 0;
136 die_codec(codec,
"Failed to get the tile size");
137 const unsigned int tile_width = tile_size >> 16;
138 const unsigned int tile_height = tile_size & 65535;
139 const uint32_t output_frame_width_in_tiles =
140 output_frame_width / tile_width;
143 const int row_offset =
144 (*tile_idx / output_frame_width_in_tiles) * tile_height;
145 const int col_offset =
146 (*tile_idx % output_frame_width_in_tiles) * tile_width;
148 aom_img_copy_tile(img, output, row_offset, col_offset);
153static void img_write_to_file(
const aom_image_t *img, FILE *file,
155 if (output_format == YUV)
156 aom_img_write(img, file);
157 else if (output_format == NV12)
158 aom_img_write_nv12(img, file);
160 die(
"Invalid output format");
163int main(
int argc,
char **argv) {
164 FILE *outfile = NULL;
165 AvxVideoReader *reader = NULL;
166 const AvxVideoInfo *info = NULL;
169 aom_image_t reference_images[MAX_EXTERNAL_REFERENCES];
172 size_t frame_size = 0;
173 const unsigned char *frame = NULL;
175 const char *tile_list_file = NULL;
176 int output_format = YUV1D;
179 if (argc < 5) die(
"Invalid number of arguments.");
181 reader = aom_video_reader_open(argv[1]);
182 if (!reader) die(
"Failed to open %s for reading.", argv[1]);
184 if (!(outfile = fopen(argv[2],
"wb")))
185 die(
"Failed to open %s for writing.", argv[2]);
187 num_references = (int)strtol(argv[3], NULL, 0);
188 tile_list_file = argv[4];
190 if (argc > 5) output_format = (int)strtol(argv[5], NULL, 0);
191 if (output_format < YUV1D || output_format > NV12)
192 die(
"Output format out of range [0, 2]");
194 info = aom_video_reader_get_info(reader);
197 if (info->codec_fourcc == LST_FOURCC)
198 decoder = get_aom_decoder_by_fourcc(AV1_FOURCC);
200 die(
"Unknown input codec.");
205 die_codec(&codec,
"Failed to initialize decoder.");
209 die(
"Failed to set annex b status");
214 for (i = 0; i < num_references; ++i) {
215 aom_video_reader_read_frame(reader);
216 frame = aom_video_reader_get_frame(reader, &frame_size);
218 die_codec(&codec,
"Failed to decode frame.");
222 die_codec(&codec,
"Failed to get the image format");
226 die_codec(&codec,
"Failed to get the image frame size");
230 for (j = 0; j < num_references; j++) {
231 unsigned int border = AOM_DEC_BORDER_IN_PIXELS;
233 frame_res[0], frame_res[1], 32, 8,
235 die(
"Failed to allocate references.");
241 &reference_images[i]))
242 die_codec(&codec,
"Failed to copy decoded reference frame");
248 snprintf(name,
sizeof(name),
"ref_%d.yuv", i);
249 printf(
"writing ref image to %s, %u, %u\n", name, img->
d_w, img->
d_h);
250 FILE *ref_file = fopen(name,
"wb");
251 aom_img_write(img, ref_file);
256 FILE *infile = aom_video_reader_get_file(reader);
258 const FileOffset camera_frame_pos = ftello(infile);
260 printf(
"Loading compressed frames into memory.\n");
264 while (aom_video_reader_read_frame(reader)) {
267 if (num_frames < 1) die(
"Input light field has no frames.");
270 unsigned char **frames =
271 (
unsigned char **)malloc(num_frames *
sizeof(
unsigned char *));
272 size_t *frame_sizes = (
size_t *)malloc(num_frames *
sizeof(
size_t));
273 if (!(frames && frame_sizes)) die(
"Failed to allocate frame data.");
275 fseeko(infile, camera_frame_pos, SEEK_SET);
276 for (
int f = 0; f < num_frames; ++f) {
277 aom_video_reader_read_frame(reader);
278 frame = aom_video_reader_get_frame(reader, &frame_size);
279 frames[f] = (
unsigned char *)malloc(frame_size *
sizeof(
unsigned char));
280 if (!frames[f]) die(
"Failed to allocate frame data.");
281 memcpy(frames[f], frame, frame_size);
282 frame_sizes[f] = frame_size;
284 printf(
"Read %d frames.\n", num_frames);
286 if (output_format != YUV1D) {
291 output_frame_height, 32))
292 die(
"Failed to allocate output image.");
295 printf(
"Decoding tile list from file.\n");
297 FILE *tile_list_fptr = fopen(tile_list_file,
"r");
298 if (!tile_list_fptr) die_codec(&codec,
"Failed to open tile list file.");
299 int tile_list_cnt = 0;
300 int tile_list_writes = 0;
303 unsigned int output_bit_depth = 0;
305 while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
306 if (line[0] ==
'F') {
307 if (output_format != YUV1D) {
311 if (output_bit_depth != 0) {
312 if (!aom_shift_img(output_bit_depth, &out, &output_shifted)) {
313 die(
"Error allocating image");
316 img_write_to_file(out, outfile, output_format);
328 int image_idx, ref_idx, tc, tr;
329 sscanf(line,
"%d %d %d %d", &image_idx, &ref_idx, &tc, &tr);
330 if (image_idx >= num_frames) {
331 die(
"Tile list image_idx out of bounds: %d >= %d.", image_idx,
334 if (ref_idx >= num_references) {
335 die(
"Tile list ref_idx out of bounds: %d >= %d.", ref_idx,
338 frame = frames[image_idx];
339 frame_size = frame_sizes[image_idx];
342 decode_tile(&codec, frame, frame_size, tr, tc, ref_idx, reference_images,
343 &output, &tile_idx, &output_bit_depth, &img, output_format);
344 if (output_format == YUV1D) {
346 if (output_bit_depth != 0) {
347 if (!aom_shift_img(output_bit_depth, &out, &output_shifted)) {
348 die(
"Error allocating image");
351 aom_img_write(out, outfile);
355 if (output_format != YUV1D) {
357 if (tile_list_writes < tile_list_cnt) {
359 if (output_bit_depth != 0) {
360 if (!aom_shift_img(output_bit_depth, &out, &output_shifted)) {
361 die(
"Error allocating image");
364 img_write_to_file(out, outfile, output_format);
370 for (i = 0; i < num_references; i++)
aom_img_free(&reference_images[i]);
371 for (
int f = 0; f < num_frames; ++f) {
377 aom_video_reader_close(reader);
Describes the decoder algorithm interface to applications.
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.
#define AOM_IMG_FMT_HIGHBITDEPTH
Definition aom_image.h:38
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.
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
int aom_img_plane_height(const aom_image_t *img, int plane)
Get the height of a plane.
int aom_img_plane_width(const aom_image_t *img, int plane)
Get the width of a plane.
void aom_img_free(aom_image_t *img)
Close an image descriptor.
Provides definitions for using AOM or AV1 within the aom Decoder interface.
@ AV1_SET_TILE_MODE
Codec control function to set the tile coding mode, unsigned int parameter.
Definition aomdx.h:316
@ AV1D_GET_FRAME_SIZE
Codec control function to get the dimensions that the current frame is decoded at,...
Definition aomdx.h:217
@ AV1D_SET_IS_ANNEXB
Codec control function to indicate whether bitstream is in Annex-B format, unsigned int parameter.
Definition aomdx.h:352
@ AV1D_GET_TILE_SIZE
Codec control function to get the width and height (in pixels) of the tiles in a tile list,...
Definition aomdx.h:243
@ AV1_SET_DECODE_TILE_ROW
Codec control function to set the range of tile decoding, int parameter.
Definition aomdx.h:307
@ AV1D_GET_IMG_FORMAT
Codec control function to get the image format of the stream, aom_img_fmt_t* parameter.
Definition aomdx.h:235
@ AV1D_EXT_TILE_DEBUG
Codec control function to enable the ext-tile software debug and testing code in the decoder,...
Definition aomdx.h:339
@ AV1_SET_REFERENCE
Codec control function to write a frame into a reference buffer.
Definition aom.h:57
@ AV1_COPY_NEW_FRAME_IMAGE
Codec control function to copy the new frame to an external buffer.
Definition aom.h:76
const char * aom_codec_iface_name(aom_codec_iface_t *iface)
Return the name for a given interface.
const struct aom_codec_iface aom_codec_iface_t
Codec interface structure.
Definition aom_codec.h:271
aom_codec_err_t aom_codec_destroy(aom_codec_ctx_t *ctx)
Destroy a codec instance.
aom_codec_err_t
Algorithm return codes.
Definition aom_codec.h:155
#define AOM_CODEC_CONTROL_TYPECHECKED(ctx, id, data)
aom_codec_control wrapper macro (adds type-checking, less flexible)
Definition aom_codec.h:542
const void * aom_codec_iter_t
Iterator.
Definition aom_codec.h:305
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
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.
#define aom_codec_dec_init(ctx, iface, cfg, flags)
Convenience macro for aom_codec_dec_init_ver()
Definition aom_decoder.h:129
Codec context structure.
Definition aom_codec.h:315
Image Descriptor.
Definition aom_image.h:182
unsigned int bit_depth
Definition aom_image.h:194
unsigned int y_chroma_shift
Definition aom_image.h:206
aom_img_fmt_t fmt
Definition aom_image.h:183
int stride[3]
Definition aom_image.h:216
unsigned char * img_data
Definition aom_image.h:230
unsigned int x_chroma_shift
Definition aom_image.h:205
unsigned int d_w
Definition aom_image.h:197
unsigned int d_h
Definition aom_image.h:198
unsigned char * planes[3]
Definition aom_image.h:215
size_t sz
Definition aom_image.h:217
AV1 specific reference frame data struct.
Definition aom.h:89
int use_external_ref
Definition aom.h:91
aom_image_t img
Definition aom.h:92
int idx
Definition aom.h:90