]> rtime.felk.cvut.cz Git - frescor/demo.git/blobdiff - build/recorder/ffmpeg.c
Calculate and print timing characteristics
[frescor/demo.git] / build / recorder / ffmpeg.c
index f4feb170d48e61b1e305d0c63adeaab41b6ed51e..259863ea7a1dd595339a2b9cbe268026cdb14b72 100644 (file)
@@ -1558,6 +1558,55 @@ static int stream_index_from_inputs(AVFormatContext **input_files,
     return -1;
 }
 
+int
+timespec_subtract (struct timespec *result,
+                  struct timespec *x,
+                  struct timespec *y)
+{
+  /* Perform the carry for the later subtraction by updating Y. */
+  if (x->tv_nsec < y->tv_nsec) {
+    int num_sec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
+    y->tv_nsec -= 1000000000 * num_sec;
+    y->tv_sec += num_sec;
+  }
+  if (x->tv_nsec - y->tv_nsec > 1000000000) {
+    int num_sec = (x->tv_nsec - y->tv_nsec) / 1000000000;
+    y->tv_nsec += 1000000000 * num_sec;
+    y->tv_sec -= num_sec;
+  }
+
+  /* Compute the time remaining to wait.
+     `tv_nsec' is certainly positive. */
+  result->tv_sec = x->tv_sec - y->tv_sec;
+  result->tv_nsec = x->tv_nsec - y->tv_nsec;
+
+  /* Return 1 if result is negative. */
+  return x->tv_sec < y->tv_sec;
+}
+
+static void
+print_timing(void)
+{
+       static struct timespec start = {0,0};
+       struct timespec end, d;
+       static int f = 0;       /* number of frames */
+       double ifi;
+       static double ifi_avg=0, ifi_var=0;
+
+       clock_gettime(CLOCK_MONOTONIC, &end);
+       timespec_subtract(&d, &end, &start);
+       if (f++ == 0)
+               goto out;       /* First run */
+       ifi = (double)d.tv_sec + 1e-9*d.tv_nsec;
+#define SQ(x) ((x)*(x))        
+       ifi_var = ifi_var*(f-1)/f + (double)(f-1)/SQ(f)*SQ(ifi-ifi_avg);
+       ifi_avg = ifi_avg*(f-1)/f + (double)ifi/f;
+       printf("%5d: interframe interval = 1/%5.2lf s  avg=1/%.2f  stddev=1/%3.2f\n",
+              f, 1/ifi, 1/ifi_avg, 1/sqrt(ifi_var));
+out:
+       start = end;
+}
+
 /*
  * The following code is the main loop of the file converter
  */
@@ -2213,11 +2262,13 @@ static int av_encode(AVFormatContext **output_files,
             goto redo;
         }
 
+       print_timing();
+       
     discard_packet:
         av_free_packet(&pkt);
 
         /* dump report by using the output first video and audio streams */
-        print_report(output_files, ost_table, nb_ostreams, 0);
+        //print_report(output_files, ost_table, nb_ostreams, 0);
     }
 
     /* at the end of stream, we must flush the decoder buffers */