blob: fc3d3ffdddb91778c810ad09baa7085e1127ea75 [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"
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000014
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000015enum {
minyue2e03c662017-02-01 17:31:11 -080016#if WEBRTC_OPUS_SUPPORT_120MS_PTIME
17 /* Maximum supported frame size in WebRTC is 120 ms. */
18 kWebRtcOpusMaxEncodeFrameSizeMs = 120,
19#else
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +000020 /* Maximum supported frame size in WebRTC is 60 ms. */
21 kWebRtcOpusMaxEncodeFrameSizeMs = 60,
minyue2e03c662017-02-01 17:31:11 -080022#endif
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000023
tina.legrand@webrtc.org45426ea2013-07-03 13:32:04 +000024 /* The format allows up to 120 ms frames. Since we don't control the other
25 * side, we must allow for packets of that size. NetEq is currently limited
26 * to 60 ms on the receive side. */
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000027 kWebRtcOpusMaxDecodeFrameSizeMs = 120,
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000028};
29
Karl Wiberga1d1a1e2019-05-28 14:41:07 +020030static int FrameSizePerChannel(int frame_size_ms, int sample_rate_hz) {
31 RTC_DCHECK_GT(frame_size_ms, 0);
32 RTC_DCHECK_EQ(frame_size_ms % 10, 0);
33 RTC_DCHECK_GT(sample_rate_hz, 0);
34 RTC_DCHECK_EQ(sample_rate_hz % 1000, 0);
35 return frame_size_ms * (sample_rate_hz / 1000);
36}
37
38// Maximum sample count per channel.
39static int MaxFrameSizePerChannel(int sample_rate_hz) {
40 return FrameSizePerChannel(kWebRtcOpusMaxDecodeFrameSizeMs, sample_rate_hz);
41}
42
43// Default sample count per channel.
44static int DefaultFrameSizePerChannel(int sample_rate_hz) {
45 return FrameSizePerChannel(20, sample_rate_hz);
46}
47
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000048int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
Peter Kasting69558702016-01-12 16:26:35 -080049 size_t channels,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020050 int32_t application,
51 int sample_rate_hz) {
minyue3cea2562015-11-10 03:49:26 -080052 int opus_app;
53 if (!inst)
54 return -1;
tina.legrand@webrtc.orgd0d41492012-12-20 09:23:10 +000055
minyue3cea2562015-11-10 03:49:26 -080056 switch (application) {
57 case 0:
58 opus_app = OPUS_APPLICATION_VOIP;
59 break;
60 case 1:
61 opus_app = OPUS_APPLICATION_AUDIO;
62 break;
63 default:
64 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000065 }
minyue3cea2562015-11-10 03:49:26 -080066
Minyue Li54d02782019-10-29 21:36:13 +010067 OpusEncInst* state =
68 reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
kwiberg2e486462016-08-23 05:54:25 -070069 RTC_DCHECK(state);
minyue3cea2562015-11-10 03:49:26 -080070
minyue3cea2562015-11-10 03:49:26 -080071 int error;
Minyue Li54d02782019-10-29 21:36:13 +010072 state->encoder = opus_encoder_create(
73 sample_rate_hz, static_cast<int>(channels), opus_app, &error);
Alex Loiko7a3e43a2019-01-29 12:27:08 +010074
Minyue Li54d02782019-10-29 21:36:13 +010075 if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
Alex Loiko50b8c392019-04-03 15:12:01 +020076 WebRtcOpus_EncoderFree(state);
77 return -1;
Alex Loiko7a3e43a2019-01-29 12:27:08 +010078 }
79
Alex Loiko50b8c392019-04-03 15:12:01 +020080 state->in_dtx_mode = 0;
81 state->channels = channels;
82
83 *inst = state;
84 return 0;
85}
86
87int16_t WebRtcOpus_MultistreamEncoderCreate(
88 OpusEncInst** inst,
89 size_t channels,
90 int32_t application,
Alex Loikoe5b94162019-04-08 17:19:41 +020091 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +020092 size_t coupled_streams,
Minyue Li54d02782019-10-29 21:36:13 +010093 const unsigned char* channel_mapping) {
Alex Loiko50b8c392019-04-03 15:12:01 +020094 int opus_app;
95 if (!inst)
96 return -1;
97
98 switch (application) {
99 case 0:
100 opus_app = OPUS_APPLICATION_VOIP;
101 break;
102 case 1:
103 opus_app = OPUS_APPLICATION_AUDIO;
104 break;
105 default:
106 return -1;
107 }
108
Minyue Li54d02782019-10-29 21:36:13 +0100109 OpusEncInst* state =
110 reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
Alex Loiko50b8c392019-04-03 15:12:01 +0200111 RTC_DCHECK(state);
112
Alex Loiko50b8c392019-04-03 15:12:01 +0200113 int error;
114 state->multistream_encoder =
Minyue Li54d02782019-10-29 21:36:13 +0100115 opus_multistream_encoder_create(48000, channels, streams, coupled_streams,
116 channel_mapping, opus_app, &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200117
Minyue Li54d02782019-10-29 21:36:13 +0100118 if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
minyue3cea2562015-11-10 03:49:26 -0800119 WebRtcOpus_EncoderFree(state);
120 return -1;
121 }
122
123 state->in_dtx_mode = 0;
124 state->channels = channels;
125
126 *inst = state;
127 return 0;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000128}
129
130int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000131 if (inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200132 if (inst->encoder) {
133 opus_encoder_destroy(inst->encoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100134 } else {
Alex Loiko50b8c392019-04-03 15:12:01 +0200135 opus_multistream_encoder_destroy(inst->multistream_encoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100136 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000137 free(inst);
138 return 0;
139 } else {
140 return -1;
141 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000142}
143
Peter Kastingbba78072015-06-11 19:02:46 -0700144int WebRtcOpus_Encode(OpusEncInst* inst,
145 const int16_t* audio_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700146 size_t samples,
147 size_t length_encoded_buffer,
Peter Kastingbba78072015-06-11 19:02:46 -0700148 uint8_t* encoded) {
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000149 int res;
150
151 if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) {
152 return -1;
153 }
154
Alex Loiko50b8c392019-04-03 15:12:01 +0200155 if (inst->encoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100156 res = opus_encode(inst->encoder, (const opus_int16*)audio_in,
157 static_cast<int>(samples), encoded,
158 static_cast<opus_int32>(length_encoded_buffer));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100159 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100160 res = opus_multistream_encode(
161 inst->multistream_encoder, (const opus_int16*)audio_in,
162 static_cast<int>(samples), encoded,
163 static_cast<opus_int32>(length_encoded_buffer));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100164 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000165
flim64a7eab2016-08-12 04:36:05 -0700166 if (res <= 0) {
167 return -1;
168 }
169
170 if (res <= 2) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000171 // Indicates DTX since the packet has nothing but a header. In principle,
172 // there is no need to send this packet. However, we do transmit the first
173 // occurrence to let the decoder know that the encoder enters DTX mode.
174 if (inst->in_dtx_mode) {
175 return 0;
176 } else {
177 inst->in_dtx_mode = 1;
flim92382452017-02-10 13:50:38 -0800178 return res;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000179 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000180 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000181
flim64a7eab2016-08-12 04:36:05 -0700182 inst->in_dtx_mode = 0;
183 return res;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000184}
185
Minyue Li54d02782019-10-29 21:36:13 +0100186#define ENCODER_CTL(inst, vargs) \
187 (inst->encoder \
188 ? opus_encoder_ctl(inst->encoder, vargs) \
189 : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs))
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100190
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000191int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000192 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100193 return ENCODER_CTL(inst, OPUS_SET_BITRATE(rate));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000194 } else {
195 return -1;
196 }
197}
198
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000199int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate) {
200 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100201 return ENCODER_CTL(inst, OPUS_SET_PACKET_LOSS_PERC(loss_rate));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000202 } else {
203 return -1;
204 }
205}
206
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000207int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000208 opus_int32 set_bandwidth;
209
210 if (!inst)
211 return -1;
212
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000213 if (frequency_hz <= 8000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000214 set_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000215 } else if (frequency_hz <= 12000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000216 set_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000217 } else if (frequency_hz <= 16000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000218 set_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000219 } else if (frequency_hz <= 24000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000220 set_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
221 } else {
222 set_bandwidth = OPUS_BANDWIDTH_FULLBAND;
223 }
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100224 return ENCODER_CTL(inst, OPUS_SET_MAX_BANDWIDTH(set_bandwidth));
225}
226
227int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
228 int32_t* result_hz) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200229 if (inst->encoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100230 if (opus_encoder_ctl(inst->encoder, OPUS_GET_MAX_BANDWIDTH(result_hz)) ==
231 OPUS_OK) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100232 return 0;
233 }
234 return -1;
235 }
236
237 opus_int32 max_bandwidth;
238 int s;
239 int ret;
240
241 max_bandwidth = 0;
242 ret = OPUS_OK;
243 s = 0;
244 while (ret == OPUS_OK) {
Minyue Li54d02782019-10-29 21:36:13 +0100245 OpusEncoder* enc;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100246 opus_int32 bandwidth;
247
248 ret = ENCODER_CTL(inst, OPUS_MULTISTREAM_GET_ENCODER_STATE(s, &enc));
249 if (ret == OPUS_BAD_ARG)
250 break;
251 if (ret != OPUS_OK)
252 return -1;
253 if (opus_encoder_ctl(enc, OPUS_GET_MAX_BANDWIDTH(&bandwidth)) != OPUS_OK)
254 return -1;
255
256 if (max_bandwidth != 0 && max_bandwidth != bandwidth)
257 return -1;
258
259 max_bandwidth = bandwidth;
260 s++;
261 }
262 *result_hz = max_bandwidth;
263 return 0;
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000264}
265
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000266int16_t WebRtcOpus_EnableFec(OpusEncInst* inst) {
267 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100268 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(1));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000269 } else {
270 return -1;
271 }
272}
273
274int16_t WebRtcOpus_DisableFec(OpusEncInst* inst) {
275 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100276 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(0));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000277 } else {
278 return -1;
279 }
280}
281
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000282int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) {
Minyue Li092041c2015-05-11 12:19:35 +0200283 if (!inst) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000284 return -1;
285 }
Minyue Li092041c2015-05-11 12:19:35 +0200286
287 // To prevent Opus from entering CELT-only mode by forcing signal type to
288 // voice to make sure that DTX behaves correctly. Currently, DTX does not
289 // last long during a pure silence, if the signal type is not forced.
290 // TODO(minyue): Remove the signal type forcing when Opus DTX works properly
291 // without it.
Minyue Li54d02782019-10-29 21:36:13 +0100292 int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
Minyue Li092041c2015-05-11 12:19:35 +0200293 if (ret != OPUS_OK)
294 return ret;
295
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100296 return ENCODER_CTL(inst, OPUS_SET_DTX(1));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000297}
298
299int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) {
300 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100301 int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_AUTO));
Minyue Li092041c2015-05-11 12:19:35 +0200302 if (ret != OPUS_OK)
303 return ret;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100304 return ENCODER_CTL(inst, OPUS_SET_DTX(0));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000305 } else {
306 return -1;
307 }
308}
309
soren28dc2852017-04-06 05:48:36 -0700310int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst) {
311 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100312 return ENCODER_CTL(inst, OPUS_SET_VBR(0));
soren28dc2852017-04-06 05:48:36 -0700313 } else {
314 return -1;
315 }
316}
317
318int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst) {
319 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100320 return ENCODER_CTL(inst, OPUS_SET_VBR(1));
soren28dc2852017-04-06 05:48:36 -0700321 } else {
322 return -1;
323 }
324}
325
minyue@webrtc.org04546882014-03-07 08:55:48 +0000326int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) {
327 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100328 return ENCODER_CTL(inst, OPUS_SET_COMPLEXITY(complexity));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000329 } else {
330 return -1;
331 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000332}
333
Alex Luebseeb27652017-11-20 11:13:56 -0800334int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst) {
335 if (!inst) {
336 return -1;
337 }
338 int32_t bandwidth;
Minyue Li54d02782019-10-29 21:36:13 +0100339 if (ENCODER_CTL(inst, OPUS_GET_BANDWIDTH(&bandwidth)) == 0) {
Alex Luebseeb27652017-11-20 11:13:56 -0800340 return bandwidth;
341 } else {
342 return -1;
343 }
Alex Luebseeb27652017-11-20 11:13:56 -0800344}
345
346int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) {
347 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100348 return ENCODER_CTL(inst, OPUS_SET_BANDWIDTH(bandwidth));
Alex Luebseeb27652017-11-20 11:13:56 -0800349 } else {
350 return -1;
351 }
352}
353
minyue41b9c802016-10-06 07:13:54 -0700354int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels) {
minyuec8299f92016-09-27 02:08:47 -0700355 if (!inst)
356 return -1;
357 if (num_channels == 0) {
Minyue Li54d02782019-10-29 21:36:13 +0100358 return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO));
minyuec8299f92016-09-27 02:08:47 -0700359 } else if (num_channels == 1 || num_channels == 2) {
Minyue Li54d02782019-10-29 21:36:13 +0100360 return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(num_channels));
minyuec8299f92016-09-27 02:08:47 -0700361 } else {
362 return -1;
363 }
364}
365
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200366int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst,
367 size_t channels,
368 int sample_rate_hz) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000369 int error;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000370 OpusDecInst* state;
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000371
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000372 if (inst != NULL) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100373 // Create Opus decoder state.
Minyue Li54d02782019-10-29 21:36:13 +0100374 state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000375 if (state == NULL) {
376 return -1;
377 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000378
Minyue Li54d02782019-10-29 21:36:13 +0100379 state->decoder =
380 opus_decoder_create(sample_rate_hz, static_cast<int>(channels), &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200381 if (error == OPUS_OK && state->decoder) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100382 // Creation of memory all ok.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000383 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200384 state->prev_decoded_samples = DefaultFrameSizePerChannel(sample_rate_hz);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000385 state->in_dtx_mode = 0;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200386 state->sample_rate_hz = sample_rate_hz;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000387 *inst = state;
388 return 0;
389 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000390
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100391 // If memory allocation was unsuccessful, free the entire state.
Alex Loiko50b8c392019-04-03 15:12:01 +0200392 if (state->decoder) {
393 opus_decoder_destroy(state->decoder);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000394 }
395 free(state);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000396 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000397 return -1;
398}
399
Alex Loiko50b8c392019-04-03 15:12:01 +0200400int16_t WebRtcOpus_MultistreamDecoderCreate(
Minyue Li54d02782019-10-29 21:36:13 +0100401 OpusDecInst** inst,
402 size_t channels,
Alex Loikoe5b94162019-04-08 17:19:41 +0200403 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +0200404 size_t coupled_streams,
405 const unsigned char* channel_mapping) {
406 int error;
407 OpusDecInst* state;
408
409 if (inst != NULL) {
410 // Create Opus decoder state.
Minyue Li54d02782019-10-29 21:36:13 +0100411 state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
Alex Loiko50b8c392019-04-03 15:12:01 +0200412 if (state == NULL) {
413 return -1;
414 }
415
Alex Loiko50b8c392019-04-03 15:12:01 +0200416 // Create new memory, always at 48000 Hz.
417 state->multistream_decoder = opus_multistream_decoder_create(
Minyue Li54d02782019-10-29 21:36:13 +0100418 48000, channels, streams, coupled_streams, channel_mapping, &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200419
420 if (error == OPUS_OK && state->multistream_decoder) {
421 // Creation of memory all ok.
422 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200423 state->prev_decoded_samples = DefaultFrameSizePerChannel(48000);
Alex Loiko50b8c392019-04-03 15:12:01 +0200424 state->in_dtx_mode = 0;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200425 state->sample_rate_hz = 48000;
Alex Loiko50b8c392019-04-03 15:12:01 +0200426 *inst = state;
427 return 0;
428 }
429
430 // If memory allocation was unsuccessful, free the entire state.
431 opus_multistream_decoder_destroy(state->multistream_decoder);
432 free(state);
433 }
434 return -1;
435}
436
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000437int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000438 if (inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200439 if (inst->decoder) {
440 opus_decoder_destroy(inst->decoder);
441 } else if (inst->multistream_decoder) {
442 opus_multistream_decoder_destroy(inst->multistream_decoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100443 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000444 free(inst);
445 return 0;
446 } else {
447 return -1;
448 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000449}
450
Peter Kasting69558702016-01-12 16:26:35 -0800451size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst) {
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000452 return inst->channels;
453}
454
Karl Wiberg43766482015-08-27 15:22:11 +0200455void WebRtcOpus_DecoderInit(OpusDecInst* inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200456 if (inst->decoder) {
457 opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100458 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100459 opus_multistream_decoder_ctl(inst->multistream_decoder, OPUS_RESET_STATE);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100460 }
Karl Wiberg43766482015-08-27 15:22:11 +0200461 inst->in_dtx_mode = 0;
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000462}
463
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000464/* For decoder to determine if it is to output speech or comfort noise. */
Peter Kastingdce40cf2015-08-24 14:52:23 -0700465static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000466 // Audio type becomes comfort noise if |encoded_byte| is 1 and keeps
467 // to be so if the following |encoded_byte| are 0 or 1.
468 if (encoded_bytes == 0 && inst->in_dtx_mode) {
469 return 2; // Comfort noise.
henrik.lundindeaf6fb2017-03-01 00:49:18 -0800470 } else if (encoded_bytes == 1 || encoded_bytes == 2) {
471 // TODO(henrik.lundin): There is a slight risk that a 2-byte payload is in
472 // fact a 1-byte TOC with a 1-byte payload. That will be erroneously
473 // interpreted as comfort noise output, but such a payload is probably
474 // faulty anyway.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100475
476 // TODO(webrtc:10218): This is wrong for multistream opus. Then are several
477 // single-stream packets glued together with some packet size bytes in
478 // between. See https://tools.ietf.org/html/rfc6716#appendix-B
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000479 inst->in_dtx_mode = 1;
480 return 2; // Comfort noise.
481 } else {
482 inst->in_dtx_mode = 0;
483 return 0; // Speech.
484 }
485}
486
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000487/* |frame_size| is set to maximum Opus frame size in the normal case, and
488 * is set to the number of samples needed for PLC in case of losses.
489 * It is up to the caller to make sure the value is correct. */
Minyue Li54d02782019-10-29 21:36:13 +0100490static int DecodeNative(OpusDecInst* inst,
491 const uint8_t* encoded,
492 size_t encoded_bytes,
493 int frame_size,
494 int16_t* decoded,
495 int16_t* audio_type,
496 int decode_fec) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100497 int res = -1;
Alex Loiko50b8c392019-04-03 15:12:01 +0200498 if (inst->decoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100499 res = opus_decode(
500 inst->decoder, encoded, static_cast<opus_int32>(encoded_bytes),
501 reinterpret_cast<opus_int16*>(decoded), frame_size, decode_fec);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100502 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100503 res = opus_multistream_decode(inst->multistream_decoder, encoded,
504 static_cast<opus_int32>(encoded_bytes),
505 reinterpret_cast<opus_int16*>(decoded),
506 frame_size, decode_fec);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100507 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000508
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000509 if (res <= 0)
510 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000511
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000512 *audio_type = DetermineAudioType(inst, encoded_bytes);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000513
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000514 return res;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000515}
516
Minyue Lifb075d52019-10-29 21:38:15 +0100517static int DecodePlc(OpusDecInst* inst, int16_t* decoded) {
518 int16_t audio_type = 0;
519 int decoded_samples;
520 int plc_samples;
521
522 /* The number of samples we ask for is |number_of_lost_frames| times
523 * |prev_decoded_samples_|. Limit the number of samples to maximum
524 * |MaxFrameSizePerChannel()|. */
525 plc_samples = inst->prev_decoded_samples;
526 const int max_samples_per_channel =
527 MaxFrameSizePerChannel(inst->sample_rate_hz);
528 plc_samples = plc_samples <= max_samples_per_channel
529 ? plc_samples
530 : max_samples_per_channel;
531 decoded_samples =
532 DecodeNative(inst, NULL, 0, plc_samples, decoded, &audio_type, 0);
533 if (decoded_samples < 0) {
534 return -1;
535 }
536
537 return decoded_samples;
538}
539
Minyue Li54d02782019-10-29 21:36:13 +0100540int WebRtcOpus_Decode(OpusDecInst* inst,
541 const uint8_t* encoded,
542 size_t encoded_bytes,
543 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700544 int16_t* audio_type) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000545 int decoded_samples;
546
547 if (encoded_bytes == 0) {
548 *audio_type = DetermineAudioType(inst, encoded_bytes);
Minyue Lifb075d52019-10-29 21:38:15 +0100549 decoded_samples = DecodePlc(inst, decoded);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000550 } else {
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200551 decoded_samples = DecodeNative(inst, encoded, encoded_bytes,
552 MaxFrameSizePerChannel(inst->sample_rate_hz),
553 decoded, audio_type, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000554 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000555 if (decoded_samples < 0) {
556 return -1;
557 }
558
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000559 /* Update decoded sample memory, to be used by the PLC in case of losses. */
560 inst->prev_decoded_samples = decoded_samples;
561
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000562 return decoded_samples;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000563}
564
Minyue Li54d02782019-10-29 21:36:13 +0100565int WebRtcOpus_DecodeFec(OpusDecInst* inst,
566 const uint8_t* encoded,
567 size_t encoded_bytes,
568 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700569 int16_t* audio_type) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000570 int decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000571 int fec_samples;
572
573 if (WebRtcOpus_PacketHasFec(encoded, encoded_bytes) != 1) {
574 return 0;
575 }
576
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200577 fec_samples =
578 opus_packet_get_samples_per_frame(encoded, inst->sample_rate_hz);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000579
Minyue Li54d02782019-10-29 21:36:13 +0100580 decoded_samples = DecodeNative(inst, encoded, encoded_bytes, fec_samples,
581 decoded, audio_type, 1);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000582 if (decoded_samples < 0) {
583 return -1;
584 }
585
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000586 return decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000587}
588
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000589int WebRtcOpus_DurationEst(OpusDecInst* inst,
590 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700591 size_t payload_length_bytes) {
minyuel6d92bf52015-09-23 15:20:39 +0200592 if (payload_length_bytes == 0) {
593 // WebRtcOpus_Decode calls PLC when payload length is zero. So we return
594 // PLC duration correspondingly.
595 return WebRtcOpus_PlcDuration(inst);
596 }
597
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000598 int frames, samples;
Minyue Li54d02782019-10-29 21:36:13 +0100599 frames = opus_packet_get_nb_frames(
600 payload, static_cast<opus_int32>(payload_length_bytes));
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000601 if (frames < 0) {
602 /* Invalid payload data. */
603 return 0;
604 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200605 samples =
606 frames * opus_packet_get_samples_per_frame(payload, inst->sample_rate_hz);
607 if (samples > 120 * inst->sample_rate_hz / 1000) {
608 // More than 120 ms' worth of samples.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000609 return 0;
610 }
611 return samples;
612}
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000613
minyuel6d92bf52015-09-23 15:20:39 +0200614int WebRtcOpus_PlcDuration(OpusDecInst* inst) {
615 /* The number of samples we ask for is |number_of_lost_frames| times
616 * |prev_decoded_samples_|. Limit the number of samples to maximum
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200617 * |MaxFrameSizePerChannel()|. */
minyuel6d92bf52015-09-23 15:20:39 +0200618 const int plc_samples = inst->prev_decoded_samples;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200619 const int max_samples_per_channel =
620 MaxFrameSizePerChannel(inst->sample_rate_hz);
621 return plc_samples <= max_samples_per_channel ? plc_samples
622 : max_samples_per_channel;
minyuel6d92bf52015-09-23 15:20:39 +0200623}
624
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000625int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200626 size_t payload_length_bytes,
627 int sample_rate_hz) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000628 if (WebRtcOpus_PacketHasFec(payload, payload_length_bytes) != 1) {
629 return 0;
630 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200631 const int samples =
632 opus_packet_get_samples_per_frame(payload, sample_rate_hz);
633 const int samples_per_ms = sample_rate_hz / 1000;
634 if (samples < 10 * samples_per_ms || samples > 120 * samples_per_ms) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000635 /* Invalid payload duration. */
636 return 0;
637 }
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000638 return samples;
639}
640
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200641// This method is based on Definition of the Opus Audio Codec
642// (https://tools.ietf.org/html/rfc6716). Basically, this method is based on
643// parsing the LP layer of an Opus packet, particularly the LBRR flag.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000644int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700645 size_t payload_length_bytes) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700646 if (payload == NULL || payload_length_bytes == 0)
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000647 return 0;
648
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200649 // In CELT_ONLY mode, packets should not have FEC.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000650 if (payload[0] & 0x80)
651 return 0;
652
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200653 // Max number of frames in an Opus packet is 48.
654 opus_int16 frame_sizes[48];
Minyue Li54d02782019-10-29 21:36:13 +0100655 const unsigned char* frame_data[48];
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000656
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200657 // Parse packet to get the frames. But we only care about the first frame,
658 // since we can only decode the FEC from the first one.
Minyue Li54d02782019-10-29 21:36:13 +0100659 if (opus_packet_parse(payload, static_cast<opus_int32>(payload_length_bytes),
660 NULL, frame_data, frame_sizes, NULL) < 0) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000661 return 0;
662 }
663
664 if (frame_sizes[0] <= 1) {
665 return 0;
666 }
667
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200668 // For computing the payload length in ms, the sample rate is not important
669 // since it cancels out. We use 48 kHz, but any valid sample rate would work.
670 int payload_length_ms =
671 opus_packet_get_samples_per_frame(payload, 48000) / 48;
672 if (payload_length_ms < 10)
673 payload_length_ms = 10;
674
675 int silk_frames;
676 switch (payload_length_ms) {
677 case 10:
678 case 20:
679 silk_frames = 1;
680 break;
681 case 40:
682 silk_frames = 2;
683 break;
684 case 60:
685 silk_frames = 3;
686 break;
687 default:
Minyue Li54d02782019-10-29 21:36:13 +0100688 return 0; // It is actually even an invalid packet.
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200689 }
690
691 const int channels = opus_packet_get_nb_channels(payload);
692 RTC_DCHECK(channels == 1 || channels == 2);
693
694 // A frame starts with the LP layer. The LP layer begins with two to eight
695 // header bits.These consist of one VAD bit per SILK frame (up to 3),
696 // followed by a single flag indicating the presence of LBRR frames.
697 // For a stereo packet, these first flags correspond to the mid channel, and
698 // a second set of flags is included for the side channel. Because these are
699 // the first symbols decoded by the range coder and because they are coded
700 // as binary values with uniform probability, they can be extracted directly
701 // from the most significant bits of the first byte of compressed data.
702 for (int n = 0; n < channels; n++) {
703 // The LBRR bit for channel 1 is on the (|silk_frames| + 1)-th bit, and
704 // that of channel 2 is on the |(|silk_frames| + 1) * 2 + 1|-th bit.
705 if (frame_data[0][0] & (0x80 >> ((n + 1) * (silk_frames + 1) - 1)))
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000706 return 1;
707 }
708
709 return 0;
710}