-
Notifications
You must be signed in to change notification settings - Fork 169
/
swr.go
137 lines (102 loc) · 2.74 KB
/
swr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package gmf
/*
#cgo pkg-config: libswresample
#include "libswresample/swresample.h"
#include <libavcodec/avcodec.h>
#include <libavutil/frame.h>
int gmf_sw_resample(SwrContext* ctx, AVFrame* dstFrame, AVFrame* srcFrame){
return swr_convert(ctx, dstFrame->data, dstFrame->nb_samples,
(const uint8_t **)srcFrame->data, srcFrame->nb_samples);
}
int gmf_swr_flush(SwrContext* ctx, AVFrame* dstFrame) {
return swr_convert(ctx, dstFrame->data, dstFrame->nb_samples,
NULL, 0);
}
*/
import "C"
import (
"fmt"
)
type SwrCtx struct {
swrCtx *C.struct_SwrContext
channels int
format int32
}
func NewSwrCtx(options []*Option, channels int, format int32) (*SwrCtx, error) {
ctx := &SwrCtx{
swrCtx: C.swr_alloc(),
channels: channels,
format: format,
}
for _, option := range options {
option.Set(ctx.swrCtx)
}
if ret := int(C.swr_init(ctx.swrCtx)); ret < 0 {
return nil, fmt.Errorf("error initializing swr context - %s", AvError(ret))
}
return ctx, nil
}
func (ctx *SwrCtx) Free() {
C.swr_free(&ctx.swrCtx)
}
func (ctx *SwrCtx) Convert(input *Frame) (*Frame, error) {
var (
dst *Frame
err error
)
if dst, err = NewAudioFrame(ctx.format, ctx.channels, input.NbSamples()); err != nil {
return nil, fmt.Errorf("error creating new audio frame - %s\n", err)
}
C.gmf_sw_resample(ctx.swrCtx, dst.avFrame, input.avFrame)
return dst, nil
}
func (ctx *SwrCtx) Flush(nbSamples int) (*Frame, error) {
var (
dst *Frame
err error
)
if dst, err = NewAudioFrame(ctx.format, ctx.channels, nbSamples); err != nil {
return nil, fmt.Errorf("error creating new audio frame - %s\n", err)
}
C.gmf_swr_flush(ctx.swrCtx, dst.avFrame)
return dst, nil
}
func DefaultResampler(ost *Stream, frames []*Frame, flush bool) []*Frame {
var (
result []*Frame = make([]*Frame, 0)
winFrame, tmpFrame *Frame
)
if ost.SwrCtx == nil || ost.AvFifo == nil {
return frames
}
frameSize := ost.CodecCtx().FrameSize()
for i, _ := range frames {
ost.AvFifo.Write(frames[i])
for ost.AvFifo.SamplesToRead() >= frameSize {
winFrame = ost.AvFifo.Read(frameSize)
winFrame.SetChannelLayout(ost.CodecCtx().GetDefaultChannelLayout(ost.CodecCtx().Channels()))
tmpFrame, _ = ost.SwrCtx.Convert(winFrame)
if tmpFrame == nil || tmpFrame.IsNil() {
break
}
tmpFrame.SetPts(ost.Pts)
tmpFrame.SetPktDts(int(ost.Pts))
ost.Pts += int64(frameSize)
result = append(result, tmpFrame)
}
}
if flush {
if tmpFrame, _ = ost.SwrCtx.Flush(frameSize); tmpFrame != nil && !tmpFrame.IsNil() {
tmpFrame.SetPts(ost.Pts)
tmpFrame.SetPktDts(int(ost.Pts))
ost.Pts += int64(frameSize)
result = append(result, tmpFrame)
}
}
for i := 0; i < len(frames); i++ {
if frames[i] != nil {
frames[i].Free()
}
}
return result
}