1 /* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
19 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include "opus_types.h"
32 #include "opus_defines.h"
39 #define OPUS_BITRATE_AUTO -1
41 #define OPUS_APPLICATION_VOIP 2000
42 #define OPUS_APPLICATION_AUDIO 2001
44 #define OPUS_SIGNAL_AUTO 3000
45 #define OPUS_SIGNAL_VOICE 3001
46 #define OPUS_SIGNAL_MUSIC 3002
48 #define OPUS_BANDWIDTH_AUTO 1100
49 #define OPUS_BANDWIDTH_NARROWBAND 1101
50 #define OPUS_BANDWIDTH_MEDIUMBAND 1102
51 #define OPUS_BANDWIDTH_WIDEBAND 1103
52 #define OPUS_BANDWIDTH_SUPERWIDEBAND 1104
53 #define OPUS_BANDWIDTH_FULLBAND 1105
57 typedef struct OpusEncoder OpusEncoder;
58 typedef struct OpusDecoder OpusDecoder;
60 OPUS_EXPORT int opus_encoder_get_size(int channels);
63 * There are two coding modes:
64 * OPUS_APPLICATION_VOIP gives best quality at a given bitrate for voice
65 * signals. It enhances the input signal by high-pass filtering and
66 * emphasizing formants and harmonics. Optionally it includes in-band
67 * forward error correction to protect against packet loss. Use this
68 * mode for typical VoIP applications. Because of the enhancement,
69 * even at high bitrates the output may sound different from the input.
70 * OPUS_APPLICATION_AUDIO gives best quality at a given bitrate for most
71 * non-voice signals like music. Use this mode for music and mixed
72 * (music/voice) content, broadcast, and applications requiring less
73 * than 15 ms of coding delay.
76 /* Returns initialized encoder state */
77 OPUS_EXPORT OpusEncoder *opus_encoder_create(
78 int Fs, /* Sampling rate of input signal (Hz) */
79 int channels, /* Number of channels (1/2) in input signal */
80 int application, /* Coding mode (OPUS_APPLICATION_VOIP/OPUS_APPLICATION_AUDIO) */
81 int *error /* Error code */
84 OPUS_EXPORT int opus_encoder_init(
85 OpusEncoder *st, /* Encoder state */
86 int Fs, /* Sampling rate of input signal (Hz) */
87 int channels, /* Number of channels (1/2) in input signal */
88 int application /* Coding mode (OPUS_APPLICATION_VOIP/OPUS_APPLICATION_AUDIO) */
91 /* Returns length of the data payload (in bytes) */
92 OPUS_EXPORT int opus_encode(
93 OpusEncoder *st, /* Encoder state */
94 const opus_int16 *pcm, /* Input signal (interleaved if 2 channels). length is frame_size*channels */
95 int frame_size, /* Number of samples per frame of input signal */
96 unsigned char *data, /* Output payload (no more than max_data_bytes long) */
97 int max_data_bytes /* Allocated memory for payload; don't use for controlling bitrate */
100 /* Returns length of the data payload (in bytes) */
101 OPUS_EXPORT int opus_encode_float(
102 OpusEncoder *st, /* Encoder state */
103 const float *pcm, /* Input signal (interleaved if 2 channels). length is frame_size*channels 0dbFS range of +/-1.0*/
104 int frame_size, /* Number of samples per frame of input signal */
105 unsigned char *data, /* Output payload (no more than max_data_bytes long) */
106 int max_data_bytes /* Allocated memory for payload; don't use for controlling bitrate */
109 OPUS_EXPORT void opus_encoder_destroy(OpusEncoder *st);
111 OPUS_EXPORT int opus_encoder_ctl(OpusEncoder *st, int request, ...);
115 OPUS_EXPORT int opus_decoder_get_size(int channels);
117 OPUS_EXPORT OpusDecoder *opus_decoder_create(
118 int Fs, /* Sampling rate of output signal (Hz) */
119 int channels, /* Number of channels (1/2) in output signal */
120 int *error /* Error code*/
123 OPUS_EXPORT int opus_decoder_init(OpusDecoder *st,
124 int Fs, /* Sampling rate of output signal (Hz) */
125 int channels /* Number of channels (1/2) in output signal */
128 /* Returns the number of samples decoded or a negative error code */
129 OPUS_EXPORT int opus_decode(
130 OpusDecoder *st, /* Decoder state */
131 const unsigned char *data, /* Input payload. Use a NULL pointer to indicate packet loss */
132 int len, /* Number of bytes in payload */
133 opus_int16 *pcm, /* Output signal (interleaved if 2 channels). length is frame_size*channels */
134 int frame_size, /* Number of samples per frame of input signal */
135 int decode_fec /* Flag (0/1) to request that any in-band forward error correction data be */
136 /* decoded. If no such data is available the frame is decoded as if it were lost. */
139 /* Returns the number of samples decoded or a negative error code */
140 OPUS_EXPORT int opus_decode_float(
141 OpusDecoder *st, /* Decoder state */
142 const unsigned char *data, /* Input payload. Use a NULL pointer to indicate packet loss */
143 int len, /* Number of bytes in payload */
144 float *pcm, /* Output signal (interleaved if 2 channels). length is frame_size*channels 0dbFS range of -/+1.0*/
145 int frame_size, /* Number of samples per frame of input signal */
146 int decode_fec /* Flag (0/1) to request that any in-band forward error correction data be */
147 /* decoded. If no such data is available the frame is decoded as if it were lost. */
150 OPUS_EXPORT int opus_decoder_ctl(OpusDecoder *st, int request, ...);
152 OPUS_EXPORT void opus_decoder_destroy(OpusDecoder *st);
154 OPUS_EXPORT int opus_packet_parse(const unsigned char *data, int len,
155 unsigned char *out_toc, const unsigned char *frames[48],
156 short size[48], int *payload_offset);
158 OPUS_EXPORT int opus_packet_get_bandwidth(const unsigned char *data);
159 OPUS_EXPORT int opus_packet_get_samples_per_frame(const unsigned char *data, int Fs);
160 OPUS_EXPORT int opus_packet_get_nb_channels(const unsigned char *data);
161 OPUS_EXPORT int opus_packet_get_nb_frames(const unsigned char packet[], int len);
162 OPUS_EXPORT int opus_decoder_get_nb_samples(const OpusDecoder *dec, const unsigned char packet[], int len);
166 typedef struct OpusRepacketizer OpusRepacketizer;
168 OPUS_EXPORT int opus_repacketizer_get_size(void);
170 OPUS_EXPORT OpusRepacketizer *opus_repacketizer_init(OpusRepacketizer *rp);
172 OPUS_EXPORT OpusRepacketizer *opus_repacketizer_create(void);
174 OPUS_EXPORT void opus_repacketizer_destroy(OpusRepacketizer *rp);
176 OPUS_EXPORT int opus_repacketizer_cat(OpusRepacketizer *rp, const unsigned char *data, int len);
178 OPUS_EXPORT int opus_repacketizer_out_range(OpusRepacketizer *rp, int begin, int end, unsigned char *data, int maxlen);
180 OPUS_EXPORT int opus_repacketizer_get_nb_frames(OpusRepacketizer *rp);
182 OPUS_EXPORT int opus_repacketizer_out(OpusRepacketizer *rp, unsigned char *data, int maxlen);