1 /*
2  *  Copyright (c) 2014 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 
11 #include "modules/audio_coding/codecs/pcm16b/audio_encoder_pcm16b.h"
12 
13 #include <algorithm>
14 
15 #include "common_types.h"  // NOLINT(build/include)
16 #include "modules/audio_coding/codecs/pcm16b/pcm16b.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/numerics/safe_conversions.h"
19 
20 namespace webrtc {
21 
EncodeCall(const int16_t * audio,size_t input_len,uint8_t * encoded)22 size_t AudioEncoderPcm16B::EncodeCall(const int16_t* audio,
23                                       size_t input_len,
24                                       uint8_t* encoded) {
25   return WebRtcPcm16b_Encode(audio, input_len, encoded);
26 }
27 
BytesPerSample() const28 size_t AudioEncoderPcm16B::BytesPerSample() const {
29   return 2;
30 }
31 
GetCodecType() const32 AudioEncoder::CodecType AudioEncoderPcm16B::GetCodecType() const {
33   return CodecType::kOther;
34 }
35 
36 namespace {
37 
CreateConfig(const CodecInst & codec_inst)38 AudioEncoderPcm16B::Config CreateConfig(const CodecInst& codec_inst) {
39   AudioEncoderPcm16B::Config config;
40   config.num_channels = codec_inst.channels;
41   config.sample_rate_hz = codec_inst.plfreq;
42   config.frame_size_ms = rtc::CheckedDivExact(
43       codec_inst.pacsize, rtc::CheckedDivExact(config.sample_rate_hz, 1000));
44   config.payload_type = codec_inst.pltype;
45   return config;
46 }
47 
48 }  // namespace
49 
IsOk() const50 bool AudioEncoderPcm16B::Config::IsOk() const {
51   if ((sample_rate_hz != 8000) && (sample_rate_hz != 16000) &&
52       (sample_rate_hz != 32000) && (sample_rate_hz != 48000))
53     return false;
54   return AudioEncoderPcm::Config::IsOk();
55 }
56 
AudioEncoderPcm16B(const CodecInst & codec_inst)57 AudioEncoderPcm16B::AudioEncoderPcm16B(const CodecInst& codec_inst)
58     : AudioEncoderPcm16B(CreateConfig(codec_inst)) {}
59 
60 }  // namespace webrtc
61