1 #pragma once
2 
3 #include <QLibrary>
4 #include <QString>
5 #include <memory>
6 
7 #include "encoder/encoder.h"
8 #include "util/fifo.h"
9 
10 class EncoderFdkAac : public Encoder {
11   public:
12     EncoderFdkAac(EncoderCallback* pCallback);
13     virtual ~EncoderFdkAac();
14 
15     int initEncoder(int samplerate, QString* pUserErrorMessage) override;
16     void encodeBuffer(const CSAMPLE* samples, const int sampleCount) override;
17     void updateMetaData(const QString& artist, const QString& title, const QString& album) override;
18     void flush() override;
19     void setEncoderSettings(const EncoderSettings& settings) override;
20 
21   private:
22     QString buttWindowsFdkAac();
23 
24     // libfdk-aac common AOTs
25     static const int AOT_AAC_LC = 2; // AAC-LC: Low Complexity (iTunes)
26     static const int AOT_SBR = 5;    // HE-AAC: with Spectral Band Replication
27     static const int AOT_PS = 29;    // HE-AACv2: Parametric Stereo (includes SBR)
28 
29     // libfdk-aac types and structs
30     typedef signed int INT;
31     typedef unsigned int UINT;
32     typedef signed short SHORT;
33     typedef unsigned short USHORT;
34     typedef signed char SCHAR;
35     typedef unsigned char UCHAR;
36 
37     typedef enum {
38         AACENC_OK = 0x0000,
39 
40         AACENC_INVALID_HANDLE = 0x0020,
41         AACENC_MEMORY_ERROR = 0x0021,
42         AACENC_UNSUPPORTED_PARAMETER = 0x0022,
43         AACENC_INVALID_CONFIG = 0x0023,
44 
45         AACENC_INIT_ERROR = 0x0040,
46         AACENC_INIT_AAC_ERROR = 0x0041,
47         AACENC_INIT_SBR_ERROR = 0x0042,
48         AACENC_INIT_TP_ERROR = 0x0043,
49         AACENC_INIT_META_ERROR = 0x0044,
50 
51         AACENC_ENCODE_ERROR = 0x0060,
52 
53         AACENC_ENCODE_EOF = 0x0080,
54     } AACENC_ERROR;
55 
56     typedef enum {
57         AACENC_AOT = 0x0100,
58         AACENC_BITRATE = 0x0101,
59         AACENC_BITRATEMODE = 0x0102,
60         AACENC_SAMPLERATE = 0x0103,
61         AACENC_SBR_MODE = 0x0104,
62         AACENC_GRANULE_LENGTH = 0x0105,
63         AACENC_CHANNELMODE = 0x0106,
64         AACENC_CHANNELORDER = 0x0107,
65         AACENC_SBR_RATIO = 0x0108,
66         AACENC_AFTERBURNER = 0x0200,
67         AACENC_BANDWIDTH = 0x0203,
68         AACENC_PEAK_BITRATE = 0x0207,
69         AACENC_TRANSMUX = 0x0300,
70         AACENC_HEADER_PERIOD = 0x0301,
71         AACENC_SIGNALING_MODE = 0x0302,
72         AACENC_TPSUBFRAMES = 0x0303,
73         AACENC_AUDIOMUXVER = 0x0304,
74         AACENC_PROTECTION = 0x0306,
75         AACENC_ANCILLARY_BITRATE = 0x0500,
76         AACENC_METADATA_MODE = 0x0600,
77         AACENC_CONTROL_STATE = 0xFF00,
78         AACENC_NONE = 0xFFFF
79     } AACENC_PARAM;
80 
81     typedef enum {
82         IN_AUDIO_DATA = 0,
83         IN_ANCILLRY_DATA = 1, // as is in fdk-aac
84         IN_METADATA_SETUP = 2,
85         OUT_BITSTREAM_DATA = 3,
86         OUT_AU_SIZES = 4
87     } AACENC_BufferIdentifier;
88 
89     typedef enum {
90         FDK_NONE = 0,
91         FDK_TOOLS = 1,
92         FDK_SYSLIB = 2,
93         FDK_AACDEC = 3,
94         FDK_AACENC = 4,
95         FDK_SBRDEC = 5,
96         FDK_SBRENC = 6,
97         FDK_TPDEC = 7,
98         FDK_TPENC = 8,
99         FDK_MPSDEC = 9,
100         FDK_MPEGFILEREAD = 10,
101         FDK_MPEGFILEWRITE = 11,
102         FDK_MP2DEC = 12,
103         FDK_DABDEC = 13,
104         FDK_DABPARSE = 14,
105         FDK_DRMDEC = 15,
106         FDK_DRMPARSE = 16,
107         FDK_AACLDENC = 17,
108         FDK_MP2ENC = 18,
109         FDK_MP3ENC = 19,
110         FDK_MP3DEC = 20,
111         FDK_MP3HEADPHONE = 21,
112         FDK_MP3SDEC = 22,
113         FDK_MP3SENC = 23,
114         FDK_EAEC = 24,
115         FDK_DABENC = 25,
116         FDK_DMBDEC = 26,
117         FDK_FDREVERB = 27,
118         FDK_DRMENC = 28,
119         FDK_METADATATRANSCODER = 29,
120         FDK_AC3DEC = 30,
121         FDK_PCMDMX = 31,
122 
123         FDK_MODULE_LAST
124     } FDK_MODULE_ID;
125 
126     typedef struct AACENCODER* HANDLE_AACENCODER;
127     typedef struct {
128         UINT maxOutBufBytes;
129         UINT maxAncBytes;
130         UINT inBufFillLevel;
131         UINT inputChannels;
132         UINT frameLength;
133         UINT encoderDelay;
134         UCHAR confBuf[64];
135         UINT confSize;
136     } AACENC_InfoStruct;
137     typedef struct {
138         INT numBufs;
139         void** bufs;
140         INT* bufferIdentifiers;
141         INT* bufSizes;
142         INT* bufElSizes;
143     } AACENC_BufDesc;
144     typedef struct {
145         INT numInSamples;
146         INT numAncBytes;
147     } AACENC_InArgs;
148     typedef struct {
149         INT numOutBytes;
150         INT numInSamples;
151         INT numAncBytes;
152     } AACENC_OutArgs;
153     typedef struct {
154         const char* title;
155         const char* build_date;
156         const char* build_time;
157         FDK_MODULE_ID module_id;
158         INT version;
159         UINT flags;
160         char versionStr[32];
161     } LIB_INFO;
162 
163     // libfdk-aac functions prototypes
164     typedef AACENC_ERROR (*aacEncGetLibInfo_)(LIB_INFO*);
165 
166     typedef AACENC_ERROR (*aacEncOpen_)(
167             HANDLE_AACENCODER*,
168             const UINT,
169             const UINT);
170 
171     typedef AACENC_ERROR (*aacEncClose_)(HANDLE_AACENCODER*);
172 
173     typedef AACENC_ERROR (*aacEncEncode_)(
174             const HANDLE_AACENCODER,
175             const AACENC_BufDesc*,
176             const AACENC_BufDesc*,
177             const AACENC_InArgs*,
178             AACENC_OutArgs*);
179 
180     typedef AACENC_ERROR (*aacEncInfo_)(
181             const HANDLE_AACENCODER,
182             AACENC_InfoStruct*);
183 
184     typedef AACENC_ERROR (*aacEncoder_SetParam_)(
185             const HANDLE_AACENCODER,
186             const AACENC_PARAM,
187             const UINT);
188 
189     // libfdk-aac function pointers
190     aacEncGetLibInfo_ aacEncGetLibInfo;
191     aacEncOpen_ aacEncOpen;
192     aacEncClose_ aacEncClose;
193     aacEncEncode_ aacEncEncode;
194     aacEncInfo_ aacEncInfo;
195     aacEncoder_SetParam_ aacEncoder_SetParam;
196 
197     // Instance methods
198     void processFIFO();
199 
200     // Instance attributes
201     int m_aacAot;
202     int m_bitrate;
203     int m_channels;
204     int m_samplerate;
205     EncoderCallback* m_pCallback;
206     std::unique_ptr<QLibrary> m_pLibrary;
207     FIFO<SAMPLE>* m_pInputFifo;
208     SAMPLE* m_pFifoChunkBuffer;
209     int m_readRequired;
210     HANDLE_AACENCODER m_aacEnc;
211     unsigned char* m_pAacDataBuffer;
212     AACENC_InfoStruct m_aacInfo;
213     bool m_hasSbr;
214 };
215