aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/opusenc_psy.h
blob: 67e96b2fa5e358434ec9ea3bc7a43d78116f3cb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Opus encoder
 * Copyright (c) 2017 Rostislav Pehlivanov <atomnuker@gmail.com>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef AVCODEC_OPUSENC_PSY_H
#define AVCODEC_OPUSENC_PSY_H

#include "libavutil/tx.h"
#include "libavutil/mem_internal.h"

#include "opusenc.h"
#include "opus_celt.h"
#include "opusenc_utils.h"

/* Each step is 2.5ms */
typedef struct OpusPsyStep {
    int   index; /* Current index */
    int   silence;
    float energy[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]; /* Masking effects included */
    float tone[OPUS_MAX_CHANNELS][CELT_MAX_BANDS];   /* Tonality */
    float stereo[CELT_MAX_BANDS];                    /* IS/MS compatibility */
    float change_amp[OPUS_MAX_CHANNELS][CELT_MAX_BANDS]; /* Jump over last frame */
    float total_change; /* Total change */

    float *bands[OPUS_MAX_CHANNELS][CELT_MAX_BANDS];
    float coeffs[OPUS_MAX_CHANNELS][OPUS_BLOCK_SIZE(CELT_BLOCK_960)];
} OpusPsyStep;

typedef struct OpusBandExcitation {
    float excitation;
    float excitation_dist;
    float excitation_init;
} OpusBandExcitation;

typedef struct PsyChain {
    int start;
    int end;
} PsyChain;

typedef struct OpusPsyContext {
    AVCodecContext *avctx;
    AVFloatDSPContext *dsp;
    struct FFBufQueue *bufqueue;
    OpusEncOptions *options;

    PsyChain cs[128];
    int cs_num;

    OpusBandExcitation ex[OPUS_MAX_CHANNELS][CELT_MAX_BANDS];
    FFBesselFilter bfilter_lo[OPUS_MAX_CHANNELS][CELT_MAX_BANDS];
    FFBesselFilter bfilter_hi[OPUS_MAX_CHANNELS][CELT_MAX_BANDS];

    OpusPsyStep *steps[FF_BUFQUEUE_SIZE + 1];
    int max_steps;

    float *window[CELT_BLOCK_NB];
    AVTXContext *mdct[CELT_BLOCK_NB];
    av_tx_fn mdct_fn[CELT_BLOCK_NB];
    int bsize_analysis;

    DECLARE_ALIGNED(32, float, scratch)[2048];

    /* Stats */
    float rc_waste;
    float avg_is_band;
    int64_t dual_stereo_used;
    int64_t total_packets_out;

    /* State */
    FFBesselFilter lambda_lp;
    OpusPacketInfo p;
    int redo_analysis;
    int buffered_steps;
    int steps_to_process;
    int eof;
    float lambda;
    int *inflection_points;
    int inflection_points_count;
} OpusPsyContext;

int  ff_opus_psy_process           (OpusPsyContext *s, OpusPacketInfo *p);
void ff_opus_psy_celt_frame_init   (OpusPsyContext *s, CeltFrame *f, int index);
int  ff_opus_psy_celt_frame_process(OpusPsyContext *s, CeltFrame *f, int index);
void ff_opus_psy_postencode_update (OpusPsyContext *s, CeltFrame *f, OpusRangeCoder *rc);

int  ff_opus_psy_init(OpusPsyContext *s, AVCodecContext *avctx,
                      struct FFBufQueue *bufqueue, OpusEncOptions *options);
void ff_opus_psy_signal_eof(OpusPsyContext *s);
int  ff_opus_psy_end(OpusPsyContext *s);

#endif /* AVCODEC_OPUSENC_PSY_H */