Debian Codegraph @

components/encoder/fdkaac/worker.cpp

raw symbols SCIP
1 /* BoCA - BonkEnc Component Architecture
2 * Copyright (C) 2007-2025 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <smooth.h>
14
15 #include "worker.h"
16 #include "config.h"
17
18 BoCA::SuperWorker::SuperWorker(const Config *config, const Format &iFormat) : processSignal(1), readySignal(1)
19 {
20 processSignal.Wait();
21
22 flush = False;
23 quit = False;
24
25 format = iFormat;
26
27 threadMain.Connect(&SuperWorker::Run, this);
28
29 /* Get configuration.
30 */
31 Bool mp4Container = config->GetIntValue(ConfigureFDKAAC::ConfigID, "MP4Container", True);
32 Int mpegVersion = config->GetIntValue(ConfigureFDKAAC::ConfigID, "MPEGVersion", 0);
33 Int aacType = config->GetIntValue(ConfigureFDKAAC::ConfigID, "AACType", AOT_AAC_LC);
34 Int mode = config->GetIntValue(ConfigureFDKAAC::ConfigID, "Mode", 0);
35 Int bitrate = config->GetIntValue(ConfigureFDKAAC::ConfigID, "Bitrate", 64);
36 Int quality = config->GetIntValue(ConfigureFDKAAC::ConfigID, "Quality", 4);
37 Bool afterburner = config->GetIntValue(ConfigureFDKAAC::ConfigID, "Afterburner", True);
38 Int bandwidth = config->GetIntValue(ConfigureFDKAAC::ConfigID, "Bandwidth", 0);
39
40 /* Fall back to HE if HEv2 is selected for non-stereo input.
41 */
42 if (aacType == AOT_PS && format.channels != 2) aacType = AOT_SBR;
43
44 /* Create and configure FDK AAC encoder.
45 */
46 ex_aacEncOpen(&handle, 0x07, format.channels);
47
48 Int channelMode = MODE_UNKNOWN;
49
50 switch (format.channels)
51 {
52 case 1: channelMode = MODE_1; break;
53 case 2: channelMode = MODE_2; break;
54 case 3: channelMode = MODE_1_2; break;
55 case 4: channelMode = MODE_1_2_1; break;
56 case 5: channelMode = MODE_1_2_2; break;
57 case 6: channelMode = MODE_1_2_2_1; break;
58 case 8: channelMode = MODE_1_2_2_2_1; break;
59 }
60
61 ex_aacEncoder_SetParam(handle, AACENC_SAMPLERATE, format.rate);
62 ex_aacEncoder_SetParam(handle, AACENC_CHANNELMODE, channelMode);
63 ex_aacEncoder_SetParam(handle, AACENC_CHANNELORDER, 1 /* WAVE channel order */);
64
65 if (mode == 0) ex_aacEncoder_SetParam(handle, AACENC_BITRATE, bitrate * 1000 * format.channels);
66 else ex_aacEncoder_SetParam(handle, AACENC_BITRATEMODE, quality);
67
68 if (aacType == AOT_AAC_LC ||
69 aacType == AOT_ER_AAC_LD) ex_aacEncoder_SetParam(handle, AACENC_BANDWIDTH, Math::Min(bandwidth, format.rate / 2));
70
71 ex_aacEncoder_SetParam(handle, AACENC_AOT, mpegVersion + aacType);
72 ex_aacEncoder_SetParam(handle, AACENC_AFTERBURNER, afterburner);
73 ex_aacEncoder_SetParam(handle, AACENC_TRANSMUX, mp4Container ? TT_MP4_RAW : TT_MP4_ADTS);
74
75 if ( mp4Container && (aacType == AOT_SBR || aacType == AOT_PS )) ex_aacEncoder_SetParam(handle, AACENC_SIGNALING_MODE, 1);
76 if (!mp4Container && (aacType == AOT_ER_AAC_LD || aacType == AOT_ER_AAC_ELD)) ex_aacEncoder_SetParam(handle, AACENC_TRANSMUX, TT_MP4_LOAS);
77
78 AACENC_InfoStruct aacInfo;
79
80 ex_aacEncEncode(handle, NULL, NULL, NULL, NULL);
81 ex_aacEncInfo(handle, &aacInfo);
82
83 frameSize = aacInfo.frameLength;
84 maxPacketSize = aacInfo.maxOutBufBytes;
85 }
86
87 BoCA::SuperWorker::~SuperWorker()
88 {
89 /* Destroy FDK encoder.
90 */
91 ex_aacEncClose(&handle);
92 }
93
94 Int BoCA::SuperWorker::Run()
95 {
96 while (!Threads::Access::Value(quit))
97 {
98 processSignal.Wait();
99
100 if (Threads::Access::Value(quit)) break;
101
102 packetBuffer.Resize(0);
103 packetSizes.RemoveAll();
104
105 Int samplesLeft = samplesBuffer.Size();
106 Int samplesPerFrame = frameSize * format.channels;
107
108 Int framesProcessed = 0;
109
110 while (flush || samplesLeft >= samplesPerFrame)
111 {
112 packetBuffer.Resize(packetBuffer.Size() + maxPacketSize);
113
114 /* Prepare buffer information.
115 */
116 Void *inputBuffer = (int16_t *) samplesBuffer + framesProcessed * samplesPerFrame;
117 Int inputBufferID = IN_AUDIO_DATA;
118 Int inputBufferSize = samplesPerFrame;
119 Int inputElementSize = 2;
120
121 Void *outputBuffer = (uint8_t *) packetBuffer + packetBuffer.Size() - maxPacketSize;
122 Int outputBufferID = OUT_BITSTREAM_DATA;
123 Int outputBufferSize = maxPacketSize;
124 Int outputElementSize = 1;
125
126 /* Configure buffer descriptors.
127 */
128 AACENC_BufDesc input = { 0 };
129 AACENC_BufDesc output = { 0 };
130
131 input.numBufs = 1;
132 input.bufs = &inputBuffer;
133 input.bufferIdentifiers = &inputBufferID;
134 input.bufSizes = &inputBufferSize;
135 input.bufElSizes = &inputElementSize;
136
137 output.numBufs = 1;
138 output.bufs = &outputBuffer;
139 output.bufferIdentifiers = &outputBufferID;
140 output.bufSizes = &outputBufferSize;
141 output.bufElSizes = &outputElementSize;
142
143 /* Hand input data to encoder and retrieve output.
144 */
145 AACENC_InArgs inputInfo = { 0 };
146 AACENC_OutArgs outputInfo = { 0 };
147
148 if (samplesLeft > 0) inputInfo.numInSamples = Math::Min(samplesLeft, samplesPerFrame);
149 else inputInfo.numInSamples = -1;
150
151 Int dataLength = 0;
152
153 if (ex_aacEncEncode(handle, &input, &output, &inputInfo, &outputInfo) == AACENC_OK) dataLength = outputInfo.numOutBytes;
154
155 packetBuffer.Resize(packetBuffer.Size() - maxPacketSize + dataLength);
156
157 if (samplesLeft < 0 && dataLength == 0) break;
158
159 if (dataLength > 0) packetSizes.Add(dataLength);
160
161 framesProcessed++;
162 samplesLeft -= samplesPerFrame;
163 }
164
165 readySignal.Release();
166 }
167
168 return Success();
169 }
170
171 Void BoCA::SuperWorker::Encode(const Buffer<int16_t> &buffer, Int offset, Int size, Bool last)
172 {
173 samplesBuffer.Resize(size);
174
175 memcpy(samplesBuffer, buffer + offset, size * sizeof(int16_t));
176
177 flush = last;
178
179 processSignal.Release();
180 }
181
182 Void BoCA::SuperWorker::WaitUntilReady()
183 {
184 readySignal.Wait();
185 }
186
187 Int BoCA::SuperWorker::Quit()
188 {
189 Threads::Access::Set(quit, True);
190
191 processSignal.Release();
192
193 return Success();
194 }