Browse code

ffprobe: add count_frames and count_packets options

Signed-off-by: Stefano Sabatini <stefasab@gmail.com>

Matthieu Bouron authored on 2012/02/02 00:23:53
Showing 4 changed files
... ...
@@ -6,6 +6,7 @@ version next:
6 6
 - setfield filter
7 7
 - CDXL demuxer and decoder
8 8
 - Apple ProRes encoder
9
+- ffprobe -count_packets and -count_frames options
9 10
 
10 11
 
11 12
 version 0.10:
... ...
@@ -127,6 +127,14 @@ multimedia stream.
127 127
 Each media stream information is printed within a dedicated section
128 128
 with name "STREAM".
129 129
 
130
+@item -count_frames
131
+Count the number of frames per stream and report it in the
132
+corresponding stream section.
133
+
134
+@item -count_packets
135
+Count the number of packets per stream and report it in the
136
+corresponding stream section.
137
+
130 138
 @item -show_private_data, -private
131 139
 Show private data, that is data depending on the format of the
132 140
 particular shown element.
... ...
@@ -111,6 +111,8 @@
111 111
       <xsd:attribute name="start_time"       type="xsd:float"/>
112 112
       <xsd:attribute name="duration"         type="xsd:float"/>
113 113
       <xsd:attribute name="nb_frames"        type="xsd:int"/>
114
+      <xsd:attribute name="nb_read_frames"   type="xsd:int"/>
115
+      <xsd:attribute name="nb_read_packets"  type="xsd:int"/>
114 116
     </xsd:complexType>
115 117
 
116 118
     <xsd:complexType name="formatType">
... ...
@@ -42,6 +42,10 @@
42 42
 const char program_name[] = "ffprobe";
43 43
 const int program_birth_year = 2007;
44 44
 
45
+static int do_count_frames = 0;
46
+static int do_count_packets = 0;
47
+static int do_read_frames  = 0;
48
+static int do_read_packets = 0;
45 49
 static int do_show_error   = 0;
46 50
 static int do_show_format  = 0;
47 51
 static int do_show_frames  = 0;
... ...
@@ -71,6 +75,8 @@ static const char *unit_second_str          = "s"    ;
71 71
 static const char *unit_hertz_str           = "Hz"   ;
72 72
 static const char *unit_byte_str            = "byte" ;
73 73
 static const char *unit_bit_per_second_str  = "bit/s";
74
+static uint64_t *nb_streams_packets;
75
+static uint64_t *nb_streams_frames;
74 76
 
75 77
 void av_noreturn exit_program(int ret)
76 78
 {
... ...
@@ -1360,7 +1366,7 @@ static av_always_inline int get_decoded_frame(AVFormatContext *fmt_ctx,
1360 1360
     return ret;
1361 1361
 }
1362 1362
 
1363
-static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1363
+static void read_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1364 1364
 {
1365 1365
     AVPacket pkt, pkt1;
1366 1366
     AVFrame frame;
... ...
@@ -1369,18 +1375,23 @@ static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1369 1369
     av_init_packet(&pkt);
1370 1370
 
1371 1371
     while (!av_read_frame(fmt_ctx, &pkt)) {
1372
-        if (do_show_packets)
1373
-            show_packet(w, fmt_ctx, &pkt, i++);
1374
-        if (do_show_frames) {
1372
+        if (do_read_packets) {
1373
+            if (do_show_packets)
1374
+                show_packet(w, fmt_ctx, &pkt, i++);
1375
+            nb_streams_packets[pkt.stream_index]++;
1376
+        }
1377
+        if (do_read_frames) {
1375 1378
             pkt1 = pkt;
1376 1379
             while (1) {
1377 1380
                 avcodec_get_frame_defaults(&frame);
1378 1381
                 ret = get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt1);
1379 1382
                 if (ret < 0 || !got_frame)
1380 1383
                     break;
1381
-                show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1384
+                if (do_show_frames)
1385
+                    show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1382 1386
                 pkt1.data += ret;
1383 1387
                 pkt1.size -= ret;
1388
+                nb_streams_frames[pkt.stream_index]++;
1384 1389
             }
1385 1390
         }
1386 1391
         av_free_packet(&pkt);
... ...
@@ -1391,8 +1402,13 @@ static void show_packets(WriterContext *w, AVFormatContext *fmt_ctx)
1391 1391
     //Flush remaining frames that are cached in the decoder
1392 1392
     for (i = 0; i < fmt_ctx->nb_streams; i++) {
1393 1393
         pkt.stream_index = i;
1394
-        while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame)
1395
-            show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1394
+        while (get_decoded_frame(fmt_ctx, &frame, &got_frame, &pkt) >= 0 && got_frame) {
1395
+            if (do_read_frames) {
1396
+                if (do_show_frames)
1397
+                    show_frame(w, &frame, fmt_ctx->streams[pkt.stream_index]);
1398
+                nb_streams_frames[pkt.stream_index]++;
1399
+            }
1400
+        }
1396 1401
     }
1397 1402
 }
1398 1403
 
... ...
@@ -1495,6 +1511,10 @@ static void show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_i
1495 1495
     print_time("duration",      stream->duration,   &stream->time_base);
1496 1496
     if (stream->nb_frames) print_fmt    ("nb_frames", "%"PRId64, stream->nb_frames);
1497 1497
     else                   print_str_opt("nb_frames", "N/A");
1498
+    if (nb_streams_frames[stream_idx])  print_fmt    ("nb_read_frames", "%"PRIu64, nb_streams_frames[stream_idx]);
1499
+    else                                print_str_opt("nb_read_frames", "N/A");
1500
+    if (nb_streams_packets[stream_idx]) print_fmt    ("nb_read_packets", "%"PRIu64, nb_streams_packets[stream_idx]);
1501
+    else                                print_str_opt("nb_read_packets", "N/A");
1498 1502
     show_tags(stream->metadata);
1499 1503
 
1500 1504
     print_section_footer("stream");
... ...
@@ -1603,9 +1623,14 @@ static int probe_file(WriterContext *wctx, const char *filename)
1603 1603
     AVFormatContext *fmt_ctx;
1604 1604
     int ret, i;
1605 1605
 
1606
+    do_read_frames = do_show_frames || do_count_frames;
1607
+    do_read_packets = do_show_packets || do_count_packets;
1608
+
1606 1609
     ret = open_input_file(&fmt_ctx, filename);
1610
+    nb_streams_frames  = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_frames));
1611
+    nb_streams_packets = av_calloc(fmt_ctx->nb_streams, sizeof(*nb_streams_packets));
1607 1612
     if (ret >= 0) {
1608
-        if (do_show_packets || do_show_frames) {
1613
+        if (do_read_frames || do_read_packets) {
1609 1614
             const char *chapter;
1610 1615
             if (do_show_frames && do_show_packets &&
1611 1616
                 wctx->writer->flags & WRITER_FLAG_PUT_PACKETS_AND_FRAMES_IN_SAME_CHAPTER)
... ...
@@ -1614,9 +1639,11 @@ static int probe_file(WriterContext *wctx, const char *filename)
1614 1614
                 chapter = "packets";
1615 1615
             else // (!do_show_packets && do_show_frames)
1616 1616
                 chapter = "frames";
1617
-            writer_print_chapter_header(wctx, chapter);
1618
-            show_packets(wctx, fmt_ctx);
1619
-            writer_print_chapter_footer(wctx, chapter);
1617
+            if (do_show_frames || do_show_packets)
1618
+                writer_print_chapter_header(wctx, chapter);
1619
+            read_packets(wctx, fmt_ctx);
1620
+            if (do_show_frames || do_show_packets)
1621
+                writer_print_chapter_footer(wctx, chapter);
1620 1622
         }
1621 1623
         PRINT_CHAPTER(streams);
1622 1624
         PRINT_CHAPTER(format);
... ...
@@ -1626,6 +1653,9 @@ static int probe_file(WriterContext *wctx, const char *filename)
1626 1626
         avformat_close_input(&fmt_ctx);
1627 1627
     }
1628 1628
 
1629
+    av_freep(&nb_streams_frames);
1630
+    av_freep(&nb_streams_packets);
1631
+
1629 1632
     return ret;
1630 1633
 }
1631 1634
 
... ...
@@ -1753,6 +1783,8 @@ static const OptionDef options[] = {
1753 1753
     { "show_frames",  OPT_BOOL, {(void*)&do_show_frames} , "show frames info" },
1754 1754
     { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
1755 1755
     { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
1756
+    { "count_frames", OPT_BOOL, {(void*)&do_count_frames}, "count the number of frames per stream" },
1757
+    { "count_packets", OPT_BOOL, {(void*)&do_count_packets}, "count the number of packets per stream" },
1756 1758
     { "show_program_version",  OPT_BOOL, {(void*)&do_show_program_version},  "show ffprobe version" },
1757 1759
     { "show_library_versions", OPT_BOOL, {(void*)&do_show_library_versions}, "show library versions" },
1758 1760
     { "show_versions",         0, {(void*)&opt_show_versions}, "show program and library versions" },