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

Scaling bug fix (issue 353) #354

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions ffmpeg/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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},
}

Expand Down
2 changes: 1 addition & 1 deletion ffmpeg/ffmpeg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down