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

Q: Dolby Vision to SDR #9982

Closed
regystro opened this issue Mar 15, 2022 · 20 comments
Closed

Q: Dolby Vision to SDR #9982

regystro opened this issue Mar 15, 2022 · 20 comments

Comments

@regystro
Copy link

Hi.

Since last git builds support --vo=gpu-next for Dolby Vision reshaping, which by the way works great, I was wondering if it could be possible to dump that tone-mapped displayed version to a file.

With mpv --vo=gpu-next P5_DVtest.mkv --o=SDR.mkv all you get is the version before tone-mapping applied.

P5_DVtest.mkv is a Profile 5 DV sample (no HDR10 fallback).

Greetings.

Log file

output.txt

@haasn
Copy link
Member

haasn commented Mar 15, 2022

Hi. You should probably use the vf_libplacebo FFmpeg filter for this.

Pending documentation (tm). I'll put it on my TODO list.

@regystro
Copy link
Author

Thank you @haasn! Inspired by your answer and #9589, I came up with this command line that works:

ffmpeg -y -i P5_DVtest.mkv -init_hw_device vulkan -vf format=yuv420p,hwupload,libplacebo,hwdownload,format=yuv420p -vcodec h264_nvenc SDR.mkv

Awesome :)

@haasn
Copy link
Member

haasn commented Mar 15, 2022

Great :) (You probably want to use yuv420p10, not yuv420p, since this is a 10-bit file)

@regystro
Copy link
Author

Great :) (You probably want to use yuv420p10, not yuv420p, since this is a 10-bit file)

Thanks for the tip! Will do when using software encoding. Unfortunately right now dealing with an old not-10bit-encoding-capable-GPU, and need speed over quality ;)

@haasn
Copy link
Member

haasn commented Mar 15, 2022

Unfortunately right now dealing with an old not-10bit-encoding-capable-GPU, and need speed over quality ;)

What you are doing right now is software converting from 10-bit to 8-bit. If you want speed, you should do the conversion on GPU. I propose:

-vf hwupload,libplacebo=format=yuv420p,hwdownload,format=yuv420p

(This has the added bonus of working on all input files)

@haasn
Copy link
Member

haasn commented Mar 15, 2022

Also, if you want speed, you should directly derive the nvdec device from the vulkan device (or vice versa) and map the GPU frame directly to CUDA, instead of downloading to RAM and uploading back to nvdec.

@philipl
Copy link
Member

philipl commented Mar 15, 2022

Yeah. Add hwupload=derive_device=cuda to the end of the filter chain to replace the hwdownload

@regystro
Copy link
Author

Thank you so much to both of you. Those suggestions worked like a charm and transcoding speed increased from 1.52x to 1.94x which is a huge improvement.

@regystro
Copy link
Author

Ffmpeg also requires you to tag the 8 bit ycbcr correctly for it to be hdr. PQ at the very least + bt.2020 ncl and bt.2020 primaries. -colorspace bt2020nc -color_primaries bt2020  -color_trc smpte2084

Thanks, but what I meant was to map from [BT.2020, PQ] (HDR) to traditional [BT.709, BT.1886] (SDR).

@regystro
Copy link
Author

I've got a question about the outcoming SDR mkv file regarding mediainfo data:

Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : [email protected]
HDR format                               : Dolby Vision, Version 1.0, dvhe.05.06, BL+RPU
Format settings                          : CABAC / 2 Ref Frames
Format settings, CABAC                   : Yes
Format settings, Reference frames        : 2 frames
Codec ID                                 : V_MPEG4/ISO/AVC
Duration                                 : 3 s 24 ms
Bit rate mode                            : Variable
Maximum bit rate                         : 120 Mb/s
Width                                    : 1920 pixels
Height                                   : 1080 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Variable
Original frame rate                      : 23.976 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Writing library                          : Lavc59.18.100 h264_nvenc
Default                                  : No
Forced                                   : No
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709
  • I've managed to change all color specs to bt709, but cannot remove the HDR format: Dolby Vision, Version 1.0, dvhe.05.06, BL+RPU data. I thought apply_dolbyvision=0 would do the trick but it didn't, and Plex complains about unsupported color space :( This is my try:

hwupload,libplacebo=w=-2:h=1080:colorspace=bt709:color_primaries=bt709:color_trc=bt709:range=limited:apply_dolbyvision=0:format=yuv420p,hwupload=derive_device=cuda

Any ideas?

@haasn
Copy link
Member

haasn commented Mar 16, 2022

You want the opposite, I think. apply_dolbyvision=1 should strip it from the output frames.

@haasn
Copy link
Member

haasn commented Mar 16, 2022

Oh, it's possible that the metadata it's seeing is the dolby vision configuration record, not the actual RPU record?

@regystro
Copy link
Author

regystro commented Mar 16, 2022

but what I meant was to map from

That is too hard to do. You need zscale for this, etc.

Already accomplished with libplacebo. See #9982 (comment).

This is my current command:
ffmpeg -y -i P5_DVtest.mkv -map_metadata -1 -init_hw_device vulkan -vf hwupload,libplacebo=w=-2:h=1080:colorspace=bt709:color_primaries=bt709:color_trc=bt709:range=limited:format=yuv420p,hwupload=derive_device=cuda -c:v h264_nvenc -c:a aac SDR.mkv

You want the opposite, I think. apply_dolbyvision=1 should strip it from the output frames.

Same result :(

Oh, it's possible that the metadata it's seeing is the dolby vision configuration record, not the actual RPU record?

Sorry, I don't know how to check. Here you'll find the sample once tone-mapped (no audio): https://0x0.st/oZ7N.mkv

@regystro
Copy link
Author

Just -c copy without -strict unofficial.

Didn't work, but maybe I'm missing something:

  1. Download the sample from my previous post
  2. ffmpeg -i oZ7N.mkv -c copy out1.mkv
  3. ffmpeg -i oZ7N.mkv -c copy -strict unofficial out2.mkv (just in case)

Result: both mediainfo still show "HDR format: Dolby Vision, Version 1.0, dvhe.05.06, BL+RPU"

@regystro
Copy link
Author

To mp4 works, then you can just -c copy back to mkv.

All right. Or I could just transcode to mp4 in the first place, which also strips all DoVi record, and saves some steps. I was wondering if there could be a way to do it when transcoding to mkv.

@haasn
Copy link
Member

haasn commented Mar 16, 2022

Sorry, I don't know how to check. Here you'll find the sample once tone-mapped (no audio): https://0x0.st/oZ7N.mkv

I had a look at it. Yeah, the dovi metadata is intact in the stream metadata:

Input #0, matroska,webm, from '/mem/oZ7N.mkv':
  Metadata:
    ENCODER         : Lavf59.16.100
  Duration: 00:00:10.01, start: 0.000000, bitrate: 1730 kb/s
  Stream #0:0: Video: h264 (Main), yuv420p(tv, bt709, progressive), 2160x1080 [SAR 1:1 DAR 2:1], 23.98 fps, 23.98 tbr, 1k tbn (default)
    Metadata:
      ENCODER         : Lavc59.18.100 h264_nvenc
      DURATION        : 00:00:10.010000000
    Side data:
      DOVI configuration record: version: 1.0, profile: 5, level: 6, rpu flag: 1, el flag: 0, bl flag: 1, compatibility id: 0

Apparently there's no way to suppress writing this configuration record at the moment. I'll look into it (tm)

@acedogblast
Copy link

@haasn Sorry if this is not directly releated to this issue but I have also been trying to get Dolby Vision HDR to SDR tone-mapping to work on FFmpeg that is built with libplacebo and vulkan enabled. I keep getting this error [AVHWFramesContext @ 0x55c084bdcb40] There are no supported modifiers for the given sw_format
[Parsed_hwupload_0 @ 0x55c084bd5f40] Failed to configure output pad on Parsed_hwupload_0
My ffmpeg command look like this $FFMPEG -i "$INPUT" -map 0:v:0 -init_hw_device vulkan=gpu:0.0 -vf "hwupload,libplacebo=tonemapping=bt.2446a:colorspace=bt709:color_primaries=bt709:color_trc=bt709:range=limited:format=nv12,hwdownload,format=yuv420p" -c:v libx265 -crf 20 -preset:v slow -pix_fmt yuv420p10le "test.mkv"

@haasn
Copy link
Member

haasn commented Sep 14, 2022

@acedogblast file an FFmpeg issue, this is an error coming from the ffmpeg vulkan code

cc @cyanreg

@Brainiarc7
Copy link

I have the same issue as @acedogblast , which I reported on ffmpeg trac as https://trac.ffmpeg.org/ticket/10046

@rom4ster
Copy link

Hello I tried all of this stuff, waited for a long time, AND IT DID NOT RESOLVE my green video issue. Can you guys give any reason why?

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

No branches or pull requests

8 participants
@philipl @haasn @Brainiarc7 @Dudemanguy @regystro @rom4ster @acedogblast and others