celt.h 10.4 KB
Newer Older
1
/* (C) 2007-2008 Jean-Marc Valin, CSIRO
2
   (C) 2008 Gregory Maxwell */
3
4
5
6
7
/**
  @file celt.h
  @brief Contains all the functions for encoding and decoding audio streams
 */

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
/*
   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
40
41
#include "celt_types.h"

42
43
44
45
#ifdef __cplusplus
extern "C" {
#endif

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

54
#define _celt_check_int(x) (((void)((x) == (celt_int32_t)0)), (celt_int32_t)(x))
55

56
/* Error codes */
57
/** No error */
58
#define CELT_OK                0
59
/** An (or more) invalid argument (e.g. out of range) */
60
#define CELT_BAD_ARG          -1
61
/** The mode struct passed is invalid */
62
#define CELT_INVALID_MODE     -2
63
/** An internal error was detected */
64
#define CELT_INTERNAL_ERROR   -3
65
/** The data passed (e.g. compressed data to decoder) is corrupted */
66
#define CELT_CORRUPTED_DATA   -4
67
/** Invalid/unsupported request number */
68
#define CELT_UNIMPLEMENTED    -5
69

70
/* Requests */
71
#define CELT_SET_COMPLEXITY_REQUEST    2
Jean-Marc Valin's avatar
Jean-Marc Valin committed
72
/** Controls the complexity from 0-10 (int) */
73
#define CELT_SET_COMPLEXITY(x) CELT_SET_COMPLEXITY_REQUEST, _celt_check_int(x)
74
#define CELT_SET_LTP_REQUEST    4
75
76
/** Activate or deactivate the use of the long term predictor (PITCH) from 0 or 1 (int) */
#define CELT_SET_LTP(x) CELT_SET_LTP_REQUEST, _celt_check_int(x)
77
78
79
#define CELT_SET_VBR_RATE_REQUEST    6
/** Set the target VBR rate in bits per second (int); 0=CBR (default) */
#define CELT_SET_VBR_RATE(x) CELT_SET_VBR_RATE_REQUEST, _celt_check_int(x)
80
81
82
/** Reset the encoder/decoder memories to zero*/
#define CELT_RESET_STATE_REQUEST        8
#define CELT_RESET_STATE       CELT_RESET_STATE_REQUEST
83

Jean-Marc Valin's avatar
Jean-Marc Valin committed
84
/** GET the frame size used in the current mode */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
85
#define CELT_GET_FRAME_SIZE   1000
Jean-Marc Valin's avatar
Jean-Marc Valin committed
86
/** GET the lookahead used in the current mode */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
87
#define CELT_GET_LOOKAHEAD    1001
Jean-Marc Valin's avatar
Jean-Marc Valin committed
88
/** GET the number of channels used in the current mode */
89
#define CELT_GET_NB_CHANNELS  1002
90

91
92
93
/** GET the bit-stream version for compatibility check */
#define CELT_GET_BITSTREAM_VERSION 2000

94
95
96
97
98
99

/** 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.
   @brief Encoder state
 */
100
typedef struct CELTEncoder CELTEncoder;
101
102
103
104

/** 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 */
105
typedef struct CELTDecoder CELTDecoder;
106

107
108
109
/** 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
110
typedef struct CELTMode CELTMode;
111

112

113
114
115
116
/** \defgroup codec Encoding and decoding */
/*  @{ */

/* Mode calls */
117
118
119
120

/** 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
121
 @param Fs Sampling rate (32000 to 96000 Hz)
122
 @param channels Number of channels
Gregory Maxwell's avatar
Gregory Maxwell committed
123
 @param frame_size Number of samples (per channel) to encode in each packet (even values; 64 - 512)
124
125
126
 @param error Returned error code (if NULL, no error will be returned)
 @return A newly created mode
*/
127
EXPORT CELTMode *celt_mode_create(celt_int32_t Fs, int channels, int frame_size, int *error);
128
129
130
131
132

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

135
/** Query information from a mode */
136
EXPORT int celt_mode_info(const CELTMode *mode, int request, celt_int32_t *value);
137

138
139
/* Encoder stuff */

140

141
142
143
144
145
146
/** Creates a new encoder state. Each stream needs its own encoder 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 decoder)
 @return Newly created encoder state.
*/
147
EXPORT CELTEncoder *celt_encoder_create(const CELTMode *mode);
148
149

/** Destroys a an encoder state.
150
 @param st Encoder state to be destroyed
151
 */
152
EXPORT void celt_encoder_destroy(CELTEncoder *st);
153

154
155
/** Encodes a frame of audio.
 @param st Encoder state
Gregory Maxwell's avatar
Gregory Maxwell committed
156
157
158
159
160
 @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. 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
161
162
163
 @param optional_synthesis If not NULL, the encoder copies the audio signal that
 *                         the decoder would decode. It is the same as calling the
 *                         decoder on the compressed data, just faster.
Gregory Maxwell's avatar
Gregory Maxwell committed
164
165
166
167
 *                         This may alias pcm. 
 @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
Jean-Marc Valin's avatar
Jean-Marc Valin committed
168
 *                        (can change from one frame to another)
Gregory Maxwell's avatar
Gregory Maxwell committed
169
170
171
172
173
 @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.
174
*/
175
EXPORT int celt_encode_float(CELTEncoder *st, const float *pcm, float *optional_synthesis, unsigned char *compressed, int nbCompressedBytes);
Gregory Maxwell's avatar
Gregory Maxwell committed
176

Jean-Marc Valin's avatar
Jean-Marc Valin committed
177
178
179
/** Encodes a frame of audio.
 @param st Encoder state
 @param pcm PCM audio in signed 16-bit format (native endian). There must be 
Gregory Maxwell's avatar
Gregory Maxwell committed
180
 *          exactly frame_size samples per channel. 
Jean-Marc Valin's avatar
Jean-Marc Valin committed
181
182
183
 @param optional_synthesis If not NULL, the encoder copies the audio signal that
 *                         the decoder would decode. It is the same as calling the
 *                         decoder on the compressed data, just faster.
Gregory Maxwell's avatar
Gregory Maxwell committed
184
185
186
187
 *                         This may alias pcm. 
 @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
Jean-Marc Valin's avatar
Jean-Marc Valin committed
188
 *                        (can change from one frame to another)
Gregory Maxwell's avatar
Gregory Maxwell committed
189
190
191
192
193
 @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.
Jean-Marc Valin's avatar
Jean-Marc Valin committed
194
 */
195
EXPORT int celt_encode(CELTEncoder *st, const celt_int16_t *pcm, celt_int16_t *optional_synthesis, unsigned char *compressed, int nbCompressedBytes);
196

197
198
199
200
201
202
/** 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
*/
203
EXPORT int celt_encoder_ctl(CELTEncoder * st, int request, ...);
204

205
206
/* Decoder stuff */

207

208
209
210
211
212
213
/** 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)
 @return Newly created decoder state.
 */
214
EXPORT CELTDecoder *celt_decoder_create(const CELTMode *mode);
215
216

/** Destroys a a decoder state.
217
 @param st Decoder state to be destroyed
218
 */
219
EXPORT void celt_decoder_destroy(CELTDecoder *st);
220

221
222
223
224
225
226
/** 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
227
            returned here in float format. 
228
229
 @return Error code.
   */
230
EXPORT int celt_decode_float(CELTDecoder *st, const unsigned char *data, int len, float *pcm);
Gregory Maxwell's avatar
Gregory Maxwell committed
231

Jean-Marc Valin's avatar
Jean-Marc Valin committed
232
233
234
235
236
237
238
239
240
/** 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.
 */
241
EXPORT int celt_decode(CELTDecoder *st, const unsigned char *data, int len, celt_int16_t *pcm);
242

243
244
245
246
247
248
249
250
251
/** 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, ...);


252
/*  @} */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
253
254


255
256
257
258
#ifdef __cplusplus
}
#endif

259
#endif /*CELT_H */