blob: a423b95046a6f7ea98928b309aee3f24bce2a006 [file] [log] [blame]
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04001/* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2009 Xiph.Org Foundation
Gregory Maxwell71d39ad2011-07-30 00:00:29 -04003 Copyright (c) 2008 Gregory Maxwell
Jean-Marc Valin8b2ff0d2009-10-17 21:40:10 -04004 Written by Jean-Marc Valin and Gregory Maxwell */
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +11005/**
6 @file celt.h
Jean-Marc Valin56522ad2009-06-05 17:17:25 -04007 @brief Contains all the functions for encoding and decoding audio
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +11008 */
9
Jean-Marc Valin013c31d2007-11-30 11:36:46 +110010/*
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions
13 are met:
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040014
Jean-Marc Valin013c31d2007-11-30 11:36:46 +110015 - Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040017
Jean-Marc Valin013c31d2007-11-30 11:36:46 +110018 - 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.
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040021
Jean-Marc Valin013c31d2007-11-30 11:36:46 +110022 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
Jean-Marc Valincb05e7c2012-04-20 16:40:24 -040025 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
26 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Jean-Marc Valin013c31d2007-11-30 11:36:46 +110027 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
Jean-Marc Valind77d6a52011-07-29 17:33:06 -040038#include "opus_types.h"
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -040039#include "opus_defines.h"
Jean-Marc Valin06237d72011-09-01 13:20:40 -040040#include "opus_custom.h"
Jean-Marc Valinfbbd9bf2011-09-05 21:13:53 -040041#include "entenc.h"
42#include "entdec.h"
43#include "arch.h"
Jean-Marc Valin3d3bb682008-01-31 10:05:39 +110044
Jean-Marc Valin77ff83a2008-01-22 13:16:36 +110045#ifdef __cplusplus
46extern "C" {
47#endif
48
Jean-Marc Valin242da532011-09-05 20:53:33 -040049#define CELTEncoder OpusCustomEncoder
50#define CELTDecoder OpusCustomDecoder
51#define CELTMode OpusCustomMode
52
Jean-Marc Valine9c353a2011-11-14 17:58:29 +080053typedef struct {
54 int valid;
Jean-Marc Valin3ab03e02013-09-06 16:00:39 -040055 float tonality;
56 float tonality_slope;
57 float noisiness;
58 float activity;
59 float music_prob;
Jean-Marc Valin7509fdb2012-12-20 22:48:35 -050060 int bandwidth;
Jean-Marc Valine9c353a2011-11-14 17:58:29 +080061}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)))
Jean-Marc Valina76a0b22008-01-17 22:43:05 +110066
Jean-Marc Valinc3086a92011-03-21 13:26:03 -040067/* Encoder/decoder Requests */
Jean-Marc Valin7bb26e12011-02-01 17:04:27 -050068
Jean-Marc Valin51748172013-11-08 19:54:03 -050069/* Expose this option again when variable framesize actually works */
70#define OPUS_FRAMESIZE_VARIABLE 5010 /**< Optimize the frame size dynamically */
71
72
Jean-Marc Valinfbbd9bf2011-09-05 21:13:53 -040073#define CELT_SET_PREDICTION_REQUEST 10002
Gregory Maxwell2dd3d322009-06-05 14:05:51 -040074/** Controls the use of interframe prediction.
75 0=Independent frames
76 1=Short term interframe prediction allowed
77 2=Long term prediction allowed
78 */
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -040079#define CELT_SET_PREDICTION(x) CELT_SET_PREDICTION_REQUEST, __opus_check_int(x)
Jean-Marc Valin7bb26e12011-02-01 17:04:27 -050080
Jean-Marc Valinfbbd9bf2011-09-05 21:13:53 -040081#define CELT_SET_INPUT_CLIPPING_REQUEST 10004
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -040082#define CELT_SET_INPUT_CLIPPING(x) CELT_SET_INPUT_CLIPPING_REQUEST, __opus_check_int(x)
Jean-Marc Valin7bb26e12011-02-01 17:04:27 -050083
Jean-Marc Valinfbbd9bf2011-09-05 21:13:53 -040084#define CELT_GET_AND_CLEAR_ERROR_REQUEST 10007
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -040085#define CELT_GET_AND_CLEAR_ERROR(x) CELT_GET_AND_CLEAR_ERROR_REQUEST, __opus_check_int_ptr(x)
Jean-Marc Valinc18fb1d2008-09-30 07:36:54 -040086
Jean-Marc Valinfbbd9bf2011-09-05 21:13:53 -040087#define CELT_SET_CHANNELS_REQUEST 10008
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -040088#define CELT_SET_CHANNELS(x) CELT_SET_CHANNELS_REQUEST, __opus_check_int(x)
Jean-Marc Valin79b34eb2010-12-05 17:22:06 -050089
Jean-Marc Valin69653882011-04-21 10:41:13 -040090
Jean-Marc Valinef20e392011-03-18 15:34:11 -040091/* Internal */
Jean-Marc Valinfbbd9bf2011-09-05 21:13:53 -040092#define CELT_SET_START_BAND_REQUEST 10010
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -040093#define CELT_SET_START_BAND(x) CELT_SET_START_BAND_REQUEST, __opus_check_int(x)
Jean-Marc Valin5f961462010-05-19 13:38:10 -040094
Jean-Marc Valinfbbd9bf2011-09-05 21:13:53 -040095#define CELT_SET_END_BAND_REQUEST 10012
Jean-Marc Valinf9e701a2011-08-31 17:47:48 -040096#define CELT_SET_END_BAND(x) CELT_SET_END_BAND_REQUEST, __opus_check_int(x)
Jean-Marc Valin525d7cf2010-07-13 14:14:16 -040097
Jean-Marc Valinfbbd9bf2011-09-05 21:13:53 -040098#define CELT_GET_MODE_REQUEST 10015
99/** Get the CELTMode used by an encoder or decoder */
Jean-Marc Valine9c353a2011-11-14 17:58:29 +0800100#define CELT_GET_MODE(x) CELT_GET_MODE_REQUEST, __celt_check_mode_ptr_ptr(x)
Jean-Marc Valin06cee2b2011-08-19 16:11:41 -0400101
Jean-Marc Valinfbbd9bf2011-09-05 21:13:53 -0400102#define CELT_SET_SIGNALLING_REQUEST 10016
103#define CELT_SET_SIGNALLING(x) CELT_SET_SIGNALLING_REQUEST, __opus_check_int(x)
Jean-Marc Valin013c31d2007-11-30 11:36:46 +1100104
Jean-Marc Valin1a2e7652011-11-06 23:27:16 -0500105#define CELT_SET_TONALITY_REQUEST 10018
106#define CELT_SET_TONALITY(x) CELT_SET_TONALITY_REQUEST, __opus_check_int(x)
107#define CELT_SET_TONALITY_SLOPE_REQUEST 10020
108#define CELT_SET_TONALITY_SLOPE(x) CELT_SET_TONALITY_SLOPE_REQUEST, __opus_check_int(x)
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100109
Jean-Marc Valine9c353a2011-11-14 17:58:29 +0800110#define CELT_SET_ANALYSIS_REQUEST 10022
111#define CELT_SET_ANALYSIS(x) CELT_SET_ANALYSIS_REQUEST, __celt_check_analysis_ptr(x)
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100112
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -0400113#define OPUS_SET_LFE_REQUEST 10024
Jean-Marc Valin1b723862013-04-25 21:34:04 -0400114#define OPUS_SET_LFE(x) OPUS_SET_LFE_REQUEST, __opus_check_int(x)
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100115
Jean-Marc Valinfdceae82013-08-30 21:58:02 -0400116#define OPUS_SET_ENERGY_MASK_REQUEST 10026
Jean-Marc Valina4dccd32013-05-04 23:54:20 -0400117#define OPUS_SET_ENERGY_MASK(x) OPUS_SET_ENERGY_MASK_REQUEST, __opus_check_val16_ptr(x)
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100118
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100119/* Encoder stuff */
120
Jean-Marc Valin06237d72011-09-01 13:20:40 -0400121int celt_encoder_get_size(int channels);
Jean-Marc Valin8cf29f02011-01-30 23:38:28 -0500122
Gregory Maxwellde0b5322012-07-18 12:12:35 -0400123int celt_encode_with_ec(OpusCustomEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc);
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100124
Timothy B. Terriberry39386e02013-11-18 13:30:13 -0500125int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels,
126 int arch);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100127
Jean-Marc Valinc97b2582011-01-28 23:07:32 -0500128
Jean-Marc Valinc18fb1d2008-09-30 07:36:54 -0400129
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100130/* Decoder stuff */
131
Jean-Marc Valin06237d72011-09-01 13:20:40 -0400132int celt_decoder_get_size(int channels);
Jean-Marc Valin8cf29f02011-01-30 23:38:28 -0500133
Jean-Marc Valin879fbfd2008-02-20 17:17:13 +1100134
Gregory Maxwell64a35412011-09-02 10:31:17 -0400135int celt_decoder_init(CELTDecoder *st, opus_int32 sampling_rate, int channels);
Jean-Marc Valin8c19b032008-02-20 15:53:15 +1100136
Jean-Marc Valin4d07b132014-01-06 17:43:20 -0500137int celt_decode_with_ec(OpusCustomDecoder * OPUS_RESTRICT st, const unsigned char *data,
138 int len, opus_val16 * OPUS_RESTRICT pcm, int frame_size, ec_dec *dec, int accum);
Jean-Marc Valina3803a92009-10-24 11:24:34 -0400139
Jean-Marc Valin06237d72011-09-01 13:20:40 -0400140#define celt_encoder_ctl opus_custom_encoder_ctl
141#define celt_decoder_ctl opus_custom_decoder_ctl
142
Jean-Marc Valin69062102012-11-08 09:42:27 -0500143
Jean-Marc Valin69062102012-11-08 09:42:27 -0500144#ifdef CUSTOM_MODES
145#define OPUS_CUSTOM_NOSTATIC
146#else
Gregory Maxwell7830cf12013-10-17 15:56:52 -0700147#define OPUS_CUSTOM_NOSTATIC static OPUS_INLINE
Jean-Marc Valin69062102012-11-08 09:42:27 -0500148#endif
149
150static const unsigned char trim_icdf[11] = {126, 124, 119, 109, 87, 41, 19, 9, 4, 2, 0};
151/* Probs: NONE: 21.875%, LIGHT: 6.25%, NORMAL: 65.625%, AGGRESSIVE: 6.25% */
152static const unsigned char spread_icdf[4] = {25, 23, 2, 0};
153
154static const unsigned char tapset_icdf[3]={2,1,0};
155
156#ifdef CUSTOM_MODES
157static const unsigned char toOpusTable[20] = {
158 0xE0, 0xE8, 0xF0, 0xF8,
159 0xC0, 0xC8, 0xD0, 0xD8,
160 0xA0, 0xA8, 0xB0, 0xB8,
161 0x00, 0x00, 0x00, 0x00,
162 0x80, 0x88, 0x90, 0x98,
163};
164
165static const unsigned char fromOpusTable[16] = {
166 0x80, 0x88, 0x90, 0x98,
167 0x40, 0x48, 0x50, 0x58,
168 0x20, 0x28, 0x30, 0x38,
169 0x00, 0x08, 0x10, 0x18
170};
171
Gregory Maxwell7830cf12013-10-17 15:56:52 -0700172static OPUS_INLINE int toOpus(unsigned char c)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500173{
174 int ret=0;
175 if (c<0xA0)
176 ret = toOpusTable[c>>3];
177 if (ret == 0)
178 return -1;
179 else
180 return ret|(c&0x7);
181}
182
Gregory Maxwell7830cf12013-10-17 15:56:52 -0700183static OPUS_INLINE int fromOpus(unsigned char c)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500184{
185 if (c<0x80)
186 return -1;
187 else
188 return fromOpusTable[(c>>3)-16] | (c&0x7);
189}
190#endif /* CUSTOM_MODES */
191
192#define COMBFILTER_MAXPERIOD 1024
193#define COMBFILTER_MINPERIOD 15
194
195extern const signed char tf_select_table[4][8];
196
197int resampling_factor(opus_int32 rate);
198
Jean-Marc Valin2dc27df2013-11-13 19:35:43 -0500199void celt_preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
Jean-Marc Valinfdceae82013-08-30 21:58:02 -0400200 int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip);
201
Jean-Marc Valin69062102012-11-08 09:42:27 -0500202void comb_filter(opus_val32 *y, opus_val32 *x, int T0, int T1, int N,
203 opus_val16 g0, opus_val16 g1, int tapset0, int tapset1,
Jonathan Lennox43120f02015-08-03 17:04:27 -0400204 const opus_val16 *window, int overlap, int arch);
205
206#ifdef NON_STATIC_COMB_FILTER_CONST_C
207void comb_filter_const_c(opus_val32 *y, opus_val32 *x, int T, int N,
208 opus_val16 g10, opus_val16 g11, opus_val16 g12);
209#endif
210
211#ifndef OVERRIDE_COMB_FILTER_CONST
212# define comb_filter_const(y, x, T, N, g10, g11, g12, arch) \
213 ((void)(arch),comb_filter_const_c(y, x, T, N, g10, g11, g12))
214#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -0500215
216void init_caps(const CELTMode *m,int *cap,int LM,int C);
217
218#ifdef RESYNTH
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -0500219void deemphasis(celt_sig *in[], opus_val16 *pcm, int N, int C, int downsample, const opus_val16 *coef, celt_sig *mem);
220void celt_synthesis(const CELTMode *mode, celt_norm *X, celt_sig * out_syn[],
221 opus_val16 *oldBandE, int start, int effEnd, int C, int CC, int isTransient,
222 int LM, int downsample, int silence);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500223#endif
224
Jean-Marc Valin77ff83a2008-01-22 13:16:36 +1100225#ifdef __cplusplus
226}
227#endif
228
Ralph Giles120800f2011-11-25 13:02:00 -0800229#endif /* CELT_H */