summaryrefslogtreecommitdiff
path: root/avprobe.c
diff options
context:
space:
mode:
authorLuca Barbato <lu_zero@gentoo.org>2012-05-24 12:22:29 -0700
committerLuca Barbato <lu_zero@gentoo.org>2012-05-25 14:20:34 -0700
commit0d242a7713c795c4f4b8df8cac38f128aa3a811e (patch)
tree4df2d56b3d2f5cf62c2054b46154777bef0fdc7e /avprobe.c
parent3a8c95f730732b9f1ffacdbfbf79a01b202a67af (diff)
avprobe: provide JSON output
JSON usage is quite widespread.
Diffstat (limited to 'avprobe.c')
-rw-r--r--avprobe.c120
1 files changed, 120 insertions, 0 deletions
diff --git a/avprobe.c b/avprobe.c
index f3bb437b1e..53138943ee 100644
--- a/avprobe.c
+++ b/avprobe.c
@@ -97,6 +97,7 @@ typedef struct {
static AVIOContext *probe_out = NULL;
static OutputContext octx;
+#define AVP_INDENT() avio_printf(probe_out, "%*c", octx.level * 2, ' ')
/*
* Default format, INI
@@ -190,6 +191,96 @@ static void ini_print_string(const char *key, const char *value)
}
/*
+ * Alternate format, JSON
+ */
+
+static void json_print_header(void)
+{
+ avio_printf(probe_out, "{");
+}
+static void json_print_footer(void)
+{
+ avio_printf(probe_out, "}\n");
+}
+
+static void json_print_array_header(const char *name)
+{
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ",\n");
+ AVP_INDENT();
+ avio_printf(probe_out, "\"%s\" : ", name);
+ avio_printf(probe_out, "[\n");
+}
+
+static void json_print_array_footer(const char *name)
+{
+ avio_printf(probe_out, "\n");
+ AVP_INDENT();
+ avio_printf(probe_out, "]");
+}
+
+static void json_print_object_header(const char *name)
+{
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ",\n");
+ AVP_INDENT();
+ if (octx.prefix[octx.level -1].type == OBJECT)
+ avio_printf(probe_out, "\"%s\" : ", name);
+ avio_printf(probe_out, "{\n");
+}
+
+static void json_print_object_footer(const char *name)
+{
+ avio_printf(probe_out, "\n");
+ AVP_INDENT();
+ avio_printf(probe_out, "}");
+}
+
+static void json_print_integer(const char *key, int64_t value)
+{
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ",\n");
+ AVP_INDENT();
+ avio_printf(probe_out, "\"%s\" : %"PRId64"", key, value);
+}
+
+static void json_escape_print(const char *s)
+{
+ int i = 0;
+ char c = 0;
+
+ while (c = s[i++]) {
+ switch (c) {
+ case '\r': avio_printf(probe_out, "%s", "\\r"); break;
+ case '\n': avio_printf(probe_out, "%s", "\\n"); break;
+ case '\f': avio_printf(probe_out, "%s", "\\f"); break;
+ case '\b': avio_printf(probe_out, "%s", "\\b"); break;
+ case '\t': avio_printf(probe_out, "%s", "\\t"); break;
+ case '\\':
+ case '"' : avio_w8(probe_out, '\\');
+ default:
+ if ((unsigned char)c < 32)
+ avio_printf(probe_out, "\\u00%02x", c & 0xff);
+ else
+ avio_w8(probe_out, c);
+ break;
+ }
+ }
+}
+
+static void json_print_string(const char *key, const char *value)
+{
+ if (octx.prefix[octx.level -1].nb_elems)
+ avio_printf(probe_out, ",\n");
+ AVP_INDENT();
+ avio_w8(probe_out, '\"');
+ json_escape_print(key);
+ avio_printf(probe_out, "\" : \"");
+ json_escape_print(value);
+ avio_w8(probe_out, '\"');
+}
+
+/*
* Simple Formatter for single entries.
*/
@@ -666,6 +757,34 @@ static int opt_format(const char *opt, const char *arg)
return 0;
}
+static void opt_output_format(const char *opt, const char *arg)
+{
+
+ if (!strcmp(arg, "json")) {
+ print_header = json_print_header;
+ print_footer = json_print_footer;
+ print_array_header = json_print_array_header;
+ print_array_footer = json_print_array_footer;
+ print_object_header = json_print_object_header;
+ print_object_footer = json_print_object_footer;
+
+ print_integer = json_print_integer;
+ print_string = json_print_string;
+ } else
+ if (!strcmp(arg, "ini")) {
+ print_header = ini_print_header;
+ print_footer = ini_print_footer;
+ print_array_header = ini_print_array_header;
+ print_object_header = ini_print_object_header;
+
+ print_integer = ini_print_integer;
+ print_string = ini_print_string;
+ } else {
+ av_log(NULL, AV_LOG_ERROR, "Unsupported formatter %s\n", arg);
+ exit(1);
+ }
+}
+
static int opt_show_format_entry(const char *opt, const char *arg)
{
do_show_format = 1;
@@ -716,6 +835,7 @@ static void opt_pretty(void)
static const OptionDef options[] = {
#include "cmdutils_common_opts.h"
{ "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
+ { "of", HAS_ARG, {(void*)&opt_output_format}, "output the document either as ini or json", "output_format" },
{ "unit", OPT_BOOL, {(void*)&show_value_unit},
"show unit of the displayed values" },
{ "prefix", OPT_BOOL, {(void*)&use_value_prefix},