FFmpeg
Loading...
Searching...
No Matches
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
24#include "libavutil/mem.h"
25
26#include "avcodec.h"
27#include "codec_internal.h"
28#include "bytestream.h"
29#include "decode.h"
30
31typedef struct ACoder {
33 uint32_t low, high;
34 uint32_t value;
35} ACoder;
36
37typedef struct FiltCoeffs {
39 unsigned size;
41
42typedef 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
52typedef struct AdaptiveModel {
53 int last;
54 int total;
56 int16_t sum;
57 uint16_t aprob0;
58 uint16_t aprob1;
59 uint16_t *prob[2];
61
62typedef struct ChContext {
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
105
106static 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:
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
180static 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
247static void init_acoder(ACoder *ac)
248{
249 ac->low = 0x0;
250 ac->high = 0xffffffff;
251 ac->value = bytestream2_get_be32(&ac->gb);
252}
253
254static 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
301static 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
321static 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
339static 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
364static 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
410static int amdl_decode_int(AdaptiveModel *am, ACoder *ac, unsigned *dst, unsigned size)
411{
412 unsigned freq, size2, val, mul;
413
414 size = FFMIN(size, am->buf_size - 1);
415
416 if (am->aprob0 >= am->sum)
418
419 if (am->aprob1 && (am->total == am->buf_size ||
420 ac_decode_bool(ac, am->aprob0, am->aprob1) == 0)) {
421 if (am->total <= 1) {
422 dst[0] = am->last;
423 amdl_update_prob(am, dst[0], 1);
424 return 0;
425 }
426 if (size == am->buf_size - 1) {
427 freq = am->aprob0;
428 } else {
429 freq = am->prob[0][0];
430 for (int j = size; j > 0; j &= (j - 1) )
431 freq += am->prob[0][j];
432 }
433 ac_get_freq(ac, freq, &freq);
434 size2 = am->buf_size >> 1;
435 val = am->prob[0][0];
436 if (freq >= val) {
437 int sum = 0, j;
438 for (j = freq - val; size2; size2 >>= 1) {
439 unsigned v = am->prob[0][size2 + sum];
440 if (j >= v) {
441 sum += size2;
442 j -= v;
443 }
444 }
445 freq -= j;
446 val = sum + 1;
447 } else {
448 freq = 0;
449 val = 0;
450 }
451 dst[0] = val;
452 mul = am->prob[0][val];
453 if (val > 0) {
454 for (int k = val - 1; (val & (val - 1)) != k; k &= k - 1)
455 mul -= am->prob[0][k];
456 }
457 ac_update(ac, freq, mul);
458 amdl_update_prob(am, dst[0], 1);
459 return 0;
460 }
461 am->aprob1++;
462 if (size == am->buf_size - 1) {
463 ac_get_freq(ac, am->buf_size - am->total, &val);
464 } else {
465 freq = 1;
466 for (dst[0] = 0; dst[0] < size; dst[0]++) {
467 if (!am->prob[1][dst[0]])
468 freq++;
469 }
470 ac_get_freq(ac, freq, &val);
471 }
472 freq = 0;
473 dst[0] = 0;
474 if (val > 0 && am->buf_size > 0) {
475 for (dst[0] = 0; dst[0] < size & freq < val; dst[0]++) {
476 if (!am->prob[1][dst[0]])
477 freq++;
478 }
479 }
480 if (am->prob[1][dst[0]]) {
481 do {
482 val = dst[0]++;
483 } while (val + 1 < am->buf_size && am->prob[1][val + 1]);
484 }
485 ac_update(ac, freq, 1);
486 am->prob[1][dst[0]]++;
487 am->total++;
488 amdl_update_prob(am, dst[0], 1);
489 am->last = dst[0];
490
491 return 0;
492}
493
495{
496 unsigned val, bits;
497 int idx = 0;
498
499 if (amdl_decode_int(ctx->filt_size, ac, &dst->size, 256) < 0)
500 return -1;
501
502 if (dst->size == 0)
503 return 0;
504
505 if (amdl_decode_int(ctx->filt_bits, ac, &bits, 10) < 0)
506 return -1;
507
508 do {
509 if (((idx == 8) || (idx == 20)) && (0 < bits))
510 bits--;
511
512 if (bits > 10)
513 return -1;
514
515 if (amdl_decode_int(&ctx->coeff_bits[bits], ac, &val, 31) < 0)
516 return -1;
517
518 if (val == 31) {
519 ac_get_freq(ac, 65536, &val);
520 ac_update(ac, val, 1);
521 }
522
523 if (val == 0) {
524 dst->coeffs[idx++] = 0;
525 } else {
526 unsigned freq = 0;
527 int sign;
528
529 if (bits > 0) {
530 ac_get_freq(ac, 1 << bits, &freq);
531 ac_update(ac, freq, 1);
532 }
533 dst->coeffs[idx] = freq + 1 + ((val - 1U) << bits);
534 sign = decode_bool(ac, ctx, idx);
535 if (sign < 0)
536 return -1;
537 if (sign == 1)
538 dst->coeffs[idx] = -dst->coeffs[idx];
539 idx++;
540 }
541 } while (idx < dst->size);
542
543 return 0;
544}
545
546static int ac_dec_bit(ACoder *ac)
547{
548 uint32_t high, low;
549
550 low = ac->low;
551 ac->high = high = ac->high >> 1;
552 if (ac->value - low < high) {
553 do {
554 if (((high + low) ^ low) > 0xffffff) {
555 if (high > 0xffff)
556 return 0;
557 ac->high = (uint16_t)-(int16_t)low;
558 }
559
560 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
561 break;
562
563 ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb);
564 ac->high = high = ac->high << 8;
565 ac->low = low = ac->low << 8;
566 } while (1);
567
568 return -1;
569 }
570 ac->low = low = low + high;
571 do {
572 if (((high + low) ^ low) > 0xffffff) {
573 if (high > 0xffff)
574 return 1;
575 ac->high = (uint16_t)-(int16_t)low;
576 }
577
578 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
579 break;
580
581 ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb);
582 ac->high = high = ac->high << 8;
583 ac->low = low = ac->low << 8;
584 } while (1);
585
586 return -1;
587}
588
589static int mdl64_decode(ACoder *ac, Model64 *ctx, int *dst)
590{
591 int sign, idx, bits;
592 unsigned val = 0;
593
594 if (ctx->zero[0] + ctx->zero[1] > 4000U) {
595 ctx->zero[0] = (ctx->zero[0] >> 1) + 1;
596 ctx->zero[1] = (ctx->zero[1] >> 1) + 1;
597 }
598 if (ctx->sign[0] + ctx->sign[1] > 4000U) {
599 ctx->sign[0] = (ctx->sign[0] >> 1) + 1;
600 ctx->sign[1] = (ctx->sign[1] >> 1) + 1;
601 }
602 sign = ac_decode_bool(ac, ctx->zero[0], ctx->zero[1]);
603 if (sign == 0) {
604 ctx->zero[0] += 2;
605 dst[0] = 0;
606 return 0;
607 } else if (sign < 0) {
608 return -1;
609 }
610
611 ctx->zero[1] += 2;
612 sign = ac_decode_bool(ac, ctx->sign[0], ctx->sign[1]);
613 if (sign < 0)
614 return -1;
615 ctx->sign[sign]++;
616 bits = ctx->bits;
617 if (bits > 0) {
618 if (bits < 13) {
619 ac_get_freq(ac, 1 << bits, &val);
620 ac_update(ac, val, 1);
621 } else {
622 int hbits = bits / 2;
623 ac_get_freq(ac, 1 << hbits, &val);
624 ac_update(ac, val, 1);
625 ac_get_freq(ac, 1 << (ctx->bits - (hbits)), &bits);
626 ac_update(ac, val, 1);
627 val += (bits << hbits);
628 }
629 }
630 bits = ctx->size;
631 idx = 0;
632 if (bits >= 0) {
633 do {
634 uint16_t *val4 = ctx->val4;
635 int b;
636
637 if (val4[idx] + ctx->val1[idx] > 2000U) {
638 val4[idx] = (val4[idx] >> 1) + 1;
639 ctx->val1[idx] = (ctx->val1[idx] >> 1) + 1;
640 }
641 b = ac_decode_bool(ac, ctx->val4[idx], ctx->val1[idx]);
642 if (b == 1) {
643 ctx->val1[idx] += 4;
644 break;
645 } else if (b < 0) {
646 return -1;
647 }
648 ctx->val4[idx] += 4;
649 idx++;
650 } while (idx <= ctx->size);
651 bits = ctx->size;
652 if (idx <= bits) {
653 dst[0] = val + 1 + (idx << ctx->bits);
654 if (sign)
655 dst[0] = -dst[0];
656 return 0;
657 }
658 }
659 bits++;
660 while (ac_dec_bit(ac) == 0)
661 bits += 64;
662 ac_get_freq(ac, 64, &idx);
663 ac_update(ac, idx, 1);
664 idx += bits;
665 dst[0] = val + 1 + (idx << ctx->bits);
666 if (sign)
667 dst[0] = -dst[0];
668
669 return 0;
670}
671
672static const uint8_t vrq_qfactors[8] = { 3, 3, 2, 2, 1, 1, 1, 1 };
673
674static int decode_filter(RKAContext *s, ChContext *ctx, ACoder *ac, int off, unsigned size)
675{
677 Model64 *mdl64;
678 int split, val, last_val = 0, ret;
679 unsigned rsize, idx = 3, bits = 0, m = 0;
680
681 if (ctx->qfactor == 0) {
682 if (amdl_decode_int(&ctx->fshift, ac, &bits, 15) < 0)
683 return -1;
684 bits &= 31U;
685 }
686
687 ret = decode_filt_coeffs(s, ctx, ac, &filt);
688 if (ret < 0)
689 return ret;
690
691 if (size < 512)
692 split = size / 2;
693 else
694 split = size >> 4;
695
696 if (size <= 1)
697 return 0;
698
699 for (int x = 0; x < size;) {
700 if (amdl_decode_int(&ctx->position, ac, &idx, 10) < 0)
701 return -1;
702
703 m = 0;
704 idx = (ctx->pos_idx + idx) % 11;
705 ctx->pos_idx = idx;
706
707 rsize = FFMIN(split, size - x);
708 for (int y = 0; y < rsize; y++, off++) {
709 int midx, shift = idx, *src, sum = 16;
710
711 if (off >= FF_ARRAY_ELEMS(ctx->buf0))
712 return -1;
713
714 midx = FFABS(last_val) >> shift;
715 if (midx >= 15) {
716 mdl64 = &ctx->mdl64[3][idx];
717 } else if (midx >= 7) {
718 mdl64 = &ctx->mdl64[2][idx];
719 } else if (midx >= 4) {
720 mdl64 = &ctx->mdl64[1][idx];
721 } else {
722 mdl64 = &ctx->mdl64[0][idx];
723 }
724 ret = mdl64_decode(ac, mdl64, &val);
725 if (ret < 0)
726 return -1;
727 last_val = val;
728 src = &ctx->buf1[off + -1];
729 for (int i = 0; i < filt.size && i < 15; i++)
730 sum += filt.coeffs[i] * (unsigned)src[-i];
731 sum = sum * 2U;
732 for (int i = 15; i < filt.size; i++)
733 sum += filt.coeffs[i] * (unsigned)src[-i];
734 sum = sum >> 6;
735 if (ctx->qfactor == 0) {
736 if (bits == 0) {
737 ctx->buf1[off] = sum + val;
738 } else {
739 ctx->buf1[off] = (val + (sum >> bits)) * (1U << bits) +
740 (((1U << bits) - 1U) & ctx->buf1[off + -1]);
741 }
742 ctx->buf0[off] = ctx->buf1[off] + (unsigned)ctx->buf0[off + -1];
743 } else {
744 val *= 1U << ctx->qfactor;
745 sum += ctx->buf0[off + -1] + (unsigned)val;
746 switch (s->bps) {
747 case 16: sum = av_clip_int16(sum); break;
748 case 8: sum = av_clip_int8(sum); break;
749 }
750 ctx->buf1[off] = sum - ctx->buf0[off + -1];
751 ctx->buf0[off] = sum;
752 m += (unsigned)FFABS(ctx->buf1[off]);
753 }
754 }
755 if (ctx->vrq != 0) {
756 int sum = 0;
757 for (unsigned i = (m << 6) / rsize; i > 0; i = i >> 1)
758 sum++;
759 sum -= (ctx->vrq + 7);
760 ctx->qfactor = FFMAX(sum, vrq_qfactors[ctx->vrq - 1]);
761 }
762
763 x += split;
764 }
765
766 return 0;
767}
768
770{
771 RKAContext *s = avctx->priv_data;
772 int segment_size, offset2, mode, ret;
773
774 ret = amdl_decode_int(&ctx->nb_segments, ac, &mode, 5);
775 if (ret < 0)
776 return ret;
777
778 if (mode == 5) {
779 ret = ac_get_freq(ac, ctx->srate_pad >> 2, &segment_size);
780 if (ret < 0)
781 return ret;
782 ac_update(ac, segment_size, 1);
783 segment_size *= 4;
784 ret = decode_filter(s, ctx, ac, offset, segment_size);
785 if (ret < 0)
786 return ret;
787 } else {
788 segment_size = ctx->srate_pad;
789
790 if (mode) {
791 if (mode > 2) {
792 ret = decode_filter(s, ctx, ac, offset, segment_size / 4);
793 if (ret < 0)
794 return ret;
795 offset2 = segment_size / 4 + offset;
796 ret = decode_filter(s, ctx, ac, offset2, segment_size / 4);
797 if (ret < 0)
798 return ret;
799 offset2 = segment_size / 4 + offset2;
800 } else {
801 ret = decode_filter(s, ctx, ac, offset, segment_size / 2);
802 if (ret < 0)
803 return ret;
804 offset2 = segment_size / 2 + offset;
805 }
806 if (mode & 1) {
807 ret = decode_filter(s, ctx, ac, offset2, segment_size / 2);
808 if (ret < 0)
809 return ret;
810 } else {
811 ret = decode_filter(s, ctx, ac, offset2, segment_size / 4);
812 if (ret < 0)
813 return ret;
814 ret = decode_filter(s, ctx, ac, segment_size / 4 + offset2, segment_size / 4);
815 if (ret < 0)
816 return ret;
817 }
818 } else {
819 ret = decode_filter(s, ctx, ac, offset, ctx->srate_pad);
820 if (ret < 0)
821 return ret;
822 }
823 }
824
825 return segment_size;
826}
827
829{
830 RKAContext *s = avctx->priv_data;
831 ACoder *ac = &s->ac;
832 int nb_decoded = 0;
833
834 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
835 return 0;
836
837 memmove(c->buf0, &c->buf0[c->last_nb_decoded], 2560 * sizeof(*c->buf0));
838 memmove(c->buf1, &c->buf1[c->last_nb_decoded], 2560 * sizeof(*c->buf1));
839
840 nb_decoded = decode_samples(avctx, ac, c, 2560);
841 if (nb_decoded < 0)
842 return nb_decoded;
843 c->last_nb_decoded = nb_decoded;
844
845 return nb_decoded;
846}
847
849 int *got_frame_ptr, AVPacket *avpkt)
850{
851 RKAContext *s = avctx->priv_data;
852 ACoder *ac = &s->ac;
853 int ret;
854
855 bytestream2_init(&ac->gb, avpkt->data, avpkt->size);
856 init_acoder(ac);
857
858 for (int ch = 0; ch < s->channels; ch++) {
859 ret = chctx_init(s, &s->ch[ch], avctx->sample_rate,
860 avctx->bits_per_raw_sample);
861 if (ret < 0)
862 return ret;
863 }
864
865 frame->nb_samples = s->frame_samples;
866 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
867 return ret;
868
869 if (s->channels == 2 && s->correlated) {
870 int16_t *l16 = (int16_t *)frame->extended_data[0];
871 int16_t *r16 = (int16_t *)frame->extended_data[1];
872 uint8_t *l8 = frame->extended_data[0];
873 uint8_t *r8 = frame->extended_data[1];
874
875 for (int n = 0; n < frame->nb_samples;) {
876 ret = decode_ch_samples(avctx, &s->ch[0]);
877 if (ret == 0) {
878 frame->nb_samples = n;
879 break;
880 }
881 if (ret < 0 || n + ret > frame->nb_samples)
882 return AVERROR_INVALIDDATA;
883
884 ret = decode_ch_samples(avctx, &s->ch[1]);
885 if (ret == 0) {
886 frame->nb_samples = n;
887 break;
888 }
889 if (ret < 0 || n + ret > frame->nb_samples)
890 return AVERROR_INVALIDDATA;
891
892 switch (avctx->sample_fmt) {
894 for (int i = 0; i < ret; i++) {
895 int l = s->ch[0].buf0[2560 + i];
896 int r = s->ch[1].buf0[2560 + i];
897
898 l16[n + i] = (l * 2 + r + 1) >> 1;
899 r16[n + i] = (l * 2 - r + 1) >> 1;
900 }
901 break;
903 for (int i = 0; i < ret; i++) {
904 int l = s->ch[0].buf0[2560 + i];
905 int r = s->ch[1].buf0[2560 + i];
906
907 l8[n + i] = ((l * 2 + r + 1) >> 1) + 0x7f;
908 r8[n + i] = ((l * 2 - r + 1) >> 1) + 0x7f;
909 }
910 break;
911 default:
912 return AVERROR_INVALIDDATA;
913 }
914
915 n += ret;
916 }
917 } else {
918 for (int n = 0; n < frame->nb_samples;) {
919 for (int ch = 0; ch < s->channels; ch++) {
920 int16_t *m16 = (int16_t *)frame->data[ch];
921 uint8_t *m8 = frame->data[ch];
922
923 ret = decode_ch_samples(avctx, &s->ch[ch]);
924 if (ret == 0) {
925 frame->nb_samples = n;
926 break;
927 }
928
929 if (ret < 0 || n + ret > frame->nb_samples)
930 return AVERROR_INVALIDDATA;
931
932 switch (avctx->sample_fmt) {
934 for (int i = 0; i < ret; i++) {
935 int m = s->ch[ch].buf0[2560 + i];
936
937 m16[n + i] = m;
938 }
939 break;
941 for (int i = 0; i < ret; i++) {
942 int m = s->ch[ch].buf0[2560 + i];
943
944 m8[n + i] = m + 0x7f;
945 }
946 break;
947 default:
948 return AVERROR_INVALIDDATA;
949 }
950 }
951
952 n += ret;
953 }
954 }
955
956 if (frame->nb_samples < s->frame_samples &&
957 frame->nb_samples > s->last_nb_samples)
958 frame->nb_samples = s->last_nb_samples;
959
960 *got_frame_ptr = 1;
961
962 return avpkt->size;
963}
964
966{
967 RKAContext *s = avctx->priv_data;
968
969 for (int ch = 0; ch < 2; ch++) {
970 ChContext *c = &s->ch[ch];
971
972 for (int i = 0; i < 11; i++)
973 adaptive_model_free(&c->coeff_bits[i]);
974
975 adaptive_model_free(&c->position);
976 adaptive_model_free(&c->nb_segments);
977 adaptive_model_free(&c->fshift);
978 }
979
980 adaptive_model_free(&s->filt_size);
981 adaptive_model_free(&s->filt_bits);
982
983 return 0;
984}
985
987 .p.name = "rka",
988 CODEC_LONG_NAME("RKA (RK Audio)"),
989 .p.type = AVMEDIA_TYPE_AUDIO,
990 .p.id = AV_CODEC_ID_RKA,
991 .priv_data_size = sizeof(RKAContext),
996 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
997};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
static char * split(char *message, char delim)
static const int8_t filt[NUMTAPS *2]
Definition af_earwax.c:40
const FFCodec ff_rka_decoder
Definition rka.c:986
static av_cold void close(AVCodecParserContext *s)
Definition apv_parser.c:197
int32_t
Libavcodec external API header.
static av_always_inline int bytestream2_get_bytes_left(const GetByteContext *g)
Definition bytestream.h:158
static av_always_inline void bytestream2_init(GetByteContext *g, const uint8_t *buf, int buf_size)
Definition bytestream.h:137
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define prob(name, subs,...)
Definition cbs_vp9.c:252
Public libavutil channel layout APIs header.
#define FF_CODEC_DECODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define av_clip_int8
Definition common.h:109
#define av_clip
Definition common.h:100
#define av_clip_int16
Definition common.h:115
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
long long int64_t
Definition coverity.c:34
static void help(void)
Definition dct.c:457
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
static AVFrame * frame
int high
Definition dovi_rpuenc.c:39
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
double value
Definition eval.c:102
static const uint8_t bits[8]
Definition fastaudio.c:100
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition codec.h:94
@ AV_CODEC_ID_RKA
Definition codec_id.h:555
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
@ AVMEDIA_TYPE_AUDIO
Definition avutil.h:201
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_U8P
unsigned 8 bits, planar
Definition samplefmt.h:63
#define r
Definition input.c:42
#define b
Definition input.c:43
#define AV_RL32(p)
unsigned offset
Definition libaomenc.c:763
static int shift(int a, int b)
Definition bonk.c:261
static int ac_get_freq(ACoder *ac, unsigned freq, int *result)
Definition rka.c:321
static int decode_ch_samples(AVCodecContext *avctx, ChContext *c)
Definition rka.c:828
static int ac_update(ACoder *ac, int freq, int mul)
Definition rka.c:339
static int decode_bool(ACoder *ac, ChContext *c, int idx)
Definition rka.c:301
static void adaptive_model_free(AdaptiveModel *am)
Definition rka.c:126
static int decode_filter(RKAContext *s, ChContext *ctx, ACoder *ac, int off, unsigned size)
Definition rka.c:674
static int amdl_decode_int(AdaptiveModel *am, ACoder *ac, unsigned *dst, unsigned size)
Definition rka.c:410
static int chctx_init(RKAContext *s, ChContext *c, int sample_rate, int bps)
Definition rka.c:201
static int mdl64_decode(ACoder *ac, Model64 *ctx, int *dst)
Definition rka.c:589
static int ac_decode_bool(ACoder *ac, int freq1, int freq2)
Definition rka.c:254
static int decode_filt_coeffs(RKAContext *s, ChContext *ctx, ACoder *ac, FiltCoeffs *dst)
Definition rka.c:494
static int ac_dec_bit(ACoder *ac)
Definition rka.c:546
static int adaptive_model_init(AdaptiveModel *am, int buf_size)
Definition rka.c:106
static av_cold int rka_decode_init(AVCodecContext *avctx)
Definition rka.c:132
static void amdl_update_prob(AdaptiveModel *am, int val, int diff)
Definition rka.c:364
static void model64_init(Model64 *m, unsigned bits)
Definition rka.c:180
static void update_ch_subobj(AdaptiveModel *am)
Definition rka.c:377
static const uint8_t vrq_qfactors[8]
Definition rka.c:672
static void init_acoder(ACoder *ac)
Definition rka.c:247
static int decode_samples(AVCodecContext *avctx, ACoder *ac, ChContext *ctx, int offset)
Definition rka.c:769
static av_cold int rka_decode_close(AVCodecContext *avctx)
Definition rka.c:965
static int rka_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition rka.c:848
#define av_cold
Definition attributes.h:117
#define FFMIN(a, b)
Definition macros.h:49
#define FFMAX(a, b)
Definition macros.h:47
Memory handling functions.
unsigned bps
Definition movenc.c:2074
#define FF_ARRAY_ELEMS(a)
Definition rka.c:31
uint32_t high
Definition rka.c:33
uint32_t value
Definition rka.c:34
GetByteContext gb
Definition rka.c:32
uint32_t low
Definition rka.c:33
int nb_channels
Number of channels in this layout.
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
AVChannelLayout ch_layout
Audio channel layout.
Definition avcodec.h:1055
enum AVSampleFormat sample_fmt
audio sample format
Definition avcodec.h:1047
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition avcodec.h:1571
int sample_rate
samples per second
Definition avcodec.h:1040
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition avcodec.h:526
int extradata_size
Definition avcodec.h:527
void * priv_data
Definition avcodec.h:470
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
int size
Definition packet.h:604
uint8_t * data
Definition packet.h:603
int16_t sum
Definition rka.c:56
uint16_t aprob1
Definition rka.c:58
uint16_t aprob0
Definition rka.c:57
int buf_size
Definition rka.c:55
uint16_t * prob[2]
Definition rka.c:59
int last
Definition rka.c:53
int total
Definition rka.c:54
int32_t buf0[131072+2560]
Definition rka.c:81
int32_t buf1[131072+2560]
Definition rka.c:82
int qfactor
Definition rka.c:63
AdaptiveModel coeff_bits[11]
Definition rka.c:77
AdaptiveModel * filt_size
Definition rka.c:69
AdaptiveModel fshift
Definition rka.c:75
AdaptiveModel nb_segments
Definition rka.c:76
int last_nb_decoded
Definition rka.c:65
AdaptiveModel position
Definition rka.c:74
Model64 mdl64[4][11]
Definition rka.c:79
unsigned pos_idx
Definition rka.c:67
unsigned srate_pad
Definition rka.c:66
uint32_t * bprob[2]
Definition rka.c:72
int vrq
Definition rka.c:64
AdaptiveModel * filt_bits
Definition rka.c:70
unsigned size
Definition rka.c:39
int32_t coeffs[257]
Definition rka.c:38
Definition rka.c:42
uint16_t val4[65]
Definition rka.c:48
int bits
Definition rka.c:46
uint32_t sign[2]
Definition rka.c:44
uint16_t val1[65]
Definition rka.c:49
unsigned size
Definition rka.c:45
uint32_t zero[2]
Definition rka.c:43
int channels
Definition rka.c:93
ACoder ac
Definition rka.c:88
ChContext ch[2]
Definition rka.c:89
uint32_t samples_left
Definition rka.c:98
uint32_t bprob[2][257]
Definition rka.c:100
int last_nb_samples
Definition rka.c:96
uint32_t total_nb_samples
Definition rka.c:97
int bps
Definition rka.c:91
int align
Definition rka.c:92
int frame_samples
Definition rka.c:95
AdaptiveModel filt_size
Definition rka.c:102
AdaptiveModel filt_bits
Definition rka.c:103
int correlated
Definition rka.c:94
Definition swscale.c:71
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_log(a,...)
#define src
Definition vp8dsp.c:248
static AVFormatContext * ctx
Definition movenc.c:49
int size
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
static double c[64]