| Line | Branch | Exec | Source |
|---|---|---|---|
| 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/mem.h" | ||
| 22 | #include "libavutil/opt.h" | ||
| 23 | #include "libavutil/channel_layout.h" | ||
| 24 | #include "libavutil/downmix_info.h" | ||
| 25 | #include "libavutil/thread.h" | ||
| 26 | |||
| 27 | #include "codec_internal.h" | ||
| 28 | #include "dcadec.h" | ||
| 29 | #include "dcadata.h" | ||
| 30 | #include "dcahuff.h" | ||
| 31 | #include "dca_syncwords.h" | ||
| 32 | #include "profiles.h" | ||
| 33 | |||
| 34 | #define MIN_PACKET_SIZE 16 | ||
| 35 | #define MAX_PACKET_SIZE 0x104000 | ||
| 36 | |||
| 37 | |||
| 38 | static const uint8_t dca2wav_norm[28] = { | ||
| 39 | 2, 0, 1, 9, 10, 3, 8, 4, 5, 9, 10, 6, 7, 12, | ||
| 40 | 13, 14, 3, 6, 7, 11, 12, 14, 16, 15, 17, 8, 4, 5, | ||
| 41 | }; | ||
| 42 | |||
| 43 | static const uint8_t dca2wav_wide[28] = { | ||
| 44 | 2, 0, 1, 4, 5, 3, 8, 4, 5, 9, 10, 6, 7, 12, | ||
| 45 | 13, 14, 3, 6, 7, 11, 12, 14, 16, 15, 17, 8, 4, 5, | ||
| 46 | }; | ||
| 47 | |||
| 48 | 3753 | int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask) | |
| 49 | { | ||
| 50 | 3753 | DCAContext *s = avctx->priv_data; | |
| 51 | |||
| 52 | 3753 | int dca_ch, wav_ch, nchannels = 0; | |
| 53 | const uint8_t *dca2wav; | ||
| 54 | |||
| 55 |
3/4✓ Branch 0 taken 3753 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2350 times.
✓ Branch 3 taken 1403 times.
|
3753 | if (dca_mask == DCA_SPEAKER_LAYOUT_7POINT0_WIDE || |
| 56 | dca_mask == DCA_SPEAKER_LAYOUT_7POINT1_WIDE) | ||
| 57 | 2350 | dca2wav = dca2wav_wide; | |
| 58 | else | ||
| 59 | 1403 | dca2wav = dca2wav_norm; | |
| 60 | |||
| 61 | 3753 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 62 |
2/2✓ Branch 0 taken 1173 times.
✓ Branch 1 taken 2580 times.
|
3753 | if (s->output_channel_order == CHANNEL_ORDER_CODED) { |
| 63 | int ret; | ||
| 64 |
2/2✓ Branch 0 taken 37536 times.
✓ Branch 1 taken 1173 times.
|
38709 | for (dca_ch = 0; dca_ch < DCA_SPEAKER_COUNT; dca_ch++) |
| 65 |
2/2✓ Branch 0 taken 9384 times.
✓ Branch 1 taken 28152 times.
|
37536 | if (dca_mask & (1U << dca_ch)) |
| 66 | 9384 | ch_remap[nchannels++] = dca_ch; | |
| 67 | 1173 | ret = av_channel_layout_custom_init(&avctx->ch_layout, nchannels); | |
| 68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1173 times.
|
1173 | if (ret < 0) |
| 69 | ✗ | return ret; | |
| 70 | |||
| 71 | 1173 | nchannels = 0; | |
| 72 |
2/2✓ Branch 0 taken 37536 times.
✓ Branch 1 taken 1173 times.
|
38709 | for (dca_ch = 0; dca_ch < DCA_SPEAKER_COUNT; dca_ch++) |
| 73 |
2/2✓ Branch 0 taken 9384 times.
✓ Branch 1 taken 28152 times.
|
37536 | if (dca_mask & (1U << dca_ch)) |
| 74 | 9384 | avctx->ch_layout.u.map[nchannels++].id = dca2wav[dca_ch]; | |
| 75 | } else { | ||
| 76 | 2580 | int wav_mask = 0; | |
| 77 | int wav_map[18]; | ||
| 78 |
2/2✓ Branch 0 taken 72240 times.
✓ Branch 1 taken 2580 times.
|
74820 | for (dca_ch = 0; dca_ch < 28; dca_ch++) { |
| 79 |
2/2✓ Branch 0 taken 16076 times.
✓ Branch 1 taken 56164 times.
|
72240 | if (dca_mask & (1 << dca_ch)) { |
| 80 | 16076 | wav_ch = dca2wav[dca_ch]; | |
| 81 |
1/2✓ Branch 0 taken 16076 times.
✗ Branch 1 not taken.
|
16076 | if (!(wav_mask & (1 << wav_ch))) { |
| 82 | 16076 | wav_map[wav_ch] = dca_ch; | |
| 83 | 16076 | wav_mask |= 1 << wav_ch; | |
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 |
2/2✓ Branch 0 taken 46440 times.
✓ Branch 1 taken 2580 times.
|
49020 | for (wav_ch = 0; wav_ch < 18; wav_ch++) |
| 88 |
2/2✓ Branch 0 taken 16076 times.
✓ Branch 1 taken 30364 times.
|
46440 | if (wav_mask & (1 << wav_ch)) |
| 89 | 16076 | ch_remap[nchannels++] = wav_map[wav_ch]; | |
| 90 | |||
| 91 | 2580 | av_channel_layout_from_mask(&avctx->ch_layout, wav_mask); | |
| 92 | } | ||
| 93 | |||
| 94 | 3753 | return nchannels; | |
| 95 | } | ||
| 96 | |||
| 97 | 7 | void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples, | |
| 98 | int *coeff_l, int nsamples, int ch_mask) | ||
| 99 | { | ||
| 100 | 7 | int pos, spkr, max_spkr = av_log2(ch_mask); | |
| 101 | 7 | int *coeff_r = coeff_l + av_popcount(ch_mask); | |
| 102 | |||
| 103 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | av_assert0(DCA_HAS_STEREO(ch_mask)); |
| 104 | |||
| 105 | // Scale left and right channels | ||
| 106 | 7 | pos = (ch_mask & DCA_SPEAKER_MASK_C); | |
| 107 | 7 | dcadsp->dmix_scale(samples[DCA_SPEAKER_L], coeff_l[pos ], nsamples); | |
| 108 | 7 | dcadsp->dmix_scale(samples[DCA_SPEAKER_R], coeff_r[pos + 1], nsamples); | |
| 109 | |||
| 110 | // Downmix remaining channels | ||
| 111 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7 times.
|
49 | for (spkr = 0; spkr <= max_spkr; spkr++) { |
| 112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (!(ch_mask & (1U << spkr))) |
| 113 | ✗ | continue; | |
| 114 | |||
| 115 |
4/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
|
42 | if (*coeff_l && spkr != DCA_SPEAKER_L) |
| 116 | 7 | dcadsp->dmix_add(samples[DCA_SPEAKER_L], samples[spkr], | |
| 117 | *coeff_l, nsamples); | ||
| 118 | |||
| 119 |
4/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
|
42 | if (*coeff_r && spkr != DCA_SPEAKER_R) |
| 120 | 7 | dcadsp->dmix_add(samples[DCA_SPEAKER_R], samples[spkr], | |
| 121 | *coeff_r, nsamples); | ||
| 122 | |||
| 123 | 42 | coeff_l++; | |
| 124 | 42 | coeff_r++; | |
| 125 | } | ||
| 126 | 7 | } | |
| 127 | |||
| 128 | 7 | void ff_dca_downmix_to_stereo_float(AVFloatDSPContext *fdsp, float **samples, | |
| 129 | int *coeff_l, int nsamples, int ch_mask) | ||
| 130 | { | ||
| 131 | 7 | int pos, spkr, max_spkr = av_log2(ch_mask); | |
| 132 | 7 | int *coeff_r = coeff_l + av_popcount(ch_mask); | |
| 133 | 7 | const float scale = 1.0f / (1 << 15); | |
| 134 | |||
| 135 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
|
7 | av_assert0(DCA_HAS_STEREO(ch_mask)); |
| 136 | |||
| 137 | // Scale left and right channels | ||
| 138 | 7 | pos = (ch_mask & DCA_SPEAKER_MASK_C); | |
| 139 | 7 | fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_L], samples[DCA_SPEAKER_L], | |
| 140 | 7 | coeff_l[pos ] * scale, nsamples); | |
| 141 | 7 | fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_R], samples[DCA_SPEAKER_R], | |
| 142 | 7 | coeff_r[pos + 1] * scale, nsamples); | |
| 143 | |||
| 144 | // Downmix remaining channels | ||
| 145 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 7 times.
|
49 | for (spkr = 0; spkr <= max_spkr; spkr++) { |
| 146 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
|
42 | if (!(ch_mask & (1U << spkr))) |
| 147 | ✗ | continue; | |
| 148 | |||
| 149 |
4/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
|
42 | if (*coeff_l && spkr != DCA_SPEAKER_L) |
| 150 | 7 | fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_L], samples[spkr], | |
| 151 | 7 | *coeff_l * scale, nsamples); | |
| 152 | |||
| 153 |
4/4✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 7 times.
|
42 | if (*coeff_r && spkr != DCA_SPEAKER_R) |
| 154 | 7 | fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_R], samples[spkr], | |
| 155 | 7 | *coeff_r * scale, nsamples); | |
| 156 | |||
| 157 | 42 | coeff_l++; | |
| 158 | 42 | coeff_r++; | |
| 159 | } | ||
| 160 | 7 | } | |
| 161 | |||
| 162 | 39 | int ff_dca_export_downmix_matrix(AVCodecContext *avctx, AVFrame *frame, | |
| 163 | enum DCADownMixType downmix_type, | ||
| 164 | int output_mask, const int *coeff_l) | ||
| 165 | { | ||
| 166 | 39 | enum AVDownmixType dmix_type = (downmix_type == DCA_DMIX_TYPE_LoRo) ? | |
| 167 |
1/2✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
|
39 | AV_DOWNMIX_TYPE_LORO : AV_DOWNMIX_TYPE_LTRT; |
| 168 | size_t size; | ||
| 169 | 39 | AVDownmixMatrix *dm = av_downmix_matrix_alloc(dmix_type, av_popcount(output_mask), &size); | |
| 170 | 39 | const int *coeff_r = coeff_l + av_popcount(output_mask); | |
| 171 | 39 | const double scale = 1.0 / (1 << 15); | |
| 172 | AVDownmixCoeff *matrix_l, *matrix_r; | ||
| 173 | const uint8_t *dca2wav; | ||
| 174 | |||
| 175 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (!dm) |
| 176 | ✗ | return AVERROR(ENOMEM); | |
| 177 | |||
| 178 |
2/4✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 39 times.
|
39 | if (output_mask == DCA_SPEAKER_LAYOUT_7POINT0_WIDE || |
| 179 | output_mask == DCA_SPEAKER_LAYOUT_7POINT1_WIDE) | ||
| 180 | ✗ | dca2wav = dca2wav_wide; | |
| 181 | else | ||
| 182 | 39 | dca2wav = dca2wav_norm; | |
| 183 | |||
| 184 | 39 | matrix_l = av_downmix_matrix_coeff(dm, 0, 0); | |
| 185 | 39 | matrix_r = av_downmix_matrix_coeff(dm, 1, 0); | |
| 186 | |||
| 187 |
2/2✓ Branch 0 taken 234 times.
✓ Branch 1 taken 39 times.
|
273 | for (int i = 0; i <= av_log2(output_mask); i++) { |
| 188 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 234 times.
|
234 | if (!(output_mask & (1U << i))) |
| 189 | ✗ | continue; | |
| 190 | |||
| 191 | 234 | int idx = av_channel_layout_index_from_channel(&avctx->ch_layout, dca2wav[i]); | |
| 192 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 234 times.
|
234 | av_assert0(idx >= 0); |
| 193 | |||
| 194 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 156 times.
|
234 | if (*coeff_l) |
| 195 | 78 | matrix_l[idx] = *coeff_l * scale; | |
| 196 | |||
| 197 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 156 times.
|
234 | if (*coeff_r) |
| 198 | 78 | matrix_r[idx] = *coeff_r * scale; | |
| 199 | |||
| 200 | 234 | coeff_l++; | |
| 201 | 234 | coeff_r++; | |
| 202 | } | ||
| 203 | |||
| 204 | 39 | AVBufferRef *buf = av_buffer_create((uint8_t *)dm, size, NULL, NULL, 0); | |
| 205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
|
39 | if (!buf) { |
| 206 | ✗ | av_free(dm); | |
| 207 | ✗ | return AVERROR(ENOMEM); | |
| 208 | } | ||
| 209 | |||
| 210 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
|
39 | if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_DOWNMIX_MATRIX, buf)) { |
| 211 | ✗ | av_buffer_unref(&buf); | |
| 212 | ✗ | return AVERROR(ENOMEM); | |
| 213 | } | ||
| 214 | |||
| 215 | 39 | return 0; | |
| 216 | } | ||
| 217 | |||
| 218 | 3753 | static int dcadec_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 219 | int *got_frame_ptr, AVPacket *avpkt) | ||
| 220 | { | ||
| 221 | 3753 | DCAContext *s = avctx->priv_data; | |
| 222 | 3753 | const uint8_t *input = avpkt->data; | |
| 223 | 3753 | int input_size = avpkt->size; | |
| 224 | 3753 | int i, ret, prev_packet = s->packet; | |
| 225 | uint32_t mrk; | ||
| 226 | |||
| 227 |
2/4✓ Branch 0 taken 3753 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3753 times.
|
3753 | if (input_size < MIN_PACKET_SIZE || input_size > MAX_PACKET_SIZE) { |
| 228 | ✗ | av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n"); | |
| 229 | ✗ | return AVERROR_INVALIDDATA; | |
| 230 | } | ||
| 231 | |||
| 232 | // Convert input to BE format | ||
| 233 | 3753 | mrk = AV_RB32(input); | |
| 234 |
4/4✓ Branch 0 taken 19 times.
✓ Branch 1 taken 3734 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 18 times.
|
3753 | if (mrk != DCA_SYNCWORD_CORE_BE && mrk != DCA_SYNCWORD_SUBSTREAM) { |
| 235 | 1 | av_fast_padded_malloc(&s->buffer, &s->buffer_size, input_size); | |
| 236 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!s->buffer) |
| 237 | ✗ | return AVERROR(ENOMEM); | |
| 238 | |||
| 239 |
3/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
|
2 | for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - MIN_PACKET_SIZE + 1 && ret < 0; i++) |
| 240 | 1 | ret = avpriv_dca_convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size); | |
| 241 | |||
| 242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (ret < 0) { |
| 243 | ✗ | av_log(avctx, AV_LOG_ERROR, "Not a valid DCA frame\n"); | |
| 244 | ✗ | return ret; | |
| 245 | } | ||
| 246 | |||
| 247 | 1 | input = s->buffer; | |
| 248 | 1 | input_size = ret; | |
| 249 | } | ||
| 250 | |||
| 251 | 3753 | s->packet = 0; | |
| 252 | |||
| 253 | // Parse backward compatible core sub-stream | ||
| 254 |
2/2✓ Branch 0 taken 3735 times.
✓ Branch 1 taken 18 times.
|
3753 | if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) { |
| 255 | int frame_size; | ||
| 256 | |||
| 257 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3735 times.
|
3735 | if ((ret = ff_dca_core_parse(&s->core, input, input_size)) < 0) |
| 258 | ✗ | return ret; | |
| 259 | |||
| 260 | 3735 | s->packet |= DCA_PACKET_CORE; | |
| 261 | |||
| 262 | // EXXS data must be aligned on 4-byte boundary | ||
| 263 | 3735 | frame_size = FFALIGN(s->core.frame_size, 4); | |
| 264 |
2/2✓ Branch 0 taken 2655 times.
✓ Branch 1 taken 1080 times.
|
3735 | if (input_size - 4 > frame_size) { |
| 265 | 2655 | input += frame_size; | |
| 266 | 2655 | input_size -= frame_size; | |
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 |
1/2✓ Branch 0 taken 3753 times.
✗ Branch 1 not taken.
|
3753 | if (!s->core_only) { |
| 271 | 3753 | DCAExssAsset *asset = NULL; | |
| 272 | |||
| 273 | // Parse extension sub-stream (EXSS) | ||
| 274 |
2/2✓ Branch 0 taken 2673 times.
✓ Branch 1 taken 1080 times.
|
3753 | if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) { |
| 275 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2673 times.
|
2673 | if ((ret = ff_dca_exss_parse(&s->exss, input, input_size)) < 0) { |
| 276 | ✗ | if (avctx->err_recognition & AV_EF_EXPLODE) | |
| 277 | ✗ | return ret; | |
| 278 | } else { | ||
| 279 | 2673 | s->packet |= DCA_PACKET_EXSS; | |
| 280 | 2673 | asset = &s->exss.assets[0]; | |
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | // Parse XLL component in EXSS | ||
| 285 |
4/4✓ Branch 0 taken 2673 times.
✓ Branch 1 taken 1080 times.
✓ Branch 2 taken 2617 times.
✓ Branch 3 taken 56 times.
|
3753 | if (asset && (asset->extension_mask & DCA_EXSS_XLL)) { |
| 286 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2617 times.
|
2617 | if ((ret = ff_dca_xll_parse(&s->xll, input, asset)) < 0) { |
| 287 | // Conceal XLL synchronization error | ||
| 288 | ✗ | if (ret == AVERROR(EAGAIN)) { | |
| 289 | ✗ | if ((prev_packet & DCA_PACKET_XLL) && (s->packet & DCA_PACKET_CORE)) | |
| 290 | ✗ | s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY; | |
| 291 | ✗ | } else if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE)) | |
| 292 | ✗ | return ret; | |
| 293 | } else { | ||
| 294 | 2617 | s->packet |= DCA_PACKET_XLL; | |
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | // Parse LBR component in EXSS | ||
| 299 |
3/4✓ Branch 0 taken 2673 times.
✓ Branch 1 taken 1080 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2673 times.
|
3753 | if (asset && (asset->extension_mask & DCA_EXSS_LBR)) { |
| 300 | ✗ | if ((ret = ff_dca_lbr_parse(&s->lbr, input, asset)) < 0) { | |
| 301 | ✗ | if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE)) | |
| 302 | ✗ | return ret; | |
| 303 | } else { | ||
| 304 | ✗ | s->packet |= DCA_PACKET_LBR; | |
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | // Parse core extensions in EXSS or backward compatible core sub-stream | ||
| 309 |
2/2✓ Branch 0 taken 3735 times.
✓ Branch 1 taken 18 times.
|
3753 | if ((s->packet & DCA_PACKET_CORE) |
| 310 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3735 times.
|
3735 | && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0) |
| 311 | ✗ | return ret; | |
| 312 | } | ||
| 313 | |||
| 314 | // Filter the frame | ||
| 315 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3753 times.
|
3753 | if (s->packet & DCA_PACKET_LBR) { |
| 316 | ✗ | if ((ret = ff_dca_lbr_filter_frame(&s->lbr, frame)) < 0) | |
| 317 | ✗ | return ret; | |
| 318 |
2/2✓ Branch 0 taken 2617 times.
✓ Branch 1 taken 1136 times.
|
3753 | } else if (s->packet & DCA_PACKET_XLL) { |
| 319 |
2/2✓ Branch 0 taken 2599 times.
✓ Branch 1 taken 18 times.
|
2617 | if (s->packet & DCA_PACKET_CORE) { |
| 320 | 2599 | int x96_synth = -1; | |
| 321 | |||
| 322 | // Enable X96 synthesis if needed | ||
| 323 |
3/4✓ Branch 0 taken 120 times.
✓ Branch 1 taken 2479 times.
✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
|
2599 | if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000) |
| 324 | 120 | x96_synth = 1; | |
| 325 | |||
| 326 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2599 times.
|
2599 | if ((ret = ff_dca_core_filter_fixed(&s->core, x96_synth)) < 0) |
| 327 | ✗ | return ret; | |
| 328 | |||
| 329 | // Force lossy downmixed output on the first core frame filtered. | ||
| 330 | // This prevents audible clicks when seeking and is consistent with | ||
| 331 | // what reference decoder does when there are multiple channel sets. | ||
| 332 |
3/4✓ Branch 0 taken 82 times.
✓ Branch 1 taken 2517 times.
✓ Branch 2 taken 82 times.
✗ Branch 3 not taken.
|
2599 | if (!(prev_packet & DCA_PACKET_RESIDUAL) && s->xll.nreschsets > 0 |
| 333 |
2/2✓ Branch 0 taken 53 times.
✓ Branch 1 taken 29 times.
|
82 | && s->xll.nchsets > 1) { |
| 334 | 53 | av_log(avctx, AV_LOG_VERBOSE, "Forcing XLL recovery mode\n"); | |
| 335 | 53 | s->packet |= DCA_PACKET_RECOVERY; | |
| 336 | } | ||
| 337 | |||
| 338 | // Set 'residual ok' flag for the next frame | ||
| 339 | 2599 | s->packet |= DCA_PACKET_RESIDUAL; | |
| 340 | } | ||
| 341 | |||
| 342 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2617 times.
|
2617 | if ((ret = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) { |
| 343 | // Fall back to core unless hard error | ||
| 344 | ✗ | if (!(s->packet & DCA_PACKET_CORE)) | |
| 345 | ✗ | return ret; | |
| 346 | ✗ | if (ret != AVERROR_INVALIDDATA || (avctx->err_recognition & AV_EF_EXPLODE)) | |
| 347 | ✗ | return ret; | |
| 348 | ✗ | if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0) | |
| 349 | ✗ | return ret; | |
| 350 | } | ||
| 351 |
1/2✓ Branch 0 taken 1136 times.
✗ Branch 1 not taken.
|
1136 | } else if (s->packet & DCA_PACKET_CORE) { |
| 352 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1136 times.
|
1136 | if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0) |
| 353 | ✗ | return ret; | |
| 354 |
2/2✓ Branch 0 taken 519 times.
✓ Branch 1 taken 617 times.
|
1136 | if (s->core.filter_mode & DCA_FILTER_MODE_FIXED) |
| 355 | 519 | s->packet |= DCA_PACKET_RESIDUAL; | |
| 356 | } else { | ||
| 357 | ✗ | av_log(avctx, AV_LOG_ERROR, "No valid DCA sub-stream found\n"); | |
| 358 | ✗ | if (s->core_only) | |
| 359 | ✗ | av_log(avctx, AV_LOG_WARNING, "Consider disabling 'core_only' option\n"); | |
| 360 | ✗ | return AVERROR_INVALIDDATA; | |
| 361 | } | ||
| 362 | |||
| 363 | 3753 | *got_frame_ptr = 1; | |
| 364 | |||
| 365 | 3753 | return avpkt->size; | |
| 366 | } | ||
| 367 | |||
| 368 | ✗ | static av_cold void dcadec_flush(AVCodecContext *avctx) | |
| 369 | { | ||
| 370 | ✗ | DCAContext *s = avctx->priv_data; | |
| 371 | |||
| 372 | ✗ | ff_dca_core_flush(&s->core); | |
| 373 | ✗ | ff_dca_xll_flush(&s->xll); | |
| 374 | ✗ | ff_dca_lbr_flush(&s->lbr); | |
| 375 | |||
| 376 | ✗ | s->packet &= DCA_PACKET_MASK; | |
| 377 | ✗ | } | |
| 378 | |||
| 379 | 161 | static av_cold int dcadec_close(AVCodecContext *avctx) | |
| 380 | { | ||
| 381 | 161 | DCAContext *s = avctx->priv_data; | |
| 382 | |||
| 383 | 161 | ff_dca_core_close(&s->core); | |
| 384 | 161 | ff_dca_xll_close(&s->xll); | |
| 385 | 161 | ff_dca_lbr_close(&s->lbr); | |
| 386 | |||
| 387 | 161 | av_freep(&s->buffer); | |
| 388 | 161 | s->buffer_size = 0; | |
| 389 | |||
| 390 | 161 | return 0; | |
| 391 | } | ||
| 392 | |||
| 393 | 85 | static av_cold void dcadec_init_static(void) | |
| 394 | { | ||
| 395 | 85 | ff_dca_lbr_init_tables(); | |
| 396 | 85 | ff_dca_init_vlcs(); | |
| 397 | 85 | } | |
| 398 | |||
| 399 | 161 | static av_cold int dcadec_init(AVCodecContext *avctx) | |
| 400 | { | ||
| 401 | static AVOnce init_static_once = AV_ONCE_INIT; | ||
| 402 | 161 | DCAContext *s = avctx->priv_data; | |
| 403 | |||
| 404 | 161 | s->avctx = avctx; | |
| 405 | 161 | s->core.avctx = avctx; | |
| 406 | 161 | s->exss.avctx = avctx; | |
| 407 | 161 | s->xll.avctx = avctx; | |
| 408 | 161 | s->lbr.avctx = avctx; | |
| 409 | |||
| 410 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 161 times.
|
161 | if (ff_dca_core_init(&s->core) < 0) |
| 411 | ✗ | return AVERROR(ENOMEM); | |
| 412 | |||
| 413 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 161 times.
|
161 | if (ff_dca_lbr_init(&s->lbr) < 0) |
| 414 | ✗ | return AVERROR(ENOMEM); | |
| 415 | |||
| 416 | 161 | ff_dcadsp_init(&s->dcadsp); | |
| 417 | 161 | s->core.dcadsp = &s->dcadsp; | |
| 418 | 161 | s->xll.dcadsp = &s->dcadsp; | |
| 419 | 161 | s->lbr.dcadsp = &s->dcadsp; | |
| 420 | |||
| 421 | 161 | s->crctab = av_crc_get_table(AV_CRC_16_CCITT); | |
| 422 | |||
| 423 |
2/2✓ Branch 0 taken 44 times.
✓ Branch 1 taken 117 times.
|
161 | if (s->downmix_layout.nb_channels) { |
| 424 |
3/4✓ Branch 1 taken 22 times.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
|
66 | if (!av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO) || |
| 425 | 22 | !av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)) { | |
| 426 | 22 | s->request_channel_layout = DCA_SPEAKER_LAYOUT_STEREO; | |
| 427 | 22 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 428 | 22 | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO; | |
| 429 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
|
22 | } else if (!av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0)) { |
| 430 | ✗ | s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT0; | |
| 431 | ✗ | av_channel_layout_uninit(&avctx->ch_layout); | |
| 432 | ✗ | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0; | |
| 433 |
1/2✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
|
22 | } else if (!av_channel_layout_compare(&s->downmix_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1)) { |
| 434 | 22 | s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT1; | |
| 435 | 22 | av_channel_layout_uninit(&avctx->ch_layout); | |
| 436 | 22 | avctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1; | |
| 437 | } | ||
| 438 | else | ||
| 439 | ✗ | av_log(avctx, AV_LOG_WARNING, "Invalid downmix layout\n"); | |
| 440 | } | ||
| 441 | |||
| 442 | 161 | ff_thread_once(&init_static_once, dcadec_init_static); | |
| 443 | |||
| 444 | 161 | return 0; | |
| 445 | } | ||
| 446 | |||
| 447 | #define OFFSET(x) offsetof(DCAContext, x) | ||
| 448 | #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM | ||
| 449 | |||
| 450 | static const AVOption dcadec_options[] = { | ||
| 451 | { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM }, | ||
| 452 | |||
| 453 | { "channel_order", "Order in which the channels are to be exported", | ||
| 454 | OFFSET(output_channel_order), AV_OPT_TYPE_INT, | ||
| 455 | { .i64 = CHANNEL_ORDER_DEFAULT }, 0, 1, PARAM, .unit = "channel_order" }, | ||
| 456 | { "default", "normal libavcodec channel order", 0, AV_OPT_TYPE_CONST, | ||
| 457 | { .i64 = CHANNEL_ORDER_DEFAULT }, .flags = PARAM, .unit = "channel_order" }, | ||
| 458 | { "coded", "order in which the channels are coded in the bitstream", | ||
| 459 | 0, AV_OPT_TYPE_CONST, { .i64 = CHANNEL_ORDER_CODED }, .flags = PARAM, .unit = "channel_order" }, | ||
| 460 | |||
| 461 | { "downmix", "Request a specific channel layout from the decoder", OFFSET(downmix_layout), | ||
| 462 | AV_OPT_TYPE_CHLAYOUT, {.str = NULL}, .flags = PARAM }, | ||
| 463 | |||
| 464 | { NULL } | ||
| 465 | }; | ||
| 466 | |||
| 467 | static const AVClass dcadec_class = { | ||
| 468 | .class_name = "DCA decoder", | ||
| 469 | .item_name = av_default_item_name, | ||
| 470 | .option = dcadec_options, | ||
| 471 | .version = LIBAVUTIL_VERSION_INT, | ||
| 472 | .category = AV_CLASS_CATEGORY_DECODER, | ||
| 473 | }; | ||
| 474 | |||
| 475 | const FFCodec ff_dca_decoder = { | ||
| 476 | .p.name = "dca", | ||
| 477 | CODEC_LONG_NAME("DCA (DTS Coherent Acoustics)"), | ||
| 478 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 479 | .p.id = AV_CODEC_ID_DTS, | ||
| 480 | .priv_data_size = sizeof(DCAContext), | ||
| 481 | .init = dcadec_init, | ||
| 482 | FF_CODEC_DECODE_CB(dcadec_decode_frame), | ||
| 483 | .close = dcadec_close, | ||
| 484 | .flush = dcadec_flush, | ||
| 485 | .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF, | ||
| 486 | .p.priv_class = &dcadec_class, | ||
| 487 | .p.profiles = NULL_IF_CONFIG_SMALL(ff_dca_profiles), | ||
| 488 | .caps_internal = FF_CODEC_CAP_INIT_CLEANUP, | ||
| 489 | }; | ||
| 490 |