FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/scpr3.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 0 814 0.0%
Functions: 0 33 0.0%
Branches: 0 435 0.0%

Line Branch Exec Source
1 /*
2 * ScreenPressor version 3 decoder
3 *
4 * Copyright (c) 2017 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "libavutil/qsort.h"
28
29 #include "avcodec.h"
30 #include "bytestream.h"
31 #include "scpr.h"
32
33 static void renew_table3(uint32_t nsym, uint32_t *cntsum,
34 uint16_t *freqs, uint16_t *freqs1,
35 uint16_t *cnts, uint8_t *dectab)
36 {
37 uint32_t a = 0, b = 4096 / nsym, c = b - (b >> 1);
38
39 *cntsum = c * nsym;
40
41 for (int d = 0; d < nsym; d++) {
42 freqs[d] = b;
43 freqs1[d] = a;
44 cnts[d] = c;
45 for (int q = a + 128 - 1 >> 7, f = (a + b - 1 >> 7) + 1; q < f; q++)
46 dectab[q] = d;
47
48 a += b;
49 }
50 }
51
52 static void reinit_tables3(SCPRContext * s)
53 {
54 for (int i = 0; i < 3; i++) {
55 for (int j = 0; j < 4096; j++) {
56 PixelModel3 *m = &s->pixel_model3[i][j];
57 m->type = 0;
58 }
59 }
60
61 for (int i = 0; i < 6; i++) {
62 renew_table3(256, &s->run_model3[i].cntsum,
63 s->run_model3[i].freqs[0], s->run_model3[i].freqs[1],
64 s->run_model3[i].cnts, s->run_model3[i].dectab);
65 }
66
67 renew_table3(256, &s->range_model3.cntsum,
68 s->range_model3.freqs[0], s->range_model3.freqs[1],
69 s->range_model3.cnts, s->range_model3.dectab);
70
71 renew_table3(5, &s->fill_model3.cntsum,
72 s->fill_model3.freqs[0], s->fill_model3.freqs[1],
73 s->fill_model3.cnts, s->fill_model3.dectab);
74
75 renew_table3(256, &s->count_model3.cntsum,
76 s->count_model3.freqs[0], s->count_model3.freqs[1],
77 s->count_model3.cnts, s->count_model3.dectab);
78
79 for (int i = 0; i < 4; i++) {
80 renew_table3(16, &s->sxy_model3[i].cntsum,
81 s->sxy_model3[i].freqs[0], s->sxy_model3[i].freqs[1],
82 s->sxy_model3[i].cnts, s->sxy_model3[i].dectab);
83 }
84
85 for (int i = 0; i < 2; i++) {
86 renew_table3(512, &s->mv_model3[i].cntsum,
87 s->mv_model3[i].freqs[0], s->mv_model3[i].freqs[1],
88 s->mv_model3[i].cnts, s->mv_model3[i].dectab);
89 }
90
91 for (int i = 0; i < 6; i++) {
92 renew_table3(6, &s->op_model3[i].cntsum,
93 s->op_model3[i].freqs[0], s->op_model3[i].freqs[1],
94 s->op_model3[i].cnts, s->op_model3[i].dectab);
95 }
96 }
97
98 static int decode3(GetByteContext *gb, RangeCoder *rc, uint32_t a, uint32_t b)
99 {
100 uint32_t code = a * (rc->code >> 12) + (rc->code & 0xFFF) - b;
101
102 while (code < 0x800000 && bytestream2_get_bytes_left(gb) > 0)
103 code = bytestream2_get_byteu(gb) | (code << 8);
104 rc->code = code;
105
106 return 0;
107 }
108
109 static void rescale(PixelModel3 *m, int *totfr)
110 {
111 uint32_t a;
112
113 a = 256 - m->size;
114 for (int b = 0; b < m->size; b++) {
115 m->freqs[b] -= m->freqs[b] >> 1;
116 a += m->freqs[b];
117 }
118
119 *totfr = a;
120 }
121
122 static int add_symbol(PixelModel3 *m, int index, uint32_t symbol, int *totfr, int max)
123 {
124 if (m->size == max)
125 return 0;
126
127 for (int c = m->size - 1; c >= index; c--) {
128 m->symbols[c + 1] = m->symbols[c];
129 m->freqs[c + 1] = m->freqs[c];
130 }
131
132 m->symbols[index] = symbol;
133 m->freqs[index] = 50;
134 m->size++;
135
136 if (m->maxpos >= index)
137 m->maxpos++;
138
139 *totfr += 50;
140 if (*totfr + 50 > 4096)
141 rescale(m, totfr);
142
143 return 1;
144 }
145
146 static int decode_adaptive45(PixelModel3 *m, int rccode, uint32_t *value,
147 uint16_t *a, uint16_t *b, uint32_t *c, int max)
148 {
149 uint32_t q, g, maxpos, d, e = *c, totfr = *c;
150 int ret;
151
152 for (d = 0; e <= 2048; d++)
153 e <<= 1;
154 maxpos = m->maxpos;
155 rccode >>= d;
156 *c = m->freqs[maxpos];
157 m->freqs[maxpos] += 4096 - e >> d;
158
159 for (q = 0, g = 0, e = 0; q < m->size; q++) {
160 uint32_t f = m->symbols[q];
161 uint32_t p = e + f - g;
162 uint32_t k = m->freqs[q];
163
164 if (rccode < p) {
165 *value = rccode - e + g;
166 *b = rccode << d;
167 *a = 1 << d;
168 m->freqs[maxpos] = *c;
169 ret = add_symbol(m, q, *value, &totfr, max);
170 *c = totfr;
171 return ret;
172 }
173
174 if (p + k > rccode) {
175 *value = f;
176 e += *value - g;
177 *b = e << d;
178 *a = k << d;
179 m->freqs[maxpos] = *c;
180 m->freqs[q] += 50;
181 totfr += 50;
182 if ((q != maxpos) && (m->freqs[q] > m->freqs[maxpos]))
183 m->maxpos = q;
184 if (totfr + 50 > 4096)
185 rescale(m, &totfr);
186 *c = totfr;
187 return 1;
188 }
189
190 e += f - g + k;
191 g = f + 1;
192 }
193
194 m->freqs[maxpos] = *c;
195 *value = g + rccode - e;
196 *b = rccode << d;
197 *a = 1 << d;
198 ret = add_symbol(m, q, *value, &totfr, max);
199 *c = totfr;
200 return ret;
201 }
202
203 static int update_model6_to_7(PixelModel3 *m)
204 {
205 PixelModel3 n = {0};
206 int c, d, e, f, k, p, length, i, j, index;
207 uint16_t *freqs, *freqs1, *cnts;
208
209 n.type = 7;
210
211 length = m->length;
212 freqs = n.freqs;
213 freqs1 = n.freqs1;
214 cnts = n.cnts;
215 n.cntsum = m->cnts[length];
216 for (i = 0; i < length; i++) {
217 if (!m->cnts[i])
218 continue;
219 index = m->symbols[i];
220 freqs[index] = m->freqs[2 * i];
221 freqs1[index] = m->freqs[2 * i + 1];
222 cnts[index] = m->cnts[i];
223 }
224 c = 1 << m->fshift;
225 d = c - (c >> 1);
226 for (j = 0, e = 0; j < 256; j++) {
227 f = freqs[j];
228 if (!f) {
229 f = c;
230 freqs[j] = c;
231 freqs1[j] = e;
232 cnts[j] = d;
233 }
234 p = (e + 127) >> 7;
235 k = ((f + e - 1) >> 7) + 1;
236 if (k > FF_ARRAY_ELEMS(n.dectab))
237 return AVERROR_INVALIDDATA;
238 for (i = 0; i < k - p; i++)
239 n.dectab[p + i] = j;
240 e += f;
241 }
242
243 memcpy(m, &n, sizeof(n));
244
245 return 0;
246 }
247
248 static void calc_sum(PixelModel3 *m)
249 {
250 uint32_t a;
251 int len;
252
253 len = m->length;
254 a = 256 - m->size << (m->fshift > 0 ? m->fshift - 1 : 0);
255 for (int c = 0; c < len; c++)
256 a += m->cnts[c];
257 m->cnts[len] = a;
258 }
259
260 static void rescale_dec(PixelModel3 *m)
261 {
262 uint16_t cnts[256] = {0};
263 uint16_t freqs[512] = {0};
264 int b, c, e, g;
265 uint32_t a;
266
267 for (a = 1 << (0 < m->fshift ? m->fshift - 1 : 0), b = 0; b < 256; b++)
268 cnts[b] = a;
269
270 for (a = 0, b = m->size; a < b; a++)
271 cnts[m->symbols[a]] = m->cnts[a];
272
273 for (b = a = 0; b < 256; b++) {
274 freqs[2 * b] = cnts[b];
275 freqs[2 * b + 1] = a;
276 a += cnts[b];
277 }
278
279 if (m->fshift > 0)
280 m->fshift--;
281
282 a = 256 - m->size << (0 < m->fshift ? m->fshift - 1 : 0);
283 for (b = 0, c = m->size; b < c; b++) {
284 m->cnts[b] -= m->cnts[b] >> 1;
285 a = a + m->cnts[b];
286 e = m->symbols[b];
287 g = freqs[2 * e + 1];
288 m->freqs[2 * b] = freqs[2 * e];
289 m->freqs[2 * b + 1] = g;
290 }
291 m->cnts[m->length] = a;
292 }
293
294 static int update_model5_to_6(PixelModel3 *m, uint8_t value)
295 {
296 PixelModel3 n = {0};
297 int c, d, e, f, g, k, q, p;
298
299 n.type = 6;
300 n.length = 32;
301
302 for (c = m->size, d = 256 - c, e = 0; e < c; e++)
303 d = d + m->freqs[e];
304
305 for (e = 0; d <= 2048; e++)
306 d <<= 1;
307
308 for (q = d = 0, g = q = 0; g < c; g++) {
309 p = m->symbols[g];
310 d = d + (p - q);
311 q = m->freqs[g];
312 k = q << e;
313 n.freqs[2 * g] = k;
314 n.freqs[2 * g + 1] = d << e;
315 n.cnts[g] = k - (k >> 1);
316 n.symbols[g] = p;
317 d += q;
318 q = p + 1;
319 }
320
321 n.fshift = e;
322 e = 1 << n.fshift;
323 d = 0;
324 if (value > 0) {
325 d = -1;
326 for (p = f = g = 0; p < c; p++) {
327 k = n.symbols[p];
328 if (k > d && k < value) {
329 d = k;
330 g = n.freqs[2 * p];
331 f = n.freqs[2 * p + 1];
332 }
333 }
334 d = 0 < g ? f + g + (value - d - 1 << n.fshift) : value << n.fshift;
335 }
336 n.freqs[2 * c] = e;
337 n.freqs[2 * c + 1] = d;
338 n.cnts[c] = e - (e >> 1);
339 n.symbols[c] = value;
340 n.size = c + 1;
341 e = 25 << n.fshift;
342 n.cnts[c] += e;
343 n.cnts[32] += e;
344 if (n.cnts[32] + e > 4096)
345 rescale_dec(&n);
346
347 calc_sum(&n);
348 for (c = 0, e = n.size - 1; c < e; c++) {
349 for (g = c + 1, f = n.size; g < f; g++) {
350 if (q = n.freqs[2 * g], k = n.freqs[2 * c], q > k) {
351 int l = n.freqs[2 * c + 1];
352 int h = n.freqs[2 * g + 1];
353 n.freqs[2 * c] = q;
354 n.freqs[2 * c + 1] = h;
355 n.freqs[2 * g] = k;
356 n.freqs[2 * g + 1] = l;
357 FFSWAP(uint16_t, n.cnts[c], n.cnts[g]);
358 FFSWAP(uint8_t, n.symbols[c], n.symbols[g]);
359 }
360 }
361 }
362
363 memcpy(m, &n, sizeof(n));
364
365 return 0;
366 }
367
368 static void grow_dec(PixelModel3 *m)
369 {
370 int a;
371
372 a = 2 * m->length;
373 m->cnts[2 * m->length] = m->cnts[m->length];
374 m->length = a;
375 }
376
377 static int add_dec(PixelModel3 *m, int sym, int f1, int f2)
378 {
379 int size;
380
381 if (m->size >= 40 || m->size >= m->length)
382 return -1;
383
384 size = m->size;
385 m->symbols[size] = sym;
386 m->freqs[2 * size] = f1;
387 m->freqs[2 * size + 1] = f2;
388 m->cnts[size] = f1 - (f1 >> 1);
389 m->size++;
390
391 return size;
392 }
393
394 static void incr_cntdec(PixelModel3 *m, int a)
395 {
396 int b, len, d, e, g;
397
398 b = 25 << m->fshift;
399 len = m->length;
400 m->cnts[a] += b;
401 m->cnts[len] += b;
402 if (a > 0 && m->cnts[a] > m->cnts[a - 1]) {
403 FFSWAP(uint16_t, m->cnts[a], m->cnts[a - 1]);
404 d = m->freqs[2 * a];
405 e = m->freqs[2 * a + 1];
406 g = m->freqs[2 * (a - 1) + 1];
407 m->freqs[2 * a] = m->freqs[2 * (a - 1)];
408 m->freqs[2 * a + 1] = g;
409 g = a - 1;
410 m->freqs[2 * g] = d;
411 m->freqs[2 * g + 1] = e;
412 FFSWAP(uint8_t, m->symbols[a], m->symbols[a - 1]);
413 }
414
415 if (m->cnts[len] + b > 4096)
416 rescale_dec(m);
417 }
418
419 static int decode_adaptive6(PixelModel3 *m, uint32_t code, uint32_t *value,
420 uint16_t *a, uint16_t *b)
421 {
422 int c, d, e, f, g, q;
423
424 for (c = 0, d = 0, e = 0, f = 0, g = 0, q = m->size; g < q; g++) {
425 uint32_t p = m->freqs[2 * g + 1];
426
427 if (p <= code) {
428 uint32_t k = m->freqs[2 * g];
429
430 if (p + k > code) {
431 *value = m->symbols[g];
432 *a = k;
433 *b = p;
434 incr_cntdec(m, g);
435 return 1;
436 }
437
438 if (p >= d) {
439 c = k;
440 d = p;
441 e = m->symbols[g];
442 }
443 }
444 }
445
446 g = 1 << m->fshift;
447 q = f = 0;
448
449 if (c > 0) {
450 f = code - (d + c) >> m->fshift;
451 q = f + e + 1;
452 f = d + c + (f << m->fshift);
453 } else {
454 q = code >> m->fshift;
455 f = q << m->fshift;
456 }
457
458 *a = g;
459 *b = f;
460 *value = q;
461
462 c = add_dec(m, q, g, f);
463 if (c < 0) {
464 if (m->length == 64)
465 return 0;
466 grow_dec(m);
467 c = add_dec(m, q, g, f);
468 }
469
470 incr_cntdec(m, c);
471 return 1;
472 }
473
474 static int cmpbytes(const void *p1, const void *p2)
475 {
476 int left = *(const uint8_t *)p1;
477 int right = *(const uint8_t *)p2;
478 return FFDIFFSIGN(left, right);
479 }
480
481 static int update_model1_to_2(PixelModel3 *m, uint32_t val)
482 {
483 PixelModel3 n = {0};
484 int i, b;
485
486 n.type = 2;
487 n.size = m->size + 1;
488 b = m->size;
489 for (i = 0; i < b; i++)
490 n.symbols[i] = m->symbols[i];
491 n.symbols[b] = val;
492
493 memcpy(m, &n, sizeof(n));
494
495 return 0;
496 }
497
498 static int update_model1_to_4(PixelModel3 *m, uint32_t val)
499 {
500 PixelModel3 n = {0};
501 int size, i;
502
503 size = m->size;
504 n.type = 4;
505 n.size = size;
506 for (i = 0; i < n.size; i++) {
507 n.symbols[i] = m->symbols[i];
508 }
509 AV_QSORT(n.symbols, size, uint8_t, cmpbytes);
510 for (i = 0; i < n.size; i++) {
511 if (val == n.symbols[i]) {
512 n.freqs[i] = 100;
513 n.maxpos = i;
514 } else {
515 n.freqs[i] = 50;
516 }
517 }
518
519 memcpy(m, &n, sizeof(n));
520
521 return 0;
522 }
523
524 static int update_model1_to_5(PixelModel3 *m, uint32_t val)
525 {
526 int i, size, freqs;
527 uint32_t a;
528
529 update_model1_to_4(m, val);
530 size = m->size;
531 a = 256 - size;
532 for (i = 0; i < size; i++, a += freqs)
533 freqs = m->freqs[i];
534 m->type = 5;
535 m->cntsum = a;
536
537 return 0;
538 }
539
540 static int decode_static1(PixelModel3 *m, uint32_t val)
541 {
542 uint32_t size;
543
544 size = m->size;
545 for (int i = 0; i < size; i++) {
546 if (val == m->symbols[i]) {
547 if (size <= 4)
548 return update_model1_to_4(m, val);
549 else
550 return update_model1_to_5(m, val);
551 }
552 }
553
554 if (size >= 14)
555 return update_model1_to_2(m, val);
556
557 m->symbols[size] = val;
558 m->size++;
559 return 0;
560 }
561
562 static int update_model2_to_6(PixelModel3 *m, uint8_t value, int a4)
563 {
564 PixelModel3 n = {0};
565 int c, d, e, f, g, q;
566
567 n.type = 6;
568 n.length = a4;
569
570 memset(n.symbols, 1u, a4);
571
572 c = m->size;
573 d = 256 - c + (64 * c + 64);
574 for (e = 0; d <= 2048; e++) {
575 d <<= 1;
576 }
577
578 g = q = 0;
579 AV_QSORT(m->symbols, c, uint8_t, cmpbytes);
580 for (f = d = 0; f < c; f++) {
581 int p = f;
582 int k = m->symbols[p];
583 int l;
584 g = g + (k - q);
585
586 if (k == value) {
587 d = p;
588 q = 128;
589 } else {
590 q = 64;
591 }
592 l = q << e;
593 n.freqs[2 * p] = l;
594 n.freqs[2 * p + 1] = g << e;
595 n.symbols[p] = k;
596 n.cnts[p] = l - (l >> 1);
597 g += q;
598 q = k + 1;
599 }
600 n.size = c;
601 n.fshift = e;
602 calc_sum(&n);
603
604 if (d > 0) {
605 c = n.freqs[0];
606 e = n.freqs[1];
607 g = n.freqs[2 * d + 1];
608 n.freqs[0] = n.freqs[2 * d];
609 n.freqs[1] = g;
610 n.freqs[2 * d] = c;
611 n.freqs[2 * d + 1] = e;
612 FFSWAP(uint16_t, n.cnts[0], n.cnts[d]);
613 FFSWAP(uint8_t, n.symbols[0], n.symbols[d]);
614 }
615
616 memcpy(m, &n, sizeof(n));
617
618 return 0;
619 }
620
621 static int update_model2_to_3(PixelModel3 *m, uint32_t val)
622 {
623 PixelModel3 n = {0};
624 uint32_t size;
625
626 n.type = 3;
627 n.size = m->size + 1;
628
629 size = m->size;
630 for (int i = 0; i < size; i++)
631 n.symbols[i] = m->symbols[i];
632 n.symbols[size] = val;
633
634 memcpy(m, &n, sizeof(n));
635
636 return 0;
637 }
638
639 static int decode_static2(PixelModel3 *m, uint32_t val)
640 {
641 uint32_t size;
642
643 size = m->size;
644 for (int i = 0; i < size; i++) {
645 if (val == m->symbols[i]) {
646 int a;
647
648 if (m->size <= 32)
649 a = 32;
650 else
651 a = 64;
652 return update_model2_to_6(m, val, a);
653 }
654 }
655
656 if (size >= 64)
657 return update_model2_to_3(m, val);
658
659 m->symbols[size] = val;
660 m->size++;
661
662 return 0;
663 }
664
665 static int update_model3_to_7(PixelModel3 *m, uint8_t value)
666 {
667 PixelModel3 n = {0};
668 int c, d, e, f, g, q;
669
670 n.type = 7;
671
672 for (c = 0; c < 256; c++) {
673 d = c;
674 n.freqs[d] = 1;
675 n.cnts[d] = 1;
676 }
677
678 for (c = m->size, d = (4096 - (256 - c)) / (c + 1) | 0, e = d - (d >> 1), g = 0; g < c;) {
679 q = g++;
680 q = m->symbols[q];
681 n.freqs[q] = d;
682 n.cnts[q] = e;
683 }
684 n.freqs[value] += d;
685 n.cnts[value] += 16;
686 for (d = c = n.cntsum = 0; 256 > d; d++) {
687 e = d;
688 n.cntsum += n.cnts[e];
689 n.freqs1[e] = c;
690 g = n.freqs[e];
691 f = (c + g - 1 >> 7) + 1;
692 if (f > FF_ARRAY_ELEMS(n.dectab))
693 return AVERROR_INVALIDDATA;
694 for (q = c + 128 - 1 >> 7; q < f; q++) {
695 n.dectab[q] = e;
696 }
697 c += g;
698 }
699
700 memcpy(m, &n, sizeof(n));
701
702 return 0;
703 }
704
705 static int decode_static3(PixelModel3 *m, uint32_t val)
706 {
707 uint32_t size = m->size;
708
709 for (int i = 0; i < size; i++) {
710 if (val == m->symbols[i])
711 return update_model3_to_7(m, val);
712 }
713
714 if (size >= 256)
715 return 0;
716
717 m->symbols[size] = val;
718 m->size++;
719 return 0;
720 }
721
722 static void sync_code3(GetByteContext *gb, RangeCoder *rc)
723 {
724 rc->code1++;
725 if (rc->code1 == 0x20000) {
726 rc->code = bytestream2_get_le32(gb);
727 rc->code1 = 0;
728 }
729 }
730
731 static int decode_value3(SCPRContext *s, uint32_t max, uint32_t *cntsum,
732 uint16_t *freqs1, uint16_t *freqs2,
733 uint16_t *cnts, uint8_t *dectable,
734 uint32_t *value)
735 {
736 GetByteContext *gb = &s->gb;
737 RangeCoder *rc = &s->rc;
738 uint32_t r, y, a, b, e, g, q;
739
740 r = dectable[(rc->code & 0xFFFu) >> 7];
741 if (r < max) {
742 while (freqs2[r + 1] <= (rc->code & 0xFFF)) {
743 if (++r >= max)
744 break;
745 }
746 }
747
748 if (r > max)
749 return AVERROR_INVALIDDATA;
750
751 cnts[r] += 16;
752 a = freqs1[r];
753 b = freqs2[r];
754 *cntsum += 16;
755 if (*cntsum + 16 > 4096) {
756 *cntsum = 0;
757 for (int c = 0, i = 0; i < max + 1; i++) {
758 e = cnts[i];
759 freqs2[i] = c;
760 freqs1[i] = e;
761 g = (c + 127) >> 7;
762 c += e;
763 q = ((c - 1) >> 7) + 1;
764 if (q > g) {
765 for (int j = 0; j < q - g; j++)
766 dectable[j + g] = i;
767 }
768 y = e - (e >> 1);
769 cnts[i] = y;
770 *cntsum += y;
771 }
772 }
773
774 decode3(gb, rc, a, b);
775 sync_code3(gb, rc);
776
777 *value = r;
778
779 return 0;
780 }
781
782 static void calc_sum5(PixelModel3 *m)
783 {
784 uint32_t a;
785
786 a = 256 - m->size;
787 for (int b = 0; b < m->size; b++)
788 a += m->freqs[b];
789 m->cntsum = a;
790 }
791
792 static int update_model4_to_5(PixelModel3 *m, uint32_t value)
793 {
794 PixelModel3 n = {0};
795 int c, e, g, totfr;
796
797 n.type = 5;
798
799 for (c = 0, e = 0; c < m->size && m->symbols[c] < value; c++) {
800 n.symbols[c] = m->symbols[c];
801 e += n.freqs[c] = m->freqs[c];
802 }
803
804 g = c;
805 n.symbols[g] = value;
806 e += n.freqs[g++] = 50;
807 for (; c < m->size; g++, c++) {
808 n.symbols[g] = m->symbols[c];
809 e += n.freqs[g] = m->freqs[c];
810 }
811 n.size = m->size + 1;
812 if (e > 4096)
813 rescale(&n, &totfr);
814
815 calc_sum5(&n);
816
817 memcpy(m, &n, sizeof(n));
818
819 return 0;
820 }
821
822 static int decode_unit3(SCPRContext *s, PixelModel3 *m, uint32_t code, uint32_t *value)
823 {
824 GetByteContext *gb = &s->gb;
825 RangeCoder *rc = &s->rc;
826 uint16_t a = 0, b = 0;
827 uint32_t param;
828 int type;
829 int ret;
830
831 type = m->type;
832 switch (type) {
833 case 0:
834 *value = bytestream2_get_byte(&s->gb);
835 m->type = 1;
836 m->size = 1;
837 m->symbols[0] = *value;
838 sync_code3(gb, rc);
839 break;
840 case 1:
841 *value = bytestream2_get_byte(&s->gb);
842 decode_static1(m, *value);
843 sync_code3(gb, rc);
844 break;
845 case 2:
846 *value = bytestream2_get_byte(&s->gb);
847 decode_static2(m, *value);
848 sync_code3(gb, rc);
849 break;
850 case 3:
851 *value = bytestream2_get_byte(&s->gb);
852 ret = decode_static3(m, *value);
853 if (ret < 0)
854 return AVERROR_INVALIDDATA;
855 sync_code3(gb, rc);
856 break;
857 case 4:
858 param = m->freqs[0] + m->freqs[1] + m->freqs[2] + m->freqs[3] + 256 - m->size;
859 if (!decode_adaptive45(m, code, value, &a, &b, &param, 4))
860 update_model4_to_5(m, *value);
861 decode3(gb, rc, a, b);
862 sync_code3(gb, rc);
863 break;
864 case 5:
865 if (!decode_adaptive45(m, code, value, &a, &b, &m->cntsum, 16))
866 update_model5_to_6(m, *value);
867 decode3(gb, rc, a, b);
868 sync_code3(gb, rc);
869 break;
870 case 6:
871 if (!decode_adaptive6(m, code, value, &a, &b)) {
872 ret = update_model6_to_7(m);
873 if (ret < 0)
874 return AVERROR_INVALIDDATA;
875 }
876 decode3(gb, rc, a, b);
877 sync_code3(gb, rc);
878 break;
879 case 7:
880 return decode_value3(s, 255, &m->cntsum,
881 m->freqs, m->freqs1,
882 m->cnts, m->dectab, value);
883 }
884
885 if (*value > 255)
886 return AVERROR_INVALIDDATA;
887
888 return 0;
889 }
890
891 static int decode_units3(SCPRContext * s, uint32_t *red,
892 uint32_t *green, uint32_t *blue,
893 int *cx, int *cx1)
894 {
895 RangeCoder *rc = &s->rc;
896 int ret;
897
898 ret = decode_unit3(s, &s->pixel_model3[0][*cx + *cx1], rc->code & 0xFFF, red);
899 if (ret < 0)
900 return ret;
901
902 *cx1 = (*cx << 6) & 0xFC0;
903 *cx = *red >> 2;
904
905 ret = decode_unit3(s, &s->pixel_model3[1][*cx + *cx1], rc->code & 0xFFF, green);
906 if (ret < 0)
907 return ret;
908
909 *cx1 = (*cx << 6) & 0xFC0;
910 *cx = *green >> 2;
911
912 ret = decode_unit3(s, &s->pixel_model3[2][*cx + *cx1], rc->code & 0xFFF, blue);
913 if (ret < 0)
914 return ret;
915
916 *cx1 = (*cx << 6) & 0xFC0;
917 *cx = *blue >> 2;
918
919 return 0;
920 }
921
922 static void init_rangecoder3(RangeCoder *rc, GetByteContext *gb)
923 {
924 rc->code = bytestream2_get_le32(gb);
925 rc->code1 = 0;
926 }
927
928 static int decompress_i3(AVCodecContext *avctx, uint32_t *dst, int linesize)
929 {
930 SCPRContext *s = avctx->priv_data;
931 GetByteContext *gb = &s->gb;
932 RangeCoder *rc = &s->rc;
933 int cx = 0, cx1 = 0, k = 0;
934 int run, off, y = 0, x = 0, ret;
935 uint32_t backstep = linesize - avctx->width;
936 uint32_t clr = 0, lx, ly, ptype, r, g, b;
937
938 bytestream2_skip(gb, 1);
939 init_rangecoder3(rc, gb);
940 reinit_tables3(s);
941
942 while (k < avctx->width + 1) {
943 ret = decode_units3(s, &r, &g, &b, &cx, &cx1);
944 if (ret < 0)
945 return ret;
946 ret = decode_value3(s, 255, &s->run_model3[0].cntsum,
947 s->run_model3[0].freqs[0],
948 s->run_model3[0].freqs[1],
949 s->run_model3[0].cnts,
950 s->run_model3[0].dectab, &run);
951 if (ret < 0)
952 return ret;
953 if (run <= 0)
954 return AVERROR_INVALIDDATA;
955
956 clr = (b << 16) + (g << 8) + r;
957 k += run;
958 while (run-- > 0) {
959 if (y >= avctx->height)
960 return AVERROR_INVALIDDATA;
961
962 dst[y * linesize + x] = clr;
963 lx = x;
964 ly = y;
965 x++;
966 if (x >= avctx->width) {
967 x = 0;
968 y++;
969 }
970 }
971 }
972 off = -linesize - 1;
973 ptype = 0;
974
975 while (x < avctx->width && y < avctx->height) {
976 ret = decode_value3(s, 5, &s->op_model3[ptype].cntsum,
977 s->op_model3[ptype].freqs[0],
978 s->op_model3[ptype].freqs[1],
979 s->op_model3[ptype].cnts,
980 s->op_model3[ptype].dectab, &ptype);
981 if (ret < 0)
982 return ret;
983 if (ptype == 0) {
984 ret = decode_units3(s, &r, &g, &b, &cx, &cx1);
985 if (ret < 0)
986 return ret;
987 clr = (b << 16) + (g << 8) + r;
988 }
989 if (ptype > 5)
990 return AVERROR_INVALIDDATA;
991 ret = decode_value3(s, 255, &s->run_model3[ptype].cntsum,
992 s->run_model3[ptype].freqs[0],
993 s->run_model3[ptype].freqs[1],
994 s->run_model3[ptype].cnts,
995 s->run_model3[ptype].dectab, &run);
996 if (ret < 0)
997 return ret;
998 if (run <= 0)
999 return AVERROR_INVALIDDATA;
1000
1001 ret = decode_run_i(avctx, ptype, run, &x, &y, clr,
1002 dst, linesize, &lx, &ly,
1003 backstep, off, &cx, &cx1);
1004 if (ret < 0)
1005 return ret;
1006 }
1007
1008 return 0;
1009 }
1010
1011 static int decompress_p3(AVCodecContext *avctx,
1012 uint32_t *dst, int linesize,
1013 uint32_t *prev, int plinesize)
1014 {
1015 SCPRContext *s = avctx->priv_data;
1016 GetByteContext *gb = &s->gb;
1017 int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
1018 int backstep = linesize - avctx->width;
1019 int mvx = 0, mvy = 0;
1020
1021 if (bytestream2_get_byte(gb) == 0)
1022 return 1;
1023 init_rangecoder3(&s->rc, gb);
1024
1025 ret = decode_value3(s, 255, &s->range_model3.cntsum,
1026 s->range_model3.freqs[0],
1027 s->range_model3.freqs[1],
1028 s->range_model3.cnts,
1029 s->range_model3.dectab, &min);
1030 ret |= decode_value3(s, 255, &s->range_model3.cntsum,
1031 s->range_model3.freqs[0],
1032 s->range_model3.freqs[1],
1033 s->range_model3.cnts,
1034 s->range_model3.dectab, &temp);
1035 if (ret < 0)
1036 return ret;
1037
1038 min += temp << 8;
1039 ret |= decode_value3(s, 255, &s->range_model3.cntsum,
1040 s->range_model3.freqs[0],
1041 s->range_model3.freqs[1],
1042 s->range_model3.cnts,
1043 s->range_model3.dectab, &max);
1044 ret |= decode_value3(s, 255, &s->range_model3.cntsum,
1045 s->range_model3.freqs[0],
1046 s->range_model3.freqs[1],
1047 s->range_model3.cnts,
1048 s->range_model3.dectab, &temp);
1049 if (ret < 0)
1050 return ret;
1051
1052 max += temp << 8;
1053 if (min > max || min >= s->nbcount)
1054 return AVERROR_INVALIDDATA;
1055
1056 memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
1057
1058 while (min <= max) {
1059 int fill, count;
1060
1061 ret = decode_value3(s, 4, &s->fill_model3.cntsum,
1062 s->fill_model3.freqs[0],
1063 s->fill_model3.freqs[1],
1064 s->fill_model3.cnts,
1065 s->fill_model3.dectab, &fill);
1066 ret |= decode_value3(s, 255, &s->count_model3.cntsum,
1067 s->count_model3.freqs[0],
1068 s->count_model3.freqs[1],
1069 s->count_model3.cnts,
1070 s->count_model3.dectab, &count);
1071 if (ret < 0)
1072 return ret;
1073 if (count <= 0)
1074 return AVERROR_INVALIDDATA;
1075
1076 while (min < s->nbcount && count-- > 0) {
1077 s->blocks[min++] = fill;
1078 }
1079 }
1080
1081 ret = av_frame_copy(s->current_frame, s->last_frame);
1082 if (ret < 0)
1083 return ret;
1084
1085 for (y = 0; y < s->nby; y++) {
1086 for (x = 0; x < s->nbx; x++) {
1087 int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
1088
1089 if (s->blocks[y * s->nbx + x] == 0)
1090 continue;
1091
1092 if (((s->blocks[y * s->nbx + x] + 1) & 1) > 0) {
1093 ret = decode_value3(s, 15, &s->sxy_model3[0].cntsum,
1094 s->sxy_model3[0].freqs[0],
1095 s->sxy_model3[0].freqs[1],
1096 s->sxy_model3[0].cnts,
1097 s->sxy_model3[0].dectab, &sx1);
1098 ret |= decode_value3(s, 15, &s->sxy_model3[1].cntsum,
1099 s->sxy_model3[1].freqs[0],
1100 s->sxy_model3[1].freqs[1],
1101 s->sxy_model3[1].cnts,
1102 s->sxy_model3[1].dectab, &sy1);
1103 ret |= decode_value3(s, 15, &s->sxy_model3[2].cntsum,
1104 s->sxy_model3[2].freqs[0],
1105 s->sxy_model3[2].freqs[1],
1106 s->sxy_model3[2].cnts,
1107 s->sxy_model3[2].dectab, &sx2);
1108 ret |= decode_value3(s, 15, &s->sxy_model3[3].cntsum,
1109 s->sxy_model3[3].freqs[0],
1110 s->sxy_model3[3].freqs[1],
1111 s->sxy_model3[3].cnts,
1112 s->sxy_model3[3].dectab, &sy2);
1113 if (ret < 0)
1114 return ret;
1115
1116 sx2++;
1117 sy2++;
1118 }
1119 if (((s->blocks[y * s->nbx + x] + 3) & 2) > 0) {
1120 int i, a, b, c, j, by = y * 16, bx = x * 16;
1121 uint32_t code;
1122
1123 a = s->rc.code & 0xFFF;
1124 c = 1;
1125
1126 if (a < 0x800)
1127 c = 0;
1128 b = 2048;
1129 if (!c)
1130 b = 0;
1131
1132 code = a + ((s->rc.code >> 1) & 0xFFFFF800) - b;
1133 while (code < 0x800000 && bytestream2_get_bytes_left(gb) > 0)
1134 code = bytestream2_get_byteu(gb) | (code << 8);
1135 s->rc.code = code;
1136
1137 sync_code3(gb, &s->rc);
1138
1139 if (!c) {
1140 ret = decode_value3(s, 511, &s->mv_model3[0].cntsum,
1141 s->mv_model3[0].freqs[0],
1142 s->mv_model3[0].freqs[1],
1143 s->mv_model3[0].cnts,
1144 s->mv_model3[0].dectab, &mvx);
1145 ret |= decode_value3(s, 511, &s->mv_model3[1].cntsum,
1146 s->mv_model3[1].freqs[0],
1147 s->mv_model3[1].freqs[1],
1148 s->mv_model3[1].cnts,
1149 s->mv_model3[1].dectab, &mvy);
1150 if (ret < 0)
1151 return ret;
1152
1153 mvx -= 256;
1154 mvy -= 256;
1155 }
1156
1157 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
1158 by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
1159 return AVERROR_INVALIDDATA;
1160
1161 for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
1162 for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
1163 dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
1164 }
1165 }
1166 } else {
1167 int run, bx = x * 16 + sx1, by = y * 16 + sy1;
1168 uint32_t clr, ptype = 0, r, g, b;
1169
1170 if (bx >= avctx->width)
1171 return AVERROR_INVALIDDATA;
1172
1173 for (; by < y * 16 + sy2 && by < avctx->height;) {
1174 ret = decode_value3(s, 5, &s->op_model3[ptype].cntsum,
1175 s->op_model3[ptype].freqs[0],
1176 s->op_model3[ptype].freqs[1],
1177 s->op_model3[ptype].cnts,
1178 s->op_model3[ptype].dectab, &ptype);
1179 if (ret < 0)
1180 return ret;
1181 if (ptype == 0) {
1182 ret = decode_units3(s, &r, &g, &b, &cx, &cx1);
1183 if (ret < 0)
1184 return ret;
1185
1186 clr = (b << 16) + (g << 8) + r;
1187 }
1188 if (ptype > 5)
1189 return AVERROR_INVALIDDATA;
1190 ret = decode_value3(s, 255, &s->run_model3[ptype].cntsum,
1191 s->run_model3[ptype].freqs[0],
1192 s->run_model3[ptype].freqs[1],
1193 s->run_model3[ptype].cnts,
1194 s->run_model3[ptype].dectab, &run);
1195 if (ret < 0)
1196 return ret;
1197 if (run <= 0)
1198 return AVERROR_INVALIDDATA;
1199
1200 ret = decode_run_p(avctx, ptype, run, x, y, clr,
1201 dst, prev, linesize, plinesize, &bx, &by,
1202 backstep, sx1, sx2, &cx, &cx1);
1203 if (ret < 0)
1204 return ret;
1205 }
1206 }
1207 }
1208 }
1209
1210 return 0;
1211 }
1212