tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_coding/codecs/opus/opus_interface.h" |
kwiberg | 2e48646 | 2016-08-23 05:54:25 -0700 | [diff] [blame] | 12 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "rtc_base/checks.h" |
Minyue Li | 8e83c7a | 2019-11-04 14:47:52 +0100 | [diff] [blame] | 14 | #include "system_wrappers/include/field_trial.h" |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 15 | |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 16 | enum { |
minyue | 2e03c66 | 2017-02-01 17:31:11 -0800 | [diff] [blame] | 17 | #if WEBRTC_OPUS_SUPPORT_120MS_PTIME |
| 18 | /* Maximum supported frame size in WebRTC is 120 ms. */ |
| 19 | kWebRtcOpusMaxEncodeFrameSizeMs = 120, |
| 20 | #else |
tina.legrand@webrtc.org | 46d90dc | 2013-02-01 14:20:06 +0000 | [diff] [blame] | 21 | /* Maximum supported frame size in WebRTC is 60 ms. */ |
| 22 | kWebRtcOpusMaxEncodeFrameSizeMs = 60, |
minyue | 2e03c66 | 2017-02-01 17:31:11 -0800 | [diff] [blame] | 23 | #endif |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 24 | |
tina.legrand@webrtc.org | 45426ea | 2013-07-03 13:32:04 +0000 | [diff] [blame] | 25 | /* The format allows up to 120 ms frames. Since we don't control the other |
| 26 | * side, we must allow for packets of that size. NetEq is currently limited |
| 27 | * to 60 ms on the receive side. */ |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 28 | kWebRtcOpusMaxDecodeFrameSizeMs = 120, |
Minyue Li | 8e83c7a | 2019-11-04 14:47:52 +0100 | [diff] [blame] | 29 | |
| 30 | // Duration of audio that each call to packet loss concealment covers. |
| 31 | kWebRtcOpusPlcFrameSizeMs = 10, |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Minyue Li | 8e83c7a | 2019-11-04 14:47:52 +0100 | [diff] [blame] | 34 | constexpr char kPlcUsePrevDecodedSamplesFieldTrial[] = |
| 35 | "WebRTC-Audio-OpusPlcUsePrevDecodedSamples"; |
| 36 | |
Karl Wiberg | a1d1a1e | 2019-05-28 14:41:07 +0200 | [diff] [blame] | 37 | static int FrameSizePerChannel(int frame_size_ms, int sample_rate_hz) { |
| 38 | RTC_DCHECK_GT(frame_size_ms, 0); |
| 39 | RTC_DCHECK_EQ(frame_size_ms % 10, 0); |
| 40 | RTC_DCHECK_GT(sample_rate_hz, 0); |
| 41 | RTC_DCHECK_EQ(sample_rate_hz % 1000, 0); |
| 42 | return frame_size_ms * (sample_rate_hz / 1000); |
| 43 | } |
| 44 | |
| 45 | // Maximum sample count per channel. |
| 46 | static int MaxFrameSizePerChannel(int sample_rate_hz) { |
| 47 | return FrameSizePerChannel(kWebRtcOpusMaxDecodeFrameSizeMs, sample_rate_hz); |
| 48 | } |
| 49 | |
| 50 | // Default sample count per channel. |
| 51 | static int DefaultFrameSizePerChannel(int sample_rate_hz) { |
| 52 | return FrameSizePerChannel(20, sample_rate_hz); |
| 53 | } |
| 54 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 55 | int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 56 | size_t channels, |
Karl Wiberg | 7e7c5c3 | 2019-05-21 11:50:32 +0200 | [diff] [blame] | 57 | int32_t application, |
| 58 | int sample_rate_hz) { |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 59 | int opus_app; |
| 60 | if (!inst) |
| 61 | return -1; |
tina.legrand@webrtc.org | d0d4149 | 2012-12-20 09:23:10 +0000 | [diff] [blame] | 62 | |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 63 | switch (application) { |
| 64 | case 0: |
| 65 | opus_app = OPUS_APPLICATION_VOIP; |
| 66 | break; |
| 67 | case 1: |
| 68 | opus_app = OPUS_APPLICATION_AUDIO; |
| 69 | break; |
| 70 | default: |
| 71 | return -1; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 72 | } |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 73 | |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 74 | OpusEncInst* state = |
| 75 | reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst))); |
kwiberg | 2e48646 | 2016-08-23 05:54:25 -0700 | [diff] [blame] | 76 | RTC_DCHECK(state); |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 77 | |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 78 | int error; |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 79 | state->encoder = opus_encoder_create( |
| 80 | sample_rate_hz, static_cast<int>(channels), opus_app, &error); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 81 | |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 82 | if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) { |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 83 | WebRtcOpus_EncoderFree(state); |
| 84 | return -1; |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 87 | state->in_dtx_mode = 0; |
| 88 | state->channels = channels; |
| 89 | |
| 90 | *inst = state; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | int16_t WebRtcOpus_MultistreamEncoderCreate( |
| 95 | OpusEncInst** inst, |
| 96 | size_t channels, |
| 97 | int32_t application, |
Alex Loiko | e5b9416 | 2019-04-08 17:19:41 +0200 | [diff] [blame] | 98 | size_t streams, |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 99 | size_t coupled_streams, |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 100 | const unsigned char* channel_mapping) { |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 101 | int opus_app; |
| 102 | if (!inst) |
| 103 | return -1; |
| 104 | |
| 105 | switch (application) { |
| 106 | case 0: |
| 107 | opus_app = OPUS_APPLICATION_VOIP; |
| 108 | break; |
| 109 | case 1: |
| 110 | opus_app = OPUS_APPLICATION_AUDIO; |
| 111 | break; |
| 112 | default: |
| 113 | return -1; |
| 114 | } |
| 115 | |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 116 | OpusEncInst* state = |
| 117 | reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst))); |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 118 | RTC_DCHECK(state); |
| 119 | |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 120 | int error; |
| 121 | state->multistream_encoder = |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 122 | opus_multistream_encoder_create(48000, channels, streams, coupled_streams, |
| 123 | channel_mapping, opus_app, &error); |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 124 | |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 125 | if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) { |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 126 | WebRtcOpus_EncoderFree(state); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | state->in_dtx_mode = 0; |
| 131 | state->channels = channels; |
| 132 | |
| 133 | *inst = state; |
| 134 | return 0; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst) { |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 138 | if (inst) { |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 139 | if (inst->encoder) { |
| 140 | opus_encoder_destroy(inst->encoder); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 141 | } else { |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 142 | opus_multistream_encoder_destroy(inst->multistream_encoder); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 143 | } |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 144 | free(inst); |
| 145 | return 0; |
| 146 | } else { |
| 147 | return -1; |
| 148 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 151 | int WebRtcOpus_Encode(OpusEncInst* inst, |
| 152 | const int16_t* audio_in, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 153 | size_t samples, |
| 154 | size_t length_encoded_buffer, |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 155 | uint8_t* encoded) { |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 156 | int res; |
| 157 | |
| 158 | if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) { |
| 159 | return -1; |
| 160 | } |
| 161 | |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 162 | if (inst->encoder) { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 163 | res = opus_encode(inst->encoder, (const opus_int16*)audio_in, |
| 164 | static_cast<int>(samples), encoded, |
| 165 | static_cast<opus_int32>(length_encoded_buffer)); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 166 | } else { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 167 | res = opus_multistream_encode( |
| 168 | inst->multistream_encoder, (const opus_int16*)audio_in, |
| 169 | static_cast<int>(samples), encoded, |
| 170 | static_cast<opus_int32>(length_encoded_buffer)); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 171 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 172 | |
flim | 64a7eab | 2016-08-12 04:36:05 -0700 | [diff] [blame] | 173 | if (res <= 0) { |
| 174 | return -1; |
| 175 | } |
| 176 | |
| 177 | if (res <= 2) { |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 178 | // Indicates DTX since the packet has nothing but a header. In principle, |
| 179 | // there is no need to send this packet. However, we do transmit the first |
| 180 | // occurrence to let the decoder know that the encoder enters DTX mode. |
| 181 | if (inst->in_dtx_mode) { |
| 182 | return 0; |
| 183 | } else { |
| 184 | inst->in_dtx_mode = 1; |
flim | 9238245 | 2017-02-10 13:50:38 -0800 | [diff] [blame] | 185 | return res; |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 186 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 187 | } |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 188 | |
flim | 64a7eab | 2016-08-12 04:36:05 -0700 | [diff] [blame] | 189 | inst->in_dtx_mode = 0; |
| 190 | return res; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 193 | #define ENCODER_CTL(inst, vargs) \ |
| 194 | (inst->encoder \ |
| 195 | ? opus_encoder_ctl(inst->encoder, vargs) \ |
| 196 | : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs)) |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 197 | |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 198 | int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) { |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 199 | if (inst) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 200 | return ENCODER_CTL(inst, OPUS_SET_BITRATE(rate)); |
minyue@webrtc.org | 0454688 | 2014-03-07 08:55:48 +0000 | [diff] [blame] | 201 | } else { |
| 202 | return -1; |
| 203 | } |
| 204 | } |
| 205 | |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 206 | int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate) { |
| 207 | if (inst) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 208 | return ENCODER_CTL(inst, OPUS_SET_PACKET_LOSS_PERC(loss_rate)); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 209 | } else { |
| 210 | return -1; |
| 211 | } |
| 212 | } |
| 213 | |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 214 | int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 215 | opus_int32 set_bandwidth; |
| 216 | |
| 217 | if (!inst) |
| 218 | return -1; |
| 219 | |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 220 | if (frequency_hz <= 8000) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 221 | set_bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 222 | } else if (frequency_hz <= 12000) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 223 | set_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 224 | } else if (frequency_hz <= 16000) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 225 | set_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 226 | } else if (frequency_hz <= 24000) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 227 | set_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
| 228 | } else { |
| 229 | set_bandwidth = OPUS_BANDWIDTH_FULLBAND; |
| 230 | } |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 231 | return ENCODER_CTL(inst, OPUS_SET_MAX_BANDWIDTH(set_bandwidth)); |
| 232 | } |
| 233 | |
| 234 | int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst, |
| 235 | int32_t* result_hz) { |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 236 | if (inst->encoder) { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 237 | if (opus_encoder_ctl(inst->encoder, OPUS_GET_MAX_BANDWIDTH(result_hz)) == |
| 238 | OPUS_OK) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 239 | return 0; |
| 240 | } |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | opus_int32 max_bandwidth; |
| 245 | int s; |
| 246 | int ret; |
| 247 | |
| 248 | max_bandwidth = 0; |
| 249 | ret = OPUS_OK; |
| 250 | s = 0; |
| 251 | while (ret == OPUS_OK) { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 252 | OpusEncoder* enc; |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 253 | opus_int32 bandwidth; |
| 254 | |
| 255 | ret = ENCODER_CTL(inst, OPUS_MULTISTREAM_GET_ENCODER_STATE(s, &enc)); |
| 256 | if (ret == OPUS_BAD_ARG) |
| 257 | break; |
| 258 | if (ret != OPUS_OK) |
| 259 | return -1; |
| 260 | if (opus_encoder_ctl(enc, OPUS_GET_MAX_BANDWIDTH(&bandwidth)) != OPUS_OK) |
| 261 | return -1; |
| 262 | |
| 263 | if (max_bandwidth != 0 && max_bandwidth != bandwidth) |
| 264 | return -1; |
| 265 | |
| 266 | max_bandwidth = bandwidth; |
| 267 | s++; |
| 268 | } |
| 269 | *result_hz = max_bandwidth; |
| 270 | return 0; |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 271 | } |
| 272 | |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 273 | int16_t WebRtcOpus_EnableFec(OpusEncInst* inst) { |
| 274 | if (inst) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 275 | return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(1)); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 276 | } else { |
| 277 | return -1; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | int16_t WebRtcOpus_DisableFec(OpusEncInst* inst) { |
| 282 | if (inst) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 283 | return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(0)); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 284 | } else { |
| 285 | return -1; |
| 286 | } |
| 287 | } |
| 288 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 289 | int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) { |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 290 | if (!inst) { |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 291 | return -1; |
| 292 | } |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 293 | |
| 294 | // To prevent Opus from entering CELT-only mode by forcing signal type to |
| 295 | // voice to make sure that DTX behaves correctly. Currently, DTX does not |
| 296 | // last long during a pure silence, if the signal type is not forced. |
| 297 | // TODO(minyue): Remove the signal type forcing when Opus DTX works properly |
| 298 | // without it. |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 299 | int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE)); |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 300 | if (ret != OPUS_OK) |
| 301 | return ret; |
| 302 | |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 303 | return ENCODER_CTL(inst, OPUS_SET_DTX(1)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) { |
| 307 | if (inst) { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 308 | int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_AUTO)); |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 309 | if (ret != OPUS_OK) |
| 310 | return ret; |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 311 | return ENCODER_CTL(inst, OPUS_SET_DTX(0)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 312 | } else { |
| 313 | return -1; |
| 314 | } |
| 315 | } |
| 316 | |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 317 | int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst) { |
| 318 | if (inst) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 319 | return ENCODER_CTL(inst, OPUS_SET_VBR(0)); |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 320 | } else { |
| 321 | return -1; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst) { |
| 326 | if (inst) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 327 | return ENCODER_CTL(inst, OPUS_SET_VBR(1)); |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 328 | } else { |
| 329 | return -1; |
| 330 | } |
| 331 | } |
| 332 | |
minyue@webrtc.org | 0454688 | 2014-03-07 08:55:48 +0000 | [diff] [blame] | 333 | int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) { |
| 334 | if (inst) { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 335 | return ENCODER_CTL(inst, OPUS_SET_COMPLEXITY(complexity)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 336 | } else { |
| 337 | return -1; |
| 338 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 341 | int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst) { |
| 342 | if (!inst) { |
| 343 | return -1; |
| 344 | } |
| 345 | int32_t bandwidth; |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 346 | if (ENCODER_CTL(inst, OPUS_GET_BANDWIDTH(&bandwidth)) == 0) { |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 347 | return bandwidth; |
| 348 | } else { |
| 349 | return -1; |
| 350 | } |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) { |
| 354 | if (inst) { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 355 | return ENCODER_CTL(inst, OPUS_SET_BANDWIDTH(bandwidth)); |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 356 | } else { |
| 357 | return -1; |
| 358 | } |
| 359 | } |
| 360 | |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 361 | int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels) { |
minyue | c8299f9 | 2016-09-27 02:08:47 -0700 | [diff] [blame] | 362 | if (!inst) |
| 363 | return -1; |
| 364 | if (num_channels == 0) { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 365 | return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO)); |
minyue | c8299f9 | 2016-09-27 02:08:47 -0700 | [diff] [blame] | 366 | } else if (num_channels == 1 || num_channels == 2) { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 367 | return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(num_channels)); |
minyue | c8299f9 | 2016-09-27 02:08:47 -0700 | [diff] [blame] | 368 | } else { |
| 369 | return -1; |
| 370 | } |
| 371 | } |
| 372 | |
Minyue Li | 332274d | 2019-11-13 16:05:46 +0100 | [diff] [blame^] | 373 | int32_t WebRtcOpus_GetInDtx(OpusEncInst* inst) { |
| 374 | if (!inst) { |
| 375 | return -1; |
| 376 | } |
| 377 | int32_t in_dtx; |
| 378 | if (ENCODER_CTL(inst, OPUS_GET_IN_DTX(&in_dtx)) == 0) { |
| 379 | return in_dtx; |
| 380 | } else { |
| 381 | return -1; |
| 382 | } |
| 383 | } |
| 384 | |
Karl Wiberg | a1d1a1e | 2019-05-28 14:41:07 +0200 | [diff] [blame] | 385 | int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, |
| 386 | size_t channels, |
| 387 | int sample_rate_hz) { |
minyue@webrtc.org | 33ccdfa | 2014-12-04 12:14:12 +0000 | [diff] [blame] | 388 | int error; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 389 | OpusDecInst* state; |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 390 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 391 | if (inst != NULL) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 392 | // Create Opus decoder state. |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 393 | state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst))); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 394 | if (state == NULL) { |
| 395 | return -1; |
| 396 | } |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 397 | |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 398 | state->decoder = |
| 399 | opus_decoder_create(sample_rate_hz, static_cast<int>(channels), &error); |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 400 | if (error == OPUS_OK && state->decoder) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 401 | // Creation of memory all ok. |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 402 | state->channels = channels; |
Karl Wiberg | a1d1a1e | 2019-05-28 14:41:07 +0200 | [diff] [blame] | 403 | state->sample_rate_hz = sample_rate_hz; |
Minyue Li | 8e83c7a | 2019-11-04 14:47:52 +0100 | [diff] [blame] | 404 | state->plc_use_prev_decoded_samples = |
| 405 | webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial); |
| 406 | if (state->plc_use_prev_decoded_samples) { |
| 407 | state->prev_decoded_samples = |
| 408 | DefaultFrameSizePerChannel(state->sample_rate_hz); |
| 409 | } |
| 410 | state->in_dtx_mode = 0; |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 411 | *inst = state; |
| 412 | return 0; |
| 413 | } |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 414 | |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 415 | // If memory allocation was unsuccessful, free the entire state. |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 416 | if (state->decoder) { |
| 417 | opus_decoder_destroy(state->decoder); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 418 | } |
| 419 | free(state); |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 420 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 421 | return -1; |
| 422 | } |
| 423 | |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 424 | int16_t WebRtcOpus_MultistreamDecoderCreate( |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 425 | OpusDecInst** inst, |
| 426 | size_t channels, |
Alex Loiko | e5b9416 | 2019-04-08 17:19:41 +0200 | [diff] [blame] | 427 | size_t streams, |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 428 | size_t coupled_streams, |
| 429 | const unsigned char* channel_mapping) { |
| 430 | int error; |
| 431 | OpusDecInst* state; |
| 432 | |
| 433 | if (inst != NULL) { |
| 434 | // Create Opus decoder state. |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 435 | state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst))); |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 436 | if (state == NULL) { |
| 437 | return -1; |
| 438 | } |
| 439 | |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 440 | // Create new memory, always at 48000 Hz. |
| 441 | state->multistream_decoder = opus_multistream_decoder_create( |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 442 | 48000, channels, streams, coupled_streams, channel_mapping, &error); |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 443 | |
| 444 | if (error == OPUS_OK && state->multistream_decoder) { |
| 445 | // Creation of memory all ok. |
| 446 | state->channels = channels; |
Karl Wiberg | a1d1a1e | 2019-05-28 14:41:07 +0200 | [diff] [blame] | 447 | state->sample_rate_hz = 48000; |
Minyue Li | 8e83c7a | 2019-11-04 14:47:52 +0100 | [diff] [blame] | 448 | state->plc_use_prev_decoded_samples = |
| 449 | webrtc::field_trial::IsEnabled(kPlcUsePrevDecodedSamplesFieldTrial); |
| 450 | if (state->plc_use_prev_decoded_samples) { |
| 451 | state->prev_decoded_samples = |
| 452 | DefaultFrameSizePerChannel(state->sample_rate_hz); |
| 453 | } |
| 454 | state->in_dtx_mode = 0; |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 455 | *inst = state; |
| 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | // If memory allocation was unsuccessful, free the entire state. |
| 460 | opus_multistream_decoder_destroy(state->multistream_decoder); |
| 461 | free(state); |
| 462 | } |
| 463 | return -1; |
| 464 | } |
| 465 | |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 466 | int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) { |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 467 | if (inst) { |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 468 | if (inst->decoder) { |
| 469 | opus_decoder_destroy(inst->decoder); |
| 470 | } else if (inst->multistream_decoder) { |
| 471 | opus_multistream_decoder_destroy(inst->multistream_decoder); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 472 | } |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 473 | free(inst); |
| 474 | return 0; |
| 475 | } else { |
| 476 | return -1; |
| 477 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 480 | size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst) { |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 481 | return inst->channels; |
| 482 | } |
| 483 | |
Karl Wiberg | 4376648 | 2015-08-27 15:22:11 +0200 | [diff] [blame] | 484 | void WebRtcOpus_DecoderInit(OpusDecInst* inst) { |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 485 | if (inst->decoder) { |
| 486 | opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 487 | } else { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 488 | opus_multistream_decoder_ctl(inst->multistream_decoder, OPUS_RESET_STATE); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 489 | } |
Karl Wiberg | 4376648 | 2015-08-27 15:22:11 +0200 | [diff] [blame] | 490 | inst->in_dtx_mode = 0; |
tina.legrand@webrtc.org | 0ad3c1a | 2012-11-07 08:07:29 +0000 | [diff] [blame] | 491 | } |
| 492 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 493 | /* For decoder to determine if it is to output speech or comfort noise. */ |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 494 | static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) { |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 495 | // Audio type becomes comfort noise if |encoded_byte| is 1 and keeps |
| 496 | // to be so if the following |encoded_byte| are 0 or 1. |
| 497 | if (encoded_bytes == 0 && inst->in_dtx_mode) { |
| 498 | return 2; // Comfort noise. |
henrik.lundin | deaf6fb | 2017-03-01 00:49:18 -0800 | [diff] [blame] | 499 | } else if (encoded_bytes == 1 || encoded_bytes == 2) { |
| 500 | // TODO(henrik.lundin): There is a slight risk that a 2-byte payload is in |
| 501 | // fact a 1-byte TOC with a 1-byte payload. That will be erroneously |
| 502 | // interpreted as comfort noise output, but such a payload is probably |
| 503 | // faulty anyway. |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 504 | |
| 505 | // TODO(webrtc:10218): This is wrong for multistream opus. Then are several |
| 506 | // single-stream packets glued together with some packet size bytes in |
| 507 | // between. See https://tools.ietf.org/html/rfc6716#appendix-B |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 508 | inst->in_dtx_mode = 1; |
| 509 | return 2; // Comfort noise. |
| 510 | } else { |
| 511 | inst->in_dtx_mode = 0; |
| 512 | return 0; // Speech. |
| 513 | } |
| 514 | } |
| 515 | |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 516 | /* |frame_size| is set to maximum Opus frame size in the normal case, and |
| 517 | * is set to the number of samples needed for PLC in case of losses. |
| 518 | * It is up to the caller to make sure the value is correct. */ |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 519 | static int DecodeNative(OpusDecInst* inst, |
| 520 | const uint8_t* encoded, |
| 521 | size_t encoded_bytes, |
| 522 | int frame_size, |
| 523 | int16_t* decoded, |
| 524 | int16_t* audio_type, |
| 525 | int decode_fec) { |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 526 | int res = -1; |
Alex Loiko | 50b8c39 | 2019-04-03 15:12:01 +0200 | [diff] [blame] | 527 | if (inst->decoder) { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 528 | res = opus_decode( |
| 529 | inst->decoder, encoded, static_cast<opus_int32>(encoded_bytes), |
| 530 | reinterpret_cast<opus_int16*>(decoded), frame_size, decode_fec); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 531 | } else { |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 532 | res = opus_multistream_decode(inst->multistream_decoder, encoded, |
| 533 | static_cast<opus_int32>(encoded_bytes), |
| 534 | reinterpret_cast<opus_int16*>(decoded), |
| 535 | frame_size, decode_fec); |
Alex Loiko | 7a3e43a | 2019-01-29 12:27:08 +0100 | [diff] [blame] | 536 | } |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 537 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 538 | if (res <= 0) |
| 539 | return -1; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 540 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 541 | *audio_type = DetermineAudioType(inst, encoded_bytes); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 542 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 543 | return res; |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Minyue Li | fb075d5 | 2019-10-29 21:38:15 +0100 | [diff] [blame] | 546 | static int DecodePlc(OpusDecInst* inst, int16_t* decoded) { |
| 547 | int16_t audio_type = 0; |
| 548 | int decoded_samples; |
Minyue Li | 8e83c7a | 2019-11-04 14:47:52 +0100 | [diff] [blame] | 549 | int plc_samples = |
| 550 | FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz); |
Minyue Li | fb075d5 | 2019-10-29 21:38:15 +0100 | [diff] [blame] | 551 | |
Minyue Li | 8e83c7a | 2019-11-04 14:47:52 +0100 | [diff] [blame] | 552 | if (inst->plc_use_prev_decoded_samples) { |
| 553 | /* The number of samples we ask for is |number_of_lost_frames| times |
| 554 | * |prev_decoded_samples_|. Limit the number of samples to maximum |
| 555 | * |MaxFrameSizePerChannel()|. */ |
| 556 | plc_samples = inst->prev_decoded_samples; |
| 557 | const int max_samples_per_channel = |
| 558 | MaxFrameSizePerChannel(inst->sample_rate_hz); |
| 559 | plc_samples = plc_samples <= max_samples_per_channel |
| 560 | ? plc_samples |
| 561 | : max_samples_per_channel; |
| 562 | } |
Minyue Li | fb075d5 | 2019-10-29 21:38:15 +0100 | [diff] [blame] | 563 | decoded_samples = |
| 564 | DecodeNative(inst, NULL, 0, plc_samples, decoded, &audio_type, 0); |
| 565 | if (decoded_samples < 0) { |
| 566 | return -1; |
| 567 | } |
| 568 | |
| 569 | return decoded_samples; |
| 570 | } |
| 571 | |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 572 | int WebRtcOpus_Decode(OpusDecInst* inst, |
| 573 | const uint8_t* encoded, |
| 574 | size_t encoded_bytes, |
| 575 | int16_t* decoded, |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 576 | int16_t* audio_type) { |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 577 | int decoded_samples; |
| 578 | |
| 579 | if (encoded_bytes == 0) { |
| 580 | *audio_type = DetermineAudioType(inst, encoded_bytes); |
Minyue Li | fb075d5 | 2019-10-29 21:38:15 +0100 | [diff] [blame] | 581 | decoded_samples = DecodePlc(inst, decoded); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 582 | } else { |
Karl Wiberg | a1d1a1e | 2019-05-28 14:41:07 +0200 | [diff] [blame] | 583 | decoded_samples = DecodeNative(inst, encoded, encoded_bytes, |
| 584 | MaxFrameSizePerChannel(inst->sample_rate_hz), |
| 585 | decoded, audio_type, 0); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 586 | } |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 587 | if (decoded_samples < 0) { |
| 588 | return -1; |
| 589 | } |
| 590 | |
Minyue Li | 8e83c7a | 2019-11-04 14:47:52 +0100 | [diff] [blame] | 591 | if (inst->plc_use_prev_decoded_samples) { |
| 592 | /* Update decoded sample memory, to be used by the PLC in case of losses. */ |
| 593 | inst->prev_decoded_samples = decoded_samples; |
| 594 | } |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 595 | |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 596 | return decoded_samples; |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 599 | int WebRtcOpus_DecodeFec(OpusDecInst* inst, |
| 600 | const uint8_t* encoded, |
| 601 | size_t encoded_bytes, |
| 602 | int16_t* decoded, |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 603 | int16_t* audio_type) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 604 | int decoded_samples; |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 605 | int fec_samples; |
| 606 | |
| 607 | if (WebRtcOpus_PacketHasFec(encoded, encoded_bytes) != 1) { |
| 608 | return 0; |
| 609 | } |
| 610 | |
Karl Wiberg | a1d1a1e | 2019-05-28 14:41:07 +0200 | [diff] [blame] | 611 | fec_samples = |
| 612 | opus_packet_get_samples_per_frame(encoded, inst->sample_rate_hz); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 613 | |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 614 | decoded_samples = DecodeNative(inst, encoded, encoded_bytes, fec_samples, |
| 615 | decoded, audio_type, 1); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 616 | if (decoded_samples < 0) { |
| 617 | return -1; |
| 618 | } |
| 619 | |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 620 | return decoded_samples; |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 621 | } |
| 622 | |
tina.legrand@webrtc.org | 4275ab1 | 2012-12-19 09:52:45 +0000 | [diff] [blame] | 623 | int WebRtcOpus_DurationEst(OpusDecInst* inst, |
| 624 | const uint8_t* payload, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 625 | size_t payload_length_bytes) { |
minyuel | 6d92bf5 | 2015-09-23 15:20:39 +0200 | [diff] [blame] | 626 | if (payload_length_bytes == 0) { |
| 627 | // WebRtcOpus_Decode calls PLC when payload length is zero. So we return |
| 628 | // PLC duration correspondingly. |
| 629 | return WebRtcOpus_PlcDuration(inst); |
| 630 | } |
| 631 | |
tina.legrand@webrtc.org | 4275ab1 | 2012-12-19 09:52:45 +0000 | [diff] [blame] | 632 | int frames, samples; |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 633 | frames = opus_packet_get_nb_frames( |
| 634 | payload, static_cast<opus_int32>(payload_length_bytes)); |
tina.legrand@webrtc.org | 4275ab1 | 2012-12-19 09:52:45 +0000 | [diff] [blame] | 635 | if (frames < 0) { |
| 636 | /* Invalid payload data. */ |
| 637 | return 0; |
| 638 | } |
Karl Wiberg | a1d1a1e | 2019-05-28 14:41:07 +0200 | [diff] [blame] | 639 | samples = |
| 640 | frames * opus_packet_get_samples_per_frame(payload, inst->sample_rate_hz); |
| 641 | if (samples > 120 * inst->sample_rate_hz / 1000) { |
| 642 | // More than 120 ms' worth of samples. |
tina.legrand@webrtc.org | 4275ab1 | 2012-12-19 09:52:45 +0000 | [diff] [blame] | 643 | return 0; |
| 644 | } |
| 645 | return samples; |
| 646 | } |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 647 | |
minyuel | 6d92bf5 | 2015-09-23 15:20:39 +0200 | [diff] [blame] | 648 | int WebRtcOpus_PlcDuration(OpusDecInst* inst) { |
Minyue Li | 8e83c7a | 2019-11-04 14:47:52 +0100 | [diff] [blame] | 649 | if (inst->plc_use_prev_decoded_samples) { |
| 650 | /* The number of samples we ask for is |number_of_lost_frames| times |
| 651 | * |prev_decoded_samples_|. Limit the number of samples to maximum |
| 652 | * |MaxFrameSizePerChannel()|. */ |
| 653 | const int plc_samples = inst->prev_decoded_samples; |
| 654 | const int max_samples_per_channel = |
| 655 | MaxFrameSizePerChannel(inst->sample_rate_hz); |
| 656 | return plc_samples <= max_samples_per_channel ? plc_samples |
| 657 | : max_samples_per_channel; |
| 658 | } |
| 659 | return FrameSizePerChannel(kWebRtcOpusPlcFrameSizeMs, inst->sample_rate_hz); |
minyuel | 6d92bf5 | 2015-09-23 15:20:39 +0200 | [diff] [blame] | 660 | } |
| 661 | |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 662 | int WebRtcOpus_FecDurationEst(const uint8_t* payload, |
Karl Wiberg | a1d1a1e | 2019-05-28 14:41:07 +0200 | [diff] [blame] | 663 | size_t payload_length_bytes, |
| 664 | int sample_rate_hz) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 665 | if (WebRtcOpus_PacketHasFec(payload, payload_length_bytes) != 1) { |
| 666 | return 0; |
| 667 | } |
Karl Wiberg | a1d1a1e | 2019-05-28 14:41:07 +0200 | [diff] [blame] | 668 | const int samples = |
| 669 | opus_packet_get_samples_per_frame(payload, sample_rate_hz); |
| 670 | const int samples_per_ms = sample_rate_hz / 1000; |
| 671 | if (samples < 10 * samples_per_ms || samples > 120 * samples_per_ms) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 672 | /* Invalid payload duration. */ |
| 673 | return 0; |
| 674 | } |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 675 | return samples; |
| 676 | } |
| 677 | |
Minyue Li | e8fbc5d | 2019-07-03 10:14:18 +0200 | [diff] [blame] | 678 | // This method is based on Definition of the Opus Audio Codec |
| 679 | // (https://tools.ietf.org/html/rfc6716). Basically, this method is based on |
| 680 | // parsing the LP layer of an Opus packet, particularly the LBRR flag. |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 681 | int WebRtcOpus_PacketHasFec(const uint8_t* payload, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 682 | size_t payload_length_bytes) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 683 | if (payload == NULL || payload_length_bytes == 0) |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 684 | return 0; |
| 685 | |
Minyue Li | e8fbc5d | 2019-07-03 10:14:18 +0200 | [diff] [blame] | 686 | // In CELT_ONLY mode, packets should not have FEC. |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 687 | if (payload[0] & 0x80) |
| 688 | return 0; |
| 689 | |
Minyue Li | e8fbc5d | 2019-07-03 10:14:18 +0200 | [diff] [blame] | 690 | // Max number of frames in an Opus packet is 48. |
| 691 | opus_int16 frame_sizes[48]; |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 692 | const unsigned char* frame_data[48]; |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 693 | |
Minyue Li | e8fbc5d | 2019-07-03 10:14:18 +0200 | [diff] [blame] | 694 | // Parse packet to get the frames. But we only care about the first frame, |
| 695 | // since we can only decode the FEC from the first one. |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 696 | if (opus_packet_parse(payload, static_cast<opus_int32>(payload_length_bytes), |
| 697 | NULL, frame_data, frame_sizes, NULL) < 0) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | if (frame_sizes[0] <= 1) { |
| 702 | return 0; |
| 703 | } |
| 704 | |
Minyue Li | e8fbc5d | 2019-07-03 10:14:18 +0200 | [diff] [blame] | 705 | // For computing the payload length in ms, the sample rate is not important |
| 706 | // since it cancels out. We use 48 kHz, but any valid sample rate would work. |
| 707 | int payload_length_ms = |
| 708 | opus_packet_get_samples_per_frame(payload, 48000) / 48; |
| 709 | if (payload_length_ms < 10) |
| 710 | payload_length_ms = 10; |
| 711 | |
| 712 | int silk_frames; |
| 713 | switch (payload_length_ms) { |
| 714 | case 10: |
| 715 | case 20: |
| 716 | silk_frames = 1; |
| 717 | break; |
| 718 | case 40: |
| 719 | silk_frames = 2; |
| 720 | break; |
| 721 | case 60: |
| 722 | silk_frames = 3; |
| 723 | break; |
| 724 | default: |
Minyue Li | 54d0278 | 2019-10-29 21:36:13 +0100 | [diff] [blame] | 725 | return 0; // It is actually even an invalid packet. |
Minyue Li | e8fbc5d | 2019-07-03 10:14:18 +0200 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | const int channels = opus_packet_get_nb_channels(payload); |
| 729 | RTC_DCHECK(channels == 1 || channels == 2); |
| 730 | |
| 731 | // A frame starts with the LP layer. The LP layer begins with two to eight |
| 732 | // header bits.These consist of one VAD bit per SILK frame (up to 3), |
| 733 | // followed by a single flag indicating the presence of LBRR frames. |
| 734 | // For a stereo packet, these first flags correspond to the mid channel, and |
| 735 | // a second set of flags is included for the side channel. Because these are |
| 736 | // the first symbols decoded by the range coder and because they are coded |
| 737 | // as binary values with uniform probability, they can be extracted directly |
| 738 | // from the most significant bits of the first byte of compressed data. |
| 739 | for (int n = 0; n < channels; n++) { |
| 740 | // The LBRR bit for channel 1 is on the (|silk_frames| + 1)-th bit, and |
| 741 | // that of channel 2 is on the |(|silk_frames| + 1) * 2 + 1|-th bit. |
| 742 | if (frame_data[0][0] & (0x80 >> ((n + 1) * (silk_frames + 1) - 1))) |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 743 | return 1; |
| 744 | } |
| 745 | |
| 746 | return 0; |
| 747 | } |