-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaac_code.cpp
More file actions
129 lines (109 loc) · 4.99 KB
/
aac_code.cpp
File metadata and controls
129 lines (109 loc) · 4.99 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
// g++ aac_code.cpp -o aac_code -lavformat -lavutil -lfdk-aac
#include <fdk-aac/aacdecoder_lib.h>
#include <fdk-aac/aacenc_lib.h>
#include <fstream>
#include <iostream>
#include <memory>
extern "C" {
#include <libavformat/avformat.h>
}
using namespace std;
int main()
{
const char* inputfile = "luca.aac";
const char* outputfile = "lucaa.aac";
auto outfile = make_unique<ofstream>();
outfile->open(outputfile, ios::out | ios::binary);
AVFormatContext* input_fmt_ctx = nullptr;
int ret;
if ((ret = avformat_open_input(&input_fmt_ctx, inputfile, nullptr, nullptr)) < 0) {
av_log(nullptr, AV_LOG_ERROR, "cannot open input file\n");
return ret;
}
if ((ret = avformat_find_stream_info(input_fmt_ctx, nullptr)) < 0) {
av_log(nullptr, AV_LOG_ERROR, "cannot find stream info\n");
return ret;
}
int audio_stream_index = -1;
for (int i = 0; i < input_fmt_ctx->nb_streams; ++i) {
AVStream* stream = input_fmt_ctx->streams[i];
if (stream->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
audio_stream_index = i;
break;
}
}
if (audio_stream_index < 0) {
av_log(nullptr, AV_LOG_WARNING, "cannot find audio stream\n");
return 0;
}
auto handle = aacDecoder_Open(TT_MP4_ADTS, 1);
HANDLE_AACENCODER enhandle = nullptr;
aacEncOpen(&enhandle, 0, 2);
aacEncoder_SetParam(enhandle, AACENC_AOT, AOT_AAC_LC);
aacEncoder_SetParam(enhandle, AACENC_TRANSMUX, TT_MP4_ADTS);
aacEncoder_SetParam(enhandle, AACENC_CHANNELMODE, 2);
aacEncoder_SetParam(enhandle, AACENC_CHANNELORDER, 1);
aacEncoder_SetParam(enhandle, AACENC_GRANULE_LENGTH, 1024);
aacEncoder_SetParam(enhandle, AACENC_SAMPLERATE, 48000);
aacEncoder_SetParam(enhandle, AACENC_BITRATE, 48000 / 44 * 128);
aacEncoder_SetParam(enhandle, AACENC_AFTERBURNER, 1);
aacEncEncode(enhandle, nullptr, nullptr, nullptr, nullptr);
uint8_t* buffer = new uint8_t[2048 * 2 * sizeof(INT_PCM)];
AVPacket packet;
uint32_t validsize = 0;
AACENC_InfoStruct eninfo = {0};
aacEncInfo(enhandle, &eninfo);
cout << "bufbytes: " << eninfo.maxOutBufBytes << ", framelength: " << eninfo.frameLength << endl;
uint8_t* out_buf = new uint8_t[2 * 2 * eninfo.maxOutBufBytes];
while (true) {
if (ret = av_read_frame(input_fmt_ctx, &packet) < 0) {
break;
}
do {
UCHAR* inbuffer = (UCHAR*)packet.data;
uint32_t insize = packet.size;
validsize = packet.size;
if (packet.stream_index == audio_stream_index) {
ret = aacDecoder_Fill(handle, &inbuffer, &insize, &validsize);
memset(buffer, 0, 2048 * 2 * sizeof(INT_PCM));
ret = aacDecoder_DecodeFrame(handle, (INT_PCM*)buffer, 2048 * 2, 0);
if (ret == AAC_DEC_OK) {
CStreamInfo* info = aacDecoder_GetStreamInfo(handle);
uint32_t outlen = info->frameSize * info->numChannels * sizeof(INT_PCM);
cout << "channel: " << info->numChannels << ", sample: " << info->sampleRate
<< ", size: " << info->frameSize << ", :" << info->aot << endl;
AACENC_BufDesc inBuf = {0};
AACENC_BufDesc outBuf = {0};
AACENC_InArgs inArgs = {0};
AACENC_OutArgs outArgs = {0};
int32_t inIdentifier = IN_AUDIO_DATA;
int32_t inElemSize = 2;
inArgs.numInSamples = 1024 * 2;
inBuf.numBufs = 1;
inBuf.bufs = reinterpret_cast<void**>(&buffer);
inBuf.bufferIdentifiers = &inIdentifier;
inBuf.bufSizes = (INT*)&outlen;
inBuf.bufElSizes = &inElemSize;
int outIdentifier = OUT_BITSTREAM_DATA;
int32_t outElemSize = 2;
outBuf.numBufs = 1;
outBuf.bufs = reinterpret_cast<void**>(&out_buf);
outBuf.bufferIdentifiers = &outIdentifier;
outBuf.bufElSizes = &outElemSize;
int32_t outSize = 2 * 2 * eninfo.frameLength;
outBuf.bufSizes = reinterpret_cast<INT*>(&outSize);
aacEncEncode(enhandle, &inBuf, &outBuf, &inArgs, &outArgs);
std::cout << "encoded: sample:" << outArgs.numInSamples << ", bytes:" << outArgs.numOutBytes
<< endl;
outfile->write((char*)out_buf, outArgs.numOutBytes);
outfile->flush();
}
}
} while (validsize > 0 && ret != AAC_DEC_NOT_ENOUGH_BITS);
}
avformat_close_input(&input_fmt_ctx);
aacDecoder_Close(handle);
aacEncClose(&enhandle);
outfile->close();
return 0;
}