Skip to content

Vaapi encoder Issue #1083 - #1111

Closed
sgothel wants to merge 28 commits into
HandBrake:masterfrom
sgothel:vaapi-encoder
Closed

sgothel wants to merge 28 commits into
HandBrake:masterfrom
sgothel:vaapi-encoder

Conversation

@sgothel

@sgothel sgothel commented Jan 12, 2018

Copy link
Copy Markdown
Contributor
VAAPI: Initial Patch (WIP)

- Enabling VAAPI for GNU/Linux (SYS_LINUX) only, adding libraries 'X11 va-drm va-x11 va'
  and USE_VAAPI define.

- hb.c:
  - Adding global static 'vaapi_device_ctx0' default device
    initialized via hb_avcodec_init() and released via hb_avcodec_free()

  - hb_avcodec_vaapi_set_hwframe_ctx() attaches the new AVHWFramesContext
    to the AVCodecContext

  - hb_avcodec_test_encoder(..) adds VAAPI branch,
    needs refinement

- encavcodec.c
  - adding VAAPI branches, enabling available, needs refinement

Discussion:

First of all, only on Intel hardware I was able to produce a correct
encoded result. On AMD we seemingly have YUV420 / NV12 artifacts.
Therefor I need to learn more details on the pix_fmt handling
as described below.

While adding the VAAPI ffmpeg support the following might be an issue.

Usually HB seems to operate with AV_PIX_FMT_YUV420P in general
being set in the AVCodecContext's pix_fmt.

VAAPI performs as follows:
  - It uses hardware frames on the target device, which are being transported
    from the software device. Both format should be allowed to differ??

  - AVCodecContext's pix_fmt uses AV_PIX_FMT_VAAPI

  - AVHWFramesContext: format uses AV_PIX_FMT_VAAPI and sw_format AV_PIX_FMT_NV12,
    the latter hinting on the actual hw-frame's target format.

  - AV_PIX_FMT_NV12 uses interleaved UV data, where AV_PIX_FMT_YUV420P uses
    seperated planes. Both use a separated Y plane upfront.
    Therefor both formats are not picture compatible, memory requirements are same.

  - Encode(..) hardcodes the source frame's to AV_PIX_FMT_YUV420P in VAAPI case,
    usually the AVCodecContext's pix_fmt is being used.
    I need to identify how HB uses AVCodecContext's pix_fmt, i.e. respects
    the desired pix_fmt.

    Then a hw_frame gets allocated and the source frame is being transported
    to this target. Probably it happens here in VAAPI AMD case, that the YUV420
    data is not being transformed to NV12.

    Now the hw_frame is being sent 'avcodec_send_frame' and the code-path re-aligns
    with non VAAPI.

Further fixes to do:
    Naming of VAAPI's profile and level .. figuring out which settings are generally accepted.
    Now the bf (b-frames) is set to zero, since it produces a failure otherwise.
    The latter should only happen in certain profiles, but .. it does even in 100=high

sgothel and others added 28 commits December 25, 2017 23:51
Patch 1/2 for HandBrake#974
moving to FFMPEG 3.4.1 from LIBAV 12.2.

The re-ordering of to be linked modules was required to solve
statically linked dependencies. See libhb/module.defs etc.
Patch 2/2 for HandBrake#974
moving to FFMPEG 3.4.1 from LIBAV 12.2.

All patches have been moved to subfolder 'old' since they do not apply cleanly anymore.
Work has to be performed to validate whether patches are still required.
- Fix HB_VCODEC_FFMPEG_MASK covered values to lie within mask
- Fix HB_VCODEC_FFMPEG_H264 usage (gtk version missed a rename)
- Fix FFMPEG hwaccel build is required for Nvenc

Basically working:
  - h264_nvenc
  - hevc_nvenc
    Possible optimization:
  - Even in constant quality mode, a user ceiling bit_rate might be desirable,
    currently only the maximum is used like in VP8/9: w x h x fps

  Remaining issues:
    1) Muxer issue with h264_nvenc + MKV, ffprobe shows:

    [h264 @ 0x55ae4dedacc0] sps_id 32 out of range
    [h264 @ 0x55ae4dedacc0] Invalid nal size 1677734572
    [h264 @ 0x55ae4dedacc0] missing picture in access unit with size 40644

     Notable: This does not happen with hevc_nvenc

  Tested:
    - GNU/Linux Debian 9
    - GeForce GTX 950
    - BD 1080p source, cropped: Around 70fps h264 + hevc
Adding 'rc-lookahead 32, init_qp 1, spatial_aq 1, aq-strength 8' for h264 and hevc
in case of VBR CQ mode, see
  <https://devtalk.nvidia.com/default/topic/1013152/video-technologies/crf-for-nvenc/post/5179877/#5179877>
and NVENC_VideoEncoder_API_ProgGuide.pdf v08 Chapter 8 p27, as well as Table 1 p33.
1st patch was ab0cca4
NVENC: Adding ffmpeg nvenc.c patch to add verbosity on parameters
Initial VBR CQ patch 037de2c
'NVENC: Fix VBR CQ Settings to be more adaptive'
caused encoding errors w/ max lookahead 32.
Now using lookahead 16 only plus the driver settings default of aq-strength.

This fixes encoding/muxing using nvenc and also removes related av-sync issues
on certain input streams.
@sr55 'Something odd happened with this comment. It was incorrect. HandBrake.Interop is gone. Correct path is: HandBrake.ApplicationServices\Interop\HbLib'
HandBrake#983 (comment)
Adding the specific FFMPEG encoder suffix allows support for more encoders like VAAPI.

Cleaned up overall HB_VCODED_* to 32bit signed integer space within 0x7FFFFFFF.
Proper availability tests requires to open the codec's context,
only if successful we can deem it available on the runtime platform.
…codec_close(..)

Hence rename hb_avcodec_close -> hb_avcodec_free_context and pass the required ptr-ptr.
avcodec_free_context(..) ensures releasing of all resources attached to the context.
… def instead

Also add HB_VCODEC_FFMPEG_{VP8,VP9}_VAAPI for future use.
- Enabling VAAPI for GNU/Linux (SYS_LINUX) only, adding libraries 'X11 va-drm va-x11 va'
  and USE_VAAPI define.

- hb.c:
  - Adding global static 'vaapi_device_ctx0' default device
    initialized via hb_avcodec_init() and released via hb_avcodec_free()

  - hb_avcodec_vaapi_set_hwframe_ctx() attaches the new AVHWFramesContext
    to the AVCodecContext

  - hb_avcodec_test_encoder(..) adds VAAPI branch,
    needs refinement

- encavcodec.c
  - adding VAAPI branches, enabling available, needs refinement

Discussion:

First of all, only on Intel hardware I was able to produce a correct
encoded result. On AMD we seemingly have YUV420 / NV12 artifacts.
Therefor I need to learn more details on the pix_fmt handling
as described below.

While adding the VAAPI ffmpeg support the following might be an issue.

Usually HB seems to operate with AV_PIX_FMT_YUV420P in general
being set in the AVCodecContext's pix_fmt.

VAAPI performs as follows:
  - It uses hardware frames on the target device, which are being transported
    from the software device. Both format should be allowed to differ??

  - AVCodecContext's pix_fmt uses AV_PIX_FMT_VAAPI

  - AVHWFramesContext: format uses AV_PIX_FMT_VAAPI and sw_format AV_PIX_FMT_NV12,
    the latter hinting on the actual hw-frame's target format.

  - AV_PIX_FMT_NV12 uses interleaved UV data, where AV_PIX_FMT_YUV420P uses
    seperated planes. Both use a separated Y plane upfront.
    Therefor both formats are not picture compatible, memory requirements are same.

  - Encode(..) hardcodes the source frame's to AV_PIX_FMT_YUV420P in VAAPI case,
    usually the AVCodecContext's pix_fmt is being used.
    I need to identify how HB uses AVCodecContext's pix_fmt, i.e. respects
    the desired pix_fmt.

    Then a hw_frame gets allocated and the source frame is being transported
    to this target. Probably it happens here in VAAPI AMD case, that the YUV420
    data is not being transformed to NV12.

    Now the hw_frame is being sent 'avcodec_send_frame' and the code-path re-aligns
    with non VAAPI.

Further fixes to do:
    Naming of VAAPI's profile and level .. figuring out which settings are generally accepted.
    Now the bf (b-frames) is set to zero, since it produces a failure otherwise.
    The latter should only happen in certain profiles, but .. it does even in 100=high
@maximd33

Copy link
Copy Markdown
Contributor

an interesting pull request - need to try it

@sr55

sr55 commented Jun 29, 2018

Copy link
Copy Markdown
Contributor

Closing in favour of our native NVEnc support on Linux

@sr55 sr55 closed this Jun 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants