blob: 8719325a9ed207387261f1883c44f71f9469ec1a [file] [log] [blame]
Paul B Maholb8c08022023-01-31 20:03:381/*
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
22#include "libavutil/channel_layout.h"
23#include "libavutil/intreadwrite.h"
24
25#include "avcodec.h"
26#include "codec_internal.h"
27#include "bytestream.h"
28#include "decode.h"
29
30typedef struct ACoder {
31 GetByteContext gb;
32 uint32_t low, high;
33 uint32_t value;
34} ACoder;
35
36typedef struct FiltCoeffs {
37 int32_t coeffs[257];
38 unsigned size;
39} FiltCoeffs;
40
41typedef struct Model64 {
42 uint32_t zero[2];
43 uint32_t sign[2];
44 unsigned size;
45 int bits;
46
47 uint16_t val4[65];
48 uint16_t val1[65];
49} Model64;
50
51typedef struct AdaptiveModel {
52 int last;
53 int total;
54 int buf_size;
55 int16_t sum;
56 uint16_t aprob0;
57 uint16_t aprob1;
58 uint16_t *prob[2];
59} AdaptiveModel;
60
61typedef struct ChContext {
62 int cmode;
63 int cmode2;
64 int last_nb_decoded;
65 unsigned srate_pad;
66 unsigned pos_idx;
67
68 AdaptiveModel *filt_size;
69 AdaptiveModel *filt_bits;
70
Paul B Mahol7c41a082023-02-13 17:21:5671 uint32_t *bprob[2];
Paul B Maholb8c08022023-01-31 20:03:3872
73 AdaptiveModel position;
74 AdaptiveModel fshift;
75 AdaptiveModel nb_segments;
76 AdaptiveModel coeff_bits[11];
77
78 Model64 mdl64[4][11];
79
Paul B Maholcb350362023-02-15 12:54:1180 int32_t buf0[131072+2560];
81 int32_t buf1[131072+2560];
Paul B Maholb8c08022023-01-31 20:03:3882} ChContext;
83
84typedef struct RKAContext {
85 AVClass *class;
86
87 ACoder ac;
88 ChContext ch[2];
89
90 int bps;
91 int align;
92 int channels;
93 int frame_samples;
94 int last_nb_samples;
95 uint32_t total_nb_samples;
96 uint32_t samples_left;
97
Paul B Mahol7c41a082023-02-13 17:21:5698 uint32_t bprob[2][257];
Paul B Maholb8c08022023-01-31 20:03:3899
100 AdaptiveModel filt_size;
101 AdaptiveModel filt_bits;
102} RKAContext;
103
104static int adaptive_model_init(AdaptiveModel *am, int buf_size)
105{
106 am->buf_size = buf_size;
107 am->sum = 2000;
108 am->aprob0 = 0;
109 am->aprob1 = 0;
110 am->total = 0;
111
112 if (!am->prob[0])
113 am->prob[0] = av_malloc_array(buf_size + 5, sizeof(*am->prob[0]));
114 if (!am->prob[1])
115 am->prob[1] = av_malloc_array(buf_size + 5, sizeof(*am->prob[1]));
116
117 if (!am->prob[0] || !am->prob[1])
118 return AVERROR(ENOMEM);
119 memset(am->prob[0], 0, (buf_size + 5) * sizeof(*am->prob[0]));
120 memset(am->prob[1], 0, (buf_size + 5) * sizeof(*am->prob[1]));
121 return 0;
122}
123
124static void adaptive_model_free(AdaptiveModel *am)
125{
126 av_freep(&am->prob[0]);
127 av_freep(&am->prob[1]);
128}
129
130static av_cold int rka_decode_init(AVCodecContext *avctx)
131{
132 RKAContext *s = avctx->priv_data;
133 int cmode;
134
135 if (avctx->extradata_size < 16)
136 return AVERROR_INVALIDDATA;
137
138 s->bps = avctx->bits_per_raw_sample = avctx->extradata[13];
139
140 switch (s->bps) {
141 case 8:
142 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
143 break;
144 case 16:
145 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
146 break;
147 default:
148 return AVERROR_INVALIDDATA;
149 }
150
151 s->channels = avctx->ch_layout.nb_channels;
152 if (s->channels < 1 || s->channels > 2)
153 return AVERROR_INVALIDDATA;
154
155 s->align = (s->channels * (avctx->bits_per_raw_sample >> 3));
156 s->samples_left = s->total_nb_samples = (AV_RL32(avctx->extradata + 4)) / s->align;
157 s->frame_samples = 131072 / s->align;
158 s->last_nb_samples = s->total_nb_samples % s->frame_samples;
159
160 cmode = avctx->extradata[14] & 0xf;
161 if ((avctx->extradata[15] & 4) != 0)
162 cmode = -cmode;
163
Paul B Maholb5534b92023-02-14 08:46:46164 s->ch[0].cmode = s->ch[1].cmode = cmode < 0 ? 2 : cmode;
165 s->ch[0].cmode2 = cmode < 0 ? FFABS(cmode) : 0;
166 s->ch[1].cmode2 = cmode < 0 ? FFABS(cmode) : 0;
Paul B Maholb8c08022023-01-31 20:03:38167 av_log(avctx, AV_LOG_DEBUG, "cmode: %d\n", cmode);
168
169 return 0;
170}
171
172static void model64_init(Model64 *m, unsigned bits)
173{
174 unsigned x;
175
176 m->bits = bits;
177 m->size = 64;
178 m->zero[0] = 1;
179
180 x = (1 << (bits >> 1)) + 3;
181 x = FFMIN(x, 20);
182
183 m->zero[1] = x;
184 m->sign[0] = 1;
185 m->sign[1] = 1;
186
187 for (int i = 0; i < FF_ARRAY_ELEMS(m->val4); i++) {
188 m->val4[i] = 4;
189 m->val1[i] = 1;
190 }
191}
192
193static int chctx_init(RKAContext *s, ChContext *c,
194 int sample_rate, int bps)
195{
196 int ret;
197
198 memset(c->buf0, 0, sizeof(c->buf0));
199 memset(c->buf1, 0, sizeof(c->buf1));
200
201 c->filt_size = &s->filt_size;
202 c->filt_bits = &s->filt_bits;
203
Paul B Mahol7c41a082023-02-13 17:21:56204 c->bprob[0] = s->bprob[0];
205 c->bprob[1] = s->bprob[1];
Paul B Maholb8c08022023-01-31 20:03:38206
207 c->srate_pad = (sample_rate << 13) / 44100 & 0xFFFFFFFCU;
208 c->pos_idx = 1;
209
Paul B Mahol7c41a082023-02-13 17:21:56210 for (int i = 0; i < FF_ARRAY_ELEMS(s->bprob[0]); i++)
211 c->bprob[0][i] = c->bprob[1][i] = 1;
Paul B Maholb8c08022023-01-31 20:03:38212
213 for (int i = 0; i < 11; i++) {
214 ret = adaptive_model_init(&c->coeff_bits[i], 32);
215 if (ret < 0)
216 return ret;
217
218 model64_init(&c->mdl64[0][i], i);
219 model64_init(&c->mdl64[1][i], i);
220 model64_init(&c->mdl64[2][i], i+1);
221 model64_init(&c->mdl64[3][i], i+1);
222 }
223
224 ret = adaptive_model_init(c->filt_size, 256);
225 if (ret < 0)
226 return ret;
227 ret = adaptive_model_init(c->filt_bits, 16);
228 if (ret < 0)
229 return ret;
230 ret = adaptive_model_init(&c->position, 16);
231 if (ret < 0)
232 return ret;
233 ret = adaptive_model_init(&c->nb_segments, 8);
234 if (ret < 0)
235 return ret;
236 return adaptive_model_init(&c->fshift, 32);
237}
238
239static void init_acoder(ACoder *ac)
240{
241 ac->low = 0x0;
242 ac->high = 0xffffffff;
243 ac->value = bytestream2_get_be32(&ac->gb);
244}
245
246static int ac_decode_bool(ACoder *ac, int freq1, int freq2)
247{
Paul B Mahol7c41a082023-02-13 17:21:56248 unsigned help, add, high, value;
249 int low;
Paul B Maholb8c08022023-01-31 20:03:38250
251 low = ac->low;
252 help = ac->high / (unsigned)(freq2 + freq1);
253 value = ac->value;
254 add = freq1 * help;
255 ac->high = help;
256
257 if (value - low >= add) {
258 ac->low = low = add + low;
259 ac->high = high = freq2 * help;
260 while (1) {
261 if ((low ^ (high + low)) > 0xFFFFFF) {
262 if (high > 0xFFFF)
263 return 1;
264 ac->high = (uint16_t)-(int16_t)low;
265 }
266
267 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
268 break;
269 ac->value = bytestream2_get_byteu(&ac->gb) | (ac->value << 8);
270 ac->high = high = ac->high << 8;
Paul B Mahol7c41a082023-02-13 17:21:56271 low = ac->low = ac->low << 8;
Paul B Maholb8c08022023-01-31 20:03:38272 }
273 return -1;
274 }
275
276 ac->high = add;
277 while (1) {
278 if ((low ^ (add + low)) > 0xFFFFFF) {
279 if (add > 0xFFFF)
280 return 0;
281 ac->high = (uint16_t)-(int16_t)low;
282 }
283
284 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
285 break;
286 ac->value = bytestream2_get_byteu(&ac->gb) | (ac->value << 8);
287 ac->high = add = ac->high << 8;
Paul B Mahol7c41a082023-02-13 17:21:56288 low = ac->low = ac->low << 8;
Paul B Maholb8c08022023-01-31 20:03:38289 }
290 return -1;
291}
292
293static int decode_bool(ACoder *ac, ChContext *c, int idx)
294{
Paul B Mahol7c41a082023-02-13 17:21:56295 uint32_t x;
296 int b;
Paul B Maholb8c08022023-01-31 20:03:38297
Paul B Mahol7c41a082023-02-13 17:21:56298 x = c->bprob[0][idx];
299 if (x + c->bprob[1][idx] > 4096) {
300 c->bprob[0][idx] = (x >> 1) + 1;
301 c->bprob[1][idx] = (c->bprob[1][idx] >> 1) + 1;
Paul B Maholb8c08022023-01-31 20:03:38302 }
303
Paul B Mahol7c41a082023-02-13 17:21:56304 b = ac_decode_bool(ac, c->bprob[0][idx], c->bprob[1][idx]);
305 if (b < 0)
306 return b;
307
308 c->bprob[b][idx]++;
Paul B Maholb8c08022023-01-31 20:03:38309
310 return b;
311}
312
313static int ac_get_freq(ACoder *ac, unsigned freq, int *result)
314{
315 uint32_t new_high;
316
317 if (freq == 0)
318 return -1;
319
320 new_high = ac->high / freq;
321 ac->high = new_high;
322
323 if (new_high == 0)
324 return -1;
325
326 *result = (ac->value - ac->low) / new_high;
327
328 return 0;
329}
330
331static int ac_update(ACoder *ac, int freq, int mul)
332{
333 uint32_t low, high;
334
335 low = ac->low = ac->high * freq + ac->low;
336 high = ac->high = ac->high * mul;
337
338 while (1) {
339 if (((high + low) ^ low) > 0xffffff) {
340 if (high > 0xffff)
341 return 0;
Paul B Mahol7c41a082023-02-13 17:21:56342 ac->high = (uint16_t)-(int16_t)low;
Paul B Maholb8c08022023-01-31 20:03:38343 }
344
345 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
346 break;
347
348 ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb);
349 low = ac->low = ac->low << 8;
350 high = ac->high = ac->high << 8;
351 }
352
353 return -1;
354}
355
356static void amdl_update_prob(AdaptiveModel *am, int val, int diff)
357{
358 am->aprob0 += diff;
359 if (val <= 0) {
360 am->prob[0][0] += diff;
361 } else {
362 do {
363 am->prob[0][val] += diff;
364 val += (val & -val);
365 } while (val < am->buf_size);
366 }
367}
368
369static void update_ch_subobj(AdaptiveModel *am)
370{
371 int idx2, idx = am->buf_size - 1;
372
373 if (idx >= 0) {
374 do {
375 uint16_t *prob = am->prob[0];
376 int diff, prob_idx = prob[idx];
377
378 idx2 = idx - 1;
379 if (idx > 0) {
380 int idx3 = idx - 1;
381
382 if ((idx2 & idx) != idx2) {
383 do {
384 prob_idx -= prob[idx3];
385 idx3 &= idx3 - 1;
386 } while ((idx2 & idx) != idx3);
387 }
388 }
389
390 diff = ((prob_idx > 0) - prob_idx) >> 1;
391 amdl_update_prob(am, idx, diff);
392 idx--;
393 } while (idx2 >= 0);
394 }
395
396 if (am->sum < 8000)
397 am->sum += 200;
398
399 am->aprob1 = (am->aprob1 + 1) >> 1;
400}
401
402static int amdl_decode_int(AdaptiveModel *am, ACoder *ac, unsigned *dst, unsigned size)
403{
Paul B Mahol7c41a082023-02-13 17:21:56404 unsigned freq, size2, val, mul;
405 int j;
Paul B Maholb8c08022023-01-31 20:03:38406
407 size = FFMIN(size, am->buf_size - 1);
408
409 if (am->aprob0 >= am->sum)
410 update_ch_subobj(am);
411
412 if (am->aprob1 && (am->total == am->buf_size ||
413 ac_decode_bool(ac, am->aprob0, am->aprob1) == 0)) {
414 if (am->total <= 1) {
415 dst[0] = am->last;
416 amdl_update_prob(am, dst[0], 1);
417 return 0;
418 }
419 if (size == am->buf_size - 1) {
420 freq = am->aprob0;
421 } else {
422 freq = am->prob[0][0];
423 for (int j = size; j > 0; j &= (j - 1) )
424 freq += am->prob[0][j];
425 }
426 ac_get_freq(ac, freq, &freq);
427 size2 = am->buf_size >> 1;
428 val = am->prob[0][0];
429 if (freq >= val) {
430 int sum = 0;
431 for (j = freq - val; size2; size2 >>= 1) {
432 unsigned v = am->prob[0][size2 + sum];
433 if (j >= v) {
434 sum += size2;
435 j -= v;
436 }
437 }
Paul B Mahol7c41a082023-02-13 17:21:56438 freq -= j;
Paul B Maholb8c08022023-01-31 20:03:38439 val = sum + 1;
440 } else {
441 freq = 0;
442 val = 0;
443 }
444 dst[0] = val;
445 mul = am->prob[0][val];
446 if (val > 0) {
447 for (int k = val - 1; (val & (val - 1)) != k; k &= k - 1)
448 mul -= am->prob[0][k];
449 }
450 ac_update(ac, freq, mul);
451 amdl_update_prob(am, dst[0], 1);
452 return 0;
453 }
454 am->aprob1++;
455 if (size == am->buf_size - 1) {
456 ac_get_freq(ac, am->buf_size - am->total, &val);
457 } else {
458 freq = 1;
459 for (dst[0] = 0; dst[0] < size; dst[0]++) {
460 if (!am->prob[1][dst[0]])
461 freq++;
462 }
463 ac_get_freq(ac, freq, &val);
464 }
465 freq = 0;
466 dst[0] = 0;
467 if (val > 0 && am->buf_size > 0) {
468 for (dst[0] = 0; dst[0] < size & freq < val; dst[0]++) {
469 if (!am->prob[1][dst[0]])
470 freq++;
471 }
472 }
473 if (am->prob[1][dst[0]]) {
474 do {
475 val = dst[0]++;
476 } while (val + 1 < am->buf_size && am->prob[1][val + 1]);
477 }
478 ac_update(ac, freq, 1);
479 am->prob[1][dst[0]]++;
480 am->total++;
481 amdl_update_prob(am, dst[0], 1);
482 am->last = dst[0];
483
484 return 0;
485}
486
487static int decode_filt_coeffs(RKAContext *s, ChContext *ctx, ACoder *ac, FiltCoeffs *dst)
488{
489 unsigned val, bits;
490 int idx = 0;
491
492 if (amdl_decode_int(ctx->filt_size, ac, &dst->size, 256) < 0)
493 return -1;
494
495 if (dst->size == 0)
496 return 0;
497
498 if (amdl_decode_int(ctx->filt_bits, ac, &bits, 10) < 0)
499 return -1;
500
501 do {
502 if (((idx == 8) || (idx == 20)) && (0 < bits))
503 bits--;
504
505 if (bits > 10)
506 return -1;
507
508 if (amdl_decode_int(&ctx->coeff_bits[bits], ac, &val, 31) < 0)
509 return -1;
510
511 if (val == 31) {
512 ac_get_freq(ac, 65536, &val);
513 ac_update(ac, val, 1);
514 }
515
516 if (val == 0) {
517 dst->coeffs[idx++] = 0;
518 } else {
519 unsigned freq = 0;
520 int sign;
521
522 if (bits > 0) {
523 ac_get_freq(ac, 1 << bits, &freq);
524 ac_update(ac, freq, 1);
525 }
526 dst->coeffs[idx] = freq + 1 + ((val - 1U) << bits);
527 sign = decode_bool(ac, ctx, idx);
528 if (sign < 0)
529 return -1;
530 if (sign == 1)
531 dst->coeffs[idx] = -dst->coeffs[idx];
532 idx++;
533 }
534 } while (idx < dst->size);
535
536 return 0;
537}
538
539static int ac_dec_bit(ACoder *ac)
540{
541 uint32_t high, low;
542
543 low = ac->low;
544 ac->high = high = ac->high >> 1;
545 if (ac->value - low < high) {
546 do {
547 if (((high + low) ^ low) > 0xffffff) {
548 if (high > 0xffff)
549 return 0;
Paul B Mahol7c41a082023-02-13 17:21:56550 ac->high = (uint16_t)-(int16_t)low;
Paul B Maholb8c08022023-01-31 20:03:38551 }
552
553 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
554 break;
555
556 ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb);
557 ac->high = high = ac->high << 8;
558 ac->low = low = ac->low << 8;
559 } while (1);
560
561 return -1;
562 }
563 ac->low = low = low + high;
564 do {
565 if (((high + low) ^ low) > 0xffffff) {
566 if (high > 0xffff)
567 return 1;
Paul B Mahol7c41a082023-02-13 17:21:56568 ac->high = (uint16_t)-(int16_t)low;
Paul B Maholb8c08022023-01-31 20:03:38569 }
570
571 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
572 break;
573
574 ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb);
575 ac->high = high = ac->high << 8;
576 ac->low = low = ac->low << 8;
577 } while (1);
578
579 return -1;
580}
581
582static int mdl64_decode(ACoder *ac, Model64 *ctx, int *dst)
583{
584 int sign, idx, bits;
585 unsigned val = 0;
586
Paul B Mahol7c41a082023-02-13 17:21:56587 if (ctx->zero[0] + ctx->zero[1] > 4000U) {
Paul B Maholb8c08022023-01-31 20:03:38588 ctx->zero[0] = (ctx->zero[0] >> 1) + 1;
589 ctx->zero[1] = (ctx->zero[1] >> 1) + 1;
590 }
Paul B Mahol7c41a082023-02-13 17:21:56591 if (ctx->sign[0] + ctx->sign[1] > 4000U) {
Paul B Maholb8c08022023-01-31 20:03:38592 ctx->sign[0] = (ctx->sign[0] >> 1) + 1;
593 ctx->sign[1] = (ctx->sign[1] >> 1) + 1;
594 }
595 sign = ac_decode_bool(ac, ctx->zero[0], ctx->zero[1]);
596 if (sign == 0) {
597 ctx->zero[0] += 2;
598 dst[0] = 0;
599 return 0;
Paul B Mahol7c41a082023-02-13 17:21:56600 } else if (sign < 0) {
Paul B Maholb8c08022023-01-31 20:03:38601 return -1;
Paul B Mahol7c41a082023-02-13 17:21:56602 }
Paul B Maholb8c08022023-01-31 20:03:38603
604 ctx->zero[1] += 2;
605 sign = ac_decode_bool(ac, ctx->sign[0], ctx->sign[1]);
606 if (sign < 0)
607 return -1;
608 ctx->sign[sign]++;
609 bits = ctx->bits;
610 if (bits > 0) {
611 if (bits < 13) {
612 ac_get_freq(ac, 1 << bits, &val);
613 ac_update(ac, val, 1);
614 } else {
Paul B Mahol7c41a082023-02-13 17:21:56615 int hbits = bits / 2;
616 ac_get_freq(ac, 1 << hbits, &val);
Paul B Maholb8c08022023-01-31 20:03:38617 ac_update(ac, val, 1);
Paul B Mahol7c41a082023-02-13 17:21:56618 ac_get_freq(ac, 1 << (ctx->bits - (hbits)), &bits);
Paul B Maholb8c08022023-01-31 20:03:38619 ac_update(ac, val, 1);
Paul B Mahol7c41a082023-02-13 17:21:56620 val += (bits << hbits);
Paul B Maholb8c08022023-01-31 20:03:38621 }
622 }
623 bits = ctx->size;
624 idx = 0;
625 if (bits >= 0) {
626 do {
627 uint16_t *val4 = ctx->val4;
628 int b;
629
630 if (val4[idx] + ctx->val1[idx] > 2000U) {
631 val4[idx] = (val4[idx] >> 1) + 1;
632 ctx->val1[idx] = (ctx->val1[idx] >> 1) + 1;
633 }
634 b = ac_decode_bool(ac, ctx->val4[idx], ctx->val1[idx]);
635 if (b == 1) {
636 ctx->val1[idx] += 4;
637 break;
638 } else if (b < 0) {
639 return -1;
640 }
641 ctx->val4[idx] += 4;
642 idx++;
643 } while (idx <= ctx->size);
644 bits = ctx->size;
645 if (idx <= bits) {
646 dst[0] = val + 1 + (idx << ctx->bits);
647 if (sign)
648 dst[0] = -dst[0];
649 return 0;
650 }
651 }
Paul B Mahol7c41a082023-02-13 17:21:56652 bits++;
Paul B Maholb8c08022023-01-31 20:03:38653 while (ac_dec_bit(ac) == 0)
Paul B Mahol7c41a082023-02-13 17:21:56654 bits += 64;
Paul B Maholb8c08022023-01-31 20:03:38655 ac_get_freq(ac, 64, &idx);
656 ac_update(ac, idx, 1);
657 idx += bits;
Paul B Mahol7c41a082023-02-13 17:21:56658 dst[0] = val + 1 + (idx << ctx->bits);
Paul B Maholb8c08022023-01-31 20:03:38659 if (sign)
660 dst[0] = -dst[0];
661
662 return 0;
663}
664
Paul B Maholb5534b92023-02-14 08:46:46665static const uint8_t tab[16] = {
666 0, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
667};
Paul B Maholb8c08022023-01-31 20:03:38668
669static int decode_filter(RKAContext *s, ChContext *ctx, ACoder *ac, int off, unsigned size)
670{
671 FiltCoeffs filt;
672 Model64 *mdl64;
673 int m = 0, split, val, last_val = 0, ret;
674 unsigned idx = 3, bits = 0;
675
676 if (ctx->cmode == 0) {
677 if (amdl_decode_int(&ctx->fshift, ac, &bits, 15) < 0)
678 return -1;
679 bits &= 31U;
680 }
681
682 ret = decode_filt_coeffs(s, ctx, ac, &filt);
683 if (ret < 0)
684 return ret;
685
686 if (size < 512)
687 split = size / 2;
688 else
689 split = size >> 4;
690
691 if (size <= 0)
692 return 0;
693
694 for (int x = 0; x < size;) {
695 if (amdl_decode_int(&ctx->position, ac, &idx, 10) < 0)
696 return -1;
697
698 idx = (ctx->pos_idx + idx) % 11;
699 ctx->pos_idx = idx;
700
Paul B Mahol7c41a082023-02-13 17:21:56701 for (int y = 0; y < FFMIN(split, size - x); y++, off++) {
Paul B Maholb8c08022023-01-31 20:03:38702 int midx, shift = idx, *src, sum = 16;
703
Paul B Maholcb350362023-02-15 12:54:11704 if (off >= FF_ARRAY_ELEMS(ctx->buf0))
705 return -1;
706
Paul B Maholb8c08022023-01-31 20:03:38707 midx = FFABS(last_val) >> shift;
708 if (midx >= 15) {
709 mdl64 = &ctx->mdl64[3][idx];
710 } else if (midx >= 7) {
711 mdl64 = &ctx->mdl64[2][idx];
712 } else if (midx >= 4) {
713 mdl64 = &ctx->mdl64[1][idx];
714 } else {
715 mdl64 = &ctx->mdl64[0][idx];
716 }
717 ret = mdl64_decode(ac, mdl64, &val);
718 if (ret < 0)
719 return -1;
720 last_val = val;
721 src = &ctx->buf1[off + -1];
722 for (int i = 0; i < filt.size && i < 15; i++)
723 sum += filt.coeffs[i] * src[-i];
724 sum = sum * 2;
725 for (int i = 15; i < filt.size; i++)
726 sum += filt.coeffs[i] * src[-i];
727 sum = sum >> 6;
728 if (ctx->cmode == 0) {
729 if (bits == 0) {
730 ctx->buf1[off] = sum + val;
731 } else {
Paul B Mahol7c41a082023-02-13 17:21:56732 ctx->buf1[off] = (val + (sum >> bits) << bits) +
Paul B Maholb8c08022023-01-31 20:03:38733 (((1U << bits) - 1U) & ctx->buf1[off + -1]);
734 }
735 ctx->buf0[off] = ctx->buf1[off] + ctx->buf0[off + -1];
736 } else {
Paul B Maholb5534b92023-02-14 08:46:46737 val <<= ctx->cmode;
Paul B Maholb8c08022023-01-31 20:03:38738 sum += ctx->buf0[off + -1] + val;
739 switch (s->bps) {
Paul B Mahol7c41a082023-02-13 17:21:56740 case 16: sum = av_clip_int16(sum); break;
741 case 8: sum = av_clip_int8(sum); break;
Paul B Maholb8c08022023-01-31 20:03:38742 }
743 ctx->buf1[off] = sum - ctx->buf0[off + -1];
744 ctx->buf0[off] = sum;
745 m += FFABS(ctx->buf1[off]);
746 }
747 }
748 if (ctx->cmode2 != 0) {
749 int sum = 0;
750 for (int i = (m << 6) / split; i > 0; i = i >> 1)
751 sum++;
752 sum = sum - (ctx->cmode2 + 7);
753 ctx->cmode = FFMAX(sum, tab[ctx->cmode2]);
754 }
755
756 x += split;
757 }
758
759 return 0;
760}
761
762static int decode_samples(AVCodecContext *avctx, ACoder *ac, ChContext *ctx, int offset)
763{
764 RKAContext *s = avctx->priv_data;
765 int segment_size, offset2, mode, ret;
766
767 ret = amdl_decode_int(&ctx->nb_segments, ac, &mode, 5);
768 if (ret < 0)
769 return ret;
770
771 if (mode == 5) {
772 ret = ac_get_freq(ac, ctx->srate_pad >> 2, &segment_size);
773 if (ret < 0)
774 return ret;
775 ac_update(ac, segment_size, 1);
776 segment_size *= 4;
Paul B Mahol7c41a082023-02-13 17:21:56777 ret = decode_filter(s, ctx, ac, offset, segment_size);
778 if (ret < 0)
779 return ret;
Paul B Maholb8c08022023-01-31 20:03:38780 } else {
781 segment_size = ctx->srate_pad;
782
783 if (mode) {
784 if (mode > 2) {
Paul B Mahol7c41a082023-02-13 17:21:56785 ret = decode_filter(s, ctx, ac, offset, segment_size / 4);
786 if (ret < 0)
787 return ret;
Paul B Maholb8c08022023-01-31 20:03:38788 offset2 = segment_size / 4 + offset;
Paul B Mahol7c41a082023-02-13 17:21:56789 ret = decode_filter(s, ctx, ac, offset2, segment_size / 4);
790 if (ret < 0)
791 return ret;
Paul B Maholb8c08022023-01-31 20:03:38792 offset2 = segment_size / 4 + offset2;
793 } else {
Paul B Mahol7c41a082023-02-13 17:21:56794 ret = decode_filter(s, ctx, ac, offset, segment_size / 2);
795 if (ret < 0)
796 return ret;
Paul B Maholb8c08022023-01-31 20:03:38797 offset2 = segment_size / 2 + offset;
798 }
799 if (mode & 1) {
Paul B Mahol7c41a082023-02-13 17:21:56800 ret = decode_filter(s, ctx, ac, offset2, segment_size / 2);
801 if (ret < 0)
802 return ret;
Paul B Maholb8c08022023-01-31 20:03:38803 } else {
Paul B Mahol7c41a082023-02-13 17:21:56804 ret = decode_filter(s, ctx, ac, offset2, segment_size / 4);
805 if (ret < 0)
806 return ret;
807 ret = decode_filter(s, ctx, ac, segment_size / 4 + offset2, segment_size / 4);
808 if (ret < 0)
809 return ret;
Paul B Maholb8c08022023-01-31 20:03:38810 }
811 } else {
Paul B Mahol7c41a082023-02-13 17:21:56812 ret = decode_filter(s, ctx, ac, offset, ctx->srate_pad);
813 if (ret < 0)
814 return ret;
Paul B Maholb8c08022023-01-31 20:03:38815 }
816 }
817
818 return segment_size;
819}
820
821static int decode_ch_samples(AVCodecContext *avctx, ChContext *c)
822{
823 RKAContext *s = avctx->priv_data;
824 ACoder *ac = &s->ac;
825 int nb_decoded = 0;
826
827 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
828 return 0;
829
830 memmove(c->buf0, &c->buf0[c->last_nb_decoded], 2560 * sizeof(*c->buf0));
831 memmove(c->buf1, &c->buf1[c->last_nb_decoded], 2560 * sizeof(*c->buf1));
832
833 nb_decoded = decode_samples(avctx, ac, c, 2560);
834 if (nb_decoded < 0)
835 return nb_decoded;
836 c->last_nb_decoded = nb_decoded;
837
838 return nb_decoded;
839}
840
841static int rka_decode_frame(AVCodecContext *avctx, AVFrame *frame,
842 int *got_frame_ptr, AVPacket *avpkt)
843{
844 RKAContext *s = avctx->priv_data;
845 ACoder *ac = &s->ac;
846 int ret;
847
848 bytestream2_init(&ac->gb, avpkt->data, avpkt->size);
849 init_acoder(ac);
850
851 for (int ch = 0; ch < s->channels; ch++) {
852 ret = chctx_init(s, &s->ch[ch], avctx->sample_rate,
853 avctx->bits_per_raw_sample);
854 if (ret < 0)
855 return ret;
856 }
857
858 frame->nb_samples = s->frame_samples;
859 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
860 return ret;
861
862 if (s->channels == 2) {
863 int16_t *l16 = (int16_t *)frame->extended_data[0];
864 int16_t *r16 = (int16_t *)frame->extended_data[1];
Paul B Mahol052674f2023-02-13 20:03:13865 uint8_t *l8 = frame->extended_data[0];
866 uint8_t *r8 = frame->extended_data[1];
Paul B Maholb8c08022023-01-31 20:03:38867
Paul B Mahol052674f2023-02-13 20:03:13868 for (int n = 0; n < frame->nb_samples;) {
869 ret = decode_ch_samples(avctx, &s->ch[0]);
870 if (ret == 0) {
871 frame->nb_samples = n;
872 break;
873 }
874 if (ret < 0 || n + ret > frame->nb_samples)
875 return AVERROR_INVALIDDATA;
Paul B Maholb8c08022023-01-31 20:03:38876
Paul B Mahol052674f2023-02-13 20:03:13877 ret = decode_ch_samples(avctx, &s->ch[1]);
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;
Paul B Maholb8c08022023-01-31 20:03:38884
Paul B Mahol052674f2023-02-13 20:03:13885 switch (avctx->sample_fmt) {
886 case AV_SAMPLE_FMT_S16P:
Paul B Maholb8c08022023-01-31 20:03:38887 for (int i = 0; i < ret; i++) {
888 int l = s->ch[0].buf0[2560 + i];
889 int r = s->ch[1].buf0[2560 + i];
890
891 l16[n + i] = (l * 2 + r + 1) >> 1;
892 r16[n + i] = (l * 2 - r + 1) >> 1;
893 }
Paul B Mahol052674f2023-02-13 20:03:13894 break;
895 case AV_SAMPLE_FMT_U8P:
896 for (int i = 0; i < ret; i++) {
897 int l = s->ch[0].buf0[2560 + i];
898 int r = s->ch[1].buf0[2560 + i];
Paul B Maholb8c08022023-01-31 20:03:38899
Paul B Mahol052674f2023-02-13 20:03:13900 l8[n + i] = ((l * 2 + r + 1) >> 1) + 0x7f;
901 r8[n + i] = ((l * 2 - r + 1) >> 1) + 0x7f;
902 }
903 break;
904 default:
905 return AVERROR_INVALIDDATA;
Paul B Maholb8c08022023-01-31 20:03:38906 }
Paul B Mahol052674f2023-02-13 20:03:13907
908 n += ret;
Paul B Maholb8c08022023-01-31 20:03:38909 }
910 } else {
911 int16_t *m16 = (int16_t *)frame->data[0];
Paul B Mahol052674f2023-02-13 20:03:13912 uint8_t *m8 = frame->data[0];
Paul B Maholb8c08022023-01-31 20:03:38913
Paul B Mahol052674f2023-02-13 20:03:13914 for (int n = 0; n < frame->nb_samples;) {
915 ret = decode_ch_samples(avctx, &s->ch[0]);
916 if (ret == 0) {
917 frame->nb_samples = n;
918 break;
919 }
920 if (ret < 0 || n + ret > frame->nb_samples)
921 return AVERROR_INVALIDDATA;
Paul B Maholb8c08022023-01-31 20:03:38922
Paul B Mahol052674f2023-02-13 20:03:13923 switch (avctx->sample_fmt) {
924 case AV_SAMPLE_FMT_S16P:
Paul B Maholb8c08022023-01-31 20:03:38925 for (int i = 0; i < ret; i++) {
926 int m = s->ch[0].buf0[2560 + i];
927
928 m16[n + i] = m;
929 }
Paul B Mahol052674f2023-02-13 20:03:13930 break;
931 case AV_SAMPLE_FMT_U8P:
932 for (int i = 0; i < ret; i++) {
933 int m = s->ch[0].buf0[2560 + i];
Paul B Maholb8c08022023-01-31 20:03:38934
Paul B Mahol052674f2023-02-13 20:03:13935 m8[n + i] = m + 0x7f;
936 }
937 break;
938 default:
939 return AVERROR_INVALIDDATA;
Paul B Maholb8c08022023-01-31 20:03:38940 }
Paul B Mahol052674f2023-02-13 20:03:13941
942 n += ret;
Paul B Maholb8c08022023-01-31 20:03:38943 }
944 }
945
946 *got_frame_ptr = 1;
947
948 return avpkt->size;
949}
950
951static av_cold int rka_decode_close(AVCodecContext *avctx)
952{
953 RKAContext *s = avctx->priv_data;
954
955 for (int ch = 0; ch < 2; ch++) {
956 ChContext *c = &s->ch[ch];
957
958 for (int i = 0; i < 11; i++)
959 adaptive_model_free(&c->coeff_bits[i]);
960
961 adaptive_model_free(&c->position);
962 adaptive_model_free(&c->nb_segments);
963 adaptive_model_free(&c->fshift);
964 }
965
966 adaptive_model_free(&s->filt_size);
967 adaptive_model_free(&s->filt_bits);
968
969 return 0;
970}
971
972const FFCodec ff_rka_decoder = {
973 .p.name = "rka",
974 CODEC_LONG_NAME("RKA (RK Audio"),
975 .p.type = AVMEDIA_TYPE_AUDIO,
976 .p.id = AV_CODEC_ID_RKA,
977 .priv_data_size = sizeof(RKAContext),
978 .init = rka_decode_init,
979 .close = rka_decode_close,
980 FF_CODEC_DECODE_CB(rka_decode_frame),
981 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
982 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
983};