forked from 3d0c/gmf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathswr.go
64 lines (48 loc) · 1.37 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
/*
2015 Sleepy Programmer <[email protected]>
*/
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);
}
*/
import "C"
type SwrCtx struct {
swrCtx *C.struct_SwrContext
cc *CodecCtx
CgoMemoryManage
}
func NewSwrCtx(options []*Option, cc *CodecCtx) *SwrCtx {
this := &SwrCtx{swrCtx: C.swr_alloc(), cc: cc}
for _, option := range options {
option.Set(this.swrCtx)
}
if int(C.swr_init(this.swrCtx)) < 0 {
return nil
}
return this
}
func (this *SwrCtx) Free() {
C.swr_free(&this.swrCtx)
}
func (this *SwrCtx) Convert(input *Frame) *Frame {
if this.cc == nil {
return nil
}
dstSamples := input.NbSamples()
channels := this.cc.Channels()
format := this.cc.SampleFmt()
dstFrame, _ := NewAudioFrame(format, channels, dstSamples)
C.gmf_sw_resample(this.swrCtx, dstFrame.avFrame, input.avFrame)
// frame := NewFrame()
// dstNbSamples := C.av_rescale_rnd(C.swr_get_delay(this.swrCtx, this.cc.avCodecCtx.sample_rate)+input.avFrame.nb_samples, C.int64_t(this.cc.SampleRate()), this.cc.SampleRate(), C.AV_ROUND_UP)
// if dstNbSamples > input.NbSamples() {
// }
return dstFrame
}