blob: cf95a6912d3ae0491c04c1ff63d8ddc6afa77a73 [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.
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020038 * - sample_rate_hz : sample rate of input audio
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000039 *
40 * Output:
41 * - inst : a pointer to Encoder context that is created
42 * if success.
43 *
44 * Return value : 0 - Success
45 * -1 - Error
46 */
47int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
Peter Kasting69558702016-01-12 16:26:35 -080048 size_t channels,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020049 int32_t application,
50 int sample_rate_hz);
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000051
Alex Loiko50b8c392019-04-03 15:12:01 +020052/****************************************************************************
53 * WebRtcOpus_MultistreamEncoderCreate(...)
54 *
55 * This function creates an Opus encoder with any supported channel count.
56 *
57 * Input:
Alex Loikoe5b94162019-04-08 17:19:41 +020058 * - channels : number of channels in the input of the encoder.
Alex Loiko50b8c392019-04-03 15:12:01 +020059 * - application : 0 - VOIP applications.
60 * Favor speech intelligibility.
61 * 1 - Audio applications.
62 * Favor faithfulness to the original input.
Alex Loikoe5b94162019-04-08 17:19:41 +020063 * - streams : number of streams, as described in RFC 7845.
Alex Loiko50b8c392019-04-03 15:12:01 +020064 * - coupled_streams : number of coupled streams, as described in
65 * RFC 7845.
66 * - channel_mapping : the channel mapping; pointer to array of
67 * `channel` bytes, as described in RFC 7845.
68 *
69 * Output:
70 * - inst : a pointer to Encoder context that is created
71 * if success.
72 *
73 * Return value : 0 - Success
74 * -1 - Error
75 */
76int16_t WebRtcOpus_MultistreamEncoderCreate(
77 OpusEncInst** inst,
78 size_t channels,
79 int32_t application,
Alex Loikoe5b94162019-04-08 17:19:41 +020080 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +020081 size_t coupled_streams,
82 const unsigned char* channel_mapping);
83
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000084int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst);
85
86/****************************************************************************
87 * WebRtcOpus_Encode(...)
88 *
89 * This function encodes audio as a series of Opus frames and inserts
90 * it into a packet. Input buffer can be any length.
91 *
92 * Input:
93 * - inst : Encoder context
94 * - audio_in : Input speech data buffer
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +000095 * - samples : Samples per channel in audio_in
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000096 * - length_encoded_buffer : Output buffer size
97 *
98 * Output:
99 * - encoded : Output compressed data buffer
100 *
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000101 * Return value : >=0 - Length (in bytes) of coded data
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000102 * -1 - Error
103 */
Peter Kastingbba78072015-06-11 19:02:46 -0700104int WebRtcOpus_Encode(OpusEncInst* inst,
105 const int16_t* audio_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700106 size_t samples,
107 size_t length_encoded_buffer,
Peter Kastingbba78072015-06-11 19:02:46 -0700108 uint8_t* encoded);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000109
110/****************************************************************************
111 * WebRtcOpus_SetBitRate(...)
112 *
113 * This function adjusts the target bitrate of the encoder.
114 *
115 * Input:
116 * - inst : Encoder context
117 * - rate : New target bitrate
118 *
119 * Return value : 0 - Success
120 * -1 - Error
121 */
122int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate);
123
minyue@webrtc.org04546882014-03-07 08:55:48 +0000124/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000125 * WebRtcOpus_SetPacketLossRate(...)
126 *
127 * This function configures the encoder's expected packet loss percentage.
128 *
129 * Input:
130 * - inst : Encoder context
131 * - loss_rate : loss percentage in the range 0-100, inclusive.
132 * Return value : 0 - Success
133 * -1 - Error
134 */
135int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate);
136
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000137/****************************************************************************
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000138 * WebRtcOpus_SetMaxPlaybackRate(...)
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000139 *
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000140 * Configures the maximum playback rate for encoding. Due to hardware
141 * limitations, the receiver may render audio up to a playback rate. Opus
142 * encoder can use this information to optimize for network usage and encoding
143 * complexity. This will affect the audio bandwidth in the coded audio. However,
144 * the input/output sample rate is not affected.
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000145 *
146 * Input:
147 * - inst : Encoder context
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000148 * - frequency_hz : Maximum playback rate in Hz.
149 * This parameter can take any value. The relation
150 * between the value and the Opus internal mode is
151 * as following:
152 * frequency_hz <= 8000 narrow band
153 * 8000 < frequency_hz <= 12000 medium band
154 * 12000 < frequency_hz <= 16000 wide band
155 * 16000 < frequency_hz <= 24000 super wide band
156 * frequency_hz > 24000 full band
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000157 * Return value : 0 - Success
158 * -1 - Error
159 */
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000160int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000161
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100162/****************************************************************************
163 * WebRtcOpus_GetMaxPlaybackRate(...)
164 *
165 * Queries the maximum playback rate for encoding. If different single-stream
166 * encoders have different maximum playback rates, this function fails.
167 *
168 * Input:
169 * - inst : Encoder context.
170 * Output:
171 * - result_hz : The maximum playback rate in Hz.
172 * Return value : 0 - Success
173 * -1 - Error
174 */
175int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
176 int32_t* result_hz);
177
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000178/* TODO(minyue): Check whether an API to check the FEC and the packet loss rate
179 * is needed. It might not be very useful since there are not many use cases and
180 * the caller can always maintain the states. */
181
182/****************************************************************************
183 * WebRtcOpus_EnableFec()
184 *
185 * This function enables FEC for encoding.
186 *
187 * Input:
188 * - inst : Encoder context
189 *
190 * Return value : 0 - Success
191 * -1 - Error
192 */
193int16_t WebRtcOpus_EnableFec(OpusEncInst* inst);
194
195/****************************************************************************
196 * WebRtcOpus_DisableFec()
197 *
198 * This function disables FEC for encoding.
199 *
200 * Input:
201 * - inst : Encoder context
202 *
203 * Return value : 0 - Success
204 * -1 - Error
205 */
206int16_t WebRtcOpus_DisableFec(OpusEncInst* inst);
207
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000208/****************************************************************************
209 * WebRtcOpus_EnableDtx()
210 *
211 * This function enables Opus internal DTX for encoding.
212 *
213 * Input:
214 * - inst : Encoder context
215 *
216 * Return value : 0 - Success
217 * -1 - Error
218 */
219int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst);
220
221/****************************************************************************
222 * WebRtcOpus_DisableDtx()
223 *
224 * This function disables Opus internal DTX for encoding.
225 *
226 * Input:
227 * - inst : Encoder context
228 *
229 * Return value : 0 - Success
230 * -1 - Error
231 */
232int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst);
233
soren28dc2852017-04-06 05:48:36 -0700234/****************************************************************************
235 * WebRtcOpus_EnableCbr()
236 *
237 * This function enables CBR for encoding.
238 *
239 * Input:
240 * - inst : Encoder context
241 *
242 * Return value : 0 - Success
243 * -1 - Error
244 */
245int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst);
246
247/****************************************************************************
248 * WebRtcOpus_DisableCbr()
249 *
250 * This function disables CBR for encoding.
251 *
252 * Input:
253 * - inst : Encoder context
254 *
255 * Return value : 0 - Success
256 * -1 - Error
257 */
258int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst);
259
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000260/*
minyue@webrtc.org04546882014-03-07 08:55:48 +0000261 * WebRtcOpus_SetComplexity(...)
262 *
263 * This function adjusts the computational complexity. The effect is the same as
264 * calling the complexity setting of Opus as an Opus encoder related CTL.
265 *
266 * Input:
267 * - inst : Encoder context
268 * - complexity : New target complexity (0-10, inclusive)
269 *
270 * Return value : 0 - Success
271 * -1 - Error
272 */
273int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity);
274
minyuec8299f92016-09-27 02:08:47 -0700275/*
Alex Luebseeb27652017-11-20 11:13:56 -0800276 * WebRtcOpus_GetBandwidth(...)
277 *
278 * This function returns the current bandwidth.
279 *
280 * Input:
281 * - inst : Encoder context
282 *
283 * Return value : Bandwidth - Success
284 * -1 - Error
285 */
286int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst);
287
288/*
289 * WebRtcOpus_SetBandwidth(...)
290 *
291 * By default Opus decides which bandwidth to encode the signal in depending on
292 * the the bitrate. This function overrules the previous setting and forces the
293 * encoder to encode in narrowband/wideband/fullband/etc.
294 *
295 * Input:
296 * - inst : Encoder context
297 * - bandwidth : New target bandwidth. Valid values are:
298 * OPUS_BANDWIDTH_NARROWBAND
299 * OPUS_BANDWIDTH_MEDIUMBAND
300 * OPUS_BANDWIDTH_WIDEBAND
301 * OPUS_BANDWIDTH_SUPERWIDEBAND
302 * OPUS_BANDWIDTH_FULLBAND
303 *
304 * Return value : 0 - Success
305 * -1 - Error
306 */
307int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth);
308
309/*
minyuec8299f92016-09-27 02:08:47 -0700310 * WebRtcOpus_SetForceChannels(...)
311 *
312 * If the encoder is initialized as a stereo encoder, Opus will by default
313 * decide whether to encode in mono or stereo based on the bitrate. This
314 * function overrules the previous setting, and forces the encoder to encode
315 * in auto/mono/stereo.
316 *
317 * If the Encoder is initialized as a mono encoder, and one tries to force
318 * stereo, the function will return an error.
319 *
320 * Input:
321 * - inst : Encoder context
322 * - num_channels : 0 - Not forced
323 * 1 - Mono
324 * 2 - Stereo
325 *
326 * Return value : 0 - Success
327 * -1 - Error
328 */
minyue41b9c802016-10-06 07:13:54 -0700329int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels);
minyuec8299f92016-09-27 02:08:47 -0700330
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200331int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst,
332 size_t channels,
333 int sample_rate_hz);
Alex Loiko50b8c392019-04-03 15:12:01 +0200334
335/****************************************************************************
336 * WebRtcOpus_MultistreamDecoderCreate(...)
337 *
338 * This function creates an Opus decoder with any supported channel count.
339 *
340 * Input:
Alex Loikoe5b94162019-04-08 17:19:41 +0200341 * - channels : number of output channels that the decoder
342 * will produce.
343 * - streams : number of encoded streams, as described in
344 * RFC 7845.
Alex Loiko50b8c392019-04-03 15:12:01 +0200345 * - coupled_streams : number of coupled streams, as described in
346 * RFC 7845.
347 * - channel_mapping : the channel mapping; pointer to array of
348 * `channel` bytes, as described in RFC 7845.
349 *
350 * Output:
351 * - inst : a pointer to a Decoder context that is created
352 * if success.
353 *
354 * Return value : 0 - Success
355 * -1 - Error
356 */
357int16_t WebRtcOpus_MultistreamDecoderCreate(
358 OpusDecInst** inst,
359 size_t channels,
Alex Loikoe5b94162019-04-08 17:19:41 +0200360 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +0200361 size_t coupled_streams,
362 const unsigned char* channel_mapping);
363
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000364int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst);
365
366/****************************************************************************
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000367 * WebRtcOpus_DecoderChannels(...)
368 *
369 * This function returns the number of channels created for Opus decoder.
370 */
Peter Kasting69558702016-01-12 16:26:35 -0800371size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000372
373/****************************************************************************
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000374 * WebRtcOpus_DecoderInit(...)
375 *
376 * This function resets state of the decoder.
377 *
378 * Input:
379 * - inst : Decoder context
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000380 */
Karl Wiberg43766482015-08-27 15:22:11 +0200381void WebRtcOpus_DecoderInit(OpusDecInst* inst);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000382
383/****************************************************************************
384 * WebRtcOpus_Decode(...)
385 *
386 * This function decodes an Opus packet into one or more audio frames at the
387 * ACM interface's sampling rate (32 kHz).
388 *
389 * Input:
390 * - inst : Decoder context
391 * - encoded : Encoded data
392 * - encoded_bytes : Bytes in encoded vector
393 *
394 * Output:
395 * - decoded : The decoded vector
396 * - audio_type : 1 normal, 2 CNG (for Opus it should
397 * always return 1 since we're not using Opus's
398 * built-in DTX/CNG scheme)
399 *
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +0000400 * Return value : >0 - Samples per channel in decoded vector
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000401 * -1 - Error
402 */
Yves Gerey665174f2018-06-19 15:03:05 +0200403int WebRtcOpus_Decode(OpusDecInst* inst,
404 const uint8_t* encoded,
405 size_t encoded_bytes,
406 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700407 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000408
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000409/****************************************************************************
410 * WebRtcOpus_DecodePlc(...)
411 *
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000412 * This function processes PLC for opus frame(s).
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000413 * Input:
414 * - inst : Decoder context
415 * - number_of_lost_frames : Number of PLC frames to produce
416 *
417 * Output:
418 * - decoded : The decoded vector
419 *
420 * Return value : >0 - number of samples in decoded PLC vector
421 * -1 - Error
422 */
Yves Gerey665174f2018-06-19 15:03:05 +0200423int WebRtcOpus_DecodePlc(OpusDecInst* inst,
424 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700425 int number_of_lost_frames);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000426
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000427/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000428 * WebRtcOpus_DecodeFec(...)
429 *
430 * This function decodes the FEC data from an Opus packet into one or more audio
431 * frames at the ACM interface's sampling rate (32 kHz).
432 *
433 * Input:
434 * - inst : Decoder context
435 * - encoded : Encoded data
436 * - encoded_bytes : Bytes in encoded vector
437 *
438 * Output:
439 * - decoded : The decoded vector (previous frame)
440 *
441 * Return value : >0 - Samples per channel in decoded vector
442 * 0 - No FEC data in the packet
443 * -1 - Error
444 */
Yves Gerey665174f2018-06-19 15:03:05 +0200445int WebRtcOpus_DecodeFec(OpusDecInst* inst,
446 const uint8_t* encoded,
447 size_t encoded_bytes,
448 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700449 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000450
451/****************************************************************************
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000452 * WebRtcOpus_DurationEst(...)
453 *
454 * This function calculates the duration of an opus packet.
455 * Input:
456 * - inst : Decoder context
457 * - payload : Encoded data pointer
458 * - payload_length_bytes : Bytes of encoded data
459 *
Minyue323b1322015-05-25 13:49:37 +0200460 * Return value : The duration of the packet, in samples per
461 * channel.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000462 */
463int WebRtcOpus_DurationEst(OpusDecInst* inst,
464 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700465 size_t payload_length_bytes);
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000466
minyuel6d92bf52015-09-23 15:20:39 +0200467/****************************************************************************
468 * WebRtcOpus_PlcDuration(...)
469 *
470 * This function calculates the duration of a frame returned by packet loss
471 * concealment (PLC).
472 *
473 * Input:
474 * - inst : Decoder context
475 *
476 * Return value : The duration of a frame returned by PLC, in
477 * samples per channel.
478 */
479int WebRtcOpus_PlcDuration(OpusDecInst* inst);
480
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000481/* TODO(minyue): Check whether it is needed to add a decoder context to the
482 * arguments, like WebRtcOpus_DurationEst(...). In fact, the packet itself tells
483 * the duration. The decoder context in WebRtcOpus_DurationEst(...) is not used.
484 * So it may be advisable to remove it from WebRtcOpus_DurationEst(...). */
485
486/****************************************************************************
487 * WebRtcOpus_FecDurationEst(...)
488 *
489 * This function calculates the duration of the FEC data within an opus packet.
490 * Input:
491 * - payload : Encoded data pointer
492 * - payload_length_bytes : Bytes of encoded data
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200493 * - sample_rate_hz : Sample rate of output audio
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000494 *
495 * Return value : >0 - The duration of the FEC data in the
Minyue323b1322015-05-25 13:49:37 +0200496 * packet in samples per channel.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000497 * 0 - No FEC data in the packet.
498 */
499int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200500 size_t payload_length_bytes,
501 int sample_rate_hz);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000502
503/****************************************************************************
504 * WebRtcOpus_PacketHasFec(...)
505 *
506 * This function detects if an opus packet has FEC.
507 * Input:
508 * - payload : Encoded data pointer
509 * - payload_length_bytes : Bytes of encoded data
510 *
511 * Return value : 0 - the packet does NOT contain FEC.
512 * 1 - the packet contains FEC.
513 */
514int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700515 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000516
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000517#ifdef __cplusplus
518} // extern "C"
519#endif
520
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200521#endif // MODULES_AUDIO_CODING_CODECS_OPUS_OPUS_INTERFACE_H_