celt.h 6.19 KB
Newer Older
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
/* (C) 2007 Jean-Marc Valin, CSIRO
*/
/*
   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
35
36
#include "celt_types.h"

37
38
39
40
#ifdef __cplusplus
extern "C" {
#endif

41
/* Error codes */
42
43
44
45
46
#define CELT_OK                0
#define CELT_BAD_ARG          -1
#define CELT_INVALID_MODE     -2
#define CELT_INTERNAL_ERROR   -3
#define CELT_CORRUPTED_DATA   -4
47

48
/* Requests */
Jean-Marc Valin's avatar
Jean-Marc Valin committed
49
50
#define CELT_GET_FRAME_SIZE   1000
#define CELT_GET_LOOKAHEAD    1001
51
#define CELT_GET_NB_CHANNELS  1002
52
53
54
55

/** State of the 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 */
56
typedef struct CELTEncoder CELTEncoder;
57
58
59
60

/** 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 */
61
typedef struct CELTDecoder CELTDecoder;
62

63
64
65
/** 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
66
typedef struct CELTMode CELTMode;
67

68
/* To be removed soon */
69
70
#define celt_mono celt_mode_create(44100, 1, 256, 128, NULL)
#define celt_stereo celt_mode_create(44100, 2, 256, 128, NULL)
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85


/* Mode calls */


/** 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.
 @param Fs Sampling rate (32000 to 64000 Hz)
 @param channels Number of channels
 @param frame_size Number of samples (per channel) to encode in each packet (64 - 256)
 @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)
 @param error Returned error code (if NULL, no error will be returned)
 @return A newly created mode
*/
86
CELTMode *celt_mode_create(int Fs, int channels, int frame_size, int lookahead, int *error);
87
88
89
90
91

/** 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
*/
92
void celt_mode_destroy(CELTMode *mode);
93

94
95
/** Query information from a mode */
int celt_mode_info(const CELTMode *mode, int request, celt_int32_t *value);
96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111


/* Encoder stuff */

/** 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.
*/
CELTEncoder *celt_encoder_create(const CELTMode *mode);

/** Destroys a an encoder state.
 @param mode Encoder state to be destroyed
 */
112
void celt_encoder_destroy(CELTEncoder *st);
113

114
115
116
117
118
119
120
121
122
123
124
125
126
/** 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. The input data is 
            overwritten by a copy of what the remote decoder would decode.
 @param compressed The compressed data is written here
 @param nbCompressedBytes Number of bytes to use for compressing the frame
                          (can change from one frame to another)
 @return Number of bytes written to "compressed". Should be the same as 
         "nbCompressedBytes" unless the stream is VBR. If negative, an error
         has occured (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
127
int celt_encode(CELTEncoder *st, celt_int16_t *pcm, unsigned char *compressed, int nbCompressedBytes);
128

129
130


131
132
133
134
135
136
137
138
139
140
141
142
143
/* Decoder stuff */

/** 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.
 */
CELTDecoder *celt_decoder_create(const CELTMode *mode);

/** Destroys a a decoder state.
 @param mode Decoder state to be destroyed
 */
144
145
void celt_decoder_destroy(CELTDecoder *st);

146
147
148
149
150
151
152
153
154
/** 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. 
 @return Error code.
   */
155
int celt_decode(CELTDecoder *st, unsigned char *data, int len, celt_int16_t *pcm);
156

Jean-Marc Valin's avatar
Jean-Marc Valin committed
157
158


159
160
161
162
#ifdef __cplusplus
}
#endif

163
#endif /*CELT_H */