blob: 684480a1d3ee3f1ba436071f9895cb6098e7b66f [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"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000019
niklase@google.com470e71d2011-07-07 08:21:25 +000020#define WEBRTC_CNG_MAX_LPC_ORDER 12
niklase@google.com470e71d2011-07-07 08:21:25 +000021
ossu97ba30e2016-04-25 07:55:58 -070022namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000023
ossu97ba30e2016-04-25 07:55:58 -070024class ComfortNoiseDecoder {
25 public:
26 ComfortNoiseDecoder();
27 ~ComfortNoiseDecoder() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +000028
ossu97ba30e2016-04-25 07:55:58 -070029 ComfortNoiseDecoder(const ComfortNoiseDecoder&) = delete;
30 ComfortNoiseDecoder& operator=(const ComfortNoiseDecoder&) = delete;
niklase@google.com470e71d2011-07-07 08:21:25 +000031
ossu97ba30e2016-04-25 07:55:58 -070032 void Reset();
niklase@google.com470e71d2011-07-07 08:21:25 +000033
ossu97ba30e2016-04-25 07:55:58 -070034 // Updates the CN state when a new SID packet arrives.
35 // |sid| is a view of the SID packet without the headers.
36 void UpdateSid(rtc::ArrayView<const uint8_t> sid);
niklase@google.com470e71d2011-07-07 08:21:25 +000037
ossu97ba30e2016-04-25 07:55:58 -070038 // Generates comfort noise.
39 // |out_data| will be filled with samples - its size determines the number of
40 // samples generated. When |new_period| is true, CNG history will be reset
41 // before any audio is generated. Returns |false| if outData is too large -
42 // currently 640 bytes (equalling 10ms at 64kHz).
43 // TODO(ossu): Specify better limits for the size of out_data. Either let it
44 // be unbounded or limit to 10ms in the current sample rate.
45 bool Generate(rtc::ArrayView<int16_t> out_data, bool new_period);
niklase@google.com470e71d2011-07-07 08:21:25 +000046
ossu97ba30e2016-04-25 07:55:58 -070047 private:
48 uint32_t dec_seed_;
49 int32_t dec_target_energy_;
50 int32_t dec_used_energy_;
51 int16_t dec_target_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
52 int16_t dec_used_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
53 int16_t dec_filtstate_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
54 int16_t dec_filtstateLow_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
55 uint16_t dec_order_;
Yves Gerey665174f2018-06-19 15:03:05 +020056 int16_t dec_target_scale_factor_; /* Q29 */
57 int16_t dec_used_scale_factor_; /* Q29 */
ossu97ba30e2016-04-25 07:55:58 -070058};
niklase@google.com470e71d2011-07-07 08:21:25 +000059
ossu97ba30e2016-04-25 07:55:58 -070060class ComfortNoiseEncoder {
61 public:
62 // Creates a comfort noise encoder.
63 // |fs| selects sample rate: 8000 for narrowband or 16000 for wideband.
64 // |interval| sets the interval at which to generate SID data (in ms).
65 // |quality| selects the number of refl. coeffs. Maximum allowed is 12.
66 ComfortNoiseEncoder(int fs, int interval, int quality);
67 ~ComfortNoiseEncoder() = default;
niklase@google.com470e71d2011-07-07 08:21:25 +000068
ossu97ba30e2016-04-25 07:55:58 -070069 ComfortNoiseEncoder(const ComfortNoiseEncoder&) = delete;
70 ComfortNoiseEncoder& operator=(const ComfortNoiseEncoder&) = delete;
niklase@google.com470e71d2011-07-07 08:21:25 +000071
ossu97ba30e2016-04-25 07:55:58 -070072 // Resets the comfort noise encoder to its initial state.
73 // Parameters are set as during construction.
74 void Reset(int fs, int interval, int quality);
niklase@google.com470e71d2011-07-07 08:21:25 +000075
ossu97ba30e2016-04-25 07:55:58 -070076 // Analyzes background noise from |speech| and appends coefficients to
77 // |output|. Returns the number of coefficients generated. If |force_sid| is
78 // true, a SID frame is forced and the internal sid interval counter is reset.
79 // Will fail if the input size is too large (> 640 samples, see
80 // ComfortNoiseDecoder::Generate).
81 size_t Encode(rtc::ArrayView<const int16_t> speech,
82 bool force_sid,
83 rtc::Buffer* output);
niklase@google.com470e71d2011-07-07 08:21:25 +000084
ossu97ba30e2016-04-25 07:55:58 -070085 private:
86 size_t enc_nrOfCoefs_;
87 int enc_sampfreq_;
88 int16_t enc_interval_;
89 int16_t enc_msSinceSid_;
90 int32_t enc_Energy_;
91 int16_t enc_reflCoefs_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
92 int32_t enc_corrVector_[WEBRTC_CNG_MAX_LPC_ORDER + 1];
93 uint32_t enc_seed_;
94};
95
96} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +000097
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020098#endif // MODULES_AUDIO_CODING_CODECS_CNG_WEBRTC_CNG_H_