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