Limit max number of threads as per libaom req

Fix #7
This commit is contained in:
Kagami Hiiragi 2019-08-29 10:42:14 +03:00
parent 6487e4d17d
commit e54663f722
1 changed files with 11 additions and 6 deletions

17
avif.go
View File

@ -17,17 +17,19 @@ import (
// Encoder constants. // Encoder constants.
const ( const (
MinThreads = 1
MaxThreads = 64
MinSpeed = 0 MinSpeed = 0
MaxSpeed = 8 MaxSpeed = 8
MinQuality = 0 MinQuality = 0
MaxQuality = 63 MaxQuality = 63
) )
// Options are the encoding parameters. Threads ranges from 1, 0 means // Options are the encoding parameters. Threads ranges from MinThreads
// use all available cores. Speed ranges from MinSpeed to MaxSpeed. // to MaxThreads, 0 means use all available cores. Speed ranges from
// Quality ranges from MinQuality to MaxQuality, lower is better, 0 // MinSpeed to MaxSpeed. Quality ranges from MinQuality to MaxQuality,
// means lossless encoding. SubsampleRatio specifies subsampling of the // lower is better, 0 means lossless encoding. SubsampleRatio specifies
// encoded image, nil means 4:2:0. // subsampling of the encoded image, nil means 4:2:0.
type Options struct { type Options struct {
Threads int Threads int
Speed int Speed int
@ -113,6 +115,9 @@ func Encode(w io.Writer, m image.Image, o *Options) error {
} }
if o.Threads == 0 { if o.Threads == 0 {
o.Threads = runtime.NumCPU() o.Threads = runtime.NumCPU()
if o.Threads > MaxThreads {
o.Threads = MaxThreads
}
} }
if o.SubsampleRatio == nil { if o.SubsampleRatio == nil {
s := image.YCbCrSubsampleRatio420 s := image.YCbCrSubsampleRatio420
@ -121,7 +126,7 @@ func Encode(w io.Writer, m image.Image, o *Options) error {
// o.SubsampleRatio = &yuvImg.SubsampleRatio // o.SubsampleRatio = &yuvImg.SubsampleRatio
// } // }
} }
if o.Threads < 1 { if o.Threads < MinThreads || o.Threads > MaxThreads {
return OptionsError("bad threads number") return OptionsError("bad threads number")
} }
if o.Speed < MinSpeed || o.Speed > MaxSpeed { if o.Speed < MinSpeed || o.Speed > MaxSpeed {