audio_encoder_g722.cc revision 51ccf376387266225cd8c78e63238b725860f0af
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 "webrtc/modules/audio_coding/codecs/g722/include/audio_encoder_g722.h"
12
13#include <limits>
14#include "webrtc/base/checks.h"
15#include "webrtc/modules/audio_coding/codecs/g722/include/g722_interface.h"
16
17namespace webrtc {
18
19namespace {
20
21const int kSampleRateHz = 16000;
22
23}  // namespace
24
25AudioEncoderG722::EncoderState::EncoderState() {
26  CHECK_EQ(0, WebRtcG722_CreateEncoder(&encoder));
27  CHECK_EQ(0, WebRtcG722_EncoderInit(encoder));
28}
29
30AudioEncoderG722::EncoderState::~EncoderState() {
31  CHECK_EQ(0, WebRtcG722_FreeEncoder(encoder));
32}
33
34AudioEncoderG722::AudioEncoderG722(const Config& config)
35    : num_channels_(config.num_channels),
36      payload_type_(config.payload_type),
37      num_10ms_frames_per_packet_(config.frame_size_ms / 10),
38      num_10ms_frames_buffered_(0),
39      first_timestamp_in_buffer_(0),
40      encoders_(new EncoderState[num_channels_]),
41      interleave_buffer_(new uint8_t[2 * num_channels_]) {
42  CHECK_EQ(config.frame_size_ms % 10, 0)
43      << "Frame size must be an integer multiple of 10 ms.";
44  const int samples_per_channel =
45      kSampleRateHz / 100 * num_10ms_frames_per_packet_;
46  for (int i = 0; i < num_channels_; ++i) {
47    encoders_[i].speech_buffer.reset(new int16_t[samples_per_channel]);
48    encoders_[i].encoded_buffer.reset(new uint8_t[samples_per_channel / 2]);
49  }
50}
51
52AudioEncoderG722::~AudioEncoderG722() {}
53
54int AudioEncoderG722::SampleRateHz() const {
55  return kSampleRateHz;
56}
57
58int AudioEncoderG722::RtpTimestampRateHz() const {
59  // The RTP timestamp rate for G.722 is 8000 Hz, even though it is a 16 kHz
60  // codec.
61  return kSampleRateHz / 2;
62}
63
64int AudioEncoderG722::NumChannels() const {
65  return num_channels_;
66}
67
68size_t AudioEncoderG722::MaxEncodedBytes() const {
69  return static_cast<size_t>(SamplesPerChannel() / 2 * num_channels_);
70}
71
72int AudioEncoderG722::Num10MsFramesInNextPacket() const {
73  return num_10ms_frames_per_packet_;
74}
75
76int AudioEncoderG722::Max10MsFramesInAPacket() const {
77  return num_10ms_frames_per_packet_;
78}
79
80void AudioEncoderG722::EncodeInternal(uint32_t rtp_timestamp,
81                                      const int16_t* audio,
82                                      size_t max_encoded_bytes,
83                                      uint8_t* encoded,
84                                      EncodedInfo* info) {
85  CHECK_GE(max_encoded_bytes, MaxEncodedBytes());
86
87  if (num_10ms_frames_buffered_ == 0)
88    first_timestamp_in_buffer_ = rtp_timestamp;
89
90  // Deinterleave samples and save them in each channel's buffer.
91  const int start = kSampleRateHz / 100 * num_10ms_frames_buffered_;
92  for (int i = 0; i < kSampleRateHz / 100; ++i)
93    for (int j = 0; j < num_channels_; ++j)
94      encoders_[j].speech_buffer[start + i] = audio[i * num_channels_ + j];
95
96  // If we don't yet have enough samples for a packet, we're done for now.
97  if (++num_10ms_frames_buffered_ < num_10ms_frames_per_packet_) {
98    info->encoded_bytes = 0;
99    return;
100  }
101
102  // Encode each channel separately.
103  CHECK_EQ(num_10ms_frames_buffered_, num_10ms_frames_per_packet_);
104  num_10ms_frames_buffered_ = 0;
105  const int samples_per_channel = SamplesPerChannel();
106  for (int i = 0; i < num_channels_; ++i) {
107    const int encoded = WebRtcG722_Encode(
108        encoders_[i].encoder, encoders_[i].speech_buffer.get(),
109        samples_per_channel, encoders_[i].encoded_buffer.get());
110    CHECK_GE(encoded, 0);
111    CHECK_EQ(encoded, samples_per_channel / 2);
112  }
113
114  // Interleave the encoded bytes of the different channels. Each separate
115  // channel and the interleaved stream encodes two samples per byte, most
116  // significant half first.
117  for (int i = 0; i < samples_per_channel / 2; ++i) {
118    for (int j = 0; j < num_channels_; ++j) {
119      uint8_t two_samples = encoders_[j].encoded_buffer[i];
120      interleave_buffer_[j] = two_samples >> 4;
121      interleave_buffer_[num_channels_ + j] = two_samples & 0xf;
122    }
123    for (int j = 0; j < num_channels_; ++j)
124      encoded[i * num_channels_ + j] =
125          interleave_buffer_[2 * j] << 4 | interleave_buffer_[2 * j + 1];
126  }
127  info->encoded_bytes = samples_per_channel / 2 * num_channels_;
128  info->encoded_timestamp = first_timestamp_in_buffer_;
129  info->payload_type = payload_type_;
130}
131
132int AudioEncoderG722::SamplesPerChannel() const {
133  return kSampleRateHz / 100 * num_10ms_frames_per_packet_;
134}
135
136}  // namespace webrtc
137