Skip to content

Commit

Permalink
Merge pull request #34 from mutablelogic/ffmpeg61
Browse files Browse the repository at this point in the history
Fixed audio resampling issues
  • Loading branch information
djthorpe authored Jul 29, 2024
2 parents 08903f1 + a30a366 commit 5af5cc1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
13 changes: 9 additions & 4 deletions pkg/ffmpeg/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,17 @@ func (d *Decoder) decode(packet *ff.AVPacket, fn DecoderFrameFn) error {
dest = (*Frame)(d.frame)
}

// If we have a nil frame here, then don't pass back to the caller
if dest == nil {
ff.AVUtil_frame_unref(d.frame)
continue
}

// Copy across the timebase and pts
// TODO if dest != nil {
// TODO
// fmt.Println("pts=", d.frame.Pts())
// (*ff.AVFrame)(dest).SetPts(d.frame.Pts())
// (*ff.AVFrame)(dest).SetTimeBase(d.timeBase)
//}
// (*ff.AVFrame)(dest).SetPts(d.frame.Pts())
// (*ff.AVFrame)(dest).SetTimeBase(d.timeBase)

// Pass back to the caller
if err := fn(d.stream, dest); errors.Is(err, io.EOF) {
Expand Down
14 changes: 13 additions & 1 deletion pkg/ffmpeg/resampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (r *resampler) Frame(src *Frame) (*Frame, error) {
}

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

//func swrConvertFrame_(ctx *ff.SWRContext, src, dest *ff.AVFrame) error {
// return ff.SWResample_convert_frame(ctx, src, dest)
//}

func swrConvertFrame(ctx *ff.SWRContext, src, dest *ff.AVFrame) error {
_, err := ff.SWResample_convert(ctx, ff.AVUtil_samples_frame(dest), ff.AVUtil_samples_frame(src))
if err != nil {
return err
}
return nil
}

func newResampler(dest, src *Frame) (*ff.SWRContext, error) {
// Create a new resampler
ctx := ff.SWResample_alloc()
Expand Down
14 changes: 13 additions & 1 deletion sys/ffmpeg61/avutil_samples.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
import "C"

const (
AV_NUM_PLANES = 48
AV_NUM_PLANES = 8
)

type AVSamples struct {
Expand Down Expand Up @@ -83,6 +83,18 @@ func (data *AVSamples) NumSamples() int {
////////////////////////////////////////////////////////////////////////////////
// BINDINGS

// Get AVSamples from an AVFrame
func AVUtil_samples_frame(frame *AVFrame) *AVSamples {
return &AVSamples{
nb_samples: frame.nb_samples,
nb_channels: frame.channels,
sample_fmt: C.enum_AVSampleFormat(frame.format),
plane_size: frame.linesize[0],
buffer_size: frame.linesize[0] * frame.nb_samples,
planes: frame.data,
}
}

// Allocate a samples buffer for nb_samples samples. Return allocated data for each plane, and the stride.
func AVUtil_samples_alloc(nb_samples, nb_channels int, sample_fmt AVSampleFormat, align bool) (*AVSamples, error) {
if nb_channels < 1 {
Expand Down
17 changes: 8 additions & 9 deletions sys/ffmpeg61/swresample_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import "C"
// PUBLIC METHODS

// Core conversion function. Returns number of samples output per channel.
// in and in_count can be set to 0 to flush the last few samples out at the end.
func SWResample_convert(ctx *SWRContext, dst *AVSamples, dst_nb_samples int, src *AVSamples, src_nb_samples int) (int, error) {
n := int(C.swr_convert(
(*C.struct_SwrContext)(ctx),
&dst.planes[0],
C.int(dst_nb_samples),
&src.planes[0],
C.int(src_nb_samples),
))
// src can be set to nil to flush the last few samples out at the end.
func SWResample_convert(ctx *SWRContext, dest, src *AVSamples) (int, error) {
var n int
if src != nil {
n = int(C.swr_convert((*C.struct_SwrContext)(ctx), &dest.planes[0], dest.nb_samples, &src.planes[0], src.nb_samples))
} else {
n = int(C.swr_convert((*C.struct_SwrContext)(ctx), &dest.planes[0], dest.nb_samples, nil, 0))
}
if n < 0 {
return 0, AVError(n)
} else {
Expand Down

0 comments on commit 5af5cc1

Please sign in to comment.