2012-10-18 10:00:52 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_coding/codecs/opus/opus_interface.h"
|
2016-08-23 05:54:25 -07:00
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "rtc_base/checks.h"
|
2012-10-18 10:00:52 +00:00
|
|
|
|
|
|
|
|
enum {
|
2017-02-01 17:31:11 -08:00
|
|
|
#if WEBRTC_OPUS_SUPPORT_120MS_PTIME
|
|
|
|
|
/* Maximum supported frame size in WebRTC is 120 ms. */
|
|
|
|
|
kWebRtcOpusMaxEncodeFrameSizeMs = 120,
|
|
|
|
|
#else
|
2013-02-01 14:20:06 +00:00
|
|
|
/* Maximum supported frame size in WebRTC is 60 ms. */
|
|
|
|
|
kWebRtcOpusMaxEncodeFrameSizeMs = 60,
|
2017-02-01 17:31:11 -08:00
|
|
|
#endif
|
2012-10-18 10:00:52 +00:00
|
|
|
|
2013-07-03 13:32:04 +00:00
|
|
|
/* The format allows up to 120 ms frames. Since we don't control the other
|
|
|
|
|
* side, we must allow for packets of that size. NetEq is currently limited
|
|
|
|
|
* to 60 ms on the receive side. */
|
2012-10-18 10:00:52 +00:00
|
|
|
kWebRtcOpusMaxDecodeFrameSizeMs = 120,
|
2019-05-28 14:41:07 +02:00
|
|
|
};
|
2012-10-18 10:00:52 +00:00
|
|
|
|
2019-05-28 14:41:07 +02:00
|
|
|
static int FrameSizePerChannel(int frame_size_ms, int sample_rate_hz) {
|
|
|
|
|
RTC_DCHECK_GT(frame_size_ms, 0);
|
|
|
|
|
RTC_DCHECK_EQ(frame_size_ms % 10, 0);
|
|
|
|
|
RTC_DCHECK_GT(sample_rate_hz, 0);
|
|
|
|
|
RTC_DCHECK_EQ(sample_rate_hz % 1000, 0);
|
|
|
|
|
return frame_size_ms * (sample_rate_hz / 1000);
|
|
|
|
|
}
|
2013-07-03 13:32:04 +00:00
|
|
|
|
2019-05-28 14:41:07 +02:00
|
|
|
// Maximum sample count per channel.
|
|
|
|
|
static int MaxFrameSizePerChannel(int sample_rate_hz) {
|
|
|
|
|
return FrameSizePerChannel(kWebRtcOpusMaxDecodeFrameSizeMs, sample_rate_hz);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default sample count per channel.
|
|
|
|
|
static int DefaultFrameSizePerChannel(int sample_rate_hz) {
|
|
|
|
|
return FrameSizePerChannel(20, sample_rate_hz);
|
|
|
|
|
}
|
2012-10-18 10:00:52 +00:00
|
|
|
|
2015-01-20 16:01:50 +00:00
|
|
|
int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
size_t channels,
|
2019-05-21 11:50:32 +02:00
|
|
|
int32_t application,
|
|
|
|
|
int sample_rate_hz) {
|
2015-11-10 03:49:26 -08:00
|
|
|
int opus_app;
|
|
|
|
|
if (!inst)
|
|
|
|
|
return -1;
|
2015-11-09 10:08:15 -08:00
|
|
|
|
2015-11-10 03:49:26 -08:00
|
|
|
switch (application) {
|
|
|
|
|
case 0:
|
|
|
|
|
opus_app = OPUS_APPLICATION_VOIP;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
opus_app = OPUS_APPLICATION_AUDIO;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
2015-11-09 10:08:15 -08:00
|
|
|
}
|
2015-11-10 03:49:26 -08:00
|
|
|
|
2019-10-29 21:36:13 +01:00
|
|
|
OpusEncInst* state =
|
|
|
|
|
reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
|
2016-08-23 05:54:25 -07:00
|
|
|
RTC_DCHECK(state);
|
2015-11-10 03:49:26 -08:00
|
|
|
|
|
|
|
|
int error;
|
2019-10-29 21:36:13 +01:00
|
|
|
state->encoder = opus_encoder_create(
|
|
|
|
|
sample_rate_hz, static_cast<int>(channels), opus_app, &error);
|
2019-01-29 12:27:08 +01:00
|
|
|
|
2019-10-29 21:36:13 +01:00
|
|
|
if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
|
2019-04-03 15:12:01 +02:00
|
|
|
WebRtcOpus_EncoderFree(state);
|
|
|
|
|
return -1;
|
2019-01-29 12:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
2019-04-03 15:12:01 +02:00
|
|
|
state->in_dtx_mode = 0;
|
|
|
|
|
state->channels = channels;
|
|
|
|
|
|
|
|
|
|
*inst = state;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t WebRtcOpus_MultistreamEncoderCreate(
|
|
|
|
|
OpusEncInst** inst,
|
|
|
|
|
size_t channels,
|
|
|
|
|
int32_t application,
|
2019-04-08 17:19:41 +02:00
|
|
|
size_t streams,
|
2019-04-03 15:12:01 +02:00
|
|
|
size_t coupled_streams,
|
2019-10-29 21:36:13 +01:00
|
|
|
const unsigned char* channel_mapping) {
|
2019-04-03 15:12:01 +02:00
|
|
|
int opus_app;
|
|
|
|
|
if (!inst)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
switch (application) {
|
|
|
|
|
case 0:
|
|
|
|
|
opus_app = OPUS_APPLICATION_VOIP;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
opus_app = OPUS_APPLICATION_AUDIO;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-29 21:36:13 +01:00
|
|
|
OpusEncInst* state =
|
|
|
|
|
reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
|
2019-04-03 15:12:01 +02:00
|
|
|
RTC_DCHECK(state);
|
|
|
|
|
|
|
|
|
|
int error;
|
|
|
|
|
state->multistream_encoder =
|
2019-10-29 21:36:13 +01:00
|
|
|
opus_multistream_encoder_create(48000, channels, streams, coupled_streams,
|
|
|
|
|
channel_mapping, opus_app, &error);
|
|
|
|
|
|
|
|
|
|
if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
|
2015-11-10 03:49:26 -08:00
|
|
|
WebRtcOpus_EncoderFree(state);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state->in_dtx_mode = 0;
|
|
|
|
|
state->channels = channels;
|
|
|
|
|
|
|
|
|
|
*inst = state;
|
|
|
|
|
return 0;
|
2012-10-18 10:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t WebRtcOpus_EncoderFree(OpusEncInst* inst) {
|
2013-04-17 10:39:41 +00:00
|
|
|
if (inst) {
|
2019-04-03 15:12:01 +02:00
|
|
|
if (inst->encoder) {
|
|
|
|
|
opus_encoder_destroy(inst->encoder);
|
2019-01-29 12:27:08 +01:00
|
|
|
} else {
|
2019-04-03 15:12:01 +02:00
|
|
|
opus_multistream_encoder_destroy(inst->multistream_encoder);
|
2019-01-29 12:27:08 +01:00
|
|
|
}
|
2013-04-17 10:39:41 +00:00
|
|
|
free(inst);
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2012-10-18 10:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
Reland "Upconvert various types to int.", misc. codecs portion.
This reverts portions of commit cb180976dd0e9672cde4523d87b5f4857478b5e9, which
reverted commit 83ad33a8aed1fb00e422b6abd33c3e8942821c24. Specifically, the
files in webrtc/modules/audio_coding/codecs/ that are not in ilbc/ or isac/, as
well as webrtc/modules/audio_coding/main/test/opus_test.cc, are relanded.
The original commit message is below:
Upconvert various types to int.
Per comments from HL/kwiberg on https://webrtc-codereview.appspot.com/42569004 , when there is existing usage of mixed types (int16_t, int, etc.), we'd prefer to standardize on larger types like int and phase out use of int16_t.
Specifically, "Using int16 just because we're sure all reasonable values will fit in 16 bits isn't usually meaningful in C."
This converts some existing uses of int16_t (and, in a few cases, other types such as uint16_t) to int (or, in a few places, int32_t). Other locations will be converted to size_t in a separate change.
BUG=none
TBR=kwiberg
Review URL: https://codereview.webrtc.org/1179093003
Cr-Commit-Position: refs/heads/master@{#9424}
2015-06-11 19:02:46 -07:00
|
|
|
int WebRtcOpus_Encode(OpusEncInst* inst,
|
|
|
|
|
const int16_t* audio_in,
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
size_t samples,
|
|
|
|
|
size_t length_encoded_buffer,
|
Reland "Upconvert various types to int.", misc. codecs portion.
This reverts portions of commit cb180976dd0e9672cde4523d87b5f4857478b5e9, which
reverted commit 83ad33a8aed1fb00e422b6abd33c3e8942821c24. Specifically, the
files in webrtc/modules/audio_coding/codecs/ that are not in ilbc/ or isac/, as
well as webrtc/modules/audio_coding/main/test/opus_test.cc, are relanded.
The original commit message is below:
Upconvert various types to int.
Per comments from HL/kwiberg on https://webrtc-codereview.appspot.com/42569004 , when there is existing usage of mixed types (int16_t, int, etc.), we'd prefer to standardize on larger types like int and phase out use of int16_t.
Specifically, "Using int16 just because we're sure all reasonable values will fit in 16 bits isn't usually meaningful in C."
This converts some existing uses of int16_t (and, in a few cases, other types such as uint16_t) to int (or, in a few places, int32_t). Other locations will be converted to size_t in a separate change.
BUG=none
TBR=kwiberg
Review URL: https://codereview.webrtc.org/1179093003
Cr-Commit-Position: refs/heads/master@{#9424}
2015-06-11 19:02:46 -07:00
|
|
|
uint8_t* encoded) {
|
2012-10-18 10:00:52 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
if (samples > 48 * kWebRtcOpusMaxEncodeFrameSizeMs) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-03 15:12:01 +02:00
|
|
|
if (inst->encoder) {
|
2019-10-29 21:36:13 +01:00
|
|
|
res = opus_encode(inst->encoder, (const opus_int16*)audio_in,
|
|
|
|
|
static_cast<int>(samples), encoded,
|
|
|
|
|
static_cast<opus_int32>(length_encoded_buffer));
|
2019-01-29 12:27:08 +01:00
|
|
|
} else {
|
2019-10-29 21:36:13 +01:00
|
|
|
res = opus_multistream_encode(
|
|
|
|
|
inst->multistream_encoder, (const opus_int16*)audio_in,
|
|
|
|
|
static_cast<int>(samples), encoded,
|
|
|
|
|
static_cast<opus_int32>(length_encoded_buffer));
|
2019-01-29 12:27:08 +01:00
|
|
|
}
|
2012-10-18 10:00:52 +00:00
|
|
|
|
2016-08-12 04:36:05 -07:00
|
|
|
if (res <= 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res <= 2) {
|
2014-12-11 16:09:35 +00:00
|
|
|
// Indicates DTX since the packet has nothing but a header. In principle,
|
|
|
|
|
// there is no need to send this packet. However, we do transmit the first
|
|
|
|
|
// occurrence to let the decoder know that the encoder enters DTX mode.
|
|
|
|
|
if (inst->in_dtx_mode) {
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
inst->in_dtx_mode = 1;
|
2017-02-10 13:50:38 -08:00
|
|
|
return res;
|
2014-12-11 16:09:35 +00:00
|
|
|
}
|
2012-10-18 10:00:52 +00:00
|
|
|
}
|
2014-12-11 16:09:35 +00:00
|
|
|
|
2016-08-12 04:36:05 -07:00
|
|
|
inst->in_dtx_mode = 0;
|
|
|
|
|
return res;
|
2012-10-18 10:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-29 21:36:13 +01:00
|
|
|
#define ENCODER_CTL(inst, vargs) \
|
|
|
|
|
(inst->encoder \
|
|
|
|
|
? opus_encoder_ctl(inst->encoder, vargs) \
|
|
|
|
|
: opus_multistream_encoder_ctl(inst->multistream_encoder, vargs))
|
2019-01-29 12:27:08 +01:00
|
|
|
|
2012-10-18 10:00:52 +00:00
|
|
|
int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) {
|
2013-04-17 10:39:41 +00:00
|
|
|
if (inst) {
|
2019-01-29 12:27:08 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_BITRATE(rate));
|
2014-03-07 08:55:48 +00:00
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-07 11:49:11 +00:00
|
|
|
int16_t WebRtcOpus_SetPacketLossRate(OpusEncInst* inst, int32_t loss_rate) {
|
|
|
|
|
if (inst) {
|
2019-01-29 12:27:08 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_PACKET_LOSS_PERC(loss_rate));
|
2014-03-07 11:49:11 +00:00
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-03 12:28:06 +00:00
|
|
|
int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz) {
|
2014-08-04 14:41:57 +00:00
|
|
|
opus_int32 set_bandwidth;
|
|
|
|
|
|
|
|
|
|
if (!inst)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2014-09-03 12:28:06 +00:00
|
|
|
if (frequency_hz <= 8000) {
|
2014-08-04 14:41:57 +00:00
|
|
|
set_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
|
2014-09-03 12:28:06 +00:00
|
|
|
} else if (frequency_hz <= 12000) {
|
2014-08-04 14:41:57 +00:00
|
|
|
set_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
|
2014-09-03 12:28:06 +00:00
|
|
|
} else if (frequency_hz <= 16000) {
|
2014-08-04 14:41:57 +00:00
|
|
|
set_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
|
2014-09-03 12:28:06 +00:00
|
|
|
} else if (frequency_hz <= 24000) {
|
2014-08-04 14:41:57 +00:00
|
|
|
set_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
|
|
|
|
|
} else {
|
|
|
|
|
set_bandwidth = OPUS_BANDWIDTH_FULLBAND;
|
|
|
|
|
}
|
2019-01-29 12:27:08 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_MAX_BANDWIDTH(set_bandwidth));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
|
|
|
|
|
int32_t* result_hz) {
|
2019-04-03 15:12:01 +02:00
|
|
|
if (inst->encoder) {
|
2019-10-29 21:36:13 +01:00
|
|
|
if (opus_encoder_ctl(inst->encoder, OPUS_GET_MAX_BANDWIDTH(result_hz)) ==
|
|
|
|
|
OPUS_OK) {
|
2019-01-29 12:27:08 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opus_int32 max_bandwidth;
|
|
|
|
|
int s;
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
|
|
max_bandwidth = 0;
|
|
|
|
|
ret = OPUS_OK;
|
|
|
|
|
s = 0;
|
|
|
|
|
while (ret == OPUS_OK) {
|
2019-10-29 21:36:13 +01:00
|
|
|
OpusEncoder* enc;
|
2019-01-29 12:27:08 +01:00
|
|
|
opus_int32 bandwidth;
|
|
|
|
|
|
|
|
|
|
ret = ENCODER_CTL(inst, OPUS_MULTISTREAM_GET_ENCODER_STATE(s, &enc));
|
|
|
|
|
if (ret == OPUS_BAD_ARG)
|
|
|
|
|
break;
|
|
|
|
|
if (ret != OPUS_OK)
|
|
|
|
|
return -1;
|
|
|
|
|
if (opus_encoder_ctl(enc, OPUS_GET_MAX_BANDWIDTH(&bandwidth)) != OPUS_OK)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if (max_bandwidth != 0 && max_bandwidth != bandwidth)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
max_bandwidth = bandwidth;
|
|
|
|
|
s++;
|
|
|
|
|
}
|
|
|
|
|
*result_hz = max_bandwidth;
|
|
|
|
|
return 0;
|
2014-08-04 14:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-07 11:49:11 +00:00
|
|
|
int16_t WebRtcOpus_EnableFec(OpusEncInst* inst) {
|
|
|
|
|
if (inst) {
|
2019-01-29 12:27:08 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(1));
|
2014-03-07 11:49:11 +00:00
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t WebRtcOpus_DisableFec(OpusEncInst* inst) {
|
|
|
|
|
if (inst) {
|
2019-01-29 12:27:08 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_INBAND_FEC(0));
|
2014-03-07 11:49:11 +00:00
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-11 16:09:35 +00:00
|
|
|
int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) {
|
2015-05-11 12:19:35 +02:00
|
|
|
if (!inst) {
|
2014-12-11 16:09:35 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2015-05-11 12:19:35 +02:00
|
|
|
|
|
|
|
|
// To prevent Opus from entering CELT-only mode by forcing signal type to
|
|
|
|
|
// voice to make sure that DTX behaves correctly. Currently, DTX does not
|
|
|
|
|
// last long during a pure silence, if the signal type is not forced.
|
|
|
|
|
// TODO(minyue): Remove the signal type forcing when Opus DTX works properly
|
|
|
|
|
// without it.
|
2019-10-29 21:36:13 +01:00
|
|
|
int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
|
2015-05-11 12:19:35 +02:00
|
|
|
if (ret != OPUS_OK)
|
|
|
|
|
return ret;
|
|
|
|
|
|
2019-01-29 12:27:08 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_DTX(1));
|
2014-12-11 16:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) {
|
|
|
|
|
if (inst) {
|
2019-10-29 21:36:13 +01:00
|
|
|
int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_AUTO));
|
2015-05-11 12:19:35 +02:00
|
|
|
if (ret != OPUS_OK)
|
|
|
|
|
return ret;
|
2019-01-29 12:27:08 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_DTX(0));
|
2014-12-11 16:09:35 +00:00
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-06 05:48:36 -07:00
|
|
|
int16_t WebRtcOpus_EnableCbr(OpusEncInst* inst) {
|
|
|
|
|
if (inst) {
|
2019-01-29 12:27:08 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_VBR(0));
|
2017-04-06 05:48:36 -07:00
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst) {
|
|
|
|
|
if (inst) {
|
2019-01-29 12:27:08 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_VBR(1));
|
2017-04-06 05:48:36 -07:00
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-03-07 08:55:48 +00:00
|
|
|
int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) {
|
|
|
|
|
if (inst) {
|
2019-10-29 21:36:13 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_COMPLEXITY(complexity));
|
2013-04-17 10:39:41 +00:00
|
|
|
} else {
|
2017-11-20 11:13:56 -08:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst) {
|
|
|
|
|
if (!inst) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int32_t bandwidth;
|
2019-10-29 21:36:13 +01:00
|
|
|
if (ENCODER_CTL(inst, OPUS_GET_BANDWIDTH(&bandwidth)) == 0) {
|
2017-11-20 11:13:56 -08:00
|
|
|
return bandwidth;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) {
|
|
|
|
|
if (inst) {
|
2019-10-29 21:36:13 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_BANDWIDTH(bandwidth));
|
2017-11-20 11:13:56 -08:00
|
|
|
} else {
|
2013-04-17 10:39:41 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
2012-10-18 10:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
2016-10-06 07:13:54 -07:00
|
|
|
int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels) {
|
2016-09-27 02:08:47 -07:00
|
|
|
if (!inst)
|
|
|
|
|
return -1;
|
|
|
|
|
if (num_channels == 0) {
|
2019-10-29 21:36:13 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO));
|
2016-09-27 02:08:47 -07:00
|
|
|
} else if (num_channels == 1 || num_channels == 2) {
|
2019-10-29 21:36:13 +01:00
|
|
|
return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(num_channels));
|
2016-09-27 02:08:47 -07:00
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 14:41:07 +02:00
|
|
|
int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst,
|
|
|
|
|
size_t channels,
|
|
|
|
|
int sample_rate_hz) {
|
2014-12-04 12:14:12 +00:00
|
|
|
int error;
|
2012-10-18 10:00:52 +00:00
|
|
|
OpusDecInst* state;
|
2012-11-28 12:23:29 +00:00
|
|
|
|
2013-04-17 10:39:41 +00:00
|
|
|
if (inst != NULL) {
|
2019-01-29 12:27:08 +01:00
|
|
|
// Create Opus decoder state.
|
2019-10-29 21:36:13 +01:00
|
|
|
state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
|
2013-04-17 10:39:41 +00:00
|
|
|
if (state == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2012-11-28 12:23:29 +00:00
|
|
|
|
2019-10-29 21:36:13 +01:00
|
|
|
state->decoder =
|
|
|
|
|
opus_decoder_create(sample_rate_hz, static_cast<int>(channels), &error);
|
2019-04-03 15:12:01 +02:00
|
|
|
if (error == OPUS_OK && state->decoder) {
|
2019-01-29 12:27:08 +01:00
|
|
|
// Creation of memory all ok.
|
2013-04-17 10:39:41 +00:00
|
|
|
state->channels = channels;
|
2019-05-28 14:41:07 +02:00
|
|
|
state->prev_decoded_samples = DefaultFrameSizePerChannel(sample_rate_hz);
|
2014-12-11 16:09:35 +00:00
|
|
|
state->in_dtx_mode = 0;
|
2019-05-28 14:41:07 +02:00
|
|
|
state->sample_rate_hz = sample_rate_hz;
|
2013-04-17 10:39:41 +00:00
|
|
|
*inst = state;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2012-11-28 12:23:29 +00:00
|
|
|
|
2019-01-29 12:27:08 +01:00
|
|
|
// If memory allocation was unsuccessful, free the entire state.
|
2019-04-03 15:12:01 +02:00
|
|
|
if (state->decoder) {
|
|
|
|
|
opus_decoder_destroy(state->decoder);
|
|
|
|
|
}
|
|
|
|
|
free(state);
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t WebRtcOpus_MultistreamDecoderCreate(
|
2019-10-29 21:36:13 +01:00
|
|
|
OpusDecInst** inst,
|
|
|
|
|
size_t channels,
|
2019-04-08 17:19:41 +02:00
|
|
|
size_t streams,
|
2019-04-03 15:12:01 +02:00
|
|
|
size_t coupled_streams,
|
|
|
|
|
const unsigned char* channel_mapping) {
|
|
|
|
|
int error;
|
|
|
|
|
OpusDecInst* state;
|
2019-01-29 12:27:08 +01:00
|
|
|
|
2019-04-03 15:12:01 +02:00
|
|
|
if (inst != NULL) {
|
|
|
|
|
// Create Opus decoder state.
|
2019-10-29 21:36:13 +01:00
|
|
|
state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
|
2019-04-03 15:12:01 +02:00
|
|
|
if (state == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create new memory, always at 48000 Hz.
|
|
|
|
|
state->multistream_decoder = opus_multistream_decoder_create(
|
2019-10-29 21:36:13 +01:00
|
|
|
48000, channels, streams, coupled_streams, channel_mapping, &error);
|
2019-04-03 15:12:01 +02:00
|
|
|
|
|
|
|
|
if (error == OPUS_OK && state->multistream_decoder) {
|
|
|
|
|
// Creation of memory all ok.
|
|
|
|
|
state->channels = channels;
|
2019-05-28 14:41:07 +02:00
|
|
|
state->prev_decoded_samples = DefaultFrameSizePerChannel(48000);
|
2019-04-03 15:12:01 +02:00
|
|
|
state->in_dtx_mode = 0;
|
2019-05-28 14:41:07 +02:00
|
|
|
state->sample_rate_hz = 48000;
|
2019-04-03 15:12:01 +02:00
|
|
|
*inst = state;
|
|
|
|
|
return 0;
|
2013-04-17 10:39:41 +00:00
|
|
|
}
|
2019-04-03 15:12:01 +02:00
|
|
|
|
|
|
|
|
// If memory allocation was unsuccessful, free the entire state.
|
|
|
|
|
opus_multistream_decoder_destroy(state->multistream_decoder);
|
2013-04-17 10:39:41 +00:00
|
|
|
free(state);
|
2012-11-28 12:23:29 +00:00
|
|
|
}
|
2012-10-18 10:00:52 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int16_t WebRtcOpus_DecoderFree(OpusDecInst* inst) {
|
2013-04-17 10:39:41 +00:00
|
|
|
if (inst) {
|
2019-04-03 15:12:01 +02:00
|
|
|
if (inst->decoder) {
|
|
|
|
|
opus_decoder_destroy(inst->decoder);
|
|
|
|
|
} else if (inst->multistream_decoder) {
|
|
|
|
|
opus_multistream_decoder_destroy(inst->multistream_decoder);
|
2019-01-29 12:27:08 +01:00
|
|
|
}
|
2013-04-17 10:39:41 +00:00
|
|
|
free(inst);
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2012-10-18 10:00:52 +00:00
|
|
|
}
|
|
|
|
|
|
Convert channel counts to size_t.
IIRC, this was originally requested by ajm during review of the other size_t conversions I did over the past year, and I agreed it made sense, but wanted to do it separately since those changes were already gargantuan.
BUG=chromium:81439
TEST=none
R=henrik.lundin@webrtc.org, henrika@webrtc.org, kjellander@webrtc.org, minyue@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1316523002 .
Cr-Commit-Position: refs/heads/master@{#11229}
2016-01-12 16:26:35 -08:00
|
|
|
size_t WebRtcOpus_DecoderChannels(OpusDecInst* inst) {
|
2012-11-28 12:23:29 +00:00
|
|
|
return inst->channels;
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-27 15:22:11 +02:00
|
|
|
void WebRtcOpus_DecoderInit(OpusDecInst* inst) {
|
2019-04-03 15:12:01 +02:00
|
|
|
if (inst->decoder) {
|
|
|
|
|
opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE);
|
2019-01-29 12:27:08 +01:00
|
|
|
} else {
|
2019-10-29 21:36:13 +01:00
|
|
|
opus_multistream_decoder_ctl(inst->multistream_decoder, OPUS_RESET_STATE);
|
2019-01-29 12:27:08 +01:00
|
|
|
}
|
2015-08-27 15:22:11 +02:00
|
|
|
inst->in_dtx_mode = 0;
|
2012-11-07 08:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-11 16:09:35 +00:00
|
|
|
/* For decoder to determine if it is to output speech or comfort noise. */
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) {
|
2014-12-11 16:09:35 +00:00
|
|
|
// Audio type becomes comfort noise if |encoded_byte| is 1 and keeps
|
|
|
|
|
// to be so if the following |encoded_byte| are 0 or 1.
|
|
|
|
|
if (encoded_bytes == 0 && inst->in_dtx_mode) {
|
|
|
|
|
return 2; // Comfort noise.
|
2017-03-01 00:49:18 -08:00
|
|
|
} else if (encoded_bytes == 1 || encoded_bytes == 2) {
|
|
|
|
|
// TODO(henrik.lundin): There is a slight risk that a 2-byte payload is in
|
|
|
|
|
// fact a 1-byte TOC with a 1-byte payload. That will be erroneously
|
|
|
|
|
// interpreted as comfort noise output, but such a payload is probably
|
|
|
|
|
// faulty anyway.
|
2019-01-29 12:27:08 +01:00
|
|
|
|
|
|
|
|
// TODO(webrtc:10218): This is wrong for multistream opus. Then are several
|
|
|
|
|
// single-stream packets glued together with some packet size bytes in
|
|
|
|
|
// between. See https://tools.ietf.org/html/rfc6716#appendix-B
|
2014-12-11 16:09:35 +00:00
|
|
|
inst->in_dtx_mode = 1;
|
|
|
|
|
return 2; // Comfort noise.
|
|
|
|
|
} else {
|
|
|
|
|
inst->in_dtx_mode = 0;
|
|
|
|
|
return 0; // Speech.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 11:01:07 +00:00
|
|
|
/* |frame_size| is set to maximum Opus frame size in the normal case, and
|
|
|
|
|
* is set to the number of samples needed for PLC in case of losses.
|
|
|
|
|
* It is up to the caller to make sure the value is correct. */
|
2019-10-29 21:36:13 +01:00
|
|
|
static int DecodeNative(OpusDecInst* inst,
|
|
|
|
|
const uint8_t* encoded,
|
|
|
|
|
size_t encoded_bytes,
|
|
|
|
|
int frame_size,
|
|
|
|
|
int16_t* decoded,
|
|
|
|
|
int16_t* audio_type,
|
|
|
|
|
int decode_fec) {
|
2019-01-29 12:27:08 +01:00
|
|
|
int res = -1;
|
2019-04-03 15:12:01 +02:00
|
|
|
if (inst->decoder) {
|
2019-10-29 21:36:13 +01:00
|
|
|
res = opus_decode(
|
|
|
|
|
inst->decoder, encoded, static_cast<opus_int32>(encoded_bytes),
|
|
|
|
|
reinterpret_cast<opus_int16*>(decoded), frame_size, decode_fec);
|
2019-01-29 12:27:08 +01:00
|
|
|
} else {
|
2019-10-29 21:36:13 +01:00
|
|
|
res = opus_multistream_decode(inst->multistream_decoder, encoded,
|
|
|
|
|
static_cast<opus_int32>(encoded_bytes),
|
|
|
|
|
reinterpret_cast<opus_int16*>(decoded),
|
|
|
|
|
frame_size, decode_fec);
|
2019-01-29 12:27:08 +01:00
|
|
|
}
|
2014-03-07 11:49:11 +00:00
|
|
|
|
2014-12-11 16:09:35 +00:00
|
|
|
if (res <= 0)
|
|
|
|
|
return -1;
|
2014-03-07 11:49:11 +00:00
|
|
|
|
2014-12-11 16:09:35 +00:00
|
|
|
*audio_type = DetermineAudioType(inst, encoded_bytes);
|
2014-12-09 15:11:15 +00:00
|
|
|
|
2014-12-11 16:09:35 +00:00
|
|
|
return res;
|
2014-03-07 11:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-29 21:38:15 +01:00
|
|
|
static int DecodePlc(OpusDecInst* inst, int16_t* decoded) {
|
|
|
|
|
int16_t audio_type = 0;
|
|
|
|
|
int decoded_samples;
|
|
|
|
|
int plc_samples;
|
|
|
|
|
|
|
|
|
|
/* The number of samples we ask for is |number_of_lost_frames| times
|
|
|
|
|
* |prev_decoded_samples_|. Limit the number of samples to maximum
|
|
|
|
|
* |MaxFrameSizePerChannel()|. */
|
|
|
|
|
plc_samples = inst->prev_decoded_samples;
|
|
|
|
|
const int max_samples_per_channel =
|
|
|
|
|
MaxFrameSizePerChannel(inst->sample_rate_hz);
|
|
|
|
|
plc_samples = plc_samples <= max_samples_per_channel
|
|
|
|
|
? plc_samples
|
|
|
|
|
: max_samples_per_channel;
|
|
|
|
|
decoded_samples =
|
|
|
|
|
DecodeNative(inst, NULL, 0, plc_samples, decoded, &audio_type, 0);
|
|
|
|
|
if (decoded_samples < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decoded_samples;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-29 21:36:13 +01:00
|
|
|
int WebRtcOpus_Decode(OpusDecInst* inst,
|
|
|
|
|
const uint8_t* encoded,
|
|
|
|
|
size_t encoded_bytes,
|
|
|
|
|
int16_t* decoded,
|
Reland "Upconvert various types to int.", misc. codecs portion.
This reverts portions of commit cb180976dd0e9672cde4523d87b5f4857478b5e9, which
reverted commit 83ad33a8aed1fb00e422b6abd33c3e8942821c24. Specifically, the
files in webrtc/modules/audio_coding/codecs/ that are not in ilbc/ or isac/, as
well as webrtc/modules/audio_coding/main/test/opus_test.cc, are relanded.
The original commit message is below:
Upconvert various types to int.
Per comments from HL/kwiberg on https://webrtc-codereview.appspot.com/42569004 , when there is existing usage of mixed types (int16_t, int, etc.), we'd prefer to standardize on larger types like int and phase out use of int16_t.
Specifically, "Using int16 just because we're sure all reasonable values will fit in 16 bits isn't usually meaningful in C."
This converts some existing uses of int16_t (and, in a few cases, other types such as uint16_t) to int (or, in a few places, int32_t). Other locations will be converted to size_t in a separate change.
BUG=none
TBR=kwiberg
Review URL: https://codereview.webrtc.org/1179093003
Cr-Commit-Position: refs/heads/master@{#9424}
2015-06-11 19:02:46 -07:00
|
|
|
int16_t* audio_type) {
|
2014-12-11 16:09:35 +00:00
|
|
|
int decoded_samples;
|
|
|
|
|
|
|
|
|
|
if (encoded_bytes == 0) {
|
|
|
|
|
*audio_type = DetermineAudioType(inst, encoded_bytes);
|
2019-10-29 21:38:15 +01:00
|
|
|
decoded_samples = DecodePlc(inst, decoded);
|
2014-12-11 16:09:35 +00:00
|
|
|
} else {
|
2019-05-28 14:41:07 +02:00
|
|
|
decoded_samples = DecodeNative(inst, encoded, encoded_bytes,
|
|
|
|
|
MaxFrameSizePerChannel(inst->sample_rate_hz),
|
|
|
|
|
decoded, audio_type, 0);
|
2014-12-11 16:09:35 +00:00
|
|
|
}
|
2013-04-17 10:39:41 +00:00
|
|
|
if (decoded_samples < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-08 11:01:07 +00:00
|
|
|
/* Update decoded sample memory, to be used by the PLC in case of losses. */
|
|
|
|
|
inst->prev_decoded_samples = decoded_samples;
|
|
|
|
|
|
2014-07-18 21:11:27 +00:00
|
|
|
return decoded_samples;
|
2013-04-17 10:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-29 21:36:13 +01:00
|
|
|
int WebRtcOpus_DecodeFec(OpusDecInst* inst,
|
|
|
|
|
const uint8_t* encoded,
|
|
|
|
|
size_t encoded_bytes,
|
|
|
|
|
int16_t* decoded,
|
Reland "Upconvert various types to int.", misc. codecs portion.
This reverts portions of commit cb180976dd0e9672cde4523d87b5f4857478b5e9, which
reverted commit 83ad33a8aed1fb00e422b6abd33c3e8942821c24. Specifically, the
files in webrtc/modules/audio_coding/codecs/ that are not in ilbc/ or isac/, as
well as webrtc/modules/audio_coding/main/test/opus_test.cc, are relanded.
The original commit message is below:
Upconvert various types to int.
Per comments from HL/kwiberg on https://webrtc-codereview.appspot.com/42569004 , when there is existing usage of mixed types (int16_t, int, etc.), we'd prefer to standardize on larger types like int and phase out use of int16_t.
Specifically, "Using int16 just because we're sure all reasonable values will fit in 16 bits isn't usually meaningful in C."
This converts some existing uses of int16_t (and, in a few cases, other types such as uint16_t) to int (or, in a few places, int32_t). Other locations will be converted to size_t in a separate change.
BUG=none
TBR=kwiberg
Review URL: https://codereview.webrtc.org/1179093003
Cr-Commit-Position: refs/heads/master@{#9424}
2015-06-11 19:02:46 -07:00
|
|
|
int16_t* audio_type) {
|
2014-03-07 11:49:11 +00:00
|
|
|
int decoded_samples;
|
|
|
|
|
int fec_samples;
|
|
|
|
|
|
|
|
|
|
if (WebRtcOpus_PacketHasFec(encoded, encoded_bytes) != 1) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 14:41:07 +02:00
|
|
|
fec_samples =
|
|
|
|
|
opus_packet_get_samples_per_frame(encoded, inst->sample_rate_hz);
|
2014-03-07 11:49:11 +00:00
|
|
|
|
2019-10-29 21:36:13 +01:00
|
|
|
decoded_samples = DecodeNative(inst, encoded, encoded_bytes, fec_samples,
|
|
|
|
|
decoded, audio_type, 1);
|
2014-03-07 11:49:11 +00:00
|
|
|
if (decoded_samples < 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-18 21:11:27 +00:00
|
|
|
return decoded_samples;
|
2014-03-07 11:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-19 09:52:45 +00:00
|
|
|
int WebRtcOpus_DurationEst(OpusDecInst* inst,
|
|
|
|
|
const uint8_t* payload,
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
size_t payload_length_bytes) {
|
2015-09-23 15:20:39 +02:00
|
|
|
if (payload_length_bytes == 0) {
|
|
|
|
|
// WebRtcOpus_Decode calls PLC when payload length is zero. So we return
|
|
|
|
|
// PLC duration correspondingly.
|
|
|
|
|
return WebRtcOpus_PlcDuration(inst);
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-19 09:52:45 +00:00
|
|
|
int frames, samples;
|
2019-10-29 21:36:13 +01:00
|
|
|
frames = opus_packet_get_nb_frames(
|
|
|
|
|
payload, static_cast<opus_int32>(payload_length_bytes));
|
2012-12-19 09:52:45 +00:00
|
|
|
if (frames < 0) {
|
|
|
|
|
/* Invalid payload data. */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-05-28 14:41:07 +02:00
|
|
|
samples =
|
|
|
|
|
frames * opus_packet_get_samples_per_frame(payload, inst->sample_rate_hz);
|
|
|
|
|
if (samples > 120 * inst->sample_rate_hz / 1000) {
|
|
|
|
|
// More than 120 ms' worth of samples.
|
2012-12-19 09:52:45 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return samples;
|
|
|
|
|
}
|
2014-03-07 11:49:11 +00:00
|
|
|
|
2015-09-23 15:20:39 +02:00
|
|
|
int WebRtcOpus_PlcDuration(OpusDecInst* inst) {
|
|
|
|
|
/* The number of samples we ask for is |number_of_lost_frames| times
|
|
|
|
|
* |prev_decoded_samples_|. Limit the number of samples to maximum
|
2019-05-28 14:41:07 +02:00
|
|
|
* |MaxFrameSizePerChannel()|. */
|
2015-09-23 15:20:39 +02:00
|
|
|
const int plc_samples = inst->prev_decoded_samples;
|
2019-05-28 14:41:07 +02:00
|
|
|
const int max_samples_per_channel =
|
|
|
|
|
MaxFrameSizePerChannel(inst->sample_rate_hz);
|
|
|
|
|
return plc_samples <= max_samples_per_channel ? plc_samples
|
|
|
|
|
: max_samples_per_channel;
|
2015-09-23 15:20:39 +02:00
|
|
|
}
|
|
|
|
|
|
2014-03-07 11:49:11 +00:00
|
|
|
int WebRtcOpus_FecDurationEst(const uint8_t* payload,
|
2019-05-28 14:41:07 +02:00
|
|
|
size_t payload_length_bytes,
|
|
|
|
|
int sample_rate_hz) {
|
2014-03-07 11:49:11 +00:00
|
|
|
if (WebRtcOpus_PacketHasFec(payload, payload_length_bytes) != 1) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-05-28 14:41:07 +02:00
|
|
|
const int samples =
|
|
|
|
|
opus_packet_get_samples_per_frame(payload, sample_rate_hz);
|
|
|
|
|
const int samples_per_ms = sample_rate_hz / 1000;
|
|
|
|
|
if (samples < 10 * samples_per_ms || samples > 120 * samples_per_ms) {
|
2014-03-07 11:49:11 +00:00
|
|
|
/* Invalid payload duration. */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return samples;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-03 10:14:18 +02:00
|
|
|
// This method is based on Definition of the Opus Audio Codec
|
|
|
|
|
// (https://tools.ietf.org/html/rfc6716). Basically, this method is based on
|
|
|
|
|
// parsing the LP layer of an Opus packet, particularly the LBRR flag.
|
2014-03-07 11:49:11 +00:00
|
|
|
int WebRtcOpus_PacketHasFec(const uint8_t* payload,
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
size_t payload_length_bytes) {
|
2019-07-03 10:14:18 +02:00
|
|
|
if (payload == NULL || payload_length_bytes == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// In CELT_ONLY mode, packets should not have FEC.
|
|
|
|
|
if (payload[0] & 0x80)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// Max number of frames in an Opus packet is 48.
|
2014-03-07 11:49:11 +00:00
|
|
|
opus_int16 frame_sizes[48];
|
2019-10-29 21:36:13 +01:00
|
|
|
const unsigned char* frame_data[48];
|
2014-03-07 11:49:11 +00:00
|
|
|
|
2019-07-03 10:14:18 +02:00
|
|
|
// Parse packet to get the frames. But we only care about the first frame,
|
|
|
|
|
// since we can only decode the FEC from the first one.
|
2019-10-29 21:36:13 +01:00
|
|
|
if (opus_packet_parse(payload, static_cast<opus_int32>(payload_length_bytes),
|
|
|
|
|
NULL, frame_data, frame_sizes, NULL) < 0) {
|
2014-03-07 11:49:11 +00:00
|
|
|
return 0;
|
2019-07-03 10:14:18 +02:00
|
|
|
}
|
2014-03-07 11:49:11 +00:00
|
|
|
|
2019-07-03 10:14:18 +02:00
|
|
|
if (frame_sizes[0] <= 1) {
|
2014-03-07 11:49:11 +00:00
|
|
|
return 0;
|
2019-07-03 10:14:18 +02:00
|
|
|
}
|
2014-03-07 11:49:11 +00:00
|
|
|
|
2019-05-28 14:41:07 +02:00
|
|
|
// For computing the payload length in ms, the sample rate is not important
|
|
|
|
|
// since it cancels out. We use 48 kHz, but any valid sample rate would work.
|
2019-07-03 10:14:18 +02:00
|
|
|
int payload_length_ms =
|
|
|
|
|
opus_packet_get_samples_per_frame(payload, 48000) / 48;
|
|
|
|
|
if (payload_length_ms < 10)
|
2014-03-07 11:49:11 +00:00
|
|
|
payload_length_ms = 10;
|
|
|
|
|
|
2019-07-03 10:14:18 +02:00
|
|
|
int silk_frames;
|
2014-03-07 11:49:11 +00:00
|
|
|
switch (payload_length_ms) {
|
|
|
|
|
case 10:
|
2019-07-03 10:14:18 +02:00
|
|
|
case 20:
|
|
|
|
|
silk_frames = 1;
|
2014-03-07 11:49:11 +00:00
|
|
|
break;
|
2019-07-03 10:14:18 +02:00
|
|
|
case 40:
|
|
|
|
|
silk_frames = 2;
|
2014-03-07 11:49:11 +00:00
|
|
|
break;
|
2019-07-03 10:14:18 +02:00
|
|
|
case 60:
|
|
|
|
|
silk_frames = 3;
|
2014-03-07 11:49:11 +00:00
|
|
|
break;
|
2019-07-03 10:14:18 +02:00
|
|
|
default:
|
2019-10-29 21:36:13 +01:00
|
|
|
return 0; // It is actually even an invalid packet.
|
2014-03-07 11:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
2019-07-03 10:14:18 +02:00
|
|
|
const int channels = opus_packet_get_nb_channels(payload);
|
|
|
|
|
RTC_DCHECK(channels == 1 || channels == 2);
|
|
|
|
|
|
|
|
|
|
// A frame starts with the LP layer. The LP layer begins with two to eight
|
|
|
|
|
// header bits.These consist of one VAD bit per SILK frame (up to 3),
|
|
|
|
|
// followed by a single flag indicating the presence of LBRR frames.
|
|
|
|
|
// For a stereo packet, these first flags correspond to the mid channel, and
|
|
|
|
|
// a second set of flags is included for the side channel. Because these are
|
|
|
|
|
// the first symbols decoded by the range coder and because they are coded
|
|
|
|
|
// as binary values with uniform probability, they can be extracted directly
|
|
|
|
|
// from the most significant bits of the first byte of compressed data.
|
|
|
|
|
for (int n = 0; n < channels; n++) {
|
|
|
|
|
// The LBRR bit for channel 1 is on the (|silk_frames| + 1)-th bit, and
|
|
|
|
|
// that of channel 2 is on the |(|silk_frames| + 1) * 2 + 1|-th bit.
|
|
|
|
|
if (frame_data[0][0] & (0x80 >> ((n + 1) * (silk_frames + 1) - 1)))
|
2014-03-07 11:49:11 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|