FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/shorten.c
Date: 2022-07-05 19:52:29
Exec Total Coverage
Lines: 233 410 56.8%
Branches: 123 269 45.7%

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