1/* Copyright (c) 2007-2008 CSIRO
2   Copyright (c) 2007-2009 Xiph.Org Foundation
3   Copyright (c) 2008 Gregory Maxwell
4   Written by Jean-Marc Valin and Gregory Maxwell */
5/**
6  @file celt.h
7  @brief Contains all the functions for encoding and decoding audio
8 */
9
10/*
11   Redistribution and use in source and binary forms, with or without
12   modification, are permitted provided that the following conditions
13   are met:
14
15   - Redistributions of source code must retain the above copyright
16   notice, this list of conditions and the following disclaimer.
17
18   - Redistributions in binary form must reproduce the above copyright
19   notice, this list of conditions and the following disclaimer in the
20   documentation and/or other materials provided with the distribution.
21
22   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*/
34
35#ifndef CELT_H
36#define CELT_H
37
38#include "opus_types.h"
39#include "opus_defines.h"
40#include "opus_custom.h"
41#include "entenc.h"
42#include "entdec.h"
43#include "arch.h"
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
49#define CELTEncoder OpusCustomEncoder
50#define CELTDecoder OpusCustomDecoder
51#define CELTMode OpusCustomMode
52
53typedef struct {
54   int valid;
55   float tonality;
56   float tonality_slope;
57   float noisiness;
58   float activity;
59   float music_prob;
60   int        bandwidth;
61}AnalysisInfo;
62
63#define __celt_check_mode_ptr_ptr(ptr) ((ptr) + ((ptr) - (const CELTMode**)(ptr)))
64
65#define __celt_check_analysis_ptr(ptr) ((ptr) + ((ptr) - (const AnalysisInfo*)(ptr)))
66
67/* Encoder/decoder Requests */
68
69#define CELT_SET_PREDICTION_REQUEST    10002
70/** Controls the use of interframe prediction.
71    0=Independent frames
72    1=Short term interframe prediction allowed
73    2=Long term prediction allowed
74 */
75#define CELT_SET_PREDICTION(x) CELT_SET_PREDICTION_REQUEST, __opus_check_int(x)
76
77#define CELT_SET_INPUT_CLIPPING_REQUEST    10004
78#define CELT_SET_INPUT_CLIPPING(x) CELT_SET_INPUT_CLIPPING_REQUEST, __opus_check_int(x)
79
80#define CELT_GET_AND_CLEAR_ERROR_REQUEST   10007
81#define CELT_GET_AND_CLEAR_ERROR(x) CELT_GET_AND_CLEAR_ERROR_REQUEST, __opus_check_int_ptr(x)
82
83#define CELT_SET_CHANNELS_REQUEST    10008
84#define CELT_SET_CHANNELS(x) CELT_SET_CHANNELS_REQUEST, __opus_check_int(x)
85
86
87/* Internal */
88#define CELT_SET_START_BAND_REQUEST    10010
89#define CELT_SET_START_BAND(x) CELT_SET_START_BAND_REQUEST, __opus_check_int(x)
90
91#define CELT_SET_END_BAND_REQUEST    10012
92#define CELT_SET_END_BAND(x) CELT_SET_END_BAND_REQUEST, __opus_check_int(x)
93
94#define CELT_GET_MODE_REQUEST    10015
95/** Get the CELTMode used by an encoder or decoder */
96#define CELT_GET_MODE(x) CELT_GET_MODE_REQUEST, __celt_check_mode_ptr_ptr(x)
97
98#define CELT_SET_SIGNALLING_REQUEST    10016
99#define CELT_SET_SIGNALLING(x) CELT_SET_SIGNALLING_REQUEST, __opus_check_int(x)
100
101#define CELT_SET_TONALITY_REQUEST    10018
102#define CELT_SET_TONALITY(x) CELT_SET_TONALITY_REQUEST, __opus_check_int(x)
103#define CELT_SET_TONALITY_SLOPE_REQUEST    10020
104#define CELT_SET_TONALITY_SLOPE(x) CELT_SET_TONALITY_SLOPE_REQUEST, __opus_check_int(x)
105
106#define CELT_SET_ANALYSIS_REQUEST    10022
107#define CELT_SET_ANALYSIS(x) CELT_SET_ANALYSIS_REQUEST, __celt_check_analysis_ptr(x)
108
109#define OPUS_SET_LFE_REQUEST    10024
110#define OPUS_SET_LFE(x) OPUS_SET_LFE_REQUEST, __opus_check_int(x)
111
112#define OPUS_SET_ENERGY_MASK_REQUEST    10026
113#define OPUS_SET_ENERGY_MASK(x) OPUS_SET_ENERGY_MASK_REQUEST, __opus_check_val16_ptr(x)
114
115/* Encoder stuff */
116
117int celt_encoder_get_size(int channels);
118
119int celt_encode_with_ec(OpusCustomEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc);
120
121int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels);
122
123
124
125/* Decoder stuff */
126
127int celt_decoder_get_size(int channels);
128
129
130int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels);
131
132int celt_decode_with_ec(OpusCustomDecoder * OPUS_RESTRICT st, const unsigned char *data, int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec);
133
134#define celt_encoder_ctl opus_custom_encoder_ctl
135#define celt_decoder_ctl opus_custom_decoder_ctl
136
137
138#ifdef CUSTOM_MODES
139#define OPUS_CUSTOM_NOSTATIC
140#else
141#define OPUS_CUSTOM_NOSTATIC static inline
142#endif
143
144static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0};
145/* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */
146static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
147
148static const unsigned char tapset_icdf[3]={2,1,0};
149
150#ifdef CUSTOM_MODES
151static const unsigned char toOpusTable[20] = {
152      0xE0, 0xE8, 0xF0, 0xF8,
153      0xC0, 0xC8, 0xD0, 0xD8,
154      0xA0, 0xA8, 0xB0, 0xB8,
155      0x00, 0x00, 0x00, 0x00,
156      0x80, 0x88, 0x90, 0x98,
157};
158
159static const unsigned char fromOpusTable[16] = {
160      0x80, 0x88, 0x90, 0x98,
161      0x40, 0x48, 0x50, 0x58,
162      0x20, 0x28, 0x30, 0x38,
163      0x00, 0x08, 0x10, 0x18
164};
165
166static inline int toOpus(unsigned char c)
167{
168   int ret=0;
169   if (c<0xA0)
170      ret = toOpusTable[c>>3];
171   if (ret == 0)
172      return -1;
173   else
174      return ret|(c&0x7);
175}
176
177static inline int fromOpus(unsigned char c)
178{
179   if (c<0x80)
180      return -1;
181   else
182      return fromOpusTable[(c>>3)-16] | (c&0x7);
183}
184#endif /* CUSTOM_MODES */
185
186#define COMBFILTER_MAXPERIOD 1024
187#define COMBFILTER_MINPERIOD 15
188
189extern const signed char tf_select_table[4][8];
190
191int resampling_factor(opus_int32 rate);
192
193void preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
194                        int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip);
195
196void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
197      opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
198      const opus_val16 *window, int overlap);
199
200void init_caps(const CELTMode *m,int *cap,int LM,int C);
201
202#ifdef RESYNTH
203void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem, celt_sig * OPUS_RESTRICT scratch);
204
205void compute_inv_mdcts(const CELTMode *mode, int shortBlocks, celt_sig *X,
206      celt_sig * OPUS_RESTRICT out_mem[], int C, int LM);
207#endif
208
209#ifdef __cplusplus
210}
211#endif
212
213#endif /* CELT_H */
214