]> git.sesse.net Git - ffmpeg/blob - libavcodec/dstdec.c
avcodec/dstdec: Check that AC probabilities are within range
[ffmpeg] / libavcodec / dstdec.c
1 /*
2  * Direct Stream Transfer (DST) decoder
3  * Copyright (c) 2014 Peter Ross <pross@xvid.org>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Direct Stream Transfer (DST) decoder
25  * ISO/IEC 14496-3 Part 3 Subpart 10: Technical description of lossless coding of oversampled audio
26  */
27
28 #include "libavutil/avassert.h"
29 #include "libavutil/intreadwrite.h"
30 #include "internal.h"
31 #include "get_bits.h"
32 #include "avcodec.h"
33 #include "golomb.h"
34 #include "mathops.h"
35 #include "dsd.h"
36
37 #define DST_MAX_CHANNELS 6
38 #define DST_MAX_ELEMENTS (2 * DST_MAX_CHANNELS)
39
40 #define DSD_FS44(sample_rate) (sample_rate * 8LL / 44100)
41
42 #define DST_SAMPLES_PER_FRAME(sample_rate) (588 * DSD_FS44(sample_rate))
43
44 static const int8_t fsets_code_pred_coeff[3][3] = {
45     {  -8 },
46     { -16,  8 },
47     {  -9, -5, 6 },
48 };
49
50 static const int8_t probs_code_pred_coeff[3][3] = {
51     {  -8 },
52     { -16,  8 },
53     { -24, 24, -8 },
54 };
55
56 typedef struct ArithCoder {
57     unsigned int a;
58     unsigned int c;
59     int overread;
60 } ArithCoder;
61
62 typedef struct Table {
63     unsigned int elements;
64     unsigned int length[DST_MAX_ELEMENTS];
65     int coeff[DST_MAX_ELEMENTS][128];
66 } Table;
67
68 typedef struct DSTContext {
69     AVClass *class;
70
71     GetBitContext gb;
72     ArithCoder ac;
73     Table fsets, probs;
74     DECLARE_ALIGNED(16, uint8_t, status)[DST_MAX_CHANNELS][16];
75     DECLARE_ALIGNED(16, int16_t, filter)[DST_MAX_ELEMENTS][16][256];
76     DSDContext dsdctx[DST_MAX_CHANNELS];
77 } DSTContext;
78
79 static av_cold int decode_init(AVCodecContext *avctx)
80 {
81     DSTContext *s = avctx->priv_data;
82     int i;
83
84     if (avctx->channels > DST_MAX_CHANNELS) {
85         avpriv_request_sample(avctx, "Channel count %d", avctx->channels);
86         return AVERROR_PATCHWELCOME;
87     }
88
89     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
90
91     for (i = 0; i < avctx->channels; i++)
92         memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
93
94     ff_init_dsd_data();
95
96     return 0;
97 }
98
99 static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels)
100 {
101     int ch;
102     t->elements = 1;
103     map[0] = 0;
104     if (!get_bits1(gb)) {
105         for (ch = 1; ch < channels; ch++) {
106             int bits = av_log2(t->elements) + 1;
107             map[ch] = get_bits(gb, bits);
108             if (map[ch] == t->elements) {
109                 t->elements++;
110                 if (t->elements >= DST_MAX_ELEMENTS)
111                     return AVERROR_INVALIDDATA;
112             } else if (map[ch] > t->elements) {
113                 return AVERROR_INVALIDDATA;
114             }
115         }
116     } else {
117         memset(map, 0, sizeof(*map) * DST_MAX_CHANNELS);
118     }
119     return 0;
120 }
121
122 static av_always_inline int get_sr_golomb_dst(GetBitContext *gb, unsigned int k)
123 {
124     int v = get_ur_golomb(gb, k, get_bits_left(gb), 0);
125     if (v && get_bits1(gb))
126         v = -v;
127     return v;
128 }
129
130 static void read_uncoded_coeff(GetBitContext *gb, int *dst, unsigned int elements,
131                                int coeff_bits, int is_signed, int offset)
132 {
133     int i;
134
135     for (i = 0; i < elements; i++) {
136         dst[i] = (is_signed ? get_sbits(gb, coeff_bits) : get_bits(gb, coeff_bits)) + offset;
137     }
138 }
139
140 static int read_table(GetBitContext *gb, Table *t, const int8_t code_pred_coeff[3][3],
141                       int length_bits, int coeff_bits, int is_signed, int offset)
142 {
143     unsigned int i, j, k;
144     for (i = 0; i < t->elements; i++) {
145         t->length[i] = get_bits(gb, length_bits) + 1;
146         if (!get_bits1(gb)) {
147             read_uncoded_coeff(gb, t->coeff[i], t->length[i], coeff_bits, is_signed, offset);
148         } else {
149             int method = get_bits(gb, 2), lsb_size;
150             if (method == 3)
151                 return AVERROR_INVALIDDATA;
152
153             read_uncoded_coeff(gb, t->coeff[i], method + 1, coeff_bits, is_signed, offset);
154
155             lsb_size  = get_bits(gb, 3);
156             for (j = method + 1; j < t->length[i]; j++) {
157                 int c, x = 0;
158                 for (k = 0; k < method + 1; k++)
159                     x += code_pred_coeff[method][k] * t->coeff[i][j - k - 1];
160                 c = get_sr_golomb_dst(gb, lsb_size);
161                 if (x >= 0)
162                     c -= (x + 4) / 8;
163                 else
164                     c += (-x + 3) / 8;
165                 if (!is_signed) {
166                     if (c < offset || c >= offset + (1<<coeff_bits))
167                         return AVERROR_INVALIDDATA;
168                 }
169                 t->coeff[i][j] = c;
170             }
171         }
172     }
173     return 0;
174 }
175
176 static void ac_init(ArithCoder *ac, GetBitContext *gb)
177 {
178     ac->a = 4095;
179     ac->c = get_bits(gb, 12);
180     ac->overread = 0;
181 }
182
183 static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, int *e)
184 {
185     unsigned int k = (ac->a >> 8) | ((ac->a >> 7) & 1);
186     unsigned int q = k * p;
187     unsigned int a_q = ac->a - q;
188
189     *e = ac->c < a_q;
190     if (*e) {
191         ac->a  = a_q;
192     } else {
193         ac->a  = q;
194         ac->c -= a_q;
195     }
196
197     if (ac->a < 2048) {
198         int n = 11 - av_log2(ac->a);
199         ac->a <<= n;
200         if (get_bits_left(gb) < n)
201             ac->overread ++;
202         ac->c = (ac->c << n) | get_bits(gb, n);
203     }
204 }
205
206 static uint8_t prob_dst_x_bit(int c)
207 {
208     return (ff_reverse[c & 127] >> 1) + 1;
209 }
210
211 static void build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *fsets)
212 {
213     int i, j, k, l;
214
215     for (i = 0; i < fsets->elements; i++) {
216         int length = fsets->length[i];
217
218         for (j = 0; j < 16; j++) {
219             int total = av_clip(length - j * 8, 0, 8);
220
221             for (k = 0; k < 256; k++) {
222                 int v = 0;
223
224                 for (l = 0; l < total; l++)
225                     v += (((k >> l) & 1) * 2 - 1) * fsets->coeff[i][j * 8 + l];
226                 table[i][j][k] = v;
227             }
228         }
229     }
230 }
231
232 static int decode_frame(AVCodecContext *avctx, void *data,
233                         int *got_frame_ptr, AVPacket *avpkt)
234 {
235     unsigned samples_per_frame = DST_SAMPLES_PER_FRAME(avctx->sample_rate);
236     unsigned map_ch_to_felem[DST_MAX_CHANNELS];
237     unsigned map_ch_to_pelem[DST_MAX_CHANNELS];
238     unsigned i, ch, same_map, dst_x_bit;
239     unsigned half_prob[DST_MAX_CHANNELS];
240     const int channels = avctx->channels;
241     DSTContext *s = avctx->priv_data;
242     GetBitContext *gb = &s->gb;
243     ArithCoder *ac = &s->ac;
244     AVFrame *frame = data;
245     uint8_t *dsd;
246     float *pcm;
247     int ret;
248
249     if (avpkt->size <= 1)
250         return AVERROR_INVALIDDATA;
251
252     frame->nb_samples = samples_per_frame / 8;
253     if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
254         return ret;
255     dsd = frame->data[0];
256     pcm = (float *)frame->data[0];
257
258     if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
259         return ret;
260
261     if (!get_bits1(gb)) {
262         skip_bits1(gb);
263         if (get_bits(gb, 6))
264             return AVERROR_INVALIDDATA;
265         memcpy(frame->data[0], avpkt->data + 1, FFMIN(avpkt->size - 1, frame->nb_samples * avctx->channels));
266         goto dsd;
267     }
268
269     /* Segmentation (10.4, 10.5, 10.6) */
270
271     if (!get_bits1(gb)) {
272         avpriv_request_sample(avctx, "Not Same Segmentation");
273         return AVERROR_PATCHWELCOME;
274     }
275
276     if (!get_bits1(gb)) {
277         avpriv_request_sample(avctx, "Not Same Segmentation For All Channels");
278         return AVERROR_PATCHWELCOME;
279     }
280
281     if (!get_bits1(gb)) {
282         avpriv_request_sample(avctx, "Not End Of Channel Segmentation");
283         return AVERROR_PATCHWELCOME;
284     }
285
286     /* Mapping (10.7, 10.8, 10.9) */
287
288     same_map = get_bits1(gb);
289
290     if ((ret = read_map(gb, &s->fsets, map_ch_to_felem, avctx->channels)) < 0)
291         return ret;
292
293     if (same_map) {
294         s->probs.elements = s->fsets.elements;
295         memcpy(map_ch_to_pelem, map_ch_to_felem, sizeof(map_ch_to_felem));
296     } else {
297         avpriv_request_sample(avctx, "Not Same Mapping");
298         if ((ret = read_map(gb, &s->probs, map_ch_to_pelem, avctx->channels)) < 0)
299             return ret;
300     }
301
302     /* Half Probability (10.10) */
303
304     for (ch = 0; ch < avctx->channels; ch++)
305         half_prob[ch] = get_bits1(gb);
306
307     /* Filter Coef Sets (10.12) */
308
309     ret = read_table(gb, &s->fsets, fsets_code_pred_coeff, 7, 9, 1, 0);
310     if (ret < 0)
311         return ret;
312
313     /* Probability Tables (10.13) */
314
315     ret = read_table(gb, &s->probs, probs_code_pred_coeff, 6, 7, 0, 1);
316     if (ret < 0)
317         return ret;
318
319     /* Arithmetic Coded Data (10.11) */
320
321     if (get_bits1(gb))
322         return AVERROR_INVALIDDATA;
323     ac_init(ac, gb);
324
325     build_filter(s->filter, &s->fsets);
326
327     memset(s->status, 0xAA, sizeof(s->status));
328     memset(dsd, 0, frame->nb_samples * 4 * avctx->channels);
329
330     ac_get(ac, gb, prob_dst_x_bit(s->fsets.coeff[0][0]), &dst_x_bit);
331
332     for (i = 0; i < samples_per_frame; i++) {
333         for (ch = 0; ch < channels; ch++) {
334             const unsigned felem = map_ch_to_felem[ch];
335             int16_t (*filter)[256] = s->filter[felem];
336             uint8_t *status = s->status[ch];
337             int prob, residual, v;
338
339 #define F(x) filter[(x)][status[(x)]]
340             const int16_t predict = F( 0) + F( 1) + F( 2) + F( 3) +
341                                     F( 4) + F( 5) + F( 6) + F( 7) +
342                                     F( 8) + F( 9) + F(10) + F(11) +
343                                     F(12) + F(13) + F(14) + F(15);
344 #undef F
345
346             if (!half_prob[ch] || i >= s->fsets.length[felem]) {
347                 unsigned pelem = map_ch_to_pelem[ch];
348                 unsigned index = FFABS(predict) >> 3;
349                 prob = s->probs.coeff[pelem][FFMIN(index, s->probs.length[pelem] - 1)];
350             } else {
351                 prob = 128;
352             }
353
354             if (ac->overread > 16)
355                 return AVERROR_INVALIDDATA;
356
357             ac_get(ac, gb, prob, &residual);
358             v = ((predict >> 15) ^ residual) & 1;
359             dsd[((i >> 3) * channels + ch) << 2] |= v << (7 - (i & 0x7 ));
360
361             AV_WL64A(status + 8, (AV_RL64A(status + 8) << 1) | ((AV_RL64A(status) >> 63) & 1));
362             AV_WL64A(status, (AV_RL64A(status) << 1) | v);
363         }
364     }
365
366 dsd:
367     for (i = 0; i < avctx->channels; i++) {
368         ff_dsd2pcm_translate(&s->dsdctx[i], frame->nb_samples, 0,
369                              frame->data[0] + i * 4,
370                              avctx->channels * 4, pcm + i, avctx->channels);
371     }
372
373     *got_frame_ptr = 1;
374
375     return avpkt->size;
376 }
377
378 AVCodec ff_dst_decoder = {
379     .name           = "dst",
380     .long_name      = NULL_IF_CONFIG_SMALL("DST (Digital Stream Transfer)"),
381     .type           = AVMEDIA_TYPE_AUDIO,
382     .id             = AV_CODEC_ID_DST,
383     .priv_data_size = sizeof(DSTContext),
384     .init           = decode_init,
385     .decode         = decode_frame,
386     .capabilities   = AV_CODEC_CAP_DR1,
387     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
388                                                       AV_SAMPLE_FMT_NONE },
389 };