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
28
33
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
41
42 for (
int d = 0;
d < nsym;
d++) {
46 for (
int q =
a + 128 - 1 >> 7,
f = (
a +
b - 1 >> 7) + 1; q <
f; q++)
48
50 }
51 }
52
54 {
55 for (
int i = 0;
i < 3;
i++) {
56 for (int j = 0; j < 4096; j++) {
59 }
60 }
61
62 for (
int i = 0;
i < 6;
i++) {
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],
70 s->range_model3.cnts,
s->range_model3.dectab);
71
73 s->fill_model3.freqs[0],
s->fill_model3.freqs[1],
74 s->fill_model3.cnts,
s->fill_model3.dectab);
75
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++) {
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++) {
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
100 {
102
104 code = bytestream2_get_byteu(gb) | (
code << 8);
106
107 return 0;
108 }
109
111 {
113
115 for (
int b = 0;
b < m->
size;
b++) {
118 }
119
121 }
122
124 {
126 return 0;
127
131 }
132
136
139
140 *totfr += 50;
141 if (*totfr + 50 > 4096)
143
144 return 1;
145 }
146
148 uint16_t *
a, uint16_t *
b, uint32_t *
c,
int max)
149 {
150 uint32_t q,
g, maxpos,
d, e = *
c, totfr = *
c;
152
153 for (
d = 0; e <= 2048;
d++)
154 e <<= 1;
158 m->
freqs[maxpos] += 4096 - e >>
d;
159
160 for (q = 0,
g = 0, e = 0; q < m->
size; q++) {
162 uint32_t p = e +
f -
g;
163 uint32_t k = m->
freqs[q];
164
165 if (rccode < p) {
173 }
174
175 if (p + k > rccode) {
182 totfr += 50;
183 if ((q != maxpos) && (m->
freqs[q] > m->
freqs[maxpos]))
185 if (totfr + 50 > 4096)
188 return 1;
189 }
190
193 }
194
202 }
203
205 {
207 int c,
d, e,
f, k, p, length,
i, j,
index;
208 uint16_t *freqs, *freqs1, *cnts;
209
211
217 for (
i = 0;
i < length;
i++) {
219 continue;
224 }
227 for (j = 0, e = 0; j < 256; j++) {
232 freqs1[j] = e;
234 }
235 p = (e + 127) >> 7;
236 k = ((
f + e - 1) >> 7) + 1;
239 for (
i = 0;
i < k - p;
i++)
242 }
243
244 memcpy(m, &n, sizeof(n));
245
246 return 0;
247 }
248
250 {
253
256 for (
int c = 0;
c <
len;
c++)
259 }
260
262 {
263 uint16_t cnts[256] = {0};
264 uint16_t freqs[512] = {0};
267
270
273
274 for (
b =
a = 0;
b < 256;
b++) {
275 freqs[2 *
b] = cnts[
b];
276 freqs[2 *
b + 1] =
a;
278 }
279
282
288 g = freqs[2 * e + 1];
289 m->
freqs[2 *
b] = freqs[2 * e];
291 }
293 }
294
296 {
298 int c,
d, e,
f,
g, k, q, p;
299
302
303 for (
c = m->
size,
d = 256 -
c, e = 0; e <
c; e++)
305
306 for (e = 0;
d <= 2048; e++)
308
309 for (q =
d = 0,
g = q = 0;
g <
c;
g++) {
313 k = q << e;
316 n.
cnts[
g] = k - (k >> 1);
319 q = p + 1;
320 }
321
327 for (p =
f =
g = 0; p <
c; p++) {
333 }
334 }
336 }
339 n.
cnts[
c] = e - (e >> 1);
345 if (n.
cnts[32] + e > 4096)
347
349 for (
c = 0, e = n.
size - 1;
c < e;
c++) {
352 int l = n.
freqs[2 *
c + 1];
360 }
361 }
362 }
363
364 memcpy(m, &n, sizeof(n));
365
366 return 0;
367 }
368
370 {
372
376 }
377
379 {
381
383 return -1;
384
391
393 }
394
396 {
398
414 }
415
418 }
419
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
429 uint32_t k = m->
freqs[2 *
g];
430
436 return 1;
437 }
438
443 }
444 }
445 }
446
449
454 } else {
457 }
458
462
466 return 0;
469 }
470
472 return 1;
473 }
474
475 static int cmpbytes(
const void *p1,
const void *p2)
476 {
477 int left = *(
const uint8_t *)p1;
478 int right = *(const uint8_t *)p2;
480 }
481
483 {
486
490 for (
i = 0;
i <
b;
i++)
493
494 memcpy(m, &n, sizeof(n));
495
496 return 0;
497 }
498
500 {
503
509 }
515 } else {
517 }
518 }
519
520 memcpy(m, &n, sizeof(n));
521
522 return 0;
523 }
524
526 {
529
533 for (
i = 0;
i <
size;
i++,
a += freqs)
537
538 return 0;
539 }
540
542 {
544
546 for (
int i = 0;
i <
size;
i++) {
550 else
552 }
553 }
554
557
560 return 0;
561 }
562
564 {
566 int c,
d, e,
f,
g, q;
567
570
572
574 d = 256 -
c + (64 *
c + 64);
575 for (e = 0;
d <= 2048; e++) {
577 }
578
581 for (
f =
d = 0;
f <
c;
f++) {
584 int l;
586
589 q = 128;
590 } else {
591 q = 64;
592 }
593 l = q << e;
595 n.
freqs[2 * p + 1] =
g << e;
597 n.
cnts[p] = l - (l >> 1);
599 q = k + 1;
600 }
604
615 }
616
617 memcpy(m, &n, sizeof(n));
618
619 return 0;
620 }
621
623 {
626
629
634
635 memcpy(m, &n, sizeof(n));
636
637 return 0;
638 }
639
641 {
643
645 for (
int i = 0;
i <
size;
i++) {
648
651 else
654 }
655 }
656
659
662
663 return 0;
664 }
665
667 {
669 int c,
d, e,
f,
g, q;
670
672
673 for (
c = 0;
c < 256;
c++) {
677 }
678
679 for (
c = m->
size,
d = (4096 - (256 -
c)) / (
c + 1) | 0, e =
d - (
d >> 1),
g = 0;
g <
c;) {
684 }
692 f = (
c +
g - 1 >> 7) + 1;
695 for (q =
c + 128 - 1 >> 7; q <
f; q++) {
697 }
699 }
700
701 memcpy(m, &n, sizeof(n));
702
703 return 0;
704 }
705
707 {
709
710 for (
int i = 0;
i <
size;
i++) {
713 }
714
716 return 0;
717
720 return 0;
721 }
722
724 {
726 if (rc->
code1 == 0x20000) {
727 rc->
code = bytestream2_get_le32(gb);
729 }
730 }
731
733 uint16_t *freqs1, uint16_t *freqs2,
734 uint16_t *cnts, uint8_t *dectable,
736 {
739 uint32_t
r, y,
a,
b, e,
g, q;
740
741 r = dectable[(rc->
code & 0xFFF
u) >> 7];
743 while (freqs2[
r + 1] <= (rc->
code & 0xFFF)) {
745 break;
746 }
747 }
748
751
755 *cntsum += 16;
756 if (*cntsum + 16 > 4096) {
757 *cntsum = 0;
758 for (
int c = 0,
i = 0;
i <
max + 1;
i++) {
764 q = ((
c - 1) >> 7) + 1;
766 for (
int j = 0; j < q -
g; j++)
768 }
769 y = e - (e >> 1);
771 *cntsum += y;
772 }
773 }
774
777
779
780 return 0;
781 }
782
784 {
786
788 for (
int b = 0;
b < m->
size;
b++)
791 }
792
794 {
797
799
803 }
804
808 for (;
c < m->
size;
g++,
c++) {
811 }
813 if (e > 4096)
815
817
818 memcpy(m, &n, sizeof(n));
819
820 return 0;
821 }
822
824 {
827 uint16_t
a = 0,
b = 0;
828 uint32_t param;
831
834 case 0:
835 *
value = bytestream2_get_byte(&
s->gb);
840 break;
841 case 1:
842 *
value = bytestream2_get_byte(&
s->gb);
845 break;
846 case 2:
847 *
value = bytestream2_get_byte(&
s->gb);
850 break;
851 case 3:
852 *
value = bytestream2_get_byte(&
s->gb);
857 break;
858 case 4:
864 break;
865 case 5:
870 break;
871 case 6:
876 }
879 break;
880 case 7:
884 }
885
888
889 return 0;
890 }
891
893 uint32_t *green, uint32_t *blue,
894 int *cx, int *cx1)
895 {
898
902
903 *cx1 = (*cx << 6) & 0xFC0;
904 *cx = *red >> 2;
905
909
910 *cx1 = (*cx << 6) & 0xFC0;
911 *cx = *green >> 2;
912
916
917 *cx1 = (*cx << 6) & 0xFC0;
918 *cx = *blue >> 2;
919
920 return 0;
921 }
922
924 {
925 rc->
code = bytestream2_get_le32(gb);
927 }
928
930 {
934 int cx = 0, cx1 = 0, k = 0;
935 int run, off, y = 0, x = 0,
ret;
936 uint32_t backstep = linesize - avctx->
width;
937 uint32_t clr = 0, lx, ly, ptype,
r,
g,
b;
938
942
943 while (k < avctx->
width + 1) {
948 s->run_model3[0].freqs[0],
949 s->run_model3[0].freqs[1],
950 s->run_model3[0].cnts,
951 s->run_model3[0].dectab, &
run);
956
957 clr = (
b << 16) + (
g << 8) +
r;
962
963 dst[y * linesize + x] = clr;
964 lx = x;
965 ly = y;
966 x++;
967 if (x >= avctx->
width) {
968 x = 0;
969 y++;
970 }
971 }
972 }
973 off = -linesize - 1;
974 ptype = 0;
975
978 s->op_model3[ptype].freqs[0],
979 s->op_model3[ptype].freqs[1],
980 s->op_model3[ptype].cnts,
981 s->op_model3[ptype].dectab, &ptype);
984 if (ptype == 0) {
988 clr = (
b << 16) + (
g << 8) +
r;
989 }
990 if (ptype > 5)
993 s->run_model3[ptype].freqs[0],
994 s->run_model3[ptype].freqs[1],
995 s->run_model3[ptype].cnts,
996 s->run_model3[ptype].dectab, &
run);
1001
1003 dst, linesize, &lx, &ly,
1004 backstep, off, &cx, &cx1);
1007 }
1008
1009 return 0;
1010 }
1011
1013 uint32_t *dst, int linesize,
1014 uint32_t *prev, int plinesize)
1015 {
1019 int backstep = linesize - avctx->
width;
1020 int mvx = 0, mvy = 0;
1021
1022 if (bytestream2_get_byte(gb) == 0)
1023 return 1;
1025
1027 s->range_model3.freqs[0],
1028 s->range_model3.freqs[1],
1029 s->range_model3.cnts,
1030 s->range_model3.dectab, &
min);
1032 s->range_model3.freqs[0],
1033 s->range_model3.freqs[1],
1034 s->range_model3.cnts,
1035 s->range_model3.dectab, &
temp);
1038
1041 s->range_model3.freqs[0],
1042 s->range_model3.freqs[1],
1043 s->range_model3.cnts,
1044 s->range_model3.dectab, &
max);
1046 s->range_model3.freqs[0],
1047 s->range_model3.freqs[1],
1048 s->range_model3.cnts,
1049 s->range_model3.dectab, &
temp);
1052
1056
1057 memset(
s->blocks, 0,
sizeof(*
s->blocks) *
s->nbcount);
1058
1060 int fill, count;
1061
1063 s->fill_model3.freqs[0],
1064 s->fill_model3.freqs[1],
1065 s->fill_model3.cnts,
1066 s->fill_model3.dectab, &fill);
1068 s->count_model3.freqs[0],
1069 s->count_model3.freqs[1],
1070 s->count_model3.cnts,
1071 s->count_model3.dectab, &count);
1074 if (count <= 0)
1076
1077 while (min < s->nbcount && count-- > 0) {
1078 s->blocks[
min++] = fill;
1079 }
1080 }
1081
1085
1086 for (y = 0; y <
s->nby; y++) {
1087 for (x = 0; x <
s->nbx; x++) {
1088 int sy1 = 0, sy2 = 16, sx1 = 0, sx2 = 16;
1089
1090 if (
s->blocks[y *
s->nbx + x] == 0)
1091 continue;
1092
1093 if (((
s->blocks[y *
s->nbx + x] + 1) & 1) > 0) {
1095 s->sxy_model3[0].freqs[0],
1096 s->sxy_model3[0].freqs[1],
1097 s->sxy_model3[0].cnts,
1098 s->sxy_model3[0].dectab, &sx1);
1100 s->sxy_model3[1].freqs[0],
1101 s->sxy_model3[1].freqs[1],
1102 s->sxy_model3[1].cnts,
1103 s->sxy_model3[1].dectab, &sy1);
1105 s->sxy_model3[2].freqs[0],
1106 s->sxy_model3[2].freqs[1],
1107 s->sxy_model3[2].cnts,
1108 s->sxy_model3[2].dectab, &sx2);
1110 s->sxy_model3[3].freqs[0],
1111 s->sxy_model3[3].freqs[1],
1112 s->sxy_model3[3].cnts,
1113 s->sxy_model3[3].dectab, &sy2);
1116
1117 sx2++;
1118 sy2++;
1119 }
1120 if (((
s->blocks[y *
s->nbx + x] + 3) & 2) > 0) {
1121 int i,
a,
b,
c, j, by = y * 16, bx = x * 16;
1123
1124 a =
s->rc.code & 0xFFF;
1126
1132
1133 code =
a + ((
s->rc.code >> 1) & 0xFFFFF800) -
b;
1135 code = bytestream2_get_byteu(gb) | (
code << 8);
1137
1139
1142 s->mv_model3[0].freqs[0],
1143 s->mv_model3[0].freqs[1],
1144 s->mv_model3[0].cnts,
1145 s->mv_model3[0].dectab, &mvx);
1147 s->mv_model3[1].freqs[0],
1148 s->mv_model3[1].freqs[1],
1149 s->mv_model3[1].cnts,
1150 s->mv_model3[1].dectab, &mvy);
1153
1154 mvx -= 256;
1155 mvy -= 256;
1156 }
1157
1158 if (by + mvy + sy1 < 0 || bx + mvx + sx1 < 0 ||
1159 by + mvy + sy1 >= avctx->
height || bx + mvx + sx1 >= avctx->
width)
1161
1162 for (
i = 0;
i < sy2 - sy1 && (by + sy1 +
i) < avctx->
height && (by + mvy + sy1 +
i) < avctx->
height;
i++) {
1163 for (j = 0; j < sx2 - sx1 && (bx + sx1 + j) < avctx->
width && (bx + mvx + sx1 + j) < avctx->
width; j++) {
1164 dst[(by +
i + sy1) * linesize + bx + sx1 + j] = prev[(by + mvy + sy1 +
i) * plinesize + bx + sx1 + mvx + j];
1165 }
1166 }
1167 } else {
1168 int run, bx = x * 16 + sx1, by = y * 16 + sy1;
1169 uint32_t clr, ptype = 0,
r,
g,
b;
1170
1171 for (; by < y * 16 + sy2 && by < avctx->
height;) {
1173 s->op_model3[ptype].freqs[0],
1174 s->op_model3[ptype].freqs[1],
1175 s->op_model3[ptype].cnts,
1176 s->op_model3[ptype].dectab, &ptype);
1179 if (ptype == 0) {
1183
1184 clr = (
b << 16) + (
g << 8) +
r;
1185 }
1186 if (ptype > 5)
1189 s->run_model3[ptype].freqs[0],
1190 s->run_model3[ptype].freqs[1],
1191 s->run_model3[ptype].cnts,
1192 s->run_model3[ptype].dectab, &
run);
1197
1199 dst, prev, linesize, plinesize, &bx, &by,
1200 backstep, sx1, sx2, &cx, &cx1);
1203 }
1204 }
1205 }
1206 }
1207
1208 return 0;
1209 }
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