henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |
| 12 | #define MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 13 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/audio_coding/neteq/defines.h" |
| 15 | #include "modules/audio_coding/neteq/include/neteq.h" |
| 16 | #include "modules/audio_coding/neteq/tick_timer.h" |
| 17 | #include "rtc_base/constructormagic.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 18 | #include "typedefs.h" // NOLINT(build/include) |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
| 22 | // Forward declarations. |
| 23 | class BufferLevelFilter; |
| 24 | class DecoderDatabase; |
| 25 | class DelayManager; |
| 26 | class Expand; |
| 27 | class PacketBuffer; |
| 28 | class SyncBuffer; |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 29 | struct Packet; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 30 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 31 | // This is the class for the decision tree implementation. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 32 | class DecisionLogic { |
| 33 | public: |
| 34 | // Static factory function which creates different types of objects depending |
| 35 | // on the |playout_mode|. |
| 36 | static DecisionLogic* Create(int fs_hz, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 37 | size_t output_size_samples, |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 38 | bool disallow_time_stretching, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 39 | DecoderDatabase* decoder_database, |
| 40 | const PacketBuffer& packet_buffer, |
| 41 | DelayManager* delay_manager, |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 42 | BufferLevelFilter* buffer_level_filter, |
| 43 | const TickTimer* tick_timer); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 44 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 45 | static const int kReinitAfterExpands = 100; |
| 46 | static const int kMaxWaitForPacket = 10; |
| 47 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 48 | // Constructor. |
| 49 | DecisionLogic(int fs_hz, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 50 | size_t output_size_samples, |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 51 | bool disallow_time_stretching, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 52 | DecoderDatabase* decoder_database, |
| 53 | const PacketBuffer& packet_buffer, |
| 54 | DelayManager* delay_manager, |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 55 | BufferLevelFilter* buffer_level_filter, |
| 56 | const TickTimer* tick_timer); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 57 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 58 | ~DecisionLogic(); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 59 | |
| 60 | // Resets object to a clean state. |
| 61 | void Reset(); |
| 62 | |
| 63 | // Resets parts of the state. Typically done when switching codecs. |
| 64 | void SoftReset(); |
| 65 | |
| 66 | // Sets the sample rate and the output block size. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 67 | void SetSampleRate(int fs_hz, size_t output_size_samples); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 68 | |
| 69 | // Returns the operation that should be done next. |sync_buffer| and |expand| |
| 70 | // are provided for reference. |decoder_frame_length| is the number of samples |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 71 | // obtained from the last decoded frame. If there is a packet available, it |
| 72 | // should be supplied in |next_packet|; otherwise it should be NULL. The mode |
| 73 | // resulting from the last call to NetEqImpl::GetAudio is supplied in |
| 74 | // |prev_mode|. If there is a DTMF event to play, |play_dtmf| should be set to |
| 75 | // true. The output variable |reset_decoder| will be set to true if a reset is |
| 76 | // required; otherwise it is left unchanged (i.e., it can remain true if it |
| 77 | // was true before the call). This method end with calling |
| 78 | // GetDecisionSpecialized to get the actual return value. |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 79 | Operations GetDecision(const SyncBuffer& sync_buffer, |
| 80 | const Expand& expand, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 81 | size_t decoder_frame_length, |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 82 | const Packet* next_packet, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 83 | Modes prev_mode, |
| 84 | bool play_dtmf, |
henrik.lundin | b1fb72b | 2016-05-03 08:18:47 -0700 | [diff] [blame] | 85 | size_t generated_noise_samples, |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 86 | bool* reset_decoder); |
| 87 | |
| 88 | // These methods test the |cng_state_| for different conditions. |
| 89 | bool CngRfc3389On() const { return cng_state_ == kCngRfc3389On; } |
| 90 | bool CngOff() const { return cng_state_ == kCngOff; } |
| 91 | |
| 92 | // Resets the |cng_state_| to kCngOff. |
| 93 | void SetCngOff() { cng_state_ = kCngOff; } |
| 94 | |
| 95 | // Reports back to DecisionLogic whether the decision to do expand remains or |
| 96 | // not. Note that this is necessary, since an expand decision can be changed |
| 97 | // to kNormal in NetEqImpl::GetDecision if there is still enough data in the |
| 98 | // sync buffer. |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 99 | void ExpandDecision(Operations operation); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 100 | |
| 101 | // Adds |value| to |sample_memory_|. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 102 | void AddSampleMemory(int32_t value) { sample_memory_ += value; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 103 | |
| 104 | // Accessors and mutators. |
| 105 | void set_sample_memory(int32_t value) { sample_memory_ = value; } |
henrik.lundin | b1fb72b | 2016-05-03 08:18:47 -0700 | [diff] [blame] | 106 | size_t noise_fast_forward() const { return noise_fast_forward_; } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 107 | size_t packet_length_samples() const { return packet_length_samples_; } |
| 108 | void set_packet_length_samples(size_t value) { |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 109 | packet_length_samples_ = value; |
| 110 | } |
| 111 | void set_prev_time_scale(bool value) { prev_time_scale_ = value; } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 112 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 113 | private: |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 114 | // The value 5 sets maximum time-stretch rate to about 100 ms/s. |
| 115 | static const int kMinTimescaleInterval = 5; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 116 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 117 | enum CngState { kCngOff, kCngRfc3389On, kCngInternalOn }; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 118 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 119 | // Updates the |buffer_level_filter_| with the current buffer level |
| 120 | // |buffer_size_packets|. |
| 121 | void FilterBufferLevel(size_t buffer_size_packets, Modes prev_mode); |
| 122 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 123 | // Returns the operation that should be done next. |sync_buffer| and |expand| |
| 124 | // are provided for reference. |decoder_frame_length| is the number of samples |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 125 | // obtained from the last decoded frame. If there is a packet available, it |
| 126 | // should be supplied in |next_packet|; otherwise it should be NULL. The mode |
| 127 | // resulting from the last call to NetEqImpl::GetAudio is supplied in |
| 128 | // |prev_mode|. If there is a DTMF event to play, |play_dtmf| should be set to |
| 129 | // true. The output variable |reset_decoder| will be set to true if a reset is |
| 130 | // required; otherwise it is left unchanged (i.e., it can remain true if it |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 131 | // was true before the call). |
| 132 | // TODO(henrik.lundin) Fold this method into GetDecision. |
| 133 | Operations GetDecisionSpecialized(const SyncBuffer& sync_buffer, |
| 134 | const Expand& expand, |
| 135 | size_t decoder_frame_length, |
| 136 | const Packet* next_packet, |
| 137 | Modes prev_mode, |
| 138 | bool play_dtmf, |
| 139 | bool* reset_decoder, |
| 140 | size_t generated_noise_samples, |
| 141 | size_t cur_size_samples); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 142 | |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 143 | // Returns the operation given that the next available packet is a comfort |
| 144 | // noise payload (RFC 3389 only, not codec-internal). |
| 145 | Operations CngOperation(Modes prev_mode, |
| 146 | uint32_t target_timestamp, |
| 147 | uint32_t available_timestamp, |
| 148 | size_t generated_noise_samples); |
| 149 | |
| 150 | // Returns the operation given that no packets are available (except maybe |
| 151 | // a DTMF event, flagged by setting |play_dtmf| true). |
| 152 | Operations NoPacket(bool play_dtmf); |
| 153 | |
| 154 | // Returns the operation to do given that the expected packet is available. |
| 155 | Operations ExpectedPacketAvailable(Modes prev_mode, bool play_dtmf); |
| 156 | |
| 157 | // Returns the operation to do given that the expected packet is not |
| 158 | // available, but a packet further into the future is at hand. |
| 159 | Operations FuturePacketAvailable(const SyncBuffer& sync_buffer, |
| 160 | const Expand& expand, |
| 161 | size_t decoder_frame_length, |
| 162 | Modes prev_mode, |
| 163 | uint32_t target_timestamp, |
| 164 | uint32_t available_timestamp, |
| 165 | bool play_dtmf, |
| 166 | size_t generated_noise_samples); |
| 167 | |
| 168 | // Checks if enough time has elapsed since the last successful timescale |
| 169 | // operation was done (i.e., accelerate or preemptive expand). |
| 170 | bool TimescaleAllowed() const { |
| 171 | return !timescale_countdown_ || timescale_countdown_->Finished(); |
| 172 | } |
| 173 | |
| 174 | // Checks if the current (filtered) buffer level is under the target level. |
| 175 | bool UnderTargetLevel() const; |
| 176 | |
| 177 | // Checks if |timestamp_leap| is so long into the future that a reset due |
| 178 | // to exceeding kReinitAfterExpands will be done. |
| 179 | bool ReinitAfterExpands(uint32_t timestamp_leap) const; |
| 180 | |
| 181 | // Checks if we still have not done enough expands to cover the distance from |
| 182 | // the last decoded packet to the next available packet, the distance beeing |
| 183 | // conveyed in |timestamp_leap|. |
| 184 | bool PacketTooEarly(uint32_t timestamp_leap) const; |
| 185 | |
| 186 | // Checks if num_consecutive_expands_ >= kMaxWaitForPacket. |
| 187 | bool MaxWaitForPacket() const; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 188 | |
| 189 | DecoderDatabase* decoder_database_; |
| 190 | const PacketBuffer& packet_buffer_; |
| 191 | DelayManager* delay_manager_; |
| 192 | BufferLevelFilter* buffer_level_filter_; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 193 | const TickTimer* tick_timer_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 194 | int fs_mult_; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 195 | size_t output_size_samples_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 196 | CngState cng_state_; // Remember if comfort noise is interrupted by other |
| 197 | // event (e.g., DTMF). |
henrik.lundin | b1fb72b | 2016-05-03 08:18:47 -0700 | [diff] [blame] | 198 | size_t noise_fast_forward_ = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 199 | size_t packet_length_samples_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 200 | int sample_memory_; |
| 201 | bool prev_time_scale_; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 202 | bool disallow_time_stretching_; |
Henrik Lundin | 47b17dc | 2016-05-10 10:20:59 +0200 | [diff] [blame] | 203 | std::unique_ptr<TickTimer::Countdown> timescale_countdown_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 204 | int num_consecutive_expands_; |
Henrik Lundin | 7687ad5 | 2018-07-02 10:14:46 +0200 | [diff] [blame] | 205 | const bool postpone_decoding_after_expand_; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 206 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 207 | RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
| 210 | } // namespace webrtc |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 211 | #endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_ |