Skip to content

Commit

Permalink
Removed pointer on ChannelLayout + now cloning channel layouts in get…
Browse files Browse the repository at this point in the history
…ters
  • Loading branch information
asticode committed Sep 28, 2023
1 parent 63a083b commit 7048b09
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
26 changes: 17 additions & 9 deletions channel_layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ type ChannelLayout struct {
c *C.struct_AVChannelLayout
}

func newChannelLayoutFromC(c *C.struct_AVChannelLayout) *ChannelLayout {
return &ChannelLayout{c: c}
func newChannelLayoutFromC(c *C.struct_AVChannelLayout) ChannelLayout {
return ChannelLayout{c: c}
}

func (l *ChannelLayout) NbChannels() int {
func (l ChannelLayout) NbChannels() int {
return int(l.c.nb_channels)
}

func (l *ChannelLayout) String() string {
func (l ChannelLayout) String() string {
b := make([]byte, 1024)
n, err := l.Describe(b)
if err != nil {
Expand All @@ -92,31 +92,39 @@ func (l *ChannelLayout) String() string {
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)))
if ret < 0 {
return 0, newError(ret)
}
return int(ret), nil
}

func (l *ChannelLayout) Valid() bool {
func (l ChannelLayout) Valid() bool {
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)
if ret < 0 {
return false, newError(ret)
}
return ret == 0, nil
}

func (l *ChannelLayout) Equal(l2 *ChannelLayout) bool {
func (l ChannelLayout) Equal(l2 ChannelLayout) bool {
v, _ := l.Compare(l2)
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))
}

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
}
4 changes: 2 additions & 2 deletions codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func (c *Codec) String() string {
return c.Name()
}

func (c *Codec) ChannelLayouts() (o []*ChannelLayout) {
func (c *Codec) ChannelLayouts() (o []ChannelLayout) {
if c.c.ch_layouts == nil {
return nil
}
size := unsafe.Sizeof(*c.c.ch_layouts)
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() {
break
}
Expand Down
7 changes: 4 additions & 3 deletions codec_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ func (cc *CodecContext) SetChannels(channels int) {
cc.c.channels = C.int(channels)
}

func (cc *CodecContext) ChannelLayout() *ChannelLayout {
return newChannelLayoutFromC(&cc.c.ch_layout)
func (cc *CodecContext) ChannelLayout() ChannelLayout {
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
}

Expand Down
7 changes: 4 additions & 3 deletions codec_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ func (cp *CodecParameters) BitRate() int64 {
return int64(cp.c.bit_rate)
}

func (cp *CodecParameters) ChannelLayout() *ChannelLayout {
return newChannelLayoutFromC(&cp.c.ch_layout)
func (cp *CodecParameters) ChannelLayout() ChannelLayout {
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
}

Expand Down
2 changes: 1 addition & 1 deletion codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestCodec(t *testing.T) {

c = astiav.FindDecoderByName("aac")
require.NotNil(t, c)
els := []*astiav.ChannelLayout{
els := []astiav.ChannelLayout{
astiav.ChannelLayoutMono,
astiav.ChannelLayoutStereo,
astiav.ChannelLayoutSurround,
Expand Down
7 changes: 4 additions & 3 deletions frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
}

func (f *Frame) ChannelLayout() *ChannelLayout {
return newChannelLayoutFromC(&f.c.ch_layout)
func (f *Frame) ChannelLayout() ChannelLayout {
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
}

Expand Down

0 comments on commit 7048b09

Please sign in to comment.