-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtranscoder_test.go
More file actions
358 lines (296 loc) · 7.92 KB
/
Copy pathtranscoder_test.go
File metadata and controls
358 lines (296 loc) · 7.92 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package audio_transcoder
import (
"fmt"
"github.com/lkmio/g726"
"os"
"testing"
)
func parseADTSFrameSize(data []byte) int {
if len(data) < 7 {
return 0
}
return (int(data[3]&0x03) << 11) |
(int(data[4]) << 3) |
(int(data[5]&0xE0) >> 5)
}
func readADTSFrame(data []byte) ([]byte, []byte) {
frameSize := parseADTSFrameSize(data)
if frameSize == 0 {
return nil, nil
} else if len(data) < frameSize {
return nil, nil
}
return data[:frameSize], data[frameSize:]
}
func DecodeAAC(path string) (string, int, int) {
file, err := os.ReadFile(path)
if err != nil {
panic(err)
}
aacDecoder := FindDecoder("AAC")
if aacDecoder == nil {
panic("aac decoder not found")
}
err = aacDecoder.(*AACDecoder).Create(file[:7], nil)
if err != nil {
panic(err)
}
defer aacDecoder.Destroy()
// 创建同名文件, 添加.pcm后缀
pcmPath := path + ".pcm"
pcmFos, err := os.Create(pcmPath)
if err != nil {
panic(err)
}
defer pcmFos.Close()
pcmBuffer := make([]byte, 1024*1024)
for offset := 0; offset < len(file); {
readFrame, _ := readADTSFrame(file[offset:])
if readFrame == nil {
break
}
pcmN, err := aacDecoder.Decode(readFrame, pcmBuffer)
if err != nil {
panic(err)
} else if pcmN > 0 {
pcmFos.Write(pcmBuffer[:pcmN])
}
offset += len(readFrame)
}
return pcmPath, aacDecoder.SampleRate(), aacDecoder.Channels()
}
func EncodeAAC(pcmPath string, sampleRate int, channels int) string {
file, err := os.ReadFile(pcmPath)
if err != nil {
panic(err)
}
aacEncoder, err := FindEncoder("AAC", sampleRate, channels)
if aacEncoder == nil {
panic(err)
}
sampleSize, err := aacEncoder.(*AACEncoder).Create(sampleRate, channels, 1)
if err != nil {
panic(err)
}
defer aacEncoder.Destroy()
// 创建同名文件, 添加.aac后缀
aacFos, err := os.Create(pcmPath + ".aac")
if err != nil {
panic(err)
}
defer aacFos.Close()
for offset := 0; offset < len(file); {
size := sampleSize
if offset+size > len(file) {
size = len(file) - offset
}
_, _ = aacEncoder.Encode(file[offset:offset+size], func(bytes []byte) {
aacFos.Write(bytes)
})
offset += size
}
return pcmPath + ".aac"
}
func DecodeOpus(path string, sampleRate int, channels int) (*OpusDecoder, *os.File) {
opusDecoder := FindDecoder("OPUS")
if opusDecoder == nil {
panic("opus decoder not found")
}
err := opusDecoder.(*OpusDecoder).Create(sampleRate, channels)
if err != nil {
panic(err)
}
// 创建同名文件, 添加.pcm后缀
pcmFos, err := os.Create(path + ".pcm")
if err != nil {
panic(err)
}
return opusDecoder.(*OpusDecoder), pcmFos
}
func EncodeOpus(pcmPath string, sampleRate int, channels int, pktCb func([]byte)) {
file, err := os.ReadFile(pcmPath)
if err != nil {
panic(err)
}
opusEncoder, err := FindEncoder("OPUS", sampleRate, channels)
if opusEncoder == nil {
panic(err)
}
sampleSize, err := opusEncoder.(*OpusEncoder).Create(sampleRate, channels)
if err != nil {
panic(err)
}
defer opusEncoder.Destroy()
// 创建同名文件, 添加.opus后缀
opusFos, err := os.Create(pcmPath + ".opus")
if err != nil {
panic(err)
}
defer opusFos.Close()
for offset := 0; offset < len(file); {
size := sampleSize
if offset+size > len(file) {
size = len(file) - offset
}
_, _ = opusEncoder.Encode(file[offset:offset+size], func(bytes []byte) {
opusFos.Write(bytes)
if pktCb != nil {
pktCb(bytes)
}
})
offset += size
}
}
func DecodeG711(g711Path string, codeType string) (string, int, int) {
file, err := os.ReadFile(g711Path)
if err != nil {
panic(err)
}
g711Decoder := FindDecoder(codeType)
if g711Decoder == nil {
panic("g711 decoder not found")
}
pcm := make([]byte, len(file)*2)
n, err := g711Decoder.Decode(file, pcm)
if err != nil {
panic(err)
} else if n != len(file)*2 {
panic("g711 decode error")
}
// 创建同名文件, 添加.pcm后缀
pcmPath := g711Path + ".pcm"
pcmFos, err := os.Create(pcmPath)
if err != nil {
panic(err)
}
defer pcmFos.Close()
pcmFos.Write(pcm[:n])
return pcmPath, g711Decoder.SampleRate(), g711Decoder.Channels()
}
func EncodeG711(pcmPath string, codeType string) {
file, err := os.ReadFile(pcmPath)
if err != nil {
panic(err)
}
g711Encoder, err := FindEncoder(codeType, 8000, 1)
if g711Encoder == nil {
panic(err)
}
out, err := os.Create(pcmPath + "." + codeType)
if err != nil {
panic(err)
}
defer out.Close()
_, _ = g711Encoder.Encode(file, func(bytes []byte) {
out.Write(bytes)
})
}
func DecodeG726(g726Path string, rate g726.G726Rate) (string, int, int) {
file, err := os.ReadFile(g726Path)
if err != nil {
panic(err)
}
g726Decoder := FindDecoder("G726")
if g726Decoder == nil {
panic("g726 decoder not found")
}
err = g726Decoder.(*G726Decoder).Create(rate)
if err != nil {
panic(err)
}
defer g726Decoder.Destroy()
pcm := make([]byte, len(file)*8)
n, err := g726Decoder.Decode(file, pcm)
if err != nil {
panic(err)
}
// 创建同名文件, 添加.pcm后缀
pcmPath := g726Path + ".pcm"
os.WriteFile(pcmPath, pcm[:n], 0644)
return pcmPath, g726Decoder.SampleRate(), g726Decoder.Channels()
}
func EncodeG726(pcmPath string, rate g726.G726Rate) string {
file, err := os.ReadFile(pcmPath)
if err != nil {
panic(err)
}
g726Encoder, err := FindEncoder("G726", 8000, 1)
if g726Encoder == nil {
panic(err)
}
err = g726Encoder.(*G726Encoder).Create(rate)
if err != nil {
panic(err)
}
defer g726Encoder.Destroy()
out, err := os.Create(pcmPath + ".g726")
if err != nil {
panic(err)
}
defer out.Close()
_, _ = g726Encoder.Encode(file, func(bytes []byte) {
out.Write(bytes)
})
return pcmPath + ".g726"
}
func testOpusCodec(pcmPath string, sampleRate int, channels int) {
// 重新解码为pcm, 检查opus解码器是否正常
opusDecoder, opusPcmFile := DecodeOpus(pcmPath+".opus", sampleRate, channels)
defer opusPcmFile.Close()
defer opusDecoder.Destroy()
pcm := make([]byte, 1024*1024)
// 编码为opus
EncodeOpus(pcmPath, sampleRate, channels, func(bytes []byte) {
n, _ := opusDecoder.Decode(bytes, pcm)
if n > 0 {
opusPcmFile.Write(pcm[:n])
}
})
}
// 先解码再重新编码
func testAACCodec(aacPath string) (pcmPath string, sampleRate int, channels int) {
pcmPath, sampleRate, channels = DecodeAAC(aacPath)
fmt.Println("aac sample rate:", sampleRate)
fmt.Println("aac channels:", channels)
// 重新编码为aac, 检查aac编码器是否正常
EncodeAAC(pcmPath, sampleRate, channels)
return pcmPath, sampleRate, channels
}
func testG711Codec(g711Path string, codeType string) (string, int, int) {
pcmPath, sampleRate, channels := DecodeG711(g711Path, codeType)
// 重新编码为g711, 检查编码器是否正常
EncodeG711(pcmPath, codeType)
return pcmPath, sampleRate, channels
}
func TestTranscoder(t *testing.T) {
t.Run("aac_transcode", func(t *testing.T) {
aacPath := "../source_files/frxx_48000_2.aac"
//aacPath := "../source_files/wwzjdy_44100_2.aac"
pcmPath, sampleRate, channels := testAACCodec(aacPath)
testOpusCodec(pcmPath, sampleRate, channels)
})
t.Run("g711u_transcode", func(t *testing.T) {
g711aPath := "../source_files/u.g711"
pcmPath, sampleRate, channels := testG711Codec(g711aPath, "PCMU")
// 测试aac编解码器, 先编码再重新解码
aacPath := EncodeAAC(pcmPath, sampleRate, channels)
DecodeAAC(aacPath)
// 测试opus编解码器
testOpusCodec(pcmPath, sampleRate, channels)
})
t.Run("g711a_transcode", func(t *testing.T) {
g711aPath := "../source_files/11111.raw"
pcmPath, sampleRate, channels := testG711Codec(g711aPath, "PCMA")
// 测试aac编解码器, 先编码再重新解码
aacPath := EncodeAAC(pcmPath, sampleRate, channels)
DecodeAAC(aacPath)
// 测试opus编解码器
testOpusCodec(pcmPath, sampleRate, channels)
})
t.Run("g726", func(t *testing.T) {
g726Path := "../source_files/ps_h264_g726.raw.audio.1.G726"
pcmPath, _, _ := DecodeG726(g726Path, g726.G726Rate16kbps)
reG726Path := EncodeG726(pcmPath, g726.G726Rate16kbps)
_, _, _ = DecodeG726(reG726Path, g726.G726Rate16kbps)
})
}