libdts support by (Benjamin Zores <ben at geexbox dot org>)
[libav.git] / libavcodec / dts_internal.h
1 /*
2 * dts_internal.h
3 * Copyright (C) 2004 Gildas Bazin <gbazin@videolan.org>
4 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
5 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 *
7 * This file is part of dtsdec, a free DTS Coherent Acoustics stream decoder.
8 * See http://www.videolan.org/dtsdec.html for updates.
9 *
10 * dtsdec is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * dtsdec 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
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 #define DTS_SUBFRAMES_MAX (16)
26 #define DTS_PRIM_CHANNELS_MAX (5)
27 #define DTS_SUBBANDS (32)
28 #define DTS_ABITS_MAX (32) /* Should be 28 */
29 #define DTS_SUBSUBFAMES_MAX (4)
30 #define DTS_LFE_MAX (3)
31
32 struct dts_state_s {
33
34 /* Frame header */
35 int frame_type; /* type of the current frame */
36 int samples_deficit; /* deficit sample count */
37 int crc_present; /* crc is present in the bitstream */
38 int sample_blocks; /* number of PCM sample blocks */
39 int frame_size; /* primary frame byte size */
40 int amode; /* audio channels arrangement */
41 int sample_rate; /* audio sampling rate */
42 int bit_rate; /* transmission bit rate */
43
44 int downmix; /* embedded downmix enabled */
45 int dynrange; /* embedded dynamic range flag */
46 int timestamp; /* embedded time stamp flag */
47 int aux_data; /* auxiliary data flag */
48 int hdcd; /* source material is mastered in HDCD */
49 int ext_descr; /* extension audio descriptor flag */
50 int ext_coding; /* extended coding flag */
51 int aspf; /* audio sync word insertion flag */
52 int lfe; /* low frequency effects flag */
53 int predictor_history; /* predictor history flag */
54 int header_crc; /* header crc check bytes */
55 int multirate_inter; /* multirate interpolator switch */
56 int version; /* encoder software revision */
57 int copy_history; /* copy history */
58 int source_pcm_res; /* source pcm resolution */
59 int front_sum; /* front sum/difference flag */
60 int surround_sum; /* surround sum/difference flag */
61 int dialog_norm; /* dialog normalisation parameter */
62
63 /* Primary audio coding header */
64 int subframes; /* number of subframes */
65 int prim_channels; /* number of primary audio channels */
66 /* subband activity count */
67 int subband_activity[DTS_PRIM_CHANNELS_MAX];
68 /* high frequency vq start subband */
69 int vq_start_subband[DTS_PRIM_CHANNELS_MAX];
70 /* joint intensity coding index */
71 int joint_intensity[DTS_PRIM_CHANNELS_MAX];
72 /* transient mode code book */
73 int transient_huffman[DTS_PRIM_CHANNELS_MAX];
74 /* scale factor code book */
75 int scalefactor_huffman[DTS_PRIM_CHANNELS_MAX];
76 /* bit allocation quantizer select */
77 int bitalloc_huffman[DTS_PRIM_CHANNELS_MAX];
78 /* quantization index codebook select */
79 int quant_index_huffman[DTS_PRIM_CHANNELS_MAX][DTS_ABITS_MAX];
80 /* scale factor adjustment */
81 float scalefactor_adj[DTS_PRIM_CHANNELS_MAX][DTS_ABITS_MAX];
82
83 /* Primary audio coding side information */
84 int subsubframes; /* number of subsubframes */
85 int partial_samples; /* partial subsubframe samples count */
86 /* prediction mode (ADPCM used or not) */
87 int prediction_mode[DTS_PRIM_CHANNELS_MAX][DTS_SUBBANDS];
88 /* prediction VQ coefs */
89 int prediction_vq[DTS_PRIM_CHANNELS_MAX][DTS_SUBBANDS];
90 /* bit allocation index */
91 int bitalloc[DTS_PRIM_CHANNELS_MAX][DTS_SUBBANDS];
92 /* transition mode (transients) */
93 int transition_mode[DTS_PRIM_CHANNELS_MAX][DTS_SUBBANDS];
94 /* scale factors (2 if transient)*/
95 int scale_factor[DTS_PRIM_CHANNELS_MAX][DTS_SUBBANDS][2];
96 /* joint subband scale factors codebook */
97 int joint_huff[DTS_PRIM_CHANNELS_MAX];
98 /* joint subband scale factors */
99 int joint_scale_factor[DTS_PRIM_CHANNELS_MAX][DTS_SUBBANDS];
100 /* stereo downmix coefficients */
101 int downmix_coef[DTS_PRIM_CHANNELS_MAX][2];
102 /* dynamic range coefficient */
103 int dynrange_coef;
104
105 /* VQ encoded high frequency subbands */
106 int high_freq_vq[DTS_PRIM_CHANNELS_MAX][DTS_SUBBANDS];
107
108 /* Low frequency effect data */
109 double lfe_data[2*DTS_SUBSUBFAMES_MAX*DTS_LFE_MAX * 2 /*history*/];
110 int lfe_scale_factor;
111
112 /* Subband samples history (for ADPCM) */
113 double subband_samples_hist[DTS_PRIM_CHANNELS_MAX][DTS_SUBBANDS][4];
114 double subband_fir_hist[DTS_PRIM_CHANNELS_MAX][512];
115 double subband_fir_noidea[DTS_PRIM_CHANNELS_MAX][64];
116
117 /* Audio output */
118 level_t clev; /* centre channel mix level */
119 level_t slev; /* surround channels mix level */
120
121 int output; /* type of output */
122 level_t level; /* output level */
123 sample_t bias; /* output bias */
124
125 sample_t * samples; /* pointer to the internal audio samples buffer */
126 int downmixed;
127
128 int dynrnge; /* apply dynamic range */
129 level_t dynrng; /* dynamic range */
130 void * dynrngdata; /* dynamic range callback funtion and data */
131 level_t (* dynrngcall) (level_t range, void * dynrngdata);
132
133 /* Bitstream handling */
134 uint32_t * buffer_start;
135 uint32_t bits_left;
136 uint32_t current_word;
137 int word_mode; /* 16/14 bits word format (1 -> 16, 0 -> 14) */
138 int bigendian_mode; /* endianness (1 -> be, 0 -> le) */
139
140 /* Current position in DTS frame */
141 int current_subframe;
142 int current_subsubframe;
143
144 /* Pre-calculated cosine modulation coefs for the QMF */
145 double cos_mod[544];
146
147 /* Debug flag */
148 int debug_flag;
149 };
150
151 #define LEVEL_PLUS6DB 2.0
152 #define LEVEL_PLUS3DB 1.4142135623730951
153 #define LEVEL_3DB 0.7071067811865476
154 #define LEVEL_45DB 0.5946035575013605
155 #define LEVEL_6DB 0.5
156
157 int dts_downmix_init (int input, int flags, level_t * level,
158 level_t clev, level_t slev);
159 int dts_downmix_coeff (level_t * coeff, int acmod, int output, level_t level,
160 level_t clev, level_t slev);
161 void dts_downmix (sample_t * samples, int acmod, int output, sample_t bias,
162 level_t clev, level_t slev);
163 void dts_upmix (sample_t * samples, int acmod, int output);
164
165 #define ROUND(x) ((int)((x) + ((x) > 0 ? 0.5 : -0.5)))
166
167 #ifndef LIBDTS_FIXED
168
169 typedef sample_t quantizer_t;
170 #define SAMPLE(x) (x)
171 #define LEVEL(x) (x)
172 #define MUL(a,b) ((a) * (b))
173 #define MUL_L(a,b) ((a) * (b))
174 #define MUL_C(a,b) ((a) * (b))
175 #define DIV(a,b) ((a) / (b))
176 #define BIAS(x) ((x) + bias)
177
178 #else /* LIBDTS_FIXED */
179
180 typedef int16_t quantizer_t;
181 #define SAMPLE(x) (sample_t)((x) * (1 << 30))
182 #define LEVEL(x) (level_t)((x) * (1 << 26))
183
184 #if 0
185 #define MUL(a,b) ((int)(((int64_t)(a) * (b) + (1 << 29)) >> 30))
186 #define MUL_L(a,b) ((int)(((int64_t)(a) * (b) + (1 << 25)) >> 26))
187 #elif 1
188 #define MUL(a,b) \
189 ({ int32_t _ta=(a), _tb=(b), _tc; \
190 _tc=(_ta & 0xffff)*(_tb >> 16)+(_ta >> 16)*(_tb & 0xffff); (int32_t)(((_tc >> 14))+ (((_ta >> 16)*(_tb >> 16)) << 2 )); })
191 #define MUL_L(a,b) \
192 ({ int32_t _ta=(a), _tb=(b), _tc; \
193 _tc=(_ta & 0xffff)*(_tb >> 16)+(_ta >> 16)*(_tb & 0xffff); (int32_t)((_tc >> 10) + (((_ta >> 16)*(_tb >> 16)) << 6)); })
194 #else
195 #define MUL(a,b) (((a) >> 15) * ((b) >> 15))
196 #define MUL_L(a,b) (((a) >> 13) * ((b) >> 13))
197 #endif
198
199 #define MUL_C(a,b) MUL_L (a, LEVEL (b))
200 #define DIV(a,b) ((((int64_t)LEVEL (a)) << 26) / (b))
201 #define BIAS(x) (x)
202
203 #endif