]> git.sesse.net Git - ffmpeg/blobdiff - ffprobe.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / ffprobe.c
index b87059ebb78d85d51b8e02e809680abfac6e6726..a40f756d8291831f9eab58d6c6e31055169d23b3 100644 (file)
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -33,6 +33,7 @@
 #include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/dict.h"
+#include "libavutil/libm.h"
 #include "libavutil/timecode.h"
 #include "libavdevice/avdevice.h"
 #include "libswscale/swscale.h"
@@ -53,6 +54,7 @@ static int do_show_frames  = 0;
 static AVDictionary *fmt_entries_to_show = NULL;
 static int do_show_packets = 0;
 static int do_show_streams = 0;
+static int do_show_data    = 0;
 static int do_show_program_version  = 0;
 static int do_show_library_versions = 0;
 
@@ -64,7 +66,7 @@ static int show_private_data            = 1;
 
 static char *print_format;
 
-static const OptionDef options[];
+static const OptionDef *options;
 
 /* FFprobe context */
 static const char *input_filename;
@@ -120,7 +122,7 @@ static char *value_string(char *buf, int buf_size, struct unit_value uv)
             long long int index;
 
             if (uv.unit == unit_byte_str && use_byte_value_binary_prefix) {
-                index = (long long int) (log(vald)/log(2)) / 10;
+                index = (long long int) (log2(vald)) / 10;
                 index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) - 1);
                 vald /= pow(2, index * 10);
                 prefix_string = binary_unit_prefixes[index];
@@ -165,6 +167,7 @@ typedef struct Writer {
     void (*print_section_header)(WriterContext *wctx, const char *);
     void (*print_section_footer)(WriterContext *wctx, const char *);
     void (*print_integer)       (WriterContext *wctx, const char *, long long int);
+    void (*print_rational)      (WriterContext *wctx, AVRational *q, char *sep);
     void (*print_string)        (WriterContext *wctx, const char *, const char *);
     void (*show_tags)           (WriterContext *wctx, AVDictionary *dict);
     int flags;                  ///< a combination or WRITER_FLAG_*
@@ -309,6 +312,16 @@ static inline void writer_print_integer(WriterContext *wctx,
     }
 }
 
+static inline void writer_print_rational(WriterContext *wctx,
+                                         const char *key, AVRational q, char sep)
+{
+    AVBPrint buf;
+    av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
+    av_bprintf(&buf, "%d%c%d", q.num, sep, q.den);
+    wctx->writer->print_string(wctx, key, buf.str);
+    wctx->nb_item++;
+}
+
 static inline void writer_print_string(WriterContext *wctx,
                                        const char *key, const char *val, int opt)
 {
@@ -350,6 +363,34 @@ static inline void writer_show_tags(WriterContext *wctx, AVDictionary *dict)
     wctx->writer->show_tags(wctx, dict);
 }
 
+static void writer_print_data(WriterContext *wctx, const char *name,
+                              uint8_t *data, int size)
+{
+    AVBPrint bp;
+    int offset = 0, l, i;
+
+    av_bprint_init(&bp, 0, AV_BPRINT_SIZE_UNLIMITED);
+    av_bprintf(&bp, "\n");
+    while (size) {
+        av_bprintf(&bp, "%08x: ", offset);
+        l = FFMIN(size, 16);
+        for (i = 0; i < l; i++) {
+            av_bprintf(&bp, "%02x", data[i]);
+            if (i & 1)
+                av_bprintf(&bp, " ");
+        }
+        av_bprint_chars(&bp, ' ', 41 - 2 * i - i / 2);
+        for (i = 0; i < l; i++)
+            av_bprint_chars(&bp, data[i] - 32U < 95 ? data[i] : '.', 1);
+        av_bprintf(&bp, "\n");
+        offset += l;
+        data   += l;
+        size   -= l;
+    }
+    writer_print_string(wctx, name, bp.str, 0);
+    av_bprint_finalize(&bp, NULL);
+}
+
 #define MAX_REGISTERED_WRITERS_NB 64
 
 static const Writer *registered_writers[MAX_REGISTERED_WRITERS_NB + 1];
@@ -417,10 +458,8 @@ static av_cold int default_init(WriterContext *wctx, const char *args, void *opa
     av_opt_set_defaults(def);
 
     if (args &&
-        (err = (av_set_options_string(def, args, "=", ":"))) < 0) {
-        av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
+        (err = (av_set_options_string(def, args, "=", ":"))) < 0)
         return err;
-    }
 
     return 0;
 }
@@ -610,10 +649,8 @@ static av_cold int compact_init(WriterContext *wctx, const char *args, void *opa
     av_opt_set_defaults(compact);
 
     if (args &&
-        (err = (av_set_options_string(compact, args, "=", ":"))) < 0) {
-        av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
+        (err = (av_set_options_string(compact, args, "=", ":"))) < 0)
         return err;
-    }
     if (strlen(compact->item_sep_str) != 1) {
         av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
                compact->item_sep_str);
@@ -743,7 +780,7 @@ typedef struct FlatContext {
 static const AVOption flat_options[]= {
     {"sep_char", "set separator",    OFFSET(sep_str),    AV_OPT_TYPE_STRING, {.str="."},  CHAR_MIN, CHAR_MAX },
     {"s",        "set separator",    OFFSET(sep_str),    AV_OPT_TYPE_STRING, {.str="."},  CHAR_MIN, CHAR_MAX },
-    {"hierachical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
+    {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
     {"h",           "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
     {NULL},
 };
@@ -768,10 +805,8 @@ static av_cold int flat_init(WriterContext *wctx, const char *args, void *opaque
     av_opt_set_defaults(flat);
 
     if (args &&
-        (err = (av_set_options_string(flat, args, "=", ":"))) < 0) {
-        av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
+        (err = (av_set_options_string(flat, args, "=", ":"))) < 0)
         return err;
-    }
     if (strlen(flat->sep_str) != 1) {
         av_log(wctx, AV_LOG_ERROR, "Item separator '%s' specified, but must contain a single character\n",
                flat->sep_str);
@@ -899,7 +934,7 @@ typedef struct {
 #define OFFSET(x) offsetof(INIContext, x)
 
 static const AVOption ini_options[] = {
-    {"hierachical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
+    {"hierarchical", "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
     {"h",           "specify if the section specification should be hierarchical", OFFSET(hierarchical), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
     {NULL},
 };
@@ -926,10 +961,8 @@ static av_cold int ini_init(WriterContext *wctx, const char *args, void *opaque)
     ini->class = &ini_class;
     av_opt_set_defaults(ini);
 
-    if (args && (err = av_set_options_string(ini, args, "=", ":")) < 0) {
-        av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
+    if (args && (err = av_set_options_string(ini, args, "=", ":")) < 0)
         return err;
-    }
 
     return 0;
 }
@@ -1085,10 +1118,8 @@ static av_cold int json_init(WriterContext *wctx, const char *args, void *opaque
     av_opt_set_defaults(json);
 
     if (args &&
-        (err = (av_set_options_string(json, args, "=", ":"))) < 0) {
-        av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
+        (err = (av_set_options_string(json, args, "=", ":"))) < 0)
         return err;
-    }
 
     json->item_sep       = json->compact ? ", " : ",\n";
     json->item_start_end = json->compact ? " "  : "\n";
@@ -1310,10 +1341,8 @@ static av_cold int xml_init(WriterContext *wctx, const char *args, void *opaque)
     av_opt_set_defaults(xml);
 
     if (args &&
-        (err = (av_set_options_string(xml, args, "=", ":"))) < 0) {
-        av_log(wctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
+        (err = (av_set_options_string(xml, args, "=", ":"))) < 0)
         return err;
-    }
 
     if (xml->xsd_strict) {
         xml->fully_qualified = 1;
@@ -1508,6 +1537,7 @@ static void writer_register_all(void)
 } while (0)
 
 #define print_int(k, v)         writer_print_integer(w, k, v)
+#define print_q(k, v, s)        writer_print_rational(w, k, v, s)
 #define print_str(k, v)         writer_print_string(w, k, v, 0)
 #define print_str_opt(k, v)     writer_print_string(w, k, v, 1)
 #define print_time(k, v, tb)    writer_print_time(w, k, v, tb, 0)
@@ -1540,17 +1570,22 @@ static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pk
     print_time("dts_time",        pkt->dts, &st->time_base);
     print_duration_ts("duration",        pkt->duration);
     print_duration_time("duration_time", pkt->duration, &st->time_base);
+    print_duration_ts("convergence_duration", pkt->convergence_duration);
+    print_duration_time("convergence_duration_time", pkt->convergence_duration, &st->time_base);
     print_val("size",             pkt->size, unit_byte_str);
     if (pkt->pos != -1) print_fmt    ("pos", "%"PRId64, pkt->pos);
     else                print_str_opt("pos", "N/A");
     print_fmt("flags", "%c",      pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
+    if (do_show_data)
+        writer_print_data(w, "data", pkt->data, pkt->size);
     print_section_footer("packet");
 
     av_bprint_finalize(&pbuf, NULL);
     fflush(stdout);
 }
 
-static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
+static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
+                       AVFormatContext *fmt_ctx)
 {
     AVBPrint pbuf;
     const char *s;
@@ -1573,16 +1608,17 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
     else                      print_str_opt("pkt_pos", "N/A");
 
     switch (stream->codec->codec_type) {
+        AVRational sar;
+
     case AVMEDIA_TYPE_VIDEO:
         print_int("width",                  frame->width);
         print_int("height",                 frame->height);
         s = av_get_pix_fmt_name(frame->format);
         if (s) print_str    ("pix_fmt", s);
         else   print_str_opt("pix_fmt", "unknown");
-        if (frame->sample_aspect_ratio.num) {
-            print_fmt("sample_aspect_ratio", "%d:%d",
-                      frame->sample_aspect_ratio.num,
-                      frame->sample_aspect_ratio.den);
+        sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, frame);
+        if (sar.num) {
+            print_q("sample_aspect_ratio", sar, ':');
         } else {
             print_str_opt("sample_aspect_ratio", "N/A");
         }
@@ -1600,8 +1636,17 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
         if (s) print_str    ("sample_fmt", s);
         else   print_str_opt("sample_fmt", "unknown");
         print_int("nb_samples",         frame->nb_samples);
+        print_int("channels", av_frame_get_channels(frame));
+        if (av_frame_get_channel_layout(frame)) {
+            av_bprint_clear(&pbuf);
+            av_bprint_channel_layout(&pbuf, av_frame_get_channels(frame),
+                                     av_frame_get_channel_layout(frame));
+            print_str    ("channel_layout", pbuf.str);
+        } else
+            print_str_opt("channel_layout", "unknown");
         break;
     }
+    show_tags(av_frame_get_metadata(frame));
 
     print_section_footer("frame");
 
@@ -1609,32 +1654,44 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
     fflush(stdout);
 }
 
-static av_always_inline int get_decoded_frame(AVFormatContext *fmt_ctx,
-                                              AVFrame *frame, int *got_frame,
-                                              AVPacket *pkt)
+static av_always_inline int process_frame(WriterContext *w,
+                                          AVFormatContext *fmt_ctx,
+                                          AVFrame *frame, AVPacket *pkt)
 {
     AVCodecContext *dec_ctx = fmt_ctx->streams[pkt->stream_index]->codec;
-    int ret = 0;
+    int ret = 0, got_frame = 0;
 
-    *got_frame = 0;
-    switch (dec_ctx->codec_type) {
-    case AVMEDIA_TYPE_VIDEO:
-        ret = avcodec_decode_video2(dec_ctx, frame, got_frame, pkt);
-        break;
+    avcodec_get_frame_defaults(frame);
+    if (dec_ctx->codec) {
+        switch (dec_ctx->codec_type) {
+        case AVMEDIA_TYPE_VIDEO:
+            ret = avcodec_decode_video2(dec_ctx, frame, &got_frame, pkt);
+            break;
 
-    case AVMEDIA_TYPE_AUDIO:
-        ret = avcodec_decode_audio4(dec_ctx, frame, got_frame, pkt);
-        break;
+        case AVMEDIA_TYPE_AUDIO:
+            ret = avcodec_decode_audio4(dec_ctx, frame, &got_frame, pkt);
+            break;
+        }
     }
 
-    return ret;
+    if (ret < 0)
+        return ret;
+    ret = FFMIN(ret, pkt->size); /* guard against bogus return values */
+    pkt->data += ret;
+    pkt->size -= ret;
+    if (got_frame) {
+        nb_streams_frames[pkt->stream_index]++;
+        if (do_show_frames)
+            show_frame(w, frame, fmt_ctx->streams[pkt->stream_index], fmt_ctx);
+    }
+    return got_frame;
 }
 
 static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
 {
     AVPacket pkt, pkt1;
     AVFrame frame;
-    int i = 0, ret, got_frame;
+    int i = 0;
 
     av_init_packet(&pkt);
 
@@ -1646,17 +1703,7 @@ static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
         }
         if (do_read_frames) {
             pkt1 = pkt;
-            while (pkt1.size) {
-                avcodec_get_frame_defaults(&frame);
-                ret = get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt1);
-                if (ret < 0 || !got_frame)
-                    break;
-                if (do_show_frames)
-                    show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
-                pkt1.data += ret;
-                pkt1.size -= ret;
-                nb_streams_frames[pkt.stream_index]++;
-            }
+            while (pkt1.size && process_frame(w, fmt_ctx, &frame, &pkt1) > 0);
         }
         av_free_packet(&pkt);
     }
@@ -1666,13 +1713,8 @@ static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
     //Flush remaining frames that are cached in the decoder
     for (i = 0; i < fmt_ctx->nb_streams; i++) {
         pkt.stream_index = i;
-        while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame) {
-            if (do_read_frames) {
-                if (do_show_frames)
-                    show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
-                nb_streams_frames[pkt.stream_index]++;
-            }
-        }
+        if (do_read_frames)
+            while (process_frame(w, fmt_ctx, &frame, &pkt) > 0);
     }
 }
 
@@ -1683,7 +1725,7 @@ static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i
     AVCodec *dec;
     char val_str[128];
     const char *s;
-    AVRational display_aspect_ratio;
+    AVRational sar, dar;
     AVBPrint pbuf;
 
     av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
@@ -1710,7 +1752,7 @@ static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i
         s = av_get_media_type_string(dec_ctx->codec_type);
         if (s) print_str    ("codec_type", s);
         else   print_str_opt("codec_type", "unknown");
-        print_fmt("codec_time_base", "%d/%d", dec_ctx->time_base.num, dec_ctx->time_base.den);
+        print_q("codec_time_base", dec_ctx->time_base, '/');
 
         /* print AVI/FourCC tag */
         av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
@@ -1722,17 +1764,14 @@ static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i
             print_int("width",        dec_ctx->width);
             print_int("height",       dec_ctx->height);
             print_int("has_b_frames", dec_ctx->has_b_frames);
-            if (dec_ctx->sample_aspect_ratio.num) {
-                print_fmt("sample_aspect_ratio", "%d:%d",
-                          dec_ctx->sample_aspect_ratio.num,
-                          dec_ctx->sample_aspect_ratio.den);
-                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
-                          dec_ctx->width  * dec_ctx->sample_aspect_ratio.num,
-                          dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
+            sar = av_guess_sample_aspect_ratio(fmt_ctx, stream, NULL);
+            if (sar.den) {
+                print_q("sample_aspect_ratio", sar, ':');
+                av_reduce(&dar.num, &dar.den,
+                          dec_ctx->width  * sar.num,
+                          dec_ctx->height * sar.den,
                           1024*1024);
-                print_fmt("display_aspect_ratio", "%d:%d",
-                          display_aspect_ratio.num,
-                          display_aspect_ratio.den);
+                print_q("display_aspect_ratio", dar, ':');
             } else {
                 print_str_opt("sample_aspect_ratio", "N/A");
                 print_str_opt("display_aspect_ratio", "N/A");
@@ -1776,9 +1815,9 @@ static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i
 
     if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) print_fmt    ("id", "0x%x", stream->id);
     else                                          print_str_opt("id", "N/A");
-    print_fmt("r_frame_rate",   "%d/%d", stream->r_frame_rate.num,   stream->r_frame_rate.den);
-    print_fmt("avg_frame_rate", "%d/%d", stream->avg_frame_rate.num, stream->avg_frame_rate.den);
-    print_fmt("time_base",      "%d/%d", stream->time_base.num,      stream->time_base.den);
+    print_q("r_frame_rate",   stream->r_frame_rate,   '/');
+    print_q("avg_frame_rate", stream->avg_frame_rate, '/');
+    print_q("time_base",      stream->time_base,      '/');
     print_time("start_time",    stream->start_time, &stream->time_base);
     print_time("duration",      stream->duration,   &stream->time_base);
     if (dec_ctx->bit_rate > 0) print_val    ("bit_rate", dec_ctx->bit_rate, unit_bit_per_second_str);
@@ -1789,6 +1828,9 @@ static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i
     else                                print_str_opt("nb_read_frames", "N/A");
     if (nb_streams_packets[stream_idx]) print_fmt    ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
     else                                print_str_opt("nb_read_packets", "N/A");
+    if (do_show_data)
+        writer_print_data(w, "extradata", dec_ctx->extradata,
+                                          dec_ctx->extradata_size);
     show_tags(stream->metadata);
 
     print_section_footer("stream");
@@ -1870,7 +1912,7 @@ static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
         AVStream *stream = fmt_ctx->streams[i];
         AVCodec *codec;
 
-        if (stream->codec->codec_id == CODEC_ID_PROBE) {
+        if (stream->codec->codec_id == AV_CODEC_ID_PROBE) {
             av_log(NULL, AV_LOG_ERROR,
                    "Failed to probe codec for input stream %d\n",
                     stream->index);
@@ -1895,7 +1937,7 @@ static void close_input_file(AVFormatContext **ctx_ptr)
 
     /* close decoder for each stream */
     for (i = 0; i < fmt_ctx->nb_streams; i++)
-        if (fmt_ctx->streams[i]->codec->codec_id != CODEC_ID_NONE)
+        if (fmt_ctx->streams[i]->codec->codec_id != AV_CODEC_ID_NONE)
             avcodec_close(fmt_ctx->streams[i]->codec);
 
     avformat_close_input(ctx_ptr);
@@ -1964,8 +2006,7 @@ static void ffprobe_show_program_version(WriterContext *w)
               program_birth_year, this_year);
     print_str("build_date", __DATE__);
     print_str("build_time", __TIME__);
-    print_str("compiler_type", CC_TYPE);
-    print_str("compiler_version", CC_VERSION);
+    print_str("compiler_ident", CC_IDENT);
     print_str("configuration", FFMPEG_CONFIGURATION);
     print_section_footer("program_version");
     writer_print_chapter_footer(w, "program_version");
@@ -2059,7 +2100,7 @@ static int opt_show_versions(const char *opt, const char *arg)
     return 0;
 }
 
-static const OptionDef options[] = {
+static const OptionDef real_options[] = {
 #include "cmdutils_common_opts.h"
     { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
     { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
@@ -2073,6 +2114,7 @@ static const OptionDef options[] = {
     { "print_format", OPT_STRING | HAS_ARG, {(void*)&print_format},
       "set the output printing format (available formats are: default, compact, csv, flat, ini, json, xml)", "format" },
     { "of", OPT_STRING | HAS_ARG, {(void*)&print_format}, "alias for -print_format", "format" },
+    { "show_data",    OPT_BOOL, {(void*)&do_show_data}, "show packets data" },
     { "show_error",   OPT_BOOL, {(void*)&do_show_error} ,  "show probing error" },
     { "show_format",  OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
     { "show_frames",  OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
@@ -2101,6 +2143,7 @@ int main(int argc, char **argv)
     int ret;
 
     av_log_set_flags(AV_LOG_SKIP_REPEATED);
+    options = real_options;
     parse_loglevel(argc, argv, options);
     av_register_all();
     avformat_network_init();