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