FFmpeg coverage


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