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
25
30
36
41
47
51
61
68
71
73
78
80
84
87
90
99
101
105
107 {
113
118
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 {
130 }
131
133 {
135 int qfactor;
136
139
141
143 case 8:
145 break;
146 case 16:
148 break;
149 default:
151 }
152
155 if (
s->channels < 1 ||
s->channels > 2)
157
160 s->frame_samples = 131072 /
s->align;
161 s->last_nb_samples =
s->total_nb_samples %
s->frame_samples;
163
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 }
176
177 return 0;
178 }
179
181 {
182 unsigned x;
183
187
188 x = (1 << (
bits >> 1)) + 3;
190
194
198 }
199 }
200
202 int sample_rate,
int bps)
203 {
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 & 0xFFFFFFFC
U;
217
219 c->bprob[0][
i] =
c->bprob[1][
i] = 1;
220
221 for (
int i = 0;
i < 11;
i++) {
225
230 }
231
245 }
246
248 {
250 ac->
high = 0xffffffff;
251 ac->
value = bytestream2_get_be32(&ac->
gb);
252 }
253
255 {
257 int low;
258
260 help = ac->
high / (unsigned)(freq2 + freq1);
264
265 if (
value - low >= add) {
266 ac->
low = low = add + low;
268 while (1) {
269 if ((low ^ (
high + low)) > 0xFFFFFF) {
271 return 1;
272 ac->
high = (uint16_t)-(int16_t)low;
273 }
274
276 break;
277 ac->
value = bytestream2_get_byteu(&ac->
gb) | (ac->
value << 8);
279 low = ac->
low = ac->
low << 8;
280 }
281 return -1;
282 }
283
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
293 break;
294 ac->
value = bytestream2_get_byteu(&ac->
gb) | (ac->
value << 8);
296 low = ac->
low = ac->
low << 8;
297 }
298 return -1;
299 }
300
302 {
303 uint32_t x;
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
315
317
319 }
320
322 {
323 uint32_t new_high;
324
325 if (freq == 0)
326 return -1;
327
328 new_high = ac->
high / freq;
330
331 if (new_high == 0)
332 return -1;
333
335
336 return 0;
337 }
338
340 {
342
345
346 while (1) {
347 if (((
high + low) ^ low) > 0xffffff) {
349 return 0;
350 ac->
high = (uint16_t)-(int16_t)low;
351 }
352
354 break;
355
356 ac->
value = (ac->
value << 8) | bytestream2_get_byteu(&ac->
gb);
357 low = ac->
low = ac->
low << 8;
359 }
360
361 return -1;
362 }
363
365 {
369 } else {
370 do {
373 } while (val < am->buf_size);
374 }
375 }
376
378 {
380
381 if (idx >= 0) {
382 do {
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;
400 idx--;
401 } while (idx2 >= 0);
402 }
403
406
408 }
409
411 {
412 unsigned freq, size2,
val, mul;
413 int j;
414
416
419
422 if (am->
total <= 1) {
425 return 0;
426 }
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 }
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;
448 } else {
449 freq = 0;
451 }
455 for (
int k =
val - 1; (
val & (
val - 1)) != k; k &= k - 1)
456 mul -= am->
prob[0][k];
457 }
460 return 0;
461 }
465 } else {
466 freq = 1;
469 freq++;
470 }
472 }
473 freq = 0;
478 freq++;
479 }
480 }
482 do {
485 }
491
492 return 0;
493 }
494
496 {
498 int idx = 0;
499
501 return -1;
502
504 return 0;
505
507 return -1;
508
509 do {
510 if (((idx == 8) || (idx == 20)) && (0 <
bits))
512
514 return -1;
515
517 return -1;
518
522 }
523
525 dst->coeffs[idx++] = 0;
526 } else {
527 unsigned freq = 0;
528 int sign;
529
533 }
534 dst->coeffs[idx] = freq + 1 + ((
val - 1
U) <<
bits);
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
548 {
550
554 do {
555 if (((
high + low) ^ low) > 0xffffff) {
557 return 0;
558 ac->
high = (uint16_t)-(int16_t)low;
559 }
560
562 break;
563
564 ac->
value = (ac->
value << 8) | bytestream2_get_byteu(&ac->
gb);
566 ac->
low = low = ac->
low << 8;
567 } while (1);
568
569 return -1;
570 }
572 do {
573 if (((
high + low) ^ low) > 0xffffff) {
575 return 1;
576 ac->
high = (uint16_t)-(int16_t)low;
577 }
578
580 break;
581
582 ac->
value = (ac->
value << 8) | bytestream2_get_byteu(&ac->
gb);
584 ac->
low = low = ac->
low << 8;
585 } while (1);
586
587 return -1;
588 }
589
591 {
594
595 if (
ctx->zero[0] +
ctx->zero[1] > 4000
U) {
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] > 4000
U) {
600 ctx->sign[0] = (
ctx->sign[0] >> 1) + 1;
601 ctx->sign[1] = (
ctx->sign[1] >> 1) + 1;
602 }
604 if (sign == 0) {
607 return 0;
608 } else if (sign < 0) {
609 return -1;
610 }
611
614 if (sign < 0)
615 return -1;
622 } else {
623 int hbits =
bits / 2;
629 }
630 }
632 idx = 0;
634 do {
635 uint16_t *val4 =
ctx->val4;
637
638 if (val4[idx] +
ctx->val1[idx] > 2000
U) {
639 val4[idx] = (val4[idx] >> 1) + 1;
640 ctx->val1[idx] = (
ctx->val1[idx] >> 1) + 1;
641 }
645 break;
647 return -1;
648 }
650 idx++;
651 }
while (idx <= ctx->
size);
655 if (sign)
657 return 0;
658 }
659 }
667 if (sign)
669
670 return 0;
671 }
672
674
676 {
680 unsigned rsize, idx = 3,
bits = 0, m = 0;
681
682 if (
ctx->qfactor == 0) {
684 return -1;
686 }
687
691
694 else
696
698 return 0;
699
700 for (
int x = 0; x <
size;) {
702 return -1;
703
704 m = 0;
705 idx = (
ctx->pos_idx + idx) % 11;
707
709 for (int y = 0; y < rsize; y++, off++) {
710 int midx,
shift = idx, *
src, sum = 16;
711
713 return -1;
714
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 }
727 return -1;
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];
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) {
738 ctx->buf1[off] = sum +
val;
739 } else {
741 (((1
U <<
bits) - 1
U) &
ctx->buf1[off + -1]);
742 }
743 ctx->buf0[off] =
ctx->buf1[off] + (unsigned)
ctx->buf0[off + -1];
744 } else {
746 sum +=
ctx->buf0[off + -1] + (unsigned)
val;
750 }
751 ctx->buf1[off] = sum -
ctx->buf0[off + -1];
752 ctx->buf0[off] = sum;
753 m += (unsigned)
FFABS(
ctx->buf1[off]);
754 }
755 }
757 int sum = 0;
758 for (
unsigned i = (m << 6) / rsize;
i > 0;
i =
i >> 1)
759 sum++;
760 sum -= (
ctx->vrq + 7);
762 }
763
765 }
766
767 return 0;
768 }
769
771 {
773 int segment_size, offset2,
mode,
ret;
774
778
784 segment_size *= 4;
788 } else {
789 segment_size =
ctx->srate_pad;
790
796 offset2 = segment_size / 4 +
offset;
800 offset2 = segment_size / 4 + offset2;
801 } else {
805 offset2 = segment_size / 2 +
offset;
806 }
811 } else {
818 }
819 } else {
823 }
824 }
825
826 return segment_size;
827 }
828
830 {
833 int nb_decoded = 0;
834
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
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 {
855
858
859 for (
int ch = 0; ch <
s->channels; ch++) {
864 }
865
866 frame->nb_samples =
s->frame_samples;
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;) {
879 frame->nb_samples = n;
880 break;
881 }
882 if (ret < 0 || n + ret >
frame->nb_samples)
884
887 frame->nb_samples = n;
888 break;
889 }
890 if (ret < 0 || n + ret >
frame->nb_samples)
892
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;
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:
914 }
915
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
926 frame->nb_samples = n;
927 break;
928 }
929
930 if (ret < 0 || n + ret >
frame->nb_samples)
932
935 for (
int i = 0;
i <
ret;
i++) {
936 int m =
s->ch[ch].buf0[2560 +
i];
937
939 }
940 break;
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:
950 }
951 }
952
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
964 }
965
967 {
969
970 for (int ch = 0; ch < 2; ch++) {
972
973 for (
int i = 0;
i < 11;
i++)
975
979 }
980
983
984 return 0;
985 }
986
998 };