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

Support for Intel Arc with VAAPI #208

Open
doihaveto opened this issue Jun 26, 2024 · 12 comments
Open

Support for Intel Arc with VAAPI #208

doihaveto opened this issue Jun 26, 2024 · 12 comments

Comments

@doihaveto
Copy link

I'm trying to make this work with the av1_vaapi encoder so it will utilise my Intel Arc A380 card. The transcode command that worked for me manually is this:
ffmpeg -vaapi_device /dev/dri/renderD128 -i test.mkv -vf 'format=nv12|vaapi,hwupload' -c:v av1_vaapi -qp 28 -sn test.av1.mkv

I tried running ab-av1 auto-encode --encoder av1_vaapi -i test.mkv -o test.av1.mkv and I'm getting this error:

Incompatible pixel format 'yuv420p10le' for codec 'av1_vaapi', auto-selecting format 'vaapi'
[out#0/matroska @ 0x5b0806908e40] Codec AVOption qp (use constant quantizer mode) has not been used for any stream. The most likely reason is either wrong type (e.g. a video option with no video streams) or that it is a private option of some encoder which was not actually used for any stream.
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> av1 (av1_vaapi))
Press [q] to stop, [?] for help
Impossible to convert between the formats supported by the filter 'Parsed_null_0' and the filter 'auto_scale_0'
[vf#0:0 @ 0x5b080690bc80] Error reinitializing filters!

I then tried ab-av1 auto-encode -i test.mkv --encoder av1_vaapi --enc-input vaapi_device=/dev/dri/renderD128 --vfilter 'format=nv12|vaapi,hwupload' -o test.av1.mkv and got this error:

[hwupload @ 0x7579fc005000] A hardware device reference is required to upload frames to.
[Parsed_hwupload_4 @ 0x7579fc004f00] Query format failed for 'Parsed_hwupload_4': Invalid argument
[fc#0 @ 0x5a052980fc80] Error reinitializing filters!

It looks like ffmpeg is not getting the vaapi_device parameter. I tried to add it manually by creating a shell script that will prepend it whenever the ffmpeg command is invoked, and got this error:

Impossible to convert between the formats supported by the filter 'Parsed_setpts_5' and the filter 'auto_scale_3'
[fc#0 @ 0x5c338adbab40] Error reinitializing filters!
[fc#0 @ 0x5c338adbab40] Task finished with error code: -38 (Function not implemented)
[fc#0 @ 0x5c338adbab40] Terminating thread with return code -38 (Function not implemented)
[vost#0:0/wrapped_avframe @ 0x5c338ae943c0] Could not open encoder before EOF
[vost#0:0/wrapped_avframe @ 0x5c338ae943c0] Task finished with error code: -22 (Invalid argument)
[vost#0:0/wrapped_avframe @ 0x5c338ae943c0] Terminating thread with return code -22 (Invalid argument)
[out#0/null @ 0x5c338adafa00] Nothing was written into output file, because at least one of its streams received no packets.

This is the shell script I used:

#!/bin/bash

# Define the log file path
LOG_FILE="/ffmpeg_commands.log"

# Log the current timestamp
echo "$(date +"%Y-%m-%d %H:%M:%S") - Running command: ffmpeg $@" >> "$LOG_FILE"

# Execute the FFmpeg command
/usr/local/bin/ffmpeg -vaapi_device /dev/dri/renderD128 "$@"

I then also tried to add -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 to every ffmpeg call with my script, and got this error when using the same auto-encode command as above:

Impossible to convert between the formats supported by the filter 'graph 0 input from stream 0:0' and the filter 'auto_scale_0'
[fc#0 @ 0x643b8c513b80] Error reinitializing filters!
[fc#0 @ 0x643b8c513b80] Task finished with error code: -38 (Function not implemented)
[fc#0 @ 0x643b8c513b80] Terminating thread with return code -38 (Function not implemented)
[vost#0:0/wrapped_avframe @ 0x643b8c698200] Could not open encoder before EOF
[vost#0:0/wrapped_avframe @ 0x643b8c698200] Task finished with error code: -22 (Invalid argument)
[vost#0:0/wrapped_avframe @ 0x643b8c698200] Terminating thread with return code -22 (Invalid argument)
[out#0/null @ 0x643b8d9b8700] Nothing was written into output file, because at least one of its streams received no packets.

The ffmpeg command it's failing to run is this:
ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -vaapi_device /dev/dri/renderD128 -r 24 -i /.ab-av1-FjQUZnSHXUZ5/test.sample290+480f.av1_vaapi.crf32.mkv -r 24 -i /.ab-av1-FjQUZnSHXUZ5/test.sample290+480f.mkv -filter_complex [0:v]format=yuv420p10le,setpts=PTS-STARTPTS[dis];[1:v]format=yuv420p10le,format=nv12|vaapi,hwupload,setpts=PTS-STARTPTS[ref];[dis][ref]libvmaf=n_threads=12 -f null -

I'm not sure what else to try.

@alexheretic
Copy link
Owner

We definitely should have some better tools to debug ffmpeg calls. I'll try to add some logging options when I get some time.

The way I've used vaapi, somewhat successfully, before was more like:

ab-av1 sample-encode -i test.mkv --crf 32 -e av1_vaapi --enc-input hwaccel=vaapi --enc-input hwaccel_output_format=vaapi 

@doihaveto
Copy link
Author

I tried that, getting a similar error:

ffmpeg version 7.0.1 Copyright (c) 2000-2024 the FFmpeg developers
  built with gcc 13 (Ubuntu 13.2.0-23ubuntu4)
  configuration: --disable-debug --disable-doc --disable-ffplay --enable-alsa --enable-cuvid --enable-ffprobe --enable-gpl --enable-libaom --enable-libass --enable-libfdk_aac --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libkvazaar --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libplacebo --enable-librav1e --enable-libshaderc --enable-libsvtav1 --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpl --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-nonfree --enable-nvdec --enable-nvenc --enable-cuda-llvm --enable-opencl --enable-openssl --enable-stripping --enable-vaapi --enable-vdpau --enable-version3 --enable-vulkan
  libavutil      59.  8.100 / 59.  8.100
  libavcodec     61.  3.100 / 61.  3.100
  libavformat    61.  1.100 / 61.  1.100
  libavdevice    61.  1.100 / 61.  1.100
  libavfilter    10.  1.100 / 10.  1.100
  libswscale      8.  1.100 /  8.  1.100
  libswresample   5.  1.100 /  5.  1.100
  libpostproc    58.  1.100 / 58.  1.100
Input #0, matroska,webm, from '/.ab-av1-l2uXdheV1RVP/test.sample290+480f.av1_vaapi.crf32.mkv':
  Metadata:
    ENCODER         : Lavf61.1.100
  Duration: 00:00:19.94, start: 0.000000, bitrate: 1213 kb/s
  Stream #0:0(eng): Video: av1 (libaom-av1) (Main), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn (default)
      Metadata:
        ENCODER         : Lavc61.3.100 av1_vaapi
        DURATION        : 00:00:19.937000000
Input #1, matroska,webm, from '/.ab-av1-l2uXdheV1RVP/test.sample290+480f.mkv':
  Metadata:
    ENCODER         : Lavf61.1.100
  Duration: 00:00:20.02, start: 0.000000, bitrate: 9410 kb/s
  Stream #1:0(eng): Video: h264 (High), yuv420p(tv, bt709, progressive), 1920x1080 [SAR 1:1 DAR 16:9], 23.98 fps, 23.98 tbr, 1k tbn (default)
      Metadata:
        DURATION        : 00:00:20.019000000
Stream mapping:
  Stream #0:0 (av1) -> format:default
  Stream #1:0 (h264) -> format:default
  libvmaf:default -> Stream #0:0 (wrapped_avframe)
Press [q] to stop, [?] for help
Impossible to convert between the formats supported by the filter 'graph 0 input from stream 0:0' and the filter 'auto_scale_0'
[fc#0 @ 0x5cebde1fcb80] Error reinitializing filters!
[fc#0 @ 0x5cebde1fcb80] Task finished with error code: -38 (Function not implemented)
[fc#0 @ 0x5cebde1fcb80] Terminating thread with return code -38 (Function not implemented)
[vost#0:0/wrapped_avframe @ 0x5cebde2b6680] Could not open encoder before EOF
[vost#0:0/wrapped_avframe @ 0x5cebde2b6680] Task finished with error code: -22 (Invalid argument)
[vost#0:0/wrapped_avframe @ 0x5cebde2b6680] Terminating thread with return code -22 (Invalid argument)
[out#0/null @ 0x5cebde383400] 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!

@alexheretic
Copy link
Owner

It's the vmaf run which is failing, which implies encoding ran successfully.

If you run with --keep you can look at the sample & encoded sample in dir .ab-av1-xxxx/. When we have the exact ffmpeg vmaf call it may be clearer why it doesn't work here.

@alexheretic
Copy link
Owner

I've added the full ffmpeg command to the error output in 58384eb. Can you install from the latest git commit and retry to show the exact failing vmaf call?

@doihaveto
Copy link
Author

doihaveto commented Jun 27, 2024

I'm sorry, I made a mistake in my last comment. The command you sent did work:

# ab-av1 sample-encode -i test.mkv --crf 32 -e av1_vaapi --enc-input hwaccel=vaapi --enc-input hwaccel_output_format=vaapi
- Sample 1 (13%) vmaf 28.48
  00:00:11 Sample 1/1 ######################################### (sampling,     eta 0s)
Encode with: ab-av1 encode -e av1_vaapi -i /data/hdd1/downloads/test.mkv --crf 32 --enc-input hwaccel=vaapi --enc-input hwaccel_output_format=vaapi

VMAF 28.48 predicted video stream size 86.82 MiB (13%) taking 31 seconds

I still had my script that changed the ffmpeg command, that's why it returned an error.

As you can see though, now I have a different problem - the VMAF score is way too low. When I run auto-encode I get this:

# ab-av1 auto-encode -i test.mkv -e av1_vaapi --enc-input hwaccel=vaapi --enc-input hwaccel_output_format=vaapi -o test.av1.mkv
  Searching 00:00:18 ######################################### (crf 10, VMAF 28.48, size 13%)
Error: Failed to find a suitable crf

I don't think it's an issue with the video file - I used the same one with av1_nvenc on a different machine and it found a suitable crf value just fine.

@alexheretic
Copy link
Owner

With the latest commit 6d66b20 you can run with RUST_LOG=ab_av1=debug ab-av1 ... to print out all ffmpeg calls.

I think in this case vmaf is not liking the input, it may be possible to jiggle it so it works or perhaps it is some issue with the encoder that causes this. If you can find better working ffmpeg vmaf invocation I can perhaps address this here.

@doihaveto
Copy link
Author

A bit of a mess, but here's the command I used and the output:

# RUST_LOG=ab_av1=debug ab-av1 auto-encode -i test.mkv -e av1_vaapi --enc-input hwaccel=vaapi --enc-input hwaccel_output_format=vaapi
Encoding test.av1_vaapi.mkv
⠁ Searching 00:00:00 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (sampling crf 132, eta 0s)[2024-06-27T17:49:16Z INFO  ab_av1::command::sample_encode] encoding sample 1/1 crf 132
⠁ Searching 00:00:00 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (sampling crf 132, eta 0s)[2024-06-27T17:49:16Z DEBUG ab_av1::ffmpeg] cmd `ffmpeg -y -hwaccel vaapi -hwaccel_output_format vaapi -i /.ab-av1-7DgGFBkgRgDy/test.sample290+480f.mkv -c:v av1_vaapi -g 240 -q 132 -pix_fmt yuv420p10le -an /.ab-av1-7DgGFBkgRgDy/test.sample290+480f.av1_vaapi.crf132.mkv`
⠲ Searching 00:00:00 ###################--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (sampling crf 132, eta 8s)[2024-06-27T17:49:16Z INFO  ab_av1::vmaf] vmaf test.sample290+480f.av1_vaapi.crf132.mkv vs reference test.sample290+480f.mkv
[2024-06-27T17:49:16Z DEBUG ab_av1::vmaf] cmd `ffmpeg -r 24 -i /.ab-av1-7DgGFBkgRgDy/test.sample290+480f.av1_vaapi.crf132.mkv -r 24 -i /.ab-av1-7DgGFBkgRgDy/test.sample290+480f.mkv -filter_complex [0:v]format=yuv420p10le,setpts=PTS-STARTPTS[dis];[1:v]format=yuv420p10le,setpts=PTS-STARTPTS[ref];[dis][ref]libvmaf=n_threads=12 -f null -`
⠁ Searching 00:00:08 ########################################################------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (sampling crf 132, eta 20s)[2024-06-27T17:49:24Z INFO  ab_av1::command::sample_encode] crf 132 VMAF 30.49 predicted video stream size 27.52 MiB (4%) taking 25 seconds
⠁ Searching 00:00:08 ########################################################-------------------------------------------------------------------------------------------------------------------------------------------------------------------------- (sampling crf 71, eta 20s)[2024-06-27T17:49:24Z INFO  ab_av1::command::sample_encode] encoding sample 1/1 crf 71
[2024-06-27T17:49:24Z DEBUG ab_av1::ffmpeg] cmd `ffmpeg -y -hwaccel vaapi -hwaccel_output_format vaapi -i /.ab-av1-7DgGFBkgRgDy/test.sample290+480f.mkv -c:v av1_vaapi -g 240 -q 71 -pix_fmt yuv420p10le -an /.ab-av1-7DgGFBkgRgDy/test.sample290+480f.av1_vaapi.crf71.mkv`
⠲ Searching 00:00:09 #####################################################################################--------------------------------------------------------------------------------------------------------------------------------------------- (sampling crf 71, eta 17s)[2024-06-27T17:49:25Z INFO  ab_av1::vmaf] vmaf test.sample290+480f.av1_vaapi.crf71.mkv vs reference test.sample290+480f.mkv
[2024-06-27T17:49:25Z DEBUG ab_av1::vmaf] cmd `ffmpeg -r 24 -i /.ab-av1-7DgGFBkgRgDy/test.sample290+480f.av1_vaapi.crf71.mkv -r 24 -i /.ab-av1-7DgGFBkgRgDy/test.sample290+480f.mkv -filter_complex [0:v]format=yuv420p10le,setpts=PTS-STARTPTS[dis];[1:v]format=yuv420p10le,setpts=PTS-STARTPTS[ref];[dis][ref]libvmaf=n_threads=12 -f null -`
⠁ Searching 00:00:17 ################################################################################################################------------------------------------------------------------------------------------------------------------------ (sampling crf 71, eta 17s)[2024-06-27T17:49:33Z INFO  ab_av1::command::sample_encode] crf 71 VMAF 28.57 predicted video stream size 105.90 MiB (16%) taking 24 seconds
⠁ Searching 00:00:17 ################################################################################################################------------------------------------------------------------------------------------------------------------------ (sampling crf 10, eta 17s)[2024-06-27T17:49:33Z INFO  ab_av1::command::sample_encode] encoding sample 1/1 crf 10
[2024-06-27T17:49:33Z INFO  ab_av1::command::sample_encode] crf 10 VMAF 28.48 predicted video stream size 86.82 MiB (13%) taking 32 seconds (cache)
  Searching 00:00:17 ############################################################################################################################################################################################################################## (crf 10, VMAF 28.48, size 13%)Error: Failed to find a suitable crf

@alexheretic
Copy link
Owner

alexheretic commented Jun 27, 2024

Thanks. If you use --keep (maybe also --cache false if necessary) you can try running the vmaf directly on the samples and perhaps see if modifying it yields better results. It would also be possible to report this upstream that way, in the case ffmpeg is doing the wrong thing here. It seems possible that the problem could also be with the encoded result, maybe it has a bug or something that results in low vmaf when comparing to the original.

@doihaveto
Copy link
Author

I checked the sample files that came from RUST_LOG=ab_av1=debug ab-av1 auto-encode -i test.mkv -e av1_vaapi --enc-input hwaccel=vaapi --enc-input hwaccel_output_format=vaapi --keep --cache false
The original file looks fine, but the encoded ones (test.sample290+480f.av1_vaapi.crf132.mkv, test.sample290+480f.av1_vaapi.crf71.mkv, and test.sample290+480f.av1_vaapi.crf10.mkv) are all choppy and with pixelated areas in some frames, so the vmaf score is probably accurate and the encoding is defective.

@mr44er
Copy link

mr44er commented Jun 30, 2024

That is likely the pain with variable framerate in your source file.
Play with mpv, activate SHIFT+I if framerate is changing (estimated has other value) or check with mediainfo file and if there is VFR anywhere, that's the problem.

@doihaveto
Copy link
Author

The framerate is constant, 23.976. Checked the original file with both mpv and mediainfo.

@mr44er
Copy link

mr44er commented Jun 30, 2024

It's possible that the codec in the test.mkv isn't compatible with hwaccel=vaapi or the file is corrupt at some point.
If that happens and the problem isn't VFR I write the problematic file again and into ffmpeg lossless codec (it's quick, x264 lossless mode should do the same job and your way to go if vaapi can't read ffv):
ffmpeg -i test.mkv -map_metadata -1 -map_chapters -1 -bsf:v filter_units=remove_types=6 -vcodec ffv1 -level 3 -an -sn -dn purevideofile.mkv

If frames are corrupt, ffmpeg will show it.

Then I would do ab-av1 crf-search -i purevideofile.mkv and compare with ab-av1 crf-search -i test.mkv completely in software mode to check if they both get same results first.

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