Coverage Report

Created: 2025-11-16 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/opus/celt.h
Line
Count
Source
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 "libavcodec/avcodec.h"
30
31
#include "dsp.h"
32
#include "rc.h"
33
34
#include "libavutil/float_dsp.h"
35
#include "libavutil/libm.h"
36
#include "libavutil/mem_internal.h"
37
#include "libavutil/tx.h"
38
39
16.9M
#define CELT_SHORT_BLOCKSIZE         120
40
16.8M
#define CELT_OVERLAP                 CELT_SHORT_BLOCKSIZE
41
206k
#define CELT_MAX_LOG_BLOCKS          3
42
#define CELT_MAX_FRAME_SIZE          (CELT_SHORT_BLOCKSIZE * (1 << CELT_MAX_LOG_BLOCKS))
43
16.7M
#define CELT_MAX_BANDS               21
44
45
1.81M
#define CELT_VECTORS                 11
46
12.8M
#define CELT_ALLOC_STEPS             6
47
714k
#define CELT_FINE_OFFSET             21
48
522k
#define CELT_MAX_FINE_BITS           8
49
#define CELT_NORM_SCALE              16384
50
1.19M
#define CELT_QTHETA_OFFSET           4
51
123k
#define CELT_QTHETA_OFFSET_TWOPHASE  16
52
#define CELT_POSTFILTER_MINPERIOD    15
53
6.68M
#define CELT_ENERGY_SILENCE          (-28.0f)
54
55
enum CeltSpread {
56
    CELT_SPREAD_NONE,
57
    CELT_SPREAD_LIGHT,
58
    CELT_SPREAD_NORMAL,
59
    CELT_SPREAD_AGGRESSIVE
60
};
61
62
enum CeltBlockSize {
63
    CELT_BLOCK_120,
64
    CELT_BLOCK_240,
65
    CELT_BLOCK_480,
66
    CELT_BLOCK_960,
67
68
    CELT_BLOCK_NB
69
};
70
71
typedef struct CeltBlock {
72
    float energy[CELT_MAX_BANDS];
73
    float lin_energy[CELT_MAX_BANDS];
74
    float error_energy[CELT_MAX_BANDS];
75
    float prev_energy[2][CELT_MAX_BANDS];
76
77
    uint8_t collapse_masks[CELT_MAX_BANDS];
78
79
    /* buffer for mdct output + postfilter */
80
    DECLARE_ALIGNED(32, float, buf)[2048];
81
    DECLARE_ALIGNED(32, float, coeffs)[CELT_MAX_FRAME_SIZE];
82
83
    /* Used by the encoder */
84
    DECLARE_ALIGNED(32, float, overlap)[FFALIGN(CELT_OVERLAP, 16)];
85
    DECLARE_ALIGNED(32, float, samples)[FFALIGN(CELT_MAX_FRAME_SIZE, 16)];
86
87
    /* postfilter parameters */
88
    int   pf_period_new;
89
    float pf_gains_new[3];
90
    int   pf_period;
91
    float pf_gains[3];
92
    int   pf_period_old;
93
    float pf_gains_old[3];
94
95
    float emph_coeff;
96
} CeltBlock;
97
98
typedef struct CeltFrame {
99
    // constant values that do not change during context lifetime
100
    AVCodecContext      *avctx;
101
    AVTXContext        *tx[4];
102
    av_tx_fn            tx_fn[4];
103
    AVFloatDSPContext   *dsp;
104
    CeltBlock           block[2];
105
    struct CeltPVQ      *pvq;
106
    OpusDSP             opusdsp;
107
    int channels;
108
    int output_channels;
109
    int apply_phase_inv;
110
111
    enum CeltBlockSize size;
112
    int start_band;
113
    int end_band;
114
    int coded_bands;
115
    int transient;
116
    int pfilter;
117
    int skip_band_floor;
118
    int tf_select;
119
    int alloc_trim;
120
    int alloc_boost[CELT_MAX_BANDS];
121
    int blocks;        /* number of iMDCT blocks in the frame, depends on transient */
122
    int blocksize;     /* size of each block */
123
    int silence;       /* Frame is filled with silence */
124
    int anticollapse_needed; /* Whether to expect an anticollapse bit */
125
    int anticollapse;  /* Encoded anticollapse bit */
126
    int intensity_stereo;
127
    int dual_stereo;
128
    int flushed;
129
    uint32_t seed;
130
    enum CeltSpread spread;
131
132
    /* Encoder PF coeffs */
133
    int pf_octave;
134
    int pf_period;
135
    int pf_tapset;
136
    float pf_gain;
137
138
    /* Bit allocation */
139
    int framebits;
140
    int remaining;
141
    int remaining2;
142
    int caps         [CELT_MAX_BANDS];
143
    int fine_bits    [CELT_MAX_BANDS];
144
    int fine_priority[CELT_MAX_BANDS];
145
    int pulses       [CELT_MAX_BANDS];
146
    int tf_change    [CELT_MAX_BANDS];
147
} CeltFrame;
148
149
/* LCG for noise generation */
150
static av_always_inline uint32_t celt_rng(CeltFrame *f)
151
29.6M
{
152
29.6M
    f->seed = 1664525 * f->seed + 1013904223;
153
29.6M
    return f->seed;
154
29.6M
}
Unexecuted instantiation: enc.c:celt_rng
Unexecuted instantiation: enc_psy.c:celt_rng
pvq.c:celt_rng
Line
Count
Source
151
29.2M
{
152
29.2M
    f->seed = 1664525 * f->seed + 1013904223;
153
29.2M
    return f->seed;
154
29.2M
}
Unexecuted instantiation: celt_pvq_init.c:celt_rng
Unexecuted instantiation: celt.c:celt_rng
Unexecuted instantiation: dec.c:celt_rng
dec_celt.c:celt_rng
Line
Count
Source
151
404k
{
152
404k
    f->seed = 1664525 * f->seed + 1013904223;
153
404k
    return f->seed;
154
404k
}
155
156
static av_always_inline void celt_renormalize_vector(float *X, int N, float gain)
157
1.12M
{
158
1.12M
    int i;
159
1.12M
    float g = 1e-15f;
160
31.1M
    for (i = 0; i < N; i++)
161
30.0M
        g += X[i] * X[i];
162
1.12M
    g = gain / sqrtf(g);
163
164
31.1M
    for (i = 0; i < N; i++)
165
30.0M
        X[i] *= g;
166
1.12M
}
Unexecuted instantiation: enc.c:celt_renormalize_vector
Unexecuted instantiation: enc_psy.c:celt_renormalize_vector
pvq.c:celt_renormalize_vector
Line
Count
Source
157
1.08M
{
158
1.08M
    int i;
159
1.08M
    float g = 1e-15f;
160
30.2M
    for (i = 0; i < N; i++)
161
29.2M
        g += X[i] * X[i];
162
1.08M
    g = gain / sqrtf(g);
163
164
30.2M
    for (i = 0; i < N; i++)
165
29.2M
        X[i] *= g;
166
1.08M
}
Unexecuted instantiation: celt_pvq_init.c:celt_renormalize_vector
Unexecuted instantiation: celt.c:celt_renormalize_vector
Unexecuted instantiation: dec.c:celt_renormalize_vector
dec_celt.c:celt_renormalize_vector
Line
Count
Source
157
36.1k
{
158
36.1k
    int i;
159
36.1k
    float g = 1e-15f;
160
845k
    for (i = 0; i < N; i++)
161
809k
        g += X[i] * X[i];
162
36.1k
    g = gain / sqrtf(g);
163
164
845k
    for (i = 0; i < N; i++)
165
809k
        X[i] *= g;
166
36.1k
}
167
168
int ff_celt_init(AVCodecContext *avctx, CeltFrame **f, int output_channels,
169
                 int apply_phase_inv);
170
171
void ff_celt_free(CeltFrame **f);
172
173
void ff_celt_flush(CeltFrame *f);
174
175
int ff_celt_decode_frame(CeltFrame *f, OpusRangeCoder *rc, float **output,
176
                         int coded_channels, int frame_size, int startband, int endband);
177
178
/* Encode or decode CELT bands */
179
void ff_celt_quant_bands(CeltFrame *f, OpusRangeCoder *rc);
180
181
/* Encode or decode CELT bitallocation */
182
void ff_celt_bitalloc(CeltFrame *f, OpusRangeCoder *rc, int encode);
183
184
#endif /* AVCODEC_OPUS_CELT_H */