blob: 0b1c64d62f4d15eb93803e0e7dbdcb7f254c9166 [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
Alex Luebseeb27652017-11-20 11:13:56 -080016#include "modules/audio_coding/codecs/opus/opus_inst.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020017#include "typedefs.h" // NOLINT(build/include)
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000018
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23// Opaque wrapper types for the codec state.
24typedef struct WebRtcOpusEncInst OpusEncInst;
25typedef struct WebRtcOpusDecInst OpusDecInst;
26
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000027/****************************************************************************
28 * WebRtcOpus_EncoderCreate(...)
29 *
30 * This function create an Opus encoder.
31 *
32 * Input:
33 * - channels : number of channels.
34 * - application : 0 - VOIP applications.
35 * Favor speech intelligibility.
36 * 1 - Audio applications.
37 * Favor faithfulness to the original input.
38 *
39 * Output:
40 * - inst : a pointer to Encoder context that is created
41 * if success.
42 *
43 * Return value : 0 - Success
44 * -1 - Error
45 */
46int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
Peter Kasting69558702016-01-12 16:26:35 -080047 size_t channels,
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000048 int32_t application);
49
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000050int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst);
51
52/****************************************************************************
53 * WebRtcOpus_Encode(...)
54 *
55 * This function encodes audio as a series of Opus frames and inserts
56 * it into a packet. Input buffer can be any length.
57 *
58 * Input:
59 * - inst : Encoder context
60 * - audio_in : Input speech data buffer
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +000061 * - samples : Samples per channel in audio_in
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000062 * - length_encoded_buffer : Output buffer size
63 *
64 * Output:
65 * - encoded : Output compressed data buffer
66 *
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000067 * Return value : >=0 - Length (in bytes) of coded data
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000068 * -1 - Error
69 */
Peter Kastingbba78072015-06-11 19:02:46 -070070int WebRtcOpus_Encode(OpusEncInst* inst,
71 const int16_t* audio_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -070072 size_t samples,
73 size_t length_encoded_buffer,
Peter Kastingbba78072015-06-11 19:02:46 -070074 uint8_t* encoded);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000075
76/****************************************************************************
77 * WebRtcOpus_SetBitRate(...)
78 *
79 * This function adjusts the target bitrate of the encoder.
80 *
81 * Input:
82 * - inst : Encoder context
83 * - rate : New target bitrate
84 *
85 * Return value : 0 - Success
86 * -1 - Error
87 */
88int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate);
89
minyue@webrtc.org04546882014-03-07 08:55:48 +000090/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +000091 * WebRtcOpus_SetPacketLossRate(...)
92 *
93 * This function configures the encoder's expected packet loss percentage.
94 *
95 * Input:
96 * - inst : Encoder context
97 * - loss_rate : loss percentage in the range 0-100, inclusive.
98 * Return value : 0 - Success
99 * -1 - Error
100 */
101int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate);
102
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000103/****************************************************************************
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000104 * WebRtcOpus_SetMaxPlaybackRate(...)
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000105 *
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000106 * Configures the maximum playback rate for encoding. Due to hardware
107 * limitations, the receiver may render audio up to a playback rate. Opus
108 * encoder can use this information to optimize for network usage and encoding
109 * complexity. This will affect the audio bandwidth in the coded audio. However,
110 * the input/output sample rate is not affected.
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000111 *
112 * Input:
113 * - inst : Encoder context
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000114 * - frequency_hz : Maximum playback rate in Hz.
115 * This parameter can take any value. The relation
116 * between the value and the Opus internal mode is
117 * as following:
118 * frequency_hz <= 8000 narrow band
119 * 8000 < frequency_hz <= 12000 medium band
120 * 12000 < frequency_hz <= 16000 wide band
121 * 16000 < frequency_hz <= 24000 super wide band
122 * frequency_hz > 24000 full band
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000123 * Return value : 0 - Success
124 * -1 - Error
125 */
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000126int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000127
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000128/* TODO(minyue): Check whether an API to check the FEC and the packet loss rate
129 * is needed. It might not be very useful since there are not many use cases and
130 * the caller can always maintain the states. */
131
132/****************************************************************************
133 * WebRtcOpus_EnableFec()
134 *
135 * This function enables FEC for encoding.
136 *
137 * Input:
138 * - inst : Encoder context
139 *
140 * Return value : 0 - Success
141 * -1 - Error
142 */
143int16_t WebRtcOpus_EnableFec(OpusEncInst* inst);
144
145/****************************************************************************
146 * WebRtcOpus_DisableFec()
147 *
148 * This function disables FEC for encoding.
149 *
150 * Input:
151 * - inst : Encoder context
152 *
153 * Return value : 0 - Success
154 * -1 - Error
155 */
156int16_t WebRtcOpus_DisableFec(OpusEncInst* inst);
157
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000158/****************************************************************************
159 * WebRtcOpus_EnableDtx()
160 *
161 * This function enables Opus internal DTX for encoding.
162 *
163 * Input:
164 * - inst : Encoder context
165 *
166 * Return value : 0 - Success
167 * -1 - Error
168 */
169int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst);
170
171/****************************************************************************
172 * WebRtcOpus_DisableDtx()
173 *
174 * This function disables Opus internal DTX for encoding.
175 *
176 * Input:
177 * - inst : Encoder context
178 *
179 * Return value : 0 - Success
180 * -1 - Error
181 */
182int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst);
183
soren28dc2852017-04-06 05:48:36 -0700184/****************************************************************************
185 * WebRtcOpus_EnableCbr()
186 *
187 * This function enables CBR for encoding.
188 *
189 * Input:
190 * - inst : Encoder context
191 *
192 * Return value : 0 - Success
193 * -1 - Error
194 */
195int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst);
196
197/****************************************************************************
198 * WebRtcOpus_DisableCbr()
199 *
200 * This function disables CBR for encoding.
201 *
202 * Input:
203 * - inst : Encoder context
204 *
205 * Return value : 0 - Success
206 * -1 - Error
207 */
208int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst);
209
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000210/*
minyue@webrtc.org04546882014-03-07 08:55:48 +0000211 * WebRtcOpus_SetComplexity(...)
212 *
213 * This function adjusts the computational complexity. The effect is the same as
214 * calling the complexity setting of Opus as an Opus encoder related CTL.
215 *
216 * Input:
217 * - inst : Encoder context
218 * - complexity : New target complexity (0-10, inclusive)
219 *
220 * Return value : 0 - Success
221 * -1 - Error
222 */
223int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity);
224
minyuec8299f92016-09-27 02:08:47 -0700225/*
Alex Luebseeb27652017-11-20 11:13:56 -0800226 * WebRtcOpus_GetBandwidth(...)
227 *
228 * This function returns the current bandwidth.
229 *
230 * Input:
231 * - inst : Encoder context
232 *
233 * Return value : Bandwidth - Success
234 * -1 - Error
235 */
236int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst);
237
238/*
239 * WebRtcOpus_SetBandwidth(...)
240 *
241 * By default Opus decides which bandwidth to encode the signal in depending on
242 * the the bitrate. This function overrules the previous setting and forces the
243 * encoder to encode in narrowband/wideband/fullband/etc.
244 *
245 * Input:
246 * - inst : Encoder context
247 * - bandwidth : New target bandwidth. Valid values are:
248 * OPUS_BANDWIDTH_NARROWBAND
249 * OPUS_BANDWIDTH_MEDIUMBAND
250 * OPUS_BANDWIDTH_WIDEBAND
251 * OPUS_BANDWIDTH_SUPERWIDEBAND
252 * OPUS_BANDWIDTH_FULLBAND
253 *
254 * Return value : 0 - Success
255 * -1 - Error
256 */
257int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth);
258
259/*
minyuec8299f92016-09-27 02:08:47 -0700260 * WebRtcOpus_SetForceChannels(...)
261 *
262 * If the encoder is initialized as a stereo encoder, Opus will by default
263 * decide whether to encode in mono or stereo based on the bitrate. This
264 * function overrules the previous setting, and forces the encoder to encode
265 * in auto/mono/stereo.
266 *
267 * If the Encoder is initialized as a mono encoder, and one tries to force
268 * stereo, the function will return an error.
269 *
270 * Input:
271 * - inst : Encoder context
272 * - num_channels : 0 - Not forced
273 * 1 - Mono
274 * 2 - Stereo
275 *
276 * Return value : 0 - Success
277 * -1 - Error
278 */
minyue41b9c802016-10-06 07:13:54 -0700279int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels);
minyuec8299f92016-09-27 02:08:47 -0700280
Peter Kasting69558702016-01-12 16:26:35 -0800281int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, size_t channels);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000282int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst);
283
284/****************************************************************************
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000285 * WebRtcOpus_DecoderChannels(...)
286 *
287 * This function returns the number of channels created for Opus decoder.
288 */
Peter Kasting69558702016-01-12 16:26:35 -0800289size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000290
291/****************************************************************************
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000292 * WebRtcOpus_DecoderInit(...)
293 *
294 * This function resets state of the decoder.
295 *
296 * Input:
297 * - inst : Decoder context
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000298 */
Karl Wiberg43766482015-08-27 15:22:11 +0200299void WebRtcOpus_DecoderInit(OpusDecInst* inst);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000300
301/****************************************************************************
302 * WebRtcOpus_Decode(...)
303 *
304 * This function decodes an Opus packet into one or more audio frames at the
305 * ACM interface's sampling rate (32 kHz).
306 *
307 * Input:
308 * - inst : Decoder context
309 * - encoded : Encoded data
310 * - encoded_bytes : Bytes in encoded vector
311 *
312 * Output:
313 * - decoded : The decoded vector
314 * - audio_type : 1 normal, 2 CNG (for Opus it should
315 * always return 1 since we're not using Opus's
316 * built-in DTX/CNG scheme)
317 *
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +0000318 * Return value : >0 - Samples per channel in decoded vector
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000319 * -1 - Error
320 */
Yves Gerey665174f2018-06-19 15:03:05 +0200321int WebRtcOpus_Decode(OpusDecInst* inst,
322 const uint8_t* encoded,
323 size_t encoded_bytes,
324 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700325 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000326
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000327/****************************************************************************
328 * WebRtcOpus_DecodePlc(...)
329 *
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000330 * This function processes PLC for opus frame(s).
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000331 * Input:
332 * - inst : Decoder context
333 * - number_of_lost_frames : Number of PLC frames to produce
334 *
335 * Output:
336 * - decoded : The decoded vector
337 *
338 * Return value : >0 - number of samples in decoded PLC vector
339 * -1 - Error
340 */
Yves Gerey665174f2018-06-19 15:03:05 +0200341int WebRtcOpus_DecodePlc(OpusDecInst* inst,
342 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700343 int number_of_lost_frames);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000344
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000345/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000346 * WebRtcOpus_DecodeFec(...)
347 *
348 * This function decodes the FEC data from an Opus packet into one or more audio
349 * frames at the ACM interface's sampling rate (32 kHz).
350 *
351 * Input:
352 * - inst : Decoder context
353 * - encoded : Encoded data
354 * - encoded_bytes : Bytes in encoded vector
355 *
356 * Output:
357 * - decoded : The decoded vector (previous frame)
358 *
359 * Return value : >0 - Samples per channel in decoded vector
360 * 0 - No FEC data in the packet
361 * -1 - Error
362 */
Yves Gerey665174f2018-06-19 15:03:05 +0200363int WebRtcOpus_DecodeFec(OpusDecInst* inst,
364 const uint8_t* encoded,
365 size_t encoded_bytes,
366 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700367 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000368
369/****************************************************************************
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000370 * WebRtcOpus_DurationEst(...)
371 *
372 * This function calculates the duration of an opus packet.
373 * Input:
374 * - inst : Decoder context
375 * - payload : Encoded data pointer
376 * - payload_length_bytes : Bytes of encoded data
377 *
Minyue323b1322015-05-25 13:49:37 +0200378 * Return value : The duration of the packet, in samples per
379 * channel.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000380 */
381int WebRtcOpus_DurationEst(OpusDecInst* inst,
382 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700383 size_t payload_length_bytes);
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000384
minyuel6d92bf52015-09-23 15:20:39 +0200385/****************************************************************************
386 * WebRtcOpus_PlcDuration(...)
387 *
388 * This function calculates the duration of a frame returned by packet loss
389 * concealment (PLC).
390 *
391 * Input:
392 * - inst : Decoder context
393 *
394 * Return value : The duration of a frame returned by PLC, in
395 * samples per channel.
396 */
397int WebRtcOpus_PlcDuration(OpusDecInst* inst);
398
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000399/* TODO(minyue): Check whether it is needed to add a decoder context to the
400 * arguments, like WebRtcOpus_DurationEst(...). In fact, the packet itself tells
401 * the duration. The decoder context in WebRtcOpus_DurationEst(...) is not used.
402 * So it may be advisable to remove it from WebRtcOpus_DurationEst(...). */
403
404/****************************************************************************
405 * WebRtcOpus_FecDurationEst(...)
406 *
407 * This function calculates the duration of the FEC data within an opus packet.
408 * Input:
409 * - payload : Encoded data pointer
410 * - payload_length_bytes : Bytes of encoded data
411 *
412 * Return value : >0 - The duration of the FEC data in the
Minyue323b1322015-05-25 13:49:37 +0200413 * packet in samples per channel.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000414 * 0 - No FEC data in the packet.
415 */
416int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700417 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000418
419/****************************************************************************
420 * WebRtcOpus_PacketHasFec(...)
421 *
422 * This function detects if an opus packet has FEC.
423 * Input:
424 * - payload : Encoded data pointer
425 * - payload_length_bytes : Bytes of encoded data
426 *
427 * Return value : 0 - the packet does NOT contain FEC.
428 * 1 - the packet contains FEC.
429 */
430int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700431 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000432
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000433#ifdef __cplusplus
434} // extern "C"
435#endif
436
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200437#endif // MODULES_AUDIO_CODING_CODECS_OPUS_OPUS_INTERFACE_H_