blob: 0e97734f38f85dbf68f86998223969339420bacb [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 *
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
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100128/****************************************************************************
129 * WebRtcOpus_GetMaxPlaybackRate(...)
130 *
131 * Queries the maximum playback rate for encoding. If different single-stream
132 * encoders have different maximum playback rates, this function fails.
133 *
134 * Input:
135 * - inst : Encoder context.
136 * Output:
137 * - result_hz : The maximum playback rate in Hz.
138 * Return value : 0 - Success
139 * -1 - Error
140 */
141int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
142 int32_t* result_hz);
143
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000144/* TODO(minyue): Check whether an API to check the FEC and the packet loss rate
145 * is needed. It might not be very useful since there are not many use cases and
146 * the caller can always maintain the states. */
147
148/****************************************************************************
149 * WebRtcOpus_EnableFec()
150 *
151 * This function enables FEC for encoding.
152 *
153 * Input:
154 * - inst : Encoder context
155 *
156 * Return value : 0 - Success
157 * -1 - Error
158 */
159int16_t WebRtcOpus_EnableFec(OpusEncInst* inst);
160
161/****************************************************************************
162 * WebRtcOpus_DisableFec()
163 *
164 * This function disables FEC for encoding.
165 *
166 * Input:
167 * - inst : Encoder context
168 *
169 * Return value : 0 - Success
170 * -1 - Error
171 */
172int16_t WebRtcOpus_DisableFec(OpusEncInst* inst);
173
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000174/****************************************************************************
175 * WebRtcOpus_EnableDtx()
176 *
177 * This function enables Opus internal DTX for encoding.
178 *
179 * Input:
180 * - inst : Encoder context
181 *
182 * Return value : 0 - Success
183 * -1 - Error
184 */
185int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst);
186
187/****************************************************************************
188 * WebRtcOpus_DisableDtx()
189 *
190 * This function disables Opus internal DTX for encoding.
191 *
192 * Input:
193 * - inst : Encoder context
194 *
195 * Return value : 0 - Success
196 * -1 - Error
197 */
198int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst);
199
soren28dc2852017-04-06 05:48:36 -0700200/****************************************************************************
201 * WebRtcOpus_EnableCbr()
202 *
203 * This function enables CBR for encoding.
204 *
205 * Input:
206 * - inst : Encoder context
207 *
208 * Return value : 0 - Success
209 * -1 - Error
210 */
211int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst);
212
213/****************************************************************************
214 * WebRtcOpus_DisableCbr()
215 *
216 * This function disables CBR for encoding.
217 *
218 * Input:
219 * - inst : Encoder context
220 *
221 * Return value : 0 - Success
222 * -1 - Error
223 */
224int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst);
225
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000226/*
minyue@webrtc.org04546882014-03-07 08:55:48 +0000227 * WebRtcOpus_SetComplexity(...)
228 *
229 * This function adjusts the computational complexity. The effect is the same as
230 * calling the complexity setting of Opus as an Opus encoder related CTL.
231 *
232 * Input:
233 * - inst : Encoder context
234 * - complexity : New target complexity (0-10, inclusive)
235 *
236 * Return value : 0 - Success
237 * -1 - Error
238 */
239int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity);
240
minyuec8299f92016-09-27 02:08:47 -0700241/*
Alex Luebseeb27652017-11-20 11:13:56 -0800242 * WebRtcOpus_GetBandwidth(...)
243 *
244 * This function returns the current bandwidth.
245 *
246 * Input:
247 * - inst : Encoder context
248 *
249 * Return value : Bandwidth - Success
250 * -1 - Error
251 */
252int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst);
253
254/*
255 * WebRtcOpus_SetBandwidth(...)
256 *
257 * By default Opus decides which bandwidth to encode the signal in depending on
258 * the the bitrate. This function overrules the previous setting and forces the
259 * encoder to encode in narrowband/wideband/fullband/etc.
260 *
261 * Input:
262 * - inst : Encoder context
263 * - bandwidth : New target bandwidth. Valid values are:
264 * OPUS_BANDWIDTH_NARROWBAND
265 * OPUS_BANDWIDTH_MEDIUMBAND
266 * OPUS_BANDWIDTH_WIDEBAND
267 * OPUS_BANDWIDTH_SUPERWIDEBAND
268 * OPUS_BANDWIDTH_FULLBAND
269 *
270 * Return value : 0 - Success
271 * -1 - Error
272 */
273int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth);
274
275/*
minyuec8299f92016-09-27 02:08:47 -0700276 * WebRtcOpus_SetForceChannels(...)
277 *
278 * If the encoder is initialized as a stereo encoder, Opus will by default
279 * decide whether to encode in mono or stereo based on the bitrate. This
280 * function overrules the previous setting, and forces the encoder to encode
281 * in auto/mono/stereo.
282 *
283 * If the Encoder is initialized as a mono encoder, and one tries to force
284 * stereo, the function will return an error.
285 *
286 * Input:
287 * - inst : Encoder context
288 * - num_channels : 0 - Not forced
289 * 1 - Mono
290 * 2 - Stereo
291 *
292 * Return value : 0 - Success
293 * -1 - Error
294 */
minyue41b9c802016-10-06 07:13:54 -0700295int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels);
minyuec8299f92016-09-27 02:08:47 -0700296
Peter Kasting69558702016-01-12 16:26:35 -0800297int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, size_t channels);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000298int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst);
299
300/****************************************************************************
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000301 * WebRtcOpus_DecoderChannels(...)
302 *
303 * This function returns the number of channels created for Opus decoder.
304 */
Peter Kasting69558702016-01-12 16:26:35 -0800305size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000306
307/****************************************************************************
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000308 * WebRtcOpus_DecoderInit(...)
309 *
310 * This function resets state of the decoder.
311 *
312 * Input:
313 * - inst : Decoder context
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000314 */
Karl Wiberg43766482015-08-27 15:22:11 +0200315void WebRtcOpus_DecoderInit(OpusDecInst* inst);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000316
317/****************************************************************************
318 * WebRtcOpus_Decode(...)
319 *
320 * This function decodes an Opus packet into one or more audio frames at the
321 * ACM interface's sampling rate (32 kHz).
322 *
323 * Input:
324 * - inst : Decoder context
325 * - encoded : Encoded data
326 * - encoded_bytes : Bytes in encoded vector
327 *
328 * Output:
329 * - decoded : The decoded vector
330 * - audio_type : 1 normal, 2 CNG (for Opus it should
331 * always return 1 since we're not using Opus's
332 * built-in DTX/CNG scheme)
333 *
minyue@webrtc.orgecbe0aa2013-08-12 06:48:09 +0000334 * Return value : >0 - Samples per channel in decoded vector
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000335 * -1 - Error
336 */
Yves Gerey665174f2018-06-19 15:03:05 +0200337int WebRtcOpus_Decode(OpusDecInst* inst,
338 const uint8_t* encoded,
339 size_t encoded_bytes,
340 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700341 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000342
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000343/****************************************************************************
344 * WebRtcOpus_DecodePlc(...)
345 *
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000346 * This function processes PLC for opus frame(s).
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000347 * Input:
348 * - inst : Decoder context
349 * - number_of_lost_frames : Number of PLC frames to produce
350 *
351 * Output:
352 * - decoded : The decoded vector
353 *
354 * Return value : >0 - number of samples in decoded PLC vector
355 * -1 - Error
356 */
Yves Gerey665174f2018-06-19 15:03:05 +0200357int WebRtcOpus_DecodePlc(OpusDecInst* inst,
358 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700359 int number_of_lost_frames);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000360
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000361/****************************************************************************
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000362 * WebRtcOpus_DecodeFec(...)
363 *
364 * This function decodes the FEC data from an Opus packet into one or more audio
365 * frames at the ACM interface's sampling rate (32 kHz).
366 *
367 * Input:
368 * - inst : Decoder context
369 * - encoded : Encoded data
370 * - encoded_bytes : Bytes in encoded vector
371 *
372 * Output:
373 * - decoded : The decoded vector (previous frame)
374 *
375 * Return value : >0 - Samples per channel in decoded vector
376 * 0 - No FEC data in the packet
377 * -1 - Error
378 */
Yves Gerey665174f2018-06-19 15:03:05 +0200379int WebRtcOpus_DecodeFec(OpusDecInst* inst,
380 const uint8_t* encoded,
381 size_t encoded_bytes,
382 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700383 int16_t* audio_type);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000384
385/****************************************************************************
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000386 * WebRtcOpus_DurationEst(...)
387 *
388 * This function calculates the duration of an opus packet.
389 * Input:
390 * - inst : Decoder context
391 * - payload : Encoded data pointer
392 * - payload_length_bytes : Bytes of encoded data
393 *
Minyue323b1322015-05-25 13:49:37 +0200394 * Return value : The duration of the packet, in samples per
395 * channel.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000396 */
397int WebRtcOpus_DurationEst(OpusDecInst* inst,
398 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700399 size_t payload_length_bytes);
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000400
minyuel6d92bf52015-09-23 15:20:39 +0200401/****************************************************************************
402 * WebRtcOpus_PlcDuration(...)
403 *
404 * This function calculates the duration of a frame returned by packet loss
405 * concealment (PLC).
406 *
407 * Input:
408 * - inst : Decoder context
409 *
410 * Return value : The duration of a frame returned by PLC, in
411 * samples per channel.
412 */
413int WebRtcOpus_PlcDuration(OpusDecInst* inst);
414
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000415/* TODO(minyue): Check whether it is needed to add a decoder context to the
416 * arguments, like WebRtcOpus_DurationEst(...). In fact, the packet itself tells
417 * the duration. The decoder context in WebRtcOpus_DurationEst(...) is not used.
418 * So it may be advisable to remove it from WebRtcOpus_DurationEst(...). */
419
420/****************************************************************************
421 * WebRtcOpus_FecDurationEst(...)
422 *
423 * This function calculates the duration of the FEC data within an opus packet.
424 * Input:
425 * - payload : Encoded data pointer
426 * - payload_length_bytes : Bytes of encoded data
427 *
428 * Return value : >0 - The duration of the FEC data in the
Minyue323b1322015-05-25 13:49:37 +0200429 * packet in samples per channel.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000430 * 0 - No FEC data in the packet.
431 */
432int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700433 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000434
435/****************************************************************************
436 * WebRtcOpus_PacketHasFec(...)
437 *
438 * This function detects if an opus packet has FEC.
439 * Input:
440 * - payload : Encoded data pointer
441 * - payload_length_bytes : Bytes of encoded data
442 *
443 * Return value : 0 - the packet does NOT contain FEC.
444 * 1 - the packet contains FEC.
445 */
446int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700447 size_t payload_length_bytes);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000448
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000449#ifdef __cplusplus
450} // extern "C"
451#endif
452
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200453#endif // MODULES_AUDIO_CODING_CODECS_OPUS_OPUS_INTERFACE_H_