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