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