FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/wavarc.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 0 558 0.0%
Functions: 0 16 0.0%
Branches: 0 359 0.0%

Line Branch Exec Source
1 /*
2 * WavArc audio decoder
3 * Copyright (c) 2023 Paul B Mahol
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 #include "libavutil/internal.h"
23 #include "libavutil/intreadwrite.h"
24 #include "avcodec.h"
25 #include "codec_internal.h"
26 #include "decode.h"
27 #include "get_bits.h"
28 #include "bytestream.h"
29 #include "mathops.h"
30 #include "unary.h"
31
32 typedef struct WavArcContext {
33 AVClass *av_class;
34
35 GetBitContext gb;
36
37 int shift;
38 int nb_samples;
39 int offset;
40 int align;
41
42 int eof;
43 int skip;
44 uint8_t *bitstream;
45 int64_t max_framesize;
46 int bitstream_size;
47 int bitstream_index;
48
49 int pred[2][70];
50 int filter[2][70];
51 int samples[2][640];
52 uint8_t model[256];
53 uint16_t freqs[257];
54 uint16_t ac_value;
55 uint16_t ac_low;
56 uint16_t ac_high;
57 uint16_t range_high;
58 uint16_t range_low;
59 uint16_t freq_range;
60 int ac_pred[70];
61 int ac_out[570];
62 } WavArcContext;
63
64 static av_cold int wavarc_init(AVCodecContext *avctx)
65 {
66 WavArcContext *s = avctx->priv_data;
67
68 if (avctx->extradata_size < 52)
69 return AVERROR_INVALIDDATA;
70 if (AV_RL32(avctx->extradata + 16) != MKTAG('R','I','F','F'))
71 return AVERROR_INVALIDDATA;
72 if (AV_RL32(avctx->extradata + 24) != MKTAG('W','A','V','E'))
73 return AVERROR_INVALIDDATA;
74 if (AV_RL32(avctx->extradata + 28) != MKTAG('f','m','t',' '))
75 return AVERROR_INVALIDDATA;
76 if (AV_RL16(avctx->extradata + 38) != 1 &&
77 AV_RL16(avctx->extradata + 38) != 2)
78 return AVERROR_INVALIDDATA;
79
80 av_channel_layout_uninit(&avctx->ch_layout);
81 av_channel_layout_default(&avctx->ch_layout, AV_RL16(avctx->extradata + 38));
82 avctx->sample_rate = AV_RL32(avctx->extradata + 40);
83
84 s->align = avctx->ch_layout.nb_channels;
85
86 switch (AV_RL16(avctx->extradata + 50)) {
87 case 8: avctx->sample_fmt = AV_SAMPLE_FMT_U8P; break;
88 case 16: s->align *= 2;
89 avctx->sample_fmt = AV_SAMPLE_FMT_S16P; break;
90 }
91
92 s->shift = 0;
93 switch (avctx->codec_tag) {
94 case MKTAG('0','C','P','Y'):
95 s->nb_samples = 640;
96 s->offset = 0;
97 break;
98 case MKTAG('1','D','I','F'):
99 s->nb_samples = 256;
100 s->offset = 4;
101 break;
102 case MKTAG('2','S','L','P'):
103 case MKTAG('3','N','L','P'):
104 case MKTAG('4','A','L','P'):
105 case MKTAG('5','E','L','P'):
106 s->nb_samples = 570;
107 s->offset = 70;
108 break;
109 default:
110 return AVERROR_INVALIDDATA;
111 }
112
113 s->max_framesize = s->nb_samples * 16;
114 s->bitstream = av_calloc(s->max_framesize + AV_INPUT_BUFFER_PADDING_SIZE, sizeof(*s->bitstream));
115 if (!s->bitstream)
116 return AVERROR(ENOMEM);
117
118 return 0;
119 }
120
121 static unsigned get_urice(GetBitContext *gb, int k)
122 {
123 unsigned x = get_unary(gb, 1, get_bits_left(gb));
124 unsigned y = get_bits_long(gb, k);
125 unsigned z = (x << k) | y;
126
127 return z;
128 }
129
130 static int get_srice(GetBitContext *gb, int k)
131 {
132 unsigned z = get_urice(gb, k);
133
134 return (z & 1) ? ~((int)(z >> 1)) : z >> 1;
135 }
136
137 static void do_stereo(WavArcContext *s, int ch, int correlated, int len)
138 {
139 const int nb_samples = s->nb_samples;
140 const int shift = s->shift;
141
142 if (ch == 0) {
143 if (correlated) {
144 for (int n = 0; n < len; n++) {
145 s->samples[0][n] = s->samples[0][nb_samples + n] >> shift;
146 s->samples[1][n] = s->pred[1][n] >> shift;
147 }
148 } else {
149 for (int n = 0; n < len; n++) {
150 s->samples[0][n] = s->samples[0][nb_samples + n] >> shift;
151 s->samples[1][n] = s->pred[0][n] >> shift;
152 }
153 }
154 } else {
155 if (correlated) {
156 for (int n = 0; n < nb_samples; n++)
157 s->samples[1][n + len] += (unsigned)s->samples[0][n + len];
158 }
159 for (int n = 0; n < len; n++) {
160 s->pred[0][n] = s->samples[1][nb_samples + n];
161 s->pred[1][n] = s->pred[0][n] - (unsigned)s->samples[0][nb_samples + n];
162 }
163 }
164 }
165
166 static int decode_0cpy(AVCodecContext *avctx,
167 WavArcContext *s, GetBitContext *gb)
168 {
169 const int bits = s->align * 8;
170
171 s->nb_samples = FFMIN(640, get_bits_left(gb) / bits);
172
173 switch (avctx->sample_fmt) {
174 case AV_SAMPLE_FMT_U8P:
175 for (int n = 0; n < s->nb_samples; n++) {
176 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
177 s->samples[ch][n] = get_bits(gb, 8) - 0x80;
178 }
179 break;
180 case AV_SAMPLE_FMT_S16P:
181 for (int n = 0; n < s->nb_samples; n++) {
182 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++)
183 s->samples[ch][n] = sign_extend(av_bswap16(get_bits(gb, 16)), 16);
184 }
185 break;
186 }
187 return 0;
188 }
189
190 static int decode_1dif(AVCodecContext *avctx,
191 WavArcContext *s, GetBitContext *gb)
192 {
193 int ch, finished, fill, correlated;
194
195 ch = 0;
196 finished = 0;
197 while (!finished) {
198 int *samples = s->samples[ch];
199 int k, block_type;
200
201 if (get_bits_left(gb) <= 0)
202 return AVERROR_INVALIDDATA;
203
204 block_type = get_urice(gb, 1);
205 if (block_type < 4 && block_type >= 0) {
206 k = 1 + (avctx->sample_fmt == AV_SAMPLE_FMT_S16P);
207 k = get_urice(gb, k) + 1;
208 if (k >= 32)
209 return AVERROR_INVALIDDATA;
210 }
211
212 switch (block_type) {
213 case 8:
214 s->eof = 1;
215 return AVERROR_EOF;
216 case 7:
217 s->nb_samples = get_bits(gb, 8);
218 continue;
219 case 6:
220 s->shift = get_urice(gb, 2);
221 if ((unsigned)s->shift > 31) {
222 s->shift = 0;
223 return AVERROR_INVALIDDATA;
224 }
225 continue;
226 case 5:
227 if (avctx->sample_fmt == AV_SAMPLE_FMT_U8P) {
228 fill = (int8_t)get_bits(gb, 8);
229 fill -= 0x80;
230 } else {
231 fill = (int16_t)get_bits(gb, 16);
232 fill -= 0x8000;
233 }
234
235 for (int n = 0; n < s->nb_samples; n++)
236 samples[n + 4] = fill;
237 finished = 1;
238 break;
239 case 4:
240 for (int n = 0; n < s->nb_samples; n++)
241 samples[n + 4] = 0;
242 finished = 1;
243 break;
244 case 3:
245 for (int n = 0; n < s->nb_samples; n++)
246 samples[n + 4] = get_srice(gb, k) + (samples[n + 3] - (unsigned)samples[n + 2]) * 3 +
247 samples[n + 1];
248 finished = 1;
249 break;
250 case 2:
251 for (int n = 0; n < s->nb_samples; n++)
252 samples[n + 4] = get_srice(gb, k) + (samples[n + 3] * 2U - samples[n + 2]);
253 finished = 1;
254 break;
255 case 1:
256 for (int n = 0; n < s->nb_samples; n++)
257 samples[n + 4] = get_srice(gb, k) + (unsigned)samples[n + 3];
258 finished = 1;
259 break;
260 case 0:
261 for (int n = 0; n < s->nb_samples; n++)
262 samples[n + 4] = get_srice(gb, k);
263 finished = 1;
264 break;
265 default:
266 return AVERROR_INVALIDDATA;
267 }
268
269 if (finished == 1 && avctx->ch_layout.nb_channels == 2) {
270 if (ch == 0)
271 correlated = get_bits1(gb);
272 finished = ch != 0;
273 do_stereo(s, ch, correlated, 4);
274 ch = 1;
275 }
276 }
277
278 if (avctx->ch_layout.nb_channels == 1) {
279 for (int n = 0; n < 4; n++)
280 s->samples[0][n] = s->samples[0][s->nb_samples + n];
281 }
282
283 return 0;
284 }
285
286 static int decode_2slp(AVCodecContext *avctx,
287 WavArcContext *s, GetBitContext *gb)
288 {
289 int ch, finished, fill, correlated, order;
290
291 ch = 0;
292 finished = 0;
293 while (!finished) {
294 int *samples = s->samples[ch];
295 int k, block_type;
296
297 if (get_bits_left(gb) <= 0)
298 return AVERROR_INVALIDDATA;
299
300 block_type = get_urice(gb, 1);
301 if (block_type < 5 && block_type >= 0) {
302 k = 1 + (avctx->sample_fmt == AV_SAMPLE_FMT_S16P);
303 k = get_urice(gb, k) + 1;
304 if (k >= 32)
305 return AVERROR_INVALIDDATA;
306 }
307
308 switch (block_type) {
309 case 9:
310 s->eof = 1;
311 return AVERROR_EOF;
312 case 8:
313 s->nb_samples = get_urice(gb, 8);
314 if (s->nb_samples > 570U) {
315 s->nb_samples = 570;
316 return AVERROR_INVALIDDATA;
317 }
318 continue;
319 case 7:
320 s->shift = get_urice(gb, 2);
321 if ((unsigned)s->shift > 31) {
322 s->shift = 0;
323 return AVERROR_INVALIDDATA;
324 }
325 continue;
326 case 6:
327 if (avctx->sample_fmt == AV_SAMPLE_FMT_U8P) {
328 fill = (int8_t)get_bits(gb, 8);
329 fill -= 0x80;
330 } else {
331 fill = (int16_t)get_bits(gb, 16);
332 fill -= 0x8000;
333 }
334
335 for (int n = 0; n < s->nb_samples; n++)
336 samples[n + 70] = fill;
337 finished = 1;
338 break;
339 case 5:
340 for (int n = 0; n < s->nb_samples; n++)
341 samples[n + 70] = 0;
342 finished = 1;
343 break;
344 case 4:
345 for (int n = 0; n < s->nb_samples; n++)
346 samples[n + 70] = get_srice(gb, k) + (samples[n + 69] - (unsigned)samples[n + 68]) * 3 +
347 samples[n + 67];
348 finished = 1;
349 break;
350 case 3:
351 for (int n = 0; n < s->nb_samples; n++)
352 samples[n + 70] = get_srice(gb, k) + (samples[n + 69] * 2U - samples[n + 68]);
353 finished = 1;
354 break;
355 case 2:
356 for (int n = 0; n < s->nb_samples; n++)
357 samples[n + 70] = get_srice(gb, k);
358 finished = 1;
359 break;
360 case 1:
361 for (int n = 0; n < s->nb_samples; n++)
362 samples[n + 70] = get_srice(gb, k) + (unsigned)samples[n + 69];
363 finished = 1;
364 break;
365 case 0:
366 order = get_urice(gb, 2);
367 if ((unsigned)order > FF_ARRAY_ELEMS(s->filter[ch]))
368 return AVERROR_INVALIDDATA;
369 for (int o = 0; o < order; o++)
370 s->filter[ch][o] = get_srice(gb, 2);
371 for (int n = 0; n < s->nb_samples; n++) {
372 int sum = 15;
373
374 for (int o = 0; o < order; o++)
375 sum += s->filter[ch][o] * (unsigned)samples[n + 70 - o - 1];
376
377 samples[n + 70] = get_srice(gb, k) + (sum >> 4);
378 }
379 finished = 1;
380 break;
381 default:
382 return AVERROR_INVALIDDATA;
383 }
384
385 if (finished == 1 && avctx->ch_layout.nb_channels == 2) {
386 if (ch == 0)
387 correlated = get_bits1(gb);
388 finished = ch != 0;
389 do_stereo(s, ch, correlated, 70);
390 ch = 1;
391 }
392 }
393
394 if (avctx->ch_layout.nb_channels == 1) {
395 for (int n = 0; n < 70; n++)
396 s->samples[0][n] = s->samples[0][s->nb_samples + n];
397 }
398
399 return 0;
400 }
401
402 static int ac_init(AVCodecContext *avctx,
403 WavArcContext *s, GetBitContext *gb)
404 {
405 s->ac_low = 0;
406 s->ac_high = 0xffffu;
407 s->ac_value = get_bits(gb, 16);
408
409 s->freq_range = s->freqs[256];
410 if (!s->freq_range)
411 return AVERROR_INVALIDDATA;
412 return 0;
413 }
414
415 static uint16_t ac_get_prob(WavArcContext *s)
416 {
417 return ((s->freq_range - 1) + (s->ac_value - s->ac_low) * s->freq_range) /
418 ((s->ac_high - s->ac_low) + 1U);
419 }
420
421 static uint8_t ac_map_symbol(WavArcContext *s, uint16_t prob)
422 {
423 int idx = 255;
424
425 while (prob < s->freqs[idx])
426 idx--;
427
428 s->range_high = s->freqs[idx + 1];
429 s->range_low = s->freqs[idx];
430
431 return idx;
432 }
433
434 static int ac_normalize(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
435 {
436 int range;
437
438 if (s->ac_high < s->ac_low)
439 goto fail;
440
441 range = (s->ac_high - s->ac_low) + 1;
442 s->ac_high = (range * s->range_high) / s->freq_range + s->ac_low - 1;
443 s->ac_low += (range * s->range_low) / s->freq_range;
444
445 if (s->ac_high < s->ac_low)
446 goto fail;
447
448 for (;;) {
449 if ((s->ac_high & 0x8000) != (s->ac_low & 0x8000)) {
450 if (((s->ac_low & 0x4000) == 0) || ((s->ac_high & 0x4000) != 0))
451 return 0;
452 s->ac_value ^= 0x4000;
453 s->ac_low &= 0x3fff;
454 s->ac_high |= 0x4000;
455 }
456
457 s->ac_low = s->ac_low * 2;
458 s->ac_high = s->ac_high * 2 | 1;
459 if (s->ac_high < s->ac_low)
460 goto fail;
461
462 if (get_bits_left(gb) <= 0) {
463 av_log(avctx, AV_LOG_ERROR, "overread in arithmetic coder\n");
464 goto fail;
465 }
466
467 s->ac_value = s->ac_value * 2 + get_bits1(gb);
468 if (s->ac_low > s->ac_value || s->ac_high < s->ac_value)
469 goto fail;
470 }
471
472 fail:
473 av_log(avctx, AV_LOG_ERROR, "invalid state\n");
474 return AVERROR_INVALIDDATA;
475 }
476
477 static void ac_init_model(WavArcContext *s)
478 {
479 memset(s->freqs, 0, sizeof(s->freqs));
480
481 for (int n = 0; n < 256; n++)
482 s->freqs[n+1] = s->model[n] + s->freqs[n];
483 }
484
485 static int ac_read_model(AVCodecContext *avctx,
486 WavArcContext *s,
487 GetBitContext *gb)
488 {
489 unsigned start, end;
490
491 memset(s->model, 0, sizeof(s->model));
492
493 start = get_bits(gb, 8);
494 end = get_bits(gb, 8);
495
496 for (;;) {
497 while (start <= end) {
498 if (get_bits_left(gb) < 8)
499 return AVERROR_INVALIDDATA;
500 s->model[start++] = get_bits(gb, 8);
501 }
502
503 if (get_bits_left(gb) < 8)
504 return AVERROR_INVALIDDATA;
505
506 start = get_bits(gb, 8);
507 if (!start)
508 break;
509
510 end = get_bits(gb, 8);
511 }
512
513 ac_init_model(s);
514
515 return 0;
516 }
517
518 static int decode_5elp(AVCodecContext *avctx,
519 WavArcContext *s, GetBitContext *gb)
520 {
521 int ch, finished, fill, correlated, order = 0;
522
523 ch = 0;
524 finished = 0;
525 while (!finished) {
526 int *samples = s->samples[ch];
527 int *ac_pred = s->ac_pred;
528 int *ac_out = s->ac_out;
529 int k, block_type;
530
531 if (get_bits_left(gb) <= 0)
532 return AVERROR_INVALIDDATA;
533
534 memset(s->ac_out, 0, sizeof(s->ac_out));
535
536 block_type = get_urice(gb, 1);
537 av_log(avctx, AV_LOG_DEBUG, "block_type : %d\n", block_type);
538
539 if (block_type >= 0 && block_type <= 7) {
540 k = 1 + (avctx->sample_fmt == AV_SAMPLE_FMT_S16P);
541 k = get_urice(gb, k) + 1;
542 if (k >= 32)
543 return AVERROR_INVALIDDATA;
544 }
545
546 if (block_type <= 2 || block_type == 6 || block_type == 13 ||
547 block_type == 14 || block_type == 15 || block_type == 19) {
548 order = get_urice(gb, 2);
549 if ((unsigned)order > FF_ARRAY_ELEMS(s->filter[ch]))
550 return AVERROR_INVALIDDATA;
551 for (int o = 0; o < order; o++)
552 s->filter[ch][o] = get_srice(gb, 2);
553 }
554
555 if (block_type >= 0 && block_type <= 7) {
556 for (int n = 0; n < s->nb_samples; n++)
557 samples[n + 70] = get_srice(gb, k);
558 } else {
559 for (int n = 0; n < s->nb_samples; n++)
560 samples[n + 70] = 0;
561 }
562
563 if (block_type >= 13 && block_type <= 20) {
564 const int ac_size = get_bits(gb, 12);
565 const int ac_pos = get_bits_count(gb);
566 GetBitContext ac_gb = *gb;
567 int ret;
568
569 skip_bits_long(gb, ac_size);
570 ret = ac_read_model(avctx, s, &ac_gb);
571 if (ret < 0) {
572 av_log(avctx, AV_LOG_ERROR, "bad arithmetic model\n");
573 return ret;
574 }
575
576 ret = ac_init(avctx, s, &ac_gb);
577 if (ret < 0) {
578 av_log(avctx, AV_LOG_ERROR, "cannot init arithmetic decoder\n");
579 return ret;
580 }
581
582 for (int n = 0; n < s->nb_samples; n++) {
583 uint16_t prob = ac_get_prob(s);
584 int ac = ac_map_symbol(s, prob);
585 ac_out[n] = ac - 0x80;
586 if ((ret = ac_normalize(avctx, s, &ac_gb)) < 0)
587 return ret;
588 }
589
590 if (get_bits_count(&ac_gb) != ac_pos + ac_size) {
591 av_log(avctx, AV_LOG_DEBUG, "over/under-read in arithmetic coder: %d\n",
592 ac_pos + ac_size - get_bits_count(&ac_gb));
593 }
594 }
595
596 switch (block_type) {
597 case 12:
598 s->eof = 1;
599 return AVERROR_EOF;
600 case 11:
601 s->nb_samples = get_urice(gb, 8);
602 if (s->nb_samples > 570U) {
603 s->nb_samples = 570;
604 return AVERROR_INVALIDDATA;
605 }
606 continue;
607 case 10:
608 s->shift = get_urice(gb, 2);
609 if ((unsigned)s->shift > 31) {
610 s->shift = 0;
611 return AVERROR_INVALIDDATA;
612 }
613 continue;
614 case 9:
615 if (avctx->sample_fmt == AV_SAMPLE_FMT_U8P) {
616 fill = (int8_t)get_bits(gb, 8);
617 fill -= 0x80;
618 } else {
619 fill = (int16_t)get_bits(gb, 16);
620 fill -= 0x8000;
621 }
622
623 for (int n = 0; n < s->nb_samples; n++)
624 samples[n + 70] = fill;
625 finished = 1;
626 break;
627 case 8:
628 for (int n = 0; n < s->nb_samples; n++)
629 samples[n + 70] = 0;
630 finished = 1;
631 break;
632 case 20:
633 case 7:
634 for (int n = 0; n < s->nb_samples; n++)
635 samples[n + 70] += ac_out[n] + samples[n + 69] * 3U - samples[n + 68] * 3U + samples[n + 67];
636 finished = 1;
637 break;
638 case 19:
639 case 6:
640 for (int n = 0; n < 70; n++) {
641 ac_pred[n] = samples[n];
642 samples[n] = 0;
643 }
644
645 for (int n = 0; n < s->nb_samples; n++) {
646 int sum = 15;
647
648 for (int o = 0; o < order; o++)
649 sum += s->filter[ch][o] * (unsigned)samples[n + 70 - o - 1];
650
651 samples[n + 70] += ac_out[n] + (sum >> 4);
652 }
653
654 for (int n = 0; n < 70; n++)
655 samples[n] = ac_pred[n];
656
657 for (int n = 0; n < s->nb_samples; n++)
658 samples[n + 70] += ac_out[n] + samples[n + 69] * 3U - samples[n + 68] * 3U + samples[n + 67];
659
660 finished = 1;
661 break;
662 case 18:
663 case 5:
664 for (int n = 0; n < s->nb_samples; n++)
665 samples[n + 70] += ac_out[n] + samples[n + 69] * 2U - samples[n + 68];
666 finished = 1;
667 break;
668 case 17:
669 case 4:
670 for (int n = 0; n < s->nb_samples; n++)
671 samples[n + 70] += ac_out[n];
672 finished = 1;
673 break;
674 case 16:
675 case 3:
676 for (int n = 0; n < s->nb_samples; n++)
677 samples[n + 70] += ac_out[n] + (unsigned)samples[n + 69];
678 finished = 1;
679 break;
680 case 15:
681 case 2:
682 for (int n = 0; n < 70; n++) {
683 ac_pred[n] = samples[n];
684 samples[n] = 0;
685 }
686
687 for (int n = 0; n < s->nb_samples; n++) {
688 int sum = 15;
689
690 for (int o = 0; o < order; o++)
691 sum += s->filter[ch][o] * (unsigned)samples[n + 70 - o - 1];
692
693 samples[n + 70] += ac_out[n] + (sum >> 4);
694 }
695
696 for (int n = 0; n < 70; n++)
697 samples[n] = ac_pred[n];
698
699 for (int n = 0; n < s->nb_samples; n++)
700 samples[n + 70] += samples[n + 69] * 2U - samples[n + 68];
701
702 finished = 1;
703 break;
704 case 14:
705 case 1:
706 for (int n = 0; n < 70; n++) {
707 ac_pred[n] = samples[n];
708 samples[n] = 0;
709 }
710
711 for (int n = 0; n < s->nb_samples; n++) {
712 int sum = 15;
713
714 for (int o = 0; o < order; o++)
715 sum += s->filter[ch][o] * (unsigned)samples[n + 70 - o - 1];
716
717 samples[n + 70] += (unsigned)ac_out[n] + (sum >> 4);
718 }
719
720 for (int n = 0; n < 70; n++)
721 samples[n] = ac_pred[n];
722
723 for (int n = 0; n < s->nb_samples; n++)
724 samples[n + 70] += (unsigned)samples[n + 69];
725
726 finished = 1;
727 break;
728 case 13:
729 case 0:
730 for (int n = 0; n < s->nb_samples; n++) {
731 int sum = 15;
732
733 for (int o = 0; o < order; o++)
734 sum += s->filter[ch][o] * (unsigned)samples[n + 70 - o - 1];
735
736 samples[n + 70] += (unsigned)ac_out[n] + (sum >> 4);
737 }
738 finished = 1;
739 break;
740 default:
741 return AVERROR_INVALIDDATA;
742 }
743
744 if (finished == 1 && avctx->ch_layout.nb_channels == 2) {
745 if (ch == 0)
746 correlated = get_bits1(gb);
747 finished = ch != 0;
748 do_stereo(s, ch, correlated, 70);
749 ch = 1;
750 }
751 }
752
753 if (avctx->ch_layout.nb_channels == 1) {
754 for (int n = 0; n < 70; n++)
755 s->samples[0][n] = s->samples[0][s->nb_samples + n];
756 }
757
758 return 0;
759 }
760
761 static int wavarc_decode(AVCodecContext *avctx, AVFrame *frame,
762 int *got_frame_ptr, AVPacket *pkt)
763 {
764 WavArcContext *s = avctx->priv_data;
765 GetBitContext *gb = &s->gb;
766 int buf_size, input_buf_size;
767 const uint8_t *buf;
768 int ret, n;
769
770 if ((!pkt->size && !s->bitstream_size) || s->nb_samples == 0 || s->eof) {
771 *got_frame_ptr = 0;
772 return pkt->size;
773 }
774
775 buf_size = FFMIN(pkt->size, s->max_framesize - s->bitstream_size);
776 input_buf_size = buf_size;
777 if (s->bitstream_index + s->bitstream_size + buf_size + AV_INPUT_BUFFER_PADDING_SIZE > s->max_framesize) {
778 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
779 s->bitstream_index = 0;
780 }
781 if (pkt->data)
782 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], pkt->data, buf_size);
783 buf = &s->bitstream[s->bitstream_index];
784 buf_size += s->bitstream_size;
785 s->bitstream_size = buf_size;
786 if (buf_size < s->max_framesize && pkt->data) {
787 *got_frame_ptr = 0;
788 return input_buf_size;
789 }
790
791 if ((ret = init_get_bits8(gb, buf, buf_size)) < 0)
792 goto fail;
793 skip_bits(gb, s->skip);
794
795 switch (avctx->codec_tag) {
796 case MKTAG('0','C','P','Y'):
797 ret = decode_0cpy(avctx, s, gb);
798 break;
799 case MKTAG('1','D','I','F'):
800 ret = decode_1dif(avctx, s, gb);
801 break;
802 case MKTAG('2','S','L','P'):
803 case MKTAG('3','N','L','P'):
804 case MKTAG('4','A','L','P'):
805 ret = decode_2slp(avctx, s, gb);
806 break;
807 case MKTAG('5','E','L','P'):
808 ret = decode_5elp(avctx, s, gb);
809 break;
810 default:
811 ret = AVERROR_INVALIDDATA;
812 }
813
814 if (ret < 0)
815 goto fail;
816
817 s->skip = get_bits_count(gb) - 8 * (get_bits_count(gb) / 8);
818 n = get_bits_count(gb) / 8;
819
820 if (n > buf_size) {
821 fail:
822 s->bitstream_size = 0;
823 s->bitstream_index = 0;
824 if (ret == AVERROR_EOF)
825 return 0;
826 return AVERROR_INVALIDDATA;
827 }
828
829 frame->nb_samples = s->nb_samples;
830 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
831 goto fail;
832
833 switch (avctx->sample_fmt) {
834 case AV_SAMPLE_FMT_U8P:
835 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
836 uint8_t *dst = (uint8_t *)frame->extended_data[ch];
837 const int *src = s->samples[ch] + s->offset;
838
839 for (int n = 0; n < frame->nb_samples; n++)
840 dst[n] = src[n] * (1U << s->shift) + 0x80U;
841 }
842 break;
843 case AV_SAMPLE_FMT_S16P:
844 for (int ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
845 int16_t *dst = (int16_t *)frame->extended_data[ch];
846 const int *src = s->samples[ch] + s->offset;
847
848 for (int n = 0; n < frame->nb_samples; n++)
849 dst[n] = src[n] * (1U << s->shift);
850 }
851 break;
852 }
853
854 *got_frame_ptr = 1;
855
856 if (s->bitstream_size) {
857 s->bitstream_index += n;
858 s->bitstream_size -= n;
859 return input_buf_size;
860 }
861
862 return n;
863 }
864
865 static av_cold int wavarc_close(AVCodecContext *avctx)
866 {
867 WavArcContext *s = avctx->priv_data;
868
869 av_freep(&s->bitstream);
870 s->bitstream_size = 0;
871
872 return 0;
873 }
874
875 const FFCodec ff_wavarc_decoder = {
876 .p.name = "wavarc",
877 CODEC_LONG_NAME("Waveform Archiver"),
878 .p.type = AVMEDIA_TYPE_AUDIO,
879 .p.id = AV_CODEC_ID_WAVARC,
880 .priv_data_size = sizeof(WavArcContext),
881 .init = wavarc_init,
882 FF_CODEC_DECODE_CB(wavarc_decode),
883 .close = wavarc_close,
884 .p.capabilities = AV_CODEC_CAP_DR1 |
885 #if FF_API_SUBFRAMES
886 AV_CODEC_CAP_SUBFRAMES |
887 #endif
888 AV_CODEC_CAP_DELAY,
889 .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
890 AV_SAMPLE_FMT_S16P,
891 AV_SAMPLE_FMT_NONE },
892 };
893