blob: 39761da0702b419b7d55573f332e7697d13f4246 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
12#define MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#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"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018
19namespace webrtc {
20
21// Forward declarations.
22class BufferLevelFilter;
23class DecoderDatabase;
24class DelayManager;
25class Expand;
26class PacketBuffer;
27class SyncBuffer;
ossu7a377612016-10-18 04:06:13 -070028struct Packet;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000029
Henrik Lundin7687ad52018-07-02 10:14:46 +020030// This is the class for the decision tree implementation.
Henrik Lundin5afa61c2018-07-02 14:53:24 +020031class DecisionLogic final {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000032 public:
33 // Static factory function which creates different types of objects depending
34 // on the |playout_mode|.
35 static DecisionLogic* Create(int fs_hz,
Peter Kastingdce40cf2015-08-24 14:52:23 -070036 size_t output_size_samples,
Henrik Lundin7687ad52018-07-02 10:14:46 +020037 bool disallow_time_stretching,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000038 DecoderDatabase* decoder_database,
39 const PacketBuffer& packet_buffer,
40 DelayManager* delay_manager,
Henrik Lundin47b17dc2016-05-10 10:20:59 +020041 BufferLevelFilter* buffer_level_filter,
42 const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043
Henrik Lundin7687ad52018-07-02 10:14:46 +020044 static const int kReinitAfterExpands = 100;
45 static const int kMaxWaitForPacket = 10;
46
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000047 // Constructor.
48 DecisionLogic(int fs_hz,
Peter Kastingdce40cf2015-08-24 14:52:23 -070049 size_t output_size_samples,
Henrik Lundin7687ad52018-07-02 10:14:46 +020050 bool disallow_time_stretching,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000051 DecoderDatabase* decoder_database,
52 const PacketBuffer& packet_buffer,
53 DelayManager* delay_manager,
Henrik Lundin47b17dc2016-05-10 10:20:59 +020054 BufferLevelFilter* buffer_level_filter,
55 const TickTimer* tick_timer);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000056
Henrik Lundin7687ad52018-07-02 10:14:46 +020057 ~DecisionLogic();
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000058
59 // Resets object to a clean state.
60 void Reset();
61
62 // Resets parts of the state. Typically done when switching codecs.
63 void SoftReset();
64
65 // Sets the sample rate and the output block size.
Peter Kastingdce40cf2015-08-24 14:52:23 -070066 void SetSampleRate(int fs_hz, size_t output_size_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000067
68 // Returns the operation that should be done next. |sync_buffer| and |expand|
69 // are provided for reference. |decoder_frame_length| is the number of samples
ossu7a377612016-10-18 04:06:13 -070070 // obtained from the last decoded frame. If there is a packet available, it
71 // should be supplied in |next_packet|; otherwise it should be NULL. The mode
72 // resulting from the last call to NetEqImpl::GetAudio is supplied in
73 // |prev_mode|. If there is a DTMF event to play, |play_dtmf| should be set to
74 // true. The output variable |reset_decoder| will be set to true if a reset is
75 // required; otherwise it is left unchanged (i.e., it can remain true if it
76 // was true before the call). This method end with calling
77 // GetDecisionSpecialized to get the actual return value.
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000078 Operations GetDecision(const SyncBuffer& sync_buffer,
79 const Expand& expand,
Peter Kastingdce40cf2015-08-24 14:52:23 -070080 size_t decoder_frame_length,
ossu7a377612016-10-18 04:06:13 -070081 const Packet* next_packet,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082 Modes prev_mode,
83 bool play_dtmf,
henrik.lundinb1fb72b2016-05-03 08:18:47 -070084 size_t generated_noise_samples,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000085 bool* reset_decoder);
86
87 // These methods test the |cng_state_| for different conditions.
88 bool CngRfc3389On() const { return cng_state_ == kCngRfc3389On; }
89 bool CngOff() const { return cng_state_ == kCngOff; }
90
91 // Resets the |cng_state_| to kCngOff.
92 void SetCngOff() { cng_state_ = kCngOff; }
93
94 // Reports back to DecisionLogic whether the decision to do expand remains or
95 // not. Note that this is necessary, since an expand decision can be changed
96 // to kNormal in NetEqImpl::GetDecision if there is still enough data in the
97 // sync buffer.
Henrik Lundin7687ad52018-07-02 10:14:46 +020098 void ExpandDecision(Operations operation);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000099
100 // Adds |value| to |sample_memory_|.
Yves Gerey665174f2018-06-19 15:03:05 +0200101 void AddSampleMemory(int32_t value) { sample_memory_ += value; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000102
103 // Accessors and mutators.
104 void set_sample_memory(int32_t value) { sample_memory_ = value; }
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700105 size_t noise_fast_forward() const { return noise_fast_forward_; }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700106 size_t packet_length_samples() const { return packet_length_samples_; }
107 void set_packet_length_samples(size_t value) {
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000108 packet_length_samples_ = value;
109 }
110 void set_prev_time_scale(bool value) { prev_time_scale_ = value; }
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111
Minyue Li7f6417f2018-10-03 21:19:08 +0200112 int postpone_decoding_level_for_test() const {
113 return postpone_decoding_level_;
114 }
115
Henrik Lundin7687ad52018-07-02 10:14:46 +0200116 private:
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200117 // The value 5 sets maximum time-stretch rate to about 100 ms/s.
118 static const int kMinTimescaleInterval = 5;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000119
Yves Gerey665174f2018-06-19 15:03:05 +0200120 enum CngState { kCngOff, kCngRfc3389On, kCngInternalOn };
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000121
Henrik Lundin7687ad52018-07-02 10:14:46 +0200122 // Updates the |buffer_level_filter_| with the current buffer level
123 // |buffer_size_packets|.
124 void FilterBufferLevel(size_t buffer_size_packets, Modes prev_mode);
125
Henrik Lundin7687ad52018-07-02 10:14:46 +0200126 // Returns the operation given that the next available packet is a comfort
127 // noise payload (RFC 3389 only, not codec-internal).
128 Operations CngOperation(Modes prev_mode,
129 uint32_t target_timestamp,
130 uint32_t available_timestamp,
131 size_t generated_noise_samples);
132
133 // Returns the operation given that no packets are available (except maybe
134 // a DTMF event, flagged by setting |play_dtmf| true).
135 Operations NoPacket(bool play_dtmf);
136
137 // Returns the operation to do given that the expected packet is available.
138 Operations ExpectedPacketAvailable(Modes prev_mode, bool play_dtmf);
139
140 // Returns the operation to do given that the expected packet is not
141 // available, but a packet further into the future is at hand.
142 Operations FuturePacketAvailable(const SyncBuffer& sync_buffer,
143 const Expand& expand,
144 size_t decoder_frame_length,
145 Modes prev_mode,
146 uint32_t target_timestamp,
147 uint32_t available_timestamp,
148 bool play_dtmf,
149 size_t generated_noise_samples);
150
151 // Checks if enough time has elapsed since the last successful timescale
152 // operation was done (i.e., accelerate or preemptive expand).
153 bool TimescaleAllowed() const {
154 return !timescale_countdown_ || timescale_countdown_->Finished();
155 }
156
157 // Checks if the current (filtered) buffer level is under the target level.
158 bool UnderTargetLevel() const;
159
160 // Checks if |timestamp_leap| is so long into the future that a reset due
161 // to exceeding kReinitAfterExpands will be done.
162 bool ReinitAfterExpands(uint32_t timestamp_leap) const;
163
164 // Checks if we still have not done enough expands to cover the distance from
165 // the last decoded packet to the next available packet, the distance beeing
166 // conveyed in |timestamp_leap|.
167 bool PacketTooEarly(uint32_t timestamp_leap) const;
168
169 // Checks if num_consecutive_expands_ >= kMaxWaitForPacket.
170 bool MaxWaitForPacket() const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000171
172 DecoderDatabase* decoder_database_;
173 const PacketBuffer& packet_buffer_;
174 DelayManager* delay_manager_;
175 BufferLevelFilter* buffer_level_filter_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200176 const TickTimer* tick_timer_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000177 int fs_mult_;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700178 size_t output_size_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000179 CngState cng_state_; // Remember if comfort noise is interrupted by other
180 // event (e.g., DTMF).
henrik.lundinb1fb72b2016-05-03 08:18:47 -0700181 size_t noise_fast_forward_ = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700182 size_t packet_length_samples_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000183 int sample_memory_;
184 bool prev_time_scale_;
Henrik Lundin7687ad52018-07-02 10:14:46 +0200185 bool disallow_time_stretching_;
Henrik Lundin47b17dc2016-05-10 10:20:59 +0200186 std::unique_ptr<TickTimer::Countdown> timescale_countdown_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000187 int num_consecutive_expands_;
Minyue Li7f6417f2018-10-03 21:19:08 +0200188 const int postpone_decoding_level_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000189
henrikg3c089d72015-09-16 05:37:44 -0700190 RTC_DISALLOW_COPY_AND_ASSIGN(DecisionLogic);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000191};
192
193} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200194#endif // MODULES_AUDIO_CODING_NETEQ_DECISION_LOGIC_H_