Now the report is printed even if poll_filters() does not
return often.
---

 avconv.c | 108 +++++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 64 insertions(+), 44 deletions(-)

diff --git a/avconv.c b/avconv.c
index 6b37d6e..676e81d 100644
--- a/avconv.c
+++ b/avconv.c
@@ -699,8 +699,27 @@ static int poll_filters(void)
     return ret;
 }

-static void print_final_stats(int64_t total_size)
+static uint64_t get_total_size(AVFormatContext *oc)
 {
+    int64_t total_size;
+
+    total_size = avio_size(oc->pb);
+    if (total_size <= 0) // FIXME improve avio_size() so it works with non 
seekable output too
+        total_size = avio_tell(oc->pb);
+    if (total_size < 0) {
+        char errbuf[128];
+        av_strerror(total_size, errbuf, sizeof(errbuf));
+        av_log(NULL, AV_LOG_VERBOSE, "Bitrate not available, "
+               "avio_tell() failed: %s\n", errbuf);
+        total_size = 0;
+    }
+
+    return total_size;
+}
+
+static void print_final_stats(void)
+{
+    uint64_t total_size = get_total_size(output_files[0]->ctx);
     uint64_t video_size = 0, audio_size = 0, extra_size = 0, other_size = 0;
     uint64_t data_size = 0;
     float percent = -1.0;
@@ -802,7 +821,7 @@ static void print_final_stats(int64_t total_size)
     }
 }

-static void print_report(int is_last_report, int64_t timer_start)
+static void print_report(int64_t timer_start)
 {
     char buf[1024];
     OutputStream *ost;
@@ -813,36 +832,24 @@ static void print_report(int is_last_report, int64_t 
timer_start)
     double bitrate, ti1, pts;
     static int64_t last_time = -1;
     static int qp_histogram[52];
+    int64_t cur_time;

-    if (!print_stats && !is_last_report)
+    if (!print_stats)
         return;

-    if (!is_last_report) {
-        int64_t cur_time;
-        /* display the report every 0.5 seconds */
-        cur_time = av_gettime_relative();
-        if (last_time == -1) {
-            last_time = cur_time;
-            return;
-        }
-        if ((cur_time - last_time) < 500000)
-            return;
+    /* display the report every 0.5 seconds */
+    cur_time = av_gettime_relative();
+    if (last_time == -1) {
         last_time = cur_time;
+        return;
     }
-
+    if ((cur_time - last_time) < 500000)
+        return;
+    last_time = cur_time;

     oc = output_files[0]->ctx;

-    total_size = avio_size(oc->pb);
-    if (total_size <= 0) // FIXME improve avio_size() so it works with non 
seekable output too
-        total_size = avio_tell(oc->pb);
-    if (total_size < 0) {
-        char errbuf[128];
-        av_strerror(total_size, errbuf, sizeof(errbuf));
-        av_log(NULL, AV_LOG_VERBOSE, "Bitrate not available, "
-               "avio_tell() failed: %s\n", errbuf);
-        total_size = 0;
-    }
+    total_size = get_total_size(oc);

     buf[0] = '\0';
     ti1 = 1e10;
@@ -863,8 +870,6 @@ static void print_report(int is_last_report, int64_t 
timer_start)
             frame_number = ost->frame_number;
             snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "frame=%5d 
fps=%3d q=%3.1f ",
                      frame_number, (t > 1) ? (int)(frame_number / t + 0.5) : 
0, q);
-            if (is_last_report)
-                snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "L");
             if (qp_hist) {
                 int j;
                 int qp = lrintf(q);
@@ -883,13 +888,9 @@ FF_DISABLE_DEPRECATION_WARNINGS
                 char type[3] = { 'Y','U','V' };
                 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), 
"PSNR=");
                 for (j = 0; j < 3; j++) {
-                    if (is_last_report) {
-                        error = enc->error[j];
-                        scale = enc->width * enc->height * 255.0 * 255.0 * 
frame_number;
-                    } else {
-                        error = enc->coded_frame->error[j];
-                        scale = enc->width * enc->height * 255.0 * 255.0;
-                    }
+                    error = enc->coded_frame->error[j];
+                    scale = enc->width * enc->height * 255.0 * 255.0;
+
                     if (j)
                         scale /= 4;
                     error_sum += error;
@@ -923,10 +924,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
     av_log(NULL, AV_LOG_INFO, "%s    \r", buf);

     fflush(stderr);
-
-    if (is_last_report)
-        print_final_stats(total_size);
-
 }

 static void flush_encoders(void)
@@ -2243,6 +2240,25 @@ static int get_input_packet_mt(InputFile *f, AVPacket 
*pkt)

     return ret;
 }
+
+static void *ui_thread(void *unused)
+{
+    int64_t start_time = av_gettime_relative();
+
+    while (!transcoding_finished) {
+        print_report(start_time);
+        av_usleep(500 * 1000);
+    }
+
+    return NULL;
+}
+
+static int init_ui_thread(pthread_t *ui)
+{
+    int ret = pthread_create(ui, NULL, ui_thread, NULL);
+
+    return AVERROR(ret);
+}
 #endif

 static int get_input_packet(InputFile *f, AVPacket *pkt)
@@ -2525,7 +2541,11 @@ static int transcode(void)
     AVFormatContext *os;
     OutputStream *ost;
     InputStream *ist;
-    int64_t timer_start;
+#if HAVE_PTHREADS
+    pthread_t ui_thread;
+#else
+    int64_t start_time = av_gettime_relative();
+#endif

     ret = transcode_init();
     if (ret < 0)
@@ -2534,11 +2554,11 @@ static int transcode(void)
     av_log(NULL, AV_LOG_INFO, "Press ctrl-c to stop encoding\n");
     term_init();

-    timer_start = av_gettime_relative();
-
 #if HAVE_PTHREADS
     if ((ret = init_input_threads()) < 0)
         goto fail;
+    if ((ret = init_ui_thread(&ui_thread)) < 0)
+        goto fail;
 #endif

     while (!received_sigterm) {
@@ -2563,9 +2583,9 @@ static int transcode(void)
             av_log(NULL, AV_LOG_ERROR, "Error while filtering: %s\n", errbuf);
             break;
         }
-
-        /* dump report by using the output first video and audio streams */
-        print_report(0, timer_start);
+#if !HAVE_PTHREADS
+        print_report(start_time);
+#endif
     }
 #if HAVE_PTHREADS
     free_input_threads();
@@ -2590,7 +2610,7 @@ static int transcode(void)
     }

     /* dump report by using the first video and audio streams */
-    print_report(1, timer_start);
+    print_final_stats();

     /* close each encoder */
     for (i = 0; i < nb_output_streams; i++) {
--
2.6.1

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to