AOMedia AV1 Codec
inspect
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 // Inspect Decoder
13 // ================
14 //
15 // This is a simple decoder loop that writes JSON stats to stdout. This tool
16 // can also be compiled with Emscripten and used as a library.
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #ifdef __EMSCRIPTEN__
23 #include <emscripten.h>
24 #else
25 #define EMSCRIPTEN_KEEPALIVE
26 #endif
27 
28 #include "config/aom_config.h"
29 
30 #include "aom/aom_decoder.h"
31 #include "aom/aomdx.h"
32 #include "av1/common/av1_common_int.h"
33 
34 #if CONFIG_ACCOUNTING
35 #include "av1/decoder/accounting.h"
36 #endif
37 
38 #include "av1/decoder/inspection.h"
39 #include "common/args.h"
40 #include "common/tools_common.h"
41 #include "common/video_common.h"
42 #include "common/video_reader.h"
43 
44 // Max JSON buffer size.
45 const int MAX_BUFFER = 1024 * 1024 * 256;
46 
47 typedef enum {
48  ACCOUNTING_LAYER = 1,
49  BLOCK_SIZE_LAYER = 1 << 1,
50  TRANSFORM_SIZE_LAYER = 1 << 2,
51  TRANSFORM_TYPE_LAYER = 1 << 3,
52  MODE_LAYER = 1 << 4,
53  SKIP_LAYER = 1 << 5,
54  FILTER_LAYER = 1 << 6,
55  CDEF_LAYER = 1 << 7,
56  REFERENCE_FRAME_LAYER = 1 << 8,
57  MOTION_VECTORS_LAYER = 1 << 9,
58  UV_MODE_LAYER = 1 << 10,
59  CFL_LAYER = 1 << 11,
60  DUAL_FILTER_LAYER = 1 << 12,
61  Q_INDEX_LAYER = 1 << 13,
62  SEGMENT_ID_LAYER = 1 << 14,
63  MOTION_MODE_LAYER = 1 << 15,
64  COMPOUND_TYPE_LAYER = 1 << 16,
65  INTRABC_LAYER = 1 << 17,
66  PALETTE_LAYER = 1 << 18,
67  UV_PALETTE_LAYER = 1 << 19,
68  BITS_LAYER = 1 << 20,
69  ALL_LAYERS = (1 << 21) - 1
70 } LayerType;
71 
72 static LayerType layers = 0;
73 
74 static int stop_after = 0;
75 static int compress = 0;
76 
77 static const arg_def_t limit_arg =
78  ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
79 static const arg_def_t dump_all_arg = ARG_DEF("A", "all", 0, "Dump All");
80 static const arg_def_t compress_arg =
81  ARG_DEF("x", "compress", 0, "Compress JSON using RLE");
82 static const arg_def_t dump_accounting_arg =
83  ARG_DEF("a", "accounting", 0, "Dump Accounting");
84 static const arg_def_t dump_block_size_arg =
85  ARG_DEF("bs", "blockSize", 0, "Dump Block Size");
86 static const arg_def_t dump_motion_vectors_arg =
87  ARG_DEF("mv", "motionVectors", 0, "Dump Motion Vectors");
88 static const arg_def_t dump_transform_size_arg =
89  ARG_DEF("ts", "transformSize", 0, "Dump Transform Size");
90 static const arg_def_t dump_transform_type_arg =
91  ARG_DEF("tt", "transformType", 0, "Dump Transform Type");
92 static const arg_def_t dump_mode_arg = ARG_DEF("m", "mode", 0, "Dump Mode");
93 static const arg_def_t dump_motion_mode_arg =
94  ARG_DEF("mm", "motion_mode", 0, "Dump Motion Modes");
95 static const arg_def_t dump_compound_type_arg =
96  ARG_DEF("ct", "compound_type", 0, "Dump Compound Types");
97 static const arg_def_t dump_uv_mode_arg =
98  ARG_DEF("uvm", "uv_mode", 0, "Dump UV Intra Prediction Modes");
99 static const arg_def_t dump_skip_arg = ARG_DEF("s", "skip", 0, "Dump Skip");
100 static const arg_def_t dump_filter_arg =
101  ARG_DEF("f", "filter", 0, "Dump Filter");
102 static const arg_def_t dump_cdef_arg = ARG_DEF("c", "cdef", 0, "Dump CDEF");
103 static const arg_def_t dump_cfl_arg =
104  ARG_DEF("cfl", "chroma_from_luma", 0, "Dump Chroma from Luma Alphas");
105 static const arg_def_t dump_dual_filter_type_arg =
106  ARG_DEF("df", "dualFilterType", 0, "Dump Dual Filter Type");
107 static const arg_def_t dump_reference_frame_arg =
108  ARG_DEF("r", "referenceFrame", 0, "Dump Reference Frame");
109 static const arg_def_t dump_delta_q_arg =
110  ARG_DEF("dq", "delta_q", 0, "Dump QIndex");
111 static const arg_def_t dump_seg_id_arg =
112  ARG_DEF("si", "seg_id", 0, "Dump Segment ID");
113 static const arg_def_t dump_intrabc_arg =
114  ARG_DEF("ibc", "intrabc", 0, "Dump If IntraBC Is Used");
115 static const arg_def_t dump_palette_arg =
116  ARG_DEF("plt", "palette", 0, "Dump Palette Size");
117 static const arg_def_t dump_uv_palette_arg =
118  ARG_DEF("uvp", "uv_palette", 0, "Dump UV Palette Size");
119 static const arg_def_t dump_bits_arg =
120  ARG_DEF("sb", "sb_bits", 0, "Dump Bits of each superblock");
121 static const arg_def_t usage_arg = ARG_DEF("h", "help", 0, "Help");
122 static const arg_def_t skip_non_transform_arg = ARG_DEF(
123  "snt", "skip_non_transform", 1, "Skip is counted as a non transform.");
124 static const arg_def_t combined_arg =
125  ARG_DEF("comb", "combined", 1, "combinining parameters into one output.");
126 
127 int combined_parm_list[15];
128 int combined_parm_count = 0;
129 
130 static const arg_def_t *main_args[] = { &limit_arg,
131  &dump_all_arg,
132  &compress_arg,
133 #if CONFIG_ACCOUNTING
134  &dump_accounting_arg,
135 #endif
136  &dump_block_size_arg,
137  &dump_transform_size_arg,
138  &dump_transform_type_arg,
139  &dump_mode_arg,
140  &dump_uv_mode_arg,
141  &dump_motion_mode_arg,
142  &dump_compound_type_arg,
143  &dump_skip_arg,
144  &dump_filter_arg,
145  &dump_cdef_arg,
146  &dump_dual_filter_type_arg,
147  &dump_cfl_arg,
148  &dump_reference_frame_arg,
149  &dump_motion_vectors_arg,
150  &dump_delta_q_arg,
151  &dump_seg_id_arg,
152  &dump_intrabc_arg,
153  &dump_palette_arg,
154  &dump_uv_palette_arg,
155  &dump_bits_arg,
156  &usage_arg,
157  &skip_non_transform_arg,
158  &combined_arg,
159  NULL };
160 #define ENUM(name) { #name, name }
161 #define LAST_ENUM { NULL, 0 }
162 typedef struct map_entry {
163  const char *name;
164  int value;
165 } map_entry;
166 
167 const map_entry refs_map[] = {
168  ENUM(INTRA_FRAME), ENUM(LAST_FRAME), ENUM(LAST2_FRAME),
169  ENUM(LAST3_FRAME), ENUM(GOLDEN_FRAME), ENUM(BWDREF_FRAME),
170  ENUM(ALTREF2_FRAME), ENUM(ALTREF_FRAME), LAST_ENUM
171 };
172 
173 const map_entry block_size_map[] = {
174  ENUM(BLOCK_4X4), ENUM(BLOCK_4X8), ENUM(BLOCK_8X4),
175  ENUM(BLOCK_8X8), ENUM(BLOCK_8X16), ENUM(BLOCK_16X8),
176  ENUM(BLOCK_16X16), ENUM(BLOCK_16X32), ENUM(BLOCK_32X16),
177  ENUM(BLOCK_32X32), ENUM(BLOCK_32X64), ENUM(BLOCK_64X32),
178  ENUM(BLOCK_64X64), ENUM(BLOCK_64X128), ENUM(BLOCK_128X64),
179  ENUM(BLOCK_128X128), ENUM(BLOCK_4X16), ENUM(BLOCK_16X4),
180  ENUM(BLOCK_8X32), ENUM(BLOCK_32X8), ENUM(BLOCK_16X64),
181  ENUM(BLOCK_64X16), LAST_ENUM
182 };
183 
184 #define TX_SKIP -1
185 
186 const map_entry tx_size_map[] = {
187  ENUM(TX_4X4), ENUM(TX_8X8), ENUM(TX_16X16), ENUM(TX_32X32),
188  ENUM(TX_64X64), ENUM(TX_4X8), ENUM(TX_8X4), ENUM(TX_8X16),
189  ENUM(TX_16X8), ENUM(TX_16X32), ENUM(TX_32X16), ENUM(TX_32X64),
190  ENUM(TX_64X32), ENUM(TX_4X16), ENUM(TX_16X4), ENUM(TX_8X32),
191  ENUM(TX_32X8), ENUM(TX_16X64), ENUM(TX_64X16), LAST_ENUM
192 };
193 
194 const map_entry tx_type_map[] = { ENUM(DCT_DCT),
195  ENUM(ADST_DCT),
196  ENUM(DCT_ADST),
197  ENUM(ADST_ADST),
198  ENUM(FLIPADST_DCT),
199  ENUM(DCT_FLIPADST),
200  ENUM(FLIPADST_FLIPADST),
201  ENUM(ADST_FLIPADST),
202  ENUM(FLIPADST_ADST),
203  ENUM(IDTX),
204  ENUM(V_DCT),
205  ENUM(H_DCT),
206  ENUM(V_ADST),
207  ENUM(H_ADST),
208  ENUM(V_FLIPADST),
209  ENUM(H_FLIPADST),
210  LAST_ENUM };
211 const map_entry dual_filter_map[] = { ENUM(REG_REG), ENUM(REG_SMOOTH),
212  ENUM(REG_SHARP), ENUM(SMOOTH_REG),
213  ENUM(SMOOTH_SMOOTH), ENUM(SMOOTH_SHARP),
214  ENUM(SHARP_REG), ENUM(SHARP_SMOOTH),
215  ENUM(SHARP_SHARP), LAST_ENUM };
216 
217 const map_entry prediction_mode_map[] = {
218  ENUM(DC_PRED), ENUM(V_PRED), ENUM(H_PRED),
219  ENUM(D45_PRED), ENUM(D135_PRED), ENUM(D113_PRED),
220  ENUM(D157_PRED), ENUM(D203_PRED), ENUM(D67_PRED),
221  ENUM(SMOOTH_PRED), ENUM(SMOOTH_V_PRED), ENUM(SMOOTH_H_PRED),
222  ENUM(PAETH_PRED), ENUM(NEARESTMV), ENUM(NEARMV),
223  ENUM(GLOBALMV), ENUM(NEWMV), ENUM(NEAREST_NEARESTMV),
224  ENUM(NEAR_NEARMV), ENUM(NEAREST_NEWMV), ENUM(NEW_NEARESTMV),
225  ENUM(NEAR_NEWMV), ENUM(NEW_NEARMV), ENUM(GLOBAL_GLOBALMV),
226  ENUM(NEW_NEWMV), ENUM(INTRA_INVALID), LAST_ENUM
227 };
228 
229 const map_entry motion_mode_map[] = { ENUM(SIMPLE_TRANSLATION),
230  ENUM(OBMC_CAUSAL), // 2-sided OBMC
231  ENUM(WARPED_CAUSAL), // 2-sided WARPED
232  LAST_ENUM };
233 
234 const map_entry compound_type_map[] = { ENUM(COMPOUND_AVERAGE),
235  ENUM(COMPOUND_WEDGE),
236  ENUM(COMPOUND_DIFFWTD), LAST_ENUM };
237 
238 const map_entry uv_prediction_mode_map[] = {
239  ENUM(UV_DC_PRED), ENUM(UV_V_PRED),
240  ENUM(UV_H_PRED), ENUM(UV_D45_PRED),
241  ENUM(UV_D135_PRED), ENUM(UV_D113_PRED),
242  ENUM(UV_D157_PRED), ENUM(UV_D203_PRED),
243  ENUM(UV_D67_PRED), ENUM(UV_SMOOTH_PRED),
244  ENUM(UV_SMOOTH_V_PRED), ENUM(UV_SMOOTH_H_PRED),
245  ENUM(UV_PAETH_PRED), ENUM(UV_CFL_PRED),
246  ENUM(UV_MODE_INVALID), LAST_ENUM
247 };
248 #define NO_SKIP 0
249 #define SKIP 1
250 
251 const map_entry skip_map[] = { ENUM(SKIP), ENUM(NO_SKIP), LAST_ENUM };
252 
253 const map_entry intrabc_map[] = { { "INTRABC", 1 },
254  { "NO_INTRABC", 0 },
255  LAST_ENUM };
256 
257 const map_entry palette_map[] = {
258  { "ZERO_COLORS", 0 }, { "TWO_COLORS", 2 }, { "THREE_COLORS", 3 },
259  { "FOUR_COLORS", 4 }, { "FIVE_COLORS", 5 }, { "SIX_COLORS", 6 },
260  { "SEVEN_COLORS", 7 }, { "EIGHT_COLORS", 8 }, LAST_ENUM
261 };
262 
263 const map_entry config_map[] = { ENUM(MI_SIZE), LAST_ENUM };
264 
265 static const char *exec_name;
266 
267 struct parm_offset {
268  char parm[60];
269  char offset;
270 };
271 struct parm_offset parm_offsets[] = {
272  { "blockSize", offsetof(insp_mi_data, bsize) },
273  { "transformSize", offsetof(insp_mi_data, tx_size) },
274  { "transformType", offsetof(insp_mi_data, tx_type) },
275  { "dualFilterType", offsetof(insp_mi_data, dual_filter_type) },
276  { "mode", offsetof(insp_mi_data, mode) },
277  { "uv_mode", offsetof(insp_mi_data, uv_mode) },
278  { "motion_mode", offsetof(insp_mi_data, motion_mode) },
279  { "compound_type", offsetof(insp_mi_data, compound_type) },
280  { "referenceFrame", offsetof(insp_mi_data, ref_frame) },
281  { "skip", offsetof(insp_mi_data, skip) },
282  { "sb_bits", offsetof(insp_mi_data, sb_bits) },
283 };
284 int parm_count = sizeof(parm_offsets) / sizeof(parm_offsets[0]);
285 
286 static int convert_to_indices(char *str, int *indices, int maxCount,
287  int *count) {
288  *count = 0;
289  do {
290  char *comma = strchr(str, ',');
291  int length = (comma ? (int)(comma - str) : (int)strlen(str));
292  int i;
293  for (i = 0; i < parm_count; ++i) {
294  if (!strncmp(str, parm_offsets[i].parm, length)) {
295  break;
296  }
297  }
298  if (i == parm_count) return 0;
299  indices[(*count)++] = i;
300  if (*count > maxCount) return 0;
301  str += length + 1;
302  } while (strlen(str) > 0);
303  return 1;
304 }
305 
306 insp_frame_data frame_data;
307 int frame_count = 0;
308 int decoded_frame_count = 0;
309 aom_codec_ctx_t codec;
310 AvxVideoReader *reader = NULL;
311 const AvxVideoInfo *info = NULL;
312 aom_image_t *img = NULL;
313 
314 static void on_frame_decoded_dump(char *json) {
315 #ifdef __EMSCRIPTEN__
316  EM_ASM_({ Module.on_frame_decoded_json($0); }, json);
317 #else
318  printf("%s", json);
319 #endif
320 }
321 
322 // Writing out the JSON buffer using snprintf is very slow, especially when
323 // compiled with emscripten, these functions speed things up quite a bit.
324 static int put_str(char *buffer, const char *str) {
325  int i;
326  for (i = 0; str[i] != '\0'; i++) {
327  buffer[i] = str[i];
328  }
329  return i;
330 }
331 
332 static int put_str_with_escape(char *buffer, const char *str) {
333  int i;
334  int j = 0;
335  for (i = 0; str[i] != '\0'; i++) {
336  if (str[i] < ' ') {
337  continue;
338  } else if (str[i] == '"' || str[i] == '\\') {
339  buffer[j++] = '\\';
340  }
341  buffer[j++] = str[i];
342  }
343  return j;
344 }
345 
346 static int put_num(char *buffer, char prefix, int num, char suffix) {
347  int i = 0;
348  char *buf = buffer;
349  int is_neg = 0;
350  if (prefix) {
351  buf[i++] = prefix;
352  }
353  if (num == 0) {
354  buf[i++] = '0';
355  } else {
356  if (num < 0) {
357  num = -num;
358  is_neg = 1;
359  }
360  int s = i;
361  while (num != 0) {
362  buf[i++] = '0' + (num % 10);
363  num = num / 10;
364  }
365  if (is_neg) {
366  buf[i++] = '-';
367  }
368  int e = i - 1;
369  while (s < e) {
370  int t = buf[s];
371  buf[s] = buf[e];
372  buf[e] = t;
373  s++;
374  e--;
375  }
376  }
377  if (suffix) {
378  buf[i++] = suffix;
379  }
380  return i;
381 }
382 
383 static int put_map(char *buffer, const map_entry *map) {
384  char *buf = buffer;
385  const map_entry *entry = map;
386  while (entry->name != NULL) {
387  *(buf++) = '"';
388  buf += put_str(buf, entry->name);
389  *(buf++) = '"';
390  buf += put_num(buf, ':', entry->value, 0);
391  entry++;
392  if (entry->name != NULL) {
393  *(buf++) = ',';
394  }
395  }
396  return (int)(buf - buffer);
397 }
398 
399 #if 0
400 static int put_reference_frame(char *buffer) {
401  const int mi_rows = frame_data.mi_rows;
402  const int mi_cols = frame_data.mi_cols;
403  char *buf = buffer;
404  int r, c, t;
405  buf += put_str(buf, " \"referenceFrameMap\": {");
406  buf += put_map(buf, refs_map);
407  buf += put_str(buf, "},\n");
408  buf += put_str(buf, " \"referenceFrame\": [");
409  for (r = 0; r < mi_rows; ++r) {
410  *(buf++) = '[';
411  for (c = 0; c < mi_cols; ++c) {
412  insp_mi_data *mi = &frame_data.mi_grid[r * mi_cols + c];
413  buf += put_num(buf, '[', mi->ref_frame[0], 0);
414  buf += put_num(buf, ',', mi->ref_frame[1], ']');
415  if (compress) { // RLE
416  for (t = c + 1; t < mi_cols; ++t) {
417  insp_mi_data *next_mi = &frame_data.mi_grid[r * mi_cols + t];
418  if (mi->ref_frame[0] != next_mi->ref_frame[0] ||
419  mi->ref_frame[1] != next_mi->ref_frame[1]) {
420  break;
421  }
422  }
423  if (t - c > 1) {
424  *(buf++) = ',';
425  buf += put_num(buf, '[', t - c - 1, ']');
426  c = t - 1;
427  }
428  }
429  if (c < mi_cols - 1) *(buf++) = ',';
430  }
431  *(buf++) = ']';
432  if (r < mi_rows - 1) *(buf++) = ',';
433  }
434  buf += put_str(buf, "],\n");
435  return (int)(buf - buffer);
436 }
437 #endif
438 
439 static int put_motion_vectors(char *buffer) {
440  const int mi_rows = frame_data.mi_rows;
441  const int mi_cols = frame_data.mi_cols;
442  char *buf = buffer;
443  int r, c, t;
444  buf += put_str(buf, " \"motionVectors\": [");
445  for (r = 0; r < mi_rows; ++r) {
446  *(buf++) = '[';
447  for (c = 0; c < mi_cols; ++c) {
448  insp_mi_data *mi = &frame_data.mi_grid[r * mi_cols + c];
449  buf += put_num(buf, '[', mi->mv[0].col, 0);
450  buf += put_num(buf, ',', mi->mv[0].row, 0);
451  buf += put_num(buf, ',', mi->mv[1].col, 0);
452  buf += put_num(buf, ',', mi->mv[1].row, ']');
453  if (compress) { // RLE
454  for (t = c + 1; t < mi_cols; ++t) {
455  insp_mi_data *next_mi = &frame_data.mi_grid[r * mi_cols + t];
456  if (mi->mv[0].col != next_mi->mv[0].col ||
457  mi->mv[0].row != next_mi->mv[0].row ||
458  mi->mv[1].col != next_mi->mv[1].col ||
459  mi->mv[1].row != next_mi->mv[1].row) {
460  break;
461  }
462  }
463  if (t - c > 1) {
464  *(buf++) = ',';
465  buf += put_num(buf, '[', t - c - 1, ']');
466  c = t - 1;
467  }
468  }
469  if (c < mi_cols - 1) *(buf++) = ',';
470  }
471  *(buf++) = ']';
472  if (r < mi_rows - 1) *(buf++) = ',';
473  }
474  buf += put_str(buf, "],\n");
475  return (int)(buf - buffer);
476 }
477 
478 static int put_combined(char *buffer) {
479  const int mi_rows = frame_data.mi_rows;
480  const int mi_cols = frame_data.mi_cols;
481  char *buf = buffer;
482  int r, c, p;
483  buf += put_str(buf, " \"");
484  for (p = 0; p < combined_parm_count; ++p) {
485  if (p) buf += put_str(buf, "&");
486  buf += put_str(buf, parm_offsets[combined_parm_list[p]].parm);
487  }
488  buf += put_str(buf, "\": [");
489  for (r = 0; r < mi_rows; ++r) {
490  *(buf++) = '[';
491  for (c = 0; c < mi_cols; ++c) {
492  insp_mi_data *mi = &frame_data.mi_grid[r * mi_cols + c];
493  *(buf++) = '[';
494  for (p = 0; p < combined_parm_count; ++p) {
495  if (p) *(buf++) = ',';
496  int16_t *v = (int16_t *)(((int8_t *)mi) +
497  parm_offsets[combined_parm_list[p]].offset);
498  buf += put_num(buf, 0, v[0], 0);
499  }
500  *(buf++) = ']';
501  if (c < mi_cols - 1) *(buf++) = ',';
502  }
503  *(buf++) = ']';
504  if (r < mi_rows - 1) *(buf++) = ',';
505  }
506  buf += put_str(buf, "],\n");
507  return (int)(buf - buffer);
508 }
509 
510 static int put_block_info(char *buffer, const map_entry *map, const char *name,
511  size_t offset, int len) {
512  const int mi_rows = frame_data.mi_rows;
513  const int mi_cols = frame_data.mi_cols;
514  char *buf = buffer;
515  int r, c, t, i;
516  if (compress && len == 1) {
517  die("Can't encode scalars as arrays when RLE compression is enabled.");
518  }
519  if (map) {
520  buf += snprintf(buf, MAX_BUFFER, " \"%sMap\": {", name);
521  buf += put_map(buf, map);
522  buf += put_str(buf, "},\n");
523  }
524  buf += snprintf(buf, MAX_BUFFER, " \"%s\": [", name);
525  for (r = 0; r < mi_rows; ++r) {
526  *(buf++) = '[';
527  for (c = 0; c < mi_cols; ++c) {
528  insp_mi_data *mi = &frame_data.mi_grid[r * mi_cols + c];
529  int16_t *v = (int16_t *)(((int8_t *)mi) + offset);
530  if (len == 0) {
531  buf += put_num(buf, 0, v[0], 0);
532  } else {
533  buf += put_str(buf, "[");
534  for (i = 0; i < len; i++) {
535  buf += put_num(buf, 0, v[i], 0);
536  if (i < len - 1) {
537  buf += put_str(buf, ",");
538  }
539  }
540  buf += put_str(buf, "]");
541  }
542  if (compress) { // RLE
543  for (t = c + 1; t < mi_cols; ++t) {
544  insp_mi_data *next_mi = &frame_data.mi_grid[r * mi_cols + t];
545  int16_t *nv = (int16_t *)(((int8_t *)next_mi) + offset);
546  int same = 0;
547  if (len == 0) {
548  same = v[0] == nv[0];
549  } else {
550  for (i = 0; i < len; i++) {
551  same = v[i] == nv[i];
552  if (!same) {
553  break;
554  }
555  }
556  }
557  if (!same) {
558  break;
559  }
560  }
561  if (t - c > 1) {
562  *(buf++) = ',';
563  buf += put_num(buf, '[', t - c - 1, ']');
564  c = t - 1;
565  }
566  }
567  if (c < mi_cols - 1) *(buf++) = ',';
568  }
569  *(buf++) = ']';
570  if (r < mi_rows - 1) *(buf++) = ',';
571  }
572  buf += put_str(buf, "],\n");
573  return (int)(buf - buffer);
574 }
575 
576 #if CONFIG_ACCOUNTING
577 static int put_accounting(char *buffer) {
578  char *buf = buffer;
579  int i;
580  const Accounting *accounting = frame_data.accounting;
581  if (accounting == NULL) {
582  printf("XXX\n");
583  return 0;
584  }
585  const int num_syms = accounting->syms.num_syms;
586  const int num_strs = accounting->syms.dictionary.num_strs;
587  buf += put_str(buf, " \"symbolsMap\": [");
588  for (i = 0; i < num_strs; i++) {
589  buf += snprintf(buf, MAX_BUFFER, "\"%s\"",
590  accounting->syms.dictionary.strs[i]);
591  if (i < num_strs - 1) *(buf++) = ',';
592  }
593  buf += put_str(buf, "],\n");
594  buf += put_str(buf, " \"symbols\": [\n ");
595  AccountingSymbolContext context;
596  context.x = -2;
597  context.y = -2;
598  AccountingSymbol *sym;
599  for (i = 0; i < num_syms; i++) {
600  sym = &accounting->syms.syms[i];
601  if (memcmp(&context, &sym->context, sizeof(AccountingSymbolContext)) != 0) {
602  buf += put_num(buf, '[', sym->context.x, 0);
603  buf += put_num(buf, ',', sym->context.y, ']');
604  } else {
605  buf += put_num(buf, '[', sym->id, 0);
606  buf += put_num(buf, ',', sym->bits, 0);
607  buf += put_num(buf, ',', sym->samples, ']');
608  }
609  context = sym->context;
610  if (i < num_syms - 1) *(buf++) = ',';
611  }
612  buf += put_str(buf, "],\n");
613  return (int)(buf - buffer);
614 }
615 #endif
616 
617 int skip_non_transform = 0;
618 
619 static void inspect(void *pbi, void *data) {
620  /* Fetch frame data. */
621  ifd_inspect(&frame_data, pbi, skip_non_transform);
622 
623  // Show existing frames just show a reference buffer we've already decoded.
624  // There's no information to show.
625  if (frame_data.show_existing_frame) return;
626 
627  (void)data;
628  // We allocate enough space and hope we don't write out of bounds. Totally
629  // unsafe but this speeds things up, especially when compiled to Javascript.
630  char *buffer = malloc(MAX_BUFFER);
631  if (!buffer) {
632  fprintf(stderr, "Error allocating inspect info buffer\n");
633  abort();
634  }
635  char *buf = buffer;
636  buf += put_str(buf, "{\n");
637  if (layers & BLOCK_SIZE_LAYER) {
638  buf += put_block_info(buf, block_size_map, "blockSize",
639  offsetof(insp_mi_data, bsize), 0);
640  }
641  if (layers & TRANSFORM_SIZE_LAYER) {
642  buf += put_block_info(buf, tx_size_map, "transformSize",
643  offsetof(insp_mi_data, tx_size), 0);
644  }
645  if (layers & TRANSFORM_TYPE_LAYER) {
646  buf += put_block_info(buf, tx_type_map, "transformType",
647  offsetof(insp_mi_data, tx_type), 0);
648  }
649  if (layers & DUAL_FILTER_LAYER) {
650  buf += put_block_info(buf, dual_filter_map, "dualFilterType",
651  offsetof(insp_mi_data, dual_filter_type), 0);
652  }
653  if (layers & MODE_LAYER) {
654  buf += put_block_info(buf, prediction_mode_map, "mode",
655  offsetof(insp_mi_data, mode), 0);
656  }
657  if (layers & UV_MODE_LAYER) {
658  buf += put_block_info(buf, uv_prediction_mode_map, "uv_mode",
659  offsetof(insp_mi_data, uv_mode), 0);
660  }
661  if (layers & MOTION_MODE_LAYER) {
662  buf += put_block_info(buf, motion_mode_map, "motion_mode",
663  offsetof(insp_mi_data, motion_mode), 0);
664  }
665  if (layers & COMPOUND_TYPE_LAYER) {
666  buf += put_block_info(buf, compound_type_map, "compound_type",
667  offsetof(insp_mi_data, compound_type), 0);
668  }
669  if (layers & SKIP_LAYER) {
670  buf +=
671  put_block_info(buf, skip_map, "skip", offsetof(insp_mi_data, skip), 0);
672  }
673  if (layers & FILTER_LAYER) {
674  buf +=
675  put_block_info(buf, NULL, "filter", offsetof(insp_mi_data, filter), 2);
676  }
677  if (layers & CDEF_LAYER) {
678  buf += put_block_info(buf, NULL, "cdef_level",
679  offsetof(insp_mi_data, cdef_level), 0);
680  buf += put_block_info(buf, NULL, "cdef_strength",
681  offsetof(insp_mi_data, cdef_strength), 0);
682  }
683  if (layers & CFL_LAYER) {
684  buf += put_block_info(buf, NULL, "cfl_alpha_idx",
685  offsetof(insp_mi_data, cfl_alpha_idx), 0);
686  buf += put_block_info(buf, NULL, "cfl_alpha_sign",
687  offsetof(insp_mi_data, cfl_alpha_sign), 0);
688  }
689  if (layers & Q_INDEX_LAYER) {
690  buf += put_block_info(buf, NULL, "delta_q",
691  offsetof(insp_mi_data, current_qindex), 0);
692  }
693  if (layers & SEGMENT_ID_LAYER) {
694  buf += put_block_info(buf, NULL, "seg_id",
695  offsetof(insp_mi_data, segment_id), 0);
696  }
697  if (layers & MOTION_VECTORS_LAYER) {
698  buf += put_motion_vectors(buf);
699  }
700  if (layers & INTRABC_LAYER) {
701  buf += put_block_info(buf, intrabc_map, "intrabc",
702  offsetof(insp_mi_data, intrabc), 0);
703  }
704  if (layers & PALETTE_LAYER) {
705  buf += put_block_info(buf, palette_map, "palette",
706  offsetof(insp_mi_data, palette), 0);
707  }
708  if (layers & UV_PALETTE_LAYER) {
709  buf += put_block_info(buf, palette_map, "uv_palette",
710  offsetof(insp_mi_data, uv_palette), 0);
711  }
712  if (layers & BITS_LAYER) {
713  buf += put_block_info(buf, NULL, "sb_bits", offsetof(insp_mi_data, sb_bits),
714  0);
715  }
716  if (combined_parm_count > 0) buf += put_combined(buf);
717  if (layers & REFERENCE_FRAME_LAYER) {
718  buf += put_block_info(buf, refs_map, "referenceFrame",
719  offsetof(insp_mi_data, ref_frame), 2);
720  }
721 #if CONFIG_ACCOUNTING
722  if (layers & ACCOUNTING_LAYER) {
723  buf += put_accounting(buf);
724  }
725 #endif
726  buf +=
727  snprintf(buf, MAX_BUFFER, " \"frame\": %d,\n", frame_data.frame_number);
728  buf += snprintf(buf, MAX_BUFFER, " \"showFrame\": %d,\n",
729  frame_data.show_frame);
730  buf += snprintf(buf, MAX_BUFFER, " \"frameType\": %d,\n",
731  frame_data.frame_type);
732  buf += snprintf(buf, MAX_BUFFER, " \"baseQIndex\": %d,\n",
733  frame_data.base_qindex);
734  buf += snprintf(buf, MAX_BUFFER, " \"tileCols\": %d,\n",
735  frame_data.tile_mi_cols);
736  buf += snprintf(buf, MAX_BUFFER, " \"tileRows\": %d,\n",
737  frame_data.tile_mi_rows);
738  buf += snprintf(buf, MAX_BUFFER, " \"deltaQPresentFlag\": %d,\n",
739  frame_data.delta_q_present_flag);
740  buf += snprintf(buf, MAX_BUFFER, " \"deltaQRes\": %d,\n",
741  frame_data.delta_q_res);
742  buf += put_str(buf, " \"config\": {");
743  buf += put_map(buf, config_map);
744  buf += put_str(buf, "},\n");
745  buf += put_str(buf, " \"configString\": \"");
746  buf += put_str_with_escape(buf, aom_codec_build_config());
747  buf += put_str(buf, "\"\n");
748  decoded_frame_count++;
749  buf += put_str(buf, "},\n");
750  *(buf++) = 0;
751  on_frame_decoded_dump(buffer);
752  free(buffer);
753 }
754 
755 static void ifd_init_cb(void) {
756  aom_inspect_init ii;
757  ii.inspect_cb = inspect;
758  ii.inspect_ctx = NULL;
760 }
761 
762 EMSCRIPTEN_KEEPALIVE int open_file(char *file);
763 
764 EMSCRIPTEN_KEEPALIVE
765 int open_file(char *file) {
766  if (file == NULL) {
767  // The JS analyzer puts the .ivf file at this location.
768  file = "/tmp/input.ivf";
769  }
770  reader = aom_video_reader_open(file);
771  if (!reader) die("Failed to open %s for reading.", file);
772  info = aom_video_reader_get_info(reader);
773  aom_codec_iface_t *decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
774  if (!decoder) die("Unknown input codec.");
775  fprintf(stderr, "Using %s\n", aom_codec_iface_name(decoder));
776  if (aom_codec_dec_init(&codec, decoder, NULL, 0))
777  die("Failed to initialize decoder.");
778  ifd_init(&frame_data, info->frame_width, info->frame_height);
779  ifd_init_cb();
780  return EXIT_SUCCESS;
781 }
782 
783 Av1DecodeReturn adr;
784 int have_frame = 0;
785 const unsigned char *frame;
786 const unsigned char *end_frame;
787 size_t frame_size = 0;
788 struct av1_ref_frame ref_dec;
789 
790 EMSCRIPTEN_KEEPALIVE int read_frame(void);
791 
792 EMSCRIPTEN_KEEPALIVE
793 int read_frame(void) {
794  img = NULL;
795 
796  // This loop skips over any frames that are show_existing_frames, as
797  // there is nothing to analyze.
798  do {
799  if (!have_frame) {
800  if (!aom_video_reader_read_frame(reader)) return EXIT_FAILURE;
801  frame = aom_video_reader_get_frame(reader, &frame_size);
802 
803  have_frame = 1;
804  end_frame = frame + frame_size;
805  }
806 
807  if (aom_codec_decode(&codec, frame, (unsigned int)frame_size, &adr) !=
808  AOM_CODEC_OK) {
809  die_codec(&codec, "Failed to decode frame.");
810  }
811 
812  frame = adr.buf;
813  frame_size = end_frame - frame;
814  if (frame == end_frame) have_frame = 0;
815  } while (adr.show_existing);
816 
817  int got_any_frames = 0;
818  aom_image_t *frame_img;
819  ref_dec.idx = adr.idx;
820 
821  // ref_dec.idx is the index to the reference buffer idx to AV1_GET_REFERENCE
822  // if its -1 the decoder didn't update any reference buffer and the only
823  // way to see the frame is aom_codec_get_frame.
824  if (ref_dec.idx == -1) {
825  aom_codec_iter_t iter = NULL;
826  img = frame_img = aom_codec_get_frame(&codec, &iter);
827  ++frame_count;
828  got_any_frames = 1;
829  } else if (!aom_codec_control(&codec, AV1_GET_REFERENCE, &ref_dec)) {
830  img = frame_img = &ref_dec.img;
831  ++frame_count;
832  got_any_frames = 1;
833  }
834  if (!got_any_frames) {
835  return EXIT_FAILURE;
836  }
837  return EXIT_SUCCESS;
838 }
839 
840 EMSCRIPTEN_KEEPALIVE const char *get_aom_codec_build_config(void);
841 
842 EMSCRIPTEN_KEEPALIVE
843 const char *get_aom_codec_build_config(void) {
844  return aom_codec_build_config();
845 }
846 
847 EMSCRIPTEN_KEEPALIVE int get_bit_depth(void);
848 
849 EMSCRIPTEN_KEEPALIVE
850 int get_bit_depth(void) { return img->bit_depth; }
851 
852 EMSCRIPTEN_KEEPALIVE int get_bits_per_sample(void);
853 
854 EMSCRIPTEN_KEEPALIVE
855 int get_bits_per_sample(void) { return img->bps; }
856 
857 EMSCRIPTEN_KEEPALIVE int get_image_format(void);
858 
859 EMSCRIPTEN_KEEPALIVE
860 int get_image_format(void) { return img->fmt; }
861 
862 EMSCRIPTEN_KEEPALIVE unsigned char *get_plane(int plane);
863 
864 EMSCRIPTEN_KEEPALIVE
865 unsigned char *get_plane(int plane) { return img->planes[plane]; }
866 
867 EMSCRIPTEN_KEEPALIVE int get_plane_stride(int plane);
868 
869 EMSCRIPTEN_KEEPALIVE
870 int get_plane_stride(int plane) { return img->stride[plane]; }
871 
872 EMSCRIPTEN_KEEPALIVE int get_plane_width(int plane);
873 
874 EMSCRIPTEN_KEEPALIVE
875 int get_plane_width(int plane) { return aom_img_plane_width(img, plane); }
876 
877 EMSCRIPTEN_KEEPALIVE int get_plane_height(int plane);
878 
879 EMSCRIPTEN_KEEPALIVE
880 int get_plane_height(int plane) { return aom_img_plane_height(img, plane); }
881 
882 EMSCRIPTEN_KEEPALIVE int get_frame_width(void);
883 
884 EMSCRIPTEN_KEEPALIVE
885 int get_frame_width(void) { return info->frame_width; }
886 
887 EMSCRIPTEN_KEEPALIVE int get_frame_height(void);
888 
889 EMSCRIPTEN_KEEPALIVE
890 int get_frame_height(void) { return info->frame_height; }
891 
892 static void parse_args(char **argv) {
893  char **argi, **argj;
894  struct arg arg;
895  (void)dump_accounting_arg;
896  (void)dump_cdef_arg;
897  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
898  arg.argv_step = 1;
899  if (arg_match(&arg, &dump_block_size_arg, argi)) layers |= BLOCK_SIZE_LAYER;
900 #if CONFIG_ACCOUNTING
901  else if (arg_match(&arg, &dump_accounting_arg, argi))
902  layers |= ACCOUNTING_LAYER;
903 #endif
904  else if (arg_match(&arg, &dump_transform_size_arg, argi))
905  layers |= TRANSFORM_SIZE_LAYER;
906  else if (arg_match(&arg, &dump_transform_type_arg, argi))
907  layers |= TRANSFORM_TYPE_LAYER;
908  else if (arg_match(&arg, &dump_mode_arg, argi))
909  layers |= MODE_LAYER;
910  else if (arg_match(&arg, &dump_uv_mode_arg, argi))
911  layers |= UV_MODE_LAYER;
912  else if (arg_match(&arg, &dump_motion_mode_arg, argi))
913  layers |= MOTION_MODE_LAYER;
914  else if (arg_match(&arg, &dump_compound_type_arg, argi))
915  layers |= COMPOUND_TYPE_LAYER;
916  else if (arg_match(&arg, &dump_skip_arg, argi))
917  layers |= SKIP_LAYER;
918  else if (arg_match(&arg, &dump_filter_arg, argi))
919  layers |= FILTER_LAYER;
920  else if (arg_match(&arg, &dump_cdef_arg, argi))
921  layers |= CDEF_LAYER;
922  else if (arg_match(&arg, &dump_cfl_arg, argi))
923  layers |= CFL_LAYER;
924  else if (arg_match(&arg, &dump_reference_frame_arg, argi))
925  layers |= REFERENCE_FRAME_LAYER;
926  else if (arg_match(&arg, &dump_motion_vectors_arg, argi))
927  layers |= MOTION_VECTORS_LAYER;
928  else if (arg_match(&arg, &dump_dual_filter_type_arg, argi))
929  layers |= DUAL_FILTER_LAYER;
930  else if (arg_match(&arg, &dump_delta_q_arg, argi))
931  layers |= Q_INDEX_LAYER;
932  else if (arg_match(&arg, &dump_seg_id_arg, argi))
933  layers |= SEGMENT_ID_LAYER;
934  else if (arg_match(&arg, &dump_intrabc_arg, argi))
935  layers |= INTRABC_LAYER;
936  else if (arg_match(&arg, &dump_palette_arg, argi))
937  layers |= PALETTE_LAYER;
938  else if (arg_match(&arg, &dump_uv_palette_arg, argi))
939  layers |= UV_PALETTE_LAYER;
940  else if (arg_match(&arg, &dump_bits_arg, argi))
941  layers |= BITS_LAYER;
942  else if (arg_match(&arg, &dump_all_arg, argi))
943  layers |= ALL_LAYERS;
944  else if (arg_match(&arg, &compress_arg, argi))
945  compress = 1;
946  else if (arg_match(&arg, &usage_arg, argi))
947  usage_exit();
948  else if (arg_match(&arg, &limit_arg, argi))
949  stop_after = arg_parse_uint(&arg);
950  else if (arg_match(&arg, &skip_non_transform_arg, argi))
951  skip_non_transform = arg_parse_uint(&arg);
952  else if (arg_match(&arg, &combined_arg, argi))
953  convert_to_indices(
954  (char *)arg.val, combined_parm_list,
955  sizeof(combined_parm_list) / sizeof(combined_parm_list[0]),
956  &combined_parm_count);
957  else
958  argj++;
959  }
960 }
961 
962 static const char *exec_name;
963 
964 void usage_exit(void) {
965  fprintf(stderr, "Usage: %s src_filename <options>\n", exec_name);
966  fprintf(stderr, "\nOptions:\n");
967  arg_show_usage(stderr, main_args);
968  exit(EXIT_FAILURE);
969 }
970 
971 EMSCRIPTEN_KEEPALIVE
972 int main(int argc, char **argv) {
973  exec_name = argv[0];
974  parse_args(argv);
975  if (argc >= 2) {
976  open_file(argv[1]);
977  printf("[\n");
978  while (1) {
979  if (stop_after && (decoded_frame_count >= stop_after)) break;
980  if (read_frame()) break;
981  }
982  printf("null\n");
983  printf("]");
984  } else {
985  usage_exit();
986  }
987 }
988 
989 EMSCRIPTEN_KEEPALIVE void quit(void);
990 
991 EMSCRIPTEN_KEEPALIVE
992 void quit(void) {
993  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
994  aom_video_reader_close(reader);
995 }
996 
997 EMSCRIPTEN_KEEPALIVE void set_layers(LayerType v);
998 
999 EMSCRIPTEN_KEEPALIVE
1000 void set_layers(LayerType v) { layers = v; }
1001 
1002 EMSCRIPTEN_KEEPALIVE void set_compress(int v);
1003 
1004 EMSCRIPTEN_KEEPALIVE
1005 void set_compress(int v) { compress = v; }
Codec control function to get a pointer to a reference frame.
Definition: aom.h:51
Operation completed without error.
Definition: aom_codec.h:157
int show_existing
Definition: aomdx.h:84
aom_inspect_cb inspect_cb
Definition: aomdx.h:66
aom_image_t * aom_codec_get_frame(aom_codec_ctx_t *ctx, aom_codec_iter_t *iter)
Decoded frames iterator.
unsigned char * planes[3]
Definition: aom_image.h:244
int idx
Definition: aomdx.h:82
Codec context structure.
Definition: aom_codec.h:315
const unsigned char * buf
Definition: aomdx.h:80
Describes the decoder algorithm interface to applications.
Image Descriptor.
Definition: aom_image.h:211
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.
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.
Codec control function to set an aom_inspect_cb callback that is invoked each time a frame is decoded...
Definition: aomdx.h:382
int aom_img_plane_width(const aom_image_t *img, int plane)
Get the width of a plane.
void * inspect_ctx
Definition: aomdx.h:69
const char * aom_codec_build_config(void)
Return the build configuration.
AV1 specific reference frame data struct.
Definition: aom.h:89
int aom_img_plane_height(const aom_image_t *img, int plane)
Get the height of a plane.
Structure to hold inspection callback and context.
Definition: aomdx.h:64
const void * aom_codec_iter_t
Iterator.
Definition: aom_codec.h:305
int bps
Definition: aom_image.h:253
Provides definitions for using AOM or AV1 within the aom Decoder interface.
int stride[3]
Definition: aom_image.h:250
Structure to collect a buffer index when inspecting.
Definition: aomdx.h:78
unsigned int bit_depth
Definition: aom_image.h:223
aom_codec_err_t aom_codec_control(aom_codec_ctx_t *ctx, int ctrl_id,...)
Algorithm Control.
aom_img_fmt_t fmt
Definition: aom_image.h:212
struct Accounting Accounting
Definition: aomdx.h:50