ffprobe: refactor frames decoding.
[ffmpeg.git] / ffprobe.c
index 20b0baa..af69792 100644 (file)
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -65,7 +65,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;
@@ -1595,7 +1595,8 @@ static void show_packet(WriterContext *w, AVFormatContext *fmt_ctx, AVPacket *pk
     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;
@@ -1618,14 +1619,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_q("sample_aspect_ratio", frame->sample_aspect_ratio, ':');
+        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");
         }
@@ -1643,6 +1647,14 @@ 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));
@@ -1653,34 +1665,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;
+    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);
+            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);
+            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);
 
@@ -1692,17 +1714,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);
     }
@@ -1712,13 +1724,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);
     }
 }
 
@@ -1729,7 +1736,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);
@@ -1768,13 +1775,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_q("sample_aspect_ratio", dec_ctx->sample_aspect_ratio, ':');
-                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_q("display_aspect_ratio", display_aspect_ratio, ':');
+                print_q("display_aspect_ratio", dar, ':');
             } else {
                 print_str_opt("sample_aspect_ratio", "N/A");
                 print_str_opt("display_aspect_ratio", "N/A");
@@ -1831,10 +1839,10 @@ 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");
-    show_tags(stream->metadata);
     if (do_show_data)
         writer_print_data(w, "extradata", dec_ctx->extradata,
                                           dec_ctx->extradata_size);
+    show_tags(stream->metadata);
 
     print_section_footer("stream");
     av_bprint_finalize(&pbuf, NULL);
@@ -2009,8 +2017,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");
@@ -2104,7 +2111,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" },
@@ -2147,6 +2154,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();