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 "dca_syncwords.h"
26 #include "get_bits.h"
27 
28 /* extensions that reside in core substream */
29 #define DCA_CORE_EXTS (DCA_EXT_XCH | DCA_EXT_XXCH | DCA_EXT_X96)
30 
31 /* these are unconfirmed but should be mostly correct */
36  DCA_EXSS_LFE = 0x0008,
45  DCA_EXSS_LFE2 = 0x1000,
49 };
50 
51 /**
52  * Return the number of channels in an ExSS speaker mask (HD)
53  */
54 static int dca_exss_mask2count(int mask)
55 {
56  /* count bits that mean speaker pairs twice */
57  return av_popcount(mask) +
58  av_popcount(mask & (DCA_EXSS_CENTER_LEFT_RIGHT |
67 }
68 
69 /**
70  * Skip mixing coefficients of a single mix out configuration (HD)
71  */
72 static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
73 {
74  int i;
75 
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);
80  }
81 }
82 
83 /**
84  * Parse extension substream asset header (HD)
85  */
87 {
88  int header_pos = get_bits_count(&s->gb);
89  int header_size;
90  int channels = 0;
91  int embedded_stereo = 0;
92  int embedded_6ch = 0;
93  int drc_code_present;
94  int extensions_mask = 0;
95  int i, j;
96 
97  if (get_bits_left(&s->gb) < 16)
98  return AVERROR_INVALIDDATA;
99 
100  /* We will parse just enough to get to the extensions bitmask with which
101  * we can set the profile value. */
102 
103  header_size = get_bits(&s->gb, 9) + 1;
104  skip_bits(&s->gb, 3); // asset index
105 
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
111 
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
119  }
120 
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;
124 
126  if (s->one2one_map_chtospkr) {
127  int spkr_remap_sets;
128  int spkr_mask_size = 16;
129  int num_spkrs[7];
130 
131  if (channels > 2)
132  embedded_stereo = get_bits1(&s->gb);
133  if (channels > 6)
134  embedded_6ch = get_bits1(&s->gb);
135 
136  if (get_bits1(&s->gb)) {
137  spkr_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
138  skip_bits(&s->gb, spkr_mask_size); // spkr activity mask
139  }
140 
141  spkr_remap_sets = get_bits(&s->gb, 3);
142 
143  for (i = 0; i < spkr_remap_sets; i++) {
144  /* std layout mask for each remap set */
145  num_spkrs[i] = dca_exss_mask2count(get_bits(&s->gb, spkr_mask_size));
146  }
147 
148  for (i = 0; i < spkr_remap_sets; i++) {
149  int num_dec_ch_remaps = get_bits(&s->gb, 5) + 1;
150  if (get_bits_left(&s->gb) < 0)
151  return AVERROR_INVALIDDATA;
152 
153  for (j = 0; j < num_spkrs[i]; j++) {
154  int remap_dec_ch_mask = get_bits_long(&s->gb, num_dec_ch_remaps);
155  int num_dec_ch = av_popcount(remap_dec_ch_mask);
156  skip_bits_long(&s->gb, num_dec_ch * 5); // remap codes
157  }
158  }
159  } else {
160  skip_bits(&s->gb, 3); // representation type
161  }
162  }
163 
164  drc_code_present = get_bits1(&s->gb);
165  if (drc_code_present)
166  get_bits(&s->gb, 8); // drc code
167 
168  if (get_bits1(&s->gb))
169  skip_bits(&s->gb, 5); // dialog normalization code
170 
171  if (drc_code_present && embedded_stereo)
172  get_bits(&s->gb, 8); // drc stereo code
173 
174  if (s->mix_metadata && get_bits1(&s->gb)) {
175  skip_bits(&s->gb, 1); // external mix
176  skip_bits(&s->gb, 6); // post mix gain code
177 
178  if (get_bits(&s->gb, 2) != 3) // mixer drc code
179  skip_bits(&s->gb, 3); // drc limit
180  else
181  skip_bits(&s->gb, 8); // custom drc code
182 
183  if (get_bits1(&s->gb)) // channel specific scaling
184  for (i = 0; i < s->num_mix_configs; i++)
185  skip_bits_long(&s->gb, s->mix_config_num_ch[i] * 6); // scale codes
186  else
187  skip_bits_long(&s->gb, s->num_mix_configs * 6); // scale codes
188 
189  for (i = 0; i < s->num_mix_configs; i++) {
190  if (get_bits_left(&s->gb) < 0)
191  return AVERROR_INVALIDDATA;
192  dca_exss_skip_mix_coeffs(&s->gb, channels, s->mix_config_num_ch[i]);
193  if (embedded_6ch)
195  if (embedded_stereo)
197  }
198  }
199 
200  switch (get_bits(&s->gb, 2)) {
201  case 0:
202  extensions_mask = get_bits(&s->gb, 12);
203  break;
204  case 1:
205  extensions_mask = DCA_EXT_EXSS_XLL;
206  break;
207  case 2:
208  extensions_mask = DCA_EXT_EXSS_LBR;
209  break;
210  case 3:
211  extensions_mask = 0; /* aux coding */
212  break;
213  }
214 
215  /* not parsed further, we were only interested in the extensions mask */
216 
217  if (get_bits_left(&s->gb) < 0)
218  return AVERROR_INVALIDDATA;
219 
220  if (get_bits_count(&s->gb) - header_pos > header_size * 8) {
221  av_log(s->avctx, AV_LOG_WARNING, "Asset header size mismatch.\n");
222  return AVERROR_INVALIDDATA;
223  }
224  skip_bits_long(&s->gb, header_pos + header_size * 8 - get_bits_count(&s->gb));
225 
226  if (extensions_mask & DCA_EXT_EXSS_XLL)
228  else if (extensions_mask & (DCA_EXT_EXSS_XBR | DCA_EXT_EXSS_X96 |
231 
232  if (!(extensions_mask & DCA_EXT_CORE))
233  av_log(s->avctx, AV_LOG_WARNING, "DTS core detection mismatch.\n");
234  if ((extensions_mask & DCA_CORE_EXTS) != s->core_ext_mask)
236  "DTS extensions detection mismatch (%d, %d)\n",
237  extensions_mask & DCA_CORE_EXTS, s->core_ext_mask);
238 
239  return 0;
240 }
241 
242 /**
243  * Parse extension substream header (HD)
244  */
246 {
247  int asset_size[8];
248  int ss_index;
249  int blownup;
250  int num_audiop = 1;
251  int num_assets = 1;
252  int active_ss_mask[8];
253  int i, j;
254  int start_pos;
255  int hdrsize;
256  uint32_t mkr;
257 
258  if (get_bits_left(&s->gb) < 52)
259  return;
260 
261  start_pos = get_bits_count(&s->gb) - 32;
262 
263  skip_bits(&s->gb, 8); // user data
264  ss_index = get_bits(&s->gb, 2);
265 
266  blownup = get_bits1(&s->gb);
267  hdrsize = get_bits(&s->gb, 8 + 4 * blownup) + 1; // header_size
268  skip_bits(&s->gb, 16 + 4 * blownup); // hd_size
269 
270  s->static_fields = get_bits1(&s->gb);
271  if (s->static_fields) {
272  skip_bits(&s->gb, 2); // reference clock code
273  skip_bits(&s->gb, 3); // frame duration code
274 
275  if (get_bits1(&s->gb))
276  skip_bits_long(&s->gb, 36); // timestamp
277 
278  /* a single stream can contain multiple audio assets that can be
279  * combined to form multiple audio presentations */
280 
281  num_audiop = get_bits(&s->gb, 3) + 1;
282  if (num_audiop > 1) {
284  "Multiple DTS-HD audio presentations");
285  /* ignore such streams for now */
286  return;
287  }
288 
289  num_assets = get_bits(&s->gb, 3) + 1;
290  if (num_assets > 1) {
291  avpriv_request_sample(s->avctx, "Multiple DTS-HD audio assets");
292  /* ignore such streams for now */
293  return;
294  }
295 
296  for (i = 0; i < num_audiop; i++)
297  active_ss_mask[i] = get_bits(&s->gb, ss_index + 1);
298 
299  for (i = 0; i < num_audiop; i++)
300  for (j = 0; j <= ss_index; j++)
301  if (active_ss_mask[i] & (1 << j))
302  skip_bits(&s->gb, 8); // active asset mask
303 
304  s->mix_metadata = get_bits1(&s->gb);
305  if (s->mix_metadata) {
306  int mix_out_mask_size;
307 
308  skip_bits(&s->gb, 2); // adjustment level
309  mix_out_mask_size = (get_bits(&s->gb, 2) + 1) << 2;
310  s->num_mix_configs = get_bits(&s->gb, 2) + 1;
311 
312  for (i = 0; i < s->num_mix_configs; i++) {
313  int mix_out_mask = get_bits(&s->gb, mix_out_mask_size);
314  s->mix_config_num_ch[i] = dca_exss_mask2count(mix_out_mask);
315  }
316  }
317  }
318 
319  av_assert0(num_assets > 0); // silence a warning
320 
321  for (i = 0; i < num_assets; i++)
322  asset_size[i] = get_bits_long(&s->gb, 16 + 4 * blownup) + 1;
323 
324  for (i = 0; i < num_assets; i++) {
326  return;
327  }
328 
329  j = get_bits_count(&s->gb);
330  if (start_pos + hdrsize * 8 > j)
331  skip_bits_long(&s->gb, start_pos + hdrsize * 8 - j);
332 
333  for (i = 0; i < num_assets; i++) {
334  int end_pos;
335  start_pos = get_bits_count(&s->gb);
336  end_pos = start_pos + asset_size[i] * 8;
337  mkr = get_bits_long(&s->gb, 32);
338 
339  /* parse extensions that we know about */
340  switch (mkr) {
341  case DCA_SYNCWORD_XBR:
343  break;
344  case DCA_SYNCWORD_XXCH:
346  s->core_ext_mask |= DCA_EXT_XXCH; /* xxx use for chan reordering */
347  break;
348  case DCA_SYNCWORD_XLL:
349  if (s->xll_disable) {
351  "DTS-XLL: ignoring XLL extension\n");
352  break;
353  }
355  "DTS-XLL: decoding XLL extension\n");
356  if (ff_dca_xll_decode_header(s) == 0 &&
357  ff_dca_xll_decode_navi(s, end_pos) == 0)
359  break;
360  default:
362  "DTS-ExSS: unknown marker = 0x%08x\n", mkr);
363  }
364 
365  /* skip to end of block */
366  j = get_bits_count(&s->gb);
367  if (j > end_pos)
369  "DTS-ExSS: Processed asset too long.\n");
370  if (j < end_pos)
371  skip_bits_long(&s->gb, end_pos - j);
372  }
373 }
const char * s
Definition: avisynth_c.h:631
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int ff_dca_xll_decode_header(DCAContext *s)
Definition: dca_xll.c:77
int one2one_map_chtospkr
Definition: dca.h:278
lossless extension in ExSS
Definition: dca.h:77
static void dca_exss_skip_mix_coeffs(GetBitContext *gb, int channels, int out_ch)
Skip mixing coefficients of a single mix out configuration (HD)
Definition: dca_exss.c:72
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:260
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
int mix_config_num_ch[4]
number of channels in each mix out configuration
Definition: dca.h:275
static void skip_bits_long(GetBitContext *s, int n)
Definition: get_bits.h:217
static int dca_exss_parse_asset_header(DCAContext *s)
Parse extension substream asset header (HD)
Definition: dca_exss.c:86
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:212
bitstream reader API header.
AVCodecContext * avctx
Definition: dca.h:137
#define av_log(a,...)
int xll_disable
Definition: dca.h:254
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:588
DCAExSSSpeakerMask
Definition: dca_exss.c:32
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
static const uint16_t mask[17]
Definition: lzw.c:38
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static int dca_exss_mask2count(int mask)
Return the number of channels in an ExSS speaker mask (HD)
Definition: dca_exss.c:54
int mix_metadata
mixing metadata present
Definition: dca.h:273
int ff_dca_xbr_parse_frame(DCAContext *s)
Definition: dcadec.c:1218
low bitrate component in ExSS
Definition: dca.h:76
#define FF_PROFILE_DTS_HD_HRA
Definition: avcodec.h:3133
XXCh channels extension in ExSS.
Definition: dca.h:74
96/24 extension in ExSS
Definition: dca.h:75
int ff_dca_xll_decode_navi(DCAContext *s, int asset_end)
Definition: dca_xll.c:291
int core_ext_mask
present extensions in the core substream
Definition: dca.h:231
#define DCA_CORE_EXTS
Definition: dca_exss.c:29
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
int exss_ext_mask
Non-core extensions.
Definition: dca.h:232
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:297
core in core substream
Definition: dca.h:68
extended bitrate extension in ExSS
Definition: dca.h:73
void ff_dca_exss_parse_header(DCAContext *s)
Parse extension substream header (HD)
Definition: dca_exss.c:245
GetBitContext gb
Definition: dca.h:226
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:337
int ff_dca_xxch_decode_frame(DCAContext *s)
Definition: dcadec.c:1396
int num_mix_configs
number of mix out configurations
Definition: dca.h:274
common internal and external API header
#define FF_PROFILE_DTS_HD_MA
Definition: avcodec.h:3134
int static_fields
static fields present
Definition: dca.h:272
XXCh channels extension in core substream.
Definition: dca.h:69
int profile
Definition: dca.h:277