Removed pointer on ChannelLayout + now cloning channel layouts in getters

This commit is contained in:
Quentin Renard 2023-09-28 15:21:32 +02:00
parent 63a083bcad
commit 7048b09f25
6 changed files with 32 additions and 21 deletions

View File

@ -75,15 +75,15 @@ type ChannelLayout struct {
c *C.struct_AVChannelLayout c *C.struct_AVChannelLayout
} }
func newChannelLayoutFromC(c *C.struct_AVChannelLayout) *ChannelLayout { func newChannelLayoutFromC(c *C.struct_AVChannelLayout) ChannelLayout {
return &ChannelLayout{c: c} return ChannelLayout{c: c}
} }
func (l *ChannelLayout) NbChannels() int { func (l ChannelLayout) NbChannels() int {
return int(l.c.nb_channels) return int(l.c.nb_channels)
} }
func (l *ChannelLayout) String() string { func (l ChannelLayout) String() string {
b := make([]byte, 1024) b := make([]byte, 1024)
n, err := l.Describe(b) n, err := l.Describe(b)
if err != nil { if err != nil {
@ -92,7 +92,7 @@ func (l *ChannelLayout) String() string {
return string(b[:n]) return string(b[:n])
} }
func (l *ChannelLayout) Describe(b []byte) (int, error) { func (l ChannelLayout) Describe(b []byte) (int, error) {
ret := C.av_channel_layout_describe(l.c, (*C.char)(unsafe.Pointer(&b[0])), cUlong(len(b))) ret := C.av_channel_layout_describe(l.c, (*C.char)(unsafe.Pointer(&b[0])), cUlong(len(b)))
if ret < 0 { if ret < 0 {
return 0, newError(ret) return 0, newError(ret)
@ -100,11 +100,11 @@ func (l *ChannelLayout) Describe(b []byte) (int, error) {
return int(ret), nil return int(ret), nil
} }
func (l *ChannelLayout) Valid() bool { func (l ChannelLayout) Valid() bool {
return C.av_channel_layout_check(l.c) > 0 return C.av_channel_layout_check(l.c) > 0
} }
func (l *ChannelLayout) Compare(l2 *ChannelLayout) (equal bool, err error) { func (l ChannelLayout) Compare(l2 ChannelLayout) (equal bool, err error) {
ret := C.av_channel_layout_compare(l.c, l2.c) ret := C.av_channel_layout_compare(l.c, l2.c)
if ret < 0 { if ret < 0 {
return false, newError(ret) return false, newError(ret)
@ -112,11 +112,19 @@ func (l *ChannelLayout) Compare(l2 *ChannelLayout) (equal bool, err error) {
return ret == 0, nil return ret == 0, nil
} }
func (l *ChannelLayout) Equal(l2 *ChannelLayout) bool { func (l ChannelLayout) Equal(l2 ChannelLayout) bool {
v, _ := l.Compare(l2) v, _ := l.Compare(l2)
return v return v
} }
func (l *ChannelLayout) copy(dst *C.struct_AVChannelLayout) error { func (l ChannelLayout) copy(dst *C.struct_AVChannelLayout) error {
return newError(C.av_channel_layout_copy(dst, l.c)) return newError(C.av_channel_layout_copy(dst, l.c))
} }
func (l ChannelLayout) clone() (ChannelLayout, error) {
// TODO Should it be freed?
cl := C.struct_AVChannelLayout{}
err := l.copy(&cl)
dst := newChannelLayoutFromC(&cl)
return dst, err
}

View File

@ -28,13 +28,13 @@ func (c *Codec) String() string {
return c.Name() return c.Name()
} }
func (c *Codec) ChannelLayouts() (o []*ChannelLayout) { func (c *Codec) ChannelLayouts() (o []ChannelLayout) {
if c.c.ch_layouts == nil { if c.c.ch_layouts == nil {
return nil return nil
} }
size := unsafe.Sizeof(*c.c.ch_layouts) size := unsafe.Sizeof(*c.c.ch_layouts)
for i := 0; ; i++ { for i := 0; ; i++ {
v := newChannelLayoutFromC((*C.struct_AVChannelLayout)(unsafe.Pointer(uintptr(unsafe.Pointer(c.c.ch_layouts)) + uintptr(i)*size))) v, _ := newChannelLayoutFromC((*C.struct_AVChannelLayout)(unsafe.Pointer(uintptr(unsafe.Pointer(c.c.ch_layouts)) + uintptr(i)*size))).clone()
if !v.Valid() { if !v.Valid() {
break break
} }

View File

@ -53,11 +53,12 @@ func (cc *CodecContext) SetChannels(channels int) {
cc.c.channels = C.int(channels) cc.c.channels = C.int(channels)
} }
func (cc *CodecContext) ChannelLayout() *ChannelLayout { func (cc *CodecContext) ChannelLayout() ChannelLayout {
return newChannelLayoutFromC(&cc.c.ch_layout) l, _ := newChannelLayoutFromC(&cc.c.ch_layout).clone()
return l
} }
func (cc *CodecContext) SetChannelLayout(l *ChannelLayout) { func (cc *CodecContext) SetChannelLayout(l ChannelLayout) {
l.copy(&cc.c.ch_layout) //nolint: errcheck l.copy(&cc.c.ch_layout) //nolint: errcheck
} }

View File

@ -28,11 +28,12 @@ func (cp *CodecParameters) BitRate() int64 {
return int64(cp.c.bit_rate) return int64(cp.c.bit_rate)
} }
func (cp *CodecParameters) ChannelLayout() *ChannelLayout { func (cp *CodecParameters) ChannelLayout() ChannelLayout {
return newChannelLayoutFromC(&cp.c.ch_layout) l, _ := newChannelLayoutFromC(&cp.c.ch_layout).clone()
return l
} }
func (cp *CodecParameters) SetChannelLayout(l *ChannelLayout) { func (cp *CodecParameters) SetChannelLayout(l ChannelLayout) {
l.copy(&cp.c.ch_layout) //nolint: errcheck l.copy(&cp.c.ch_layout) //nolint: errcheck
} }

View File

@ -20,7 +20,7 @@ func TestCodec(t *testing.T) {
c = astiav.FindDecoderByName("aac") c = astiav.FindDecoderByName("aac")
require.NotNil(t, c) require.NotNil(t, c)
els := []*astiav.ChannelLayout{ els := []astiav.ChannelLayout{
astiav.ChannelLayoutMono, astiav.ChannelLayoutMono,
astiav.ChannelLayoutStereo, astiav.ChannelLayoutStereo,
astiav.ChannelLayoutSurround, astiav.ChannelLayoutSurround,

View File

@ -38,11 +38,12 @@ func (f *Frame) AllocSamples(align int) error {
return newError(C.av_samples_alloc(&f.c.data[0], &f.c.linesize[0], f.c.ch_layout.nb_channels, f.c.nb_samples, (C.enum_AVSampleFormat)(f.c.format), C.int(align))) return newError(C.av_samples_alloc(&f.c.data[0], &f.c.linesize[0], f.c.ch_layout.nb_channels, f.c.nb_samples, (C.enum_AVSampleFormat)(f.c.format), C.int(align)))
} }
func (f *Frame) ChannelLayout() *ChannelLayout { func (f *Frame) ChannelLayout() ChannelLayout {
return newChannelLayoutFromC(&f.c.ch_layout) l, _ := newChannelLayoutFromC(&f.c.ch_layout).clone()
return l
} }
func (f *Frame) SetChannelLayout(l *ChannelLayout) { func (f *Frame) SetChannelLayout(l ChannelLayout) {
l.copy(&f.c.ch_layout) //nolint: errcheck l.copy(&f.c.ch_layout) //nolint: errcheck
} }