Skip to content

Commit

Permalink
Add fix for YCbCr range on hardware transcoding
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Oct 10, 2023
1 parent c9c8e73 commit 50732c9
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/ffmpeg/hardware/hardware.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func MakeHardware(args *ffmpeg.Args, engine string, defaults map[string]string)
case EngineVAAPI:
args.Codecs[i] = defaults[name+"/"+engine]

fixYCbCrRange(args)

if !args.HasFilters("drawtext=") {
args.Input = "-hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch " + args.Input

Expand Down Expand Up @@ -87,6 +89,8 @@ func MakeHardware(args *ffmpeg.Args, engine string, defaults map[string]string)
case EngineCUDA:
args.Codecs[i] = defaults[name+"/"+engine]

fixYCbCrRange(args)

// CUDA doesn't support hardware transpose
// https://github.com/AlexxIT/go2rtc/issues/389
if !args.HasFilters("drawtext=", "transpose=") {
Expand All @@ -107,6 +111,8 @@ func MakeHardware(args *ffmpeg.Args, engine string, defaults map[string]string)
args.Input = "-hwaccel dxva2 -hwaccel_output_format dxva2_vld " + args.Input
args.Codecs[i] = defaults[name+"/"+engine]

fixYCbCrRange(args)

for i, filter := range args.Filters {
if strings.HasPrefix(filter, "scale=") {
args.Filters[i] = "scale_qsv=" + filter[6:]
Expand Down Expand Up @@ -154,3 +160,21 @@ func cut(s string, sep byte, pos int) string {
}
return s
}

// fixYCbCrRange convert jpeg/pc range to mpeg/tv range
// vaapi(pc, bt709, progressive) == yuvj420p (jpeg/full/pc)
// vaapi(tv, bt709, progressive) == yuv420p (mpeg/limited/tv)
// https://ffmpeg.org/ffmpeg-all.html#scale-1
func fixYCbCrRange(args *ffmpeg.Args) {
for i, filter := range args.Filters {
if strings.HasPrefix(filter, "scale=") {
if !strings.Contains(filter, "out_range=") {
args.Filters[i] = filter + ":out_range=tv"
}
return
}
}

// scale=out_color_matrix=bt709:out_range=tv
args.Filters = append(args.Filters, "scale=out_range=tv")
}

0 comments on commit 50732c9

Please sign in to comment.