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