blob: 0909ef869f2708317aef3f4fe12a4fedac4e1e98 [file] [log] [blame]
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_CODECS_OPUS_OPUS_INTERFACE_H_
12#define MODULES_AUDIO_CODING_CODECS_OPUS_OPUS_INTERFACE_H_
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000013
Peter Kastingdce40cf2015-08-24 14:52:23 -070014#include <stddef.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "typedefs.h"
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000017
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22// Opaque wrapper types for the codec state.
23typedef struct WebRtcOpusEncInst OpusEncInst;
24typedef struct WebRtcOpusDecInst OpusDecInst;
25
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000026/****************************************************************************
27 * WebRtcOpus_EncoderCreate(...)
28 *
29 * This function create an Opus encoder.
30 *
31 * Input:
32 * - channels : number of channels.
33 * - application : 0 - VOIP applications.
34 * Favor speech intelligibility.
35 * 1 - Audio applications.
36 * Favor faithfulness to the original input.
37 *
38 * Output:
39 * - inst : a pointer to Encoder context that is created
40 * if success.
41 *
42 * Return value : 0 - Success
43 * -1 - Error
44 */
45int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
Peter Kasting69558702016-01-12 16:26:35 -080046 size_t channels,
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000047 int32_t application);
48
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000049int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst);
50
51/****************************************************************************
52 * WebRtcOpus_Encode(...)
53 *
54 * This function encodes audio as a series of Opus frames and inserts
55 * it into a packet. Input buffer can be any length.
56 *
57 * Input:
58 * - inst : Encoder context
59 * - audio_in : Input speech data buffer
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +000060 * - samples : Samples per channel in audio_in
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000061 * - length_encoded_buffer : Output buffer size
62 *
63 * Output:
64 * - encoded : Output compressed data buffer
65 *
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000066 * Return value : >=0 - Length (in bytes) of coded data
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000067 * -1 - Error
68 */
Peter Kastingbba78072015-06-11 19:02:46 -070069int WebRtcOpus_Encode(OpusEncInst* inst,
70 const int16_t* audio_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -070071 size_t samples,
72 size_t length_encoded_buffer,
Peter Kastingbba78072015-06-11 19:02:46 -070073 uint8_t* encoded);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000074
75/****************************************************************************
76 * WebRtcOpus_SetBitRate(...)
77 *
78 * This function adjusts the target bitrate of the encoder.
79 *
80 * Input:
81 * - inst : Encoder context
82 * - rate : New target bitrate
83 *
84 * Return value : 0 - Success
85 * -1 - Error
86 */
87int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate);
88
minyue@webrtc.org04546882014-03-07 08:55:48 +000089/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +000090 * WebRtcOpus_SetPacketLossRate(...)
91 *
92 * This function configures the encoder's expected packet loss percentage.
93 *
94 * Input:
95 * - inst : Encoder context
96 * - loss_rate : loss percentage in the range 0-100, inclusive.
97 * Return value : 0 - Success
98 * -1 - Error
99 */
100int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate);
101
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000102/****************************************************************************
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000103 * WebRtcOpus_SetMaxPlaybackRate(...)
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000104 *
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000105 * Configures the maximum playback rate for encoding. Due to hardware
106 * limitations, the receiver may render audio up to a playback rate. Opus
107 * encoder can use this information to optimize for network usage and encoding
108 * complexity. This will affect the audio bandwidth in the coded audio. However,
109 * the input/output sample rate is not affected.
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000110 *
111 * Input:
112 * - inst : Encoder context
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000113 * - frequency_hz : Maximum playback rate in Hz.
114 * This parameter can take any value. The relation
115 * between the value and the Opus internal mode is
116 * as following:
117 * frequency_hz <= 8000 narrow band
118 * 8000 < frequency_hz <= 12000 medium band
119 * 12000 < frequency_hz <= 16000 wide band
120 * 16000 < frequency_hz <= 24000 super wide band
121 * frequency_hz > 24000 full band
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000122 * Return value : 0 - Success
123 * -1 - Error
124 */
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000125int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000126
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000127/* TODO(minyue): Check whether an API to check the FEC and the packet loss rate
128 * is needed. It might not be very useful since there are not many use cases and
129 * the caller can always maintain the states. */
130
131/****************************************************************************
132 * WebRtcOpus_EnableFec()
133 *
134 * This function enables FEC for encoding.
135 *
136 * Input:
137 * - inst : Encoder context
138 *
139 * Return value : 0 - Success
140 * -1 - Error
141 */
142int16_t WebRtcOpus_EnableFec(OpusEncInst* inst);
143
144/****************************************************************************
145 * WebRtcOpus_DisableFec()
146 *
147 * This function disables FEC for encoding.
148 *
149 * Input:
150 * - inst : Encoder context
151 *
152 * Return value : 0 - Success
153 * -1 - Error
154 */
155int16_t WebRtcOpus_DisableFec(OpusEncInst* inst);
156
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000157/****************************************************************************
158 * WebRtcOpus_EnableDtx()
159 *
160 * This function enables Opus internal DTX for encoding.
161 *
162 * Input:
163 * - inst : Encoder context
164 *
165 * Return value : 0 - Success
166 * -1 - Error
167 */
168int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst);
169
170/****************************************************************************
171 * WebRtcOpus_DisableDtx()
172 *
173 * This function disables Opus internal DTX for encoding.
174 *
175 * Input:
176 * - inst : Encoder context
177 *
178 * Return value : 0 - Success
179 * -1 - Error
180 */
181int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst);
182
soren28dc2852017-04-06 05:48:36 -0700183/****************************************************************************
184 * WebRtcOpus_EnableCbr()
185 *
186 * This function enables CBR for encoding.
187 *
188 * Input:
189 * - inst : Encoder context
190 *
191 * Return value : 0 - Success
192 * -1 - Error
193 */
194int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst);
195
196/****************************************************************************
197 * WebRtcOpus_DisableCbr()
198 *
199 * This function disables CBR for encoding.
200 *
201 * Input:
202 * - inst : Encoder context
203 *
204 * Return value : 0 - Success
205 * -1 - Error
206 */
207int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst);
208
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000209/*
minyue@webrtc.org04546882014-03-07 08:55:48 +0000210 * WebRtcOpus_SetComplexity(...)
211 *
212 * This function adjusts the computational complexity. The effect is the same as
213 * calling the complexity setting of Opus as an Opus encoder related CTL.
214 *
215 * Input:
216 * - inst : Encoder context
217 * - complexity : New target complexity (0-10, inclusive)
218 *
219 * Return value : 0 - Success
220 * -1 - Error
221 */
222int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity);
223
minyuec8299f92016-09-27 02:08:47 -0700224/*
225 * WebRtcOpus_SetForceChannels(...)
226 *
227 * If the encoder is initialized as a stereo encoder, Opus will by default
228 * decide whether to encode in mono or stereo based on the bitrate. This
229 * function overrules the previous setting, and forces the encoder to encode
230 * in auto/mono/stereo.
231 *
232 * If the Encoder is initialized as a mono encoder, and one tries to force
233 * stereo, the function will return an error.
234 *
235 * Input:
236 * - inst : Encoder context
237 * - num_channels : 0 - Not forced
238 * 1 - Mono
239 * 2 - Stereo
240 *
241 * Return value : 0 - Success
242 * -1 - Error
243 */
minyue41b9c802016-10-06 07:13:54 -0700244int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels);
minyuec8299f92016-09-27 02:08:47 -0700245
Peter Kasting69558702016-01-12 16:26:35 -0800246int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, size_t channels);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000247int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst);
248
249/****************************************************************************
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000250 * WebRtcOpus_DecoderChannels(...)
251 *
252 * This function returns the number of channels created for Opus decoder.
253 */
Peter Kasting69558702016-01-12 16:26:35 -0800254size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000255
256/****************************************************************************
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000257 * WebRtcOpus_DecoderInit(...)
258 *
259 * This function resets state of the decoder.
260 *
261 * Input:
262 * - inst : Decoder context
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000263 */
Karl Wiberg43766482015-08-27 15:22:11 +0200264void WebRtcOpus_DecoderInit(OpusDecInst* inst);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000265
266/****************************************************************************
267 * WebRtcOpus_Decode(...)
268 *
269 * This function decodes an Opus packet into one or more audio frames at the
270 * ACM interface's sampling rate (32 kHz).
271 *
272 * Input:
273 * - inst : Decoder context
274 * - encoded : Encoded data
275 * - encoded_bytes : Bytes in encoded vector
276 *
277 * Output:
278 * - decoded : The decoded vector
279 * - audio_type : 1 normal, 2 CNG (for Opus it should
280 * always return 1 since we're not using Opus's
281 * built-in DTX/CNG scheme)
282 *
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +0000283 * Return value : >0 - Samples per channel in decoded vector
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000284 * -1 - Error
285 */
Peter Kastingbba78072015-06-11 19:02:46 -0700286int WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700287 size_t encoded_bytes, int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700288 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000289
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000290/****************************************************************************
291 * WebRtcOpus_DecodePlc(...)
292 *
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000293 * This function processes PLC for opus frame(s).
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000294 * Input:
295 * - inst : Decoder context
296 * - number_of_lost_frames : Number of PLC frames to produce
297 *
298 * Output:
299 * - decoded : The decoded vector
300 *
301 * Return value : >0 - number of samples in decoded PLC vector
302 * -1 - Error
303 */
Peter Kastingbba78072015-06-11 19:02:46 -0700304int WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded,
305 int number_of_lost_frames);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000306
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000307/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000308 * WebRtcOpus_DecodeFec(...)
309 *
310 * This function decodes the FEC data from an Opus packet into one or more audio
311 * frames at the ACM interface's sampling rate (32 kHz).
312 *
313 * Input:
314 * - inst : Decoder context
315 * - encoded : Encoded data
316 * - encoded_bytes : Bytes in encoded vector
317 *
318 * Output:
319 * - decoded : The decoded vector (previous frame)
320 *
321 * Return value : >0 - Samples per channel in decoded vector
322 * 0 - No FEC data in the packet
323 * -1 - Error
324 */
Peter Kastingbba78072015-06-11 19:02:46 -0700325int WebRtcOpus_DecodeFec(OpusDecInst* inst, const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700326 size_t encoded_bytes, int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700327 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000328
329/****************************************************************************
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000330 * WebRtcOpus_DurationEst(...)
331 *
332 * This function calculates the duration of an opus packet.
333 * Input:
334 * - inst : Decoder context
335 * - payload : Encoded data pointer
336 * - payload_length_bytes : Bytes of encoded data
337 *
Minyue323b1322015-05-25 13:49:37 +0200338 * Return value : The duration of the packet, in samples per
339 * channel.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000340 */
341int WebRtcOpus_DurationEst(OpusDecInst* inst,
342 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700343 size_t payload_length_bytes);
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000344
minyuel6d92bf52015-09-23 15:20:39 +0200345/****************************************************************************
346 * WebRtcOpus_PlcDuration(...)
347 *
348 * This function calculates the duration of a frame returned by packet loss
349 * concealment (PLC).
350 *
351 * Input:
352 * - inst : Decoder context
353 *
354 * Return value : The duration of a frame returned by PLC, in
355 * samples per channel.
356 */
357int WebRtcOpus_PlcDuration(OpusDecInst* inst);
358
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000359/* TODO(minyue): Check whether it is needed to add a decoder context to the
360 * arguments, like WebRtcOpus_DurationEst(...). In fact, the packet itself tells
361 * the duration. The decoder context in WebRtcOpus_DurationEst(...) is not used.
362 * So it may be advisable to remove it from WebRtcOpus_DurationEst(...). */
363
364/****************************************************************************
365 * WebRtcOpus_FecDurationEst(...)
366 *
367 * This function calculates the duration of the FEC data within an opus packet.
368 * Input:
369 * - payload : Encoded data pointer
370 * - payload_length_bytes : Bytes of encoded data
371 *
372 * Return value : >0 - The duration of the FEC data in the
Minyue323b1322015-05-25 13:49:37 +0200373 * packet in samples per channel.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000374 * 0 - No FEC data in the packet.
375 */
376int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700377 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000378
379/****************************************************************************
380 * WebRtcOpus_PacketHasFec(...)
381 *
382 * This function detects if an opus packet has FEC.
383 * Input:
384 * - payload : Encoded data pointer
385 * - payload_length_bytes : Bytes of encoded data
386 *
387 * Return value : 0 - the packet does NOT contain FEC.
388 * 1 - the packet contains FEC.
389 */
390int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700391 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000392
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000393#ifdef __cplusplus
394} // extern "C"
395#endif
396
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200397#endif // MODULES_AUDIO_CODING_CODECS_OPUS_OPUS_INTERFACE_H_