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