FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dca_exss.c
Go to the documentation of this file.
1 /*
2  * DCA ExSS extension
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include "libavutil/common.h"
22 #include "libavutil/log.h"
23 
24 #include "dca.h"
25 #include "get_bits.h"
26 
27 /* extensions that reside in core substream */
28 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
29 
30 /* these are unconfirmed but should be mostly correct */
35  DCA_EXSS_LFE = 0x0008,
44  DCA_EXSS_LFE2 = 0x1000,
48 };
49 
50 /**
51  * Return the number of channels in an ExSS speaker mask (HD)
52  */
53 static int dca_exss_mask2count(int mask)
54 {
55  /* count bits that mean speaker pairs twice */
56  return av_popcount(mask) +
57  av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
66 }
67 
68 /**
69  * Skip mixing coefficients of a single mix out configuration (HD)
70  */
71 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
72 {
73  int i;
74 
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);
79  }
80 }
81 
82 /**
83  * Parse extension substream asset header (HD)
84  */
86 {
87  int header_pos = get_bits_count(&s->gb);
88  int header_size;
89  int channels = 0;
90  int embedded_stereo = 0;
91  int embedded_6ch = 0;
92  int drc_code_present;
93  int extensions_mask = 0;
94  int i, j;
95 
96  if (get_bits_left(&s->gb) < 16)
97  return AVERROR_INVALIDDATA;
98 
99  /* We will parse just enough to get to the extensions bitmask with which
100  * we can set the profile value. */
101 
102  header_size = get_bits(&s->gb, 9) + 1;
103  skip_bits(&s->gb, 3); // asset index
104 
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
110 
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
118  }
119 
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;
123 
124  if (get_bits1(&s->gb)) { // 1-to-1 channels to speakers
125  int spkr_remap_sets;
126  int spkr_mask_size = 16;
127  int num_spkrs[7];
128 
129  if (channels > 2)
130  embedded_stereo = get_bits1(&s->gb);
131  if (channels > 6)
132  embedded_6ch = get_bits1(&s->gb);
133 
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
137  }
138 
139  spkr_remap_sets = get_bits(&s->gb, 3);
140 
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));
144  }
145 
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;
150 
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
155  }
156  }
157  } else {
158  skip_bits(&s->gb, 3); // representation type
159  }
160  }
161 
162  drc_code_present = get_bits1(&s->gb);
163  if (drc_code_present)
164  get_bits(&s->gb, 8); // drc code
165 
166  if (get_bits1(&s->gb))
167  skip_bits(&s->gb, 5); // dialog normalization code
168 
169  if (drc_code_present && embedded_stereo)
170  get_bits(&s->gb, 8); // drc stereo code
171 
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
175 
176  if (get_bits(&s->gb, 2) != 3) // mixer drc code
177  skip_bits(&s->gb, 3); // drc limit
178  else
179  skip_bits(&s->gb, 8); // custom drc code
180 
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
184  else
185  skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
186 
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]);
191  if (embedded_6ch)
193  if (embedded_stereo)
195  }
196  }
197 
198  switch (get_bits(&s->gb, 2)) {
199  case 0:
200  extensions_mask = get_bits(&s->gb, 12);
201  break;
202  case 1:
203  extensions_mask = DCA_EXT_EXSS_XLL;
204  break;
205  case 2:
206  extensions_mask = DCA_EXT_EXSS_LBR;
207  break;
208  case 3:
209  extensions_mask = 0; /* aux coding */
210  break;
211  }
212 
213  /* not parsed further, we were only interested in the extensions mask */
214 
215  if (get_bits_left(&s->gb) < 0)
216  return AVERROR_INVALIDDATA;
217 
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;
221  }
222  skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
223 
224  if (extensions_mask & DCA_EXT_EXSS_XLL)
226  else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
229 
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)
234  "DTS extensions detection mismatch (%d, %d)\n",
235  extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
236 
237  return 0;
238 }
239 
240 /**
241  * Parse extension substream header (HD)
242  */
244 {
245  int asset_size[8];
246  int ss_index;
247  int blownup;
248  int num_audiop = 1;
249  int num_assets = 1;
250  int active_ss_mask[8];
251  int i, j;
252  int start_posn;
253  int hdrsize;
254  uint32_t mkr;
255 
256  if (get_bits_left(&s->gb) < 52)
257  return;
258 
259  start_posn = get_bits_count(&s->gb) - 32;
260 
261  skip_bits(&s->gb, 8); // user data
262  ss_index = get_bits(&s->gb, 2);
263 
264  blownup = get_bits1(&s->gb);
265  hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
266  skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
267 
268  s->static_fields = get_bits1(&s->gb);
269  if (s->static_fields) {
270  skip_bits(&s->gb, 2); // reference clock code
271  skip_bits(&s->gb, 3); // frame duration code
272 
273  if (get_bits1(&s->gb))
274  skip_bits_long(&s->gb, 36); // timestamp
275 
276  /* a single stream can contain multiple audio assets that can be
277  * combined to form multiple audio presentations */
278 
279  num_audiop = get_bits(&s->gb, 3) + 1;
280  if (num_audiop > 1) {
282  "Multiple DTS-HD audio presentations");
283  /* ignore such streams for now */
284  return;
285  }
286 
287  num_assets = get_bits(&s->gb, 3) + 1;
288  if (num_assets > 1) {
289  avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
290  /* ignore such streams for now */
291  return;
292  }
293 
294  for (i = 0; i < num_audiop; i++)
295  active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
296 
297  for (i = 0; i < num_audiop; i++)
298  for (j = 0; j <= ss_index; j++)
299  if (active_ss_mask[i] & (1 << j))
300  skip_bits(&s->gb, 8); // active asset mask
301 
302  s->mix_metadata = get_bits1(&s->gb);
303  if (s->mix_metadata) {
304  int mix_out_mask_size;
305 
306  skip_bits(&s->gb, 2); // adjustment level
307  mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
308  s->num_mix_configs = get_bits(&s->gb, 2) + 1;
309 
310  for (i = 0; i < s->num_mix_configs; i++) {
311  int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
312  s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
313  }
314  }
315  }
316 
317  av_assert0(num_assets > 0); // silence a warning
318 
319  for (i = 0; i < num_assets; i++)
320  asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup);
321 
322  for (i = 0; i < num_assets; i++) {
324  return;
325  }
326 
327  /* not parsed further, we were only interested in the extensions mask
328  * from the asset header */
329 
330  j = get_bits_count(&s->gb);
331  if (start_posn + hdrsize * 8 > j)
332  skip_bits_long(&s->gb, start_posn + hdrsize * 8 - j);
333 
334  for (i = 0; i < num_assets; i++) {
335  start_posn = get_bits_count(&s->gb);
336  mkr = get_bits_long(&s->gb, 32);
337 
338  /* parse extensions that we know about */
339  if (mkr == 0x655e315e) {
341  } else if (mkr == 0x47004a03) {
343  s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
344  } else {
346  "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
347  }
348 
349  /* skip to end of block */
350  j = get_bits_count(&s->gb);
351  if (start_posn + asset_size[i] * 8 > j)
352  skip_bits_long(&s->gb, start_posn + asset_size[i] * 8 - j);
353  }
354 }