Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / codecs / opus / audio_encoder_opus.cc
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/opus/interface/audio_encoder_opus.h"
12
13 #include "webrtc/modules/audio_coding/codecs/opus/interface/opus_interface.h"
14
15 namespace webrtc {
16
17 namespace {
18
19 // We always encode at 48 kHz.
20 const int kSampleRateHz = 48000;
21
22 int DivExact(int a, int b) {
23   CHECK_EQ(a % b, 0);
24   return a / b;
25 }
26
27 int16_t ClampInt16(size_t x) {
28   return static_cast<int16_t>(
29       std::min(x, static_cast<size_t>(std::numeric_limits<int16_t>::max())));
30 }
31
32 int16_t CastInt16(size_t x) {
33   DCHECK_LE(x, static_cast<size_t>(std::numeric_limits<int16_t>::max()));
34   return static_cast<int16_t>(x);
35 }
36
37 }  // namespace
38
39 AudioEncoderOpus::Config::Config() : frame_size_ms(20), num_channels(1) {}
40
41 bool AudioEncoderOpus::Config::IsOk() const {
42   if (frame_size_ms <= 0 || frame_size_ms % 10 != 0)
43     return false;
44   if (num_channels <= 0)
45     return false;
46   return true;
47 }
48
49 AudioEncoderOpus::AudioEncoderOpus(const Config& config)
50     : num_10ms_frames_per_packet_(DivExact(config.frame_size_ms, 10)),
51       num_channels_(config.num_channels),
52       samples_per_10ms_frame_(DivExact(kSampleRateHz, 100) * num_channels_) {
53   CHECK(config.IsOk());
54   input_buffer_.reserve(num_10ms_frames_per_packet_ * samples_per_10ms_frame_);
55   CHECK_EQ(0, WebRtcOpus_EncoderCreate(&inst_, num_channels_));
56 }
57
58 AudioEncoderOpus::~AudioEncoderOpus() {
59   CHECK_EQ(0, WebRtcOpus_EncoderFree(inst_));
60 }
61
62 int AudioEncoderOpus::sample_rate_hz() const {
63   return kSampleRateHz;
64 }
65
66 int AudioEncoderOpus::num_channels() const {
67   return num_channels_;
68 }
69
70 int AudioEncoderOpus::Num10MsFramesInNextPacket() const {
71   return num_10ms_frames_per_packet_;
72 }
73
74 bool AudioEncoderOpus::Encode(uint32_t timestamp,
75                               const int16_t* audio,
76                               size_t max_encoded_bytes,
77                               uint8_t* encoded,
78                               size_t* encoded_bytes,
79                               uint32_t* encoded_timestamp) {
80   if (input_buffer_.empty())
81     first_timestamp_in_buffer_ = timestamp;
82   input_buffer_.insert(input_buffer_.end(), audio,
83                        audio + samples_per_10ms_frame_);
84   if (input_buffer_.size() < (static_cast<size_t>(num_10ms_frames_per_packet_) *
85                               samples_per_10ms_frame_)) {
86     *encoded_bytes = 0;
87     return true;
88   }
89   CHECK_EQ(input_buffer_.size(),
90            static_cast<size_t>(num_10ms_frames_per_packet_) *
91            samples_per_10ms_frame_);
92   int16_t r = WebRtcOpus_Encode(
93       inst_, &input_buffer_[0],
94       DivExact(CastInt16(input_buffer_.size()), num_channels_),
95       ClampInt16(max_encoded_bytes), encoded);
96   input_buffer_.clear();
97   if (r < 0)
98     return false;
99   *encoded_bytes = r;
100   *encoded_timestamp = first_timestamp_in_buffer_;
101   return true;
102 }
103
104 }  // namespace webrtc