Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: webrtc/modules/audio_coding/codecs/g711/audio_encoder_pcm.cc

Issue 1322973004: Fold AudioEncoderMutable into AudioEncoder (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: review fixes Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/audio_coding/codecs/g711/include/audio_encoder_pcm.h" 11 #include "webrtc/modules/audio_coding/codecs/g711/include/audio_encoder_pcm.h"
12 12
13 #include <limits> 13 #include <limits>
14 14
15 #include "webrtc/base/checks.h" 15 #include "webrtc/base/checks.h"
16 #include "webrtc/common_types.h" 16 #include "webrtc/common_types.h"
17 #include "webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h" 17 #include "webrtc/modules/audio_coding/codecs/g711/include/g711_interface.h"
18 18
19 namespace webrtc { 19 namespace webrtc {
20 20
21 namespace { 21 namespace {
22
22 int16_t NumSamplesPerFrame(int num_channels, 23 int16_t NumSamplesPerFrame(int num_channels,
23 int frame_size_ms, 24 int frame_size_ms,
24 int sample_rate_hz) { 25 int sample_rate_hz) {
25 int samples_per_frame = num_channels * frame_size_ms * sample_rate_hz / 1000; 26 int samples_per_frame = num_channels * frame_size_ms * sample_rate_hz / 1000;
26 CHECK_LE(samples_per_frame, std::numeric_limits<int16_t>::max()) 27 CHECK_LE(samples_per_frame, std::numeric_limits<int16_t>::max())
27 << "Frame size too large."; 28 << "Frame size too large.";
28 return static_cast<int16_t>(samples_per_frame); 29 return static_cast<int16_t>(samples_per_frame);
29 } 30 }
31
32 template <typename T>
33 typename T::Config CreateConfig(const CodecInst& codec_inst) {
34 typename T::Config config;
35 config.frame_size_ms = codec_inst.pacsize / 8;
36 config.num_channels = codec_inst.channels;
37 config.payload_type = codec_inst.pltype;
38 return config;
39 }
40
30 } // namespace 41 } // namespace
31 42
32 bool AudioEncoderPcm::Config::IsOk() const { 43 bool AudioEncoderPcm::Config::IsOk() const {
33 return (frame_size_ms % 10 == 0) && (num_channels >= 1); 44 return (frame_size_ms % 10 == 0) && (num_channels >= 1);
34 } 45 }
35 46
36 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz) 47 AudioEncoderPcm::AudioEncoderPcm(const Config& config, int sample_rate_hz)
37 : sample_rate_hz_(sample_rate_hz), 48 : sample_rate_hz_(sample_rate_hz),
38 num_channels_(config.num_channels), 49 num_channels_(config.num_channels),
39 payload_type_(config.payload_type), 50 payload_type_(config.payload_type),
40 num_10ms_frames_per_packet_( 51 num_10ms_frames_per_packet_(
41 static_cast<size_t>(config.frame_size_ms / 10)), 52 static_cast<size_t>(config.frame_size_ms / 10)),
42 full_frame_samples_(NumSamplesPerFrame(config.num_channels, 53 full_frame_samples_(NumSamplesPerFrame(config.num_channels,
43 config.frame_size_ms, 54 config.frame_size_ms,
44 sample_rate_hz_)), 55 sample_rate_hz_)),
45 first_timestamp_in_buffer_(0) { 56 first_timestamp_in_buffer_(0) {
46 CHECK_GT(sample_rate_hz, 0) << "Sample rate must be larger than 0 Hz"; 57 CHECK_GT(sample_rate_hz, 0) << "Sample rate must be larger than 0 Hz";
47 CHECK_EQ(config.frame_size_ms % 10, 0) 58 CHECK_EQ(config.frame_size_ms % 10, 0)
48 << "Frame size must be an integer multiple of 10 ms."; 59 << "Frame size must be an integer multiple of 10 ms.";
49 speech_buffer_.reserve(full_frame_samples_); 60 speech_buffer_.reserve(full_frame_samples_);
50 } 61 }
51 62
52 AudioEncoderPcm::~AudioEncoderPcm() { 63 AudioEncoderPcm::~AudioEncoderPcm() = default;
64
65 size_t AudioEncoderPcm::MaxEncodedBytes() const {
66 return full_frame_samples_ * BytesPerSample();
53 } 67 }
54 68
55 int AudioEncoderPcm::SampleRateHz() const { 69 int AudioEncoderPcm::SampleRateHz() const {
56 return sample_rate_hz_; 70 return sample_rate_hz_;
57 } 71 }
58 72
59 int AudioEncoderPcm::NumChannels() const { 73 int AudioEncoderPcm::NumChannels() const {
60 return num_channels_; 74 return num_channels_;
61 } 75 }
62 76
63 size_t AudioEncoderPcm::MaxEncodedBytes() const {
64 return full_frame_samples_ * BytesPerSample();
65 }
66
67 size_t AudioEncoderPcm::Num10MsFramesInNextPacket() const { 77 size_t AudioEncoderPcm::Num10MsFramesInNextPacket() const {
68 return num_10ms_frames_per_packet_; 78 return num_10ms_frames_per_packet_;
69 } 79 }
70 80
71 size_t AudioEncoderPcm::Max10MsFramesInAPacket() const { 81 size_t AudioEncoderPcm::Max10MsFramesInAPacket() const {
72 return num_10ms_frames_per_packet_; 82 return num_10ms_frames_per_packet_;
73 } 83 }
74 84
75 int AudioEncoderPcm::GetTargetBitrate() const { 85 int AudioEncoderPcm::GetTargetBitrate() const {
76 return 8 * BytesPerSample() * SampleRateHz() * NumChannels(); 86 return 8 * BytesPerSample() * SampleRateHz() * NumChannels();
(...skipping 18 matching lines...) Expand all
95 CHECK_GE(max_encoded_bytes, full_frame_samples_); 105 CHECK_GE(max_encoded_bytes, full_frame_samples_);
96 EncodedInfo info; 106 EncodedInfo info;
97 info.encoded_timestamp = first_timestamp_in_buffer_; 107 info.encoded_timestamp = first_timestamp_in_buffer_;
98 info.payload_type = payload_type_; 108 info.payload_type = payload_type_;
99 info.encoded_bytes = 109 info.encoded_bytes =
100 EncodeCall(&speech_buffer_[0], full_frame_samples_, encoded); 110 EncodeCall(&speech_buffer_[0], full_frame_samples_, encoded);
101 speech_buffer_.clear(); 111 speech_buffer_.clear();
102 return info; 112 return info;
103 } 113 }
104 114
115 void AudioEncoderPcm::Reset() {
116 speech_buffer_.clear();
117 }
118
119 AudioEncoderPcmA::AudioEncoderPcmA(const CodecInst& codec_inst)
120 : AudioEncoderPcmA(CreateConfig<AudioEncoderPcmA>(codec_inst)) {}
121
105 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio, 122 size_t AudioEncoderPcmA::EncodeCall(const int16_t* audio,
106 size_t input_len, 123 size_t input_len,
107 uint8_t* encoded) { 124 uint8_t* encoded) {
108 return WebRtcG711_EncodeA(audio, input_len, encoded); 125 return WebRtcG711_EncodeA(audio, input_len, encoded);
109 } 126 }
110 127
111 int AudioEncoderPcmA::BytesPerSample() const { 128 int AudioEncoderPcmA::BytesPerSample() const {
112 return 1; 129 return 1;
113 } 130 }
114 131
132 AudioEncoderPcmU::AudioEncoderPcmU(const CodecInst& codec_inst)
133 : AudioEncoderPcmU(CreateConfig<AudioEncoderPcmU>(codec_inst)) {}
134
115 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio, 135 size_t AudioEncoderPcmU::EncodeCall(const int16_t* audio,
116 size_t input_len, 136 size_t input_len,
117 uint8_t* encoded) { 137 uint8_t* encoded) {
118 return WebRtcG711_EncodeU(audio, input_len, encoded); 138 return WebRtcG711_EncodeU(audio, input_len, encoded);
119 } 139 }
120 140
121 int AudioEncoderPcmU::BytesPerSample() const { 141 int AudioEncoderPcmU::BytesPerSample() const {
122 return 1; 142 return 1;
123 } 143 }
124 144
125 namespace {
126 template <typename T>
127 typename T::Config CreateConfig(const CodecInst& codec_inst) {
128 typename T::Config config;
129 config.frame_size_ms = codec_inst.pacsize / 8;
130 config.num_channels = codec_inst.channels;
131 config.payload_type = codec_inst.pltype;
132 return config;
133 }
134 } // namespace
135
136 AudioEncoderMutablePcmU::AudioEncoderMutablePcmU(const CodecInst& codec_inst)
137 : AudioEncoderMutableImpl<AudioEncoderPcmU>(
138 CreateConfig<AudioEncoderPcmU>(codec_inst)) {
139 }
140
141 AudioEncoderMutablePcmA::AudioEncoderMutablePcmA(const CodecInst& codec_inst)
142 : AudioEncoderMutableImpl<AudioEncoderPcmA>(
143 CreateConfig<AudioEncoderPcmA>(codec_inst)) {
144 }
145
146 } // namespace webrtc 145 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698