Skip to content

Commit 5af5cc1

Browse files
authored
Merge pull request #34 from mutablelogic/ffmpeg61
Fixed audio resampling issues
2 parents 08903f1 + a30a366 commit 5af5cc1

File tree

4 files changed

+43
-15
lines changed

4 files changed

+43
-15
lines changed

pkg/ffmpeg/decoder.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,17 @@ func (d *Decoder) decode(packet *ff.AVPacket, fn DecoderFrameFn) error {
155155
dest = (*Frame)(d.frame)
156156
}
157157

158+
// If we have a nil frame here, then don't pass back to the caller
159+
if dest == nil {
160+
ff.AVUtil_frame_unref(d.frame)
161+
continue
162+
}
163+
158164
// Copy across the timebase and pts
159-
// TODO if dest != nil {
165+
// TODO
160166
// fmt.Println("pts=", d.frame.Pts())
161-
// (*ff.AVFrame)(dest).SetPts(d.frame.Pts())
162-
// (*ff.AVFrame)(dest).SetTimeBase(d.timeBase)
163-
//}
167+
// (*ff.AVFrame)(dest).SetPts(d.frame.Pts())
168+
// (*ff.AVFrame)(dest).SetTimeBase(d.timeBase)
164169

165170
// Pass back to the caller
166171
if err := fn(d.stream, dest); errors.Is(err, io.EOF) {

pkg/ffmpeg/resampler.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (r *resampler) Frame(src *Frame) (*Frame, error) {
128128
}
129129

130130
// Perform resampling
131-
if err := ff.SWResample_convert_frame(r.ctx, (*ff.AVFrame)(src), (*ff.AVFrame)(r.dest)); err != nil {
131+
if err := swrConvertFrame(r.ctx, (*ff.AVFrame)(src), (*ff.AVFrame)(r.dest)); err != nil {
132132
return nil, fmt.Errorf("SWResample_convert_frame: %w", err)
133133
} else if r.dest.NumSamples() == 0 {
134134
return nil, nil
@@ -146,6 +146,18 @@ func (r *resampler) Frame(src *Frame) (*Frame, error) {
146146
////////////////////////////////////////////////////////////////////////////////
147147
// PRIVATE METHODS
148148

149+
//func swrConvertFrame_(ctx *ff.SWRContext, src, dest *ff.AVFrame) error {
150+
// return ff.SWResample_convert_frame(ctx, src, dest)
151+
//}
152+
153+
func swrConvertFrame(ctx *ff.SWRContext, src, dest *ff.AVFrame) error {
154+
_, err := ff.SWResample_convert(ctx, ff.AVUtil_samples_frame(dest), ff.AVUtil_samples_frame(src))
155+
if err != nil {
156+
return err
157+
}
158+
return nil
159+
}
160+
149161
func newResampler(dest, src *Frame) (*ff.SWRContext, error) {
150162
// Create a new resampler
151163
ctx := ff.SWResample_alloc()

sys/ffmpeg61/avutil_samples.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
import "C"
1717

1818
const (
19-
AV_NUM_PLANES = 48
19+
AV_NUM_PLANES = 8
2020
)
2121

2222
type AVSamples struct {
@@ -83,6 +83,18 @@ func (data *AVSamples) NumSamples() int {
8383
////////////////////////////////////////////////////////////////////////////////
8484
// BINDINGS
8585

86+
// Get AVSamples from an AVFrame
87+
func AVUtil_samples_frame(frame *AVFrame) *AVSamples {
88+
return &AVSamples{
89+
nb_samples: frame.nb_samples,
90+
nb_channels: frame.channels,
91+
sample_fmt: C.enum_AVSampleFormat(frame.format),
92+
plane_size: frame.linesize[0],
93+
buffer_size: frame.linesize[0] * frame.nb_samples,
94+
planes: frame.data,
95+
}
96+
}
97+
8698
// Allocate a samples buffer for nb_samples samples. Return allocated data for each plane, and the stride.
8799
func AVUtil_samples_alloc(nb_samples, nb_channels int, sample_fmt AVSampleFormat, align bool) (*AVSamples, error) {
88100
if nb_channels < 1 {

sys/ffmpeg61/swresample_convert.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ import "C"
1313
// PUBLIC METHODS
1414

1515
// Core conversion function. Returns number of samples output per channel.
16-
// in and in_count can be set to 0 to flush the last few samples out at the end.
17-
func SWResample_convert(ctx *SWRContext, dst *AVSamples, dst_nb_samples int, src *AVSamples, src_nb_samples int) (int, error) {
18-
n := int(C.swr_convert(
19-
(*C.struct_SwrContext)(ctx),
20-
&dst.planes[0],
21-
C.int(dst_nb_samples),
22-
&src.planes[0],
23-
C.int(src_nb_samples),
24-
))
16+
// src can be set to nil to flush the last few samples out at the end.
17+
func SWResample_convert(ctx *SWRContext, dest, src *AVSamples) (int, error) {
18+
var n int
19+
if src != nil {
20+
n = int(C.swr_convert((*C.struct_SwrContext)(ctx), &dest.planes[0], dest.nb_samples, &src.planes[0], src.nb_samples))
21+
} else {
22+
n = int(C.swr_convert((*C.struct_SwrContext)(ctx), &dest.planes[0], dest.nb_samples, nil, 0))
23+
}
2524
if n < 0 {
2625
return 0, AVError(n)
2726
} else {

0 commit comments

Comments
 (0)