]> git.sesse.net Git - ffmpeg/blob - libavcodec/opus_celt.h
avformat/mpegtsenc: reindent the last commit
[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 #include "opus_pvq.h"
31 #include "opusdsp.h"
32
33 #include "mdct15.h"
34 #include "libavutil/float_dsp.h"
35 #include "libavutil/libm.h"
36
37 #define CELT_VECTORS                 11
38 #define CELT_ALLOC_STEPS             6
39 #define CELT_FINE_OFFSET             21
40 #define CELT_MAX_FINE_BITS           8
41 #define CELT_NORM_SCALE              16384
42 #define CELT_QTHETA_OFFSET           4
43 #define CELT_QTHETA_OFFSET_TWOPHASE  16
44 #define CELT_POSTFILTER_MINPERIOD    15
45 #define CELT_ENERGY_SILENCE          (-28.0f)
46
47 typedef struct CeltPVQ CeltPVQ;
48
49 enum CeltSpread {
50     CELT_SPREAD_NONE,
51     CELT_SPREAD_LIGHT,
52     CELT_SPREAD_NORMAL,
53     CELT_SPREAD_AGGRESSIVE
54 };
55
56 enum CeltBlockSize {
57     CELT_BLOCK_120,
58     CELT_BLOCK_240,
59     CELT_BLOCK_480,
60     CELT_BLOCK_960,
61
62     CELT_BLOCK_NB
63 };
64
65 typedef struct CeltBlock {
66     float energy[CELT_MAX_BANDS];
67     float lin_energy[CELT_MAX_BANDS];
68     float error_energy[CELT_MAX_BANDS];
69     float prev_energy[2][CELT_MAX_BANDS];
70
71     uint8_t collapse_masks[CELT_MAX_BANDS];
72
73     /* buffer for mdct output + postfilter */
74     DECLARE_ALIGNED(32, float, buf)[2048];
75     DECLARE_ALIGNED(32, float, coeffs)[CELT_MAX_FRAME_SIZE];
76
77     /* Used by the encoder */
78     DECLARE_ALIGNED(32, float, overlap)[FFALIGN(CELT_OVERLAP, 16)];
79     DECLARE_ALIGNED(32, float, samples)[FFALIGN(CELT_MAX_FRAME_SIZE, 16)];
80
81     /* postfilter parameters */
82     int   pf_period_new;
83     float pf_gains_new[3];
84     int   pf_period;
85     float pf_gains[3];
86     int   pf_period_old;
87     float pf_gains_old[3];
88
89     float emph_coeff;
90 } CeltBlock;
91
92 struct CeltFrame {
93     // constant values that do not change during context lifetime
94     AVCodecContext      *avctx;
95     MDCT15Context       *imdct[4];
96     AVFloatDSPContext   *dsp;
97     CeltBlock           block[2];
98     CeltPVQ             *pvq;
99     OpusDSP             opusdsp;
100     int channels;
101     int output_channels;
102     int apply_phase_inv;
103
104     enum CeltBlockSize size;
105     int start_band;
106     int end_band;
107     int coded_bands;
108     int transient;
109     int pfilter;
110     int skip_band_floor;
111     int tf_select;
112     int alloc_trim;
113     int alloc_boost[CELT_MAX_BANDS];
114     int blocks;        /* number of iMDCT blocks in the frame, depends on transient */
115     int blocksize;     /* size of each block */
116     int silence;       /* Frame is filled with silence */
117     int anticollapse_needed; /* Whether to expect an anticollapse bit */
118     int anticollapse;  /* Encoded anticollapse bit */
119     int intensity_stereo;
120     int dual_stereo;
121     int flushed;
122     uint32_t seed;
123     enum CeltSpread spread;
124
125     /* Encoder PF coeffs */
126     int pf_octave;
127     int pf_period;
128     int pf_tapset;
129     float pf_gain;
130
131     /* Bit allocation */
132     int framebits;
133     int remaining;
134     int remaining2;
135     int caps         [CELT_MAX_BANDS];
136     int fine_bits    [CELT_MAX_BANDS];
137     int fine_priority[CELT_MAX_BANDS];
138     int pulses       [CELT_MAX_BANDS];
139     int tf_change    [CELT_MAX_BANDS];
140 };
141
142 /* LCG for noise generation */
143 static av_always_inline uint32_t celt_rng(CeltFrame *f)
144 {
145     f->seed = 1664525 * f->seed + 1013904223;
146     return f->seed;
147 }
148
149 static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
150 {
151     int i;
152     float g = 1e-15f;
153     for (i = 0; i < N; i++)
154         g += X[i] * X[i];
155     g = gain / sqrtf(g);
156
157     for (i = 0; i < N; i++)
158         X[i] *= g;
159 }
160
161 int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
162                  int apply_phase_inv);
163
164 void ff_celt_free(CeltFrame **f);
165
166 void ff_celt_flush(CeltFrame *f);
167
168 int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output,
169                          int coded_channels, int frame_size, int startband, int endband);
170
171 #endif /* AVCODEC_OPUS_CELT_H */