Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load video with LWLibavSource causes video clip repeat and freeze #78

Open
TobisLee opened this issue Dec 1, 2024 · 7 comments
Open

Comments

@TobisLee
Copy link

TobisLee commented Dec 1, 2024

Hi, recently I use this lib to do some encode work. It works well. But fails on a specific video. I load the video with the following code:

src_web = core.lsmas.LWLibavSource("./The.Perfect.Host.2010.1080p.POCK.WEB-DL.x264.AAC-PTerWEB.mkv")
src_web = add_tag(src_web, "WEB-DL")
src_web.set_output(9)

After running vspreview test.py and playing back the video in vspreview, I noticed that the video freezes and repeats. Here’s a screen recording of the issue.

_20241201_113350.webm

When I play the video in MPV, the problem doesn’t occur. And I play the video with mpv, the problem not happen. I also tried using ffms2 and AkarinVS/L-SMASH-Works, both works fine.

Here is the sample of this video: https://files.catbox.moe/trc2xg.mkv
Here is the reproduce code:

from vapoursynth import core

source = r'sample.mkv'

web = core.lsmas.LWLibavSource(source)
web2 = core.ffms2.Source(source)

web.set_output()
web2.set_output(9)

Jump to frame 48, lsmash/ffms2 have different output.

Here is the mediainfo of this video:

General
Unique ID                                : 106356805884221267768608124056367096552 (0x50039362A8ED9B4259512989310162E8)
Complete name                            : The.Perfect.Host.2010.1080p.POCK.WEB-DL.x264.AAC-PTerWEB.mkv
Format                                   : Matroska
Format version                           : Version 4
File size                                : 5.11 GiB
Duration                                 : 1 h 32 min
Overall bit rate                         : 7 888 kb/s
Frame rate                               : 23.976 FPS
Encoded date                             : 2023-04-04 07:41:25 UTC
Writing application                      : mkvmerge v74.0.0 ('You Oughta Know') 64-bit
Writing library                          : libebml v1.4.4 + libmatroska v1.7.1

Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L4
Format settings                          : CABAC / 3 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 3 frames
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 1 h 32 min
Bit rate                                 : 7 814 kb/s
Width                                    : 1 920 pixels
Height                                   : 1 080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Constant
Frame rate                               : 23.976 (24000/1001) FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.157
Stream size                              : 5.04 GiB (99%)
Writing library                          : x264 core 164 r3095 baee400
Encoding settings                        : cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=umh / subme=7 / psy=1 / psy_rd=0.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=0 / threads=34 / lookahead_threads=5 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=2 / b_pyramid=0 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=48 / keyint_min=4 / scenecut=0 / intra_refresh=0 / rc_lookahead=48 / rc=2pass / mbtree=1 / bitrate=7814 / ratetol=1.0 / qcomp=0.60 / qpmin=10 / qpmax=51 / qpstep=4 / cplxblur=20.0 / qblur=0.5 / vbv_maxrate=11729 / vbv_bufsize=11729 / nal_hrd=none / filler=0 / ip_ratio=1.41 / aq=1:1.00
Default                                  : Yes
Forced                                   : No
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709

Audio
ID                                       : 2
Format                                   : AAC LC
Format/Info                              : Advanced Audio Codec Low Complexity
Codec ID                                 : A_AAC-2
Duration                                 : 1 h 32 min
Bit rate                                 : 96.0 kb/s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Frame rate                               : 46.875 FPS (1024 SPF)
Compression mode                         : Lossy
Stream size                              : 63.8 MiB (1%)
Language                                 : English
Default                                  : Yes
Forced                                   : No

Text
ID                                       : 3
Format                                   : UTF-8
Codec ID                                 : S_TEXT/UTF8
Codec ID/Info                            : UTF-8 Plain Text
Duration                                 : 1 h 32 min
Bit rate                                 : 58 b/s
Frame rate                               : 0.261 FPS
Count of elements                        : 1444
Stream size                              : 39.7 KiB (0%)
Language                                 : English
Default                                  : No
Forced                                   : No
@L4cache
Copy link

L4cache commented Dec 16, 2024

There's something unusual with this file, if you remux it by extract then mux, or using ffmpeg, the problem is gone.
You probably used the original mkv directly as input when you cut the sample, it inherits the problem because mkvtoolnix reuses some information from container level of input file, such as matroska or isobmff (mp4).

The file is not faulty I guess, since it plays with out issue, but something unusual makes it interfere with the seeking mechanism of lsmas. Linear decode seems fine.

@TobisLee
Copy link
Author

Thanks for the reply.

but something unusual makes it interfere with the seeking mechanism of lsmas. Linear decode seems fine.

So you mean that different decoders have different seeking mechanisms? Can I ask the reason? For speed up? Because in my opinion, the seek operation should be a common function.

@L4cache
Copy link

L4cache commented Dec 19, 2024

When I test the sample you provided, the bug is not happening when doing linear decoding, but only happens when seeking to frame 48.
I don't know why "playback" in vspreview is triggering it.
Can you test full clip with other "playback" methods, like vspipe to ffplay or mpv?

There's some difference in seeking between this and akarin fork which may be why it's not happening in akarin's fork.

@TobisLee
Copy link
Author

I've tested the full movie in these cases, and all of them work fine, no bug happens:

  1. Play it with mpv and drag the progress bar.
  2. Use ffms2 to load the video and play it in vspreview.
  3. Use ffms2 to load the video and encode with vstest script (vspipe to x264 encoder), then play the encoded video with mpv.

I'm not sure whether these infos can help. If you need other test result, please let me know, I am willing to test and provide it. Or if you need the full video, also let me know, but I don't know how to share it with you :<.

Asd-g pushed a commit that referenced this issue Dec 31, 2024
The issue is that somehow newer mkvmerge generates way less cue points
than ffmpeg and older versions of mkvmerge. This makes av_seek_frame
much less reliable. For the specific test case mentioned in #14, rap
for frame 32 is frame 24, however, the best av_seek_frame can do is
just seek to the very beginning of the video track.

lsmas does not depend on fully reliable av_seek_frame, however, in this
particular cases, because the mkv is using PTS as seeking method, lsmas
will not attempt to call correct_current_frame_number to correct for the
mis-seek, which means it will assume the current position (frame 0) as
actually the rap (frame 24), then it follows that it will return frame
8 as frame 32.

To fix this, we must call correct_current_frame_number even for SEEK_PTS_BASED
formats. This fixes much of the issues, except for the first few frames.

Then there is another issue lurking in correct_current_frame_number:
when it finds that the current packet has a dts of AV_NOPTS_VALUE, it
will always abort. Unfortunately, as frame 0 typically has such a dts,
it will not correct the current frame number when the actual current
is frame 0. We fix this by disregarding pkt->dts == AV_NOPTS_VALUE
when the seek mode is not just SEEK_DTS_BASED (note that mkv has a
seek mode of SEEK_DTS_BASED | SEEK_POS_CORRECTION, which means that
we can reliably use the pkt->pos field to determine the current frame.)

Fixes AkarinVS/L-SMASH-Works#14.

Signed-off-by: akarin <[email protected]>
(cherry picked from commit AkarinVS/L-SMASH-Works@95de1ab)

Related #78.
Asd-g pushed a commit that referenced this issue Dec 31, 2024
It turns out not only the first frame could have invalid DTS, there
are mkv files in the wild that contains first few frames with an
invalid DTS. It's beyond me how mkvmerge could have created such a
file given that mkv are supposed to be seeking by DTS.

Next time, if you face such a mkv, you can:
1. mkvextract the video and then remux with mkvmerge to force it to
   recreate cue points for all IDR..
2. add `--cue 0:all` to mkvmerge and remux the broken mkv to fix it.

Fixes AkarinVS/L-SMASH-Works#14.

Signed-off-by: akarin <[email protected]>
(cherry picked from commit AkarinVS/L-SMASH-Works@d73d2dc)

Related #78.
@Asd-g
Copy link
Contributor

Asd-g commented Feb 6, 2025

Try the attached version.

LSMASHSource.zip

@TobisLee
Copy link
Author

TobisLee commented Feb 8, 2025

Thanks for your work, @Asd-g . But I work on linux system, so can you provide the so version? Or can I just use the latest code to build the so?

@Asd-g
Copy link
Contributor

Asd-g commented Feb 8, 2025

Build with the latest changes but use this FFMpeg version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants