summaryrefslogtreecommitdiff
path: root/src/opus.h
blob: fb49b0b1fe0fa0654a8121cc8e2a9d7db009c41d (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
   Written by Jean-Marc Valin and Koen Vos */
/*
   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.

   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 OPUS_H
#define OPUS_H

#include "opus_types.h"

#ifdef __cplusplus
extern "C" {
#endif

#if defined(__GNUC__) && defined(OPUS_BUILD)

# define OPUS_EXPORT __attribute__ ((visibility ("default")))

#elif defined(WIN32)

# ifdef OPUS_BUILD
#   define OPUS_EXPORT __declspec(dllexport)
# else
#   define OPUS_EXPORT __declspec(dllimport)
# endif

#else

# define OPUS_EXPORT

#endif

#define __opus_check_int(x) (((void)((x) == (opus_int32)0)), (opus_int32)(x))
#define __opus_check_int_ptr(ptr) ((ptr) + ((ptr) - (opus_int32*)(ptr)))
#define __opus_check_uint_ptr(ptr) ((ptr) + ((ptr) - (opus_uint32*)(ptr)))

/* Error codes */
/** No error */
#define OPUS_OK                0
/** An (or more) invalid argument (e.g. out of range) */
#define OPUS_BAD_ARG          -1
/** The mode struct passed is invalid */
#define OPUS_BUFFER_TOO_SMALL -2
/** An internal error was detected */
#define OPUS_INTERNAL_ERROR   -3
/** The data passed (e.g. compressed data to decoder) is corrupted */
#define OPUS_CORRUPTED_DATA   -4
/** Invalid/unsupported request number */
#define OPUS_UNIMPLEMENTED    -5
/** An encoder or decoder structure is invalid or already freed */
#define OPUS_INVALID_STATE    -6
/** Memory allocation has failed */
#define OPUS_ALLOC_FAIL       -7

#define OPUS_BITRATE_AUTO       -1

#define OPUS_APPLICATION_VOIP        2000
#define OPUS_APPLICATION_AUDIO       2001

#define OPUS_SIGNAL_AUTO             3000
#define OPUS_SIGNAL_VOICE            3001
#define OPUS_SIGNAL_MUSIC            3002

#define OPUS_BANDWIDTH_AUTO          1100
#define OPUS_BANDWIDTH_NARROWBAND    1101
#define OPUS_BANDWIDTH_MEDIUMBAND    1102
#define OPUS_BANDWIDTH_WIDEBAND      1103
#define OPUS_BANDWIDTH_SUPERWIDEBAND 1104
#define OPUS_BANDWIDTH_FULLBAND      1105


/* OPUS_APPLICATION_VOIP or OPUS_APPLICATION_AUDIO */
#define OPUS_SET_APPLICATION_REQUEST 0
#define OPUS_SET_APPLICATION(x) OPUS_SET_APPLICATION_REQUEST, __opus_check_int(x)
#define OPUS_GET_APPLICATION_REQUEST 1
#define OPUS_GET_APPLICATION(x) OPUS_GET_APPLICATION_REQUEST, __opus_check_int_ptr(x)

/* Coding bit-rate in bit/second */
#define OPUS_SET_BITRATE_REQUEST 2
#define OPUS_SET_BITRATE(x) OPUS_SET_BITRATE_REQUEST, __opus_check_int(x)
#define OPUS_GET_BITRATE_REQUEST 3
#define OPUS_GET_BITRATE(x) OPUS_GET_BITRATE_REQUEST, __opus_check_int_ptr(x)

/* 0 for CBR, 1 for VBR */
#define OPUS_SET_VBR_REQUEST 6
#define OPUS_SET_VBR(x) OPUS_SET_VBR_REQUEST, __opus_check_int(x)
#define OPUS_GET_VBR_REQUEST 7
#define OPUS_GET_VBR(x) OPUS_GET_VBR_REQUEST, __opus_check_int_ptr(x)

#define OPUS_SET_BANDWIDTH_REQUEST 8
#define OPUS_SET_BANDWIDTH(x) OPUS_SET_BANDWIDTH_REQUEST, __opus_check_int(x)
#define OPUS_GET_BANDWIDTH_REQUEST 9
#define OPUS_GET_BANDWIDTH(x) OPUS_GET_BANDWIDTH_REQUEST, __opus_check_int_ptr(x)

#define OPUS_SET_COMPLEXITY_REQUEST 10
#define OPUS_SET_COMPLEXITY(x) OPUS_SET_COMPLEXITY_REQUEST, __opus_check_int(x)
#define OPUS_GET_COMPLEXITY_REQUEST 11
#define OPUS_GET_COMPLEXITY(x) OPUS_GET_COMPLEXITY_REQUEST, __opus_check_int_ptr(x)

#define OPUS_SET_INBAND_FEC_REQUEST 12
#define OPUS_SET_INBAND_FEC(x) OPUS_SET_INBAND_FEC_REQUEST, __opus_check_int(x)
#define OPUS_GET_INBAND_FEC_REQUEST 13
#define OPUS_GET_INBAND_FEC(x) OPUS_GET_INBAND_FEC_REQUEST, __opus_check_int_ptr(x)

#define OPUS_SET_PACKET_LOSS_PERC_REQUEST 14
#define OPUS_SET_PACKET_LOSS_PERC(x) OPUS_SET_PACKET_LOSS_PERC_REQUEST, __opus_check_int(x)
#define OPUS_GET_PACKET_LOSS_PERC_REQUEST 15
#define OPUS_GET_PACKET_LOSS_PERC(x) OPUS_GET_PACKET_LOSS_PERC_REQUEST, __opus_check_int_ptr(x)

#define OPUS_SET_DTX_REQUEST 16
#define OPUS_SET_DTX(x) OPUS_SET_DTX_REQUEST, __opus_check_int(x)
#define OPUS_GET_DTX_REQUEST 17
#define OPUS_GET_DTX(x) OPUS_GET_DTX_REQUEST, __opus_check_int_ptr(x)

#define OPUS_SET_VOICE_RATIO_REQUEST 18
#define OPUS_SET_VOICE_RATIO(x) OPUS_SET_VOICE_RATIO_REQUEST, __opus_check_int(x)
#define OPUS_GET_VOICE_RATIO_REQUEST 19
#define OPUS_GET_VOICE_RATIO(x) OPUS_GET_VOICE_RATIO_REQUEST, __opus_check_int_ptr(x)

#define OPUS_SET_VBR_CONSTRAINT_REQUEST 20
#define OPUS_SET_VBR_CONSTRAINT(x) OPUS_SET_VBR_CONSTRAINT_REQUEST, __opus_check_int(x)
#define OPUS_GET_VBR_CONSTRAINT_REQUEST 21
#define OPUS_GET_VBR_CONSTRAINT(x) OPUS_GET_VBR_CONSTRAINT_REQUEST, __opus_check_int_ptr(x)

#define OPUS_SET_FORCE_MONO_REQUEST 22
#define OPUS_SET_FORCE_MONO(x) OPUS_SET_FORCE_MONO_REQUEST, __opus_check_int(x)
#define OPUS_GET_FORCE_MONO_REQUEST 23
#define OPUS_GET_FORCE_MONO(x) OPUS_GET_FORCE_MONO_REQUEST, __opus_check_int_ptr(x)

#define OPUS_SET_SIGNAL_REQUEST 24
#define OPUS_SET_SIGNAL(x) OPUS_SET_SIGNAL_REQUEST, __opus_check_int(x)
#define OPUS_GET_SIGNAL_REQUEST 25
#define OPUS_GET_SIGNAL(x) OPUS_GET_SIGNAL_REQUEST, __opus_check_int_ptr(x)

#define OPUS_GET_LOOKAHEAD_REQUEST 27
#define OPUS_GET_LOOKAHEAD(x) OPUS_GET_LOOKAHEAD_REQUEST, __opus_check_int_ptr(x)

/* For testing purposes: the encoder and decoder state should
   always be identical after coding a payload */
#define OPUS_GET_FINAL_RANGE_REQUEST 29
#define OPUS_GET_FINAL_RANGE(x) OPUS_GET_FINAL_RANGE_REQUEST, __opus_check_uint_ptr(x)

typedef struct OpusEncoder OpusEncoder;
typedef struct OpusDecoder OpusDecoder;

OPUS_EXPORT int opus_encoder_get_size(int channels);

/*
 * There are two coding modes:
 * OPUS_APPLICATION_VOIP gives best quality at a given bitrate for voice
 *    signals. It enhances the  input signal by high-pass filtering and
 *    emphasizing formants and harmonics. Optionally  it includes in-band
 *    forward error correction to protect against packet loss. Use this
 *    mode for typical VoIP applications. Because of the enhancement,
 *    even at high bitrates the output may sound different from the input.
 * OPUS_APPLICATION_AUDIO gives best quality at a given bitrate for most
 *    non-voice signals like music. Use this mode for music and mixed
 *    (music/voice) content, broadcast, and applications requiring less
 *    than 15 ms of coding delay.
 */

/* Returns initialized encoder state */
OPUS_EXPORT OpusEncoder *opus_encoder_create(
    int Fs,                     /* Sampling rate of input signal (Hz) */
    int channels,               /* Number of channels (1/2) in input signal */
    int application,            /* Coding mode (OPUS_APPLICATION_VOIP/OPUS_APPLICATION_AUDIO) */
    int *error                  /* Error code */
);

OPUS_EXPORT int opus_encoder_init(
    OpusEncoder *st,            /* Encoder state */
    int Fs,                     /* Sampling rate of input signal (Hz) */
    int channels,               /* Number of channels (1/2) in input signal */
    int application             /* Coding mode (OPUS_APPLICATION_VOIP/OPUS_APPLICATION_AUDIO) */
);

/* Returns length of the data payload (in bytes) */
OPUS_EXPORT int opus_encode(
    OpusEncoder *st,            /* Encoder state */
    const opus_int16 *pcm,      /* Input signal (interleaved if 2 channels). length is frame_size*channels */
    int frame_size,             /* Number of samples per frame of input signal */
    unsigned char *data,        /* Output payload (no more than max_data_bytes long) */
    int max_data_bytes          /* Allocated memory for payload; don't use for controlling bitrate */
);

/* Returns length of the data payload (in bytes) */
OPUS_EXPORT int opus_encode_float(
    OpusEncoder *st,            /* Encoder state */
    const float *pcm,           /* Input signal (interleaved if 2 channels). length is frame_size*channels 0dbFS range of +/-1.0*/
    int frame_size,             /* Number of samples per frame of input signal */
    unsigned char *data,        /* Output payload (no more than max_data_bytes long) */
    int max_data_bytes          /* Allocated memory for payload; don't use for controlling bitrate */
);

OPUS_EXPORT void opus_encoder_destroy(OpusEncoder *st);

OPUS_EXPORT int opus_encoder_ctl(OpusEncoder *st, int request, ...);



OPUS_EXPORT int opus_decoder_get_size(int channels);

OPUS_EXPORT OpusDecoder *opus_decoder_create(
    int Fs,                     /* Sampling rate of output signal (Hz) */
    int channels,               /* Number of channels (1/2) in output signal */
    int *error                  /* Error code*/
);

OPUS_EXPORT int opus_decoder_init(OpusDecoder *st,
    int Fs,                     /* Sampling rate of output signal (Hz) */
    int channels                /* Number of channels (1/2) in output signal */
);

/* Returns the number of samples decoded or a negative error code */
OPUS_EXPORT int opus_decode(
    OpusDecoder *st,            /* Decoder state */
    const unsigned char *data,  /* Input payload. Use a NULL pointer to indicate packet loss */
    int len,                    /* Number of bytes in payload */
    opus_int16 *pcm,            /* Output signal (interleaved if 2 channels). length is frame_size*channels */
    int frame_size,             /* Number of samples per frame of input signal */
    int decode_fec              /* Flag (0/1) to request that any in-band forward error correction data be */
                                /* decoded. If no such data is available the frame is decoded as if it were lost. */
);

/* Returns the number of samples decoded or a negative error code */
OPUS_EXPORT int opus_decode_float(
    OpusDecoder *st,            /* Decoder state */
    const unsigned char *data,  /* Input payload. Use a NULL pointer to indicate packet loss */
    int len,                    /* Number of bytes in payload */
    float *pcm,                 /* Output signal (interleaved if 2 channels). length is frame_size*channels 0dbFS range of -/+1.0*/
    int frame_size,             /* Number of samples per frame of input signal */
    int decode_fec              /* Flag (0/1) to request that any in-band forward error correction data be */
                                /* decoded. If no such data is available the frame is decoded as if it were lost. */
);

OPUS_EXPORT int opus_decoder_ctl(OpusDecoder *st, int request, ...);

OPUS_EXPORT void opus_decoder_destroy(OpusDecoder *st);

OPUS_EXPORT int opus_packet_parse(const unsigned char *data, int len,
      unsigned char *out_toc, const unsigned char *frames[48],
      short size[48], int *payload_offset);

OPUS_EXPORT int opus_packet_get_bandwidth(const unsigned char *data);
OPUS_EXPORT int opus_packet_get_samples_per_frame(const unsigned char *data, int Fs);
OPUS_EXPORT int opus_packet_get_nb_channels(const unsigned char *data);
OPUS_EXPORT int opus_packet_get_nb_frames(const unsigned char packet[], int len);
OPUS_EXPORT int opus_decoder_get_nb_samples(const OpusDecoder *dec, const unsigned char packet[], int len);

OPUS_EXPORT const char *opus_strerror(int error);

OPUS_EXPORT const char *opus_get_version_string(void);

/* Repacketizer */
typedef struct OpusRepacketizer OpusRepacketizer;

OPUS_EXPORT int opus_repacketizer_get_size(void);

OPUS_EXPORT OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp);

OPUS_EXPORT OpusRepacketizer *opus_repacketizer_create(void);

OPUS_EXPORT void opus_repacketizer_destroy(OpusRepacketizer *rp);

OPUS_EXPORT int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, int len);

OPUS_EXPORT int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsigned char *data, int maxlen);

OPUS_EXPORT int opus_repacketizer_get_nb_frames(OpusRepacketizer *rp);

OPUS_EXPORT int opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, int maxlen);

#ifdef __cplusplus
}
#endif

#endif /* OPUS_H */