update audio.go
This commit is contained in:
parent
3b9c06f2e1
commit
8e14c48c2a
39
audio.go
39
audio.go
@ -10,15 +10,24 @@ import (
|
|||||||
void set_sample_fmt(AVCodecContext *ctx, int sample_fmt) {
|
void set_sample_fmt(AVCodecContext *ctx, int sample_fmt) {
|
||||||
ctx->sample_fmt = sample_fmt;
|
ctx->sample_fmt = sample_fmt;
|
||||||
}
|
}
|
||||||
|
int wrap_av_get_bytes_per_sample(int sample_fmt) {
|
||||||
|
return av_get_bytes_per_sample(sample_fmt);
|
||||||
|
}
|
||||||
*/
|
*/
|
||||||
"C"
|
"C"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SampleFormat int
|
||||||
|
|
||||||
|
func (self SampleFormat) BytesPerSample() int {
|
||||||
|
return int(C.wrap_av_get_bytes_per_sample(C.int(self)))
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
S16 = C.AV_SAMPLE_FMT_S16
|
S16 = SampleFormat(C.AV_SAMPLE_FMT_S16)
|
||||||
FLTP = C.AV_SAMPLE_FMT_FLTP
|
FLTP = SampleFormat(C.AV_SAMPLE_FMT_FLTP)
|
||||||
)
|
)
|
||||||
|
|
||||||
type AudioEncoder struct {
|
type AudioEncoder struct {
|
||||||
@ -26,8 +35,8 @@ type AudioEncoder struct {
|
|||||||
SampleRate int
|
SampleRate int
|
||||||
BitRate int
|
BitRate int
|
||||||
ChannelCount int
|
ChannelCount int
|
||||||
SampleFormat int
|
SampleFormat SampleFormat
|
||||||
sampleSize int
|
FrameSampleCount int
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *AudioEncoder) Setup() (err error) {
|
func (self *AudioEncoder) Setup() (err error) {
|
||||||
@ -46,28 +55,26 @@ func (self *AudioEncoder) Setup() (err error) {
|
|||||||
err = fmt.Errorf("avcodec_open2 failed")
|
err = fmt.Errorf("avcodec_open2 failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
self.SampleFormat = SampleFormat(int(ff.codecCtx.sample_fmt))
|
||||||
|
self.ChannelCount = int(ff.codecCtx.channels)
|
||||||
|
self.FrameSampleCount = int(ff.codecCtx.frame_size)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *AudioEncoder) Extradata() (data []byte) {
|
func (self *AudioEncoder) Extradata() (data []byte) {
|
||||||
data = make([]byte, (int)(self.ff.codecCtx.extradata_size))
|
data = C.GoBytes(unsafe.Pointer(self.ff.codecCtx.extradata), self.ff.codecCtx.extradata_size)
|
||||||
C.memcpy(
|
|
||||||
unsafe.Pointer(&data[0]),
|
|
||||||
unsafe.Pointer(self.ff.codecCtx.extradata),
|
|
||||||
(C.size_t)(len(data)),
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *AudioEncoder) Encode(sample []byte, flush bool) (gotPkt bool, pkt []byte, err error) {
|
func (self *AudioEncoder) Encode(sample []byte, flush bool) (gotPkt bool, pkt []byte, err error) {
|
||||||
ff := &self.ff
|
ff := &self.ff
|
||||||
nbSamples := int(ff.codecCtx.frame_size)
|
nbSamples := self.FrameSampleCount
|
||||||
channelCount := int(ff.codecCtx.channels)
|
channelCount := int(ff.codecCtx.channels)
|
||||||
sampleSize := int(C.av_get_bytes_per_sample(ff.codecCtx.sample_fmt))
|
sampleSize := int(C.av_get_bytes_per_sample(ff.codecCtx.sample_fmt))
|
||||||
expectedSize := nbSamples*sampleSize*channelCount
|
expectedSize := nbSamples*sampleSize*channelCount
|
||||||
|
|
||||||
frame := self.ff.frame
|
frame := ff.frame
|
||||||
if flush {
|
if flush {
|
||||||
frame = nil
|
frame = nil
|
||||||
} else {
|
} else {
|
||||||
@ -77,6 +84,8 @@ func (self *AudioEncoder) Encode(sample []byte, flush bool) (gotPkt bool, pkt []
|
|||||||
}
|
}
|
||||||
|
|
||||||
frame.nb_samples = C.int(nbSamples)
|
frame.nb_samples = C.int(nbSamples)
|
||||||
|
frame.format = C.int(ff.codecCtx.sample_fmt)
|
||||||
|
frame.channel_layout = ff.codecCtx.channel_layout
|
||||||
if C.av_sample_fmt_is_planar(ff.codecCtx.sample_fmt) != 0 {
|
if C.av_sample_fmt_is_planar(ff.codecCtx.sample_fmt) != 0 {
|
||||||
for i := 0; i < self.ChannelCount; i++ {
|
for i := 0; i < self.ChannelCount; i++ {
|
||||||
frame.data[i] = (*C.uint8_t)(unsafe.Pointer(&sample[i*nbSamples*sampleSize]))
|
frame.data[i] = (*C.uint8_t)(unsafe.Pointer(&sample[i*nbSamples*sampleSize]))
|
||||||
@ -84,7 +93,7 @@ func (self *AudioEncoder) Encode(sample []byte, flush bool) (gotPkt bool, pkt []
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
frame.data[0] = (*C.uint8_t)(unsafe.Pointer(&sample[0]))
|
frame.data[0] = (*C.uint8_t)(unsafe.Pointer(&sample[0]))
|
||||||
frame.linesize[0] = C.int(channelCount*nbSamples*self.sampleSize)
|
frame.linesize[0] = C.int(channelCount*nbSamples*sampleSize)
|
||||||
}
|
}
|
||||||
//frame.extended_data = &frame.data[0]
|
//frame.extended_data = &frame.data[0]
|
||||||
}
|
}
|
||||||
@ -109,7 +118,7 @@ func (self *AudioEncoder) Encode(sample []byte, flush bool) (gotPkt bool, pkt []
|
|||||||
type AudioDecoder struct {
|
type AudioDecoder struct {
|
||||||
ff C.FFCtx
|
ff C.FFCtx
|
||||||
ChannelCount int
|
ChannelCount int
|
||||||
SampleFormat int
|
SampleFormat SampleFormat
|
||||||
Extradata []byte
|
Extradata []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,6 +137,8 @@ func (self *AudioDecoder) Setup() (err error) {
|
|||||||
err = fmt.Errorf("avcodec_open2 failed")
|
err = fmt.Errorf("avcodec_open2 failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
self.SampleFormat = SampleFormat(int(ff.codecCtx.sample_fmt))
|
||||||
|
self.ChannelCount = int(ff.codecCtx.channels)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user