mpv-player / mpv

🎥 Command line video player
https://mpv.io
Other
28.57k stars 2.92k forks source link

Q: Dolby Vision to SDR #9982

Closed regystro closed 1 year ago

regystro commented 2 years ago

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 commented 2 years ago

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

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

regystro commented 2 years ago

Thank you @haasn! Inspired by your answer and https://github.com/mpv-player/mpv/issues/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 commented 2 years ago

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

regystro commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

regystro commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : High@L5.1
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

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 commented 2 years ago

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

haasn commented 2 years ago

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

regystro commented 2 years ago

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 https://github.com/mpv-player/mpv/issues/9982#issuecomment-1067977248.

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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

cc @cyanreg

Brainiarc7 commented 1 year ago

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

rom4ster commented 8 months ago

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?