]> git.sesse.net Git - ffmpeg/blob - libavcodec/dca.h
Merge commit '31d2039cb42668ebcf08248bc48bbad44aa05f49'
[ffmpeg] / libavcodec / dca.h
1 /*
2  * DCA compatible decoder
3  * Copyright (C) 2004 Gildas Bazin
4  * Copyright (C) 2004 Benjamin Zores
5  * Copyright (C) 2006 Benjamin Larsson
6  * Copyright (C) 2007 Konstantin Shishkov
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24
25 #ifndef AVCODEC_DCA_H
26 #define AVCODEC_DCA_H
27
28 #include <stdint.h>
29
30 #include "libavutil/float_dsp.h"
31 #include "libavutil/internal.h"
32
33 #include "avcodec.h"
34 #include "dcadsp.h"
35 #include "fmtconvert.h"
36 #include "get_bits.h"
37
38 /** DCA syncwords, also used for bitstream type detection */
39 #define DCA_MARKER_RAW_BE 0x7FFE8001
40 #define DCA_MARKER_RAW_LE 0xFE7F0180
41 #define DCA_MARKER_14B_BE 0x1FFFE800
42 #define DCA_MARKER_14B_LE 0xFF1F00E8
43
44 /** DCA-HD specific block starts with this marker. */
45 #define DCA_HD_MARKER     0x64582025
46
47 #define DCA_PRIM_CHANNELS_MAX  (7)
48 #define DCA_ABITS_MAX         (32)      /* Should be 28 */
49 #define DCA_SUBSUBFRAMES_MAX   (4)
50 #define DCA_SUBFRAMES_MAX     (16)
51 #define DCA_BLOCKS_MAX        (16)
52 #define DCA_LFE_MAX            (3)
53 #define DCA_CHSETS_MAX         (4)
54 #define DCA_CHSET_CHANS_MAX    (8)
55
56 #define DCA_MAX_FRAME_SIZE       16384
57 #define DCA_MAX_EXSS_HEADER_SIZE  4096
58
59 #define DCA_BUFFER_PADDING_SIZE   1024
60
61 enum DCAExtensionMask {
62     DCA_EXT_CORE       = 0x001, ///< core in core substream
63     DCA_EXT_XXCH       = 0x002, ///< XXCh channels extension in core substream
64     DCA_EXT_X96        = 0x004, ///< 96/24 extension in core substream
65     DCA_EXT_XCH        = 0x008, ///< XCh channel extension in core substream
66     DCA_EXT_EXSS_CORE  = 0x010, ///< core in ExSS (extension substream)
67     DCA_EXT_EXSS_XBR   = 0x020, ///< extended bitrate extension in ExSS
68     DCA_EXT_EXSS_XXCH  = 0x040, ///< XXCh channels extension in ExSS
69     DCA_EXT_EXSS_X96   = 0x080, ///< 96/24 extension in ExSS
70     DCA_EXT_EXSS_LBR   = 0x100, ///< low bitrate component in ExSS
71     DCA_EXT_EXSS_XLL   = 0x200, ///< lossless extension in ExSS
72 };
73
74 typedef struct DCAContext {
75     const AVClass *class;       ///< class for AVOptions
76     AVCodecContext *avctx;
77     /* Frame header */
78     int frame_type;             ///< type of the current frame
79     int samples_deficit;        ///< deficit sample count
80     int crc_present;            ///< crc is present in the bitstream
81     int sample_blocks;          ///< number of PCM sample blocks
82     int frame_size;             ///< primary frame byte size
83     int amode;                  ///< audio channels arrangement
84     int sample_rate;            ///< audio sampling rate
85     int bit_rate;               ///< transmission bit rate
86     int bit_rate_index;         ///< transmission bit rate index
87
88     int dynrange;               ///< embedded dynamic range flag
89     int timestamp;              ///< embedded time stamp flag
90     int aux_data;               ///< auxiliary data flag
91     int hdcd;                   ///< source material is mastered in HDCD
92     int ext_descr;              ///< extension audio descriptor flag
93     int ext_coding;             ///< extended coding flag
94     int aspf;                   ///< audio sync word insertion flag
95     int lfe;                    ///< low frequency effects flag
96     int predictor_history;      ///< predictor history flag
97     int header_crc;             ///< header crc check bytes
98     int multirate_inter;        ///< multirate interpolator switch
99     int version;                ///< encoder software revision
100     int copy_history;           ///< copy history
101     int source_pcm_res;         ///< source pcm resolution
102     int front_sum;              ///< front sum/difference flag
103     int surround_sum;           ///< surround sum/difference flag
104     int dialog_norm;            ///< dialog normalisation parameter
105
106     /* Primary audio coding header */
107     int subframes;              ///< number of subframes
108     int total_channels;         ///< number of channels including extensions
109     int prim_channels;          ///< number of primary audio channels
110     int subband_activity[DCA_PRIM_CHANNELS_MAX];    ///< subband activity count
111     int vq_start_subband[DCA_PRIM_CHANNELS_MAX];    ///< high frequency vq start subband
112     int joint_intensity[DCA_PRIM_CHANNELS_MAX];     ///< joint intensity coding index
113     int transient_huffman[DCA_PRIM_CHANNELS_MAX];   ///< transient mode code book
114     int scalefactor_huffman[DCA_PRIM_CHANNELS_MAX]; ///< scale factor code book
115     int bitalloc_huffman[DCA_PRIM_CHANNELS_MAX];    ///< bit allocation quantizer select
116     int quant_index_huffman[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX]; ///< quantization index codebook select
117     float scalefactor_adj[DCA_PRIM_CHANNELS_MAX][DCA_ABITS_MAX];   ///< scale factor adjustment
118
119     /* Primary audio coding side information */
120     int subsubframes[DCA_SUBFRAMES_MAX];                         ///< number of subsubframes
121     int partial_samples[DCA_SUBFRAMES_MAX];                      ///< partial subsubframe samples count
122     int prediction_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< prediction mode (ADPCM used or not)
123     int prediction_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];      ///< prediction VQ coefs
124     int bitalloc[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];           ///< bit allocation index
125     int transition_mode[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];    ///< transition mode (transients)
126     int32_t scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][2];///< scale factors (2 if transient)
127     int joint_huff[DCA_PRIM_CHANNELS_MAX];                       ///< joint subband scale factors codebook
128     int joint_scale_factor[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS]; ///< joint subband scale factors
129     float downmix_coef[DCA_PRIM_CHANNELS_MAX + 1][2];            ///< stereo downmix coefficients
130     int dynrange_coef;                                           ///< dynamic range coefficient
131
132     /* Core substream's embedded downmix coefficients (cf. ETSI TS 102 114 V1.4.1)
133      * Input:  primary audio channels (incl. LFE if present)
134      * Output: downmix audio channels (up to 4, no LFE) */
135     uint8_t  core_downmix;                                       ///< embedded downmix coefficients available
136     uint8_t  core_downmix_amode;                                 ///< audio channel arrangement of embedded downmix
137     uint16_t core_downmix_codes[DCA_PRIM_CHANNELS_MAX + 1][4];   ///< embedded downmix coefficients (9-bit codes)
138
139     int32_t  high_freq_vq[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS];  ///< VQ encoded high frequency subbands
140
141     float lfe_data[2 * DCA_LFE_MAX * (DCA_BLOCKS_MAX + 4)];      ///< Low frequency effect data
142     int lfe_scale_factor;
143
144     /* Subband samples history (for ADPCM) */
145     DECLARE_ALIGNED(16, float, subband_samples_hist)[DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][4];
146     DECLARE_ALIGNED(32, float, subband_fir_hist)[DCA_PRIM_CHANNELS_MAX][512];
147     DECLARE_ALIGNED(32, float, subband_fir_noidea)[DCA_PRIM_CHANNELS_MAX][32];
148     int hist_index[DCA_PRIM_CHANNELS_MAX];
149     DECLARE_ALIGNED(32, float, raXin)[32];
150
151     int output;                 ///< type of output
152
153     DECLARE_ALIGNED(32, float, subband_samples)[DCA_BLOCKS_MAX][DCA_PRIM_CHANNELS_MAX][DCA_SUBBANDS][8];
154     float *samples_chanptr[DCA_PRIM_CHANNELS_MAX + 1];
155     float *extra_channels[DCA_PRIM_CHANNELS_MAX + 1];
156     uint8_t *extra_channels_buffer;
157     unsigned int extra_channels_buffer_size;
158
159     uint8_t dca_buffer[DCA_MAX_FRAME_SIZE + DCA_MAX_EXSS_HEADER_SIZE + DCA_BUFFER_PADDING_SIZE];
160     int dca_buffer_size;        ///< how much data is in the dca_buffer
161
162     const int8_t *channel_order_tab;  ///< channel reordering table, lfe and non lfe
163     GetBitContext gb;
164     /* Current position in DCA frame */
165     int current_subframe;
166     int current_subsubframe;
167
168     int core_ext_mask;          ///< present extensions in the core substream
169
170     /* XCh extension information */
171     int xch_present;            ///< XCh extension present and valid
172     int xch_base_channel;       ///< index of first (only) channel containing XCH data
173     int xch_disable;            ///< whether the XCh extension should be decoded or not
174
175     /* XXCH extension information */
176     int xxch_chset;
177     int xxch_nbits_spk_mask;
178     uint32_t xxch_core_spkmask;
179     uint32_t xxch_spk_masks[4]; /* speaker masks, last element is core mask */
180     int xxch_chset_nch[4];
181     float xxch_dmix_sf[DCA_CHSETS_MAX];
182
183     uint32_t xxch_dmix_embedded;  /* lower layer has mix pre-embedded, per chset */
184     float xxch_dmix_coeff[DCA_PRIM_CHANNELS_MAX][32]; /* worst case sizing */
185
186     int8_t xxch_order_tab[32];
187     int8_t lfe_index;
188
189     /* ExSS header parser */
190     int static_fields;          ///< static fields present
191     int mix_metadata;           ///< mixing metadata present
192     int num_mix_configs;        ///< number of mix out configurations
193     int mix_config_num_ch[4];   ///< number of channels in each mix out configuration
194
195     int profile;
196
197     int debug_flag;             ///< used for suppressing repeated error messages output
198     AVFloatDSPContext *fdsp;
199     FFTContext imdct;
200     SynthFilterContext synth;
201     DCADSPContext dcadsp;
202     FmtConvertContext fmt_conv;
203 } DCAContext;
204
205 extern av_export const uint32_t avpriv_dca_sample_rates[16];
206
207 /**
208  * Convert bitstream to one representation based on sync marker
209  */
210 int avpriv_dca_convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst,
211                              int max_size);
212
213 int ff_dca_xbr_parse_frame(DCAContext *s);
214 int ff_dca_xxch_decode_frame(DCAContext *s);
215
216 void ff_dca_exss_parse_header(DCAContext *s);
217
218 #endif /* AVCODEC_DCA_H */