1 /*
2 * This file is part of libdcadec.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published by the
6 * Free Software Foundation; either version 2.1 of the License, or (at your
7 * option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
12 * for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "common.h"
20 #include "exss_parser.h"
21
22 // Table 8-5: Sample rate decoding
23 const uint32_t exss_sample_rates[16] = {
24 8000, 16000, 32000, 64000,
25 128000, 22050, 44100, 88200,
26 176400, 352800, 12000, 24000,
27 48000, 96000, 192000, 384000
28 };
29
count_chs_for_mask(int mask)30 static int count_chs_for_mask(int mask)
31 {
32 return dca_popcount(mask) + dca_popcount(mask & SPEAKER_PAIR_ALL_2);
33 }
34
parse_xll_parameters(struct exss_asset * asset)35 static void parse_xll_parameters(struct exss_asset *asset)
36 {
37 struct exss_parser *exss = asset->parser;
38
39 // Size of XLL data in extension substream
40 asset->xll_size = bits_get(&exss->bits, exss->exss_size_nbits) + 1;
41 // XLL sync word present flag
42 asset->xll_sync_present = bits_get1(&exss->bits);
43 if (asset->xll_sync_present) {
44 // Peak bit rate smoothing buffer size
45 bits_skip(&exss->bits, 4);
46 // Number of bits for XLL decoding delay
47 int xll_delay_nbits = bits_get(&exss->bits, 5) + 1;
48 // Initial XLL decoding delay in frames
49 asset->xll_delay_nframes = bits_get(&exss->bits, xll_delay_nbits);
50 // Number of bytes offset to XLL sync
51 asset->xll_sync_offset = bits_get(&exss->bits, exss->exss_size_nbits);
52 } else {
53 asset->xll_delay_nframes = 0;
54 asset->xll_sync_offset = 0;
55 }
56 }
57
parse_lbr_parameters(struct exss_asset * asset)58 static void parse_lbr_parameters(struct exss_asset *asset)
59 {
60 struct exss_parser *exss = asset->parser;
61
62 // Size of LBR component in extension substream
63 asset->lbr_size = bits_get(&exss->bits, 14) + 1;
64 // LBR sync word present flag
65 if (bits_get1(&exss->bits))
66 // LBR sync distance
67 bits_skip(&exss->bits, 2);
68 }
69
parse_descriptor(struct exss_asset * asset)70 static int parse_descriptor(struct exss_asset *asset)
71 {
72 struct exss_parser *exss = asset->parser;
73 int i, j, ret, descr_pos = exss->bits.index;
74
75 // Size of audio asset descriptor in bytes
76 int descr_size = bits_get(&exss->bits, 9) + 1;
77
78 // Audio asset identifier
79 asset->asset_index = bits_get(&exss->bits, 3);
80
81 //
82 // Per stream static metadata
83 //
84
85 if (exss->static_fields_present) {
86 // Asset type descriptor presence
87 if (bits_get1(&exss->bits))
88 // Asset type descriptor
89 bits_skip(&exss->bits, 4);
90
91 // Language descriptor presence
92 if (bits_get1(&exss->bits))
93 // Language descriptor
94 bits_skip(&exss->bits, 24);
95
96 // Additional textual information presence
97 if (bits_get1(&exss->bits)) {
98 // Byte size of additional text info
99 int text_size = bits_get(&exss->bits, 10) + 1;
100 // Additional textual information string
101 bits_skip(&exss->bits, text_size * 8);
102 }
103
104 // PCM bit resolution
105 asset->pcm_bit_res = bits_get(&exss->bits, 5) + 1;
106
107 // Maximum sample rate
108 asset->max_sample_rate = exss_sample_rates[bits_get(&exss->bits, 4)];
109
110 // Total number of channels
111 asset->nchannels_total = bits_get(&exss->bits, 8) + 1;
112
113 // One to one map channel to speakers
114 asset->one_to_one_map_ch_to_spkr = bits_get1(&exss->bits);
115 if (asset->one_to_one_map_ch_to_spkr) {
116 // Embedded stereo flag
117 if (asset->nchannels_total > 2)
118 asset->embedded_stereo = bits_get1(&exss->bits);
119
120 // Embedded 6 channels flag
121 if (asset->nchannels_total > 6)
122 asset->embedded_6ch = bits_get1(&exss->bits);
123
124 // Speaker mask enabled flag
125 asset->spkr_mask_enabled = bits_get1(&exss->bits);
126
127 int spkr_mask_nbits = 0;
128 if (asset->spkr_mask_enabled) {
129 // Number of bits for speaker activity mask
130 spkr_mask_nbits = (bits_get(&exss->bits, 2) + 1) << 2;
131 // Loudspeaker activity mask
132 asset->spkr_mask = bits_get(&exss->bits, spkr_mask_nbits);
133 }
134
135 // Number of speaker remapping sets
136 int spkr_remap_nsets = bits_get(&exss->bits, 3);
137 if (spkr_remap_nsets && !spkr_mask_nbits) {
138 exss_err("Speaker mask disabled yet there are remapping sets");
139 return -DCADEC_EBADDATA;
140 }
141
142 // Standard loudspeaker layout mask
143 int nspeakers[8];
144 for (i = 0; i < spkr_remap_nsets; i++)
145 nspeakers[i] = count_chs_for_mask(bits_get(&exss->bits, spkr_mask_nbits));
146
147 for (i = 0; i < spkr_remap_nsets; i++) {
148 // Number of channels to be decoded for speaker remapping
149 int nch_for_remaps = bits_get(&exss->bits, 5) + 1;
150 for (j = 0; j < nspeakers[i]; j++) {
151 // Decoded channels to output speaker mapping mask
152 int remap_ch_mask = bits_get(&exss->bits, nch_for_remaps);
153 // Loudspeaker remapping codes
154 int ncodes = dca_popcount(remap_ch_mask);
155 bits_skip(&exss->bits, ncodes * 5);
156 }
157 }
158 } else {
159 asset->embedded_stereo = false;
160 asset->embedded_6ch = false;
161 asset->spkr_mask_enabled = false;
162 asset->spkr_mask = 0;
163
164 // Representation type
165 asset->representation_type = bits_get(&exss->bits, 3);
166 }
167 }
168
169 //
170 // DRC, DNC and mixing metadata
171 //
172
173 // Dynamic range coefficient presence flag
174 bool drc_present = bits_get1(&exss->bits);
175
176 // Code for dynamic range coefficient
177 if (drc_present)
178 bits_skip(&exss->bits, 8);
179
180 // Dialog normalization presence flag
181 if (bits_get1(&exss->bits))
182 // Dialog normalization code
183 bits_skip(&exss->bits, 5);
184
185 // DRC for stereo downmix
186 if (drc_present && asset->embedded_stereo)
187 bits_skip(&exss->bits, 8);
188
189 // Mixing metadata presence flag
190 if (exss->mix_metadata_enabled && bits_get1(&exss->bits)) {
191 // External mixing flag
192 bits_skip1(&exss->bits);
193
194 // Post mixing / replacement gain adjustment
195 bits_skip(&exss->bits, 6);
196
197 // DRC prior to mixing
198 if (bits_get(&exss->bits, 2) == 3)
199 // Custom code for mixing DRC
200 bits_skip(&exss->bits, 8);
201 else
202 // Limit for mixing DRC
203 bits_skip(&exss->bits, 3);
204
205 // Scaling type for channels of main audio
206 // Scaling parameters of main audio
207 if (bits_get1(&exss->bits))
208 for (i = 0; i < exss->nmixoutconfigs; i++)
209 bits_skip(&exss->bits, 6 * exss->nmixoutchs[i]);
210 else
211 bits_skip(&exss->bits, 6 * exss->nmixoutconfigs);
212
213 int nchannels_dmix = asset->nchannels_total;
214 if (asset->embedded_6ch)
215 nchannels_dmix += 6;
216 if (asset->embedded_stereo)
217 nchannels_dmix += 2;
218 for (i = 0; i < exss->nmixoutconfigs; i++) {
219 for (j = 0; j < nchannels_dmix; j++) {
220 // Mix output mask
221 int mix_map_mask = bits_get(&exss->bits, exss->nmixoutchs[i]);
222 // Mixing coefficients
223 int nmixcoefs = dca_popcount(mix_map_mask);
224 bits_skip(&exss->bits, 6 * nmixcoefs);
225 }
226 }
227 }
228
229 //
230 // Decoder navigation data
231 //
232
233 // Coding mode for the asset
234 asset->coding_mode = bits_get(&exss->bits, 2);
235
236 // Coding components used in asset
237 switch (asset->coding_mode) {
238 case 0: // Coding mode that may contain multiple coding components
239 asset->extension_mask = bits_get(&exss->bits, 12);
240 if (asset->extension_mask & EXSS_CORE) {
241 // Size of core component in extension substream
242 asset->core_size = bits_get(&exss->bits, 14) + 1;
243 // Core sync word present flag
244 if (bits_get1(&exss->bits))
245 // Core sync distance
246 bits_skip(&exss->bits, 2);
247 }
248 if (asset->extension_mask & EXSS_XBR)
249 // Size of XBR extension in extension substream
250 asset->xbr_size = bits_get(&exss->bits, 14) + 1;
251 if (asset->extension_mask & EXSS_XXCH)
252 // Size of XXCH extension in extension substream
253 asset->xxch_size = bits_get(&exss->bits, 14) + 1;
254 if (asset->extension_mask & EXSS_X96)
255 // Size of X96 extension in extension substream
256 asset->x96_size = bits_get(&exss->bits, 12) + 1;
257 if (asset->extension_mask & EXSS_LBR)
258 parse_lbr_parameters(asset);
259 if (asset->extension_mask & EXSS_XLL)
260 parse_xll_parameters(asset);
261 if (asset->extension_mask & EXSS_RSV1)
262 bits_skip(&exss->bits, 16);
263 if (asset->extension_mask & EXSS_RSV2)
264 bits_skip(&exss->bits, 16);
265 break;
266
267 case 1: // Loss-less coding mode without CBR component
268 asset->extension_mask = EXSS_XLL;
269 parse_xll_parameters(asset);
270 break;
271
272 case 2: // Low bit rate mode
273 asset->extension_mask = EXSS_LBR;
274 parse_lbr_parameters(asset);
275 break;
276
277 case 3: // Auxiliary coding mode
278 asset->extension_mask = 0;
279 // Size of auxiliary coded data
280 bits_skip(&exss->bits, 14);
281 // Auxiliary codec identification
282 bits_skip(&exss->bits, 8);
283 // Aux sync word present flag
284 if (bits_get1(&exss->bits))
285 // Aux sync distance
286 bits_skip(&exss->bits, 3);
287 break;
288 }
289
290 if (asset->extension_mask & EXSS_XLL)
291 // DTS-HD stream ID
292 asset->hd_stream_id = bits_get(&exss->bits, 3);
293
294 // One to one mixing flag
295 // Per channel main audio scaling flag
296 // Main audio scaling codes
297 // Decode asset in secondary decoder flag
298 // Revision 2 DRC metadata
299 // Reserved
300 // Zero pad
301 if ((ret = bits_seek(&exss->bits, descr_pos + descr_size * 8)) < 0)
302 exss_err("Read past end of asset descriptor");
303 return ret;
304 }
305
set_exss_offsets(struct exss_asset * asset)306 static int set_exss_offsets(struct exss_asset *asset)
307 {
308 int offs = asset->asset_offset;
309 int size = asset->asset_size;
310
311 if (asset->extension_mask & EXSS_CORE) {
312 asset->core_offset = offs;
313 if (offs & 3 || asset->core_size > size)
314 return -DCADEC_EBADREAD;
315 offs += asset->core_size;
316 size -= asset->core_size;
317 }
318
319 if (asset->extension_mask & EXSS_XBR) {
320 asset->xbr_offset = offs;
321 if (offs & 3 || asset->xbr_size > size)
322 return -DCADEC_EBADREAD;
323 offs += asset->xbr_size;
324 size -= asset->xbr_size;
325 }
326
327 if (asset->extension_mask & EXSS_XXCH) {
328 asset->xxch_offset = offs;
329 if (offs & 3 || asset->xxch_size > size)
330 return -DCADEC_EBADREAD;
331 offs += asset->xxch_size;
332 size -= asset->xxch_size;
333 }
334
335 if (asset->extension_mask & EXSS_X96) {
336 asset->x96_offset = offs;
337 if (offs & 3 || asset->x96_size > size)
338 return -DCADEC_EBADREAD;
339 offs += asset->x96_size;
340 size -= asset->x96_size;
341 }
342
343 if (asset->extension_mask & EXSS_LBR) {
344 asset->lbr_offset = offs;
345 if (offs & 3 || asset->lbr_size > size)
346 return -DCADEC_EBADREAD;
347 offs += asset->lbr_size;
348 size -= asset->lbr_size;
349 }
350
351 if (asset->extension_mask & EXSS_XLL) {
352 asset->xll_offset = offs;
353 if (offs & 3 || asset->xll_size > size)
354 return -DCADEC_EBADREAD;
355 offs += asset->xll_size;
356 size -= asset->xll_size;
357 }
358
359 return 0;
360 }
361
exss_parse(struct exss_parser * exss,uint8_t * data,int size)362 int exss_parse(struct exss_parser *exss, uint8_t *data, int size)
363 {
364 int i, j, ret;
365
366 bits_init(&exss->bits, data, size);
367
368 // Extension substream sync word
369 bits_skip(&exss->bits, 32);
370
371 // User defined bits
372 bits_skip(&exss->bits, 8);
373
374 // Extension substream index
375 exss->exss_index = bits_get(&exss->bits, 2);
376
377 // Flag indicating short or long header size
378 bool wide_hdr = bits_get1(&exss->bits);
379
380 // Extension substream header length
381 int header_size = bits_get(&exss->bits, 8 + 4 * wide_hdr) + 1;
382
383 // Check CRC
384 if ((ret = bits_check_crc(&exss->bits, 32 + 8, header_size * 8)) < 0) {
385 exss_err("Invalid EXSS header checksum");
386 return ret;
387 }
388
389 exss->exss_size_nbits = 16 + 4 * wide_hdr;
390
391 // Number of bytes of extension substream
392 exss->exss_size = bits_get(&exss->bits, exss->exss_size_nbits) + 1;
393 if (exss->exss_size > size) {
394 exss_err("Packet too short for EXSS frame");
395 return -DCADEC_EBADDATA;
396 }
397
398 // Per stream static fields presence flag
399 exss->static_fields_present = bits_get1(&exss->bits);
400 if (exss->static_fields_present) {
401 // Reference clock code
402 bits_skip(&exss->bits, 2);
403
404 // Extension substream frame duration
405 bits_skip(&exss->bits, 3);
406
407 // Timecode presence flag
408 if (bits_get1(&exss->bits)) {
409 // Timecode data
410 bits_skip(&exss->bits, 32);
411 bits_skip(&exss->bits, 4);
412 }
413
414 // Number of defined audio presentations
415 exss->npresents = bits_get(&exss->bits, 3) + 1;
416
417 // Number of audio assets in extension substream
418 exss->nassets = bits_get(&exss->bits, 3) + 1;
419
420 // Active extension substream mask for audio presentation
421 int active_exss_mask[8];
422 for (i = 0; i < exss->npresents; i++)
423 active_exss_mask[i] = bits_get(&exss->bits, exss->exss_index + 1);
424
425 // Active audio asset mask
426 for (i = 0; i < exss->npresents; i++)
427 for (j = 0; j <= exss->exss_index; j++)
428 if (active_exss_mask[i] & (1 << j))
429 bits_skip(&exss->bits, 8);
430
431 // Mixing metadata enable flag
432 exss->mix_metadata_enabled = bits_get1(&exss->bits);
433 if (exss->mix_metadata_enabled) {
434 // Mixing metadata adjustment level
435 bits_skip(&exss->bits, 2);
436
437 // Number of bits for mixer output speaker activity mask
438 int spkr_mask_nbits = (bits_get(&exss->bits, 2) + 1) << 2;
439
440 // Number of mixing configurations
441 exss->nmixoutconfigs = bits_get(&exss->bits, 2) + 1;
442
443 // Speaker layout mask for mixer output channels
444 for (i = 0; i < exss->nmixoutconfigs; i++)
445 exss->nmixoutchs[i] = count_chs_for_mask(bits_get(&exss->bits, spkr_mask_nbits));
446 }
447 } else {
448 exss->npresents = 1;
449 exss->nassets = 1;
450 }
451
452 // Reject unsupported features for now
453 if (exss->exss_index > 0 || exss->npresents != 1 || exss->nassets != 1) {
454 exss_err_once("Multiple sub-streams, audio presentations "
455 "and/or assets are not supported");
456 return -DCADEC_ENOSUP;
457 }
458
459 // Reallocate assets
460 if (ta_zalloc_fast(exss, &exss->assets, exss->nassets, sizeof(struct exss_asset)) < 0)
461 return -DCADEC_ENOMEM;
462
463 // Size of encoded asset data in bytes
464 int offset = header_size;
465 for (i = 0; i < exss->nassets; i++) {
466 exss->assets[i].asset_offset = offset;
467 exss->assets[i].asset_size = bits_get(&exss->bits, exss->exss_size_nbits) + 1;
468 offset += exss->assets[i].asset_size;
469 if (offset > exss->exss_size) {
470 exss_err("Asset out of bounds");
471 return -DCADEC_EBADDATA;
472 }
473 }
474
475 // Audio asset descriptor
476 for (i = 0; i < exss->nassets; i++) {
477 exss->assets[i].parser = exss;
478 if ((ret = parse_descriptor(&exss->assets[i])) < 0)
479 return ret;
480 if ((ret = set_exss_offsets(&exss->assets[i])) < 0) {
481 exss_err("Invalid extension size in asset descriptor");
482 return ret;
483 }
484 }
485
486 // Backward compatible core present
487 // Backward compatible core substream index
488 // Backward compatible core asset index
489 // Reserved
490 // Byte align
491 // CRC16 of extension substream header
492 if ((ret = bits_seek(&exss->bits, header_size * 8)) < 0)
493 exss_err("Read past end of EXSS header");
494 return ret;
495 }
496
exss_get_info(struct exss_parser * exss)497 struct dcadec_exss_info *exss_get_info(struct exss_parser *exss)
498 {
499 struct dcadec_exss_info *info = ta_znew(NULL, struct dcadec_exss_info);
500 if (!info)
501 return NULL;
502
503 struct exss_asset *asset = &exss->assets[0];
504
505 info->nchannels = asset->nchannels_total;
506 info->sample_rate = asset->max_sample_rate;
507 info->bits_per_sample = asset->pcm_bit_res;
508
509 if (asset->extension_mask & EXSS_XLL)
510 info->profile = DCADEC_PROFILE_HD_MA;
511 else if (asset->extension_mask & (EXSS_XBR | EXSS_XXCH | EXSS_X96))
512 info->profile = DCADEC_PROFILE_HD_HRA;
513 else if (asset->extension_mask & EXSS_LBR)
514 info->profile = DCADEC_PROFILE_EXPRESS;
515 else
516 info->profile = DCADEC_PROFILE_UNKNOWN;
517
518 info->embedded_stereo = asset->embedded_stereo;
519 info->embedded_6ch = asset->embedded_6ch;
520
521 if (asset->spkr_mask_enabled)
522 info->spkr_mask = asset->spkr_mask;
523 else if (asset->nchannels_total == 2)
524 info->spkr_mask = SPEAKER_PAIR_LR;
525
526 if (!asset->one_to_one_map_ch_to_spkr) {
527 if (asset->representation_type == REPR_TYPE_LtRt)
528 info->matrix_encoding = DCADEC_MATRIX_ENCODING_SURROUND;
529 else if (asset->representation_type == REPR_TYPE_LhRh)
530 info->matrix_encoding = DCADEC_MATRIX_ENCODING_HEADPHONE;
531 }
532
533 return info;
534 }
535