MLV-App icon indicating copy to clipboard operation
MLV-App copied to clipboard

GPU support for ffmpeg

Open masc4ii opened this issue 7 years ago • 11 comments

I create a new issue for that. Last dicussion was in #77 , but was very OT there.

@bouncyball-git : do you get ffmpeg compiled in the way we need it on all 3 OSes? Win you have already... 😄

masc4ii avatar Feb 25 '18 18:02 masc4ii

Not yet, but I will try for Linux 1st.

bouncyball-git avatar Feb 26 '18 08:02 bouncyball-git

Meanwhile you can try it on windows.. Included ffmpeg works with HW encoder.

bouncyball-git avatar Feb 26 '18 08:02 bouncyball-git

Unfortunately, I don't get it to work on Windows - ffmpeg tells my nvidia driver is to old, but in reality it is newer. 😆

masc4ii avatar Feb 28 '18 16:02 masc4ii

Then maybe it is because of supported cuda version?

bouncyball-git avatar Feb 28 '18 16:02 bouncyball-git

I don't know... maybe. But then the error message from ffmpeg was wrong.

masc4ii avatar Feb 28 '18 16:02 masc4ii

would be good.

ilia3101 avatar Mar 01 '18 17:03 ilia3101

Tests from @ cmh (ML forum): MLV-App's ffmpeg binary for Windows is already compiled with --enable-nvenc. I own an nVidia card and I could install some distros real quick if further testing is needed. As for Luther's suggestion, he's probably thinking of a simple slider for Constant Quality RF. No parsing required. I don't care about any of those features personally because there's no way to edit in MLV-App efficiently, so Prores it is.

Anyway, ffmpeg -h encoder=h264_nvenc and ffmpeg -h encoder=hevc_nvenc gives me those outputs on Windows. I omitted the whole AVOptions part but here's the whole output https://pastebin.com/n13ueWaE

PS C:\Users\Windows\Desktop\MLV.App.v1.9.Win64.static> .\ffmpeg.exe -h encoder=h264_nvenc ffmpeg version N-93580-g036b4b0f85 Copyright (c) 2000-2019 the FFmpeg developers built with gcc 8.3.1 (GCC) 20190414 configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt Encoder h264_nvenc [NVIDIA NVENC H.264 encoder]: General capabilities: delay hardware Threading capabilities: none Supported pixel formats: yuv420p nv12 p010le yuv444p p016le yuv444p16le bgr0 rgb0 cuda d3d11

PS C:\Users\Windows\Desktop\MLV.App.v1.9.Win64.static> .\ffmpeg.exe -h encoder=hevc_nvenc ffmpeg version N-93580-g036b4b0f85 Copyright (c) 2000-2019 the FFmpeg developers built with gcc 8.3.1 (GCC) 20190414 configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt Encoder hevc_nvenc [NVIDIA NVENC hevc encoder]: General capabilities: delay hardware Threading capabilities: none Supported pixel formats: yuv420p nv12 p010le yuv444p p016le yuv444p16le bgr0 rgb0 cuda d3d11

I did some tests on a GTX1060, h264_nvenc and h264_cuvid are capped at 4k apparently.

I tried the whole command line but -f rawvideo doesn't work, even with libx264 (or the right size or -profile high444p -pixel_format yuv444p). M17-2143.mov: corrupt input packet in stream 0 [rawvideo @ 0647e740] Invalid buffer size, packet size 47686523 < expected frame_size 48382272 Error while decoding stream #0:0: Invalid argument

M17-2143.mov, 4392x1836

  • h264_cuvid: fails .\ffmpeg.exe -hwaccel cuvid -c:v h264_cuvid -i 'M17-2143.mov' 'test.mp4' [h264_cuvid @ 06bd0080] Video width 4392 not within range from 48 to 4096 Stream mapping: Stream #0:0 -> #0:0 (h264 (h264_cuvid) -> h264 (libx264)) Error while opening decoder for input stream #0:0 : Invalid argument

  • h264_nvenc: fails .\ffmpeg.exe -i 'M17-2143.mov' -c:v h264_nvenc -preset default 'test.mp4' [h264_nvenc @ 06f60080] No NVENC capable devices found Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height Fails

  • hevc_nvenc: pass .\ffmpeg.exe -i 'M17-2143.mov' -c:v hevc_nvenc -preset default 'test.mp4'

Peppa_Pig-Patata-Parc.mp4, 1025x576

  • h264_cuvid: pass .\ffmpeg.exe -vsync 0 -hwaccel cuvid -c:v h264_cuvid -i 'Peppa_Pig-Patata-Parc.mp4' -c:a copy -c:v h264_nvenc -b:v 5M 'test.mp4'

  • h264_nvenc: pass .\ffmpeg.exe -i '.\Peppa_Pig-Patata-Parc.mp4' -c:v h264_nvenc -preset default 'test.mp4'

I can't test the GPU based scalers with h264_nvenc encoding because the ffmpeg binary is not compiled with --enable-libnpp and I can't use -filter_complex nvresize=1:s= wihtout ffmpeg compiled with the flag --enable-nvresize. I can't find a solution for resizing. Other than that there's a problem with the pixel formats for some reason "Option pixel_format not found".

-c:v libx265 replaced by -vcodec hevc_nvenc
-f rawvideo -s 4392x1826 replaced by -filter_complex nvresize=1:s=4392x1826 -pix_fmt rgb48 replaced by any other supported pixel formats: yuv420p nv12 p010le yuv444p p016le yuv444p16le bgr0 rgb0 cuda d3d11 (I tried yuv420p, yuv444p and rgb0). -crf replaced by -cq but the quality is different between -crf 18 and -cq 18, need further testing resizeFilter need futher testing too, I just skipped it.

masc4ii avatar Oct 18 '19 17:10 masc4ii

Testing some parameters.

I made a list of settings that will keep the quality constant across all settings if you decided to implement nvenc:

Medium quality libx264 -crf 23 is roughly equivalent to: h264_nvenc -cq 27 -qmin 27 -qmax 27 or -rc constqp -qp 27 libx265 -preset medium -crf 25 hevc_nvenc -cq 27 -qmin 27 -qmax 27 or -rc constqp -qp 27

High quality libx264 -crf 18 is roughly equivalent to: h264_nvenc -cq 21 -qmin 21 -qmax 21 or -rc constqp -qp 21 libx265 -crf 19 hevc-nvenc -cq 24 -qmin 24 -qmax 24 or -rc constqp -qp 24

The source (M19-1145.avi) is a rawvideo, bgr24, 2240x938, 1211492 kb/s, 23.98 fps SSIM (Structural SIMilarity) and PSNR (Peak Signal to Noise Ratio) values have been checked with: .\ffmpeg.exe -i .\test_file.mp4 -i .\source.avi -lavfi "ssim;[0:v][1:v]psnr" -f null - SSIM: The higher the better, perfect match is SSIM=1 and no similarity SSIM=0. PSNR: Typical values for the PSNR in lossy image and video compression are between 30 and 50 dB, provided the bit depth is 8 bits, where higher is better.

If requested, I can upload those videos so people can do a visual check. I also encourage people to make a little script and test with other scenes.

As a footnote, Handbrake uses -rc constqp -qp instead of the recommended -rc vbr -cq for some reason. https://github.com/HandBrake/HandBrake/issues/2231

More info on this setting: https://www.pixeltools.com/rate_control_paper.html

"[...]the quantization parameter QP can only influence the detail of information carried in the transformed residuals. QP has no direct effect on the bitrates associated with overhead, prediction data, or motion vectors. The Mean Average Difference (or MAD) of the prediction error is used for this purpose. [...] Small values of QP more accurately approximate the block's spatial frequency spectrum, but at the cost of more bits. In H.264, each unit increase of QP lengthens the step size by 12% and reduces the bitrate by roughly 12%."

https://slhck.info/video/2017/03/01/rate-control.html

"The Quantization Parameter controls the amount of compression for every Macroblock in a frame. Large values mean that there will be higher quantization, more compression, and lower quality. Lower values mean the opposite. [...] Unless you know what you’re doing and you explicitly want this, do not use this mode! Setting a fixed QP means that the resulting bitrate will be varying strongly depending on each scene’s complexity, and it will result in rather inefficient encodes for your input video. You may waste space and you have no control of the actual bitrate."

H.264 CRF 23 comparison

libx264 -crf 23 .\ffmpeg.exe -i .\M19-1145.avi -c:v libx264 -preset medium -crf 23 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\1-x264_crf23.mp4 File size : 8.59 MiB Bit rate : 3 369 kb/s SSIM 0.963138 PSNR average: 41.866066

As you can see below, there's a difference between libx264 -crf 23 and h264_nvenc -cq 23 or -qp 23 in terms of quality and filesize:

h264_nvenc -cq 23 .\ffmpeg.exe -i .\M19-1145.avi -c:v h264_nvenc -preset medium -rc vbr -cq 23 -qmin 23 -qmax 23 -b:v 0 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\2-h264-nvenc_vbr-cq23.mp4 File size : 21.7 MiB Bit rate mode : Variable Bit rate : 8 723 kb/s Maximum bit rate : 40.0 Mb/s SSIM 0.969865 PSNR average: 43.397301

h264_nvenc -qp 23 .\ffmpeg.exe -i .\M19-1145.avi -c:v h264_nvenc -preset medium -rc constqp -qp 23 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\3-h264-nvenc_constqp-qp23.mp4 File size : 21.9 MiB Bit rate mode : Variable Bit rate : 8 819 kb/s Maximum bit rate : 2 000 kb/s SSIM 0.969887 PSNR average: 43.403189

The closest to libx264 -crf 23 in terms of bitrate is h264_nvenc -cq 27 -qmin 27 -qmax 27 or -rc constqp -qp 27:

h264_nvenc -cq 27 .\ffmpeg.exe -i .\M19-1145.avi -c:v h264_nvenc -preset medium -rc vbr -cq 27 -qmin 27 -qmax 27 -b:v 0 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\4-h264-nvenc_vbr-cq27.mp4 File size : 9.48 MiB Bit rate mode : Variable Bit rate : 3 736 kb/s Maximum bit rate : 40.0 Mb/s SSIM 0.964333 PSNR average: 42.169649

h264_nvenc -qp 27 .\ffmpeg.exe -i .\M19-1145.avi -c:v h264_nvenc -preset medium -rc constqp -qp 27 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\5-h264-nvenc_const-qp-qp27.mp4 File size : 9.59 MiB Bit rate mode : Variable Bit rate : 3 780 kb/s Maximum bit rate : 2 000 kb/s SSIM 0.964372 PSNR average: 42.179685

H.264 CRF 18 comparison

libx264 -crf 18 .\ffmpeg.exe -i .\M19-1145.avi -c:v libx264 -preset medium -crf 18 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\1-x264_crf18.mp4 File size : 33.7 MiB Bit rate : 13.6 Mb/s SSIM 0.970516 PSNR average: 43.268195

h264_nvenc -cq 18 .\ffmpeg.exe -i .\M19-1145.avi -c:v h264_nvenc -preset medium -rc vbr -cq 18 -qmin 18 -qmax 18 -b:v 0 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\2-h264-nvenc_vbr-cq18.mp4 File size : 76.3 MiB Bit rate mode : Variable Bit rate : 31.0 Mb/s Maximum bit rate : 40.0 Mb/s SSIM 0.978946 PSNR average: 45.463665

h264-nvenc -qp 18 .\ffmpeg.exe -i .\M19-1145.avi -c:v h264_nvenc -preset medium -rc constqp -qp 18 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\3-h264-nvenc_constqp-qp18.mp4 File size : 76.6 MiB Bit rate mode : Variable Bit rate : 31.1 Mb/s Maximum bit rate : 2 000 kb/s SSIM 0.978975 PSNR average: 45.470090

The closest to libx264 -crf 18 in terms of bitrate is h264_nvenc -cq 21 -qmin 21 -qmax 21 or -rc constqp -qp 21

h264_nvenc -cq 21 .\ffmpeg.exe -i .\M19-1145.avi -c:v h264_nvenc -preset medium -rc vbr -cq 21 -qmin 21 -qmax 21 -b:v 0 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\4-h264-nvenc_vbr-cq21.mp4 File size : 37.1 MiB Bit rate mode : Variable Bit rate : 15.0 Mb/s Maximum bit rate : 40.0 Mb/s SSIM 0.973280 PSNR average: 44.177173

h264_nvenc -qp 21 .\ffmpeg.exe -i .\M19-1145.avi -c:v h264_nvenc -preset medium -rc constqp -qp 21 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\5-h264-nvenc_constqp-qp21.mp4 File size : 37.3 MiB Bit rate mode : Variable Bit rate : 15.1 Mb/s Maximum bit rate : 2 000 kb/s SSIM 0.973305 PSNR average: 44.182372

H.265 CRF 25 comparison To get closer to libx264 -crf 23 quality settings in regards to SSIM and PSNR values, libx265 should be set to -crf 25 (which gives a substantial file size reduction).

libx265 -crf 25 .\ffmpeg.exe -i .\M19-1145.avi -c:v libx265 -preset medium -crf 25 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\1-x265_crf25.mp4 File size : 3.99 MiB Bit rate : 1 492 kb/s SSIM 0.964055 PSNR average: 42.128965

hevc_nvenc -cq 25 .\ffmpeg.exe -i .\M19-1145.avi -vcodec hevc_nvenc -preset medium -rc vbr -cq 25 -qmin 25 -qmax 25 -b:v 0 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\2-hevc-nvenc_vbr-cq25.mp4 File size : 11.2 MiB Bit rate : 4 426 kb/s SSIM 0.968229 (14.979722) PSNR average: 42.994428

hevc_nvenc -qp 25 .\ffmpeg.exe -i .\M19-1145.avi -vcodec hevc_nvenc -preset medium -rc constqp -qp 25 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\3-hevc-nvenc_constqp-qp25.mp4 File size : 11.3 MiB Bit rate : 4 484 kb/s SSIM 0.968248 PSNR average: 43.000091

The closest to libx265 -crf 25 in terms of bitrate is hevc_nvenc -cq 27 -qmin 27 -qmax 27 or -rc constqp -qp 27

hevc_nvenc -cq 27 .\ffmpeg.exe -i .\M19-1145.avi -vcodec hevc_nvenc -preset medium -rc vbr -cq 27 -qmin 27 -qmax 27 -b:v 0 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\4-hevc-nvenc_vbr-cq27.mp4 File size : 7.00 MiB Video Bit rate : 2 723 kb/s SSIM 0.965916 PSNR average: 42.483462

hevc_nvenc -qp 27 .\ffmpeg.exe -i .\M19-1145.avi -vcodec hevc_nvenc -preset medium -rc constqp -qp 27 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\5-hevc-nvenc_constqp-qp27.mp4 File size : 7.09 MiB Video Bit rate : 2 759 kb/s SSIM 0.965955 PSNR average: 42.492249

H.265 CRF 19 comparison To get closer to libx264 -crf 18 quality settings in regards to SSIM and PSNR values, libx265 should be set to -crf 19.

libx265 -crf 19 .\ffmpeg.exe -i .\M19-1145.avi -c:v libx265 -preset medium -crf 19 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\1-x265_crf19.mp4 File size : 14.9 MiB Video Bit rate : 5 937 kb/s SSIM 0.969753 PSNR average: 43.337615

hevc_nvenc -cq 19 .\ffmpeg.exe -i .\M19-1145.avi -vcodec hevc_nvenc -preset medium -rc vbr -cq 19 -qmin 19 -qmax 19 -b:v 0 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\2-hevc-nvenc_vbr-cq19.mp4 File size : 47.4 MiB Video Bit rate : 19.2 Mb/s SSIM 0.975980 PSNR average: 44.806863

hevc_nvenc -qp 19 .\ffmpeg.exe -i .\M19-1145.avi -vcodec hevc_nvenc -preset medium -rc constqp -qp 19 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\3-hevc-nvenc_constqp-qp19.mp4 File size : 47.7 MiB Video Bit rate : 19.3 Mb/s SSIM 0.976011 PSNR average: 44.812823

The closest to libx265 -crf 19 in terms of bitrate is hevc-nvenc -cq 24 -qmin 24 -qmax 24 or -rc constqp -qp 24

hevc_nvenc -cq 24 .\ffmpeg.exe -i .\M19-1145.avi -vcodec hevc_nvenc -preset medium -rc vbr -cq 24 -qmin 24 -qmax 24 -b:v 0 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\4-hevc-nvenc_vbr-cq24.mp4 File size : 14.0 MiB Video Bit rate : 5 562 kb/s SSIM 0.969343 PSNR average: 43.249003

hevc_nvenc -qp 24 .\ffmpeg.exe -i .\M19-1145.avi -vcodec hevc_nvenc -preset medium -rc constqp -qp 24 -pix_fmt yuv420p -color_primaries bt709 -color_trc bt709 -colorspace bt709 .\5-hevc-nvenc_constqp-qp24.mp4 File size : 14.1 MiB Bit rate : 5 636 kb/s SSIM 0.969370 PSNR average: 43.255357

cmhamiche avatar Oct 19 '19 17:10 cmhamiche

I guess all this deserves one more slider on export dialog ;)

bouncyball-git avatar Oct 22 '19 07:10 bouncyball-git

I again would more like to decide for one or two defined settings and add them to H.26x settings. Additional GUI elements aren't worth it.

masc4ii avatar Oct 22 '19 07:10 masc4ii

OKi Doki :)

bouncyball-git avatar Oct 23 '19 09:10 bouncyball-git