blob: 4bac365a89a0a62f370088ab23e02237464735a6 [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#include "modules/audio_coding/codecs/opus/opus_interface.h"
kwiberg2e486462016-08-23 05:54:25 -070012
Niels Möller0aa7e372020-01-07 14:23:54 +010013#include <cstdlib>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/checks.h"
Minyue Li8e83c7a2019-11-04 14:47:52 +010016#include "system_wrappers/include/field_trial.h"
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000017
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000018enum {
minyue2e03c662017-02-01 17:31:11 -080019#if WEBRTC_OPUS_SUPPORT_120MS_PTIME
20 /* Maximum supported frame size in WebRTC is 120 ms. */
21 kWebRtcOpusMaxEncodeFrameSizeMs = 120,
22#else
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +000023 /* Maximum supported frame size in WebRTC is 60 ms. */
24 kWebRtcOpusMaxEncodeFrameSizeMs = 60,
minyue2e03c662017-02-01 17:31:11 -080025#endif
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000026
tina.legrand@webrtc.org45426ea2013-07-03 13:32:04 +000027 /* The format allows up to 120 ms frames. Since we don't control the other
28 * side, we must allow for packets of that size. NetEq is currently limited
29 * to 60 ms on the receive side. */
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000030 kWebRtcOpusMaxDecodeFrameSizeMs = 120,
Minyue Li8e83c7a2019-11-04 14:47:52 +010031
32 // Duration of audio that each call to packet loss concealment covers.
33 kWebRtcOpusPlcFrameSizeMs = 10,
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000034};
35
Minyue Li8e83c7a2019-11-04 14:47:52 +010036constexpr char kPlcUsePrevDecodedSamplesFieldTrial[] =
37 "WebRTC-Audio-OpusPlcUsePrevDecodedSamples";
38
Karl Wiberga1d1a1e2019-05-28 14:41:07 +020039static int FrameSizePerChannel(int frame_size_ms, int sample_rate_hz) {
40 RTC_DCHECK_GT(frame_size_ms, 0);
41 RTC_DCHECK_EQ(frame_size_ms % 10, 0);
42 RTC_DCHECK_GT(sample_rate_hz, 0);
43 RTC_DCHECK_EQ(sample_rate_hz % 1000, 0);
44 return frame_size_ms * (sample_rate_hz / 1000);
45}
46
47// Maximum sample count per channel.
48static int MaxFrameSizePerChannel(int sample_rate_hz) {
49 return FrameSizePerChannel(kWebRtcOpusMaxDecodeFrameSizeMs, sample_rate_hz);
50}
51
52// Default sample count per channel.
53static int DefaultFrameSizePerChannel(int sample_rate_hz) {
54 return FrameSizePerChannel(20, sample_rate_hz);
55}
56
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000057int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
Peter Kasting69558702016-01-12 16:26:35 -080058 size_t channels,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020059 int32_t application,
60 int sample_rate_hz) {
minyue3cea2562015-11-10 03:49:26 -080061 int opus_app;
62 if (!inst)
63 return -1;
tina.legrand@webrtc.orgd0d41492012-12-20 09:23:10 +000064
minyue3cea2562015-11-10 03:49:26 -080065 switch (application) {
66 case 0:
67 opus_app = OPUS_APPLICATION_VOIP;
68 break;
69 case 1:
70 opus_app = OPUS_APPLICATION_AUDIO;
71 break;
72 default:
73 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000074 }
minyue3cea2562015-11-10 03:49:26 -080075
Minyue Li54d02782019-10-29 21:36:13 +010076 OpusEncInst* state =
77 reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
kwiberg2e486462016-08-23 05:54:25 -070078 RTC_DCHECK(state);
minyue3cea2562015-11-10 03:49:26 -080079
minyue3cea2562015-11-10 03:49:26 -080080 int error;
Minyue Li54d02782019-10-29 21:36:13 +010081 state->encoder = opus_encoder_create(
82 sample_rate_hz, static_cast<int>(channels), opus_app, &error);
Alex Loiko7a3e43a2019-01-29 12:27:08 +010083
Minyue Li54d02782019-10-29 21:36:13 +010084 if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
Alex Loiko50b8c392019-04-03 15:12:01 +020085 WebRtcOpus_EncoderFree(state);
86 return -1;
Alex Loiko7a3e43a2019-01-29 12:27:08 +010087 }
88
Alex Loiko50b8c392019-04-03 15:12:01 +020089 state->in_dtx_mode = 0;
90 state->channels = channels;
91
92 *inst = state;
93 return 0;
94}
95
96int16_t WebRtcOpus_MultistreamEncoderCreate(
97 OpusEncInst** inst,
98 size_t channels,
99 int32_t application,
Alex Loikoe5b94162019-04-08 17:19:41 +0200100 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +0200101 size_t coupled_streams,
Minyue Li54d02782019-10-29 21:36:13 +0100102 const unsigned char* channel_mapping) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200103 int opus_app;
104 if (!inst)
105 return -1;
106
107 switch (application) {
108 case 0:
109 opus_app = OPUS_APPLICATION_VOIP;
110 break;
111 case 1:
112 opus_app = OPUS_APPLICATION_AUDIO;
113 break;
114 default:
115 return -1;
116 }
117
Minyue Li54d02782019-10-29 21:36:13 +0100118 OpusEncInst* state =
119 reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
Alex Loiko50b8c392019-04-03 15:12:01 +0200120 RTC_DCHECK(state);
121
Alex Loiko50b8c392019-04-03 15:12:01 +0200122 int error;
123 state->multistream_encoder =
Minyue Li54d02782019-10-29 21:36:13 +0100124 opus_multistream_encoder_create(48000, channels, streams, coupled_streams,
125 channel_mapping, opus_app, &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200126
Minyue Li54d02782019-10-29 21:36:13 +0100127 if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
minyue3cea2562015-11-10 03:49:26 -0800128 WebRtcOpus_EncoderFree(state);
129 return -1;
130 }
131
132 state->in_dtx_mode = 0;
133 state->channels = channels;
134
135 *inst = state;
136 return 0;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000137}
138
139int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000140 if (inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200141 if (inst->encoder) {
142 opus_encoder_destroy(inst->encoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100143 } else {
Alex Loiko50b8c392019-04-03 15:12:01 +0200144 opus_multistream_encoder_destroy(inst->multistream_encoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100145 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000146 free(inst);
147 return 0;
148 } else {
149 return -1;
150 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000151}
152
Peter Kastingbba78072015-06-11 19:02:46 -0700153int WebRtcOpus_Encode(OpusEncInst* inst,
154 const int16_t* audio_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700155 size_t samples,
156 size_t length_encoded_buffer,
Peter Kastingbba78072015-06-11 19:02:46 -0700157 uint8_t* encoded) {
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000158 int res;
159
160 if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) {
161 return -1;
162 }
163
Alex Loiko50b8c392019-04-03 15:12:01 +0200164 if (inst->encoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100165 res = opus_encode(inst->encoder, (const opus_int16*)audio_in,
166 static_cast<int>(samples), encoded,
167 static_cast<opus_int32>(length_encoded_buffer));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100168 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100169 res = opus_multistream_encode(
170 inst->multistream_encoder, (const opus_int16*)audio_in,
171 static_cast<int>(samples), encoded,
172 static_cast<opus_int32>(length_encoded_buffer));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100173 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000174
flim64a7eab2016-08-12 04:36:05 -0700175 if (res <= 0) {
176 return -1;
177 }
178
179 if (res <= 2) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000180 // Indicates DTX since the packet has nothing but a header. In principle,
181 // there is no need to send this packet. However, we do transmit the first
182 // occurrence to let the decoder know that the encoder enters DTX mode.
183 if (inst->in_dtx_mode) {
184 return 0;
185 } else {
186 inst->in_dtx_mode = 1;
flim92382452017-02-10 13:50:38 -0800187 return res;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000188 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000189 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000190
flim64a7eab2016-08-12 04:36:05 -0700191 inst->in_dtx_mode = 0;
192 return res;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000193}
194
Minyue Li54d02782019-10-29 21:36:13 +0100195#define ENCODER_CTL(inst, vargs) \
196 (inst->encoder \
197 ? opus_encoder_ctl(inst->encoder, vargs) \
198 : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs))
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100199
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000200int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000201 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100202 return ENCODER_CTL(inst, OPUS_SET_BITRATE(rate));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000203 } else {
204 return -1;
205 }
206}
207
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000208int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate) {
209 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100210 return ENCODER_CTL(inst, OPUS_SET_PACKET_LOSS_PERC(loss_rate));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000211 } else {
212 return -1;
213 }
214}
215
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000216int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000217 opus_int32 set_bandwidth;
218
219 if (!inst)
220 return -1;
221
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000222 if (frequency_hz <= 8000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000223 set_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000224 } else if (frequency_hz <= 12000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000225 set_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000226 } else if (frequency_hz <= 16000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000227 set_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000228 } else if (frequency_hz <= 24000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000229 set_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
230 } else {
231 set_bandwidth = OPUS_BANDWIDTH_FULLBAND;
232 }
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100233 return ENCODER_CTL(inst, OPUS_SET_MAX_BANDWIDTH(set_bandwidth));
234}
235
236int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
237 int32_t* result_hz) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200238 if (inst->encoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100239 if (opus_encoder_ctl(inst->encoder, OPUS_GET_MAX_BANDWIDTH(result_hz)) ==
240 OPUS_OK) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100241 return 0;
242 }
243 return -1;
244 }
245
246 opus_int32 max_bandwidth;
247 int s;
248 int ret;
249
250 max_bandwidth = 0;
251 ret = OPUS_OK;
252 s = 0;
253 while (ret == OPUS_OK) {
Minyue Li54d02782019-10-29 21:36:13 +0100254 OpusEncoder* enc;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100255 opus_int32 bandwidth;
256
257 ret = ENCODER_CTL(inst, OPUS_MULTISTREAM_GET_ENCODER_STATE(s, &enc));
258 if (ret == OPUS_BAD_ARG)
259 break;
260 if (ret != OPUS_OK)
261 return -1;
262 if (opus_encoder_ctl(enc, OPUS_GET_MAX_BANDWIDTH(&bandwidth)) != OPUS_OK)
263 return -1;
264
265 if (max_bandwidth != 0 && max_bandwidth != bandwidth)
266 return -1;
267
268 max_bandwidth = bandwidth;
269 s++;
270 }
271 *result_hz = max_bandwidth;
272 return 0;
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000273}
274
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000275int16_t WebRtcOpus_EnableFec(OpusEncInst* inst) {
276 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100277 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(1));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000278 } else {
279 return -1;
280 }
281}
282
283int16_t WebRtcOpus_DisableFec(OpusEncInst* inst) {
284 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100285 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(0));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000286 } else {
287 return -1;
288 }
289}
290
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000291int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) {
Minyue Li092041c2015-05-11 12:19:35 +0200292 if (!inst) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000293 return -1;
294 }
Minyue Li092041c2015-05-11 12:19:35 +0200295
296 // To prevent Opus from entering CELT-only mode by forcing signal type to
297 // voice to make sure that DTX behaves correctly. Currently, DTX does not
298 // last long during a pure silence, if the signal type is not forced.
299 // TODO(minyue): Remove the signal type forcing when Opus DTX works properly
300 // without it.
Minyue Li54d02782019-10-29 21:36:13 +0100301 int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
Minyue Li092041c2015-05-11 12:19:35 +0200302 if (ret != OPUS_OK)
303 return ret;
304
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100305 return ENCODER_CTL(inst, OPUS_SET_DTX(1));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000306}
307
308int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) {
309 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100310 int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_AUTO));
Minyue Li092041c2015-05-11 12:19:35 +0200311 if (ret != OPUS_OK)
312 return ret;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100313 return ENCODER_CTL(inst, OPUS_SET_DTX(0));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000314 } else {
315 return -1;
316 }
317}
318
soren28dc2852017-04-06 05:48:36 -0700319int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst) {
320 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100321 return ENCODER_CTL(inst, OPUS_SET_VBR(0));
soren28dc2852017-04-06 05:48:36 -0700322 } else {
323 return -1;
324 }
325}
326
327int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst) {
328 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100329 return ENCODER_CTL(inst, OPUS_SET_VBR(1));
soren28dc2852017-04-06 05:48:36 -0700330 } else {
331 return -1;
332 }
333}
334
minyue@webrtc.org04546882014-03-07 08:55:48 +0000335int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) {
336 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100337 return ENCODER_CTL(inst, OPUS_SET_COMPLEXITY(complexity));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000338 } else {
339 return -1;
340 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000341}
342
Alex Luebseeb27652017-11-20 11:13:56 -0800343int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst) {
344 if (!inst) {
345 return -1;
346 }
347 int32_t bandwidth;
Minyue Li54d02782019-10-29 21:36:13 +0100348 if (ENCODER_CTL(inst, OPUS_GET_BANDWIDTH(&bandwidth)) == 0) {
Alex Luebseeb27652017-11-20 11:13:56 -0800349 return bandwidth;
350 } else {
351 return -1;
352 }
Alex Luebseeb27652017-11-20 11:13:56 -0800353}
354
355int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) {
356 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100357 return ENCODER_CTL(inst, OPUS_SET_BANDWIDTH(bandwidth));
Alex Luebseeb27652017-11-20 11:13:56 -0800358 } else {
359 return -1;
360 }
361}
362
minyue41b9c802016-10-06 07:13:54 -0700363int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels) {
minyuec8299f92016-09-27 02:08:47 -0700364 if (!inst)
365 return -1;
366 if (num_channels == 0) {
Minyue Li54d02782019-10-29 21:36:13 +0100367 return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO));
minyuec8299f92016-09-27 02:08:47 -0700368 } else if (num_channels == 1 || num_channels == 2) {
Minyue Li54d02782019-10-29 21:36:13 +0100369 return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(num_channels));
minyuec8299f92016-09-27 02:08:47 -0700370 } else {
371 return -1;
372 }
373}
374
Minyue Li332274d2019-11-13 16:05:46 +0100375int32_t WebRtcOpus_GetInDtx(OpusEncInst* inst) {
376 if (!inst) {
377 return -1;
378 }
Tom Anderson422f9dd2020-02-24 12:01:11 -0800379#ifdef OPUS_GET_IN_DTX
Minyue Li332274d2019-11-13 16:05:46 +0100380 int32_t in_dtx;
381 if (ENCODER_CTL(inst, OPUS_GET_IN_DTX(&in_dtx)) == 0) {
382 return in_dtx;
Minyue Li332274d2019-11-13 16:05:46 +0100383 }
Tom Anderson422f9dd2020-02-24 12:01:11 -0800384#endif
385 return -1;
Minyue Li332274d2019-11-13 16:05:46 +0100386}
387
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200388int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst,
389 size_t channels,
390 int sample_rate_hz) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000391 int error;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000392 OpusDecInst* state;
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000393
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000394 if (inst != NULL) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100395 // Create Opus decoder state.
Minyue Li54d02782019-10-29 21:36:13 +0100396 state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000397 if (state == NULL) {
398 return -1;
399 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000400
Minyue Li54d02782019-10-29 21:36:13 +0100401 state->decoder =
402 opus_decoder_create(sample_rate_hz, static_cast<int>(channels), &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200403 if (error == OPUS_OK && state->decoder) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100404 // Creation of memory all ok.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000405 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200406 state->sample_rate_hz = sample_rate_hz;
Minyue Li8e83c7a2019-11-04 14:47:52 +0100407 state->plc_use_prev_decoded_samples =
408 webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial);
409 if (state->plc_use_prev_decoded_samples) {
410 state->prev_decoded_samples =
411 DefaultFrameSizePerChannel(state->sample_rate_hz);
412 }
413 state->in_dtx_mode = 0;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000414 *inst = state;
415 return 0;
416 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000417
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100418 // If memory allocation was unsuccessful, free the entire state.
Alex Loiko50b8c392019-04-03 15:12:01 +0200419 if (state->decoder) {
420 opus_decoder_destroy(state->decoder);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000421 }
422 free(state);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000423 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000424 return -1;
425}
426
Alex Loiko50b8c392019-04-03 15:12:01 +0200427int16_t WebRtcOpus_MultistreamDecoderCreate(
Minyue Li54d02782019-10-29 21:36:13 +0100428 OpusDecInst** inst,
429 size_t channels,
Alex Loikoe5b94162019-04-08 17:19:41 +0200430 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +0200431 size_t coupled_streams,
432 const unsigned char* channel_mapping) {
433 int error;
434 OpusDecInst* state;
435
436 if (inst != NULL) {
437 // Create Opus decoder state.
Minyue Li54d02782019-10-29 21:36:13 +0100438 state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
Alex Loiko50b8c392019-04-03 15:12:01 +0200439 if (state == NULL) {
440 return -1;
441 }
442
Alex Loiko50b8c392019-04-03 15:12:01 +0200443 // Create new memory, always at 48000 Hz.
444 state->multistream_decoder = opus_multistream_decoder_create(
Minyue Li54d02782019-10-29 21:36:13 +0100445 48000, channels, streams, coupled_streams, channel_mapping, &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200446
447 if (error == OPUS_OK && state->multistream_decoder) {
448 // Creation of memory all ok.
449 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200450 state->sample_rate_hz = 48000;
Minyue Li8e83c7a2019-11-04 14:47:52 +0100451 state->plc_use_prev_decoded_samples =
452 webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial);
453 if (state->plc_use_prev_decoded_samples) {
454 state->prev_decoded_samples =
455 DefaultFrameSizePerChannel(state->sample_rate_hz);
456 }
457 state->in_dtx_mode = 0;
Alex Loiko50b8c392019-04-03 15:12:01 +0200458 *inst = state;
459 return 0;
460 }
461
462 // If memory allocation was unsuccessful, free the entire state.
463 opus_multistream_decoder_destroy(state->multistream_decoder);
464 free(state);
465 }
466 return -1;
467}
468
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000469int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000470 if (inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200471 if (inst->decoder) {
472 opus_decoder_destroy(inst->decoder);
473 } else if (inst->multistream_decoder) {
474 opus_multistream_decoder_destroy(inst->multistream_decoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100475 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000476 free(inst);
477 return 0;
478 } else {
479 return -1;
480 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000481}
482
Peter Kasting69558702016-01-12 16:26:35 -0800483size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst) {
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000484 return inst->channels;
485}
486
Karl Wiberg43766482015-08-27 15:22:11 +0200487void WebRtcOpus_DecoderInit(OpusDecInst* inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200488 if (inst->decoder) {
489 opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100490 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100491 opus_multistream_decoder_ctl(inst->multistream_decoder, OPUS_RESET_STATE);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100492 }
Karl Wiberg43766482015-08-27 15:22:11 +0200493 inst->in_dtx_mode = 0;
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000494}
495
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000496/* For decoder to determine if it is to output speech or comfort noise. */
Peter Kastingdce40cf2015-08-24 14:52:23 -0700497static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000498 // Audio type becomes comfort noise if |encoded_byte| is 1 and keeps
499 // to be so if the following |encoded_byte| are 0 or 1.
500 if (encoded_bytes == 0 && inst->in_dtx_mode) {
501 return 2; // Comfort noise.
henrik.lundindeaf6fb2017-03-01 00:49:18 -0800502 } else if (encoded_bytes == 1 || encoded_bytes == 2) {
503 // TODO(henrik.lundin): There is a slight risk that a 2-byte payload is in
504 // fact a 1-byte TOC with a 1-byte payload. That will be erroneously
505 // interpreted as comfort noise output, but such a payload is probably
506 // faulty anyway.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100507
508 // TODO(webrtc:10218): This is wrong for multistream opus. Then are several
509 // single-stream packets glued together with some packet size bytes in
510 // between. See https://tools.ietf.org/html/rfc6716#appendix-B
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000511 inst->in_dtx_mode = 1;
512 return 2; // Comfort noise.
513 } else {
514 inst->in_dtx_mode = 0;
515 return 0; // Speech.
516 }
517}
518
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000519/* |frame_size| is set to maximum Opus frame size in the normal case, and
520 * is set to the number of samples needed for PLC in case of losses.
521 * It is up to the caller to make sure the value is correct. */
Minyue Li54d02782019-10-29 21:36:13 +0100522static int DecodeNative(OpusDecInst* inst,
523 const uint8_t* encoded,
524 size_t encoded_bytes,
525 int frame_size,
526 int16_t* decoded,
527 int16_t* audio_type,
528 int decode_fec) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100529 int res = -1;
Alex Loiko50b8c392019-04-03 15:12:01 +0200530 if (inst->decoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100531 res = opus_decode(
532 inst->decoder, encoded, static_cast<opus_int32>(encoded_bytes),
533 reinterpret_cast<opus_int16*>(decoded), frame_size, decode_fec);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100534 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100535 res = opus_multistream_decode(inst->multistream_decoder, encoded,
536 static_cast<opus_int32>(encoded_bytes),
537 reinterpret_cast<opus_int16*>(decoded),
538 frame_size, decode_fec);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100539 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000540
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000541 if (res <= 0)
542 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000543
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000544 *audio_type = DetermineAudioType(inst, encoded_bytes);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000545
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000546 return res;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000547}
548
Minyue Lifb075d52019-10-29 21:38:15 +0100549static int DecodePlc(OpusDecInst* inst, int16_t* decoded) {
550 int16_t audio_type = 0;
551 int decoded_samples;
Minyue Li8e83c7a2019-11-04 14:47:52 +0100552 int plc_samples =
553 FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz);
Minyue Lifb075d52019-10-29 21:38:15 +0100554
Minyue Li8e83c7a2019-11-04 14:47:52 +0100555 if (inst->plc_use_prev_decoded_samples) {
556 /* The number of samples we ask for is |number_of_lost_frames| times
557 * |prev_decoded_samples_|. Limit the number of samples to maximum
558 * |MaxFrameSizePerChannel()|. */
559 plc_samples = inst->prev_decoded_samples;
560 const int max_samples_per_channel =
561 MaxFrameSizePerChannel(inst->sample_rate_hz);
562 plc_samples = plc_samples <= max_samples_per_channel
563 ? plc_samples
564 : max_samples_per_channel;
565 }
Minyue Lifb075d52019-10-29 21:38:15 +0100566 decoded_samples =
567 DecodeNative(inst, NULL, 0, plc_samples, decoded, &audio_type, 0);
568 if (decoded_samples < 0) {
569 return -1;
570 }
571
572 return decoded_samples;
573}
574
Minyue Li54d02782019-10-29 21:36:13 +0100575int WebRtcOpus_Decode(OpusDecInst* inst,
576 const uint8_t* encoded,
577 size_t encoded_bytes,
578 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700579 int16_t* audio_type) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000580 int decoded_samples;
581
582 if (encoded_bytes == 0) {
583 *audio_type = DetermineAudioType(inst, encoded_bytes);
Minyue Lifb075d52019-10-29 21:38:15 +0100584 decoded_samples = DecodePlc(inst, decoded);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000585 } else {
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200586 decoded_samples = DecodeNative(inst, encoded, encoded_bytes,
587 MaxFrameSizePerChannel(inst->sample_rate_hz),
588 decoded, audio_type, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000589 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000590 if (decoded_samples < 0) {
591 return -1;
592 }
593
Minyue Li8e83c7a2019-11-04 14:47:52 +0100594 if (inst->plc_use_prev_decoded_samples) {
595 /* Update decoded sample memory, to be used by the PLC in case of losses. */
596 inst->prev_decoded_samples = decoded_samples;
597 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000598
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000599 return decoded_samples;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000600}
601
Minyue Li54d02782019-10-29 21:36:13 +0100602int WebRtcOpus_DecodeFec(OpusDecInst* inst,
603 const uint8_t* encoded,
604 size_t encoded_bytes,
605 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700606 int16_t* audio_type) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000607 int decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000608 int fec_samples;
609
610 if (WebRtcOpus_PacketHasFec(encoded, encoded_bytes) != 1) {
611 return 0;
612 }
613
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200614 fec_samples =
615 opus_packet_get_samples_per_frame(encoded, inst->sample_rate_hz);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000616
Minyue Li54d02782019-10-29 21:36:13 +0100617 decoded_samples = DecodeNative(inst, encoded, encoded_bytes, fec_samples,
618 decoded, audio_type, 1);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000619 if (decoded_samples < 0) {
620 return -1;
621 }
622
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000623 return decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000624}
625
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000626int WebRtcOpus_DurationEst(OpusDecInst* inst,
627 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700628 size_t payload_length_bytes) {
minyuel6d92bf52015-09-23 15:20:39 +0200629 if (payload_length_bytes == 0) {
630 // WebRtcOpus_Decode calls PLC when payload length is zero. So we return
631 // PLC duration correspondingly.
632 return WebRtcOpus_PlcDuration(inst);
633 }
634
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000635 int frames, samples;
Minyue Li54d02782019-10-29 21:36:13 +0100636 frames = opus_packet_get_nb_frames(
637 payload, static_cast<opus_int32>(payload_length_bytes));
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000638 if (frames < 0) {
639 /* Invalid payload data. */
640 return 0;
641 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200642 samples =
643 frames * opus_packet_get_samples_per_frame(payload, inst->sample_rate_hz);
644 if (samples > 120 * inst->sample_rate_hz / 1000) {
645 // More than 120 ms' worth of samples.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000646 return 0;
647 }
648 return samples;
649}
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000650
minyuel6d92bf52015-09-23 15:20:39 +0200651int WebRtcOpus_PlcDuration(OpusDecInst* inst) {
Minyue Li8e83c7a2019-11-04 14:47:52 +0100652 if (inst->plc_use_prev_decoded_samples) {
653 /* The number of samples we ask for is |number_of_lost_frames| times
654 * |prev_decoded_samples_|. Limit the number of samples to maximum
655 * |MaxFrameSizePerChannel()|. */
656 const int plc_samples = inst->prev_decoded_samples;
657 const int max_samples_per_channel =
658 MaxFrameSizePerChannel(inst->sample_rate_hz);
659 return plc_samples <= max_samples_per_channel ? plc_samples
660 : max_samples_per_channel;
661 }
662 return FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz);
minyuel6d92bf52015-09-23 15:20:39 +0200663}
664
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000665int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200666 size_t payload_length_bytes,
667 int sample_rate_hz) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000668 if (WebRtcOpus_PacketHasFec(payload, payload_length_bytes) != 1) {
669 return 0;
670 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200671 const int samples =
672 opus_packet_get_samples_per_frame(payload, sample_rate_hz);
673 const int samples_per_ms = sample_rate_hz / 1000;
674 if (samples < 10 * samples_per_ms || samples > 120 * samples_per_ms) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000675 /* Invalid payload duration. */
676 return 0;
677 }
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000678 return samples;
679}
680
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200681// This method is based on Definition of the Opus Audio Codec
682// (https://tools.ietf.org/html/rfc6716). Basically, this method is based on
683// parsing the LP layer of an Opus packet, particularly the LBRR flag.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000684int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700685 size_t payload_length_bytes) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700686 if (payload == NULL || payload_length_bytes == 0)
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000687 return 0;
688
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200689 // In CELT_ONLY mode, packets should not have FEC.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000690 if (payload[0] & 0x80)
691 return 0;
692
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200693 // Max number of frames in an Opus packet is 48.
694 opus_int16 frame_sizes[48];
Minyue Li54d02782019-10-29 21:36:13 +0100695 const unsigned char* frame_data[48];
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000696
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200697 // Parse packet to get the frames. But we only care about the first frame,
698 // since we can only decode the FEC from the first one.
Minyue Li54d02782019-10-29 21:36:13 +0100699 if (opus_packet_parse(payload, static_cast<opus_int32>(payload_length_bytes),
700 NULL, frame_data, frame_sizes, NULL) < 0) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000701 return 0;
702 }
703
704 if (frame_sizes[0] <= 1) {
705 return 0;
706 }
707
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200708 // For computing the payload length in ms, the sample rate is not important
709 // since it cancels out. We use 48 kHz, but any valid sample rate would work.
710 int payload_length_ms =
711 opus_packet_get_samples_per_frame(payload, 48000) / 48;
712 if (payload_length_ms < 10)
713 payload_length_ms = 10;
714
715 int silk_frames;
716 switch (payload_length_ms) {
717 case 10:
718 case 20:
719 silk_frames = 1;
720 break;
721 case 40:
722 silk_frames = 2;
723 break;
724 case 60:
725 silk_frames = 3;
726 break;
727 default:
Minyue Li54d02782019-10-29 21:36:13 +0100728 return 0; // It is actually even an invalid packet.
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200729 }
730
731 const int channels = opus_packet_get_nb_channels(payload);
732 RTC_DCHECK(channels == 1 || channels == 2);
733
734 // A frame starts with the LP layer. The LP layer begins with two to eight
735 // header bits.These consist of one VAD bit per SILK frame (up to 3),
736 // followed by a single flag indicating the presence of LBRR frames.
737 // For a stereo packet, these first flags correspond to the mid channel, and
738 // a second set of flags is included for the side channel. Because these are
739 // the first symbols decoded by the range coder and because they are coded
740 // as binary values with uniform probability, they can be extracted directly
741 // from the most significant bits of the first byte of compressed data.
742 for (int n = 0; n < channels; n++) {
743 // The LBRR bit for channel 1 is on the (|silk_frames| + 1)-th bit, and
744 // that of channel 2 is on the |(|silk_frames| + 1) * 2 + 1|-th bit.
745 if (frame_data[0][0] & (0x80 >> ((n + 1) * (silk_frames + 1) - 1)))
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000746 return 1;
747 }
748
749 return 0;
750}