merge.h (4084B)
1 /* 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 11 #ifndef MODULES_AUDIO_CODING_NETEQ_MERGE_H_ 12 #define MODULES_AUDIO_CODING_NETEQ_MERGE_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <vector> 17 18 #include "modules/audio_coding/neteq/audio_multi_vector.h" 19 20 namespace webrtc { 21 22 // Forward declarations. 23 class Expand; 24 class SyncBuffer; 25 26 // This class handles the transition from expansion to normal operation. 27 // When a packet is not available for decoding when needed, the expand operation 28 // is called to generate extrapolation data. If the missing packet arrives, 29 // i.e., it was just delayed, it can be decoded and appended directly to the 30 // end of the expanded data (thanks to how the Expand class operates). However, 31 // if a later packet arrives instead, the loss is a fact, and the new data must 32 // be stitched together with the end of the expanded data. This stitching is 33 // what the Merge class does. 34 class Merge { 35 public: 36 Merge(int fs_hz, 37 size_t num_channels, 38 Expand* expand, 39 SyncBuffer* sync_buffer); 40 virtual ~Merge(); 41 42 Merge(const Merge&) = delete; 43 Merge& operator=(const Merge&) = delete; 44 45 // The main method to produce the audio data. The decoded data is supplied in 46 // `input`, having `input_length` samples in total for all channels 47 // (interleaved). The result is written to `output`. The number of channels 48 // allocated in `output` defines the number of channels that will be used when 49 // de-interleaving `input`. 50 virtual size_t Process(int16_t* input, 51 size_t input_length, 52 AudioMultiVector* output); 53 54 virtual size_t RequiredFutureSamples(); 55 56 protected: 57 const int fs_hz_; 58 const size_t num_channels_; 59 60 private: 61 static const int kMaxSampleRate = 48000; 62 static const size_t kExpandDownsampLength = 100; 63 static const size_t kInputDownsampLength = 40; 64 static const size_t kMaxCorrelationLength = 60; 65 66 // Calls `expand_` to get more expansion data to merge with. The data is 67 // written to `expanded_signal_`. Returns the length of the expanded data, 68 // while `expand_period` will be the number of samples in one expansion period 69 // (typically one pitch period). The value of `old_length` will be the number 70 // of samples that were taken from the `sync_buffer_`. 71 size_t GetExpandedSignal(size_t* old_length, size_t* expand_period); 72 73 // Analyzes `input` and `expanded_signal` and returns muting factor (Q14) to 74 // be used on the new data. 75 int16_t SignalScaling(const int16_t* input, 76 size_t input_length, 77 const int16_t* expanded_signal) const; 78 79 // Downsamples `input` (`input_length` samples) and `expanded_signal` to 80 // 4 kHz sample rate. The downsampled signals are written to 81 // `input_downsampled_` and `expanded_downsampled_`, respectively. 82 void Downsample(const int16_t* input, 83 size_t input_length, 84 const int16_t* expanded_signal, 85 size_t expanded_length); 86 87 // Calculates cross-correlation between `input_downsampled_` and 88 // `expanded_downsampled_`, and finds the correlation maximum. The maximizing 89 // lag is returned. 90 size_t CorrelateAndPeakSearch(size_t start_position, 91 size_t input_length, 92 size_t expand_period) const; 93 94 const int fs_mult_; // fs_hz_ / 8000. 95 const size_t timestamps_per_call_; 96 Expand* expand_; 97 SyncBuffer* sync_buffer_; 98 int16_t expanded_downsampled_[kExpandDownsampLength]; 99 int16_t input_downsampled_[kInputDownsampLength]; 100 AudioMultiVector expanded_; 101 std::vector<int16_t> temp_data_; 102 }; 103 104 } // namespace webrtc 105 #endif // MODULES_AUDIO_CODING_NETEQ_MERGE_H_