| 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 int kSampleRateHz = 16000; | 22 const size_t kSampleRateHz = 16000; |
| 23 | 23 |
| 24 } // namespace | 24 } // namespace |
| 25 | 25 |
| 26 bool AudioEncoderG722::Config::IsOk() const { | 26 bool AudioEncoderG722::Config::IsOk() const { |
| 27 return (frame_size_ms > 0) && (frame_size_ms % 10 == 0) && | 27 return (frame_size_ms > 0) && (frame_size_ms % 10 == 0) && |
| 28 (num_channels >= 1); | 28 (num_channels >= 1); |
| 29 } | 29 } |
| 30 | 30 |
| 31 AudioEncoderG722::EncoderState::EncoderState() { | 31 AudioEncoderG722::EncoderState::EncoderState() { |
| 32 CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder)); | 32 CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder)); |
| 33 CHECK_EQ(0, WebRtcG722_EncoderInit(encoder)); | 33 CHECK_EQ(0, WebRtcG722_EncoderInit(encoder)); |
| 34 } | 34 } |
| 35 | 35 |
| 36 AudioEncoderG722::EncoderState::~EncoderState() { | 36 AudioEncoderG722::EncoderState::~EncoderState() { |
| 37 CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder)); | 37 CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder)); |
| 38 } | 38 } |
| 39 | 39 |
| 40 AudioEncoderG722::AudioEncoderG722(const Config& config) | 40 AudioEncoderG722::AudioEncoderG722(const Config& config) |
| 41 : num_channels_(config.num_channels), | 41 : num_channels_(config.num_channels), |
| 42 payload_type_(config.payload_type), | 42 payload_type_(config.payload_type), |
| 43 num_10ms_frames_per_packet_(config.frame_size_ms / 10), | 43 num_10ms_frames_per_packet_( |
| 44 static_cast<size_t>(config.frame_size_ms / 10)), |
| 44 num_10ms_frames_buffered_(0), | 45 num_10ms_frames_buffered_(0), |
| 45 first_timestamp_in_buffer_(0), | 46 first_timestamp_in_buffer_(0), |
| 46 encoders_(new EncoderState[num_channels_]), | 47 encoders_(new EncoderState[num_channels_]), |
| 47 interleave_buffer_(2 * num_channels_) { | 48 interleave_buffer_(2 * num_channels_) { |
| 48 CHECK(config.IsOk()); | 49 CHECK(config.IsOk()); |
| 49 const int samples_per_channel = | 50 const size_t samples_per_channel = |
| 50 kSampleRateHz / 100 * num_10ms_frames_per_packet_; | 51 kSampleRateHz / 100 * num_10ms_frames_per_packet_; |
| 51 for (int i = 0; i < num_channels_; ++i) { | 52 for (int i = 0; i < num_channels_; ++i) { |
| 52 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]); | 53 encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]); |
| 53 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2); | 54 encoders_[i].encoded_buffer.SetSize(samples_per_channel / 2); |
| 54 } | 55 } |
| 55 } | 56 } |
| 56 | 57 |
| 57 AudioEncoderG722::~AudioEncoderG722() {} | 58 AudioEncoderG722::~AudioEncoderG722() {} |
| 58 | 59 |
| 59 int AudioEncoderG722::SampleRateHz() const { | 60 int AudioEncoderG722::SampleRateHz() const { |
| 60 return kSampleRateHz; | 61 return kSampleRateHz; |
| 61 } | 62 } |
| 62 | 63 |
| 63 int AudioEncoderG722::RtpTimestampRateHz() const { | 64 int AudioEncoderG722::RtpTimestampRateHz() const { |
| 64 // The RTP timestamp rate for G.722 is 8000 Hz, even though it is a 16 kHz | 65 // The RTP timestamp rate for G.722 is 8000 Hz, even though it is a 16 kHz |
| 65 // codec. | 66 // codec. |
| 66 return kSampleRateHz / 2; | 67 return kSampleRateHz / 2; |
| 67 } | 68 } |
| 68 | 69 |
| 69 int AudioEncoderG722::NumChannels() const { | 70 int AudioEncoderG722::NumChannels() const { |
| 70 return num_channels_; | 71 return num_channels_; |
| 71 } | 72 } |
| 72 | 73 |
| 73 size_t AudioEncoderG722::MaxEncodedBytes() const { | 74 size_t AudioEncoderG722::MaxEncodedBytes() const { |
| 74 return static_cast<size_t>(SamplesPerChannel() / 2 * num_channels_); | 75 return SamplesPerChannel() / 2 * num_channels_; |
| 75 } | 76 } |
| 76 | 77 |
| 77 int AudioEncoderG722::Num10MsFramesInNextPacket() const { | 78 size_t AudioEncoderG722::Num10MsFramesInNextPacket() const { |
| 78 return num_10ms_frames_per_packet_; | 79 return num_10ms_frames_per_packet_; |
| 79 } | 80 } |
| 80 | 81 |
| 81 int AudioEncoderG722::Max10MsFramesInAPacket() const { | 82 size_t AudioEncoderG722::Max10MsFramesInAPacket() const { |
| 82 return num_10ms_frames_per_packet_; | 83 return num_10ms_frames_per_packet_; |
| 83 } | 84 } |
| 84 | 85 |
| 85 int AudioEncoderG722::GetTargetBitrate() const { | 86 int AudioEncoderG722::GetTargetBitrate() const { |
| 86 // 4 bits/sample, 16000 samples/s/channel. | 87 // 4 bits/sample, 16000 samples/s/channel. |
| 87 return 64000 * NumChannels(); | 88 return 64000 * NumChannels(); |
| 88 } | 89 } |
| 89 | 90 |
| 90 AudioEncoder::EncodedInfo AudioEncoderG722::EncodeInternal( | 91 AudioEncoder::EncodedInfo AudioEncoderG722::EncodeInternal( |
| 91 uint32_t rtp_timestamp, | 92 uint32_t rtp_timestamp, |
| 92 const int16_t* audio, | 93 const int16_t* audio, |
| 93 size_t max_encoded_bytes, | 94 size_t max_encoded_bytes, |
| 94 uint8_t* encoded) { | 95 uint8_t* encoded) { |
| 95 CHECK_GE(max_encoded_bytes, MaxEncodedBytes()); | 96 CHECK_GE(max_encoded_bytes, MaxEncodedBytes()); |
| 96 | 97 |
| 97 if (num_10ms_frames_buffered_ == 0) | 98 if (num_10ms_frames_buffered_ == 0) |
| 98 first_timestamp_in_buffer_ = rtp_timestamp; | 99 first_timestamp_in_buffer_ = rtp_timestamp; |
| 99 | 100 |
| 100 // Deinterleave samples and save them in each channel's buffer. | 101 // Deinterleave samples and save them in each channel's buffer. |
| 101 const int start = kSampleRateHz / 100 * num_10ms_frames_buffered_; | 102 const size_t start = kSampleRateHz / 100 * num_10ms_frames_buffered_; |
| 102 for (int i = 0; i < kSampleRateHz / 100; ++i) | 103 for (size_t i = 0; i < kSampleRateHz / 100; ++i) |
| 103 for (int j = 0; j < num_channels_; ++j) | 104 for (int j = 0; j < num_channels_; ++j) |
| 104 encoders_[j].speech_buffer[start + i] = audio[i * num_channels_ + j]; | 105 encoders_[j].speech_buffer[start + i] = audio[i * num_channels_ + j]; |
| 105 | 106 |
| 106 // If we don't yet have enough samples for a packet, we're done for now. | 107 // If we don't yet have enough samples for a packet, we're done for now. |
| 107 if (++num_10ms_frames_buffered_ < num_10ms_frames_per_packet_) { | 108 if (++num_10ms_frames_buffered_ < num_10ms_frames_per_packet_) { |
| 108 return EncodedInfo(); | 109 return EncodedInfo(); |
| 109 } | 110 } |
| 110 | 111 |
| 111 // Encode each channel separately. | 112 // Encode each channel separately. |
| 112 CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_); | 113 CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_); |
| 113 num_10ms_frames_buffered_ = 0; | 114 num_10ms_frames_buffered_ = 0; |
| 114 const int samples_per_channel = SamplesPerChannel(); | 115 const size_t samples_per_channel = SamplesPerChannel(); |
| 115 for (int i = 0; i < num_channels_; ++i) { | 116 for (int i = 0; i < num_channels_; ++i) { |
| 116 const int encoded = WebRtcG722_Encode( | 117 const size_t encoded = WebRtcG722_Encode( |
| 117 encoders_[i].encoder, encoders_[i].speech_buffer.get(), | 118 encoders_[i].encoder, encoders_[i].speech_buffer.get(), |
| 118 samples_per_channel, encoders_[i].encoded_buffer.data<uint8_t>()); | 119 samples_per_channel, encoders_[i].encoded_buffer.data<uint8_t>()); |
| 119 CHECK_GE(encoded, 0); | |
| 120 CHECK_EQ(encoded, samples_per_channel / 2); | 120 CHECK_EQ(encoded, samples_per_channel / 2); |
| 121 } | 121 } |
| 122 | 122 |
| 123 // Interleave the encoded bytes of the different channels. Each separate | 123 // Interleave the encoded bytes of the different channels. Each separate |
| 124 // channel and the interleaved stream encodes two samples per byte, most | 124 // channel and the interleaved stream encodes two samples per byte, most |
| 125 // significant half first. | 125 // significant half first. |
| 126 for (int i = 0; i < samples_per_channel / 2; ++i) { | 126 for (size_t i = 0; i < samples_per_channel / 2; ++i) { |
| 127 for (int j = 0; j < num_channels_; ++j) { | 127 for (int j = 0; j < num_channels_; ++j) { |
| 128 uint8_t two_samples = encoders_[j].encoded_buffer.data()[i]; | 128 uint8_t two_samples = encoders_[j].encoded_buffer.data()[i]; |
| 129 interleave_buffer_.data()[j] = two_samples >> 4; | 129 interleave_buffer_.data()[j] = two_samples >> 4; |
| 130 interleave_buffer_.data()[num_channels_ + j] = two_samples & 0xf; | 130 interleave_buffer_.data()[num_channels_ + j] = two_samples & 0xf; |
| 131 } | 131 } |
| 132 for (int j = 0; j < num_channels_; ++j) | 132 for (int j = 0; j < num_channels_; ++j) |
| 133 encoded[i * num_channels_ + j] = interleave_buffer_.data()[2 * j] << 4 | | 133 encoded[i * num_channels_ + j] = interleave_buffer_.data()[2 * j] << 4 | |
| 134 interleave_buffer_.data()[2 * j + 1]; | 134 interleave_buffer_.data()[2 * j + 1]; |
| 135 } | 135 } |
| 136 EncodedInfo info; | 136 EncodedInfo info; |
| 137 info.encoded_bytes = samples_per_channel / 2 * num_channels_; | 137 info.encoded_bytes = samples_per_channel / 2 * num_channels_; |
| 138 info.encoded_timestamp = first_timestamp_in_buffer_; | 138 info.encoded_timestamp = first_timestamp_in_buffer_; |
| 139 info.payload_type = payload_type_; | 139 info.payload_type = payload_type_; |
| 140 return info; | 140 return info; |
| 141 } | 141 } |
| 142 | 142 |
| 143 int AudioEncoderG722::SamplesPerChannel() const { | 143 size_t AudioEncoderG722::SamplesPerChannel() const { |
| 144 return kSampleRateHz / 100 * num_10ms_frames_per_packet_; | 144 return kSampleRateHz / 100 * num_10ms_frames_per_packet_; |
| 145 } | 145 } |
| 146 | 146 |
| 147 namespace { | 147 namespace { |
| 148 AudioEncoderG722::Config CreateConfig(const CodecInst& codec_inst) { | 148 AudioEncoderG722::Config CreateConfig(const CodecInst& codec_inst) { |
| 149 AudioEncoderG722::Config config; | 149 AudioEncoderG722::Config config; |
| 150 config.num_channels = codec_inst.channels; | 150 config.num_channels = codec_inst.channels; |
| 151 config.frame_size_ms = codec_inst.pacsize / 16; | 151 config.frame_size_ms = codec_inst.pacsize / 16; |
| 152 config.payload_type = codec_inst.pltype; | 152 config.payload_type = codec_inst.pltype; |
| 153 return config; | 153 return config; |
| 154 } | 154 } |
| 155 } // namespace | 155 } // namespace |
| 156 | 156 |
| 157 AudioEncoderMutableG722::AudioEncoderMutableG722(const CodecInst& codec_inst) | 157 AudioEncoderMutableG722::AudioEncoderMutableG722(const CodecInst& codec_inst) |
| 158 : AudioEncoderMutableImpl<AudioEncoderG722>(CreateConfig(codec_inst)) { | 158 : AudioEncoderMutableImpl<AudioEncoderG722>(CreateConfig(codec_inst)) { |
| 159 } | 159 } |
| 160 | 160 |
| 161 } // namespace webrtc | 161 } // namespace webrtc |
| OLD | NEW |