blob: 600c16002877137ecd5ea83b5319c0de17324260 [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"
Andreas Rheinhardt790f7932024-03-25 00:30:3724#include "libavutil/mem.h"
Paul B Maholb8c08022023-01-31 20:03:3825
26#include "avcodec.h"
27#include "codec_internal.h"
28#include "bytestream.h"
29#include "decode.h"
30
31typedef struct ACoder {
32 GetByteContext gb;
33 uint32_t low, high;
34 uint32_t value;
35} ACoder;
36
37typedef struct FiltCoeffs {
38 int32_t coeffs[257];
39 unsigned size;
40} FiltCoeffs;
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;
55 int buf_size;
56 int16_t sum;
57 uint16_t aprob0;
58 uint16_t aprob1;
59 uint16_t *prob[2];
60} AdaptiveModel;
61
62typedef struct ChContext {
Paul B Mahol8bfadac2023-09-30 11:22:3263 int qfactor;
64 int vrq;
Paul B Maholb8c08022023-01-31 20:03:3865 int last_nb_decoded;
66 unsigned srate_pad;
67 unsigned pos_idx;
68
69 AdaptiveModel *filt_size;
70 AdaptiveModel *filt_bits;
71
Paul B Mahol7c41a082023-02-13 17:21:5672 uint32_t *bprob[2];
Paul B Maholb8c08022023-01-31 20:03:3873
74 AdaptiveModel position;
75 AdaptiveModel fshift;
76 AdaptiveModel nb_segments;
77 AdaptiveModel coeff_bits[11];
78
79 Model64 mdl64[4][11];
80
Paul B Maholcb350362023-02-15 12:54:1181 int32_t buf0[131072+2560];
82 int32_t buf1[131072+2560];
Paul B Maholb8c08022023-01-31 20:03:3883} ChContext;
84
85typedef struct RKAContext {
86 AVClass *class;
87
88 ACoder ac;
89 ChContext ch[2];
90
91 int bps;
92 int align;
93 int channels;
Paul B Mahol83a20072023-02-15 13:08:2894 int correlated;
Paul B Maholb8c08022023-01-31 20:03:3895 int frame_samples;
96 int last_nb_samples;
97 uint32_t total_nb_samples;
98 uint32_t samples_left;
99
Paul B Mahol7c41a082023-02-13 17:21:56100 uint32_t bprob[2][257];
Paul B Maholb8c08022023-01-31 20:03:38101
102 AdaptiveModel filt_size;
103 AdaptiveModel filt_bits;
104} RKAContext;
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
126static void adaptive_model_free(AdaptiveModel *am)
127{
128 av_freep(&am->prob[0]);
129 av_freep(&am->prob[1]);
130}
131
132static av_cold int rka_decode_init(AVCodecContext *avctx)
133{
134 RKAContext *s = avctx->priv_data;
Paul B Mahol8bfadac2023-09-30 11:22:32135 int qfactor;
Paul B Maholb8c08022023-01-31 20:03:38136
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:
147 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
148 break;
149 default:
150 return AVERROR_INVALIDDATA;
151 }
152
Paul B Mahol05b859a2023-02-15 13:11:16153 av_channel_layout_uninit(&avctx->ch_layout);
154 s->channels = avctx->ch_layout.nb_channels = avctx->extradata[12];
Paul B Maholb8c08022023-01-31 20:03:38155 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;
Paul B Mahol83a20072023-02-15 13:08:28162 s->correlated = avctx->extradata[15] & 1;
Paul B Maholb8c08022023-01-31 20:03:38163
Paul B Mahol8bfadac2023-09-30 11:22:32164 qfactor = avctx->extradata[14] & 0xf;
Paul B Maholb8c08022023-01-31 20:03:38165 if ((avctx->extradata[15] & 4) != 0)
Paul B Mahol8bfadac2023-09-30 11:22:32166 qfactor = -qfactor;
Paul B Maholb8c08022023-01-31 20:03:38167
Paul B Mahol8bfadac2023-09-30 11:22:32168 s->ch[0].qfactor = s->ch[1].qfactor = qfactor < 0 ? 2 : qfactor;
Paul B Maholead14262023-09-30 13:01:38169 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 }
Paul B Mahol8bfadac2023-09-30 11:22:32175 av_log(avctx, AV_LOG_DEBUG, "qfactor: %d\n", qfactor);
Paul B Maholb8c08022023-01-31 20:03:38176
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
201static int chctx_init(RKAContext *s, ChContext *c,
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
Paul B Mahol7c41a082023-02-13 17:21:56212 c->bprob[0] = s->bprob[0];
213 c->bprob[1] = s->bprob[1];
Paul B Maholb8c08022023-01-31 20:03:38214
Michael Niedermayercbe5e482023-03-04 22:58:08215 c->srate_pad = ((int64_t)sample_rate << 13) / 44100 & 0xFFFFFFFCU;
Paul B Maholb8c08022023-01-31 20:03:38216 c->pos_idx = 1;
217
Paul B Mahol7c41a082023-02-13 17:21:56218 for (int i = 0; i < FF_ARRAY_ELEMS(s->bprob[0]); i++)
219 c->bprob[0][i] = c->bprob[1][i] = 1;
Paul B Maholb8c08022023-01-31 20:03:38220
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{
Paul B Mahol7c41a082023-02-13 17:21:56256 unsigned help, add, high, value;
257 int low;
Paul B Maholb8c08022023-01-31 20:03:38258
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;
Paul B Mahol7c41a082023-02-13 17:21:56279 low = ac->low = ac->low << 8;
Paul B Maholb8c08022023-01-31 20:03:38280 }
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;
Paul B Mahol7c41a082023-02-13 17:21:56296 low = ac->low = ac->low << 8;
Paul B Maholb8c08022023-01-31 20:03:38297 }
298 return -1;
299}
300
301static int decode_bool(ACoder *ac, ChContext *c, int idx)
302{
Paul B Mahol7c41a082023-02-13 17:21:56303 uint32_t x;
304 int b;
Paul B Maholb8c08022023-01-31 20:03:38305
Paul B Mahol7c41a082023-02-13 17:21:56306 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;
Paul B Maholb8c08022023-01-31 20:03:38310 }
311
Paul B Mahol7c41a082023-02-13 17:21:56312 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]++;
Paul B Maholb8c08022023-01-31 20:03:38317
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;
Paul B Mahol7c41a082023-02-13 17:21:56350 ac->high = (uint16_t)-(int16_t)low;
Paul B Maholb8c08022023-01-31 20:03:38351 }
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
377static void update_ch_subobj(AdaptiveModel *am)
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{
Paul B Mahol7c41a082023-02-13 17:21:56412 unsigned freq, size2, val, mul;
413 int j;
Paul B Maholb8c08022023-01-31 20:03:38414
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 }
Paul B Mahol7c41a082023-02-13 17:21:56446 freq -= j;
Paul B Maholb8c08022023-01-31 20:03:38447 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
495static int decode_filt_coeffs(RKAContext *s, ChContext *ctx, ACoder *ac, FiltCoeffs *dst)
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
547static 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;
Paul B Mahol7c41a082023-02-13 17:21:56558 ac->high = (uint16_t)-(int16_t)low;
Paul B Maholb8c08022023-01-31 20:03:38559 }
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;
Paul B Mahol7c41a082023-02-13 17:21:56576 ac->high = (uint16_t)-(int16_t)low;
Paul B Maholb8c08022023-01-31 20:03:38577 }
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
590static int mdl64_decode(ACoder *ac, Model64 *ctx, int *dst)
591{
592 int sign, idx, bits;
593 unsigned val = 0;
594
Paul B Mahol7c41a082023-02-13 17:21:56595 if (ctx->zero[0] + ctx->zero[1] > 4000U) {
Paul B Maholb8c08022023-01-31 20:03:38596 ctx->zero[0] = (ctx->zero[0] >> 1) + 1;
597 ctx->zero[1] = (ctx->zero[1] >> 1) + 1;
598 }
Paul B Mahol7c41a082023-02-13 17:21:56599 if (ctx->sign[0] + ctx->sign[1] > 4000U) {
Paul B Maholb8c08022023-01-31 20:03:38600 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;
Paul B Mahol7c41a082023-02-13 17:21:56608 } else if (sign < 0) {
Paul B Maholb8c08022023-01-31 20:03:38609 return -1;
Paul B Mahol7c41a082023-02-13 17:21:56610 }
Paul B Maholb8c08022023-01-31 20:03:38611
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 {
Paul B Mahol7c41a082023-02-13 17:21:56623 int hbits = bits / 2;
624 ac_get_freq(ac, 1 << hbits, &val);
Paul B Maholb8c08022023-01-31 20:03:38625 ac_update(ac, val, 1);
Paul B Mahol7c41a082023-02-13 17:21:56626 ac_get_freq(ac, 1 << (ctx->bits - (hbits)), &bits);
Paul B Maholb8c08022023-01-31 20:03:38627 ac_update(ac, val, 1);
Paul B Mahol7c41a082023-02-13 17:21:56628 val += (bits << hbits);
Paul B Maholb8c08022023-01-31 20:03:38629 }
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 }
Paul B Mahol7c41a082023-02-13 17:21:56660 bits++;
Paul B Maholb8c08022023-01-31 20:03:38661 while (ac_dec_bit(ac) == 0)
Paul B Mahol7c41a082023-02-13 17:21:56662 bits += 64;
Paul B Maholb8c08022023-01-31 20:03:38663 ac_get_freq(ac, 64, &idx);
664 ac_update(ac, idx, 1);
665 idx += bits;
Paul B Mahol7c41a082023-02-13 17:21:56666 dst[0] = val + 1 + (idx << ctx->bits);
Paul B Maholb8c08022023-01-31 20:03:38667 if (sign)
668 dst[0] = -dst[0];
669
670 return 0;
671}
672
Paul B Maholead14262023-09-30 13:01:38673static const uint8_t vrq_qfactors[8] = { 3, 3, 2, 2, 1, 1, 1, 1 };
Paul B Maholb8c08022023-01-31 20:03:38674
675static int decode_filter(RKAContext *s, ChContext *ctx, ACoder *ac, int off, unsigned size)
676{
677 FiltCoeffs filt;
678 Model64 *mdl64;
Paul B Maholead14262023-09-30 13:01:38679 int split, val, last_val = 0, ret;
680 unsigned rsize, idx = 3, bits = 0, m = 0;
Paul B Maholb8c08022023-01-31 20:03:38681
Paul B Mahol8bfadac2023-09-30 11:22:32682 if (ctx->qfactor == 0) {
Paul B Maholb8c08022023-01-31 20:03:38683 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
Michael Niedermayer8874cfa2023-02-20 19:00:44697 if (size <= 1)
Paul B Maholb8c08022023-01-31 20:03:38698 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
Paul B Maholead14262023-09-30 13:01:38704 m = 0;
Paul B Maholb8c08022023-01-31 20:03:38705 idx = (ctx->pos_idx + idx) % 11;
706 ctx->pos_idx = idx;
707
Paul B Maholead14262023-09-30 13:01:38708 rsize = FFMIN(split, size - x);
709 for (int y = 0; y < rsize; y++, off++) {
Paul B Maholb8c08022023-01-31 20:03:38710 int midx, shift = idx, *src, sum = 16;
711
Paul B Maholcb350362023-02-15 12:54:11712 if (off >= FF_ARRAY_ELEMS(ctx->buf0))
713 return -1;
714
Paul B Maholb8c08022023-01-31 20:03:38715 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++)
Michael Niedermayerd4957472023-02-20 18:53:19731 sum += filt.coeffs[i] * (unsigned)src[-i];
Michael Niedermayera02e45a2023-02-20 19:09:36732 sum = sum * 2U;
Paul B Maholb8c08022023-01-31 20:03:38733 for (int i = 15; i < filt.size; i++)
Michael Niedermayerd4957472023-02-20 18:53:19734 sum += filt.coeffs[i] * (unsigned)src[-i];
Paul B Maholb8c08022023-01-31 20:03:38735 sum = sum >> 6;
Paul B Mahol8bfadac2023-09-30 11:22:32736 if (ctx->qfactor == 0) {
Paul B Maholb8c08022023-01-31 20:03:38737 if (bits == 0) {
738 ctx->buf1[off] = sum + val;
739 } else {
Michael Niedermayerb168aeb2023-05-01 23:09:58740 ctx->buf1[off] = (val + (sum >> bits)) * (1U << bits) +
Paul B Maholb8c08022023-01-31 20:03:38741 (((1U << bits) - 1U) & ctx->buf1[off + -1]);
742 }
Michael Niedermayere7362382023-06-08 13:05:50743 ctx->buf0[off] = ctx->buf1[off] + (unsigned)ctx->buf0[off + -1];
Paul B Maholb8c08022023-01-31 20:03:38744 } else {
Paul B Mahol8bfadac2023-09-30 11:22:32745 val *= 1U << ctx->qfactor;
Michael Niedermayere7362382023-06-08 13:05:50746 sum += ctx->buf0[off + -1] + (unsigned)val;
Paul B Maholb8c08022023-01-31 20:03:38747 switch (s->bps) {
Paul B Mahol7c41a082023-02-13 17:21:56748 case 16: sum = av_clip_int16(sum); break;
749 case 8: sum = av_clip_int8(sum); break;
Paul B Maholb8c08022023-01-31 20:03:38750 }
751 ctx->buf1[off] = sum - ctx->buf0[off + -1];
752 ctx->buf0[off] = sum;
Michael Niedermayerf9b29452023-06-29 23:24:20753 m += (unsigned)FFABS(ctx->buf1[off]);
Paul B Maholb8c08022023-01-31 20:03:38754 }
755 }
Paul B Mahol8bfadac2023-09-30 11:22:32756 if (ctx->vrq != 0) {
Paul B Maholb8c08022023-01-31 20:03:38757 int sum = 0;
Paul B Maholead14262023-09-30 13:01:38758 for (unsigned i = (m << 6) / rsize; i > 0; i = i >> 1)
Paul B Maholb8c08022023-01-31 20:03:38759 sum++;
Paul B Maholead14262023-09-30 13:01:38760 sum -= (ctx->vrq + 7);
761 ctx->qfactor = FFMAX(sum, vrq_qfactors[ctx->vrq - 1]);
Paul B Maholb8c08022023-01-31 20:03:38762 }
763
764 x += split;
765 }
766
767 return 0;
768}
769
770static 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;
Paul B Mahol7c41a082023-02-13 17:21:56785 ret = decode_filter(s, ctx, ac, offset, segment_size);
786 if (ret < 0)
787 return ret;
Paul B Maholb8c08022023-01-31 20:03:38788 } else {
789 segment_size = ctx->srate_pad;
790
791 if (mode) {
792 if (mode > 2) {
Paul B Mahol7c41a082023-02-13 17:21:56793 ret = decode_filter(s, ctx, ac, offset, segment_size / 4);
794 if (ret < 0)
795 return ret;
Paul B Maholb8c08022023-01-31 20:03:38796 offset2 = segment_size / 4 + offset;
Paul B Mahol7c41a082023-02-13 17:21:56797 ret = decode_filter(s, ctx, ac, offset2, segment_size / 4);
798 if (ret < 0)
799 return ret;
Paul B Maholb8c08022023-01-31 20:03:38800 offset2 = segment_size / 4 + offset2;
801 } else {
Paul B Mahol7c41a082023-02-13 17:21:56802 ret = decode_filter(s, ctx, ac, offset, segment_size / 2);
803 if (ret < 0)
804 return ret;
Paul B Maholb8c08022023-01-31 20:03:38805 offset2 = segment_size / 2 + offset;
806 }
807 if (mode & 1) {
Paul B Mahol7c41a082023-02-13 17:21:56808 ret = decode_filter(s, ctx, ac, offset2, segment_size / 2);
809 if (ret < 0)
810 return ret;
Paul B Maholb8c08022023-01-31 20:03:38811 } else {
Paul B Mahol7c41a082023-02-13 17:21:56812 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;
Paul B Maholb8c08022023-01-31 20:03:38818 }
819 } else {
Paul B Mahol7c41a082023-02-13 17:21:56820 ret = decode_filter(s, ctx, ac, offset, ctx->srate_pad);
821 if (ret < 0)
822 return ret;
Paul B Maholb8c08022023-01-31 20:03:38823 }
824 }
825
826 return segment_size;
827}
828
829static int decode_ch_samples(AVCodecContext *avctx, ChContext *c)
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
849static int rka_decode_frame(AVCodecContext *avctx, AVFrame *frame,
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
Paul B Mahol83a20072023-02-15 13:08:28870 if (s->channels == 2 && s->correlated) {
Paul B Maholb8c08022023-01-31 20:03:38871 int16_t *l16 = (int16_t *)frame->extended_data[0];
872 int16_t *r16 = (int16_t *)frame->extended_data[1];
Paul B Mahol052674f2023-02-13 20:03:13873 uint8_t *l8 = frame->extended_data[0];
874 uint8_t *r8 = frame->extended_data[1];
Paul B Maholb8c08022023-01-31 20:03:38875
Paul B Mahol052674f2023-02-13 20:03:13876 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;
Paul B Maholb8c08022023-01-31 20:03:38884
Paul B Mahol052674f2023-02-13 20:03:13885 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;
Paul B Maholb8c08022023-01-31 20:03:38892
Paul B Mahol052674f2023-02-13 20:03:13893 switch (avctx->sample_fmt) {
894 case AV_SAMPLE_FMT_S16P:
Paul B Maholb8c08022023-01-31 20:03:38895 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 }
Paul B Mahol052674f2023-02-13 20:03:13902 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];
Paul B Maholb8c08022023-01-31 20:03:38907
Paul B Mahol052674f2023-02-13 20:03:13908 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;
Paul B Maholb8c08022023-01-31 20:03:38914 }
Paul B Mahol052674f2023-02-13 20:03:13915
916 n += ret;
Paul B Maholb8c08022023-01-31 20:03:38917 }
918 } else {
Paul B Mahol052674f2023-02-13 20:03:13919 for (int n = 0; n < frame->nb_samples;) {
Paul B Mahol83a20072023-02-15 13:08:28920 for (int ch = 0; ch < s->channels; ch++) {
921 int16_t *m16 = (int16_t *)frame->data[ch];
922 uint8_t *m8 = frame->data[ch];
Paul B Maholb8c08022023-01-31 20:03:38923
Paul B Mahol83a20072023-02-15 13:08:28924 ret = decode_ch_samples(avctx, &s->ch[ch]);
925 if (ret == 0) {
926 frame->nb_samples = n;
927 break;
Paul B Maholb8c08022023-01-31 20:03:38928 }
929
Paul B Mahol83a20072023-02-15 13:08:28930 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;
Paul B Mahol052674f2023-02-13 20:03:13950 }
Paul B Maholb8c08022023-01-31 20:03:38951 }
Paul B Mahol052674f2023-02-13 20:03:13952
953 n += ret;
Paul B Maholb8c08022023-01-31 20:03:38954 }
955 }
956
Paul B Mahol7aa71ab2023-08-30 21:00:37957 if (frame->nb_samples < s->frame_samples &&
958 frame->nb_samples > s->last_nb_samples)
959 frame->nb_samples = s->last_nb_samples;
960
Paul B Maholb8c08022023-01-31 20:03:38961 *got_frame_ptr = 1;
962
963 return avpkt->size;
964}
965
966static av_cold int rka_decode_close(AVCodecContext *avctx)
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
987const FFCodec ff_rka_decoder = {
988 .p.name = "rka",
Paul B Mahol5247dab2023-02-15 13:13:38989 CODEC_LONG_NAME("RKA (RK Audio)"),
Paul B Maholb8c08022023-01-31 20:03:38990 .p.type = AVMEDIA_TYPE_AUDIO,
991 .p.id = AV_CODEC_ID_RKA,
992 .priv_data_size = sizeof(RKAContext),
993 .init = rka_decode_init,
994 .close = rka_decode_close,
995 FF_CODEC_DECODE_CB(rka_decode_frame),
996 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
997 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
998};