blob: f8ff656a0a60e67f7fd104676bd6fde0600cb064 [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
15#include <stdlib.h>
16#include <string.h>
17
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,
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000031};
32
Karl Wiberga1d1a1e2019-05-28 14:41:07 +020033static int FrameSizePerChannel(int frame_size_ms, int sample_rate_hz) {
34 RTC_DCHECK_GT(frame_size_ms, 0);
35 RTC_DCHECK_EQ(frame_size_ms % 10, 0);
36 RTC_DCHECK_GT(sample_rate_hz, 0);
37 RTC_DCHECK_EQ(sample_rate_hz % 1000, 0);
38 return frame_size_ms * (sample_rate_hz / 1000);
39}
40
41// Maximum sample count per channel.
42static int MaxFrameSizePerChannel(int sample_rate_hz) {
43 return FrameSizePerChannel(kWebRtcOpusMaxDecodeFrameSizeMs, sample_rate_hz);
44}
45
46// Default sample count per channel.
47static int DefaultFrameSizePerChannel(int sample_rate_hz) {
48 return FrameSizePerChannel(20, sample_rate_hz);
49}
50
minyue@webrtc.org7dba7862015-01-20 16:01:50 +000051int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
Peter Kasting69558702016-01-12 16:26:35 -080052 size_t channels,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020053 int32_t application,
54 int sample_rate_hz) {
minyue3cea2562015-11-10 03:49:26 -080055 int opus_app;
56 if (!inst)
57 return -1;
tina.legrand@webrtc.orgd0d41492012-12-20 09:23:10 +000058
minyue3cea2562015-11-10 03:49:26 -080059 switch (application) {
60 case 0:
61 opus_app = OPUS_APPLICATION_VOIP;
62 break;
63 case 1:
64 opus_app = OPUS_APPLICATION_AUDIO;
65 break;
66 default:
67 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000068 }
minyue3cea2562015-11-10 03:49:26 -080069
Alex Loiko7a3e43a2019-01-29 12:27:08 +010070 OpusEncInst* state = (OpusEncInst*)calloc(1, sizeof(OpusEncInst));
kwiberg2e486462016-08-23 05:54:25 -070071 RTC_DCHECK(state);
minyue3cea2562015-11-10 03:49:26 -080072
minyue3cea2562015-11-10 03:49:26 -080073 int error;
Karl Wiberg7e7c5c32019-05-21 11:50:32 +020074 state->encoder = opus_encoder_create(sample_rate_hz, (int)channels, opus_app,
Alex Loiko50b8c392019-04-03 15:12:01 +020075 &error);
Alex Loiko7a3e43a2019-01-29 12:27:08 +010076
Alex Loiko50b8c392019-04-03 15:12:01 +020077 if (error != OPUS_OK || (!state->encoder &&
78 !state->multistream_encoder)) {
79 WebRtcOpus_EncoderFree(state);
80 return -1;
Alex Loiko7a3e43a2019-01-29 12:27:08 +010081 }
82
Alex Loiko50b8c392019-04-03 15:12:01 +020083 state->in_dtx_mode = 0;
84 state->channels = channels;
85
86 *inst = state;
87 return 0;
88}
89
90int16_t WebRtcOpus_MultistreamEncoderCreate(
91 OpusEncInst** inst,
92 size_t channels,
93 int32_t application,
Alex Loikoe5b94162019-04-08 17:19:41 +020094 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +020095 size_t coupled_streams,
96 const unsigned char *channel_mapping) {
97 int opus_app;
98 if (!inst)
99 return -1;
100
101 switch (application) {
102 case 0:
103 opus_app = OPUS_APPLICATION_VOIP;
104 break;
105 case 1:
106 opus_app = OPUS_APPLICATION_AUDIO;
107 break;
108 default:
109 return -1;
110 }
111
112 OpusEncInst* state = (OpusEncInst*)calloc(1, sizeof(OpusEncInst));
113 RTC_DCHECK(state);
114
Alex Loiko50b8c392019-04-03 15:12:01 +0200115 int error;
116 state->multistream_encoder =
117 opus_multistream_encoder_create(
118 48000,
119 channels,
120 streams,
121 coupled_streams,
122 channel_mapping,
123 opus_app,
124 &error);
125
126 if (error != OPUS_OK || (!state->encoder &&
127 !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) {
165 res = opus_encode(inst->encoder,
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100166 (const opus_int16*)audio_in,
167 (int)samples,
168 encoded,
169 (opus_int32)length_encoded_buffer);
170 } else {
Alex Loiko50b8c392019-04-03 15:12:01 +0200171 res = opus_multistream_encode(inst->multistream_encoder,
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100172 (const opus_int16*)audio_in,
173 (int)samples,
174 encoded,
175 (opus_int32)length_encoded_buffer);
176 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000177
flim64a7eab2016-08-12 04:36:05 -0700178 if (res <= 0) {
179 return -1;
180 }
181
182 if (res <= 2) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000183 // Indicates DTX since the packet has nothing but a header. In principle,
184 // there is no need to send this packet. However, we do transmit the first
185 // occurrence to let the decoder know that the encoder enters DTX mode.
186 if (inst->in_dtx_mode) {
187 return 0;
188 } else {
189 inst->in_dtx_mode = 1;
flim92382452017-02-10 13:50:38 -0800190 return res;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000191 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000192 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000193
flim64a7eab2016-08-12 04:36:05 -0700194 inst->in_dtx_mode = 0;
195 return res;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000196}
197
Alex Loiko50b8c392019-04-03 15:12:01 +0200198#define ENCODER_CTL(inst, vargs) ( \
199 inst->encoder ? \
200 opus_encoder_ctl(inst->encoder, vargs) \
201 : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs))
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100202
203
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000204int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000205 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100206 return ENCODER_CTL(inst, OPUS_SET_BITRATE(rate));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000207 } else {
208 return -1;
209 }
210}
211
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000212int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate) {
213 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100214 return ENCODER_CTL(inst, OPUS_SET_PACKET_LOSS_PERC(loss_rate));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000215 } else {
216 return -1;
217 }
218}
219
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000220int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000221 opus_int32 set_bandwidth;
222
223 if (!inst)
224 return -1;
225
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000226 if (frequency_hz <= 8000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000227 set_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000228 } else if (frequency_hz <= 12000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000229 set_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000230 } else if (frequency_hz <= 16000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000231 set_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000232 } else if (frequency_hz <= 24000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000233 set_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
234 } else {
235 set_bandwidth = OPUS_BANDWIDTH_FULLBAND;
236 }
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100237 return ENCODER_CTL(inst, OPUS_SET_MAX_BANDWIDTH(set_bandwidth));
238}
239
240int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
241 int32_t* result_hz) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200242 if (inst->encoder) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100243 if (opus_encoder_ctl(
Alex Loiko50b8c392019-04-03 15:12:01 +0200244 inst->encoder,
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100245 OPUS_GET_MAX_BANDWIDTH(result_hz)) == OPUS_OK) {
246 return 0;
247 }
248 return -1;
249 }
250
251 opus_int32 max_bandwidth;
252 int s;
253 int ret;
254
255 max_bandwidth = 0;
256 ret = OPUS_OK;
257 s = 0;
258 while (ret == OPUS_OK) {
259 OpusEncoder *enc;
260 opus_int32 bandwidth;
261
262 ret = ENCODER_CTL(inst, OPUS_MULTISTREAM_GET_ENCODER_STATE(s, &enc));
263 if (ret == OPUS_BAD_ARG)
264 break;
265 if (ret != OPUS_OK)
266 return -1;
267 if (opus_encoder_ctl(enc, OPUS_GET_MAX_BANDWIDTH(&bandwidth)) != OPUS_OK)
268 return -1;
269
270 if (max_bandwidth != 0 && max_bandwidth != bandwidth)
271 return -1;
272
273 max_bandwidth = bandwidth;
274 s++;
275 }
276 *result_hz = max_bandwidth;
277 return 0;
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000278}
279
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000280int16_t WebRtcOpus_EnableFec(OpusEncInst* inst) {
281 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100282 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(1));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000283 } else {
284 return -1;
285 }
286}
287
288int16_t WebRtcOpus_DisableFec(OpusEncInst* inst) {
289 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100290 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(0));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000291 } else {
292 return -1;
293 }
294}
295
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000296int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) {
Minyue Li092041c2015-05-11 12:19:35 +0200297 if (!inst) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000298 return -1;
299 }
Minyue Li092041c2015-05-11 12:19:35 +0200300
301 // To prevent Opus from entering CELT-only mode by forcing signal type to
302 // voice to make sure that DTX behaves correctly. Currently, DTX does not
303 // last long during a pure silence, if the signal type is not forced.
304 // TODO(minyue): Remove the signal type forcing when Opus DTX works properly
305 // without it.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100306 int ret = ENCODER_CTL(inst,
307 OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
Minyue Li092041c2015-05-11 12:19:35 +0200308 if (ret != OPUS_OK)
309 return ret;
310
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100311 return ENCODER_CTL(inst, OPUS_SET_DTX(1));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000312}
313
314int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) {
315 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100316 int ret = ENCODER_CTL(inst,
317 OPUS_SET_SIGNAL(OPUS_AUTO));
Minyue Li092041c2015-05-11 12:19:35 +0200318 if (ret != OPUS_OK)
319 return ret;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100320 return ENCODER_CTL(inst, OPUS_SET_DTX(0));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000321 } else {
322 return -1;
323 }
324}
325
soren28dc2852017-04-06 05:48:36 -0700326int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst) {
327 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100328 return ENCODER_CTL(inst, OPUS_SET_VBR(0));
soren28dc2852017-04-06 05:48:36 -0700329 } else {
330 return -1;
331 }
332}
333
334int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst) {
335 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100336 return ENCODER_CTL(inst, OPUS_SET_VBR(1));
soren28dc2852017-04-06 05:48:36 -0700337 } else {
338 return -1;
339 }
340}
341
minyue@webrtc.org04546882014-03-07 08:55:48 +0000342int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) {
343 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100344 return ENCODER_CTL(inst,
345 OPUS_SET_COMPLEXITY(complexity));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000346 } else {
347 return -1;
348 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000349}
350
Alex Luebseeb27652017-11-20 11:13:56 -0800351int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst) {
352 if (!inst) {
353 return -1;
354 }
355 int32_t bandwidth;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100356 if (ENCODER_CTL(inst,
357 OPUS_GET_BANDWIDTH(&bandwidth)) == 0) {
Alex Luebseeb27652017-11-20 11:13:56 -0800358 return bandwidth;
359 } else {
360 return -1;
361 }
362
363}
364
365int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) {
366 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100367 return ENCODER_CTL(inst,
368 OPUS_SET_BANDWIDTH(bandwidth));
Alex Luebseeb27652017-11-20 11:13:56 -0800369 } else {
370 return -1;
371 }
372}
373
minyue41b9c802016-10-06 07:13:54 -0700374int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels) {
minyuec8299f92016-09-27 02:08:47 -0700375 if (!inst)
376 return -1;
377 if (num_channels == 0) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100378 return ENCODER_CTL(inst,
minyuec8299f92016-09-27 02:08:47 -0700379 OPUS_SET_FORCE_CHANNELS(OPUS_AUTO));
380 } else if (num_channels == 1 || num_channels == 2) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100381 return ENCODER_CTL(inst,
382 OPUS_SET_FORCE_CHANNELS(num_channels));
minyuec8299f92016-09-27 02:08:47 -0700383 } else {
384 return -1;
385 }
386}
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.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000396 state = (OpusDecInst*) calloc(1, sizeof(OpusDecInst));
397 if (state == NULL) {
398 return -1;
399 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000400
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200401 state->decoder = opus_decoder_create(sample_rate_hz, (int)channels, &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200402 if (error == OPUS_OK && state->decoder) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100403 // Creation of memory all ok.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000404 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200405 state->prev_decoded_samples = DefaultFrameSizePerChannel(sample_rate_hz);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000406 state->in_dtx_mode = 0;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200407 state->sample_rate_hz = sample_rate_hz;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000408 *inst = state;
409 return 0;
410 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000411
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100412 // If memory allocation was unsuccessful, free the entire state.
Alex Loiko50b8c392019-04-03 15:12:01 +0200413 if (state->decoder) {
414 opus_decoder_destroy(state->decoder);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000415 }
416 free(state);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000417 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000418 return -1;
419}
420
Alex Loiko50b8c392019-04-03 15:12:01 +0200421int16_t WebRtcOpus_MultistreamDecoderCreate(
422 OpusDecInst** inst, size_t channels,
Alex Loikoe5b94162019-04-08 17:19:41 +0200423 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +0200424 size_t coupled_streams,
425 const unsigned char* channel_mapping) {
426 int error;
427 OpusDecInst* state;
428
429 if (inst != NULL) {
430 // Create Opus decoder state.
431 state = (OpusDecInst*) calloc(1, sizeof(OpusDecInst));
432 if (state == NULL) {
433 return -1;
434 }
435
Alex Loiko50b8c392019-04-03 15:12:01 +0200436 // Create new memory, always at 48000 Hz.
437 state->multistream_decoder = opus_multistream_decoder_create(
Alex Loikoe5b94162019-04-08 17:19:41 +0200438 48000, channels,
Alex Loiko50b8c392019-04-03 15:12:01 +0200439 streams,
440 coupled_streams,
441 channel_mapping,
442 &error);
443
444 if (error == OPUS_OK && state->multistream_decoder) {
445 // Creation of memory all ok.
446 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200447 state->prev_decoded_samples = DefaultFrameSizePerChannel(48000);
Alex Loiko50b8c392019-04-03 15:12:01 +0200448 state->in_dtx_mode = 0;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200449 state->sample_rate_hz = 48000;
Alex Loiko50b8c392019-04-03 15:12:01 +0200450 *inst = state;
451 return 0;
452 }
453
454 // If memory allocation was unsuccessful, free the entire state.
455 opus_multistream_decoder_destroy(state->multistream_decoder);
456 free(state);
457 }
458 return -1;
459}
460
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000461int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000462 if (inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200463 if (inst->decoder) {
464 opus_decoder_destroy(inst->decoder);
465 } else if (inst->multistream_decoder) {
466 opus_multistream_decoder_destroy(inst->multistream_decoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100467 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000468 free(inst);
469 return 0;
470 } else {
471 return -1;
472 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000473}
474
Peter Kasting69558702016-01-12 16:26:35 -0800475size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst) {
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000476 return inst->channels;
477}
478
Karl Wiberg43766482015-08-27 15:22:11 +0200479void WebRtcOpus_DecoderInit(OpusDecInst* inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200480 if (inst->decoder) {
481 opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100482 } else {
Alex Loiko50b8c392019-04-03 15:12:01 +0200483 opus_multistream_decoder_ctl(inst->multistream_decoder,
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100484 OPUS_RESET_STATE);
485 }
Karl Wiberg43766482015-08-27 15:22:11 +0200486 inst->in_dtx_mode = 0;
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000487}
488
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000489/* For decoder to determine if it is to output speech or comfort noise. */
Peter Kastingdce40cf2015-08-24 14:52:23 -0700490static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000491 // Audio type becomes comfort noise if |encoded_byte| is 1 and keeps
492 // to be so if the following |encoded_byte| are 0 or 1.
493 if (encoded_bytes == 0 && inst->in_dtx_mode) {
494 return 2; // Comfort noise.
henrik.lundindeaf6fb2017-03-01 00:49:18 -0800495 } else if (encoded_bytes == 1 || encoded_bytes == 2) {
496 // TODO(henrik.lundin): There is a slight risk that a 2-byte payload is in
497 // fact a 1-byte TOC with a 1-byte payload. That will be erroneously
498 // interpreted as comfort noise output, but such a payload is probably
499 // faulty anyway.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100500
501 // TODO(webrtc:10218): This is wrong for multistream opus. Then are several
502 // single-stream packets glued together with some packet size bytes in
503 // between. See https://tools.ietf.org/html/rfc6716#appendix-B
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000504 inst->in_dtx_mode = 1;
505 return 2; // Comfort noise.
506 } else {
507 inst->in_dtx_mode = 0;
508 return 0; // Speech.
509 }
510}
511
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000512/* |frame_size| is set to maximum Opus frame size in the normal case, and
513 * is set to the number of samples needed for PLC in case of losses.
514 * It is up to the caller to make sure the value is correct. */
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000515static int DecodeNative(OpusDecInst* inst, const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700516 size_t encoded_bytes, int frame_size,
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000517 int16_t* decoded, int16_t* audio_type, int decode_fec) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100518 int res = -1;
Alex Loiko50b8c392019-04-03 15:12:01 +0200519 if (inst->decoder) {
520 res = opus_decode(inst->decoder, encoded, (opus_int32)encoded_bytes,
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100521 (opus_int16*)decoded, frame_size, decode_fec);
522 } else {
523 res = opus_multistream_decode(
Alex Loiko50b8c392019-04-03 15:12:01 +0200524 inst->multistream_decoder, encoded, (opus_int32)encoded_bytes,
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100525 (opus_int16*)decoded, frame_size, decode_fec);
526 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000527
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000528 if (res <= 0)
529 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000530
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000531 *audio_type = DetermineAudioType(inst, encoded_bytes);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000532
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000533 return res;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000534}
535
Peter Kastingbba78072015-06-11 19:02:46 -0700536int WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700537 size_t encoded_bytes, int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700538 int16_t* audio_type) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000539 int decoded_samples;
540
541 if (encoded_bytes == 0) {
542 *audio_type = DetermineAudioType(inst, encoded_bytes);
543 decoded_samples = WebRtcOpus_DecodePlc(inst, decoded, 1);
544 } else {
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200545 decoded_samples = DecodeNative(inst, encoded, encoded_bytes,
546 MaxFrameSizePerChannel(inst->sample_rate_hz),
547 decoded, audio_type, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000548 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000549 if (decoded_samples < 0) {
550 return -1;
551 }
552
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000553 /* Update decoded sample memory, to be used by the PLC in case of losses. */
554 inst->prev_decoded_samples = decoded_samples;
555
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000556 return decoded_samples;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000557}
558
Peter Kastingbba78072015-06-11 19:02:46 -0700559int WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded,
560 int number_of_lost_frames) {
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000561 int16_t audio_type = 0;
562 int decoded_samples;
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000563 int plc_samples;
564
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000565 /* The number of samples we ask for is |number_of_lost_frames| times
566 * |prev_decoded_samples_|. Limit the number of samples to maximum
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200567 * |MaxFrameSizePerChannel()|. */
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000568 plc_samples = number_of_lost_frames * inst->prev_decoded_samples;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200569 const int max_samples_per_channel =
570 MaxFrameSizePerChannel(inst->sample_rate_hz);
571 plc_samples = plc_samples <= max_samples_per_channel
572 ? plc_samples
573 : max_samples_per_channel;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000574 decoded_samples = DecodeNative(inst, NULL, 0, plc_samples,
575 decoded, &audio_type, 0);
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000576 if (decoded_samples < 0) {
577 return -1;
578 }
579
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000580 return decoded_samples;
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000581}
582
Peter Kastingbba78072015-06-11 19:02:46 -0700583int WebRtcOpus_DecodeFec(OpusDecInst* inst, const uint8_t* encoded,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700584 size_t encoded_bytes, int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700585 int16_t* audio_type) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000586 int decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000587 int fec_samples;
588
589 if (WebRtcOpus_PacketHasFec(encoded, encoded_bytes) != 1) {
590 return 0;
591 }
592
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200593 fec_samples =
594 opus_packet_get_samples_per_frame(encoded, inst->sample_rate_hz);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000595
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000596 decoded_samples = DecodeNative(inst, encoded, encoded_bytes,
597 fec_samples, decoded, audio_type, 1);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000598 if (decoded_samples < 0) {
599 return -1;
600 }
601
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000602 return decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000603}
604
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000605int WebRtcOpus_DurationEst(OpusDecInst* inst,
606 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700607 size_t payload_length_bytes) {
minyuel6d92bf52015-09-23 15:20:39 +0200608 if (payload_length_bytes == 0) {
609 // WebRtcOpus_Decode calls PLC when payload length is zero. So we return
610 // PLC duration correspondingly.
611 return WebRtcOpus_PlcDuration(inst);
612 }
613
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000614 int frames, samples;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700615 frames = opus_packet_get_nb_frames(payload, (opus_int32)payload_length_bytes);
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000616 if (frames < 0) {
617 /* Invalid payload data. */
618 return 0;
619 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200620 samples =
621 frames * opus_packet_get_samples_per_frame(payload, inst->sample_rate_hz);
622 if (samples > 120 * inst->sample_rate_hz / 1000) {
623 // More than 120 ms' worth of samples.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000624 return 0;
625 }
626 return samples;
627}
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000628
minyuel6d92bf52015-09-23 15:20:39 +0200629int WebRtcOpus_PlcDuration(OpusDecInst* inst) {
630 /* The number of samples we ask for is |number_of_lost_frames| times
631 * |prev_decoded_samples_|. Limit the number of samples to maximum
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200632 * |MaxFrameSizePerChannel()|. */
minyuel6d92bf52015-09-23 15:20:39 +0200633 const int plc_samples = inst->prev_decoded_samples;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200634 const int max_samples_per_channel =
635 MaxFrameSizePerChannel(inst->sample_rate_hz);
636 return plc_samples <= max_samples_per_channel ? plc_samples
637 : max_samples_per_channel;
minyuel6d92bf52015-09-23 15:20:39 +0200638}
639
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000640int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200641 size_t payload_length_bytes,
642 int sample_rate_hz) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000643 if (WebRtcOpus_PacketHasFec(payload, payload_length_bytes) != 1) {
644 return 0;
645 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200646 const int samples =
647 opus_packet_get_samples_per_frame(payload, sample_rate_hz);
648 const int samples_per_ms = sample_rate_hz / 1000;
649 if (samples < 10 * samples_per_ms || samples > 120 * samples_per_ms) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000650 /* Invalid payload duration. */
651 return 0;
652 }
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000653 return samples;
654}
655
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200656// This method is based on Definition of the Opus Audio Codec
657// (https://tools.ietf.org/html/rfc6716). Basically, this method is based on
658// parsing the LP layer of an Opus packet, particularly the LBRR flag.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000659int WebRtcOpus_PacketHasFec(const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700660 size_t payload_length_bytes) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700661 if (payload == NULL || payload_length_bytes == 0)
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000662 return 0;
663
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200664 // In CELT_ONLY mode, packets should not have FEC.
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000665 if (payload[0] & 0x80)
666 return 0;
667
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200668 // Max number of frames in an Opus packet is 48.
669 opus_int16 frame_sizes[48];
670 const unsigned char *frame_data[48];
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000671
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200672 // Parse packet to get the frames. But we only care about the first frame,
673 // since we can only decode the FEC from the first one.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700674 if (opus_packet_parse(payload, (opus_int32)payload_length_bytes, NULL,
675 frame_data, frame_sizes, NULL) < 0) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000676 return 0;
677 }
678
679 if (frame_sizes[0] <= 1) {
680 return 0;
681 }
682
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200683 // For computing the payload length in ms, the sample rate is not important
684 // since it cancels out. We use 48 kHz, but any valid sample rate would work.
685 int payload_length_ms =
686 opus_packet_get_samples_per_frame(payload, 48000) / 48;
687 if (payload_length_ms < 10)
688 payload_length_ms = 10;
689
690 int silk_frames;
691 switch (payload_length_ms) {
692 case 10:
693 case 20:
694 silk_frames = 1;
695 break;
696 case 40:
697 silk_frames = 2;
698 break;
699 case 60:
700 silk_frames = 3;
701 break;
702 default:
703 return 0; // It is actually even an invalid packet.
704 }
705
706 const int channels = opus_packet_get_nb_channels(payload);
707 RTC_DCHECK(channels == 1 || channels == 2);
708
709 // A frame starts with the LP layer. The LP layer begins with two to eight
710 // header bits.These consist of one VAD bit per SILK frame (up to 3),
711 // followed by a single flag indicating the presence of LBRR frames.
712 // For a stereo packet, these first flags correspond to the mid channel, and
713 // a second set of flags is included for the side channel. Because these are
714 // the first symbols decoded by the range coder and because they are coded
715 // as binary values with uniform probability, they can be extracted directly
716 // from the most significant bits of the first byte of compressed data.
717 for (int n = 0; n < channels; n++) {
718 // The LBRR bit for channel 1 is on the (|silk_frames| + 1)-th bit, and
719 // that of channel 2 is on the |(|silk_frames| + 1) * 2 + 1|-th bit.
720 if (frame_data[0][0] & (0x80 >> ((n + 1) * (silk_frames + 1) - 1)))
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000721 return 1;
722 }
723
724 return 0;
725}