FFmpeg
opus_celt.h
Go to the documentation of this file.
1 /*
2  * Opus decoder/encoder CELT 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 <stdint.h>
28 
29 #include "avcodec.h"
30 #include "opusdsp.h"
31 #include "opus_rc.h"
32 
33 #include "libavutil/float_dsp.h"
34 #include "libavutil/libm.h"
35 #include "libavutil/mem_internal.h"
36 #include "libavutil/tx.h"
37 
38 #define CELT_SHORT_BLOCKSIZE 120
39 #define CELT_OVERLAP CELT_SHORT_BLOCKSIZE
40 #define CELT_MAX_LOG_BLOCKS 3
41 #define CELT_MAX_FRAME_SIZE (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS))
42 #define CELT_MAX_BANDS 21
43 
44 #define CELT_VECTORS 11
45 #define CELT_ALLOC_STEPS 6
46 #define CELT_FINE_OFFSET 21
47 #define CELT_MAX_FINE_BITS 8
48 #define CELT_NORM_SCALE 16384
49 #define CELT_QTHETA_OFFSET 4
50 #define CELT_QTHETA_OFFSET_TWOPHASE 16
51 #define CELT_POSTFILTER_MINPERIOD 15
52 #define CELT_ENERGY_SILENCE (-28.0f)
53 
54 enum CeltSpread {
59 };
60 
66 
68 };
69 
70 typedef struct CeltBlock {
75 
77 
78  /* buffer for mdct output + postfilter */
79  DECLARE_ALIGNED(32, float, buf)[2048];
81 
82  /* Used by the encoder */
85 
86  /* postfilter parameters */
88  float pf_gains_new[3];
89  int pf_period;
90  float pf_gains[3];
92  float pf_gains_old[3];
93 
94  float emph_coeff;
95 } CeltBlock;
96 
97 typedef struct CeltFrame {
98  // constant values that do not change during context lifetime
104  struct CeltPVQ *pvq;
106  int channels;
109 
112  int end_band;
114  int transient;
115  int pfilter;
120  int blocks; /* number of iMDCT blocks in the frame, depends on transient */
121  int blocksize; /* size of each block */
122  int silence; /* Frame is filled with silence */
123  int anticollapse_needed; /* Whether to expect an anticollapse bit */
124  int anticollapse; /* Encoded anticollapse bit */
127  int flushed;
128  uint32_t seed;
130 
131  /* Encoder PF coeffs */
135  float pf_gain;
136 
137  /* Bit allocation */
146 } CeltFrame;
147 
148 /* LCG for noise generation */
150 {
151  f->seed = 1664525 * f->seed + 1013904223;
152  return f->seed;
153 }
154 
155 static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
156 {
157  int i;
158  float g = 1e-15f;
159  for (i = 0; i < N; i++)
160  g += X[i] * X[i];
161  g = gain / sqrtf(g);
162 
163  for (i = 0; i < N; i++)
164  X[i] *= g;
165 }
166 
167 int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
168  int apply_phase_inv);
169 
170 void ff_celt_free(CeltFrame **f);
171 
172 void ff_celt_flush(CeltFrame *f);
173 
175  int coded_channels, int frame_size, int startband, int endband);
176 
177 /* Encode or decode CELT bands */
179 
180 /* Encode or decode CELT bitallocation */
182 
183 #endif /* AVCODEC_OPUS_CELT_H */
CeltFrame::dsp
AVFloatDSPContext * dsp
Definition: opus_celt.h:102
CeltFrame::tx_fn
av_tx_fn tx_fn[4]
Definition: opus_celt.h:101
libm.h
CeltBlock::buf
float buf[2048]
Definition: opus_celt.h:79
mem_internal.h
CeltFrame::size
enum CeltBlockSize size
Definition: opus_celt.h:110
AVTXContext
Definition: tx_priv.h:228
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
CeltFrame::spread
enum CeltSpread spread
Definition: opus_celt.h:129
opusdsp.h
CeltFrame::start_band
int start_band
Definition: opus_celt.h:111
CELT_SPREAD_NONE
@ CELT_SPREAD_NONE
Definition: opus_celt.h:55
CeltFrame::avctx
AVCodecContext * avctx
Definition: opus_celt.h:99
CeltBlock::overlap
float overlap[FFALIGN(CELT_OVERLAP, 16)]
Definition: opus_celt.h:83
CeltBlock::lin_energy
float lin_energy[CELT_MAX_BANDS]
Definition: opus_celt.h:72
CELT_SPREAD_LIGHT
@ CELT_SPREAD_LIGHT
Definition: opus_celt.h:56
CeltFrame::framebits
int framebits
Definition: opus_celt.h:138
CELT_BLOCK_960
@ CELT_BLOCK_960
Definition: opus_celt.h:65
CeltFrame::coded_bands
int coded_bands
Definition: opus_celt.h:113
CeltBlock::coeffs
float coeffs[CELT_MAX_FRAME_SIZE]
Definition: opus_celt.h:80
CeltBlock
Definition: opus_celt.h:70
CeltBlockSize
CeltBlockSize
Definition: opus_celt.h:61
ff_celt_flush
void ff_celt_flush(CeltFrame *f)
Definition: opusdec_celt.c:494
ff_celt_decode_frame
int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output, int coded_channels, int frame_size, int startband, int endband)
Definition: opusdec_celt.c:322
X
@ X
Definition: vf_addroi.c:26
CeltFrame::blocksize
int blocksize
Definition: opus_celt.h:121
CeltPVQ
Definition: opus_pvq.h:37
CeltFrame::output_channels
int output_channels
Definition: opus_celt.h:107
ff_celt_quant_bands
void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc)
Definition: opus_celt.c:28
CELT_BLOCK_480
@ CELT_BLOCK_480
Definition: opus_celt.h:64
CeltBlock::pf_gains_new
float pf_gains_new[3]
Definition: opus_celt.h:88
ff_celt_bitalloc
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode)
Definition: opus_celt.c:137
CeltFrame::anticollapse
int anticollapse
Definition: opus_celt.h:124
CELT_BLOCK_120
@ CELT_BLOCK_120
Definition: opus_celt.h:62
celt_renormalize_vector
static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
Definition: opus_celt.h:155
CeltFrame::apply_phase_inv
int apply_phase_inv
Definition: opus_celt.h:108
CeltBlock::samples
float samples[FFALIGN(CELT_MAX_FRAME_SIZE, 16)]
Definition: opus_celt.h:84
av_tx_fn
void(* av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride)
Function pointer to a function to perform the transform.
Definition: tx.h:127
CELT_BLOCK_240
@ CELT_BLOCK_240
Definition: opus_celt.h:63
CeltFrame::remaining2
int remaining2
Definition: opus_celt.h:140
CeltFrame::pf_gain
float pf_gain
Definition: opus_celt.h:135
CeltBlock::energy
float energy[CELT_MAX_BANDS]
Definition: opus_celt.h:71
CeltFrame::skip_band_floor
int skip_band_floor
Definition: opus_celt.h:116
g
const char * g
Definition: vf_curves.c:127
frame_size
int frame_size
Definition: mxfenc.c:2202
CELT_SPREAD_NORMAL
@ CELT_SPREAD_NORMAL
Definition: opus_celt.h:57
CeltFrame::seed
uint32_t seed
Definition: opus_celt.h:128
CeltFrame::silence
int silence
Definition: opus_celt.h:122
CELT_MAX_FRAME_SIZE
#define CELT_MAX_FRAME_SIZE
Definition: opus_celt.h:41
CeltFrame::alloc_boost
int alloc_boost[CELT_MAX_BANDS]
Definition: opus_celt.h:119
CeltBlock::error_energy
float error_energy[CELT_MAX_BANDS]
Definition: opus_celt.h:73
CeltFrame::dual_stereo
int dual_stereo
Definition: opus_celt.h:126
CeltFrame::blocks
int blocks
Definition: opus_celt.h:120
CeltFrame::tf_change
int tf_change[CELT_MAX_BANDS]
Definition: opus_celt.h:145
CeltFrame::caps
int caps[CELT_MAX_BANDS]
Definition: opus_celt.h:141
CeltFrame::fine_bits
int fine_bits[CELT_MAX_BANDS]
Definition: opus_celt.h:142
sqrtf
static __device__ float sqrtf(float a)
Definition: cuda_runtime.h:184
CeltBlock::prev_energy
float prev_energy[2][CELT_MAX_BANDS]
Definition: opus_celt.h:74
CeltFrame::tx
AVTXContext * tx[4]
Definition: opus_celt.h:100
float_dsp.h
CeltBlock::pf_gains
float pf_gains[3]
Definition: opus_celt.h:90
CELT_OVERLAP
#define CELT_OVERLAP
Definition: opus_celt.h:39
CELT_MAX_BANDS
#define CELT_MAX_BANDS
Definition: opus_celt.h:42
f
f
Definition: af_crystalizer.c:122
CeltFrame::fine_priority
int fine_priority[CELT_MAX_BANDS]
Definition: opus_celt.h:143
OpusRangeCoder
Definition: opus_rc.h:39
CeltBlock::pf_period
int pf_period
Definition: opus_celt.h:89
AVFloatDSPContext
Definition: float_dsp.h:24
celt_rng
static av_always_inline uint32_t celt_rng(CeltFrame *f)
Definition: opus_celt.h:149
encode
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt, FILE *output)
Definition: encode_audio.c:94
N
#define N
Definition: af_mcompand.c:53
CeltFrame::channels
int channels
Definition: opus_celt.h:106
DECLARE_ALIGNED
#define DECLARE_ALIGNED(n, t, v)
Definition: mem.h:116
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
CeltFrame::pvq
struct CeltPVQ * pvq
Definition: opus_celt.h:104
CeltFrame::pulses
int pulses[CELT_MAX_BANDS]
Definition: opus_celt.h:144
CeltFrame::end_band
int end_band
Definition: opus_celt.h:112
CeltFrame::intensity_stereo
int intensity_stereo
Definition: opus_celt.h:125
av_always_inline
#define av_always_inline
Definition: attributes.h:49
avcodec.h
CeltFrame::remaining
int remaining
Definition: opus_celt.h:139
CELT_SPREAD_AGGRESSIVE
@ CELT_SPREAD_AGGRESSIVE
Definition: opus_celt.h:58
CeltFrame::anticollapse_needed
int anticollapse_needed
Definition: opus_celt.h:123
AVCodecContext
main external API structure.
Definition: avcodec.h:398
CeltBlock::collapse_masks
uint8_t collapse_masks[CELT_MAX_BANDS]
Definition: opus_celt.h:76
CeltFrame::opusdsp
OpusDSP opusdsp
Definition: opus_celt.h:105
CeltFrame::pfilter
int pfilter
Definition: opus_celt.h:115
CeltSpread
CeltSpread
Definition: opus_celt.h:54
CeltFrame::pf_period
int pf_period
Definition: opus_celt.h:133
CeltBlock::pf_period_old
int pf_period_old
Definition: opus_celt.h:91
CeltBlock::emph_coeff
float emph_coeff
Definition: opus_celt.h:94
CeltFrame::flushed
int flushed
Definition: opus_celt.h:127
FFALIGN
#define FFALIGN(x, a)
Definition: macros.h:78
CELT_BLOCK_NB
@ CELT_BLOCK_NB
Definition: opus_celt.h:67
ff_celt_free
void ff_celt_free(CeltFrame **f)
Definition: opusdec_celt.c:525
opus_rc.h
CeltBlock::pf_gains_old
float pf_gains_old[3]
Definition: opus_celt.h:92
CeltFrame::pf_octave
int pf_octave
Definition: opus_celt.h:132
CeltFrame::alloc_trim
int alloc_trim
Definition: opus_celt.h:118
CeltFrame::block
CeltBlock block[2]
Definition: opus_celt.h:103
CeltBlock::pf_period_new
int pf_period_new
Definition: opus_celt.h:87
ff_celt_init
int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels, int apply_phase_inv)
Definition: opusdec_celt.c:542
tx.h
CeltFrame::tf_select
int tf_select
Definition: opus_celt.h:117
CeltFrame
Definition: opus_celt.h:97
CeltFrame::pf_tapset
int pf_tapset
Definition: opus_celt.h:134
OpusDSP
Definition: opusdsp.h:24