Log in

View Full Version : 2pass encoding: 1st pass slower than 2nd pass?


Selur
12th February 2018, 20:19
I'm confused. :)

using for the 1st pass:
ffmpeg -y -loglevel fatal -hwaccel dxva2 -threads 1 -i "C:\Users\Selur\Desktop\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001.mkv" -map 0:0 -an -sn -vsync 0 -pix_fmt yuv420p -f rawvideo - | x264 --pass 1 --bitrate 527 --profile high --level 4.1 --bframes 5 --b-pyramid none --direct auto --b-adapt 2 --lookahead-threads 10 --rc-lookahead 50 --chroma-qp-offset -2 --aq-strength 0.6 --sar 1:1 --deblock 1:1 --non-deterministic --range tv --stats "H:\Temp\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001_new_20_01_11_2010_01.stats" --demuxer raw --input-res 1920x1080 --input-csp i420 --input-depth 8 --fps 24000/1001 --output-depth 8 --output NUL -
I get:
x264 [info]: frame I:64 Avg QP:35.08 size: 25571
x264 [info]: frame P:3562 Avg QP:38.70 size: 4281
x264 [info]: frame B:3663 Avg QP:39.86 size: 775
x264 [info]: consecutive B-frames: 14.5% 47.6% 27.6% 4.9% 3.7% 1.6%
x264 [info]: mb I I16..4: 86.9% 0.0% 13.1%
x264 [info]: mb P I16..4: 16.1% 0.0% 0.0% P16..4: 8.9% 0.0% 0.0% 0.0% 0.0% skip:75.0%
x264 [info]: mb B I16..4: 0.7% 0.0% 0.0% B16..8: 3.7% 0.0% 0.0% direct: 0.9% skip:94.7% L0:49.6% L1:49.0% BI: 1.4%
x264 [info]: final ratefactor: 38.30
x264 [info]: direct mvs spatial:88.0% temporal:12.0%
x264 [info]: coded y,uvDC,uvAC intra: 5.0% 28.0% 4.5% inter: 0.5% 2.0% 0.0%
x264 [info]: i16 v,h,dc,p: 57% 25% 12% 5%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 18% 26% 8% 5% 5% 5% 5% 5%
x264 [info]: i8c dc,h,v,p: 80% 10% 8% 1%
x264 [info]: Weighted P-Frames: Y:2.0% UV:1.2%
x264 [info]: kb/s:519.06

encoded 7289 frames, 57.56 fps, 519.06 kb/s
then using:
{code]ffmpeg -y -loglevel fatal -hwaccel dxva2 -threads 1 -i "C:\Users\Selur\Desktop\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001.mkv" -map 0:0 -an -sn -vsync 0 -pix_fmt yuv420p -f rawvideo - | x264 --pass 2 --bitrate 527 --profile high --level 4.1 --bframes 5 --b-pyramid none --direct auto --b-adapt 2 --lookahead-threads 10 --rc-lookahead 50 --chroma-qp-offset -2 --aq-strength 0.6 --vbv-maxrate 62500 --vbv-bufsize 78125 --sar 1:1 --deblock 1:1 --non-deterministic --range tv --colormatrix bt709 --stats "H:\Temp\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001_new_20_01_11_2010_01.stats" --demuxer raw --input-res 1920x1080 --input-csp i420 --input-depth 8 --fps 24000/1001 --output-depth 8 --output "H:\Temp\20_01_11_2010_02.264" -[/code]
for the 2nd pass, I get:
: frame I:64 Avg QP:35.09 size: 21913
x264 [info]: frame P:3562 Avg QP:39.21 size: 4318
x264 [info]: frame B:3663 Avg QP:42.73 size: 903
x264 [info]: consecutive B-frames: 14.5% 47.6% 27.6% 4.9% 3.7% 1.6%
x264 [info]: mb I I16..4: 25.2% 69.7% 5.1%
x264 [info]: mb P I16..4: 3.4% 13.7% 0.2% P16..4: 11.0% 0.6% 0.3% 0.0% 0.0% skip:70.7%
x264 [info]: mb B I16..4: 0.1% 0.1% 0.0% B16..8: 10.3% 0.1% 0.0% direct: 0.0% skip:89.3% L0:37.6% L1:62.2% BI: 0.3%
x264 [info]: 8x8 transform intra:78.1% inter:93.4%
x264 [info]: direct mvs spatial:80.6% temporal:19.4%
x264 [info]: coded y,uvDC,uvAC intra: 6.6% 30.1% 5.4% inter: 0.5% 1.7% 0.0%
x264 [info]: i16 v,h,dc,p: 41% 34% 8% 17%
x264 [info]: i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 6% 61% 1% 1% 1% 1% 1% 1%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 19% 20% 6% 7% 7% 6% 5% 3%
x264 [info]: i8c dc,h,v,p: 83% 7% 8% 1%
x264 [info]: Weighted P-Frames: Y:2.2% UV:1.2%
x264 [info]: ref P L0: 60.6% 11.5% 18.8% 8.9% 0.2%
x264 [info]: ref B L0: 72.4% 27.6%
x264 [info]: kb/s:528.68

encoded 7289 frames, 93.97 fps, 528.68 kb/s]


When using:
ffmpeg -y -loglevel fatal -hwaccel dxva2 -threads 1 -i "C:\Users\Selur\Desktop\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001.mkv" -map 0:0 -an -sn -vsync 0 -pix_fmt yuv420p -f rawvideo - | x264 --pass 1 --bitrate 527 --profile high --level 4.1 --bframes 5 --direct auto --b-adapt 2 --lookahead-threads 10 --rc-lookahead 50 --chroma-qp-offset -2 --aq-strength 0.6 --sar 1:1 --deblock 1:1 --non-deterministic --range tv --stats "H:\Temp\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001_new_20_09_41_0810_01.stats" --demuxer raw --input-res 1920x1080 --input-csp i420 --input-depth 8 --fps 24000/1001 --output-depth 8 --output NUL -
thus using b-frame pyramid 1st pass gets faster:
[info]: frame I:64 Avg QP:34.77 size: 26768
x264 [info]: frame P:2627 Avg QP:38.92 size: 5050
x264 [info]: frame B:4598 Avg QP:39.38 size: 1028
x264 [info]: consecutive B-frames: 12.7% 9.5% 16.6% 39.4% 12.9% 8.9%
x264 [info]: mb I I16..4: 86.4% 0.0% 13.6%
x264 [info]: mb P I16..4: 20.6% 0.0% 0.0% P16..4: 9.4% 0.0% 0.0% 0.0% 0.0% skip:70.0%
x264 [info]: mb B I16..4: 0.9% 0.0% 0.0% B16..8: 4.1% 0.0% 0.0% direct: 1.1% skip:93.8% L0:47.8% L1:50.0% BI: 2.2%
x264 [info]: final ratefactor: 38.19
x264 [info]: direct mvs spatial:95.7% temporal:4.3%
x264 [info]: coded y,uvDC,uvAC intra: 5.1% 27.9% 4.5% inter: 0.5% 2.1% 0.0%
x264 [info]: i16 v,h,dc,p: 58% 25% 12% 5%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 18% 25% 8% 5% 5% 5% 5% 5%
x264 [info]: i8c dc,h,v,p: 80% 10% 8% 1%
x264 [info]: Weighted P-Frames: Y:2.9% UV:1.8%
x264 [info]: kb/s:518.52

encoded 7289 frames, 67.84 fps, 518.52 kb/s
and 2nd pass
ffmpeg -y -loglevel fatal -hwaccel dxva2 -threads 1 -i "C:\Users\Selur\Desktop\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001.mkv" -map 0:0 -an -sn -vsync 0 -pix_fmt yuv420p -f rawvideo - | x264 --pass 2 --bitrate 527 --profile high --level 4.1 --bframes 5 --direct auto --b-adapt 2 --lookahead-threads 10 --rc-lookahead 50 --chroma-qp-offset -2 --aq-strength 0.6 --vbv-maxrate 62500 --vbv-bufsize 78125 --sar 1:1 --deblock 1:1 --non-deterministic --range tv --colormatrix bt709 --stats "H:\Temp\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001_new_20_09_41_0810_01.stats" --demuxer raw --input-res 1920x1080 --input-csp i420 --input-depth 8 --fps 24000/1001 --output-depth 8 --output "H:\Temp\20_09_41_0810_02.264" -
gives me:
x264 [info]: frame I:64 Avg QP:34.75 size: 22954
x264 [info]: frame P:2627 Avg QP:38.95 size: 5126
x264 [info]: frame B:4598 Avg QP:42.19 size: 1119
x264 [info]: consecutive B-frames: 12.7% 9.5% 16.6% 39.4% 12.9% 8.9%
x264 [info]: mb I I16..4: 24.7% 69.9% 5.3%
x264 [info]: mb P I16..4: 4.4% 17.4% 0.3% P16..4: 11.4% 0.7% 0.3% 0.0% 0.0% skip:65.4%
x264 [info]: mb B I16..4: 0.1% 0.2% 0.0% B16..8: 11.1% 0.2% 0.0% direct: 0.1% skip:88.3% L0:41.8% L1:58.0% BI: 0.3%
x264 [info]: 8x8 transform intra:77.5% inter:93.1%
x264 [info]: direct mvs spatial:92.4% temporal:7.6%
x264 [info]: coded y,uvDC,uvAC intra: 7.0% 30.8% 5.7% inter: 0.5% 1.7% 0.0%
x264 [info]: i16 v,h,dc,p: 41% 34% 8% 16%
x264 [info]: i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 7% 59% 1% 1% 1% 1% 1% 1%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 19% 19% 6% 7% 7% 6% 5% 3%
x264 [info]: i8c dc,h,v,p: 83% 8% 9% 1%
x264 [info]: Weighted P-Frames: Y:3.2% UV:1.8%
x264 [info]: ref P L0: 59.2% 12.0% 17.4% 11.0% 0.4%
x264 [info]: ref B L0: 80.0% 16.4% 3.6%
x264 [info]: ref B L1: 92.9% 7.1%
x264 [info]: kb/s:528.41

encoded 7289 frames, 89.77 fps, 528.41 kb/s
which is a bit slower than before, but still way faster than the first pass.


I thought the 1st pass should be the faster of the 2pass not the other way around. :)
-> Can someone explain this?

Cu Selur

Ps.: Is it my x264 version? I'm using:
x264 0.155.2901 7d0ff22
(libswscale 5.0.101)
(libavformat 58.5.100)
(ffmpegsource 2.27.0.0)
built on Jan 19 2018, gcc: 7.2.0
x264 configuration: --chroma-format=all
libx264 configuration: --chroma-format=all
x264 license: GPL version 2 or later
libswscale/libavformat/ffmpegsource license: GPL version 2 or later
(using the builds from https://download.videolan.org/x264/binaries/win64/ doesn't change the effect)

sneaker_ger
12th February 2018, 20:35
Without taking a close look: frame-type decision only happens in first pass. Depending on the settings that can take significant cpu time (relative to the rest of the time spent). I see you are using --bframes 5 with --b-adapt 2.

Selur
13th February 2018, 04:24
Okay, so you are saying that using b-frames makes 1st pass naturally slower than 2nd pass?

Disabling all b-frames:
ffmpeg -y -loglevel fatal -hwaccel dxva2 -threads 1 -i "C:\Users\Selur\Desktop\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001.mkv" -map 0:0 -an -sn -vsync 0 -pix_fmt yuv420p -f rawvideo - | x264 --pass 1 --bitrate 527 --profile high --level 4.1 --bframes 0 --lookahead-threads 10 --rc-lookahead 50 --chroma-qp-offset -2 --aq-strength 0.6 --sar 1:1 --deblock 1:1 --non-deterministic --range tv --stats "H:\Temp\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001_new_20_01_11_2010_01.stats" --demuxer raw --input-res 1920x1080 --input-csp i420 --input-depth 8 --fps 24000/1001 --output-depth 8 --output NUL - I get:
x264 [info]: using SAR=1/1
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
x264 [info]: profile Main, level 4.1
x264 [info]: frame I:332 Avg QP:39.50 size: 11202
x264 [info]: frame P:6957 Avg QP:39.88 size: 2295
x264 [info]: mb I I16..4: 96.1% 0.0% 3.9%
x264 [info]: mb P I16..4: 7.5% 0.0% 0.0% P16..4: 6.2% 0.0% 0.0% 0.0% 0.0% skip:86.3%
x264 [info]: final ratefactor: 39.47
x264 [info]: coded y,uvDC,uvAC intra: 4.3% 26.3% 3.6% inter: 0.4% 1.7% 0.0%
x264 [info]: i16 v,h,dc,p: 59% 24% 11% 5%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 15% 33% 10% 5% 5% 4% 5% 4%
x264 [info]: i8c dc,h,v,p: 82% 9% 7% 1%
x264 [info]: Weighted P-Frames: Y:0.7% UV:0.4%
x264 [info]: kb/s:518.05

encoded 7289 frames, 115.64 fps, 518.05 kb/s
(also setting b-frame count to 16 slows it down to ~20fps)

But is it just me not having payed attention to the speeds that close. I don't remember the impact of having more b-frames being that heavy.

Cu Selur

Ps.: using '--slow-firstpass':
ffmpeg -y -loglevel fatal -hwaccel dxva2 -threads 1 -i "C:\Users\Selur\Desktop\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001.mkv" -map 0:0 -an -sn -vsync 0 -pix_fmt yuv420p -f rawvideo - | x264 --pass 1 --bitrate 527 --profile high --level 4.1 --slow-firstpass --bframes 5 --b-pyramid none --direct auto --b-adapt 2 --lookahead-threads 10 --rc-lookahead 50 --chroma-qp-offset -2 --aq-strength 0.6 --sar 1:1 --deblock 1:1 --non-deterministic --range tv --stats "H:\Temp\Dragonball.Z.-.001.-.1080p.BluRay.x264.DHD (1)-001_new_20_01_11_2010_01.stats" --demuxer raw --input-res 1920x1080 --input-csp i420 --input-depth 8 --fps 24000/1001 --output-depth 8 --output NUL -
still gives me still gives me:
x264 [info]: using SAR=1/1
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
x264 [info]: profile High, level 4.1
x264 [info]: frame I:66 Avg QP:37.10 size: 21874
x264 [info]: frame P:3470 Avg QP:40.24 size: 4151
x264 [info]: frame B:3753 Avg QP:42.47 size: 1059
x264 [info]: consecutive B-frames: 13.2% 46.0% 29.7% 5.2% 3.8% 2.1%
x264 [info]: mb I I16..4: 22.8% 72.6% 4.6%
x264 [info]: mb P I16..4: 2.6% 13.7% 0.1% P16..4: 12.1% 0.7% 0.4% 0.0% 0.0% skip:70.4%
x264 [info]: mb B I16..4: 0.1% 0.2% 0.0% B16..8: 11.7% 0.1% 0.0% direct: 0.1% skip:87.9% L0:37.3% L1:62.5% BI: 0.2%
x264 [info]: final ratefactor: 39.23
x264 [info]: 8x8 transform intra:81.9% inter:92.8%
x264 [info]: direct mvs spatial:94.5% temporal:5.5%
x264 [info]: coded y,uvDC,uvAC intra: 5.4% 28.9% 4.3% inter: 0.6% 2.2% 0.0%
x264 [info]: i16 v,h,dc,p: 42% 35% 8% 15%
x264 [info]: i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 23% 6% 66% 1% 1% 1% 1% 1% 1%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 19% 20% 6% 7% 7% 6% 5% 3%
x264 [info]: i8c dc,h,v,p: 87% 6% 5% 1%
x264 [info]: Weighted P-Frames: Y:2.5% UV:1.6%
x264 [info]: ref P L0: 62.0% 11.6% 17.7% 8.5% 0.2%
x264 [info]: ref B L0: 73.1% 26.9%
x264 [info]: kb/s:521.63

encoded 7289 frames, 52.96 fps, 521.63 kb/s
So when using multiple b-frames using fast-firstpass might not really be worth it,..

sneaker_ger
13th February 2018, 12:14
Okay, so you are saying that using b-frames makes 1st pass naturally slower than 2nd pass?
No, not naturally. It can happen. Sometimes. It depends. But I fail to see why this is a problem in itself? Is there a rule that says 1 has to be faster than 2?

Disabling all b-frames:
[...]
Ps.: using '--slow-firstpass':
[...]
So when using multiple b-frames using fast-firstpass might not really be worth it,..
That is bad logic. Time spent/wasted is time spent/wasted. Maybe you should be sticking to the presets? --b-adapt 2 isn't used before preset slower.

Sharc
13th February 2018, 16:28
In my experience, for relaxed encoder settings the 1st pass is normally slower than the 2nd pass. And in such case (relaxed settings) there is not much speed which can be gained with a fast 1st pass.

Selur
13th February 2018, 17:50
When using '--b-adapt 0' I get:
x264 [info]: using SAR=1/1
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
x264 [info]: profile Main, level 4.1
x264 [info]: frame I:185 Avg QP:38.37 size: 15069
x264 [info]: frame P:1551 Avg QP:39.58 size: 4848
x264 [info]: frame B:5553 Avg QP:42.11 size: 1522
x264 [info]: consecutive B-frames: 7.8% 1.0% 1.1% 0.8% 3.1% 86.3%
x264 [info]: mb I I16..4: 93.9% 0.0% 6.1%
x264 [info]: mb P I16..4: 18.7% 0.0% 0.0% P16..4: 8.4% 0.0% 0.0% 0.0% 0.0% skip:72.9%
x264 [info]: mb B I16..4: 2.3% 0.0% 0.0% B16..8: 4.8% 0.0% 0.0% direct: 1.5% skip:91.5% L0:49.4% L1:49.0% BI: 1.6%
x264 [info]: final ratefactor: 39.88
x264 [info]: direct mvs spatial:85.9% temporal:14.1%
x264 [info]: coded y,uvDC,uvAC intra: 6.0% 26.7% 4.5% inter: 0.4% 2.3% 0.0%
x264 [info]: i16 v,h,dc,p: 59% 25% 11% 5%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 17% 30% 9% 5% 5% 5% 5% 4%
x264 [info]: i8c dc,h,v,p: 81% 10% 8% 1%
x264 [info]: Weighted P-Frames: Y:4.8% UV:2.3%
x264 [info]: kb/s:493.58

encoded 7289 frames, 96.66 fps, 493.58 kb/s
When the default '--b-adapt 1' I get:
x264 [info]: using SAR=1/1
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
x264 [info]: profile Main, level 4.1
x264 [info]: frame I:184 Avg QP:36.78 size: 16908
x264 [info]: frame P:3267 Avg QP:39.03 size: 4204
x264 [info]: frame B:3838 Avg QP:40.08 size: 739
x264 [info]: consecutive B-frames: 17.3% 31.7% 27.5% 11.1% 5.6% 6.7%
x264 [info]: mb I I16..4: 92.7% 0.0% 7.3%
x264 [info]: mb P I16..4: 15.5% 0.0% 0.0% P16..4: 8.8% 0.0% 0.0% 0.0% 0.0% skip:75.7%
x264 [info]: mb B I16..4: 0.6% 0.0% 0.0% B16..8: 3.6% 0.0% 0.0% direct: 0.8% skip:95.0% L0:43.0% L1:55.6% BI: 1.4%
x264 [info]: final ratefactor: 38.30
x264 [info]: direct mvs spatial:86.9% temporal:13.1%
x264 [info]: coded y,uvDC,uvAC intra: 5.5% 27.3% 4.4% inter: 0.5% 1.9% 0.0%
x264 [info]: i16 v,h,dc,p: 58% 25% 12% 5%
x264 [info]: i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 17% 29% 9% 5% 5% 5% 5% 5%
x264 [info]: i8c dc,h,v,p: 81% 10% 8% 1%
x264 [info]: Weighted P-Frames: Y:2.1% UV:1.1%
x264 [info]: kb/s:517.92

encoded 7289 frames, 95.05 fps, 517.92 kb/s
so using '--b-adapt 2' is the main culprit. :)

benwaggoner
19th February 2018, 01:34
so using '--b-adapt 2' is the main culprit. :)
And --b-adapt 1 got some pretty big improvements in the last year or so. So folks who have been using --b-adapt 2 for forever likely should revisit that for time-sensitive compression.