OLD | NEW |
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/g722/include/audio_encoder_g722.h" | 11 #include "webrtc/modules/audio_coding/codecs/g722/include/audio_encoder_g722.h" |
12 | 12 |
13 #include <limits> | 13 #include <limits> |
14 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
15 #include "webrtc/common_types.h" | 15 #include "webrtc/common_types.h" |
16 #include "webrtc/modules/audio_coding/codecs/g722/include/g722_interface.h" | 16 #include "webrtc/modules/audio_coding/codecs/g722/include/g722_interface.h" |
17 | 17 |
18 namespace webrtc { | 18 namespace webrtc { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 const size_t kSampleRateHz = 16000; | 22 const size_t kSampleRateHz = 16000; |
23 | 23 |
| 24 AudioEncoderG722::Config CreateConfig(const CodecInst& codec_inst) { |
| 25 AudioEncoderG722::Config config; |
| 26 config.num_channels = codec_inst.channels; |
| 27 config.frame_size_ms = codec_inst.pacsize / 16; |
| 28 config.payload_type = codec_inst.pltype; |
| 29 return config; |
| 30 } |
| 31 |
24 } // namespace | 32 } // namespace |
25 | 33 |
26 bool AudioEncoderG722::Config::IsOk() const { | 34 bool AudioEncoderG722::Config::IsOk() const { |
27 return (frame_size_ms > 0) && (frame_size_ms % 10 == 0) && | 35 return (frame_size_ms > 0) && (frame_size_ms % 10 == 0) && |
28 (num_channels >= 1); | 36 (num_channels >= 1); |
29 } | 37 } |
30 | 38 |
31 AudioEncoderG722::EncoderState::EncoderState() { | |
32 CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder)); | |
33 CHECK_EQ(0, WebRtcG722_EncoderInit(encoder)); | |
34 } | |
35 | |
36 AudioEncoderG722::EncoderState::~EncoderState() { | |
37 CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder)); | |
38 } | |
39 | |
40 AudioEncoderG722::AudioEncoderG722(const Config& config) | 39 AudioEncoderG722::AudioEncoderG722(const Config& config) |
41 : num_channels_(config.num_channels), | 40 : num_channels_(config.num_channels), |
42 payload_type_(config.payload_type), | 41 payload_type_(config.payload_type), |
43 num_10ms_frames_per_packet_( | 42 num_10ms_frames_per_packet_( |
44 static_cast<size_t>(config.frame_size_ms / 10)), | 43 static_cast<size_t>(config.frame_size_ms / 10)), |
45 num_10ms_frames_buffered_(0), | 44 num_10ms_frames_buffered_(0), |
46 first_timestamp_in_buffer_(0), | 45 first_timestamp_in_buffer_(0), |
47 encoders_(new EncoderState[num_channels_]), | 46 encoders_(new EncoderState[num_channels_]), |
48 interleave_buffer_(2 * num_channels_) { | 47 interleave_buffer_(2 * num_channels_) { |
49 CHECK(config.IsOk()); | 48 CHECK(config.IsOk()); |
50 const size_t samples_per_channel = | 49 const size_t samples_per_channel = |
51 kSampleRateHz / 100 * num_10ms_frames_per_packet_; | 50 kSampleRateHz / 100 * num_10ms_frames_per_packet_; |
52 for (int i = 0; i < num_channels_; ++i) { | 51 for (int i = 0; i < num_channels_; ++i) { |
53 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]); | 52 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]); |
54 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2); | 53 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2); |
55 } | 54 } |
| 55 Reset(); |
56 } | 56 } |
57 | 57 |
58 AudioEncoderG722::~AudioEncoderG722() {} | 58 AudioEncoderG722::AudioEncoderG722(const CodecInst& codec_inst) |
| 59 : AudioEncoderG722(CreateConfig(codec_inst)) {} |
| 60 |
| 61 AudioEncoderG722::~AudioEncoderG722() = default; |
| 62 |
| 63 size_t AudioEncoderG722::MaxEncodedBytes() const { |
| 64 return SamplesPerChannel() / 2 * num_channels_; |
| 65 } |
59 | 66 |
60 int AudioEncoderG722::SampleRateHz() const { | 67 int AudioEncoderG722::SampleRateHz() const { |
61 return kSampleRateHz; | 68 return kSampleRateHz; |
62 } | 69 } |
63 | 70 |
| 71 int AudioEncoderG722::NumChannels() const { |
| 72 return num_channels_; |
| 73 } |
| 74 |
64 int AudioEncoderG722::RtpTimestampRateHz() const { | 75 int AudioEncoderG722::RtpTimestampRateHz() const { |
65 // 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 |
66 // codec. | 77 // codec. |
67 return kSampleRateHz / 2; | 78 return kSampleRateHz / 2; |
68 } | 79 } |
69 | 80 |
70 int AudioEncoderG722::NumChannels() const { | |
71 return num_channels_; | |
72 } | |
73 | |
74 size_t AudioEncoderG722::MaxEncodedBytes() const { | |
75 return SamplesPerChannel() / 2 * num_channels_; | |
76 } | |
77 | |
78 size_t AudioEncoderG722::Num10MsFramesInNextPacket() const { | 81 size_t AudioEncoderG722::Num10MsFramesInNextPacket() const { |
79 return num_10ms_frames_per_packet_; | 82 return num_10ms_frames_per_packet_; |
80 } | 83 } |
81 | 84 |
82 size_t AudioEncoderG722::Max10MsFramesInAPacket() const { | 85 size_t AudioEncoderG722::Max10MsFramesInAPacket() const { |
83 return num_10ms_frames_per_packet_; | 86 return num_10ms_frames_per_packet_; |
84 } | 87 } |
85 | 88 |
86 int AudioEncoderG722::GetTargetBitrate() const { | 89 int AudioEncoderG722::GetTargetBitrate() const { |
87 // 4 bits/sample, 16000 samples/s/channel. | 90 // 4 bits/sample, 16000 samples/s/channel. |
(...skipping 21 matching lines...) Expand all Loading... |
109 return EncodedInfo(); | 112 return EncodedInfo(); |
110 } | 113 } |
111 | 114 |
112 // Encode each channel separately. | 115 // Encode each channel separately. |
113 CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_); | 116 CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_); |
114 num_10ms_frames_buffered_ = 0; | 117 num_10ms_frames_buffered_ = 0; |
115 const size_t samples_per_channel = SamplesPerChannel(); | 118 const size_t samples_per_channel = SamplesPerChannel(); |
116 for (int i = 0; i < num_channels_; ++i) { | 119 for (int i = 0; i < num_channels_; ++i) { |
117 const size_t encoded = WebRtcG722_Encode( | 120 const size_t encoded = WebRtcG722_Encode( |
118 encoders_[i].encoder, encoders_[i].speech_buffer.get(), | 121 encoders_[i].encoder, encoders_[i].speech_buffer.get(), |
119 samples_per_channel, encoders_[i].encoded_buffer.data<uint8_t>()); | 122 samples_per_channel, encoders_[i].encoded_buffer.data()); |
120 CHECK_EQ(encoded, samples_per_channel / 2); | 123 CHECK_EQ(encoded, samples_per_channel / 2); |
121 } | 124 } |
122 | 125 |
123 // Interleave the encoded bytes of the different channels. Each separate | 126 // Interleave the encoded bytes of the different channels. Each separate |
124 // channel and the interleaved stream encodes two samples per byte, most | 127 // channel and the interleaved stream encodes two samples per byte, most |
125 // significant half first. | 128 // significant half first. |
126 for (size_t i = 0; i < samples_per_channel / 2; ++i) { | 129 for (size_t i = 0; i < samples_per_channel / 2; ++i) { |
127 for (int j = 0; j < num_channels_; ++j) { | 130 for (int j = 0; j < num_channels_; ++j) { |
128 uint8_t two_samples = encoders_[j].encoded_buffer.data()[i]; | 131 uint8_t two_samples = encoders_[j].encoded_buffer.data()[i]; |
129 interleave_buffer_.data()[j] = two_samples >> 4; | 132 interleave_buffer_.data()[j] = two_samples >> 4; |
130 interleave_buffer_.data()[num_channels_ + j] = two_samples & 0xf; | 133 interleave_buffer_.data()[num_channels_ + j] = two_samples & 0xf; |
131 } | 134 } |
132 for (int j = 0; j < num_channels_; ++j) | 135 for (int j = 0; j < num_channels_; ++j) |
133 encoded[i * num_channels_ + j] = interleave_buffer_.data()[2 * j] << 4 | | 136 encoded[i * num_channels_ + j] = interleave_buffer_.data()[2 * j] << 4 | |
134 interleave_buffer_.data()[2 * j + 1]; | 137 interleave_buffer_.data()[2 * j + 1]; |
135 } | 138 } |
136 EncodedInfo info; | 139 EncodedInfo info; |
137 info.encoded_bytes = samples_per_channel / 2 * num_channels_; | 140 info.encoded_bytes = samples_per_channel / 2 * num_channels_; |
138 info.encoded_timestamp = first_timestamp_in_buffer_; | 141 info.encoded_timestamp = first_timestamp_in_buffer_; |
139 info.payload_type = payload_type_; | 142 info.payload_type = payload_type_; |
140 return info; | 143 return info; |
141 } | 144 } |
142 | 145 |
| 146 void AudioEncoderG722::Reset() { |
| 147 num_10ms_frames_buffered_ = 0; |
| 148 for (int i = 0; i < num_channels_; ++i) |
| 149 CHECK_EQ(0, WebRtcG722_EncoderInit(encoders_[i].encoder)); |
| 150 } |
| 151 |
| 152 AudioEncoderG722::EncoderState::EncoderState() { |
| 153 CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder)); |
| 154 } |
| 155 |
| 156 AudioEncoderG722::EncoderState::~EncoderState() { |
| 157 CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder)); |
| 158 } |
| 159 |
143 size_t AudioEncoderG722::SamplesPerChannel() const { | 160 size_t AudioEncoderG722::SamplesPerChannel() const { |
144 return kSampleRateHz / 100 * num_10ms_frames_per_packet_; | 161 return kSampleRateHz / 100 * num_10ms_frames_per_packet_; |
145 } | 162 } |
146 | 163 |
147 namespace { | |
148 AudioEncoderG722::Config CreateConfig(const CodecInst& codec_inst) { | |
149 AudioEncoderG722::Config config; | |
150 config.num_channels = codec_inst.channels; | |
151 config.frame_size_ms = codec_inst.pacsize / 16; | |
152 config.payload_type = codec_inst.pltype; | |
153 return config; | |
154 } | |
155 } // namespace | |
156 | |
157 AudioEncoderMutableG722::AudioEncoderMutableG722(const CodecInst& codec_inst) | |
158 : AudioEncoderMutableImpl<AudioEncoderG722>(CreateConfig(codec_inst)) { | |
159 } | |
160 | |
161 } // namespace webrtc | 164 } // namespace webrtc |
OLD | NEW |