blob: 235c07f5b10b096281618a66fba3c5316c98488a [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
14#include <assert.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_coding/neteq/audio_multi_vector.h"
17#include "rtc_base/constructormagic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000018
19namespace webrtc {
20
21// Forward declarations.
22class Expand;
23class SyncBuffer;
24
25// This class handles the transition from expansion to normal operation.
26// When a packet is not available for decoding when needed, the expand operation
27// is called to generate extrapolation data. If the missing packet arrives,
28// i.e., it was just delayed, it can be decoded and appended directly to the
29// end of the expanded data (thanks to how the Expand class operates). However,
30// if a later packet arrives instead, the loss is a fact, and the new data must
31// be stitched together with the end of the expanded data. This stitching is
32// what the Merge class does.
33class Merge {
34 public:
Karl Wiberg7f6c4d42015-04-09 15:44:22 +020035 Merge(int fs_hz,
36 size_t num_channels,
37 Expand* expand,
38 SyncBuffer* sync_buffer);
minyue5bd33972016-05-02 04:46:11 -070039 virtual ~Merge();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000040
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000041 // The main method to produce the audio data. The decoded data is supplied in
42 // |input|, having |input_length| samples in total for all channels
43 // (interleaved). The result is written to |output|. The number of channels
44 // allocated in |output| defines the number of channels that will be used when
Henrik Lundin6dc82e82018-05-22 10:40:23 +020045 // de-interleaving |input|.
Yves Gerey665174f2018-06-19 15:03:05 +020046 virtual size_t Process(int16_t* input,
47 size_t input_length,
Peter Kastingdce40cf2015-08-24 14:52:23 -070048 AudioMultiVector* output);
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000049
Peter Kastingdce40cf2015-08-24 14:52:23 -070050 virtual size_t RequiredFutureSamples();
turaj@webrtc.org8d1cdaa2014-04-11 18:47:55 +000051
52 protected:
53 const int fs_hz_;
54 const size_t num_channels_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000055
56 private:
57 static const int kMaxSampleRate = 48000;
Peter Kastingdce40cf2015-08-24 14:52:23 -070058 static const size_t kExpandDownsampLength = 100;
59 static const size_t kInputDownsampLength = 40;
60 static const size_t kMaxCorrelationLength = 60;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000061
62 // Calls |expand_| to get more expansion data to merge with. The data is
63 // written to |expanded_signal_|. Returns the length of the expanded data,
64 // while |expand_period| will be the number of samples in one expansion period
65 // (typically one pitch period). The value of |old_length| will be the number
66 // of samples that were taken from the |sync_buffer_|.
Peter Kastingdce40cf2015-08-24 14:52:23 -070067 size_t GetExpandedSignal(size_t* old_length, size_t* expand_period);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068
minyue53ff70f2016-05-02 01:50:30 -070069 // Analyzes |input| and |expanded_signal| and returns muting factor (Q14) to
70 // be used on the new data.
Yves Gerey665174f2018-06-19 15:03:05 +020071 int16_t SignalScaling(const int16_t* input,
72 size_t input_length,
minyue53ff70f2016-05-02 01:50:30 -070073 const int16_t* expanded_signal) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000074
75 // Downsamples |input| (|input_length| samples) and |expanded_signal| to
76 // 4 kHz sample rate. The downsampled signals are written to
77 // |input_downsampled_| and |expanded_downsampled_|, respectively.
Yves Gerey665174f2018-06-19 15:03:05 +020078 void Downsample(const int16_t* input,
79 size_t input_length,
80 const int16_t* expanded_signal,
81 size_t expanded_length);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082
83 // Calculates cross-correlation between |input_downsampled_| and
84 // |expanded_downsampled_|, and finds the correlation maximum. The maximizing
85 // lag is returned.
Yves Gerey665174f2018-06-19 15:03:05 +020086 size_t CorrelateAndPeakSearch(size_t start_position,
87 size_t input_length,
Peter Kastingdce40cf2015-08-24 14:52:23 -070088 size_t expand_period) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000089
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000090 const int fs_mult_; // fs_hz_ / 8000.
Peter Kastingdce40cf2015-08-24 14:52:23 -070091 const size_t timestamps_per_call_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000092 Expand* expand_;
93 SyncBuffer* sync_buffer_;
94 int16_t expanded_downsampled_[kExpandDownsampLength];
95 int16_t input_downsampled_[kInputDownsampLength];
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000096 AudioMultiVector expanded_;
minyue5bd33972016-05-02 04:46:11 -070097 std::vector<int16_t> temp_data_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000098
henrikg3c089d72015-09-16 05:37:44 -070099 RTC_DISALLOW_COPY_AND_ASSIGN(Merge);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000100};
101
102} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200103#endif // MODULES_AUDIO_CODING_NETEQ_MERGE_H_