+/* JSON output */
+
+static void json_print_header(const char *section)
+{
+ printf("{\n");
+}
+
+static char *json_escape_str(const char *s)
+{
+ static const char json_escape[] = {'"', '\\', '\b', '\f', '\n', '\r', '\t', 0};
+ static const char json_subst[] = {'"', '\\', 'b', 'f', 'n', 'r', 't', 0};
+ char *ret, *p;
+ int i, len = 0;
+
+ // compute the length of the escaped string
+ for (i = 0; s[i]; i++) {
+ if (strchr(json_escape, s[i])) len += 2; // simple escape
+ else if ((unsigned char)s[i] < 32) len += 6; // handle non-printable chars
+ else len += 1; // char copy
+ }
+
+ p = ret = av_malloc(len + 1);
+ if (!p)
+ return NULL;
+ for (i = 0; s[i]; i++) {
+ char *q = strchr(json_escape, s[i]);
+ if (q) {
+ *p++ = '\\';
+ *p++ = json_subst[q - json_escape];
+ } else if ((unsigned char)s[i] < 32) {
+ snprintf(p, 7, "\\u00%02x", s[i] & 0xff);
+ p += 6;
+ } else {
+ *p++ = s[i];
+ }
+ }
+ *p = 0;
+ return ret;
+}
+
+static void json_print_str(const char *key, const char *value)
+{
+ char *key_esc = json_escape_str(key);
+ char *value_esc = json_escape_str(value);
+ printf(" \"%s\": \"%s\"",
+ key_esc ? key_esc : "",
+ value_esc ? value_esc : "");
+ av_free(key_esc);
+ av_free(value_esc);
+}
+
+static void json_print_int(const char *key, int value)
+{
+ char *key_esc = json_escape_str(key);
+ printf(" \"%s\": %d", key_esc ? key_esc : "", value);
+ av_free(key_esc);
+}
+
+static void json_print_footer(const char *section)
+{
+ printf("\n }");
+}
+
+static void json_print_section_start(const char *section, int multiple_entries)
+{
+ char *section_esc = json_escape_str(section);
+ printf("\n \"%s\":%s", section_esc ? section_esc : "",
+ multiple_entries ? " [" : " ");
+ av_free(section_esc);
+}
+
+static void json_print_section_end(const char *section, int multiple_entries)
+{
+ if (multiple_entries)
+ printf("]");
+}
+
+