4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/common.h"
22 #include "libavutil/log.h"
27 /* extensions that reside in core substream */
28 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
30 /* these are unconfirmed but should be mostly correct */
31 enum DCAExSSSpeakerMask {
32 DCA_EXSS_FRONT_CENTER = 0x0001,
33 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
34 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
35 DCA_EXSS_LFE = 0x0008,
36 DCA_EXSS_REAR_CENTER = 0x0010,
37 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
38 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
39 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
40 DCA_EXSS_OVERHEAD = 0x0100,
41 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
42 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
43 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
44 DCA_EXSS_LFE2 = 0x1000,
45 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
46 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
47 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
51 * Return the number of channels in an ExSS speaker mask (HD)
53 static int dca_exss_mask2count(int mask)
55 /* count bits that mean speaker pairs twice */
56 return av_popcount(mask) +
57 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
58 DCA_EXSS_FRONT_LEFT_RIGHT |
59 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
60 DCA_EXSS_WIDE_LEFT_RIGHT |
61 DCA_EXSS_SIDE_LEFT_RIGHT |
62 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
63 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
64 DCA_EXSS_REAR_LEFT_RIGHT |
65 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
69 * Skip mixing coefficients of a single mix out configuration (HD)
71 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
75 for (i = 0; i < channels; i++) {
76 int mix_map_mask = get_bits(gb, out_ch);
77 int num_coeffs = av_popcount(mix_map_mask);
78 skip_bits_long(gb, num_coeffs * 6);
83 * Parse extension substream asset header (HD)
85 static int dca_exss_parse_asset_header(DCAContext *s)
87 int header_pos = get_bits_count(&s->gb);
90 int embedded_stereo = 0;
93 int extensions_mask = 0;
96 if (get_bits_left(&s->gb) < 16)
97 return AVERROR_INVALIDDATA;
99 /* We will parse just enough to get to the extensions bitmask with which
100 * we can set the profile value. */
102 header_size = get_bits(&s->gb, 9) + 1;
103 skip_bits(&s->gb, 3); // asset index
105 if (s->static_fields) {
106 if (get_bits1(&s->gb))
107 skip_bits(&s->gb, 4); // asset type descriptor
108 if (get_bits1(&s->gb))
109 skip_bits_long(&s->gb, 24); // language descriptor
111 if (get_bits1(&s->gb)) {
112 /* How can one fit 1024 bytes of text here if the maximum value
113 * for the asset header size field above was 512 bytes? */
114 int text_length = get_bits(&s->gb, 10) + 1;
115 if (get_bits_left(&s->gb) < text_length * 8)
116 return AVERROR_INVALIDDATA;
117 skip_bits_long(&s->gb, text_length * 8); // info text
120 skip_bits(&s->gb, 5); // bit resolution - 1
121 skip_bits(&s->gb, 4); // max sample rate code
122 channels = get_bits(&s->gb, 8) + 1;
124 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
126 int spkr_mask_size = 16;
130 embedded_stereo = get_bits1(&s->gb);
132 embedded_6ch = get_bits1(&s->gb);
134 if (get_bits1(&s->gb)) {
135 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
136 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
139 spkr_remap_sets = get_bits(&s->gb, 3);
141 for (i = 0; i < spkr_remap_sets; i++) {
142 /* std layout mask for each remap set */
143 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
146 for (i = 0; i < spkr_remap_sets; i++) {
147 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
148 if (get_bits_left(&s->gb) < 0)
149 return AVERROR_INVALIDDATA;
151 for (j = 0; j < num_spkrs[i]; j++) {
152 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
153 int num_dec_ch = av_popcount(remap_dec_ch_mask);
154 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
158 skip_bits(&s->gb, 3); // representation type
162 drc_code_present = get_bits1(&s->gb);
163 if (drc_code_present)
164 get_bits(&s->gb, 8); // drc code
166 if (get_bits1(&s->gb))
167 skip_bits(&s->gb, 5); // dialog normalization code
169 if (drc_code_present && embedded_stereo)
170 get_bits(&s->gb, 8); // drc stereo code
172 if (s->mix_metadata && get_bits1(&s->gb)) {
173 skip_bits(&s->gb, 1); // external mix
174 skip_bits(&s->gb, 6); // post mix gain code
176 if (get_bits(&s->gb, 2) != 3) // mixer drc code
177 skip_bits(&s->gb, 3); // drc limit
179 skip_bits(&s->gb, 8); // custom drc code
181 if (get_bits1(&s->gb)) // channel specific scaling
182 for (i = 0; i < s->num_mix_configs; i++)
183 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
185 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
187 for (i = 0; i < s->num_mix_configs; i++) {
188 if (get_bits_left(&s->gb) < 0)
189 return AVERROR_INVALIDDATA;
190 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
192 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
194 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
198 switch (get_bits(&s->gb, 2)) {
200 extensions_mask = get_bits(&s->gb, 12);
203 extensions_mask = DCA_EXT_EXSS_XLL;
206 extensions_mask = DCA_EXT_EXSS_LBR;
209 extensions_mask = 0; /* aux coding */
213 /* not parsed further, we were only interested in the extensions mask */
215 if (get_bits_left(&s->gb) < 0)
216 return AVERROR_INVALIDDATA;
218 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
219 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
220 return AVERROR_INVALIDDATA;
222 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
224 if (extensions_mask & DCA_EXT_EXSS_XLL)
225 s->profile = FF_PROFILE_DTS_HD_MA;
226 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
228 s->profile = FF_PROFILE_DTS_HD_HRA;
230 if (!(extensions_mask & DCA_EXT_CORE))
231 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
232 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
233 av_log(s->avctx, AV_LOG_WARNING,
234 "DTS extensions detection mismatch (%d, %d)\n",
235 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
241 * Parse extension substream header (HD)
243 void ff_dca_exss_parse_header(DCAContext *s)
249 int active_ss_mask[8];
252 if (get_bits_left(&s->gb) < 52)
255 skip_bits(&s->gb, 8); // user data
256 ss_index = get_bits(&s->gb, 2);
258 blownup = get_bits1(&s->gb);
259 skip_bits(&s->gb, 8 + 4 * blownup); // header_size
260 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
262 s->static_fields = get_bits1(&s->gb);
263 if (s->static_fields) {
264 skip_bits(&s->gb, 2); // reference clock code
265 skip_bits(&s->gb, 3); // frame duration code
267 if (get_bits1(&s->gb))
268 skip_bits_long(&s->gb, 36); // timestamp
270 /* a single stream can contain multiple audio assets that can be
271 * combined to form multiple audio presentations */
273 num_audiop = get_bits(&s->gb, 3) + 1;
274 if (num_audiop > 1) {
275 avpriv_request_sample(s->avctx,
276 "Multiple DTS-HD audio presentations");
277 /* ignore such streams for now */
281 num_assets = get_bits(&s->gb, 3) + 1;
282 if (num_assets > 1) {
283 avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
284 /* ignore such streams for now */
288 for (i = 0; i < num_audiop; i++)
289 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
291 for (i = 0; i < num_audiop; i++)
292 for (j = 0; j <= ss_index; j++)
293 if (active_ss_mask[i] & (1 << j))
294 skip_bits(&s->gb, 8); // active asset mask
296 s->mix_metadata = get_bits1(&s->gb);
297 if (s->mix_metadata) {
298 int mix_out_mask_size;
300 skip_bits(&s->gb, 2); // adjustment level
301 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
302 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
304 for (i = 0; i < s->num_mix_configs; i++) {
305 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
306 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
311 for (i = 0; i < num_assets; i++)
312 skip_bits_long(&s->gb, 16 + 4 * blownup); // asset size
314 for (i = 0; i < num_assets; i++) {
315 if (dca_exss_parse_asset_header(s))
319 /* not parsed further, we were only interested in the extensions mask
320 * from the asset header */