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