-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaoo_codec.h
More file actions
281 lines (246 loc) · 8.51 KB
/
Copy pathaoo_codec.h
File metadata and controls
281 lines (246 loc) · 8.51 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
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
/* Copyright (c) 2021 Christof Ressi
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution. */
/** \file
* \brief AOO codec API
*
* This header contains the API for adding new audio codecs to the AOO library.
*/
#pragma once
#include "aoo_config.h"
#include "aoo_defines.h"
#include "aoo_types.h"
AOO_PACK_BEGIN
/*-----------------------------------------------------*/
/** \brief base class for all codec classes */
typedef struct AooCodec
{
struct AooCodecInterface *cls; /**< interface pointer */
} AooCodec;
/** \brief codec constructor */
typedef AooCodec * (AOO_CALL *AooCodecNewFunc)(void);
/** \brief codec destructor */
typedef void (AOO_CALL *AooCodecFreeFunc)(AooCodec *codec);
/** \brief codec setup function */
typedef AooError (AOO_CALL *AooCodecSetupFunc)(
/** the encoder/decoder instance */
AooCodec *codec,
/** the desired format; validated and updated on success */
AooFormat *format
);
/** \brief encode audio samples to bytes */
typedef AooError (AOO_CALL *AooCodecEncodeFunc)(
/** the encoder instance */
AooCodec *encoder,
/** [in] input samples (interleaved) */
const AooSample *inSamples,
/** [in] frame size (number of sample per-channel) */
AooInt32 frameSize,
/** [out] output buffer */
AooByte *outData,
/** [in,out] max. buffer size in bytes
* (updated to actual size) */
AooInt32 *outSize
);
/** \brief decode bytes to samples */
typedef AooError (AOO_CALL *AooCodecDecodeFunc)(
/** the decoder instance */
AooCodec *decoder,
/** [in] input data */
const AooByte *inData,
/** [in] input data size in bytes */
AooInt32 numBytes,
/** [out] output samples (interleaved) */
AooSample *outSamples,
/** [in,out] max. number of frames
* (updated to actual number) */
AooInt32 *numFrames
);
/** \brief AOO codec controls
*
* Negative values are reserved for generic controls;
* codec specific controls must be positiv.
*/
AOO_ENUM(AooCodecCtl)
{
/** reset the codec state (`NULL`) */
kAooCodecCtlReset = -1000,
/** get encoding/decoding latency in samples (AooInt32) */
kAooCodecCtlGetLatency
};
/** \brief codec control function */
typedef AooError (AOO_CALL *AooCodecControlFunc)(
/** the encoder/decoder instance */
AooCodec *codec,
/** the control constant */
AooCodecCtl ctl,
/** pointer to value */
void *data,
/** the value size */
AooSize size
);
/** \brief serialize format extension
*
* (= everything after the AooFormat header).
* On success, write the format extension to the given buffer.
*/
typedef AooError (AOO_CALL *AooCodecSerializeFunc)(
/** [in] the source format */
const AooFormat *format,
/** [out] extension buffer; `NULL` returns the required buffer size. */
AooByte *buffer,
/** [in,out] max. buffer size; updated to actual resp. required size */
AooInt32 *bufsize
);
/** \brief deserialize format extension
*
* (= everything after the AooFormat header).
* On success, write the format extension to the given format structure.
*
* \note This function should *not* update the `structSize` member of the `format` argument.
*/
typedef AooError (AOO_CALL *AooCodecDeserializeFunc)(
/** [in] the extension buffer */
const AooByte *buffer,
/** [in] the extension buffer size */
AooInt32 bufsize,
/** [out] destination format structure;
* if `NULL`, just return the required format size via `fmtsize`. */
AooFormat *format,
/** max. format size; updated to actual resp. required size */
AooInt32 *fmtsize
);
/** \brief interface to be implemented by AOO codec classes */
typedef struct AooCodecInterface
{
/** struct size */
AooSize structSize;
/** the code name */
const AooChar * name;
/* encoder methods */
/** create a new encoder instance */
AooCodecNewFunc encoderNew;
/** destroy encoder instance */
AooCodecFreeFunc encoderFree;
/** setup encoder */
AooCodecSetupFunc encoderSetup;
/** encoder controls */
AooCodecControlFunc encoderControl;
/** encode audio data */
AooCodecEncodeFunc encoderEncode;
/* decoder methods */
/** create a new decoder instance */
AooCodecNewFunc decoderNew;
/** destroy decoder instance */
AooCodecFreeFunc decoderFree;
/** setup decoder */
AooCodecSetupFunc decoderSetup;
/** decoder controls */
AooCodecControlFunc decoderControl;
/** decode audio data */
AooCodecDecodeFunc decoderDecode;
/* free functions */
/** serialize format extension */
AooCodecSerializeFunc serialize;
/** deserialize format extension */
AooCodecDeserializeFunc deserialize;
} AooCodecInterface;
/*----------------- helper functions ----------------------*/
/** \brief setup encoder
* \see AooCodecSetupFunc */
AOO_INLINE AooError AooEncoder_setup(
AooCodec *enc, AooFormat *format)
{
return enc->cls->encoderSetup(enc, format);
}
/** \brief encode audio samples to bytes
* \see AooCodecEncodeFunc */
AOO_INLINE AooError AooEncoder_encode(
AooCodec *enc, const AooSample *inSamples, AooInt32 frameSize,
AooByte *outData, AooInt32 *size)
{
return enc->cls->encoderEncode(enc, inSamples, frameSize, outData, size);
}
/** \brief control encoder instance
* \see AooCodecControlFunc */
AOO_INLINE AooError AooEncoder_control(
AooCodec *enc, AooCodecCtl ctl, void *data, AooSize size)
{
return enc->cls->encoderControl(enc, ctl, data, size);
}
/** \brief reset encoder state */
AOO_INLINE AooError AooEncoder_reset(AooCodec *enc)
{
return enc->cls->encoderControl(enc, kAooCodecCtlReset, NULL, 0);
}
/** \brief setup decoder
* \see AooCodecSetupFunc */
AOO_INLINE AooError AooDecoder_setup(
AooCodec *dec, AooFormat *format)
{
return dec->cls->decoderSetup(dec, format);
}
/** \brief decode bytes to audio samples
* \see AooCodecDecodeFunc */
AOO_INLINE AooError AooDecoder_decode(
AooCodec *dec, const AooByte *inData, AooInt32 size,
AooSample *outSamples, AooInt32 *frameSize)
{
return dec->cls->decoderDecode(dec, inData, size, outSamples, frameSize);
}
/** \brief control decoder instance
* \see AooCodecControlFunc */
AOO_INLINE AooError AooDecoder_control(
AooCodec *dec, AooCodecCtl ctl, void *data, AooSize size)
{
return dec->cls->decoderControl(dec, ctl, data, size);
}
/** \brief reset decoder state */
AOO_INLINE AooError AooDecoder_reset(AooCodec *dec)
{
return dec->cls->decoderControl(dec, kAooCodecCtlReset, NULL, 0);
}
/*----------------- register codecs -----------------------*/
/** \brief function for registering codecs */
typedef AooError (AOO_CALL *AooCodecRegisterFunc)(
const AooCodecInterface *cls
);
/** \brief host interface passed to codec plugins */
typedef struct AooCodecHostInterface
{
/** struct size */
AooSize structSize;
/** function for registering new codecs */
AooCodecRegisterFunc registerCodec;
/** allocator function */
AooAllocFunc alloc;
/** log function */
AooLogFunc log;
} AooCodecHostInterface;
/** \brief global host interface instance */
AOO_API const AooCodecHostInterface * aoo_getCodecHostInterface(void);
/** \brief register an external codec plugin */
AOO_API AooError AOO_CALL aoo_registerCodec(const AooCodecInterface *codec);
/** \brief type of entry function for codec plugin module
*
* \note AOO doesn't support dynamic plugin loading out of the box,
* but it is quite easy to implement on your own.
* You just have to put one or more codecs in a shared library and export
* a single function of type AooCodecLoadFunc with the name `aoo_load`:
*
* void aoo_load(const AooCodecHostInterface *interface);
*
* In your host application, you would then scan directories for shared libraries,
* check if they export a function named `aoo_load`, and if yes, call it with a
* pointer to the host interface table.
*/
typedef AooError (AOO_CALL *AooCodecLoadFunc)
(const AooCodecHostInterface *);
/** \brief type of exit function for codec plugin module
*
* Your codec plugin can optionally export a function `aoo_unload` which should be
* called before program exit to properly release shared resources.
*/
typedef AooError (AOO_CALL *AooCodecUnloadFunc)(void);
/*-------------------------------------------------------------------------------------*/
AOO_PACK_END