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/ilbc/interface/audio_encoder_ilbc.h
" | 11 #include "webrtc/modules/audio_coding/codecs/ilbc/interface/audio_encoder_ilbc.h
" |
12 | 12 |
13 #include <cstring> | 13 #include <cstring> |
14 #include <limits> | 14 #include <limits> |
15 #include "webrtc/base/checks.h" | 15 #include "webrtc/base/checks.h" |
16 #include "webrtc/common_types.h" | 16 #include "webrtc/common_types.h" |
17 #include "webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h" | 17 #include "webrtc/modules/audio_coding/codecs/ilbc/interface/ilbc.h" |
18 | 18 |
19 namespace webrtc { | 19 namespace webrtc { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 const int kSampleRateHz = 8000; | 23 const int kSampleRateHz = 8000; |
24 | 24 |
| 25 AudioEncoderIlbc::Config CreateConfig(const CodecInst& codec_inst) { |
| 26 AudioEncoderIlbc::Config config; |
| 27 config.frame_size_ms = codec_inst.pacsize / 8; |
| 28 config.payload_type = codec_inst.pltype; |
| 29 return config; |
| 30 } |
| 31 |
25 } // namespace | 32 } // namespace |
26 | 33 |
27 // static | 34 // static |
28 const size_t AudioEncoderIlbc::kMaxSamplesPerPacket; | 35 const size_t AudioEncoderIlbc::kMaxSamplesPerPacket; |
29 | 36 |
30 bool AudioEncoderIlbc::Config::IsOk() const { | 37 bool AudioEncoderIlbc::Config::IsOk() const { |
31 return (frame_size_ms == 20 || frame_size_ms == 30 || frame_size_ms == 40 || | 38 return (frame_size_ms == 20 || frame_size_ms == 30 || frame_size_ms == 40 || |
32 frame_size_ms == 60) && | 39 frame_size_ms == 60) && |
33 static_cast<size_t>(kSampleRateHz / 100 * (frame_size_ms / 10)) <= | 40 static_cast<size_t>(kSampleRateHz / 100 * (frame_size_ms / 10)) <= |
34 kMaxSamplesPerPacket; | 41 kMaxSamplesPerPacket; |
35 } | 42 } |
36 | 43 |
37 AudioEncoderIlbc::AudioEncoderIlbc(const Config& config) | 44 AudioEncoderIlbc::AudioEncoderIlbc(const Config& config) |
38 : payload_type_(config.payload_type), | 45 : config_(config), |
39 num_10ms_frames_per_packet_( | 46 num_10ms_frames_per_packet_( |
40 static_cast<size_t>(config.frame_size_ms / 10)), | 47 static_cast<size_t>(config.frame_size_ms / 10)), |
41 num_10ms_frames_buffered_(0) { | 48 encoder_(nullptr) { |
42 CHECK(config.IsOk()); | 49 Reset(); |
43 CHECK_EQ(0, WebRtcIlbcfix_EncoderCreate(&encoder_)); | |
44 const int encoder_frame_size_ms = config.frame_size_ms > 30 | |
45 ? config.frame_size_ms / 2 | |
46 : config.frame_size_ms; | |
47 CHECK_EQ(0, WebRtcIlbcfix_EncoderInit(encoder_, encoder_frame_size_ms)); | |
48 } | 50 } |
49 | 51 |
| 52 AudioEncoderIlbc::AudioEncoderIlbc(const CodecInst& codec_inst) |
| 53 : AudioEncoderIlbc(CreateConfig(codec_inst)) {} |
| 54 |
50 AudioEncoderIlbc::~AudioEncoderIlbc() { | 55 AudioEncoderIlbc::~AudioEncoderIlbc() { |
51 CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_)); | 56 CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_)); |
52 } | 57 } |
53 | 58 |
| 59 size_t AudioEncoderIlbc::MaxEncodedBytes() const { |
| 60 return RequiredOutputSizeBytes(); |
| 61 } |
| 62 |
54 int AudioEncoderIlbc::SampleRateHz() const { | 63 int AudioEncoderIlbc::SampleRateHz() const { |
55 return kSampleRateHz; | 64 return kSampleRateHz; |
56 } | 65 } |
57 | 66 |
58 int AudioEncoderIlbc::NumChannels() const { | 67 int AudioEncoderIlbc::NumChannels() const { |
59 return 1; | 68 return 1; |
60 } | 69 } |
61 | 70 |
62 size_t AudioEncoderIlbc::MaxEncodedBytes() const { | |
63 return RequiredOutputSizeBytes(); | |
64 } | |
65 | |
66 size_t AudioEncoderIlbc::Num10MsFramesInNextPacket() const { | 71 size_t AudioEncoderIlbc::Num10MsFramesInNextPacket() const { |
67 return num_10ms_frames_per_packet_; | 72 return num_10ms_frames_per_packet_; |
68 } | 73 } |
69 | 74 |
70 size_t AudioEncoderIlbc::Max10MsFramesInAPacket() const { | 75 size_t AudioEncoderIlbc::Max10MsFramesInAPacket() const { |
71 return num_10ms_frames_per_packet_; | 76 return num_10ms_frames_per_packet_; |
72 } | 77 } |
73 | 78 |
74 int AudioEncoderIlbc::GetTargetBitrate() const { | 79 int AudioEncoderIlbc::GetTargetBitrate() const { |
75 switch (num_10ms_frames_per_packet_) { | 80 switch (num_10ms_frames_per_packet_) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 const int output_len = WebRtcIlbcfix_Encode( | 117 const int output_len = WebRtcIlbcfix_Encode( |
113 encoder_, | 118 encoder_, |
114 input_buffer_, | 119 input_buffer_, |
115 kSampleRateHz / 100 * num_10ms_frames_per_packet_, | 120 kSampleRateHz / 100 * num_10ms_frames_per_packet_, |
116 encoded); | 121 encoded); |
117 CHECK_GE(output_len, 0); | 122 CHECK_GE(output_len, 0); |
118 EncodedInfo info; | 123 EncodedInfo info; |
119 info.encoded_bytes = static_cast<size_t>(output_len); | 124 info.encoded_bytes = static_cast<size_t>(output_len); |
120 DCHECK_EQ(info.encoded_bytes, RequiredOutputSizeBytes()); | 125 DCHECK_EQ(info.encoded_bytes, RequiredOutputSizeBytes()); |
121 info.encoded_timestamp = first_timestamp_in_buffer_; | 126 info.encoded_timestamp = first_timestamp_in_buffer_; |
122 info.payload_type = payload_type_; | 127 info.payload_type = config_.payload_type; |
123 return info; | 128 return info; |
124 } | 129 } |
125 | 130 |
| 131 void AudioEncoderIlbc::Reset() { |
| 132 if (encoder_) |
| 133 CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_)); |
| 134 CHECK(config_.IsOk()); |
| 135 CHECK_EQ(0, WebRtcIlbcfix_EncoderCreate(&encoder_)); |
| 136 const int encoder_frame_size_ms = config_.frame_size_ms > 30 |
| 137 ? config_.frame_size_ms / 2 |
| 138 : config_.frame_size_ms; |
| 139 CHECK_EQ(0, WebRtcIlbcfix_EncoderInit(encoder_, encoder_frame_size_ms)); |
| 140 num_10ms_frames_buffered_ = 0; |
| 141 } |
| 142 |
126 size_t AudioEncoderIlbc::RequiredOutputSizeBytes() const { | 143 size_t AudioEncoderIlbc::RequiredOutputSizeBytes() const { |
127 switch (num_10ms_frames_per_packet_) { | 144 switch (num_10ms_frames_per_packet_) { |
128 case 2: return 38; | 145 case 2: return 38; |
129 case 3: return 50; | 146 case 3: return 50; |
130 case 4: return 2 * 38; | 147 case 4: return 2 * 38; |
131 case 6: return 2 * 50; | 148 case 6: return 2 * 50; |
132 default: FATAL(); | 149 default: FATAL(); |
133 } | 150 } |
134 } | 151 } |
135 | 152 |
136 namespace { | |
137 AudioEncoderIlbc::Config CreateConfig(const CodecInst& codec_inst) { | |
138 AudioEncoderIlbc::Config config; | |
139 config.frame_size_ms = codec_inst.pacsize / 8; | |
140 config.payload_type = codec_inst.pltype; | |
141 return config; | |
142 } | |
143 } // namespace | |
144 | |
145 AudioEncoderMutableIlbc::AudioEncoderMutableIlbc(const CodecInst& codec_inst) | |
146 : AudioEncoderMutableImpl<AudioEncoderIlbc>(CreateConfig(codec_inst)) { | |
147 } | |
148 | |
149 } // namespace webrtc | 153 } // namespace webrtc |
OLD | NEW |