blob: f5bcc456d04ac1e8b69ece6e8c257f0e1159b3ca [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>
Niels Möllera12c42a2018-07-25 16:05:48 +020015#include <stdint.h>
Peter Kastingdce40cf2015-08-24 14:52:23 -070016
Alex Luebseeb27652017-11-20 11:13:56 -080017#include "modules/audio_coding/codecs/opus/opus_inst.h"
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 *
Alex Loiko50b8c392019-04-03 15:12:01 +020030 * This function creates an Opus encoder that encodes mono or stereo.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000031 *
32 * Input:
Alex Loiko50b8c392019-04-03 15:12:01 +020033 * - channels : number of channels; 1 or 2.
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000034 * - 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
Alex Loiko50b8c392019-04-03 15:12:01 +020050/****************************************************************************
51 * WebRtcOpus_MultistreamEncoderCreate(...)
52 *
53 * This function creates an Opus encoder with any supported channel count.
54 *
55 * Input:
Alex Loikoe5b94162019-04-08 17:19:41 +020056 * - channels : number of channels in the input of the encoder.
Alex Loiko50b8c392019-04-03 15:12:01 +020057 * - application : 0 - VOIP applications.
58 * Favor speech intelligibility.
59 * 1 - Audio applications.
60 * Favor faithfulness to the original input.
Alex Loikoe5b94162019-04-08 17:19:41 +020061 * - streams : number of streams, as described in RFC 7845.
Alex Loiko50b8c392019-04-03 15:12:01 +020062 * - coupled_streams : number of coupled streams, as described in
63 * RFC 7845.
64 * - channel_mapping : the channel mapping; pointer to array of
65 * `channel` bytes, as described in RFC 7845.
66 *
67 * Output:
68 * - inst : a pointer to Encoder context that is created
69 * if success.
70 *
71 * Return value : 0 - Success
72 * -1 - Error
73 */
74int16_t WebRtcOpus_MultistreamEncoderCreate(
75 OpusEncInst** inst,
76 size_t channels,
77 int32_t application,
Alex Loikoe5b94162019-04-08 17:19:41 +020078 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +020079 size_t coupled_streams,
80 const unsigned char* channel_mapping);
81
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000082int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst);
83
84/****************************************************************************
85 * WebRtcOpus_Encode(...)
86 *
87 * This function encodes audio as a series of Opus frames and inserts
88 * it into a packet. Input buffer can be any length.
89 *
90 * Input:
91 * - inst : Encoder context
92 * - audio_in : Input speech data buffer
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +000093 * - samples : Samples per channel in audio_in
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000094 * - length_encoded_buffer : Output buffer size
95 *
96 * Output:
97 * - encoded : Output compressed data buffer
98 *
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000099 * Return value : >=0 - Length (in bytes) of coded data
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000100 * -1 - Error
101 */
Peter Kastingbba78072015-06-11 19:02:46 -0700102int WebRtcOpus_Encode(OpusEncInst* inst,
103 const int16_t* audio_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700104 size_t samples,
105 size_t length_encoded_buffer,
Peter Kastingbba78072015-06-11 19:02:46 -0700106 uint8_t* encoded);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000107
108/****************************************************************************
109 * WebRtcOpus_SetBitRate(...)
110 *
111 * This function adjusts the target bitrate of the encoder.
112 *
113 * Input:
114 * - inst : Encoder context
115 * - rate : New target bitrate
116 *
117 * Return value : 0 - Success
118 * -1 - Error
119 */
120int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate);
121
minyue@webrtc.org04546882014-03-07 08:55:48 +0000122/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000123 * WebRtcOpus_SetPacketLossRate(...)
124 *
125 * This function configures the encoder's expected packet loss percentage.
126 *
127 * Input:
128 * - inst : Encoder context
129 * - loss_rate : loss percentage in the range 0-100, inclusive.
130 * Return value : 0 - Success
131 * -1 - Error
132 */
133int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate);
134
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000135/****************************************************************************
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000136 * WebRtcOpus_SetMaxPlaybackRate(...)
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000137 *
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000138 * Configures the maximum playback rate for encoding. Due to hardware
139 * limitations, the receiver may render audio up to a playback rate. Opus
140 * encoder can use this information to optimize for network usage and encoding
141 * complexity. This will affect the audio bandwidth in the coded audio. However,
142 * the input/output sample rate is not affected.
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000143 *
144 * Input:
145 * - inst : Encoder context
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000146 * - frequency_hz : Maximum playback rate in Hz.
147 * This parameter can take any value. The relation
148 * between the value and the Opus internal mode is
149 * as following:
150 * frequency_hz <= 8000 narrow band
151 * 8000 < frequency_hz <= 12000 medium band
152 * 12000 < frequency_hz <= 16000 wide band
153 * 16000 < frequency_hz <= 24000 super wide band
154 * frequency_hz > 24000 full band
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000155 * Return value : 0 - Success
156 * -1 - Error
157 */
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000158int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000159
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100160/****************************************************************************
161 * WebRtcOpus_GetMaxPlaybackRate(...)
162 *
163 * Queries the maximum playback rate for encoding. If different single-stream
164 * encoders have different maximum playback rates, this function fails.
165 *
166 * Input:
167 * - inst : Encoder context.
168 * Output:
169 * - result_hz : The maximum playback rate in Hz.
170 * Return value : 0 - Success
171 * -1 - Error
172 */
173int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
174 int32_t* result_hz);
175
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000176/* TODO(minyue): Check whether an API to check the FEC and the packet loss rate
177 * is needed. It might not be very useful since there are not many use cases and
178 * the caller can always maintain the states. */
179
180/****************************************************************************
181 * WebRtcOpus_EnableFec()
182 *
183 * This function enables FEC for encoding.
184 *
185 * Input:
186 * - inst : Encoder context
187 *
188 * Return value : 0 - Success
189 * -1 - Error
190 */
191int16_t WebRtcOpus_EnableFec(OpusEncInst* inst);
192
193/****************************************************************************
194 * WebRtcOpus_DisableFec()
195 *
196 * This function disables FEC for encoding.
197 *
198 * Input:
199 * - inst : Encoder context
200 *
201 * Return value : 0 - Success
202 * -1 - Error
203 */
204int16_t WebRtcOpus_DisableFec(OpusEncInst* inst);
205
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000206/****************************************************************************
207 * WebRtcOpus_EnableDtx()
208 *
209 * This function enables Opus internal DTX for encoding.
210 *
211 * Input:
212 * - inst : Encoder context
213 *
214 * Return value : 0 - Success
215 * -1 - Error
216 */
217int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst);
218
219/****************************************************************************
220 * WebRtcOpus_DisableDtx()
221 *
222 * This function disables Opus internal DTX for encoding.
223 *
224 * Input:
225 * - inst : Encoder context
226 *
227 * Return value : 0 - Success
228 * -1 - Error
229 */
230int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst);
231
soren28dc2852017-04-06 05:48:36 -0700232/****************************************************************************
233 * WebRtcOpus_EnableCbr()
234 *
235 * This function enables CBR for encoding.
236 *
237 * Input:
238 * - inst : Encoder context
239 *
240 * Return value : 0 - Success
241 * -1 - Error
242 */
243int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst);
244
245/****************************************************************************
246 * WebRtcOpus_DisableCbr()
247 *
248 * This function disables CBR for encoding.
249 *
250 * Input:
251 * - inst : Encoder context
252 *
253 * Return value : 0 - Success
254 * -1 - Error
255 */
256int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst);
257
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000258/*
minyue@webrtc.org04546882014-03-07 08:55:48 +0000259 * WebRtcOpus_SetComplexity(...)
260 *
261 * This function adjusts the computational complexity. The effect is the same as
262 * calling the complexity setting of Opus as an Opus encoder related CTL.
263 *
264 * Input:
265 * - inst : Encoder context
266 * - complexity : New target complexity (0-10, inclusive)
267 *
268 * Return value : 0 - Success
269 * -1 - Error
270 */
271int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity);
272
minyuec8299f92016-09-27 02:08:47 -0700273/*
Alex Luebseeb27652017-11-20 11:13:56 -0800274 * WebRtcOpus_GetBandwidth(...)
275 *
276 * This function returns the current bandwidth.
277 *
278 * Input:
279 * - inst : Encoder context
280 *
281 * Return value : Bandwidth - Success
282 * -1 - Error
283 */
284int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst);
285
286/*
287 * WebRtcOpus_SetBandwidth(...)
288 *
289 * By default Opus decides which bandwidth to encode the signal in depending on
290 * the the bitrate. This function overrules the previous setting and forces the
291 * encoder to encode in narrowband/wideband/fullband/etc.
292 *
293 * Input:
294 * - inst : Encoder context
295 * - bandwidth : New target bandwidth. Valid values are:
296 * OPUS_BANDWIDTH_NARROWBAND
297 * OPUS_BANDWIDTH_MEDIUMBAND
298 * OPUS_BANDWIDTH_WIDEBAND
299 * OPUS_BANDWIDTH_SUPERWIDEBAND
300 * OPUS_BANDWIDTH_FULLBAND
301 *
302 * Return value : 0 - Success
303 * -1 - Error
304 */
305int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth);
306
307/*
minyuec8299f92016-09-27 02:08:47 -0700308 * WebRtcOpus_SetForceChannels(...)
309 *
310 * If the encoder is initialized as a stereo encoder, Opus will by default
311 * decide whether to encode in mono or stereo based on the bitrate. This
312 * function overrules the previous setting, and forces the encoder to encode
313 * in auto/mono/stereo.
314 *
315 * If the Encoder is initialized as a mono encoder, and one tries to force
316 * stereo, the function will return an error.
317 *
318 * Input:
319 * - inst : Encoder context
320 * - num_channels : 0 - Not forced
321 * 1 - Mono
322 * 2 - Stereo
323 *
324 * Return value : 0 - Success
325 * -1 - Error
326 */
minyue41b9c802016-10-06 07:13:54 -0700327int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels);
minyuec8299f92016-09-27 02:08:47 -0700328
Peter Kasting69558702016-01-12 16:26:35 -0800329int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, size_t channels);
Alex Loiko50b8c392019-04-03 15:12:01 +0200330
331/****************************************************************************
332 * WebRtcOpus_MultistreamDecoderCreate(...)
333 *
334 * This function creates an Opus decoder with any supported channel count.
335 *
336 * Input:
Alex Loikoe5b94162019-04-08 17:19:41 +0200337 * - channels : number of output channels that the decoder
338 * will produce.
339 * - streams : number of encoded streams, as described in
340 * RFC 7845.
Alex Loiko50b8c392019-04-03 15:12:01 +0200341 * - coupled_streams : number of coupled streams, as described in
342 * RFC 7845.
343 * - channel_mapping : the channel mapping; pointer to array of
344 * `channel` bytes, as described in RFC 7845.
345 *
346 * Output:
347 * - inst : a pointer to a Decoder context that is created
348 * if success.
349 *
350 * Return value : 0 - Success
351 * -1 - Error
352 */
353int16_t WebRtcOpus_MultistreamDecoderCreate(
354 OpusDecInst** inst,
355 size_t channels,
Alex Loikoe5b94162019-04-08 17:19:41 +0200356 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +0200357 size_t coupled_streams,
358 const unsigned char* channel_mapping);
359
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000360int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst);
361
362/****************************************************************************
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000363 * WebRtcOpus_DecoderChannels(...)
364 *
365 * This function returns the number of channels created for Opus decoder.
366 */
Peter Kasting69558702016-01-12 16:26:35 -0800367size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000368
369/****************************************************************************
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000370 * WebRtcOpus_DecoderInit(...)
371 *
372 * This function resets state of the decoder.
373 *
374 * Input:
375 * - inst : Decoder context
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000376 */
Karl Wiberg43766482015-08-27 15:22:11 +0200377void WebRtcOpus_DecoderInit(OpusDecInst* inst);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000378
379/****************************************************************************
380 * WebRtcOpus_Decode(...)
381 *
382 * This function decodes an Opus packet into one or more audio frames at the
383 * ACM interface's sampling rate (32 kHz).
384 *
385 * Input:
386 * - inst : Decoder context
387 * - encoded : Encoded data
388 * - encoded_bytes : Bytes in encoded vector
389 *
390 * Output:
391 * - decoded : The decoded vector
392 * - audio_type : 1 normal, 2 CNG (for Opus it should
393 * always return 1 since we're not using Opus's
394 * built-in DTX/CNG scheme)
395 *
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +0000396 * Return value : >0 - Samples per channel in decoded vector
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000397 * -1 - Error
398 */
Yves Gerey665174f2018-06-19 15:03:05 +0200399int WebRtcOpus_Decode(OpusDecInst* inst,
400 const uint8_t* encoded,
401 size_t encoded_bytes,
402 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700403 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000404
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000405/****************************************************************************
406 * WebRtcOpus_DecodePlc(...)
407 *
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000408 * This function processes PLC for opus frame(s).
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000409 * Input:
410 * - inst : Decoder context
411 * - number_of_lost_frames : Number of PLC frames to produce
412 *
413 * Output:
414 * - decoded : The decoded vector
415 *
416 * Return value : >0 - number of samples in decoded PLC vector
417 * -1 - Error
418 */
Yves Gerey665174f2018-06-19 15:03:05 +0200419int WebRtcOpus_DecodePlc(OpusDecInst* inst,
420 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700421 int number_of_lost_frames);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000422
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000423/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000424 * WebRtcOpus_DecodeFec(...)
425 *
426 * This function decodes the FEC data from an Opus packet into one or more audio
427 * frames at the ACM interface's sampling rate (32 kHz).
428 *
429 * Input:
430 * - inst : Decoder context
431 * - encoded : Encoded data
432 * - encoded_bytes : Bytes in encoded vector
433 *
434 * Output:
435 * - decoded : The decoded vector (previous frame)
436 *
437 * Return value : >0 - Samples per channel in decoded vector
438 * 0 - No FEC data in the packet
439 * -1 - Error
440 */
Yves Gerey665174f2018-06-19 15:03:05 +0200441int WebRtcOpus_DecodeFec(OpusDecInst* inst,
442 const uint8_t* encoded,
443 size_t encoded_bytes,
444 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700445 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000446
447/****************************************************************************
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000448 * WebRtcOpus_DurationEst(...)
449 *
450 * This function calculates the duration of an opus packet.
451 * Input:
452 * - inst : Decoder context
453 * - payload : Encoded data pointer
454 * - payload_length_bytes : Bytes of encoded data
455 *
Minyue323b1322015-05-25 13:49:37 +0200456 * Return value : The duration of the packet, in samples per
457 * channel.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000458 */
459int WebRtcOpus_DurationEst(OpusDecInst* inst,
460 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700461 size_t payload_length_bytes);
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000462
minyuel6d92bf52015-09-23 15:20:39 +0200463/****************************************************************************
464 * WebRtcOpus_PlcDuration(...)
465 *
466 * This function calculates the duration of a frame returned by packet loss
467 * concealment (PLC).
468 *
469 * Input:
470 * - inst : Decoder context
471 *
472 * Return value : The duration of a frame returned by PLC, in
473 * samples per channel.
474 */
475int WebRtcOpus_PlcDuration(OpusDecInst* inst);
476
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000477/* TODO(minyue): Check whether it is needed to add a decoder context to the
478 * arguments, like WebRtcOpus_DurationEst(...). In fact, the packet itself tells
479 * the duration. The decoder context in WebRtcOpus_DurationEst(...) is not used.
480 * So it may be advisable to remove it from WebRtcOpus_DurationEst(...). */
481
482/****************************************************************************
483 * WebRtcOpus_FecDurationEst(...)
484 *
485 * This function calculates the duration of the FEC data within an opus packet.
486 * Input:
487 * - payload : Encoded data pointer
488 * - payload_length_bytes : Bytes of encoded data
489 *
490 * Return value : >0 - The duration of the FEC data in the
Minyue323b1322015-05-25 13:49:37 +0200491 * packet in samples per channel.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000492 * 0 - No FEC data in the packet.
493 */
494int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700495 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000496
497/****************************************************************************
498 * WebRtcOpus_PacketHasFec(...)
499 *
500 * This function detects if an opus packet has FEC.
501 * Input:
502 * - payload : Encoded data pointer
503 * - payload_length_bytes : Bytes of encoded data
504 *
505 * Return value : 0 - the packet does NOT contain FEC.
506 * 1 - the packet contains FEC.
507 */
508int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700509 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000510
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000511#ifdef __cplusplus
512} // extern "C"
513#endif
514
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200515#endif // MODULES_AUDIO_CODING_CODECS_OPUS_OPUS_INTERFACE_H_