]> git.sesse.net Git - ffmpeg/blob - libavcodec/dcadec.c
Merge commit 'cd846b47977485bd4063e77a3324e6b7840567a2'
[ffmpeg] / libavcodec / dcadec.c
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/opt.h"
22 #include "libavutil/channel_layout.h"
23
24 #include "dcadec.h"
25 #include "dcamath.h"
26 #include "dca_syncwords.h"
27 #include "profiles.h"
28
29 #define MIN_PACKET_SIZE     16
30 #define MAX_PACKET_SIZE     0x104000
31
32 int ff_dca_set_channel_layout(AVCodecContext *avctx, int *ch_remap, int dca_mask)
33 {
34     static const uint8_t dca2wav_norm[28] = {
35          2,  0, 1, 9, 10,  3,  8,  4,  5,  9, 10, 6, 7, 12,
36         13, 14, 3, 6,  7, 11, 12, 14, 16, 15, 17, 8, 4,  5,
37     };
38
39     static const uint8_t dca2wav_wide[28] = {
40          2,  0, 1, 4,  5,  3,  8,  4,  5,  9, 10, 6, 7, 12,
41         13, 14, 3, 9, 10, 11, 12, 14, 16, 15, 17, 8, 4,  5,
42     };
43
44     int dca_ch, wav_ch, nchannels = 0;
45
46     if (avctx->request_channel_layout & AV_CH_LAYOUT_NATIVE) {
47         for (dca_ch = 0; dca_ch < DCA_SPEAKER_COUNT; dca_ch++)
48             if (dca_mask & (1U << dca_ch))
49                 ch_remap[nchannels++] = dca_ch;
50         avctx->channel_layout = dca_mask;
51     } else {
52         int wav_mask = 0;
53         int wav_map[18];
54         const uint8_t *dca2wav;
55         if (dca_mask == DCA_SPEAKER_LAYOUT_7POINT0_WIDE ||
56             dca_mask == DCA_SPEAKER_LAYOUT_7POINT1_WIDE)
57             dca2wav = dca2wav_wide;
58         else
59             dca2wav = dca2wav_norm;
60         for (dca_ch = 0; dca_ch < 28; dca_ch++) {
61             if (dca_mask & (1 << dca_ch)) {
62                 wav_ch = dca2wav[dca_ch];
63                 if (!(wav_mask & (1 << wav_ch))) {
64                     wav_map[wav_ch] = dca_ch;
65                     wav_mask |= 1 << wav_ch;
66                 }
67             }
68         }
69         for (wav_ch = 0; wav_ch < 18; wav_ch++)
70             if (wav_mask & (1 << wav_ch))
71                 ch_remap[nchannels++] = wav_map[wav_ch];
72         avctx->channel_layout = wav_mask;
73     }
74
75     avctx->channels = nchannels;
76     return nchannels;
77 }
78
79 static uint16_t crc16(const uint8_t *data, int size)
80 {
81     static const uint16_t crctab[16] = {
82         0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
83         0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
84     };
85
86     uint16_t res = 0xffff;
87     int i;
88
89     for (i = 0; i < size; i++) {
90         res = (res << 4) ^ crctab[(data[i] >> 4) ^ (res >> 12)];
91         res = (res << 4) ^ crctab[(data[i] & 15) ^ (res >> 12)];
92     }
93
94     return res;
95 }
96
97 int ff_dca_check_crc(GetBitContext *s, int p1, int p2)
98 {
99     if (((p1 | p2) & 7) || p1 < 0 || p2 > s->size_in_bits || p2 - p1 < 16)
100         return -1;
101     if (crc16(s->buffer + p1 / 8, (p2 - p1) / 8))
102         return -1;
103     return 0;
104 }
105
106 void ff_dca_downmix_to_stereo_fixed(DCADSPContext *dcadsp, int32_t **samples,
107                                     int *coeff_l, int nsamples, int ch_mask)
108 {
109     int pos, spkr, max_spkr = av_log2(ch_mask);
110     int *coeff_r = coeff_l + av_popcount(ch_mask);
111
112     av_assert0(DCA_HAS_STEREO(ch_mask));
113
114     // Scale left and right channels
115     pos = (ch_mask & DCA_SPEAKER_MASK_C);
116     dcadsp->dmix_scale(samples[DCA_SPEAKER_L], coeff_l[pos    ], nsamples);
117     dcadsp->dmix_scale(samples[DCA_SPEAKER_R], coeff_r[pos + 1], nsamples);
118
119     // Downmix remaining channels
120     for (spkr = 0; spkr <= max_spkr; spkr++) {
121         if (!(ch_mask & (1U << spkr)))
122             continue;
123
124         if (*coeff_l && spkr != DCA_SPEAKER_L)
125             dcadsp->dmix_add(samples[DCA_SPEAKER_L], samples[spkr],
126                              *coeff_l, nsamples);
127
128         if (*coeff_r && spkr != DCA_SPEAKER_R)
129             dcadsp->dmix_add(samples[DCA_SPEAKER_R], samples[spkr],
130                              *coeff_r, nsamples);
131
132         coeff_l++;
133         coeff_r++;
134     }
135 }
136
137 void ff_dca_downmix_to_stereo_float(AVFloatDSPContext *fdsp, float **samples,
138                                     int *coeff_l, int nsamples, int ch_mask)
139 {
140     int pos, spkr, max_spkr = av_log2(ch_mask);
141     int *coeff_r = coeff_l + av_popcount(ch_mask);
142     const float scale = 1.0f / (1 << 15);
143
144     av_assert0(DCA_HAS_STEREO(ch_mask));
145
146     // Scale left and right channels
147     pos = (ch_mask & DCA_SPEAKER_MASK_C);
148     fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_L], samples[DCA_SPEAKER_L],
149                              coeff_l[pos    ] * scale, nsamples);
150     fdsp->vector_fmul_scalar(samples[DCA_SPEAKER_R], samples[DCA_SPEAKER_R],
151                              coeff_r[pos + 1] * scale, nsamples);
152
153     // Downmix remaining channels
154     for (spkr = 0; spkr <= max_spkr; spkr++) {
155         if (!(ch_mask & (1U << spkr)))
156             continue;
157
158         if (*coeff_l && spkr != DCA_SPEAKER_L)
159             fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_L], samples[spkr],
160                                      *coeff_l * scale, nsamples);
161
162         if (*coeff_r && spkr != DCA_SPEAKER_R)
163             fdsp->vector_fmac_scalar(samples[DCA_SPEAKER_R], samples[spkr],
164                                      *coeff_r * scale, nsamples);
165
166         coeff_l++;
167         coeff_r++;
168     }
169 }
170
171 static int convert_bitstream(const uint8_t *src, int src_size, uint8_t *dst, int max_size)
172 {
173     switch (AV_RB32(src)) {
174     case DCA_SYNCWORD_CORE_BE:
175     case DCA_SYNCWORD_SUBSTREAM:
176         memcpy(dst, src, src_size);
177         return src_size;
178     case DCA_SYNCWORD_CORE_LE:
179     case DCA_SYNCWORD_CORE_14B_BE:
180     case DCA_SYNCWORD_CORE_14B_LE:
181         return avpriv_dca_convert_bitstream(src, src_size, dst, max_size);
182     default:
183         return AVERROR_INVALIDDATA;
184     }
185 }
186
187 static int dcadec_decode_frame(AVCodecContext *avctx, void *data,
188                                int *got_frame_ptr, AVPacket *avpkt)
189 {
190     DCAContext *s = avctx->priv_data;
191     AVFrame *frame = data;
192     uint8_t *input = avpkt->data;
193     int input_size = avpkt->size;
194     int i, ret, prev_packet = s->packet;
195
196     if (input_size < MIN_PACKET_SIZE || input_size > MAX_PACKET_SIZE) {
197         av_log(avctx, AV_LOG_ERROR, "Invalid packet size\n");
198         return AVERROR_INVALIDDATA;
199     }
200
201     av_fast_malloc(&s->buffer, &s->buffer_size,
202                    FFALIGN(input_size, 4096) + DCA_BUFFER_PADDING_SIZE);
203     if (!s->buffer)
204         return AVERROR(ENOMEM);
205
206     for (i = 0, ret = AVERROR_INVALIDDATA; i < input_size - MIN_PACKET_SIZE + 1 && ret < 0; i++)
207         ret = convert_bitstream(input + i, input_size - i, s->buffer, s->buffer_size);
208
209     if (ret < 0)
210         return ret;
211
212     input      = s->buffer;
213     input_size = ret;
214
215     s->packet = 0;
216
217     // Parse backward compatible core sub-stream
218     if (AV_RB32(input) == DCA_SYNCWORD_CORE_BE) {
219         int frame_size;
220
221         if ((ret = ff_dca_core_parse(&s->core, input, input_size)) < 0) {
222             s->core_residual_valid = 0;
223             return ret;
224         }
225
226         s->packet |= DCA_PACKET_CORE;
227
228         // EXXS data must be aligned on 4-byte boundary
229         frame_size = FFALIGN(s->core.frame_size, 4);
230         if (input_size - 4 > frame_size) {
231             input      += frame_size;
232             input_size -= frame_size;
233         }
234     }
235
236     if (!s->core_only) {
237         DCAExssAsset *asset = NULL;
238
239         // Parse extension sub-stream (EXSS)
240         if (AV_RB32(input) == DCA_SYNCWORD_SUBSTREAM) {
241             if ((ret = ff_dca_exss_parse(&s->exss, input, input_size)) < 0) {
242                 if (avctx->err_recognition & AV_EF_EXPLODE)
243                     return ret;
244             } else {
245                 s->packet |= DCA_PACKET_EXSS;
246                 asset = &s->exss.assets[0];
247             }
248         }
249
250         // Parse XLL component in EXSS
251         if (asset && (asset->extension_mask & DCA_EXSS_XLL)) {
252             if ((ret = ff_dca_xll_parse(&s->xll, input, asset)) < 0) {
253                 // Conceal XLL synchronization error
254                 if (ret == AVERROR(EAGAIN)
255                     && (prev_packet & DCA_PACKET_XLL)
256                     && (s->packet & DCA_PACKET_CORE))
257                     s->packet |= DCA_PACKET_XLL | DCA_PACKET_RECOVERY;
258                 else if (ret == AVERROR(ENOMEM) || (avctx->err_recognition & AV_EF_EXPLODE))
259                     return ret;
260             } else {
261                 s->packet |= DCA_PACKET_XLL;
262             }
263         }
264
265         // Parse core extensions in EXSS or backward compatible core sub-stream
266         if ((s->packet & DCA_PACKET_CORE)
267             && (ret = ff_dca_core_parse_exss(&s->core, input, asset)) < 0)
268             return ret;
269     }
270
271     // Filter the frame
272     if (s->packet & DCA_PACKET_XLL) {
273         if (s->packet & DCA_PACKET_CORE) {
274             int x96_synth = -1;
275
276             // Enable X96 synthesis if needed
277             if (s->xll.chset[0].freq == 96000 && s->core.sample_rate == 48000)
278                 x96_synth = 1;
279
280             if ((ret = ff_dca_core_filter_fixed(&s->core, x96_synth)) < 0) {
281                 s->core_residual_valid = 0;
282                 return ret;
283             }
284
285             // Force lossy downmixed output on the first core frame filtered.
286             // This prevents audible clicks when seeking and is consistent with
287             // what reference decoder does when there are multiple channel sets.
288             if (!s->core_residual_valid) {
289                 if (s->xll.nreschsets > 0 && s->xll.nchsets > 1)
290                     s->packet |= DCA_PACKET_RECOVERY;
291                 s->core_residual_valid = 1;
292             }
293         }
294
295         if ((ret = ff_dca_xll_filter_frame(&s->xll, frame)) < 0) {
296             // Fall back to core unless hard error
297             if (!(s->packet & DCA_PACKET_CORE))
298                 return ret;
299             if (ret != AVERROR_INVALIDDATA || (avctx->err_recognition & AV_EF_EXPLODE))
300                 return ret;
301             if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0) {
302                 s->core_residual_valid = 0;
303                 return ret;
304             }
305         }
306     } else if (s->packet & DCA_PACKET_CORE) {
307         if ((ret = ff_dca_core_filter_frame(&s->core, frame)) < 0) {
308             s->core_residual_valid = 0;
309             return ret;
310         }
311         s->core_residual_valid = !!(s->core.filter_mode & DCA_FILTER_MODE_FIXED);
312     } else {
313         return AVERROR_INVALIDDATA;
314     }
315
316     *got_frame_ptr = 1;
317
318     return avpkt->size;
319 }
320
321 static av_cold void dcadec_flush(AVCodecContext *avctx)
322 {
323     DCAContext *s = avctx->priv_data;
324
325     ff_dca_core_flush(&s->core);
326     ff_dca_xll_flush(&s->xll);
327
328     s->core_residual_valid = 0;
329 }
330
331 static av_cold int dcadec_close(AVCodecContext *avctx)
332 {
333     DCAContext *s = avctx->priv_data;
334
335     ff_dca_core_close(&s->core);
336     ff_dca_xll_close(&s->xll);
337
338     av_freep(&s->buffer);
339     s->buffer_size = 0;
340
341     return 0;
342 }
343
344 static av_cold int dcadec_init(AVCodecContext *avctx)
345 {
346     DCAContext *s = avctx->priv_data;
347
348     s->avctx = avctx;
349     s->core.avctx = avctx;
350     s->exss.avctx = avctx;
351     s->xll.avctx = avctx;
352
353     if (ff_dca_core_init(&s->core) < 0)
354         return AVERROR(ENOMEM);
355
356     ff_dcadsp_init(&s->dcadsp);
357     s->core.dcadsp = &s->dcadsp;
358     s->xll.dcadsp = &s->dcadsp;
359
360     switch (avctx->request_channel_layout & ~AV_CH_LAYOUT_NATIVE) {
361     case 0:
362         s->request_channel_layout = 0;
363         break;
364     case AV_CH_LAYOUT_STEREO:
365     case AV_CH_LAYOUT_STEREO_DOWNMIX:
366         s->request_channel_layout = DCA_SPEAKER_LAYOUT_STEREO;
367         break;
368     case AV_CH_LAYOUT_5POINT0:
369         s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT0;
370         break;
371     case AV_CH_LAYOUT_5POINT1:
372         s->request_channel_layout = DCA_SPEAKER_LAYOUT_5POINT1;
373         break;
374     default:
375         av_log(avctx, AV_LOG_WARNING, "Invalid request_channel_layout\n");
376         break;
377     }
378
379     avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
380     avctx->bits_per_raw_sample = 24;
381
382     return 0;
383 }
384
385 #define OFFSET(x) offsetof(DCAContext, x)
386 #define PARAM AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_DECODING_PARAM
387
388 static const AVOption dcadec_options[] = {
389     { "core_only", "Decode core only without extensions", OFFSET(core_only), AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, PARAM },
390     { NULL }
391 };
392
393 static const AVClass dcadec_class = {
394     .class_name = "DCA decoder",
395     .item_name  = av_default_item_name,
396     .option     = dcadec_options,
397     .version    = LIBAVUTIL_VERSION_INT,
398     .category   = AV_CLASS_CATEGORY_DECODER,
399 };
400
401 AVCodec ff_dca_decoder = {
402     .name           = "dca",
403     .long_name      = NULL_IF_CONFIG_SMALL("DCA (DTS Coherent Acoustics)"),
404     .type           = AVMEDIA_TYPE_AUDIO,
405     .id             = AV_CODEC_ID_DTS,
406     .priv_data_size = sizeof(DCAContext),
407     .init           = dcadec_init,
408     .decode         = dcadec_decode_frame,
409     .close          = dcadec_close,
410     .flush          = dcadec_flush,
411     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
412     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, AV_SAMPLE_FMT_S32P,
413                                                       AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE },
414     .priv_class     = &dcadec_class,
415     .profiles       = NULL_IF_CONFIG_SMALL(ff_dca_profiles),
416     .caps_internal  = FF_CODEC_CAP_INIT_CLEANUP,
417 };