blob: 3f8fadcd71b4dc7774d9b859e9c46745199bb7a6 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
leozwang@webrtc.org91b359e2012-02-28 17:26:14 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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_CODECS_CNG_WEBRTC_CNG_H_
12#define MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
ossu97ba30e2016-04-25 07:55:58 -070014#include <cstddef>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "api/array_view.h"
17#include "rtc_base/buffer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
niklase@google.com470e71d2011-07-07 08:21:25 +000019#define WEBRTC_CNG_MAX_LPC_ORDER 12
niklase@google.com470e71d2011-07-07 08:21:25 +000020
ossu97ba30e2016-04-25 07:55:58 -070021namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000022
ossu97ba30e2016-04-25 07:55:58 -070023class ComfortNoiseDecoder {
24 public:
25 ComfortNoiseDecoder();
26 ~ComfortNoiseDecoder() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +000027
ossu97ba30e2016-04-25 07:55:58 -070028 ComfortNoiseDecoder(const ComfortNoiseDecoder&) = delete;
29 ComfortNoiseDecoder& operator=(const ComfortNoiseDecoder&) = delete;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
ossu97ba30e2016-04-25 07:55:58 -070031 void Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000032
ossu97ba30e2016-04-25 07:55:58 -070033 // Updates the CN state when a new SID packet arrives.
34 // |sid| is a view of the SID packet without the headers.
35 void UpdateSid(rtc::ArrayView<const uint8_t> sid);
niklase@google.com470e71d2011-07-07 08:21:25 +000036
ossu97ba30e2016-04-25 07:55:58 -070037 // Generates comfort noise.
38 // |out_data| will be filled with samples - its size determines the number of
39 // samples generated. When |new_period| is true, CNG history will be reset
40 // before any audio is generated. Returns |false| if outData is too large -
41 // currently 640 bytes (equalling 10ms at 64kHz).
42 // TODO(ossu): Specify better limits for the size of out_data. Either let it
43 // be unbounded or limit to 10ms in the current sample rate.
44 bool Generate(rtc::ArrayView<int16_t> out_data, bool new_period);
niklase@google.com470e71d2011-07-07 08:21:25 +000045
ossu97ba30e2016-04-25 07:55:58 -070046 private:
47 uint32_t dec_seed_;
48 int32_t dec_target_energy_;
49 int32_t dec_used_energy_;
50 int16_t dec_target_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
51 int16_t dec_used_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
52 int16_t dec_filtstate_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
53 int16_t dec_filtstateLow_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
54 uint16_t dec_order_;
Yves Gerey665174f2018-06-19 15:03:05 +020055 int16_t dec_target_scale_factor_; /* Q29 */
56 int16_t dec_used_scale_factor_; /* Q29 */
ossu97ba30e2016-04-25 07:55:58 -070057};
niklase@google.com470e71d2011-07-07 08:21:25 +000058
ossu97ba30e2016-04-25 07:55:58 -070059class ComfortNoiseEncoder {
60 public:
61 // Creates a comfort noise encoder.
62 // |fs| selects sample rate: 8000 for narrowband or 16000 for wideband.
63 // |interval| sets the interval at which to generate SID data (in ms).
64 // |quality| selects the number of refl. coeffs. Maximum allowed is 12.
65 ComfortNoiseEncoder(int fs, int interval, int quality);
66 ~ComfortNoiseEncoder() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +000067
ossu97ba30e2016-04-25 07:55:58 -070068 ComfortNoiseEncoder(const ComfortNoiseEncoder&) = delete;
69 ComfortNoiseEncoder& operator=(const ComfortNoiseEncoder&) = delete;
niklase@google.com470e71d2011-07-07 08:21:25 +000070
ossu97ba30e2016-04-25 07:55:58 -070071 // Resets the comfort noise encoder to its initial state.
72 // Parameters are set as during construction.
73 void Reset(int fs, int interval, int quality);
niklase@google.com470e71d2011-07-07 08:21:25 +000074
ossu97ba30e2016-04-25 07:55:58 -070075 // Analyzes background noise from |speech| and appends coefficients to
76 // |output|. Returns the number of coefficients generated. If |force_sid| is
77 // true, a SID frame is forced and the internal sid interval counter is reset.
78 // Will fail if the input size is too large (> 640 samples, see
79 // ComfortNoiseDecoder::Generate).
80 size_t Encode(rtc::ArrayView<const int16_t> speech,
81 bool force_sid,
82 rtc::Buffer* output);
niklase@google.com470e71d2011-07-07 08:21:25 +000083
ossu97ba30e2016-04-25 07:55:58 -070084 private:
85 size_t enc_nrOfCoefs_;
86 int enc_sampfreq_;
87 int16_t enc_interval_;
88 int16_t enc_msSinceSid_;
89 int32_t enc_Energy_;
90 int16_t enc_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
91 int32_t enc_corrVector_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
92 uint32_t enc_seed_;
93};
94
95} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000096
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020097#endif // MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_