ffprobe: add writer_print_rational()
authorStefano Sabatini <stefasab@gmail.com>
Wed, 13 Jun 2012 12:33:32 +0000 (14:33 +0200)
committerStefano Sabatini <stefasab@gmail.com>
Fri, 15 Jun 2012 22:42:54 +0000 (00:42 +0200)
Improve overall consistency, allow some factorization.

ffprobe.c

index b87059e..0827983 100644 (file)
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -165,6 +165,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 +310,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)
 {
@@ -1508,6 +1519,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)
@@ -1580,9 +1592,7 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream)
         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);
+            print_q("sample_aspect_ratio", frame->sample_aspect_ratio, ':');
         } else {
             print_str_opt("sample_aspect_ratio", "N/A");
         }
@@ -1710,7 +1720,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);
@@ -1723,16 +1733,12 @@ static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i
             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);
+                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,
                           1024*1024);
-                print_fmt("display_aspect_ratio", "%d:%d",
-                          display_aspect_ratio.num,
-                          display_aspect_ratio.den);
+                print_q("display_aspect_ratio", display_aspect_ratio, ':');
             } else {
                 print_str_opt("sample_aspect_ratio", "N/A");
                 print_str_opt("display_aspect_ratio", "N/A");
@@ -1776,9 +1782,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);