AOMedia Codec SDK
aomdec
1 /*
2  * Copyright (c) 2016, 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 #include <assert.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #include <limits.h>
18 
19 #include "config/aom_config.h"
20 
21 #if CONFIG_OS_SUPPORT
22 #if HAVE_UNISTD_H
23 #include <unistd.h> // NOLINT
24 #elif !defined(STDOUT_FILENO)
25 #define STDOUT_FILENO 1
26 #endif
27 #endif
28 
29 #include "aom/aom_decoder.h"
30 #include "aom/aomdx.h"
31 #include "aom_ports/aom_timer.h"
32 #include "aom_ports/mem_ops.h"
33 #include "common/args.h"
34 #include "common/ivfdec.h"
35 #include "common/md5_utils.h"
36 #include "common/obudec.h"
37 #include "common/tools_common.h"
38 
39 #if CONFIG_WEBM_IO
40 #include "common/webmdec.h"
41 #endif
42 
43 #include "common/y4menc.h"
44 
45 #if CONFIG_LIBYUV
46 #include "third_party/libyuv/include/libyuv/scale.h"
47 #endif
48 
49 static const char *exec_name;
50 
51 struct AvxDecInputContext {
52  struct AvxInputContext *aom_input_ctx;
53  struct ObuDecInputContext *obu_ctx;
54  struct WebmInputContext *webm_ctx;
55 };
56 
57 static const arg_def_t help =
58  ARG_DEF(NULL, "help", 0, "Show usage options and exit");
59 static const arg_def_t looparg =
60  ARG_DEF(NULL, "loops", 1, "Number of times to decode the file");
61 static const arg_def_t codecarg = ARG_DEF(NULL, "codec", 1, "Codec to use");
62 static const arg_def_t use_yv12 =
63  ARG_DEF(NULL, "yv12", 0, "Output raw YV12 frames");
64 static const arg_def_t use_i420 =
65  ARG_DEF(NULL, "i420", 0, "Output raw I420 frames");
66 static const arg_def_t flipuvarg =
67  ARG_DEF(NULL, "flipuv", 0, "Flip the chroma planes in the output");
68 static const arg_def_t rawvideo =
69  ARG_DEF(NULL, "rawvideo", 0, "Output raw YUV frames");
70 static const arg_def_t noblitarg =
71  ARG_DEF(NULL, "noblit", 0, "Don't process the decoded frames");
72 static const arg_def_t progressarg =
73  ARG_DEF(NULL, "progress", 0, "Show progress after each frame decodes");
74 static const arg_def_t limitarg =
75  ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
76 static const arg_def_t skiparg =
77  ARG_DEF(NULL, "skip", 1, "Skip the first n input frames");
78 static const arg_def_t postprocarg =
79  ARG_DEF(NULL, "postproc", 0, "Postprocess decoded frames");
80 static const arg_def_t summaryarg =
81  ARG_DEF(NULL, "summary", 0, "Show timing summary");
82 static const arg_def_t outputfile =
83  ARG_DEF("o", "output", 1, "Output file name pattern (see below)");
84 static const arg_def_t threadsarg =
85  ARG_DEF("t", "threads", 1, "Max threads to use");
86 static const arg_def_t verbosearg =
87  ARG_DEF("v", "verbose", 0, "Show version string");
88 static const arg_def_t scalearg =
89  ARG_DEF("S", "scale", 0, "Scale output frames uniformly");
90 static const arg_def_t continuearg =
91  ARG_DEF("k", "keep-going", 0, "(debug) Continue decoding after error");
92 static const arg_def_t fb_arg =
93  ARG_DEF(NULL, "frame-buffers", 1, "Number of frame buffers to use");
94 static const arg_def_t md5arg =
95  ARG_DEF(NULL, "md5", 0, "Compute the MD5 sum of the decoded frame");
96 static const arg_def_t framestatsarg =
97  ARG_DEF(NULL, "framestats", 1, "Output per-frame stats (.csv format)");
98 static const arg_def_t outbitdeptharg =
99  ARG_DEF(NULL, "output-bit-depth", 1, "Output bit-depth for decoded frames");
100 static const arg_def_t tilem = ARG_DEF(NULL, "tile-mode", 1,
101  "Tile coding mode "
102  "(0 for normal tile coding mode)");
103 static const arg_def_t tiler = ARG_DEF(NULL, "tile-row", 1,
104  "Row index of tile to decode "
105  "(-1 for all rows)");
106 static const arg_def_t tilec = ARG_DEF(NULL, "tile-column", 1,
107  "Column index of tile to decode "
108  "(-1 for all columns)");
109 static const arg_def_t isannexb =
110  ARG_DEF(NULL, "annexb", 0, "Bitstream is in Annex-B format");
111 static const arg_def_t oppointarg = ARG_DEF(
112  NULL, "oppoint", 1, "Select an operating point of a scalable bitstream");
113 static const arg_def_t outallarg = ARG_DEF(
114  NULL, "all-layers", 0, "Output all decoded frames of a scalable bitstream");
115 
116 static const arg_def_t *all_args[] = {
117  &help, &codecarg, &use_yv12, &use_i420, &flipuvarg,
118  &rawvideo, &noblitarg, &progressarg, &limitarg, &skiparg,
119  &postprocarg, &summaryarg, &outputfile, &threadsarg, &verbosearg,
120  &scalearg, &fb_arg, &md5arg, &framestatsarg, &continuearg,
121  &outbitdeptharg, &tilem, &tiler, &tilec, &isannexb,
122  &oppointarg, &outallarg, NULL
123 };
124 
125 #if CONFIG_LIBYUV
126 static INLINE int libyuv_scale(aom_image_t *src, aom_image_t *dst,
127  FilterModeEnum mode) {
128  if (src->fmt == AOM_IMG_FMT_I42016) {
129  assert(dst->fmt == AOM_IMG_FMT_I42016);
130  return I420Scale_16(
131  (uint16_t *)src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y] / 2,
132  (uint16_t *)src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U] / 2,
133  (uint16_t *)src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V] / 2,
134  src->d_w, src->d_h, (uint16_t *)dst->planes[AOM_PLANE_Y],
135  dst->stride[AOM_PLANE_Y] / 2, (uint16_t *)dst->planes[AOM_PLANE_U],
136  dst->stride[AOM_PLANE_U] / 2, (uint16_t *)dst->planes[AOM_PLANE_V],
137  dst->stride[AOM_PLANE_V] / 2, dst->d_w, dst->d_h, mode);
138  }
139  assert(src->fmt == AOM_IMG_FMT_I420);
140  assert(dst->fmt == AOM_IMG_FMT_I420);
141  return I420Scale(src->planes[AOM_PLANE_Y], src->stride[AOM_PLANE_Y],
142  src->planes[AOM_PLANE_U], src->stride[AOM_PLANE_U],
143  src->planes[AOM_PLANE_V], src->stride[AOM_PLANE_V], src->d_w,
144  src->d_h, dst->planes[AOM_PLANE_Y], dst->stride[AOM_PLANE_Y],
145  dst->planes[AOM_PLANE_U], dst->stride[AOM_PLANE_U],
146  dst->planes[AOM_PLANE_V], dst->stride[AOM_PLANE_V], dst->d_w,
147  dst->d_h, mode);
148 }
149 #endif
150 
151 void show_help(FILE *fout, int shorthelp) {
152  fprintf(fout, "Usage: %s <options> filename\n\n", exec_name);
153 
154  if (shorthelp) {
155  fprintf(fout, "Use --help to see the full list of options.\n");
156  return;
157  }
158 
159  fprintf(fout, "Options:\n");
160  arg_show_usage(fout, all_args);
161  fprintf(fout,
162  "\nOutput File Patterns:\n\n"
163  " The -o argument specifies the name of the file(s) to "
164  "write to. If the\n argument does not include any escape "
165  "characters, the output will be\n written to a single file. "
166  "Otherwise, the filename will be calculated by\n expanding "
167  "the following escape characters:\n");
168  fprintf(fout,
169  "\n\t%%w - Frame width"
170  "\n\t%%h - Frame height"
171  "\n\t%%<n> - Frame number, zero padded to <n> places (1..9)"
172  "\n\n Pattern arguments are only supported in conjunction "
173  "with the --yv12 and\n --i420 options. If the -o option is "
174  "not specified, the output will be\n directed to stdout.\n");
175  fprintf(fout, "\nIncluded decoders:\n\n");
176 
177  for (int i = 0; i < get_aom_decoder_count(); ++i) {
178  const AvxInterface *const decoder = get_aom_decoder_by_index(i);
179  fprintf(fout, " %-6s - %s\n", decoder->name,
180  aom_codec_iface_name(decoder->codec_interface()));
181  }
182 }
183 
184 void usage_exit(void) {
185  show_help(stderr, 1);
186  exit(EXIT_FAILURE);
187 }
188 
189 static int raw_read_frame(FILE *infile, uint8_t **buffer, size_t *bytes_read,
190  size_t *buffer_size) {
191  char raw_hdr[RAW_FRAME_HDR_SZ];
192  size_t frame_size = 0;
193 
194  if (fread(raw_hdr, RAW_FRAME_HDR_SZ, 1, infile) != 1) {
195  if (!feof(infile)) warn("Failed to read RAW frame size\n");
196  } else {
197  const size_t kCorruptFrameThreshold = 256 * 1024 * 1024;
198  const size_t kFrameTooSmallThreshold = 256 * 1024;
199  frame_size = mem_get_le32(raw_hdr);
200 
201  if (frame_size > kCorruptFrameThreshold) {
202  warn("Read invalid frame size (%u)\n", (unsigned int)frame_size);
203  frame_size = 0;
204  }
205 
206  if (frame_size < kFrameTooSmallThreshold) {
207  warn("Warning: Read invalid frame size (%u) - not a raw file?\n",
208  (unsigned int)frame_size);
209  }
210 
211  if (frame_size > *buffer_size) {
212  uint8_t *new_buf = realloc(*buffer, 2 * frame_size);
213  if (new_buf) {
214  *buffer = new_buf;
215  *buffer_size = 2 * frame_size;
216  } else {
217  warn("Failed to allocate compressed data buffer\n");
218  frame_size = 0;
219  }
220  }
221  }
222 
223  if (!feof(infile)) {
224  if (fread(*buffer, 1, frame_size, infile) != frame_size) {
225  warn("Failed to read full frame\n");
226  return 1;
227  }
228  *bytes_read = frame_size;
229  }
230 
231  return 0;
232 }
233 
234 static int read_frame(struct AvxDecInputContext *input, uint8_t **buf,
235  size_t *bytes_in_buffer, size_t *buffer_size) {
236  switch (input->aom_input_ctx->file_type) {
237 #if CONFIG_WEBM_IO
238  case FILE_TYPE_WEBM:
239  return webm_read_frame(input->webm_ctx, buf, bytes_in_buffer,
240  buffer_size);
241 #endif
242  case FILE_TYPE_RAW:
243  return raw_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
244  buffer_size);
245  case FILE_TYPE_IVF:
246  return ivf_read_frame(input->aom_input_ctx->file, buf, bytes_in_buffer,
247  buffer_size, NULL);
248  case FILE_TYPE_OBU:
249  return obudec_read_temporal_unit(input->obu_ctx, buf, bytes_in_buffer,
250  buffer_size);
251  default: return 1;
252  }
253 }
254 
255 static void update_image_md5(const aom_image_t *img, const int planes[3],
256  MD5Context *md5) {
257  int i, y;
258 
259  for (i = 0; i < 3; ++i) {
260  const int plane = planes[i];
261  const unsigned char *buf = img->planes[plane];
262  const int stride = img->stride[plane];
263  const int w = aom_img_plane_width(img, plane) *
264  ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
265  const int h = aom_img_plane_height(img, plane);
266 
267  for (y = 0; y < h; ++y) {
268  MD5Update(md5, buf, w);
269  buf += stride;
270  }
271  }
272 }
273 
274 static void write_image_file(const aom_image_t *img, const int *planes,
275  const int num_planes, FILE *file) {
276  int i, y;
277  const int bytes_per_sample = ((img->fmt & AOM_IMG_FMT_HIGHBITDEPTH) ? 2 : 1);
278 
279  for (i = 0; i < num_planes; ++i) {
280  const int plane = planes[i];
281  const unsigned char *buf = img->planes[plane];
282  const int stride = img->stride[plane];
283  const int w = aom_img_plane_width(img, plane);
284  const int h = aom_img_plane_height(img, plane);
285 
286  for (y = 0; y < h; ++y) {
287  fwrite(buf, bytes_per_sample, w, file);
288  buf += stride;
289  }
290  }
291 }
292 
293 static int file_is_raw(struct AvxInputContext *input) {
294  uint8_t buf[32];
295  int is_raw = 0;
297  memset(&si, 0, sizeof(si));
298 
299  if (fread(buf, 1, 32, input->file) == 32) {
300  int i;
301 
302  if (mem_get_le32(buf) < 256 * 1024 * 1024) {
303  for (i = 0; i < get_aom_decoder_count(); ++i) {
304  const AvxInterface *const decoder = get_aom_decoder_by_index(i);
305  if (!aom_codec_peek_stream_info(decoder->codec_interface(), buf + 4,
306  32 - 4, &si)) {
307  is_raw = 1;
308  input->fourcc = decoder->fourcc;
309  input->width = si.w;
310  input->height = si.h;
311  input->framerate.numerator = 30;
312  input->framerate.denominator = 1;
313  break;
314  }
315  }
316  }
317  }
318 
319  rewind(input->file);
320  return is_raw;
321 }
322 
323 static void show_progress(int frame_in, int frame_out, uint64_t dx_time) {
324  fprintf(stderr,
325  "%d decoded frames/%d showed frames in %" PRId64 " us (%.2f fps)\r",
326  frame_in, frame_out, dx_time,
327  (double)frame_out * 1000000.0 / (double)dx_time);
328 }
329 
330 struct ExternalFrameBuffer {
331  uint8_t *data;
332  size_t size;
333  int in_use;
334 };
335 
336 struct ExternalFrameBufferList {
337  int num_external_frame_buffers;
338  struct ExternalFrameBuffer *ext_fb;
339 };
340 
341 // Callback used by libaom to request an external frame buffer. |cb_priv|
342 // Application private data passed into the set function. |min_size| is the
343 // minimum size in bytes needed to decode the next frame. |fb| pointer to the
344 // frame buffer.
345 static int get_av1_frame_buffer(void *cb_priv, size_t min_size,
347  int i;
348  struct ExternalFrameBufferList *const ext_fb_list =
349  (struct ExternalFrameBufferList *)cb_priv;
350  if (ext_fb_list == NULL) return -1;
351 
352  // Find a free frame buffer.
353  for (i = 0; i < ext_fb_list->num_external_frame_buffers; ++i) {
354  if (!ext_fb_list->ext_fb[i].in_use) break;
355  }
356 
357  if (i == ext_fb_list->num_external_frame_buffers) return -1;
358 
359  if (ext_fb_list->ext_fb[i].size < min_size) {
360  free(ext_fb_list->ext_fb[i].data);
361  ext_fb_list->ext_fb[i].data = (uint8_t *)calloc(min_size, sizeof(uint8_t));
362  if (!ext_fb_list->ext_fb[i].data) return -1;
363 
364  ext_fb_list->ext_fb[i].size = min_size;
365  }
366 
367  fb->data = ext_fb_list->ext_fb[i].data;
368  fb->size = ext_fb_list->ext_fb[i].size;
369  ext_fb_list->ext_fb[i].in_use = 1;
370 
371  // Set the frame buffer's private data to point at the external frame buffer.
372  fb->priv = &ext_fb_list->ext_fb[i];
373  return 0;
374 }
375 
376 // Callback used by libaom when there are no references to the frame buffer.
377 // |cb_priv| user private data passed into the set function. |fb| pointer
378 // to the frame buffer.
379 static int release_av1_frame_buffer(void *cb_priv,
381  struct ExternalFrameBuffer *const ext_fb =
382  (struct ExternalFrameBuffer *)fb->priv;
383  (void)cb_priv;
384  ext_fb->in_use = 0;
385  return 0;
386 }
387 
388 static void generate_filename(const char *pattern, char *out, size_t q_len,
389  unsigned int d_w, unsigned int d_h,
390  unsigned int frame_in) {
391  const char *p = pattern;
392  char *q = out;
393 
394  do {
395  char *next_pat = strchr(p, '%');
396 
397  if (p == next_pat) {
398  size_t pat_len;
399 
400  /* parse the pattern */
401  q[q_len - 1] = '\0';
402  switch (p[1]) {
403  case 'w': snprintf(q, q_len - 1, "%d", d_w); break;
404  case 'h': snprintf(q, q_len - 1, "%d", d_h); break;
405  case '1': snprintf(q, q_len - 1, "%d", frame_in); break;
406  case '2': snprintf(q, q_len - 1, "%02d", frame_in); break;
407  case '3': snprintf(q, q_len - 1, "%03d", frame_in); break;
408  case '4': snprintf(q, q_len - 1, "%04d", frame_in); break;
409  case '5': snprintf(q, q_len - 1, "%05d", frame_in); break;
410  case '6': snprintf(q, q_len - 1, "%06d", frame_in); break;
411  case '7': snprintf(q, q_len - 1, "%07d", frame_in); break;
412  case '8': snprintf(q, q_len - 1, "%08d", frame_in); break;
413  case '9': snprintf(q, q_len - 1, "%09d", frame_in); break;
414  default: die("Unrecognized pattern %%%c\n", p[1]); break;
415  }
416 
417  pat_len = strlen(q);
418  if (pat_len >= q_len - 1) die("Output filename too long.\n");
419  q += pat_len;
420  p += 2;
421  q_len -= pat_len;
422  } else {
423  size_t copy_len;
424 
425  /* copy the next segment */
426  if (!next_pat)
427  copy_len = strlen(p);
428  else
429  copy_len = next_pat - p;
430 
431  if (copy_len >= q_len - 1) die("Output filename too long.\n");
432 
433  memcpy(q, p, copy_len);
434  q[copy_len] = '\0';
435  q += copy_len;
436  p += copy_len;
437  q_len -= copy_len;
438  }
439  } while (*p);
440 }
441 
442 static int is_single_file(const char *outfile_pattern) {
443  const char *p = outfile_pattern;
444 
445  do {
446  p = strchr(p, '%');
447  if (p && p[1] >= '1' && p[1] <= '9')
448  return 0; // pattern contains sequence number, so it's not unique
449  if (p) p++;
450  } while (p);
451 
452  return 1;
453 }
454 
455 static void print_md5(unsigned char digest[16], const char *filename) {
456  int i;
457 
458  for (i = 0; i < 16; ++i) printf("%02x", digest[i]);
459  printf(" %s\n", filename);
460 }
461 
462 static FILE *open_outfile(const char *name) {
463  if (strcmp("-", name) == 0) {
464  set_binary_mode(stdout);
465  return stdout;
466  } else {
467  FILE *file = fopen(name, "wb");
468  if (!file) fatal("Failed to open output file '%s'", name);
469  return file;
470  }
471 }
472 
473 static int img_shifted_realloc_required(const aom_image_t *img,
474  const aom_image_t *shifted,
475  aom_img_fmt_t required_fmt) {
476  return img->d_w != shifted->d_w || img->d_h != shifted->d_h ||
477  required_fmt != shifted->fmt;
478 }
479 
480 static int main_loop(int argc, const char **argv_) {
481  aom_codec_ctx_t decoder;
482  char *fn = NULL;
483  int i;
484  int ret = EXIT_FAILURE;
485  uint8_t *buf = NULL;
486  size_t bytes_in_buffer = 0, buffer_size = 0;
487  FILE *infile;
488  int frame_in = 0, frame_out = 0, flipuv = 0, noblit = 0;
489  int do_md5 = 0, progress = 0;
490  int stop_after = 0, postproc = 0, summary = 0, quiet = 1;
491  int arg_skip = 0;
492  int keep_going = 0;
493  const AvxInterface *interface = NULL;
494  const AvxInterface *fourcc_interface = NULL;
495  uint64_t dx_time = 0;
496  struct arg arg;
497  char **argv, **argi, **argj;
498 
499  int single_file;
500  int use_y4m = 1;
501  int opt_yv12 = 0;
502  int opt_i420 = 0;
503  int opt_raw = 0;
504  aom_codec_dec_cfg_t cfg = { 0, 0, 0, CONFIG_LOWBITDEPTH, { 1 } };
505  unsigned int output_bit_depth = 0;
506  unsigned int tile_mode = 0;
507  unsigned int is_annexb = 0;
508  int tile_row = -1;
509  int tile_col = -1;
510  int frames_corrupted = 0;
511  int dec_flags = 0;
512  int do_scale = 0;
513  int operating_point = 0;
514  int output_all_layers = 0;
515  aom_image_t *scaled_img = NULL;
516  aom_image_t *img_shifted = NULL;
517  int frame_avail, got_data, flush_decoder = 0;
518  int num_external_frame_buffers = 0;
519  struct ExternalFrameBufferList ext_fb_list = { 0, NULL };
520 
521  const char *outfile_pattern = NULL;
522  char outfile_name[PATH_MAX] = { 0 };
523  FILE *outfile = NULL;
524 
525  FILE *framestats_file = NULL;
526 
527  MD5Context md5_ctx;
528  unsigned char md5_digest[16];
529 
530  struct AvxDecInputContext input = { NULL, NULL, NULL };
531  struct AvxInputContext aom_input_ctx;
532  memset(&aom_input_ctx, 0, sizeof(aom_input_ctx));
533 #if CONFIG_WEBM_IO
534  struct WebmInputContext webm_ctx;
535  memset(&webm_ctx, 0, sizeof(webm_ctx));
536  input.webm_ctx = &webm_ctx;
537 #endif
538  struct ObuDecInputContext obu_ctx = { NULL, NULL, 0, 0, 0 };
539 
540  obu_ctx.avx_ctx = &aom_input_ctx;
541  input.obu_ctx = &obu_ctx;
542  input.aom_input_ctx = &aom_input_ctx;
543 
544  /* Parse command line */
545  exec_name = argv_[0];
546  argv = argv_dup(argc - 1, argv_ + 1);
547 
548  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
549  memset(&arg, 0, sizeof(arg));
550  arg.argv_step = 1;
551 
552  if (arg_match(&arg, &help, argi)) {
553  show_help(stdout, 0);
554  exit(EXIT_SUCCESS);
555  } else if (arg_match(&arg, &codecarg, argi)) {
556  interface = get_aom_decoder_by_name(arg.val);
557  if (!interface)
558  die("Error: Unrecognized argument (%s) to --codec\n", arg.val);
559  } else if (arg_match(&arg, &looparg, argi)) {
560  // no-op
561  } else if (arg_match(&arg, &outputfile, argi)) {
562  outfile_pattern = arg.val;
563  } else if (arg_match(&arg, &use_yv12, argi)) {
564  use_y4m = 0;
565  flipuv = 1;
566  opt_yv12 = 1;
567  opt_i420 = 0;
568  opt_raw = 0;
569  } else if (arg_match(&arg, &use_i420, argi)) {
570  use_y4m = 0;
571  flipuv = 0;
572  opt_yv12 = 0;
573  opt_i420 = 1;
574  opt_raw = 0;
575  } else if (arg_match(&arg, &rawvideo, argi)) {
576  use_y4m = 0;
577  opt_yv12 = 0;
578  opt_i420 = 0;
579  opt_raw = 1;
580  } else if (arg_match(&arg, &flipuvarg, argi)) {
581  flipuv = 1;
582  } else if (arg_match(&arg, &noblitarg, argi)) {
583  noblit = 1;
584  } else if (arg_match(&arg, &progressarg, argi)) {
585  progress = 1;
586  } else if (arg_match(&arg, &limitarg, argi)) {
587  stop_after = arg_parse_uint(&arg);
588  } else if (arg_match(&arg, &skiparg, argi)) {
589  arg_skip = arg_parse_uint(&arg);
590  } else if (arg_match(&arg, &postprocarg, argi)) {
591  postproc = 1;
592  } else if (arg_match(&arg, &md5arg, argi)) {
593  do_md5 = 1;
594  } else if (arg_match(&arg, &framestatsarg, argi)) {
595  framestats_file = fopen(arg.val, "w");
596  if (!framestats_file) {
597  die("Error: Could not open --framestats file (%s) for writing.\n",
598  arg.val);
599  }
600  } else if (arg_match(&arg, &summaryarg, argi)) {
601  summary = 1;
602  } else if (arg_match(&arg, &threadsarg, argi)) {
603  cfg.threads = arg_parse_uint(&arg);
604  } else if (arg_match(&arg, &verbosearg, argi)) {
605  quiet = 0;
606  } else if (arg_match(&arg, &scalearg, argi)) {
607  do_scale = 1;
608  } else if (arg_match(&arg, &fb_arg, argi)) {
609  num_external_frame_buffers = arg_parse_uint(&arg);
610  } else if (arg_match(&arg, &continuearg, argi)) {
611  keep_going = 1;
612  } else if (arg_match(&arg, &outbitdeptharg, argi)) {
613  output_bit_depth = arg_parse_uint(&arg);
614  } else if (arg_match(&arg, &tilem, argi)) {
615  tile_mode = arg_parse_int(&arg);
616  } else if (arg_match(&arg, &isannexb, argi)) {
617  is_annexb = 1;
618  input.obu_ctx->is_annexb = 1;
619  } else if (arg_match(&arg, &tiler, argi)) {
620  tile_row = arg_parse_int(&arg);
621  } else if (arg_match(&arg, &tilec, argi)) {
622  tile_col = arg_parse_int(&arg);
623  } else if (arg_match(&arg, &oppointarg, argi)) {
624  operating_point = arg_parse_int(&arg);
625  } else if (arg_match(&arg, &outallarg, argi)) {
626  output_all_layers = 1;
627  } else {
628  argj++;
629  }
630  }
631 
632  /* Check for unrecognized options */
633  for (argi = argv; *argi; argi++)
634  if (argi[0][0] == '-' && strlen(argi[0]) > 1)
635  die("Error: Unrecognized option %s\n", *argi);
636 
637  /* Handle non-option arguments */
638  fn = argv[0];
639 
640  if (!fn) {
641  free(argv);
642  fprintf(stderr, "No input file specified!\n");
643  usage_exit();
644  }
645  /* Open file */
646  infile = strcmp(fn, "-") ? fopen(fn, "rb") : set_binary_mode(stdin);
647 
648  if (!infile) {
649  fatal("Failed to open input file '%s'", strcmp(fn, "-") ? fn : "stdin");
650  }
651 #if CONFIG_OS_SUPPORT
652  /* Make sure we don't dump to the terminal, unless forced to with -o - */
653  if (!outfile_pattern && isatty(STDOUT_FILENO) && !do_md5 && !noblit) {
654  fprintf(stderr,
655  "Not dumping raw video to your terminal. Use '-o -' to "
656  "override.\n");
657  return EXIT_FAILURE;
658  }
659 #endif
660  input.aom_input_ctx->filename = fn;
661  input.aom_input_ctx->file = infile;
662  if (file_is_ivf(input.aom_input_ctx))
663  input.aom_input_ctx->file_type = FILE_TYPE_IVF;
664 #if CONFIG_WEBM_IO
665  else if (file_is_webm(input.webm_ctx, input.aom_input_ctx))
666  input.aom_input_ctx->file_type = FILE_TYPE_WEBM;
667 #endif
668  else if (file_is_obu(&obu_ctx))
669  input.aom_input_ctx->file_type = FILE_TYPE_OBU;
670  else if (file_is_raw(input.aom_input_ctx))
671  input.aom_input_ctx->file_type = FILE_TYPE_RAW;
672  else {
673  fprintf(stderr, "Unrecognized input file type.\n");
674 #if !CONFIG_WEBM_IO
675  fprintf(stderr, "aomdec was built without WebM container support.\n");
676 #endif
677  return EXIT_FAILURE;
678  }
679 
680  outfile_pattern = outfile_pattern ? outfile_pattern : "-";
681  single_file = is_single_file(outfile_pattern);
682 
683  if (!noblit && single_file) {
684  generate_filename(outfile_pattern, outfile_name, PATH_MAX,
685  aom_input_ctx.width, aom_input_ctx.height, 0);
686  if (do_md5)
687  MD5Init(&md5_ctx);
688  else
689  outfile = open_outfile(outfile_name);
690  }
691 
692  if (use_y4m && !noblit) {
693  if (!single_file) {
694  fprintf(stderr,
695  "YUV4MPEG2 not supported with output patterns,"
696  " try --i420 or --yv12 or --rawvideo.\n");
697  return EXIT_FAILURE;
698  }
699 
700 #if CONFIG_WEBM_IO
701  if (aom_input_ctx.file_type == FILE_TYPE_WEBM) {
702  if (webm_guess_framerate(input.webm_ctx, input.aom_input_ctx)) {
703  fprintf(stderr,
704  "Failed to guess framerate -- error parsing "
705  "webm file?\n");
706  return EXIT_FAILURE;
707  }
708  }
709 #endif
710  }
711 
712  fourcc_interface = get_aom_decoder_by_fourcc(aom_input_ctx.fourcc);
713  if (interface && fourcc_interface && interface != fourcc_interface)
714  warn("Header indicates codec: %s\n", fourcc_interface->name);
715  else
716  interface = fourcc_interface;
717 
718  if (!interface) interface = get_aom_decoder_by_index(0);
719 
720  dec_flags = (postproc ? AOM_CODEC_USE_POSTPROC : 0);
721  if (aom_codec_dec_init(&decoder, interface->codec_interface(), &cfg,
722  dec_flags)) {
723  fprintf(stderr, "Failed to initialize decoder: %s\n",
724  aom_codec_error(&decoder));
725  goto fail2;
726  }
727 
728  if (!quiet) fprintf(stderr, "%s\n", decoder.name);
729 
730 #if CONFIG_AV1_DECODER
731  if (aom_codec_control(&decoder, AV1_SET_TILE_MODE, tile_mode)) {
732  fprintf(stderr, "Failed to set decode_tile_mode: %s\n",
733  aom_codec_error(&decoder));
734  goto fail;
735  }
736 
737  if (aom_codec_control(&decoder, AV1D_SET_IS_ANNEXB, is_annexb)) {
738  fprintf(stderr, "Failed to set is_annexb: %s\n", aom_codec_error(&decoder));
739  goto fail;
740  }
741 
742  if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_ROW, tile_row)) {
743  fprintf(stderr, "Failed to set decode_tile_row: %s\n",
744  aom_codec_error(&decoder));
745  goto fail;
746  }
747 
748  if (aom_codec_control(&decoder, AV1_SET_DECODE_TILE_COL, tile_col)) {
749  fprintf(stderr, "Failed to set decode_tile_col: %s\n",
750  aom_codec_error(&decoder));
751  goto fail;
752  }
753 
754  if (aom_codec_control(&decoder, AV1D_SET_OPERATING_POINT, operating_point)) {
755  fprintf(stderr, "Failed to set operating_point: %s\n",
756  aom_codec_error(&decoder));
757  goto fail;
758  }
759 
761  output_all_layers)) {
762  fprintf(stderr, "Failed to set output_all_layers: %s\n",
763  aom_codec_error(&decoder));
764  goto fail;
765  }
766 #endif
767 
768  if (arg_skip) fprintf(stderr, "Skipping first %d frames.\n", arg_skip);
769  while (arg_skip) {
770  if (read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) break;
771  arg_skip--;
772  }
773 
774  if (num_external_frame_buffers > 0) {
775  ext_fb_list.num_external_frame_buffers = num_external_frame_buffers;
776  ext_fb_list.ext_fb = (struct ExternalFrameBuffer *)calloc(
777  num_external_frame_buffers, sizeof(*ext_fb_list.ext_fb));
778  if (aom_codec_set_frame_buffer_functions(&decoder, get_av1_frame_buffer,
779  release_av1_frame_buffer,
780  &ext_fb_list)) {
781  fprintf(stderr, "Failed to configure external frame buffers: %s\n",
782  aom_codec_error(&decoder));
783  goto fail;
784  }
785  }
786 
787  frame_avail = 1;
788  got_data = 0;
789 
790  if (framestats_file) fprintf(framestats_file, "bytes,qp\r\n");
791 
792  /* Decode file */
793  while (frame_avail || got_data) {
794  aom_codec_iter_t iter = NULL;
795  aom_image_t *img;
796  struct aom_usec_timer timer;
797  int corrupted = 0;
798 
799  frame_avail = 0;
800  if (!stop_after || frame_in < stop_after) {
801  if (!read_frame(&input, &buf, &bytes_in_buffer, &buffer_size)) {
802  frame_avail = 1;
803  frame_in++;
804 
805  aom_usec_timer_start(&timer);
806 
807  if (aom_codec_decode(&decoder, buf, bytes_in_buffer, NULL)) {
808  const char *detail = aom_codec_error_detail(&decoder);
809  warn("Failed to decode frame %d: %s", frame_in,
810  aom_codec_error(&decoder));
811 
812  if (detail) warn("Additional information: %s", detail);
813  if (!keep_going) goto fail;
814  }
815 
816  if (framestats_file) {
817  int qp;
818  if (aom_codec_control(&decoder, AOMD_GET_LAST_QUANTIZER, &qp)) {
819  warn("Failed AOMD_GET_LAST_QUANTIZER: %s",
820  aom_codec_error(&decoder));
821  if (!keep_going) goto fail;
822  }
823  fprintf(framestats_file, "%d,%d\r\n", (int)bytes_in_buffer, qp);
824  }
825 
826  aom_usec_timer_mark(&timer);
827  dx_time += aom_usec_timer_elapsed(&timer);
828  } else {
829  flush_decoder = 1;
830  }
831  } else {
832  flush_decoder = 1;
833  }
834 
835  aom_usec_timer_start(&timer);
836 
837  if (flush_decoder) {
838  // Flush the decoder in frame parallel decode.
839  if (aom_codec_decode(&decoder, NULL, 0, NULL)) {
840  warn("Failed to flush decoder: %s", aom_codec_error(&decoder));
841  }
842  }
843 
844  aom_usec_timer_mark(&timer);
845  dx_time += aom_usec_timer_elapsed(&timer);
846 
847  got_data = 0;
848  while ((img = aom_codec_get_frame(&decoder, &iter))) {
849  ++frame_out;
850  got_data = 1;
851 
852  if (aom_codec_control(&decoder, AOMD_GET_FRAME_CORRUPTED, &corrupted)) {
853  warn("Failed AOM_GET_FRAME_CORRUPTED: %s", aom_codec_error(&decoder));
854  if (!keep_going) goto fail;
855  }
856  frames_corrupted += corrupted;
857 
858  if (progress) show_progress(frame_in, frame_out, dx_time);
859 
860  if (!noblit) {
861  const int PLANES_YUV[] = { AOM_PLANE_Y, AOM_PLANE_U, AOM_PLANE_V };
862  const int PLANES_YVU[] = { AOM_PLANE_Y, AOM_PLANE_V, AOM_PLANE_U };
863  const int *planes = flipuv ? PLANES_YVU : PLANES_YUV;
864 
865  if (do_scale) {
866  if (frame_out == 1) {
867  // If the output frames are to be scaled to a fixed display size
868  // then use the width and height specified in the container. If
869  // either of these is set to 0, use the display size set in the
870  // first frame header. If that is unavailable, use the raw decoded
871  // size of the first decoded frame.
872  int render_width = aom_input_ctx.width;
873  int render_height = aom_input_ctx.height;
874  if (!render_width || !render_height) {
875  int render_size[2];
877  render_size)) {
878  // As last resort use size of first frame as display size.
879  render_width = img->d_w;
880  render_height = img->d_h;
881  } else {
882  render_width = render_size[0];
883  render_height = render_size[1];
884  }
885  }
886  scaled_img =
887  aom_img_alloc(NULL, img->fmt, render_width, render_height, 16);
888  scaled_img->bit_depth = img->bit_depth;
889  }
890 
891  if (img->d_w != scaled_img->d_w || img->d_h != scaled_img->d_h) {
892 #if CONFIG_LIBYUV
893  libyuv_scale(img, scaled_img, kFilterBox);
894  img = scaled_img;
895 #else
896  fprintf(
897  stderr,
898  "Failed to scale output frame: %s.\n"
899  "libyuv is required for scaling but is currently disabled.\n"
900  "Be sure to specify -DCONFIG_LIBYUV=1 when running cmake.\n",
901  aom_codec_error(&decoder));
902  goto fail;
903 #endif
904  }
905  }
906  // Default to codec bit depth if output bit depth not set
907  if (!output_bit_depth && single_file && !do_md5) {
908  output_bit_depth = img->bit_depth;
909  }
910  // Shift up or down if necessary
911  if (output_bit_depth != 0) {
912  const aom_img_fmt_t shifted_fmt =
913  output_bit_depth == 8
914  ? img->fmt ^ (img->fmt & AOM_IMG_FMT_HIGHBITDEPTH)
915  : img->fmt | AOM_IMG_FMT_HIGHBITDEPTH;
916 
917  if (shifted_fmt != img->fmt || output_bit_depth != img->bit_depth) {
918  if (img_shifted &&
919  img_shifted_realloc_required(img, img_shifted, shifted_fmt)) {
920  aom_img_free(img_shifted);
921  img_shifted = NULL;
922  }
923  if (!img_shifted) {
924  img_shifted =
925  aom_img_alloc(NULL, shifted_fmt, img->d_w, img->d_h, 16);
926  img_shifted->bit_depth = output_bit_depth;
927  img_shifted->monochrome = img->monochrome;
928  }
929  if (output_bit_depth > img->bit_depth) {
930  aom_img_upshift(img_shifted, img,
931  output_bit_depth - img->bit_depth);
932  } else {
933  aom_img_downshift(img_shifted, img,
934  img->bit_depth - output_bit_depth);
935  }
936  img = img_shifted;
937  }
938  }
939 
940  aom_input_ctx.width = img->d_w;
941  aom_input_ctx.height = img->d_h;
942 
943  int num_planes = (!use_y4m && opt_raw && img->monochrome) ? 1 : 3;
944 
945  if (single_file) {
946  if (use_y4m) {
947  char y4m_buf[Y4M_BUFFER_SIZE] = { 0 };
948  size_t len = 0;
949  if (frame_out == 1) {
950  // Y4M file header
951  len = y4m_write_file_header(
952  y4m_buf, sizeof(y4m_buf), aom_input_ctx.width,
953  aom_input_ctx.height, &aom_input_ctx.framerate, img->fmt,
954  img->bit_depth);
955  if (do_md5) {
956  MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
957  } else {
958  fputs(y4m_buf, outfile);
959  }
960  }
961 
962  // Y4M frame header
963  len = y4m_write_frame_header(y4m_buf, sizeof(y4m_buf));
964  if (do_md5) {
965  MD5Update(&md5_ctx, (md5byte *)y4m_buf, (unsigned int)len);
966  } else {
967  fputs(y4m_buf, outfile);
968  }
969  } else {
970  if (frame_out == 1) {
971  // Check if --yv12 or --i420 options are consistent with the
972  // bit-stream decoded
973  if (opt_i420) {
974  if (img->fmt != AOM_IMG_FMT_I420 &&
975  img->fmt != AOM_IMG_FMT_I42016) {
976  fprintf(stderr,
977  "Cannot produce i420 output for bit-stream.\n");
978  goto fail;
979  }
980  }
981  if (opt_yv12) {
982  if ((img->fmt != AOM_IMG_FMT_I420 &&
983  img->fmt != AOM_IMG_FMT_YV12) ||
984  img->bit_depth != 8) {
985  fprintf(stderr,
986  "Cannot produce yv12 output for bit-stream.\n");
987  goto fail;
988  }
989  }
990  }
991  }
992 
993  if (do_md5) {
994  update_image_md5(img, planes, &md5_ctx);
995  } else {
996  write_image_file(img, planes, num_planes, outfile);
997  }
998  } else {
999  generate_filename(outfile_pattern, outfile_name, PATH_MAX, img->d_w,
1000  img->d_h, frame_in);
1001  if (do_md5) {
1002  MD5Init(&md5_ctx);
1003  update_image_md5(img, planes, &md5_ctx);
1004  MD5Final(md5_digest, &md5_ctx);
1005  print_md5(md5_digest, outfile_name);
1006  } else {
1007  outfile = open_outfile(outfile_name);
1008  write_image_file(img, planes, num_planes, outfile);
1009  fclose(outfile);
1010  }
1011  }
1012  }
1013  }
1014  }
1015 
1016  if (summary || progress) {
1017  show_progress(frame_in, frame_out, dx_time);
1018  fprintf(stderr, "\n");
1019  }
1020 
1021  if (frames_corrupted) {
1022  fprintf(stderr, "WARNING: %d frames corrupted.\n", frames_corrupted);
1023  } else {
1024  ret = EXIT_SUCCESS;
1025  }
1026 
1027 fail:
1028 
1029  if (aom_codec_destroy(&decoder)) {
1030  fprintf(stderr, "Failed to destroy decoder: %s\n",
1031  aom_codec_error(&decoder));
1032  }
1033 
1034 fail2:
1035 
1036  if (!noblit && single_file) {
1037  if (do_md5) {
1038  MD5Final(md5_digest, &md5_ctx);
1039  print_md5(md5_digest, outfile_name);
1040  } else {
1041  fclose(outfile);
1042  }
1043  }
1044 
1045 #if CONFIG_WEBM_IO
1046  if (input.aom_input_ctx->file_type == FILE_TYPE_WEBM)
1047  webm_free(input.webm_ctx);
1048 #endif
1049  if (input.aom_input_ctx->file_type == FILE_TYPE_OBU)
1050  obudec_free(input.obu_ctx);
1051 
1052  if (input.aom_input_ctx->file_type != FILE_TYPE_WEBM) free(buf);
1053 
1054  if (scaled_img) aom_img_free(scaled_img);
1055  if (img_shifted) aom_img_free(img_shifted);
1056 
1057  for (i = 0; i < ext_fb_list.num_external_frame_buffers; ++i) {
1058  free(ext_fb_list.ext_fb[i].data);
1059  }
1060  free(ext_fb_list.ext_fb);
1061 
1062  fclose(infile);
1063  if (framestats_file) fclose(framestats_file);
1064 
1065  free(argv);
1066 
1067  return ret;
1068 }
1069 
1070 int main(int argc, const char **argv_) {
1071  unsigned int loops = 1, i;
1072  char **argv, **argi, **argj;
1073  struct arg arg;
1074  int error = 0;
1075 
1076  argv = argv_dup(argc - 1, argv_ + 1);
1077  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
1078  memset(&arg, 0, sizeof(arg));
1079  arg.argv_step = 1;
1080 
1081  if (arg_match(&arg, &looparg, argi)) {
1082  loops = arg_parse_uint(&arg);
1083  break;
1084  }
1085  }
1086  free(argv);
1087  for (i = 0; !error && i < loops; i++) error = main_loop(argc, argv_);
1088  return error;
1089 }
#define AOM_PLANE_U
Definition: aom_image.h:170
unsigned int d_h
Definition: aom_image.h:157
#define AOM_PLANE_V
Definition: aom_image.h:171
aom_codec_err_t aom_codec_peek_stream_info(aom_codec_iface_t *iface, const uint8_t *data, size_t data_sz, aom_codec_stream_info_t *si)
Parse stream info from a buffer.
Definition: aomdx.h:199
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
enum aom_img_fmt aom_img_fmt_t
List of supported image formats.
const char * name
Definition: aom_codec.h:205
Definition: aomdx.h:191
uint8_t * data
Definition: aom_frame_buffer.h:41
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
External frame buffer.
Definition: aom_frame_buffer.h:40
Describes the decoder algorithm interface to applications.
int monochrome
Definition: aom_image.h:146
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.
Definition: aomdx.h:102
#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.
void * priv
Definition: aom_frame_buffer.h:43
void aom_img_free(aom_image_t *img)
Close an image descriptor.
unsigned int h
Definition: aom_decoder.h:85
Initialization Configurations.
Definition: aom_decoder.h:103
#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.
Definition: aomdx.h:157
Stream properties.
Definition: aom_decoder.h:83
unsigned int w
Definition: aom_decoder.h:84
int aom_img_plane_height(const aom_image_t *img, int plane)
Get the height of a plane.
Definition: aomdx.h:165
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:194
size_t size
Definition: aom_frame_buffer.h:42
Definition: aomdx.h:171
Definition: aomdx.h:210
const char * aom_codec_error(aom_codec_ctx_t *ctx)
Retrieve error synopsis for codec context.
Definition: aom_image.h:52
Provides definitions for using AOM or AV1 within the aom Decoder interface.
aom_codec_err_t aom_codec_set_frame_buffer_functions(aom_codec_ctx_t *ctx, aom_get_frame_buffer_cb_fn_t cb_get, aom_release_frame_buffer_cb_fn_t cb_release, void *cb_priv)
Pass in external frame buffers for the decoder to use.
Definition: aom_image.h:45
const char * aom_codec_error_detail(aom_codec_ctx_t *ctx)
Retrieve detailed error information for codec context.
unsigned int threads
Definition: aom_decoder.h:104
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
Definition: aomdx.h:117
Definition: aom_image.h:43
aom_img_fmt_t fmt
Definition: aom_image.h:142
#define AOM_CODEC_USE_POSTPROC
Definition: aom_decoder.h:73
#define AOM_PLANE_Y
Definition: aom_image.h:169