]> git.sesse.net Git - ffmpeg/blobdiff - ffprobe.c
Dirac: Doxygen comments and some formatting enhancements
[ffmpeg] / ffprobe.c
index b64f6160bbbc6cf45a929e57798f773e5f478a06..470001c4064abd37e2cd278cae1f0f90a36b5088 100644 (file)
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -155,6 +155,19 @@ struct WriterContext {
     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)
@@ -179,6 +192,7 @@ static int writer_open(WriterContext **wctx, const Writer *writer,
         goto fail;
     }
 
+    (*wctx)->class = &writer_class;
     (*wctx)->writer = writer;
     if ((*wctx)->writer->init)
         ret = (*wctx)->writer->init(*wctx, args, opaque);
@@ -321,6 +335,31 @@ fail:
     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 */
@@ -396,39 +435,60 @@ static Writer default_writer = {
 
 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)
@@ -444,15 +504,12 @@ static void json_print_footer(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)
@@ -478,14 +535,10 @@ static inline void json_print_item_str(WriterContext *wctx,
                                        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 "    "
@@ -498,11 +551,11 @@ static void json_print_str(WriterContext *wctx, const char *key, const char *val
 
 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)
@@ -524,6 +577,8 @@ static Writer json_writer = {
     .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,
@@ -644,6 +699,8 @@ static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i
             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));
@@ -752,26 +809,20 @@ static int probe_file(const char *filename)
     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;
     }
@@ -791,8 +842,7 @@ static int probe_file(const char *filename)
     writer_close(&wctx);
 
 end:
-    av_free(w_str);
-    av_free(w_args);
+    av_freep(&print_format);
 
     return ret;
 }
@@ -828,13 +878,13 @@ static void opt_input_file(void *optctx, const char *arg)
 
 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;
 }