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_XLL_H
22cabdff1aSopenharmony_ci#define AVCODEC_DCA_XLL_H
23cabdff1aSopenharmony_ci
24cabdff1aSopenharmony_ci#include "libavutil/mem_internal.h"
25cabdff1aSopenharmony_ci
26cabdff1aSopenharmony_ci#include "avcodec.h"
27cabdff1aSopenharmony_ci#include "get_bits.h"
28cabdff1aSopenharmony_ci#include "dca.h"
29cabdff1aSopenharmony_ci#include "dcadsp.h"
30cabdff1aSopenharmony_ci#include "dca_exss.h"
31cabdff1aSopenharmony_ci
32cabdff1aSopenharmony_ci#define DCA_XLL_CHSETS_MAX              3
33cabdff1aSopenharmony_ci#define DCA_XLL_CHANNELS_MAX            8
34cabdff1aSopenharmony_ci#define DCA_XLL_BANDS_MAX               2
35cabdff1aSopenharmony_ci#define DCA_XLL_ADAPT_PRED_ORDER_MAX    16
36cabdff1aSopenharmony_ci#define DCA_XLL_DECI_HISTORY_MAX        8
37cabdff1aSopenharmony_ci#define DCA_XLL_DMIX_SCALES_MAX         ((DCA_XLL_CHSETS_MAX - 1) * DCA_XLL_CHANNELS_MAX)
38cabdff1aSopenharmony_ci#define DCA_XLL_DMIX_COEFFS_MAX         (DCA_XLL_DMIX_SCALES_MAX * DCA_XLL_CHANNELS_MAX)
39cabdff1aSopenharmony_ci#define DCA_XLL_PBR_BUFFER_MAX          (240 << 10)
40cabdff1aSopenharmony_ci#define DCA_XLL_SAMPLE_BUFFERS_MAX      3
41cabdff1aSopenharmony_ci
42cabdff1aSopenharmony_citypedef struct DCAXllBand {
43cabdff1aSopenharmony_ci    int     decor_enabled;                          ///< Pairwise channel decorrelation flag
44cabdff1aSopenharmony_ci    int     orig_order[DCA_XLL_CHANNELS_MAX];       ///< Original channel order
45cabdff1aSopenharmony_ci    int     decor_coeff[DCA_XLL_CHANNELS_MAX / 2];  ///< Pairwise channel coefficients
46cabdff1aSopenharmony_ci
47cabdff1aSopenharmony_ci    int     adapt_pred_order[DCA_XLL_CHANNELS_MAX]; ///< Adaptive predictor order
48cabdff1aSopenharmony_ci    int     highest_pred_order;                     ///< Highest adaptive predictor order
49cabdff1aSopenharmony_ci    int     fixed_pred_order[DCA_XLL_CHANNELS_MAX]; ///< Fixed predictor order
50cabdff1aSopenharmony_ci    int     adapt_refl_coeff[DCA_XLL_CHANNELS_MAX][DCA_XLL_ADAPT_PRED_ORDER_MAX];   ///< Adaptive predictor reflection coefficients
51cabdff1aSopenharmony_ci
52cabdff1aSopenharmony_ci    int     dmix_embedded;  ///< Downmix performed by encoder in frequency band
53cabdff1aSopenharmony_ci
54cabdff1aSopenharmony_ci    int     lsb_section_size;                       ///< Size of LSB section in any segment
55cabdff1aSopenharmony_ci    int     nscalablelsbs[DCA_XLL_CHANNELS_MAX];    ///< Number of bits to represent the samples in LSB part
56cabdff1aSopenharmony_ci    int     bit_width_adjust[DCA_XLL_CHANNELS_MAX]; ///< Number of bits discarded by authoring
57cabdff1aSopenharmony_ci
58cabdff1aSopenharmony_ci    int32_t *msb_sample_buffer[DCA_XLL_CHANNELS_MAX];   ///< MSB sample buffer pointers
59cabdff1aSopenharmony_ci    int32_t *lsb_sample_buffer[DCA_XLL_CHANNELS_MAX];   ///< LSB sample buffer pointers or NULL
60cabdff1aSopenharmony_ci} DCAXllBand;
61cabdff1aSopenharmony_ci
62cabdff1aSopenharmony_citypedef struct DCAXllChSet {
63cabdff1aSopenharmony_ci    // Channel set header
64cabdff1aSopenharmony_ci    int     nchannels;          ///< Number of channels in the channel set (N)
65cabdff1aSopenharmony_ci    int     residual_encode;    ///< Residual encoding mask (0 - residual, 1 - full channel)
66cabdff1aSopenharmony_ci    int     pcm_bit_res;        ///< PCM bit resolution (variable)
67cabdff1aSopenharmony_ci    int     storage_bit_res;    ///< Storage bit resolution (16 or 24)
68cabdff1aSopenharmony_ci    int     freq;               ///< Original sampling frequency (max. 96000 Hz)
69cabdff1aSopenharmony_ci
70cabdff1aSopenharmony_ci    int     primary_chset;          ///< Primary channel set flag
71cabdff1aSopenharmony_ci    int     dmix_coeffs_present;    ///< Downmix coefficients present in stream
72cabdff1aSopenharmony_ci    int     dmix_embedded;          ///< Downmix already performed by encoder
73cabdff1aSopenharmony_ci    int     dmix_type;              ///< Primary channel set downmix type
74cabdff1aSopenharmony_ci    int     hier_chset;             ///< Whether the channel set is part of a hierarchy
75cabdff1aSopenharmony_ci    int     hier_ofs;               ///< Number of preceding channels in a hierarchy (M)
76cabdff1aSopenharmony_ci    int     dmix_coeff[DCA_XLL_DMIX_COEFFS_MAX];       ///< Downmixing coefficients
77cabdff1aSopenharmony_ci    int     dmix_scale[DCA_XLL_DMIX_SCALES_MAX];       ///< Downmixing scales
78cabdff1aSopenharmony_ci    int     dmix_scale_inv[DCA_XLL_DMIX_SCALES_MAX];   ///< Inverse downmixing scales
79cabdff1aSopenharmony_ci    int     ch_mask;                ///< Channel mask for set
80cabdff1aSopenharmony_ci    int     ch_remap[DCA_XLL_CHANNELS_MAX];    ///< Channel to speaker map
81cabdff1aSopenharmony_ci
82cabdff1aSopenharmony_ci    int     nfreqbands; ///< Number of frequency bands (1 or 2)
83cabdff1aSopenharmony_ci    int     nabits;     ///< Number of bits to read bit allocation coding parameter
84cabdff1aSopenharmony_ci
85cabdff1aSopenharmony_ci    DCAXllBand     bands[DCA_XLL_BANDS_MAX];   ///< Frequency bands
86cabdff1aSopenharmony_ci
87cabdff1aSopenharmony_ci    // Frequency band coding parameters
88cabdff1aSopenharmony_ci    int     seg_common;                                     ///< Segment type
89cabdff1aSopenharmony_ci    int     rice_code_flag[DCA_XLL_CHANNELS_MAX];           ///< Rice coding flag
90cabdff1aSopenharmony_ci    int     bitalloc_hybrid_linear[DCA_XLL_CHANNELS_MAX];   ///< Binary code length for isolated samples
91cabdff1aSopenharmony_ci    int     bitalloc_part_a[DCA_XLL_CHANNELS_MAX];          ///< Coding parameter for part A of segment
92cabdff1aSopenharmony_ci    int     bitalloc_part_b[DCA_XLL_CHANNELS_MAX];          ///< Coding parameter for part B of segment
93cabdff1aSopenharmony_ci    int     nsamples_part_a[DCA_XLL_CHANNELS_MAX];          ///< Number of samples in part A of segment
94cabdff1aSopenharmony_ci
95cabdff1aSopenharmony_ci    // Decimator history
96cabdff1aSopenharmony_ci    DECLARE_ALIGNED(32, int32_t, deci_history)[DCA_XLL_CHANNELS_MAX][DCA_XLL_DECI_HISTORY_MAX]; ///< Decimator history for frequency band 1
97cabdff1aSopenharmony_ci
98cabdff1aSopenharmony_ci    // Sample buffers
99cabdff1aSopenharmony_ci    unsigned int    sample_size[DCA_XLL_SAMPLE_BUFFERS_MAX];
100cabdff1aSopenharmony_ci    int32_t         *sample_buffer[DCA_XLL_SAMPLE_BUFFERS_MAX];
101cabdff1aSopenharmony_ci} DCAXllChSet;
102cabdff1aSopenharmony_ci
103cabdff1aSopenharmony_citypedef struct DCAXllDecoder {
104cabdff1aSopenharmony_ci    AVCodecContext  *avctx;
105cabdff1aSopenharmony_ci    GetBitContext   gb;
106cabdff1aSopenharmony_ci
107cabdff1aSopenharmony_ci    int     frame_size;             ///< Number of bytes in a lossless frame
108cabdff1aSopenharmony_ci    int     nchsets;                ///< Number of channels sets per frame
109cabdff1aSopenharmony_ci    int     nframesegs;             ///< Number of segments per frame
110cabdff1aSopenharmony_ci    int     nsegsamples_log2;       ///< log2(nsegsamples)
111cabdff1aSopenharmony_ci    int     nsegsamples;            ///< Samples in segment per one frequency band
112cabdff1aSopenharmony_ci    int     nframesamples_log2;     ///< log2(nframesamples)
113cabdff1aSopenharmony_ci    int     nframesamples;          ///< Samples in frame per one frequency band
114cabdff1aSopenharmony_ci    int     seg_size_nbits;         ///< Number of bits used to read segment size
115cabdff1aSopenharmony_ci    int     band_crc_present;       ///< Presence of CRC16 within each frequency band
116cabdff1aSopenharmony_ci    int     scalable_lsbs;          ///< MSB/LSB split flag
117cabdff1aSopenharmony_ci    int     ch_mask_nbits;          ///< Number of bits used to read channel mask
118cabdff1aSopenharmony_ci    int     fixed_lsb_width;        ///< Fixed LSB width
119cabdff1aSopenharmony_ci
120cabdff1aSopenharmony_ci    DCAXllChSet    chset[DCA_XLL_CHSETS_MAX]; ///< Channel sets
121cabdff1aSopenharmony_ci
122cabdff1aSopenharmony_ci    int             *navi;          ///< NAVI table
123cabdff1aSopenharmony_ci    unsigned int    navi_size;
124cabdff1aSopenharmony_ci
125cabdff1aSopenharmony_ci    int     nfreqbands;     ///< Highest number of frequency bands
126cabdff1aSopenharmony_ci    int     nchannels;      ///< Total number of channels in a hierarchy
127cabdff1aSopenharmony_ci    int     nreschsets;     ///< Number of channel sets that have residual encoded channels
128cabdff1aSopenharmony_ci    int     nactivechsets;  ///< Number of active channel sets to decode
129cabdff1aSopenharmony_ci
130cabdff1aSopenharmony_ci    int     hd_stream_id;   ///< Previous DTS-HD stream ID for detecting changes
131cabdff1aSopenharmony_ci
132cabdff1aSopenharmony_ci    uint8_t     *pbr_buffer;        ///< Peak bit rate (PBR) smoothing buffer
133cabdff1aSopenharmony_ci    int         pbr_length;         ///< Length in bytes of data currently buffered
134cabdff1aSopenharmony_ci    int         pbr_delay;          ///< Delay in frames before decoding buffered data
135cabdff1aSopenharmony_ci
136cabdff1aSopenharmony_ci    DCADSPContext   *dcadsp;
137cabdff1aSopenharmony_ci
138cabdff1aSopenharmony_ci    int     output_mask;
139cabdff1aSopenharmony_ci    int32_t *output_samples[DCA_SPEAKER_COUNT];
140cabdff1aSopenharmony_ci} DCAXllDecoder;
141cabdff1aSopenharmony_ci
142cabdff1aSopenharmony_ciint ff_dca_xll_parse(DCAXllDecoder *s, const uint8_t *data, DCAExssAsset *asset);
143cabdff1aSopenharmony_ciint ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame);
144cabdff1aSopenharmony_ciav_cold void ff_dca_xll_flush(DCAXllDecoder *s);
145cabdff1aSopenharmony_ciav_cold void ff_dca_xll_close(DCAXllDecoder *s);
146cabdff1aSopenharmony_ci
147cabdff1aSopenharmony_ci#endif
148