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