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