diff mbox series

[FFmpeg-devel,07/10] fftools/ffmpeg_filter: refactor setting input timebase

Message ID 20240214182435.31380-7-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/10] fftools/ffmpeg_filter: stop taking display matrix from global side data | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov Feb. 14, 2024, 6:24 p.m. UTC
Treat it analogously to stream parameters like format/dimensions/etc.
This is functionally different from previous code in 2 ways:
* for non-CFR video, the frame timebase (set by the decoder) is used
  rather than the demuxer timebase
* for sub2video, AV_TIME_BASE_Q is used, which is hardcoded by the
  subtitle decoding API

These changes should avoid unnecessary and potentially lossy timestamp
conversions from decoder timebase into the demuxer one.

Changes the timebases used in sub2video tests.
---
 fftools/ffmpeg_filter.c               |  17 ++-
 tests/ref/fate/sub2video_basic        | 182 +++++++++++++-------------
 tests/ref/fate/sub2video_time_limited |   8 +-
 3 files changed, 106 insertions(+), 101 deletions(-)

Comments

Michael Niedermayer Feb. 15, 2024, 1:58 a.m. UTC | #1
On Wed, Feb 14, 2024 at 07:24:32PM +0100, Anton Khirnov wrote:
> Treat it analogously to stream parameters like format/dimensions/etc.
> This is functionally different from previous code in 2 ways:
> * for non-CFR video, the frame timebase (set by the decoder) is used
>   rather than the demuxer timebase
> * for sub2video, AV_TIME_BASE_Q is used, which is hardcoded by the
>   subtitle decoding API
> 
> These changes should avoid unnecessary and potentially lossy timestamp
> conversions from decoder timebase into the demuxer one.
> 
> Changes the timebases used in sub2video tests.
> ---
>  fftools/ffmpeg_filter.c               |  17 ++-
>  tests/ref/fate/sub2video_basic        | 182 +++++++++++++-------------
>  tests/ref/fate/sub2video_time_limited |   8 +-
>  3 files changed, 106 insertions(+), 101 deletions(-)

breaks:

./ffmpeg -i \[a-s\]_full_metal_panic_fumoffu_-_01_-_the_man_from_the_south_-_a_hostage_with_no_compromises__rs2_\[1080p_bd-rip\]\[BBB48A25\].mkv  -filter_complex '[0:s:1]scale=800:600' -t 15 -qscale 2 -bitexact -y /tmp/file-pgstest2.avi
...
Press [q] to stop, [?] for help
[aac @ 0x55dcd4a5a940] This stream seems to incorrectly report its last channel as LFE[3], mapping to LFE[0]
[mpeg4 @ 0x55dcd4a5b780] timebase 1/1000000 not supported by MPEG 4 standard, the maximum admitted value for the timebase denominator is 65535
[vost#0:0/mpeg4 @ 0x55dcd4a5b2c0] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height.
[fc#0 @ 0x55dcd4a37a00] Error sending frames to consumers: Invalid argument
[fc#0 @ 0x55dcd4a37a00] Task finished with error code: -22 (Invalid argument)
[fc#0 @ 0x55dcd4a37a00] Terminating thread with return code -22 (Invalid argument)
[vost#0:0/mpeg4 @ 0x55dcd4a5b2c0] Could not open encoder before EOF
[vost#0:0/mpeg4 @ 0x55dcd4a5b2c0] Task finished with error code: -22 (Invalid argument)
[vost#0:0/mpeg4 @ 0x55dcd4a5b2c0] Terminating thread with return code -22 (Invalid argument)
[libmp3lame @ 0x55dcd4a5ca80] Trying to remove 1152 samples, but the queue is empty
[out#0/avi @ 0x55dcd4a7f680] Nothing was written into output file, because at least one of its streams received no packets.
frame=    0 fps=0.0 q=0.0 Lsize=       0KiB time=N/A bitrate=N/A speed=N/A
Conversion failed!

vs.

Press [q] to stop, [?] for help
[aac @ 0x55b416e87e00] This stream seems to incorrectly report its last channel as LFE[3], mapping to LFE[0]
Output #0, avi, to '/tmp/file-pgstest2.avi':
  Stream #0:0: Video: mpeg4 (FMP4 / 0x34504D46), yuv420p(progressive), 800x600, q=2-31, 200 kb/s, 1k tbn
      Metadata:
        encoder         : Lavc mpeg4
      Side data:
        cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: N/A
  Stream #0:1(eng): Audio: mp3 (U[0][0][0] / 0x0055), 48000 Hz, stereo, fltp (default)
      Metadata:
        title           : AAC
        encoder         : Lavc libmp3lame
[libmp3lame @ 0x55b416e76640] Trying to remove 1152 samples, but the queue is empty
[out#0/avi @ 0x55b416e7fec0] video:496KiB audio:235KiB subtitle:0KiB other streams:0KiB global headers:0KiB muxing overhead: 51.412292%
frame=  360 fps=0.0 q=2.0 Lsize=    1108KiB time=00:00:14.97 bitrate= 605.9kbits/s dup=0 drop=16 speed=44.9x


[...]
diff mbox series

Patch

diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index dc65b441a6..a13b2ccf95 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -148,6 +148,8 @@  typedef struct InputFilterPriv {
 
     // fallback parameters to use when no input is ever sent
     struct {
+        AVRational          time_base;
+
         int                 format;
 
         int                 width;
@@ -697,6 +699,8 @@  static int ifilter_bind_ist(InputFilter *ifilter, InputStream *ist)
            palettes for all rectangles are identical or compatible */
         ifp->format = AV_PIX_FMT_RGB32;
 
+        ifp->time_base = AV_TIME_BASE_Q;
+
         av_log(fgp, AV_LOG_VERBOSE, "sub2video: using %dx%d canvas\n",
                ifp->width, ifp->height);
     }
@@ -1485,7 +1489,6 @@  static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
     AVFilterContext *last_filter;
     const AVFilter *buffer_filt = avfilter_get_by_name("buffer");
     const AVPixFmtDescriptor *desc;
-    InputStream *ist = ifp->ist;
     AVRational fr = ifp->opts.framerate;
     AVRational sar;
     AVBPrint args;
@@ -1498,9 +1501,6 @@  static int configure_input_video_filter(FilterGraph *fg, AVFilterGraph *graph,
     if (ifp->type_src == AVMEDIA_TYPE_SUBTITLE)
         sub2video_prepare(ifp);
 
-    ifp->time_base = (ifp->opts.flags & IFILTER_FLAG_CFR) ?
-                     av_inv_q(ifp->opts.framerate) : ist->st->time_base;
-
     sar = ifp->sample_aspect_ratio;
     if(!sar.den)
         sar = (AVRational){0,1};
@@ -1591,8 +1591,6 @@  static int configure_input_audio_filter(FilterGraph *fg, AVFilterGraph *graph,
     char name[255];
     int ret, pad_idx = 0;
 
-    ifp->time_base = (AVRational){ 1, ifp->sample_rate };
-
     av_bprint_init(&args, 0, AV_BPRINT_SIZE_AUTOMATIC);
     av_bprintf(&args, "time_base=%d/%d:sample_rate=%d:sample_fmt=%s",
                ifp->time_base.num, ifp->time_base.den,
@@ -1821,6 +1819,8 @@  int ifilter_parameters_from_dec(InputFilter *ifilter, const AVCodecContext *dec)
 {
     InputFilterPriv *ifp = ifp_from_ifilter(ifilter);
 
+    ifp->fallback.time_base = dec->pkt_timebase;
+
     if (dec->codec_type == AVMEDIA_TYPE_VIDEO) {
         ifp->fallback.format                 = dec->pix_fmt;
         ifp->fallback.width                  = dec->width;
@@ -1852,6 +1852,10 @@  static int ifilter_parameters_from_frame(InputFilter *ifilter, const AVFrame *fr
     if (ret < 0)
         return ret;
 
+    ifp->time_base = (ifp->type == AVMEDIA_TYPE_AUDIO)    ? (AVRational){ 1, frame->sample_rate } :
+                     (ifp->opts.flags & IFILTER_FLAG_CFR) ? av_inv_q(ifp->opts.framerate)         :
+                     frame->time_base;
+
     ifp->format              = frame->format;
 
     ifp->width               = frame->width;
@@ -2541,6 +2545,7 @@  static int send_eof(FilterGraphThread *fgt, InputFilter *ifilter,
             ifp->sample_aspect_ratio    = ifp->fallback.sample_aspect_ratio;
             ifp->color_space            = ifp->fallback.color_space;
             ifp->color_range            = ifp->fallback.color_range;
+            ifp->time_base              = ifp->fallback.time_base;
 
             ret = av_channel_layout_copy(&ifp->ch_layout,
                                          &ifp->fallback.ch_layout);
diff --git a/tests/ref/fate/sub2video_basic b/tests/ref/fate/sub2video_basic
index a6eb1a34ea..2e4dcb625e 100644
--- a/tests/ref/fate/sub2video_basic
+++ b/tests/ref/fate/sub2video_basic
@@ -1,95 +1,95 @@ 
-#tb 0: 1/1000
+#tb 0: 1/1000000
 #media_type 0: video
 #codec_id 0: rawvideo
 #dimensions 0: 720x480
 #sar 0: 0/1
-0,     132499,     132499,        0,  1382400, 0x00000000
-0,     132499,     132499,        0,  1382400, 0x8c93c2ba
-0,     137459,     137459,        0,  1382400, 0x00000000
-0,     147355,     147355,        0,  1382400, 0xb02e32ca
-0,     152088,     152088,        0,  1382400, 0x00000000
-0,     180797,     180797,        0,  1382400, 0x83b71116
-0,     183357,     183357,        0,  1382400, 0x00000000
-0,     183433,     183433,        0,  1382400, 0x85547fd1
-0,     185799,     185799,        0,  1382400, 0x00000000
-0,     185909,     185909,        0,  1382400, 0x00000000
-0,     185910,     185910,        0,  1382400, 0xb6a8f181
-0,     188606,     188606,        0,  1382400, 0x00000000
-0,     188663,     188663,        0,  1382400, 0xb64d1a2c
-0,     189925,     189925,        0,  1382400, 0x00000000
-0,     190014,     190014,        0,  1382400, 0x7b37ecf3
-0,     191675,     191675,        0,  1382400, 0x00000000
-0,     199724,     199724,        0,  1382400, 0xdc025bd1
-0,     201089,     201089,        0,  1382400, 0x00000000
-0,     201175,     201175,        0,  1382400, 0x688b294d
-0,     202733,     202733,        0,  1382400, 0x00000000
-0,     202819,     202819,        0,  1382400, 0xa2b33d1b
-0,     204684,     204684,        0,  1382400, 0x00000000
-0,     204762,     204762,        0,  1382400, 0xb3e525e3
-0,     206730,     206730,        0,  1382400, 0x00000000
-0,     206806,     206806,        0,  1382400, 0xaa8fbdd7
-0,     208637,     208637,        0,  1382400, 0x00000000
-0,     208716,     208716,        0,  1382400, 0x7b7f26dd
-0,     209978,     209978,        0,  1382400, 0x00000000
-0,     210051,     210051,        0,  1382400, 0x15e2f836
-0,     211575,     211575,        0,  1382400, 0x00000000
-0,     211644,     211644,        0,  1382400, 0x0fee9b0c
-0,     214306,     214306,        0,  1382400, 0x00000000
-0,     214380,     214380,        0,  1382400, 0x89d62791
-0,     217144,     217144,        0,  1382400, 0x00000000
-0,     217225,     217225,        0,  1382400, 0xa6a9fd74
-0,     219591,     219591,        0,  1382400, 0x00000000
-0,     219652,     219652,        0,  1382400, 0x7896178d
-0,     221483,     221483,        0,  1382400, 0x00000000
-0,     223531,     223531,        0,  1382400, 0x01751a52
-0,     225863,     225863,        0,  1382400, 0x00000000
-0,     227510,     227510,        0,  1382400, 0xa3959c6f
-0,     230809,     230809,        0,  1382400, 0x00000000
-0,     230872,     230872,        0,  1382400, 0x3d3ea47b
-0,     233033,     233033,        0,  1382400, 0x00000000
-0,     233124,     233124,        0,  1382400, 0x593f8b24
-0,     237220,     237220,        0,  1382400, 0x00000000
-0,     237303,     237303,        0,  1382400, 0x171f05ba
-0,     240033,     240033,        0,  1382400, 0x00000000
-0,     240106,     240106,        0,  1382400, 0xb014cdf1
-0,     242165,     242165,        0,  1382400, 0x00000000
-0,     273556,     273556,        0,  1382400, 0xd918e667
-0,     275217,     275217,        0,  1382400, 0x00000000
-0,     295445,     295445,        0,  1382400, 0xc9406331
-0,     296776,     296776,        0,  1382400, 0x00000000
-0,     300049,     300049,        0,  1382400, 0xaf08b10d
-0,     301949,     301949,        0,  1382400, 0x00000000
-0,     302034,     302034,        0,  1382400, 0x00000000
-0,     302035,     302035,        0,  1382400, 0x853a9d93
-0,     303559,     303559,        0,  1382400, 0x00000000
-0,     304203,     304203,        0,  1382400, 0x7491a87d
-0,     305898,     305898,        0,  1382400, 0x00000000
-0,     305947,     305947,        0,  1382400, 0xf7383c58
-0,     307881,     307881,        0,  1382400, 0x00000000
-0,     307957,     307957,        0,  1382400, 0xe66be411
-0,     309720,     309720,        0,  1382400, 0x00000000
-0,     321295,     321295,        0,  1382400, 0xd6850362
-0,     323263,     323263,        0,  1382400, 0x00000000
-0,     323356,     323356,        0,  1382400, 0x3e1ed109
-0,     324584,     324584,        0,  1382400, 0x00000000
-0,     324640,     324640,        0,  1382400, 0x39c1b7bd
-0,     326403,     326403,        0,  1382400, 0x00000000
-0,     327193,     327193,        0,  1382400, 0x35b85f2e
-0,     328285,     328285,        0,  1382400, 0x00000000
-0,     328360,     328360,        0,  1382400, 0x00000000
-0,     328361,     328361,        0,  1382400, 0x83f103e5
-0,     329885,     329885,        0,  1382400, 0x00000000
-0,     329946,     329946,        0,  1382400, 0xbc1ca9b3
-0,     331106,     331106,        0,  1382400, 0x00000000
-0,     331230,     331230,        0,  1382400, 0x94d4a51e
-0,     332857,     332857,        0,  1382400, 0x00000000
-0,     332924,     332924,        0,  1382400, 0xf88cdfde
-0,     334687,     334687,        0,  1382400, 0x00000000
-0,     342600,     342600,        0,  1382400, 0xdd51423b
-0,     344431,     344431,        0,  1382400, 0x00000000
-0,     346771,     346771,        0,  1382400, 0x08259fa4
-0,     348329,     348329,        0,  1382400, 0x00000000
-0,     357640,     357640,        0,  1382400, 0x1663fa34
-0,     359767,     359767,        0,  1382400, 0x00000000
-0,     359834,     359834,        0,  1382400, 0xda2ceb55
-0,     361096,     361096,        0,  1382400, 0x00000000
+0,  132499000,  132499000,        0,  1382400, 0x00000000
+0,  132499000,  132499000,        0,  1382400, 0x8c93c2ba
+0,  137459000,  137459000,        0,  1382400, 0x00000000
+0,  147355000,  147355000,        0,  1382400, 0xb02e32ca
+0,  152088000,  152088000,        0,  1382400, 0x00000000
+0,  180797000,  180797000,        0,  1382400, 0x83b71116
+0,  183357000,  183357000,        0,  1382400, 0x00000000
+0,  183433000,  183433000,        0,  1382400, 0x85547fd1
+0,  185799000,  185799000,        0,  1382400, 0x00000000
+0,  185909999,  185909999,        0,  1382400, 0x00000000
+0,  185910000,  185910000,        0,  1382400, 0xb6a8f181
+0,  188606000,  188606000,        0,  1382400, 0x00000000
+0,  188663000,  188663000,        0,  1382400, 0xb64d1a2c
+0,  189925000,  189925000,        0,  1382400, 0x00000000
+0,  190014000,  190014000,        0,  1382400, 0x7b37ecf3
+0,  191675000,  191675000,        0,  1382400, 0x00000000
+0,  199724000,  199724000,        0,  1382400, 0xdc025bd1
+0,  201089000,  201089000,        0,  1382400, 0x00000000
+0,  201175000,  201175000,        0,  1382400, 0x688b294d
+0,  202733000,  202733000,        0,  1382400, 0x00000000
+0,  202819000,  202819000,        0,  1382400, 0xa2b33d1b
+0,  204684000,  204684000,        0,  1382400, 0x00000000
+0,  204762000,  204762000,        0,  1382400, 0xb3e525e3
+0,  206730000,  206730000,        0,  1382400, 0x00000000
+0,  206806000,  206806000,        0,  1382400, 0xaa8fbdd7
+0,  208637000,  208637000,        0,  1382400, 0x00000000
+0,  208716000,  208716000,        0,  1382400, 0x7b7f26dd
+0,  209978000,  209978000,        0,  1382400, 0x00000000
+0,  210051000,  210051000,        0,  1382400, 0x15e2f836
+0,  211575000,  211575000,        0,  1382400, 0x00000000
+0,  211644000,  211644000,        0,  1382400, 0x0fee9b0c
+0,  214306000,  214306000,        0,  1382400, 0x00000000
+0,  214380000,  214380000,        0,  1382400, 0x89d62791
+0,  217144000,  217144000,        0,  1382400, 0x00000000
+0,  217225000,  217225000,        0,  1382400, 0xa6a9fd74
+0,  219591000,  219591000,        0,  1382400, 0x00000000
+0,  219652000,  219652000,        0,  1382400, 0x7896178d
+0,  221483000,  221483000,        0,  1382400, 0x00000000
+0,  223531000,  223531000,        0,  1382400, 0x01751a52
+0,  225863000,  225863000,        0,  1382400, 0x00000000
+0,  227510000,  227510000,        0,  1382400, 0xa3959c6f
+0,  230809000,  230809000,        0,  1382400, 0x00000000
+0,  230872000,  230872000,        0,  1382400, 0x3d3ea47b
+0,  233033000,  233033000,        0,  1382400, 0x00000000
+0,  233124000,  233124000,        0,  1382400, 0x593f8b24
+0,  237220000,  237220000,        0,  1382400, 0x00000000
+0,  237303000,  237303000,        0,  1382400, 0x171f05ba
+0,  240033000,  240033000,        0,  1382400, 0x00000000
+0,  240106000,  240106000,        0,  1382400, 0xb014cdf1
+0,  242165000,  242165000,        0,  1382400, 0x00000000
+0,  273556000,  273556000,        0,  1382400, 0xd918e667
+0,  275217000,  275217000,        0,  1382400, 0x00000000
+0,  295445000,  295445000,        0,  1382400, 0xc9406331
+0,  296776000,  296776000,        0,  1382400, 0x00000000
+0,  300049000,  300049000,        0,  1382400, 0xaf08b10d
+0,  301949000,  301949000,        0,  1382400, 0x00000000
+0,  302034999,  302034999,        0,  1382400, 0x00000000
+0,  302035000,  302035000,        0,  1382400, 0x853a9d93
+0,  303559000,  303559000,        0,  1382400, 0x00000000
+0,  304203000,  304203000,        0,  1382400, 0x7491a87d
+0,  305898000,  305898000,        0,  1382400, 0x00000000
+0,  305947000,  305947000,        0,  1382400, 0xf7383c58
+0,  307881000,  307881000,        0,  1382400, 0x00000000
+0,  307957000,  307957000,        0,  1382400, 0xe66be411
+0,  309720000,  309720000,        0,  1382400, 0x00000000
+0,  321295000,  321295000,        0,  1382400, 0xd6850362
+0,  323263000,  323263000,        0,  1382400, 0x00000000
+0,  323356000,  323356000,        0,  1382400, 0x3e1ed109
+0,  324584000,  324584000,        0,  1382400, 0x00000000
+0,  324640000,  324640000,        0,  1382400, 0x39c1b7bd
+0,  326403000,  326403000,        0,  1382400, 0x00000000
+0,  327193000,  327193000,        0,  1382400, 0x35b85f2e
+0,  328285000,  328285000,        0,  1382400, 0x00000000
+0,  328360999,  328360999,        0,  1382400, 0x00000000
+0,  328361000,  328361000,        0,  1382400, 0x83f103e5
+0,  329885000,  329885000,        0,  1382400, 0x00000000
+0,  329946000,  329946000,        0,  1382400, 0xbc1ca9b3
+0,  331106000,  331106000,        0,  1382400, 0x00000000
+0,  331230000,  331230000,        0,  1382400, 0x94d4a51e
+0,  332857000,  332857000,        0,  1382400, 0x00000000
+0,  332924000,  332924000,        0,  1382400, 0xf88cdfde
+0,  334687000,  334687000,        0,  1382400, 0x00000000
+0,  342600000,  342600000,        0,  1382400, 0xdd51423b
+0,  344431000,  344431000,        0,  1382400, 0x00000000
+0,  346771000,  346771000,        0,  1382400, 0x08259fa4
+0,  348329000,  348329000,        0,  1382400, 0x00000000
+0,  357640000,  357640000,        0,  1382400, 0x1663fa34
+0,  359767000,  359767000,        0,  1382400, 0x00000000
+0,  359834000,  359834000,        0,  1382400, 0xda2ceb55
+0,  361096000,  361096000,        0,  1382400, 0x00000000
diff --git a/tests/ref/fate/sub2video_time_limited b/tests/ref/fate/sub2video_time_limited
index c7d48d639f..07d50fc697 100644
--- a/tests/ref/fate/sub2video_time_limited
+++ b/tests/ref/fate/sub2video_time_limited
@@ -1,8 +1,8 @@ 
-#tb 0: 1/90000
+#tb 0: 1/1000000
 #media_type 0: video
 #codec_id 0: rawvideo
 #dimensions 0: 1920x1080
 #sar 0: 0/1
-0,       6072,       6072,        0,  8294400, 0x00000000
-0,       6072,       6072,        0,  8294400, 0xa87c518f
-0,      36101,      36101,        0,  8294400, 0xa87c518f
+0,      67467,      67467,        0,  8294400, 0x00000000
+0,      67467,      67467,        0,  8294400, 0xa87c518f
+0,     401132,     401132,        0,  8294400, 0xa87c518f