-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAacEncoder.cpp
More file actions
190 lines (153 loc) · 4.73 KB
/
Copy pathAacEncoder.cpp
File metadata and controls
190 lines (153 loc) · 4.73 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
#include "AacEncoder.h"
#include <QDebug>
AacEncoder::AacEncoder()
{
}
AacEncoder::~AacEncoder()
{
}
int AacEncoder::InitEncode(int sample_rate, int bit_rate, AVSampleFormat sample_fmt, int chanel_layout)
{
//avcodec_register_all();
av_register_all();
const AVCodec* codec = nullptr;
//while ((codec = av_codec_next(codec))) {
// if (codec->encode2 && codec->type == AVMediaType::AVMEDIA_TYPE_AUDIO) {
// qDebug() << "Codec Name :" << codec->name;
// qDebug() << "Type: " << av_get_media_type_string(codec->type);
// qDebug() << "Description: " << (codec->long_name ? codec->long_name : codec->name);
// qDebug() << "---";
// }
//}
/* find the MP2 encoder */
codec = avcodec_find_encoder_by_name("libfdk_aac");
//codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
qDebug() << "codec name: " << codec->name;
qDebug() << "codec long name: " << codec->long_name;
const enum AVSampleFormat* p = codec->sample_fmts;
while (*p != AV_SAMPLE_FMT_NONE) {
qDebug() << "supoort codec fmt : " << av_get_sample_fmt_name(*p);
p++;
}
audioCodecCtx = avcodec_alloc_context3(codec);
if (!audioCodecCtx) {
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
//打印看到只支持 AV_SAMPLE_FMT_S16,所以这里写死
audioCodecCtx->sample_rate = sample_rate;
audioCodecCtx->channel_layout = chanel_layout;
audioCodecCtx->channels = av_get_channel_layout_nb_channels(audioCodecCtx->channel_layout);
audioCodecCtx->sample_fmt = sample_fmt;
audioCodecCtx->bit_rate = bit_rate;
audioCodecCtx->profile = FF_PROFILE_AAC_HE;
//检查是否支持fmt
if (!check_sample_fmt(codec, audioCodecCtx->sample_fmt)) {
//fprintf(stderr, "Encoder does not support sample format %s",av_get_sample_fmt_name(audioCodecCtx->sample_fmt));
qDebug() << "Encoder does not support sample format " << av_get_sample_fmt_name(audioCodecCtx->sample_fmt);
exit(1);
}
if (!check_sample_rate(codec, audioCodecCtx->sample_rate)) {
//fprintf(stderr, "Encoder does not support sample format %s",av_get_sample_fmt_name(audioCodecCtx->sample_fmt));
qDebug() << "Encoder does not support sample rate " << audioCodecCtx->sample_rate;
exit(1);
}
// Set ADTS flag,设置后,就没有单个adts头了
audioCodecCtx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
/* open it */
if (avcodec_open2(audioCodecCtx, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
//编码输入数据的长度 1024* channel*fmt
frame_byte_size = audioCodecCtx->frame_size * audioCodecCtx->channels * 2;
pkt = av_packet_alloc();
if (!pkt) {
fprintf(stderr, "could not allocate the packet\n");
exit(1);
}
/* frame containing input raw audio */
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
frame->nb_samples = audioCodecCtx->frame_size;
frame->format = audioCodecCtx->sample_fmt;
frame->channel_layout = audioCodecCtx->channel_layout;
/* allocate the data buffers */
int ret = av_frame_get_buffer(frame, 0);
if (ret < 0) {
fprintf(stderr, "Could not allocate audio data buffers\n");
exit(1);
}
#ifdef WRITE_CAPTURE_AAC
if (!aac_out_file) {
aac_out_file = fopen("ouput.aac", "wb");
if (aac_out_file == nullptr)
{
}
}
#endif // WRITE_CAPTURE_AAC
//保存编码信息
codec_config.codec_type = static_cast<int>(AVMediaType::AVMEDIA_TYPE_AUDIO);
codec_config.codec_id = static_cast<int>(AV_CODEC_ID_AAC);
codec_config.sample_rate = audioCodecCtx->sample_rate;
codec_config.channel = audioCodecCtx->channels;
codec_config.format = audioCodecCtx->sample_fmt;
return 0;
}
int AacEncoder::Encode(const char* src_buf, int src_len, unsigned char* dst_buf)
{
if (frame_byte_size != src_len)
{
//编码器输入的数据不符合一帧数据,要求的pcm数据长度
qDebug() << "src_len is not " << frame_byte_size;
}
int planar = av_sample_fmt_is_planar(audioCodecCtx->sample_fmt);
if (planar)
{
// 我编码用的非planer结构
}
else
{
memcpy(frame->data[0], src_buf, src_len);
}
int ret;
/* send the frame for encoding */
ret = avcodec_send_frame(audioCodecCtx, frame);
if (ret < 0) {
fprintf(stderr, "Error sending the frame to the encoder\n");
exit(1);
}
ret = avcodec_receive_packet(audioCodecCtx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
qDebug() << "Error encoding audio frame " << ret;
return 0;
}
else if (ret < 0) {
qDebug()<< "Error encoding audio frame" << ret;
exit(1);
}
int pkt_len = pkt->size;
if (dst_buf != nullptr)
{
memcpy(dst_buf, pkt->data, pkt->size);
}
#ifdef WRITE_CAPTURE_AAC
fwrite(pkt->data, 1, pkt->size, aac_out_file);
#endif
av_packet_unref(pkt);
return pkt_len;
}
int AacEncoder::StopEncode()
{
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&audioCodecCtx);
return 0;
}