4 * This file is part of FFmpeg.
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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"
28 /* extensions that reside in core substream */
29 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
31 /* these are unconfirmed but should be mostly correct */
32 enum DCAExSSSpeakerMask {
33 DCA_EXSS_FRONT_CENTER = 0x0001,
34 DCA_EXSS_FRONT_LEFT_RIGHT = 0x0002,
35 DCA_EXSS_SIDE_REAR_LEFT_RIGHT = 0x0004,
36 DCA_EXSS_LFE = 0x0008,
37 DCA_EXSS_REAR_CENTER = 0x0010,
38 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT = 0x0020,
39 DCA_EXSS_REAR_LEFT_RIGHT = 0x0040,
40 DCA_EXSS_FRONT_HIGH_CENTER = 0x0080,
41 DCA_EXSS_OVERHEAD = 0x0100,
42 DCA_EXSS_CENTER_LEFT_RIGHT = 0x0200,
43 DCA_EXSS_WIDE_LEFT_RIGHT = 0x0400,
44 DCA_EXSS_SIDE_LEFT_RIGHT = 0x0800,
45 DCA_EXSS_LFE2 = 0x1000,
46 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT = 0x2000,
47 DCA_EXSS_REAR_HIGH_CENTER = 0x4000,
48 DCA_EXSS_REAR_HIGH_LEFT_RIGHT = 0x8000,
52 * Return the number of channels in an ExSS speaker mask (HD)
54 static int dca_exss_mask2count(int mask)
56 /* count bits that mean speaker pairs twice */
57 return av_popcount(mask) +
58 av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
59 DCA_EXSS_FRONT_LEFT_RIGHT |
60 DCA_EXSS_FRONT_HIGH_LEFT_RIGHT |
61 DCA_EXSS_WIDE_LEFT_RIGHT |
62 DCA_EXSS_SIDE_LEFT_RIGHT |
63 DCA_EXSS_SIDE_HIGH_LEFT_RIGHT |
64 DCA_EXSS_SIDE_REAR_LEFT_RIGHT |
65 DCA_EXSS_REAR_LEFT_RIGHT |
66 DCA_EXSS_REAR_HIGH_LEFT_RIGHT));
70 * Skip mixing coefficients of a single mix out configuration (HD)
72 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
76 for (i = 0; i < channels; i++) {
77 int mix_map_mask = get_bits(gb, out_ch);
78 int num_coeffs = av_popcount(mix_map_mask);
79 skip_bits_long(gb, num_coeffs * 6);
84 * Parse extension substream asset header (HD)
86 static int dca_exss_parse_asset_header(DCAContext *s)
88 int header_pos = get_bits_count(&s->gb);
91 int embedded_stereo = 0;
94 int extensions_mask = 0;
97 if (get_bits_left(&s->gb) < 16)
98 return AVERROR_INVALIDDATA;
100 /* We will parse just enough to get to the extensions bitmask with which
101 * we can set the profile value. */
103 header_size = get_bits(&s->gb, 9) + 1;
104 skip_bits(&s->gb, 3); // asset index
106 if (s->static_fields) {
107 if (get_bits1(&s->gb))
108 skip_bits(&s->gb, 4); // asset type descriptor
109 if (get_bits1(&s->gb))
110 skip_bits_long(&s->gb, 24); // language descriptor
112 if (get_bits1(&s->gb)) {
113 /* How can one fit 1024 bytes of text here if the maximum value
114 * for the asset header size field above was 512 bytes? */
115 int text_length = get_bits(&s->gb, 10) + 1;
116 if (get_bits_left(&s->gb) < text_length * 8)
117 return AVERROR_INVALIDDATA;
118 skip_bits_long(&s->gb, text_length * 8); // info text
121 skip_bits(&s->gb, 5); // bit resolution - 1
122 skip_bits(&s->gb, 4); // max sample rate code
123 channels = get_bits(&s->gb, 8) + 1;
125 if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
127 int spkr_mask_size = 16;
131 embedded_stereo = get_bits1(&s->gb);
133 embedded_6ch = get_bits1(&s->gb);
135 if (get_bits1(&s->gb)) {
136 spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
137 skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
140 spkr_remap_sets = get_bits(&s->gb, 3);
142 for (i = 0; i < spkr_remap_sets; i++) {
143 /* std layout mask for each remap set */
144 num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
147 for (i = 0; i < spkr_remap_sets; i++) {
148 int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
149 if (get_bits_left(&s->gb) < 0)
150 return AVERROR_INVALIDDATA;
152 for (j = 0; j < num_spkrs[i]; j++) {
153 int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
154 int num_dec_ch = av_popcount(remap_dec_ch_mask);
155 skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
159 skip_bits(&s->gb, 3); // representation type
163 drc_code_present = get_bits1(&s->gb);
164 if (drc_code_present)
165 get_bits(&s->gb, 8); // drc code
167 if (get_bits1(&s->gb))
168 skip_bits(&s->gb, 5); // dialog normalization code
170 if (drc_code_present && embedded_stereo)
171 get_bits(&s->gb, 8); // drc stereo code
173 if (s->mix_metadata && get_bits1(&s->gb)) {
174 skip_bits(&s->gb, 1); // external mix
175 skip_bits(&s->gb, 6); // post mix gain code
177 if (get_bits(&s->gb, 2) != 3) // mixer drc code
178 skip_bits(&s->gb, 3); // drc limit
180 skip_bits(&s->gb, 8); // custom drc code
182 if (get_bits1(&s->gb)) // channel specific scaling
183 for (i = 0; i < s->num_mix_configs; i++)
184 skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
186 skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
188 for (i = 0; i < s->num_mix_configs; i++) {
189 if (get_bits_left(&s->gb) < 0)
190 return AVERROR_INVALIDDATA;
191 dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
193 dca_exss_skip_mix_coeffs(&s->gb, 6, s->mix_config_num_ch[i]);
195 dca_exss_skip_mix_coeffs(&s->gb, 2, s->mix_config_num_ch[i]);
199 switch (get_bits(&s->gb, 2)) {
201 extensions_mask = get_bits(&s->gb, 12);
204 extensions_mask = DCA_EXT_EXSS_XLL;
207 extensions_mask = DCA_EXT_EXSS_LBR;
210 extensions_mask = 0; /* aux coding */
214 /* not parsed further, we were only interested in the extensions mask */
216 if (get_bits_left(&s->gb) < 0)
217 return AVERROR_INVALIDDATA;
219 if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
220 av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
221 return AVERROR_INVALIDDATA;
223 skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
225 if (extensions_mask & DCA_EXT_EXSS_XLL)
226 s->profile = FF_PROFILE_DTS_HD_MA;
227 else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
229 s->profile = FF_PROFILE_DTS_HD_HRA;
231 if (!(extensions_mask & DCA_EXT_CORE))
232 av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
233 if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
234 av_log(s->avctx, AV_LOG_WARNING,
235 "DTS extensions detection mismatch (%d, %d)\n",
236 extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
242 * Parse extension substream header (HD)
244 void ff_dca_exss_parse_header(DCAContext *s)
251 int active_ss_mask[8];
257 if (get_bits_left(&s->gb) < 52)
260 start_posn = get_bits_count(&s->gb) - 32;
262 skip_bits(&s->gb, 8); // user data
263 ss_index = get_bits(&s->gb, 2);
265 blownup = get_bits1(&s->gb);
266 hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
267 skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
269 s->static_fields = get_bits1(&s->gb);
270 if (s->static_fields) {
271 skip_bits(&s->gb, 2); // reference clock code
272 skip_bits(&s->gb, 3); // frame duration code
274 if (get_bits1(&s->gb))
275 skip_bits_long(&s->gb, 36); // timestamp
277 /* a single stream can contain multiple audio assets that can be
278 * combined to form multiple audio presentations */
280 num_audiop = get_bits(&s->gb, 3) + 1;
281 if (num_audiop > 1) {
282 avpriv_request_sample(s->avctx,
283 "Multiple DTS-HD audio presentations");
284 /* ignore such streams for now */
288 num_assets = get_bits(&s->gb, 3) + 1;
289 if (num_assets > 1) {
290 avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
291 /* ignore such streams for now */
295 for (i = 0; i < num_audiop; i++)
296 active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
298 for (i = 0; i < num_audiop; i++)
299 for (j = 0; j <= ss_index; j++)
300 if (active_ss_mask[i] & (1 << j))
301 skip_bits(&s->gb, 8); // active asset mask
303 s->mix_metadata = get_bits1(&s->gb);
304 if (s->mix_metadata) {
305 int mix_out_mask_size;
307 skip_bits(&s->gb, 2); // adjustment level
308 mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
309 s->num_mix_configs = get_bits(&s->gb, 2) + 1;
311 for (i = 0; i < s->num_mix_configs; i++) {
312 int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
313 s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
318 av_assert0(num_assets > 0); // silence a warning
320 for (i = 0; i < num_assets; i++)
321 asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
323 for (i = 0; i < num_assets; i++) {
324 if (dca_exss_parse_asset_header(s))
328 /* not parsed further, we were only interested in the extensions mask
329 * from the asset header */
331 j = get_bits_count(&s->gb);
332 if (start_posn + hdrsize * 8 > j)
333 skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
335 for (i = 0; i < num_assets; i++) {
336 start_posn = get_bits_count(&s->gb);
337 mkr = get_bits_long(&s->gb, 32);
339 /* parse extensions that we know about */
340 if (mkr == 0x655e315e) {
341 ff_dca_xbr_parse_frame(s);
342 } else if (mkr == 0x47004a03) {
343 ff_dca_xxch_decode_frame(s);
344 s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
346 av_log(s->avctx, AV_LOG_DEBUG,
347 "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
350 /* skip to end of block */
351 j = get_bits_count(&s->gb);
352 if (start_posn + asset_size[i] * 8 > j)
353 skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);