44 #include "aom_scale/yv12config.h"
45 #include "av1/common/enums.h"
46 #include "common/tools_common.h"
47 #include "common/video_reader.h"
49 static const char *exec_name;
51 void usage_exit(
void) {
53 "Usage: %s <infile> <outfile> <num_references> <tile_list> <output "
54 "format(optional)>\n",
60 const int output_frame_width = 512;
61 const int output_frame_height = 512;
64 int dst_row_offset,
int dst_col_offset) {
68 for (plane = 0; plane < 3; ++plane) {
69 const unsigned char *src_buf = src->
planes[plane];
70 const int src_stride = src->
stride[plane];
71 unsigned char *dst_buf = dst->
planes[plane];
72 const int dst_stride = dst->
stride[plane];
74 (plane > 0) ? dst_row_offset >> dst->
y_chroma_shift : dst_row_offset;
76 (plane > 0) ? dst_col_offset >> dst->
x_chroma_shift : dst_col_offset;
79 dst_buf += roffset * dst_stride + (coffset << shift);
85 for (y = 0; y < h; ++y) {
86 memcpy(dst_buf, src_buf, w);
87 src_buf += src_stride;
88 dst_buf += dst_stride;
94 size_t frame_size,
int tr,
int tc,
int ref_idx,
96 int *tile_idx,
unsigned int *output_bit_depth,
106 ref.
img = reference_images[ref_idx];
108 die_codec(codec,
"Failed to set reference frame.");
112 if (aom_status) die_codec(codec,
"Failed to decode tile.");
116 if (!img) die_codec(codec,
"Failed to get frame.");
126 if (output_format != YUV1D) {
128 unsigned int tile_size = 0;
130 die_codec(codec,
"Failed to get the tile size");
131 const unsigned int tile_width = tile_size >> 16;
132 const unsigned int tile_height = tile_size & 65535;
133 const uint32_t output_frame_width_in_tiles =
134 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 if (info->codec_fourcc == LST_FOURCC)
193 decoder = get_aom_decoder_by_fourcc(AV1_FOURCC);
195 die(
"Unknown input codec.");
199 die_codec(&codec,
"Failed to initialize decoder.");
202 die(
"Failed to set annex b status");
207 for (i = 0; i < num_references; ++i) {
208 aom_video_reader_read_frame(reader);
209 frame = aom_video_reader_get_frame(reader, &frame_size);
211 die_codec(&codec,
"Failed to decode frame.");
215 die_codec(&codec,
"Failed to get the image format");
219 die_codec(&codec,
"Failed to get the image frame size");
223 for (j = 0; j < num_references; j++) {
224 unsigned int border = AOM_DEC_BORDER_IN_PIXELS;
226 frame_res[0], frame_res[1], 32, 8,
228 die(
"Failed to allocate references.");
234 &reference_images[i]))
235 die_codec(&codec,
"Failed to copy decoded reference frame");
241 snprintf(name,
sizeof(name),
"ref_%d.yuv", i);
242 printf(
"writing ref image to %s, %d, %d\n", name, img->
d_w, img->
d_h);
243 FILE *ref_file = fopen(name,
"wb");
244 aom_img_write(img, ref_file);
249 FILE *infile = aom_video_reader_get_file(reader);
251 const FileOffset camera_frame_pos = ftello(infile);
253 printf(
"Loading compressed frames into memory.\n");
257 while (aom_video_reader_read_frame(reader)) {
260 if (num_frames < 1) die(
"Input light field has no frames.");
263 unsigned char **frames =
264 (
unsigned char **)malloc(num_frames *
sizeof(
unsigned char *));
265 size_t *frame_sizes = (
size_t *)malloc(num_frames *
sizeof(
size_t));
267 fseeko(infile, camera_frame_pos, SEEK_SET);
268 for (
int f = 0; f < num_frames; ++f) {
269 aom_video_reader_read_frame(reader);
270 frame = aom_video_reader_get_frame(reader, &frame_size);
271 frames[f] = (
unsigned char *)malloc(frame_size *
sizeof(
unsigned char));
272 memcpy(frames[f], frame, frame_size);
273 frame_sizes[f] = frame_size;
275 printf(
"Read %d frames.\n", num_frames);
277 if (output_format != YUV1D) {
282 output_frame_height, 32))
283 die(
"Failed to allocate output image.");
286 printf(
"Decoding tile list from file.\n");
288 FILE *tile_list_fptr = fopen(tile_list_file,
"r");
289 int tile_list_cnt = 0;
290 int tile_list_writes = 0;
293 unsigned int output_bit_depth = 0;
295 while ((fgets(line, 1024, tile_list_fptr)) != NULL) {
296 if (line[0] ==
'F') {
297 if (output_format != YUV1D) {
301 if (output_bit_depth != 0)
302 aom_shift_img(output_bit_depth, &out, &output_shifted);
303 img_write_to_file(out, outfile, output_format);
315 int image_idx, ref_idx, tc, tr;
316 sscanf(line,
"%d %d %d %d", &image_idx, &ref_idx, &tc, &tr);
317 if (image_idx >= num_frames) {
318 die(
"Tile list image_idx out of bounds: %d >= %d.", image_idx,
321 if (ref_idx >= num_references) {
322 die(
"Tile list ref_idx out of bounds: %d >= %d.", ref_idx,
325 frame = frames[image_idx];
326 frame_size = frame_sizes[image_idx];
329 decode_tile(&codec, frame, frame_size, tr, tc, ref_idx, reference_images,
330 &output, &tile_idx, &output_bit_depth, &img, output_format);
331 if (output_format == YUV1D) {
333 if (output_bit_depth != 0)
334 aom_shift_img(output_bit_depth, &out, &output_shifted);
335 aom_img_write(out, outfile);
339 if (output_format != YUV1D) {
341 if (tile_list_writes < tile_list_cnt) {
343 if (output_bit_depth != 0)
344 aom_shift_img(output_bit_depth, &out, &output_shifted);
345 img_write_to_file(out, outfile, output_format);
351 for (i = 0; i < num_references; i++)
aom_img_free(&reference_images[i]);
352 for (
int f = 0; f < num_frames; ++f) {
358 aom_video_reader_close(reader);