blob: 13aa31df8e59e0db6ec78accc9d015148264a991 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
2 * Copyright (c) 2012 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_MERGE_H_
12#define MODULES_AUDIO_CODING_NETEQ_MERGE_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/audio_coding/neteq/audio_multi_vector.h"
Steve Anton10542f22019-01-11 09:11:00 -080015#include "rtc_base/constructor_magic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
17namespace webrtc {
18
19// Forward declarations.
20class Expand;
21class SyncBuffer;
22
23// This class handles the transition from expansion to normal operation.
24// When a packet is not available for decoding when needed, the expand operation
25// is called to generate extrapolation data. If the missing packet arrives,
26// i.e., it was just delayed, it can be decoded and appended directly to the
27// end of the expanded data (thanks to how the Expand class operates). However,
28// if a later packet arrives instead, the loss is a fact, and the new data must
29// be stitched together with the end of the expanded data. This stitching is
30// what the Merge class does.
31class Merge {
32 public:
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020033 Merge(int fs_hz,
34 size_t num_channels,
35 Expand* expand,
36 SyncBuffer* sync_buffer);
minyue5bd33972016-05-02 04:46:11 -070037 virtual ~Merge();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000038
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 // The main method to produce the audio data. The decoded data is supplied in
Artem Titovd00ce742021-07-28 20:00:17 +020040 // `input`, having `input_length` samples in total for all channels
41 // (interleaved). The result is written to `output`. The number of channels
42 // allocated in `output` defines the number of channels that will be used when
43 // de-interleaving `input`.
Yves Gerey665174f2018-06-19 15:03:05 +020044 virtual size_t Process(int16_t* input,
45 size_t input_length,
Peter Kastingdce40cf2015-08-24 14:52:23 -070046 AudioMultiVector* output);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000047
Peter Kastingdce40cf2015-08-24 14:52:23 -070048 virtual size_t RequiredFutureSamples();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000049
50 protected:
51 const int fs_hz_;
52 const size_t num_channels_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000053
54 private:
55 static const int kMaxSampleRate = 48000;
Peter Kastingdce40cf2015-08-24 14:52:23 -070056 static const size_t kExpandDownsampLength = 100;
57 static const size_t kInputDownsampLength = 40;
58 static const size_t kMaxCorrelationLength = 60;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000059
Artem Titovd00ce742021-07-28 20:00:17 +020060 // Calls `expand_` to get more expansion data to merge with. The data is
61 // written to `expanded_signal_`. Returns the length of the expanded data,
62 // while `expand_period` will be the number of samples in one expansion period
63 // (typically one pitch period). The value of `old_length` will be the number
64 // of samples that were taken from the `sync_buffer_`.
Peter Kastingdce40cf2015-08-24 14:52:23 -070065 size_t GetExpandedSignal(size_t* old_length, size_t* expand_period);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000066
Artem Titovd00ce742021-07-28 20:00:17 +020067 // Analyzes `input` and `expanded_signal` and returns muting factor (Q14) to
minyue53ff70f2016-05-02 01:50:30 -070068 // be used on the new data.
Yves Gerey665174f2018-06-19 15:03:05 +020069 int16_t SignalScaling(const int16_t* input,
70 size_t input_length,
minyue53ff70f2016-05-02 01:50:30 -070071 const int16_t* expanded_signal) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000072
Artem Titovd00ce742021-07-28 20:00:17 +020073 // Downsamples `input` (`input_length` samples) and `expanded_signal` to
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074 // 4 kHz sample rate. The downsampled signals are written to
Artem Titovd00ce742021-07-28 20:00:17 +020075 // `input_downsampled_` and `expanded_downsampled_`, respectively.
Yves Gerey665174f2018-06-19 15:03:05 +020076 void Downsample(const int16_t* input,
77 size_t input_length,
78 const int16_t* expanded_signal,
79 size_t expanded_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000080
Artem Titovd00ce742021-07-28 20:00:17 +020081 // Calculates cross-correlation between `input_downsampled_` and
82 // `expanded_downsampled_`, and finds the correlation maximum. The maximizing
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000083 // lag is returned.
Yves Gerey665174f2018-06-19 15:03:05 +020084 size_t CorrelateAndPeakSearch(size_t start_position,
85 size_t input_length,
Peter Kastingdce40cf2015-08-24 14:52:23 -070086 size_t expand_period) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000087
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088 const int fs_mult_; // fs_hz_ / 8000.
Peter Kastingdce40cf2015-08-24 14:52:23 -070089 const size_t timestamps_per_call_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000090 Expand* expand_;
91 SyncBuffer* sync_buffer_;
92 int16_t expanded_downsampled_[kExpandDownsampLength];
93 int16_t input_downsampled_[kInputDownsampLength];
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000094 AudioMultiVector expanded_;
minyue5bd33972016-05-02 04:46:11 -070095 std::vector<int16_t> temp_data_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000096
henrikg3c089d72015-09-16 05:37:44 -070097 RTC_DISALLOW_COPY_AND_ASSIGN(Merge);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098};
99
100} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200101#endif // MODULES_AUDIO_CODING_NETEQ_MERGE_H_