-
Notifications
You must be signed in to change notification settings - Fork 0
/
swr.go
189 lines (144 loc) · 4.09 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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"
"unsafe"
)
type SwrCtx struct {
swrCtx *C.struct_SwrContext
channels int
format int32
sampleRate int
inSampleRate int
layout int
}
var AV_ROUND_UP uint32 = C.AV_ROUND_UP
//
func NewSwrCtxWithParams(in_channel_count int, in_sample_format int32, in_sample_rate int,
out_channel_count int, out_sample_format int32, out_sample_rate int) (*SwrCtx, error) {
in_channel_layout := C.int64_t(C.av_get_default_channel_layout(C.int(in_channel_count)))
out_channel_layout := C.int64_t(C.av_get_default_channel_layout(C.int(out_channel_count)))
var swrCtx *C.struct_SwrContext = nil
swrCtx = C.swr_alloc_set_opts(swrCtx,
out_channel_layout,
out_sample_format,
C.int(out_sample_rate),
in_channel_layout,
in_sample_format,
C.int(in_sample_rate),
0,
unsafe.Pointer(nil))
ctx := &SwrCtx{
swrCtx: swrCtx,
channels: out_channel_count,
format: out_sample_format,
sampleRate: out_sample_rate,
inSampleRate: in_sample_rate,
layout: int(out_channel_layout),
}
if ret := int(C.swr_init(ctx.swrCtx)); ret < 0 {
return nil, fmt.Errorf("error initializing swr context - %s", AvError(ret))
}
return ctx, nil
// init with sws
}
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
)
out_samples := int(C.av_rescale_rnd(
C.swr_get_delay(ctx.swrCtx, C.int64_t(ctx.inSampleRate))+
C.int64_t(input.NbSamples()),
C.int64_t(ctx.sampleRate),
C.int64_t(ctx.inSampleRate),
AV_ROUND_UP))
if dst, err = NewAudioFrame(ctx.format, ctx.channels, out_samples); err != nil {
return nil, fmt.Errorf("error creating new audio frame - %s\n", err)
}
dst.SetChannelLayout(ctx.layout)
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)
}
dst.SetChannelLayout(ctx.layout)
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
}