From: Michael Bebenita Date: Wed, 15 Nov 2017 01:28:25 +0000 (-0800) Subject: Backport AOM inspector to VP9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f0f4ef264c3dab46402e77f305c42f2e7cc94061;p=libvpx Backport AOM inspector to VP9 Change-Id: I7f2a04ecf53e100629cafa0a0cb12ef619853568 --- diff --git a/build/make/configure.sh b/build/make/configure.sh index 262204053..daf838ce2 100644 --- a/build/make/configure.sh +++ b/build/make/configure.sh @@ -499,7 +499,7 @@ NM=${NM} CFLAGS = ${CFLAGS} CXXFLAGS = ${CXXFLAGS} -ARFLAGS = -crs\$(if \$(quiet),,v) +ARFLAGS = crs\$(if \$(quiet),,v) LDFLAGS = ${LDFLAGS} ASFLAGS = ${ASFLAGS} extralibs = ${extralibs} diff --git a/configure b/configure index e0ffb55b4..0bf7b76cb 100755 --- a/configure +++ b/configure @@ -63,6 +63,7 @@ Advanced options: enable vp9 temporal denoising ${toggle_webm_io} enable input from and output to WebM container ${toggle_libyuv} enable libyuv + ${toggle_inspection} enable bitstream inspection Codecs: Codecs can be selectively enabled or disabled individually, or by family: @@ -325,6 +326,7 @@ CONFIG_LIST=" unit_tests webm_io libyuv + inspection decode_perf_tests encode_perf_tests multi_res_encoding @@ -387,6 +389,7 @@ CMDLINE_SELECT=" unit_tests webm_io libyuv + inspection decode_perf_tests encode_perf_tests multi_res_encoding diff --git a/examples.mk b/examples.mk index 1187f147a..f871fec46 100644 --- a/examples.mk +++ b/examples.mk @@ -145,6 +145,19 @@ simple_decoder.SRCS += vpx_ports/mem_ops.h simple_decoder.SRCS += vpx_ports/mem_ops_aligned.h simple_decoder.SRCS += vpx_ports/msvc.h simple_decoder.DESCRIPTION = Simplified decoder loop +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 += vpx_ports/mem_ops.h +inspect.SRCS += vpx_ports/mem_ops_aligned.h +inspect.SRCS += vpx_ports/msvc.h +inspect.DESCRIPTION = Dump inspection data +endif EXAMPLES-$(CONFIG_DECODERS) += postproc.c postproc.SRCS += ivfdec.h ivfdec.c postproc.SRCS += tools_common.h tools_common.c diff --git a/examples/inspect.c b/examples/inspect.c new file mode 100644 index 000000000..8c132a6da --- /dev/null +++ b/examples/inspect.c @@ -0,0 +1,681 @@ +/* + * 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 +#include +#include + +#include "./args.h" +#ifdef __EMSCRIPTEN__ +#include +#else +#define EMSCRIPTEN_KEEPALIVE +#endif + +#include "vpx/vpx_decoder.h" +#include "./vpx_config.h" +#include "../vp9/decoder/inspection.h" +#if CONFIG_ACCOUNTING +#include "../av1/decoder/accounting.h" +#endif +#include "../vp9/common/vp9_enums.h" +#include "../vp9/common/vp9_blockd.h" +// #include "../vp8/common/blockd.h" +#include "vpx/vp8dx.h" + +#include "../tools_common.h" +#include "../video_reader.h" +// #include "../vp8/common/onyxc_int.h" +// #include "../vp8/encoder/onyx_int.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, + REFERENCE_FRAME_LAYER = 1 << 8, + MOTION_VECTORS_LAYER = 1 << 9, + UV_MODE_LAYER = 1 << 10, + CFL_LAYER = 1 << 11, + DUAL_FILTER_LAYER = 1 << 12, + Q_INDEX_LAYER = 1 << 13, + SEGMENT_ID_LAYER = 1 << 14, + ALL_LAYERS = (1 << 15) - 1 +} LayerType; + +static LayerType layers = 0; + +static int stop_after = 0; +static int compress = 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 compress_arg = + ARG_DEF("x", "compress", 0, "Compress JSON using RLE"); +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_uv_mode_arg = + ARG_DEF("uvm", "uv_mode", 0, "Dump UV Intra Prediction Modes"); +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_reference_frame_arg = + ARG_DEF("r", "referenceFrame", 0, "Dump Reference Frame"); +static const arg_def_t dump_delta_q_arg = + ARG_DEF("dq", "delta_q", 0, "Dump QIndex"); +static const arg_def_t dump_seg_id_arg = + ARG_DEF("si", "seg_id", 0, "Dump Segment ID"); +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, + &compress_arg, +#if CONFIG_ACCOUNTING + &dump_accounting_arg, +#endif + &dump_block_size_arg, + &dump_transform_size_arg, + &dump_transform_type_arg, + &dump_mode_arg, + &dump_uv_mode_arg, + &dump_skip_arg, + &dump_filter_arg, + &dump_reference_frame_arg, + &dump_motion_vectors_arg, + &dump_delta_q_arg, + &dump_seg_id_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), + ENUM(GOLDEN_FRAME), ENUM(ALTREF_FRAME), + LAST_ENUM }; + +const map_entry block_size_map[] = { + 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), LAST_ENUM +}; + +const map_entry tx_size_map[] = { ENUM(TX_4X4), ENUM(TX_8X8), ENUM(TX_16X16), + ENUM(TX_32X32), LAST_ENUM }; + +const map_entry tx_type_map[] = { ENUM(DCT_DCT), ENUM(ADST_DCT), ENUM(DCT_ADST), + ENUM(ADST_ADST), 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), ENUM(TM_PRED), ENUM(NEARESTMV), ENUM(NEARMV), + ENUM(ZEROMV), ENUM(NEWMV), LAST_ENUM +}; + +#define uv_prediction_mode_map prediction_mode_map + +#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; +vpx_codec_ctx_t codec; +VpxVideoReader *reader = NULL; +const VpxVideoInfo *info = NULL; +vpx_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_str_with_escape(char *buffer, const char *str) { + int i; + int j = 0; + for (i = 0; str[i] != '\0'; i++) { + if (str[i] < ' ') { + continue; + } else if (str[i] == '"' || str[i] == '\\') { + buffer[j++] = '\\'; + } + buffer[j++] = str[i]; + } + return j; +} + +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 (int)(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, t; + 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]; + buf += put_num(buf, '[', mi->ref_frame[0], 0); + buf += put_num(buf, ',', mi->ref_frame[1], ']'); + if (compress) { // RLE + for (t = c + 1; t < mi_cols; ++t) { + insp_mi_data *next_mi = &frame_data.mi_grid[r * mi_cols + t]; + if (mi->ref_frame[0] != next_mi->ref_frame[0] || + mi->ref_frame[1] != next_mi->ref_frame[1]) { + break; + } + } + if (t - c > 1) { + *(buf++) = ','; + buf += put_num(buf, '[', t - c - 1, ']'); + c = t - 1; + } + } + if (c < mi_cols - 1) *(buf++) = ','; + } + *(buf++) = ']'; + if (r < mi_rows - 1) *(buf++) = ','; + } + buf += put_str(buf, "],\n"); + return (int)(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, t; + 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]; + 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, ']'); + if (compress) { // RLE + for (t = c + 1; t < mi_cols; ++t) { + insp_mi_data *next_mi = &frame_data.mi_grid[r * mi_cols + t]; + if (mi->mv[0].col != next_mi->mv[0].col || + mi->mv[0].row != next_mi->mv[0].row || + mi->mv[1].col != next_mi->mv[1].col || + mi->mv[1].row != next_mi->mv[1].row) { + break; + } + } + if (t - c > 1) { + *(buf++) = ','; + buf += put_num(buf, '[', t - c - 1, ']'); + c = t - 1; + } + } + if (c < mi_cols - 1) *(buf++) = ','; + } + *(buf++) = ']'; + if (r < mi_rows - 1) *(buf++) = ','; + } + buf += put_str(buf, "],\n"); + return (int)(buf - buffer); +} + +int put_block_info(char *buffer, const map_entry *map, const char *name, + size_t offset, int len) { + const int mi_rows = frame_data.mi_rows; + const int mi_cols = frame_data.mi_cols; + char *buf = buffer; + int r, c, t, i; + if (compress && len == 1) { + die("Can't encode scalars as arrays when RLE compression is enabled."); + return -1; + } + 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]; + int16_t *v = (int16_t *)(((int8_t *)mi) + offset); + if (len == 0) { + buf += put_num(buf, 0, v[0], 0); + } else { + buf += put_str(buf, "["); + for (i = 0; i < len; i++) { + buf += put_num(buf, 0, v[i], 0); + if (i < len - 1) { + buf += put_str(buf, ","); + } + } + buf += put_str(buf, "]"); + } + if (compress) { // RLE + for (t = c + 1; t < mi_cols; ++t) { + insp_mi_data *next_mi = &frame_data.mi_grid[r * mi_cols + t]; + int16_t *nv = (int16_t *)(((int8_t *)next_mi) + offset); + int same = 0; + if (len == 0) { + same = v[0] == nv[0]; + } else { + for (i = 0; i < len; i++) { + same = v[i] == nv[i]; + if (!same) { + break; + } + } + } + if (!same) { + break; + } + } + if (t - c > 1) { + *(buf++) = ','; + buf += put_num(buf, '[', t - c - 1, ']'); + c = t - 1; + } + } + if (c < mi_cols - 1) *(buf++) = ','; + } + *(buf++) = ']'; + if (r < mi_rows - 1) *(buf++) = ','; + } + buf += put_str(buf, "],\n"); + return (int)(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 (int)(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 = vpx_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), 0); + } + if (layers & TRANSFORM_SIZE_LAYER) { + buf += put_block_info(buf, tx_size_map, "transformSize", + offsetof(insp_mi_data, tx_size), 0); + } + if (layers & TRANSFORM_TYPE_LAYER) { + buf += put_block_info(buf, tx_type_map, "transformType", + offsetof(insp_mi_data, tx_type), 0); + } + if (layers & MODE_LAYER) { + buf += put_block_info(buf, prediction_mode_map, "mode", + offsetof(insp_mi_data, mode), 0); + } + if (layers & UV_MODE_LAYER) { + buf += put_block_info(buf, uv_prediction_mode_map, "uv_mode", + offsetof(insp_mi_data, uv_mode), 0); + } + if (layers & SKIP_LAYER) { + buf += + put_block_info(buf, skip_map, "skip", offsetof(insp_mi_data, skip), 0); + } + if (layers & FILTER_LAYER) { + buf += + put_block_info(buf, NULL, "filter", offsetof(insp_mi_data, filter), 0); + } + if (layers & Q_INDEX_LAYER) { + buf += put_block_info(buf, NULL, "delta_q", + offsetof(insp_mi_data, current_qindex), 0); + } + if (layers & SEGMENT_ID_LAYER) { + buf += put_block_info(buf, NULL, "seg_id", + offsetof(insp_mi_data, segment_id), 0); + } + if (layers & MOTION_VECTORS_LAYER) { + buf += put_motion_vectors(buf); + } + if (layers & REFERENCE_FRAME_LAYER) { + buf += put_block_info(buf, refs_map, "referenceFrame", + offsetof(insp_mi_data, ref_frame), 2); + } +#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 += snprintf(buf, MAX_BUFFER, " \"tileCols\": %d,\n", + frame_data.tile_mi_cols); + buf += snprintf(buf, MAX_BUFFER, " \"tileRows\": %d,\n", + frame_data.tile_mi_rows); + buf += put_str(buf, " \"config\": {"); + buf += put_map(buf, config_map); + buf += put_str(buf, "},\n"); + buf += put_str(buf, " \"configString\": \""); + buf += put_str_with_escape(buf, vpx_codec_build_config()); + buf += put_str(buf, "\"\n"); + decoded_frame_count++; + buf += put_str(buf, "},\n"); + *(buf++) = 0; + on_frame_decoded_dump(buffer); + vpx_free(buffer); +} + +void ifd_init_cb() { + vpx_inspect_init ii; + ii.inspect_cb = inspect; + ii.inspect_ctx = NULL; + vpx_codec_control(&codec, VP9_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 = vpx_video_reader_open(file); + if (!reader) die("Failed to open %s for reading.", file); + info = vpx_video_reader_get_info(reader); + const VpxInterface *decoder = get_vpx_decoder_by_fourcc(info->codec_fourcc); + if (!decoder) die("Unknown input codec."); + fprintf(stderr, "Using %s\n", + vpx_codec_iface_name(decoder->codec_interface())); + if (vpx_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 (!vpx_video_reader_read_frame(reader)) return EXIT_FAILURE; + img = NULL; + vpx_codec_iter_t iter = NULL; + size_t frame_size = 0; + const unsigned char *frame = vpx_video_reader_get_frame(reader, &frame_size); + if (vpx_codec_decode(&codec, frame, (unsigned int)frame_size, NULL, 0) != + VPX_CODEC_OK) { + die_codec(&codec, "Failed to decode frame."); + } + img = vpx_codec_get_frame(&codec, &iter); + if (img == NULL) { + return EXIT_FAILURE; + } + ++frame_count; + return EXIT_SUCCESS; +} + +EMSCRIPTEN_KEEPALIVE +const char *get_codec_build_config() { return vpx_codec_build_config(); } + +EMSCRIPTEN_KEEPALIVE +int get_bit_depth() { return img->bit_depth; } + +EMSCRIPTEN_KEEPALIVE +int get_bits_per_sample() { return img->bps; } + +EMSCRIPTEN_KEEPALIVE +int get_image_format() { return img->fmt; } + +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 vpx_img_plane_width(img, plane); } + +EMSCRIPTEN_KEEPALIVE +int get_plane_height(int plane) { return vpx_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_uv_mode_arg, argi)) + layers |= UV_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_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_delta_q_arg, argi)) + layers |= Q_INDEX_LAYER; + else if (arg_match(&arg, &dump_seg_id_arg, argi)) + layers |= SEGMENT_ID_LAYER; + else if (arg_match(&arg, &dump_all_arg, argi)) + layers |= ALL_LAYERS; + else if (arg_match(&arg, &compress_arg, argi)) + compress = 1; + 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 \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 (vpx_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec"); + vpx_video_reader_close(reader); +} + +EMSCRIPTEN_KEEPALIVE +void set_layers(LayerType v) { layers = v; } + +EMSCRIPTEN_KEEPALIVE +void set_compress(int v) { compress = v; } diff --git a/tools/build_inspector.sh b/tools/build_inspector.sh new file mode 100755 index 000000000..3cceb60f3 --- /dev/null +++ b/tools/build_inspector.sh @@ -0,0 +1,48 @@ +#!/bin/sh +## +## Copyright (c) 2017, 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. +## + +if ! [ -x "$(command -v emcc)" ] \ + || ! [ -x "$(command -v emconfigure)" ] \ + || ! [ -x "$(command -v emmake)" ]; then + cat << EOF >& 2 +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. +EOF + 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 \ + --disable-docs \ + --disable-unit-tests \ + --enable-inspection \ + --extra-cflags="-D_POSIX_SOURCE" + cd .. +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-post.js b/tools/inspect-post.js new file mode 100644 index 000000000..31c40bb82 --- /dev/null +++ b/tools/inspect-post.js @@ -0,0 +1 @@ +Module["FS"] = FS; diff --git a/vp9/decoder/inspection.c b/vp9/decoder/inspection.c new file mode 100644 index 000000000..b6b38a5a0 --- /dev/null +++ b/vp9/decoder/inspection.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2017, 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. + */ +#include "vp9/decoder/vp9_decoder.h" +#include "vp9/decoder/inspection.h" +#include "vp9/common/vp9_enums.h" + +static void ifd_init_mi_rc(insp_frame_data *fd, int mi_cols, int mi_rows) { + fd->mi_cols = mi_cols; + fd->mi_rows = mi_rows; + fd->mi_grid = (insp_mi_data *)vpx_malloc(sizeof(insp_mi_data) * fd->mi_rows * + fd->mi_cols); +} + +void ifd_init(insp_frame_data *fd, int frame_width, int frame_height) { + int mi_cols = ALIGN_POWER_OF_TWO(frame_width, 3) >> MI_SIZE_LOG2; + int mi_rows = ALIGN_POWER_OF_TWO(frame_height, 3) >> MI_SIZE_LOG2; + ifd_init_mi_rc(fd, mi_cols, mi_rows); +} + +void ifd_clear(insp_frame_data *fd) { + vpx_free(fd->mi_grid); + fd->mi_grid = NULL; +} + +/* TODO(negge): This function may be called by more than one thread when using + a multi-threaded decoder and this may cause a data race. */ +int ifd_inspect(insp_frame_data *fd, void *decoder) { + struct VP9Decoder *pbi = (struct VP9Decoder *)decoder; + VP9_COMMON *const cm = &pbi->common; + if (fd->mi_rows != cm->mi_rows || fd->mi_cols != cm->mi_cols) { + ifd_clear(fd); + ifd_init_mi_rc(fd, cm->mi_rows, cm->mi_cols); + } + fd->show_frame = cm->show_frame; + fd->frame_type = cm->frame_type; + fd->base_qindex = cm->base_qindex; + // TODO(jimbankoski): copy tile data + // fd->tile_mi_cols = cm->tile_width; + // fd->tile_mi_rows = cm->tile_height; +#if CONFIG_ACCOUNTING + fd->accounting = &pbi->accounting; +#endif + int i, j; + for (i = 0; i < MAX_SEGMENTS; i++) { + for (j = 0; j < 2; j++) { + fd->y_dequant[i][j] = cm->y_dequant[i][j]; + fd->uv_dequant[i][j] = cm->uv_dequant[i][j]; + } + } + for (j = 0; j < cm->mi_rows; j++) { + for (i = 0; i < cm->mi_cols; i++) { + const MODE_INFO *bmi = cm->mi_grid_visible[j * cm->mi_stride + i]; + insp_mi_data *mi = &fd->mi_grid[j * cm->mi_cols + i]; + // Segment + mi->segment_id = bmi->segment_id; + // Motion Vectors + mi->mv[0].row = bmi->mv[0].as_mv.row; + mi->mv[0].col = bmi->mv[0].as_mv.col; + mi->mv[1].row = bmi->mv[1].as_mv.row; + mi->mv[1].col = bmi->mv[1].as_mv.col; + // Reference Frames + mi->ref_frame[0] = bmi->ref_frame[0]; + mi->ref_frame[1] = bmi->ref_frame[1]; + // Prediction Mode + mi->mode = bmi->mode; + // Prediction Mode for Chromatic planes + // TODO(jbb): mbx handle UV_MODE_INVALID case + // if (mi->mode < INTRA_MODES) { + mi->uv_mode = bmi->uv_mode; + // } else { + // mi->uv_mode = UV_MODE_INVALID; + // } + // Block Size + mi->sb_type = bmi->sb_type; + // Skip Flag + mi->skip = bmi->skip; + // Filters + mi->filter = bmi->interp_filter; + // Transform + // TODO(jbb): mi->tx_type = bmi->tx_type; + mi->tx_size = bmi->tx_size; + // delta_q + // TODO(jbb): mi->current_qindex = bmi->current_q_index; + } + } + return 1; +} diff --git a/vp9/decoder/inspection.h b/vp9/decoder/inspection.h new file mode 100644 index 000000000..a3c98076b --- /dev/null +++ b/vp9/decoder/inspection.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2017, 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. + */ +#ifndef AOM_INSPECTION_H_ +#define AOM_INSPECTION_H_ + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +#include "vp9/common/vp9_seg_common.h" +#define CONFIG_ACCOUNTING 0 +#if CONFIG_ACCOUNTING +#include "av1/decoder/accounting.h" +#endif + +#ifndef AOM_AOMDX_H_ +typedef void (*aom_inspect_cb)(void *decoder, void *data); +#endif + +typedef struct insp_mv insp_mv; + +struct insp_mv { + int16_t row; + int16_t col; +}; + +typedef struct insp_mi_data insp_mi_data; + +struct insp_mi_data { + insp_mv mv[2]; + int16_t ref_frame[2]; + int16_t mode; + int16_t uv_mode; + int16_t sb_type; + int16_t skip; + int16_t segment_id; + int16_t filter; + int16_t tx_type; + int16_t tx_size; + int16_t current_qindex; +}; + +typedef struct insp_frame_data insp_frame_data; + +struct insp_frame_data { +#if CONFIG_ACCOUNTING + Accounting *accounting; +#endif + insp_mi_data *mi_grid; + int show_frame; + int frame_type; + int base_qindex; + int mi_rows; + int mi_cols; + int tile_mi_rows; + int tile_mi_cols; + int16_t y_dequant[MAX_SEGMENTS][2]; + int16_t uv_dequant[MAX_SEGMENTS][2]; +}; + +void ifd_init(insp_frame_data *fd, int frame_width, int frame_height); +void ifd_clear(insp_frame_data *fd); +int ifd_inspect(insp_frame_data *fd, void *decoder); + +#ifdef __cplusplus +} // extern "C" +#endif // __cplusplus +#endif // AOM_INSPECTION_H_ diff --git a/vp9/decoder/vp9_decodeframe.c b/vp9/decoder/vp9_decodeframe.c index bc0fc6197..2c5fd8c51 100644 --- a/vp9/decoder/vp9_decodeframe.c +++ b/vp9/decoder/vp9_decodeframe.c @@ -2202,4 +2202,8 @@ void vp9_decode_frame(VP9Decoder *pbi, const uint8_t *data, // Non frame parallel update frame context here. if (cm->refresh_frame_context && !context_updated) cm->frame_contexts[cm->frame_context_idx] = *cm->fc; + +#if CONFIG_INSPECTION + if (pbi->inspect_cb != NULL) (*pbi->inspect_cb)(pbi, pbi->inspect_ctx); +#endif } diff --git a/vp9/decoder/vp9_decoder.h b/vp9/decoder/vp9_decoder.h index 9a582fffb..a63098f86 100644 --- a/vp9/decoder/vp9_decoder.h +++ b/vp9/decoder/vp9_decoder.h @@ -22,6 +22,10 @@ #include "vp9/common/vp9_onyxc_int.h" #include "vp9/common/vp9_ppflags.h" +#if CONFIG_INSPECTION +#include "vp9/decoder/inspection.h" +#endif + #ifdef __cplusplus extern "C" { #endif @@ -90,6 +94,10 @@ typedef struct VP9Decoder { int row_mt; int lpf_mt_opt; RowMTWorkerData *row_mt_worker_data; +#if CONFIG_INSPECTION + vpx_inspect_cb inspect_cb; + void *inspect_ctx; +#endif } VP9Decoder; int vp9_receive_compressed_data(struct VP9Decoder *pbi, size_t size, diff --git a/vp9/vp9_dx_iface.c b/vp9/vp9_dx_iface.c index 6a4cb9acf..2bf0d78d2 100644 --- a/vp9/vp9_dx_iface.c +++ b/vp9/vp9_dx_iface.c @@ -316,6 +316,11 @@ static vpx_codec_err_t decode_one(vpx_codec_alg_priv_t *ctx, ctx->pbi->decrypt_cb = ctx->decrypt_cb; ctx->pbi->decrypt_state = ctx->decrypt_state; +#if CONFIG_INSPECTION + ctx->pbi->inspect_cb = ctx->inspect_cb; + ctx->pbi->inspect_ctx = ctx->inspect_ctx; +#endif + if (vp9_receive_compressed_data(ctx->pbi, data_sz, data)) { ctx->pbi->cur_buf->buf.corrupted = 1; ctx->pbi->need_resync = 1; @@ -667,6 +672,19 @@ static vpx_codec_err_t ctrl_enable_lpf_opt(vpx_codec_alg_priv_t *ctx, return VPX_CODEC_OK; } +static vpx_codec_err_t ctrl_set_inspection_callback(vpx_codec_alg_priv_t *ctx, + va_list args) { +#if !CONFIG_INSPECTION + (void)ctx; + (void)args; + return VPX_CODEC_INCAPABLE; +#else + vpx_inspect_init *init = va_arg(args, vpx_inspect_init *); + ctx->inspect_cb = init->inspect_cb; + ctx->inspect_ctx = init->inspect_ctx; + return VPX_CODEC_OK; +#endif +} static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = { { VP8_COPY_REFERENCE, ctrl_copy_reference }, @@ -679,6 +697,8 @@ static vpx_codec_ctrl_fn_map_t decoder_ctrl_maps[] = { { VP9_SET_BYTE_ALIGNMENT, ctrl_set_byte_alignment }, { VP9_SET_SKIP_LOOP_FILTER, ctrl_set_skip_loop_filter }, { VP9_DECODE_SVC_SPATIAL_LAYER, ctrl_set_spatial_layer_svc }, + { VP9_SET_INSPECTION_CALLBACK, ctrl_set_inspection_callback }, + { VP9D_SET_ROW_MT, ctrl_set_row_mt }, { VP9D_SET_LOOP_FILTER_OPT, ctrl_enable_lpf_opt }, diff --git a/vp9/vp9_dx_iface.h b/vp9/vp9_dx_iface.h index f60688c4d..722959737 100644 --- a/vp9/vp9_dx_iface.h +++ b/vp9/vp9_dx_iface.h @@ -47,6 +47,11 @@ struct vpx_codec_alg_priv { int svc_spatial_layer; int row_mt; int lpf_opt; + +#if CONFIG_INSPECTION + vpx_inspect_cb inspect_cb; + void *inspect_ctx; +#endif }; #endif // VPX_VP9_VP9_DX_IFACE_H_ diff --git a/vp9/vp9dx.mk b/vp9/vp9dx.mk index 59f612b94..7f239cd1b 100644 --- a/vp9/vp9dx.mk +++ b/vp9/vp9dx.mk @@ -29,4 +29,9 @@ VP9_DX_SRCS-yes += decoder/vp9_decoder.h VP9_DX_SRCS-yes += decoder/vp9_dsubexp.c VP9_DX_SRCS-yes += decoder/vp9_dsubexp.h +ifeq ($(CONFIG_INSPECTION),yes) +VP9_DX_SRCS-yes += decoder/inspection.c +VP9_DX_SRCS-yes += decoder/inspection.h +endif + VP9_DX_SRCS-yes := $(filter-out $(VP9_DX_SRCS_REMOVE-yes),$(VP9_DX_SRCS-yes)) diff --git a/vpx/vp8dx.h b/vpx/vp8dx.h index c31afc1e6..5d0e533f1 100644 --- a/vpx/vp8dx.h +++ b/vpx/vp8dx.h @@ -45,6 +45,25 @@ extern vpx_codec_iface_t vpx_codec_vp9_dx_algo; extern vpx_codec_iface_t *vpx_codec_vp9_dx(void); /*!@} - end algorithm interface member group*/ +#ifndef VPX_INSPECTION_H_ +/** Callback that inspects decoder frame data. + */ +typedef void (*vpx_inspect_cb)(void *decoder, void *ctx); +#endif + +/*!\brief Structure to hold inspection callback and context. + * + * Defines a structure to hold the inspection callback function and calling + * context. + */ +typedef struct vpx_inspect_init { + /*! Inspection callback. */ + vpx_inspect_cb inspect_cb; + + /*! Inspection context. */ + void *inspect_ctx; +} vpx_inspect_init; + /*!\enum vp8_dec_control_id * \brief VP8 decoder control functions * @@ -130,6 +149,7 @@ enum vp8_dec_control_id { * * Supported in codecs: VP9 */ + VP9D_SET_ROW_MT, /*!\brief Codec control function to set loopfilter optimization. @@ -142,6 +162,12 @@ enum vp8_dec_control_id { */ VP9D_SET_LOOP_FILTER_OPT, + /** control function to set an vpx_inspect_cb callback that is invoked each + * time a frame is decoded. When compiled without --enable-inspection, this + * returns VPX_CODEC_INCAPABLE. + */ + VP9_SET_INSPECTION_CALLBACK, + VP8_DECODER_CTRL_ID_MAX }; @@ -203,6 +229,8 @@ VPX_CTRL_USE_TYPE(VP9_SET_SKIP_LOOP_FILTER, int) VPX_CTRL_USE_TYPE(VP9D_SET_ROW_MT, int) #define VPX_CTRL_VP9_SET_LOOP_FILTER_OPT VPX_CTRL_USE_TYPE(VP9D_SET_LOOP_FILTER_OPT, int) +#define VPX_CTRL_VP9_SET_INSPECTION_CALLBACK +VPX_CTRL_USE_TYPE(VP9_SET_INSPECTION_CALLBACK, vpx_inspect_init *) /*!\endcond */ /*! @} - end defgroup vp8_decoder */