diff mbox series

[FFmpeg-devel,02/24] ffmpeg: simplify getting output file size

Message ID 20211213152042.5900-2-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/24] ffmpeg: pass the muxer context explicitly to some functions | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Anton Khirnov Dec. 13, 2021, 3:20 p.m. UTC
Stop calling avio_size()/avio_tell(), which are potentially heavy
operations and may interfere with muxer operation. Use the recently
introduced bytes_written field instead.
---
 fftools/ffmpeg.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)

Comments

Andreas Rheinhardt Dec. 16, 2021, 11:54 a.m. UTC | #1
Anton Khirnov:
> Stop calling avio_size()/avio_tell(), which are potentially heavy
> operations and may interfere with muxer operation. Use the recently
> introduced bytes_written field instead.
> ---
>  fftools/ffmpeg.c | 10 +++-------
>  1 file changed, 3 insertions(+), 7 deletions(-)
> 
> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
> index bdeff9a12e..b77531cb7f 100644
> --- a/fftools/ffmpeg.c
> +++ b/fftools/ffmpeg.c
> @@ -1677,8 +1677,7 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
>  {
>      AVBPrint buf, buf_script;
>      OutputStream *ost;
> -    AVFormatContext *oc;
> -    int64_t total_size;
> +    int64_t total_size = -1;
>      AVCodecContext *enc;
>      int frame_number, vid, i;
>      double bitrate;
> @@ -1708,11 +1707,8 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
>      t = (cur_time-timer_start) / 1000000.0;
>  
>  
> -    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 (output_files[0]->ctx->pb)
> +        total_size = output_files[0]->ctx->pb->bytes_written;
>  
>      vid = 0;
>      av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
> 

1. bytes_written is only updated when the data is actually written, i.e.
the stuff still residing in the AVIOContext's internal buffer is not
accounted yet. This might lead to the bitrate being significantly
underestimated at the beginning of output.
2. In case of seekable output when data already written is overwritten
the new stats will be totally off. With -movflags +faststart the final
size is off by a factor of two.

- Andreas
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index bdeff9a12e..b77531cb7f 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1677,8 +1677,7 @@  static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
 {
     AVBPrint buf, buf_script;
     OutputStream *ost;
-    AVFormatContext *oc;
-    int64_t total_size;
+    int64_t total_size = -1;
     AVCodecContext *enc;
     int frame_number, vid, i;
     double bitrate;
@@ -1708,11 +1707,8 @@  static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
     t = (cur_time-timer_start) / 1000000.0;
 
 
-    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 (output_files[0]->ctx->pb)
+        total_size = output_files[0]->ctx->pb->bytes_written;
 
     vid = 0;
     av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);