mirror of
https://github.com/asticode/go-astiav.git
synced 2025-10-05 00:02:45 +08:00
Added FindPixelFormatByName + frame.AllocSamples
This commit is contained in:
1
bytes.go
1
bytes.go
@@ -1,6 +1,7 @@
|
||||
package astiav
|
||||
|
||||
//#include <stdlib.h>
|
||||
//#include <stdint.h>
|
||||
import "C"
|
||||
import (
|
||||
"errors"
|
||||
|
@@ -58,7 +58,15 @@ func (g *FilterGraph) NewFilterContext(f *Filter, name string, args FilterArgs)
|
||||
func (g *FilterGraph) Parse(content string, inputs, outputs *FilterInOut) error {
|
||||
cc := C.CString(content)
|
||||
defer C.free(unsafe.Pointer(cc))
|
||||
return newError(C.avfilter_graph_parse_ptr(g.c, cc, &inputs.c, &outputs.c, nil))
|
||||
var ic **C.struct_AVFilterInOut
|
||||
if inputs != nil {
|
||||
ic = &inputs.c
|
||||
}
|
||||
var oc **C.struct_AVFilterInOut
|
||||
if outputs != nil {
|
||||
oc = &outputs.c
|
||||
}
|
||||
return newError(C.avfilter_graph_parse_ptr(g.c, cc, ic, oc, nil))
|
||||
}
|
||||
|
||||
func (g *FilterGraph) Configure() error {
|
||||
|
12
frame.go
12
frame.go
@@ -2,6 +2,7 @@ package astiav
|
||||
|
||||
//#cgo pkg-config: libavutil
|
||||
//#include <libavutil/frame.h>
|
||||
//#include <libavutil/samplefmt.h>
|
||||
import "C"
|
||||
|
||||
const NumDataPointers = uint(C.AV_NUM_DATA_POINTERS)
|
||||
@@ -26,6 +27,10 @@ func (f *Frame) AllocBuffer(align int) error {
|
||||
return newError(C.av_frame_get_buffer(f.c, C.int(align)))
|
||||
}
|
||||
|
||||
func (f *Frame) AllocSamples(sf SampleFormat, nbChannels, nbSamples, align int) error {
|
||||
return newError(C.av_samples_alloc(&f.c.data[0], &f.c.linesize[0], C.int(nbChannels), C.int(nbSamples), (C.enum_AVSampleFormat)(sf), C.int(align)))
|
||||
}
|
||||
|
||||
func (f *Frame) ChannelLayout() ChannelLayout {
|
||||
return ChannelLayout(f.c.channel_layout)
|
||||
}
|
||||
@@ -38,7 +43,12 @@ func (f *Frame) Data() [NumDataPointers][]byte {
|
||||
b := [NumDataPointers][]byte{}
|
||||
for i := 0; i < int(NumDataPointers); i++ {
|
||||
b[i] = bytesFromC(func(size *C.int) *C.uint8_t {
|
||||
*size = f.c.linesize[i] * f.c.height
|
||||
*size = f.c.linesize[i]
|
||||
if f.c.height > 0 {
|
||||
*size = *size * f.c.height
|
||||
} else if f.c.channels > 0 {
|
||||
*size = *size * f.c.channels
|
||||
}
|
||||
return f.c.data[i]
|
||||
})
|
||||
}
|
||||
|
@@ -175,4 +175,16 @@ func TestFrame(t *testing.T) {
|
||||
|
||||
f3.Unref()
|
||||
require.Equal(t, 0, f3.Height())
|
||||
|
||||
f4 := astiav.AllocFrame()
|
||||
require.NotNil(t, f4)
|
||||
defer f4.Free()
|
||||
f4.SetNbSamples(960)
|
||||
f4.SetChannelLayout(astiav.ChannelLayoutStereo)
|
||||
f4.SetSampleFormat(astiav.SampleFormatS16)
|
||||
f4.SetSampleRate(48000)
|
||||
err = f4.AllocBuffer(0)
|
||||
require.NoError(t, err)
|
||||
err = f4.AllocSamples(astiav.SampleFormatS16, 2, 960, 0)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ package astiav
|
||||
//#include <libavutil/pixdesc.h>
|
||||
//#include <libavutil/pixfmt.h>
|
||||
import "C"
|
||||
import "unsafe"
|
||||
|
||||
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavutil/pixfmt.h#L64
|
||||
type PixelFormat C.enum_AVPixelFormat
|
||||
@@ -210,3 +211,9 @@ func (f PixelFormat) Name() string {
|
||||
func (f PixelFormat) String() string {
|
||||
return f.Name()
|
||||
}
|
||||
|
||||
func FindPixelFormatByName(name string) PixelFormat {
|
||||
cn := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cn))
|
||||
return PixelFormat(C.av_get_pix_fmt(cn))
|
||||
}
|
||||
|
@@ -8,5 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func TestPixelFormat(t *testing.T) {
|
||||
require.Equal(t, "yuv420p", astiav.PixelFormatYuv420P.String())
|
||||
p := astiav.FindPixelFormatByName("yuv420p")
|
||||
require.Equal(t, astiav.PixelFormatYuv420P, p)
|
||||
require.Equal(t, "yuv420p", p.String())
|
||||
}
|
||||
|
Reference in New Issue
Block a user