blob: 04eaba95f71f4c3206c499c9366d4655ca006495 [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:
56 * - channels : number of channels.
57 * - application : 0 - VOIP applications.
58 * Favor speech intelligibility.
59 * 1 - Audio applications.
60 * Favor faithfulness to the original input.
61 * - coupled_streams : number of coupled streams, as described in
62 * RFC 7845.
63 * - channel_mapping : the channel mapping; pointer to array of
64 * `channel` bytes, as described in RFC 7845.
65 *
66 * Output:
67 * - inst : a pointer to Encoder context that is created
68 * if success.
69 *
70 * Return value : 0 - Success
71 * -1 - Error
72 */
73int16_t WebRtcOpus_MultistreamEncoderCreate(
74 OpusEncInst** inst,
75 size_t channels,
76 int32_t application,
77 size_t coupled_streams,
78 const unsigned char* channel_mapping);
79
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000080int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst);
81
82/****************************************************************************
83 * WebRtcOpus_Encode(...)
84 *
85 * This function encodes audio as a series of Opus frames and inserts
86 * it into a packet. Input buffer can be any length.
87 *
88 * Input:
89 * - inst : Encoder context
90 * - audio_in : Input speech data buffer
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +000091 * - samples : Samples per channel in audio_in
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000092 * - length_encoded_buffer : Output buffer size
93 *
94 * Output:
95 * - encoded : Output compressed data buffer
96 *
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +000097 * Return value : >=0 - Length (in bytes) of coded data
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000098 * -1 - Error
99 */
Peter Kastingbba78072015-06-11 19:02:46 -0700100int WebRtcOpus_Encode(OpusEncInst* inst,
101 const int16_t* audio_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700102 size_t samples,
103 size_t length_encoded_buffer,
Peter Kastingbba78072015-06-11 19:02:46 -0700104 uint8_t* encoded);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000105
106/****************************************************************************
107 * WebRtcOpus_SetBitRate(...)
108 *
109 * This function adjusts the target bitrate of the encoder.
110 *
111 * Input:
112 * - inst : Encoder context
113 * - rate : New target bitrate
114 *
115 * Return value : 0 - Success
116 * -1 - Error
117 */
118int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate);
119
minyue@webrtc.org04546882014-03-07 08:55:48 +0000120/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000121 * WebRtcOpus_SetPacketLossRate(...)
122 *
123 * This function configures the encoder's expected packet loss percentage.
124 *
125 * Input:
126 * - inst : Encoder context
127 * - loss_rate : loss percentage in the range 0-100, inclusive.
128 * Return value : 0 - Success
129 * -1 - Error
130 */
131int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate);
132
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000133/****************************************************************************
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000134 * WebRtcOpus_SetMaxPlaybackRate(...)
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000135 *
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000136 * Configures the maximum playback rate for encoding. Due to hardware
137 * limitations, the receiver may render audio up to a playback rate. Opus
138 * encoder can use this information to optimize for network usage and encoding
139 * complexity. This will affect the audio bandwidth in the coded audio. However,
140 * the input/output sample rate is not affected.
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000141 *
142 * Input:
143 * - inst : Encoder context
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000144 * - frequency_hz : Maximum playback rate in Hz.
145 * This parameter can take any value. The relation
146 * between the value and the Opus internal mode is
147 * as following:
148 * frequency_hz <= 8000 narrow band
149 * 8000 < frequency_hz <= 12000 medium band
150 * 12000 < frequency_hz <= 16000 wide band
151 * 16000 < frequency_hz <= 24000 super wide band
152 * frequency_hz > 24000 full band
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000153 * Return value : 0 - Success
154 * -1 - Error
155 */
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000156int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz);
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000157
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100158/****************************************************************************
159 * WebRtcOpus_GetMaxPlaybackRate(...)
160 *
161 * Queries the maximum playback rate for encoding. If different single-stream
162 * encoders have different maximum playback rates, this function fails.
163 *
164 * Input:
165 * - inst : Encoder context.
166 * Output:
167 * - result_hz : The maximum playback rate in Hz.
168 * Return value : 0 - Success
169 * -1 - Error
170 */
171int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
172 int32_t* result_hz);
173
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000174/* TODO(minyue): Check whether an API to check the FEC and the packet loss rate
175 * is needed. It might not be very useful since there are not many use cases and
176 * the caller can always maintain the states. */
177
178/****************************************************************************
179 * WebRtcOpus_EnableFec()
180 *
181 * This function enables FEC for encoding.
182 *
183 * Input:
184 * - inst : Encoder context
185 *
186 * Return value : 0 - Success
187 * -1 - Error
188 */
189int16_t WebRtcOpus_EnableFec(OpusEncInst* inst);
190
191/****************************************************************************
192 * WebRtcOpus_DisableFec()
193 *
194 * This function disables FEC for encoding.
195 *
196 * Input:
197 * - inst : Encoder context
198 *
199 * Return value : 0 - Success
200 * -1 - Error
201 */
202int16_t WebRtcOpus_DisableFec(OpusEncInst* inst);
203
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000204/****************************************************************************
205 * WebRtcOpus_EnableDtx()
206 *
207 * This function enables Opus internal DTX for encoding.
208 *
209 * Input:
210 * - inst : Encoder context
211 *
212 * Return value : 0 - Success
213 * -1 - Error
214 */
215int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst);
216
217/****************************************************************************
218 * WebRtcOpus_DisableDtx()
219 *
220 * This function disables Opus internal DTX for encoding.
221 *
222 * Input:
223 * - inst : Encoder context
224 *
225 * Return value : 0 - Success
226 * -1 - Error
227 */
228int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst);
229
soren28dc2852017-04-06 05:48:36 -0700230/****************************************************************************
231 * WebRtcOpus_EnableCbr()
232 *
233 * This function enables CBR for encoding.
234 *
235 * Input:
236 * - inst : Encoder context
237 *
238 * Return value : 0 - Success
239 * -1 - Error
240 */
241int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst);
242
243/****************************************************************************
244 * WebRtcOpus_DisableCbr()
245 *
246 * This function disables CBR for encoding.
247 *
248 * Input:
249 * - inst : Encoder context
250 *
251 * Return value : 0 - Success
252 * -1 - Error
253 */
254int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst);
255
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000256/*
minyue@webrtc.org04546882014-03-07 08:55:48 +0000257 * WebRtcOpus_SetComplexity(...)
258 *
259 * This function adjusts the computational complexity. The effect is the same as
260 * calling the complexity setting of Opus as an Opus encoder related CTL.
261 *
262 * Input:
263 * - inst : Encoder context
264 * - complexity : New target complexity (0-10, inclusive)
265 *
266 * Return value : 0 - Success
267 * -1 - Error
268 */
269int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity);
270
minyuec8299f92016-09-27 02:08:47 -0700271/*
Alex Luebseeb27652017-11-20 11:13:56 -0800272 * WebRtcOpus_GetBandwidth(...)
273 *
274 * This function returns the current bandwidth.
275 *
276 * Input:
277 * - inst : Encoder context
278 *
279 * Return value : Bandwidth - Success
280 * -1 - Error
281 */
282int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst);
283
284/*
285 * WebRtcOpus_SetBandwidth(...)
286 *
287 * By default Opus decides which bandwidth to encode the signal in depending on
288 * the the bitrate. This function overrules the previous setting and forces the
289 * encoder to encode in narrowband/wideband/fullband/etc.
290 *
291 * Input:
292 * - inst : Encoder context
293 * - bandwidth : New target bandwidth. Valid values are:
294 * OPUS_BANDWIDTH_NARROWBAND
295 * OPUS_BANDWIDTH_MEDIUMBAND
296 * OPUS_BANDWIDTH_WIDEBAND
297 * OPUS_BANDWIDTH_SUPERWIDEBAND
298 * OPUS_BANDWIDTH_FULLBAND
299 *
300 * Return value : 0 - Success
301 * -1 - Error
302 */
303int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth);
304
305/*
minyuec8299f92016-09-27 02:08:47 -0700306 * WebRtcOpus_SetForceChannels(...)
307 *
308 * If the encoder is initialized as a stereo encoder, Opus will by default
309 * decide whether to encode in mono or stereo based on the bitrate. This
310 * function overrules the previous setting, and forces the encoder to encode
311 * in auto/mono/stereo.
312 *
313 * If the Encoder is initialized as a mono encoder, and one tries to force
314 * stereo, the function will return an error.
315 *
316 * Input:
317 * - inst : Encoder context
318 * - num_channels : 0 - Not forced
319 * 1 - Mono
320 * 2 - Stereo
321 *
322 * Return value : 0 - Success
323 * -1 - Error
324 */
minyue41b9c802016-10-06 07:13:54 -0700325int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels);
minyuec8299f92016-09-27 02:08:47 -0700326
Peter Kasting69558702016-01-12 16:26:35 -0800327int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, size_t channels);
Alex Loiko50b8c392019-04-03 15:12:01 +0200328
329/****************************************************************************
330 * WebRtcOpus_MultistreamDecoderCreate(...)
331 *
332 * This function creates an Opus decoder with any supported channel count.
333 *
334 * Input:
335 * - channels : number of channels.
336 * - coupled_streams : number of coupled streams, as described in
337 * RFC 7845.
338 * - channel_mapping : the channel mapping; pointer to array of
339 * `channel` bytes, as described in RFC 7845.
340 *
341 * Output:
342 * - inst : a pointer to a Decoder context that is created
343 * if success.
344 *
345 * Return value : 0 - Success
346 * -1 - Error
347 */
348int16_t WebRtcOpus_MultistreamDecoderCreate(
349 OpusDecInst** inst,
350 size_t channels,
351 size_t coupled_streams,
352 const unsigned char* channel_mapping);
353
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000354int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst);
355
356/****************************************************************************
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000357 * WebRtcOpus_DecoderChannels(...)
358 *
359 * This function returns the number of channels created for Opus decoder.
360 */
Peter Kasting69558702016-01-12 16:26:35 -0800361size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000362
363/****************************************************************************
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000364 * WebRtcOpus_DecoderInit(...)
365 *
366 * This function resets state of the decoder.
367 *
368 * Input:
369 * - inst : Decoder context
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000370 */
Karl Wiberg43766482015-08-27 15:22:11 +0200371void WebRtcOpus_DecoderInit(OpusDecInst* inst);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000372
373/****************************************************************************
374 * WebRtcOpus_Decode(...)
375 *
376 * This function decodes an Opus packet into one or more audio frames at the
377 * ACM interface's sampling rate (32 kHz).
378 *
379 * Input:
380 * - inst : Decoder context
381 * - encoded : Encoded data
382 * - encoded_bytes : Bytes in encoded vector
383 *
384 * Output:
385 * - decoded : The decoded vector
386 * - audio_type : 1 normal, 2 CNG (for Opus it should
387 * always return 1 since we're not using Opus's
388 * built-in DTX/CNG scheme)
389 *
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +0000390 * Return value : >0 - Samples per channel in decoded vector
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000391 * -1 - Error
392 */
Yves Gerey665174f2018-06-19 15:03:05 +0200393int WebRtcOpus_Decode(OpusDecInst* inst,
394 const uint8_t* encoded,
395 size_t encoded_bytes,
396 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700397 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000398
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000399/****************************************************************************
400 * WebRtcOpus_DecodePlc(...)
401 *
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000402 * This function processes PLC for opus frame(s).
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000403 * Input:
404 * - inst : Decoder context
405 * - number_of_lost_frames : Number of PLC frames to produce
406 *
407 * Output:
408 * - decoded : The decoded vector
409 *
410 * Return value : >0 - number of samples in decoded PLC vector
411 * -1 - Error
412 */
Yves Gerey665174f2018-06-19 15:03:05 +0200413int WebRtcOpus_DecodePlc(OpusDecInst* inst,
414 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700415 int number_of_lost_frames);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000416
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000417/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000418 * WebRtcOpus_DecodeFec(...)
419 *
420 * This function decodes the FEC data from an Opus packet into one or more audio
421 * frames at the ACM interface's sampling rate (32 kHz).
422 *
423 * Input:
424 * - inst : Decoder context
425 * - encoded : Encoded data
426 * - encoded_bytes : Bytes in encoded vector
427 *
428 * Output:
429 * - decoded : The decoded vector (previous frame)
430 *
431 * Return value : >0 - Samples per channel in decoded vector
432 * 0 - No FEC data in the packet
433 * -1 - Error
434 */
Yves Gerey665174f2018-06-19 15:03:05 +0200435int WebRtcOpus_DecodeFec(OpusDecInst* inst,
436 const uint8_t* encoded,
437 size_t encoded_bytes,
438 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700439 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000440
441/****************************************************************************
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000442 * WebRtcOpus_DurationEst(...)
443 *
444 * This function calculates the duration of an opus packet.
445 * Input:
446 * - inst : Decoder context
447 * - payload : Encoded data pointer
448 * - payload_length_bytes : Bytes of encoded data
449 *
Minyue323b1322015-05-25 13:49:37 +0200450 * Return value : The duration of the packet, in samples per
451 * channel.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000452 */
453int WebRtcOpus_DurationEst(OpusDecInst* inst,
454 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700455 size_t payload_length_bytes);
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000456
minyuel6d92bf52015-09-23 15:20:39 +0200457/****************************************************************************
458 * WebRtcOpus_PlcDuration(...)
459 *
460 * This function calculates the duration of a frame returned by packet loss
461 * concealment (PLC).
462 *
463 * Input:
464 * - inst : Decoder context
465 *
466 * Return value : The duration of a frame returned by PLC, in
467 * samples per channel.
468 */
469int WebRtcOpus_PlcDuration(OpusDecInst* inst);
470
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000471/* TODO(minyue): Check whether it is needed to add a decoder context to the
472 * arguments, like WebRtcOpus_DurationEst(...). In fact, the packet itself tells
473 * the duration. The decoder context in WebRtcOpus_DurationEst(...) is not used.
474 * So it may be advisable to remove it from WebRtcOpus_DurationEst(...). */
475
476/****************************************************************************
477 * WebRtcOpus_FecDurationEst(...)
478 *
479 * This function calculates the duration of the FEC data within an opus packet.
480 * Input:
481 * - payload : Encoded data pointer
482 * - payload_length_bytes : Bytes of encoded data
483 *
484 * Return value : >0 - The duration of the FEC data in the
Minyue323b1322015-05-25 13:49:37 +0200485 * packet in samples per channel.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000486 * 0 - No FEC data in the packet.
487 */
488int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700489 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000490
491/****************************************************************************
492 * WebRtcOpus_PacketHasFec(...)
493 *
494 * This function detects if an opus packet has FEC.
495 * Input:
496 * - payload : Encoded data pointer
497 * - payload_length_bytes : Bytes of encoded data
498 *
499 * Return value : 0 - the packet does NOT contain FEC.
500 * 1 - the packet contains FEC.
501 */
502int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700503 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000504
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000505#ifdef __cplusplus
506} // extern "C"
507#endif
508
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200509#endif // MODULES_AUDIO_CODING_CODECS_OPUS_OPUS_INTERFACE_H_