blob: 563f6765108bee7c1b1cf33e3bb890aee626686a [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
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
ossu97ba30e2016-04-25 07:55:58 -070016#include <cstddef>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
19#include "rtc_base/buffer.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000020
niklase@google.com470e71d2011-07-07 08:21:25 +000021#define WEBRTC_CNG_MAX_LPC_ORDER 12
niklase@google.com470e71d2011-07-07 08:21:25 +000022
ossu97ba30e2016-04-25 07:55:58 -070023namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
ossu97ba30e2016-04-25 07:55:58 -070025class ComfortNoiseDecoder {
26 public:
27 ComfortNoiseDecoder();
28 ~ComfortNoiseDecoder() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +000029
ossu97ba30e2016-04-25 07:55:58 -070030 ComfortNoiseDecoder(const ComfortNoiseDecoder&) = delete;
31 ComfortNoiseDecoder& operator=(const ComfortNoiseDecoder&) = delete;
niklase@google.com470e71d2011-07-07 08:21:25 +000032
ossu97ba30e2016-04-25 07:55:58 -070033 void Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000034
ossu97ba30e2016-04-25 07:55:58 -070035 // Updates the CN state when a new SID packet arrives.
36 // |sid| is a view of the SID packet without the headers.
37 void UpdateSid(rtc::ArrayView<const uint8_t> sid);
niklase@google.com470e71d2011-07-07 08:21:25 +000038
ossu97ba30e2016-04-25 07:55:58 -070039 // Generates comfort noise.
40 // |out_data| will be filled with samples - its size determines the number of
41 // samples generated. When |new_period| is true, CNG history will be reset
42 // before any audio is generated. Returns |false| if outData is too large -
43 // currently 640 bytes (equalling 10ms at 64kHz).
44 // TODO(ossu): Specify better limits for the size of out_data. Either let it
45 // be unbounded or limit to 10ms in the current sample rate.
46 bool Generate(rtc::ArrayView<int16_t> out_data, bool new_period);
niklase@google.com470e71d2011-07-07 08:21:25 +000047
ossu97ba30e2016-04-25 07:55:58 -070048 private:
49 uint32_t dec_seed_;
50 int32_t dec_target_energy_;
51 int32_t dec_used_energy_;
52 int16_t dec_target_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
53 int16_t dec_used_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
54 int16_t dec_filtstate_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
55 int16_t dec_filtstateLow_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
56 uint16_t dec_order_;
Yves Gerey665174f2018-06-19 15:03:05 +020057 int16_t dec_target_scale_factor_; /* Q29 */
58 int16_t dec_used_scale_factor_; /* Q29 */
ossu97ba30e2016-04-25 07:55:58 -070059};
niklase@google.com470e71d2011-07-07 08:21:25 +000060
ossu97ba30e2016-04-25 07:55:58 -070061class ComfortNoiseEncoder {
62 public:
63 // Creates a comfort noise encoder.
64 // |fs| selects sample rate: 8000 for narrowband or 16000 for wideband.
65 // |interval| sets the interval at which to generate SID data (in ms).
66 // |quality| selects the number of refl. coeffs. Maximum allowed is 12.
67 ComfortNoiseEncoder(int fs, int interval, int quality);
68 ~ComfortNoiseEncoder() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +000069
ossu97ba30e2016-04-25 07:55:58 -070070 ComfortNoiseEncoder(const ComfortNoiseEncoder&) = delete;
71 ComfortNoiseEncoder& operator=(const ComfortNoiseEncoder&) = delete;
niklase@google.com470e71d2011-07-07 08:21:25 +000072
ossu97ba30e2016-04-25 07:55:58 -070073 // Resets the comfort noise encoder to its initial state.
74 // Parameters are set as during construction.
75 void Reset(int fs, int interval, int quality);
niklase@google.com470e71d2011-07-07 08:21:25 +000076
ossu97ba30e2016-04-25 07:55:58 -070077 // Analyzes background noise from |speech| and appends coefficients to
78 // |output|. Returns the number of coefficients generated. If |force_sid| is
79 // true, a SID frame is forced and the internal sid interval counter is reset.
80 // Will fail if the input size is too large (> 640 samples, see
81 // ComfortNoiseDecoder::Generate).
82 size_t Encode(rtc::ArrayView<const int16_t> speech,
83 bool force_sid,
84 rtc::Buffer* output);
niklase@google.com470e71d2011-07-07 08:21:25 +000085
ossu97ba30e2016-04-25 07:55:58 -070086 private:
87 size_t enc_nrOfCoefs_;
88 int enc_sampfreq_;
89 int16_t enc_interval_;
90 int16_t enc_msSinceSid_;
91 int32_t enc_Energy_;
92 int16_t enc_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
93 int32_t enc_corrVector_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
94 uint32_t enc_seed_;
95};
96
97} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000098
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020099#endif // MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_