-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffmpeg_libopus.cpp
341 lines (290 loc) · 9.12 KB
/
ffmpeg_libopus.cpp
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
#include <iostream>
using namespace std;
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswresample/swresample.h"
#include "libavdevice/avdevice.h"
#include "libavutil/avutil.h"
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#include <libavutil/samplefmt.h>
#include <libavutil/audio_fifo.h>
}
int64_t base_pts = 0;
int64_t current_pts = 0;
void Encodec(AVFormatContext *ofmt_ctx, AVCodecContext *codec_ctx, AVFrame *frame, FILE *out2)
{
int ret;
AVPacket *pkt = av_packet_alloc();
ret = avcodec_send_frame(codec_ctx, frame);
if (ret < 0) {
fprintf(stderr, "Error sending the frame to the encoder\n");
exit(1);
}
while (ret >= 0) {
ret = avcodec_receive_packet(codec_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
av_packet_free(&pkt);
return;
}
else if (ret < 0) {
fprintf(stderr, "Error encoding audio frame\n");
av_packet_free(&pkt);
exit(1);
}
// 调整 PTS 使其从 0 开始
if (base_pts == 0)
{
base_pts = pkt->pts;
}
pkt->pts -= base_pts;
cout << "Encodec pkt.PTS = " << pkt->pts << endl;
fwrite(pkt->data, 1, pkt->size, out2);
ret = av_interleaved_write_frame(ofmt_ctx, pkt);
if (ret < 0)
{
cout << "av_interleaved_write_frame to failed\n";
exit(1);
}
av_packet_unref(pkt);
}
}
int main(int argc, char **argv)
{
//1.初始化
av_log_set_level(AV_LOG_ERROR);
avdevice_register_all();
int ret = 0;
char error[128];
int count = 0;
const AVInputFormat *ifmt = nullptr;
AVFormatContext *ifmt_ctx = nullptr;
AVFormatContext *ofmt_ctx = nullptr;
AVCodecContext *codec_ctx = nullptr;
const AVCodec *codec = nullptr;
AVDictionary *opt = nullptr;
AVPacket *pkt = nullptr;
AVFrame *frame = nullptr;
AVStream *ost = nullptr;
AVAudioFifo *audio_fifo = nullptr;
const char *outfilename = "s16.pcm";
const char *outfilename2 = "test_encoder.opus";
const char *outfilename3 = "test_muxer.opus";
FILE *out = nullptr;
FILE *out2 = nullptr;
FILE *out3 = nullptr;
//cout << av_version_info() << endl;
ifmt = av_find_input_format("dshow");
av_dict_set(&opt, "channels", "2", 0);
av_dict_set(&opt, "sample_rate", "48000", 0);
av_dict_set(&opt, "sample_size", "16", 0);
ret = avformat_open_input(&ifmt_ctx,
"audio=virtual-audio-capturer", //麦克风 (Realtek(R) Audio)
ifmt,
&opt);
if (ret < 0)
{
av_strerror(ret, error, sizeof(error) - 1);
av_log(nullptr, AV_LOG_INFO, "avformat_open_input is error, %s\n", error);
goto end_;
}
out = fopen(outfilename, "wb");
if (out == nullptr)
{
cout << "FILE out is nullptr\n";
goto end_;
}
out2 = fopen(outfilename2, "wb");
if (out2 == nullptr)
{
cout << "FILE out is nullptr\n";
goto end_;
}
pkt = av_packet_alloc();
if (!pkt)
{
cout << "packet is nullptr\n";
goto end_;
}
//1.2 初始化编码器
codec = avcodec_find_encoder_by_name("libopus");
if(!codec)
{
cout << "avcodec_find_encoder_by_name to failed\n";
goto end_;
}
codec_ctx = avcodec_alloc_context3(codec);
if(!codec_ctx)
{
cout << "avcodec_alloc_context3 to failed\n";
goto end_;
}
codec_ctx->codec_type = AVMEDIA_TYPE_AUDIO;
codec_ctx->bit_rate = 32 * 1024;
codec_ctx->ch_layout = AV_CHANNEL_LAYOUT_STEREO;
codec_ctx->sample_rate = 48000;
codec_ctx->sample_fmt = AV_SAMPLE_FMT_S16;
//设置libopus私有的参数
av_dict_set(&opt, "b", "24000", 0); //比特率, 单位bit/s
av_dict_set(&opt, "compression_level", "8", 0); //设置编码算法复杂度, 0 ~ 10。
av_dict_set(&opt, "frame_duration", "10", 0); //设置最大帧大小或帧的持续时间(以毫秒为单位)
av_dict_set(&opt, "application", "audio", 0); //设置预期的应用程序类型。
ret = avcodec_open2(codec_ctx, codec, &opt);
if(ret < 0)
{
av_strerror(ret, error, sizeof(error) - 1);
av_log(nullptr, AV_LOG_INFO, "avcodec_open2 is error, %s\n", error);
goto end_;
}
//1.3初始化AVframe(存储原始数据帧----PCM数据)
frame = av_frame_alloc();
/* 每次送多少数据给编码器由:
* (1)frame_size(每帧单个通道的采样点数);
* (2)sample_fmt(采样点格式);
* (3)channel_layout(通道布局情况);
* 3个要素决定
*/
frame->nb_samples = codec_ctx->frame_size;
frame->format = codec_ctx->sample_fmt;
frame->ch_layout = codec_ctx->ch_layout;
ret = av_frame_get_buffer(frame, 0);
if (ret < 0)
{
cout << "av_frame_get_buffer failed" << endl;
goto end_;
}
//1.4 初始化输出文件
ret = avformat_alloc_output_context2(&ofmt_ctx, nullptr, "opus", outfilename3);
if(ret < 0)
{
av_strerror(ret, error, sizeof(error) - 1);
av_log(nullptr, AV_LOG_INFO, "avformat_alloc_output_context2 is error, %s\n", error);
goto end_;
}
//1.5 添加流
ost = avformat_new_stream(ofmt_ctx, codec);
if(!ost)
{
cout << "Failed to allcoat stream\n";
goto end_;
}
ret = avcodec_parameters_from_context(ost->codecpar, codec_ctx);
if(ret < 0)
{
av_strerror(ret, error, sizeof(error) - 1);
av_log(nullptr, AV_LOG_INFO, "avcodec_parameters_from_context is error, %s\n", error);
goto end_;
}
//1.6 打开输出文件
if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
{
ret = avio_open(&ofmt_ctx->pb, outfilename3, AVIO_FLAG_WRITE);
if (ret < 0)
{
av_strerror(ret, error, sizeof(error) - 1);
av_log(nullptr, AV_LOG_INFO, "avio_open is error, %s\n", error);
goto end_;
}
}
//1.7 写入文件头
ret = avformat_write_header(ofmt_ctx, nullptr);
if(ret < 0)
{
av_strerror(ret, error, sizeof(error) - 1);
av_log(nullptr, AV_LOG_INFO, "avformat_write_header is error, %s\n", error);
goto end_;
}
//av_read_frame()的pkt.szie是1920,采集样本数小于4096,需要使用AVAudioFifo存储数据帧。
//设置大小为frame->nb_samples(单通道样本数)
audio_fifo = av_audio_fifo_alloc(codec_ctx->sample_fmt, frame->ch_layout.nb_channels, frame->nb_samples);
if(!audio_fifo)
{
cout << "audio_fifo is nullptr\n";
goto end_;
}
//2.采集音频数据
//采样间隔默认是10ms,最终时长为20s
while (av_read_frame(ifmt_ctx, pkt) == 0 && count < 2000)
{
cout << "pkt.size = " << pkt->size << endl;
ret = av_frame_make_writable(frame);
if (ret < 0)
exit(1);
fwrite(pkt->data, 1, pkt->size, out); //原始数据
// 创建一个 void* 类型的指针数组
void* data_ptrs[1] = { pkt->data };
// 计算样本数
int samples = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->ch_layout.nb_channels);
av_audio_fifo_write(audio_fifo, data_ptrs, samples); //每帧数据都存入audio_fifo中
cout << "audio_fifo.szie = " << av_audio_fifo_size(audio_fifo) << endl;
//audio_fifo中的数据满足一个AVframe就取出一帧数据送入解码
if(av_audio_fifo_size(audio_fifo) >= frame->nb_samples)
{
ret = av_audio_fifo_read(audio_fifo, (void**)frame->data, frame->nb_samples);
if(ret < frame->nb_samples)
{
cout << "error,av_audio_fifo_read frame size = " << ret << endl;
goto end_;
}
// 设置PTS
frame->pts = current_pts;
cout << "frame->pts = " << frame->pts << endl;
Encodec(ofmt_ctx, codec_ctx, frame, out2);
current_pts += frame->nb_samples;
}
av_packet_unref(pkt);
count++;
}
//最后一帧数据全部送入编码器。
ret = av_audio_fifo_read(audio_fifo, (void**)frame->data, av_audio_fifo_size(audio_fifo));
if(ret > 0)
{
frame->pts = current_pts;
Encodec(ofmt_ctx, codec_ctx, frame, out2);
current_pts += ret;
}
fflush(out);
//3.释放资源
end_:
if(out)
{
fclose(out);
}
if(frame)
{
//释放frame后冲刷最后解码的数据,保证数据完整
av_frame_free(&frame);
Encodec(ofmt_ctx, codec_ctx, frame, out2);
fflush(out2);
av_write_trailer(ofmt_ctx);
}
if(out2)
{
fclose(out2);
}
if(pkt)
{
av_packet_free(&pkt);
}
if(opt)
{
av_dict_free(&opt);
}
if(ifmt_ctx)
{
avformat_close_input(&ifmt_ctx);
}
if(ofmt_ctx)
{
avformat_close_input(&ofmt_ctx);
}
if(codec_ctx)
{
avcodec_free_context(&codec_ctx);
}
system("pause");
return 0;
}