LCOV - code coverage report
Current view: top level - libavcodec - dca_xll.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 611 802 76.2 %
Date: 2017-12-11 04:34:20 Functions: 33 36 91.7 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2016 foo86
       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 "dcadec.h"
      22             : #include "dcadata.h"
      23             : #include "dcamath.h"
      24             : #include "dca_syncwords.h"
      25             : #include "unary.h"
      26             : 
      27      115057 : static int get_linear(GetBitContext *gb, int n)
      28             : {
      29      115057 :     unsigned int v = get_bits_long(gb, n);
      30      115057 :     return (v >> 1) ^ -(v & 1);
      31             : }
      32             : 
      33     4939345 : static int get_rice_un(GetBitContext *gb, int k)
      34             : {
      35     4939345 :     unsigned int v = get_unary(gb, 1, get_bits_left(gb));
      36     4939345 :     return (v << k) | get_bits_long(gb, k);
      37             : }
      38             : 
      39     4939345 : static int get_rice(GetBitContext *gb, int k)
      40             : {
      41     4939345 :     unsigned int v = get_rice_un(gb, k);
      42     4939345 :     return (v >> 1) ^ -(v & 1);
      43             : }
      44             : 
      45         856 : static void get_array(GetBitContext *gb, int32_t *array, int size, int n)
      46             : {
      47             :     int i;
      48             : 
      49       70488 :     for (i = 0; i < size; i++)
      50       69632 :         array[i] = get_bits(gb, n);
      51         856 : }
      52             : 
      53       14998 : static void get_linear_array(GetBitContext *gb, int32_t *array, int size, int n)
      54             : {
      55             :     int i;
      56             : 
      57       14998 :     if (n == 0)
      58       14327 :         memset(array, 0, sizeof(*array) * size);
      59       66272 :     else for (i = 0; i < size; i++)
      60       65601 :         array[i] = get_linear(gb, n);
      61       14998 : }
      62             : 
      63       47828 : static void get_rice_array(GetBitContext *gb, int32_t *array, int size, int k)
      64             : {
      65             :     int i;
      66             : 
      67     4852877 :     for (i = 0; i < size; i++)
      68     4805049 :         array[i] = get_rice(gb, k);
      69       47828 : }
      70             : 
      71         147 : static int parse_dmix_coeffs(DCAXllDecoder *s, DCAXllChSet *c)
      72             : {
      73             :     // Size of downmix coefficient matrix
      74         147 :     int m = c->primary_chset ? ff_dca_dmix_primary_nch[c->dmix_type] : c->hier_ofs;
      75         147 :     int i, j, *coeff_ptr = c->dmix_coeff;
      76             : 
      77         777 :     for (i = 0; i < m; i++) {
      78         630 :         int code, sign, coeff, scale, scale_inv = 0;
      79             :         unsigned int index;
      80             : 
      81             :         // Downmix scale (only for non-primary channel sets)
      82         630 :         if (!c->primary_chset) {
      83         588 :             code = get_bits(&s->gb, 9);
      84         588 :             sign = (code >> 8) - 1;
      85         588 :             index = (code & 0xff) - FF_DCA_DMIXTABLE_OFFSET;
      86         588 :             if (index >= FF_DCA_INV_DMIXTABLE_SIZE) {
      87           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix scale index\n");
      88           0 :                 return AVERROR_INVALIDDATA;
      89             :             }
      90         588 :             scale = ff_dca_dmixtable[index + FF_DCA_DMIXTABLE_OFFSET];
      91         588 :             scale_inv = ff_dca_inv_dmixtable[index];
      92         588 :             c->dmix_scale[i] = (scale ^ sign) - sign;
      93         588 :             c->dmix_scale_inv[i] = (scale_inv ^ sign) - sign;
      94             :         }
      95             : 
      96             :         // Downmix coefficients
      97        2100 :         for (j = 0; j < c->nchannels; j++) {
      98        1470 :             code = get_bits(&s->gb, 9);
      99        1470 :             sign = (code >> 8) - 1;
     100        1470 :             index = code & 0xff;
     101        1470 :             if (index >= FF_DCA_DMIXTABLE_SIZE) {
     102           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL downmix coefficient index\n");
     103           0 :                 return AVERROR_INVALIDDATA;
     104             :             }
     105        1470 :             coeff = ff_dca_dmixtable[index];
     106        1470 :             if (!c->primary_chset)
     107             :                 // Multiply by |InvDmixScale| to get |UndoDmixScale|
     108        1218 :                 coeff = mul16(scale_inv, coeff);
     109        1470 :             *coeff_ptr++ = (coeff ^ sign) - sign;
     110             :         }
     111             :     }
     112             : 
     113         147 :     return 0;
     114             : }
     115             : 
     116        2652 : static int chs_parse_header(DCAXllDecoder *s, DCAXllChSet *c, DCAExssAsset *asset)
     117             : {
     118        2652 :     int i, j, k, ret, band, header_size, header_pos = get_bits_count(&s->gb);
     119        2652 :     DCAXllChSet *p = &s->chset[0];
     120             :     DCAXllBand *b;
     121             : 
     122             :     // Size of channel set sub-header
     123        2652 :     header_size = get_bits(&s->gb, 10) + 1;
     124             : 
     125             :     // Check CRC
     126        2652 :     if (ff_dca_check_crc(s->avctx, &s->gb, header_pos, header_pos + header_size * 8)) {
     127           0 :         av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL sub-header checksum\n");
     128           0 :         return AVERROR_INVALIDDATA;
     129             :     }
     130             : 
     131             :     // Number of channels in the channel set
     132        2652 :     c->nchannels = get_bits(&s->gb, 4) + 1;
     133        2652 :     if (c->nchannels > DCA_XLL_CHANNELS_MAX) {
     134           0 :         avpriv_request_sample(s->avctx, "%d XLL channels", c->nchannels);
     135           0 :         return AVERROR_PATCHWELCOME;
     136             :     }
     137             : 
     138             :     // Residual type
     139        2652 :     c->residual_encode = get_bits(&s->gb, c->nchannels);
     140             : 
     141             :     // PCM bit resolution
     142        2652 :     c->pcm_bit_res = get_bits(&s->gb, 5) + 1;
     143             : 
     144             :     // Storage unit width
     145        2652 :     c->storage_bit_res = get_bits(&s->gb, 5) + 1;
     146        2652 :     if (c->storage_bit_res != 16 && c->storage_bit_res != 20 && c->storage_bit_res != 24) {
     147           0 :         avpriv_request_sample(s->avctx, "%d-bit XLL storage resolution", c->storage_bit_res);
     148           0 :         return AVERROR_PATCHWELCOME;
     149             :     }
     150             : 
     151        2652 :     if (c->pcm_bit_res > c->storage_bit_res) {
     152           0 :         av_log(s->avctx, AV_LOG_ERROR, "Invalid PCM bit resolution for XLL channel set (%d > %d)\n", c->pcm_bit_res, c->storage_bit_res);
     153           0 :         return AVERROR_INVALIDDATA;
     154             :     }
     155             : 
     156             :     // Original sampling frequency
     157        2652 :     c->freq = ff_dca_sampling_freqs[get_bits(&s->gb, 4)];
     158        2652 :     if (c->freq > 192000) {
     159           0 :         avpriv_request_sample(s->avctx, "%d Hz XLL sampling frequency", c->freq);
     160           0 :         return AVERROR_PATCHWELCOME;
     161             :     }
     162             : 
     163             :     // Sampling frequency modifier
     164        2652 :     if (get_bits(&s->gb, 2)) {
     165           0 :         avpriv_request_sample(s->avctx, "XLL sampling frequency modifier");
     166           0 :         return AVERROR_PATCHWELCOME;
     167             :     }
     168             : 
     169             :     // Which replacement set this channel set is member of
     170        2652 :     if (get_bits(&s->gb, 2)) {
     171           0 :         avpriv_request_sample(s->avctx, "XLL replacement set");
     172           0 :         return AVERROR_PATCHWELCOME;
     173             :     }
     174             : 
     175        2652 :     if (asset->one_to_one_map_ch_to_spkr) {
     176             :         // Primary channel set flag
     177        2652 :         c->primary_chset = get_bits1(&s->gb);
     178        2652 :         if (c->primary_chset != (c == p)) {
     179           0 :             av_log(s->avctx, AV_LOG_ERROR, "The first (and only) XLL channel set must be primary\n");
     180           0 :             return AVERROR_INVALIDDATA;
     181             :         }
     182             : 
     183             :         // Downmix coefficients present in stream
     184        2652 :         c->dmix_coeffs_present = get_bits1(&s->gb);
     185             : 
     186             :         // Downmix already performed by encoder
     187        2652 :         c->dmix_embedded = c->dmix_coeffs_present && get_bits1(&s->gb);
     188             : 
     189             :         // Downmix type
     190        2652 :         if (c->dmix_coeffs_present && c->primary_chset) {
     191          21 :             c->dmix_type = get_bits(&s->gb, 3);
     192          21 :             if (c->dmix_type >= DCA_DMIX_TYPE_COUNT) {
     193           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL primary channel set downmix type\n");
     194           0 :                 return AVERROR_INVALIDDATA;
     195             :             }
     196             :         }
     197             : 
     198             :         // Whether the channel set is part of a hierarchy
     199        2652 :         c->hier_chset = get_bits1(&s->gb);
     200        2652 :         if (!c->hier_chset && s->nchsets != 1) {
     201           0 :             avpriv_request_sample(s->avctx, "XLL channel set outside of hierarchy");
     202           0 :             return AVERROR_PATCHWELCOME;
     203             :         }
     204             : 
     205             :         // Downmix coefficients
     206        2652 :         if (c->dmix_coeffs_present && (ret = parse_dmix_coeffs(s, c)) < 0)
     207           0 :             return ret;
     208             : 
     209             :         // Channel mask enabled
     210        2652 :         if (!get_bits1(&s->gb)) {
     211           0 :             avpriv_request_sample(s->avctx, "Disabled XLL channel mask");
     212           0 :             return AVERROR_PATCHWELCOME;
     213             :         }
     214             : 
     215             :         // Channel mask for set
     216        2652 :         c->ch_mask = get_bits_long(&s->gb, s->ch_mask_nbits);
     217        2652 :         if (av_popcount(c->ch_mask) != c->nchannels) {
     218           0 :             av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL channel mask\n");
     219           0 :             return AVERROR_INVALIDDATA;
     220             :         }
     221             : 
     222             :         // Build the channel to speaker map
     223       49839 :         for (i = 0, j = 0; i < s->ch_mask_nbits; i++)
     224       47187 :             if (c->ch_mask & (1U << i))
     225       10611 :                 c->ch_remap[j++] = i;
     226             :     } else {
     227             :         // Mapping coeffs present flag
     228           0 :         if (c->nchannels != 2 || s->nchsets != 1 || get_bits1(&s->gb)) {
     229           0 :             avpriv_request_sample(s->avctx, "Custom XLL channel to speaker mapping");
     230           0 :             return AVERROR_PATCHWELCOME;
     231             :         }
     232             : 
     233             :         // Setup for LtRt decoding
     234           0 :         c->primary_chset = 1;
     235           0 :         c->dmix_coeffs_present = 0;
     236           0 :         c->dmix_embedded = 0;
     237           0 :         c->hier_chset = 0;
     238           0 :         c->ch_mask = DCA_SPEAKER_LAYOUT_STEREO;
     239           0 :         c->ch_remap[0] = DCA_SPEAKER_L;
     240           0 :         c->ch_remap[1] = DCA_SPEAKER_R;
     241             :     }
     242             : 
     243        2652 :     if (c->freq > 96000) {
     244             :         // Extra frequency bands flag
     245          63 :         if (get_bits1(&s->gb)) {
     246           0 :             avpriv_request_sample(s->avctx, "Extra XLL frequency bands");
     247           0 :             return AVERROR_PATCHWELCOME;
     248             :         }
     249          63 :         c->nfreqbands = 2;
     250             :     } else {
     251        2589 :         c->nfreqbands = 1;
     252             :     }
     253             : 
     254             :     // Set the sampling frequency to that of the first frequency band.
     255             :     // Frequency will be doubled again after bands assembly.
     256        2652 :     c->freq >>= c->nfreqbands - 1;
     257             : 
     258             :     // Verify that all channel sets have the same audio characteristics
     259        2652 :     if (c != p && (c->nfreqbands != p->nfreqbands || c->freq != p->freq
     260        1299 :                    || c->pcm_bit_res != p->pcm_bit_res
     261        1299 :                    || c->storage_bit_res != p->storage_bit_res)) {
     262           0 :         avpriv_request_sample(s->avctx, "Different XLL audio characteristics");
     263           0 :         return AVERROR_PATCHWELCOME;
     264             :     }
     265             : 
     266             :     // Determine number of bits to read bit allocation coding parameter
     267        2652 :     if (c->storage_bit_res > 16)
     268        2589 :         c->nabits = 5;
     269          63 :     else if (c->storage_bit_res > 8)
     270          63 :         c->nabits = 4;
     271             :     else
     272           0 :         c->nabits = 3;
     273             : 
     274             :     // Account for embedded downmix and decimator saturation
     275        2652 :     if ((s->nchsets > 1 || c->nfreqbands > 1) && c->nabits < 5)
     276          63 :         c->nabits++;
     277             : 
     278        5367 :     for (band = 0, b = c->bands; band < c->nfreqbands; band++, b++) {
     279             :         // Pairwise channel decorrelation
     280        5409 :         if ((b->decor_enabled = get_bits1(&s->gb)) && c->nchannels > 1) {
     281        2694 :             int ch_nbits = av_ceil_log2(c->nchannels);
     282             : 
     283             :             // Original channel order
     284       13536 :             for (i = 0; i < c->nchannels; i++) {
     285       10842 :                 b->orig_order[i] = get_bits(&s->gb, ch_nbits);
     286       10842 :                 if (b->orig_order[i] >= c->nchannels) {
     287           0 :                     av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL original channel order\n");
     288           0 :                     return AVERROR_INVALIDDATA;
     289             :                 }
     290             :             }
     291             : 
     292             :             // Pairwise channel coefficients
     293        8115 :             for (i = 0; i < c->nchannels / 2; i++)
     294        5421 :                 b->decor_coeff[i] = get_bits1(&s->gb) ? get_linear(&s->gb, 7) : 0;
     295             :         } else {
     296          42 :             for (i = 0; i < c->nchannels; i++)
     297          21 :                 b->orig_order[i] = i;
     298          21 :             for (i = 0; i < c->nchannels / 2; i++)
     299           0 :                 b->decor_coeff[i] = 0;
     300             :         }
     301             : 
     302             :         // Adaptive predictor order
     303        2715 :         b->highest_pred_order = 0;
     304       13578 :         for (i = 0; i < c->nchannels; i++) {
     305       10863 :             b->adapt_pred_order[i] = get_bits(&s->gb, 4);
     306       10863 :             if (b->adapt_pred_order[i] > b->highest_pred_order)
     307        4074 :                 b->highest_pred_order = b->adapt_pred_order[i];
     308             :         }
     309        2715 :         if (b->highest_pred_order > s->nsegsamples) {
     310           0 :             av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL adaptive predicition order\n");
     311           0 :             return AVERROR_INVALIDDATA;
     312             :         }
     313             : 
     314             :         // Fixed predictor order
     315       13578 :         for (i = 0; i < c->nchannels; i++)
     316       10863 :             b->fixed_pred_order[i] = b->adapt_pred_order[i] ? 0 : get_bits(&s->gb, 2);
     317             : 
     318             :         // Adaptive predictor quantized reflection coefficients
     319       13578 :         for (i = 0; i < c->nchannels; i++) {
     320       53120 :             for (j = 0; j < b->adapt_pred_order[i]; j++) {
     321       42257 :                 k = get_linear(&s->gb, 8);
     322       42257 :                 if (k == -128) {
     323           0 :                     av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL reflection coefficient index\n");
     324           0 :                     return AVERROR_INVALIDDATA;
     325             :                 }
     326       42257 :                 if (k < 0)
     327       15112 :                     b->adapt_refl_coeff[i][j] = -(int)ff_dca_xll_refl_coeff[-k];
     328             :                 else
     329       27145 :                     b->adapt_refl_coeff[i][j] =  (int)ff_dca_xll_refl_coeff[ k];
     330             :             }
     331             :         }
     332             : 
     333             :         // Downmix performed by encoder in extension frequency band
     334        2715 :         b->dmix_embedded = c->dmix_embedded && (band == 0 || get_bits1(&s->gb));
     335             : 
     336             :         // MSB/LSB split flag in extension frequency band
     337        2967 :         if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
     338             :             // Size of LSB section in any segment
     339         252 :             b->lsb_section_size = get_bits_long(&s->gb, s->seg_size_nbits);
     340         252 :             if (b->lsb_section_size < 0 || b->lsb_section_size > s->frame_size) {
     341           0 :                 av_log(s->avctx, AV_LOG_ERROR, "Invalid LSB section size\n");
     342           0 :                 return AVERROR_INVALIDDATA;
     343             :             }
     344             : 
     345             :             // Account for optional CRC bytes after LSB section
     346         252 :             if (b->lsb_section_size && (s->band_crc_present > 2 ||
     347          60 :                                         (band == 0 && s->band_crc_present > 1)))
     348           0 :                 b->lsb_section_size += 2;
     349             : 
     350             :             // Number of bits to represent the samples in LSB part
     351        1155 :             for (i = 0; i < c->nchannels; i++) {
     352         903 :                 b->nscalablelsbs[i] = get_bits(&s->gb, 4);
     353         903 :                 if (b->nscalablelsbs[i] && !b->lsb_section_size) {
     354           0 :                     av_log(s->avctx, AV_LOG_ERROR, "LSB section missing with non-zero LSB width\n");
     355           0 :                     return AVERROR_INVALIDDATA;
     356             :                 }
     357             :             }
     358             :         } else {
     359        2463 :             b->lsb_section_size = 0;
     360       12423 :             for (i = 0; i < c->nchannels; i++)
     361        9960 :                 b->nscalablelsbs[i] = 0;
     362             :         }
     363             : 
     364             :         // Scalable resolution flag in extension frequency band
     365        2967 :         if ((band == 0 && s->scalable_lsbs) || (band != 0 && get_bits1(&s->gb))) {
     366             :             // Number of bits discarded by authoring
     367        1155 :             for (i = 0; i < c->nchannels; i++)
     368         903 :                 b->bit_width_adjust[i] = get_bits(&s->gb, 4);
     369             :         } else {
     370       12423 :             for (i = 0; i < c->nchannels; i++)
     371        9960 :                 b->bit_width_adjust[i] = 0;
     372             :         }
     373             :     }
     374             : 
     375             :     // Reserved
     376             :     // Byte align
     377             :     // CRC16 of channel set sub-header
     378        2652 :     if (ff_dca_seek_bits(&s->gb, header_pos + header_size * 8)) {
     379           0 :         av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL sub-header\n");
     380           0 :         return AVERROR_INVALIDDATA;
     381             :     }
     382             : 
     383        2652 :     return 0;
     384             : }
     385             : 
     386        2582 : static int chs_alloc_msb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
     387             : {
     388        2582 :     int ndecisamples = c->nfreqbands > 1 ? DCA_XLL_DECI_HISTORY_MAX : 0;
     389        2582 :     int nchsamples = s->nframesamples + ndecisamples;
     390        2582 :     int i, j, nsamples = nchsamples * c->nchannels * c->nfreqbands;
     391             :     int32_t *ptr;
     392             : 
     393             :     // Reallocate MSB sample buffer
     394        2582 :     av_fast_malloc(&c->sample_buffer[0], &c->sample_size[0], nsamples * sizeof(int32_t));
     395        2582 :     if (!c->sample_buffer[0])
     396           0 :         return AVERROR(ENOMEM);
     397             : 
     398        2582 :     ptr = c->sample_buffer[0] + ndecisamples;
     399        5220 :     for (i = 0; i < c->nfreqbands; i++) {
     400       13319 :         for (j = 0; j < c->nchannels; j++) {
     401       10681 :             c->bands[i].msb_sample_buffer[j] = ptr;
     402       10681 :             ptr += nchsamples;
     403             :         }
     404             :     }
     405             : 
     406        2582 :     return 0;
     407             : }
     408             : 
     409        2582 : static int chs_alloc_lsb_band_data(DCAXllDecoder *s, DCAXllChSet *c)
     410             : {
     411        2582 :     int i, j, nsamples = 0;
     412             :     int32_t *ptr;
     413             : 
     414             :     // Determine number of frequency bands that have MSB/LSB split
     415        5220 :     for (i = 0; i < c->nfreqbands; i++)
     416        2638 :         if (c->bands[i].lsb_section_size)
     417          56 :             nsamples += s->nframesamples * c->nchannels;
     418        2582 :     if (!nsamples)
     419        2526 :         return 0;
     420             : 
     421             :     // Reallocate LSB sample buffer
     422          56 :     av_fast_malloc(&c->sample_buffer[1], &c->sample_size[1], nsamples * sizeof(int32_t));
     423          56 :     if (!c->sample_buffer[1])
     424           0 :         return AVERROR(ENOMEM);
     425             : 
     426          56 :     ptr = c->sample_buffer[1];
     427         112 :     for (i = 0; i < c->nfreqbands; i++) {
     428          56 :         if (c->bands[i].lsb_section_size) {
     429         328 :             for (j = 0; j < c->nchannels; j++) {
     430         272 :                 c->bands[i].lsb_sample_buffer[j] = ptr;
     431         272 :                 ptr += s->nframesamples;
     432             :             }
     433             :         } else {
     434           0 :             for (j = 0; j < c->nchannels; j++)
     435           0 :                 c->bands[i].lsb_sample_buffer[j] = NULL;
     436             :         }
     437             :     }
     438             : 
     439          56 :     return 0;
     440             : }
     441             : 
     442        7792 : static int chs_parse_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg, int band_data_end)
     443             : {
     444        7792 :     DCAXllBand *b = &c->bands[band];
     445             :     int i, j, k;
     446             : 
     447             :     // Start unpacking MSB portion of the segment
     448        7792 :     if (!(seg && get_bits1(&s->gb))) {
     449             :         // Unpack segment type
     450             :         // 0 - distinct coding parameters for each channel
     451             :         // 1 - common coding parameters for all channels
     452        7792 :         c->seg_common = get_bits1(&s->gb);
     453             : 
     454             :         // Determine number of coding parameters encoded in segment
     455        7792 :         k = c->seg_common ? 1 : c->nchannels;
     456             : 
     457             :         // Unpack Rice coding parameters
     458       33196 :         for (i = 0; i < k; i++) {
     459             :             // Unpack Rice coding flag
     460             :             // 0 - linear code, 1 - Rice code
     461       25404 :             c->rice_code_flag[i] = get_bits1(&s->gb);
     462             :             // Unpack Hybrid Rice coding flag
     463             :             // 0 - Rice code, 1 - Hybrid Rice code
     464       25404 :             if (!c->seg_common && c->rice_code_flag[i] && get_bits1(&s->gb))
     465             :                 // Unpack binary code length for isolated samples
     466         906 :                 c->bitalloc_hybrid_linear[i] = get_bits(&s->gb, c->nabits) + 1;
     467             :             else
     468             :                 // 0 indicates no Hybrid Rice coding
     469       24498 :                 c->bitalloc_hybrid_linear[i] = 0;
     470             :         }
     471             : 
     472             :         // Unpack coding parameters
     473       33196 :         for (i = 0; i < k; i++) {
     474       25404 :             if (seg == 0) {
     475             :                 // Unpack coding parameter for part A of segment 0
     476        8656 :                 c->bitalloc_part_a[i] = get_bits(&s->gb, c->nabits);
     477             : 
     478             :                 // Adjust for the linear code
     479        8656 :                 if (!c->rice_code_flag[i] && c->bitalloc_part_a[i])
     480          72 :                     c->bitalloc_part_a[i]++;
     481             : 
     482        8656 :                 if (!c->seg_common)
     483        7968 :                     c->nsamples_part_a[i] = b->adapt_pred_order[i];
     484             :                 else
     485         688 :                     c->nsamples_part_a[i] = b->highest_pred_order;
     486             :             } else {
     487       16748 :                 c->bitalloc_part_a[i] = 0;
     488       16748 :                 c->nsamples_part_a[i] = 0;
     489             :             }
     490             : 
     491             :             // Unpack coding parameter for part B of segment
     492       25404 :             c->bitalloc_part_b[i] = get_bits(&s->gb, c->nabits);
     493             : 
     494             :             // Adjust for the linear code
     495       25404 :             if (!c->rice_code_flag[i] && c->bitalloc_part_b[i])
     496         599 :                 c->bitalloc_part_b[i]++;
     497             :         }
     498             :     }
     499             : 
     500             :     // Unpack entropy codes
     501       39658 :     for (i = 0; i < c->nchannels; i++) {
     502             :         int32_t *part_a, *part_b;
     503             :         int nsamples_part_b;
     504             : 
     505             :         // Select index of coding parameters
     506       31866 :         k = c->seg_common ? 0 : i;
     507             : 
     508             :         // Slice the segment into parts A and B
     509       31866 :         part_a = b->msb_sample_buffer[i] + seg * s->nsegsamples;
     510       31866 :         part_b = part_a + c->nsamples_part_a[k];
     511       31866 :         nsamples_part_b = s->nsegsamples - c->nsamples_part_a[k];
     512             : 
     513       31866 :         if (get_bits_left(&s->gb) < 0)
     514           0 :             return AVERROR_INVALIDDATA;
     515             : 
     516       31866 :         if (!c->rice_code_flag[k]) {
     517             :             // Linear codes
     518             :             // Unpack all residuals of part A of segment 0
     519        7499 :             get_linear_array(&s->gb, part_a, c->nsamples_part_a[k],
     520             :                              c->bitalloc_part_a[k]);
     521             : 
     522             :             // Unpack all residuals of part B of segment 0 and others
     523        7499 :             get_linear_array(&s->gb, part_b, nsamples_part_b,
     524             :                              c->bitalloc_part_b[k]);
     525             :         } else {
     526             :             // Rice codes
     527             :             // Unpack all residuals of part A of segment 0
     528       24367 :             get_rice_array(&s->gb, part_a, c->nsamples_part_a[k],
     529             :                            c->bitalloc_part_a[k]);
     530             : 
     531       24367 :             if (c->bitalloc_hybrid_linear[k]) {
     532             :                 // Hybrid Rice codes
     533             :                 // Unpack the number of isolated samples
     534         906 :                 int nisosamples = get_bits(&s->gb, s->nsegsamples_log2);
     535             : 
     536             :                 // Set all locations to 0
     537         906 :                 memset(part_b, 0, sizeof(*part_b) * nsamples_part_b);
     538             : 
     539             :                 // Extract the locations of isolated samples and flag by -1
     540        6137 :                 for (j = 0; j < nisosamples; j++) {
     541        5231 :                     int loc = get_bits(&s->gb, s->nsegsamples_log2);
     542        5231 :                     if (loc >= nsamples_part_b) {
     543           0 :                         av_log(s->avctx, AV_LOG_ERROR, "Invalid isolated sample location\n");
     544           0 :                         return AVERROR_INVALIDDATA;
     545             :                     }
     546        5231 :                     part_b[loc] = -1;
     547             :                 }
     548             : 
     549             :                 // Unpack all residuals of part B of segment 0 and others
     550      140433 :                 for (j = 0; j < nsamples_part_b; j++) {
     551      139527 :                     if (part_b[j])
     552        5231 :                         part_b[j] = get_linear(&s->gb, c->bitalloc_hybrid_linear[k]);
     553             :                     else
     554      134296 :                         part_b[j] = get_rice(&s->gb, c->bitalloc_part_b[k]);
     555             :                 }
     556             :             } else {
     557             :                 // Rice codes
     558             :                 // Unpack all residuals of part B of segment 0 and others
     559       23461 :                 get_rice_array(&s->gb, part_b, nsamples_part_b, c->bitalloc_part_b[k]);
     560             :             }
     561             :         }
     562             :     }
     563             : 
     564             :     // Unpack decimator history for frequency band 1
     565        7792 :     if (seg == 0 && band == 1) {
     566          56 :         int nbits = get_bits(&s->gb, 5) + 1;
     567         280 :         for (i = 0; i < c->nchannels; i++)
     568        1792 :             for (j = 1; j < DCA_XLL_DECI_HISTORY_MAX; j++)
     569        1568 :                 c->deci_history[i][j] = get_sbits_long(&s->gb, nbits);
     570             :     }
     571             : 
     572             :     // Start unpacking LSB portion of the segment
     573        7792 :     if (b->lsb_section_size) {
     574             :         // Skip to the start of LSB portion
     575         428 :         if (ff_dca_seek_bits(&s->gb, band_data_end - b->lsb_section_size * 8)) {
     576           0 :             av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
     577           0 :             return AVERROR_INVALIDDATA;
     578             :         }
     579             : 
     580             :         // Unpack all LSB parts of residuals of this segment
     581        2548 :         for (i = 0; i < c->nchannels; i++) {
     582        2120 :             if (b->nscalablelsbs[i]) {
     583        1712 :                 get_array(&s->gb,
     584         856 :                           b->lsb_sample_buffer[i] + seg * s->nsegsamples,
     585             :                           s->nsegsamples, b->nscalablelsbs[i]);
     586             :             }
     587             :         }
     588             :     }
     589             : 
     590             :     // Skip to the end of band data
     591        7792 :     if (ff_dca_seek_bits(&s->gb, band_data_end)) {
     592           0 :         av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL band data\n");
     593           0 :         return AVERROR_INVALIDDATA;
     594             :     }
     595             : 
     596        7792 :     return 0;
     597             : }
     598             : 
     599          60 : static av_cold void chs_clear_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band, int seg)
     600             : {
     601          60 :     DCAXllBand *b = &c->bands[band];
     602             :     int i, offset, nsamples;
     603             : 
     604          60 :     if (seg < 0) {
     605          60 :         offset = 0;
     606          60 :         nsamples = s->nframesamples;
     607             :     } else {
     608           0 :         offset = seg * s->nsegsamples;
     609           0 :         nsamples = s->nsegsamples;
     610             :     }
     611             : 
     612         282 :     for (i = 0; i < c->nchannels; i++) {
     613         222 :         memset(b->msb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
     614         222 :         if (b->lsb_section_size)
     615           0 :             memset(b->lsb_sample_buffer[i] + offset, 0, nsamples * sizeof(int32_t));
     616             :     }
     617             : 
     618          60 :     if (seg <= 0 && band)
     619          10 :         memset(c->deci_history, 0, sizeof(c->deci_history));
     620             : 
     621          60 :     if (seg < 0) {
     622          60 :         memset(b->nscalablelsbs, 0, sizeof(b->nscalablelsbs));
     623          60 :         memset(b->bit_width_adjust, 0, sizeof(b->bit_width_adjust));
     624             :     }
     625          60 : }
     626             : 
     627        2638 : static void chs_filter_band_data(DCAXllDecoder *s, DCAXllChSet *c, int band)
     628             : {
     629        2638 :     DCAXllBand *b = &c->bands[band];
     630        2638 :     int nsamples = s->nframesamples;
     631             :     int i, j, k;
     632             : 
     633             :     // Inverse adaptive or fixed prediction
     634       13319 :     for (i = 0; i < c->nchannels; i++) {
     635       10681 :         int32_t *buf = b->msb_sample_buffer[i];
     636       10681 :         int order = b->adapt_pred_order[i];
     637       10681 :         if (order > 0) {
     638             :             int coeff[DCA_XLL_ADAPT_PRED_ORDER_MAX];
     639             :             // Conversion from reflection coefficients to direct form coefficients
     640       50598 :             for (j = 0; j < order; j++) {
     641       41701 :                 int rc = b->adapt_refl_coeff[i][j];
     642      108626 :                 for (k = 0; k < (j + 1) / 2; k++) {
     643       66925 :                     int tmp1 = coeff[    k    ];
     644       66925 :                     int tmp2 = coeff[j - k - 1];
     645       66925 :                     coeff[    k    ] = tmp1 + mul16(rc, tmp2);
     646       66925 :                     coeff[j - k - 1] = tmp2 + mul16(rc, tmp1);
     647             :                 }
     648       41701 :                 coeff[j] = rc;
     649             :             }
     650             :             // Inverse adaptive prediction
     651     4671452 :             for (j = 0; j < nsamples - order; j++) {
     652     4662555 :                 int64_t err = 0;
     653    26912560 :                 for (k = 0; k < order; k++)
     654    22250005 :                     err += (int64_t)buf[j + k] * coeff[order - k - 1];
     655     4662555 :                 buf[j + k] -= (SUINT)clip23(norm16(err));
     656             :             }
     657             :         } else {
     658             :             // Inverse fixed coefficient prediction
     659        2339 :             for (j = 0; j < b->fixed_pred_order[i]; j++)
     660      301056 :                 for (k = 1; k < nsamples; k++)
     661      300501 :                     buf[k] += (unsigned)buf[k - 1];
     662             :         }
     663             :     }
     664             : 
     665             :     // Inverse pairwise channel decorrellation
     666        2638 :     if (b->decor_enabled) {
     667             :         int32_t *tmp[DCA_XLL_CHANNELS_MAX];
     668             : 
     669        7968 :         for (i = 0; i < c->nchannels / 2; i++) {
     670        5337 :             int coeff = b->decor_coeff[i];
     671        5337 :             if (coeff) {
     672        3598 :                 s->dcadsp->decor(b->msb_sample_buffer[i * 2 + 1],
     673        1799 :                                  b->msb_sample_buffer[i * 2    ],
     674             :                                  coeff, nsamples);
     675             :             }
     676             :         }
     677             : 
     678             :         // Reorder channel pointers to the original order
     679       13305 :         for (i = 0; i < c->nchannels; i++)
     680       10674 :             tmp[i] = b->msb_sample_buffer[i];
     681             : 
     682       13305 :         for (i = 0; i < c->nchannels; i++)
     683       10674 :             b->msb_sample_buffer[b->orig_order[i]] = tmp[i];
     684             :     }
     685             : 
     686             :     // Map output channel pointers for frequency band 0
     687        2638 :     if (c->nfreqbands == 1)
     688       12759 :         for (i = 0; i < c->nchannels; i++)
     689       10233 :             s->output_samples[c->ch_remap[i]] = b->msb_sample_buffer[i];
     690        2638 : }
     691             : 
     692        8476 : static int chs_get_lsb_width(DCAXllDecoder *s, DCAXllChSet *c, int band, int ch)
     693             : {
     694        8476 :     int adj = c->bands[band].bit_width_adjust[ch];
     695        8476 :     int shift = c->bands[band].nscalablelsbs[ch];
     696             : 
     697        8476 :     if (s->fixed_lsb_width)
     698           0 :         shift = s->fixed_lsb_width;
     699        8476 :     else if (shift && adj)
     700           0 :         shift += adj - 1;
     701             :     else
     702        8476 :         shift += adj;
     703             : 
     704        8476 :     return shift;
     705             : }
     706             : 
     707         192 : static void chs_assemble_msbs_lsbs(DCAXllDecoder *s, DCAXllChSet *c, int band)
     708             : {
     709         192 :     DCAXllBand *b = &c->bands[band];
     710         192 :     int n, ch, nsamples = s->nframesamples;
     711             : 
     712         987 :     for (ch = 0; ch < c->nchannels; ch++) {
     713         795 :         int shift = chs_get_lsb_width(s, c, band, ch);
     714         795 :         if (shift) {
     715         112 :             int32_t *msb = b->msb_sample_buffer[ch];
     716         112 :             if (b->nscalablelsbs[ch]) {
     717         112 :                 int32_t *lsb = b->lsb_sample_buffer[ch];
     718         112 :                 int adj = b->bit_width_adjust[ch];
     719       69744 :                 for (n = 0; n < nsamples; n++)
     720       69632 :                     msb[n] = msb[n] * (SUINT)(1 << shift) + (lsb[n] << adj);
     721             :             } else {
     722           0 :                 for (n = 0; n < nsamples; n++)
     723           0 :                     msb[n] = msb[n] * (SUINT)(1 << shift);
     724             :             }
     725             :         }
     726             :     }
     727         192 : }
     728             : 
     729          56 : static int chs_assemble_freq_bands(DCAXllDecoder *s, DCAXllChSet *c)
     730             : {
     731          56 :     int ch, nsamples = s->nframesamples;
     732             :     int32_t *ptr;
     733             : 
     734             :     av_assert1(c->nfreqbands > 1);
     735             : 
     736             :     // Reallocate frequency band assembly buffer
     737         112 :     av_fast_malloc(&c->sample_buffer[2], &c->sample_size[2],
     738          56 :                    2 * nsamples * c->nchannels * sizeof(int32_t));
     739          56 :     if (!c->sample_buffer[2])
     740           0 :         return AVERROR(ENOMEM);
     741             : 
     742             :     // Assemble frequency bands 0 and 1
     743          56 :     ptr = c->sample_buffer[2];
     744         280 :     for (ch = 0; ch < c->nchannels; ch++) {
     745         224 :         int32_t *band0 = c->bands[0].msb_sample_buffer[ch];
     746         224 :         int32_t *band1 = c->bands[1].msb_sample_buffer[ch];
     747             : 
     748             :         // Copy decimator history
     749         224 :         memcpy(band0 - DCA_XLL_DECI_HISTORY_MAX,
     750         224 :                c->deci_history[ch], sizeof(c->deci_history[0]));
     751             : 
     752             :         // Filter
     753         224 :         s->dcadsp->assemble_freq_bands(ptr, band0, band1,
     754             :                                        ff_dca_xll_band_coeff,
     755             :                                        nsamples);
     756             : 
     757             :         // Remap output channel pointer to assembly buffer
     758         224 :         s->output_samples[c->ch_remap[ch]] = ptr;
     759         224 :         ptr += nsamples * 2;
     760             :     }
     761             : 
     762          56 :     return 0;
     763             : }
     764             : 
     765        1353 : static int parse_common_header(DCAXllDecoder *s)
     766             : {
     767             :     int stream_ver, header_size, frame_size_nbits, nframesegs_log2;
     768             : 
     769             :     // XLL extension sync word
     770        1353 :     if (get_bits_long(&s->gb, 32) != DCA_SYNCWORD_XLL) {
     771           0 :         av_log(s->avctx, AV_LOG_VERBOSE, "Invalid XLL sync word\n");
     772           0 :         return AVERROR(EAGAIN);
     773             :     }
     774             : 
     775             :     // Version number
     776        1353 :     stream_ver = get_bits(&s->gb, 4) + 1;
     777        1353 :     if (stream_ver > 1) {
     778           0 :         avpriv_request_sample(s->avctx, "XLL stream version %d", stream_ver);
     779           0 :         return AVERROR_PATCHWELCOME;
     780             :     }
     781             : 
     782             :     // Lossless frame header length
     783        1353 :     header_size = get_bits(&s->gb, 8) + 1;
     784             : 
     785             :     // Check CRC
     786        1353 :     if (ff_dca_check_crc(s->avctx, &s->gb, 32, header_size * 8)) {
     787           0 :         av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL common header checksum\n");
     788           0 :         return AVERROR_INVALIDDATA;
     789             :     }
     790             : 
     791             :     // Number of bits used to read frame size
     792        1353 :     frame_size_nbits = get_bits(&s->gb, 5) + 1;
     793             : 
     794             :     // Number of bytes in a lossless frame
     795        1353 :     s->frame_size = get_bits_long(&s->gb, frame_size_nbits);
     796        1353 :     if (s->frame_size < 0 || s->frame_size >= DCA_XLL_PBR_BUFFER_MAX) {
     797           0 :         av_log(s->avctx, AV_LOG_ERROR, "Invalid XLL frame size (%d bytes)\n", s->frame_size);
     798           0 :         return AVERROR_INVALIDDATA;
     799             :     }
     800        1353 :     s->frame_size++;
     801             : 
     802             :     // Number of channels sets per frame
     803        1353 :     s->nchsets = get_bits(&s->gb, 4) + 1;
     804        1353 :     if (s->nchsets > DCA_XLL_CHSETS_MAX) {
     805           0 :         avpriv_request_sample(s->avctx, "%d XLL channel sets", s->nchsets);
     806           0 :         return AVERROR_PATCHWELCOME;
     807             :     }
     808             : 
     809             :     // Number of segments per frame
     810        1353 :     nframesegs_log2 = get_bits(&s->gb, 4);
     811        1353 :     s->nframesegs = 1 << nframesegs_log2;
     812        1353 :     if (s->nframesegs > 1024) {
     813           0 :         av_log(s->avctx, AV_LOG_ERROR, "Too many segments per XLL frame\n");
     814           0 :         return AVERROR_INVALIDDATA;
     815             :     }
     816             : 
     817             :     // Samples in segment per one frequency band for the first channel set
     818             :     // Maximum value is 256 for sampling frequencies <= 48 kHz
     819             :     // Maximum value is 512 for sampling frequencies > 48 kHz
     820        1353 :     s->nsegsamples_log2 = get_bits(&s->gb, 4);
     821        1353 :     if (!s->nsegsamples_log2) {
     822           0 :         av_log(s->avctx, AV_LOG_ERROR, "Too few samples per XLL segment\n");
     823           0 :         return AVERROR_INVALIDDATA;
     824             :     }
     825        1353 :     s->nsegsamples = 1 << s->nsegsamples_log2;
     826        1353 :     if (s->nsegsamples > 512) {
     827           0 :         av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL segment\n");
     828           0 :         return AVERROR_INVALIDDATA;
     829             :     }
     830             : 
     831             :     // Samples in frame per one frequency band for the first channel set
     832        1353 :     s->nframesamples_log2 = s->nsegsamples_log2 + nframesegs_log2;
     833        1353 :     s->nframesamples = 1 << s->nframesamples_log2;
     834        1353 :     if (s->nframesamples > 65536) {
     835           0 :         av_log(s->avctx, AV_LOG_ERROR, "Too many samples per XLL frame\n");
     836           0 :         return AVERROR_INVALIDDATA;
     837             :     }
     838             : 
     839             :     // Number of bits used to read segment size
     840        1353 :     s->seg_size_nbits = get_bits(&s->gb, 5) + 1;
     841             : 
     842             :     // Presence of CRC16 within each frequency band
     843             :     // 0 - No CRC16 within band
     844             :     // 1 - CRC16 placed at the end of MSB0
     845             :     // 2 - CRC16 placed at the end of MSB0 and LSB0
     846             :     // 3 - CRC16 placed at the end of MSB0 and LSB0 and other frequency bands
     847        1353 :     s->band_crc_present = get_bits(&s->gb, 2);
     848             : 
     849             :     // MSB/LSB split flag
     850        1353 :     s->scalable_lsbs = get_bits1(&s->gb);
     851             : 
     852             :     // Channel position mask
     853        1353 :     s->ch_mask_nbits = get_bits(&s->gb, 5) + 1;
     854             : 
     855             :     // Fixed LSB width
     856        1353 :     if (s->scalable_lsbs)
     857         126 :         s->fixed_lsb_width = get_bits(&s->gb, 4);
     858             :     else
     859        1227 :         s->fixed_lsb_width = 0;
     860             : 
     861             :     // Reserved
     862             :     // Byte align
     863             :     // Header CRC16 protection
     864        1353 :     if (ff_dca_seek_bits(&s->gb, header_size * 8)) {
     865           0 :         av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL common header\n");
     866           0 :         return AVERROR_INVALIDDATA;
     867             :     }
     868             : 
     869        1353 :     return 0;
     870             : }
     871             : 
     872        3871 : static int is_hier_dmix_chset(DCAXllChSet *c)
     873             : {
     874        3871 :     return !c->primary_chset && c->dmix_embedded && c->hier_chset;
     875             : }
     876             : 
     877        1454 : static DCAXllChSet *find_next_hier_dmix_chset(DCAXllDecoder *s, DCAXllChSet *c)
     878             : {
     879        1454 :     if (c->hier_chset)
     880        4121 :         while (++c < &s->chset[s->nchsets])
     881        1278 :             if (is_hier_dmix_chset(c))
     882          65 :                 return c;
     883             : 
     884        1389 :     return NULL;
     885             : }
     886             : 
     887          21 : static void prescale_down_mix(DCAXllChSet *c, DCAXllChSet *o)
     888             : {
     889          21 :     int i, j, *coeff_ptr = c->dmix_coeff;
     890             : 
     891          63 :     for (i = 0; i < c->hier_ofs; i++) {
     892          42 :         int scale = o->dmix_scale[i];
     893          42 :         int scale_inv = o->dmix_scale_inv[i];
     894          42 :         c->dmix_scale[i] = mul15(c->dmix_scale[i], scale);
     895          42 :         c->dmix_scale_inv[i] = mul16(c->dmix_scale_inv[i], scale_inv);
     896         210 :         for (j = 0; j < c->nchannels; j++) {
     897         168 :             int coeff = mul16(*coeff_ptr, scale_inv);
     898         168 :             *coeff_ptr++ = mul15(coeff, o->dmix_scale[c->hier_ofs + j]);
     899             :         }
     900             :     }
     901          21 : }
     902             : 
     903        1353 : static int parse_sub_headers(DCAXllDecoder *s, DCAExssAsset *asset)
     904             : {
     905        1353 :     DCAContext *dca = s->avctx->priv_data;
     906             :     DCAXllChSet *c;
     907             :     int i, ret;
     908             : 
     909             :     // Parse channel set headers
     910        1353 :     s->nfreqbands = 0;
     911        1353 :     s->nchannels = 0;
     912        1353 :     s->nreschsets = 0;
     913        4005 :     for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
     914        2652 :         c->hier_ofs = s->nchannels;
     915        2652 :         if ((ret = chs_parse_header(s, c, asset)) < 0)
     916           0 :             return ret;
     917        2652 :         if (c->nfreqbands > s->nfreqbands)
     918        1353 :             s->nfreqbands = c->nfreqbands;
     919        2652 :         if (c->hier_chset)
     920        2652 :             s->nchannels += c->nchannels;
     921        2652 :         if (c->residual_encode != (1 << c->nchannels) - 1)
     922        1341 :             s->nreschsets++;
     923             :     }
     924             : 
     925             :     // Pre-scale downmixing coefficients for all non-primary channel sets
     926        2652 :     for (i = s->nchsets - 1, c = &s->chset[i]; i > 0; i--, c--) {
     927        1299 :         if (is_hier_dmix_chset(c)) {
     928         126 :             DCAXllChSet *o = find_next_hier_dmix_chset(s, c);
     929         126 :             if (o)
     930          21 :                 prescale_down_mix(c, o);
     931             :         }
     932             :     }
     933             : 
     934             :     // Determine number of active channel sets to decode
     935        1353 :     switch (dca->request_channel_layout) {
     936          60 :     case DCA_SPEAKER_LAYOUT_STEREO:
     937          60 :         s->nactivechsets = 1;
     938          60 :         break;
     939          60 :     case DCA_SPEAKER_LAYOUT_5POINT0:
     940             :     case DCA_SPEAKER_LAYOUT_5POINT1:
     941          60 :         s->nactivechsets = (s->chset[0].nchannels < 5 && s->nchsets > 1) ? 2 : 1;
     942          60 :         break;
     943        1233 :     default:
     944        1233 :         s->nactivechsets = s->nchsets;
     945        1233 :         break;
     946             :     }
     947             : 
     948        1353 :     return 0;
     949             : }
     950             : 
     951        1353 : static int parse_navi_table(DCAXllDecoder *s)
     952             : {
     953             :     int chs, seg, band, navi_nb, navi_pos, *navi_ptr;
     954             :     DCAXllChSet *c;
     955             : 
     956             :     // Determine size of NAVI table
     957        1353 :     navi_nb = s->nfreqbands * s->nframesegs * s->nchsets;
     958        1353 :     if (navi_nb > 1024) {
     959           0 :         av_log(s->avctx, AV_LOG_ERROR, "Too many NAVI entries (%d)\n", navi_nb);
     960           0 :         return AVERROR_INVALIDDATA;
     961             :     }
     962             : 
     963             :     // Reallocate NAVI table
     964        1353 :     av_fast_malloc(&s->navi, &s->navi_size, navi_nb * sizeof(*s->navi));
     965        1353 :     if (!s->navi)
     966           0 :         return AVERROR(ENOMEM);
     967             : 
     968             :     // Parse NAVI
     969        1353 :     navi_pos = get_bits_count(&s->gb);
     970        1353 :     navi_ptr = s->navi;
     971        2748 :     for (band = 0; band < s->nfreqbands; band++) {
     972        5719 :         for (seg = 0; seg < s->nframesegs; seg++) {
     973       12534 :             for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
     974        8210 :                 int size = 0;
     975        8210 :                 if (c->nfreqbands > band) {
     976        8210 :                     size = get_bits_long(&s->gb, s->seg_size_nbits);
     977        8210 :                     if (size < 0 || size >= s->frame_size) {
     978           0 :                         av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI segment size (%d bytes)\n", size);
     979           0 :                         return AVERROR_INVALIDDATA;
     980             :                     }
     981        8210 :                     size++;
     982             :                 }
     983        8210 :                 *navi_ptr++ = size;
     984             :             }
     985             :         }
     986             :     }
     987             : 
     988             :     // Byte align
     989             :     // CRC16
     990        1353 :     skip_bits(&s->gb, -get_bits_count(&s->gb) & 7);
     991        1353 :     skip_bits(&s->gb, 16);
     992             : 
     993             :     // Check CRC
     994        1353 :     if (ff_dca_check_crc(s->avctx, &s->gb, navi_pos, get_bits_count(&s->gb))) {
     995           0 :         av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI checksum\n");
     996           0 :         return AVERROR_INVALIDDATA;
     997             :     }
     998             : 
     999        1353 :     return 0;
    1000             : }
    1001             : 
    1002        1353 : static int parse_band_data(DCAXllDecoder *s)
    1003             : {
    1004             :     int ret, chs, seg, band, navi_pos, *navi_ptr;
    1005             :     DCAXllChSet *c;
    1006             : 
    1007        3935 :     for (chs = 0, c = s->chset; chs < s->nactivechsets; chs++, c++) {
    1008        2582 :         if ((ret = chs_alloc_msb_band_data(s, c)) < 0)
    1009           0 :             return ret;
    1010        2582 :         if ((ret = chs_alloc_lsb_band_data(s, c)) < 0)
    1011           0 :             return ret;
    1012             :     }
    1013             : 
    1014        1353 :     navi_pos = get_bits_count(&s->gb);
    1015        1353 :     navi_ptr = s->navi;
    1016        2748 :     for (band = 0; band < s->nfreqbands; band++) {
    1017        5719 :         for (seg = 0; seg < s->nframesegs; seg++) {
    1018       12534 :             for (chs = 0, c = s->chset; chs < s->nchsets; chs++, c++) {
    1019        8210 :                 if (c->nfreqbands > band) {
    1020        8210 :                     navi_pos += *navi_ptr * 8;
    1021        8210 :                     if (navi_pos > s->gb.size_in_bits) {
    1022           0 :                         av_log(s->avctx, AV_LOG_ERROR, "Invalid NAVI position\n");
    1023           0 :                         return AVERROR_INVALIDDATA;
    1024             :                     }
    1025        8210 :                     if (chs < s->nactivechsets &&
    1026             :                         (ret = chs_parse_band_data(s, c, band, seg, navi_pos)) < 0) {
    1027           0 :                         if (s->avctx->err_recognition & AV_EF_EXPLODE)
    1028           0 :                             return ret;
    1029           0 :                         chs_clear_band_data(s, c, band, seg);
    1030             :                     }
    1031        8210 :                     skip_bits_long(&s->gb, navi_pos - get_bits_count(&s->gb));
    1032             :                 }
    1033        8210 :                 navi_ptr++;
    1034             :             }
    1035             :         }
    1036             :     }
    1037             : 
    1038        1353 :     return 0;
    1039             : }
    1040             : 
    1041        1353 : static int parse_frame(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
    1042             : {
    1043             :     int ret;
    1044             : 
    1045        1353 :     if ((ret = init_get_bits8(&s->gb, data, size)) < 0)
    1046           0 :         return ret;
    1047        1353 :     if ((ret = parse_common_header(s)) < 0)
    1048           0 :         return ret;
    1049        1353 :     if ((ret = parse_sub_headers(s, asset)) < 0)
    1050           0 :         return ret;
    1051        1353 :     if ((ret = parse_navi_table(s)) < 0)
    1052           0 :         return ret;
    1053        1353 :     if ((ret = parse_band_data(s)) < 0)
    1054           0 :         return ret;
    1055        1353 :     if (ff_dca_seek_bits(&s->gb, s->frame_size * 8)) {
    1056           0 :         av_log(s->avctx, AV_LOG_ERROR, "Read past end of XLL frame\n");
    1057           0 :         return AVERROR_INVALIDDATA;
    1058             :     }
    1059        1353 :     return ret;
    1060             : }
    1061             : 
    1062          91 : static void clear_pbr(DCAXllDecoder *s)
    1063             : {
    1064          91 :     s->pbr_length = 0;
    1065          91 :     s->pbr_delay = 0;
    1066          91 : }
    1067             : 
    1068           0 : static int copy_to_pbr(DCAXllDecoder *s, uint8_t *data, int size, int delay)
    1069             : {
    1070           0 :     if (size > DCA_XLL_PBR_BUFFER_MAX)
    1071           0 :         return AVERROR(ENOSPC);
    1072             : 
    1073           0 :     if (!s->pbr_buffer && !(s->pbr_buffer = av_malloc(DCA_XLL_PBR_BUFFER_MAX + AV_INPUT_BUFFER_PADDING_SIZE)))
    1074           0 :         return AVERROR(ENOMEM);
    1075             : 
    1076           0 :     memcpy(s->pbr_buffer, data, size);
    1077           0 :     s->pbr_length = size;
    1078           0 :     s->pbr_delay = delay;
    1079           0 :     return 0;
    1080             : }
    1081             : 
    1082        1353 : static int parse_frame_no_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
    1083             : {
    1084        1353 :     int ret = parse_frame(s, data, size, asset);
    1085             : 
    1086             :     // If XLL packet data didn't start with a sync word, we must have jumped
    1087             :     // right into the middle of PBR smoothing period
    1088        1353 :     if (ret == AVERROR(EAGAIN) && asset->xll_sync_present && asset->xll_sync_offset < size) {
    1089             :         // Skip to the next sync word in this packet
    1090           0 :         data += asset->xll_sync_offset;
    1091           0 :         size -= asset->xll_sync_offset;
    1092             : 
    1093             :         // If decoding delay is set, put the frame into PBR buffer and return
    1094             :         // failure code. Higher level decoder is expected to switch to lossy
    1095             :         // core decoding or mute its output until decoding delay expires.
    1096           0 :         if (asset->xll_delay_nframes > 0) {
    1097           0 :             if ((ret = copy_to_pbr(s, data, size, asset->xll_delay_nframes)) < 0)
    1098           0 :                 return ret;
    1099           0 :             return AVERROR(EAGAIN);
    1100             :         }
    1101             : 
    1102             :         // No decoding delay, just parse the frame in place
    1103           0 :         ret = parse_frame(s, data, size, asset);
    1104             :     }
    1105             : 
    1106        1353 :     if (ret < 0)
    1107           0 :         return ret;
    1108             : 
    1109        1353 :     if (s->frame_size > size)
    1110           0 :         return AVERROR(EINVAL);
    1111             : 
    1112             :     // If the XLL decoder didn't consume full packet, start PBR smoothing period
    1113        1353 :     if (s->frame_size < size)
    1114           0 :         if ((ret = copy_to_pbr(s, data + s->frame_size, size - s->frame_size, 0)) < 0)
    1115           0 :             return ret;
    1116             : 
    1117        1353 :     return 0;
    1118             : }
    1119             : 
    1120           0 : static int parse_frame_pbr(DCAXllDecoder *s, uint8_t *data, int size, DCAExssAsset *asset)
    1121             : {
    1122             :     int ret;
    1123             : 
    1124           0 :     if (size > DCA_XLL_PBR_BUFFER_MAX - s->pbr_length) {
    1125           0 :         ret = AVERROR(ENOSPC);
    1126           0 :         goto fail;
    1127             :     }
    1128             : 
    1129           0 :     memcpy(s->pbr_buffer + s->pbr_length, data, size);
    1130           0 :     s->pbr_length += size;
    1131             : 
    1132             :     // Respect decoding delay after synchronization error
    1133           0 :     if (s->pbr_delay > 0 && --s->pbr_delay)
    1134           0 :         return AVERROR(EAGAIN);
    1135             : 
    1136           0 :     if ((ret = parse_frame(s, s->pbr_buffer, s->pbr_length, asset)) < 0)
    1137           0 :         goto fail;
    1138             : 
    1139           0 :     if (s->frame_size > s->pbr_length) {
    1140           0 :         ret = AVERROR(EINVAL);
    1141           0 :         goto fail;
    1142             :     }
    1143             : 
    1144           0 :     if (s->frame_size == s->pbr_length) {
    1145             :         // End of PBR smoothing period
    1146           0 :         clear_pbr(s);
    1147             :     } else {
    1148           0 :         s->pbr_length -= s->frame_size;
    1149           0 :         memmove(s->pbr_buffer, s->pbr_buffer + s->frame_size, s->pbr_length);
    1150             :     }
    1151             : 
    1152           0 :     return 0;
    1153             : 
    1154           0 : fail:
    1155             :     // For now, throw out all PBR state on failure.
    1156             :     // Perhaps we can be smarter and try to resync somehow.
    1157           0 :     clear_pbr(s);
    1158           0 :     return ret;
    1159             : }
    1160             : 
    1161        1353 : int ff_dca_xll_parse(DCAXllDecoder *s, uint8_t *data, DCAExssAsset *asset)
    1162             : {
    1163             :     int ret;
    1164             : 
    1165        1353 :     if (s->hd_stream_id != asset->hd_stream_id) {
    1166           0 :         clear_pbr(s);
    1167           0 :         s->hd_stream_id = asset->hd_stream_id;
    1168             :     }
    1169             : 
    1170        1353 :     if (s->pbr_length)
    1171           0 :         ret = parse_frame_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
    1172             :     else
    1173        1353 :         ret = parse_frame_no_pbr(s, data + asset->xll_offset, asset->xll_size, asset);
    1174             : 
    1175        1353 :     return ret;
    1176             : }
    1177             : 
    1178          50 : static void undo_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
    1179             : {
    1180          50 :     int i, j, k, nchannels = 0, *coeff_ptr = o->dmix_coeff;
    1181             :     DCAXllChSet *c;
    1182             : 
    1183          55 :     for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
    1184          55 :         if (!c->hier_chset)
    1185           0 :             continue;
    1186             : 
    1187             :         av_assert1(band < c->nfreqbands);
    1188         235 :         for (j = 0; j < c->nchannels; j++) {
    1189         630 :             for (k = 0; k < o->nchannels; k++) {
    1190         450 :                 int coeff = *coeff_ptr++;
    1191         450 :                 if (coeff) {
    1192         300 :                     s->dcadsp->dmix_sub(c->bands[band].msb_sample_buffer[j],
    1193         100 :                                         o->bands[band].msb_sample_buffer[k],
    1194         100 :                                         coeff, s->nframesamples);
    1195         100 :                     if (band)
    1196          40 :                         s->dcadsp->dmix_sub(c->deci_history[j],
    1197          20 :                                             o->deci_history[k],
    1198             :                                             coeff, DCA_XLL_DECI_HISTORY_MAX);
    1199             :                 }
    1200             :             }
    1201             :         }
    1202             : 
    1203          55 :         nchannels += c->nchannels;
    1204          55 :         if (nchannels >= o->hier_ofs)
    1205          50 :             break;
    1206             :     }
    1207          50 : }
    1208             : 
    1209          50 : static void scale_down_mix(DCAXllDecoder *s, DCAXllChSet *o, int band)
    1210             : {
    1211          50 :     int i, j, nchannels = 0;
    1212             :     DCAXllChSet *c;
    1213             : 
    1214          55 :     for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
    1215          55 :         if (!c->hier_chset)
    1216           0 :             continue;
    1217             : 
    1218             :         av_assert1(band < c->nfreqbands);
    1219         295 :         for (j = 0; j < c->nchannels; j++) {
    1220         240 :             int scale = o->dmix_scale[nchannels++];
    1221         240 :             if (scale != (1 << 15)) {
    1222         360 :                 s->dcadsp->dmix_scale(c->bands[band].msb_sample_buffer[j],
    1223         180 :                                       scale, s->nframesamples);
    1224         180 :                 if (band)
    1225          10 :                     s->dcadsp->dmix_scale(c->deci_history[j],
    1226             :                                           scale, DCA_XLL_DECI_HISTORY_MAX);
    1227             :             }
    1228             :         }
    1229             : 
    1230          55 :         if (nchannels >= o->hier_ofs)
    1231          50 :             break;
    1232             :     }
    1233          50 : }
    1234             : 
    1235             : // Clear all band data and replace non-residual encoded channels with lossy
    1236             : // counterparts
    1237          50 : static av_cold void force_lossy_output(DCAXllDecoder *s, DCAXllChSet *c)
    1238             : {
    1239          50 :     DCAContext *dca = s->avctx->priv_data;
    1240             :     int band, ch;
    1241             : 
    1242         110 :     for (band = 0; band < c->nfreqbands; band++)
    1243          60 :         chs_clear_band_data(s, c, band, -1);
    1244             : 
    1245         244 :     for (ch = 0; ch < c->nchannels; ch++) {
    1246         194 :         if (!(c->residual_encode & (1 << ch)))
    1247         132 :             continue;
    1248          62 :         if (ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]) < 0)
    1249          16 :             continue;
    1250          46 :         c->residual_encode &= ~(1 << ch);
    1251             :     }
    1252          50 : }
    1253             : 
    1254        1328 : static int combine_residual_frame(DCAXllDecoder *s, DCAXllChSet *c)
    1255             : {
    1256        1328 :     DCAContext *dca = s->avctx->priv_data;
    1257        1328 :     int ch, nsamples = s->nframesamples;
    1258             :     DCAXllChSet *o;
    1259             : 
    1260             :     // Verify that core is compatible
    1261        1328 :     if (!(dca->packet & DCA_PACKET_CORE)) {
    1262           0 :         av_log(s->avctx, AV_LOG_ERROR, "Residual encoded channels are present without core\n");
    1263           0 :         return AVERROR(EINVAL);
    1264             :     }
    1265             : 
    1266        1328 :     if (c->freq != dca->core.output_rate) {
    1267           0 :         av_log(s->avctx, AV_LOG_WARNING, "Sample rate mismatch between core (%d Hz) and XLL (%d Hz)\n", dca->core.output_rate, c->freq);
    1268           0 :         return AVERROR_INVALIDDATA;
    1269             :     }
    1270             : 
    1271        1328 :     if (nsamples != dca->core.npcmsamples) {
    1272           0 :         av_log(s->avctx, AV_LOG_WARNING, "Number of samples per frame mismatch between core (%d) and XLL (%d)\n", dca->core.npcmsamples, nsamples);
    1273           0 :         return AVERROR_INVALIDDATA;
    1274             :     }
    1275             : 
    1276             :     // See if this channel set is downmixed and find the next channel set in
    1277             :     // hierarchy. If downmixed, undo core pre-scaling before combining with
    1278             :     // residual (residual is not scaled).
    1279        1328 :     o = find_next_hier_dmix_chset(s, c);
    1280             : 
    1281             :     // Reduce core bit width and combine with residual
    1282        9166 :     for (ch = 0; ch < c->nchannels; ch++) {
    1283             :         int n, spkr, shift, round;
    1284             :         int32_t *src, *dst;
    1285             : 
    1286        7838 :         if (c->residual_encode & (1 << ch))
    1287         157 :             continue;
    1288             : 
    1289             :         // Map this channel to core speaker
    1290        7681 :         spkr = ff_dca_core_map_spkr(&dca->core, c->ch_remap[ch]);
    1291        7681 :         if (spkr < 0) {
    1292           0 :             av_log(s->avctx, AV_LOG_WARNING, "Residual encoded channel (%d) references unavailable core channel\n", c->ch_remap[ch]);
    1293           0 :             return AVERROR_INVALIDDATA;
    1294             :         }
    1295             : 
    1296             :         // Account for LSB width
    1297        7681 :         shift = 24 - c->pcm_bit_res + chs_get_lsb_width(s, c, 0, ch);
    1298        7681 :         if (shift > 24) {
    1299           0 :             av_log(s->avctx, AV_LOG_WARNING, "Invalid core shift (%d bits)\n", shift);
    1300           0 :             return AVERROR_INVALIDDATA;
    1301             :         }
    1302             : 
    1303        7681 :         round = shift > 0 ? 1 << (shift - 1) : 0;
    1304             : 
    1305        7681 :         src = dca->core.output_samples[spkr];
    1306        7681 :         dst = c->bands[0].msb_sample_buffer[ch];
    1307        7681 :         if (o) {
    1308             :             // Undo embedded core downmix pre-scaling
    1309         187 :             int scale_inv = o->dmix_scale_inv[c->hier_ofs + ch];
    1310      122043 :             for (n = 0; n < nsamples; n++)
    1311      121856 :                 dst[n] += (SUINT)clip23((mul16(src[n], scale_inv) + round) >> shift);
    1312             :         } else {
    1313             :             // No downmix scaling
    1314     3983686 :             for (n = 0; n < nsamples; n++)
    1315     3976192 :                 dst[n] += (unsigned)((src[n] + round) >> shift);
    1316             :         }
    1317             :     }
    1318             : 
    1319        1328 :     return 0;
    1320             : }
    1321             : 
    1322        1353 : int ff_dca_xll_filter_frame(DCAXllDecoder *s, AVFrame *frame)
    1323             : {
    1324        1353 :     AVCodecContext *avctx = s->avctx;
    1325        1353 :     DCAContext *dca = avctx->priv_data;
    1326        1353 :     DCAExssAsset *asset = &dca->exss.assets[0];
    1327        1353 :     DCAXllChSet *p = &s->chset[0], *c;
    1328        1353 :     enum AVMatrixEncoding matrix_encoding = AV_MATRIX_ENCODING_NONE;
    1329             :     int i, j, k, ret, shift, nsamples, request_mask;
    1330             :     int ch_remap[DCA_SPEAKER_COUNT];
    1331             : 
    1332             :     // Force lossy downmixed output during recovery
    1333        1353 :     if (dca->packet & DCA_PACKET_RECOVERY) {
    1334         102 :         for (i = 0, c = s->chset; i < s->nchsets; i++, c++) {
    1335          70 :             if (i < s->nactivechsets)
    1336          50 :                 force_lossy_output(s, c);
    1337             : 
    1338          70 :             if (!c->primary_chset)
    1339          38 :                 c->dmix_embedded = 0;
    1340             :         }
    1341             : 
    1342          32 :         s->scalable_lsbs = 0;
    1343          32 :         s->fixed_lsb_width = 0;
    1344             :     }
    1345             : 
    1346             :     // Filter frequency bands for active channel sets
    1347        1353 :     s->output_mask = 0;
    1348        3935 :     for (i = 0, c = s->chset; i < s->nactivechsets; i++, c++) {
    1349        2582 :         chs_filter_band_data(s, c, 0);
    1350             : 
    1351        2582 :         if (c->residual_encode != (1 << c->nchannels) - 1
    1352        1328 :             && (ret = combine_residual_frame(s, c)) < 0)
    1353           0 :             return ret;
    1354             : 
    1355        2582 :         if (s->scalable_lsbs)
    1356         136 :             chs_assemble_msbs_lsbs(s, c, 0);
    1357             : 
    1358        2582 :         if (c->nfreqbands > 1) {
    1359          56 :             chs_filter_band_data(s, c, 1);
    1360          56 :             chs_assemble_msbs_lsbs(s, c, 1);
    1361             :         }
    1362             : 
    1363        2582 :         s->output_mask |= c->ch_mask;
    1364             :     }
    1365             : 
    1366             :     // Undo hierarchial downmix and/or apply scaling
    1367        2602 :     for (i = 1, c = &s->chset[1]; i < s->nchsets; i++, c++) {
    1368        1294 :         if (!is_hier_dmix_chset(c))
    1369        1209 :             continue;
    1370             : 
    1371          85 :         if (i >= s->nactivechsets) {
    1372          95 :             for (j = 0; j < c->nfreqbands; j++)
    1373          50 :                 if (c->bands[j].dmix_embedded)
    1374          50 :                     scale_down_mix(s, c, j);
    1375          45 :             break;
    1376             :         }
    1377             : 
    1378          90 :         for (j = 0; j < c->nfreqbands; j++)
    1379          50 :             if (c->bands[j].dmix_embedded)
    1380          50 :                 undo_down_mix(s, c, j);
    1381             :     }
    1382             : 
    1383             :     // Assemble frequency bands for active channel sets
    1384        1353 :     if (s->nfreqbands > 1) {
    1385          98 :         for (i = 0; i < s->nactivechsets; i++)
    1386          56 :             if ((ret = chs_assemble_freq_bands(s, &s->chset[i])) < 0)
    1387           0 :                 return ret;
    1388             :     }
    1389             : 
    1390             :     // Normalize to regular 5.1 layout if downmixing
    1391        1353 :     if (dca->request_channel_layout) {
    1392         120 :         if (s->output_mask & DCA_SPEAKER_MASK_Lss) {
    1393          35 :             s->output_samples[DCA_SPEAKER_Ls] = s->output_samples[DCA_SPEAKER_Lss];
    1394          35 :             s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Lss) | DCA_SPEAKER_MASK_Ls;
    1395             :         }
    1396         120 :         if (s->output_mask & DCA_SPEAKER_MASK_Rss) {
    1397          35 :             s->output_samples[DCA_SPEAKER_Rs] = s->output_samples[DCA_SPEAKER_Rss];
    1398          35 :             s->output_mask = (s->output_mask & ~DCA_SPEAKER_MASK_Rss) | DCA_SPEAKER_MASK_Rs;
    1399             :         }
    1400             :     }
    1401             : 
    1402             :     // Handle downmixing to stereo request
    1403        1353 :     if (dca->request_channel_layout == DCA_SPEAKER_LAYOUT_STEREO
    1404          60 :         && DCA_HAS_STEREO(s->output_mask) && p->dmix_embedded
    1405           7 :         && (p->dmix_type == DCA_DMIX_TYPE_LoRo ||
    1406           0 :             p->dmix_type == DCA_DMIX_TYPE_LtRt))
    1407           7 :         request_mask = DCA_SPEAKER_LAYOUT_STEREO;
    1408             :     else
    1409        1346 :         request_mask = s->output_mask;
    1410        1353 :     if (!ff_dca_set_channel_layout(avctx, ch_remap, request_mask))
    1411           0 :         return AVERROR(EINVAL);
    1412             : 
    1413        1353 :     avctx->sample_rate = p->freq << (s->nfreqbands - 1);
    1414             : 
    1415        1353 :     switch (p->storage_bit_res) {
    1416          42 :     case 16:
    1417          42 :         avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
    1418          42 :         shift = 16 - p->pcm_bit_res;
    1419          42 :         break;
    1420        1311 :     case 20:
    1421             :     case 24:
    1422        1311 :         avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
    1423        1311 :         shift = 24 - p->pcm_bit_res;
    1424        1311 :         break;
    1425           0 :     default:
    1426           0 :         return AVERROR(EINVAL);
    1427             :     }
    1428             : 
    1429        1353 :     avctx->bits_per_raw_sample = p->storage_bit_res;
    1430        1353 :     avctx->profile = FF_PROFILE_DTS_HD_MA;
    1431        1353 :     avctx->bit_rate = 0;
    1432             : 
    1433        1353 :     frame->nb_samples = nsamples = s->nframesamples << (s->nfreqbands - 1);
    1434        1353 :     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
    1435           0 :         return ret;
    1436             : 
    1437             :     // Downmix primary channel set to stereo
    1438        1353 :     if (request_mask != s->output_mask) {
    1439          14 :         ff_dca_downmix_to_stereo_fixed(s->dcadsp, s->output_samples,
    1440           7 :                                        p->dmix_coeff, nsamples,
    1441             :                                        s->output_mask);
    1442             :     }
    1443             : 
    1444       11782 :     for (i = 0; i < avctx->channels; i++) {
    1445       10429 :         int32_t *samples = s->output_samples[ch_remap[i]];
    1446       10429 :         if (frame->format == AV_SAMPLE_FMT_S16P) {
    1447         224 :             int16_t *plane = (int16_t *)frame->extended_data[i];
    1448      458976 :             for (k = 0; k < nsamples; k++)
    1449      458752 :                 plane[k] = av_clip_int16(samples[k] * (SUINT)(1 << shift));
    1450             :         } else {
    1451       10205 :             int32_t *plane = (int32_t *)frame->extended_data[i];
    1452     5408221 :             for (k = 0; k < nsamples; k++)
    1453     5398016 :                 plane[k] = clip23(samples[k] * (SUINT)(1 << shift)) * (1 << 8);
    1454             :         }
    1455             :     }
    1456             : 
    1457        1353 :     if (!asset->one_to_one_map_ch_to_spkr) {
    1458           0 :         if (asset->representation_type == DCA_REPR_TYPE_LtRt)
    1459           0 :             matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
    1460           0 :         else if (asset->representation_type == DCA_REPR_TYPE_LhRh)
    1461           0 :             matrix_encoding = AV_MATRIX_ENCODING_DOLBYHEADPHONE;
    1462        1353 :     } else if (request_mask != s->output_mask && p->dmix_type == DCA_DMIX_TYPE_LtRt) {
    1463           0 :         matrix_encoding = AV_MATRIX_ENCODING_DOLBY;
    1464             :     }
    1465        1353 :     if ((ret = ff_side_data_update_matrix_encoding(frame, matrix_encoding)) < 0)
    1466           0 :         return ret;
    1467             : 
    1468        1353 :     return 0;
    1469             : }
    1470             : 
    1471           0 : av_cold void ff_dca_xll_flush(DCAXllDecoder *s)
    1472             : {
    1473           0 :     clear_pbr(s);
    1474           0 : }
    1475             : 
    1476          91 : av_cold void ff_dca_xll_close(DCAXllDecoder *s)
    1477             : {
    1478             :     DCAXllChSet *c;
    1479             :     int i, j;
    1480             : 
    1481         364 :     for (i = 0, c = s->chset; i < DCA_XLL_CHSETS_MAX; i++, c++) {
    1482        1092 :         for (j = 0; j < DCA_XLL_SAMPLE_BUFFERS_MAX; j++) {
    1483         819 :             av_freep(&c->sample_buffer[j]);
    1484         819 :             c->sample_size[j] = 0;
    1485             :         }
    1486             :     }
    1487             : 
    1488          91 :     av_freep(&s->navi);
    1489          91 :     s->navi_size = 0;
    1490             : 
    1491          91 :     av_freep(&s->pbr_buffer);
    1492          91 :     clear_pbr(s);
    1493          91 : }

Generated by: LCOV version 1.13