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/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 {
33 
35 
36  int shift;
38  int offset;
39  int align;
40 
41  int eof;
42  int skip;
43  uint8_t *bitstream;
44  int64_t max_framesize;
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];
62 
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 
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,
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,
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,
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,
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 
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 
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,
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] + (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 
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:
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 
865 {
866  WavArcContext *s = avctx->priv_data;
867 
868  av_freep(&s->bitstream);
869  s->bitstream_size = 0;
870 
871  return 0;
872 }
873 
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,
882  .close = wavarc_close,
883  .p.capabilities = AV_CODEC_CAP_DR1 |
884 #if FF_API_SUBFRAMES
885  AV_CODEC_CAP_SUBFRAMES |
886 #endif
888  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_U8P,
891 };
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:695
get_srice
static int get_srice(GetBitContext *gb, int k)
Definition: wavarc.c:129
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:1050
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:251
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
WavArcContext::samples
int samples[2][640]
Definition: wavarc.c:50
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:541
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:374
AVPacket::data
uint8_t * data
Definition: packet.h:524
ac_map_symbol
static uint8_t ac_map_symbol(WavArcContext *s, uint16_t prob)
Definition: wavarc.c:420
FFCodec
Definition: codec_internal.h:127
WavArcContext::ac_pred
int ac_pred[70]
Definition: wavarc.c:59
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:313
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:414
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
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:760
do_stereo
static void do_stereo(WavArcContext *s, int ch, int correlated, int len)
Definition: wavarc.c:136
WavArcContext::filter
int filter[2][70]
Definition: wavarc.c:49
decode_0cpy
static int decode_0cpy(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:165
pkt
AVPacket * pkt
Definition: movenc.c:60
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:38
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:31
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
WavArcContext::eof
int eof
Definition: wavarc.c:41
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:52
WavArcContext::ac_value
uint16_t ac_value
Definition: wavarc.c:53
decode_5elp
static int decode_5elp(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:517
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
WavArcContext::shift
int shift
Definition: wavarc.c:36
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
WavArcContext::bitstream_size
int bitstream_size
Definition: wavarc.c:45
WavArcContext::range_low
uint16_t range_low
Definition: wavarc.c:57
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:60
ac_init_model
static void ac_init_model(WavArcContext *s)
Definition: wavarc.c:476
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:34
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1554
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:366
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:54
AVPacket::size
int size
Definition: packet.h:525
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:261
for
for(k=2;k<=8;++k)
Definition: h264pred_template.c:425
WavArcContext::bitstream
uint8_t * bitstream
Definition: wavarc.c:43
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
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:484
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2557
WavArcContext::max_framesize
int64_t max_framesize
Definition: wavarc.c:44
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:874
decode_1dif
static int decode_1dif(AVCodecContext *avctx, WavArcContext *s, GetBitContext *gb)
Definition: wavarc.c:189
WavArcContext::model
uint8_t model[256]
Definition: wavarc.c:51
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:831
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
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:39
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
wavarc_close
static av_cold int wavarc_close(AVCodecContext *avctx)
Definition: wavarc.c:864
avcodec.h
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
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:433
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:46
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:433
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:285
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:32
mem.h
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:501
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
WavArcContext::skip
int skip
Definition: wavarc.c:42
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:120
wavarc_init
static av_cold int wavarc_init(AVCodecContext *avctx)
Definition: wavarc.c:63
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
WavArcContext::nb_samples
int nb_samples
Definition: wavarc.c:37
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:58
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:401
WavArcContext::range_high
uint16_t range_high
Definition: wavarc.c:56
WavArcContext::ac_high
uint16_t ac_high
Definition: wavarc.c:55
WavArcContext::pred
int pred[2][70]
Definition: wavarc.c:48