1 /******************************************************************************
2  *
3  *  Copyright 2022 Google LLC
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /**
20  * LC3 - Spectral coefficients encoding/decoding
21  *
22  * Reference : Low Complexity Communication Codec (LC3)
23  *             Bluetooth Specification v1.0
24  */
25 
26 #ifndef __LC3_SPEC_H
27 #define __LC3_SPEC_H
28 
29 #include "common.h"
30 #include "tables.h"
31 #include "bwdet.h"
32 #include "ltpf.h"
33 #include "tns.h"
34 #include "sns.h"
35 
36 
37 /**
38  * Spectral quantization side data
39  */
40 typedef struct lc3_spec_side {
41     int g_idx, nq;
42     bool lsb_mode;
43 } lc3_spec_side_t;
44 
45 
46 /* ----------------------------------------------------------------------------
47  *  Encoding
48  * -------------------------------------------------------------------------- */
49 
50 /**
51  * Spectrum analysis
52  * dt, sr, nbytes  Duration, samplerate and size of the frame
53  * pitch, tns      Pitch present indication and TNS bistream data
54  * spec            Context of analysis
55  * x               Spectral coefficients, scaled as output
56  * xq, side        Return quantization data
57  *
58  * The spectral coefficients `xq` storage is :
59  *   b0       0:positive or zero  1:negative
60  *   b15..b1  Absolute value
61  */
62 void lc3_spec_analyze(enum lc3_dt dt, enum lc3_srate sr,
63     int nbytes, bool pitch, const lc3_tns_data_t *tns,
64     lc3_spec_analysis_t *spec, float *x, uint16_t *xq, lc3_spec_side_t *side);
65 
66 /**
67  * Put spectral quantization side data
68  * bits            Bitstream context
69  * dt, sr          Duration and samplerate of the frame
70  * side            Spectral quantization side data
71  */
72 void lc3_spec_put_side(lc3_bits_t *bits,
73     enum lc3_dt dt, enum lc3_srate sr, const lc3_spec_side_t *side);
74 
75 /**
76  * Encode spectral coefficients
77  * bits            Bitstream context
78  * dt, sr, bw      Duration, samplerate, bandwidth
79  * nbytes          and size of the frame
80  * xq, side        Quantization data
81  * x               Scaled spectral coefficients
82  *
83  * The spectral coefficients `xq` storage is :
84  *   b0       0:positive or zero  1:negative
85  *   b15..b1  Absolute value
86  */
87 void lc3_spec_encode(lc3_bits_t *bits,
88     enum lc3_dt dt, enum lc3_srate sr, enum lc3_bandwidth bw, int nbytes,
89     const uint16_t *xq, const lc3_spec_side_t *side, const float *x);
90 
91 
92 /* ----------------------------------------------------------------------------
93  *  Decoding
94  * -------------------------------------------------------------------------- */
95 
96 /**
97  * Get spectral quantization side data
98  * bits            Bitstream context
99  * dt, sr          Duration and samplerate of the frame
100  * side            Return quantization side data
101  * return          0: Ok  -1: Invalid bandwidth indication
102  */
103 int lc3_spec_get_side(lc3_bits_t *bits,
104     enum lc3_dt dt, enum lc3_srate sr, lc3_spec_side_t *side);
105 
106 /**
107  * Decode spectral coefficients
108  * bits            Bitstream context
109  * dt, sr, bw      Duration, samplerate, bandwidth
110  * nbytes          and size of the frame
111  * side            Quantization side data
112  * x               Spectral coefficients
113  * return          0: Ok  -1: Invalid bitstream data
114  */
115 int lc3_spec_decode(lc3_bits_t *bits, enum lc3_dt dt, enum lc3_srate sr,
116     enum lc3_bandwidth bw, int nbytes, const lc3_spec_side_t *side, float *x);
117 
118 
119 #endif /* __LC3_SPEC_H */
120