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, |
| 31 | |
tina.legrand@webrtc.org | 45426ea | 2013-07-03 13:32:04 +0000 | [diff] [blame] | 32 | /* Maximum sample count per channel is 48 kHz * maximum frame size in |
| 33 | * milliseconds. */ |
| 34 | kWebRtcOpusMaxFrameSizePerChannel = 48 * kWebRtcOpusMaxDecodeFrameSizeMs, |
| 35 | |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 36 | /* Default frame size, 20 ms @ 48 kHz, in samples (for one channel). */ |
| 37 | kWebRtcOpusDefaultFrameSize = 960, |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 40 | int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 41 | size_t channels, |
minyue@webrtc.org | 7dba786 | 2015-01-20 16:01:50 +0000 | [diff] [blame] | 42 | int32_t application) { |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 43 | int opus_app; |
| 44 | if (!inst) |
| 45 | return -1; |
tina.legrand@webrtc.org | d0d4149 | 2012-12-20 09:23:10 +0000 | [diff] [blame] | 46 | |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 47 | switch (application) { |
| 48 | case 0: |
| 49 | opus_app = OPUS_APPLICATION_VOIP; |
| 50 | break; |
| 51 | case 1: |
| 52 | opus_app = OPUS_APPLICATION_AUDIO; |
| 53 | break; |
| 54 | default: |
| 55 | return -1; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 56 | } |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 57 | |
| 58 | OpusEncInst* state = calloc(1, sizeof(OpusEncInst)); |
kwiberg | 2e48646 | 2016-08-23 05:54:25 -0700 | [diff] [blame] | 59 | RTC_DCHECK(state); |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 60 | |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 61 | int error; |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 62 | state->encoder = opus_encoder_create(48000, (int)channels, opus_app, |
| 63 | &error); |
minyue | 3cea256 | 2015-11-10 03:49:26 -0800 | [diff] [blame] | 64 | if (error != OPUS_OK || !state->encoder) { |
| 65 | WebRtcOpus_EncoderFree(state); |
| 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | state->in_dtx_mode = 0; |
| 70 | state->channels = channels; |
| 71 | |
| 72 | *inst = state; |
| 73 | return 0; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst) { |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 77 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 78 | opus_encoder_destroy(inst->encoder); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 79 | free(inst); |
| 80 | return 0; |
| 81 | } else { |
| 82 | return -1; |
| 83 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 86 | int WebRtcOpus_Encode(OpusEncInst* inst, |
| 87 | const int16_t* audio_in, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 88 | size_t samples, |
| 89 | size_t length_encoded_buffer, |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 90 | uint8_t* encoded) { |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 91 | int res; |
| 92 | |
| 93 | if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) { |
| 94 | return -1; |
| 95 | } |
| 96 | |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 97 | res = opus_encode(inst->encoder, |
| 98 | (const opus_int16*)audio_in, |
| 99 | (int)samples, |
| 100 | encoded, |
| 101 | (opus_int32)length_encoded_buffer); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 102 | |
flim | 64a7eab | 2016-08-12 04:36:05 -0700 | [diff] [blame] | 103 | if (res <= 0) { |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | if (res <= 2) { |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 108 | // Indicates DTX since the packet has nothing but a header. In principle, |
| 109 | // there is no need to send this packet. However, we do transmit the first |
| 110 | // occurrence to let the decoder know that the encoder enters DTX mode. |
| 111 | if (inst->in_dtx_mode) { |
| 112 | return 0; |
| 113 | } else { |
| 114 | inst->in_dtx_mode = 1; |
flim | 9238245 | 2017-02-10 13:50:38 -0800 | [diff] [blame] | 115 | return res; |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 116 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 117 | } |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 118 | |
flim | 64a7eab | 2016-08-12 04:36:05 -0700 | [diff] [blame] | 119 | inst->in_dtx_mode = 0; |
| 120 | return res; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) { |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 124 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 125 | return opus_encoder_ctl(inst->encoder, OPUS_SET_BITRATE(rate)); |
minyue@webrtc.org | 0454688 | 2014-03-07 08:55:48 +0000 | [diff] [blame] | 126 | } else { |
| 127 | return -1; |
| 128 | } |
| 129 | } |
| 130 | |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 131 | int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate) { |
| 132 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 133 | return opus_encoder_ctl(inst->encoder, |
| 134 | OPUS_SET_PACKET_LOSS_PERC(loss_rate)); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 135 | } else { |
| 136 | return -1; |
| 137 | } |
| 138 | } |
| 139 | |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 140 | int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 141 | opus_int32 set_bandwidth; |
| 142 | |
| 143 | if (!inst) |
| 144 | return -1; |
| 145 | |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 146 | if (frequency_hz <= 8000) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 147 | set_bandwidth = OPUS_BANDWIDTH_NARROWBAND; |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 148 | } else if (frequency_hz <= 12000) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 149 | set_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND; |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 150 | } else if (frequency_hz <= 16000) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 151 | set_bandwidth = OPUS_BANDWIDTH_WIDEBAND; |
minyue@webrtc.org | adee8f9 | 2014-09-03 12:28:06 +0000 | [diff] [blame] | 152 | } else if (frequency_hz <= 24000) { |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 153 | set_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND; |
| 154 | } else { |
| 155 | set_bandwidth = OPUS_BANDWIDTH_FULLBAND; |
| 156 | } |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 157 | return opus_encoder_ctl(inst->encoder, |
| 158 | OPUS_SET_MAX_BANDWIDTH(set_bandwidth)); |
minyue@webrtc.org | 0040a6e | 2014-08-04 14:41:57 +0000 | [diff] [blame] | 159 | } |
| 160 | |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 161 | int16_t WebRtcOpus_EnableFec(OpusEncInst* inst) { |
| 162 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 163 | return opus_encoder_ctl(inst->encoder, OPUS_SET_INBAND_FEC(1)); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 164 | } else { |
| 165 | return -1; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | int16_t WebRtcOpus_DisableFec(OpusEncInst* inst) { |
| 170 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 171 | return opus_encoder_ctl(inst->encoder, OPUS_SET_INBAND_FEC(0)); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 172 | } else { |
| 173 | return -1; |
| 174 | } |
| 175 | } |
| 176 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 177 | int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) { |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 178 | if (!inst) { |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 179 | return -1; |
| 180 | } |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 181 | |
| 182 | // To prevent Opus from entering CELT-only mode by forcing signal type to |
| 183 | // voice to make sure that DTX behaves correctly. Currently, DTX does not |
| 184 | // last long during a pure silence, if the signal type is not forced. |
| 185 | // TODO(minyue): Remove the signal type forcing when Opus DTX works properly |
| 186 | // without it. |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 187 | int ret = opus_encoder_ctl(inst->encoder, |
| 188 | OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE)); |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 189 | if (ret != OPUS_OK) |
| 190 | return ret; |
| 191 | |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 192 | return opus_encoder_ctl(inst->encoder, OPUS_SET_DTX(1)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) { |
| 196 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 197 | int ret = opus_encoder_ctl(inst->encoder, |
| 198 | OPUS_SET_SIGNAL(OPUS_AUTO)); |
Minyue Li | 092041c | 2015-05-11 12:19:35 +0200 | [diff] [blame] | 199 | if (ret != OPUS_OK) |
| 200 | return ret; |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 201 | return opus_encoder_ctl(inst->encoder, OPUS_SET_DTX(0)); |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 202 | } else { |
| 203 | return -1; |
| 204 | } |
| 205 | } |
| 206 | |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 207 | int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst) { |
| 208 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 209 | return opus_encoder_ctl(inst->encoder, OPUS_SET_VBR(0)); |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 210 | } else { |
| 211 | return -1; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst) { |
| 216 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 217 | return opus_encoder_ctl(inst->encoder, OPUS_SET_VBR(1)); |
soren | 28dc285 | 2017-04-06 05:48:36 -0700 | [diff] [blame] | 218 | } else { |
| 219 | return -1; |
| 220 | } |
| 221 | } |
| 222 | |
minyue@webrtc.org | 0454688 | 2014-03-07 08:55:48 +0000 | [diff] [blame] | 223 | int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) { |
| 224 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 225 | return opus_encoder_ctl(inst->encoder, OPUS_SET_COMPLEXITY(complexity)); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 226 | } else { |
| 227 | return -1; |
| 228 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 231 | int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst) { |
| 232 | if (!inst) { |
| 233 | return -1; |
| 234 | } |
| 235 | int32_t bandwidth; |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 236 | if (opus_encoder_ctl(inst->encoder, OPUS_GET_BANDWIDTH(&bandwidth)) == 0) { |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 237 | return bandwidth; |
| 238 | } else { |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | } |
| 243 | |
| 244 | int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) { |
| 245 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 246 | return opus_encoder_ctl(inst->encoder, OPUS_SET_BANDWIDTH(bandwidth)); |
Alex Luebs | eeb2765 | 2017-11-20 11:13:56 -0800 | [diff] [blame] | 247 | } else { |
| 248 | return -1; |
| 249 | } |
| 250 | } |
| 251 | |
minyue | 41b9c80 | 2016-10-06 07:13:54 -0700 | [diff] [blame] | 252 | int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels) { |
minyue | c8299f9 | 2016-09-27 02:08:47 -0700 | [diff] [blame] | 253 | if (!inst) |
| 254 | return -1; |
| 255 | if (num_channels == 0) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 256 | return opus_encoder_ctl(inst->encoder, |
minyue | c8299f9 | 2016-09-27 02:08:47 -0700 | [diff] [blame] | 257 | OPUS_SET_FORCE_CHANNELS(OPUS_AUTO)); |
| 258 | } else if (num_channels == 1 || num_channels == 2) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 259 | return opus_encoder_ctl(inst->encoder, |
minyue | c8299f9 | 2016-09-27 02:08:47 -0700 | [diff] [blame] | 260 | OPUS_SET_FORCE_CHANNELS(num_channels)); |
| 261 | } else { |
| 262 | return -1; |
| 263 | } |
| 264 | } |
| 265 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 266 | int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst, size_t channels) { |
minyue@webrtc.org | 33ccdfa | 2014-12-04 12:14:12 +0000 | [diff] [blame] | 267 | int error; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 268 | OpusDecInst* state; |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 269 | |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 270 | if (inst != NULL) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 271 | /* Create Opus decoder state. */ |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 272 | state = (OpusDecInst*) calloc(1, sizeof(OpusDecInst)); |
| 273 | if (state == NULL) { |
| 274 | return -1; |
| 275 | } |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 276 | |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 277 | /* Create new memory, always at 48000 Hz. */ |
| 278 | state->decoder = opus_decoder_create(48000, (int)channels, &error); |
minyue@webrtc.org | 33ccdfa | 2014-12-04 12:14:12 +0000 | [diff] [blame] | 279 | if (error == OPUS_OK && state->decoder != NULL) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 280 | /* Creation of memory all ok. */ |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 281 | state->channels = channels; |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 282 | state->prev_decoded_samples = kWebRtcOpusDefaultFrameSize; |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 283 | state->in_dtx_mode = 0; |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 284 | *inst = state; |
| 285 | return 0; |
| 286 | } |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 287 | |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 288 | /* If memory allocation was unsuccessful, free the entire state. */ |
minyue@webrtc.org | 33ccdfa | 2014-12-04 12:14:12 +0000 | [diff] [blame] | 289 | if (state->decoder) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 290 | opus_decoder_destroy(state->decoder); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 291 | } |
| 292 | free(state); |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 293 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) { |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 298 | if (inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 299 | opus_decoder_destroy(inst->decoder); |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 300 | free(inst); |
| 301 | return 0; |
| 302 | } else { |
| 303 | return -1; |
| 304 | } |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 307 | size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst) { |
tina.legrand@webrtc.org | c459058 | 2012-11-28 12:23:29 +0000 | [diff] [blame] | 308 | return inst->channels; |
| 309 | } |
| 310 | |
Karl Wiberg | 4376648 | 2015-08-27 15:22:11 +0200 | [diff] [blame] | 311 | void WebRtcOpus_DecoderInit(OpusDecInst* inst) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 312 | opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE); |
Karl Wiberg | 4376648 | 2015-08-27 15:22:11 +0200 | [diff] [blame] | 313 | inst->in_dtx_mode = 0; |
tina.legrand@webrtc.org | 0ad3c1a | 2012-11-07 08:07:29 +0000 | [diff] [blame] | 314 | } |
| 315 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 316 | /* 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] | 317 | static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) { |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 318 | // Audio type becomes comfort noise if |encoded_byte| is 1 and keeps |
| 319 | // to be so if the following |encoded_byte| are 0 or 1. |
| 320 | if (encoded_bytes == 0 && inst->in_dtx_mode) { |
| 321 | return 2; // Comfort noise. |
henrik.lundin | deaf6fb | 2017-03-01 00:49:18 -0800 | [diff] [blame] | 322 | } else if (encoded_bytes == 1 || encoded_bytes == 2) { |
| 323 | // TODO(henrik.lundin): There is a slight risk that a 2-byte payload is in |
| 324 | // fact a 1-byte TOC with a 1-byte payload. That will be erroneously |
| 325 | // interpreted as comfort noise output, but such a payload is probably |
| 326 | // faulty anyway. |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 327 | inst->in_dtx_mode = 1; |
| 328 | return 2; // Comfort noise. |
| 329 | } else { |
| 330 | inst->in_dtx_mode = 0; |
| 331 | return 0; // Speech. |
| 332 | } |
| 333 | } |
| 334 | |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 335 | /* |frame_size| is set to maximum Opus frame size in the normal case, and |
| 336 | * is set to the number of samples needed for PLC in case of losses. |
| 337 | * 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] | 338 | static int DecodeNative(OpusDecInst* inst, const uint8_t* encoded, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 339 | size_t encoded_bytes, int frame_size, |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 340 | int16_t* decoded, int16_t* audio_type, int decode_fec) { |
Amit Hilbuch | 1fa51d6 | 2019-01-17 22:38:48 +0000 | [diff] [blame] | 341 | int res = opus_decode(inst->decoder, encoded, (opus_int32)encoded_bytes, |
| 342 | (opus_int16*)decoded, frame_size, decode_fec); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 343 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 344 | if (res <= 0) |
| 345 | return -1; |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 346 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 347 | *audio_type = DetermineAudioType(inst, encoded_bytes); |
tina.legrand@webrtc.org | a7d8387 | 2012-10-18 10:00:52 +0000 | [diff] [blame] | 348 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 349 | return res; |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 352 | int WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 353 | size_t encoded_bytes, int16_t* decoded, |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 354 | int16_t* audio_type) { |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 355 | int decoded_samples; |
| 356 | |
| 357 | if (encoded_bytes == 0) { |
| 358 | *audio_type = DetermineAudioType(inst, encoded_bytes); |
| 359 | decoded_samples = WebRtcOpus_DecodePlc(inst, decoded, 1); |
| 360 | } else { |
| 361 | decoded_samples = DecodeNative(inst, |
| 362 | encoded, |
| 363 | encoded_bytes, |
| 364 | kWebRtcOpusMaxFrameSizePerChannel, |
| 365 | decoded, |
| 366 | audio_type, |
| 367 | 0); |
| 368 | } |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 369 | if (decoded_samples < 0) { |
| 370 | return -1; |
| 371 | } |
| 372 | |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 373 | /* Update decoded sample memory, to be used by the PLC in case of losses. */ |
| 374 | inst->prev_decoded_samples = decoded_samples; |
| 375 | |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 376 | return decoded_samples; |
tina.legrand@webrtc.org | db11fab | 2013-04-17 10:39:41 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 379 | int WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded, |
| 380 | int number_of_lost_frames) { |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 381 | int16_t audio_type = 0; |
| 382 | int decoded_samples; |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 383 | int plc_samples; |
| 384 | |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 385 | /* The number of samples we ask for is |number_of_lost_frames| times |
| 386 | * |prev_decoded_samples_|. Limit the number of samples to maximum |
| 387 | * |kWebRtcOpusMaxFrameSizePerChannel|. */ |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 388 | plc_samples = number_of_lost_frames * inst->prev_decoded_samples; |
| 389 | plc_samples = (plc_samples <= kWebRtcOpusMaxFrameSizePerChannel) ? |
| 390 | plc_samples : kWebRtcOpusMaxFrameSizePerChannel; |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 391 | decoded_samples = DecodeNative(inst, NULL, 0, plc_samples, |
| 392 | decoded, &audio_type, 0); |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 393 | if (decoded_samples < 0) { |
| 394 | return -1; |
| 395 | } |
| 396 | |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 397 | return decoded_samples; |
tina.legrand@webrtc.org | bd21fb5 | 2013-08-08 11:01:07 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 400 | int WebRtcOpus_DecodeFec(OpusDecInst* inst, const uint8_t* encoded, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 401 | size_t encoded_bytes, int16_t* decoded, |
Peter Kasting | bba7807 | 2015-06-11 19:02:46 -0700 | [diff] [blame] | 402 | int16_t* audio_type) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 403 | int decoded_samples; |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 404 | int fec_samples; |
| 405 | |
| 406 | if (WebRtcOpus_PacketHasFec(encoded, encoded_bytes) != 1) { |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | fec_samples = opus_packet_get_samples_per_frame(encoded, 48000); |
| 411 | |
minyue@webrtc.org | 0ca768b | 2014-12-11 16:09:35 +0000 | [diff] [blame] | 412 | decoded_samples = DecodeNative(inst, encoded, encoded_bytes, |
| 413 | fec_samples, decoded, audio_type, 1); |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 414 | if (decoded_samples < 0) { |
| 415 | return -1; |
| 416 | } |
| 417 | |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 418 | return decoded_samples; |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 419 | } |
| 420 | |
tina.legrand@webrtc.org | 4275ab1 | 2012-12-19 09:52:45 +0000 | [diff] [blame] | 421 | int WebRtcOpus_DurationEst(OpusDecInst* inst, |
| 422 | const uint8_t* payload, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 423 | size_t payload_length_bytes) { |
minyuel | 6d92bf5 | 2015-09-23 15:20:39 +0200 | [diff] [blame] | 424 | if (payload_length_bytes == 0) { |
| 425 | // WebRtcOpus_Decode calls PLC when payload length is zero. So we return |
| 426 | // PLC duration correspondingly. |
| 427 | return WebRtcOpus_PlcDuration(inst); |
| 428 | } |
| 429 | |
tina.legrand@webrtc.org | 4275ab1 | 2012-12-19 09:52:45 +0000 | [diff] [blame] | 430 | int frames, samples; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 431 | 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] | 432 | if (frames < 0) { |
| 433 | /* Invalid payload data. */ |
| 434 | return 0; |
| 435 | } |
| 436 | samples = frames * opus_packet_get_samples_per_frame(payload, 48000); |
| 437 | if (samples < 120 || samples > 5760) { |
| 438 | /* Invalid payload duration. */ |
| 439 | return 0; |
| 440 | } |
| 441 | return samples; |
| 442 | } |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 443 | |
minyuel | 6d92bf5 | 2015-09-23 15:20:39 +0200 | [diff] [blame] | 444 | int WebRtcOpus_PlcDuration(OpusDecInst* inst) { |
| 445 | /* The number of samples we ask for is |number_of_lost_frames| times |
| 446 | * |prev_decoded_samples_|. Limit the number of samples to maximum |
| 447 | * |kWebRtcOpusMaxFrameSizePerChannel|. */ |
| 448 | const int plc_samples = inst->prev_decoded_samples; |
| 449 | return (plc_samples <= kWebRtcOpusMaxFrameSizePerChannel) ? |
| 450 | plc_samples : kWebRtcOpusMaxFrameSizePerChannel; |
| 451 | } |
| 452 | |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 453 | int WebRtcOpus_FecDurationEst(const uint8_t* payload, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 454 | size_t payload_length_bytes) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 455 | int samples; |
| 456 | if (WebRtcOpus_PacketHasFec(payload, payload_length_bytes) != 1) { |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | samples = opus_packet_get_samples_per_frame(payload, 48000); |
| 461 | if (samples < 480 || samples > 5760) { |
| 462 | /* Invalid payload duration. */ |
| 463 | return 0; |
| 464 | } |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 465 | return samples; |
| 466 | } |
| 467 | |
| 468 | int WebRtcOpus_PacketHasFec(const uint8_t* payload, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 469 | size_t payload_length_bytes) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 470 | int frames, channels, payload_length_ms; |
| 471 | int n; |
| 472 | opus_int16 frame_sizes[48]; |
| 473 | const unsigned char *frame_data[48]; |
| 474 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 475 | if (payload == NULL || payload_length_bytes == 0) |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 476 | return 0; |
| 477 | |
| 478 | /* In CELT_ONLY mode, packets should not have FEC. */ |
| 479 | if (payload[0] & 0x80) |
| 480 | return 0; |
| 481 | |
| 482 | payload_length_ms = opus_packet_get_samples_per_frame(payload, 48000) / 48; |
| 483 | if (10 > payload_length_ms) |
| 484 | payload_length_ms = 10; |
| 485 | |
| 486 | channels = opus_packet_get_nb_channels(payload); |
| 487 | |
| 488 | switch (payload_length_ms) { |
| 489 | case 10: |
| 490 | case 20: { |
| 491 | frames = 1; |
| 492 | break; |
| 493 | } |
| 494 | case 40: { |
| 495 | frames = 2; |
| 496 | break; |
| 497 | } |
| 498 | case 60: { |
| 499 | frames = 3; |
| 500 | break; |
| 501 | } |
| 502 | default: { |
| 503 | return 0; // It is actually even an invalid packet. |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /* The following is to parse the LBRR flags. */ |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 508 | if (opus_packet_parse(payload, (opus_int32)payload_length_bytes, NULL, |
| 509 | frame_data, frame_sizes, NULL) < 0) { |
minyue@webrtc.org | 46509c8 | 2014-03-07 11:49:11 +0000 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | if (frame_sizes[0] <= 1) { |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | for (n = 0; n < channels; n++) { |
| 518 | if (frame_data[0][0] & (0x80 >> ((n + 1) * (frames + 1) - 1))) |
| 519 | return 1; |
| 520 | } |
| 521 | |
| 522 | return 0; |
| 523 | } |