]> git.sesse.net Git - ffmpeg/blob - libavcodec/opus_celt.h
Merge commit '8dfba25ce89b62c80ba83e2116d549176c376144'
[ffmpeg] / libavcodec / opus_celt.h
1 /*
2  * Opus decoder/demuxer common functions
3  * Copyright (c) 2012 Andrew D'Addesio
4  * Copyright (c) 2013-2014 Mozilla Corporation
5  * Copyright (c) 2016 Rostislav Pehlivanov <atomnuker@gmail.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #ifndef AVCODEC_OPUS_CELT_H
25 #define AVCODEC_OPUS_CELT_H
26
27 #include <float.h>
28
29 #include "opus.h"
30
31 #include "mdct15.h"
32 #include "libavutil/float_dsp.h"
33 #include "libavutil/libm.h"
34
35 #define CELT_VECTORS                 11
36 #define CELT_ALLOC_STEPS             6
37 #define CELT_FINE_OFFSET             21
38 #define CELT_MAX_FINE_BITS           8
39 #define CELT_NORM_SCALE              16384
40 #define CELT_QTHETA_OFFSET           4
41 #define CELT_QTHETA_OFFSET_TWOPHASE  16
42 #define CELT_EMPH_COEFF              0.85000610f
43 #define CELT_POSTFILTER_MINPERIOD    15
44 #define CELT_ENERGY_SILENCE          (-28.0f)
45
46 enum CeltSpread {
47     CELT_SPREAD_NONE,
48     CELT_SPREAD_LIGHT,
49     CELT_SPREAD_NORMAL,
50     CELT_SPREAD_AGGRESSIVE
51 };
52
53 enum CeltBlockSize {
54     CELT_BLOCK_120,
55     CELT_BLOCK_240,
56     CELT_BLOCK_480,
57     CELT_BLOCK_960,
58
59     CELT_BLOCK_NB
60 };
61
62 typedef struct CeltBlock {
63     float energy[CELT_MAX_BANDS];
64     float lin_energy[CELT_MAX_BANDS];
65     float error_energy[CELT_MAX_BANDS];
66     float prev_energy[2][CELT_MAX_BANDS];
67
68     uint8_t collapse_masks[CELT_MAX_BANDS];
69
70     /* buffer for mdct output + postfilter */
71     DECLARE_ALIGNED(32, float, buf)[2048];
72     DECLARE_ALIGNED(32, float, coeffs)[CELT_MAX_FRAME_SIZE];
73
74     /* Used by the encoder */
75     DECLARE_ALIGNED(32, float, overlap)[120];
76     DECLARE_ALIGNED(32, float, samples)[CELT_MAX_FRAME_SIZE];
77
78     /* postfilter parameters */
79     int   pf_period_new;
80     float pf_gains_new[3];
81     int   pf_period;
82     float pf_gains[3];
83     int   pf_period_old;
84     float pf_gains_old[3];
85
86     float emph_coeff;
87 } CeltBlock;
88
89 struct CeltFrame {
90     // constant values that do not change during context lifetime
91     AVCodecContext      *avctx;
92     MDCT15Context       *imdct[4];
93     AVFloatDSPContext   *dsp;
94     CeltBlock           block[2];
95     int channels;
96     int output_channels;
97
98     enum CeltBlockSize size;
99     int start_band;
100     int end_band;
101     int coded_bands;
102     int transient;
103     int pfilter;
104     int skip_band_floor;
105     int tf_select;
106     int alloc_trim;
107     int alloc_boost[CELT_MAX_BANDS];
108     int blocks;        /* number of iMDCT blocks in the frame, depends on transient */
109     int blocksize;     /* size of each block */
110     int silence;       /* Frame is filled with silence */
111     int anticollapse_needed; /* Whether to expect an anticollapse bit */
112     int anticollapse;  /* Encoded anticollapse bit */
113     int intensity_stereo;
114     int dual_stereo;
115     int flushed;
116     uint32_t seed;
117     enum CeltSpread spread;
118
119     /* Bit allocation */
120     int framebits;
121     int remaining;
122     int remaining2;
123     int caps         [CELT_MAX_BANDS];
124     int fine_bits    [CELT_MAX_BANDS];
125     int fine_priority[CELT_MAX_BANDS];
126     int pulses       [CELT_MAX_BANDS];
127     int tf_change    [CELT_MAX_BANDS];
128
129     DECLARE_ALIGNED(32, float, scratch)[22 * 8]; // MAX(ff_celt_freq_range) * 1<<CELT_MAX_LOG_BLOCKS
130 };
131
132 /* LCG for noise generation */
133 static av_always_inline uint32_t celt_rng(CeltFrame *f)
134 {
135     f->seed = 1664525 * f->seed + 1013904223;
136     return f->seed;
137 }
138
139 static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
140 {
141     int i;
142     float g = 1e-15f;
143     for (i = 0; i < N; i++)
144         g += X[i] * X[i];
145     g = gain / sqrtf(g);
146
147     for (i = 0; i < N; i++)
148         X[i] *= g;
149 }
150
151 int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels);
152
153 void ff_celt_free(CeltFrame **f);
154
155 void ff_celt_flush(CeltFrame *f);
156
157 int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output,
158                          int coded_channels, int frame_size, int startband, int endband);
159
160 #endif /* AVCODEC_OPUS_CELT_H */