blob: f684452ad59f35add0b7f6b5626b2a68c965cb83 [file] [log] [blame]
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_coding/codecs/opus/opus_interface.h"
kwiberg2e486462016-08-23 05:54:25 -070012
Niels Möller0aa7e372020-01-07 14:23:54 +010013#include <cstdlib>
14
Jesús de Vicente Peña3b9abd82021-02-05 09:05:46 +010015#include <numeric>
16
17#include "api/array_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Minyue Li8e83c7a2019-11-04 14:47:52 +010019#include "system_wrappers/include/field_trial.h"
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000020
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000021enum {
minyue2e03c662017-02-01 17:31:11 -080022#if WEBRTC_OPUS_SUPPORT_120MS_PTIME
23 /* Maximum supported frame size in WebRTC is 120 ms. */
24 kWebRtcOpusMaxEncodeFrameSizeMs = 120,
25#else
tina.legrand@webrtc.org46d90dc2013-02-01 14:20:06 +000026 /* Maximum supported frame size in WebRTC is 60 ms. */
27 kWebRtcOpusMaxEncodeFrameSizeMs = 60,
minyue2e03c662017-02-01 17:31:11 -080028#endif
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000029
tina.legrand@webrtc.org45426ea2013-07-03 13:32:04 +000030 /* The format allows up to 120 ms frames. Since we don't control the other
31 * side, we must allow for packets of that size. NetEq is currently limited
32 * to 60 ms on the receive side. */
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000033 kWebRtcOpusMaxDecodeFrameSizeMs = 120,
Minyue Li8e83c7a2019-11-04 14:47:52 +010034
35 // Duration of audio that each call to packet loss concealment covers.
36 kWebRtcOpusPlcFrameSizeMs = 10,
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +000037};
38
Minyue Li8e83c7a2019-11-04 14:47:52 +010039constexpr char kPlcUsePrevDecodedSamplesFieldTrial[] =
40 "WebRTC-Audio-OpusPlcUsePrevDecodedSamples";
41
Jesús de Vicente Peña3b9abd82021-02-05 09:05:46 +010042constexpr char kAvoidNoisePumpingDuringDtxFieldTrial[] =
43 "WebRTC-Audio-OpusAvoidNoisePumpingDuringDtx";
44
Karl Wiberga1d1a1e2019-05-28 14:41:07 +020045static int FrameSizePerChannel(int frame_size_ms, int sample_rate_hz) {
46 RTC_DCHECK_GT(frame_size_ms, 0);
47 RTC_DCHECK_EQ(frame_size_ms % 10, 0);
48 RTC_DCHECK_GT(sample_rate_hz, 0);
49 RTC_DCHECK_EQ(sample_rate_hz % 1000, 0);
50 return frame_size_ms * (sample_rate_hz / 1000);
51}
52
53// Maximum sample count per channel.
54static int MaxFrameSizePerChannel(int sample_rate_hz) {
55 return FrameSizePerChannel(kWebRtcOpusMaxDecodeFrameSizeMs, sample_rate_hz);
56}
57
58// Default sample count per channel.
59static int DefaultFrameSizePerChannel(int sample_rate_hz) {
60 return FrameSizePerChannel(20, sample_rate_hz);
61}
62
Jesús de Vicente Peña3b9abd82021-02-05 09:05:46 +010063// Returns true if the `encoded` payload corresponds to a refresh DTX packet
64// whose energy is larger than the expected for non activity packets.
65static bool WebRtcOpus_IsHighEnergyRefreshDtxPacket(
66 OpusEncInst* inst,
67 rtc::ArrayView<const int16_t> frame,
68 rtc::ArrayView<const uint8_t> encoded) {
69 if (encoded.size() <= 2) {
70 return false;
71 }
72 int number_frames =
73 frame.size() / DefaultFrameSizePerChannel(inst->sample_rate_hz);
74 if (number_frames > 0 &&
75 WebRtcOpus_PacketHasVoiceActivity(encoded.data(), encoded.size()) == 0) {
76 const float average_frame_energy =
77 std::accumulate(frame.begin(), frame.end(), 0.0f,
78 [](float a, int32_t b) { return a + b * b; }) /
79 number_frames;
80 if (WebRtcOpus_GetInDtx(inst) == 1 &&
81 average_frame_energy >= inst->smooth_energy_non_active_frames * 0.5f) {
82 // This is a refresh DTX packet as the encoder is in DTX and has
83 // produced a payload > 2 bytes. This refresh packet has a higher energy
84 // than the smooth energy of non activity frames (with a 3 dB negative
85 // margin) and, therefore, it is flagged as a high energy refresh DTX
86 // packet.
87 return true;
88 }
89 // The average energy is tracked in a similar way as the modeling of the
90 // comfort noise in the Silk decoder in Opus
91 // (third_party/opus/src/silk/CNG.c).
92 if (average_frame_energy < inst->smooth_energy_non_active_frames * 0.5f) {
93 inst->smooth_energy_non_active_frames = average_frame_energy;
94 } else {
95 inst->smooth_energy_non_active_frames +=
96 (average_frame_energy - inst->smooth_energy_non_active_frames) *
97 0.25f;
98 }
99 }
100 return false;
101}
102
minyue@webrtc.org7dba7862015-01-20 16:01:50 +0000103int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
Peter Kasting69558702016-01-12 16:26:35 -0800104 size_t channels,
Karl Wiberg7e7c5c32019-05-21 11:50:32 +0200105 int32_t application,
106 int sample_rate_hz) {
minyue3cea2562015-11-10 03:49:26 -0800107 int opus_app;
108 if (!inst)
109 return -1;
tina.legrand@webrtc.orgd0d41492012-12-20 09:23:10 +0000110
minyue3cea2562015-11-10 03:49:26 -0800111 switch (application) {
112 case 0:
113 opus_app = OPUS_APPLICATION_VOIP;
114 break;
115 case 1:
116 opus_app = OPUS_APPLICATION_AUDIO;
117 break;
118 default:
119 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000120 }
minyue3cea2562015-11-10 03:49:26 -0800121
Minyue Li54d02782019-10-29 21:36:13 +0100122 OpusEncInst* state =
123 reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
kwiberg2e486462016-08-23 05:54:25 -0700124 RTC_DCHECK(state);
minyue3cea2562015-11-10 03:49:26 -0800125
minyue3cea2562015-11-10 03:49:26 -0800126 int error;
Minyue Li54d02782019-10-29 21:36:13 +0100127 state->encoder = opus_encoder_create(
128 sample_rate_hz, static_cast<int>(channels), opus_app, &error);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100129
Minyue Li54d02782019-10-29 21:36:13 +0100130 if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200131 WebRtcOpus_EncoderFree(state);
132 return -1;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100133 }
134
Alex Loiko50b8c392019-04-03 15:12:01 +0200135 state->in_dtx_mode = 0;
136 state->channels = channels;
Jesús de Vicente Peña3b9abd82021-02-05 09:05:46 +0100137 state->sample_rate_hz = sample_rate_hz;
138 state->smooth_energy_non_active_frames = 0.0f;
139 state->avoid_noise_pumping_during_dtx =
140 webrtc::field_trial::IsEnabled(kAvoidNoisePumpingDuringDtxFieldTrial);
Alex Loiko50b8c392019-04-03 15:12:01 +0200141
142 *inst = state;
143 return 0;
144}
145
146int16_t WebRtcOpus_MultistreamEncoderCreate(
147 OpusEncInst** inst,
148 size_t channels,
149 int32_t application,
Alex Loikoe5b94162019-04-08 17:19:41 +0200150 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +0200151 size_t coupled_streams,
Minyue Li54d02782019-10-29 21:36:13 +0100152 const unsigned char* channel_mapping) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200153 int opus_app;
154 if (!inst)
155 return -1;
156
157 switch (application) {
158 case 0:
159 opus_app = OPUS_APPLICATION_VOIP;
160 break;
161 case 1:
162 opus_app = OPUS_APPLICATION_AUDIO;
163 break;
164 default:
165 return -1;
166 }
167
Minyue Li54d02782019-10-29 21:36:13 +0100168 OpusEncInst* state =
169 reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
Alex Loiko50b8c392019-04-03 15:12:01 +0200170 RTC_DCHECK(state);
171
Alex Loiko50b8c392019-04-03 15:12:01 +0200172 int error;
Jesús de Vicente Peña3b9abd82021-02-05 09:05:46 +0100173 const int sample_rate_hz = 48000;
174 state->multistream_encoder = opus_multistream_encoder_create(
175 sample_rate_hz, channels, streams, coupled_streams, channel_mapping,
176 opus_app, &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200177
Minyue Li54d02782019-10-29 21:36:13 +0100178 if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
minyue3cea2562015-11-10 03:49:26 -0800179 WebRtcOpus_EncoderFree(state);
180 return -1;
181 }
182
183 state->in_dtx_mode = 0;
184 state->channels = channels;
Jesús de Vicente Peña3b9abd82021-02-05 09:05:46 +0100185 state->sample_rate_hz = sample_rate_hz;
186 state->smooth_energy_non_active_frames = 0.0f;
187 state->avoid_noise_pumping_during_dtx = false;
minyue3cea2562015-11-10 03:49:26 -0800188
189 *inst = state;
190 return 0;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000191}
192
193int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000194 if (inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200195 if (inst->encoder) {
196 opus_encoder_destroy(inst->encoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100197 } else {
Alex Loiko50b8c392019-04-03 15:12:01 +0200198 opus_multistream_encoder_destroy(inst->multistream_encoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100199 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000200 free(inst);
201 return 0;
202 } else {
203 return -1;
204 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000205}
206
Peter Kastingbba78072015-06-11 19:02:46 -0700207int WebRtcOpus_Encode(OpusEncInst* inst,
208 const int16_t* audio_in,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700209 size_t samples,
210 size_t length_encoded_buffer,
Peter Kastingbba78072015-06-11 19:02:46 -0700211 uint8_t* encoded) {
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000212 int res;
213
214 if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) {
215 return -1;
216 }
217
Alex Loiko50b8c392019-04-03 15:12:01 +0200218 if (inst->encoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100219 res = opus_encode(inst->encoder, (const opus_int16*)audio_in,
220 static_cast<int>(samples), encoded,
221 static_cast<opus_int32>(length_encoded_buffer));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100222 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100223 res = opus_multistream_encode(
224 inst->multistream_encoder, (const opus_int16*)audio_in,
225 static_cast<int>(samples), encoded,
226 static_cast<opus_int32>(length_encoded_buffer));
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100227 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000228
flim64a7eab2016-08-12 04:36:05 -0700229 if (res <= 0) {
230 return -1;
231 }
232
233 if (res <= 2) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000234 // Indicates DTX since the packet has nothing but a header. In principle,
235 // there is no need to send this packet. However, we do transmit the first
236 // occurrence to let the decoder know that the encoder enters DTX mode.
237 if (inst->in_dtx_mode) {
238 return 0;
239 } else {
240 inst->in_dtx_mode = 1;
flim92382452017-02-10 13:50:38 -0800241 return res;
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000242 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000243 }
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000244
Jesús de Vicente Peña3b9abd82021-02-05 09:05:46 +0100245 if (inst->avoid_noise_pumping_during_dtx && WebRtcOpus_GetUseDtx(inst) == 1 &&
246 WebRtcOpus_IsHighEnergyRefreshDtxPacket(
247 inst, rtc::MakeArrayView(audio_in, samples),
248 rtc::MakeArrayView(encoded, res))) {
249 // This packet is a high energy refresh DTX packet. For avoiding an increase
Jesús de Vicente Peñad674ec72021-05-11 13:54:42 +0200250 // of the energy in the DTX region at the decoder, this packet is
251 // substituted by a TOC byte with one empty frame.
252 // The number of frames described in the TOC byte
253 // (https://tools.ietf.org/html/rfc6716#section-3.1) are overwritten to
254 // always indicate one frame (last two bits equal to 0).
255 encoded[0] = encoded[0] & 0b11111100;
256 inst->in_dtx_mode = 1;
257 // The payload is just the TOC byte and has 1 byte as length.
258 return 1;
Jesús de Vicente Peña3b9abd82021-02-05 09:05:46 +0100259 }
flim64a7eab2016-08-12 04:36:05 -0700260 inst->in_dtx_mode = 0;
261 return res;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000262}
263
Minyue Li54d02782019-10-29 21:36:13 +0100264#define ENCODER_CTL(inst, vargs) \
265 (inst->encoder \
266 ? opus_encoder_ctl(inst->encoder, vargs) \
267 : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs))
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100268
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000269int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000270 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100271 return ENCODER_CTL(inst, OPUS_SET_BITRATE(rate));
minyue@webrtc.org04546882014-03-07 08:55:48 +0000272 } else {
273 return -1;
274 }
275}
276
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000277int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate) {
278 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100279 return ENCODER_CTL(inst, OPUS_SET_PACKET_LOSS_PERC(loss_rate));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000280 } else {
281 return -1;
282 }
283}
284
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000285int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000286 opus_int32 set_bandwidth;
287
288 if (!inst)
289 return -1;
290
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000291 if (frequency_hz <= 8000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000292 set_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000293 } else if (frequency_hz <= 12000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000294 set_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000295 } else if (frequency_hz <= 16000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000296 set_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +0000297 } else if (frequency_hz <= 24000) {
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000298 set_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
299 } else {
300 set_bandwidth = OPUS_BANDWIDTH_FULLBAND;
301 }
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100302 return ENCODER_CTL(inst, OPUS_SET_MAX_BANDWIDTH(set_bandwidth));
303}
304
305int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
306 int32_t* result_hz) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200307 if (inst->encoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100308 if (opus_encoder_ctl(inst->encoder, OPUS_GET_MAX_BANDWIDTH(result_hz)) ==
309 OPUS_OK) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100310 return 0;
311 }
312 return -1;
313 }
314
315 opus_int32 max_bandwidth;
316 int s;
317 int ret;
318
319 max_bandwidth = 0;
320 ret = OPUS_OK;
321 s = 0;
322 while (ret == OPUS_OK) {
Minyue Li54d02782019-10-29 21:36:13 +0100323 OpusEncoder* enc;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100324 opus_int32 bandwidth;
325
326 ret = ENCODER_CTL(inst, OPUS_MULTISTREAM_GET_ENCODER_STATE(s, &enc));
327 if (ret == OPUS_BAD_ARG)
328 break;
329 if (ret != OPUS_OK)
330 return -1;
331 if (opus_encoder_ctl(enc, OPUS_GET_MAX_BANDWIDTH(&bandwidth)) != OPUS_OK)
332 return -1;
333
334 if (max_bandwidth != 0 && max_bandwidth != bandwidth)
335 return -1;
336
337 max_bandwidth = bandwidth;
338 s++;
339 }
340 *result_hz = max_bandwidth;
341 return 0;
minyue@webrtc.org0040a6e2014-08-04 14:41:57 +0000342}
343
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000344int16_t WebRtcOpus_EnableFec(OpusEncInst* inst) {
345 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100346 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(1));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000347 } else {
348 return -1;
349 }
350}
351
352int16_t WebRtcOpus_DisableFec(OpusEncInst* inst) {
353 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100354 return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(0));
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000355 } else {
356 return -1;
357 }
358}
359
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000360int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) {
Minyue Li092041c2015-05-11 12:19:35 +0200361 if (!inst) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000362 return -1;
363 }
Minyue Li092041c2015-05-11 12:19:35 +0200364
365 // To prevent Opus from entering CELT-only mode by forcing signal type to
366 // voice to make sure that DTX behaves correctly. Currently, DTX does not
367 // last long during a pure silence, if the signal type is not forced.
368 // TODO(minyue): Remove the signal type forcing when Opus DTX works properly
369 // without it.
Minyue Li54d02782019-10-29 21:36:13 +0100370 int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
Minyue Li092041c2015-05-11 12:19:35 +0200371 if (ret != OPUS_OK)
372 return ret;
373
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100374 return ENCODER_CTL(inst, OPUS_SET_DTX(1));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000375}
376
377int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) {
378 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100379 int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_AUTO));
Minyue Li092041c2015-05-11 12:19:35 +0200380 if (ret != OPUS_OK)
381 return ret;
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100382 return ENCODER_CTL(inst, OPUS_SET_DTX(0));
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000383 } else {
384 return -1;
385 }
386}
387
Jesús de Vicente Peña3b9abd82021-02-05 09:05:46 +0100388int16_t WebRtcOpus_GetUseDtx(OpusEncInst* inst) {
389 if (inst) {
390 opus_int32 use_dtx;
391 if (ENCODER_CTL(inst, OPUS_GET_DTX(&use_dtx)) == 0) {
392 return use_dtx;
393 }
394 }
395 return -1;
396}
397
soren28dc2852017-04-06 05:48:36 -0700398int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst) {
399 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100400 return ENCODER_CTL(inst, OPUS_SET_VBR(0));
soren28dc2852017-04-06 05:48:36 -0700401 } else {
402 return -1;
403 }
404}
405
406int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst) {
407 if (inst) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100408 return ENCODER_CTL(inst, OPUS_SET_VBR(1));
soren28dc2852017-04-06 05:48:36 -0700409 } else {
410 return -1;
411 }
412}
413
minyue@webrtc.org04546882014-03-07 08:55:48 +0000414int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) {
415 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100416 return ENCODER_CTL(inst, OPUS_SET_COMPLEXITY(complexity));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000417 } else {
418 return -1;
419 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000420}
421
Alex Luebseeb27652017-11-20 11:13:56 -0800422int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst) {
423 if (!inst) {
424 return -1;
425 }
426 int32_t bandwidth;
Minyue Li54d02782019-10-29 21:36:13 +0100427 if (ENCODER_CTL(inst, OPUS_GET_BANDWIDTH(&bandwidth)) == 0) {
Alex Luebseeb27652017-11-20 11:13:56 -0800428 return bandwidth;
429 } else {
430 return -1;
431 }
Alex Luebseeb27652017-11-20 11:13:56 -0800432}
433
434int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) {
435 if (inst) {
Minyue Li54d02782019-10-29 21:36:13 +0100436 return ENCODER_CTL(inst, OPUS_SET_BANDWIDTH(bandwidth));
Alex Luebseeb27652017-11-20 11:13:56 -0800437 } else {
438 return -1;
439 }
440}
441
minyue41b9c802016-10-06 07:13:54 -0700442int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels) {
minyuec8299f92016-09-27 02:08:47 -0700443 if (!inst)
444 return -1;
445 if (num_channels == 0) {
Minyue Li54d02782019-10-29 21:36:13 +0100446 return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO));
minyuec8299f92016-09-27 02:08:47 -0700447 } else if (num_channels == 1 || num_channels == 2) {
Minyue Li54d02782019-10-29 21:36:13 +0100448 return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(num_channels));
minyuec8299f92016-09-27 02:08:47 -0700449 } else {
450 return -1;
451 }
452}
453
Minyue Li332274d2019-11-13 16:05:46 +0100454int32_t WebRtcOpus_GetInDtx(OpusEncInst* inst) {
455 if (!inst) {
456 return -1;
457 }
Tom Anderson422f9dd2020-02-24 12:01:11 -0800458#ifdef OPUS_GET_IN_DTX
Minyue Li332274d2019-11-13 16:05:46 +0100459 int32_t in_dtx;
460 if (ENCODER_CTL(inst, OPUS_GET_IN_DTX(&in_dtx)) == 0) {
461 return in_dtx;
Minyue Li332274d2019-11-13 16:05:46 +0100462 }
Tom Anderson422f9dd2020-02-24 12:01:11 -0800463#endif
464 return -1;
Minyue Li332274d2019-11-13 16:05:46 +0100465}
466
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200467int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst,
468 size_t channels,
469 int sample_rate_hz) {
minyue@webrtc.org33ccdfa2014-12-04 12:14:12 +0000470 int error;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000471 OpusDecInst* state;
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000472
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000473 if (inst != NULL) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100474 // Create Opus decoder state.
Minyue Li54d02782019-10-29 21:36:13 +0100475 state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000476 if (state == NULL) {
477 return -1;
478 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000479
Minyue Li54d02782019-10-29 21:36:13 +0100480 state->decoder =
481 opus_decoder_create(sample_rate_hz, static_cast<int>(channels), &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200482 if (error == OPUS_OK && state->decoder) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100483 // Creation of memory all ok.
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000484 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200485 state->sample_rate_hz = sample_rate_hz;
Minyue Li8e83c7a2019-11-04 14:47:52 +0100486 state->plc_use_prev_decoded_samples =
487 webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial);
488 if (state->plc_use_prev_decoded_samples) {
489 state->prev_decoded_samples =
490 DefaultFrameSizePerChannel(state->sample_rate_hz);
491 }
492 state->in_dtx_mode = 0;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000493 *inst = state;
494 return 0;
495 }
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000496
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100497 // If memory allocation was unsuccessful, free the entire state.
Alex Loiko50b8c392019-04-03 15:12:01 +0200498 if (state->decoder) {
499 opus_decoder_destroy(state->decoder);
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000500 }
501 free(state);
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000502 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000503 return -1;
504}
505
Alex Loiko50b8c392019-04-03 15:12:01 +0200506int16_t WebRtcOpus_MultistreamDecoderCreate(
Minyue Li54d02782019-10-29 21:36:13 +0100507 OpusDecInst** inst,
508 size_t channels,
Alex Loikoe5b94162019-04-08 17:19:41 +0200509 size_t streams,
Alex Loiko50b8c392019-04-03 15:12:01 +0200510 size_t coupled_streams,
511 const unsigned char* channel_mapping) {
512 int error;
513 OpusDecInst* state;
514
515 if (inst != NULL) {
516 // Create Opus decoder state.
Minyue Li54d02782019-10-29 21:36:13 +0100517 state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
Alex Loiko50b8c392019-04-03 15:12:01 +0200518 if (state == NULL) {
519 return -1;
520 }
521
Alex Loiko50b8c392019-04-03 15:12:01 +0200522 // Create new memory, always at 48000 Hz.
523 state->multistream_decoder = opus_multistream_decoder_create(
Minyue Li54d02782019-10-29 21:36:13 +0100524 48000, channels, streams, coupled_streams, channel_mapping, &error);
Alex Loiko50b8c392019-04-03 15:12:01 +0200525
526 if (error == OPUS_OK && state->multistream_decoder) {
527 // Creation of memory all ok.
528 state->channels = channels;
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200529 state->sample_rate_hz = 48000;
Minyue Li8e83c7a2019-11-04 14:47:52 +0100530 state->plc_use_prev_decoded_samples =
531 webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial);
532 if (state->plc_use_prev_decoded_samples) {
533 state->prev_decoded_samples =
534 DefaultFrameSizePerChannel(state->sample_rate_hz);
535 }
536 state->in_dtx_mode = 0;
Alex Loiko50b8c392019-04-03 15:12:01 +0200537 *inst = state;
538 return 0;
539 }
540
541 // If memory allocation was unsuccessful, free the entire state.
542 opus_multistream_decoder_destroy(state->multistream_decoder);
543 free(state);
544 }
545 return -1;
546}
547
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000548int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) {
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000549 if (inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200550 if (inst->decoder) {
551 opus_decoder_destroy(inst->decoder);
552 } else if (inst->multistream_decoder) {
553 opus_multistream_decoder_destroy(inst->multistream_decoder);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100554 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000555 free(inst);
556 return 0;
557 } else {
558 return -1;
559 }
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000560}
561
Peter Kasting69558702016-01-12 16:26:35 -0800562size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst) {
tina.legrand@webrtc.orgc4590582012-11-28 12:23:29 +0000563 return inst->channels;
564}
565
Karl Wiberg43766482015-08-27 15:22:11 +0200566void WebRtcOpus_DecoderInit(OpusDecInst* inst) {
Alex Loiko50b8c392019-04-03 15:12:01 +0200567 if (inst->decoder) {
568 opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100569 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100570 opus_multistream_decoder_ctl(inst->multistream_decoder, OPUS_RESET_STATE);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100571 }
Karl Wiberg43766482015-08-27 15:22:11 +0200572 inst->in_dtx_mode = 0;
tina.legrand@webrtc.org0ad3c1a2012-11-07 08:07:29 +0000573}
574
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000575/* For decoder to determine if it is to output speech or comfort noise. */
Peter Kastingdce40cf2015-08-24 14:52:23 -0700576static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000577 // Audio type becomes comfort noise if |encoded_byte| is 1 and keeps
578 // to be so if the following |encoded_byte| are 0 or 1.
579 if (encoded_bytes == 0 && inst->in_dtx_mode) {
580 return 2; // Comfort noise.
henrik.lundindeaf6fb2017-03-01 00:49:18 -0800581 } else if (encoded_bytes == 1 || encoded_bytes == 2) {
582 // TODO(henrik.lundin): There is a slight risk that a 2-byte payload is in
583 // fact a 1-byte TOC with a 1-byte payload. That will be erroneously
584 // interpreted as comfort noise output, but such a payload is probably
585 // faulty anyway.
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100586
587 // TODO(webrtc:10218): This is wrong for multistream opus. Then are several
588 // single-stream packets glued together with some packet size bytes in
589 // between. See https://tools.ietf.org/html/rfc6716#appendix-B
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000590 inst->in_dtx_mode = 1;
591 return 2; // Comfort noise.
592 } else {
593 inst->in_dtx_mode = 0;
594 return 0; // Speech.
595 }
596}
597
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000598/* |frame_size| is set to maximum Opus frame size in the normal case, and
599 * is set to the number of samples needed for PLC in case of losses.
600 * It is up to the caller to make sure the value is correct. */
Minyue Li54d02782019-10-29 21:36:13 +0100601static int DecodeNative(OpusDecInst* inst,
602 const uint8_t* encoded,
603 size_t encoded_bytes,
604 int frame_size,
605 int16_t* decoded,
606 int16_t* audio_type,
607 int decode_fec) {
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100608 int res = -1;
Alex Loiko50b8c392019-04-03 15:12:01 +0200609 if (inst->decoder) {
Minyue Li54d02782019-10-29 21:36:13 +0100610 res = opus_decode(
611 inst->decoder, encoded, static_cast<opus_int32>(encoded_bytes),
612 reinterpret_cast<opus_int16*>(decoded), frame_size, decode_fec);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100613 } else {
Minyue Li54d02782019-10-29 21:36:13 +0100614 res = opus_multistream_decode(inst->multistream_decoder, encoded,
615 static_cast<opus_int32>(encoded_bytes),
616 reinterpret_cast<opus_int16*>(decoded),
617 frame_size, decode_fec);
Alex Loiko7a3e43a2019-01-29 12:27:08 +0100618 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000619
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000620 if (res <= 0)
621 return -1;
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000622
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000623 *audio_type = DetermineAudioType(inst, encoded_bytes);
tina.legrand@webrtc.orga7d83872012-10-18 10:00:52 +0000624
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000625 return res;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000626}
627
Minyue Lifb075d52019-10-29 21:38:15 +0100628static int DecodePlc(OpusDecInst* inst, int16_t* decoded) {
629 int16_t audio_type = 0;
630 int decoded_samples;
Minyue Li8e83c7a2019-11-04 14:47:52 +0100631 int plc_samples =
632 FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz);
Minyue Lifb075d52019-10-29 21:38:15 +0100633
Minyue Li8e83c7a2019-11-04 14:47:52 +0100634 if (inst->plc_use_prev_decoded_samples) {
635 /* The number of samples we ask for is |number_of_lost_frames| times
636 * |prev_decoded_samples_|. Limit the number of samples to maximum
637 * |MaxFrameSizePerChannel()|. */
638 plc_samples = inst->prev_decoded_samples;
639 const int max_samples_per_channel =
640 MaxFrameSizePerChannel(inst->sample_rate_hz);
641 plc_samples = plc_samples <= max_samples_per_channel
642 ? plc_samples
643 : max_samples_per_channel;
644 }
Minyue Lifb075d52019-10-29 21:38:15 +0100645 decoded_samples =
646 DecodeNative(inst, NULL, 0, plc_samples, decoded, &audio_type, 0);
647 if (decoded_samples < 0) {
648 return -1;
649 }
650
651 return decoded_samples;
652}
653
Minyue Li54d02782019-10-29 21:36:13 +0100654int WebRtcOpus_Decode(OpusDecInst* inst,
655 const uint8_t* encoded,
656 size_t encoded_bytes,
657 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700658 int16_t* audio_type) {
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000659 int decoded_samples;
660
661 if (encoded_bytes == 0) {
662 *audio_type = DetermineAudioType(inst, encoded_bytes);
Minyue Lifb075d52019-10-29 21:38:15 +0100663 decoded_samples = DecodePlc(inst, decoded);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000664 } else {
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200665 decoded_samples = DecodeNative(inst, encoded, encoded_bytes,
666 MaxFrameSizePerChannel(inst->sample_rate_hz),
667 decoded, audio_type, 0);
minyue@webrtc.org0ca768b2014-12-11 16:09:35 +0000668 }
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000669 if (decoded_samples < 0) {
670 return -1;
671 }
672
Minyue Li8e83c7a2019-11-04 14:47:52 +0100673 if (inst->plc_use_prev_decoded_samples) {
674 /* Update decoded sample memory, to be used by the PLC in case of losses. */
675 inst->prev_decoded_samples = decoded_samples;
676 }
tina.legrand@webrtc.orgbd21fb52013-08-08 11:01:07 +0000677
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000678 return decoded_samples;
tina.legrand@webrtc.orgdb11fab2013-04-17 10:39:41 +0000679}
680
Minyue Li54d02782019-10-29 21:36:13 +0100681int WebRtcOpus_DecodeFec(OpusDecInst* inst,
682 const uint8_t* encoded,
683 size_t encoded_bytes,
684 int16_t* decoded,
Peter Kastingbba78072015-06-11 19:02:46 -0700685 int16_t* audio_type) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000686 int decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000687 int fec_samples;
688
689 if (WebRtcOpus_PacketHasFec(encoded, encoded_bytes) != 1) {
690 return 0;
691 }
692
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200693 fec_samples =
694 opus_packet_get_samples_per_frame(encoded, inst->sample_rate_hz);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000695
Minyue Li54d02782019-10-29 21:36:13 +0100696 decoded_samples = DecodeNative(inst, encoded, encoded_bytes, fec_samples,
697 decoded, audio_type, 1);
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000698 if (decoded_samples < 0) {
699 return -1;
700 }
701
minyue@webrtc.orgf563e852014-07-18 21:11:27 +0000702 return decoded_samples;
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000703}
704
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000705int WebRtcOpus_DurationEst(OpusDecInst* inst,
706 const uint8_t* payload,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700707 size_t payload_length_bytes) {
minyuel6d92bf52015-09-23 15:20:39 +0200708 if (payload_length_bytes == 0) {
709 // WebRtcOpus_Decode calls PLC when payload length is zero. So we return
710 // PLC duration correspondingly.
711 return WebRtcOpus_PlcDuration(inst);
712 }
713
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000714 int frames, samples;
Minyue Li54d02782019-10-29 21:36:13 +0100715 frames = opus_packet_get_nb_frames(
716 payload, static_cast<opus_int32>(payload_length_bytes));
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000717 if (frames < 0) {
718 /* Invalid payload data. */
719 return 0;
720 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200721 samples =
722 frames * opus_packet_get_samples_per_frame(payload, inst->sample_rate_hz);
723 if (samples > 120 * inst->sample_rate_hz / 1000) {
724 // More than 120 ms' worth of samples.
tina.legrand@webrtc.org4275ab12012-12-19 09:52:45 +0000725 return 0;
726 }
727 return samples;
728}
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000729
minyuel6d92bf52015-09-23 15:20:39 +0200730int WebRtcOpus_PlcDuration(OpusDecInst* inst) {
Minyue Li8e83c7a2019-11-04 14:47:52 +0100731 if (inst->plc_use_prev_decoded_samples) {
732 /* The number of samples we ask for is |number_of_lost_frames| times
733 * |prev_decoded_samples_|. Limit the number of samples to maximum
734 * |MaxFrameSizePerChannel()|. */
735 const int plc_samples = inst->prev_decoded_samples;
736 const int max_samples_per_channel =
737 MaxFrameSizePerChannel(inst->sample_rate_hz);
738 return plc_samples <= max_samples_per_channel ? plc_samples
739 : max_samples_per_channel;
740 }
741 return FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz);
minyuel6d92bf52015-09-23 15:20:39 +0200742}
743
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000744int WebRtcOpus_FecDurationEst(const uint8_t* payload,
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200745 size_t payload_length_bytes,
746 int sample_rate_hz) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000747 if (WebRtcOpus_PacketHasFec(payload, payload_length_bytes) != 1) {
748 return 0;
749 }
Karl Wiberga1d1a1e2019-05-28 14:41:07 +0200750 const int samples =
751 opus_packet_get_samples_per_frame(payload, sample_rate_hz);
752 const int samples_per_ms = sample_rate_hz / 1000;
753 if (samples < 10 * samples_per_ms || samples > 120 * samples_per_ms) {
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000754 /* Invalid payload duration. */
755 return 0;
756 }
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000757 return samples;
758}
759
Philipp Hancke0fd1ef12020-06-10 14:21:44 +0200760int WebRtcOpus_NumSilkFrames(const uint8_t* payload) {
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200761 // For computing the payload length in ms, the sample rate is not important
762 // since it cancels out. We use 48 kHz, but any valid sample rate would work.
763 int payload_length_ms =
764 opus_packet_get_samples_per_frame(payload, 48000) / 48;
765 if (payload_length_ms < 10)
766 payload_length_ms = 10;
767
768 int silk_frames;
769 switch (payload_length_ms) {
770 case 10:
771 case 20:
772 silk_frames = 1;
773 break;
774 case 40:
775 silk_frames = 2;
776 break;
777 case 60:
778 silk_frames = 3;
779 break;
780 default:
Minyue Li54d02782019-10-29 21:36:13 +0100781 return 0; // It is actually even an invalid packet.
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200782 }
Philipp Hancke0fd1ef12020-06-10 14:21:44 +0200783 return silk_frames;
784}
785
786// This method is based on Definition of the Opus Audio Codec
787// (https://tools.ietf.org/html/rfc6716). Basically, this method is based on
788// parsing the LP layer of an Opus packet, particularly the LBRR flag.
789int WebRtcOpus_PacketHasFec(const uint8_t* payload,
790 size_t payload_length_bytes) {
791 if (payload == NULL || payload_length_bytes == 0)
792 return 0;
793
794 // In CELT_ONLY mode, packets should not have FEC.
795 if (payload[0] & 0x80)
796 return 0;
797
798 int silk_frames = WebRtcOpus_NumSilkFrames(payload);
799 if (silk_frames == 0)
800 return 0; // Not valid.
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200801
802 const int channels = opus_packet_get_nb_channels(payload);
803 RTC_DCHECK(channels == 1 || channels == 2);
804
Philipp Hancke0fd1ef12020-06-10 14:21:44 +0200805 // Max number of frames in an Opus packet is 48.
806 opus_int16 frame_sizes[48];
807 const unsigned char* frame_data[48];
808
809 // Parse packet to get the frames. But we only care about the first frame,
810 // since we can only decode the FEC from the first one.
811 if (opus_packet_parse(payload, static_cast<opus_int32>(payload_length_bytes),
812 NULL, frame_data, frame_sizes, NULL) < 0) {
813 return 0;
814 }
815
816 if (frame_sizes[0] < 1) {
817 return 0;
818 }
819
Minyue Lie8fbc5d2019-07-03 10:14:18 +0200820 // A frame starts with the LP layer. The LP layer begins with two to eight
821 // header bits.These consist of one VAD bit per SILK frame (up to 3),
822 // followed by a single flag indicating the presence of LBRR frames.
823 // For a stereo packet, these first flags correspond to the mid channel, and
824 // a second set of flags is included for the side channel. Because these are
825 // the first symbols decoded by the range coder and because they are coded
826 // as binary values with uniform probability, they can be extracted directly
827 // from the most significant bits of the first byte of compressed data.
828 for (int n = 0; n < channels; n++) {
829 // The LBRR bit for channel 1 is on the (|silk_frames| + 1)-th bit, and
830 // that of channel 2 is on the |(|silk_frames| + 1) * 2 + 1|-th bit.
831 if (frame_data[0][0] & (0x80 >> ((n + 1) * (silk_frames + 1) - 1)))
minyue@webrtc.org46509c82014-03-07 11:49:11 +0000832 return 1;
833 }
834
835 return 0;
836}
Philipp Hancke0fd1ef12020-06-10 14:21:44 +0200837
838int WebRtcOpus_PacketHasVoiceActivity(const uint8_t* payload,
839 size_t payload_length_bytes) {
840 if (payload == NULL || payload_length_bytes == 0)
841 return 0;
842
843 // In CELT_ONLY mode we can not determine whether there is VAD.
844 if (payload[0] & 0x80)
845 return -1;
846
847 int silk_frames = WebRtcOpus_NumSilkFrames(payload);
848 if (silk_frames == 0)
Minyue Lic9408702020-11-03 23:22:26 +0100849 return -1;
Philipp Hancke0fd1ef12020-06-10 14:21:44 +0200850
851 const int channels = opus_packet_get_nb_channels(payload);
852 RTC_DCHECK(channels == 1 || channels == 2);
853
854 // Max number of frames in an Opus packet is 48.
855 opus_int16 frame_sizes[48];
856 const unsigned char* frame_data[48];
857
858 // Parse packet to get the frames.
859 int frames =
860 opus_packet_parse(payload, static_cast<opus_int32>(payload_length_bytes),
861 NULL, frame_data, frame_sizes, NULL);
862 if (frames < 0)
863 return -1;
864
865 // Iterate over all Opus frames which may contain multiple SILK frames.
866 for (int frame = 0; frame < frames; frame++) {
867 if (frame_sizes[frame] < 1) {
868 continue;
869 }
870 if (frame_data[frame][0] >> (8 - silk_frames))
871 return 1;
872 if (channels == 2 &&
873 (frame_data[frame][0] << (silk_frames + 1)) >> (8 - silk_frames))
874 return 1;
875 }
876
877 return 0;
878}