FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/shorten.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 234 412 56.8%
Functions: 10 11 90.9%
Branches: 124 271 45.8%

Line Branch Exec 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 #define MAX_CHANNELS 8
39 #define MAX_BLOCKSIZE 65535
40
41 #define OUT_BUFFER_SIZE 16384
42
43 #define ULONGSIZE 2
44
45 #define WAVE_FORMAT_PCM 0x0001
46
47 #define DEFAULT_BLOCK_SIZE 256
48
49 #define TYPESIZE 4
50 #define CHANSIZE 0
51 #define LPCQSIZE 2
52 #define ENERGYSIZE 3
53 #define BITSHIFTSIZE 2
54
55 #define TYPE_S8 1
56 #define TYPE_U8 2
57 #define TYPE_S16HL 3
58 #define TYPE_U16HL 4
59 #define TYPE_S16LH 5
60 #define TYPE_U16LH 6
61
62 #define NWRAP 3
63 #define NSKIPSIZE 1
64
65 #define LPCQUANT 5
66 #define V2LPCQOFFSET (1 << LPCQUANT)
67
68 #define FNSIZE 2
69 #define FN_DIFF0 0
70 #define FN_DIFF1 1
71 #define FN_DIFF2 2
72 #define FN_DIFF3 3
73 #define FN_QUIT 4
74 #define FN_BLOCKSIZE 5
75 #define FN_BITSHIFT 6
76 #define FN_QLPC 7
77 #define FN_ZERO 8
78 #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 #define VERBATIM_CKSIZE_SIZE 5
84 #define VERBATIM_BYTE_SIZE 8
85 #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 2 static av_cold int shorten_decode_init(AVCodecContext *avctx)
120 {
121 2 ShortenContext *s = avctx->priv_data;
122 2 s->avctx = avctx;
123
124 2 ff_bswapdsp_init(&s->bdsp);
125
126 2 return 0;
127 }
128
129 2 static int allocate_buffers(ShortenContext *s)
130 {
131 int i, chan, err;
132
133
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (chan = 0; chan < s->channels; chan++) {
134
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (FFMAX(1, s->nmean) >= UINT_MAX / sizeof(int32_t)) {
135 av_log(s->avctx, AV_LOG_ERROR, "nmean too large\n");
136 return AVERROR_INVALIDDATA;
137 }
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (s->blocksize + (uint64_t)s->nwrap >= UINT_MAX / sizeof(int32_t)) {
139 av_log(s->avctx, AV_LOG_ERROR,
140 "s->blocksize + s->nwrap too large\n");
141 return AVERROR_INVALIDDATA;
142 }
143
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if ((err = av_reallocp_array(&s->offset[chan],
145 sizeof(int32_t),
146 4 FFMAX(1, s->nmean))) < 0)
147 return err;
148
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if ((err = av_reallocp_array(&s->decoded_base[chan], (s->blocksize + s->nwrap),
150 sizeof(s->decoded_base[0][0]))) < 0)
151 return err;
152
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (i = 0; i < s->nwrap; i++)
153 12 s->decoded_base[chan][i] = 0;
154 4 s->decoded[chan] = s->decoded_base[chan] + s->nwrap;
155 }
156
157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((err = av_reallocp_array(&s->coeffs, s->nwrap, sizeof(*s->coeffs))) < 0)
158 return err;
159
160 2 return 0;
161 }
162
163 12 static inline unsigned int get_uint(ShortenContext *s, int k)
164 {
165
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (s->version != 0) {
166 12 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (k > 31U)
168 return AVERROR_INVALIDDATA;
169 }
170 12 return get_ur_golomb_shorten(&s->gb, k);
171 }
172
173 3099 static void fix_bitshift(ShortenContext *s, int32_t *buffer)
174 {
175 int i;
176
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (s->bitshift == 32) {
178 for (i = 0; i < s->blocksize; i++)
179 buffer[i] = 0;
180
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 } else if (s->bitshift != 0) {
181 for (i = 0; i < s->blocksize; i++)
182 buffer[i] *= 1U << s->bitshift;
183 }
184 3099 }
185
186 2 static int init_offset(ShortenContext *s)
187 {
188 2 int32_t mean = 0;
189 int chan, i;
190 2 int nblock = FFMAX(1, s->nmean);
191 /* initialise offset */
192
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 switch (s->internal_ftype) {
193 case TYPE_U8:
194 s->avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
195 mean = 0x80;
196 break;
197 2 case TYPE_S16HL:
198 case TYPE_S16LH:
199 2 s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
200 2 break;
201 default:
202 av_log(s->avctx, AV_LOG_ERROR, "unknown audio type\n");
203 return AVERROR_PATCHWELCOME;
204 }
205
206
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (chan = 0; chan < s->channels; chan++)
207
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for (i = 0; i < nblock; i++)
208 16 s->offset[chan][i] = mean;
209 2 return 0;
210 }
211
212 static int decode_aiff_header(AVCodecContext *avctx, const uint8_t *header,
213 int header_size)
214 {
215 ShortenContext *s = avctx->priv_data;
216 int len, bps, exp;
217 GetByteContext gb;
218 uint64_t val;
219 uint32_t tag;
220
221 bytestream2_init(&gb, header, header_size);
222
223 if (bytestream2_get_le32(&gb) != MKTAG('F', 'O', 'R', 'M')) {
224 av_log(avctx, AV_LOG_ERROR, "missing FORM tag\n");
225 return AVERROR_INVALIDDATA;
226 }
227
228 bytestream2_skip(&gb, 4); /* chunk size */
229
230 tag = bytestream2_get_le32(&gb);
231 if (tag != MKTAG('A', 'I', 'F', 'F') &&
232 tag != MKTAG('A', 'I', 'F', 'C')) {
233 av_log(avctx, AV_LOG_ERROR, "missing AIFF tag\n");
234 return AVERROR_INVALIDDATA;
235 }
236
237 while (bytestream2_get_le32(&gb) != MKTAG('C', 'O', 'M', 'M')) {
238 len = bytestream2_get_be32(&gb);
239 if (len < 0 || bytestream2_get_bytes_left(&gb) < 18LL + len + (len&1)) {
240 av_log(avctx, AV_LOG_ERROR, "no COMM chunk found\n");
241 return AVERROR_INVALIDDATA;
242 }
243 bytestream2_skip(&gb, len + (len & 1));
244 }
245 len = bytestream2_get_be32(&gb);
246
247 if (len < 18) {
248 av_log(avctx, AV_LOG_ERROR, "COMM chunk was too short\n");
249 return AVERROR_INVALIDDATA;
250 }
251
252 bytestream2_skip(&gb, 6);
253 bps = bytestream2_get_be16(&gb);
254 avctx->bits_per_coded_sample = bps;
255
256 s->swap = tag == MKTAG('A', 'I', 'F', 'C');
257
258 if (bps != 16 && bps != 8) {
259 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
260 return AVERROR(ENOSYS);
261 }
262
263 exp = bytestream2_get_be16(&gb) - 16383 - 63;
264 val = bytestream2_get_be64(&gb);
265 if (exp < -63 || exp > 63) {
266 av_log(avctx, AV_LOG_ERROR, "exp %d is out of range\n", exp);
267 return AVERROR_INVALIDDATA;
268 }
269 if (exp >= 0)
270 avctx->sample_rate = val << exp;
271 else
272 avctx->sample_rate = (val + (1ULL<<(-exp-1))) >> -exp;
273 len -= 18;
274 if (len > 0)
275 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
276
277 return 0;
278 }
279
280 2 static int decode_wave_header(AVCodecContext *avctx, const uint8_t *header,
281 int header_size)
282 {
283 int len, bps;
284 uint16_t wave_format;
285 GetByteContext gb;
286
287 2 bytestream2_init(&gb, header, header_size);
288
289
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_le32(&gb) != MKTAG('R', 'I', 'F', 'F')) {
290 av_log(avctx, AV_LOG_ERROR, "missing RIFF tag\n");
291 return AVERROR_INVALIDDATA;
292 }
293
294 2 bytestream2_skip(&gb, 4); /* chunk size */
295
296
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_le32(&gb) != MKTAG('W', 'A', 'V', 'E')) {
297 av_log(avctx, AV_LOG_ERROR, "missing WAVE tag\n");
298 return AVERROR_INVALIDDATA;
299 }
300
301
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 while (bytestream2_get_le32(&gb) != MKTAG('f', 'm', 't', ' ')) {
302 len = bytestream2_get_le32(&gb);
303 bytestream2_skip(&gb, len);
304 if (len < 0 || bytestream2_get_bytes_left(&gb) < 16) {
305 av_log(avctx, AV_LOG_ERROR, "no fmt chunk found\n");
306 return AVERROR_INVALIDDATA;
307 }
308 }
309 2 len = bytestream2_get_le32(&gb);
310
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (len < 16) {
312 av_log(avctx, AV_LOG_ERROR, "fmt chunk was too short\n");
313 return AVERROR_INVALIDDATA;
314 }
315
316 2 wave_format = bytestream2_get_le16(&gb);
317
318
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 switch (wave_format) {
319 2 case WAVE_FORMAT_PCM:
320 2 break;
321 default:
322 av_log(avctx, AV_LOG_ERROR, "unsupported wave format\n");
323 return AVERROR(ENOSYS);
324 }
325
326 2 bytestream2_skip(&gb, 2); // skip channels (already got from shorten header)
327 2 avctx->sample_rate = bytestream2_get_le32(&gb);
328 2 bytestream2_skip(&gb, 4); // skip bit rate (represents original uncompressed bit rate)
329 2 bytestream2_skip(&gb, 2); // skip block align (not needed)
330 2 bps = bytestream2_get_le16(&gb);
331 2 avctx->bits_per_coded_sample = bps;
332
333
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (bps != 16 && bps != 8) {
334 av_log(avctx, AV_LOG_ERROR, "unsupported number of bits per sample: %d\n", bps);
335 return AVERROR(ENOSYS);
336 }
337
338 2 len -= 16;
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (len > 0)
340 av_log(avctx, AV_LOG_INFO, "%d header bytes unparsed\n", len);
341
342 2 return 0;
343 }
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 3099 static int decode_subframe_lpc(ShortenContext *s, int command, int channel,
353 int residual_size, int32_t coffset)
354 {
355 int pred_order, sum, qshift, init_sum, i, j;
356 const int *coeffs;
357
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (command == FN_QLPC) {
359 /* read/validate prediction order */
360 pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
361 if ((unsigned)pred_order > s->nwrap) {
362 av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
363 pred_order);
364 return AVERROR(EINVAL);
365 }
366 /* read LPC coefficients */
367 for (i = 0; i < pred_order; i++)
368 s->coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
369 coeffs = s->coeffs;
370
371 qshift = LPCQUANT;
372 } else {
373 /* fixed LPC coeffs */
374 3099 pred_order = command;
375
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (pred_order >= FF_ARRAY_ELEMS(fixed_coeffs)) {
376 av_log(s->avctx, AV_LOG_ERROR, "invalid pred_order %d\n",
377 pred_order);
378 return AVERROR_INVALIDDATA;
379 }
380 3099 coeffs = fixed_coeffs[pred_order];
381 3099 qshift = 0;
382 }
383
384 /* subtract offset from previous samples to use in prediction */
385
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3099 if (command == FN_QLPC && coffset)
386 for (i = -pred_order; i < 0; i++)
387 s->decoded[channel][i] -= (unsigned)coffset;
388
389 /* decode residual and do LPC prediction */
390
3/4
✓ Branch 0 taken 3041 times.
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3041 times.
3099 init_sum = pred_order ? (command == FN_QLPC ? s->lpcqoffset : 0) : coffset;
391
2/2
✓ Branch 0 taken 793344 times.
✓ Branch 1 taken 3099 times.
796443 for (i = 0; i < s->blocksize; i++) {
392 793344 sum = init_sum;
393
2/2
✓ Branch 0 taken 1266688 times.
✓ Branch 1 taken 793344 times.
2060032 for (j = 0; j < pred_order; j++)
394 1266688 sum += coeffs[j] * (unsigned)s->decoded[channel][i - j - 1];
395 793344 s->decoded[channel][i] = get_sr_golomb_shorten(&s->gb, residual_size) +
396 793344 (unsigned)(sum >> qshift);
397 }
398
399 /* add offset to current samples */
400
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3099 if (command == FN_QLPC && coffset)
401 for (i = 0; i < s->blocksize; i++)
402 s->decoded[channel][i] += (unsigned)coffset;
403
404 3099 return 0;
405 }
406
407 2 static int read_header(ShortenContext *s)
408 {
409 int i, ret;
410 2 int maxnlpc = 0;
411 /* shorten signature */
412
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits_long(&s->gb, 32) != AV_RB32("ajkg")) {
413 av_log(s->avctx, AV_LOG_ERROR, "missing shorten magic 'ajkg'\n");
414 return AVERROR_INVALIDDATA;
415 }
416
417 2 s->lpcqoffset = 0;
418 2 s->blocksize = DEFAULT_BLOCK_SIZE;
419 2 s->nmean = -1;
420 2 s->version = get_bits(&s->gb, 8);
421 2 s->internal_ftype = get_uint(s, TYPESIZE);
422
423 2 s->channels = get_uint(s, CHANSIZE);
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s->channels) {
425 av_log(s->avctx, AV_LOG_ERROR, "No channels reported\n");
426 return AVERROR_INVALIDDATA;
427 }
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->channels > MAX_CHANNELS) {
429 av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
430 s->channels = 0;
431 return AVERROR_INVALIDDATA;
432 }
433
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s->avctx->ch_layout.nb_channels != s->channels) {
434 1 av_channel_layout_uninit(&s->avctx->ch_layout);
435 1 s->avctx->ch_layout.nb_channels = s->channels;
436 1 s->avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
437 }
438
439 /* get blocksize if version > 0 */
440
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->version > 0) {
441 int skip_bytes;
442 unsigned blocksize;
443
444 2 blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
445
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
446 av_log(s->avctx, AV_LOG_ERROR,
447 "invalid or unsupported block size: %d\n",
448 blocksize);
449 return AVERROR(EINVAL);
450 }
451 2 s->blocksize = blocksize;
452
453 2 maxnlpc = get_uint(s, LPCQSIZE);
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (maxnlpc > 1024U) {
455 av_log(s->avctx, AV_LOG_ERROR, "maxnlpc is: %d\n", maxnlpc);
456 return AVERROR_INVALIDDATA;
457 }
458 2 s->nmean = get_uint(s, 0);
459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->nmean > 32768U) {
460 av_log(s->avctx, AV_LOG_ERROR, "nmean is: %d\n", s->nmean);
461 return AVERROR_INVALIDDATA;
462 }
463
464 2 skip_bytes = get_uint(s, NSKIPSIZE);
465
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 if ((unsigned)skip_bytes > FFMAX(get_bits_left(&s->gb), 0)/8) {
466 av_log(s->avctx, AV_LOG_ERROR, "invalid skip_bytes: %d\n", skip_bytes);
467 return AVERROR_INVALIDDATA;
468 }
469
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 for (i = 0; i < skip_bytes; i++)
471 skip_bits(&s->gb, 8);
472 }
473 2 s->nwrap = FFMAX(NWRAP, maxnlpc);
474
475
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->version > 1)
476 2 s->lpcqoffset = V2LPCQOFFSET;
477
478
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->avctx->extradata_size > 0)
479 goto end;
480
481
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
482 av_log(s->avctx, AV_LOG_ERROR,
483 "missing verbatim section at beginning of stream\n");
484 return AVERROR_INVALIDDATA;
485 }
486
487 2 s->header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
488
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->header_size >= OUT_BUFFER_SIZE ||
489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 s->header_size < CANONICAL_HEADER_SIZE) {
490 av_log(s->avctx, AV_LOG_ERROR, "header is wrong size: %d\n",
491 s->header_size);
492 return AVERROR_INVALIDDATA;
493 }
494
495
2/2
✓ Branch 0 taken 88 times.
✓ Branch 1 taken 2 times.
90 for (i = 0; i < s->header_size; i++)
496 88 s->header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
497
498
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (AV_RL32(s->header) == MKTAG('R','I','F','F')) {
499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = decode_wave_header(s->avctx, s->header, s->header_size)) < 0)
500 return ret;
501 } else if (AV_RL32(s->header) == MKTAG('F','O','R','M')) {
502 if ((ret = decode_aiff_header(s->avctx, s->header, s->header_size)) < 0)
503 return ret;
504 } else {
505 avpriv_report_missing_feature(s->avctx, "unsupported bit packing %"
506 PRIX32, AV_RL32(s->header));
507 return AVERROR_PATCHWELCOME;
508 }
509
510 2 end:
511
512
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = allocate_buffers(s)) < 0)
513 return ret;
514
515
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = init_offset(s)) < 0)
516 return ret;
517
518 2 s->cur_chan = 0;
519 2 s->bitshift = 0;
520
521 2 s->got_header = 1;
522
523 2 return 0;
524 }
525
526 2581 static int shorten_decode_frame(AVCodecContext *avctx, AVFrame *frame,
527 int *got_frame_ptr, AVPacket *avpkt)
528 {
529 2581 const uint8_t *buf = avpkt->data;
530 2581 int buf_size = avpkt->size;
531 2581 ShortenContext *s = avctx->priv_data;
532 2581 int i, input_buf_size = 0;
533 int ret;
534
535 /* allocate internal bitstream buffer */
536
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2579 times.
2581 if (s->max_framesize == 0) {
537 void *tmp_ptr;
538 2 s->max_framesize = 8192; // should hopefully be enough for the first header
539 2 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
540 2 s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!tmp_ptr) {
542 s->max_framesize = 0;
543 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
544 return AVERROR(ENOMEM);
545 }
546 2 memset(tmp_ptr, 0, s->allocated_bitstream_size);
547 2 s->bitstream = tmp_ptr;
548 }
549
550 /* append current packet data to bitstream buffer */
551 2581 buf_size = FFMIN(buf_size, s->max_framesize - s->bitstream_size);
552 2581 input_buf_size = buf_size;
553
554 2581 if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE >
555
2/2
✓ Branch 0 taken 1411 times.
✓ Branch 1 taken 1170 times.
2581 s->allocated_bitstream_size) {
556 1411 memmove(s->bitstream, &s->bitstream[s->bitstream_index],
557 1411 s->bitstream_size);
558 1411 s->bitstream_index = 0;
559 }
560
2/2
✓ Branch 0 taken 2566 times.
✓ Branch 1 taken 15 times.
2581 if (buf)
561 2566 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf,
562 buf_size);
563 2581 buf = &s->bitstream[s->bitstream_index];
564 2581 buf_size += s->bitstream_size;
565 2581 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
4/4
✓ Branch 0 taken 1043 times.
✓ Branch 1 taken 1538 times.
✓ Branch 2 taken 1028 times.
✓ Branch 3 taken 15 times.
2581 if (buf_size < s->max_framesize && avpkt->data) {
570 1028 *got_frame_ptr = 0;
571 1028 return input_buf_size;
572 }
573 /* init and position bitstream reader */
574
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1553 times.
1553 if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
575 return ret;
576 1553 skip_bits(&s->gb, s->bitindex);
577
578 /* process header or next subblock */
579
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1551 times.
1553 if (!s->got_header) {
580
581
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = read_header(s)) < 0)
582 return ret;
583
584
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (avpkt->size) {
585 2 int max_framesize = s->blocksize * s->channels * 8;
586 void *tmp_ptr;
587
588 2 tmp_ptr = av_fast_realloc(s->bitstream, &s->allocated_bitstream_size,
589 2 max_framesize + AV_INPUT_BUFFER_PADDING_SIZE);
590
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!tmp_ptr) {
591 av_log(avctx, AV_LOG_ERROR, "error allocating bitstream buffer\n");
592 return AVERROR(ENOMEM);
593 }
594 2 s->bitstream = tmp_ptr;
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (max_framesize > s->max_framesize)
596 memset(s->bitstream + s->max_framesize, 0, (max_framesize - s->max_framesize) +
597 AV_INPUT_BUFFER_PADDING_SIZE);
598 2 s->max_framesize = FFMAX(s->max_framesize, max_framesize);
599 2 *got_frame_ptr = 0;
600 2 goto finish_frame;
601 }
602 }
603
604 /* if quit command was read previously, don't decode anything */
605
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1551 times.
1551 if (s->got_quit_command) {
606 *got_frame_ptr = 0;
607 return avpkt->size;
608 }
609
610 1551 s->cur_chan = 0;
611
2/2
✓ Branch 0 taken 3101 times.
✓ Branch 1 taken 1549 times.
4650 while (s->cur_chan < s->channels) {
612 unsigned cmd;
613 int len;
614
615
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3099 times.
3101 if (get_bits_left(&s->gb) < 3 + FNSIZE) {
616 2 *got_frame_ptr = 0;
617 2 break;
618 }
619
620 3099 cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
621
622
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (cmd > FN_VERBATIM) {
623 av_log(avctx, AV_LOG_ERROR, "unknown shorten function %d\n", cmd);
624 *got_frame_ptr = 0;
625 break;
626 }
627
628
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (!is_audio_command[cmd]) {
629 /* process non-audio command */
630 switch (cmd) {
631 case FN_VERBATIM:
632 len = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
633 if (len < 0 || len > get_bits_left(&s->gb)) {
634 av_log(avctx, AV_LOG_ERROR, "verbatim length %d invalid\n",
635 len);
636 return AVERROR_INVALIDDATA;
637 }
638 while (len--)
639 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
640 break;
641 case FN_BITSHIFT: {
642 unsigned bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
643 if (bitshift > 32) {
644 av_log(avctx, AV_LOG_ERROR, "bitshift %d is invalid\n",
645 bitshift);
646 return AVERROR_INVALIDDATA;
647 }
648 s->bitshift = bitshift;
649 break;
650 }
651 case FN_BLOCKSIZE: {
652 unsigned blocksize = get_uint(s, av_log2(s->blocksize));
653 if (blocksize > s->blocksize) {
654 avpriv_report_missing_feature(avctx,
655 "Increasing block size");
656 return AVERROR_PATCHWELCOME;
657 }
658 if (!blocksize || blocksize > MAX_BLOCKSIZE) {
659 av_log(avctx, AV_LOG_ERROR, "invalid or unsupported "
660 "block size: %d\n", blocksize);
661 return AVERROR(EINVAL);
662 }
663 s->blocksize = blocksize;
664 break;
665 }
666 case FN_QUIT:
667 s->got_quit_command = 1;
668 break;
669 }
670 if (cmd == FN_QUIT)
671 break;
672 } else {
673 /* process audio command */
674 3099 int residual_size = 0;
675 3099 int channel = s->cur_chan;
676 int32_t coffset;
677
678 /* get Rice code for residual decoding */
679
1/2
✓ Branch 0 taken 3099 times.
✗ Branch 1 not taken.
3099 if (cmd != FN_ZERO) {
680 3099 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
681 /* This is a hack as version 0 differed in the definition
682 * of get_sr_golomb_shorten(). */
683
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (s->version == 0)
684 residual_size--;
685
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (residual_size > 30U) {
686 av_log(avctx, AV_LOG_ERROR, "residual size unsupportd: %d\n", residual_size);
687 return AVERROR_INVALIDDATA;
688 }
689 }
690
691 /* calculate sample offset using means from previous blocks */
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (s->nmean == 0)
693 coffset = s->offset[channel][0];
694 else {
695
1/2
✓ Branch 0 taken 3099 times.
✗ Branch 1 not taken.
3099 int32_t sum = (s->version < 2) ? 0 : s->nmean / 2;
696
2/2
✓ Branch 0 taken 12396 times.
✓ Branch 1 taken 3099 times.
15495 for (i = 0; i < s->nmean; i++)
697 12396 sum += (unsigned)s->offset[channel][i];
698 3099 coffset = sum / s->nmean;
699
1/2
✓ Branch 0 taken 3099 times.
✗ Branch 1 not taken.
3099 if (s->version >= 2)
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 coffset = s->bitshift == 0 ? coffset : coffset >> s->bitshift - 1 >> 1;
701 }
702
703 /* decode samples for this channel */
704
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (cmd == FN_ZERO) {
705 for (i = 0; i < s->blocksize; i++)
706 s->decoded[channel][i] = 0;
707 } else {
708
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3099 times.
3099 if ((ret = decode_subframe_lpc(s, cmd, channel,
709 residual_size, coffset)) < 0)
710 return ret;
711 }
712
713 /* update means with info from the current block */
714
1/2
✓ Branch 0 taken 3099 times.
✗ Branch 1 not taken.
3099 if (s->nmean > 0) {
715
1/2
✓ Branch 0 taken 3099 times.
✗ Branch 1 not taken.
3099 int64_t sum = (s->version < 2) ? 0 : s->blocksize / 2;
716
2/2
✓ Branch 0 taken 793344 times.
✓ Branch 1 taken 3099 times.
796443 for (i = 0; i < s->blocksize; i++)
717 793344 sum += s->decoded[channel][i];
718
719
2/2
✓ Branch 0 taken 9297 times.
✓ Branch 1 taken 3099 times.
12396 for (i = 1; i < s->nmean; i++)
720 9297 s->offset[channel][i - 1] = s->offset[channel][i];
721
722
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3099 times.
3099 if (s->version < 2)
723 s->offset[channel][s->nmean - 1] = sum / s->blocksize;
724 else
725
1/2
✓ Branch 0 taken 3099 times.
✗ Branch 1 not taken.
3099 s->offset[channel][s->nmean - 1] = s->bitshift == 32 ? 0 : (sum / s->blocksize) * (1LL << s->bitshift);
726 }
727
728 /* copy wrap samples for use with next block */
729
2/2
✓ Branch 0 taken 9297 times.
✓ Branch 1 taken 3099 times.
12396 for (i = -s->nwrap; i < 0; i++)
730 9297 s->decoded[channel][i] = s->decoded[channel][i + s->blocksize];
731
732 /* shift samples to add in unused zero bits which were removed
733 * during encoding */
734 3099 fix_bitshift(s, s->decoded[channel]);
735
736 /* if this is the last channel in the block, output the samples */
737 3099 s->cur_chan++;
738
2/2
✓ Branch 0 taken 1549 times.
✓ Branch 1 taken 1550 times.
3099 if (s->cur_chan == s->channels) {
739 uint8_t *samples_u8;
740 int16_t *samples_s16;
741 int chan;
742
743 /* get output buffer */
744 1549 frame->nb_samples = s->blocksize;
745
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1549 times.
1549 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
746 return ret;
747
748
2/2
✓ Branch 0 taken 3098 times.
✓ Branch 1 taken 1549 times.
4647 for (chan = 0; chan < s->channels; chan++) {
749 3098 samples_u8 = ((uint8_t **)frame->extended_data)[chan];
750 3098 samples_s16 = ((int16_t **)frame->extended_data)[chan];
751
2/2
✓ Branch 0 taken 793088 times.
✓ Branch 1 taken 3098 times.
796186 for (i = 0; i < s->blocksize; i++) {
752
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 793088 times.
✗ Branch 2 not taken.
793088 switch (s->internal_ftype) {
753 case TYPE_U8:
754 *samples_u8++ = av_clip_uint8(s->decoded[chan][i]);
755 break;
756 793088 case TYPE_S16HL:
757 case TYPE_S16LH:
758 793088 *samples_s16++ = av_clip_int16(s->decoded[chan][i]);
759 793088 break;
760 }
761 }
762
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3098 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3098 if (s->swap && s->internal_ftype != TYPE_U8)
763 s->bdsp.bswap16_buf(((uint16_t **)frame->extended_data)[chan],
764 ((uint16_t **)frame->extended_data)[chan],
765 s->blocksize);
766
767 }
768
769 1549 *got_frame_ptr = 1;
770 }
771 }
772 }
773
2/2
✓ Branch 0 taken 1549 times.
✓ Branch 1 taken 2 times.
1551 if (s->cur_chan < s->channels)
774 2 *got_frame_ptr = 0;
775
776 1549 finish_frame:
777 1553 s->bitindex = get_bits_count(&s->gb) - 8 * (get_bits_count(&s->gb) / 8);
778 1553 i = get_bits_count(&s->gb) / 8;
779
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1552 times.
1553 if (i > buf_size) {
780 1 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
781 1 s->bitstream_size = 0;
782 1 s->bitstream_index = 0;
783 1 return AVERROR_INVALIDDATA;
784 }
785
2/2
✓ Branch 0 taken 1551 times.
✓ Branch 1 taken 1 times.
1552 if (s->bitstream_size) {
786 1551 s->bitstream_index += i;
787 1551 s->bitstream_size -= i;
788 1551 return input_buf_size;
789 } else
790 1 return i;
791 }
792
793 2 static av_cold int shorten_decode_close(AVCodecContext *avctx)
794 {
795 2 ShortenContext *s = avctx->priv_data;
796 int i;
797
798
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (i = 0; i < s->channels; i++) {
799 4 s->decoded[i] = NULL;
800 4 av_freep(&s->decoded_base[i]);
801 4 av_freep(&s->offset[i]);
802 }
803 2 av_freep(&s->bitstream);
804 2 av_freep(&s->coeffs);
805
806 2 return 0;
807 }
808
809 const FFCodec ff_shorten_decoder = {
810 .p.name = "shorten",
811 CODEC_LONG_NAME("Shorten"),
812 .p.type = AVMEDIA_TYPE_AUDIO,
813 .p.id = AV_CODEC_ID_SHORTEN,
814 .priv_data_size = sizeof(ShortenContext),
815 .init = shorten_decode_init,
816 .close = shorten_decode_close,
817 FF_CODEC_DECODE_CB(shorten_decode_frame),
818 .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
819 AV_CODEC_CAP_DELAY |
820 #if FF_API_SUBFRAMES
821 AV_CODEC_CAP_SUBFRAMES |
822 #endif
823 AV_CODEC_CAP_DR1,
824 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
825 AV_SAMPLE_FMT_U8P,
826 AV_SAMPLE_FMT_NONE },
827 };
828