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

Side by Side Diff: webrtc/modules/audio_coding/codecs/g722/audio_encoder_g722.cc

Issue 1316523002: Convert channel counts to size_t. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc@master
Patch Set: Fix compile Created 4 years, 11 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
(...skipping 30 matching lines...) Expand all
41 payload_type_(config.payload_type), 41 payload_type_(config.payload_type),
42 num_10ms_frames_per_packet_( 42 num_10ms_frames_per_packet_(
43 static_cast<size_t>(config.frame_size_ms / 10)), 43 static_cast<size_t>(config.frame_size_ms / 10)),
44 num_10ms_frames_buffered_(0), 44 num_10ms_frames_buffered_(0),
45 first_timestamp_in_buffer_(0), 45 first_timestamp_in_buffer_(0),
46 encoders_(new EncoderState[num_channels_]), 46 encoders_(new EncoderState[num_channels_]),
47 interleave_buffer_(2 * num_channels_) { 47 interleave_buffer_(2 * num_channels_) {
48 RTC_CHECK(config.IsOk()); 48 RTC_CHECK(config.IsOk());
49 const size_t samples_per_channel = 49 const size_t samples_per_channel =
50 kSampleRateHz / 100 * num_10ms_frames_per_packet_; 50 kSampleRateHz / 100 * num_10ms_frames_per_packet_;
51 for (int i = 0; i < num_channels_; ++i) { 51 for (size_t i = 0; i < num_channels_; ++i) {
52 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]); 52 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]);
53 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2); 53 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2);
54 } 54 }
55 Reset(); 55 Reset();
56 } 56 }
57 57
58 AudioEncoderG722::AudioEncoderG722(const CodecInst& codec_inst) 58 AudioEncoderG722::AudioEncoderG722(const CodecInst& codec_inst)
59 : AudioEncoderG722(CreateConfig(codec_inst)) {} 59 : AudioEncoderG722(CreateConfig(codec_inst)) {}
60 60
61 AudioEncoderG722::~AudioEncoderG722() = default; 61 AudioEncoderG722::~AudioEncoderG722() = default;
62 62
63 size_t AudioEncoderG722::MaxEncodedBytes() const { 63 size_t AudioEncoderG722::MaxEncodedBytes() const {
64 return SamplesPerChannel() / 2 * num_channels_; 64 return SamplesPerChannel() / 2 * num_channels_;
65 } 65 }
66 66
67 int AudioEncoderG722::SampleRateHz() const { 67 int AudioEncoderG722::SampleRateHz() const {
68 return kSampleRateHz; 68 return kSampleRateHz;
69 } 69 }
70 70
71 int AudioEncoderG722::NumChannels() const { 71 size_t AudioEncoderG722::NumChannels() const {
72 return num_channels_; 72 return num_channels_;
73 } 73 }
74 74
75 int AudioEncoderG722::RtpTimestampRateHz() const { 75 int AudioEncoderG722::RtpTimestampRateHz() const {
76 // The RTP timestamp rate for G.722 is 8000 Hz, even though it is a 16 kHz 76 // The RTP timestamp rate for G.722 is 8000 Hz, even though it is a 16 kHz
77 // codec. 77 // codec.
78 return kSampleRateHz / 2; 78 return kSampleRateHz / 2;
79 } 79 }
80 80
81 size_t AudioEncoderG722::Num10MsFramesInNextPacket() const { 81 size_t AudioEncoderG722::Num10MsFramesInNextPacket() const {
82 return num_10ms_frames_per_packet_; 82 return num_10ms_frames_per_packet_;
83 } 83 }
84 84
85 size_t AudioEncoderG722::Max10MsFramesInAPacket() const { 85 size_t AudioEncoderG722::Max10MsFramesInAPacket() const {
86 return num_10ms_frames_per_packet_; 86 return num_10ms_frames_per_packet_;
87 } 87 }
88 88
89 int AudioEncoderG722::GetTargetBitrate() const { 89 int AudioEncoderG722::GetTargetBitrate() const {
90 // 4 bits/sample, 16000 samples/s/channel. 90 // 4 bits/sample, 16000 samples/s/channel.
91 return 64000 * NumChannels(); 91 return static_cast<int>(64000 * NumChannels());
92 } 92 }
93 93
94 AudioEncoder::EncodedInfo AudioEncoderG722::EncodeInternal( 94 AudioEncoder::EncodedInfo AudioEncoderG722::EncodeInternal(
95 uint32_t rtp_timestamp, 95 uint32_t rtp_timestamp,
96 rtc::ArrayView<const int16_t> audio, 96 rtc::ArrayView<const int16_t> audio,
97 size_t max_encoded_bytes, 97 size_t max_encoded_bytes,
98 uint8_t* encoded) { 98 uint8_t* encoded) {
99 RTC_CHECK_GE(max_encoded_bytes, MaxEncodedBytes()); 99 RTC_CHECK_GE(max_encoded_bytes, MaxEncodedBytes());
100 100
101 if (num_10ms_frames_buffered_ == 0) 101 if (num_10ms_frames_buffered_ == 0)
102 first_timestamp_in_buffer_ = rtp_timestamp; 102 first_timestamp_in_buffer_ = rtp_timestamp;
103 103
104 // Deinterleave samples and save them in each channel's buffer. 104 // Deinterleave samples and save them in each channel's buffer.
105 const size_t start = kSampleRateHz / 100 * num_10ms_frames_buffered_; 105 const size_t start = kSampleRateHz / 100 * num_10ms_frames_buffered_;
106 for (size_t i = 0; i < kSampleRateHz / 100; ++i) 106 for (size_t i = 0; i < kSampleRateHz / 100; ++i)
107 for (int j = 0; j < num_channels_; ++j) 107 for (size_t j = 0; j < num_channels_; ++j)
108 encoders_[j].speech_buffer[start + i] = audio[i * num_channels_ + j]; 108 encoders_[j].speech_buffer[start + i] = audio[i * num_channels_ + j];
109 109
110 // If we don't yet have enough samples for a packet, we're done for now. 110 // If we don't yet have enough samples for a packet, we're done for now.
111 if (++num_10ms_frames_buffered_ < num_10ms_frames_per_packet_) { 111 if (++num_10ms_frames_buffered_ < num_10ms_frames_per_packet_) {
112 return EncodedInfo(); 112 return EncodedInfo();
113 } 113 }
114 114
115 // Encode each channel separately. 115 // Encode each channel separately.
116 RTC_CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_); 116 RTC_CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_);
117 num_10ms_frames_buffered_ = 0; 117 num_10ms_frames_buffered_ = 0;
118 const size_t samples_per_channel = SamplesPerChannel(); 118 const size_t samples_per_channel = SamplesPerChannel();
119 for (int i = 0; i < num_channels_; ++i) { 119 for (size_t i = 0; i < num_channels_; ++i) {
120 const size_t encoded = WebRtcG722_Encode( 120 const size_t encoded = WebRtcG722_Encode(
121 encoders_[i].encoder, encoders_[i].speech_buffer.get(), 121 encoders_[i].encoder, encoders_[i].speech_buffer.get(),
122 samples_per_channel, encoders_[i].encoded_buffer.data()); 122 samples_per_channel, encoders_[i].encoded_buffer.data());
123 RTC_CHECK_EQ(encoded, samples_per_channel / 2); 123 RTC_CHECK_EQ(encoded, samples_per_channel / 2);
124 } 124 }
125 125
126 // Interleave the encoded bytes of the different channels. Each separate 126 // Interleave the encoded bytes of the different channels. Each separate
127 // channel and the interleaved stream encodes two samples per byte, most 127 // channel and the interleaved stream encodes two samples per byte, most
128 // significant half first. 128 // significant half first.
129 for (size_t i = 0; i < samples_per_channel / 2; ++i) { 129 for (size_t i = 0; i < samples_per_channel / 2; ++i) {
130 for (int j = 0; j < num_channels_; ++j) { 130 for (size_t j = 0; j < num_channels_; ++j) {
131 uint8_t two_samples = encoders_[j].encoded_buffer.data()[i]; 131 uint8_t two_samples = encoders_[j].encoded_buffer.data()[i];
132 interleave_buffer_.data()[j] = two_samples >> 4; 132 interleave_buffer_.data()[j] = two_samples >> 4;
133 interleave_buffer_.data()[num_channels_ + j] = two_samples & 0xf; 133 interleave_buffer_.data()[num_channels_ + j] = two_samples & 0xf;
134 } 134 }
135 for (int j = 0; j < num_channels_; ++j) 135 for (size_t j = 0; j < num_channels_; ++j)
136 encoded[i * num_channels_ + j] = interleave_buffer_.data()[2 * j] << 4 | 136 encoded[i * num_channels_ + j] = interleave_buffer_.data()[2 * j] << 4 |
137 interleave_buffer_.data()[2 * j + 1]; 137 interleave_buffer_.data()[2 * j + 1];
138 } 138 }
139 EncodedInfo info; 139 EncodedInfo info;
140 info.encoded_bytes = samples_per_channel / 2 * num_channels_; 140 info.encoded_bytes = samples_per_channel / 2 * num_channels_;
141 info.encoded_timestamp = first_timestamp_in_buffer_; 141 info.encoded_timestamp = first_timestamp_in_buffer_;
142 info.payload_type = payload_type_; 142 info.payload_type = payload_type_;
143 return info; 143 return info;
144 } 144 }
145 145
146 void AudioEncoderG722::Reset() { 146 void AudioEncoderG722::Reset() {
147 num_10ms_frames_buffered_ = 0; 147 num_10ms_frames_buffered_ = 0;
148 for (int i = 0; i < num_channels_; ++i) 148 for (size_t i = 0; i < num_channels_; ++i)
149 RTC_CHECK_EQ(0, WebRtcG722_EncoderInit(encoders_[i].encoder)); 149 RTC_CHECK_EQ(0, WebRtcG722_EncoderInit(encoders_[i].encoder));
150 } 150 }
151 151
152 AudioEncoderG722::EncoderState::EncoderState() { 152 AudioEncoderG722::EncoderState::EncoderState() {
153 RTC_CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder)); 153 RTC_CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder));
154 } 154 }
155 155
156 AudioEncoderG722::EncoderState::~EncoderState() { 156 AudioEncoderG722::EncoderState::~EncoderState() {
157 RTC_CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder)); 157 RTC_CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder));
158 } 158 }
159 159
160 size_t AudioEncoderG722::SamplesPerChannel() const { 160 size_t AudioEncoderG722::SamplesPerChannel() const {
161 return kSampleRateHz / 100 * num_10ms_frames_per_packet_; 161 return kSampleRateHz / 100 * num_10ms_frames_per_packet_;
162 } 162 }
163 163
164 } // namespace webrtc 164 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698