Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Side by Side Diff: webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.cc

Issue 2695243005: Injectable audio encoders: BuiltinAudioEncoderFactory (Closed)
Patch Set: Moved ANA frame length calculation into its own function. Improved "ptime" parsing in non-opus code… Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/audio_encoder_ilbc.h" 11 #include "webrtc/modules/audio_coding/codecs/ilbc/audio_encoder_ilbc.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 #include <limits> 14 #include <limits>
15 #include "webrtc/base/checks.h" 15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/string_to_number.h"
16 #include "webrtc/common_types.h" 17 #include "webrtc/common_types.h"
17 #include "webrtc/modules/audio_coding/codecs/ilbc/ilbc.h" 18 #include "webrtc/modules/audio_coding/codecs/ilbc/ilbc.h"
18 19
19 namespace webrtc { 20 namespace webrtc {
20 21
21 namespace { 22 namespace {
22 23
23 const int kSampleRateHz = 8000; 24 const int kSampleRateHz = 8000;
24 25
25 AudioEncoderIlbc::Config CreateConfig(const CodecInst& codec_inst) { 26 AudioEncoderIlbc::Config CreateConfig(const CodecInst& codec_inst) {
26 AudioEncoderIlbc::Config config; 27 AudioEncoderIlbc::Config config;
27 config.frame_size_ms = codec_inst.pacsize / 8; 28 config.frame_size_ms = codec_inst.pacsize / 8;
28 config.payload_type = codec_inst.pltype; 29 config.payload_type = codec_inst.pltype;
29 return config; 30 return config;
30 } 31 }
31 32
33 AudioEncoderIlbc::Config CreateConfig(int payload_type,
34 const SdpAudioFormat& format) {
35 AudioEncoderIlbc::Config config;
36 config.payload_type = payload_type;
37 auto ptime_iter = format.parameters.find("ptime");
38 if (ptime_iter != format.parameters.end()) {
39 auto ptime = rtc::StringToNumber<int>(ptime_iter->second);
40 if (ptime && *ptime > 0) {
41 const int whole_packets = *ptime / 10;
42 config.frame_size_ms = std::max(20, std::min(whole_packets * 10, 60));
43 }
44 }
45 return config;
46 }
47
48 int GetIlbcBitrate(int ptime) {
49 switch (ptime) {
50 case 20: case 40:
51 // 38 bytes per frame of 20 ms => 15200 bits/s.
52 return 15200;
53 case 30: case 60:
54 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s.
55 return 13333;
56 default:
57 FATAL();
58 }
59 }
60
32 } // namespace 61 } // namespace
33 62
34 // static 63 // static
35 const size_t AudioEncoderIlbc::kMaxSamplesPerPacket; 64 const size_t AudioEncoderIlbc::kMaxSamplesPerPacket;
36 65
37 bool AudioEncoderIlbc::Config::IsOk() const { 66 bool AudioEncoderIlbc::Config::IsOk() const {
38 return (frame_size_ms == 20 || frame_size_ms == 30 || frame_size_ms == 40 || 67 return (frame_size_ms == 20 || frame_size_ms == 30 || frame_size_ms == 40 ||
39 frame_size_ms == 60) && 68 frame_size_ms == 60) &&
40 static_cast<size_t>(kSampleRateHz / 100 * (frame_size_ms / 10)) <= 69 static_cast<size_t>(kSampleRateHz / 100 * (frame_size_ms / 10)) <=
41 kMaxSamplesPerPacket; 70 kMaxSamplesPerPacket;
42 } 71 }
43 72
44 AudioEncoderIlbc::AudioEncoderIlbc(const Config& config) 73 AudioEncoderIlbc::AudioEncoderIlbc(const Config& config)
45 : config_(config), 74 : config_(config),
46 num_10ms_frames_per_packet_( 75 num_10ms_frames_per_packet_(
47 static_cast<size_t>(config.frame_size_ms / 10)), 76 static_cast<size_t>(config.frame_size_ms / 10)),
48 encoder_(nullptr) { 77 encoder_(nullptr) {
49 Reset(); 78 Reset();
50 } 79 }
51 80
52 AudioEncoderIlbc::AudioEncoderIlbc(const CodecInst& codec_inst) 81 AudioEncoderIlbc::AudioEncoderIlbc(const CodecInst& codec_inst)
53 : AudioEncoderIlbc(CreateConfig(codec_inst)) {} 82 : AudioEncoderIlbc(CreateConfig(codec_inst)) {}
54 83
84 AudioEncoderIlbc::AudioEncoderIlbc(int payload_type,
85 const SdpAudioFormat& format)
86 : AudioEncoderIlbc(CreateConfig(payload_type, format)) {}
87
88 rtc::Optional<AudioCodecInfo> AudioEncoderIlbc::QueryAudioEncoder(
89 const SdpAudioFormat& format) {
90 if (STR_CASE_CMP(format.name.c_str(), GetPayloadName()) == 0 &&
91 format.clockrate_hz == 8000 && format.num_channels == 1) {
92 Config config = CreateConfig(0, format);
93 if (config.IsOk()) {
94 return rtc::Optional<AudioCodecInfo>(
95 {kSampleRateHz, 1, GetIlbcBitrate(config.frame_size_ms)});
96 }
97 }
98
99 return rtc::Optional<AudioCodecInfo>();
100 }
101
102
55 AudioEncoderIlbc::~AudioEncoderIlbc() { 103 AudioEncoderIlbc::~AudioEncoderIlbc() {
56 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_)); 104 RTC_CHECK_EQ(0, WebRtcIlbcfix_EncoderFree(encoder_));
57 } 105 }
58 106
59 int AudioEncoderIlbc::SampleRateHz() const { 107 int AudioEncoderIlbc::SampleRateHz() const {
60 return kSampleRateHz; 108 return kSampleRateHz;
61 } 109 }
62 110
63 size_t AudioEncoderIlbc::NumChannels() const { 111 size_t AudioEncoderIlbc::NumChannels() const {
64 return 1; 112 return 1;
65 } 113 }
66 114
67 size_t AudioEncoderIlbc::Num10MsFramesInNextPacket() const { 115 size_t AudioEncoderIlbc::Num10MsFramesInNextPacket() const {
68 return num_10ms_frames_per_packet_; 116 return num_10ms_frames_per_packet_;
69 } 117 }
70 118
71 size_t AudioEncoderIlbc::Max10MsFramesInAPacket() const { 119 size_t AudioEncoderIlbc::Max10MsFramesInAPacket() const {
72 return num_10ms_frames_per_packet_; 120 return num_10ms_frames_per_packet_;
73 } 121 }
74 122
75 int AudioEncoderIlbc::GetTargetBitrate() const { 123 int AudioEncoderIlbc::GetTargetBitrate() const {
76 switch (num_10ms_frames_per_packet_) { 124 return GetIlbcBitrate(num_10ms_frames_per_packet_ * 10);
77 case 2: case 4:
78 // 38 bytes per frame of 20 ms => 15200 bits/s.
79 return 15200;
80 case 3: case 6:
81 // 50 bytes per frame of 30 ms => (approx) 13333 bits/s.
82 return 13333;
83 default:
84 FATAL();
85 }
86 } 125 }
87 126
88 AudioEncoder::EncodedInfo AudioEncoderIlbc::EncodeImpl( 127 AudioEncoder::EncodedInfo AudioEncoderIlbc::EncodeImpl(
89 uint32_t rtp_timestamp, 128 uint32_t rtp_timestamp,
90 rtc::ArrayView<const int16_t> audio, 129 rtc::ArrayView<const int16_t> audio,
91 rtc::Buffer* encoded) { 130 rtc::Buffer* encoded) {
92 131
93 // Save timestamp if starting a new packet. 132 // Save timestamp if starting a new packet.
94 if (num_10ms_frames_buffered_ == 0) 133 if (num_10ms_frames_buffered_ == 0)
95 first_timestamp_in_buffer_ = rtp_timestamp; 134 first_timestamp_in_buffer_ = rtp_timestamp;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 switch (num_10ms_frames_per_packet_) { 186 switch (num_10ms_frames_per_packet_) {
148 case 2: return 38; 187 case 2: return 38;
149 case 3: return 50; 188 case 3: return 50;
150 case 4: return 2 * 38; 189 case 4: return 2 * 38;
151 case 6: return 2 * 50; 190 case 6: return 2 * 50;
152 default: FATAL(); 191 default: FATAL();
153 } 192 }
154 } 193 }
155 194
156 } // namespace webrtc 195 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698