Skip to content

qsv: enable QSV encoder via FFmpeg API - #6446

Merged
galad87 merged 10 commits into
HandBrake:masterfrom
galinart:enable-ffmpeg-qsv-encoder
Mar 15, 2025
Merged

galad87 merged 10 commits into
HandBrake:masterfrom
galinart:enable-ffmpeg-qsv-encoder

Conversation

@galinart

Copy link
Copy Markdown
Contributor

Switched QSV encoding from native VPL to FFmpeg API.
It comes with a lot of advantages:

  • Aligned implementation to other HW vendors
  • Less code divergence
  • Much simpler implementation
  • Less maintenance efforts

Both --enable-hw-decoding qsv and --enable-qsv-decoding CLI options supported for backward compatibility.

@galinart
galinart force-pushed the enable-ffmpeg-qsv-encoder branch 5 times, most recently from 548add5 to a7b85b0 Compare November 28, 2024 21:57

@galad87 galad87 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems mostly ok, however encavcodec.c has become a complete mess, and it still doesn't pass the correct timestamps to the encoders, so if the source is VFR the rate control won't be able to correctly assign the bits.

Is there any plan to add a way to insert Dolby Vision RPUs and HDR10 Plus SEIs in the FFmpeg QSV encoders?

Comment thread libhb/cropscale.c Resolved
Comment thread libhb/encavcodec.c Resolved
Comment thread libhb/encavcodec.c

#if HB_PROJECT_FEATURE_QSV
// postponed encoder initialization, reused code from encavcodecInit()
if (hb_hwaccel_is_full_hardware_pipeline_enabled(pv->job) &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This postponed initialization is a quite ugly, isn't there a way to avoid it? Like manually creating a hw frames context, and is the hw frames context so expensive that needs to be reused?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not the prettiest piece of code, but the initialization logic in ffmpeg and handbrake is different. in case of two separate contexts, it requires copying frames each time, which is quite expensive.

Comment thread libhb/hb_json.c Resolved
Comment thread libhb/hbavfilter.c
hb_buffer_t * buf;
#if HB_PROJECT_FEATURE_QSV
if (hb_qsv_hw_filters_via_video_memory_are_enabled(graph->job))
if (hb_hwaccel_is_full_hardware_pipeline_enabled(graph->job) &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in encavcodec.c, is there a way to avoid waiting for these hw frames contexts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ffmpeg inside creates the video memory hw frames context for output frames during first frame processing

Comment thread libhb/hwaccel.c Resolved
Comment thread libhb/hwaccel.c Resolved
@sr55 sr55 added this to the 1.10.0 milestone Dec 2, 2024
@sr55

sr55 commented Dec 2, 2024 •

Copy link
Copy Markdown
Contributor

We can review and possibly close off the following:

@sr55
sr55 self-requested a review December 9, 2024 16:12
@sr55

sr55 commented Dec 9, 2024 •

Copy link
Copy Markdown
Contributor

Basic Testing

First pass in testing. Just basic "full path" encodes with each encoder:

Test Result
H.264 OK
H.265 OK
H.265 10-bit OK
AV1 OK
AV1 10-Bit OK

Everything seems fine. Good utilisation of the GPU.


HyperEncode Testing

It looks like there may be a performance regression using HyperEncode

A370M.txt ~176fps
HyperEncode.txt ~96fps

Seeping utilisation spread across the GPU and Integrated GPU but it's 40~60% range.

Update: Moved to 101.6314 driver and hyperencode isn't being used anymore. It's all going to the A370M

@sr55
sr55 removed their request for review December 9, 2024 17:02
@galad87

galad87 commented Feb 17, 2025

Copy link
Copy Markdown
Contributor

Do we want to merge this and then improve things later?

@galinart

Copy link
Copy Markdown
Contributor Author

I will double check this PR with latest changes from master and let you know.

In regards to the Hyper Encode issue, it looks that it is reproducible even with current major release. Investigation is in progress and fix might be provided separately.

@galinart

Copy link
Copy Markdown
Contributor Author

Thank you for feedback.

PR has been updated and tested against latest master.

Comment thread libhb/hwaccel.c Resolved
Comment thread libhb/encavcodec.c
* Reload colorimetry settings in case custom
* values were set in the encoder_options string.
*/
pv->job->color_prim_override = pv->context->color_primaries;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's too late to set these, because the previous values have already been set to the mp4/mkv/webm container.

@galinart galinart Feb 28, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhere in encavcodecInit, hopefully the avcodec_open won't change the context values.

@galad87

galad87 commented Feb 22, 2025

Copy link
Copy Markdown
Contributor

Encoding a 10bit video to 10bit using the full hw path seems to introduce a lot of artifacts, but 10bit to 8bit and 8bit to 8bit looks ok.

@galinart

Copy link
Copy Markdown
Contributor Author

Encoding a 10bit video to 10bit using the full hw path seems to introduce a lot of artifacts, but 10bit to 8bit and 8bit to 8bit looks ok.

We are not able to reproduce on Battlemage, LunarLake, AlderLake. Can you please share more information?

  • HW setup
  • Driver version
  • Is it for specific file only?

Thanks

@galad87

galad87 commented Feb 28, 2025

Copy link
Copy Markdown
Contributor

Here's a sample to reproduce the issue.

https://subler.org/downloads/Sample-10bit.mp4

I tried on Lunar Lake, with the latest driver provided by Windows Update:

HandBrake Nightly 20250221212445-ebe3d3f (2025022801)
OS: Microsoft Windows NT 10.0.26100.0
CPU: Intel(R) Core(TM) Ultra 9 288V
Ram: 32191 MB,
GPU Information:
Intel(R) Arc(TM) 140V GPU (16GB) - 32.0.101.5730
Screen: 1440x900
Temp Dir: C:\Users\galad\AppData\Local\Temp
Install Dir: C:\Program Files\HandBrake Nightly
Data Dir: C:\Users\galad\AppData\Roaming\HandBrake\Nightly


Starting Encode ...

[08:56:04] base preset: AV1 QSV 2160p 4K (Modified)
[08:56:04] Remote Process started with Process ID: 10676 using port: 8037. Max Allowed Instances: 1
[08:56:04] Worker: Starting HandBrake Engine ...
[08:56:04] Worker: Parent Process Id 3568
[08:56:04] Worker: Starting Web Server on port 8037 ...
[08:56:04] Worker: Starting Listener: 1
[08:56:04] Worker: Server Started
[08:56:05] Worker: Disconnected worker monitoring enabled!
[08:56:05] Compile-time hardening features are enabled
Cannot load nvEncodeAPI64.dll
[08:56:05] vcn: not available on this system
[08:56:06] qsv: is available on this system
[08:56:06] hb_init: starting libhb thread

[08:56:06] Starting work at: Fri Feb 28 08:56:06 2025
[08:56:06] 1 job(s) to process
[08:56:06] json job:
{
"Audio": {
"AudioList": [
{
"Bitrate": 160,
"DRC": 0,
"Encoder": "av_aac",
"Gain": 0,
"Mixdown": 4,
"NormalizeMixLevel": false,
"Samplerate": 0,
"Name": "Stereo",
"Track": 0,
"DitherMethod": 0
}
],
"CopyMask": [
"copy:aac"
],
"FallbackEncoder": "none"
},
"Destination": {
"ChapterList": [
{
"Name": "Chapter 1"
}
],
"ChapterMarkers": true,
"AlignAVStart": false,
"File": "C:\Users\galad\Videos\Sample 10Bit 2.mp4",
"Options": {
"IpodAtom": false,
"Optimize": false
},
"Mux": "av_mp4"
},
"Filters": {
"FilterList": [
{
"ID": 20,
"Settings": {
"crop-bottom": "0",
"crop-left": "0",
"crop-right": "0",
"crop-top": "0",
"height": "1080",
"width": "1920"
}
},
{
"ID": 11,
"Settings": {
"mode": "0"
}
}
]
},
"PAR": {
"Num": 1,
"Den": 1
},
"Metadata": {
"CreationTime": "2021-05-31T07:50:07.000000Z"
},
"SequenceID": 0,
"Source": {
"Angle": 1,
"Range": {
"Type": "chapter",
"Start": 1,
"End": 1
},
"Title": 1,
"Path": "C:\Users\galad\Samples\Sample 10bit 2.mp4",
"HWDecode": 2,
"KeepDuplicateTitles": false
},
"Subtitle": {
"Search": {
"Burn": false,
"Default": false,
"Enable": false,
"Forced": false
},
"SubtitleList": []
},
"Video": {
"Encoder": "qsv_av1_10bit",
"Level": "auto",
"MultiPass": false,
"Turbo": false,
"ColorMatrixCode": 0,
"Options": "lowpower=1",
"Preset": "speed",
"Profile": "auto",
"Quality": 14,
"QSV": {
"Decode": true
},
"HardwareDecode": 0
}
}
[08:56:06] CPU: Intel(R) Core(TM) Ultra 9 288V
[08:56:06] - logical processor count: 8
[08:56:06] Intel Quick Sync Video support: yes, gpu list: 0
[08:56:06] Intel Quick Sync Video integrated adapter with index 0
[08:56:06] Impl mfx-gen library path: C:\Windows\System32\DriverStore\FileRepository\iigd_dch.inf_amd64_49abc5fad909446e\libmfx64-gen.dll
[08:56:06] - Intel Media SDK hardware: API 2.11 (minimum: 1.3)
[08:56:06] - Decode support: h264 hevc (8bit: yes, 10bit: yes) av1 (8bit: yes, 10bit: yes) vvc (8bit: yes, 10bit: yes)
[08:56:06] - H.264 encoder: yes
[08:56:06] - preferred implementation: hardware (1) via D3D11
[08:56:06] - capabilities (hardware): lowpower breftype icq vsinfo chromalocinfo opt1 opt2+mbbrc+extbrc+trellis+repeatpps+ib_adapt+nmpslice
[08:56:06] - H.265 encoder: yes (8bit: yes, 10bit: yes)
[08:56:06] - preferred implementation: hardware (1) via D3D11
[08:56:06] - capabilities (hardware): lowpower bpyramid icq vsinfo masteringinfo cllinfo opt1
[08:56:06] - AV1 encoder: yes (8bit: yes, 10bit: yes)
[08:56:06] - preferred implementation: hardware (1) via D3D11
[08:56:06] - capabilities (hardware): lowpower bpyramid icq vsinfo masteringinfo cllinfo opt1 av1screencontent av1bitstream
[08:56:06] hb_scan: path=C:\Users\galad\Samples\Sample 10bit 2.mp4, title_index=1
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'C:\Users\galad\Samples\Sample 10bit 2.mp4':
Metadata:
major_brand : mp42
minor_version : 512
compatible_brands: mp42iso2mp41
creation_time : 2021-05-31T07:50:07.000000Z
encoder : Lavf61.7.100
Duration: 00:00:33.37, start: 0.000000, bitrate: 8641 kb/s
Stream #0:00x1: Video: hevc (Main 10) (hvc1 / 0x31637668), yuv420p10le(tv, bt2020nc/bt2020/smpte2084), 1920x1080 [SAR 1:1 DAR 16:9], 8494 kb/s, 59.94 fps, 59.94 tbr, 90k tbn (default)
Metadata:
creation_time : 2021-05-31T07:50:07.000000Z
handler_name : VideoHandler
vendor_id : [0][0][0][0]
Side data:
Mastering Display Metadata, has_primaries:1 has_luminance:1 r(0.1310,0.0460) g(0.7080,0.2920) b(0.1700 0.7970) wp(0.3127, 0.3127) min_luminance=0.000000, max_luminance=1000.000000
Content Light Level Metadata, MaxCLL=1100, MaxFALL=180
Stream #0:10x2: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
creation_time : 2021-05-31T07:50:07.000000Z
handler_name : Stereo
vendor_id : [0][0][0][0]
title : Stereo
[08:56:06] scan: decoding previews for title 1
[08:56:06] scan: audio 0x1: aac, rate=44100Hz, bitrate=128074 Unknown (AAC LC, 2.0 ch, 128 kbps)
[08:56:07] using container PAR 1:1
[08:56:07] scan: 10 previews, 1920x1080, 59.940 fps, autocrop = 0/0/0/0, aspect 16:9, PAR 1:1, color profile: 9-16-9, chroma location: left
[08:56:07] scan: mastering display metadata: r(0.1310,0.0460) g(0.7080,0.2920) b(0.1700 0.7970) wp(0.3127, 0.3127) min_luminance=0.000000, max_luminance=1000.000000
[08:56:07] scan: content light level: max_cll=1100, max_fall=180
[08:56:07] scan: supported video decoders: avcodec qsv hwaccel
[08:56:07] libhb: scan thread found 1 valid title(s)
[08:56:07] Starting Task: Encoding Pass
[08:56:07] work: skipping crop/scale filter
[08:56:07] work: track 1, dithering not supported by codec
[08:56:07] work: only 1 chapter, disabling chapter markers
[08:56:07] job configuration:
[08:56:07] * source
[08:56:07] + C:\Users\galad\Samples\Sample 10bit 2.mp4
[08:56:07] + title 1, chapter(s) 1 to 1
[08:56:07] + container: mov,mp4,m4a,3gp,3g2,mj2
[08:56:07] + data rate: 8641 kbps
[08:56:07] * destination
[08:56:07] + C:\Users\galad\Videos\Sample 10Bit 2.mp4
[08:56:07] + container: MPEG-4 (libavformat)
[08:56:07] * video track
[08:56:07] + decoder: hevc_qsv 10-bit (p010le)
[08:56:07] + bitrate 8494 kbps
[08:56:07] + filter
[08:56:07] + Framerate Shaper (mode=0)
[08:56:07] + frame rate: same as source (around 59.940 fps)
[08:56:07] + Output geometry
[08:56:07] + storage dimensions: 1920 x 1080
[08:56:07] + pixel aspect ratio: 1 : 1
[08:56:07] + display dimensions: 1920 x 1080
[08:56:07] + encoder: AV1 10-bit (Intel QSV)
[08:56:07] + preset: speed
[08:56:07] + options: lowpower=1
[08:56:07] + profile: auto
[08:56:07] + level: auto
[08:56:07] + quality: 14.00 (ICQ)
[08:56:07] + color profile: 9-16-9
[08:56:07] + color range: tv
[08:56:07] + chroma location: left
[08:56:07] + mastering display metadata: r(0.1310,0.0460) g(0.7080,0.2920) b(0.1700 0.7970) wp(0.3127, 0.3127) min_luminance=0.000000, max_luminance=1000.000000
[08:56:07] + content light level: max_cll=1100, max_fall=180
[08:56:07] * audio track 1
[08:56:07] + name: Stereo
[08:56:07] + decoder: Unknown (AAC LC, 2.0 ch, 128 kbps) (track 1, id 0x1)
[08:56:07] + bitrate: 128 kbps, samplerate: 44100 Hz
[08:56:07] + mixdown: Stereo
[08:56:07] + encoder: AAC (libavcodec)
[08:56:07] + bitrate: 160 kbps, samplerate: 44100 Hz
[08:56:07] sync: expecting 2000 video frames
[08:56:07] encavcodecInit: AV1 (Intel Quick Sync Video)
[08:56:07] encavcodec: encoding with preset veryfast
[08:56:07] encavcodec: using full QSV via video memory path
[08:56:07] encavcodec: using Low Power mode
[08:56:07] encavcodec: encoding with brc ICQ 14
[08:56:07] encavcodec: encoding with stored aspect 1/1
[08:56:07] sync: first pts video is 0
[08:56:07] sync: "Chapter 1" (1) at frame 1 time 0
[08:56:07] sync: first pts audio 0x1 is 0
[08:56:12] reader: done. 1 scr changes
[08:56:12] work: average encoding speed for job is 367.052032 fps
[08:56:12] vfr: 1695 frames output, 0 dropped
[08:56:12] vfr: lost time: 0 (0 frames)
[08:56:12] vfr: gained time: 0 (0 frames) (0 not accounted for)
[08:56:12] aac-decoder done: 1437 frames, 0 decoder errors
[08:56:12] hevc_qsv-decoder done: 1695 frames, 305 decoder errors
[08:56:12] sync: got 1695 frames, 2000 expected
[08:56:12] sync: framerate min 0.392 fps, max 59.940 fps, avg 50.799 fps
[08:56:12] mux: track 0, 1695 frames, 129905902 bytes, 31146.27 kbps, fifo 256
[08:56:12] mux: track 1, 1438 frames, 673970 bytes, 161.59 kbps, fifo 512
[08:56:12] Finished work at: Fri Feb 28 08:56:12 2025
[08:56:12] libhb: work result = 0

Job Completed!

@galinart

Copy link
Copy Markdown
Contributor Author

@galad87 thanks for the details.

Still no luck..

@galad87

galad87 commented Feb 28, 2025

Copy link
Copy Markdown
Contributor

It doesn't happen with the latest driver, so I guess whatever was going wrong has already been fixed 🎉

@galinart

Copy link
Copy Markdown
Contributor Author

Thanks for confirmation!

@sr55

sr55 commented Mar 2, 2025

Copy link
Copy Markdown
Contributor

I've run a suite of tests on it. Couple of questions:

  1. The level option doesn't appear to work. Anything other than "Auto" encodes but the job fails to complete at the end. Just set say Level 5.1 to reproduce.
  2. Under what situations does HyperEncode get used? Is there still a push to use that? It seems to predominantly use the A370M in the laptop I have here.
  3. On one encode, I got a "encavcodecWork: Unknown avcodec option: dual_gfx" using the source @galad87 posted above. Log attached. HandBrake Nightly 20250221212445-eb.txt

Otherwise, All my encodes, including 10bit and 8bit sources are working so it's looking mostly good.

@galinart

galinart commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback.

  1. We will fix that
  2. There is no push to use that. Currently there are some issues which are under investigation. Is it only A370M for both AVC and HEVC for balance and quality mode for 4K -> 4K content?
  3. For now, It is enabled only for AVC and HEVC in FFmpeg codebase https://github.com/FFmpeg/FFmpeg/blob/0e917389fe73c932049635d947bba076f1709589/libavcodec/qsvenc.c#L1251

@galad87

galad87 commented Mar 12, 2025

Copy link
Copy Markdown
Contributor

Is there any plan to implement AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE in the QSV encoder? Without that it would be quite hard to support Dolby Vision and HDR10 Plus.

Obviously a way to insert NALs and SEIs and OBUs would be better, but with AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE at least we could do it in HandBrake by reusing the existing code for HEVC.

@galinart

Copy link
Copy Markdown
Contributor Author

I suggest to merge it while we don't have much conflicts and not block other PRs

Hyper Encode issues are on track on our side and will be addressed as separate PR along with driver update.

@galinart

Copy link
Copy Markdown
Contributor Author

Is there any plan to implement AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE in the QSV encoder? Without that it would be quite hard to support Dolby Vision and HDR10 Plus.

Obviously a way to insert NALs and SEIs and OBUs would be better, but with AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE at least we could do it in HandBrake by reusing the existing code for HEVC.

For now, I don't see that as planned. I will keep you updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants