FFmpeg
wavarc.c
Go to the documentation of this file.
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 {
34 
36 
37  int shift;
39  int offset;
40  int align;
41 
42  int eof;
43  int skip;
44  uint8_t *bitstream;
45  int64_t max_framesize;
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];
63 
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 
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,
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,
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,
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,
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 
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 
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,
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 
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:
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 
866 {
867  WavArcContext *s = avctx->priv_data;
868 
869  av_freep(&s->bitstream);
870  s->bitstream_size = 0;
871 
872  return 0;
873 }
874 
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,
883  .close = wavarc_close,
884  .p.capabilities = AV_CODEC_CAP_DR1 |
885 #if FF_API_SUBFRAMES
886  AV_CODEC_CAP_SUBFRAMES |
887 #endif
889  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
892 };
skip_bits_long
static void skip_bits_long(GetBitContext *s, int n)
Skips the specified number of bits.
Definition: get_bits.h:278
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
get_srice
static int get_srice(GetBitContext *gb, int k)
Definition: wavarc.c:130
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1068
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:250
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
WavArcContext::samples
int samples[2][640]
Definition: wavarc.c:51
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:421
AV_CODEC_ID_WAVARC
@ AV_CODEC_ID_WAVARC
Definition: codec_id.h:544
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVPacket::data
uint8_t * data
Definition: packet.h:522
ac_map_symbol
static uint8_t ac_map_symbol(WavArcContext *s, uint16_t prob)
Definition: wavarc.c:421
FFCodec
Definition: codec_internal.h:127
WavArcContext::ac_pred
int ac_pred[70]
Definition: wavarc.c:60
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:323
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ac_get_prob
static uint16_t ac_get_prob(WavArcContext *s)
Definition: wavarc.c:415
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2111
fail
#define fail()
Definition: checkasm.h:179
GetBitContext
Definition: get_bits.h:108
wavarc_decode
static int wavarc_decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *pkt)
Definition: wavarc.c:761
do_stereo
static void do_stereo(WavArcContext *s, int ch, int correlated, int len)
Definition: wavarc.c:137
WavArcContext::filter
int filter[2][70]
Definition: wavarc.c:50
decode_0cpy
static int decode_0cpy(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:166
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
WavArcContext::offset
int offset
Definition: wavarc.c:39
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
WavArcContext
Definition: wavarc.c:32
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:547
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:306
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
WavArcContext::eof
int eof
Definition: wavarc.c:42
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
bits
uint8_t bits
Definition: vp3data.h:128
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
decode.h
get_bits.h
AV_RL16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
Definition: bytestream.h:94
WavArcContext::freqs
uint16_t freqs[257]
Definition: wavarc.c:53
WavArcContext::ac_value
uint16_t ac_value
Definition: wavarc.c:54
decode_5elp
static int decode_5elp(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:518
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
WavArcContext::shift
int shift
Definition: wavarc.c:37
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
WavArcContext::bitstream_size
int bitstream_size
Definition: wavarc.c:46
WavArcContext::range_low
uint16_t range_low
Definition: wavarc.c:58
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
mathops.h
WavArcContext::ac_out
int ac_out[570]
Definition: wavarc.c:61
ac_init_model
static void ac_init_model(WavArcContext *s)
Definition: wavarc.c:477
get_unary
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:46
WavArcContext::gb
GetBitContext gb
Definition: wavarc.c:35
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1618
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
WavArcContext::ac_low
uint16_t ac_low
Definition: wavarc.c:55
AVPacket::size
int size
Definition: packet.h:523
AV_SAMPLE_FMT_U8P
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition: samplefmt.h:63
codec_internal.h
shift
static int shift(int a, int b)
Definition: bonk.c:262
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
WavArcContext::bitstream
uint8_t * bitstream
Definition: wavarc.c:44
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1084
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
ac_read_model
static int ac_read_model(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:485
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2646
WavArcContext::max_framesize
int64_t max_framesize
Definition: wavarc.c:45
unary.h
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
ff_wavarc_decoder
const FFCodec ff_wavarc_decoder
Definition: wavarc.c:875
decode_1dif
static int decode_1dif(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:190
WavArcContext::model
uint8_t model[256]
Definition: wavarc.c:52
av_channel_layout_default
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
Definition: channel_layout.c:1033
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:546
internal.h
AVFrame::extended_data
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:401
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
WavArcContext::align
int align
Definition: wavarc.c:40
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
wavarc_close
static av_cold int wavarc_close(AVCodecContext *avctx)
Definition: wavarc.c:865
avcodec.h
ret
ret
Definition: filter_design.txt:187
prob
#define prob(name, subs,...)
Definition: cbs_vp9.c:325
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
ac_normalize
static int ac_normalize(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:434
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
U
#define U(x)
Definition: vpx_arith.h:37
AVCodecContext
main external API structure.
Definition: avcodec.h:445
WavArcContext::bitstream_index
int bitstream_index
Definition: wavarc.c:47
av_channel_layout_uninit
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
Definition: channel_layout.c:662
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:133
decode_2slp
static int decode_2slp(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:286
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
WavArcContext::av_class
AVClass * av_class
Definition: wavarc.c:33
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:470
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
WavArcContext::skip
int skip
Definition: wavarc.c:43
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
get_urice
static unsigned get_urice(GetBitContext *gb, int k)
Definition: wavarc.c:121
bytestream.h
wavarc_init
static av_cold int wavarc_init(AVCodecContext *avctx)
Definition: wavarc.c:64
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
WavArcContext::nb_samples
int nb_samples
Definition: wavarc.c:38
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
WavArcContext::freq_range
uint16_t freq_range
Definition: wavarc.c:59
MKTAG
#define MKTAG(a, b, c, d)
Definition: macros.h:55
av_bswap16
#define av_bswap16
Definition: bswap.h:27
int
int
Definition: ffmpeg_filter.c:424
ac_init
static int ac_init(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:402
WavArcContext::range_high
uint16_t range_high
Definition: wavarc.c:57
WavArcContext::ac_high
uint16_t ac_high
Definition: wavarc.c:56
WavArcContext::pred
int pred[2][70]
Definition: wavarc.c:49