unsigned int nb_chapter; ///< number of the chapter, starting at 0
};
+static const char *writer_get_name(void *p)
+{
+ WriterContext *wctx = p;
+ return wctx->writer->name;
+}
+
+static const AVClass writer_class = {
+ "Writer",
+ writer_get_name,
+ NULL,
+ LIBAVUTIL_VERSION_INT,
+};
+
static void writer_close(WriterContext **wctx)
{
if (*wctx && (*wctx)->writer->uninit)
goto fail;
}
+ (*wctx)->class = &writer_class;
(*wctx)->writer = writer;
if ((*wctx)->writer->init)
ret = (*wctx)->writer->init(*wctx, args, opaque);
return NULL;
}
+#define ESCAPE_INIT_BUF_SIZE 256
+
+#define ESCAPE_CHECK_SIZE(src, size, max_size) \
+ if (size > max_size) { \
+ char buf[64]; \
+ snprintf(buf, sizeof(buf), "%s", src); \
+ av_log(log_ctx, AV_LOG_WARNING, \
+ "String '%s...' with is too big\n", buf); \
+ return "FFPROBE_TOO_BIG_STRING"; \
+ }
+
+#define ESCAPE_REALLOC_BUF(dst_size_p, dst_p, src, size) \
+ if (*dst_size_p < size) { \
+ char *q = av_realloc(*dst_p, size); \
+ if (!q) { \
+ char buf[64]; \
+ snprintf(buf, sizeof(buf), "%s", src); \
+ av_log(log_ctx, AV_LOG_WARNING, \
+ "String '%s...' could not be escaped\n", buf); \
+ return "FFPROBE_THIS_STRING_COULD_NOT_BE_ESCAPED"; \
+ } \
+ *dst_size_p = size; \
+ *dst = q; \
+ }
+
/* WRITERS */
/* Default output */
typedef struct {
int multiple_entries; ///< tells if the given chapter requires multiple entries
+ char *buf;
+ size_t buf_size;
} JSONContext;
-static char *json_escape_str(const char *s)
+static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque)
+{
+ JSONContext *json = wctx->priv;
+
+ json->buf_size = ESCAPE_INIT_BUF_SIZE;
+ if (!(json->buf = av_malloc(json->buf_size)))
+ return AVERROR(ENOMEM);
+
+ return 0;
+}
+
+static av_cold void json_uninit(WriterContext *wctx)
+{
+ JSONContext *json = wctx->priv;
+ av_freep(&json->buf);
+}
+
+static const char *json_escape_str(char **dst, size_t *dst_size, const char *src,
+ void *log_ctx)
{
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;
+ const char *p;
+ char *q;
+ size_t size = 1;
// 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
+ for (p = src; *p; p++) {
+ ESCAPE_CHECK_SIZE(src, size, SIZE_MAX-6);
+ if (strchr(json_escape, *p)) size += 2; // simple escape
+ else if ((unsigned char)*p < 32) size += 6; // handle non-printable chars
+ else size += 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;
+ ESCAPE_REALLOC_BUF(dst_size, dst, src, size);
+
+ q = *dst;
+ for (p = src; *p; p++) {
+ char *s = strchr(json_escape, *p);
+ if (s) {
+ *q++ = '\\';
+ *q++ = json_subst[s - json_escape];
+ } else if ((unsigned char)*p < 32) {
+ snprintf(q, 7, "\\u00%02x", *p & 0xff);
+ q += 6;
} else {
- *p++ = s[i];
+ *q++ = *p;
}
}
- *p = 0;
- return ret;
+ *q = 0;
+ return *dst;
}
static void json_print_header(WriterContext *wctx)
static void json_print_chapter_header(WriterContext *wctx, const char *chapter)
{
JSONContext *json = wctx->priv;
- char *chapter_esc;
if (wctx->nb_chapter)
printf(",");
json->multiple_entries = !strcmp(chapter, "packets") || !strcmp(chapter, "streams");
- chapter_esc = json_escape_str(chapter);
- printf("\n \"%s\":%s", chapter_esc ? chapter_esc : "",
+ printf("\n \"%s\":%s", json_escape_str(&json->buf, &json->buf_size, chapter, wctx),
json->multiple_entries ? " [" : " ");
- av_free(chapter_esc);
}
static void json_print_chapter_footer(WriterContext *wctx, const char *chapter)
const char *key, const char *value,
const char *indent)
{
- char *key_esc = json_escape_str(key);
- char *value_esc = json_escape_str(value);
+ JSONContext *json = wctx->priv;
- printf("%s\"%s\": \"%s\"", indent,
- key_esc ? key_esc : "",
- value_esc ? value_esc : "");
- av_free(key_esc);
- av_free(value_esc);
+ printf("%s\"%s\":", indent, json_escape_str(&json->buf, &json->buf_size, key, wctx));
+ printf(" \"%s\"", json_escape_str(&json->buf, &json->buf_size, value, wctx));
}
#define INDENT " "
static void json_print_int(WriterContext *wctx, const char *key, int value)
{
- char *key_esc = json_escape_str(key);
+ JSONContext *json = wctx->priv;
if (wctx->nb_item) printf(",\n");
- printf(INDENT "\"%s\": %d", key_esc ? key_esc : "", value);
- av_free(key_esc);
+ printf(INDENT "\"%s\": %d",
+ json_escape_str(&json->buf, &json->buf_size, key, wctx), value);
}
static void json_show_tags(WriterContext *wctx, AVDictionary *dict)
.name = "json",
.priv_size = sizeof(JSONContext),
+ .init = json_init,
+ .uninit = json_uninit,
.print_header = json_print_header,
.print_footer = json_print_footer,
.print_chapter_header = json_print_chapter_header,
break;
case AVMEDIA_TYPE_AUDIO:
+ print_str("sample_fmt",
+ av_x_if_null(av_get_sample_fmt_name(dec_ctx->sample_fmt), "unknown"));
print_str("sample_rate", value_string(val_str, sizeof(val_str), dec_ctx->sample_rate, unit_hertz_str));
print_int("channels", dec_ctx->channels);
print_int("bits_per_sample", av_get_bits_per_sample(dec_ctx->codec_id));
AVFormatContext *fmt_ctx;
int ret;
Writer *w;
- const char *buf = print_format;
- char *w_str = NULL, *w_args = NULL;
+ char *buf;
+ char *w_name = NULL, *w_args = NULL;
WriterContext *wctx;
writer_register_all();
- if (buf) {
- w_str = av_get_token(&buf, "=");
- if (*buf == '=') {
- buf++;
- w_args = av_get_token(&buf, "");
- }
- }
+ if (!print_format)
+ print_format = av_strdup("default");
+ w_name = av_strtok(print_format, "=", &buf);
+ w_args = buf;
- if (!w_str)
- w_str = av_strdup("default");
-
- w = writer_get_by_name(w_str);
+ w = writer_get_by_name(w_name);
if (!w) {
- av_log(NULL, AV_LOG_ERROR, "Invalid output format '%s'\n", w_str);
+ av_log(NULL, AV_LOG_ERROR, "Unknown output format with name '%s'\n", w_name);
ret = AVERROR(EINVAL);
goto end;
}
writer_close(&wctx);
end:
- av_free(w_str);
- av_free(w_args);
+ av_freep(&print_format);
return ret;
}
static int opt_help(const char *opt, const char *arg)
{
- const AVClass *class = avformat_get_class();
av_log_set_callback(log_callback_help);
show_usage();
show_help_options(options, "Main options:\n", 0, 0);
printf("\n");
- av_opt_show2(&class, NULL,
- AV_OPT_FLAG_DECODING_PARAM, 0);
+
+ show_help_children(avformat_get_class(), AV_OPT_FLAG_DECODING_PARAM);
+
return 0;
}