Skip to content

Commit

Permalink
packet: Add memory management *Packet functions
Browse files Browse the repository at this point in the history
* Function '(*Packet).CopyProperties' is a wrapper for av_packet_copy_props
  It copes field values from one Packet to another.
  See: https://ffmpeg.org/doxygen/7.0/group__lavc__packet.html#gade00f67930f4e2a3401b67b701d5b3a2

* Function '(*Packet).MakeDateReferenceCounted' is a wrapper for av_packet_make_refcounted
  It makes sure the packet data is reference counted (in contrast to duplicated)
  See: https://ffmpeg.org/doxygen/7.0/group__lavc__packet.html#ga8a6deff6c1809029037ffd760db3e0d4

* Function '(*Packet).MakeDataWritable' is a wrapper for av_packet_make_writable
  It makes sure the packet data is writable (in contrast to reference counted)
  See: https://ffmpeg.org/doxygen/7.0/group__lavc__packet.html#gaaa304ffdab83984ac995d134e4298d4b
  • Loading branch information
xaionaro committed Jan 13, 2025
1 parent a6280c7 commit 575c838
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package astiav

// #include <libavutil/buffer.h>
import "C"

func isBufferWritable(buf *C.AVBufferRef) bool {
return C.av_buffer_is_writable(buf) != 0
}
16 changes: 16 additions & 0 deletions packet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package astiav

//#include <libavcodec/avcodec.h>
//#include <libavcodec/packet.h>
import "C"
import (
"errors"
Expand Down Expand Up @@ -120,6 +121,21 @@ func (p *Packet) Clone() *Packet {
return newPacketFromC(C.av_packet_clone(p.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavc__packet.html#gade00f67930f4e2a3401b67b701d5b3a2
func (p *Packet) CopyProperties(src *Packet) error {
return newError(C.av_packet_copy_props(p.c, src.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavc__packet.html#ga8a6deff6c1809029037ffd760db3e0d4
func (p *Packet) MakeDataReferenceCounted() error {
return newError(C.av_packet_make_refcounted(p.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavc__packet.html#gaaa304ffdab83984ac995d134e4298d4b
func (p *Packet) MakeDataWritable() error {
return newError(C.av_packet_make_writable(p.c))
}

// https://ffmpeg.org/doxygen/7.0/group__lavc__packet.html#gadfa708660b85a56749c753124de2da7d
func (p *Packet) AllocPayload(s int) error {
return newError(C.av_new_packet(p.c, C.int(s)))
Expand Down
28 changes: 28 additions & 0 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,32 @@ func TestPacket(t *testing.T) {
b = []byte{}
require.NoError(t, pkt6.FromData(b))
require.Equal(t, b, pkt6.Data())

t.Run("CopyProperties", func(t *testing.T) {
pktCopy := AllocPacket()
require.NotNil(t, pktCopy)
defer pktCopy.Free()

require.NotEqual(t, pkt2.c.dts, pktCopy.c.dts)
err := pktCopy.CopyProperties(pkt2)
require.NoError(t, err)
// according to the documentation, all fields but "buf", "data", "size" should
// be copied, so let's check for example dts:
require.Equal(t, pkt2.c.dts, pktCopy.c.dts)
})

t.Run("MakeDataWritable", func(t *testing.T) {
pktClone := pkt2.Clone()
defer pktClone.Free()
err = pktClone.MakeDataWritable()
require.NoError(t, err)
require.True(t, isBufferWritable(pktClone.c.buf))
})

t.Run("MakeDataReferenceCounted", func(t *testing.T) {
pktClone := pkt2.Clone()
defer pktClone.Free()
err = pktClone.MakeDataReferenceCounted()
require.NoError(t, err)
})
}

0 comments on commit 575c838

Please sign in to comment.