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