Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/shorten.c
Line
Count
Source
1
/*
2
 * Shorten decoder
3
 * Copyright (c) 2005 Jeff Muizelaar
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
 * Shorten decoder
25
 * @author Jeff Muizelaar
26
 */
27
28
#include <limits.h>
29
#include "libavutil/mem.h"
30
#include "avcodec.h"
31
#include "bswapdsp.h"
32
#include "bytestream.h"
33
#include "codec_internal.h"
34
#include "decode.h"
35
#include "get_bits.h"
36
#include "golomb.h"
37
38
20.5k
#define MAX_CHANNELS 8
39
9.87k
#define MAX_BLOCKSIZE 65535
40
41
16.5k
#define OUT_BUFFER_SIZE 16384
42
43
56.2k
#define ULONGSIZE 2
44
45
987
#define WAVE_FORMAT_PCM 0x0001
46
47
29.9k
#define DEFAULT_BLOCK_SIZE 256
48
49
20.9k
#define TYPESIZE 4
50
20.9k
#define CHANSIZE 0
51
12.7k
#define LPCQSIZE 2
52
13.9k
#define ENERGYSIZE 3
53
3.01k
#define BITSHIFTSIZE 2
54
55
#define TYPE_S8    1
56
1.51M
#define TYPE_U8    2
57
2.38M
#define TYPE_S16HL 3
58
#define TYPE_U16HL 4
59
2.57M
#define TYPE_S16LH 5
60
#define TYPE_U16LH 6
61
62
#define NWRAP 3
63
8.08k
#define NSKIPSIZE 1
64
65
34.1k
#define LPCQUANT 5
66
3.93k
#define V2LPCQOFFSET (1 << LPCQUANT)
67
68
71.9k
#define FNSIZE 2
69
#define FN_DIFF0        0
70
#define FN_DIFF1        1
71
#define FN_DIFF2        2
72
#define FN_DIFF3        3
73
5.43k
#define FN_QUIT         4
74
3.49k
#define FN_BLOCKSIZE    5
75
3.01k
#define FN_BITSHIFT     6
76
70.2k
#define FN_QLPC         7
77
39.9k
#define FN_ZERO         8
78
42.4k
#define FN_VERBATIM     9
79
80
/** indicates if the FN_* command is audio or non-audio */
81
static const uint8_t is_audio_command[10] = { 1, 1, 1, 1, 0, 0, 0, 1, 1, 0 };
82
83
10.5k
#define VERBATIM_CKSIZE_SIZE 5
84
4.24M
#define VERBATIM_BYTE_SIZE 8
85
8.09k
#define CANONICAL_HEADER_SIZE 44
86
87
typedef struct ShortenContext {
88
    AVCodecContext *avctx;
89
    GetBitContext gb;
90
91
    int min_framesize, max_framesize;
92
    unsigned channels;
93
94
    int32_t *decoded[MAX_CHANNELS];
95
    int32_t *decoded_base[MAX_CHANNELS];
96
    int32_t *offset[MAX_CHANNELS];
97
    int *coeffs;
98
    uint8_t *bitstream;
99
    int bitstream_size;
100
    int bitstream_index;
101
    unsigned int allocated_bitstream_size;
102
    int header_size;
103
    uint8_t header[OUT_BUFFER_SIZE];
104
    int version;
105
    int cur_chan;
106
    int bitshift;
107
    int nmean;
108
    int internal_ftype;
109
    int nwrap;
110
    int blocksize;
111
    int bitindex;
112
    int32_t lpcqoffset;
113
    int got_header;
114
    int got_quit_command;
115
    int swap;
116
    BswapDSPContext bdsp;
117
} ShortenContext;
118
119
static av_cold int shorten_decode_init(AVCodecContext *avctx)
120
4.18k
{
121
4.18k
    ShortenContext *s = avctx->priv_data;
122
4.18k
    s->avctx          = avctx;
123
124
4.18k
    ff_bswapdsp_init(&s->bdsp);
125
126
4.18k
    return 0;
127
4.18k
}
128
129
static int allocate_buffers(ShortenContext *s)
130
11.1k
{
131
11.1k
    int i, chan, err;
132
133
29.4k
    for (chan = 0; chan < s->channels; chan++) {
134
18.2k
        if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
135
0
            av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
136
0
            return AVERROR_INVALIDDATA;
137
0
        }
138
18.2k
        if (s->blocksize + (uint64_t)s->nwrap >= UINT_MAX / sizeof(int32_t)) {
139
0
            av_log(s->avctx, AV_LOG_ERROR,
140
0
                   "s->blocksize + s->nwrap too large\n");
141
0
            return AVERROR_INVALIDDATA;
142
0
        }
143
144
18.2k
        if ((err = av_reallocp_array(&s->offset[chan],
145
18.2k
                               sizeof(int32_t),
146
18.2k
                               FFMAX(1, s->nmean))) < 0)
147
0
            return err;
148
149
18.2k
        if ((err = av_reallocp_array(&s->decoded_base[chan], (s->blocksize + s->nwrap),
150
18.2k
                               sizeof(s->decoded_base[0][0]))) < 0)
151
0
            return err;
152
309k
        for (i = 0; i < s->nwrap; i++)
153
290k
            s->decoded_base[chan][i] = 0;
154
18.2k
        s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
155
18.2k
    }
156
157
11.1k
    if ((err = av_reallocp_array(&s->coeffs, s->nwrap, sizeof(*s->coeffs))) < 0)
158
0
        return err;
159
160
11.1k
    return 0;
161
11.1k
}
162
163
static inline unsigned int get_uint(ShortenContext *s, int k)
164
79.4k
{
165
79.4k
    if (s->version != 0) {
166
56.2k
        k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
167
56.2k
        if (k > 31U)
168
1.97k
            return AVERROR_INVALIDDATA;
169
56.2k
    }
170
77.4k
    return get_ur_golomb_shorten(&s->gb, k);
171
79.4k
}
172
173
static void fix_bitshift(ShortenContext *s, int32_t *buffer)
174
18.9k
{
175
18.9k
    int i;
176
177
18.9k
    if (s->bitshift == 32) {
178
814
        for (i = 0; i < s->blocksize; i++)
179
612
            buffer[i] = 0;
180
18.7k
    } else if (s->bitshift != 0) {
181
871k
        for (i = 0; i < s->blocksize; i++)
182
869k
            buffer[i] *= 1U << s->bitshift;
183
2.31k
    }
184
18.9k
}
185
186
static int init_offset(ShortenContext *s)
187
11.1k
{
188
11.1k
    int32_t mean = 0;
189
11.1k
    int chan, i;
190
11.1k
    int nblock = FFMAX(1, s->nmean);
191
    /* initialise offset */
192
11.1k
    switch (s->internal_ftype) {
193
707
    case TYPE_U8:
194
707
        s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
195
707
        mean = 0x80;
196
707
        break;
197
178
    case TYPE_S16HL:
198
894
    case TYPE_S16LH:
199
894
        s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
200
894
        break;
201
9.59k
    default:
202
9.59k
        av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
203
9.59k
        return AVERROR_PATCHWELCOME;
204
11.1k
    }
205
206
5.76k
    for (chan = 0; chan < s->channels; chan++)
207
408k
        for (i = 0; i < nblock; i++)
208
404k
            s->offset[chan][i] = mean;
209
1.60k
    return 0;
210
11.1k
}
211
212
static int decode_aiff_header(AVCodecContext *avctx, const uint8_t *header,
213
                              int header_size)
214
4.24k
{
215
4.24k
    ShortenContext *s = avctx->priv_data;
216
4.24k
    int len, bps, exp;
217
4.24k
    GetByteContext gb;
218
4.24k
    uint64_t val;
219
4.24k
    uint32_t tag;
220
221
4.24k
    bytestream2_init(&gb, header, header_size);
222
223
4.24k
    if (bytestream2_get_le32(&gb) != MKTAG('F', 'O', 'R', 'M')) {
224
0
        av_log(avctx, AV_LOG_ERROR, "missing FORM tag\n");
225
0
        return AVERROR_INVALIDDATA;
226
0
    }
227
228
4.24k
    bytestream2_skip(&gb, 4); /* chunk size */
229
230
4.24k
    tag = bytestream2_get_le32(&gb);
231
4.24k
    if (tag != MKTAG('A', 'I', 'F', 'F') &&
232
1.53k
        tag != MKTAG('A', 'I', 'F', 'C')) {
233
358
        av_log(avctx, AV_LOG_ERROR, "missing AIFF tag\n");
234
358
        return AVERROR_INVALIDDATA;
235
358
    }
236
237
4.46k
    while (bytestream2_get_le32(&gb) != MKTAG('C', 'O', 'M', 'M')) {
238
1.43k
        len = bytestream2_get_be32(&gb);
239
1.43k
        if (len < 0 || bytestream2_get_bytes_left(&gb) < 18LL + len + (len&1)) {
240
857
            av_log(avctx, AV_LOG_ERROR, "no COMM chunk found\n");
241
857
            return AVERROR_INVALIDDATA;
242
857
        }
243
577
        bytestream2_skip(&gb, len + (len & 1));
244
577
    }
245
3.03k
    len = bytestream2_get_be32(&gb);
246
247
3.03k
    if (len < 18) {
248
214
        av_log(avctx, AV_LOG_ERROR, "COMM chunk was too short\n");
249
214
        return AVERROR_INVALIDDATA;
250
214
    }
251
252
2.81k
    bytestream2_skip(&gb, 6);
253
2.81k
    bps = bytestream2_get_be16(&gb);
254
2.81k
    avctx->bits_per_coded_sample = bps;
255
256
2.81k
    s->swap = tag == MKTAG('A', 'I', 'F', 'C');
257
258
2.81k
    if (bps != 16 && bps != 8) {
259
252
        av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
260
252
        return AVERROR(ENOSYS);
261
252
    }
262
263
2.56k
    exp = bytestream2_get_be16(&gb) - 16383 - 63;
264
2.56k
    val = bytestream2_get_be64(&gb);
265
2.56k
    if (exp < -63 || exp > 63) {
266
611
        av_log(avctx, AV_LOG_ERROR, "exp %d is out of range\n", exp);
267
611
        return AVERROR_INVALIDDATA;
268
611
    }
269
1.95k
    if (exp >= 0)
270
771
        avctx->sample_rate = val << exp;
271
1.18k
    else
272
1.18k
        avctx->sample_rate = (val + (1ULL<<(-exp-1))) >> -exp;
273
1.95k
    len -= 18;
274
1.95k
    if (len > 0)
275
1.75k
        av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
276
277
1.95k
    return 0;
278
2.56k
}
279
280
static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
281
                              int header_size)
282
2.28k
{
283
2.28k
    int len, bps;
284
2.28k
    uint16_t wave_format;
285
2.28k
    GetByteContext gb;
286
287
2.28k
    bytestream2_init(&gb, header, header_size);
288
289
2.28k
    if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
290
0
        av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
291
0
        return AVERROR_INVALIDDATA;
292
0
    }
293
294
2.28k
    bytestream2_skip(&gb, 4); /* chunk size */
295
296
2.28k
    if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
297
344
        av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
298
344
        return AVERROR_INVALIDDATA;
299
344
    }
300
301
2.90k
    while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
302
1.48k
        len = bytestream2_get_le32(&gb);
303
1.48k
        bytestream2_skip(&gb, len);
304
1.48k
        if (len < 0 || bytestream2_get_bytes_left(&gb) < 16) {
305
519
            av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
306
519
            return AVERROR_INVALIDDATA;
307
519
        }
308
1.48k
    }
309
1.42k
    len = bytestream2_get_le32(&gb);
310
311
1.42k
    if (len < 16) {
312
230
        av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
313
230
        return AVERROR_INVALIDDATA;
314
230
    }
315
316
1.19k
    wave_format = bytestream2_get_le16(&gb);
317
318
1.19k
    switch (wave_format) {
319
987
    case WAVE_FORMAT_PCM:
320
987
        break;
321
204
    default:
322
204
        av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
323
204
        return AVERROR(ENOSYS);
324
1.19k
    }
325
326
987
    bytestream2_skip(&gb, 2); // skip channels    (already got from shorten header)
327
987
    avctx->sample_rate = bytestream2_get_le32(&gb);
328
987
    bytestream2_skip(&gb, 4); // skip bit rate    (represents original uncompressed bit rate)
329
987
    bytestream2_skip(&gb, 2); // skip block align (not needed)
330
987
    bps = bytestream2_get_le16(&gb);
331
987
    avctx->bits_per_coded_sample = bps;
332
333
987
    if (bps != 16 && bps != 8) {
334
416
        av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
335
416
        return AVERROR(ENOSYS);
336
416
    }
337
338
571
    len -= 16;
339
571
    if (len > 0)
340
341
        av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
341
342
571
    return 0;
343
987
}
344
345
static const int fixed_coeffs[][3] = {
346
    { 0,  0,  0 },
347
    { 1,  0,  0 },
348
    { 2, -1,  0 },
349
    { 3, -3,  1 }
350
};
351
352
static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
353
                               int residual_size, int32_t coffset)
354
12.8k
{
355
12.8k
    int pred_order, sum, qshift, init_sum, i, j;
356
12.8k
    const int *coeffs;
357
358
12.8k
    if (command == FN_QLPC) {
359
        /* read/validate prediction order */
360
4.19k
        pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
361
4.19k
        if ((unsigned)pred_order > s->nwrap) {
362
491
            av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
363
491
                   pred_order);
364
491
            return AVERROR(EINVAL);
365
491
        }
366
        /* read LPC coefficients */
367
30.2k
        for (i = 0; i < pred_order; i++)
368
26.5k
            s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
369
3.70k
        coeffs = s->coeffs;
370
371
3.70k
        qshift = LPCQUANT;
372
8.67k
    } else {
373
        /* fixed LPC coeffs */
374
8.67k
        pred_order = command;
375
8.67k
        if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
376
0
            av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
377
0
                   pred_order);
378
0
            return AVERROR_INVALIDDATA;
379
0
        }
380
8.67k
        coeffs     = fixed_coeffs[pred_order];
381
8.67k
        qshift     = 0;
382
8.67k
    }
383
384
    /* subtract offset from previous samples to use in prediction */
385
12.3k
    if (command == FN_QLPC && coffset)
386
28.6k
        for (i = -pred_order; i < 0; i++)
387
25.5k
            s->decoded[channel][i] -= (unsigned)coffset;
388
389
    /* decode residual and do LPC prediction */
390
12.3k
    init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
391
6.57M
    for (i = 0; i < s->blocksize; i++) {
392
6.55M
        sum = init_sum;
393
16.1M
        for (j = 0; j < pred_order; j++)
394
9.61M
            sum += coeffs[j] * (unsigned)s->decoded[channel][i - j - 1];
395
6.55M
        s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
396
6.55M
                                 (unsigned)(sum >> qshift);
397
6.55M
    }
398
399
    /* add offset to current samples */
400
12.3k
    if (command == FN_QLPC && coffset)
401
527k
        for (i = 0; i < s->blocksize; i++)
402
524k
            s->decoded[channel][i] += (unsigned)coffset;
403
404
12.3k
    return 0;
405
12.8k
}
406
407
static int read_header(ShortenContext *s)
408
27.4k
{
409
27.4k
    int i, ret;
410
27.4k
    int maxnlpc = 0;
411
    /* shorten signature */
412
27.4k
    if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
413
6.52k
        av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
414
6.52k
        return AVERROR_INVALIDDATA;
415
6.52k
    }
416
417
20.9k
    s->lpcqoffset     = 0;
418
20.9k
    s->blocksize      = DEFAULT_BLOCK_SIZE;
419
20.9k
    s->nmean          = -1;
420
20.9k
    s->version        = get_bits(&s->gb, 8);
421
20.9k
    s->internal_ftype = get_uint(s, TYPESIZE);
422
423
20.9k
    s->channels = get_uint(s, CHANSIZE);
424
20.9k
    if (!s->channels) {
425
374
        av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
426
374
        return AVERROR_INVALIDDATA;
427
374
    }
428
20.5k
    if (s->channels > MAX_CHANNELS) {
429
413
        av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
430
413
        s->channels = 0;
431
413
        return AVERROR_INVALIDDATA;
432
413
    }
433
20.1k
    if (s->avctx->ch_layout.nb_channels != s->channels) {
434
3.48k
        av_channel_layout_uninit(&s->avctx->ch_layout);
435
3.48k
        s->avctx->ch_layout.nb_channels = s->channels;
436
3.48k
        s->avctx->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
437
3.48k
    }
438
439
    /* get blocksize if version > 0 */
440
20.1k
    if (s->version > 0) {
441
9.04k
        int skip_bytes;
442
9.04k
        unsigned blocksize;
443
444
9.04k
        blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
445
9.04k
        if (!blocksize || blocksize > MAX_BLOCKSIZE) {
446
444
            av_log(s->avctx, AV_LOG_ERROR,
447
444
                   "invalid or unsupported block size: %d\n",
448
444
                   blocksize);
449
444
            return AVERROR(EINVAL);
450
444
        }
451
8.60k
        s->blocksize = blocksize;
452
453
8.60k
        maxnlpc  = get_uint(s, LPCQSIZE);
454
8.60k
        if (maxnlpc > 1024U) {
455
271
            av_log(s->avctx, AV_LOG_ERROR, "maxnlpc is: %d\n", maxnlpc);
456
271
            return AVERROR_INVALIDDATA;
457
271
        }
458
8.33k
        s->nmean = get_uint(s, 0);
459
8.33k
        if (s->nmean > 32768U) {
460
246
            av_log(s->avctx, AV_LOG_ERROR, "nmean is: %d\n", s->nmean);
461
246
            return AVERROR_INVALIDDATA;
462
246
        }
463
464
8.08k
        skip_bytes = get_uint(s, NSKIPSIZE);
465
8.08k
        if ((unsigned)skip_bytes > FFMAX(get_bits_left(&s->gb), 0)/8) {
466
1.08k
            av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
467
1.08k
            return AVERROR_INVALIDDATA;
468
1.08k
        }
469
470
2.83M
        for (i = 0; i < skip_bytes; i++)
471
2.83M
            skip_bits(&s->gb, 8);
472
7.00k
    }
473
18.0k
    s->nwrap = FFMAX(NWRAP, maxnlpc);
474
475
18.0k
    if (s->version > 1)
476
3.93k
        s->lpcqoffset = V2LPCQOFFSET;
477
478
18.0k
    if (s->avctx->extradata_size > 0)
479
8.66k
        goto end;
480
481
9.42k
    if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
482
1.12k
        av_log(s->avctx, AV_LOG_ERROR,
483
1.12k
               "missing verbatim section at beginning of stream\n");
484
1.12k
        return AVERROR_INVALIDDATA;
485
1.12k
    }
486
487
8.29k
    s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
488
8.29k
    if (s->header_size >= OUT_BUFFER_SIZE ||
489
8.09k
        s->header_size < CANONICAL_HEADER_SIZE) {
490
457
        av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
491
457
               s->header_size);
492
457
        return AVERROR_INVALIDDATA;
493
457
    }
494
495
2.69M
    for (i = 0; i < s->header_size; i++)
496
2.68M
        s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
497
498
7.84k
    if (AV_RL32(s->header) == MKTAG('R','I','F','F')) {
499
2.28k
        if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
500
1.71k
            return ret;
501
5.55k
    } else if (AV_RL32(s->header) == MKTAG('F','O','R','M')) {
502
4.24k
        if ((ret = decode_aiff_header(s->avctx, s->header, s->header_size)) < 0)
503
2.29k
            return ret;
504
4.24k
    } else {
505
1.30k
        avpriv_report_missing_feature(s->avctx, "unsupported bit packing %"
506
1.30k
                                      PRIX32, AV_RL32(s->header));
507
1.30k
        return AVERROR_PATCHWELCOME;
508
1.30k
    }
509
510
11.1k
end:
511
512
11.1k
    if ((ret = allocate_buffers(s)) < 0)
513
0
        return ret;
514
515
11.1k
    if ((ret = init_offset(s)) < 0)
516
9.59k
        return ret;
517
518
1.60k
    s->cur_chan = 0;
519
1.60k
    s->bitshift = 0;
520
521
1.60k
    s->got_header = 1;
522
523
1.60k
    return 0;
524
11.1k
}
525
526
static int shorten_decode_frame(AVCodecContext *avctx, AVFrame *frame,
527
                                int *got_frame_ptr, AVPacket *avpkt)
528
144k
{
529
144k
    const uint8_t *buf = avpkt->data;
530
144k
    int buf_size       = avpkt->size;
531
144k
    ShortenContext *s  = avctx->priv_data;
532
144k
    int i, input_buf_size = 0;
533
144k
    int ret;
534
535
    /* allocate internal bitstream buffer */
536
144k
    if (s->max_framesize == 0) {
537
4.11k
        void *tmp_ptr;
538
4.11k
        s->max_framesize = 8192; // should hopefully be enough for the first header
539
4.11k
        tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
540
4.11k
                                  s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
541
4.11k
        if (!tmp_ptr) {
542
0
            s->max_framesize = 0;
543
0
            av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
544
0
            return AVERROR(ENOMEM);
545
0
        }
546
4.11k
        memset(tmp_ptr, 0, s->allocated_bitstream_size);
547
4.11k
        s->bitstream = tmp_ptr;
548
4.11k
    }
549
550
    /* append current packet data to bitstream buffer */
551
144k
    buf_size       = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
552
144k
    input_buf_size = buf_size;
553
554
144k
    if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
555
144k
        s->allocated_bitstream_size) {
556
400
        memmove(s->bitstream, &s->bitstream[s->bitstream_index],
557
400
                s->bitstream_size);
558
400
        s->bitstream_index = 0;
559
400
    }
560
144k
    if (buf)
561
128k
        memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
562
128k
               buf_size);
563
144k
    buf               = &s->bitstream[s->bitstream_index];
564
144k
    buf_size         += s->bitstream_size;
565
144k
    s->bitstream_size = buf_size;
566
567
    /* do not decode until buffer has at least max_framesize bytes or
568
     * the end of the file has been reached */
569
144k
    if (buf_size < s->max_framesize && avpkt->data) {
570
98.1k
        *got_frame_ptr = 0;
571
98.1k
        return input_buf_size;
572
98.1k
    }
573
    /* init and position bitstream reader */
574
45.8k
    if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
575
0
        return ret;
576
45.8k
    skip_bits(&s->gb, s->bitindex);
577
578
    /* process header or next subblock */
579
45.8k
    if (!s->got_header) {
580
581
27.4k
        if ((ret = read_header(s)) < 0)
582
25.8k
            return ret;
583
584
1.60k
        if (avpkt->size) {
585
414
            int max_framesize = s->blocksize * s->channels * 8;
586
414
            void *tmp_ptr;
587
414
            unsigned old_allocated_bitstream_size = s->allocated_bitstream_size;
588
414
            tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
589
414
                                      max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
590
414
            if (!tmp_ptr) {
591
0
                av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
592
0
                return AVERROR(ENOMEM);
593
0
            }
594
414
            s->bitstream = tmp_ptr;
595
414
            if (s->allocated_bitstream_size > old_allocated_bitstream_size)
596
176
                memset(s->bitstream + old_allocated_bitstream_size, 0, s->allocated_bitstream_size - old_allocated_bitstream_size);
597
414
            s->max_framesize = FFMAX(s->max_framesize, max_framesize);
598
414
            *got_frame_ptr = 0;
599
414
            goto finish_frame;
600
414
        }
601
1.60k
    }
602
603
    /* if quit command was read previously, don't decode anything */
604
19.6k
    if (s->got_quit_command) {
605
274
        *got_frame_ptr = 0;
606
274
        return avpkt->size;
607
274
    }
608
609
19.3k
    s->cur_chan = 0;
610
43.6k
    while (s->cur_chan < s->channels) {
611
31.7k
        unsigned cmd;
612
31.7k
        int len;
613
614
31.7k
        if (get_bits_left(&s->gb) < 3 + FNSIZE) {
615
931
            *got_frame_ptr = 0;
616
931
            break;
617
931
        }
618
619
30.7k
        cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
620
621
30.7k
        if (cmd > FN_VERBATIM) {
622
1.42k
            av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
623
1.42k
            *got_frame_ptr = 0;
624
1.42k
            break;
625
1.42k
        }
626
627
29.3k
        if (!is_audio_command[cmd]) {
628
            /* process non-audio command */
629
8.83k
            switch (cmd) {
630
2.27k
            case FN_VERBATIM:
631
2.27k
                len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
632
2.27k
                if (len < 0 || len > get_bits_left(&s->gb)) {
633
495
                    av_log(avctx, AV_LOG_ERROR, "verbatim length %d invalid\n",
634
495
                           len);
635
495
                    return AVERROR_INVALIDDATA;
636
495
                }
637
1.55M
                while (len--)
638
1.55M
                    get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
639
1.78k
                break;
640
3.01k
            case FN_BITSHIFT: {
641
3.01k
                unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
642
3.01k
                if (bitshift > 32) {
643
480
                    av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
644
480
                           bitshift);
645
480
                    return AVERROR_INVALIDDATA;
646
480
                }
647
2.53k
                s->bitshift = bitshift;
648
2.53k
                break;
649
3.01k
            }
650
3.49k
            case FN_BLOCKSIZE: {
651
3.49k
                unsigned blocksize = get_uint(s, av_log2(s->blocksize));
652
3.49k
                if (blocksize > s->blocksize) {
653
782
                    avpriv_report_missing_feature(avctx,
654
782
                                                  "Increasing block size");
655
782
                    return AVERROR_PATCHWELCOME;
656
782
                }
657
2.71k
                if (!blocksize || blocksize > MAX_BLOCKSIZE) {
658
1.69k
                    av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
659
1.69k
                                                "block size: %d\n", blocksize);
660
1.69k
                    return AVERROR(EINVAL);
661
1.69k
                }
662
1.02k
                s->blocksize = blocksize;
663
1.02k
                break;
664
2.71k
            }
665
50
            case FN_QUIT:
666
50
                s->got_quit_command = 1;
667
50
                break;
668
8.83k
            }
669
5.38k
            if (cmd == FN_QUIT)
670
50
                break;
671
20.5k
        } else {
672
            /* process audio command */
673
20.5k
            int residual_size = 0;
674
20.5k
            int channel = s->cur_chan;
675
20.5k
            int32_t coffset;
676
677
            /* get Rice code for residual decoding */
678
20.5k
            if (cmd != FN_ZERO) {
679
13.9k
                residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
680
                /* This is a hack as version 0 differed in the definition
681
                 * of get_sr_golomb_shorten(). */
682
13.9k
                if (s->version == 0)
683
1.93k
                    residual_size--;
684
13.9k
                if (residual_size > 30U) {
685
1.05k
                    av_log(avctx, AV_LOG_ERROR, "residual size unsupportd: %d\n", residual_size);
686
1.05k
                    return AVERROR_INVALIDDATA;
687
1.05k
                }
688
13.9k
            }
689
690
            /* calculate sample offset using means from previous blocks */
691
19.4k
            if (s->nmean == 0)
692
7.16k
                coffset = s->offset[channel][0];
693
12.2k
            else {
694
12.2k
                int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
695
670k
                for (i = 0; i < s->nmean; i++)
696
658k
                    sum += (unsigned)s->offset[channel][i];
697
12.2k
                coffset = sum / s->nmean;
698
12.2k
                if (s->version >= 2)
699
7.51k
                    coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
700
12.2k
            }
701
702
            /* decode samples for this channel */
703
19.4k
            if (cmd == FN_ZERO) {
704
807k
                for (i = 0; i < s->blocksize; i++)
705
800k
                    s->decoded[channel][i] = 0;
706
12.8k
            } else {
707
12.8k
                if ((ret = decode_subframe_lpc(s, cmd, channel,
708
12.8k
                                               residual_size, coffset)) < 0)
709
491
                    return ret;
710
12.8k
            }
711
712
            /* update means with info from the current block */
713
18.9k
            if (s->nmean > 0) {
714
10.7k
                int64_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
715
6.64M
                for (i = 0; i < s->blocksize; i++)
716
6.63M
                    sum += s->decoded[channel][i];
717
718
657k
                for (i = 1; i < s->nmean; i++)
719
647k
                    s->offset[channel][i - 1] = s->offset[channel][i];
720
721
10.7k
                if (s->version < 2)
722
3.28k
                    s->offset[channel][s->nmean - 1] = sum / s->blocksize;
723
7.44k
                else
724
7.44k
                    s->offset[channel][s->nmean - 1] = s->bitshift == 32 ? 0 : (sum / s->blocksize) * (1LL << s->bitshift);
725
10.7k
            }
726
727
            /* copy wrap samples for use with next block */
728
702k
            for (i = -s->nwrap; i < 0; i++)
729
683k
                s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
730
731
            /* shift samples to add in unused zero bits which were removed
732
             * during encoding */
733
18.9k
            fix_bitshift(s, s->decoded[channel]);
734
735
            /* if this is the last channel in the block, output the samples */
736
18.9k
            s->cur_chan++;
737
18.9k
            if (s->cur_chan == s->channels) {
738
11.9k
                uint8_t *samples_u8;
739
11.9k
                int16_t *samples_s16;
740
11.9k
                int chan;
741
742
                /* get output buffer */
743
11.9k
                frame->nb_samples = s->blocksize;
744
11.9k
                if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
745
0
                    return ret;
746
747
25.6k
                for (chan = 0; chan < s->channels; chan++) {
748
13.6k
                    samples_u8  = ((uint8_t **)frame->extended_data)[chan];
749
13.6k
                    samples_s16 = ((int16_t **)frame->extended_data)[chan];
750
4.09M
                    for (i = 0; i < s->blocksize; i++) {
751
4.08M
                        switch (s->internal_ftype) {
752
1.50M
                        case TYPE_U8:
753
1.50M
                            *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
754
1.50M
                            break;
755
2.38M
                        case TYPE_S16HL:
756
2.57M
                        case TYPE_S16LH:
757
2.57M
                            *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
758
2.57M
                            break;
759
4.08M
                        }
760
4.08M
                    }
761
13.6k
                    if (s->swap && s->internal_ftype != TYPE_U8)
762
2.36k
                        s->bdsp.bswap16_buf(((uint16_t **)frame->extended_data)[chan],
763
2.36k
                                            ((uint16_t **)frame->extended_data)[chan],
764
2.36k
                                            s->blocksize);
765
766
13.6k
                }
767
768
11.9k
                *got_frame_ptr = 1;
769
11.9k
            }
770
18.9k
        }
771
29.3k
    }
772
14.3k
    if (s->cur_chan < s->channels)
773
2.40k
        *got_frame_ptr = 0;
774
775
14.7k
finish_frame:
776
14.7k
    s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
777
14.7k
    i           = get_bits_count(&s->gb) / 8;
778
14.7k
    if (i > buf_size) {
779
659
        av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
780
659
        s->bitstream_size  = 0;
781
659
        s->bitstream_index = 0;
782
659
        return AVERROR_INVALIDDATA;
783
659
    }
784
14.1k
    if (s->bitstream_size) {
785
13.8k
        s->bitstream_index += i;
786
13.8k
        s->bitstream_size  -= i;
787
13.8k
        return input_buf_size;
788
13.8k
    } else
789
253
        return i;
790
14.1k
}
791
792
static av_cold int shorten_decode_close(AVCodecContext *avctx)
793
4.18k
{
794
4.18k
    ShortenContext *s = avctx->priv_data;
795
4.18k
    int i;
796
797
14.1k
    for (i = 0; i < s->channels; i++) {
798
9.95k
        s->decoded[i] = NULL;
799
9.95k
        av_freep(&s->decoded_base[i]);
800
9.95k
        av_freep(&s->offset[i]);
801
9.95k
    }
802
4.18k
    av_freep(&s->bitstream);
803
4.18k
    av_freep(&s->coeffs);
804
805
4.18k
    return 0;
806
4.18k
}
807
808
const FFCodec ff_shorten_decoder = {
809
    .p.name         = "shorten",
810
    CODEC_LONG_NAME("Shorten"),
811
    .p.type         = AVMEDIA_TYPE_AUDIO,
812
    .p.id           = AV_CODEC_ID_SHORTEN,
813
    .priv_data_size = sizeof(ShortenContext),
814
    .init           = shorten_decode_init,
815
    .close          = shorten_decode_close,
816
    FF_CODEC_DECODE_CB(shorten_decode_frame),
817
    .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
818
                      AV_CODEC_CAP_DELAY |
819
                      AV_CODEC_CAP_DR1,
820
};