FFmpeg  4.4.5
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 
68  renew_table3(256, &s->range_model3.cntsum,
69  s->range_model3.freqs[0], s->range_model3.freqs[1],
70  s->range_model3.cnts, s->range_model3.dectab);
71 
72  renew_table3(5, &s->fill_model3.cntsum,
73  s->fill_model3.freqs[0], s->fill_model3.freqs[1],
74  s->fill_model3.cnts, s->fill_model3.dectab);
75 
76  renew_table3(256, &s->count_model3.cntsum,
77  s->count_model3.freqs[0], s->count_model3.freqs[1],
78  s->count_model3.cnts, s->count_model3.dectab);
79 
80  for (int i = 0; i < 4; i++) {
81  renew_table3(16, &s->sxy_model3[i].cntsum,
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++) {
93  renew_table3(6, &s->op_model3[i].cntsum,
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  }
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  int i, size, freqs;
530  uint32_t a;
531 
533  size = m->size;
534  a = 256 - size;
535  for (i = 0; i < size; i++, a += freqs)
536  freqs = m->freqs[i];
537  m->type = 5;
538  m->cntsum = a;
539 
540  return 0;
541 }
542 
543 static int decode_static1(PixelModel3 *m, uint32_t val)
544 {
545  uint32_t size;
546 
547  size = m->size;
548  for (int i = 0; i < size; i++) {
549  if (val == m->symbols[i]) {
550  if (size <= 4)
551  return update_model1_to_4(m, val);
552  else
553  return update_model1_to_5(m, val);
554  }
555  }
556 
557  if (size >= 14)
558  return update_model1_to_2(m, val);
559 
560  m->symbols[size] = val;
561  m->size++;
562  return 0;
563 }
564 
566 {
567  PixelModel3 n = {0};
568  int c, d, e, f, g, q;
569 
570  n.type = 6;
571  n.length = a4;
572 
573  memset(n.symbols, 1u, a4);
574 
575  c = m->size;
576  d = 256 - c + (64 * c + 64);
577  for (e = 0; d <= 2048; e++) {
578  d <<= 1;
579  }
580 
581  g = q = 0;
583  for (f = d = 0; f < c; f++) {
584  int p = f;
585  int k = m->symbols[p];
586  int l;
587  g = g + (k - q);
588 
589  if (k == value) {
590  d = p;
591  q = 128;
592  } else {
593  q = 64;
594  }
595  l = q << e;
596  n.freqs[2 * p] = l;
597  n.freqs[2 * p + 1] = g << e;
598  n.symbols[p] = k;
599  n.cnts[p] = l - (l >> 1);
600  g += q;
601  q = k + 1;
602  }
603  n.size = c;
604  n.fshift = e;
605  calc_sum(&n);
606 
607  if (d > 0) {
608  c = n.freqs[0];
609  e = n.freqs[1];
610  g = n.freqs[2 * d + 1];
611  n.freqs[0] = n.freqs[2 * d];
612  n.freqs[1] = g;
613  n.freqs[2 * d] = c;
614  n.freqs[2 * d + 1] = e;
615  FFSWAP(uint16_t, n.cnts[0], n.cnts[d]);
616  FFSWAP(uint8_t, n.symbols[0], n.symbols[d]);
617  }
618 
619  memcpy(m, &n, sizeof(n));
620 
621  return 0;
622 }
623 
624 static int update_model2_to_3(PixelModel3 *m, uint32_t val)
625 {
626  PixelModel3 n = {0};
627  uint32_t size;
628 
629  n.type = 3;
630  n.size = m->size + 1;
631 
632  size = m->size;
633  for (int i = 0; i < size; i++)
634  n.symbols[i] = m->symbols[i];
635  n.symbols[size] = val;
636 
637  memcpy(m, &n, sizeof(n));
638 
639  return 0;
640 }
641 
642 static int decode_static2(PixelModel3 *m, uint32_t val)
643 {
644  uint32_t size;
645 
646  size = m->size;
647  for (int i = 0; i < size; i++) {
648  if (val == m->symbols[i]) {
649  int a;
650 
651  if (m->size <= 32)
652  a = 32;
653  else
654  a = 64;
655  return update_model2_to_6(m, val, a);
656  }
657  }
658 
659  if (size >= 64)
660  return update_model2_to_3(m, val);
661 
662  m->symbols[size] = val;
663  m->size++;
664 
665  return 0;
666 }
667 
669 {
670  PixelModel3 n = {0};
671  int c, d, e, f, g, q;
672 
673  n.type = 7;
674 
675  for (c = 0; c < 256; c++) {
676  d = c;
677  n.freqs[d] = 1;
678  n.cnts[d] = 1;
679  }
680 
681  for (c = m->size, d = (4096 - (256 - c)) / (c + 1) | 0, e = d - (d >> 1), g = 0; g < c;) {
682  q = g++;
683  q = m->symbols[q];
684  n.freqs[q] = d;
685  n.cnts[q] = e;
686  }
687  n.freqs[value] += d;
688  n.cnts[value] += 16;
689  for (d = c = n.cntsum = 0; 256 > d; d++) {
690  e = d;
691  n.cntsum += n.cnts[e];
692  n.freqs1[e] = c;
693  g = n.freqs[e];
694  f = (c + g - 1 >> 7) + 1;
695  if (f > FF_ARRAY_ELEMS(n.dectab))
696  return AVERROR_INVALIDDATA;
697  for (q = c + 128 - 1 >> 7; q < f; q++) {
698  n.dectab[q] = e;
699  }
700  c += g;
701  }
702 
703  memcpy(m, &n, sizeof(n));
704 
705  return 0;
706 }
707 
708 static int decode_static3(PixelModel3 *m, uint32_t val)
709 {
710  uint32_t size = m->size;
711 
712  for (int i = 0; i < size; i++) {
713  if (val == m->symbols[i])
714  return update_model3_to_7(m, val);
715  }
716 
717  if (size >= 256)
718  return 0;
719 
720  m->symbols[size] = val;
721  m->size++;
722  return 0;
723 }
724 
725 static void sync_code3(GetByteContext *gb, RangeCoder *rc)
726 {
727  rc->code1++;
728  if (rc->code1 == 0x20000) {
729  rc->code = bytestream2_get_le32(gb);
730  rc->code1 = 0;
731  }
732 }
733 
734 static int decode_value3(SCPRContext *s, uint32_t max, uint32_t *cntsum,
735  uint16_t *freqs1, uint16_t *freqs2,
736  uint16_t *cnts, uint8_t *dectable,
737  uint32_t *value)
738 {
739  GetByteContext *gb = &s->gb;
740  RangeCoder *rc = &s->rc;
741  uint32_t r, y, a, b, e, g, q;
742 
743  r = dectable[(rc->code & 0xFFFu) >> 7];
744  if (r < max) {
745  while (freqs2[r + 1] <= (rc->code & 0xFFF)) {
746  if (++r >= max)
747  break;
748  }
749  }
750 
751  if (r > max)
752  return AVERROR_INVALIDDATA;
753 
754  cnts[r] += 16;
755  a = freqs1[r];
756  b = freqs2[r];
757  *cntsum += 16;
758  if (*cntsum + 16 > 4096) {
759  *cntsum = 0;
760  for (int c = 0, i = 0; i < max + 1; i++) {
761  e = cnts[i];
762  freqs2[i] = c;
763  freqs1[i] = e;
764  g = (c + 127) >> 7;
765  c += e;
766  q = ((c - 1) >> 7) + 1;
767  if (q > g) {
768  for (int j = 0; j < q - g; j++)
769  dectable[j + g] = i;
770  }
771  y = e - (e >> 1);
772  cnts[i] = y;
773  *cntsum += y;
774  }
775  }
776 
777  decode3(gb, rc, a, b);
778  sync_code3(gb, rc);
779 
780  *value = r;
781 
782  return 0;
783 }
784 
785 static void calc_sum5(PixelModel3 *m)
786 {
787  uint32_t a;
788 
789  a = 256 - m->size;
790  for (int b = 0; b < m->size; b++)
791  a += m->freqs[b];
792  m->cntsum = a;
793 }
794 
795 static int update_model4_to_5(PixelModel3 *m, uint32_t value)
796 {
797  PixelModel3 n = {0};
798  int c, e, g, totfr;
799 
800  n.type = 5;
801 
802  for (c = 0, e = 0; c < m->size && m->symbols[c] < value; c++) {
803  n.symbols[c] = m->symbols[c];
804  e += n.freqs[c] = m->freqs[c];
805  }
806 
807  g = c;
808  n.symbols[g] = value;
809  e += n.freqs[g++] = 50;
810  for (; c < m->size; g++, c++) {
811  n.symbols[g] = m->symbols[c];
812  e += n.freqs[g] = m->freqs[c];
813  }
814  n.size = m->size + 1;
815  if (e > 4096)
816  rescale(&n, &totfr);
817 
818  calc_sum5(&n);
819 
820  memcpy(m, &n, sizeof(n));
821 
822  return 0;
823 }
824 
825 static int decode_unit3(SCPRContext *s, PixelModel3 *m, uint32_t code, uint32_t *value)
826 {
827  GetByteContext *gb = &s->gb;
828  RangeCoder *rc = &s->rc;
829  uint16_t a = 0, b = 0;
830  uint32_t param;
831  int type;
832  int ret;
833 
834  type = m->type;
835  switch (type) {
836  case 0:
837  *value = bytestream2_get_byte(&s->gb);
838  m->type = 1;
839  m->size = 1;
840  m->symbols[0] = *value;
841  sync_code3(gb, rc);
842  break;
843  case 1:
844  *value = bytestream2_get_byte(&s->gb);
845  decode_static1(m, *value);
846  sync_code3(gb, rc);
847  break;
848  case 2:
849  *value = bytestream2_get_byte(&s->gb);
850  decode_static2(m, *value);
851  sync_code3(gb, rc);
852  break;
853  case 3:
854  *value = bytestream2_get_byte(&s->gb);
855  ret = decode_static3(m, *value);
856  if (ret < 0)
857  return AVERROR_INVALIDDATA;
858  sync_code3(gb, rc);
859  break;
860  case 4:
861  param = m->freqs[0] + m->freqs[1] + m->freqs[2] + m->freqs[3] + 256 - m->size;
862  if (!decode_adaptive45(m, code, value, &a, &b, &param, 4))
864  decode3(gb, rc, a, b);
865  sync_code3(gb, rc);
866  break;
867  case 5:
868  if (!decode_adaptive45(m, code, value, &a, &b, &m->cntsum, 16))
870  decode3(gb, rc, a, b);
871  sync_code3(gb, rc);
872  break;
873  case 6:
874  ret = decode_adaptive6(m, code, value, &a, &b);
875  if (!ret)
876  ret = update_model6_to_7(m);
877  if (ret < 0)
878  return ret;
879  decode3(gb, rc, a, b);
880  sync_code3(gb, rc);
881  break;
882  case 7:
883  return decode_value3(s, 255, &m->cntsum,
884  m->freqs, m->freqs1,
885  m->cnts, m->dectab, value);
886  }
887 
888  if (*value > 255)
889  return AVERROR_INVALIDDATA;
890 
891  return 0;
892 }
893 
894 static int decode_units3(SCPRContext * s, uint32_t *red,
895  uint32_t *green, uint32_t *blue,
896  int *cx, int *cx1)
897 {
898  RangeCoder *rc = &s->rc;
899  int ret;
900 
901  ret = decode_unit3(s, &s->pixel_model3[0][*cx + *cx1], rc->code & 0xFFF, red);
902  if (ret < 0)
903  return ret;
904 
905  *cx1 = (*cx << 6) & 0xFC0;
906  *cx = *red >> 2;
907 
908  ret = decode_unit3(s, &s->pixel_model3[1][*cx + *cx1], rc->code & 0xFFF, green);
909  if (ret < 0)
910  return ret;
911 
912  *cx1 = (*cx << 6) & 0xFC0;
913  *cx = *green >> 2;
914 
915  ret = decode_unit3(s, &s->pixel_model3[2][*cx + *cx1], rc->code & 0xFFF, blue);
916  if (ret < 0)
917  return ret;
918 
919  *cx1 = (*cx << 6) & 0xFC0;
920  *cx = *blue >> 2;
921 
922  return 0;
923 }
924 
926 {
927  rc->code = bytestream2_get_le32(gb);
928  rc->code1 = 0;
929 }
930 
931 static int decompress_i3(AVCodecContext *avctx, uint32_t *dst, int linesize)
932 {
933  SCPRContext *s = avctx->priv_data;
934  GetByteContext *gb = &s->gb;
935  RangeCoder *rc = &s->rc;
936  int cx = 0, cx1 = 0, k = 0;
937  int run, off, y = 0, x = 0, ret;
938  uint32_t backstep = linesize - avctx->width;
939  uint32_t clr = 0, lx, ly, ptype, r, g, b;
940 
941  bytestream2_skip(gb, 1);
942  init_rangecoder3(rc, gb);
943  reinit_tables3(s);
944 
945  while (k < avctx->width + 1) {
946  ret = decode_units3(s, &r, &g, &b, &cx, &cx1);
947  if (ret < 0)
948  return ret;
949  ret = decode_value3(s, 255, &s->run_model3[0].cntsum,
950  s->run_model3[0].freqs[0],
951  s->run_model3[0].freqs[1],
952  s->run_model3[0].cnts,
953  s->run_model3[0].dectab, &run);
954  if (ret < 0)
955  return ret;
956  if (run <= 0)
957  return AVERROR_INVALIDDATA;
958 
959  clr = (b << 16) + (g << 8) + r;
960  k += run;
961  while (run-- > 0) {
962  if (y >= avctx->height)
963  return AVERROR_INVALIDDATA;
964 
965  dst[y * linesize + x] = clr;
966  lx = x;
967  ly = y;
968  x++;
969  if (x >= avctx->width) {
970  x = 0;
971  y++;
972  }
973  }
974  }
975  off = -linesize - 1;
976  ptype = 0;
977 
978  while (x < avctx->width && y < avctx->height) {
979  ret = decode_value3(s, 5, &s->op_model3[ptype].cntsum,
980  s->op_model3[ptype].freqs[0],
981  s->op_model3[ptype].freqs[1],
982  s->op_model3[ptype].cnts,
983  s->op_model3[ptype].dectab, &ptype);
984  if (ret < 0)
985  return ret;
986  if (ptype == 0) {
987  ret = decode_units3(s, &r, &g, &b, &cx, &cx1);
988  if (ret < 0)
989  return ret;
990  clr = (b << 16) + (g << 8) + r;
991  }
992  if (ptype > 5)
993  return AVERROR_INVALIDDATA;
994  ret = decode_value3(s, 255, &s->run_model3[ptype].cntsum,
995  s->run_model3[ptype].freqs[0],
996  s->run_model3[ptype].freqs[1],
997  s->run_model3[ptype].cnts,
998  s->run_model3[ptype].dectab, &run);
999  if (ret < 0)
1000  return ret;
1001  if (run <= 0)
1002  return AVERROR_INVALIDDATA;
1003 
1004  ret = decode_run_i(avctx, ptype, run, &x, &y, clr,
1005  dst, linesize, &lx, &ly,
1006  backstep, off, &cx, &cx1);
1007  if (ret < 0)
1008  return ret;
1009  }
1010 
1011  return 0;
1012 }
1013 
1014 static int decompress_p3(AVCodecContext *avctx,
1015  uint32_t *dst, int linesize,
1016  uint32_t *prev, int plinesize)
1017 {
1018  SCPRContext *s = avctx->priv_data;
1019  GetByteContext *gb = &s->gb;
1020  int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
1021  int backstep = linesize - avctx->width;
1022  int mvx = 0, mvy = 0;
1023 
1024  if (bytestream2_get_byte(gb) == 0)
1025  return 1;
1026  init_rangecoder3(&s->rc, gb);
1027 
1028  ret = decode_value3(s, 255, &s->range_model3.cntsum,
1029  s->range_model3.freqs[0],
1030  s->range_model3.freqs[1],
1031  s->range_model3.cnts,
1032  s->range_model3.dectab, &min);
1033  ret |= decode_value3(s, 255, &s->range_model3.cntsum,
1034  s->range_model3.freqs[0],
1035  s->range_model3.freqs[1],
1036  s->range_model3.cnts,
1037  s->range_model3.dectab, &temp);
1038  if (ret < 0)
1039  return ret;
1040 
1041  min += temp << 8;
1042  ret |= decode_value3(s, 255, &s->range_model3.cntsum,
1043  s->range_model3.freqs[0],
1044  s->range_model3.freqs[1],
1045  s->range_model3.cnts,
1046  s->range_model3.dectab, &max);
1047  ret |= decode_value3(s, 255, &s->range_model3.cntsum,
1048  s->range_model3.freqs[0],
1049  s->range_model3.freqs[1],
1050  s->range_model3.cnts,
1051  s->range_model3.dectab, &temp);
1052  if (ret < 0)
1053  return ret;
1054 
1055  max += temp << 8;
1056  if (min > max || min >= s->nbcount)
1057  return AVERROR_INVALIDDATA;
1058 
1059  memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
1060 
1061  while (min <= max) {
1062  int fill, count;
1063 
1064  ret = decode_value3(s, 4, &s->fill_model3.cntsum,
1065  s->fill_model3.freqs[0],
1066  s->fill_model3.freqs[1],
1067  s->fill_model3.cnts,
1068  s->fill_model3.dectab, &fill);
1069  ret |= decode_value3(s, 255, &s->count_model3.cntsum,
1070  s->count_model3.freqs[0],
1071  s->count_model3.freqs[1],
1072  s->count_model3.cnts,
1073  s->count_model3.dectab, &count);
1074  if (ret < 0)
1075  return ret;
1076  if (count <= 0)
1077  return AVERROR_INVALIDDATA;
1078 
1079  while (min < s->nbcount && count-- > 0) {
1080  s->blocks[min++] = fill;
1081  }
1082  }
1083 
1084  ret = av_frame_copy(s->current_frame, s->last_frame);
1085  if (ret < 0)
1086  return ret;
1087 
1088  for (y = 0; y < s->nby; y++) {
1089  for (x = 0; x < s->nbx; x++) {
1090  int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
1091 
1092  if (s->blocks[y * s->nbx + x] == 0)
1093  continue;
1094 
1095  if (((s->blocks[y * s->nbx + x] + 1) & 1) > 0) {
1096  ret = decode_value3(s, 15, &s->sxy_model3[0].cntsum,
1097  s->sxy_model3[0].freqs[0],
1098  s->sxy_model3[0].freqs[1],
1099  s->sxy_model3[0].cnts,
1100  s->sxy_model3[0].dectab, &sx1);
1101  ret |= decode_value3(s, 15, &s->sxy_model3[1].cntsum,
1102  s->sxy_model3[1].freqs[0],
1103  s->sxy_model3[1].freqs[1],
1104  s->sxy_model3[1].cnts,
1105  s->sxy_model3[1].dectab, &sy1);
1106  ret |= decode_value3(s, 15, &s->sxy_model3[2].cntsum,
1107  s->sxy_model3[2].freqs[0],
1108  s->sxy_model3[2].freqs[1],
1109  s->sxy_model3[2].cnts,
1110  s->sxy_model3[2].dectab, &sx2);
1111  ret |= decode_value3(s, 15, &s->sxy_model3[3].cntsum,
1112  s->sxy_model3[3].freqs[0],
1113  s->sxy_model3[3].freqs[1],
1114  s->sxy_model3[3].cnts,
1115  s->sxy_model3[3].dectab, &sy2);
1116  if (ret < 0)
1117  return ret;
1118 
1119  sx2++;
1120  sy2++;
1121  }
1122  if (((s->blocks[y * s->nbx + x] + 3) & 2) > 0) {
1123  int i, a, b, c, j, by = y * 16, bx = x * 16;
1124  uint32_t code;
1125 
1126  a = s->rc.code & 0xFFF;
1127  c = 1;
1128 
1129  if (a < 0x800)
1130  c = 0;
1131  b = 2048;
1132  if (!c)
1133  b = 0;
1134 
1135  code = a + ((s->rc.code >> 1) & 0xFFFFF800) - b;
1136  while (code < 0x800000 && bytestream2_get_bytes_left(gb) > 0)
1137  code = bytestream2_get_byteu(gb) | (code << 8);
1138  s->rc.code = code;
1139 
1140  sync_code3(gb, &s->rc);
1141 
1142  if (!c) {
1143  ret = decode_value3(s, 511, &s->mv_model3[0].cntsum,
1144  s->mv_model3[0].freqs[0],
1145  s->mv_model3[0].freqs[1],
1146  s->mv_model3[0].cnts,
1147  s->mv_model3[0].dectab, &mvx);
1148  ret |= decode_value3(s, 511, &s->mv_model3[1].cntsum,
1149  s->mv_model3[1].freqs[0],
1150  s->mv_model3[1].freqs[1],
1151  s->mv_model3[1].cnts,
1152  s->mv_model3[1].dectab, &mvy);
1153  if (ret < 0)
1154  return ret;
1155 
1156  mvx -= 256;
1157  mvy -= 256;
1158  }
1159 
1160  if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
1161  by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
1162  return AVERROR_INVALIDDATA;
1163 
1164  for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
1165  for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
1166  dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
1167  }
1168  }
1169  } else {
1170  int run, bx = x * 16 + sx1, by = y * 16 + sy1;
1171  uint32_t clr, ptype = 0, r, g, b;
1172 
1173  if (bx >= avctx->width)
1174  return AVERROR_INVALIDDATA;
1175 
1176  for (; by < y * 16 + sy2 && by < avctx->height;) {
1177  ret = decode_value3(s, 5, &s->op_model3[ptype].cntsum,
1178  s->op_model3[ptype].freqs[0],
1179  s->op_model3[ptype].freqs[1],
1180  s->op_model3[ptype].cnts,
1181  s->op_model3[ptype].dectab, &ptype);
1182  if (ret < 0)
1183  return ret;
1184  if (ptype == 0) {
1185  ret = decode_units3(s, &r, &g, &b, &cx, &cx1);
1186  if (ret < 0)
1187  return ret;
1188 
1189  clr = (b << 16) + (g << 8) + r;
1190  }
1191  if (ptype > 5)
1192  return AVERROR_INVALIDDATA;
1193  ret = decode_value3(s, 255, &s->run_model3[ptype].cntsum,
1194  s->run_model3[ptype].freqs[0],
1195  s->run_model3[ptype].freqs[1],
1196  s->run_model3[ptype].cnts,
1197  s->run_model3[ptype].dectab, &run);
1198  if (ret < 0)
1199  return ret;
1200  if (run <= 0)
1201  return AVERROR_INVALIDDATA;
1202 
1203  ret = decode_run_p(avctx, ptype, run, x, y, clr,
1204  dst, prev, linesize, plinesize, &bx, &by,
1205  backstep, sx1, sx2, &cx, &cx1);
1206  if (ret < 0)
1207  return ret;
1208  }
1209  }
1210  }
1211  }
1212 
1213  return 0;
1214 }
static double val(void *priv, double ch)
Definition: aeval.c:76
uint8_t
Libavcodec external API header.
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
#define s(width, name)
Definition: cbs_vp9.c:257
#define f(width, name)
Definition: cbs_vp9.c:255
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFDIFFSIGN(x, y)
Comparator.
Definition: common.h:101
#define max(a, b)
Definition: cuda_runtime.h:33
double value
Definition: eval.c:98
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:799
int index
Definition: gxfenc.c:89
cl_device_type type
int i
Definition: input.c:407
common internal API header
#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
#define a4
Definition: regdef.h:50
static int decode3(GetByteContext *gb, RangeCoder *rc, uint32_t a, uint32_t b)
Definition: scpr3.c:99
static void grow_dec(PixelModel3 *m)
Definition: scpr3.c:369
static void incr_cntdec(PixelModel3 *m, int a)
Definition: scpr3.c:395
static int update_model5_to_6(PixelModel3 *m, uint8_t value)
Definition: scpr3.c:295
static int decode_static2(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:642
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:734
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
static int update_model3_to_7(PixelModel3 *m, uint8_t value)
Definition: scpr3.c:668
static int cmpbytes(const void *p1, const void *p2)
Definition: scpr3.c:477
static int add_dec(PixelModel3 *m, int sym, int f1, int f2)
Definition: scpr3.c:378
static int decode_unit3(SCPRContext *s, PixelModel3 *m, uint32_t code, uint32_t *value)
Definition: scpr3.c:825
static int update_model4_to_5(PixelModel3 *m, uint32_t value)
Definition: scpr3.c:795
static void calc_sum(PixelModel3 *m)
Definition: scpr3.c:249
static int decompress_p3(AVCodecContext *avctx, uint32_t *dst, int linesize, uint32_t *prev, int plinesize)
Definition: scpr3.c:1014
static int add_symbol(PixelModel3 *m, int index, uint32_t symbol, int *totfr, int max)
Definition: scpr3.c:123
static void calc_sum5(PixelModel3 *m)
Definition: scpr3.c:785
static void rescale(PixelModel3 *m, int *totfr)
Definition: scpr3.c:110
static int update_model1_to_2(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:484
static int update_model1_to_4(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:501
static int decode_adaptive6(PixelModel3 *m, uint32_t code, uint32_t *value, uint16_t *a, uint16_t *b)
Definition: scpr3.c:420
static void sync_code3(GetByteContext *gb, RangeCoder *rc)
Definition: scpr3.c:725
static int decode_static3(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:708
static int decode_static1(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:543
static void init_rangecoder3(RangeCoder *rc, GetByteContext *gb)
Definition: scpr3.c:925
static int update_model1_to_5(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:527
static int update_model2_to_3(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:624
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
static int decompress_i3(AVCodecContext *avctx, uint32_t *dst, int linesize)
Definition: scpr3.c:931
static void reinit_tables3(SCPRContext *s)
Definition: scpr3.c:53
static int update_model2_to_6(PixelModel3 *m, uint8_t value, int a4)
Definition: scpr3.c:565
static int update_model6_to_7(PixelModel3 *m)
Definition: scpr3.c:204
static int decode_units3(SCPRContext *s, uint32_t *red, uint32_t *green, uint32_t *blue, int *cx, int *cx1)
Definition: scpr3.c:894
static void rescale_dec(PixelModel3 *m)
Definition: scpr3.c:261
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
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 FF_ARRAY_ELEMS(a)
const uint8_t * code
Definition: spdifenc.c:413
main external API structure.
Definition: avcodec.h:536
int width
picture width / height.
Definition: avcodec.h:709
void * priv_data
Definition: avcodec.h:563
uint16_t cnts[256]
Definition: scpr3.h:43
uint16_t freqs1[256]
Definition: scpr3.h:42
uint16_t size
Definition: scpr3.h:38
uint8_t symbols[256]
Definition: scpr3.h:40
uint8_t dectab[32]
Definition: scpr3.h:44
uint16_t freqs[256]
Definition: scpr3.h:41
uint8_t type
Definition: scpr3.h:34
uint8_t maxpos
Definition: scpr3.h:36
uint8_t fshift
Definition: scpr3.h:37
uint32_t cntsum
Definition: scpr3.h:39
uint8_t length
Definition: scpr3.h:35
uint32_t code
Definition: scpr.h:36
uint32_t code1
Definition: scpr.h:38
uint8_t run
Definition: svq3.c:205
#define height
#define width
int size
const char * b
Definition: vf_curves.c:118
const char * g
Definition: vf_curves.c:117
const char * r
Definition: vf_curves.c:116
else temp
Definition: vf_mcdeint.c:259
float min
int len
static double c[64]