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