Coverage Report

Created: 2024-09-06 07:53

/src/ffmpeg/libavcodec/dstdec.c
Line
Count
Source (jump to first uncovered line)
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/intreadwrite.h"
29
#include "libavutil/mem_internal.h"
30
#include "libavutil/reverse.h"
31
#include "codec_internal.h"
32
#include "decode.h"
33
#include "get_bits.h"
34
#include "avcodec.h"
35
#include "golomb.h"
36
#include "dsd.h"
37
38
13.8k
#define DST_MAX_CHANNELS 6
39
5.52k
#define DST_MAX_ELEMENTS (2 * DST_MAX_CHANNELS)
40
41
114k
#define DSD_FS44(sample_rate) (sample_rate * 8LL / 44100)
42
43
114k
#define DST_SAMPLES_PER_FRAME(sample_rate) (588 * DSD_FS44(sample_rate))
44
45
static const int8_t fsets_code_pred_coeff[3][3] = {
46
    {  -8 },
47
    { -16,  8 },
48
    {  -9, -5, 6 },
49
};
50
51
static const int8_t probs_code_pred_coeff[3][3] = {
52
    {  -8 },
53
    { -16,  8 },
54
    { -24, 24, -8 },
55
};
56
57
typedef struct ArithCoder {
58
    unsigned int a;
59
    unsigned int c;
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
839
{
81
839
    DSTContext *s = avctx->priv_data;
82
839
    int i;
83
84
839
    if (avctx->ch_layout.nb_channels > DST_MAX_CHANNELS) {
85
42
        avpriv_request_sample(avctx, "Channel count %d", avctx->ch_layout.nb_channels);
86
42
        return AVERROR_PATCHWELCOME;
87
42
    }
88
89
    // the sample rate is only allowed to be 64,128,256 * 44100 by ISO/IEC 14496-3:2005(E)
90
    // We are a bit more tolerant here, but this check is needed to bound the size and duration
91
797
    if (avctx->sample_rate > 512 * 44100)
92
9
        return AVERROR_INVALIDDATA;
93
94
95
788
    if (DST_SAMPLES_PER_FRAME(avctx->sample_rate) & 7) {
96
2
        return AVERROR_PATCHWELCOME;
97
2
    }
98
99
786
    avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
100
101
2.46k
    for (i = 0; i < avctx->ch_layout.nb_channels; i++)
102
1.68k
        memset(s->dsdctx[i].buf, 0x69, sizeof(s->dsdctx[i].buf));
103
104
786
    ff_init_dsd_data();
105
106
786
    return 0;
107
788
}
108
109
static int read_map(GetBitContext *gb, Table *t, unsigned int map[DST_MAX_CHANNELS], int channels)
110
12.4k
{
111
12.4k
    int ch;
112
12.4k
    t->elements = 1;
113
12.4k
    map[0] = 0;
114
12.4k
    if (!get_bits1(gb)) {
115
20.8k
        for (ch = 1; ch < channels; ch++) {
116
16.3k
            int bits = av_log2(t->elements) + 1;
117
16.3k
            map[ch] = get_bits(gb, bits);
118
16.3k
            if (map[ch] == t->elements) {
119
5.52k
                t->elements++;
120
5.52k
                if (t->elements >= DST_MAX_ELEMENTS)
121
0
                    return AVERROR_INVALIDDATA;
122
10.7k
            } else if (map[ch] > t->elements) {
123
519
                return AVERROR_INVALIDDATA;
124
519
            }
125
16.3k
        }
126
7.47k
    } else {
127
7.47k
        memset(map, 0, sizeof(*map) * DST_MAX_CHANNELS);
128
7.47k
    }
129
11.9k
    return 0;
130
12.4k
}
131
132
static av_always_inline int get_sr_golomb_dst(GetBitContext *gb, unsigned int k)
133
312k
{
134
312k
    int v = get_ur_golomb_jpegls(gb, k, get_bits_left(gb), 0);
135
312k
    if (v && get_bits1(gb))
136
30.1k
        v = -v;
137
312k
    return v;
138
312k
}
139
140
static void read_uncoded_coeff(GetBitContext *gb, int *dst, unsigned int elements,
141
                               int coeff_bits, int is_signed, int offset)
142
22.8k
{
143
22.8k
    int i;
144
145
176k
    for (i = 0; i < elements; i++) {
146
153k
        dst[i] = (is_signed ? get_sbits(gb, coeff_bits) : get_bits(gb, coeff_bits)) + offset;
147
153k
    }
148
22.8k
}
149
150
static int read_table(GetBitContext *gb, Table *t, const int8_t code_pred_coeff[3][3],
151
                      int length_bits, int coeff_bits, int is_signed, int offset)
152
18.6k
{
153
18.6k
    unsigned int i, j, k;
154
40.5k
    for (i = 0; i < t->elements; i++) {
155
23.4k
        t->length[i] = get_bits(gb, length_bits) + 1;
156
23.4k
        if (!get_bits1(gb)) {
157
13.9k
            read_uncoded_coeff(gb, t->coeff[i], t->length[i], coeff_bits, is_signed, offset);
158
13.9k
        } else {
159
9.46k
            int method = get_bits(gb, 2), lsb_size;
160
9.46k
            if (method == 3)
161
582
                return AVERROR_INVALIDDATA;
162
163
8.88k
            read_uncoded_coeff(gb, t->coeff[i], method + 1, coeff_bits, is_signed, offset);
164
165
8.88k
            lsb_size  = get_bits(gb, 3);
166
320k
            for (j = method + 1; j < t->length[i]; j++) {
167
312k
                int c, x = 0;
168
857k
                for (k = 0; k < method + 1; k++)
169
545k
                    x += code_pred_coeff[method][k] * (unsigned)t->coeff[i][j - k - 1];
170
312k
                c = get_sr_golomb_dst(gb, lsb_size);
171
312k
                if (x >= 0)
172
184k
                    c -= (x + 4) / 8;
173
127k
                else
174
127k
                    c += (-x + 3) / 8;
175
312k
                if (!is_signed) {
176
5.48k
                    if (c < offset || c >= offset + (1<<coeff_bits))
177
1.02k
                        return AVERROR_INVALIDDATA;
178
5.48k
                }
179
311k
                t->coeff[i][j] = c;
180
311k
            }
181
8.88k
        }
182
23.4k
    }
183
17.0k
    return 0;
184
18.6k
}
185
186
static void ac_init(ArithCoder *ac, GetBitContext *gb)
187
7.27k
{
188
7.27k
    ac->a = 4095;
189
7.27k
    ac->c = get_bits(gb, 12);
190
7.27k
}
191
192
static av_always_inline void ac_get(ArithCoder *ac, GetBitContext *gb, int p, int *e)
193
65.3M
{
194
65.3M
    unsigned int k = (ac->a >> 8) | ((ac->a >> 7) & 1);
195
65.3M
    unsigned int q = k * p;
196
65.3M
    unsigned int a_q = ac->a - q;
197
198
65.3M
    *e = ac->c < a_q;
199
65.3M
    if (*e) {
200
62.6M
        ac->a  = a_q;
201
62.6M
    } else {
202
2.68M
        ac->a  = q;
203
2.68M
        ac->c -= a_q;
204
2.68M
    }
205
206
65.3M
    if (ac->a < 2048) {
207
7.26M
        int n = 11 - av_log2(ac->a);
208
7.26M
        ac->a <<= n;
209
7.26M
        ac->c = (ac->c << n) | get_bits(gb, n);
210
7.26M
    }
211
65.3M
}
212
213
static uint8_t prob_dst_x_bit(int c)
214
6.45k
{
215
6.45k
    return (ff_reverse[c & 127] >> 1) + 1;
216
6.45k
}
217
218
static int build_filter(int16_t table[DST_MAX_ELEMENTS][16][256], const Table *fsets)
219
7.27k
{
220
7.27k
    int i, j, k, l;
221
222
15.9k
    for (i = 0; i < fsets->elements; i++) {
223
9.52k
        int length = fsets->length[i];
224
225
152k
        for (j = 0; j < 16; j++) {
226
143k
            int total = av_clip(length - j * 8, 0, 8);
227
228
36.6M
            for (k = 0; k < 256; k++) {
229
36.4M
                int64_t v = 0;
230
231
121M
                for (l = 0; l < total; l++)
232
84.7M
                    v += (((k >> l) & 1) * 2 - 1) * fsets->coeff[i][j * 8 + l];
233
36.4M
                if ((int16_t)v != v)
234
821
                    return AVERROR_INVALIDDATA;
235
36.4M
                table[i][j][k] = v;
236
36.4M
            }
237
143k
        }
238
9.52k
    }
239
6.45k
    return 0;
240
7.27k
}
241
242
static int decode_frame(AVCodecContext *avctx, AVFrame *frame,
243
                        int *got_frame_ptr, AVPacket *avpkt)
244
113k
{
245
113k
    unsigned samples_per_frame = DST_SAMPLES_PER_FRAME(avctx->sample_rate);
246
113k
    unsigned map_ch_to_felem[DST_MAX_CHANNELS];
247
113k
    unsigned map_ch_to_pelem[DST_MAX_CHANNELS];
248
113k
    unsigned i, ch, same_map, dst_x_bit;
249
113k
    unsigned half_prob[DST_MAX_CHANNELS];
250
113k
    const int channels = avctx->ch_layout.nb_channels;
251
113k
    DSTContext *s = avctx->priv_data;
252
113k
    GetBitContext *gb = &s->gb;
253
113k
    ArithCoder *ac = &s->ac;
254
113k
    uint8_t *dsd;
255
113k
    float *pcm;
256
113k
    int ret;
257
258
113k
    if (avpkt->size <= 1)
259
87.0k
        return AVERROR_INVALIDDATA;
260
261
26.8k
    frame->nb_samples = samples_per_frame / 8;
262
26.8k
    if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
263
9.46k
        return ret;
264
17.3k
    dsd = frame->data[0];
265
17.3k
    pcm = (float *)frame->data[0];
266
267
17.3k
    if ((ret = init_get_bits8(gb, avpkt->data, avpkt->size)) < 0)
268
0
        return ret;
269
270
17.3k
    if (!get_bits1(gb)) {
271
6.07k
        skip_bits1(gb);
272
6.07k
        if (get_bits(gb, 6))
273
5.37k
            return AVERROR_INVALIDDATA;
274
705
        memcpy(frame->data[0], avpkt->data + 1, FFMIN(avpkt->size - 1, frame->nb_samples * channels));
275
705
        goto dsd;
276
6.07k
    }
277
278
    /* Segmentation (10.4, 10.5, 10.6) */
279
280
11.2k
    if (!get_bits1(gb)) {
281
618
        avpriv_request_sample(avctx, "Not Same Segmentation");
282
618
        return AVERROR_PATCHWELCOME;
283
618
    }
284
285
10.6k
    if (!get_bits1(gb)) {
286
254
        avpriv_request_sample(avctx, "Not Same Segmentation For All Channels");
287
254
        return AVERROR_PATCHWELCOME;
288
254
    }
289
290
10.3k
    if (!get_bits1(gb)) {
291
312
        avpriv_request_sample(avctx, "Not End Of Channel Segmentation");
292
312
        return AVERROR_PATCHWELCOME;
293
312
    }
294
295
    /* Mapping (10.7, 10.8, 10.9) */
296
297
10.0k
    same_map = get_bits1(gb);
298
299
10.0k
    if ((ret = read_map(gb, &s->fsets, map_ch_to_felem, channels)) < 0)
300
235
        return ret;
301
302
9.84k
    if (same_map) {
303
7.44k
        s->probs.elements = s->fsets.elements;
304
7.44k
        memcpy(map_ch_to_pelem, map_ch_to_felem, sizeof(map_ch_to_felem));
305
7.44k
    } else {
306
2.40k
        avpriv_request_sample(avctx, "Not Same Mapping");
307
2.40k
        if ((ret = read_map(gb, &s->probs, map_ch_to_pelem, channels)) < 0)
308
284
            return ret;
309
2.40k
    }
310
311
    /* Half Probability (10.10) */
312
313
37.7k
    for (ch = 0; ch < channels; ch++)
314
28.2k
        half_prob[ch] = get_bits1(gb);
315
316
    /* Filter Coef Sets (10.12) */
317
318
9.56k
    ret = read_table(gb, &s->fsets, fsets_code_pred_coeff, 7, 9, 1, 0);
319
9.56k
    if (ret < 0)
320
448
        return ret;
321
322
    /* Probability Tables (10.13) */
323
324
9.11k
    ret = read_table(gb, &s->probs, probs_code_pred_coeff, 6, 7, 0, 1);
325
9.11k
    if (ret < 0)
326
1.15k
        return ret;
327
328
    /* Arithmetic Coded Data (10.11) */
329
330
7.95k
    if (get_bits1(gb))
331
685
        return AVERROR_INVALIDDATA;
332
7.27k
    ac_init(ac, gb);
333
334
7.27k
    ret = build_filter(s->filter, &s->fsets);
335
7.27k
    if (ret < 0)
336
821
        return ret;
337
338
6.45k
    memset(s->status, 0xAA, sizeof(s->status));
339
6.45k
    memset(dsd, 0, frame->nb_samples * 4 * channels);
340
341
6.45k
    ac_get(ac, gb, prob_dst_x_bit(s->fsets.coeff[0][0]), &dst_x_bit);
342
343
27.5M
    for (i = 0; i < samples_per_frame; i++) {
344
92.8M
        for (ch = 0; ch < channels; ch++) {
345
65.3M
            const unsigned felem = map_ch_to_felem[ch];
346
65.3M
            int16_t (*filter)[256] = s->filter[felem];
347
65.3M
            uint8_t *status = s->status[ch];
348
65.3M
            int prob, residual, v;
349
350
1.04G
#define F(x) filter[(x)][status[(x)]]
351
65.3M
            const int16_t predict = F( 0) + F( 1) + F( 2) + F( 3) +
352
65.3M
                                    F( 4) + F( 5) + F( 6) + F( 7) +
353
65.3M
                                    F( 8) + F( 9) + F(10) + F(11) +
354
65.3M
                                    F(12) + F(13) + F(14) + F(15);
355
65.3M
#undef F
356
357
65.3M
            if (!half_prob[ch] || i >= s->fsets.length[felem]) {
358
64.8M
                unsigned pelem = map_ch_to_pelem[ch];
359
64.8M
                unsigned index = FFABS(predict) >> 3;
360
64.8M
                prob = s->probs.coeff[pelem][FFMIN(index, s->probs.length[pelem] - 1)];
361
64.8M
            } else {
362
447k
                prob = 128;
363
447k
            }
364
365
65.3M
            ac_get(ac, gb, prob, &residual);
366
65.3M
            v = ((predict >> 15) ^ residual) & 1;
367
65.3M
            dsd[((i >> 3) * channels + ch) << 2] |= v << (7 - (i & 0x7 ));
368
369
65.3M
            AV_WL64A(status + 8, (AV_RL64A(status + 8) << 1) | ((AV_RL64A(status) >> 63) & 1));
370
65.3M
            AV_WL64A(status, (AV_RL64A(status) << 1) | v);
371
65.3M
        }
372
27.5M
    }
373
374
7.15k
dsd:
375
28.2k
    for (i = 0; i < channels; i++) {
376
21.1k
        ff_dsd2pcm_translate(&s->dsdctx[i], frame->nb_samples, 0,
377
21.1k
                             frame->data[0] + i * 4,
378
21.1k
                             channels * 4, pcm + i, channels);
379
21.1k
    }
380
381
7.15k
    *got_frame_ptr = 1;
382
383
7.15k
    return avpkt->size;
384
6.45k
}
385
386
const FFCodec ff_dst_decoder = {
387
    .p.name         = "dst",
388
    CODEC_LONG_NAME("DST (Digital Stream Transfer)"),
389
    .p.type         = AVMEDIA_TYPE_AUDIO,
390
    .p.id           = AV_CODEC_ID_DST,
391
    .priv_data_size = sizeof(DSTContext),
392
    .init           = decode_init,
393
    FF_CODEC_DECODE_CB(decode_frame),
394
    .p.capabilities = AV_CODEC_CAP_DR1,
395
    .p.sample_fmts  = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_FLT,
396
                                                      AV_SAMPLE_FMT_NONE },
397
};