FFmpeg
rka.c
Go to the documentation of this file.
1 /*
2  * RKA 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 
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/mem.h"
25 
26 #include "avcodec.h"
27 #include "codec_internal.h"
28 #include "bytestream.h"
29 #include "decode.h"
30 
31 typedef struct ACoder {
33  uint32_t low, high;
34  uint32_t value;
35 } ACoder;
36 
37 typedef struct FiltCoeffs {
39  unsigned size;
40 } FiltCoeffs;
41 
42 typedef struct Model64 {
43  uint32_t zero[2];
44  uint32_t sign[2];
45  unsigned size;
46  int bits;
47 
48  uint16_t val4[65];
49  uint16_t val1[65];
50 } Model64;
51 
52 typedef struct AdaptiveModel {
53  int last;
54  int total;
55  int buf_size;
56  int16_t sum;
57  uint16_t aprob0;
58  uint16_t aprob1;
59  uint16_t *prob[2];
61 
62 typedef struct ChContext {
63  int qfactor;
64  int vrq;
66  unsigned srate_pad;
67  unsigned pos_idx;
68 
71 
72  uint32_t *bprob[2];
73 
78 
79  Model64 mdl64[4][11];
80 
81  int32_t buf0[131072+2560];
82  int32_t buf1[131072+2560];
83 } ChContext;
84 
85 typedef struct RKAContext {
86  AVClass *class;
87 
90 
91  int bps;
92  int align;
93  int channels;
97  uint32_t total_nb_samples;
98  uint32_t samples_left;
99 
100  uint32_t bprob[2][257];
101 
104 } RKAContext;
105 
106 static int adaptive_model_init(AdaptiveModel *am, int buf_size)
107 {
108  am->buf_size = buf_size;
109  am->sum = 2000;
110  am->aprob0 = 0;
111  am->aprob1 = 0;
112  am->total = 0;
113 
114  if (!am->prob[0])
115  am->prob[0] = av_malloc_array(buf_size + 5, sizeof(*am->prob[0]));
116  if (!am->prob[1])
117  am->prob[1] = av_malloc_array(buf_size + 5, sizeof(*am->prob[1]));
118 
119  if (!am->prob[0] || !am->prob[1])
120  return AVERROR(ENOMEM);
121  memset(am->prob[0], 0, (buf_size + 5) * sizeof(*am->prob[0]));
122  memset(am->prob[1], 0, (buf_size + 5) * sizeof(*am->prob[1]));
123  return 0;
124 }
125 
127 {
128  av_freep(&am->prob[0]);
129  av_freep(&am->prob[1]);
130 }
131 
133 {
134  RKAContext *s = avctx->priv_data;
135  int qfactor;
136 
137  if (avctx->extradata_size < 16)
138  return AVERROR_INVALIDDATA;
139 
140  s->bps = avctx->bits_per_raw_sample = avctx->extradata[13];
141 
142  switch (s->bps) {
143  case 8:
144  avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
145  break;
146  case 16:
148  break;
149  default:
150  return AVERROR_INVALIDDATA;
151  }
152 
154  s->channels = avctx->ch_layout.nb_channels = avctx->extradata[12];
155  if (s->channels < 1 || s->channels > 2)
156  return AVERROR_INVALIDDATA;
157 
158  s->align = (s->channels * (avctx->bits_per_raw_sample >> 3));
159  s->samples_left = s->total_nb_samples = (AV_RL32(avctx->extradata + 4)) / s->align;
160  s->frame_samples = 131072 / s->align;
161  s->last_nb_samples = s->total_nb_samples % s->frame_samples;
162  s->correlated = avctx->extradata[15] & 1;
163 
164  qfactor = avctx->extradata[14] & 0xf;
165  if ((avctx->extradata[15] & 4) != 0)
166  qfactor = -qfactor;
167 
168  s->ch[0].qfactor = s->ch[1].qfactor = qfactor < 0 ? 2 : qfactor;
169  s->ch[0].vrq = qfactor < 0 ? -qfactor : 0;
170  s->ch[1].vrq = qfactor < 0 ? -qfactor : 0;
171  if (qfactor < 0) {
172  s->ch[0].vrq = av_clip(s->ch[0].vrq, 1, 8);
173  s->ch[1].vrq = av_clip(s->ch[1].vrq, 1, 8);
174  }
175  av_log(avctx, AV_LOG_DEBUG, "qfactor: %d\n", qfactor);
176 
177  return 0;
178 }
179 
180 static void model64_init(Model64 *m, unsigned bits)
181 {
182  unsigned x;
183 
184  m->bits = bits;
185  m->size = 64;
186  m->zero[0] = 1;
187 
188  x = (1 << (bits >> 1)) + 3;
189  x = FFMIN(x, 20);
190 
191  m->zero[1] = x;
192  m->sign[0] = 1;
193  m->sign[1] = 1;
194 
195  for (int i = 0; i < FF_ARRAY_ELEMS(m->val4); i++) {
196  m->val4[i] = 4;
197  m->val1[i] = 1;
198  }
199 }
200 
202  int sample_rate, int bps)
203 {
204  int ret;
205 
206  memset(c->buf0, 0, sizeof(c->buf0));
207  memset(c->buf1, 0, sizeof(c->buf1));
208 
209  c->filt_size = &s->filt_size;
210  c->filt_bits = &s->filt_bits;
211 
212  c->bprob[0] = s->bprob[0];
213  c->bprob[1] = s->bprob[1];
214 
215  c->srate_pad = ((int64_t)sample_rate << 13) / 44100 & 0xFFFFFFFCU;
216  c->pos_idx = 1;
217 
218  for (int i = 0; i < FF_ARRAY_ELEMS(s->bprob[0]); i++)
219  c->bprob[0][i] = c->bprob[1][i] = 1;
220 
221  for (int i = 0; i < 11; i++) {
222  ret = adaptive_model_init(&c->coeff_bits[i], 32);
223  if (ret < 0)
224  return ret;
225 
226  model64_init(&c->mdl64[0][i], i);
227  model64_init(&c->mdl64[1][i], i);
228  model64_init(&c->mdl64[2][i], i+1);
229  model64_init(&c->mdl64[3][i], i+1);
230  }
231 
232  ret = adaptive_model_init(c->filt_size, 256);
233  if (ret < 0)
234  return ret;
235  ret = adaptive_model_init(c->filt_bits, 16);
236  if (ret < 0)
237  return ret;
238  ret = adaptive_model_init(&c->position, 16);
239  if (ret < 0)
240  return ret;
241  ret = adaptive_model_init(&c->nb_segments, 8);
242  if (ret < 0)
243  return ret;
244  return adaptive_model_init(&c->fshift, 32);
245 }
246 
247 static void init_acoder(ACoder *ac)
248 {
249  ac->low = 0x0;
250  ac->high = 0xffffffff;
251  ac->value = bytestream2_get_be32(&ac->gb);
252 }
253 
254 static int ac_decode_bool(ACoder *ac, int freq1, int freq2)
255 {
256  unsigned help, add, high, value;
257  int low;
258 
259  low = ac->low;
260  help = ac->high / (unsigned)(freq2 + freq1);
261  value = ac->value;
262  add = freq1 * help;
263  ac->high = help;
264 
265  if (value - low >= add) {
266  ac->low = low = add + low;
267  ac->high = high = freq2 * help;
268  while (1) {
269  if ((low ^ (high + low)) > 0xFFFFFF) {
270  if (high > 0xFFFF)
271  return 1;
272  ac->high = (uint16_t)-(int16_t)low;
273  }
274 
275  if (bytestream2_get_bytes_left(&ac->gb) <= 0)
276  break;
277  ac->value = bytestream2_get_byteu(&ac->gb) | (ac->value << 8);
278  ac->high = high = ac->high << 8;
279  low = ac->low = ac->low << 8;
280  }
281  return -1;
282  }
283 
284  ac->high = add;
285  while (1) {
286  if ((low ^ (add + low)) > 0xFFFFFF) {
287  if (add > 0xFFFF)
288  return 0;
289  ac->high = (uint16_t)-(int16_t)low;
290  }
291 
292  if (bytestream2_get_bytes_left(&ac->gb) <= 0)
293  break;
294  ac->value = bytestream2_get_byteu(&ac->gb) | (ac->value << 8);
295  ac->high = add = ac->high << 8;
296  low = ac->low = ac->low << 8;
297  }
298  return -1;
299 }
300 
301 static int decode_bool(ACoder *ac, ChContext *c, int idx)
302 {
303  uint32_t x;
304  int b;
305 
306  x = c->bprob[0][idx];
307  if (x + c->bprob[1][idx] > 4096) {
308  c->bprob[0][idx] = (x >> 1) + 1;
309  c->bprob[1][idx] = (c->bprob[1][idx] >> 1) + 1;
310  }
311 
312  b = ac_decode_bool(ac, c->bprob[0][idx], c->bprob[1][idx]);
313  if (b < 0)
314  return b;
315 
316  c->bprob[b][idx]++;
317 
318  return b;
319 }
320 
321 static int ac_get_freq(ACoder *ac, unsigned freq, int *result)
322 {
323  uint32_t new_high;
324 
325  if (freq == 0)
326  return -1;
327 
328  new_high = ac->high / freq;
329  ac->high = new_high;
330 
331  if (new_high == 0)
332  return -1;
333 
334  *result = (ac->value - ac->low) / new_high;
335 
336  return 0;
337 }
338 
339 static int ac_update(ACoder *ac, int freq, int mul)
340 {
341  uint32_t low, high;
342 
343  low = ac->low = ac->high * freq + ac->low;
344  high = ac->high = ac->high * mul;
345 
346  while (1) {
347  if (((high + low) ^ low) > 0xffffff) {
348  if (high > 0xffff)
349  return 0;
350  ac->high = (uint16_t)-(int16_t)low;
351  }
352 
353  if (bytestream2_get_bytes_left(&ac->gb) <= 0)
354  break;
355 
356  ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb);
357  low = ac->low = ac->low << 8;
358  high = ac->high = ac->high << 8;
359  }
360 
361  return -1;
362 }
363 
364 static void amdl_update_prob(AdaptiveModel *am, int val, int diff)
365 {
366  am->aprob0 += diff;
367  if (val <= 0) {
368  am->prob[0][0] += diff;
369  } else {
370  do {
371  am->prob[0][val] += diff;
372  val += (val & -val);
373  } while (val < am->buf_size);
374  }
375 }
376 
378 {
379  int idx2, idx = am->buf_size - 1;
380 
381  if (idx >= 0) {
382  do {
383  uint16_t *prob = am->prob[0];
384  int diff, prob_idx = prob[idx];
385 
386  idx2 = idx - 1;
387  if (idx > 0) {
388  int idx3 = idx - 1;
389 
390  if ((idx2 & idx) != idx2) {
391  do {
392  prob_idx -= prob[idx3];
393  idx3 &= idx3 - 1;
394  } while ((idx2 & idx) != idx3);
395  }
396  }
397 
398  diff = ((prob_idx > 0) - prob_idx) >> 1;
399  amdl_update_prob(am, idx, diff);
400  idx--;
401  } while (idx2 >= 0);
402  }
403 
404  if (am->sum < 8000)
405  am->sum += 200;
406 
407  am->aprob1 = (am->aprob1 + 1) >> 1;
408 }
409 
410 static int amdl_decode_int(AdaptiveModel *am, ACoder *ac, unsigned *dst, unsigned size)
411 {
412  unsigned freq, size2, val, mul;
413  int j;
414 
415  size = FFMIN(size, am->buf_size - 1);
416 
417  if (am->aprob0 >= am->sum)
418  update_ch_subobj(am);
419 
420  if (am->aprob1 && (am->total == am->buf_size ||
421  ac_decode_bool(ac, am->aprob0, am->aprob1) == 0)) {
422  if (am->total <= 1) {
423  dst[0] = am->last;
424  amdl_update_prob(am, dst[0], 1);
425  return 0;
426  }
427  if (size == am->buf_size - 1) {
428  freq = am->aprob0;
429  } else {
430  freq = am->prob[0][0];
431  for (int j = size; j > 0; j &= (j - 1) )
432  freq += am->prob[0][j];
433  }
434  ac_get_freq(ac, freq, &freq);
435  size2 = am->buf_size >> 1;
436  val = am->prob[0][0];
437  if (freq >= val) {
438  int sum = 0;
439  for (j = freq - val; size2; size2 >>= 1) {
440  unsigned v = am->prob[0][size2 + sum];
441  if (j >= v) {
442  sum += size2;
443  j -= v;
444  }
445  }
446  freq -= j;
447  val = sum + 1;
448  } else {
449  freq = 0;
450  val = 0;
451  }
452  dst[0] = val;
453  mul = am->prob[0][val];
454  if (val > 0) {
455  for (int k = val - 1; (val & (val - 1)) != k; k &= k - 1)
456  mul -= am->prob[0][k];
457  }
458  ac_update(ac, freq, mul);
459  amdl_update_prob(am, dst[0], 1);
460  return 0;
461  }
462  am->aprob1++;
463  if (size == am->buf_size - 1) {
464  ac_get_freq(ac, am->buf_size - am->total, &val);
465  } else {
466  freq = 1;
467  for (dst[0] = 0; dst[0] < size; dst[0]++) {
468  if (!am->prob[1][dst[0]])
469  freq++;
470  }
471  ac_get_freq(ac, freq, &val);
472  }
473  freq = 0;
474  dst[0] = 0;
475  if (val > 0 && am->buf_size > 0) {
476  for (dst[0] = 0; dst[0] < size & freq < val; dst[0]++) {
477  if (!am->prob[1][dst[0]])
478  freq++;
479  }
480  }
481  if (am->prob[1][dst[0]]) {
482  do {
483  val = dst[0]++;
484  } while (val + 1 < am->buf_size && am->prob[1][val + 1]);
485  }
486  ac_update(ac, freq, 1);
487  am->prob[1][dst[0]]++;
488  am->total++;
489  amdl_update_prob(am, dst[0], 1);
490  am->last = dst[0];
491 
492  return 0;
493 }
494 
496 {
497  unsigned val, bits;
498  int idx = 0;
499 
500  if (amdl_decode_int(ctx->filt_size, ac, &dst->size, 256) < 0)
501  return -1;
502 
503  if (dst->size == 0)
504  return 0;
505 
506  if (amdl_decode_int(ctx->filt_bits, ac, &bits, 10) < 0)
507  return -1;
508 
509  do {
510  if (((idx == 8) || (idx == 20)) && (0 < bits))
511  bits--;
512 
513  if (bits > 10)
514  return -1;
515 
516  if (amdl_decode_int(&ctx->coeff_bits[bits], ac, &val, 31) < 0)
517  return -1;
518 
519  if (val == 31) {
520  ac_get_freq(ac, 65536, &val);
521  ac_update(ac, val, 1);
522  }
523 
524  if (val == 0) {
525  dst->coeffs[idx++] = 0;
526  } else {
527  unsigned freq = 0;
528  int sign;
529 
530  if (bits > 0) {
531  ac_get_freq(ac, 1 << bits, &freq);
532  ac_update(ac, freq, 1);
533  }
534  dst->coeffs[idx] = freq + 1 + ((val - 1U) << bits);
535  sign = decode_bool(ac, ctx, idx);
536  if (sign < 0)
537  return -1;
538  if (sign == 1)
539  dst->coeffs[idx] = -dst->coeffs[idx];
540  idx++;
541  }
542  } while (idx < dst->size);
543 
544  return 0;
545 }
546 
547 static int ac_dec_bit(ACoder *ac)
548 {
549  uint32_t high, low;
550 
551  low = ac->low;
552  ac->high = high = ac->high >> 1;
553  if (ac->value - low < high) {
554  do {
555  if (((high + low) ^ low) > 0xffffff) {
556  if (high > 0xffff)
557  return 0;
558  ac->high = (uint16_t)-(int16_t)low;
559  }
560 
561  if (bytestream2_get_bytes_left(&ac->gb) <= 0)
562  break;
563 
564  ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb);
565  ac->high = high = ac->high << 8;
566  ac->low = low = ac->low << 8;
567  } while (1);
568 
569  return -1;
570  }
571  ac->low = low = low + high;
572  do {
573  if (((high + low) ^ low) > 0xffffff) {
574  if (high > 0xffff)
575  return 1;
576  ac->high = (uint16_t)-(int16_t)low;
577  }
578 
579  if (bytestream2_get_bytes_left(&ac->gb) <= 0)
580  break;
581 
582  ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb);
583  ac->high = high = ac->high << 8;
584  ac->low = low = ac->low << 8;
585  } while (1);
586 
587  return -1;
588 }
589 
590 static int mdl64_decode(ACoder *ac, Model64 *ctx, int *dst)
591 {
592  int sign, idx, bits;
593  unsigned val = 0;
594 
595  if (ctx->zero[0] + ctx->zero[1] > 4000U) {
596  ctx->zero[0] = (ctx->zero[0] >> 1) + 1;
597  ctx->zero[1] = (ctx->zero[1] >> 1) + 1;
598  }
599  if (ctx->sign[0] + ctx->sign[1] > 4000U) {
600  ctx->sign[0] = (ctx->sign[0] >> 1) + 1;
601  ctx->sign[1] = (ctx->sign[1] >> 1) + 1;
602  }
603  sign = ac_decode_bool(ac, ctx->zero[0], ctx->zero[1]);
604  if (sign == 0) {
605  ctx->zero[0] += 2;
606  dst[0] = 0;
607  return 0;
608  } else if (sign < 0) {
609  return -1;
610  }
611 
612  ctx->zero[1] += 2;
613  sign = ac_decode_bool(ac, ctx->sign[0], ctx->sign[1]);
614  if (sign < 0)
615  return -1;
616  ctx->sign[sign]++;
617  bits = ctx->bits;
618  if (bits > 0) {
619  if (bits < 13) {
620  ac_get_freq(ac, 1 << bits, &val);
621  ac_update(ac, val, 1);
622  } else {
623  int hbits = bits / 2;
624  ac_get_freq(ac, 1 << hbits, &val);
625  ac_update(ac, val, 1);
626  ac_get_freq(ac, 1 << (ctx->bits - (hbits)), &bits);
627  ac_update(ac, val, 1);
628  val += (bits << hbits);
629  }
630  }
631  bits = ctx->size;
632  idx = 0;
633  if (bits >= 0) {
634  do {
635  uint16_t *val4 = ctx->val4;
636  int b;
637 
638  if (val4[idx] + ctx->val1[idx] > 2000U) {
639  val4[idx] = (val4[idx] >> 1) + 1;
640  ctx->val1[idx] = (ctx->val1[idx] >> 1) + 1;
641  }
642  b = ac_decode_bool(ac, ctx->val4[idx], ctx->val1[idx]);
643  if (b == 1) {
644  ctx->val1[idx] += 4;
645  break;
646  } else if (b < 0) {
647  return -1;
648  }
649  ctx->val4[idx] += 4;
650  idx++;
651  } while (idx <= ctx->size);
652  bits = ctx->size;
653  if (idx <= bits) {
654  dst[0] = val + 1 + (idx << ctx->bits);
655  if (sign)
656  dst[0] = -dst[0];
657  return 0;
658  }
659  }
660  bits++;
661  while (ac_dec_bit(ac) == 0)
662  bits += 64;
663  ac_get_freq(ac, 64, &idx);
664  ac_update(ac, idx, 1);
665  idx += bits;
666  dst[0] = val + 1 + (idx << ctx->bits);
667  if (sign)
668  dst[0] = -dst[0];
669 
670  return 0;
671 }
672 
673 static const uint8_t vrq_qfactors[8] = { 3, 3, 2, 2, 1, 1, 1, 1 };
674 
675 static int decode_filter(RKAContext *s, ChContext *ctx, ACoder *ac, int off, unsigned size)
676 {
678  Model64 *mdl64;
679  int split, val, last_val = 0, ret;
680  unsigned rsize, idx = 3, bits = 0, m = 0;
681 
682  if (ctx->qfactor == 0) {
683  if (amdl_decode_int(&ctx->fshift, ac, &bits, 15) < 0)
684  return -1;
685  bits &= 31U;
686  }
687 
688  ret = decode_filt_coeffs(s, ctx, ac, &filt);
689  if (ret < 0)
690  return ret;
691 
692  if (size < 512)
693  split = size / 2;
694  else
695  split = size >> 4;
696 
697  if (size <= 1)
698  return 0;
699 
700  for (int x = 0; x < size;) {
701  if (amdl_decode_int(&ctx->position, ac, &idx, 10) < 0)
702  return -1;
703 
704  m = 0;
705  idx = (ctx->pos_idx + idx) % 11;
706  ctx->pos_idx = idx;
707 
708  rsize = FFMIN(split, size - x);
709  for (int y = 0; y < rsize; y++, off++) {
710  int midx, shift = idx, *src, sum = 16;
711 
712  if (off >= FF_ARRAY_ELEMS(ctx->buf0))
713  return -1;
714 
715  midx = FFABS(last_val) >> shift;
716  if (midx >= 15) {
717  mdl64 = &ctx->mdl64[3][idx];
718  } else if (midx >= 7) {
719  mdl64 = &ctx->mdl64[2][idx];
720  } else if (midx >= 4) {
721  mdl64 = &ctx->mdl64[1][idx];
722  } else {
723  mdl64 = &ctx->mdl64[0][idx];
724  }
725  ret = mdl64_decode(ac, mdl64, &val);
726  if (ret < 0)
727  return -1;
728  last_val = val;
729  src = &ctx->buf1[off + -1];
730  for (int i = 0; i < filt.size && i < 15; i++)
731  sum += filt.coeffs[i] * (unsigned)src[-i];
732  sum = sum * 2U;
733  for (int i = 15; i < filt.size; i++)
734  sum += filt.coeffs[i] * (unsigned)src[-i];
735  sum = sum >> 6;
736  if (ctx->qfactor == 0) {
737  if (bits == 0) {
738  ctx->buf1[off] = sum + val;
739  } else {
740  ctx->buf1[off] = (val + (sum >> bits)) * (1U << bits) +
741  (((1U << bits) - 1U) & ctx->buf1[off + -1]);
742  }
743  ctx->buf0[off] = ctx->buf1[off] + (unsigned)ctx->buf0[off + -1];
744  } else {
745  val *= 1U << ctx->qfactor;
746  sum += ctx->buf0[off + -1] + (unsigned)val;
747  switch (s->bps) {
748  case 16: sum = av_clip_int16(sum); break;
749  case 8: sum = av_clip_int8(sum); break;
750  }
751  ctx->buf1[off] = sum - ctx->buf0[off + -1];
752  ctx->buf0[off] = sum;
753  m += (unsigned)FFABS(ctx->buf1[off]);
754  }
755  }
756  if (ctx->vrq != 0) {
757  int sum = 0;
758  for (unsigned i = (m << 6) / rsize; i > 0; i = i >> 1)
759  sum++;
760  sum -= (ctx->vrq + 7);
761  ctx->qfactor = FFMAX(sum, vrq_qfactors[ctx->vrq - 1]);
762  }
763 
764  x += split;
765  }
766 
767  return 0;
768 }
769 
770 static int decode_samples(AVCodecContext *avctx, ACoder *ac, ChContext *ctx, int offset)
771 {
772  RKAContext *s = avctx->priv_data;
773  int segment_size, offset2, mode, ret;
774 
775  ret = amdl_decode_int(&ctx->nb_segments, ac, &mode, 5);
776  if (ret < 0)
777  return ret;
778 
779  if (mode == 5) {
780  ret = ac_get_freq(ac, ctx->srate_pad >> 2, &segment_size);
781  if (ret < 0)
782  return ret;
783  ac_update(ac, segment_size, 1);
784  segment_size *= 4;
785  ret = decode_filter(s, ctx, ac, offset, segment_size);
786  if (ret < 0)
787  return ret;
788  } else {
789  segment_size = ctx->srate_pad;
790 
791  if (mode) {
792  if (mode > 2) {
793  ret = decode_filter(s, ctx, ac, offset, segment_size / 4);
794  if (ret < 0)
795  return ret;
796  offset2 = segment_size / 4 + offset;
797  ret = decode_filter(s, ctx, ac, offset2, segment_size / 4);
798  if (ret < 0)
799  return ret;
800  offset2 = segment_size / 4 + offset2;
801  } else {
802  ret = decode_filter(s, ctx, ac, offset, segment_size / 2);
803  if (ret < 0)
804  return ret;
805  offset2 = segment_size / 2 + offset;
806  }
807  if (mode & 1) {
808  ret = decode_filter(s, ctx, ac, offset2, segment_size / 2);
809  if (ret < 0)
810  return ret;
811  } else {
812  ret = decode_filter(s, ctx, ac, offset2, segment_size / 4);
813  if (ret < 0)
814  return ret;
815  ret = decode_filter(s, ctx, ac, segment_size / 4 + offset2, segment_size / 4);
816  if (ret < 0)
817  return ret;
818  }
819  } else {
820  ret = decode_filter(s, ctx, ac, offset, ctx->srate_pad);
821  if (ret < 0)
822  return ret;
823  }
824  }
825 
826  return segment_size;
827 }
828 
830 {
831  RKAContext *s = avctx->priv_data;
832  ACoder *ac = &s->ac;
833  int nb_decoded = 0;
834 
835  if (bytestream2_get_bytes_left(&ac->gb) <= 0)
836  return 0;
837 
838  memmove(c->buf0, &c->buf0[c->last_nb_decoded], 2560 * sizeof(*c->buf0));
839  memmove(c->buf1, &c->buf1[c->last_nb_decoded], 2560 * sizeof(*c->buf1));
840 
841  nb_decoded = decode_samples(avctx, ac, c, 2560);
842  if (nb_decoded < 0)
843  return nb_decoded;
844  c->last_nb_decoded = nb_decoded;
845 
846  return nb_decoded;
847 }
848 
850  int *got_frame_ptr, AVPacket *avpkt)
851 {
852  RKAContext *s = avctx->priv_data;
853  ACoder *ac = &s->ac;
854  int ret;
855 
856  bytestream2_init(&ac->gb, avpkt->data, avpkt->size);
857  init_acoder(ac);
858 
859  for (int ch = 0; ch < s->channels; ch++) {
860  ret = chctx_init(s, &s->ch[ch], avctx->sample_rate,
861  avctx->bits_per_raw_sample);
862  if (ret < 0)
863  return ret;
864  }
865 
866  frame->nb_samples = s->frame_samples;
867  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
868  return ret;
869 
870  if (s->channels == 2 && s->correlated) {
871  int16_t *l16 = (int16_t *)frame->extended_data[0];
872  int16_t *r16 = (int16_t *)frame->extended_data[1];
873  uint8_t *l8 = frame->extended_data[0];
874  uint8_t *r8 = frame->extended_data[1];
875 
876  for (int n = 0; n < frame->nb_samples;) {
877  ret = decode_ch_samples(avctx, &s->ch[0]);
878  if (ret == 0) {
879  frame->nb_samples = n;
880  break;
881  }
882  if (ret < 0 || n + ret > frame->nb_samples)
883  return AVERROR_INVALIDDATA;
884 
885  ret = decode_ch_samples(avctx, &s->ch[1]);
886  if (ret == 0) {
887  frame->nb_samples = n;
888  break;
889  }
890  if (ret < 0 || n + ret > frame->nb_samples)
891  return AVERROR_INVALIDDATA;
892 
893  switch (avctx->sample_fmt) {
894  case AV_SAMPLE_FMT_S16P:
895  for (int i = 0; i < ret; i++) {
896  int l = s->ch[0].buf0[2560 + i];
897  int r = s->ch[1].buf0[2560 + i];
898 
899  l16[n + i] = (l * 2 + r + 1) >> 1;
900  r16[n + i] = (l * 2 - r + 1) >> 1;
901  }
902  break;
903  case AV_SAMPLE_FMT_U8P:
904  for (int i = 0; i < ret; i++) {
905  int l = s->ch[0].buf0[2560 + i];
906  int r = s->ch[1].buf0[2560 + i];
907 
908  l8[n + i] = ((l * 2 + r + 1) >> 1) + 0x7f;
909  r8[n + i] = ((l * 2 - r + 1) >> 1) + 0x7f;
910  }
911  break;
912  default:
913  return AVERROR_INVALIDDATA;
914  }
915 
916  n += ret;
917  }
918  } else {
919  for (int n = 0; n < frame->nb_samples;) {
920  for (int ch = 0; ch < s->channels; ch++) {
921  int16_t *m16 = (int16_t *)frame->data[ch];
922  uint8_t *m8 = frame->data[ch];
923 
924  ret = decode_ch_samples(avctx, &s->ch[ch]);
925  if (ret == 0) {
926  frame->nb_samples = n;
927  break;
928  }
929 
930  if (ret < 0 || n + ret > frame->nb_samples)
931  return AVERROR_INVALIDDATA;
932 
933  switch (avctx->sample_fmt) {
934  case AV_SAMPLE_FMT_S16P:
935  for (int i = 0; i < ret; i++) {
936  int m = s->ch[ch].buf0[2560 + i];
937 
938  m16[n + i] = m;
939  }
940  break;
941  case AV_SAMPLE_FMT_U8P:
942  for (int i = 0; i < ret; i++) {
943  int m = s->ch[ch].buf0[2560 + i];
944 
945  m8[n + i] = m + 0x7f;
946  }
947  break;
948  default:
949  return AVERROR_INVALIDDATA;
950  }
951  }
952 
953  n += ret;
954  }
955  }
956 
957  if (frame->nb_samples < s->frame_samples &&
958  frame->nb_samples > s->last_nb_samples)
959  frame->nb_samples = s->last_nb_samples;
960 
961  *got_frame_ptr = 1;
962 
963  return avpkt->size;
964 }
965 
967 {
968  RKAContext *s = avctx->priv_data;
969 
970  for (int ch = 0; ch < 2; ch++) {
971  ChContext *c = &s->ch[ch];
972 
973  for (int i = 0; i < 11; i++)
974  adaptive_model_free(&c->coeff_bits[i]);
975 
976  adaptive_model_free(&c->position);
977  adaptive_model_free(&c->nb_segments);
978  adaptive_model_free(&c->fshift);
979  }
980 
981  adaptive_model_free(&s->filt_size);
982  adaptive_model_free(&s->filt_bits);
983 
984  return 0;
985 }
986 
988  .p.name = "rka",
989  CODEC_LONG_NAME("RKA (RK Audio)"),
990  .p.type = AVMEDIA_TYPE_AUDIO,
991  .p.id = AV_CODEC_ID_RKA,
992  .priv_data_size = sizeof(RKAContext),
994  .close = rka_decode_close,
996  .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
997  .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
998 };
av_clip
#define av_clip
Definition: common.h:100
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AdaptiveModel::aprob0
uint16_t aprob0
Definition: rka.c:57
r
const char * r
Definition: vf_curves.c:127
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
ChContext::buf1
int32_t buf1[131072+2560]
Definition: rka.c:82
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
GetByteContext
Definition: bytestream.h:33
av_clip_int8
#define av_clip_int8
Definition: common.h:109
RKAContext::bprob
uint32_t bprob[2][257]
Definition: rka.c:100
int64_t
long long int64_t
Definition: coverity.c:34
ac_update
static int ac_update(ACoder *ac, int freq, int mul)
Definition: rka.c:339
Model64::zero
uint32_t zero[2]
Definition: rka.c:43
ACoder::low
uint32_t low
Definition: rka.c:33
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AdaptiveModel::prob
uint16_t * prob[2]
Definition: rka.c:59
AVPacket::data
uint8_t * data
Definition: packet.h:533
b
#define b
Definition: input.c:41
adaptive_model_free
static void adaptive_model_free(AdaptiveModel *am)
Definition: rka.c:126
high
int high
Definition: dovi_rpuenc.c:38
FFCodec
Definition: codec_internal.h:126
Model64::bits
int bits
Definition: rka.c:46
RKAContext::align
int align
Definition: rka.c:92
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
Model64::val4
uint16_t val4[65]
Definition: rka.c:48
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:321
sample_rate
sample_rate
Definition: ffmpeg_filter.c:424
FiltCoeffs
Definition: rka.c:37
Model64::size
unsigned size
Definition: rka.c:45
RKAContext::ch
ChContext ch[2]
Definition: rka.c:89
AdaptiveModel::last
int last
Definition: rka.c:53
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
ACoder::value
uint32_t value
Definition: rka.c:34
RKAContext::channels
int channels
Definition: rka.c:93
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
ChContext::vrq
int vrq
Definition: rka.c:64
ACoder::high
uint32_t high
Definition: rka.c:33
decode_filt_coeffs
static int decode_filt_coeffs(RKAContext *s, ChContext *ctx, ACoder *ac, FiltCoeffs *dst)
Definition: rka.c:495
RKAContext::total_nb_samples
uint32_t total_nb_samples
Definition: rka.c:97
val
static double val(void *priv, double ch)
Definition: aeval.c:78
AV_CODEC_ID_RKA
@ AV_CODEC_ID_RKA
Definition: codec_id.h:542
Model64::val1
uint16_t val1[65]
Definition: rka.c:49
FiltCoeffs::size
unsigned size
Definition: rka.c:39
help
static void help(void)
Definition: dct.c:454
ChContext::position
AdaptiveModel position
Definition: rka.c:74
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
RKAContext::ac
ACoder ac
Definition: rka.c:88
AdaptiveModel::total
int total
Definition: rka.c:54
ChContext::fshift
AdaptiveModel fshift
Definition: rka.c:75
decode_filter
static int decode_filter(RKAContext *s, ChContext *ctx, ACoder *ac, int off, unsigned size)
Definition: rka.c:675
Model64::sign
uint32_t sign[2]
Definition: rka.c:44
ChContext::bprob
uint32_t * bprob[2]
Definition: rka.c:72
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:524
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:286
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:198
RKAContext::correlated
int correlated
Definition: rka.c:94
RKAContext
Definition: rka.c:85
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
bits
uint8_t bits
Definition: vp3data.h:128
ChContext::qfactor
int qfactor
Definition: rka.c:63
RKAContext::last_nb_samples
int last_nb_samples
Definition: rka.c:96
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1575
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:49
decode.h
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:271
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
if
if(ret)
Definition: filter_design.txt:179
ChContext
Definition: apac.c:28
ac_dec_bit
static int ac_dec_bit(ACoder *ac)
Definition: rka.c:547
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
result
and forward the result(frame or status change) to the corresponding input. If nothing is possible
av_clip_int16
#define av_clip_int16
Definition: common.h:115
update_ch_subobj
static void update_ch_subobj(AdaptiveModel *am)
Definition: rka.c:377
init_acoder
static void init_acoder(ACoder *ac)
Definition: rka.c:247
ChContext::buf0
int32_t buf0[131072+2560]
Definition: rka.c:81
adaptive_model_init
static int adaptive_model_init(AdaptiveModel *am, int buf_size)
Definition: rka.c:106
RKAContext::filt_size
AdaptiveModel filt_size
Definition: rka.c:102
amdl_update_prob
static void amdl_update_prob(AdaptiveModel *am, int val, int diff)
Definition: rka.c:364
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
decode_bool
static int decode_bool(ACoder *ac, ChContext *c, int idx)
Definition: rka.c:301
ChContext::filt_size
AdaptiveModel * filt_size
Definition: rka.c:69
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1575
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
ff_rka_decoder
const FFCodec ff_rka_decoder
Definition: rka.c:987
AVPacket::size
int size
Definition: packet.h:534
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
bps
unsigned bps
Definition: movenc.c:1874
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
size
int size
Definition: twinvq_data.h:10344
AdaptiveModel::sum
int16_t sum
Definition: rka.c:56
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:165
split
static char * split(char *message, char delim)
Definition: af_channelmap.c:81
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
ac_decode_bool
static int ac_decode_bool(ACoder *ac, int freq1, int freq2)
Definition: rka.c:254
ChContext::filt_bits
AdaptiveModel * filt_bits
Definition: rka.c:70
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
ChContext::last_nb_decoded
int last_nb_decoded
Definition: rka.c:65
model64_init
static void model64_init(Model64 *m, unsigned bits)
Definition: rka.c:180
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
amdl_decode_int
static int amdl_decode_int(AdaptiveModel *am, ACoder *ac, unsigned *dst, unsigned size)
Definition: rka.c:410
chctx_init
static int chctx_init(RKAContext *s, ChContext *c, int sample_rate, int bps)
Definition: rka.c:201
ChContext::pos_idx
unsigned pos_idx
Definition: rka.c:67
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:31
xf
#define xf(width, name, var, range_min, range_max, subs,...)
Definition: cbs_av1.c:590
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
ChContext::nb_segments
AdaptiveModel nb_segments
Definition: rka.c:76
filt
static const int8_t filt[NUMTAPS *2]
Definition: af_earwax.c:39
RKAContext::filt_bits
AdaptiveModel filt_bits
Definition: rka.c:103
ChContext::coeff_bits
AdaptiveModel coeff_bits[11]
Definition: rka.c:77
avcodec.h
Model64
Definition: rka.c:42
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
AdaptiveModel::buf_size
int buf_size
Definition: rka.c:55
prob
#define prob(name, subs,...)
Definition: cbs_vp9.c:325
AdaptiveModel::aprob1
uint16_t aprob1
Definition: rka.c:58
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
RKAContext::bps
int bps
Definition: rka.c:91
AVCodecContext
main external API structure.
Definition: avcodec.h:445
RKAContext::samples_left
uint32_t samples_left
Definition: rka.c:98
channel_layout.h
RKAContext::frame_samples
int frame_samples
Definition: rka.c:95
mode
mode
Definition: ebur128.h:83
ChContext::srate_pad
unsigned srate_pad
Definition: rka.c:66
FiltCoeffs::coeffs
int32_t coeffs[257]
Definition: rka.c:38
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:437
vrq_qfactors
static const uint8_t vrq_qfactors[8]
Definition: rka.c:673
ac_get_freq
static int ac_get_freq(ACoder *ac, unsigned freq, int *result)
Definition: rka.c:321
ACoder
Definition: rka.c:31
mem.h
rka_decode_frame
static int rka_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: rka.c:849
mdl64_decode
static int mdl64_decode(ACoder *ac, Model64 *ctx, int *dst)
Definition: rka.c:590
rka_decode_init
static av_cold int rka_decode_init(AVCodecContext *avctx)
Definition: rka.c:132
rka_decode_close
static av_cold int rka_decode_close(AVCodecContext *avctx)
Definition: rka.c:966
AVPacket
This structure stores compressed data.
Definition: packet.h:510
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
bytestream2_init
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition: bytestream.h:137
decode_ch_samples
static int decode_ch_samples(AVCodecContext *avctx, ChContext *c)
Definition: rka.c:829
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ACoder::gb
GetByteContext gb
Definition: rka.c:32
decode_samples
static int decode_samples(AVCodecContext *avctx, ACoder *ac, ChContext *ctx, int offset)
Definition: rka.c:770
AdaptiveModel
Definition: rka.c:52
ChContext::mdl64
Model64 mdl64[4][11]
Definition: rka.c:79