master-of-zen / Av1an

Cross-platform command-line AV1 / VP9 / HEVC / H264 encoding framework with per scene quality encoding
GNU General Public License v3.0
1.51k stars 155 forks source link

Frames go missing #41

Closed utack closed 4 years ago

utack commented 4 years ago

Hi,

I have noticed that some frames vanish in the encoding process. The source file and the split files had the exact same frame count, as tested with ffmpeg
After the encode a little over 50 frames had vanished, also causing a 2 second audio desync towards the end In which step do these frames go missing, because aomenc never dropped frames for me, and obviously the splitting did neither. Could you please examine the situation?

Thank you

master-of-zen commented 4 years ago

Can i get the source?

utack commented 4 years ago

Can i get the source?

It is the main stream off a BluRay, but I will try to reproduce it with a minimal example and send it to you once done

master-of-zen commented 4 years ago

I see, i had this problem. As i think BluRay's in general have video stream made in a way that they overlap by one frame on chapters/segments. So if split is exactly on that moment (which happens in almost 100% of cases) you will get dropped frame at resulting file. Best solution i found is to transcode source to lossless x264 with ffmpeg: ffmpeg -i input -c:v libx264 -crf 0 output.mkv and when encode loseless file Personally, encoded a lot of films in that way, it works good

utack commented 4 years ago

Glad to hear you are aware of the problem
However I am not sure where you think the frames go missing exactly? After splitting ffmpeg counted the exact amount of frames as the original stream had

Provissy commented 4 years ago

I consider this frame dropping is caused by splitting.

I encoded a 90s BDMV clip with heavy scene changes, Av1an automatically split it into 43 queues.

After 2140 frames were encoded, only 2099 of them left. I noticed this because ssim calculation gives a weird number.

Will try converting to y4m first.

master-of-zen commented 4 years ago

It would be really cool to get down to core of problem and find solution

utack commented 4 years ago

FWIW splitting with mkvtoolnix instead of ffmpeg gave the very same result I am running another test now where I collect frame stats for all the split segments all the separate ivf segments to compare, but it takes time.

Provissy commented 4 years ago

I tried converting m2ts to y4m first before segmenting. No frame loss after segmentation.

If I segment a m2ts clip or a mkv clip simply containing m2ts stream, ONE frame would be lost for each segment.

I use ffmpeg -f segment when doing this.

utack commented 4 years ago

Unfortunately this very encode I tested ran into a bug I have not had before, but only one file where aomenc crashed had a frame mismatch out of 1822 parts
I am leaning towards the concat procedure failing tbh

If you also want to give this a go, use --keep and create a table with frame numbers in both the "split" and "encode" folder using a simple bash script:

PARTS=1822

count () {
    echo -n $1 >> index.csv
    echo -n ";" >> index.csv
    ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 $1 >> index.csv
    wait
}

for file in $(eval echo {0001..$PARTS}.ivf)
do
    count "$file"
done

Running it again on another movie that had problems right now, expect results by the weekend

utack commented 4 years ago

Sorry, I was wrong
I was benchmarking the split folder with ffmpeg -f concat -safe 0 over the entire file list
That way it seemed all frames were preserved
When creating the frame list as above, the frame count does not add up
Edit: Confirmed a second time, the splitting does something odd
There is definitely something weird about how ffmpeg splits the input stream

Maybe one could consider a more expensive "seek" option, where ffmpeg seeks to the frame number in the original input file, instead of splitting the source file? That is more storage friendly, but a large number of input file frames need to be decoded

master-of-zen commented 4 years ago

Thanks to effort of :1st_place_medal: @natis1 :1st_place_medal: aka @Avenging_Angle #0272 from discord, this issue were figured out! And hopefully solved forever

MagicRB commented 4 years ago

This bug has returned. Frame Count Differ for Source 00582.mkv: 1308/1309 Its always x/x+1 I'm running the latest version built right from git master and I also tried the latest "stable", which ever that is, from AUR.

master-of-zen commented 4 years ago

@MagicRB what is your source? Also, is your audio get desync after encode is finished?

MagicRB commented 4 years ago

what is your source?

My source is Saw 1 ripped from a CD, I'm thinking that maybe because the frame count is odd or something, seems stupid but worth a try.

Also, is your audio get desync after encode is finished?

What do you mean, how can I check?

master-of-zen commented 4 years ago

@MagicRB If encode is fine and audio in sync, it means that video just has preload frames, which are not real frames and have negative timestamp so they preload before previous part is over(it's happens a lot of blu rays and other stuff)(As I know). Also, you can transcode your source to lossless x264 with ffmpeg -i file -c:v libx264 -crf 0 -g 1 output.mkv and use av1an on that

MagicRB commented 4 years ago

Audio is not in sync, its so out of sync I can't even figure out which way or how much. I just tried the frame thing, still fails. h264 it is

master-of-zen commented 4 years ago

Try different lossless format, ffv1 ffmpeg -i file -c:v ffv1 output.mkv

MagicRB commented 4 years ago

can't go lossless, i run out of space in no time, one of the reasons i'm pursuing av1 :). I'll try to go CD -> h264-23 -> aom-av1-30 and see if that works