1cabdff1aSopenharmony_ci/*
2cabdff1aSopenharmony_ci * Copyright (C) 2016 foo86
3cabdff1aSopenharmony_ci *
4cabdff1aSopenharmony_ci * This file is part of FFmpeg.
5cabdff1aSopenharmony_ci *
6cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or
7cabdff1aSopenharmony_ci * modify it under the terms of the GNU Lesser General Public
8cabdff1aSopenharmony_ci * License as published by the Free Software Foundation; either
9cabdff1aSopenharmony_ci * version 2.1 of the License, or (at your option) any later version.
10cabdff1aSopenharmony_ci *
11cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful,
12cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
13cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14cabdff1aSopenharmony_ci * Lesser General Public License for more details.
15cabdff1aSopenharmony_ci *
16cabdff1aSopenharmony_ci * You should have received a copy of the GNU Lesser General Public
17cabdff1aSopenharmony_ci * License along with FFmpeg; if not, write to the Free Software
18cabdff1aSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19cabdff1aSopenharmony_ci */
20cabdff1aSopenharmony_ci
21cabdff1aSopenharmony_ci#ifndef AVCODEC_DCA_LBR_H
22cabdff1aSopenharmony_ci#define AVCODEC_DCA_LBR_H
23cabdff1aSopenharmony_ci
24cabdff1aSopenharmony_ci#include "libavutil/float_dsp.h"
25cabdff1aSopenharmony_ci#include "libavutil/mem_internal.h"
26cabdff1aSopenharmony_ci
27cabdff1aSopenharmony_ci#include "avcodec.h"
28cabdff1aSopenharmony_ci#include "get_bits.h"
29cabdff1aSopenharmony_ci#include "dca.h"
30cabdff1aSopenharmony_ci#include "dca_exss.h"
31cabdff1aSopenharmony_ci#include "dcadsp.h"
32cabdff1aSopenharmony_ci#include "fft.h"
33cabdff1aSopenharmony_ci
34cabdff1aSopenharmony_ci#define DCA_LBR_CHANNELS        6
35cabdff1aSopenharmony_ci#define DCA_LBR_CHANNELS_TOTAL  32
36cabdff1aSopenharmony_ci#define DCA_LBR_SUBBANDS        32
37cabdff1aSopenharmony_ci#define DCA_LBR_TONES           512
38cabdff1aSopenharmony_ci
39cabdff1aSopenharmony_ci#define DCA_LBR_TIME_SAMPLES    128
40cabdff1aSopenharmony_ci#define DCA_LBR_TIME_HISTORY    8
41cabdff1aSopenharmony_ci
42cabdff1aSopenharmony_cienum DCALBRHeader {
43cabdff1aSopenharmony_ci    DCA_LBR_HEADER_SYNC_ONLY    = 1,
44cabdff1aSopenharmony_ci    DCA_LBR_HEADER_DECODER_INIT = 2
45cabdff1aSopenharmony_ci};
46cabdff1aSopenharmony_ci
47cabdff1aSopenharmony_citypedef struct DCALbrTone {
48cabdff1aSopenharmony_ci    uint8_t     x_freq;     ///< Spectral line offset
49cabdff1aSopenharmony_ci    uint8_t     f_delt;     ///< Difference between original and center frequency
50cabdff1aSopenharmony_ci    uint8_t     ph_rot;     ///< Phase rotation
51cabdff1aSopenharmony_ci    uint8_t     pad;        ///< Padding field
52cabdff1aSopenharmony_ci    uint8_t     amp[DCA_LBR_CHANNELS];  ///< Per-channel amplitude
53cabdff1aSopenharmony_ci    uint8_t     phs[DCA_LBR_CHANNELS];  ///< Per-channel phase
54cabdff1aSopenharmony_ci} DCALbrTone;
55cabdff1aSopenharmony_ci
56cabdff1aSopenharmony_citypedef struct DCALbrDecoder {
57cabdff1aSopenharmony_ci    AVCodecContext  *avctx;
58cabdff1aSopenharmony_ci    GetBitContext   gb;
59cabdff1aSopenharmony_ci
60cabdff1aSopenharmony_ci    int     sample_rate;        ///< Sample rate of LBR audio
61cabdff1aSopenharmony_ci    int     ch_mask;            ///< LBR speaker mask
62cabdff1aSopenharmony_ci    int     flags;              ///< Flags for LBR decoder initialization
63cabdff1aSopenharmony_ci    int     bit_rate_orig;      ///< Original bit rate
64cabdff1aSopenharmony_ci    int     bit_rate_scaled;    ///< Scaled bit rate
65cabdff1aSopenharmony_ci
66cabdff1aSopenharmony_ci    int     nchannels;          ///< Number of fullband channels to decode
67cabdff1aSopenharmony_ci    int     nchannels_total;    ///< Total number of fullband channels
68cabdff1aSopenharmony_ci    int     freq_range;         ///< Frequency range of LBR audio
69cabdff1aSopenharmony_ci    int     band_limit;         ///< Band limit factor
70cabdff1aSopenharmony_ci    int     limited_rate;       ///< Band limited sample rate
71cabdff1aSopenharmony_ci    int     limited_range;      ///< Band limited frequency range
72cabdff1aSopenharmony_ci    int     res_profile;        ///< Resolution profile
73cabdff1aSopenharmony_ci    int     nsubbands;          ///< Number of encoded subbands
74cabdff1aSopenharmony_ci    int     g3_avg_only_start_sb;   ///< Subband index where grid 3 scale factors end
75cabdff1aSopenharmony_ci    int     min_mono_subband;   ///< Subband index where mono encoding starts
76cabdff1aSopenharmony_ci    int     max_mono_subband;   ///< Subband index where mono encoding ends
77cabdff1aSopenharmony_ci
78cabdff1aSopenharmony_ci    int     framenum;   ///< Lower 5 bits of current frame number
79cabdff1aSopenharmony_ci    int     lbr_rand;   ///< Seed for subband randomization
80cabdff1aSopenharmony_ci    int     warned;     ///< Flags for warning suppression
81cabdff1aSopenharmony_ci
82cabdff1aSopenharmony_ci    uint8_t     quant_levels[DCA_LBR_CHANNELS / 2][DCA_LBR_SUBBANDS];   ///< Quantization levels
83cabdff1aSopenharmony_ci    uint8_t     sb_indices[DCA_LBR_SUBBANDS];   ///< Subband reordering indices
84cabdff1aSopenharmony_ci
85cabdff1aSopenharmony_ci    uint8_t     sec_ch_sbms[DCA_LBR_CHANNELS / 2][DCA_LBR_SUBBANDS];    ///< Right channel inversion or mid/side decoding flags
86cabdff1aSopenharmony_ci    uint8_t     sec_ch_lrms[DCA_LBR_CHANNELS / 2][DCA_LBR_SUBBANDS];    ///< Flags indicating if left/right channel are swapped
87cabdff1aSopenharmony_ci    uint32_t    ch_pres[DCA_LBR_CHANNELS];  ///< Subband allocation flags
88cabdff1aSopenharmony_ci
89cabdff1aSopenharmony_ci    uint8_t     grid_1_scf[DCA_LBR_CHANNELS][12][8];    ///< Grid 1 scale factors
90cabdff1aSopenharmony_ci    uint8_t     grid_2_scf[DCA_LBR_CHANNELS][3][64];    ///< Grid 2 scale factors
91cabdff1aSopenharmony_ci
92cabdff1aSopenharmony_ci    int8_t      grid_3_avg[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS - 4];     ///< Grid 3 average values
93cabdff1aSopenharmony_ci    int8_t      grid_3_scf[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS - 4][8];  ///< Grid 3 scale factors
94cabdff1aSopenharmony_ci    uint32_t    grid_3_pres[DCA_LBR_CHANNELS];  ///< Grid 3 scale factors presence flags
95cabdff1aSopenharmony_ci
96cabdff1aSopenharmony_ci    uint8_t     high_res_scf[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS][8];    ///< High-frequency resolution scale factors
97cabdff1aSopenharmony_ci
98cabdff1aSopenharmony_ci    uint8_t     part_stereo[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS / 4][5]; ///< Partial stereo coefficients
99cabdff1aSopenharmony_ci    uint8_t     part_stereo_pres;   ///< Partial stereo coefficients presence flags
100cabdff1aSopenharmony_ci
101cabdff1aSopenharmony_ci    float       lpc_coeff[2][DCA_LBR_CHANNELS][3][2][8];    ///< Predictor coefficients
102cabdff1aSopenharmony_ci
103cabdff1aSopenharmony_ci    float       sb_scf[DCA_LBR_SUBBANDS];   ///< Subband randomization scale factors
104cabdff1aSopenharmony_ci
105cabdff1aSopenharmony_ci    float       *time_samples[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS]; ///< Time samples
106cabdff1aSopenharmony_ci
107cabdff1aSopenharmony_ci    float           *ts_buffer; ///< Time sample buffer base
108cabdff1aSopenharmony_ci    unsigned int    ts_size;    ///< Time sample buffer size
109cabdff1aSopenharmony_ci
110cabdff1aSopenharmony_ci    DECLARE_ALIGNED(32, float, history)[DCA_LBR_CHANNELS][DCA_LBR_SUBBANDS * 4];    ///< IMDCT history
111cabdff1aSopenharmony_ci    DECLARE_ALIGNED(32, float, window)[DCA_LBR_SUBBANDS * 4];   ///< Long window for IMDCT
112cabdff1aSopenharmony_ci
113cabdff1aSopenharmony_ci    DECLARE_ALIGNED(32, float, lfe_data)[64];       ///< Decimated LFE samples
114cabdff1aSopenharmony_ci    DECLARE_ALIGNED(32, float, lfe_history)[5][2];  ///< LFE IIR filter history
115cabdff1aSopenharmony_ci    float lfe_scale;    ///< Scale factor of LFE samples before IIR filter
116cabdff1aSopenharmony_ci
117cabdff1aSopenharmony_ci    uint8_t     tonal_scf[6];           ///< Tonal scale factors
118cabdff1aSopenharmony_ci    uint16_t    tonal_bounds[5][32][2]; ///< Per-group per-subframe start/end positions of tones
119cabdff1aSopenharmony_ci    DCALbrTone  tones[DCA_LBR_TONES];   ///< Circular buffer of tones
120cabdff1aSopenharmony_ci    int         ntones;                 ///< Circular buffer head position
121cabdff1aSopenharmony_ci
122cabdff1aSopenharmony_ci    FFTContext          imdct;
123cabdff1aSopenharmony_ci    AVFloatDSPContext   *fdsp;
124cabdff1aSopenharmony_ci    DCADSPContext       *dcadsp;
125cabdff1aSopenharmony_ci} DCALbrDecoder;
126cabdff1aSopenharmony_ci
127cabdff1aSopenharmony_ciint ff_dca_lbr_parse(DCALbrDecoder *s, const uint8_t *data, DCAExssAsset *asset);
128cabdff1aSopenharmony_ciint ff_dca_lbr_filter_frame(DCALbrDecoder *s, AVFrame *frame);
129cabdff1aSopenharmony_ciav_cold void ff_dca_lbr_flush(DCALbrDecoder *s);
130cabdff1aSopenharmony_ciav_cold void ff_dca_lbr_init_tables(void);
131cabdff1aSopenharmony_ciav_cold int ff_dca_lbr_init(DCALbrDecoder *s);
132cabdff1aSopenharmony_ciav_cold void ff_dca_lbr_close(DCALbrDecoder *s);
133cabdff1aSopenharmony_ci
134cabdff1aSopenharmony_ci#endif
135