FFmpeg
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 "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 
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 
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  if (c < 0)
469  return AVERROR_INVALIDDATA;
470  }
471 
472  incr_cntdec(m, c);
473  return 1;
474 }
475 
476 static int cmpbytes(const void *p1, const void *p2)
477 {
478  int left = *(const uint8_t *)p1;
479  int right = *(const uint8_t *)p2;
480  return FFDIFFSIGN(left, right);
481 }
482 
483 static int update_model1_to_2(PixelModel3 *m, uint32_t val)
484 {
485  PixelModel3 n = {0};
486  int i, b;
487 
488  n.type = 2;
489  n.size = m->size + 1;
490  b = m->size;
491  for (i = 0; i < b; i++)
492  n.symbols[i] = m->symbols[i];
493  n.symbols[b] = val;
494 
495  memcpy(m, &n, sizeof(n));
496 
497  return 0;
498 }
499 
500 static int update_model1_to_4(PixelModel3 *m, uint32_t val)
501 {
502  PixelModel3 n = {0};
503  int size, i;
504 
505  size = m->size;
506  n.type = 4;
507  n.size = size;
508  for (i = 0; i < n.size; i++) {
509  n.symbols[i] = m->symbols[i];
510  }
511  AV_QSORT(n.symbols, size, uint8_t, cmpbytes);
512  for (i = 0; i < n.size; i++) {
513  if (val == n.symbols[i]) {
514  n.freqs[i] = 100;
515  n.maxpos = i;
516  } else {
517  n.freqs[i] = 50;
518  }
519  }
520 
521  memcpy(m, &n, sizeof(n));
522 
523  return 0;
524 }
525 
526 static int update_model1_to_5(PixelModel3 *m, uint32_t val)
527 {
528  int i, size, freqs;
529  uint32_t a;
530 
532  size = m->size;
533  a = 256 - size;
534  for (i = 0; i < size; i++, a += freqs)
535  freqs = m->freqs[i];
536  m->type = 5;
537  m->cntsum = a;
538 
539  return 0;
540 }
541 
542 static int decode_static1(PixelModel3 *m, uint32_t val)
543 {
544  uint32_t size;
545 
546  size = m->size;
547  for (int i = 0; i < size; i++) {
548  if (val == m->symbols[i]) {
549  if (size <= 4)
550  return update_model1_to_4(m, val);
551  else
552  return update_model1_to_5(m, val);
553  }
554  }
555 
556  if (size >= 14)
557  return update_model1_to_2(m, val);
558 
559  m->symbols[size] = val;
560  m->size++;
561  return 0;
562 }
563 
564 static int update_model2_to_6(PixelModel3 *m, uint8_t value, int a4)
565 {
566  PixelModel3 n = {0};
567  int c, d, e, f, g, q;
568 
569  n.type = 6;
570  n.length = a4;
571 
572  memset(n.symbols, 1u, a4);
573 
574  c = m->size;
575  d = 256 - c + (64 * c + 64);
576  for (e = 0; d <= 2048; e++) {
577  d <<= 1;
578  }
579 
580  g = q = 0;
581  AV_QSORT(m->symbols, c, uint8_t, cmpbytes);
582  for (f = d = 0; f < c; f++) {
583  int p = f;
584  int k = m->symbols[p];
585  int l;
586  g = g + (k - q);
587 
588  if (k == value) {
589  d = p;
590  q = 128;
591  } else {
592  q = 64;
593  }
594  l = q << e;
595  n.freqs[2 * p] = l;
596  n.freqs[2 * p + 1] = g << e;
597  n.symbols[p] = k;
598  n.cnts[p] = l - (l >> 1);
599  g += q;
600  q = k + 1;
601  }
602  n.size = c;
603  n.fshift = e;
604  calc_sum(&n);
605 
606  if (d > 0) {
607  c = n.freqs[0];
608  e = n.freqs[1];
609  g = n.freqs[2 * d + 1];
610  n.freqs[0] = n.freqs[2 * d];
611  n.freqs[1] = g;
612  n.freqs[2 * d] = c;
613  n.freqs[2 * d + 1] = e;
614  FFSWAP(uint16_t, n.cnts[0], n.cnts[d]);
615  FFSWAP(uint8_t, n.symbols[0], n.symbols[d]);
616  }
617 
618  memcpy(m, &n, sizeof(n));
619 
620  return 0;
621 }
622 
623 static int update_model2_to_3(PixelModel3 *m, uint32_t val)
624 {
625  PixelModel3 n = {0};
626  uint32_t size;
627 
628  n.type = 3;
629  n.size = m->size + 1;
630 
631  size = m->size;
632  for (int i = 0; i < size; i++)
633  n.symbols[i] = m->symbols[i];
634  n.symbols[size] = val;
635 
636  memcpy(m, &n, sizeof(n));
637 
638  return 0;
639 }
640 
641 static int decode_static2(PixelModel3 *m, uint32_t val)
642 {
643  uint32_t size;
644 
645  size = m->size;
646  for (int i = 0; i < size; i++) {
647  if (val == m->symbols[i]) {
648  int a;
649 
650  if (m->size <= 32)
651  a = 32;
652  else
653  a = 64;
654  return update_model2_to_6(m, val, a);
655  }
656  }
657 
658  if (size >= 64)
659  return update_model2_to_3(m, val);
660 
661  m->symbols[size] = val;
662  m->size++;
663 
664  return 0;
665 }
666 
667 static int update_model3_to_7(PixelModel3 *m, uint8_t value)
668 {
669  PixelModel3 n = {0};
670  int c, d, e, f, g, q;
671 
672  n.type = 7;
673 
674  for (c = 0; c < 256; c++) {
675  d = c;
676  n.freqs[d] = 1;
677  n.cnts[d] = 1;
678  }
679 
680  for (c = m->size, d = (4096 - (256 - c)) / (c + 1) | 0, e = d - (d >> 1), g = 0; g < c;) {
681  q = g++;
682  q = m->symbols[q];
683  n.freqs[q] = d;
684  n.cnts[q] = e;
685  }
686  n.freqs[value] += d;
687  n.cnts[value] += 16;
688  for (d = c = n.cntsum = 0; 256 > d; d++) {
689  e = d;
690  n.cntsum += n.cnts[e];
691  n.freqs1[e] = c;
692  g = n.freqs[e];
693  f = (c + g - 1 >> 7) + 1;
694  if (f > FF_ARRAY_ELEMS(n.dectab))
695  return AVERROR_INVALIDDATA;
696  for (q = c + 128 - 1 >> 7; q < f; q++) {
697  n.dectab[q] = e;
698  }
699  c += g;
700  }
701 
702  memcpy(m, &n, sizeof(n));
703 
704  return 0;
705 }
706 
707 static int decode_static3(PixelModel3 *m, uint32_t val)
708 {
709  uint32_t size = m->size;
710 
711  for (int i = 0; i < size; i++) {
712  if (val == m->symbols[i])
713  return update_model3_to_7(m, val);
714  }
715 
716  if (size >= 256)
717  return 0;
718 
719  m->symbols[size] = val;
720  m->size++;
721  return 0;
722 }
723 
724 static void sync_code3(GetByteContext *gb, RangeCoder *rc)
725 {
726  rc->code1++;
727  if (rc->code1 == 0x20000) {
728  rc->code = bytestream2_get_le32(gb);
729  rc->code1 = 0;
730  }
731 }
732 
733 static int decode_value3(SCPRContext *s, uint32_t max, uint32_t *cntsum,
734  uint16_t *freqs1, uint16_t *freqs2,
735  uint16_t *cnts, uint8_t *dectable,
736  uint32_t *value)
737 {
738  GetByteContext *gb = &s->gb;
739  RangeCoder *rc = &s->rc;
740  uint32_t r, y, a, b, e, g, q;
741 
742  r = dectable[(rc->code & 0xFFFu) >> 7];
743  if (r < max) {
744  while (freqs2[r + 1] <= (rc->code & 0xFFF)) {
745  if (++r >= max)
746  break;
747  }
748  }
749 
750  if (r > max)
751  return AVERROR_INVALIDDATA;
752 
753  cnts[r] += 16;
754  a = freqs1[r];
755  b = freqs2[r];
756  *cntsum += 16;
757  if (*cntsum + 16 > 4096) {
758  *cntsum = 0;
759  for (int c = 0, i = 0; i < max + 1; i++) {
760  e = cnts[i];
761  freqs2[i] = c;
762  freqs1[i] = e;
763  g = (c + 127) >> 7;
764  c += e;
765  q = ((c - 1) >> 7) + 1;
766  if (q > g) {
767  for (int j = 0; j < q - g; j++)
768  dectable[j + g] = i;
769  }
770  y = e - (e >> 1);
771  cnts[i] = y;
772  *cntsum += y;
773  }
774  }
775 
776  decode3(gb, rc, a, b);
777  sync_code3(gb, rc);
778 
779  *value = r;
780 
781  return 0;
782 }
783 
784 static void calc_sum5(PixelModel3 *m)
785 {
786  uint32_t a;
787 
788  a = 256 - m->size;
789  for (int b = 0; b < m->size; b++)
790  a += m->freqs[b];
791  m->cntsum = a;
792 }
793 
794 static int update_model4_to_5(PixelModel3 *m, uint32_t value)
795 {
796  PixelModel3 n = {0};
797  int c, e, g, totfr;
798 
799  n.type = 5;
800 
801  for (c = 0, e = 0; c < m->size && m->symbols[c] < value; c++) {
802  n.symbols[c] = m->symbols[c];
803  e += n.freqs[c] = m->freqs[c];
804  }
805 
806  g = c;
807  n.symbols[g] = value;
808  e += n.freqs[g++] = 50;
809  for (; c < m->size; g++, c++) {
810  n.symbols[g] = m->symbols[c];
811  e += n.freqs[g] = m->freqs[c];
812  }
813  n.size = m->size + 1;
814  if (e > 4096)
815  rescale(&n, &totfr);
816 
817  calc_sum5(&n);
818 
819  memcpy(m, &n, sizeof(n));
820 
821  return 0;
822 }
823 
824 static int decode_unit3(SCPRContext *s, PixelModel3 *m, uint32_t code, uint32_t *value)
825 {
826  GetByteContext *gb = &s->gb;
827  RangeCoder *rc = &s->rc;
828  uint16_t a = 0, b = 0;
829  uint32_t param;
830  int type;
831  int ret;
832 
833  type = m->type;
834  switch (type) {
835  case 0:
836  *value = bytestream2_get_byte(&s->gb);
837  m->type = 1;
838  m->size = 1;
839  m->symbols[0] = *value;
840  sync_code3(gb, rc);
841  break;
842  case 1:
843  *value = bytestream2_get_byte(&s->gb);
844  decode_static1(m, *value);
845  sync_code3(gb, rc);
846  break;
847  case 2:
848  *value = bytestream2_get_byte(&s->gb);
849  decode_static2(m, *value);
850  sync_code3(gb, rc);
851  break;
852  case 3:
853  *value = bytestream2_get_byte(&s->gb);
854  ret = decode_static3(m, *value);
855  if (ret < 0)
856  return AVERROR_INVALIDDATA;
857  sync_code3(gb, rc);
858  break;
859  case 4:
860  param = m->freqs[0] + m->freqs[1] + m->freqs[2] + m->freqs[3] + 256 - m->size;
861  if (!decode_adaptive45(m, code, value, &a, &b, &param, 4))
863  decode3(gb, rc, a, b);
864  sync_code3(gb, rc);
865  break;
866  case 5:
867  if (!decode_adaptive45(m, code, value, &a, &b, &m->cntsum, 16))
869  decode3(gb, rc, a, b);
870  sync_code3(gb, rc);
871  break;
872  case 6:
873  ret = decode_adaptive6(m, code, value, &a, &b);
874  if (!ret)
875  ret = update_model6_to_7(m);
876  if (ret < 0)
877  return ret;
878  decode3(gb, rc, a, b);
879  sync_code3(gb, rc);
880  break;
881  case 7:
882  return decode_value3(s, 255, &m->cntsum,
883  m->freqs, m->freqs1,
884  m->cnts, m->dectab, value);
885  }
886 
887  if (*value > 255)
888  return AVERROR_INVALIDDATA;
889 
890  return 0;
891 }
892 
893 static int decode_units3(SCPRContext * s, uint32_t *red,
894  uint32_t *green, uint32_t *blue,
895  int *cx, int *cx1)
896 {
897  RangeCoder *rc = &s->rc;
898  int ret;
899 
900  ret = decode_unit3(s, &s->pixel_model3[0][*cx + *cx1], rc->code & 0xFFF, red);
901  if (ret < 0)
902  return ret;
903 
904  *cx1 = (*cx << 6) & 0xFC0;
905  *cx = *red >> 2;
906 
907  ret = decode_unit3(s, &s->pixel_model3[1][*cx + *cx1], rc->code & 0xFFF, green);
908  if (ret < 0)
909  return ret;
910 
911  *cx1 = (*cx << 6) & 0xFC0;
912  *cx = *green >> 2;
913 
914  ret = decode_unit3(s, &s->pixel_model3[2][*cx + *cx1], rc->code & 0xFFF, blue);
915  if (ret < 0)
916  return ret;
917 
918  *cx1 = (*cx << 6) & 0xFC0;
919  *cx = *blue >> 2;
920 
921  return 0;
922 }
923 
925 {
926  rc->code = bytestream2_get_le32(gb);
927  rc->code1 = 0;
928 }
929 
930 static int decompress_i3(AVCodecContext *avctx, uint32_t *dst, int linesize)
931 {
932  SCPRContext *s = avctx->priv_data;
933  GetByteContext *gb = &s->gb;
934  RangeCoder *rc = &s->rc;
935  int cx = 0, cx1 = 0, k = 0;
936  int run, off, y = 0, x = 0, ret;
937  uint32_t backstep = linesize - avctx->width;
938  uint32_t clr = 0, lx, ly, ptype, r, g, b;
939 
940  bytestream2_skip(gb, 1);
941  init_rangecoder3(rc, gb);
942  reinit_tables3(s);
943 
944  while (k < avctx->width + 1) {
945  ret = decode_units3(s, &r, &g, &b, &cx, &cx1);
946  if (ret < 0)
947  return ret;
948  ret = decode_value3(s, 255, &s->run_model3[0].cntsum,
949  s->run_model3[0].freqs[0],
950  s->run_model3[0].freqs[1],
951  s->run_model3[0].cnts,
952  s->run_model3[0].dectab, &run);
953  if (ret < 0)
954  return ret;
955  if (run <= 0)
956  return AVERROR_INVALIDDATA;
957 
958  clr = (b << 16) + (g << 8) + r;
959  k += run;
960  while (run-- > 0) {
961  if (y >= avctx->height)
962  return AVERROR_INVALIDDATA;
963 
964  dst[y * linesize + x] = clr;
965  lx = x;
966  ly = y;
967  x++;
968  if (x >= avctx->width) {
969  x = 0;
970  y++;
971  }
972  }
973  }
974  off = -linesize - 1;
975  ptype = 0;
976 
977  while (x < avctx->width && y < avctx->height) {
978  ret = decode_value3(s, 5, &s->op_model3[ptype].cntsum,
979  s->op_model3[ptype].freqs[0],
980  s->op_model3[ptype].freqs[1],
981  s->op_model3[ptype].cnts,
982  s->op_model3[ptype].dectab, &ptype);
983  if (ret < 0)
984  return ret;
985  if (ptype == 0) {
986  ret = decode_units3(s, &r, &g, &b, &cx, &cx1);
987  if (ret < 0)
988  return ret;
989  clr = (b << 16) + (g << 8) + r;
990  }
991  if (ptype > 5)
992  return AVERROR_INVALIDDATA;
993  ret = decode_value3(s, 255, &s->run_model3[ptype].cntsum,
994  s->run_model3[ptype].freqs[0],
995  s->run_model3[ptype].freqs[1],
996  s->run_model3[ptype].cnts,
997  s->run_model3[ptype].dectab, &run);
998  if (ret < 0)
999  return ret;
1000  if (run <= 0)
1001  return AVERROR_INVALIDDATA;
1002 
1003  ret = decode_run_i(avctx, ptype, run, &x, &y, clr,
1004  dst, linesize, &lx, &ly,
1005  backstep, off, &cx, &cx1);
1006  if (ret < 0)
1007  return ret;
1008  }
1009 
1010  return 0;
1011 }
1012 
1013 static int decompress_p3(AVCodecContext *avctx,
1014  uint32_t *dst, int linesize,
1015  uint32_t *prev, int plinesize)
1016 {
1017  SCPRContext *s = avctx->priv_data;
1018  GetByteContext *gb = &s->gb;
1019  int ret, temp, min, max, x, y, cx = 0, cx1 = 0;
1020  int backstep = linesize - avctx->width;
1021  int mvx = 0, mvy = 0;
1022 
1023  if (bytestream2_get_byte(gb) == 0)
1024  return 1;
1025  init_rangecoder3(&s->rc, gb);
1026 
1027  ret = decode_value3(s, 255, &s->range_model3.cntsum,
1028  s->range_model3.freqs[0],
1029  s->range_model3.freqs[1],
1030  s->range_model3.cnts,
1031  s->range_model3.dectab, &min);
1032  ret |= decode_value3(s, 255, &s->range_model3.cntsum,
1033  s->range_model3.freqs[0],
1034  s->range_model3.freqs[1],
1035  s->range_model3.cnts,
1036  s->range_model3.dectab, &temp);
1037  if (ret < 0)
1038  return ret;
1039 
1040  min += temp << 8;
1041  ret |= decode_value3(s, 255, &s->range_model3.cntsum,
1042  s->range_model3.freqs[0],
1043  s->range_model3.freqs[1],
1044  s->range_model3.cnts,
1045  s->range_model3.dectab, &max);
1046  ret |= decode_value3(s, 255, &s->range_model3.cntsum,
1047  s->range_model3.freqs[0],
1048  s->range_model3.freqs[1],
1049  s->range_model3.cnts,
1050  s->range_model3.dectab, &temp);
1051  if (ret < 0)
1052  return ret;
1053 
1054  max += temp << 8;
1055  if (min > max || min >= s->nbcount)
1056  return AVERROR_INVALIDDATA;
1057 
1058  memset(s->blocks, 0, sizeof(*s->blocks) * s->nbcount);
1059 
1060  while (min <= max) {
1061  int fill, count;
1062 
1063  ret = decode_value3(s, 4, &s->fill_model3.cntsum,
1064  s->fill_model3.freqs[0],
1065  s->fill_model3.freqs[1],
1066  s->fill_model3.cnts,
1067  s->fill_model3.dectab, &fill);
1068  ret |= decode_value3(s, 255, &s->count_model3.cntsum,
1069  s->count_model3.freqs[0],
1070  s->count_model3.freqs[1],
1071  s->count_model3.cnts,
1072  s->count_model3.dectab, &count);
1073  if (ret < 0)
1074  return ret;
1075  if (count <= 0)
1076  return AVERROR_INVALIDDATA;
1077 
1078  while (min < s->nbcount && count-- > 0) {
1079  s->blocks[min++] = fill;
1080  }
1081  }
1082 
1083  ret = av_frame_copy(s->current_frame, s->last_frame);
1084  if (ret < 0)
1085  return ret;
1086 
1087  for (y = 0; y < s->nby; y++) {
1088  for (x = 0; x < s->nbx; x++) {
1089  int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
1090 
1091  if (s->blocks[y * s->nbx + x] == 0)
1092  continue;
1093 
1094  if (((s->blocks[y * s->nbx + x] + 1) & 1) > 0) {
1095  ret = decode_value3(s, 15, &s->sxy_model3[0].cntsum,
1096  s->sxy_model3[0].freqs[0],
1097  s->sxy_model3[0].freqs[1],
1098  s->sxy_model3[0].cnts,
1099  s->sxy_model3[0].dectab, &sx1);
1100  ret |= decode_value3(s, 15, &s->sxy_model3[1].cntsum,
1101  s->sxy_model3[1].freqs[0],
1102  s->sxy_model3[1].freqs[1],
1103  s->sxy_model3[1].cnts,
1104  s->sxy_model3[1].dectab, &sy1);
1105  ret |= decode_value3(s, 15, &s->sxy_model3[2].cntsum,
1106  s->sxy_model3[2].freqs[0],
1107  s->sxy_model3[2].freqs[1],
1108  s->sxy_model3[2].cnts,
1109  s->sxy_model3[2].dectab, &sx2);
1110  ret |= decode_value3(s, 15, &s->sxy_model3[3].cntsum,
1111  s->sxy_model3[3].freqs[0],
1112  s->sxy_model3[3].freqs[1],
1113  s->sxy_model3[3].cnts,
1114  s->sxy_model3[3].dectab, &sy2);
1115  if (ret < 0)
1116  return ret;
1117 
1118  sx2++;
1119  sy2++;
1120  }
1121  if (((s->blocks[y * s->nbx + x] + 3) & 2) > 0) {
1122  int i, a, b, c, j, by = y * 16, bx = x * 16;
1123  uint32_t code;
1124 
1125  a = s->rc.code & 0xFFF;
1126  c = 1;
1127 
1128  if (a < 0x800)
1129  c = 0;
1130  b = 2048;
1131  if (!c)
1132  b = 0;
1133 
1134  code = a + ((s->rc.code >> 1) & 0xFFFFF800) - b;
1135  while (code < 0x800000 && bytestream2_get_bytes_left(gb) > 0)
1136  code = bytestream2_get_byteu(gb) | (code << 8);
1137  s->rc.code = code;
1138 
1139  sync_code3(gb, &s->rc);
1140 
1141  if (!c) {
1142  ret = decode_value3(s, 511, &s->mv_model3[0].cntsum,
1143  s->mv_model3[0].freqs[0],
1144  s->mv_model3[0].freqs[1],
1145  s->mv_model3[0].cnts,
1146  s->mv_model3[0].dectab, &mvx);
1147  ret |= decode_value3(s, 511, &s->mv_model3[1].cntsum,
1148  s->mv_model3[1].freqs[0],
1149  s->mv_model3[1].freqs[1],
1150  s->mv_model3[1].cnts,
1151  s->mv_model3[1].dectab, &mvy);
1152  if (ret < 0)
1153  return ret;
1154 
1155  mvx -= 256;
1156  mvy -= 256;
1157  }
1158 
1159  if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
1160  by + mvy + sy1 >= avctx->height || bx + mvx + sx1 >= avctx->width)
1161  return AVERROR_INVALIDDATA;
1162 
1163  for (i = 0; i < sy2 - sy1 && (by + sy1 + i) < avctx->height && (by + mvy + sy1 + i) < avctx->height; i++) {
1164  for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->width && (bx + mvx + sx1 + j) < avctx->width; j++) {
1165  dst[(by + i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 + i) * plinesize + bx + sx1 + mvx + j];
1166  }
1167  }
1168  } else {
1169  int run, bx = x * 16 + sx1, by = y * 16 + sy1;
1170  uint32_t clr, ptype = 0, r, g, b;
1171 
1172  if (bx >= avctx->width)
1173  return AVERROR_INVALIDDATA;
1174 
1175  for (; by < y * 16 + sy2 && by < avctx->height;) {
1176  ret = decode_value3(s, 5, &s->op_model3[ptype].cntsum,
1177  s->op_model3[ptype].freqs[0],
1178  s->op_model3[ptype].freqs[1],
1179  s->op_model3[ptype].cnts,
1180  s->op_model3[ptype].dectab, &ptype);
1181  if (ret < 0)
1182  return ret;
1183  if (ptype == 0) {
1184  ret = decode_units3(s, &r, &g, &b, &cx, &cx1);
1185  if (ret < 0)
1186  return ret;
1187 
1188  clr = (b << 16) + (g << 8) + r;
1189  }
1190  if (ptype > 5)
1191  return AVERROR_INVALIDDATA;
1192  ret = decode_value3(s, 255, &s->run_model3[ptype].cntsum,
1193  s->run_model3[ptype].freqs[0],
1194  s->run_model3[ptype].freqs[1],
1195  s->run_model3[ptype].cnts,
1196  s->run_model3[ptype].dectab, &run);
1197  if (ret < 0)
1198  return ret;
1199  if (run <= 0)
1200  return AVERROR_INVALIDDATA;
1201 
1202  ret = decode_run_p(avctx, ptype, run, x, y, clr,
1203  dst, prev, linesize, plinesize, &bx, &by,
1204  backstep, sx1, sx2, &cx, &cx1);
1205  if (ret < 0)
1206  return ret;
1207  }
1208  }
1209  }
1210  }
1211 
1212  return 0;
1213 }
update_model1_to_2
static int update_model1_to_2(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:483
r
const char * r
Definition: vf_curves.c:127
GetByteContext
Definition: bytestream.h:33
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:251
calc_sum
static void calc_sum(PixelModel3 *m)
Definition: scpr3.c:248
b
#define b
Definition: input.c:41
update_model4_to_5
static int update_model4_to_5(PixelModel3 *m, uint32_t value)
Definition: scpr3.c:794
incr_cntdec
static void incr_cntdec(PixelModel3 *m, int a)
Definition: scpr3.c:394
add_dec
static int add_dec(PixelModel3 *m, int sym, int f1, int f2)
Definition: scpr3.c:377
RangeCoder::code1
uint32_t code1
Definition: scpr.h:33
max
#define max(a, b)
Definition: cuda_runtime.h:33
PixelModel3::dectab
uint8_t dectab[32]
Definition: scpr3.h:39
decode_adaptive45
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:146
PixelModel3::fshift
uint8_t fshift
Definition: scpr3.h:32
PixelModel3::length
uint8_t length
Definition: scpr3.h:30
PixelModel3::freqs1
uint16_t freqs1[256]
Definition: scpr3.h:37
cmpbytes
static int cmpbytes(const void *p1, const void *p2)
Definition: scpr3.c:476
bytestream2_skip
static av_always_inline void bytestream2_skip(GetByteContext *g, unsigned int size)
Definition: bytestream.h:168
decode_units3
static int decode_units3(SCPRContext *s, uint32_t *red, uint32_t *green, uint32_t *blue, int *cx, int *cx1)
Definition: scpr3.c:893
val
static double val(void *priv, double ch)
Definition: aeval.c:77
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
PixelModel3::maxpos
uint8_t maxpos
Definition: scpr3.h:31
FFDIFFSIGN
#define FFDIFFSIGN(x, y)
Comparator.
Definition: macros.h:45
PixelModel3::symbols
uint8_t symbols[256]
Definition: scpr3.h:35
rescale_dec
static void rescale_dec(PixelModel3 *m)
Definition: scpr3.c:260
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
s
#define s(width, name)
Definition: cbs_vp9.c:198
update_model5_to_6
static int update_model5_to_6(PixelModel3 *m, uint8_t value)
Definition: scpr3.c:294
g
const char * g
Definition: vf_curves.c:128
update_model6_to_7
static int update_model6_to_7(PixelModel3 *m)
Definition: scpr3.c:203
decode_run_i
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:75
decode_unit3
static int decode_unit3(SCPRContext *s, PixelModel3 *m, uint32_t code, uint32_t *value)
Definition: scpr3.c:824
update_model3_to_7
static int update_model3_to_7(PixelModel3 *m, uint8_t value)
Definition: scpr3.c:667
PixelModel3::cnts
uint16_t cnts[256]
Definition: scpr3.h:38
update_model2_to_6
static int update_model2_to_6(PixelModel3 *m, uint8_t value, int a4)
Definition: scpr3.c:564
run
uint8_t run
Definition: svq3.c:204
PixelModel3::freqs
uint16_t freqs[256]
Definition: scpr3.h:36
decode_static1
static int decode_static1(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:542
PixelModel3::cntsum
uint32_t cntsum
Definition: scpr3.h:34
decode_adaptive6
static int decode_adaptive6(PixelModel3 *m, uint32_t code, uint32_t *value, uint16_t *a, uint16_t *b)
Definition: scpr3.c:419
update_model1_to_4
static int update_model1_to_4(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:500
decode_static3
static int decode_static3(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:707
index
int index
Definition: gxfenc.c:90
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
bytestream2_get_bytes_left
static av_always_inline int bytestream2_get_bytes_left(GetByteContext *g)
Definition: bytestream.h:158
qsort.h
f
f
Definition: af_crystalizer.c:122
height
#define height
Definition: dsp.h:85
renew_table3
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:33
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
av_frame_copy
int av_frame_copy(AVFrame *dst, const AVFrame *src)
Copy the frame data from src to dst.
Definition: frame.c:1003
decode_run_p
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:217
size
int size
Definition: twinvq_data.h:10344
calc_sum5
static void calc_sum5(PixelModel3 *m)
Definition: scpr3.c:784
SCPRContext
Definition: scpr.h:42
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
init_rangecoder3
static void init_rangecoder3(RangeCoder *rc, GetByteContext *gb)
Definition: scpr3.c:924
decompress_i3
static int decompress_i3(AVCodecContext *avctx, uint32_t *dst, int linesize)
Definition: scpr3.c:930
RangeCoder::code
uint32_t code
Definition: scpr.h:31
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
AV_QSORT
#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
update_model1_to_5
static int update_model1_to_5(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:526
decompress_p3
static int decompress_p3(AVCodecContext *avctx, uint32_t *dst, int linesize, uint32_t *prev, int plinesize)
Definition: scpr3.c:1013
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
len
int len
Definition: vorbis_enc_data.h:426
AVCodecContext::height
int height
Definition: avcodec.h:624
PixelModel3::size
uint16_t size
Definition: scpr3.h:33
grow_dec
static void grow_dec(PixelModel3 *m)
Definition: scpr3.c:368
avcodec.h
sync_code3
static void sync_code3(GetByteContext *gb, RangeCoder *rc)
Definition: scpr3.c:724
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
add_symbol
static int add_symbol(PixelModel3 *m, int index, uint32_t symbol, int *totfr, int max)
Definition: scpr3.c:122
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
update_model2_to_3
static int update_model2_to_3(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:623
AVCodecContext
main external API structure.
Definition: avcodec.h:451
scpr.h
decode_value3
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:733
rescale
static void rescale(PixelModel3 *m, int *totfr)
Definition: scpr3.c:109
PixelModel3::type
uint8_t type
Definition: scpr3.h:29
temp
else temp
Definition: vf_mcdeint.c:263
PixelModel3
Definition: scpr3.h:28
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
bytestream.h
decode_static2
static int decode_static2(PixelModel3 *m, uint32_t val)
Definition: scpr3.c:641
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
h
h
Definition: vp9dsp_template.c:2070
RangeCoder
Definition: mss3.c:63
decode3
static int decode3(GetByteContext *gb, RangeCoder *rc, uint32_t a, uint32_t b)
Definition: scpr3.c:98
width
#define width
Definition: dsp.h:85
reinit_tables3
static void reinit_tables3(SCPRContext *s)
Definition: scpr3.c:52
min
float min
Definition: vorbis_enc_data.h:429