FFmpeg coverage


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