celt.h 12.9 KB
Newer Older
Jean-Marc Valin's avatar
Jean-Marc Valin committed
1
2
3
4
/* Copyright (c) 2007-2008 CSIRO
   Copyright (c) 2007-2009 Xiph.Org Foundation
   Copyright (c) 2008 Gregory Maxwell 
   Written by Jean-Marc Valin and Gregory Maxwell */
5
6
/**
  @file celt.h
7
  @brief Contains all the functions for encoding and decoding audio
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
/*
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
   
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
   
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef CELT_H
#define CELT_H

Jean-Marc Valin's avatar
Jean-Marc Valin committed
42
43
#include "celt_types.h"

44
45
46
47
#ifdef __cplusplus
extern "C" {
#endif

48
49
50
51
52
53
54
55
#if defined(__GNUC__) && defined(CELT_BUILD)
#define EXPORT __attribute__ ((visibility ("default")))
#elif defined(WIN32)
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif

56
#define _celt_check_int(x) (((void)((x) == (celt_int32)0)), (celt_int32)(x))
57
#define _celt_check_mode_ptr_ptr(ptr) ((ptr) + ((ptr) - (CELTMode**)(ptr)))
58

59
/* Error codes */
60
/** No error */
61
#define CELT_OK                0
62
/** An (or more) invalid argument (e.g. out of range) */
63
#define CELT_BAD_ARG          -1
64
/** The mode struct passed is invalid */
65
#define CELT_INVALID_MODE     -2
66
/** An internal error was detected */
67
#define CELT_INTERNAL_ERROR   -3
68
/** The data passed (e.g. compressed data to decoder) is corrupted */
69
#define CELT_CORRUPTED_DATA   -4
70
/** Invalid/unsupported request number */
71
#define CELT_UNIMPLEMENTED    -5
72
/** An encoder or decoder structure is invalid or already freed */
73
#define CELT_INVALID_STATE    -6
74
/** Memory allocation has failed */
75
#define CELT_ALLOC_FAIL       -7
76

77
/* Requests */
78
79
80
#define CELT_GET_MODE_REQUEST    1
/** Get the CELTMode used by an encoder or decoder */
#define CELT_GET_MODE(x) CELT_GET_MODE_REQUEST, _celt_check_mode_ptr_ptr(x)
81

82
#define CELT_SET_COMPLEXITY_REQUEST    2
Jean-Marc Valin's avatar
Jean-Marc Valin committed
83
/** Controls the complexity from 0-10 (int) */
84
#define CELT_SET_COMPLEXITY(x) CELT_SET_COMPLEXITY_REQUEST, _celt_check_int(x)
85

86
87
88
89
90
91
92
#define CELT_SET_PREDICTION_REQUEST    4
/** Controls the use of interframe prediction.
    0=Independent frames
    1=Short term interframe prediction allowed
    2=Long term prediction allowed
 */
#define CELT_SET_PREDICTION(x) CELT_SET_PREDICTION_REQUEST, _celt_check_int(x)
93
94

#define CELT_SET_BITRATE_REQUEST    6
95
/** Set the target VBR rate in bits per second(int); 0=CBR (default) */
96
97
#define CELT_SET_BITRATE(x) CELT_SET_BITRATE_REQUEST, _celt_check_int(x)

98
99
100
/** Reset the encoder/decoder memories to zero*/
#define CELT_RESET_STATE_REQUEST        8
#define CELT_RESET_STATE       CELT_RESET_STATE_REQUEST
101

102
103
104
#define CELT_SET_VBR_CONSTRAINT_REQUEST 10
#define CELT_SET_VBR_CONSTRAINT(x)       CELT_SET_VBR_CONSTRAINT_REQUEST, _celt_check_int(x)

105
106
107
#define CELT_SET_VBR_REQUEST 12
#define CELT_SET_VBR(x)       CELT_SET_VBR_REQUEST, _celt_check_int(x)

108
109
110
#define CELT_SET_INPUT_CLIPPING_REQUEST    14
#define CELT_SET_INPUT_CLIPPING(x) CELT_SET_INPUT_CLIPPING_REQUEST, _celt_check_int(x)

111
112
113
#define CELT_SET_START_BAND_REQUEST    10000
#define CELT_SET_START_BAND(x) CELT_SET_START_BAND_REQUEST, _celt_check_int(x)

114
115
116
#define CELT_SET_END_BAND_REQUEST    10001
#define CELT_SET_END_BAND(x) CELT_SET_END_BAND_REQUEST, _celt_check_int(x)

117
118
119
#define CELT_SET_CHANNELS_REQUEST    10002
#define CELT_SET_CHANNELS(x) CELT_SET_CHANNELS_REQUEST, _celt_check_int(x)

Jean-Marc Valin's avatar
Jean-Marc Valin committed
120
/** GET the lookahead used in the current mode */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
121
#define CELT_GET_LOOKAHEAD    1001
122
123
/** GET the sample rate used in the current mode */
#define CELT_GET_SAMPLE_RATE  1003
124

125
126
127
/** GET the bit-stream version for compatibility check */
#define CELT_GET_BITSTREAM_VERSION 2000

128

129
130
131
/** Contains the state of an encoder. One encoder state is needed 
    for each stream. It is initialised once at the beginning of the
    stream. Do *not* re-initialise the state for every frame.
132
133
   @brief Encoder state
 */
134
typedef struct CELTEncoder CELTEncoder;
135

136
137
138
/** State of the decoder. One decoder state is needed for each stream.
    It is initialised once at the beginning of the stream. Do *not*
    re-initialise the state for every frame */
139
typedef struct CELTDecoder CELTDecoder;
140

141
142
143
144
/** The mode contains all the information necessary to create an
    encoder. Both the encoder and decoder need to be initialised
    with exactly the same mode, otherwise the quality will be very 
    bad */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
145
typedef struct CELTMode CELTMode;
146

147

148
149
150
151
/** \defgroup codec Encoding and decoding */
/*  @{ */

/* Mode calls */
152

153
154
155
/** Creates a new mode struct. This will be passed to an encoder or 
    decoder. The mode MUST NOT BE DESTROYED until the encoders and 
    decoders that use it are destroyed as well.
Gregory Maxwell's avatar
Gregory Maxwell committed
156
 @param Fs Sampling rate (32000 to 96000 Hz)
157
158
 @param frame_size Number of samples (per channel) to encode in each 
                   packet (even values; 64 - 512)
159
160
161
 @param error Returned error code (if NULL, no error will be returned)
 @return A newly created mode
*/
162
EXPORT CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error);
163

164
165
/** Destroys a mode struct. Only call this after all encoders and 
    decoders using this mode are destroyed as well.
166
167
 @param mode Mode to be destroyed
*/
168
EXPORT void celt_mode_destroy(CELTMode *mode);
169

170
/** Query information from a mode */
171
EXPORT int celt_mode_info(const CELTMode *mode, int request, celt_int32 *value);
172

173
174
/* Encoder stuff */

175
176
177
EXPORT int celt_encoder_get_size(int channels);

EXPORT int celt_encoder_get_size_custom(const CELTMode *mode, int channels);
178

179
180
/** Creates a new encoder state. Each stream needs its own encoder 
    state (can't be shared across simultaneous streams).
181
182
183
184
 @param channels Number of channels
 @param error Returns an error code
 @return Newly created encoder state.
*/
Jean-Marc Valin's avatar
Jean-Marc Valin committed
185
EXPORT CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error);
186
187
188

/** Creates a new encoder state. Each stream needs its own encoder
    state (can't be shared across simultaneous streams).
189
190
191
 @param mode Contains all the information about the characteristics of
 *  the stream (must be the same characteristics as used for the 
 *  decoder)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
192
193
 @param channels Number of channels
 @param error Returns an error code
194
195
 @return Newly created encoder state.
*/
196
197
EXPORT CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error);

Jean-Marc Valin's avatar
Jean-Marc Valin committed
198
EXPORT CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error);
199

200
EXPORT CELTEncoder *celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int channels, int *error);
201

202
/** Destroys a an encoder state.
203
 @param st Encoder state to be destroyed
204
 */
205
EXPORT void celt_encoder_destroy(CELTEncoder *st);
206

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/** Encodes a frame of audio.
 @param st Encoder state
 @param pcm PCM audio in float format, with a normal range of ±1.0.
 *          Samples with a range beyond ±1.0 are supported but will
 *          be clipped by decoders using the integer API and should
 *          only be used if it is known that the far end supports
 *          extended dynmaic range. There must be exactly
 *          frame_size samples per channel.
 @param compressed The compressed data is written here. This may not alias pcm or
 *                 optional_synthesis.
 @param nbCompressedBytes Maximum number of bytes to use for compressing the frame
 *          (can change from one frame to another)
 @return Number of bytes written to "compressed". Will be the same as
 *       "nbCompressedBytes" unless the stream is VBR and will never be larger.
 *       If negative, an error has occurred (see error codes). It is IMPORTANT that
 *       the length returned be somehow transmitted to the decoder. Otherwise, no
 *       decoding is possible.
*/
225
EXPORT int celt_encode_float(CELTEncoder *st, const float *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
Gregory Maxwell's avatar
Gregory Maxwell committed
226

227
228
229
230
231
232
233
234
235
236
237
238
239
240
/** Encodes a frame of audio.
 @param st Encoder state
 @param pcm PCM audio in signed 16-bit format (native endian). There must be
 *          exactly frame_size samples per channel.
 @param compressed The compressed data is written here. This may not alias pcm or
 *                         optional_synthesis.
 @param nbCompressedBytes Maximum number of bytes to use for compressing the frame
 *                        (can change from one frame to another)
 @return Number of bytes written to "compressed". Will be the same as
 *       "nbCompressedBytes" unless the stream is VBR and will never be larger.
 *       If negative, an error has occurred (see error codes). It is IMPORTANT that
 *       the length returned be somehow transmitted to the decoder. Otherwise, no
 *       decoding is possible.
 */
241
EXPORT int celt_encode(CELTEncoder *st, const celt_int16 *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
242

243
244
245
246
247
248
/** Query and set encoder parameters 
 @param st Encoder state
 @param request Parameter to change or query
 @param value Pointer to a 32-bit int value
 @return Error code
*/
249
EXPORT int celt_encoder_ctl(CELTEncoder * st, int request, ...);
250

251
252
/* Decoder stuff */

253
254
255
EXPORT int celt_decoder_get_size(int channels);

EXPORT int celt_decoder_get_size_custom(const CELTMode *mode, int channels);
256

257
258
259
260
/** Creates a new decoder state. Each stream needs its own decoder state (can't
    be shared across simultaneous streams).
 @param mode Contains all the information about the characteristics of the
             stream (must be the same characteristics as used for the encoder)
Jean-Marc Valin's avatar
Jean-Marc Valin committed
261
262
 @param channels Number of channels
 @param error Returns an error code
263
264
 @return Newly created decoder state.
 */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
265
EXPORT CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error);
266
267
268
269
270
271
272
273
274
275
276

/** Creates a new decoder state. Each stream needs its own decoder state (can't
    be shared across simultaneous streams).
 @param mode Contains all the information about the characteristics of the
             stream (must be the same characteristics as used for the encoder)
 @param channels Number of channels
 @param error Returns an error code
 @return Newly created decoder state.
 */
EXPORT CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error);

Jean-Marc Valin's avatar
Jean-Marc Valin committed
277
EXPORT CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error);
278

279
EXPORT CELTDecoder *celt_decoder_init_custom(CELTDecoder *st, const CELTMode *mode, int channels, int *error);
280

281
/** Destroys a a decoder state.
282
 @param st Decoder state to be destroyed
283
 */
284
EXPORT void celt_decoder_destroy(CELTDecoder *st);
285

286
287
288
289
290
291
/** Decodes a frame of audio.
 @param st Decoder state
 @param data Compressed data produced by an encoder
 @param len Number of bytes to read from "data". This MUST be exactly the number
            of bytes returned by the encoder. Using a larger value WILL NOT WORK.
 @param pcm One frame (frame_size samples per channel) of decoded PCM will be
Jean-Marc Valin's avatar
Jean-Marc Valin committed
292
            returned here in float format. 
293
294
 @return Error code.
   */
295
EXPORT int celt_decode_float(CELTDecoder *st, const unsigned char *data, int len, float *pcm, int frame_size);
Gregory Maxwell's avatar
Gregory Maxwell committed
296

Jean-Marc Valin's avatar
Jean-Marc Valin committed
297
298
299
300
301
302
303
304
305
/** Decodes a frame of audio.
 @param st Decoder state
 @param data Compressed data produced by an encoder
 @param len Number of bytes to read from "data". This MUST be exactly the number
            of bytes returned by the encoder. Using a larger value WILL NOT WORK.
 @param pcm One frame (frame_size samples per channel) of decoded PCM will be
            returned here in 16-bit PCM format (native endian). 
 @return Error code.
 */
306
EXPORT int celt_decode(CELTDecoder *st, const unsigned char *data, int len, celt_int16 *pcm, int frame_size);
307

308
309
310
311
312
313
314
315
316
/** Query and set decoder parameters
   @param st Decoder state
   @param request Parameter to change or query
   @param value Pointer to a 32-bit int value
   @return Error code
 */
EXPORT int celt_decoder_ctl(CELTDecoder * st, int request, ...);


Jean-Marc Valin's avatar
Jean-Marc Valin committed
317
318
319
320
/** Returns the English string that corresponds to an error code
 * @param error Error code (negative for an error, 0 for success
 * @return Constant string (must NOT be freed)
 */
321
EXPORT const char *celt_strerror(int error);
322

Jean-Marc Valin's avatar
Jean-Marc Valin committed
323
324
/*  @} */

Jean-Marc Valin's avatar
Jean-Marc Valin committed
325

326
327
328
329
#ifdef __cplusplus
}
#endif

330
#endif /*CELT_H */