1 /* (C) 2007-2008 Jean-Marc Valin, CSIRO
5 @brief Contains all the functions for encoding and decoding audio streams
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
13 - Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
16 - Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
20 - Neither the name of the Xiph.org Foundation nor the names of its
21 contributors may be used to endorse or promote products derived from
22 this software without specific prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "celt_types.h"
49 /** An (or more) invalid argument (e.g. out of range) */
50 #define CELT_BAD_ARG -1
51 /** The mode struct passed is invalid */
52 #define CELT_INVALID_MODE -2
53 /** An internal error was detected */
54 #define CELT_INTERNAL_ERROR -3
55 /** The data passed (e.g. compressed data to decoder) is corrupted */
56 #define CELT_CORRUPTED_DATA -4
59 /** GET the frame size used in the current mode */
60 #define CELT_GET_FRAME_SIZE 1000
61 /** GET the lookahead used in the current mode */
62 #define CELT_GET_LOOKAHEAD 1001
63 /** GET the number of channels used in the current mode */
64 #define CELT_GET_NB_CHANNELS 1002
67 /** Contains the state of an encoder. One encoder state is needed for each
68 stream. It is initialised once at the beginning of the stream. Do *not*
69 re-initialise the state for every frame.
72 typedef struct CELTEncoder CELTEncoder;
74 /** State of the decoder. One decoder state is needed for each stream. It is
75 initialised once at the beginning of the stream. Do *not* re-initialise
76 the state for every frame */
77 typedef struct CELTDecoder CELTDecoder;
79 /** The mode contains all the information necessary to create an encoder. Both
80 the encoder and decoder need to be initialised with exactly the same mode,
81 otherwise the quality will be very bad */
82 typedef struct CELTMode CELTMode;
85 /** \defgroup codec Encoding and decoding */
90 /** Creates a new mode struct. This will be passed to an encoder or decoder.
91 The mode MUST NOT BE DESTROYED until the encoders and decoders that use it
92 are destroyed as well.
93 @param Fs Sampling rate (32000 to 64000 Hz)
94 @param channels Number of channels
95 @param frame_size Number of samples (per channel) to encode in each packet (64 - 256)
96 @param lookahead Extra latency (in samples per channel) in addition to the frame size (between 32 and frame_size). The larger that value, the better the quality (at the expense of latency)
97 @param error Returned error code (if NULL, no error will be returned)
98 @return A newly created mode
100 CELTMode *celt_mode_create(celt_int32_t Fs, int channels, int frame_size, int lookahead, int *error);
102 /** Destroys a mode struct. Only call this after all encoders and decoders
103 using this mode are destroyed as well.
104 @param mode Mode to be destroyed
106 void celt_mode_destroy(CELTMode *mode);
108 /** Query information from a mode */
109 int celt_mode_info(const CELTMode *mode, int request, celt_int32_t *value);
115 /** Creates a new encoder state. Each stream needs its own encoder state (can't
116 be shared across simultaneous streams).
117 @param mode Contains all the information about the characteristics of the stream
118 (must be the same characteristics as used for the decoder)
119 @return Newly created encoder state.
121 CELTEncoder *celt_encoder_create(const CELTMode *mode);
123 /** Destroys a an encoder state.
124 @param st Encoder state to be destroyed
126 void celt_encoder_destroy(CELTEncoder *st);
128 /** Encodes a frame of audio.
129 @param st Encoder state
130 @param pcm PCM audio in signed 16-bit format (native endian). There must be
131 exactly frame_size samples per channel. The input data is
132 overwritten by a copy of what the remote decoder would decode.
133 @param compressed The compressed data is written here
134 @param nbCompressedBytes Number of bytes to use for compressing the frame
135 (can change from one frame to another)
136 @return Number of bytes written to "compressed". Should be the same as
137 "nbCompressedBytes" unless the stream is VBR. If negative, an error
138 has occured (see error codes). It is IMPORTANT that the length returned
139 be somehow transmitted to the decoder. Otherwise, no decoding is possible.
141 int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, int nbCompressedBytes);
146 /** Creates a new decoder state. Each stream needs its own decoder state (can't
147 be shared across simultaneous streams).
148 @param mode Contains all the information about the characteristics of the
149 stream (must be the same characteristics as used for the encoder)
150 @return Newly created decoder state.
152 CELTDecoder *celt_decoder_create(const CELTMode *mode);
154 /** Destroys a a decoder state.
155 @param st Decoder state to be destroyed
157 void celt_decoder_destroy(CELTDecoder *st);
159 /** Decodes a frame of audio.
160 @param st Decoder state
161 @param data Compressed data produced by an encoder
162 @param len Number of bytes to read from "data". This MUST be exactly the number
163 of bytes returned by the encoder. Using a larger value WILL NOT WORK.
164 @param pcm One frame (frame_size samples per channel) of decoded PCM will be
168 int celt_decode(CELTDecoder *st, unsigned char *data, int len, celt_int16_t *pcm);