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 void parse_xll_parameters(DCAExssParser *s, DCAExssAsset *asset)
26 // Size of XLL data in extension substream
27 asset->xll_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
29 // XLL sync word present flag
30 if (asset->xll_sync_present = get_bits1(&s->gb)) {
33 // Peak bit rate smoothing buffer size
36 // Number of bits for XLL decoding delay
37 xll_delay_nbits = get_bits(&s->gb, 5) + 1;
39 // Initial XLL decoding delay in frames
40 asset->xll_delay_nframes = get_bits_long(&s->gb, xll_delay_nbits);
42 // Number of bytes offset to XLL sync
43 asset->xll_sync_offset = get_bits(&s->gb, s->exss_size_nbits);
45 asset->xll_delay_nframes = 0;
46 asset->xll_sync_offset = 0;
50 static void parse_lbr_parameters(DCAExssParser *s, DCAExssAsset *asset)
52 // Size of LBR component in extension substream
53 asset->lbr_size = get_bits(&s->gb, 14) + 1;
55 // LBR sync word present flag
56 if (get_bits1(&s->gb))
61 static int parse_descriptor(DCAExssParser *s, DCAExssAsset *asset)
63 int i, j, drc_present, descr_size, descr_pos = get_bits_count(&s->gb);
65 // Size of audio asset descriptor in bytes
66 descr_size = get_bits(&s->gb, 9) + 1;
68 // Audio asset identifier
69 asset->asset_index = get_bits(&s->gb, 3);
72 // Per stream static metadata
75 if (s->static_fields_present) {
76 // Asset type descriptor presence
77 if (get_bits1(&s->gb))
78 // Asset type descriptor
81 // Language descriptor presence
82 if (get_bits1(&s->gb))
83 // Language descriptor
84 skip_bits(&s->gb, 24);
86 // Additional textual information presence
87 if (get_bits1(&s->gb)) {
88 // Byte size of additional text info
89 int text_size = get_bits(&s->gb, 10) + 1;
91 // Sanity check available size
92 if (get_bits_left(&s->gb) < text_size * 8)
93 return AVERROR_INVALIDDATA;
95 // Additional textual information string
96 skip_bits_long(&s->gb, text_size * 8);
100 asset->pcm_bit_res = get_bits(&s->gb, 5) + 1;
102 // Maximum sample rate
103 asset->max_sample_rate = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
105 // Total number of channels
106 asset->nchannels_total = get_bits(&s->gb, 8) + 1;
108 // One to one map channel to speakers
109 if (asset->one_to_one_map_ch_to_spkr = get_bits1(&s->gb)) {
110 int spkr_mask_nbits = 0;
111 int spkr_remap_nsets;
114 // Embedded stereo flag
115 if (asset->nchannels_total > 2)
116 asset->embedded_stereo = get_bits1(&s->gb);
118 // Embedded 6 channels flag
119 if (asset->nchannels_total > 6)
120 asset->embedded_6ch = get_bits1(&s->gb);
122 // Speaker mask enabled flag
123 if (asset->spkr_mask_enabled = get_bits1(&s->gb)) {
124 // Number of bits for speaker activity mask
125 spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;
127 // Loudspeaker activity mask
128 asset->spkr_mask = get_bits(&s->gb, spkr_mask_nbits);
131 // Number of speaker remapping sets
132 if ((spkr_remap_nsets = get_bits(&s->gb, 3)) && !spkr_mask_nbits) {
133 av_log(s->avctx, AV_LOG_ERROR, "Speaker mask disabled yet there are remapping sets\n");
134 return AVERROR_INVALIDDATA;
137 // Standard loudspeaker layout mask
138 for (i = 0; i < spkr_remap_nsets; i++)
139 nspeakers[i] = ff_dca_count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
141 for (i = 0; i < spkr_remap_nsets; i++) {
142 // Number of channels to be decoded for speaker remapping
143 int nch_for_remaps = get_bits(&s->gb, 5) + 1;
145 for (j = 0; j < nspeakers[i]; j++) {
146 // Decoded channels to output speaker mapping mask
147 int remap_ch_mask = get_bits_long(&s->gb, nch_for_remaps);
149 // Loudspeaker remapping codes
150 skip_bits_long(&s->gb, av_popcount(remap_ch_mask) * 5);
154 asset->embedded_stereo = 0;
155 asset->embedded_6ch = 0;
156 asset->spkr_mask_enabled = 0;
157 asset->spkr_mask = 0;
159 // Representation type
160 asset->representation_type = get_bits(&s->gb, 3);
165 // DRC, DNC and mixing metadata
168 // Dynamic range coefficient presence flag
169 drc_present = get_bits1(&s->gb);
171 // Code for dynamic range coefficient
173 skip_bits(&s->gb, 8);
175 // Dialog normalization presence flag
176 if (get_bits1(&s->gb))
177 // Dialog normalization code
178 skip_bits(&s->gb, 5);
180 // DRC for stereo downmix
181 if (drc_present && asset->embedded_stereo)
182 skip_bits(&s->gb, 8);
184 // Mixing metadata presence flag
185 if (s->mix_metadata_enabled && get_bits1(&s->gb)) {
188 // External mixing flag
191 // Post mixing / replacement gain adjustment
192 skip_bits(&s->gb, 6);
194 // DRC prior to mixing
195 if (get_bits(&s->gb, 2) == 3)
196 // Custom code for mixing DRC
197 skip_bits(&s->gb, 8);
199 // Limit for mixing DRC
200 skip_bits(&s->gb, 3);
202 // Scaling type for channels of main audio
203 // Scaling parameters of main audio
204 if (get_bits1(&s->gb))
205 for (i = 0; i < s->nmixoutconfigs; i++)
206 skip_bits_long(&s->gb, 6 * s->nmixoutchs[i]);
208 skip_bits_long(&s->gb, 6 * s->nmixoutconfigs);
210 nchannels_dmix = asset->nchannels_total;
211 if (asset->embedded_6ch)
213 if (asset->embedded_stereo)
216 for (i = 0; i < s->nmixoutconfigs; i++) {
217 if (!s->nmixoutchs[i]) {
218 av_log(s->avctx, AV_LOG_ERROR, "Invalid speaker layout mask for mixing configuration\n");
219 return AVERROR_INVALIDDATA;
221 for (j = 0; j < nchannels_dmix; j++) {
223 int mix_map_mask = get_bits(&s->gb, s->nmixoutchs[i]);
225 // Mixing coefficients
226 skip_bits_long(&s->gb, av_popcount(mix_map_mask) * 6);
232 // Decoder navigation data
235 // Coding mode for the asset
236 asset->coding_mode = get_bits(&s->gb, 2);
238 // Coding components used in asset
239 switch (asset->coding_mode) {
240 case 0: // Coding mode that may contain multiple coding components
241 asset->extension_mask = get_bits(&s->gb, 12);
243 if (asset->extension_mask & DCA_EXSS_CORE) {
244 // Size of core component in extension substream
245 asset->core_size = get_bits(&s->gb, 14) + 1;
246 // Core sync word present flag
247 if (get_bits1(&s->gb))
248 // Core sync distance
249 skip_bits(&s->gb, 2);
252 if (asset->extension_mask & DCA_EXSS_XBR)
253 // Size of XBR extension in extension substream
254 asset->xbr_size = get_bits(&s->gb, 14) + 1;
256 if (asset->extension_mask & DCA_EXSS_XXCH)
257 // Size of XXCH extension in extension substream
258 asset->xxch_size = get_bits(&s->gb, 14) + 1;
260 if (asset->extension_mask & DCA_EXSS_X96)
261 // Size of X96 extension in extension substream
262 asset->x96_size = get_bits(&s->gb, 12) + 1;
264 if (asset->extension_mask & DCA_EXSS_LBR)
265 parse_lbr_parameters(s, asset);
267 if (asset->extension_mask & DCA_EXSS_XLL)
268 parse_xll_parameters(s, asset);
270 if (asset->extension_mask & DCA_EXSS_RSV1)
271 skip_bits(&s->gb, 16);
273 if (asset->extension_mask & DCA_EXSS_RSV2)
274 skip_bits(&s->gb, 16);
277 case 1: // Loss-less coding mode without CBR component
278 asset->extension_mask = DCA_EXSS_XLL;
279 parse_xll_parameters(s, asset);
282 case 2: // Low bit rate mode
283 asset->extension_mask = DCA_EXSS_LBR;
284 parse_lbr_parameters(s, asset);
287 case 3: // Auxiliary coding mode
288 asset->extension_mask = 0;
290 // Size of auxiliary coded data
291 skip_bits(&s->gb, 14);
293 // Auxiliary codec identification
294 skip_bits(&s->gb, 8);
296 // Aux sync word present flag
297 if (get_bits1(&s->gb))
299 skip_bits(&s->gb, 3);
303 if (asset->extension_mask & DCA_EXSS_XLL)
305 asset->hd_stream_id = get_bits(&s->gb, 3);
307 // One to one mixing flag
308 // Per channel main audio scaling flag
309 // Main audio scaling codes
310 // Decode asset in secondary decoder flag
311 // Revision 2 DRC metadata
314 if (ff_dca_seek_bits(&s->gb, descr_pos + descr_size * 8)) {
315 av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS asset descriptor\n");
316 return AVERROR_INVALIDDATA;
322 static int set_exss_offsets(DCAExssAsset *asset)
324 int offs = asset->asset_offset;
325 int size = asset->asset_size;
327 if (asset->extension_mask & DCA_EXSS_CORE) {
328 asset->core_offset = offs;
329 if (asset->core_size > size)
330 return AVERROR_INVALIDDATA;
331 offs += asset->core_size;
332 size -= asset->core_size;
335 if (asset->extension_mask & DCA_EXSS_XBR) {
336 asset->xbr_offset = offs;
337 if (asset->xbr_size > size)
338 return AVERROR_INVALIDDATA;
339 offs += asset->xbr_size;
340 size -= asset->xbr_size;
343 if (asset->extension_mask & DCA_EXSS_XXCH) {
344 asset->xxch_offset = offs;
345 if (asset->xxch_size > size)
346 return AVERROR_INVALIDDATA;
347 offs += asset->xxch_size;
348 size -= asset->xxch_size;
351 if (asset->extension_mask & DCA_EXSS_X96) {
352 asset->x96_offset = offs;
353 if (asset->x96_size > size)
354 return AVERROR_INVALIDDATA;
355 offs += asset->x96_size;
356 size -= asset->x96_size;
359 if (asset->extension_mask & DCA_EXSS_LBR) {
360 asset->lbr_offset = offs;
361 if (asset->lbr_size > size)
362 return AVERROR_INVALIDDATA;
363 offs += asset->lbr_size;
364 size -= asset->lbr_size;
367 if (asset->extension_mask & DCA_EXSS_XLL) {
368 asset->xll_offset = offs;
369 if (asset->xll_size > size)
370 return AVERROR_INVALIDDATA;
371 offs += asset->xll_size;
372 size -= asset->xll_size;
378 int ff_dca_exss_parse(DCAExssParser *s, uint8_t *data, int size)
380 int i, ret, offset, wide_hdr, header_size;
382 if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
385 // Extension substream sync word
386 skip_bits_long(&s->gb, 32);
389 skip_bits(&s->gb, 8);
391 // Extension substream index
392 s->exss_index = get_bits(&s->gb, 2);
394 // Flag indicating short or long header size
395 wide_hdr = get_bits1(&s->gb);
397 // Extension substream header length
398 header_size = get_bits(&s->gb, 8 + 4 * wide_hdr) + 1;
401 if (ff_dca_check_crc(s->avctx, &s->gb, 32 + 8, header_size * 8)) {
402 av_log(s->avctx, AV_LOG_ERROR, "Invalid EXSS header checksum\n");
403 return AVERROR_INVALIDDATA;
406 s->exss_size_nbits = 16 + 4 * wide_hdr;
408 // Number of bytes of extension substream
409 s->exss_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
410 if (s->exss_size > size) {
411 av_log(s->avctx, AV_LOG_ERROR, "Packet too short for EXSS frame\n");
412 return AVERROR_INVALIDDATA;
415 // Per stream static fields presence flag
416 if (s->static_fields_present = get_bits1(&s->gb)) {
417 int active_exss_mask[8];
419 // Reference clock code
420 skip_bits(&s->gb, 2);
422 // Extension substream frame duration
423 skip_bits(&s->gb, 3);
425 // Timecode presence flag
426 if (get_bits1(&s->gb))
428 skip_bits_long(&s->gb, 36);
430 // Number of defined audio presentations
431 s->npresents = get_bits(&s->gb, 3) + 1;
432 if (s->npresents > 1) {
433 avpriv_request_sample(s->avctx, "%d audio presentations", s->npresents);
434 return AVERROR_PATCHWELCOME;
437 // Number of audio assets in extension substream
438 s->nassets = get_bits(&s->gb, 3) + 1;
439 if (s->nassets > 1) {
440 avpriv_request_sample(s->avctx, "%d audio assets", s->nassets);
441 return AVERROR_PATCHWELCOME;
444 // Active extension substream mask for audio presentation
445 for (i = 0; i < s->npresents; i++)
446 active_exss_mask[i] = get_bits(&s->gb, s->exss_index + 1);
448 // Active audio asset mask
449 for (i = 0; i < s->npresents; i++)
450 skip_bits_long(&s->gb, av_popcount(active_exss_mask[i]) * 8);
452 // Mixing metadata enable flag
453 if (s->mix_metadata_enabled = get_bits1(&s->gb)) {
456 // Mixing metadata adjustment level
457 skip_bits(&s->gb, 2);
459 // Number of bits for mixer output speaker activity mask
460 spkr_mask_nbits = (get_bits(&s->gb, 2) + 1) << 2;
462 // Number of mixing configurations
463 s->nmixoutconfigs = get_bits(&s->gb, 2) + 1;
465 // Speaker layout mask for mixer output channels
466 for (i = 0; i < s->nmixoutconfigs; i++)
467 s->nmixoutchs[i] = ff_dca_count_chs_for_mask(get_bits(&s->gb, spkr_mask_nbits));
474 // Size of encoded asset data in bytes
475 offset = header_size;
476 for (i = 0; i < s->nassets; i++) {
477 s->assets[i].asset_offset = offset;
478 s->assets[i].asset_size = get_bits(&s->gb, s->exss_size_nbits) + 1;
479 offset += s->assets[i].asset_size;
480 if (offset > s->exss_size) {
481 av_log(s->avctx, AV_LOG_ERROR, "EXSS asset out of bounds\n");
482 return AVERROR_INVALIDDATA;
486 // Audio asset descriptor
487 for (i = 0; i < s->nassets; i++) {
488 if ((ret = parse_descriptor(s, &s->assets[i])) < 0)
490 if ((ret = set_exss_offsets(&s->assets[i])) < 0) {
491 av_log(s->avctx, AV_LOG_ERROR, "Invalid extension size in EXSS asset descriptor\n");
496 // Backward compatible core present
497 // Backward compatible core substream index
498 // Backward compatible core asset index
501 // CRC16 of extension substream header
502 if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
503 av_log(s->avctx, AV_LOG_ERROR, "Read past end of EXSS header\n");
504 return AVERROR_INVALIDDATA;