diff mbox series

[FFmpeg-devel,2/4] ffmpeg: move AVFrame time base adjustment into a function

Message ID 20200913102622.168011-3-jeebjp@gmail.com
State Superseded
Headers show
Series ffmpeg: late A/V encoder init, AVFrame metadata usage | expand

Checks

Context Check Description
andriy/default pending
andriy/make_warn warning New warnings during build
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Jan Ekström Sept. 13, 2020, 10:26 a.m. UTC
This will have to be called later for video down the line.
---
 fftools/ffmpeg.c | 77 ++++++++++++++++++++++++++++++++----------------
 1 file changed, 52 insertions(+), 25 deletions(-)

Comments

Michael Niedermayer Sept. 13, 2020, 4:14 p.m. UTC | #1
On Sun, Sep 13, 2020 at 01:26:20PM +0300, Jan Ekström wrote:
> This will have to be called later for video down the line.
> ---
>  fftools/ffmpeg.c | 77 ++++++++++++++++++++++++++++++++----------------
>  1 file changed, 52 insertions(+), 25 deletions(-)

This affects the output, example:
./ffmpeg -i mm-small.mpg -vf idet  -v 50 -bitexact -f null - 2>&1 | grep idet

Reading option '-vf' ... matched as option 'vf' (set video filters) with argument 'idet'.
Applying option vf (set video filters) with argument idet.
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame: progressive, Multi frame: progressive
[Parsed_idet_0 @ 0x557fe4426b40] Repeated Field:     neither, Single frame:undetermined, Multi frame: progressive

VS:

Reading option '-vf' ... matched as option 'vf' (set video filters) with argument 'idet'.
Applying option vf (set video filters) with argument idet.
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:undetermined
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame: progressive, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame: progressive, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:         tff, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:         tff, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:         tff, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:         tff, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:         tff, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:         bff, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame: progressive
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:         bff, Multi frame:         bff
[Parsed_idet_0 @ 0x5567cbd30b40] Repeated Field:     neither, Single frame:undetermined, Multi frame:         bff

[...]
Jan Ekström Sept. 13, 2020, 6:07 p.m. UTC | #2
On Sun, Sep 13, 2020 at 7:14 PM Michael Niedermayer
<michael@niedermayer.cc> wrote:
>
> On Sun, Sep 13, 2020 at 01:26:20PM +0300, Jan Ekström wrote:
> > This will have to be called later for video down the line.
> > ---
> >  fftools/ffmpeg.c | 77 ++++++++++++++++++++++++++++++++----------------
> >  1 file changed, 52 insertions(+), 25 deletions(-)
>
> This affects the output, example:
> ./ffmpeg -i mm-small.mpg -vf idet  -v 50 -bitexact -f null - 2>&1 | grep idet
>

Apparently this was due to the leftover logging I left there. This
also happens with vanilla FFmpeg if you add such logging to
reap_filters.

In other words, just this following diff appears to fix it.
Additionally, I will be moving the debug_ts log line back to
reap_filters for now since we want to know of the AV_NOPTS_VALUE
frames as well.

Jan

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 7e6c0a962b..acaf6bc47a 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -914,12 +914,6 @@ static double
adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
     int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);

     tb.den <<= extra_bits;
-    av_log(NULL, AV_LOG_VERBOSE,
-           "%s: frame pts: %"PRId64" start_time: %"PRId64", "
-           "filter_tb: %d/%d, tb: %d/%d\n",
-           __FUNCTION__, frame->pts, start_time,
-           filter_tb.num, filter_tb.den,
-           tb.num, tb.den);

     float_pts =
         av_rescale_q(frame->pts, filter_tb, tb) -
@@ -932,10 +926,6 @@ static double
adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
         av_rescale_q(frame->pts, filter_tb, enc->time_base) -
         av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);

-    av_log(NULL, AV_LOG_VERBOSE,
-           "%s: post-adjustment PTS: %"PRId64"\n",
-           __FUNCTION__, frame->pts);
-
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s
exact:%f time_base:%d/%d\n",
                 av_ts2str(frame->pts), av_ts2timestr(frame->pts,
&enc->time_base),
Michael Niedermayer Sept. 13, 2020, 7:43 p.m. UTC | #3
On Sun, Sep 13, 2020 at 09:07:09PM +0300, Jan Ekström wrote:
> On Sun, Sep 13, 2020 at 7:14 PM Michael Niedermayer
> <michael@niedermayer.cc> wrote:
> >
> > On Sun, Sep 13, 2020 at 01:26:20PM +0300, Jan Ekström wrote:
> > > This will have to be called later for video down the line.
> > > ---
> > >  fftools/ffmpeg.c | 77 ++++++++++++++++++++++++++++++++----------------
> > >  1 file changed, 52 insertions(+), 25 deletions(-)
> >
> > This affects the output, example:
> > ./ffmpeg -i mm-small.mpg -vf idet  -v 50 -bitexact -f null - 2>&1 | grep idet
> >
> 
> Apparently this was due to the leftover logging I left there. This
> also happens with vanilla FFmpeg if you add such logging to
> reap_filters.
> 
> In other words, just this following diff appears to fix it.

That was not what i expected to cause this but in fact you are correct.
this is a issue with log repeation removial changes from interspaced
debug messages. So this is resolved

for the record as others might run into this
use "-v repeat+50" instead of -v 50 to avoid this

thx

[...]
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index cb7644de6a..7e6c0a962b 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -897,6 +897,55 @@  static int check_recording_time(OutputStream *ost)
     return 1;
 }
 
+static double adjust_frame_pts_to_encoder_tb(OutputFile *of, OutputStream *ost,
+                                             AVFrame *frame)
+{
+    if (!frame || frame->pts == AV_NOPTS_VALUE ||
+        !ost->filter || !ost->filter->graph->graph)
+        return AV_NOPTS_VALUE;
+
+    double float_pts = AV_NOPTS_VALUE; // this is identical to frame.pts but with higher precision
+    AVFilterContext *filter = ost->filter->filter;
+    AVCodecContext *enc = ost->enc_ctx;
+
+    int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
+    AVRational filter_tb = av_buffersink_get_time_base(filter);
+    AVRational tb = enc->time_base;
+    int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
+
+    tb.den <<= extra_bits;
+    av_log(NULL, AV_LOG_VERBOSE,
+           "%s: frame pts: %"PRId64" start_time: %"PRId64", "
+           "filter_tb: %d/%d, tb: %d/%d\n",
+           __FUNCTION__, frame->pts, start_time,
+           filter_tb.num, filter_tb.den,
+           tb.num, tb.den);
+
+    float_pts =
+        av_rescale_q(frame->pts, filter_tb, tb) -
+        av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
+    float_pts /= 1 << extra_bits;
+    // avoid exact midoints to reduce the chance of rounding differences, this can be removed in case the fps code is changed to work with integers
+    float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
+
+    frame->pts =
+        av_rescale_q(frame->pts, filter_tb, enc->time_base) -
+        av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
+
+    av_log(NULL, AV_LOG_VERBOSE,
+           "%s: post-adjustment PTS: %"PRId64"\n",
+           __FUNCTION__, frame->pts);
+
+    if (debug_ts) {
+        av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
+                av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
+                float_pts,
+                enc->time_base.num, enc->time_base.den);
+    }
+
+    return float_pts;
+}
+
 static void do_audio_out(OutputFile *of, OutputStream *ost,
                          AVFrame *frame)
 {
@@ -1473,37 +1522,15 @@  static int reap_filters(int flush)
                 av_frame_unref(filtered_frame);
                 continue;
             }
-            if (filtered_frame->pts != AV_NOPTS_VALUE) {
-                int64_t start_time = (of->start_time == AV_NOPTS_VALUE) ? 0 : of->start_time;
-                AVRational filter_tb = av_buffersink_get_time_base(filter);
-                AVRational tb = enc->time_base;
-                int extra_bits = av_clip(29 - av_log2(tb.den), 0, 16);
-
-                tb.den <<= extra_bits;
-                float_pts =
-                    av_rescale_q(filtered_frame->pts, filter_tb, tb) -
-                    av_rescale_q(start_time, AV_TIME_BASE_Q, tb);
-                float_pts /= 1 << extra_bits;
-                // avoid exact midoints to reduce the chance of rounding differences, this can be removed in case the fps code is changed to work with integers
-                float_pts += FFSIGN(float_pts) * 1.0 / (1<<17);
-
-                filtered_frame->pts =
-                    av_rescale_q(filtered_frame->pts, filter_tb, enc->time_base) -
-                    av_rescale_q(start_time, AV_TIME_BASE_Q, enc->time_base);
-            }
+
+            float_pts = adjust_frame_pts_to_encoder_tb(of, ost,
+                                                       filtered_frame);
 
             switch (av_buffersink_get_type(filter)) {
             case AVMEDIA_TYPE_VIDEO:
                 if (!ost->frame_aspect_ratio.num)
                     enc->sample_aspect_ratio = filtered_frame->sample_aspect_ratio;
 
-                if (debug_ts) {
-                    av_log(NULL, AV_LOG_INFO, "filter -> pts:%s pts_time:%s exact:%f time_base:%d/%d\n",
-                            av_ts2str(filtered_frame->pts), av_ts2timestr(filtered_frame->pts, &enc->time_base),
-                            float_pts,
-                            enc->time_base.num, enc->time_base.den);
-                }
-
                 do_video_out(of, ost, filtered_frame, float_pts);
                 break;
             case AVMEDIA_TYPE_AUDIO: