2 * Copyright (C) 2016 foo86
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
24 static int count_chs_for_mask(int mask)
26 return av_popcount(mask) + av_popcount(mask & 0xae66);
29 static void parse_xll_parameters(DCAExssParser *s, DCAExssAsset *asset)
31 // Size of XLL data in extension substream
32 asset->xll_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
34 // XLL sync word present flag
35 if (asset->xll_sync_present = get_bits1(&s->gb)) {
38 // Peak bit rate smoothing buffer size
41 // Number of bits for XLL decoding delay
42 xll_delay_nbits = get_bits(&s->gb, 5) + 1;
44 // Initial XLL decoding delay in frames
45 asset->xll_delay_nframes = get_bits_long(&s->gb, xll_delay_nbits);
47 // Number of bytes offset to XLL sync
48 asset->xll_sync_offset = get_bits(&s->gb, s->exss_size_nbits);
50 asset->xll_delay_nframes = 0;
51 asset->xll_sync_offset = 0;
55 static void parse_lbr_parameters(DCAExssParser *s, DCAExssAsset *asset)
57 // Size of LBR component in extension substream
58 asset->lbr_size = get_bits(&s->gb, 14) + 1;
60 // LBR sync word present flag
61 if (get_bits1(&s->gb))
66 static int parse_descriptor(DCAExssParser *s, DCAExssAsset *asset)
68 int i, j, drc_present, descr_size, descr_pos = get_bits_count(&s->gb);
70 // Size of audio asset descriptor in bytes
71 descr_size = get_bits(&s->gb, 9) + 1;
73 // Audio asset identifier
74 asset->asset_index = get_bits(&s->gb, 3);
77 // Per stream static metadata
80 if (s->static_fields_present) {
81 // Asset type descriptor presence
82 if (get_bits1(&s->gb))
83 // Asset type descriptor
86 // Language descriptor presence
87 if (get_bits1(&s->gb))
88 // Language descriptor
89 skip_bits(&s->gb, 24);
91 // Additional textual information presence
92 if (get_bits1(&s->gb)) {
93 // Byte size of additional text info
94 int text_size = get_bits(&s->gb, 10) + 1;
96 // Sanity check available size
97 if (get_bits_left(&s->gb) < text_size * 8)
98 return AVERROR_INVALIDDATA;
100 // Additional textual information string
101 skip_bits_long(&s->gb, text_size * 8);
104 // PCM bit resolution
105 asset->pcm_bit_res = get_bits(&s->gb, 5) + 1;
107 // Maximum sample rate
108 asset->max_sample_rate = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
110 // Total number of channels
111 asset->nchannels_total = get_bits(&s->gb, 8) + 1;
113 // One to one map channel to speakers
114 if (asset->one_to_one_map_ch_to_spkr = get_bits1(&s->gb)) {
115 int spkr_mask_nbits = 0;
116 int spkr_remap_nsets;
119 // Embedded stereo flag
120 if (asset->nchannels_total > 2)
121 asset->embedded_stereo = get_bits1(&s->gb);
123 // Embedded 6 channels flag
124 if (asset->nchannels_total > 6)
125 asset->embedded_6ch = get_bits1(&s->gb);
127 // Speaker mask enabled flag
128 if (asset->spkr_mask_enabled = get_bits1(&s->gb)) {
129 // Number of bits for speaker activity mask
130 spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;
132 // Loudspeaker activity mask
133 asset->spkr_mask = get_bits(&s->gb, spkr_mask_nbits);
136 // Number of speaker remapping sets
137 if ((spkr_remap_nsets = get_bits(&s->gb, 3)) && !spkr_mask_nbits) {
138 av_log(s->avctx, AV_LOG_ERROR, "Speaker mask disabled yet there are remapping sets\n");
139 return AVERROR_INVALIDDATA;
142 // Standard loudspeaker layout mask
143 for (i = 0; i < spkr_remap_nsets; i++)
144 nspeakers[i] = count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
146 for (i = 0; i < spkr_remap_nsets; i++) {
147 // Number of channels to be decoded for speaker remapping
148 int nch_for_remaps = get_bits(&s->gb, 5) + 1;
150 for (j = 0; j < nspeakers[i]; j++) {
151 // Decoded channels to output speaker mapping mask
152 int remap_ch_mask = get_bits_long(&s->gb, nch_for_remaps);
154 // Loudspeaker remapping codes
155 skip_bits_long(&s->gb, av_popcount(remap_ch_mask) * 5);
159 asset->embedded_stereo = 0;
160 asset->embedded_6ch = 0;
161 asset->spkr_mask_enabled = 0;
162 asset->spkr_mask = 0;
164 // Representation type
165 asset->representation_type = get_bits(&s->gb, 3);
170 // DRC, DNC and mixing metadata
173 // Dynamic range coefficient presence flag
174 drc_present = get_bits1(&s->gb);
176 // Code for dynamic range coefficient
178 skip_bits(&s->gb, 8);
180 // Dialog normalization presence flag
181 if (get_bits1(&s->gb))
182 // Dialog normalization code
183 skip_bits(&s->gb, 5);
185 // DRC for stereo downmix
186 if (drc_present && asset->embedded_stereo)
187 skip_bits(&s->gb, 8);
189 // Mixing metadata presence flag
190 if (s->mix_metadata_enabled && get_bits1(&s->gb)) {
193 // External mixing flag
196 // Post mixing / replacement gain adjustment
197 skip_bits(&s->gb, 6);
199 // DRC prior to mixing
200 if (get_bits(&s->gb, 2) == 3)
201 // Custom code for mixing DRC
202 skip_bits(&s->gb, 8);
204 // Limit for mixing DRC
205 skip_bits(&s->gb, 3);
207 // Scaling type for channels of main audio
208 // Scaling parameters of main audio
209 if (get_bits1(&s->gb))
210 for (i = 0; i < s->nmixoutconfigs; i++)
211 skip_bits_long(&s->gb, 6 * s->nmixoutchs[i]);
213 skip_bits_long(&s->gb, 6 * s->nmixoutconfigs);
215 nchannels_dmix = asset->nchannels_total;
216 if (asset->embedded_6ch)
218 if (asset->embedded_stereo)
221 for (i = 0; i < s->nmixoutconfigs; i++) {
222 if (!s->nmixoutchs[i]) {
223 av_log(s->avctx, AV_LOG_ERROR, "Invalid speaker layout mask for mixing configuration\n");
224 return AVERROR_INVALIDDATA;
226 for (j = 0; j < nchannels_dmix; j++) {
228 int mix_map_mask = get_bits(&s->gb, s->nmixoutchs[i]);
230 // Mixing coefficients
231 skip_bits_long(&s->gb, av_popcount(mix_map_mask) * 6);
237 // Decoder navigation data
240 // Coding mode for the asset
241 asset->coding_mode = get_bits(&s->gb, 2);
243 // Coding components used in asset
244 switch (asset->coding_mode) {
245 case 0: // Coding mode that may contain multiple coding components
246 asset->extension_mask = get_bits(&s->gb, 12);
248 if (asset->extension_mask & DCA_EXSS_CORE) {
249 // Size of core component in extension substream
250 asset->core_size = get_bits(&s->gb, 14) + 1;
251 // Core sync word present flag
252 if (get_bits1(&s->gb))
253 // Core sync distance
254 skip_bits(&s->gb, 2);
257 if (asset->extension_mask & DCA_EXSS_XBR)
258 // Size of XBR extension in extension substream
259 asset->xbr_size = get_bits(&s->gb, 14) + 1;
261 if (asset->extension_mask & DCA_EXSS_XXCH)
262 // Size of XXCH extension in extension substream
263 asset->xxch_size = get_bits(&s->gb, 14) + 1;
265 if (asset->extension_mask & DCA_EXSS_X96)
266 // Size of X96 extension in extension substream
267 asset->x96_size = get_bits(&s->gb, 12) + 1;
269 if (asset->extension_mask & DCA_EXSS_LBR)
270 parse_lbr_parameters(s, asset);
272 if (asset->extension_mask & DCA_EXSS_XLL)
273 parse_xll_parameters(s, asset);
275 if (asset->extension_mask & DCA_EXSS_RSV1)
276 skip_bits(&s->gb, 16);
278 if (asset->extension_mask & DCA_EXSS_RSV2)
279 skip_bits(&s->gb, 16);
282 case 1: // Loss-less coding mode without CBR component
283 asset->extension_mask = DCA_EXSS_XLL;
284 parse_xll_parameters(s, asset);
287 case 2: // Low bit rate mode
288 asset->extension_mask = DCA_EXSS_LBR;
289 parse_lbr_parameters(s, asset);
292 case 3: // Auxiliary coding mode
293 asset->extension_mask = 0;
295 // Size of auxiliary coded data
296 skip_bits(&s->gb, 14);
298 // Auxiliary codec identification
299 skip_bits(&s->gb, 8);
301 // Aux sync word present flag
302 if (get_bits1(&s->gb))
304 skip_bits(&s->gb, 3);
308 if (asset->extension_mask & DCA_EXSS_XLL)
310 asset->hd_stream_id = get_bits(&s->gb, 3);
312 // One to one mixing flag
313 // Per channel main audio scaling flag
314 // Main audio scaling codes
315 // Decode asset in secondary decoder flag
316 // Revision 2 DRC metadata
319 if (ff_dca_seek_bits(&s->gb, descr_pos + descr_size * 8)) {
320 av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS asset descriptor\n");
321 return AVERROR_INVALIDDATA;
327 static int set_exss_offsets(DCAExssAsset *asset)
329 int offs = asset->asset_offset;
330 int size = asset->asset_size;
332 if (asset->extension_mask & DCA_EXSS_CORE) {
333 asset->core_offset = offs;
334 if (asset->core_size > size)
335 return AVERROR_INVALIDDATA;
336 offs += asset->core_size;
337 size -= asset->core_size;
340 if (asset->extension_mask & DCA_EXSS_XBR) {
341 asset->xbr_offset = offs;
342 if (asset->xbr_size > size)
343 return AVERROR_INVALIDDATA;
344 offs += asset->xbr_size;
345 size -= asset->xbr_size;
348 if (asset->extension_mask & DCA_EXSS_XXCH) {
349 asset->xxch_offset = offs;
350 if (asset->xxch_size > size)
351 return AVERROR_INVALIDDATA;
352 offs += asset->xxch_size;
353 size -= asset->xxch_size;
356 if (asset->extension_mask & DCA_EXSS_X96) {
357 asset->x96_offset = offs;
358 if (asset->x96_size > size)
359 return AVERROR_INVALIDDATA;
360 offs += asset->x96_size;
361 size -= asset->x96_size;
364 if (asset->extension_mask & DCA_EXSS_LBR) {
365 asset->lbr_offset = offs;
366 if (asset->lbr_size > size)
367 return AVERROR_INVALIDDATA;
368 offs += asset->lbr_size;
369 size -= asset->lbr_size;
372 if (asset->extension_mask & DCA_EXSS_XLL) {
373 asset->xll_offset = offs;
374 if (asset->xll_size > size)
375 return AVERROR_INVALIDDATA;
376 offs += asset->xll_size;
377 size -= asset->xll_size;
383 int ff_dca_exss_parse(DCAExssParser *s, uint8_t *data, int size)
385 int i, ret, offset, wide_hdr, header_size;
387 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
390 // Extension substream sync word
391 skip_bits_long(&s->gb, 32);
394 skip_bits(&s->gb, 8);
396 // Extension substream index
397 s->exss_index = get_bits(&s->gb, 2);
399 // Flag indicating short or long header size
400 wide_hdr = get_bits1(&s->gb);
402 // Extension substream header length
403 header_size = get_bits(&s->gb, 8 + 4 * wide_hdr) + 1;
406 if ((s->avctx->err_recognition & (AV_EF_CRCCHECK | AV_EF_CAREFUL))
407 && ff_dca_check_crc(&s->gb, 32 + 8, header_size * 8)) {
408 av_log(s->avctx, AV_LOG_ERROR, "Invalid EXSS header checksum\n");
409 return AVERROR_INVALIDDATA;
412 s->exss_size_nbits = 16 + 4 * wide_hdr;
414 // Number of bytes of extension substream
415 s->exss_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
416 if (s->exss_size > size) {
417 av_log(s->avctx, AV_LOG_ERROR, "Packet too short for EXSS frame\n");
418 return AVERROR_INVALIDDATA;
421 // Per stream static fields presence flag
422 if (s->static_fields_present = get_bits1(&s->gb)) {
423 int active_exss_mask[8];
425 // Reference clock code
426 skip_bits(&s->gb, 2);
428 // Extension substream frame duration
429 skip_bits(&s->gb, 3);
431 // Timecode presence flag
432 if (get_bits1(&s->gb))
434 skip_bits_long(&s->gb, 36);
436 // Number of defined audio presentations
437 s->npresents = get_bits(&s->gb, 3) + 1;
438 if (s->npresents > 1) {
439 avpriv_request_sample(s->avctx, "%d audio presentations", s->npresents);
440 return AVERROR_PATCHWELCOME;
443 // Number of audio assets in extension substream
444 s->nassets = get_bits(&s->gb, 3) + 1;
445 if (s->nassets > 1) {
446 avpriv_request_sample(s->avctx, "%d audio assets", s->nassets);
447 return AVERROR_PATCHWELCOME;
450 // Active extension substream mask for audio presentation
451 for (i = 0; i < s->npresents; i++)
452 active_exss_mask[i] = get_bits(&s->gb, s->exss_index + 1);
454 // Active audio asset mask
455 for (i = 0; i < s->npresents; i++)
456 skip_bits_long(&s->gb, av_popcount(active_exss_mask[i]) * 8);
458 // Mixing metadata enable flag
459 if (s->mix_metadata_enabled = get_bits1(&s->gb)) {
462 // Mixing metadata adjustment level
463 skip_bits(&s->gb, 2);
465 // Number of bits for mixer output speaker activity mask
466 spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;
468 // Number of mixing configurations
469 s->nmixoutconfigs = get_bits(&s->gb, 2) + 1;
471 // Speaker layout mask for mixer output channels
472 for (i = 0; i < s->nmixoutconfigs; i++)
473 s->nmixoutchs[i] = count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
480 // Size of encoded asset data in bytes
481 offset = header_size;
482 for (i = 0; i < s->nassets; i++) {
483 s->assets[i].asset_offset = offset;
484 s->assets[i].asset_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
485 offset += s->assets[i].asset_size;
486 if (offset > s->exss_size) {
487 av_log(s->avctx, AV_LOG_ERROR, "EXSS asset out of bounds\n");
488 return AVERROR_INVALIDDATA;
492 // Audio asset descriptor
493 for (i = 0; i < s->nassets; i++) {
494 if ((ret = parse_descriptor(s, &s->assets[i])) < 0)
496 if ((ret = set_exss_offsets(&s->assets[i])) < 0) {
497 av_log(s->avctx, AV_LOG_ERROR, "Invalid extension size in EXSS asset descriptor\n");
502 // Backward compatible core present
503 // Backward compatible core substream index
504 // Backward compatible core asset index
507 // CRC16 of extension substream header
508 if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
509 av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS header\n");
510 return AVERROR_INVALIDDATA;