Renaming opus_interface.c to opus_interface.cc.

This is to allow advanced features of WebRTC/Chrome e.g., field trials.

More style compliant changes may follow up. Only a minimal (not in terms of line changes) is applied, so that presubmit does not complain. These changes include

1. removing unused headers.
2. eliminating c-style casting.

Bug: b/143582588
Change-Id: I6d0fd926c542ab0afdc38cc4bf03aaf584ec13dd
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/158670
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Commit-Queue: Minyue Li <minyue@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29657}
This commit is contained in:
Minyue Li 2019-10-29 21:36:13 +01:00 committed by Commit Bot
parent 09860e0bc3
commit 54d027843a
2 changed files with 79 additions and 92 deletions

View File

@ -765,7 +765,7 @@ rtc_library("webrtc_opus") {
"//third_party/abseil-cpp/absl/types:optional", "//third_party/abseil-cpp/absl/types:optional",
] ]
public_deps = [ # no-presubmit-check TODO(webrtc:8603) public_deps = [ # no-presubmit-check TODO(webrtc:8603)
":webrtc_opus_c", ":webrtc_opus_wrapper",
] ]
defines = audio_codec_defines defines = audio_codec_defines
@ -803,7 +803,7 @@ rtc_library("webrtc_multiopus") {
"//third_party/abseil-cpp/absl/types:optional", "//third_party/abseil-cpp/absl/types:optional",
] ]
public_deps = [ # no-presubmit-check TODO(webrtc:8603) public_deps = [ # no-presubmit-check TODO(webrtc:8603)
":webrtc_opus_c", ":webrtc_opus_wrapper",
] ]
defines = audio_codec_defines defines = audio_codec_defines
@ -815,11 +815,11 @@ rtc_library("webrtc_multiopus") {
} }
} }
rtc_library("webrtc_opus_c") { rtc_library("webrtc_opus_wrapper") {
poisonous = [ "audio_codecs" ] poisonous = [ "audio_codecs" ]
sources = [ sources = [
"codecs/opus/opus_inst.h", "codecs/opus/opus_inst.h",
"codecs/opus/opus_interface.c", "codecs/opus/opus_interface.cc",
"codecs/opus/opus_interface.h", "codecs/opus/opus_interface.h",
] ]
@ -1296,7 +1296,7 @@ if (rtc_include_tests) {
":audio_encoder_cng", ":audio_encoder_cng",
":pcm16b_c", ":pcm16b_c",
":red", ":red",
":webrtc_opus_c", ":webrtc_opus_wrapper",
"..:module_api", "..:module_api",
"../../api:rtp_headers", "../../api:rtp_headers",
"../../api/audio:audio_frame_api", "../../api/audio:audio_frame_api",

View File

@ -12,9 +12,6 @@
#include "rtc_base/checks.h" #include "rtc_base/checks.h"
#include <stdlib.h>
#include <string.h>
enum { enum {
#if WEBRTC_OPUS_SUPPORT_120MS_PTIME #if WEBRTC_OPUS_SUPPORT_120MS_PTIME
/* Maximum supported frame size in WebRTC is 120 ms. */ /* Maximum supported frame size in WebRTC is 120 ms. */
@ -67,15 +64,15 @@ int16_t WebRtcOpus_EncoderCreate(OpusEncInst** inst,
return -1; return -1;
} }
OpusEncInst* state = (OpusEncInst*)calloc(1, sizeof(OpusEncInst)); OpusEncInst* state =
reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
RTC_DCHECK(state); RTC_DCHECK(state);
int error; int error;
state->encoder = opus_encoder_create(sample_rate_hz, (int)channels, opus_app, state->encoder = opus_encoder_create(
&error); sample_rate_hz, static_cast<int>(channels), opus_app, &error);
if (error != OPUS_OK || (!state->encoder && if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
!state->multistream_encoder)) {
WebRtcOpus_EncoderFree(state); WebRtcOpus_EncoderFree(state);
return -1; return -1;
} }
@ -93,7 +90,7 @@ int16_t WebRtcOpus_MultistreamEncoderCreate(
int32_t application, int32_t application,
size_t streams, size_t streams,
size_t coupled_streams, size_t coupled_streams,
const unsigned char *channel_mapping) { const unsigned char* channel_mapping) {
int opus_app; int opus_app;
if (!inst) if (!inst)
return -1; return -1;
@ -109,22 +106,16 @@ int16_t WebRtcOpus_MultistreamEncoderCreate(
return -1; return -1;
} }
OpusEncInst* state = (OpusEncInst*)calloc(1, sizeof(OpusEncInst)); OpusEncInst* state =
reinterpret_cast<OpusEncInst*>(calloc(1, sizeof(OpusEncInst)));
RTC_DCHECK(state); RTC_DCHECK(state);
int error; int error;
state->multistream_encoder = state->multistream_encoder =
opus_multistream_encoder_create( opus_multistream_encoder_create(48000, channels, streams, coupled_streams,
48000, channel_mapping, opus_app, &error);
channels,
streams,
coupled_streams,
channel_mapping,
opus_app,
&error);
if (error != OPUS_OK || (!state->encoder && if (error != OPUS_OK || (!state->encoder && !state->multistream_encoder)) {
!state->multistream_encoder)) {
WebRtcOpus_EncoderFree(state); WebRtcOpus_EncoderFree(state);
return -1; return -1;
} }
@ -162,17 +153,14 @@ int WebRtcOpus_Encode(OpusEncInst* inst,
} }
if (inst->encoder) { if (inst->encoder) {
res = opus_encode(inst->encoder, res = opus_encode(inst->encoder, (const opus_int16*)audio_in,
(const opus_int16*)audio_in, static_cast<int>(samples), encoded,
(int)samples, static_cast<opus_int32>(length_encoded_buffer));
encoded,
(opus_int32)length_encoded_buffer);
} else { } else {
res = opus_multistream_encode(inst->multistream_encoder, res = opus_multistream_encode(
(const opus_int16*)audio_in, inst->multistream_encoder, (const opus_int16*)audio_in,
(int)samples, static_cast<int>(samples), encoded,
encoded, static_cast<opus_int32>(length_encoded_buffer));
(opus_int32)length_encoded_buffer);
} }
if (res <= 0) { if (res <= 0) {
@ -195,12 +183,11 @@ int WebRtcOpus_Encode(OpusEncInst* inst,
return res; return res;
} }
#define ENCODER_CTL(inst, vargs) ( \ #define ENCODER_CTL(inst, vargs) \
inst->encoder ? \ (inst->encoder \
opus_encoder_ctl(inst->encoder, vargs) \ ? opus_encoder_ctl(inst->encoder, vargs) \
: opus_multistream_encoder_ctl(inst->multistream_encoder, vargs)) : opus_multistream_encoder_ctl(inst->multistream_encoder, vargs))
int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) { int16_t WebRtcOpus_SetBitRate(OpusEncInst* inst, int32_t rate) {
if (inst) { if (inst) {
return ENCODER_CTL(inst, OPUS_SET_BITRATE(rate)); return ENCODER_CTL(inst, OPUS_SET_BITRATE(rate));
@ -240,9 +227,8 @@ int16_t WebRtcOpus_SetMaxPlaybackRate(OpusEncInst* inst, int32_t frequency_hz) {
int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst, int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
int32_t* result_hz) { int32_t* result_hz) {
if (inst->encoder) { if (inst->encoder) {
if (opus_encoder_ctl( if (opus_encoder_ctl(inst->encoder, OPUS_GET_MAX_BANDWIDTH(result_hz)) ==
inst->encoder, OPUS_OK) {
OPUS_GET_MAX_BANDWIDTH(result_hz)) == OPUS_OK) {
return 0; return 0;
} }
return -1; return -1;
@ -256,7 +242,7 @@ int16_t WebRtcOpus_GetMaxPlaybackRate(OpusEncInst* const inst,
ret = OPUS_OK; ret = OPUS_OK;
s = 0; s = 0;
while (ret == OPUS_OK) { while (ret == OPUS_OK) {
OpusEncoder *enc; OpusEncoder* enc;
opus_int32 bandwidth; opus_int32 bandwidth;
ret = ENCODER_CTL(inst, OPUS_MULTISTREAM_GET_ENCODER_STATE(s, &enc)); ret = ENCODER_CTL(inst, OPUS_MULTISTREAM_GET_ENCODER_STATE(s, &enc));
@ -303,8 +289,7 @@ int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) {
// last long during a pure silence, if the signal type is not forced. // 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 // TODO(minyue): Remove the signal type forcing when Opus DTX works properly
// without it. // without it.
int ret = ENCODER_CTL(inst, int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));
if (ret != OPUS_OK) if (ret != OPUS_OK)
return ret; return ret;
@ -313,8 +298,7 @@ int16_t WebRtcOpus_EnableDtx(OpusEncInst* inst) {
int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) { int16_t WebRtcOpus_DisableDtx(OpusEncInst* inst) {
if (inst) { if (inst) {
int ret = ENCODER_CTL(inst, int ret = ENCODER_CTL(inst, OPUS_SET_SIGNAL(OPUS_AUTO));
OPUS_SET_SIGNAL(OPUS_AUTO));
if (ret != OPUS_OK) if (ret != OPUS_OK)
return ret; return ret;
return ENCODER_CTL(inst, OPUS_SET_DTX(0)); return ENCODER_CTL(inst, OPUS_SET_DTX(0));
@ -341,8 +325,7 @@ int16_t WebRtcOpus_DisableCbr(OpusEncInst* inst) {
int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) { int16_t WebRtcOpus_SetComplexity(OpusEncInst* inst, int32_t complexity) {
if (inst) { if (inst) {
return ENCODER_CTL(inst, return ENCODER_CTL(inst, OPUS_SET_COMPLEXITY(complexity));
OPUS_SET_COMPLEXITY(complexity));
} else { } else {
return -1; return -1;
} }
@ -353,19 +336,16 @@ int32_t WebRtcOpus_GetBandwidth(OpusEncInst* inst) {
return -1; return -1;
} }
int32_t bandwidth; int32_t bandwidth;
if (ENCODER_CTL(inst, if (ENCODER_CTL(inst, OPUS_GET_BANDWIDTH(&bandwidth)) == 0) {
OPUS_GET_BANDWIDTH(&bandwidth)) == 0) {
return bandwidth; return bandwidth;
} else { } else {
return -1; return -1;
} }
} }
int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) { int16_t WebRtcOpus_SetBandwidth(OpusEncInst* inst, int32_t bandwidth) {
if (inst) { if (inst) {
return ENCODER_CTL(inst, return ENCODER_CTL(inst, OPUS_SET_BANDWIDTH(bandwidth));
OPUS_SET_BANDWIDTH(bandwidth));
} else { } else {
return -1; return -1;
} }
@ -375,11 +355,9 @@ int16_t WebRtcOpus_SetForceChannels(OpusEncInst* inst, size_t num_channels) {
if (!inst) if (!inst)
return -1; return -1;
if (num_channels == 0) { if (num_channels == 0) {
return ENCODER_CTL(inst, return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(OPUS_AUTO));
OPUS_SET_FORCE_CHANNELS(OPUS_AUTO));
} else if (num_channels == 1 || num_channels == 2) { } else if (num_channels == 1 || num_channels == 2) {
return ENCODER_CTL(inst, return ENCODER_CTL(inst, OPUS_SET_FORCE_CHANNELS(num_channels));
OPUS_SET_FORCE_CHANNELS(num_channels));
} else { } else {
return -1; return -1;
} }
@ -393,12 +371,13 @@ int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst,
if (inst != NULL) { if (inst != NULL) {
// Create Opus decoder state. // Create Opus decoder state.
state = (OpusDecInst*) calloc(1, sizeof(OpusDecInst)); state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
if (state == NULL) { if (state == NULL) {
return -1; return -1;
} }
state->decoder = opus_decoder_create(sample_rate_hz, (int)channels, &error); state->decoder =
opus_decoder_create(sample_rate_hz, static_cast<int>(channels), &error);
if (error == OPUS_OK && state->decoder) { if (error == OPUS_OK && state->decoder) {
// Creation of memory all ok. // Creation of memory all ok.
state->channels = channels; state->channels = channels;
@ -419,7 +398,8 @@ int16_t WebRtcOpus_DecoderCreate(OpusDecInst** inst,
} }
int16_t WebRtcOpus_MultistreamDecoderCreate( int16_t WebRtcOpus_MultistreamDecoderCreate(
OpusDecInst** inst, size_t channels, OpusDecInst** inst,
size_t channels,
size_t streams, size_t streams,
size_t coupled_streams, size_t coupled_streams,
const unsigned char* channel_mapping) { const unsigned char* channel_mapping) {
@ -428,18 +408,14 @@ int16_t WebRtcOpus_MultistreamDecoderCreate(
if (inst != NULL) { if (inst != NULL) {
// Create Opus decoder state. // Create Opus decoder state.
state = (OpusDecInst*) calloc(1, sizeof(OpusDecInst)); state = reinterpret_cast<OpusDecInst*>(calloc(1, sizeof(OpusDecInst)));
if (state == NULL) { if (state == NULL) {
return -1; return -1;
} }
// Create new memory, always at 48000 Hz. // Create new memory, always at 48000 Hz.
state->multistream_decoder = opus_multistream_decoder_create( state->multistream_decoder = opus_multistream_decoder_create(
48000, channels, 48000, channels, streams, coupled_streams, channel_mapping, &error);
streams,
coupled_streams,
channel_mapping,
&error);
if (error == OPUS_OK && state->multistream_decoder) { if (error == OPUS_OK && state->multistream_decoder) {
// Creation of memory all ok. // Creation of memory all ok.
@ -480,8 +456,7 @@ void WebRtcOpus_DecoderInit(OpusDecInst* inst) {
if (inst->decoder) { if (inst->decoder) {
opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE); opus_decoder_ctl(inst->decoder, OPUS_RESET_STATE);
} else { } else {
opus_multistream_decoder_ctl(inst->multistream_decoder, opus_multistream_decoder_ctl(inst->multistream_decoder, OPUS_RESET_STATE);
OPUS_RESET_STATE);
} }
inst->in_dtx_mode = 0; inst->in_dtx_mode = 0;
} }
@ -512,17 +487,23 @@ static int16_t DetermineAudioType(OpusDecInst* inst, size_t encoded_bytes) {
/* |frame_size| is set to maximum Opus frame size in the normal case, and /* |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. * 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. */ * It is up to the caller to make sure the value is correct. */
static int DecodeNative(OpusDecInst* inst, const uint8_t* encoded, static int DecodeNative(OpusDecInst* inst,
size_t encoded_bytes, int frame_size, const uint8_t* encoded,
int16_t* decoded, int16_t* audio_type, int decode_fec) { size_t encoded_bytes,
int frame_size,
int16_t* decoded,
int16_t* audio_type,
int decode_fec) {
int res = -1; int res = -1;
if (inst->decoder) { if (inst->decoder) {
res = opus_decode(inst->decoder, encoded, (opus_int32)encoded_bytes, res = opus_decode(
(opus_int16*)decoded, frame_size, decode_fec); inst->decoder, encoded, static_cast<opus_int32>(encoded_bytes),
reinterpret_cast<opus_int16*>(decoded), frame_size, decode_fec);
} else { } else {
res = opus_multistream_decode( res = opus_multistream_decode(inst->multistream_decoder, encoded,
inst->multistream_decoder, encoded, (opus_int32)encoded_bytes, static_cast<opus_int32>(encoded_bytes),
(opus_int16*)decoded, frame_size, decode_fec); reinterpret_cast<opus_int16*>(decoded),
frame_size, decode_fec);
} }
if (res <= 0) if (res <= 0)
@ -533,8 +514,10 @@ static int DecodeNative(OpusDecInst* inst, const uint8_t* encoded,
return res; return res;
} }
int WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded, int WebRtcOpus_Decode(OpusDecInst* inst,
size_t encoded_bytes, int16_t* decoded, const uint8_t* encoded,
size_t encoded_bytes,
int16_t* decoded,
int16_t* audio_type) { int16_t* audio_type) {
int decoded_samples; int decoded_samples;
@ -556,7 +539,8 @@ int WebRtcOpus_Decode(OpusDecInst* inst, const uint8_t* encoded,
return decoded_samples; return decoded_samples;
} }
int WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded, int WebRtcOpus_DecodePlc(OpusDecInst* inst,
int16_t* decoded,
int number_of_lost_frames) { int number_of_lost_frames) {
int16_t audio_type = 0; int16_t audio_type = 0;
int decoded_samples; int decoded_samples;
@ -571,8 +555,8 @@ int WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded,
plc_samples = plc_samples <= max_samples_per_channel plc_samples = plc_samples <= max_samples_per_channel
? plc_samples ? plc_samples
: max_samples_per_channel; : max_samples_per_channel;
decoded_samples = DecodeNative(inst, NULL, 0, plc_samples, decoded_samples =
decoded, &audio_type, 0); DecodeNative(inst, NULL, 0, plc_samples, decoded, &audio_type, 0);
if (decoded_samples < 0) { if (decoded_samples < 0) {
return -1; return -1;
} }
@ -580,8 +564,10 @@ int WebRtcOpus_DecodePlc(OpusDecInst* inst, int16_t* decoded,
return decoded_samples; return decoded_samples;
} }
int WebRtcOpus_DecodeFec(OpusDecInst* inst, const uint8_t* encoded, int WebRtcOpus_DecodeFec(OpusDecInst* inst,
size_t encoded_bytes, int16_t* decoded, const uint8_t* encoded,
size_t encoded_bytes,
int16_t* decoded,
int16_t* audio_type) { int16_t* audio_type) {
int decoded_samples; int decoded_samples;
int fec_samples; int fec_samples;
@ -593,8 +579,8 @@ int WebRtcOpus_DecodeFec(OpusDecInst* inst, const uint8_t* encoded,
fec_samples = fec_samples =
opus_packet_get_samples_per_frame(encoded, inst->sample_rate_hz); opus_packet_get_samples_per_frame(encoded, inst->sample_rate_hz);
decoded_samples = DecodeNative(inst, encoded, encoded_bytes, decoded_samples = DecodeNative(inst, encoded, encoded_bytes, fec_samples,
fec_samples, decoded, audio_type, 1); decoded, audio_type, 1);
if (decoded_samples < 0) { if (decoded_samples < 0) {
return -1; return -1;
} }
@ -612,7 +598,8 @@ int WebRtcOpus_DurationEst(OpusDecInst* inst,
} }
int frames, samples; int frames, samples;
frames = opus_packet_get_nb_frames(payload, (opus_int32)payload_length_bytes); frames = opus_packet_get_nb_frames(
payload, static_cast<opus_int32>(payload_length_bytes));
if (frames < 0) { if (frames < 0) {
/* Invalid payload data. */ /* Invalid payload data. */
return 0; return 0;
@ -667,12 +654,12 @@ int WebRtcOpus_PacketHasFec(const uint8_t* payload,
// Max number of frames in an Opus packet is 48. // Max number of frames in an Opus packet is 48.
opus_int16 frame_sizes[48]; opus_int16 frame_sizes[48];
const unsigned char *frame_data[48]; const unsigned char* frame_data[48];
// Parse packet to get the frames. But we only care about the first frame, // 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. // since we can only decode the FEC from the first one.
if (opus_packet_parse(payload, (opus_int32)payload_length_bytes, NULL, if (opus_packet_parse(payload, static_cast<opus_int32>(payload_length_bytes),
frame_data, frame_sizes, NULL) < 0) { NULL, frame_data, frame_sizes, NULL) < 0) {
return 0; return 0;
} }