blob: 2f475cbed11ad174f3a58760ebc6a4fc3cbd70c2 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/checks.h"
Minyue Li8e83c7a2019-11-04 14:47:52 +010014#include "system_wrappers/include/field_trial.h"
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000015
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000016enum {
minyue2e03c662017-02-01 17:31:11 -080017#if WEBRTC_OPUS_SUPPORT_120MS_PTIME
18 /* Maximum supported frame size in WebRTC is 120 ms. */
19 kWebRtcOpusMaxEncodeFrameSizeMs = 120,
20#else
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +000021 /* Maximum supported frame size in WebRTC is 60 ms. */
22 kWebRtcOpusMaxEncodeFrameSizeMs = 60,
minyue2e03c662017-02-01 17:31:11 -080023#endif
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000024
tina.legrand@webrtc.org45426ea2013-07-03 13:32:04 +000025 /* The format allows up to 120 ms frames. Since we don't control the other
26 * side, we must allow for packets of that size. NetEq is currently limited
27 * to 60 ms on the receive side. */
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000028 kWebRtcOpusMaxDecodeFrameSizeMs = 120,
Minyue Li8e83c7a2019-11-04 14:47:52 +010029
30 // Duration of audio that each call to packet loss concealment covers.
31 kWebRtcOpusPlcFrameSizeMs = 10,
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000032};
33
Minyue Li8e83c7a2019-11-04 14:47:52 +010034constexpr char kPlcUsePrevDecodedSamplesFieldTrial[] =
35 "WebRTC-Audio-OpusPlcUsePrevDecodedSamples";
36
Karl Wiberga1d1a1e2019-05-28 14:41:07 +020037static int FrameSizePerChannel(int frame_size_ms, int sample_rate_hz) {
38 RTC_DCHECK_GT(frame_size_ms, 0);
39 RTC_DCHECK_EQ(frame_size_ms % 10, 0);
40 RTC_DCHECK_GT(sample_rate_hz, 0);
41 RTC_DCHECK_EQ(sample_rate_hz % 1000, 0);
42 return frame_size_ms * (sample_rate_hz / 1000);
43}
44
45// Maximum sample count per channel.
46static int MaxFrameSizePerChannel(int sample_rate_hz) {
47 return FrameSizePerChannel(kWebRtcOpusMaxDecodeFrameSizeMs, sample_rate_hz);
48}
49
50// Default sample count per channel.
51static int DefaultFrameSizePerChannel(int sample_rate_hz) {
52 return FrameSizePerChannel(20, sample_rate_hz);
53}
54
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000055int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
Peter Kasting69558702016-01-12 16:26:35 -080056 size_t channels,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020057 int32_t application,
58 int sample_rate_hz) {
minyue3cea2562015-11-10 03:49:26 -080059 int opus_app;
60 if (!inst)
61 return -1;
tina.legrand@webrtc.orgd0d41492012-12-20 09:23:10 +000062
minyue3cea2562015-11-10 03:49:26 -080063 switch (application) {
64 case 0:
65 opus_app = OPUS_APPLICATION_VOIP;
66 break;
67 case 1:
68 opus_app = OPUS_APPLICATION_AUDIO;
69 break;
70 default:
71 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000072 }
minyue3cea2562015-11-10 03:49:26 -080073
Minyue Li54d02782019-10-29 21:36:13 +010074 OpusEncInst* state =
75 reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
kwiberg2e486462016-08-23 05:54:25 -070076 RTC_DCHECK(state);
minyue3cea2562015-11-10 03:49:26 -080077
minyue3cea2562015-11-10 03:49:26 -080078 int error;
Minyue Li54d02782019-10-29 21:36:13 +010079 state->encoder = opus_encoder_create(
80 sample_rate_hz, static_cast<int>(channels), opus_app, &error);
Alex Loiko7a3e43a2019-01-29 12:27:08 +010081
Minyue Li54d02782019-10-29 21:36:13 +010082 if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
Alex Loiko50b8c392019-04-03 15:12:01 +020083 WebRtcOpus_EncoderFree(state);
84 return -1;
Alex Loiko7a3e43a2019-01-29 12:27:08 +010085 }
86
Alex Loiko50b8c392019-04-03 15:12:01 +020087 state->in_dtx_mode = 0;
88 state->channels = channels;
89
90 *inst = state;
91 return 0;
92}
93
94int16_t WebRtcOpus_MultistreamEncoderCreate(
95 OpusEncInst** inst,
96 size_t channels,
97 int32_t application,
Alex Loikoe5b94162019-04-08 17:19:41 +020098 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +020099 size_t coupled_streams,
Minyue Li54d02782019-10-29 21:36:13 +0100100 const unsigned char* channel_mapping) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200101 int opus_app;
102 if (!inst)
103 return -1;
104
105 switch (application) {
106 case 0:
107 opus_app = OPUS_APPLICATION_VOIP;
108 break;
109 case 1:
110 opus_app = OPUS_APPLICATION_AUDIO;
111 break;
112 default:
113 return -1;
114 }
115
Minyue Li54d02782019-10-29 21:36:13 +0100116 OpusEncInst* state =
117 reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
Alex Loiko50b8c392019-04-03 15:12:01 +0200118 RTC_DCHECK(state);
119
Alex Loiko50b8c392019-04-03 15:12:01 +0200120 int error;
121 state->multistream_encoder =
Minyue Li54d02782019-10-29 21:36:13 +0100122 opus_multistream_encoder_create(48000, channels, streams, coupled_streams,
123 channel_mapping, opus_app, &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200124
Minyue Li54d02782019-10-29 21:36:13 +0100125 if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
minyue3cea2562015-11-10 03:49:26 -0800126 WebRtcOpus_EncoderFree(state);
127 return -1;
128 }
129
130 state->in_dtx_mode = 0;
131 state->channels = channels;
132
133 *inst = state;
134 return 0;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000135}
136
137int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000138 if (inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200139 if (inst->encoder) {
140 opus_encoder_destroy(inst->encoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100141 } else {
Alex Loiko50b8c392019-04-03 15:12:01 +0200142 opus_multistream_encoder_destroy(inst->multistream_encoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100143 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000144 free(inst);
145 return 0;
146 } else {
147 return -1;
148 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000149}
150
Peter Kastingbba78072015-06-11 19:02:46 -0700151int WebRtcOpus_Encode(OpusEncInst* inst,
152 const int16_t* audio_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700153 size_t samples,
154 size_t length_encoded_buffer,
Peter Kastingbba78072015-06-11 19:02:46 -0700155 uint8_t* encoded) {
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000156 int res;
157
158 if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) {
159 return -1;
160 }
161
Alex Loiko50b8c392019-04-03 15:12:01 +0200162 if (inst->encoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100163 res = opus_encode(inst->encoder, (const opus_int16*)audio_in,
164 static_cast<int>(samples), encoded,
165 static_cast<opus_int32>(length_encoded_buffer));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100166 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100167 res = opus_multistream_encode(
168 inst->multistream_encoder, (const opus_int16*)audio_in,
169 static_cast<int>(samples), encoded,
170 static_cast<opus_int32>(length_encoded_buffer));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100171 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000172
flim64a7eab2016-08-12 04:36:05 -0700173 if (res <= 0) {
174 return -1;
175 }
176
177 if (res <= 2) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000178 // Indicates DTX since the packet has nothing but a header. In principle,
179 // there is no need to send this packet. However, we do transmit the first
180 // occurrence to let the decoder know that the encoder enters DTX mode.
181 if (inst->in_dtx_mode) {
182 return 0;
183 } else {
184 inst->in_dtx_mode = 1;
flim92382452017-02-10 13:50:38 -0800185 return res;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000186 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000187 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000188
flim64a7eab2016-08-12 04:36:05 -0700189 inst->in_dtx_mode = 0;
190 return res;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000191}
192
Minyue Li54d02782019-10-29 21:36:13 +0100193#define ENCODER_CTL(inst, vargs) \
194 (inst->encoder \
195 ? opus_encoder_ctl(inst->encoder, vargs) \
196 : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs))
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100197
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000198int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000199 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100200 return ENCODER_CTL(inst, OPUS_SET_BITRATE(rate));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000201 } else {
202 return -1;
203 }
204}
205
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000206int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate) {
207 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100208 return ENCODER_CTL(inst, OPUS_SET_PACKET_LOSS_PERC(loss_rate));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000209 } else {
210 return -1;
211 }
212}
213
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000214int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000215 opus_int32 set_bandwidth;
216
217 if (!inst)
218 return -1;
219
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000220 if (frequency_hz <= 8000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000221 set_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000222 } else if (frequency_hz <= 12000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000223 set_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000224 } else if (frequency_hz <= 16000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000225 set_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000226 } else if (frequency_hz <= 24000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000227 set_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
228 } else {
229 set_bandwidth = OPUS_BANDWIDTH_FULLBAND;
230 }
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100231 return ENCODER_CTL(inst, OPUS_SET_MAX_BANDWIDTH(set_bandwidth));
232}
233
234int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
235 int32_t* result_hz) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200236 if (inst->encoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100237 if (opus_encoder_ctl(inst->encoder, OPUS_GET_MAX_BANDWIDTH(result_hz)) ==
238 OPUS_OK) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100239 return 0;
240 }
241 return -1;
242 }
243
244 opus_int32 max_bandwidth;
245 int s;
246 int ret;
247
248 max_bandwidth = 0;
249 ret = OPUS_OK;
250 s = 0;
251 while (ret == OPUS_OK) {
Minyue Li54d02782019-10-29 21:36:13 +0100252 OpusEncoder* enc;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100253 opus_int32 bandwidth;
254
255 ret = ENCODER_CTL(inst, OPUS_MULTISTREAM_GET_ENCODER_STATE(s, &enc));
256 if (ret == OPUS_BAD_ARG)
257 break;
258 if (ret != OPUS_OK)
259 return -1;
260 if (opus_encoder_ctl(enc, OPUS_GET_MAX_BANDWIDTH(&bandwidth)) != OPUS_OK)
261 return -1;
262
263 if (max_bandwidth != 0 && max_bandwidth != bandwidth)
264 return -1;
265
266 max_bandwidth = bandwidth;
267 s++;
268 }
269 *result_hz = max_bandwidth;
270 return 0;
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000271}
272
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000273int16_t WebRtcOpus_EnableFec(OpusEncInst* inst) {
274 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100275 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(1));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000276 } else {
277 return -1;
278 }
279}
280
281int16_t WebRtcOpus_DisableFec(OpusEncInst* inst) {
282 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100283 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(0));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000284 } else {
285 return -1;
286 }
287}
288
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000289int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) {
Minyue Li092041c2015-05-11 12:19:35 +0200290 if (!inst) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000291 return -1;
292 }
Minyue Li092041c2015-05-11 12:19:35 +0200293
294 // To prevent Opus from entering CELT-only mode by forcing signal type to
295 // voice to make sure that DTX behaves correctly. Currently, DTX does not
296 // last long during a pure silence, if the signal type is not forced.
297 // TODO(minyue): Remove the signal type forcing when Opus DTX works properly
298 // without it.
Minyue Li54d02782019-10-29 21:36:13 +0100299 int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
Minyue Li092041c2015-05-11 12:19:35 +0200300 if (ret != OPUS_OK)
301 return ret;
302
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100303 return ENCODER_CTL(inst, OPUS_SET_DTX(1));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000304}
305
306int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) {
307 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100308 int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_AUTO));
Minyue Li092041c2015-05-11 12:19:35 +0200309 if (ret != OPUS_OK)
310 return ret;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100311 return ENCODER_CTL(inst, OPUS_SET_DTX(0));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000312 } else {
313 return -1;
314 }
315}
316
soren28dc2852017-04-06 05:48:36 -0700317int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst) {
318 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100319 return ENCODER_CTL(inst, OPUS_SET_VBR(0));
soren28dc2852017-04-06 05:48:36 -0700320 } else {
321 return -1;
322 }
323}
324
325int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst) {
326 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100327 return ENCODER_CTL(inst, OPUS_SET_VBR(1));
soren28dc2852017-04-06 05:48:36 -0700328 } else {
329 return -1;
330 }
331}
332
minyue@webrtc.org04546882014-03-07 08:55:48 +0000333int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) {
334 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100335 return ENCODER_CTL(inst, OPUS_SET_COMPLEXITY(complexity));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000336 } else {
337 return -1;
338 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000339}
340
Alex Luebseeb27652017-11-20 11:13:56 -0800341int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst) {
342 if (!inst) {
343 return -1;
344 }
345 int32_t bandwidth;
Minyue Li54d02782019-10-29 21:36:13 +0100346 if (ENCODER_CTL(inst, OPUS_GET_BANDWIDTH(&bandwidth)) == 0) {
Alex Luebseeb27652017-11-20 11:13:56 -0800347 return bandwidth;
348 } else {
349 return -1;
350 }
Alex Luebseeb27652017-11-20 11:13:56 -0800351}
352
353int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) {
354 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100355 return ENCODER_CTL(inst, OPUS_SET_BANDWIDTH(bandwidth));
Alex Luebseeb27652017-11-20 11:13:56 -0800356 } else {
357 return -1;
358 }
359}
360
minyue41b9c802016-10-06 07:13:54 -0700361int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels) {
minyuec8299f92016-09-27 02:08:47 -0700362 if (!inst)
363 return -1;
364 if (num_channels == 0) {
Minyue Li54d02782019-10-29 21:36:13 +0100365 return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO));
minyuec8299f92016-09-27 02:08:47 -0700366 } else if (num_channels == 1 || num_channels == 2) {
Minyue Li54d02782019-10-29 21:36:13 +0100367 return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(num_channels));
minyuec8299f92016-09-27 02:08:47 -0700368 } else {
369 return -1;
370 }
371}
372
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200373int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst,
374 size_t channels,
375 int sample_rate_hz) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000376 int error;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000377 OpusDecInst* state;
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000378
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000379 if (inst != NULL) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100380 // Create Opus decoder state.
Minyue Li54d02782019-10-29 21:36:13 +0100381 state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000382 if (state == NULL) {
383 return -1;
384 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000385
Minyue Li54d02782019-10-29 21:36:13 +0100386 state->decoder =
387 opus_decoder_create(sample_rate_hz, static_cast<int>(channels), &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200388 if (error == OPUS_OK && state->decoder) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100389 // Creation of memory all ok.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000390 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200391 state->sample_rate_hz = sample_rate_hz;
Minyue Li8e83c7a2019-11-04 14:47:52 +0100392 state->plc_use_prev_decoded_samples =
393 webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial);
394 if (state->plc_use_prev_decoded_samples) {
395 state->prev_decoded_samples =
396 DefaultFrameSizePerChannel(state->sample_rate_hz);
397 }
398 state->in_dtx_mode = 0;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000399 *inst = state;
400 return 0;
401 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000402
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100403 // If memory allocation was unsuccessful, free the entire state.
Alex Loiko50b8c392019-04-03 15:12:01 +0200404 if (state->decoder) {
405 opus_decoder_destroy(state->decoder);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000406 }
407 free(state);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000408 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000409 return -1;
410}
411
Alex Loiko50b8c392019-04-03 15:12:01 +0200412int16_t WebRtcOpus_MultistreamDecoderCreate(
Minyue Li54d02782019-10-29 21:36:13 +0100413 OpusDecInst** inst,
414 size_t channels,
Alex Loikoe5b94162019-04-08 17:19:41 +0200415 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +0200416 size_t coupled_streams,
417 const unsigned char* channel_mapping) {
418 int error;
419 OpusDecInst* state;
420
421 if (inst != NULL) {
422 // Create Opus decoder state.
Minyue Li54d02782019-10-29 21:36:13 +0100423 state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
Alex Loiko50b8c392019-04-03 15:12:01 +0200424 if (state == NULL) {
425 return -1;
426 }
427
Alex Loiko50b8c392019-04-03 15:12:01 +0200428 // Create new memory, always at 48000 Hz.
429 state->multistream_decoder = opus_multistream_decoder_create(
Minyue Li54d02782019-10-29 21:36:13 +0100430 48000, channels, streams, coupled_streams, channel_mapping, &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200431
432 if (error == OPUS_OK && state->multistream_decoder) {
433 // Creation of memory all ok.
434 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200435 state->sample_rate_hz = 48000;
Minyue Li8e83c7a2019-11-04 14:47:52 +0100436 state->plc_use_prev_decoded_samples =
437 webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial);
438 if (state->plc_use_prev_decoded_samples) {
439 state->prev_decoded_samples =
440 DefaultFrameSizePerChannel(state->sample_rate_hz);
441 }
442 state->in_dtx_mode = 0;
Alex Loiko50b8c392019-04-03 15:12:01 +0200443 *inst = state;
444 return 0;
445 }
446
447 // If memory allocation was unsuccessful, free the entire state.
448 opus_multistream_decoder_destroy(state->multistream_decoder);
449 free(state);
450 }
451 return -1;
452}
453
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000454int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000455 if (inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200456 if (inst->decoder) {
457 opus_decoder_destroy(inst->decoder);
458 } else if (inst->multistream_decoder) {
459 opus_multistream_decoder_destroy(inst->multistream_decoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100460 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000461 free(inst);
462 return 0;
463 } else {
464 return -1;
465 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000466}
467
Peter Kasting69558702016-01-12 16:26:35 -0800468size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst) {
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000469 return inst->channels;
470}
471
Karl Wiberg43766482015-08-27 15:22:11 +0200472void WebRtcOpus_DecoderInit(OpusDecInst* inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200473 if (inst->decoder) {
474 opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100475 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100476 opus_multistream_decoder_ctl(inst->multistream_decoder, OPUS_RESET_STATE);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100477 }
Karl Wiberg43766482015-08-27 15:22:11 +0200478 inst->in_dtx_mode = 0;
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000479}
480
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000481/* For decoder to determine if it is to output speech or comfort noise. */
Peter Kastingdce40cf2015-08-24 14:52:23 -0700482static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000483 // Audio type becomes comfort noise if |encoded_byte| is 1 and keeps
484 // to be so if the following |encoded_byte| are 0 or 1.
485 if (encoded_bytes == 0 && inst->in_dtx_mode) {
486 return 2; // Comfort noise.
henrik.lundindeaf6fb2017-03-01 00:49:18 -0800487 } else if (encoded_bytes == 1 || encoded_bytes == 2) {
488 // TODO(henrik.lundin): There is a slight risk that a 2-byte payload is in
489 // fact a 1-byte TOC with a 1-byte payload. That will be erroneously
490 // interpreted as comfort noise output, but such a payload is probably
491 // faulty anyway.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100492
493 // TODO(webrtc:10218): This is wrong for multistream opus. Then are several
494 // single-stream packets glued together with some packet size bytes in
495 // between. See https://tools.ietf.org/html/rfc6716#appendix-B
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000496 inst->in_dtx_mode = 1;
497 return 2; // Comfort noise.
498 } else {
499 inst->in_dtx_mode = 0;
500 return 0; // Speech.
501 }
502}
503
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000504/* |frame_size| is set to maximum Opus frame size in the normal case, and
505 * is set to the number of samples needed for PLC in case of losses.
506 * It is up to the caller to make sure the value is correct. */
Minyue Li54d02782019-10-29 21:36:13 +0100507static int DecodeNative(OpusDecInst* inst,
508 const uint8_t* encoded,
509 size_t encoded_bytes,
510 int frame_size,
511 int16_t* decoded,
512 int16_t* audio_type,
513 int decode_fec) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100514 int res = -1;
Alex Loiko50b8c392019-04-03 15:12:01 +0200515 if (inst->decoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100516 res = opus_decode(
517 inst->decoder, encoded, static_cast<opus_int32>(encoded_bytes),
518 reinterpret_cast<opus_int16*>(decoded), frame_size, decode_fec);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100519 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100520 res = opus_multistream_decode(inst->multistream_decoder, encoded,
521 static_cast<opus_int32>(encoded_bytes),
522 reinterpret_cast<opus_int16*>(decoded),
523 frame_size, decode_fec);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100524 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000525
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000526 if (res <= 0)
527 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000528
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000529 *audio_type = DetermineAudioType(inst, encoded_bytes);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000530
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000531 return res;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000532}
533
Minyue Lifb075d52019-10-29 21:38:15 +0100534static int DecodePlc(OpusDecInst* inst, int16_t* decoded) {
535 int16_t audio_type = 0;
536 int decoded_samples;
Minyue Li8e83c7a2019-11-04 14:47:52 +0100537 int plc_samples =
538 FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz);
Minyue Lifb075d52019-10-29 21:38:15 +0100539
Minyue Li8e83c7a2019-11-04 14:47:52 +0100540 if (inst->plc_use_prev_decoded_samples) {
541 /* The number of samples we ask for is |number_of_lost_frames| times
542 * |prev_decoded_samples_|. Limit the number of samples to maximum
543 * |MaxFrameSizePerChannel()|. */
544 plc_samples = inst->prev_decoded_samples;
545 const int max_samples_per_channel =
546 MaxFrameSizePerChannel(inst->sample_rate_hz);
547 plc_samples = plc_samples <= max_samples_per_channel
548 ? plc_samples
549 : max_samples_per_channel;
550 }
Minyue Lifb075d52019-10-29 21:38:15 +0100551 decoded_samples =
552 DecodeNative(inst, NULL, 0, plc_samples, decoded, &audio_type, 0);
553 if (decoded_samples < 0) {
554 return -1;
555 }
556
557 return decoded_samples;
558}
559
Minyue Li54d02782019-10-29 21:36:13 +0100560int WebRtcOpus_Decode(OpusDecInst* inst,
561 const uint8_t* encoded,
562 size_t encoded_bytes,
563 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700564 int16_t* audio_type) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000565 int decoded_samples;
566
567 if (encoded_bytes == 0) {
568 *audio_type = DetermineAudioType(inst, encoded_bytes);
Minyue Lifb075d52019-10-29 21:38:15 +0100569 decoded_samples = DecodePlc(inst, decoded);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000570 } else {
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200571 decoded_samples = DecodeNative(inst, encoded, encoded_bytes,
572 MaxFrameSizePerChannel(inst->sample_rate_hz),
573 decoded, audio_type, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000574 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000575 if (decoded_samples < 0) {
576 return -1;
577 }
578
Minyue Li8e83c7a2019-11-04 14:47:52 +0100579 if (inst->plc_use_prev_decoded_samples) {
580 /* Update decoded sample memory, to be used by the PLC in case of losses. */
581 inst->prev_decoded_samples = decoded_samples;
582 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000583
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000584 return decoded_samples;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000585}
586
Minyue Li54d02782019-10-29 21:36:13 +0100587int WebRtcOpus_DecodeFec(OpusDecInst* inst,
588 const uint8_t* encoded,
589 size_t encoded_bytes,
590 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700591 int16_t* audio_type) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000592 int decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000593 int fec_samples;
594
595 if (WebRtcOpus_PacketHasFec(encoded, encoded_bytes) != 1) {
596 return 0;
597 }
598
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200599 fec_samples =
600 opus_packet_get_samples_per_frame(encoded, inst->sample_rate_hz);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000601
Minyue Li54d02782019-10-29 21:36:13 +0100602 decoded_samples = DecodeNative(inst, encoded, encoded_bytes, fec_samples,
603 decoded, audio_type, 1);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000604 if (decoded_samples < 0) {
605 return -1;
606 }
607
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000608 return decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000609}
610
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000611int WebRtcOpus_DurationEst(OpusDecInst* inst,
612 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700613 size_t payload_length_bytes) {
minyuel6d92bf52015-09-23 15:20:39 +0200614 if (payload_length_bytes == 0) {
615 // WebRtcOpus_Decode calls PLC when payload length is zero. So we return
616 // PLC duration correspondingly.
617 return WebRtcOpus_PlcDuration(inst);
618 }
619
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000620 int frames, samples;
Minyue Li54d02782019-10-29 21:36:13 +0100621 frames = opus_packet_get_nb_frames(
622 payload, static_cast<opus_int32>(payload_length_bytes));
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000623 if (frames < 0) {
624 /* Invalid payload data. */
625 return 0;
626 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200627 samples =
628 frames * opus_packet_get_samples_per_frame(payload, inst->sample_rate_hz);
629 if (samples > 120 * inst->sample_rate_hz / 1000) {
630 // More than 120 ms' worth of samples.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000631 return 0;
632 }
633 return samples;
634}
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000635
minyuel6d92bf52015-09-23 15:20:39 +0200636int WebRtcOpus_PlcDuration(OpusDecInst* inst) {
Minyue Li8e83c7a2019-11-04 14:47:52 +0100637 if (inst->plc_use_prev_decoded_samples) {
638 /* The number of samples we ask for is |number_of_lost_frames| times
639 * |prev_decoded_samples_|. Limit the number of samples to maximum
640 * |MaxFrameSizePerChannel()|. */
641 const int plc_samples = inst->prev_decoded_samples;
642 const int max_samples_per_channel =
643 MaxFrameSizePerChannel(inst->sample_rate_hz);
644 return plc_samples <= max_samples_per_channel ? plc_samples
645 : max_samples_per_channel;
646 }
647 return FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz);
minyuel6d92bf52015-09-23 15:20:39 +0200648}
649
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000650int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200651 size_t payload_length_bytes,
652 int sample_rate_hz) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000653 if (WebRtcOpus_PacketHasFec(payload, payload_length_bytes) != 1) {
654 return 0;
655 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200656 const int samples =
657 opus_packet_get_samples_per_frame(payload, sample_rate_hz);
658 const int samples_per_ms = sample_rate_hz / 1000;
659 if (samples < 10 * samples_per_ms || samples > 120 * samples_per_ms) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000660 /* Invalid payload duration. */
661 return 0;
662 }
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000663 return samples;
664}
665
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200666// This method is based on Definition of the Opus Audio Codec
667// (https://tools.ietf.org/html/rfc6716). Basically, this method is based on
668// parsing the LP layer of an Opus packet, particularly the LBRR flag.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000669int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700670 size_t payload_length_bytes) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700671 if (payload == NULL || payload_length_bytes == 0)
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000672 return 0;
673
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200674 // In CELT_ONLY mode, packets should not have FEC.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000675 if (payload[0] & 0x80)
676 return 0;
677
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200678 // Max number of frames in an Opus packet is 48.
679 opus_int16 frame_sizes[48];
Minyue Li54d02782019-10-29 21:36:13 +0100680 const unsigned char* frame_data[48];
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000681
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200682 // Parse packet to get the frames. But we only care about the first frame,
683 // since we can only decode the FEC from the first one.
Minyue Li54d02782019-10-29 21:36:13 +0100684 if (opus_packet_parse(payload, static_cast<opus_int32>(payload_length_bytes),
685 NULL, frame_data, frame_sizes, NULL) < 0) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000686 return 0;
687 }
688
689 if (frame_sizes[0] <= 1) {
690 return 0;
691 }
692
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200693 // For computing the payload length in ms, the sample rate is not important
694 // since it cancels out. We use 48 kHz, but any valid sample rate would work.
695 int payload_length_ms =
696 opus_packet_get_samples_per_frame(payload, 48000) / 48;
697 if (payload_length_ms < 10)
698 payload_length_ms = 10;
699
700 int silk_frames;
701 switch (payload_length_ms) {
702 case 10:
703 case 20:
704 silk_frames = 1;
705 break;
706 case 40:
707 silk_frames = 2;
708 break;
709 case 60:
710 silk_frames = 3;
711 break;
712 default:
Minyue Li54d02782019-10-29 21:36:13 +0100713 return 0; // It is actually even an invalid packet.
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200714 }
715
716 const int channels = opus_packet_get_nb_channels(payload);
717 RTC_DCHECK(channels == 1 || channels == 2);
718
719 // A frame starts with the LP layer. The LP layer begins with two to eight
720 // header bits.These consist of one VAD bit per SILK frame (up to 3),
721 // followed by a single flag indicating the presence of LBRR frames.
722 // For a stereo packet, these first flags correspond to the mid channel, and
723 // a second set of flags is included for the side channel. Because these are
724 // the first symbols decoded by the range coder and because they are coded
725 // as binary values with uniform probability, they can be extracted directly
726 // from the most significant bits of the first byte of compressed data.
727 for (int n = 0; n < channels; n++) {
728 // The LBRR bit for channel 1 is on the (|silk_frames| + 1)-th bit, and
729 // that of channel 2 is on the |(|silk_frames| + 1) * 2 + 1|-th bit.
730 if (frame_data[0][0] & (0x80 >> ((n + 1) * (silk_frames + 1) - 1)))
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000731 return 1;
732 }
733
734 return 0;
735}