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