diff --git a/ffmpeg/ffmpeg.go b/ffmpeg/ffmpeg.go index e5c03a6685..2ab2912a0a 100755 --- a/ffmpeg/ffmpeg.go +++ b/ffmpeg/ffmpeg.go @@ -248,12 +248,15 @@ type MediaFormatInfo struct { Width, Height int } + +// h.264 only allows even resolutions, so both functions round up the results +// to the nearest even number func (f *MediaFormatInfo) ScaledHeight(width int) int { - return int(float32(width) * float32(f.Height) / float32(f.Width)) + return int(float32(width) * float32(f.Height) / float32(f.Width) + 1) & (^1) } func (f *MediaFormatInfo) ScaledWidth(height int) int { - return int(float32(height) * float32(f.Width) / float32(f.Height)) + return int(float32(height) * float32(f.Width) / float32(f.Height) + 1) & (^1) } func GetCodecInfo(fname string) (CodecStatus, MediaFormatInfo, error) { @@ -540,6 +543,11 @@ func (l *CodingSizeLimit) Clamp(p *VideoProfile, format MediaFormatInfo) error { if err != nil { return err } + // check if we _should_ clamp at all + if (l.WidthMin <= w) && (w <= l.WidthMax) && (l.HeightMin <= h) && (h <= l.HeightMax) { + // given video resolution is within encoder limits, no need for intervention + return nil + } // detect correct rotation outputAr := float32(w) / float32(h) inputAr := float32(format.Width) / float32(format.Height) @@ -569,7 +577,7 @@ func (l *CodingSizeLimit) Clamp(p *VideoProfile, format MediaFormatInfo) error { // 7th Gen NVENC limits: var nvidiaCodecSizeLimts = map[VideoCodec]CodingSizeLimit{ - H264: {146, 50, 4096, 4096}, + H264: {146, 145, 4096, 4096}, H265: {132, 40, 8192, 8192}, } diff --git a/ffmpeg/ffmpeg_test.go b/ffmpeg/ffmpeg_test.go index 098e81d15a..507f68a4e7 100644 --- a/ffmpeg/ffmpeg_test.go +++ b/ffmpeg/ffmpeg_test.go @@ -1855,7 +1855,7 @@ func TestResolution_Clamp(t *testing.T) { test(l, Size{1000, 500}, landscape, Size{700, 350}) test(l, Size{1000, 500}, portrait, Size{250, 500}) test(l, Size{500, 1000}, landscape, Size{700, 350}) - test(l, Size{600, 300}, portrait, Size{250, 500}) + test(l, Size{600, 300}, portrait, Size{600, 300}) test(l, Size{300, 600}, portrait, Size{250, 500}) // Test impossible limits for aspect ratio == 2