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