blob: 11eb425ab70041879c6e8a95fe6aee31f929f008 [file] [log] [blame]
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04001/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
3 Copyright (c) 2008 Gregory Maxwell
4 Written by Jean-Marc Valin and Gregory Maxwell */
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +11005/**
6 @file celt.h
Jean-Marc Valin56522ad2009-06-05 17:17:25 -04007 @brief Contains all the functions for encoding and decoding audio
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +11008 */
9
Jean-Marc Valin013c31d2007-11-30 11:36:46 +110010/*
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
14
15 - Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17
18 - Redistributions in binary form must reproduce the above copyright
19 notice, this list of conditions and the following disclaimer in the
20 documentation and/or other materials provided with the distribution.
21
Jean-Marc Valin013c31d2007-11-30 11:36:46 +110022 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
26 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*/
34
35#ifndef CELT_H
36#define CELT_H
37
Jean-Marc Valin3d3bb682008-01-31 10:05:39 +110038#include "celt_types.h"
39
Jean-Marc Valin77ff83a2008-01-22 13:16:36 +110040#ifdef __cplusplus
41extern "C" {
42#endif
43
Peter Kirk19f9dc92008-06-06 14:38:38 +020044#if defined(__GNUC__) && defined(CELT_BUILD)
45#define EXPORT __attribute__ ((visibility ("default")))
46#elif defined(WIN32)
47#define EXPORT __declspec(dllexport)
48#else
49#define EXPORT
50#endif
51
Jean-Marc Valin30f7f812009-10-17 14:35:13 -040052#define _celt_check_int(x) (((void)((x) == (celt_int32)0)), (celt_int32)(x))
Jean-Marc Valinc3086a92011-03-21 13:26:03 -040053#define _celt_check_mode_ptr_ptr(ptr) ((ptr) + ((ptr) - (const CELTMode**)(ptr)))
Jean-Marc Valinef20e392011-03-18 15:34:11 -040054#define _celt_check_int_ptr(ptr) ((ptr) + ((ptr) - (int*)(ptr)))
Jean-Marc Valinb6f90612008-10-05 22:39:13 -040055
Jean-Marc Valin8c19b032008-02-20 15:53:15 +110056/* Error codes */
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110057/** No error */
Jean-Marc Valin94d4ea92008-01-27 17:34:35 +110058#define CELT_OK 0
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110059/** An (or more) invalid argument (e.g. out of range) */
Jean-Marc Valin94d4ea92008-01-27 17:34:35 +110060#define CELT_BAD_ARG -1
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110061/** The mode struct passed is invalid */
Jean-Marc Valinef20e392011-03-18 15:34:11 -040062#define CELT_BUFFER_TOO_SMALL -2
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110063/** An internal error was detected */
Jean-Marc Valin94d4ea92008-01-27 17:34:35 +110064#define CELT_INTERNAL_ERROR -3
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +110065/** The data passed (e.g. compressed data to decoder) is corrupted */
Jean-Marc Valin94d4ea92008-01-27 17:34:35 +110066#define CELT_CORRUPTED_DATA -4
Jean-Marc Valinc18fb1d2008-09-30 07:36:54 -040067/** Invalid/unsupported request number */
Jean-Marc Valinb6f90612008-10-05 22:39:13 -040068#define CELT_UNIMPLEMENTED -5
Jean-Marc Valin56522ad2009-06-05 17:17:25 -040069/** An encoder or decoder structure is invalid or already freed */
Gregory Maxwell17169992009-06-04 15:15:34 -040070#define CELT_INVALID_STATE -6
Jean-Marc Valinece94a02009-10-16 07:30:14 -040071/** Memory allocation has failed */
Jean-Marc Valin163b02e2009-10-16 21:32:52 -040072#define CELT_ALLOC_FAIL -7
Jean-Marc Valina76a0b22008-01-17 22:43:05 +110073
Jean-Marc Valinc3086a92011-03-21 13:26:03 -040074
75/* Encoder/decoder Requests */
Jean-Marc Valin7bb26e12011-02-01 17:04:27 -050076
Jean-Marc Valinb6f90612008-10-05 22:39:13 -040077#define CELT_SET_COMPLEXITY_REQUEST 2
Jean-Marc Valin5ac6d422008-10-09 07:27:10 -040078/** Controls the complexity from 0-10 (int) */
Jean-Marc Valinb6f90612008-10-05 22:39:13 -040079#define CELT_SET_COMPLEXITY(x) CELT_SET_COMPLEXITY_REQUEST, _celt_check_int(x)
Jean-Marc Valin7bb26e12011-02-01 17:04:27 -050080
Gregory Maxwell2dd3d322009-06-05 14:05:51 -040081#define CELT_SET_PREDICTION_REQUEST 4
82/** Controls the use of interframe prediction.
83 0=Independent frames
84 1=Short term interframe prediction allowed
85 2=Long term prediction allowed
86 */
87#define CELT_SET_PREDICTION(x) CELT_SET_PREDICTION_REQUEST, _celt_check_int(x)
Jean-Marc Valin7bb26e12011-02-01 17:04:27 -050088
89#define CELT_SET_BITRATE_REQUEST 6
Jean-Marc Valin56522ad2009-06-05 17:17:25 -040090/** Set the target VBR rate in bits per second(int); 0=CBR (default) */
Jean-Marc Valin7bb26e12011-02-01 17:04:27 -050091#define CELT_SET_BITRATE(x) CELT_SET_BITRATE_REQUEST, _celt_check_int(x)
92
John Ridges454d1d02009-05-21 22:38:39 -040093#define CELT_RESET_STATE_REQUEST 8
Jean-Marc Valinc3086a92011-03-21 13:26:03 -040094/** Reset the encoder/decoder memories to zero*/
John Ridges454d1d02009-05-21 22:38:39 -040095#define CELT_RESET_STATE CELT_RESET_STATE_REQUEST
Jean-Marc Valinc18fb1d2008-09-30 07:36:54 -040096
Jean-Marc Valin79b34eb2010-12-05 17:22:06 -050097#define CELT_SET_VBR_CONSTRAINT_REQUEST 10
98#define CELT_SET_VBR_CONSTRAINT(x) CELT_SET_VBR_CONSTRAINT_REQUEST, _celt_check_int(x)
99
Jean-Marc Valin7bb26e12011-02-01 17:04:27 -0500100#define CELT_SET_VBR_REQUEST 12
101#define CELT_SET_VBR(x) CELT_SET_VBR_REQUEST, _celt_check_int(x)
102
Jean-Marc Valin0b405d12011-02-04 01:03:42 -0500103#define CELT_SET_INPUT_CLIPPING_REQUEST 14
104#define CELT_SET_INPUT_CLIPPING(x) CELT_SET_INPUT_CLIPPING_REQUEST, _celt_check_int(x)
105
Jean-Marc Valinef20e392011-03-18 15:34:11 -0400106#define CELT_GET_AND_CLEAR_ERROR_REQUEST 15
107#define CELT_GET_AND_CLEAR_ERROR(x) CELT_GET_AND_CLEAR_ERROR_REQUEST, _celt_check_int_ptr(x)
108
Jean-Marc Valinff96b162011-03-21 11:32:50 -0400109#define CELT_GET_LOOKAHEAD_REQUEST 17
110#define CELT_GET_LOOKAHEAD(x) CELT_GET_LOOKAHEAD_REQUEST, _celt_check_int_ptr(x)
111
Jean-Marc Valinc3086a92011-03-21 13:26:03 -0400112#define CELT_SET_CHANNELS_REQUEST 18
113#define CELT_SET_CHANNELS(x) CELT_SET_CHANNELS_REQUEST, _celt_check_int(x)
114
Jean-Marc Valinef20e392011-03-18 15:34:11 -0400115/* Internal */
Jean-Marc Valin5f961462010-05-19 13:38:10 -0400116#define CELT_SET_START_BAND_REQUEST 10000
Jean-Marc Valin5f961462010-05-19 13:38:10 -0400117#define CELT_SET_START_BAND(x) CELT_SET_START_BAND_REQUEST, _celt_check_int(x)
118
Jean-Marc Valin525d7cf2010-07-13 14:14:16 -0400119#define CELT_SET_END_BAND_REQUEST 10001
120#define CELT_SET_END_BAND(x) CELT_SET_END_BAND_REQUEST, _celt_check_int(x)
121
Jean-Marc Valinf1916a12011-01-31 10:51:30 -0500122
Jean-Marc Valine6acfe02011-03-11 16:31:24 -0500123
Jean-Marc Valin56522ad2009-06-05 17:17:25 -0400124/** Contains the state of an encoder. One encoder state is needed
125 for each stream. It is initialised once at the beginning of the
126 stream. Do *not* re-initialise the state for every frame.
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100127 @brief Encoder state
128 */
Jean-Marc Valin269d40a2007-12-07 11:29:45 +1100129typedef struct CELTEncoder CELTEncoder;
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100130
Jean-Marc Valin56522ad2009-06-05 17:17:25 -0400131/** State of the decoder. One decoder state is needed for each stream.
132 It is initialised once at the beginning of the stream. Do *not*
133 re-initialise the state for every frame */
Jean-Marc Valin269d40a2007-12-07 11:29:45 +1100134typedef struct CELTDecoder CELTDecoder;
Jean-Marc Valin013c31d2007-11-30 11:36:46 +1100135
Jean-Marc Valin56522ad2009-06-05 17:17:25 -0400136/** The mode contains all the information necessary to create an
137 encoder. Both the encoder and decoder need to be initialised
138 with exactly the same mode, otherwise the quality will be very
139 bad */
Jean-Marc Valinc245a222007-12-06 19:14:20 +1100140typedef struct CELTMode CELTMode;
Jean-Marc Valin013c31d2007-11-30 11:36:46 +1100141
Jean-Marc Valinf997ad52008-01-31 16:47:16 +1100142
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100143/** \defgroup codec Encoding and decoding */
144/* @{ */
145
146/* Mode calls */
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100147
Jean-Marc Valin56522ad2009-06-05 17:17:25 -0400148/** Creates a new mode struct. This will be passed to an encoder or
149 decoder. The mode MUST NOT BE DESTROYED until the encoders and
150 decoders that use it are destroyed as well.
Gregory Maxwell8ed86582008-12-02 12:06:14 -0500151 @param Fs Sampling rate (32000 to 96000 Hz)
Jean-Marc Valin56522ad2009-06-05 17:17:25 -0400152 @param frame_size Number of samples (per channel) to encode in each
153 packet (even values; 64 - 512)
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100154 @param error Returned error code (if NULL, no error will be returned)
155 @return A newly created mode
156*/
Jean-Marc Valin30f7f812009-10-17 14:35:13 -0400157EXPORT CELTMode *celt_mode_create(celt_int32 Fs, int frame_size, int *error);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100158
Jean-Marc Valin56522ad2009-06-05 17:17:25 -0400159/** Destroys a mode struct. Only call this after all encoders and
160 decoders using this mode are destroyed as well.
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100161 @param mode Mode to be destroyed
162*/
Peter Kirk19f9dc92008-06-06 14:38:38 +0200163EXPORT void celt_mode_destroy(CELTMode *mode);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100164
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100165/* Encoder stuff */
166
Jean-Marc Valin8cf29f02011-01-30 23:38:28 -0500167EXPORT int celt_encoder_get_size(int channels);
168
169EXPORT int celt_encoder_get_size_custom(const CELTMode *mode, int channels);
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100170
Jean-Marc Valin56522ad2009-06-05 17:17:25 -0400171/** Creates a new encoder state. Each stream needs its own encoder
172 state (can't be shared across simultaneous streams).
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500173 @param channels Number of channels
174 @param error Returns an error code
175 @return Newly created encoder state.
176*/
Jean-Marc Valin913a1742011-01-29 10:00:20 -0500177EXPORT CELTEncoder *celt_encoder_create(int sampling_rate, int channels, int *error);
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500178
179/** Creates a new encoder state. Each stream needs its own encoder
180 state (can't be shared across simultaneous streams).
Jean-Marc Valin56522ad2009-06-05 17:17:25 -0400181 @param mode Contains all the information about the characteristics of
182 * the stream (must be the same characteristics as used for the
183 * decoder)
Jean-Marc Valin954fb112009-10-22 21:14:22 -0400184 @param channels Number of channels
185 @param error Returns an error code
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100186 @return Newly created encoder state.
187*/
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500188EXPORT CELTEncoder *celt_encoder_create_custom(const CELTMode *mode, int channels, int *error);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100189
Jean-Marc Valin913a1742011-01-29 10:00:20 -0500190EXPORT CELTEncoder *celt_encoder_init(CELTEncoder *st, int sampling_rate, int channels, int *error);
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500191
192EXPORT CELTEncoder *celt_encoder_init_custom(CELTEncoder *st, const CELTMode *mode, int channels, int *error);
Jean-Marc Valin7cfb7302010-08-27 16:54:33 -0400193
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100194/** Destroys a an encoder state.
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100195 @param st Encoder state to be destroyed
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100196 */
Peter Kirk19f9dc92008-06-06 14:38:38 +0200197EXPORT void celt_encoder_destroy(CELTEncoder *st);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100198
199/** Encodes a frame of audio.
200 @param st Encoder state
Jean-Marc Valind56c6102010-05-07 20:30:22 -0400201 @param pcm PCM audio in float format, with a normal range of ±1.0.
202 * Samples with a range beyond ±1.0 are supported but will
203 * be clipped by decoders using the integer API and should
204 * only be used if it is known that the far end supports
205 * extended dynmaic range. There must be exactly
206 * frame_size samples per channel.
207 @param compressed The compressed data is written here. This may not alias pcm or
208 * optional_synthesis.
209 @param nbCompressedBytes Maximum number of bytes to use for compressing the frame
210 * (can change from one frame to another)
211 @return Number of bytes written to "compressed". Will be the same as
212 * "nbCompressedBytes" unless the stream is VBR and will never be larger.
213 * If negative, an error has occurred (see error codes). It is IMPORTANT that
214 * the length returned be somehow transmitted to the decoder. Otherwise, no
215 * decoding is possible.
216*/
Jean-Marc Valinb3dae4b2011-02-04 21:50:54 -0500217EXPORT int celt_encode_float(CELTEncoder *st, const float *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
Gregory Maxwellf0d6e312009-02-16 21:29:15 -0500218
Jean-Marc Valin5ac6d422008-10-09 07:27:10 -0400219/** Encodes a frame of audio.
220 @param st Encoder state
Jean-Marc Valind56c6102010-05-07 20:30:22 -0400221 @param pcm PCM audio in signed 16-bit format (native endian). There must be
222 * exactly frame_size samples per channel.
223 @param compressed The compressed data is written here. This may not alias pcm or
224 * optional_synthesis.
225 @param nbCompressedBytes Maximum number of bytes to use for compressing the frame
226 * (can change from one frame to another)
227 @return Number of bytes written to "compressed". Will be the same as
228 * "nbCompressedBytes" unless the stream is VBR and will never be larger.
229 * If negative, an error has occurred (see error codes). It is IMPORTANT that
230 * the length returned be somehow transmitted to the decoder. Otherwise, no
231 * decoding is possible.
232 */
Jean-Marc Valinb3dae4b2011-02-04 21:50:54 -0500233EXPORT int celt_encode(CELTEncoder *st, const celt_int16 *pcm, int frame_size, unsigned char *compressed, int maxCompressedBytes);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100234
Jean-Marc Valinc18fb1d2008-09-30 07:36:54 -0400235/** Query and set encoder parameters
236 @param st Encoder state
237 @param request Parameter to change or query
238 @param value Pointer to a 32-bit int value
239 @return Error code
240*/
Jean-Marc Valinb6f90612008-10-05 22:39:13 -0400241EXPORT int celt_encoder_ctl(CELTEncoder * st, int request, ...);
Jean-Marc Valinc18fb1d2008-09-30 07:36:54 -0400242
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100243/* Decoder stuff */
244
Jean-Marc Valin8cf29f02011-01-30 23:38:28 -0500245EXPORT int celt_decoder_get_size(int channels);
246
247EXPORT int celt_decoder_get_size_custom(const CELTMode *mode, int channels);
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100248
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100249/** Creates a new decoder state. Each stream needs its own decoder state (can't
250 be shared across simultaneous streams).
251 @param mode Contains all the information about the characteristics of the
252 stream (must be the same characteristics as used for the encoder)
Jean-Marc Valin954fb112009-10-22 21:14:22 -0400253 @param channels Number of channels
254 @param error Returns an error code
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100255 @return Newly created decoder state.
256 */
Jean-Marc Valin913a1742011-01-29 10:00:20 -0500257EXPORT CELTDecoder *celt_decoder_create(int sampling_rate, int channels, int *error);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100258
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500259/** Creates a new decoder state. Each stream needs its own decoder state (can't
260 be shared across simultaneous streams).
261 @param mode Contains all the information about the characteristics of the
262 stream (must be the same characteristics as used for the encoder)
263 @param channels Number of channels
264 @param error Returns an error code
265 @return Newly created decoder state.
266 */
267EXPORT CELTDecoder *celt_decoder_create_custom(const CELTMode *mode, int channels, int *error);
268
Jean-Marc Valin913a1742011-01-29 10:00:20 -0500269EXPORT CELTDecoder *celt_decoder_init(CELTDecoder *st, int sampling_rate, int channels, int *error);
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500270
271EXPORT CELTDecoder *celt_decoder_init_custom(CELTDecoder *st, const CELTMode *mode, int channels, int *error);
Jean-Marc Valin7cfb7302010-08-27 16:54:33 -0400272
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100273/** Destroys a a decoder state.
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100274 @param st Decoder state to be destroyed
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100275 */
Peter Kirk19f9dc92008-06-06 14:38:38 +0200276EXPORT void celt_decoder_destroy(CELTDecoder *st);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100277
278/** Decodes a frame of audio.
279 @param st Decoder state
280 @param data Compressed data produced by an encoder
281 @param len Number of bytes to read from "data". This MUST be exactly the number
282 of bytes returned by the encoder. Using a larger value WILL NOT WORK.
283 @param pcm One frame (frame_size samples per channel) of decoded PCM will be
Jean-Marc Valin5ac6d422008-10-09 07:27:10 -0400284 returned here in float format.
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100285 @return Error code.
286 */
Jean-Marc Valin017fa852010-05-06 22:11:48 -0400287EXPORT int celt_decode_float(CELTDecoder *st, const unsigned char *data, int len, float *pcm, int frame_size);
Gregory Maxwellf0d6e312009-02-16 21:29:15 -0500288
Jean-Marc Valin5ac6d422008-10-09 07:27:10 -0400289/** Decodes a frame of audio.
290 @param st Decoder state
291 @param data Compressed data produced by an encoder
292 @param len Number of bytes to read from "data". This MUST be exactly the number
293 of bytes returned by the encoder. Using a larger value WILL NOT WORK.
294 @param pcm One frame (frame_size samples per channel) of decoded PCM will be
295 returned here in 16-bit PCM format (native endian).
296 @return Error code.
297 */
Jean-Marc Valin017fa852010-05-06 22:11:48 -0400298EXPORT int celt_decode(CELTDecoder *st, const unsigned char *data, int len, celt_int16 *pcm, int frame_size);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100299
John Ridges454d1d02009-05-21 22:38:39 -0400300/** Query and set decoder parameters
301 @param st Decoder state
302 @param request Parameter to change or query
303 @param value Pointer to a 32-bit int value
304 @return Error code
305 */
306EXPORT int celt_decoder_ctl(CELTDecoder * st, int request, ...);
307
308
Jean-Marc Valina3803a92009-10-24 11:24:34 -0400309/** Returns the English string that corresponds to an error code
310 * @param error Error code (negative for an error, 0 for success
311 * @return Constant string (must NOT be freed)
312 */
Jean-Marc Valin328b8bd2009-10-19 19:07:38 -0400313EXPORT const char *celt_strerror(int error);
Jean-Marc Valinece94a02009-10-16 07:30:14 -0400314
Jean-Marc Valina3803a92009-10-24 11:24:34 -0400315/* @} */
316
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100317
Jean-Marc Valin77ff83a2008-01-22 13:16:36 +1100318#ifdef __cplusplus
319}
320#endif
321
Jean-Marc Valin013c31d2007-11-30 11:36:46 +1100322#endif /*CELT_H */