FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/rka.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 431 585 73.7%
Functions: 20 21 95.2%
Branches: 213 336 63.4%

Line Branch Exec Source
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
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
30 typedef struct ACoder {
31 GetByteContext gb;
32 uint32_t low, high;
33 uint32_t value;
34 } ACoder;
35
36 typedef struct FiltCoeffs {
37 int32_t coeffs[257];
38 unsigned size;
39 } FiltCoeffs;
40
41 typedef 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
51 typedef 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
61 typedef struct ChContext {
62 int qfactor;
63 int vrq;
64 int last_nb_decoded;
65 unsigned srate_pad;
66 unsigned pos_idx;
67
68 AdaptiveModel *filt_size;
69 AdaptiveModel *filt_bits;
70
71 uint32_t *bprob[2];
72
73 AdaptiveModel position;
74 AdaptiveModel fshift;
75 AdaptiveModel nb_segments;
76 AdaptiveModel coeff_bits[11];
77
78 Model64 mdl64[4][11];
79
80 int32_t buf0[131072+2560];
81 int32_t buf1[131072+2560];
82 } ChContext;
83
84 typedef 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 correlated;
94 int frame_samples;
95 int last_nb_samples;
96 uint32_t total_nb_samples;
97 uint32_t samples_left;
98
99 uint32_t bprob[2][257];
100
101 AdaptiveModel filt_size;
102 AdaptiveModel filt_bits;
103 } RKAContext;
104
105 448 static int adaptive_model_init(AdaptiveModel *am, int buf_size)
106 {
107 448 am->buf_size = buf_size;
108 448 am->sum = 2000;
109 448 am->aprob0 = 0;
110 448 am->aprob1 = 0;
111 448 am->total = 0;
112
113
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 388 times.
448 if (!am->prob[0])
114 60 am->prob[0] = av_malloc_array(buf_size + 5, sizeof(*am->prob[0]));
115
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 388 times.
448 if (!am->prob[1])
116 60 am->prob[1] = av_malloc_array(buf_size + 5, sizeof(*am->prob[1]));
117
118
2/4
✓ Branch 0 taken 448 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 448 times.
448 if (!am->prob[0] || !am->prob[1])
119 return AVERROR(ENOMEM);
120 448 memset(am->prob[0], 0, (buf_size + 5) * sizeof(*am->prob[0]));
121 448 memset(am->prob[1], 0, (buf_size + 5) * sizeof(*am->prob[1]));
122 448 return 0;
123 }
124
125 60 static void adaptive_model_free(AdaptiveModel *am)
126 {
127 60 av_freep(&am->prob[0]);
128 60 av_freep(&am->prob[1]);
129 60 }
130
131 2 static av_cold int rka_decode_init(AVCodecContext *avctx)
132 {
133 2 RKAContext *s = avctx->priv_data;
134 int qfactor;
135
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (avctx->extradata_size < 16)
137 return AVERROR_INVALIDDATA;
138
139 2 s->bps = avctx->bits_per_raw_sample = avctx->extradata[13];
140
141
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 switch (s->bps) {
142 case 8:
143 avctx->sample_fmt = AV_SAMPLE_FMT_U8P;
144 break;
145 2 case 16:
146 2 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
147 2 break;
148 default:
149 return AVERROR_INVALIDDATA;
150 }
151
152 2 av_channel_layout_uninit(&avctx->ch_layout);
153 2 s->channels = avctx->ch_layout.nb_channels = avctx->extradata[12];
154
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (s->channels < 1 || s->channels > 2)
155 return AVERROR_INVALIDDATA;
156
157 2 s->align = (s->channels * (avctx->bits_per_raw_sample >> 3));
158 2 s->samples_left = s->total_nb_samples = (AV_RL32(avctx->extradata + 4)) / s->align;
159 2 s->frame_samples = 131072 / s->align;
160 2 s->last_nb_samples = s->total_nb_samples % s->frame_samples;
161 2 s->correlated = avctx->extradata[15] & 1;
162
163 2 qfactor = avctx->extradata[14] & 0xf;
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if ((avctx->extradata[15] & 4) != 0)
165 qfactor = -qfactor;
166
167
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 s->ch[0].qfactor = s->ch[1].qfactor = qfactor < 0 ? 2 : qfactor;
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 s->ch[0].vrq = qfactor < 0 ? -qfactor : 0;
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 s->ch[1].vrq = qfactor < 0 ? -qfactor : 0;
170
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (qfactor < 0) {
171 s->ch[0].vrq = av_clip(s->ch[0].vrq, 1, 8);
172 s->ch[1].vrq = av_clip(s->ch[1].vrq, 1, 8);
173 }
174 2 av_log(avctx, AV_LOG_DEBUG, "qfactor: %d\n", qfactor);
175
176 2 return 0;
177 }
178
179 1232 static void model64_init(Model64 *m, unsigned bits)
180 {
181 unsigned x;
182
183 1232 m->bits = bits;
184 1232 m->size = 64;
185 1232 m->zero[0] = 1;
186
187 1232 x = (1 << (bits >> 1)) + 3;
188 1232 x = FFMIN(x, 20);
189
190 1232 m->zero[1] = x;
191 1232 m->sign[0] = 1;
192 1232 m->sign[1] = 1;
193
194
2/2
✓ Branch 0 taken 80080 times.
✓ Branch 1 taken 1232 times.
81312 for (int i = 0; i < FF_ARRAY_ELEMS(m->val4); i++) {
195 80080 m->val4[i] = 4;
196 80080 m->val1[i] = 1;
197 }
198 1232 }
199
200 28 static int chctx_init(RKAContext *s, ChContext *c,
201 int sample_rate, int bps)
202 {
203 int ret;
204
205 28 memset(c->buf0, 0, sizeof(c->buf0));
206 28 memset(c->buf1, 0, sizeof(c->buf1));
207
208 28 c->filt_size = &s->filt_size;
209 28 c->filt_bits = &s->filt_bits;
210
211 28 c->bprob[0] = s->bprob[0];
212 28 c->bprob[1] = s->bprob[1];
213
214 28 c->srate_pad = ((int64_t)sample_rate << 13) / 44100 & 0xFFFFFFFCU;
215 28 c->pos_idx = 1;
216
217
2/2
✓ Branch 0 taken 7196 times.
✓ Branch 1 taken 28 times.
7224 for (int i = 0; i < FF_ARRAY_ELEMS(s->bprob[0]); i++)
218 7196 c->bprob[0][i] = c->bprob[1][i] = 1;
219
220
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 28 times.
336 for (int i = 0; i < 11; i++) {
221 308 ret = adaptive_model_init(&c->coeff_bits[i], 32);
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 308 times.
308 if (ret < 0)
223 return ret;
224
225 308 model64_init(&c->mdl64[0][i], i);
226 308 model64_init(&c->mdl64[1][i], i);
227 308 model64_init(&c->mdl64[2][i], i+1);
228 308 model64_init(&c->mdl64[3][i], i+1);
229 }
230
231 28 ret = adaptive_model_init(c->filt_size, 256);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
233 return ret;
234 28 ret = adaptive_model_init(c->filt_bits, 16);
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
236 return ret;
237 28 ret = adaptive_model_init(&c->position, 16);
238
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
239 return ret;
240 28 ret = adaptive_model_init(&c->nb_segments, 8);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
242 return ret;
243 28 return adaptive_model_init(&c->fshift, 32);
244 }
245
246 14 static void init_acoder(ACoder *ac)
247 {
248 14 ac->low = 0x0;
249 14 ac->high = 0xffffffff;
250 14 ac->value = bytestream2_get_be32(&ac->gb);
251 14 }
252
253 4558875 static int ac_decode_bool(ACoder *ac, int freq1, int freq2)
254 {
255 unsigned help, add, high, value;
256 int low;
257
258 4558875 low = ac->low;
259 4558875 help = ac->high / (unsigned)(freq2 + freq1);
260 4558875 value = ac->value;
261 4558875 add = freq1 * help;
262 4558875 ac->high = help;
263
264
2/2
✓ Branch 0 taken 2255650 times.
✓ Branch 1 taken 2303225 times.
4558875 if (value - low >= add) {
265 2255650 ac->low = low = add + low;
266 2255650 ac->high = high = freq2 * help;
267 while (1) {
268
2/2
✓ Branch 0 taken 2256241 times.
✓ Branch 1 taken 234703 times.
2490944 if ((low ^ (high + low)) > 0xFFFFFF) {
269
2/2
✓ Branch 0 taken 2255650 times.
✓ Branch 1 taken 591 times.
2256241 if (high > 0xFFFF)
270 2255650 return 1;
271 591 ac->high = (uint16_t)-(int16_t)low;
272 }
273
274
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 235294 times.
235294 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
275 break;
276 235294 ac->value = bytestream2_get_byteu(&ac->gb) | (ac->value << 8);
277 235294 ac->high = high = ac->high << 8;
278 235294 low = ac->low = ac->low << 8;
279 }
280 return -1;
281 }
282
283 2303225 ac->high = add;
284 while (1) {
285
2/2
✓ Branch 0 taken 2303787 times.
✓ Branch 1 taken 188574 times.
2492361 if ((low ^ (add + low)) > 0xFFFFFF) {
286
2/2
✓ Branch 0 taken 2303225 times.
✓ Branch 1 taken 562 times.
2303787 if (add > 0xFFFF)
287 2303225 return 0;
288 562 ac->high = (uint16_t)-(int16_t)low;
289 }
290
291
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 189136 times.
189136 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
292 break;
293 189136 ac->value = bytestream2_get_byteu(&ac->gb) | (ac->value << 8);
294 189136 ac->high = add = ac->high << 8;
295 189136 low = ac->low = ac->low << 8;
296 }
297 return -1;
298 }
299
300 12855 static int decode_bool(ACoder *ac, ChContext *c, int idx)
301 {
302 uint32_t x;
303 int b;
304
305 12855 x = c->bprob[0][idx];
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12855 times.
12855 if (x + c->bprob[1][idx] > 4096) {
307 c->bprob[0][idx] = (x >> 1) + 1;
308 c->bprob[1][idx] = (c->bprob[1][idx] >> 1) + 1;
309 }
310
311 12855 b = ac_decode_bool(ac, c->bprob[0][idx], c->bprob[1][idx]);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12855 times.
12855 if (b < 0)
313 return b;
314
315 12855 c->bprob[b][idx]++;
316
317 12855 return b;
318 }
319
320 923630 static int ac_get_freq(ACoder *ac, unsigned freq, int *result)
321 {
322 uint32_t new_high;
323
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923630 times.
923630 if (freq == 0)
325 return -1;
326
327 923630 new_high = ac->high / freq;
328 923630 ac->high = new_high;
329
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 923630 times.
923630 if (new_high == 0)
331 return -1;
332
333 923630 *result = (ac->value - ac->low) / new_high;
334
335 923630 return 0;
336 }
337
338 923630 static int ac_update(ACoder *ac, int freq, int mul)
339 {
340 uint32_t low, high;
341
342 923630 low = ac->low = ac->high * freq + ac->low;
343 923630 high = ac->high = ac->high * mul;
344
345 while (1) {
346
2/2
✓ Branch 0 taken 924278 times.
✓ Branch 1 taken 647483 times.
1571761 if (((high + low) ^ low) > 0xffffff) {
347
2/2
✓ Branch 0 taken 923630 times.
✓ Branch 1 taken 648 times.
924278 if (high > 0xffff)
348 923630 return 0;
349 648 ac->high = (uint16_t)-(int16_t)low;
350 }
351
352
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 648131 times.
648131 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
353 break;
354
355 648131 ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb);
356 648131 low = ac->low = ac->low << 8;
357 648131 high = ac->high = ac->high << 8;
358 }
359
360 return -1;
361 }
362
363 20802 static void amdl_update_prob(AdaptiveModel *am, int val, int diff)
364 {
365 20802 am->aprob0 += diff;
366
2/2
✓ Branch 0 taken 6283 times.
✓ Branch 1 taken 14519 times.
20802 if (val <= 0) {
367 6283 am->prob[0][0] += diff;
368 } else {
369 do {
370 54753 am->prob[0][val] += diff;
371 54753 val += (val & -val);
372
2/2
✓ Branch 0 taken 40234 times.
✓ Branch 1 taken 14519 times.
54753 } while (val < am->buf_size);
373 }
374 20802 }
375
376 static void update_ch_subobj(AdaptiveModel *am)
377 {
378 int idx2, idx = am->buf_size - 1;
379
380 if (idx >= 0) {
381 do {
382 uint16_t *prob = am->prob[0];
383 int diff, prob_idx = prob[idx];
384
385 idx2 = idx - 1;
386 if (idx > 0) {
387 int idx3 = idx - 1;
388
389 if ((idx2 & idx) != idx2) {
390 do {
391 prob_idx -= prob[idx3];
392 idx3 &= idx3 - 1;
393 } while ((idx2 & idx) != idx3);
394 }
395 }
396
397 diff = ((prob_idx > 0) - prob_idx) >> 1;
398 amdl_update_prob(am, idx, diff);
399 idx--;
400 } while (idx2 >= 0);
401 }
402
403 if (am->sum < 8000)
404 am->sum += 200;
405
406 am->aprob1 = (am->aprob1 + 1) >> 1;
407 }
408
409 20802 static int amdl_decode_int(AdaptiveModel *am, ACoder *ac, unsigned *dst, unsigned size)
410 {
411 unsigned freq, size2, val, mul;
412 int j;
413
414 20802 size = FFMIN(size, am->buf_size - 1);
415
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20802 times.
20802 if (am->aprob0 >= am->sum)
417 update_ch_subobj(am);
418
419
5/6
✓ Branch 0 taken 20581 times.
✓ Branch 1 taken 221 times.
✓ Branch 2 taken 20581 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19042 times.
✓ Branch 5 taken 1539 times.
41383 if (am->aprob1 && (am->total == am->buf_size ||
420 20581 ac_decode_bool(ac, am->aprob0, am->aprob1) == 0)) {
421
2/2
✓ Branch 0 taken 361 times.
✓ Branch 1 taken 18681 times.
19042 if (am->total <= 1) {
422 361 dst[0] = am->last;
423 361 amdl_update_prob(am, dst[0], 1);
424 361 return 0;
425 }
426
2/2
✓ Branch 0 taken 13086 times.
✓ Branch 1 taken 5595 times.
18681 if (size == am->buf_size - 1) {
427 13086 freq = am->aprob0;
428 } else {
429 5595 freq = am->prob[0][0];
430
2/2
✓ Branch 0 taken 11190 times.
✓ Branch 1 taken 5595 times.
16785 for (int j = size; j > 0; j &= (j - 1) )
431 11190 freq += am->prob[0][j];
432 }
433 18681 ac_get_freq(ac, freq, &freq);
434 18681 size2 = am->buf_size >> 1;
435 18681 val = am->prob[0][0];
436
2/2
✓ Branch 0 taken 12853 times.
✓ Branch 1 taken 5828 times.
18681 if (freq >= val) {
437 12853 int sum = 0;
438
2/2
✓ Branch 0 taken 63340 times.
✓ Branch 1 taken 12853 times.
76193 for (j = freq - val; size2; size2 >>= 1) {
439 63340 unsigned v = am->prob[0][size2 + sum];
440
2/2
✓ Branch 0 taken 14414 times.
✓ Branch 1 taken 48926 times.
63340 if (j >= v) {
441 14414 sum += size2;
442 14414 j -= v;
443 }
444 }
445 12853 freq -= j;
446 12853 val = sum + 1;
447 } else {
448 5828 freq = 0;
449 5828 val = 0;
450 }
451 18681 dst[0] = val;
452 18681 mul = am->prob[0][val];
453
2/2
✓ Branch 0 taken 12853 times.
✓ Branch 1 taken 5828 times.
18681 if (val > 0) {
454
2/2
✓ Branch 0 taken 8246 times.
✓ Branch 1 taken 12853 times.
21099 for (int k = val - 1; (val & (val - 1)) != k; k &= k - 1)
455 8246 mul -= am->prob[0][k];
456 }
457 18681 ac_update(ac, freq, mul);
458 18681 amdl_update_prob(am, dst[0], 1);
459 18681 return 0;
460 }
461 1760 am->aprob1++;
462
2/2
✓ Branch 0 taken 1486 times.
✓ Branch 1 taken 274 times.
1760 if (size == am->buf_size - 1) {
463 1486 ac_get_freq(ac, am->buf_size - am->total, &val);
464 } else {
465 274 freq = 1;
466
2/2
✓ Branch 0 taken 2480 times.
✓ Branch 1 taken 274 times.
2754 for (dst[0] = 0; dst[0] < size; dst[0]++) {
467
2/2
✓ Branch 0 taken 2179 times.
✓ Branch 1 taken 301 times.
2480 if (!am->prob[1][dst[0]])
468 2179 freq++;
469 }
470 274 ac_get_freq(ac, freq, &val);
471 }
472 1760 freq = 0;
473 1760 dst[0] = 0;
474
3/4
✓ Branch 0 taken 1418 times.
✓ Branch 1 taken 342 times.
✓ Branch 2 taken 1418 times.
✗ Branch 3 not taken.
1760 if (val > 0 && am->buf_size > 0) {
475
2/2
✓ Branch 0 taken 19788 times.
✓ Branch 1 taken 1418 times.
21206 for (dst[0] = 0; dst[0] < size & freq < val; dst[0]++) {
476
2/2
✓ Branch 0 taken 15574 times.
✓ Branch 1 taken 4214 times.
19788 if (!am->prob[1][dst[0]])
477 15574 freq++;
478 }
479 }
480
2/2
✓ Branch 0 taken 577 times.
✓ Branch 1 taken 1183 times.
1760 if (am->prob[1][dst[0]]) {
481 do {
482 1955 val = dst[0]++;
483
3/4
✓ Branch 0 taken 1955 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1378 times.
✓ Branch 3 taken 577 times.
1955 } while (val + 1 < am->buf_size && am->prob[1][val + 1]);
484 }
485 1760 ac_update(ac, freq, 1);
486 1760 am->prob[1][dst[0]]++;
487 1760 am->total++;
488 1760 amdl_update_prob(am, dst[0], 1);
489 1760 am->last = dst[0];
490
491 1760 return 0;
492 }
493
494 338 static int decode_filt_coeffs(RKAContext *s, ChContext *ctx, ACoder *ac, FiltCoeffs *dst)
495 {
496 unsigned val, bits;
497 338 int idx = 0;
498
499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 338 times.
338 if (amdl_decode_int(ctx->filt_size, ac, &dst->size, 256) < 0)
500 return -1;
501
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
338 if (dst->size == 0)
503 return 0;
504
505
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 338 times.
338 if (amdl_decode_int(ctx->filt_bits, ac, &bits, 10) < 0)
506 return -1;
507
508 do {
509
6/6
✓ Branch 0 taken 13963 times.
✓ Branch 1 taken 303 times.
✓ Branch 2 taken 200 times.
✓ Branch 3 taken 13763 times.
✓ Branch 4 taken 380 times.
✓ Branch 5 taken 123 times.
14266 if (((idx == 8) || (idx == 20)) && (0 < bits))
510 380 bits--;
511
512
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14266 times.
14266 if (bits > 10)
513 return -1;
514
515
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14266 times.
14266 if (amdl_decode_int(&ctx->coeff_bits[bits], ac, &val, 31) < 0)
516 return -1;
517
518
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 14258 times.
14266 if (val == 31) {
519 8 ac_get_freq(ac, 65536, &val);
520 8 ac_update(ac, val, 1);
521 }
522
523
2/2
✓ Branch 0 taken 1411 times.
✓ Branch 1 taken 12855 times.
14266 if (val == 0) {
524 1411 dst->coeffs[idx++] = 0;
525 } else {
526 12855 unsigned freq = 0;
527 int sign;
528
529
2/2
✓ Branch 0 taken 4081 times.
✓ Branch 1 taken 8774 times.
12855 if (bits > 0) {
530 4081 ac_get_freq(ac, 1 << bits, &freq);
531 4081 ac_update(ac, freq, 1);
532 }
533 12855 dst->coeffs[idx] = freq + 1 + ((val - 1U) << bits);
534 12855 sign = decode_bool(ac, ctx, idx);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12855 times.
12855 if (sign < 0)
536 return -1;
537
2/2
✓ Branch 0 taken 6863 times.
✓ Branch 1 taken 5992 times.
12855 if (sign == 1)
538 6863 dst->coeffs[idx] = -dst->coeffs[idx];
539 12855 idx++;
540 }
541
2/2
✓ Branch 0 taken 13928 times.
✓ Branch 1 taken 338 times.
14266 } while (idx < dst->size);
542
543 338 return 0;
544 }
545
546 1 static int ac_dec_bit(ACoder *ac)
547 {
548 uint32_t high, low;
549
550 1 low = ac->low;
551 1 ac->high = high = ac->high >> 1;
552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 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 1 ac->low = low = low + high;
571 do {
572
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (((high + low) ^ low) > 0xffffff) {
573
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (high > 0xffff)
574 1 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
589 903440 static int mdl64_decode(ACoder *ac, Model64 *ctx, int *dst)
590 {
591 int sign, idx, bits;
592 903440 unsigned val = 0;
593
594
2/2
✓ Branch 0 taken 614 times.
✓ Branch 1 taken 902826 times.
903440 if (ctx->zero[0] + ctx->zero[1] > 4000U) {
595 614 ctx->zero[0] = (ctx->zero[0] >> 1) + 1;
596 614 ctx->zero[1] = (ctx->zero[1] >> 1) + 1;
597 }
598
2/2
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 903211 times.
903440 if (ctx->sign[0] + ctx->sign[1] > 4000U) {
599 229 ctx->sign[0] = (ctx->sign[0] >> 1) + 1;
600 229 ctx->sign[1] = (ctx->sign[1] >> 1) + 1;
601 }
602 903440 sign = ac_decode_bool(ac, ctx->zero[0], ctx->zero[1]);
603
2/2
✓ Branch 0 taken 4343 times.
✓ Branch 1 taken 899097 times.
903440 if (sign == 0) {
604 4343 ctx->zero[0] += 2;
605 4343 dst[0] = 0;
606 4343 return 0;
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 899097 times.
899097 } else if (sign < 0) {
608 return -1;
609 }
610
611 899097 ctx->zero[1] += 2;
612 899097 sign = ac_decode_bool(ac, ctx->sign[0], ctx->sign[1]);
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 899097 times.
899097 if (sign < 0)
614 return -1;
615 899097 ctx->sign[sign]++;
616 899097 bits = ctx->bits;
617
1/2
✓ Branch 0 taken 899097 times.
✗ Branch 1 not taken.
899097 if (bits > 0) {
618
1/2
✓ Branch 0 taken 899097 times.
✗ Branch 1 not taken.
899097 if (bits < 13) {
619 899097 ac_get_freq(ac, 1 << bits, &val);
620 899097 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 899097 bits = ctx->size;
631 899097 idx = 0;
632
1/2
✓ Branch 0 taken 899097 times.
✗ Branch 1 not taken.
899097 if (bits >= 0) {
633 do {
634 2722902 uint16_t *val4 = ctx->val4;
635 int b;
636
637
2/2
✓ Branch 0 taken 8623 times.
✓ Branch 1 taken 2714279 times.
2722902 if (val4[idx] + ctx->val1[idx] > 2000U) {
638 8623 val4[idx] = (val4[idx] >> 1) + 1;
639 8623 ctx->val1[idx] = (ctx->val1[idx] >> 1) + 1;
640 }
641 2722902 b = ac_decode_bool(ac, ctx->val4[idx], ctx->val1[idx]);
642
2/2
✓ Branch 0 taken 899096 times.
✓ Branch 1 taken 1823806 times.
2722902 if (b == 1) {
643 899096 ctx->val1[idx] += 4;
644 899096 break;
645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1823806 times.
1823806 } else if (b < 0) {
646 return -1;
647 }
648 1823806 ctx->val4[idx] += 4;
649 1823806 idx++;
650
2/2
✓ Branch 0 taken 1823805 times.
✓ Branch 1 taken 1 times.
1823806 } while (idx <= ctx->size);
651 899097 bits = ctx->size;
652
2/2
✓ Branch 0 taken 899096 times.
✓ Branch 1 taken 1 times.
899097 if (idx <= bits) {
653 899096 dst[0] = val + 1 + (idx << ctx->bits);
654
2/2
✓ Branch 0 taken 449055 times.
✓ Branch 1 taken 450041 times.
899096 if (sign)
655 449055 dst[0] = -dst[0];
656 899096 return 0;
657 }
658 }
659 1 bits++;
660
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 while (ac_dec_bit(ac) == 0)
661 bits += 64;
662 1 ac_get_freq(ac, 64, &idx);
663 1 ac_update(ac, idx, 1);
664 1 idx += bits;
665 1 dst[0] = val + 1 + (idx << ctx->bits);
666
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (sign)
667 dst[0] = -dst[0];
668
669 1 return 0;
670 }
671
672 static const uint8_t vrq_qfactors[8] = { 3, 3, 2, 2, 1, 1, 1, 1 };
673
674 338 static int decode_filter(RKAContext *s, ChContext *ctx, ACoder *ac, int off, unsigned size)
675 {
676 FiltCoeffs filt;
677 Model64 *mdl64;
678 338 int split, val, last_val = 0, ret;
679 338 unsigned rsize, idx = 3, bits = 0, m = 0;
680
681
1/2
✓ Branch 0 taken 338 times.
✗ Branch 1 not taken.
338 if (ctx->qfactor == 0) {
682
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 338 times.
338 if (amdl_decode_int(&ctx->fshift, ac, &bits, 15) < 0)
683 return -1;
684 338 bits &= 31U;
685 }
686
687 338 ret = decode_filt_coeffs(s, ctx, ac, &filt);
688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
338 if (ret < 0)
689 return ret;
690
691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
338 if (size < 512)
692 split = size / 2;
693 else
694 338 split = size >> 4;
695
696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
338 if (size <= 1)
697 return 0;
698
699
2/2
✓ Branch 0 taken 5410 times.
✓ Branch 1 taken 338 times.
5748 for (int x = 0; x < size;) {
700
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5410 times.
5410 if (amdl_decode_int(&ctx->position, ac, &idx, 10) < 0)
701 return -1;
702
703 5410 m = 0;
704 5410 idx = (ctx->pos_idx + idx) % 11;
705 5410 ctx->pos_idx = idx;
706
707 5410 rsize = FFMIN(split, size - x);
708
2/2
✓ Branch 0 taken 903440 times.
✓ Branch 1 taken 5410 times.
908850 for (int y = 0; y < rsize; y++, off++) {
709 903440 int midx, shift = idx, *src, sum = 16;
710
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 903440 times.
903440 if (off >= FF_ARRAY_ELEMS(ctx->buf0))
712 return -1;
713
714 903440 midx = FFABS(last_val) >> shift;
715
2/2
✓ Branch 0 taken 434 times.
✓ Branch 1 taken 903006 times.
903440 if (midx >= 15) {
716 434 mdl64 = &ctx->mdl64[3][idx];
717
2/2
✓ Branch 0 taken 35765 times.
✓ Branch 1 taken 867241 times.
903006 } else if (midx >= 7) {
718 35765 mdl64 = &ctx->mdl64[2][idx];
719
2/2
✓ Branch 0 taken 156303 times.
✓ Branch 1 taken 710938 times.
867241 } else if (midx >= 4) {
720 156303 mdl64 = &ctx->mdl64[1][idx];
721 } else {
722 710938 mdl64 = &ctx->mdl64[0][idx];
723 }
724 903440 ret = mdl64_decode(ac, mdl64, &val);
725
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 903440 times.
903440 if (ret < 0)
726 return -1;
727 903440 last_val = val;
728 903440 src = &ctx->buf1[off + -1];
729
4/4
✓ Branch 0 taken 13328856 times.
✓ Branch 1 taken 180496 times.
✓ Branch 2 taken 12605912 times.
✓ Branch 3 taken 722944 times.
13509352 for (int i = 0; i < filt.size && i < 15; i++)
730 12605912 sum += filt.coeffs[i] * (unsigned)src[-i];
731 903440 sum = sum * 2U;
732
2/2
✓ Branch 0 taken 36546560 times.
✓ Branch 1 taken 903440 times.
37450000 for (int i = 15; i < filt.size; i++)
733 36546560 sum += filt.coeffs[i] * (unsigned)src[-i];
734 903440 sum = sum >> 6;
735
1/2
✓ Branch 0 taken 903440 times.
✗ Branch 1 not taken.
903440 if (ctx->qfactor == 0) {
736
1/2
✓ Branch 0 taken 903440 times.
✗ Branch 1 not taken.
903440 if (bits == 0) {
737 903440 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 903440 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5410 times.
5410 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 5410 x += split;
764 }
765
766 338 return 0;
767 }
768
769 112 static int decode_samples(AVCodecContext *avctx, ACoder *ac, ChContext *ctx, int offset)
770 {
771 112 RKAContext *s = avctx->priv_data;
772 int segment_size, offset2, mode, ret;
773
774 112 ret = amdl_decode_int(&ctx->nb_segments, ac, &mode, 5);
775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (ret < 0)
776 return ret;
777
778
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 110 times.
112 if (mode == 5) {
779 2 ret = ac_get_freq(ac, ctx->srate_pad >> 2, &segment_size);
780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
781 return ret;
782 2 ac_update(ac, segment_size, 1);
783 2 segment_size *= 4;
784 2 ret = decode_filter(s, ctx, ac, offset, segment_size);
785
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
786 return ret;
787 } else {
788 110 segment_size = ctx->srate_pad;
789
790
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 11 times.
110 if (mode) {
791
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 40 times.
99 if (mode > 2) {
792 59 ret = decode_filter(s, ctx, ac, offset, segment_size / 4);
793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ret < 0)
794 return ret;
795 59 offset2 = segment_size / 4 + offset;
796 59 ret = decode_filter(s, ctx, ac, offset2, segment_size / 4);
797
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ret < 0)
798 return ret;
799 59 offset2 = segment_size / 4 + offset2;
800 } else {
801 40 ret = decode_filter(s, ctx, ac, offset, segment_size / 2);
802
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (ret < 0)
803 return ret;
804 40 offset2 = segment_size / 2 + offset;
805 }
806
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 68 times.
99 if (mode & 1) {
807 31 ret = decode_filter(s, ctx, ac, offset2, segment_size / 2);
808
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 if (ret < 0)
809 return ret;
810 } else {
811 68 ret = decode_filter(s, ctx, ac, offset2, segment_size / 4);
812
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (ret < 0)
813 return ret;
814 68 ret = decode_filter(s, ctx, ac, segment_size / 4 + offset2, segment_size / 4);
815
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
68 if (ret < 0)
816 return ret;
817 }
818 } else {
819 11 ret = decode_filter(s, ctx, ac, offset, ctx->srate_pad);
820
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (ret < 0)
821 return ret;
822 }
823 }
824
825 112 return segment_size;
826 }
827
828 113 static int decode_ch_samples(AVCodecContext *avctx, ChContext *c)
829 {
830 113 RKAContext *s = avctx->priv_data;
831 113 ACoder *ac = &s->ac;
832 113 int nb_decoded = 0;
833
834
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 112 times.
113 if (bytestream2_get_bytes_left(&ac->gb) <= 0)
835 1 return 0;
836
837 112 memmove(c->buf0, &c->buf0[c->last_nb_decoded], 2560 * sizeof(*c->buf0));
838 112 memmove(c->buf1, &c->buf1[c->last_nb_decoded], 2560 * sizeof(*c->buf1));
839
840 112 nb_decoded = decode_samples(avctx, ac, c, 2560);
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 112 times.
112 if (nb_decoded < 0)
842 return nb_decoded;
843 112 c->last_nb_decoded = nb_decoded;
844
845 112 return nb_decoded;
846 }
847
848 14 static int rka_decode_frame(AVCodecContext *avctx, AVFrame *frame,
849 int *got_frame_ptr, AVPacket *avpkt)
850 {
851 14 RKAContext *s = avctx->priv_data;
852 14 ACoder *ac = &s->ac;
853 int ret;
854
855 14 bytestream2_init(&ac->gb, avpkt->data, avpkt->size);
856 14 init_acoder(ac);
857
858
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 14 times.
42 for (int ch = 0; ch < s->channels; ch++) {
859 28 ret = chctx_init(s, &s->ch[ch], avctx->sample_rate,
860 avctx->bits_per_raw_sample);
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
862 return ret;
863 }
864
865 14 frame->nb_samples = s->frame_samples;
866
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
867 return ret;
868
869
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
28 if (s->channels == 2 && s->correlated) {
870 14 int16_t *l16 = (int16_t *)frame->extended_data[0];
871 14 int16_t *r16 = (int16_t *)frame->extended_data[1];
872 14 uint8_t *l8 = frame->extended_data[0];
873 14 uint8_t *r8 = frame->extended_data[1];
874
875
2/2
✓ Branch 0 taken 57 times.
✓ Branch 1 taken 13 times.
70 for (int n = 0; n < frame->nb_samples;) {
876 57 ret = decode_ch_samples(avctx, &s->ch[0]);
877
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 56 times.
57 if (ret == 0) {
878 1 frame->nb_samples = n;
879 1 break;
880 }
881
2/4
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
56 if (ret < 0 || n + ret > frame->nb_samples)
882 return AVERROR_INVALIDDATA;
883
884 56 ret = decode_ch_samples(avctx, &s->ch[1]);
885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (ret == 0) {
886 frame->nb_samples = n;
887 break;
888 }
889
2/4
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
56 if (ret < 0 || n + ret > frame->nb_samples)
890 return AVERROR_INVALIDDATA;
891
892
1/3
✓ Branch 0 taken 56 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
56 switch (avctx->sample_fmt) {
893 56 case AV_SAMPLE_FMT_S16P:
894
2/2
✓ Branch 0 taken 451720 times.
✓ Branch 1 taken 56 times.
451776 for (int i = 0; i < ret; i++) {
895 451720 int l = s->ch[0].buf0[2560 + i];
896 451720 int r = s->ch[1].buf0[2560 + i];
897
898 451720 l16[n + i] = (l * 2 + r + 1) >> 1;
899 451720 r16[n + i] = (l * 2 - r + 1) >> 1;
900 }
901 56 break;
902 case AV_SAMPLE_FMT_U8P:
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 56 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) {
933 case AV_SAMPLE_FMT_S16P:
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;
940 case AV_SAMPLE_FMT_U8P:
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
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 13 times.
14 if (frame->nb_samples < s->frame_samples &&
957
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 frame->nb_samples > s->last_nb_samples)
958 1 frame->nb_samples = s->last_nb_samples;
959
960 14 *got_frame_ptr = 1;
961
962 14 return avpkt->size;
963 }
964
965 2 static av_cold int rka_decode_close(AVCodecContext *avctx)
966 {
967 2 RKAContext *s = avctx->priv_data;
968
969
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (int ch = 0; ch < 2; ch++) {
970 4 ChContext *c = &s->ch[ch];
971
972
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 4 times.
48 for (int i = 0; i < 11; i++)
973 44 adaptive_model_free(&c->coeff_bits[i]);
974
975 4 adaptive_model_free(&c->position);
976 4 adaptive_model_free(&c->nb_segments);
977 4 adaptive_model_free(&c->fshift);
978 }
979
980 2 adaptive_model_free(&s->filt_size);
981 2 adaptive_model_free(&s->filt_bits);
982
983 2 return 0;
984 }
985
986 const FFCodec ff_rka_decoder = {
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),
992 .init = rka_decode_init,
993 .close = rka_decode_close,
994 FF_CODEC_DECODE_CB(rka_decode_frame),
995 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF,
996 .caps_internal = FF_CODEC_CAP_INIT_CLEANUP,
997 };
998