Add inspect CLI tool and Emscripten build tools.

Change-Id: Id9159456084582b393c66c1b2c0bbb0a783350b0
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c974a20..3ec2bfa 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -229,6 +229,14 @@
                $<TARGET_OBJECTS:aom_common_app_util>
                $<TARGET_OBJECTS:aom_decoder_app_util>)
 
+if (CONFIG_INSPECTION)
+add_executable(inspect
+               "${AOM_ROOT}/examples/inspect.c"
+               $<TARGET_OBJECTS:aom_common_app_util>
+               $<TARGET_OBJECTS:aom_decoder_app_util>)
+set(AOM_APP_TARGETS ${AOM_APP_TARGETS} inspect)
+endif ()
+
 add_executable(simple_encoder
                "${AOM_ROOT}/examples/simple_encoder.c"
                $<TARGET_OBJECTS:aom_common_app_util>
diff --git a/build/make/configure.sh b/build/make/configure.sh
index c6a011b..1fee74b 100644
--- a/build/make/configure.sh
+++ b/build/make/configure.sh
@@ -456,7 +456,7 @@
 
 CFLAGS  = ${CFLAGS}
 CXXFLAGS  = ${CXXFLAGS}
-ARFLAGS = -crs\$(if \$(quiet),,v)
+ARFLAGS = crs\$(if \$(quiet),,v)
 LDFLAGS = ${LDFLAGS}
 ASFLAGS = ${ASFLAGS}
 extralibs = ${extralibs}
diff --git a/examples.mk b/examples.mk
index 602b2a2..b596045 100644
--- a/examples.mk
+++ b/examples.mk
@@ -111,6 +111,20 @@
 aomenc.GUID                  = 548DEC74-7A15-4B2B-AFC3-AA102E7C25C1
 aomenc.DESCRIPTION           = Full featured encoder
 
+ifeq ($(CONFIG_INSPECTION),yes)
+EXAMPLES-$(CONFIG_DECODERS) += inspect.c
+inspect.GUID                 = FA46A420-3356-441F-B0FD-60AA1345C181
+inspect.SRCS                += ivfdec.h ivfdec.c
+inspect.SRCS                += args.c args.h
+inspect.SRCS                += tools_common.h tools_common.c
+inspect.SRCS                += video_common.h
+inspect.SRCS                += video_reader.h video_reader.c
+inspect.SRCS                += aom_ports/mem_ops.h
+inspect.SRCS                += aom_ports/mem_ops_aligned.h
+inspect.SRCS                += aom_ports/msvc.h
+inspect.DESCRIPTION          = Dump inspection data
+endif
+
 EXAMPLES-$(CONFIG_DECODERS)        += simple_decoder.c
 simple_decoder.GUID                 = D3BBF1E9-2427-450D-BBFF-B2843C1D44CC
 simple_decoder.SRCS                += ivfdec.h ivfdec.c
diff --git a/examples/inspect.c b/examples/inspect.c
new file mode 100644
index 0000000..75263c8
--- /dev/null
+++ b/examples/inspect.c
@@ -0,0 +1,615 @@
+/*
+ * Copyright (c) 2016, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+
+// Inspect Decoder
+// ================
+//
+// This is a simple decoder loop that writes JSON stats to stdout. This tool
+// can also be compiled with Emscripten and used as a library.
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "./args.h"
+#ifdef __EMSCRIPTEN__
+#include <emscripten.h>
+#else
+#define EMSCRIPTEN_KEEPALIVE
+#endif
+
+#include "aom/aom_decoder.h"
+#include "aom/aomdx.h"
+
+#include "../tools_common.h"
+#include "../video_reader.h"
+#include "./aom_config.h"
+// #include "av1/av1_dx_iface.c"
+#include "../av1/common/onyxc_int.h"
+#if CONFIG_ACCOUNTING
+#include "../av1/common/accounting.h"
+#endif
+#include "../av1/decoder/inspection.h"
+
+#include "../video_common.h"
+
+// Max JSON buffer size.
+const int MAX_BUFFER = 1024 * 1024 * 32;
+
+typedef enum {
+  ACCOUNTING_LAYER = 1,
+  BLOCK_SIZE_LAYER = 1 << 1,
+  TRANSFORM_SIZE_LAYER = 1 << 2,
+  TRANSFORM_TYPE_LAYER = 1 << 3,
+  MODE_LAYER = 1 << 4,
+  SKIP_LAYER = 1 << 5,
+  FILTER_LAYER = 1 << 6,
+  CDEF_LAYER = 1 << 7,
+  REFERENCE_FRAME_LAYER = 1 << 8,
+  MOTION_VECTORS_LAYER = 1 << 9,
+  ALL_LAYERS = (1 << 10) - 1
+} LayerType;
+
+static LayerType layers = 0;
+
+static int stop_after = 0;
+
+static const arg_def_t limit_arg =
+    ARG_DEF(NULL, "limit", 1, "Stop decoding after n frames");
+static const arg_def_t dump_all_arg = ARG_DEF("A", "all", 0, "Dump All");
+static const arg_def_t dump_accounting_arg =
+    ARG_DEF("a", "accounting", 0, "Dump Accounting");
+static const arg_def_t dump_block_size_arg =
+    ARG_DEF("bs", "blockSize", 0, "Dump Block Size");
+static const arg_def_t dump_motion_vectors_arg =
+    ARG_DEF("mv", "motionVectors", 0, "Dump Motion Vectors");
+static const arg_def_t dump_transform_size_arg =
+    ARG_DEF("ts", "transformSize", 0, "Dump Transform Size");
+static const arg_def_t dump_transform_type_arg =
+    ARG_DEF("tt", "transformType", 0, "Dump Transform Type");
+static const arg_def_t dump_mode_arg = ARG_DEF("m", "mode", 0, "Dump Mode");
+static const arg_def_t dump_skip_arg = ARG_DEF("s", "skip", 0, "Dump Skip");
+static const arg_def_t dump_filter_arg =
+    ARG_DEF("f", "filter", 0, "Dump Filter");
+static const arg_def_t dump_cdef_arg = ARG_DEF("c", "cdef", 0, "Dump CDEF");
+static const arg_def_t dump_reference_frame_arg =
+    ARG_DEF("r", "referenceFrame", 0, "Dump Reference Frame");
+static const arg_def_t usage_arg = ARG_DEF("h", "help", 0, "Help");
+
+static const arg_def_t *main_args[] = { &limit_arg,
+                                        &dump_all_arg,
+#if CONFIG_ACCOUNTING
+                                        &dump_accounting_arg,
+#endif
+                                        &dump_block_size_arg,
+                                        &dump_transform_size_arg,
+                                        &dump_transform_type_arg,
+                                        &dump_mode_arg,
+                                        &dump_skip_arg,
+                                        &dump_filter_arg,
+                                        &dump_cdef_arg,
+                                        &dump_reference_frame_arg,
+                                        &dump_motion_vectors_arg,
+                                        &usage_arg,
+                                        NULL };
+#define ENUM(name) \
+  { #name, name }
+#define LAST_ENUM \
+  { NULL, 0 }
+typedef struct map_entry {
+  const char *name;
+  int value;
+} map_entry;
+
+const map_entry refs_map[] = { ENUM(INTRA_FRAME),  ENUM(LAST_FRAME),
+#if CONFIG_EXT_REFS
+                               ENUM(LAST2_FRAME),  ENUM(LAST3_FRAME),
+                               ENUM(GOLDEN_FRAME), ENUM(BWDREF_FRAME),
+                               ENUM(ALTREF_FRAME),
+#else
+                               ENUM(GOLDEN_FRAME), ENUM(ALTREF_FRAME),
+#endif
+                               LAST_ENUM };
+
+const map_entry block_size_map[] = {
+#if CONFIG_CB4X4
+  ENUM(BLOCK_2X2),    ENUM(BLOCK_2X4),    ENUM(BLOCK_4X2),
+#endif
+  ENUM(BLOCK_4X4),    ENUM(BLOCK_4X8),    ENUM(BLOCK_8X4),
+  ENUM(BLOCK_8X8),    ENUM(BLOCK_8X16),   ENUM(BLOCK_16X8),
+  ENUM(BLOCK_16X16),  ENUM(BLOCK_16X32),  ENUM(BLOCK_32X16),
+  ENUM(BLOCK_32X32),  ENUM(BLOCK_32X64),  ENUM(BLOCK_64X32),
+  ENUM(BLOCK_64X64),
+#if CONFIG_EXT_PARTITION
+  ENUM(BLOCK_64X128), ENUM(BLOCK_128X64), ENUM(BLOCK_128X128),
+#endif
+  LAST_ENUM
+};
+
+const map_entry tx_size_map[] = {
+#if CONFIG_CB4X4
+  ENUM(TX_2X2),
+#endif
+  ENUM(TX_4X4),   ENUM(TX_8X8),   ENUM(TX_16X16), ENUM(TX_32X32),
+#if CONFIG_TX64X64
+  ENUM(TX_64X64),
+#endif
+  ENUM(TX_4X8),   ENUM(TX_8X4),   ENUM(TX_8X16),  ENUM(TX_16X8),
+  ENUM(TX_16X32), ENUM(TX_32X16), ENUM(TX_4X16),  ENUM(TX_16X4),
+  ENUM(TX_8X32),  ENUM(TX_32X8),  LAST_ENUM
+};
+
+const map_entry tx_type_map[] = { ENUM(DCT_DCT),
+                                  ENUM(ADST_DCT),
+                                  ENUM(DCT_ADST),
+                                  ENUM(ADST_ADST),
+#if CONFIG_EXT_TX
+                                  ENUM(FLIPADST_DCT),
+                                  ENUM(DCT_FLIPADST),
+                                  ENUM(FLIPADST_FLIPADST),
+                                  ENUM(ADST_FLIPADST),
+                                  ENUM(FLIPADST_ADST),
+                                  ENUM(IDTX),
+                                  ENUM(V_DCT),
+                                  ENUM(H_DCT),
+                                  ENUM(V_ADST),
+                                  ENUM(H_ADST),
+                                  ENUM(V_FLIPADST),
+                                  ENUM(H_FLIPADST),
+#endif
+                                  LAST_ENUM };
+
+const map_entry prediction_mode_map[] = { ENUM(DC_PRED),
+                                          ENUM(V_PRED),
+                                          ENUM(H_PRED),
+                                          ENUM(D45_PRED),
+                                          ENUM(D135_PRED),
+                                          ENUM(D117_PRED),
+                                          ENUM(D153_PRED),
+                                          ENUM(D207_PRED),
+                                          ENUM(D63_PRED),
+#if CONFIG_ALT_INTRA
+                                          ENUM(SMOOTH_PRED),
+#endif
+                                          ENUM(TM_PRED),
+                                          ENUM(NEARESTMV),
+                                          ENUM(NEARMV),
+                                          ENUM(ZEROMV),
+                                          ENUM(NEWMV),
+#if CONFIG_EXT_INTER
+                                          ENUM(NEWFROMNEARMV),
+                                          ENUM(NEAREST_NEARESTMV),
+                                          ENUM(NEAREST_NEARMV),
+                                          ENUM(NEAR_NEARESTMV),
+                                          ENUM(NEAR_NEARMV),
+                                          ENUM(NEAREST_NEWMV),
+                                          ENUM(NEW_NEARESTMV),
+                                          ENUM(NEAR_NEWMV),
+                                          ENUM(NEW_NEARMV),
+                                          ENUM(ZERO_ZEROMV),
+                                          ENUM(NEW_NEWMV),
+#endif
+                                          LAST_ENUM };
+
+#define NO_SKIP 0
+#define SKIP 1
+
+const map_entry skip_map[] = { ENUM(SKIP), ENUM(NO_SKIP), LAST_ENUM };
+
+const map_entry config_map[] = { ENUM(MI_SIZE), LAST_ENUM };
+
+static const char *exec_name;
+
+insp_frame_data frame_data;
+int frame_count = 0;
+int decoded_frame_count = 0;
+aom_codec_ctx_t codec;
+AvxVideoReader *reader = NULL;
+const AvxVideoInfo *info = NULL;
+aom_image_t *img = NULL;
+
+void on_frame_decoded_dump(char *json) {
+#ifdef __EMSCRIPTEN__
+  EM_ASM_({ Module.on_frame_decoded_json($0); }, json);
+#else
+  printf("%s", json);
+#endif
+}
+
+// Writing out the JSON buffer using snprintf is very slow, especially when
+// compiled with emscripten, these functions speed things up quite a bit.
+int put_str(char *buffer, const char *str) {
+  int i;
+  for (i = 0; str[i] != '\0'; i++) {
+    buffer[i] = str[i];
+  }
+  return i;
+}
+
+int put_num(char *buffer, char prefix, int num, char suffix) {
+  int i = 0;
+  char *buf = buffer;
+  int is_neg = 0;
+  if (prefix) {
+    buf[i++] = prefix;
+  }
+  if (num == 0) {
+    buf[i++] = '0';
+  } else {
+    if (num < 0) {
+      num = -num;
+      is_neg = 1;
+    }
+    int s = i;
+    while (num != 0) {
+      buf[i++] = '0' + (num % 10);
+      num = num / 10;
+    }
+    if (is_neg) {
+      buf[i++] = '-';
+    }
+    int e = i - 1;
+    while (s < e) {
+      int t = buf[s];
+      buf[s] = buf[e];
+      buf[e] = t;
+      s++;
+      e--;
+    }
+  }
+  if (suffix) {
+    buf[i++] = suffix;
+  }
+  return i;
+}
+
+int put_map(char *buffer, const map_entry *map) {
+  char *buf = buffer;
+  const map_entry *entry = map;
+  while (entry->name != NULL) {
+    *(buf++) = '"';
+    buf += put_str(buf, entry->name);
+    *(buf++) = '"';
+    buf += put_num(buf, ':', entry->value, 0);
+    entry++;
+    if (entry->name != NULL) {
+      *(buf++) = ',';
+    }
+  }
+  return buf - buffer;
+}
+
+int put_reference_frame(char *buffer) {
+  const int mi_rows = frame_data.mi_rows;
+  const int mi_cols = frame_data.mi_cols;
+  char *buf = buffer;
+  int r, c;
+  buf += put_str(buf, "  \"referenceFrameMap\": {");
+  buf += put_map(buf, refs_map);
+  buf += put_str(buf, "},\n");
+  buf += put_str(buf, "  \"referenceFrame\": [");
+  for (r = 0; r < mi_rows; ++r) {
+    *(buf++) = '[';
+    for (c = 0; c < mi_cols; ++c) {
+      insp_mi_data *mi = &frame_data.mi_grid[r * mi_cols + c];
+      if (c) *(buf++) = ',';
+      buf += put_num(buf, '[', mi->ref_frame[0], 0);
+      buf += put_num(buf, ',', mi->ref_frame[1], ']');
+    }
+    *(buf++) = ']';
+    if (r < mi_rows - 1) *(buf++) = ',';
+  }
+  buf += put_str(buf, "],\n");
+  return buf - buffer;
+}
+
+int put_motion_vectors(char *buffer) {
+  const int mi_rows = frame_data.mi_rows;
+  const int mi_cols = frame_data.mi_cols;
+  char *buf = buffer;
+  int r, c;
+  buf += put_str(buf, "  \"motionVectors\": [");
+  for (r = 0; r < mi_rows; ++r) {
+    *(buf++) = '[';
+    for (c = 0; c < mi_cols; ++c) {
+      insp_mi_data *mi = &frame_data.mi_grid[r * mi_cols + c];
+      if (c) *(buf++) = ',';
+      buf += put_num(buf, '[', mi->mv[0].col, 0);
+      buf += put_num(buf, ',', mi->mv[0].row, 0);
+      buf += put_num(buf, ',', mi->mv[1].col, 0);
+      buf += put_num(buf, ',', mi->mv[1].row, ']');
+    }
+    *(buf++) = ']';
+    if (r < mi_rows - 1) *(buf++) = ',';
+  }
+  buf += put_str(buf, "],\n");
+  return buf - buffer;
+}
+
+int put_block_info(char *buffer, const map_entry *map, const char *name,
+                   size_t offset) {
+  const int mi_rows = frame_data.mi_rows;
+  const int mi_cols = frame_data.mi_cols;
+  char *buf = buffer;
+  int r, c, v;
+  if (map) {
+    buf += snprintf(buf, MAX_BUFFER, "  \"%sMap\": {", name);
+    buf += put_map(buf, map);
+    buf += put_str(buf, "},\n");
+  }
+  buf += snprintf(buf, MAX_BUFFER, "  \"%s\": [", name);
+  for (r = 0; r < mi_rows; ++r) {
+    *(buf++) = '[';
+    for (c = 0; c < mi_cols; ++c) {
+      insp_mi_data *mi = &frame_data.mi_grid[r * mi_cols + c];
+      v = *(((int8_t *)mi) + offset);
+      if (c) *(buf++) = ',';
+      buf += put_num(buf, 0, v, 0);
+    }
+    *(buf++) = ']';
+    if (r < mi_rows - 1) *(buf++) = ',';
+  }
+  buf += put_str(buf, "],\n");
+  return buf - buffer;
+}
+
+#if CONFIG_ACCOUNTING
+int put_accounting(char *buffer) {
+  char *buf = buffer;
+  int i;
+  const Accounting *accounting = frame_data.accounting;
+  if (accounting == NULL) {
+    printf("XXX\n");
+    return 0;
+  }
+  const int num_syms = accounting->syms.num_syms;
+  const int num_strs = accounting->syms.dictionary.num_strs;
+  buf += put_str(buf, "  \"symbolsMap\": [");
+  for (i = 0; i < num_strs; i++) {
+    buf += snprintf(buf, MAX_BUFFER, "\"%s\"",
+                    accounting->syms.dictionary.strs[i]);
+    if (i < num_strs - 1) *(buf++) = ',';
+  }
+  buf += put_str(buf, "],\n");
+  buf += put_str(buf, "  \"symbols\": [\n    ");
+  AccountingSymbolContext context;
+  context.x = -2;
+  context.y = -2;
+  AccountingSymbol *sym;
+  for (i = 0; i < num_syms; i++) {
+    sym = &accounting->syms.syms[i];
+    if (memcmp(&context, &sym->context, sizeof(AccountingSymbolContext)) != 0) {
+      buf += put_num(buf, '[', sym->context.x, 0);
+      buf += put_num(buf, ',', sym->context.y, ']');
+    } else {
+      buf += put_num(buf, '[', sym->id, 0);
+      buf += put_num(buf, ',', sym->bits, 0);
+      buf += put_num(buf, ',', sym->samples, ']');
+    }
+    context = sym->context;
+    if (i < num_syms - 1) *(buf++) = ',';
+  }
+  buf += put_str(buf, "],\n");
+  return buf - buffer;
+}
+#endif
+
+void inspect(void *pbi, void *data) {
+  /* Fetch frame data. */
+  ifd_inspect(&frame_data, pbi);
+  (void)data;
+  // We allocate enough space and hope we don't write out of bounds. Totally
+  // unsafe but this speeds things up, especially when compiled to Javascript.
+  char *buffer = aom_malloc(MAX_BUFFER);
+  char *buf = buffer;
+  buf += put_str(buf, "{\n");
+  if (layers & BLOCK_SIZE_LAYER) {
+    buf += put_block_info(buf, block_size_map, "blockSize",
+                          offsetof(insp_mi_data, sb_type));
+  }
+  if (layers & TRANSFORM_SIZE_LAYER) {
+    buf += put_block_info(buf, tx_size_map, "transformSize",
+                          offsetof(insp_mi_data, tx_size));
+  }
+  if (layers & TRANSFORM_TYPE_LAYER) {
+    buf += put_block_info(buf, tx_type_map, "transformType",
+                          offsetof(insp_mi_data, tx_type));
+  }
+  if (layers & MODE_LAYER) {
+    buf += put_block_info(buf, prediction_mode_map, "mode",
+                          offsetof(insp_mi_data, mode));
+  }
+  if (layers & SKIP_LAYER) {
+    buf += put_block_info(buf, skip_map, "skip", offsetof(insp_mi_data, skip));
+  }
+  if (layers & FILTER_LAYER) {
+    buf += put_block_info(buf, NULL, "filter", offsetof(insp_mi_data, filter));
+  }
+  if (layers & CDEF_LAYER) {
+    // buf += put_block_info(buf, NULL, "clpf",
+    //                         offsetof(insp_mi_data, clpf));
+    // buf += sprintf(buf, "  \"clpfStrengthY\": %d,\n",
+    // frame_data.clpf_strength_y);
+    // buf += sprintf(buf, "  \"clpfSize\": %d,\n", frame_data.clpf_size);
+  }
+  if (layers & MOTION_VECTORS_LAYER) {
+    buf += put_motion_vectors(buf);
+  }
+  if (layers & REFERENCE_FRAME_LAYER) {
+    buf += put_reference_frame(buf);
+  }
+#if CONFIG_ACCOUNTING
+  if (layers & ACCOUNTING_LAYER) {
+    buf += put_accounting(buf);
+  }
+#endif
+  buf += snprintf(buf, MAX_BUFFER, "  \"frame\": %d,\n", decoded_frame_count);
+  buf += snprintf(buf, MAX_BUFFER, "  \"showFrame\": %d,\n",
+                  frame_data.show_frame);
+  buf += snprintf(buf, MAX_BUFFER, "  \"frameType\": %d,\n",
+                  frame_data.frame_type);
+  buf += snprintf(buf, MAX_BUFFER, "  \"baseQIndex\": %d,\n",
+                  frame_data.base_qindex);
+  buf += put_str(buf, "  \"config\": {");
+  buf += put_map(buf, config_map);
+  buf += put_str(buf, "},\n");
+  buf += snprintf(buf, MAX_BUFFER, "  \"configString\": \"%s\"\n",
+                  aom_codec_build_config());
+  decoded_frame_count++;
+  buf += put_str(buf, "},\n");
+  *(buf++) = 0;
+  on_frame_decoded_dump(buffer);
+  aom_free(buffer);
+}
+
+void ifd_init_cb() {
+  aom_inspect_init ii;
+  ii.inspect_cb = inspect;
+  ii.inspect_ctx = NULL;
+  aom_codec_control(&codec, AV1_SET_INSPECTION_CALLBACK, &ii);
+}
+
+EMSCRIPTEN_KEEPALIVE
+int open_file(char *file) {
+  if (file == NULL) {
+    // The JS analyzer puts the .ivf file at this location.
+    file = "/tmp/input.ivf";
+  }
+  reader = aom_video_reader_open(file);
+  if (!reader) die("Failed to open %s for reading.", file);
+  info = aom_video_reader_get_info(reader);
+  const AvxInterface *decoder = get_aom_decoder_by_fourcc(info->codec_fourcc);
+  if (!decoder) die("Unknown input codec.");
+  fprintf(stderr, "Using %s\n",
+          aom_codec_iface_name(decoder->codec_interface()));
+  if (aom_codec_dec_init(&codec, decoder->codec_interface(), NULL, 0))
+    die_codec(&codec, "Failed to initialize decoder.");
+  ifd_init(&frame_data, info->frame_width, info->frame_height);
+  ifd_init_cb();
+  return EXIT_SUCCESS;
+}
+
+EMSCRIPTEN_KEEPALIVE
+int read_frame() {
+  if (!aom_video_reader_read_frame(reader)) return EXIT_FAILURE;
+  img = NULL;
+  aom_codec_iter_t iter = NULL;
+  size_t frame_size = 0;
+  const unsigned char *frame = aom_video_reader_get_frame(reader, &frame_size);
+  if (aom_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0) !=
+      AOM_CODEC_OK) {
+    die_codec(&codec, "Failed to decode frame.");
+  }
+  img = aom_codec_get_frame(&codec, &iter);
+  if (img == NULL) {
+    return EXIT_FAILURE;
+  }
+  ++frame_count;
+  return EXIT_SUCCESS;
+}
+
+EMSCRIPTEN_KEEPALIVE
+const char *get_aom_codec_build_config() { return aom_codec_build_config(); }
+
+EMSCRIPTEN_KEEPALIVE
+int get_bit_depth() { return img->bit_depth; }
+
+EMSCRIPTEN_KEEPALIVE
+unsigned char *get_plane(int plane) { return img->planes[plane]; }
+
+EMSCRIPTEN_KEEPALIVE
+int get_plane_stride(int plane) { return img->stride[plane]; }
+
+EMSCRIPTEN_KEEPALIVE
+int get_plane_width(int plane) { return aom_img_plane_width(img, plane); }
+
+EMSCRIPTEN_KEEPALIVE
+int get_plane_height(int plane) { return aom_img_plane_height(img, plane); }
+
+EMSCRIPTEN_KEEPALIVE
+int get_frame_width() { return info->frame_width; }
+
+EMSCRIPTEN_KEEPALIVE
+int get_frame_height() { return info->frame_height; }
+
+static void parse_args(char **argv) {
+  char **argi, **argj;
+  struct arg arg;
+  (void)dump_accounting_arg;
+  for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
+    arg.argv_step = 1;
+    if (arg_match(&arg, &dump_block_size_arg, argi)) layers |= BLOCK_SIZE_LAYER;
+#if CONFIG_ACCOUNTING
+    else if (arg_match(&arg, &dump_accounting_arg, argi))
+      layers |= ACCOUNTING_LAYER;
+#endif
+    else if (arg_match(&arg, &dump_transform_size_arg, argi))
+      layers |= TRANSFORM_SIZE_LAYER;
+    else if (arg_match(&arg, &dump_transform_type_arg, argi))
+      layers |= TRANSFORM_TYPE_LAYER;
+    else if (arg_match(&arg, &dump_mode_arg, argi))
+      layers |= MODE_LAYER;
+    else if (arg_match(&arg, &dump_skip_arg, argi))
+      layers |= SKIP_LAYER;
+    else if (arg_match(&arg, &dump_filter_arg, argi))
+      layers |= FILTER_LAYER;
+    else if (arg_match(&arg, &dump_cdef_arg, argi))
+      layers |= CDEF_LAYER;
+    else if (arg_match(&arg, &dump_reference_frame_arg, argi))
+      layers |= REFERENCE_FRAME_LAYER;
+    else if (arg_match(&arg, &dump_motion_vectors_arg, argi))
+      layers |= MOTION_VECTORS_LAYER;
+    else if (arg_match(&arg, &dump_all_arg, argi))
+      layers |= ALL_LAYERS;
+    else if (arg_match(&arg, &usage_arg, argi))
+      usage_exit();
+    else if (arg_match(&arg, &limit_arg, argi))
+      stop_after = arg_parse_uint(&arg);
+    else
+      argj++;
+  }
+}
+
+static const char *exec_name;
+
+void usage_exit(void) {
+  fprintf(stderr, "Usage: %s src_filename <options>\n", exec_name);
+  fprintf(stderr, "\nOptions:\n");
+  arg_show_usage(stderr, main_args);
+  exit(EXIT_FAILURE);
+}
+
+EMSCRIPTEN_KEEPALIVE
+int main(int argc, char **argv) {
+  exec_name = argv[0];
+  parse_args(argv);
+  if (argc >= 2) {
+    open_file(argv[1]);
+    printf("[\n");
+    while (1) {
+      if (stop_after && (decoded_frame_count >= stop_after)) break;
+      if (read_frame()) break;
+    }
+    printf("null\n");
+    printf("]");
+  } else {
+    usage_exit();
+  }
+}
+
+EMSCRIPTEN_KEEPALIVE
+void quit() {
+  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
+  aom_video_reader_close(reader);
+}
+
+EMSCRIPTEN_KEEPALIVE
+void set_layers(LayerType v) { layers = v; }
diff --git a/tools/build_inspector.sh b/tools/build_inspector.sh
new file mode 100755
index 0000000..a67aa15
--- /dev/null
+++ b/tools/build_inspector.sh
@@ -0,0 +1,16 @@
+if ! [ -x "$(command -v emcc)" ] || ! [ -x "$(command -v emconfigure)" ] || ! [ -x "$(command -v emmake)" ]; then
+  echo 'Emscripten SDK is not available (emcc, emconfigure or emmake is missing). Install it from https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html and try again.' >&2
+  exit 1
+fi
+
+echo 'Building JS Inspector'
+if [ ! -d ".inspect" ]; then
+  mkdir .inspect
+  cd .inspect && emconfigure ../../configure --disable-multithread --disable-runtime-cpu-detect --target=generic-gnu --enable-accounting --enable-inspection --enable-aom_highbitdepth --extra-cflags="-D_POSIX_SOURCE"
+fi
+
+cd .inspect
+emmake make -j 8
+cp examples/inspect inspect.bc
+emcc -O3 inspect.bc -o inspect.js -s TOTAL_MEMORY=134217728 -s MODULARIZE=1 -s EXPORT_NAME="'DecoderModule'" --post-js "../inspect-post.js" --memory-init-file 0
+cp inspect.js ../inspect.js
diff --git a/tools/inspect-cli.js b/tools/inspect-cli.js
new file mode 100644
index 0000000..a14c081
--- /dev/null
+++ b/tools/inspect-cli.js
@@ -0,0 +1,39 @@
+/**
+ * This tool lets you test if the compiled Javascript decoder is functioning properly. You'll
+ * need to download a SpiderMonkey js-shell to run this script.
+ * https://archive.mozilla.org/pub/firefox/nightly/latest-mozilla-central/
+ *
+ * Example:
+ *   js-shell inspect-cli.js video.ivf
+ */
+load("inspect.js");
+var buffer = read(scriptArgs[0], "binary");
+var Module = {
+  noExitRuntime: true,
+  noInitialRun: true,
+  preInit: [],
+  preRun: [],
+  postRun: [function () {
+    printErr(`Loaded Javascript Decoder OK`);
+  }],
+  memoryInitializerPrefixURL: "bin/",
+  arguments: ['input.ivf', 'output.raw'],
+  on_frame_decoded_json: function (jsonString) {
+    let json = JSON.parse("[" + Module.UTF8ToString(jsonString) + "null]");
+    json.forEach(frame => {
+      if (frame) {
+        print(frame.frame);
+      }
+    });
+  }
+};
+DecoderModule(Module);
+Module.FS.writeFile("/tmp/input.ivf", buffer, { encoding: "binary" });
+Module._open_file();
+Module._set_layers(0xFFFFFFFF); // Set this to zero if you want to benchmark decoding.
+while(true) {
+  printErr("Decoding Frame ...");
+  if (Module._read_frame()) {
+    break;
+  }
+}
diff --git a/tools/inspect-post.js b/tools/inspect-post.js
new file mode 100644
index 0000000..31c40bb
--- /dev/null
+++ b/tools/inspect-post.js
@@ -0,0 +1 @@
+Module["FS"] = FS;