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