x264: use same order as x264CLI settings

This commit is contained in:
DataHoarder 2022-09-15 09:41:54 +02:00
parent 84908c0ef2
commit b60d7d1274
Signed by: DataHoarder
SSH key fingerprint: SHA256:EnPQOqPpbCa7nzalCEJY2sd9iPluFIBuJu2rDFalACI

View file

@ -25,7 +25,7 @@ type Encoder struct {
cleaned atomic.Bool
params C.x264_param_t
pictureIn *C.x264_picture_t
pictureOut *C.x264_picture_t
pictureOut C.x264_picture_t
h *C.x264_t
}
@ -36,9 +36,14 @@ func NewEncoder(w io.Writer, stream *y4m.Stream, settings map[string]any) (*Enco
preset := C.CString(getSettingString(settings, "preset", "medium"))
defer C.free(unsafe.Pointer(preset))
var tune *C.char
if strVal := getSettingString(settings, "tune", ""); strVal != "" {
tune = C.CString(strVal)
defer C.free(unsafe.Pointer(tune))
}
if C.x264_param_default_preset(&e.params, preset, nil) < 0 {
return nil, errors.New("error setting preset")
if C.x264_param_default_preset(&e.params, preset, tune) < 0 {
return nil, errors.New("error setting preset or tune")
}
defaultProfile := "high"
@ -84,10 +89,6 @@ func NewEncoder(w io.Writer, stream *y4m.Stream, settings map[string]any) (*Enco
e.params.vui.b_fullrange = 0
}
if C.x264_param_apply_profile(&e.params, profile) < 0 {
return nil, errors.New("error setting profile")
}
for k, v := range settings {
if err := func() error {
var strVal *C.char
@ -122,6 +123,15 @@ func NewEncoder(w io.Writer, stream *y4m.Stream, settings map[string]any) (*Enco
}
}
if e.params.rc.b_stat_read == 0 && e.params.rc.b_stat_write == 1 && C.GoString(preset) != "placebo" {
//doing first pass
C.x264_param_apply_fastfirstpass(&e.params)
}
if C.x264_param_apply_profile(&e.params, profile) < 0 {
return nil, errors.New("error setting profile")
}
runtime.SetFinalizer(e, func(encoder *Encoder) {
encoder.Close()
})
@ -129,9 +139,6 @@ func NewEncoder(w io.Writer, stream *y4m.Stream, settings map[string]any) (*Enco
if e.pictureIn = (*C.x264_picture_t)(C.malloc(C.size_t(unsafe.Sizeof(C.x264_picture_t{})))); e.pictureIn == nil {
return nil, errors.New("error allocating memory")
}
if e.pictureOut = (*C.x264_picture_t)(C.malloc(C.size_t(unsafe.Sizeof(C.x264_picture_t{})))); e.pictureOut == nil {
return nil, errors.New("error allocating memory")
}
if C.x264_picture_alloc(e.pictureIn, e.params.i_csp, e.params.i_width, e.params.i_height) < 0 {
return nil, errors.New("error allocating input picture")
}
@ -170,7 +177,7 @@ func (e *Encoder) Encode(pts int64, f frame.Frame) error {
e.pictureIn.i_pts = C.int64_t(pts)
if frame_size = C.x264_encoder_encode(e.h, &nal, &i_nal, e.pictureIn, e.pictureOut); frame_size < 0 {
if frame_size = C.x264_encoder_encode(e.h, &nal, &i_nal, e.pictureIn, &e.pictureOut); frame_size < 0 {
return errors.New("error encoding frame")
}
@ -178,6 +185,10 @@ func (e *Encoder) Encode(pts int64, f frame.Frame) error {
return nil
}
if i_nal != 1 {
//return errors.New("more than one NAL present")
}
buf := unsafe.Slice((*byte)(nal.p_payload), int(frame_size))
if _, err := e.w.Write(buf); err != nil {
return err
@ -192,10 +203,18 @@ func (e *Encoder) Flush() error {
var frame_size C.int
for C.x264_encoder_delayed_frames(e.h) > 0 {
if frame_size = C.x264_encoder_encode(e.h, &nal, &i_nal, nil, e.pictureOut); frame_size <= 0 {
if frame_size = C.x264_encoder_encode(e.h, &nal, &i_nal, nil, &e.pictureOut); frame_size < 0 {
return errors.New("error encoding frame")
}
if frame_size == 0 {
return nil
}
if i_nal != 1 {
//return errors.New("more than one NAL present")
}
buf := unsafe.Slice((*byte)(nal.p_payload), int(frame_size))
if _, err := e.w.Write(buf); err != nil {
return err
@ -217,10 +236,6 @@ func (e *Encoder) Close() {
C.free(unsafe.Pointer(e.pictureIn))
e.pictureIn = nil
}
if e.pictureOut != nil {
C.free(unsafe.Pointer(e.pictureOut))
e.pictureOut = nil
}
}
}
@ -238,7 +253,3 @@ func getSettingString(m map[string]any, name string, fallback string) string {
}
return fallback
}
func getSettingInt(m map[string]any, name string, fallback int) int {
return fallback
}