1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
|
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Progressive Codec Bitmap Compression
*
* Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
* Copyright 2019 Armin Novak <armin.novak@thincast.com>
* Copyright 2019 Thincast Technologies GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <freerdp/config.h>
#include <winpr/assert.h>
#include <winpr/crt.h>
#include <winpr/print.h>
#include <winpr/bitstream.h>
#include <freerdp/primitives.h>
#include <freerdp/codec/color.h>
#include <freerdp/codec/progressive.h>
#include <freerdp/codec/region.h>
#include <freerdp/log.h>
#include "rfx_differential.h"
#include "rfx_quantization.h"
#include "rfx_dwt.h"
#include "rfx_rlgr.h"
#include "rfx_constants.h"
#include "rfx_types.h"
#include "progressive.h"
#define TAG FREERDP_TAG("codec.progressive")
typedef struct
{
BOOL nonLL;
wBitStream* srl;
wBitStream* raw;
/* SRL state */
UINT32 kp;
int nz;
BOOL mode;
} RFX_PROGRESSIVE_UPGRADE_STATE;
static INLINE void progressive_component_codec_quant_read(wStream* s,
RFX_COMPONENT_CODEC_QUANT* quantVal)
{
BYTE b = 0;
Stream_Read_UINT8(s, b);
quantVal->LL3 = b & 0x0F;
quantVal->HL3 = b >> 4;
Stream_Read_UINT8(s, b);
quantVal->LH3 = b & 0x0F;
quantVal->HH3 = b >> 4;
Stream_Read_UINT8(s, b);
quantVal->HL2 = b & 0x0F;
quantVal->LH2 = b >> 4;
Stream_Read_UINT8(s, b);
quantVal->HH2 = b & 0x0F;
quantVal->HL1 = b >> 4;
Stream_Read_UINT8(s, b);
quantVal->LH1 = b & 0x0F;
quantVal->HH1 = b >> 4;
}
static INLINE void progressive_rfx_quant_ladd(RFX_COMPONENT_CODEC_QUANT* q, int val)
{
q->HL1 += val; /* HL1 */
q->LH1 += val; /* LH1 */
q->HH1 += val; /* HH1 */
q->HL2 += val; /* HL2 */
q->LH2 += val; /* LH2 */
q->HH2 += val; /* HH2 */
q->HL3 += val; /* HL3 */
q->LH3 += val; /* LH3 */
q->HH3 += val; /* HH3 */
q->LL3 += val; /* LL3 */
}
static INLINE void progressive_rfx_quant_add(const RFX_COMPONENT_CODEC_QUANT* q1,
const RFX_COMPONENT_CODEC_QUANT* q2,
RFX_COMPONENT_CODEC_QUANT* dst)
{
dst->HL1 = q1->HL1 + q2->HL1; /* HL1 */
dst->LH1 = q1->LH1 + q2->LH1; /* LH1 */
dst->HH1 = q1->HH1 + q2->HH1; /* HH1 */
dst->HL2 = q1->HL2 + q2->HL2; /* HL2 */
dst->LH2 = q1->LH2 + q2->LH2; /* LH2 */
dst->HH2 = q1->HH2 + q2->HH2; /* HH2 */
dst->HL3 = q1->HL3 + q2->HL3; /* HL3 */
dst->LH3 = q1->LH3 + q2->LH3; /* LH3 */
dst->HH3 = q1->HH3 + q2->HH3; /* HH3 */
dst->LL3 = q1->LL3 + q2->LL3; /* LL3 */
}
static INLINE void progressive_rfx_quant_lsub(RFX_COMPONENT_CODEC_QUANT* q, int val)
{
q->HL1 -= val; /* HL1 */
q->LH1 -= val; /* LH1 */
q->HH1 -= val; /* HH1 */
q->HL2 -= val; /* HL2 */
q->LH2 -= val; /* LH2 */
q->HH2 -= val; /* HH2 */
q->HL3 -= val; /* HL3 */
q->LH3 -= val; /* LH3 */
q->HH3 -= val; /* HH3 */
q->LL3 -= val; /* LL3 */
}
static INLINE void progressive_rfx_quant_sub(const RFX_COMPONENT_CODEC_QUANT* q1,
const RFX_COMPONENT_CODEC_QUANT* q2,
RFX_COMPONENT_CODEC_QUANT* dst)
{
dst->HL1 = q1->HL1 - q2->HL1; /* HL1 */
dst->LH1 = q1->LH1 - q2->LH1; /* LH1 */
dst->HH1 = q1->HH1 - q2->HH1; /* HH1 */
dst->HL2 = q1->HL2 - q2->HL2; /* HL2 */
dst->LH2 = q1->LH2 - q2->LH2; /* LH2 */
dst->HH2 = q1->HH2 - q2->HH2; /* HH2 */
dst->HL3 = q1->HL3 - q2->HL3; /* HL3 */
dst->LH3 = q1->LH3 - q2->LH3; /* LH3 */
dst->HH3 = q1->HH3 - q2->HH3; /* HH3 */
dst->LL3 = q1->LL3 - q2->LL3; /* LL3 */
}
static INLINE BOOL progressive_rfx_quant_lcmp_less_equal(const RFX_COMPONENT_CODEC_QUANT* q,
int val)
{
if (q->HL1 > val)
return FALSE; /* HL1 */
if (q->LH1 > val)
return FALSE; /* LH1 */
if (q->HH1 > val)
return FALSE; /* HH1 */
if (q->HL2 > val)
return FALSE; /* HL2 */
if (q->LH2 > val)
return FALSE; /* LH2 */
if (q->HH2 > val)
return FALSE; /* HH2 */
if (q->HL3 > val)
return FALSE; /* HL3 */
if (q->LH3 > val)
return FALSE; /* LH3 */
if (q->HH3 > val)
return FALSE; /* HH3 */
if (q->LL3 > val)
return FALSE; /* LL3 */
return TRUE;
}
static INLINE BOOL progressive_rfx_quant_cmp_less_equal(const RFX_COMPONENT_CODEC_QUANT* q1,
const RFX_COMPONENT_CODEC_QUANT* q2)
{
if (q1->HL1 > q2->HL1)
return FALSE; /* HL1 */
if (q1->LH1 > q2->LH1)
return FALSE; /* LH1 */
if (q1->HH1 > q2->HH1)
return FALSE; /* HH1 */
if (q1->HL2 > q2->HL2)
return FALSE; /* HL2 */
if (q1->LH2 > q2->LH2)
return FALSE; /* LH2 */
if (q1->HH2 > q2->HH2)
return FALSE; /* HH2 */
if (q1->HL3 > q2->HL3)
return FALSE; /* HL3 */
if (q1->LH3 > q2->LH3)
return FALSE; /* LH3 */
if (q1->HH3 > q2->HH3)
return FALSE; /* HH3 */
if (q1->LL3 > q2->LL3)
return FALSE; /* LL3 */
return TRUE;
}
static INLINE BOOL progressive_rfx_quant_lcmp_greater_equal(const RFX_COMPONENT_CODEC_QUANT* q,
int val)
{
if (q->HL1 < val)
return FALSE; /* HL1 */
if (q->LH1 < val)
return FALSE; /* LH1 */
if (q->HH1 < val)
return FALSE; /* HH1 */
if (q->HL2 < val)
return FALSE; /* HL2 */
if (q->LH2 < val)
return FALSE; /* LH2 */
if (q->HH2 < val)
return FALSE; /* HH2 */
if (q->HL3 < val)
return FALSE; /* HL3 */
if (q->LH3 < val)
return FALSE; /* LH3 */
if (q->HH3 < val)
return FALSE; /* HH3 */
if (q->LL3 < val)
return FALSE; /* LL3 */
return TRUE;
}
static INLINE BOOL progressive_rfx_quant_cmp_greater_equal(const RFX_COMPONENT_CODEC_QUANT* q1,
const RFX_COMPONENT_CODEC_QUANT* q2)
{
if (q1->HL1 < q2->HL1)
return FALSE; /* HL1 */
if (q1->LH1 < q2->LH1)
return FALSE; /* LH1 */
if (q1->HH1 < q2->HH1)
return FALSE; /* HH1 */
if (q1->HL2 < q2->HL2)
return FALSE; /* HL2 */
if (q1->LH2 < q2->LH2)
return FALSE; /* LH2 */
if (q1->HH2 < q2->HH2)
return FALSE; /* HH2 */
if (q1->HL3 < q2->HL3)
return FALSE; /* HL3 */
if (q1->LH3 < q2->LH3)
return FALSE; /* LH3 */
if (q1->HH3 < q2->HH3)
return FALSE; /* HH3 */
if (q1->LL3 < q2->LL3)
return FALSE; /* LL3 */
return TRUE;
}
static INLINE BOOL progressive_rfx_quant_cmp_equal(const RFX_COMPONENT_CODEC_QUANT* q1,
const RFX_COMPONENT_CODEC_QUANT* q2)
{
if (q1->HL1 != q2->HL1)
return FALSE; /* HL1 */
if (q1->LH1 != q2->LH1)
return FALSE; /* LH1 */
if (q1->HH1 != q2->HH1)
return FALSE; /* HH1 */
if (q1->HL2 != q2->HL2)
return FALSE; /* HL2 */
if (q1->LH2 != q2->LH2)
return FALSE; /* LH2 */
if (q1->HH2 != q2->HH2)
return FALSE; /* HH2 */
if (q1->HL3 != q2->HL3)
return FALSE; /* HL3 */
if (q1->LH3 != q2->LH3)
return FALSE; /* LH3 */
if (q1->HH3 != q2->HH3)
return FALSE; /* HH3 */
if (q1->LL3 != q2->LL3)
return FALSE; /* LL3 */
return TRUE;
}
static INLINE BOOL progressive_set_surface_data(PROGRESSIVE_CONTEXT* progressive, UINT16 surfaceId,
void* pData)
{
ULONG_PTR key = 0;
key = ((ULONG_PTR)surfaceId) + 1;
if (pData)
return HashTable_Insert(progressive->SurfaceContexts, (void*)key, pData);
HashTable_Remove(progressive->SurfaceContexts, (void*)key);
return TRUE;
}
static INLINE PROGRESSIVE_SURFACE_CONTEXT*
progressive_get_surface_data(PROGRESSIVE_CONTEXT* progressive, UINT16 surfaceId)
{
void* key = (void*)(((ULONG_PTR)surfaceId) + 1);
if (!progressive)
return NULL;
return HashTable_GetItemValue(progressive->SurfaceContexts, key);
}
static void progressive_tile_free(RFX_PROGRESSIVE_TILE* tile)
{
if (tile)
{
winpr_aligned_free(tile->sign);
winpr_aligned_free(tile->current);
winpr_aligned_free(tile->data);
winpr_aligned_free(tile);
}
}
static void progressive_surface_context_free(void* ptr)
{
PROGRESSIVE_SURFACE_CONTEXT* surface = ptr;
if (!surface)
return;
if (surface->tiles)
{
for (size_t index = 0; index < surface->tilesSize; index++)
{
RFX_PROGRESSIVE_TILE* tile = surface->tiles[index];
progressive_tile_free(tile);
}
}
winpr_aligned_free(surface->tiles);
winpr_aligned_free(surface->updatedTileIndices);
winpr_aligned_free(surface);
}
static INLINE RFX_PROGRESSIVE_TILE* progressive_tile_new(void)
{
RFX_PROGRESSIVE_TILE* tile = winpr_aligned_calloc(1, sizeof(RFX_PROGRESSIVE_TILE), 32);
if (!tile)
goto fail;
tile->width = 64;
tile->height = 64;
tile->stride = 4 * tile->width;
size_t dataLen = 1ull * tile->stride * tile->height;
tile->data = (BYTE*)winpr_aligned_malloc(dataLen, 16);
if (!tile->data)
goto fail;
memset(tile->data, 0xFF, dataLen);
size_t signLen = (8192 + 32) * 3;
tile->sign = (BYTE*)winpr_aligned_calloc(signLen, sizeof(BYTE), 16);
if (!tile->sign)
goto fail;
size_t currentLen = (8192 + 32) * 3;
tile->current = (BYTE*)winpr_aligned_calloc(currentLen, sizeof(BYTE), 16);
if (!tile->current)
goto fail;
return tile;
fail:
progressive_tile_free(tile);
return NULL;
}
static BOOL progressive_allocate_tile_cache(PROGRESSIVE_SURFACE_CONTEXT* surface, size_t min)
{
size_t oldIndex = 0;
WINPR_ASSERT(surface);
WINPR_ASSERT(surface->gridSize > 0);
if (surface->tiles)
{
oldIndex = surface->gridSize;
while (surface->gridSize < min)
surface->gridSize += 1024;
}
void* tmp = winpr_aligned_recalloc(surface->tiles, surface->gridSize,
sizeof(RFX_PROGRESSIVE_TILE*), 32);
if (!tmp)
return FALSE;
surface->tilesSize = surface->gridSize;
surface->tiles = tmp;
for (size_t x = oldIndex; x < surface->tilesSize; x++)
{
surface->tiles[x] = progressive_tile_new();
if (!surface->tiles[x])
return FALSE;
}
tmp =
winpr_aligned_recalloc(surface->updatedTileIndices, surface->gridSize, sizeof(UINT32), 32);
if (!tmp)
return FALSE;
surface->updatedTileIndices = tmp;
return TRUE;
}
static PROGRESSIVE_SURFACE_CONTEXT* progressive_surface_context_new(UINT16 surfaceId, UINT32 width,
UINT32 height)
{
PROGRESSIVE_SURFACE_CONTEXT* surface = (PROGRESSIVE_SURFACE_CONTEXT*)winpr_aligned_calloc(
1, sizeof(PROGRESSIVE_SURFACE_CONTEXT), 32);
if (!surface)
return NULL;
surface->id = surfaceId;
surface->width = width;
surface->height = height;
surface->gridWidth = (width + (64 - width % 64)) / 64;
surface->gridHeight = (height + (64 - height % 64)) / 64;
surface->gridSize = surface->gridWidth * surface->gridHeight;
if (!progressive_allocate_tile_cache(surface, surface->gridSize))
{
progressive_surface_context_free(surface);
return NULL;
}
return surface;
}
static BOOL progressive_surface_tile_replace(PROGRESSIVE_SURFACE_CONTEXT* surface,
PROGRESSIVE_BLOCK_REGION* region,
const RFX_PROGRESSIVE_TILE* tile, BOOL upgrade)
{
RFX_PROGRESSIVE_TILE* t = NULL;
size_t zIdx = 0;
if (!surface || !tile)
return FALSE;
zIdx = (tile->yIdx * surface->gridWidth) + tile->xIdx;
if (zIdx >= surface->tilesSize)
{
WLog_ERR(TAG, "Invalid zIndex %" PRIuz, zIdx);
return FALSE;
}
t = surface->tiles[zIdx];
t->blockType = tile->blockType;
t->blockLen = tile->blockLen;
t->quantIdxY = tile->quantIdxY;
t->quantIdxCb = tile->quantIdxCb;
t->quantIdxCr = tile->quantIdxCr;
t->xIdx = tile->xIdx;
t->yIdx = tile->yIdx;
t->flags = tile->flags;
t->quality = tile->quality;
t->x = tile->xIdx * t->width;
t->y = tile->yIdx * t->height;
if (upgrade)
{
t->ySrlLen = tile->ySrlLen;
t->yRawLen = tile->yRawLen;
t->cbSrlLen = tile->cbSrlLen;
t->cbRawLen = tile->cbRawLen;
t->crSrlLen = tile->crSrlLen;
t->crRawLen = tile->crRawLen;
t->ySrlData = tile->ySrlData;
t->yRawData = tile->yRawData;
t->cbSrlData = tile->cbSrlData;
t->cbRawData = tile->cbRawData;
t->crSrlData = tile->crSrlData;
t->crRawData = tile->crRawData;
}
else
{
t->yLen = tile->yLen;
t->cbLen = tile->cbLen;
t->crLen = tile->crLen;
t->tailLen = tile->tailLen;
t->yData = tile->yData;
t->cbData = tile->cbData;
t->crData = tile->crData;
t->tailData = tile->tailData;
}
if (region->usedTiles >= region->numTiles)
{
WLog_ERR(TAG, "Invalid tile count, only expected %" PRIu16 ", got %" PRIu16,
region->numTiles, region->usedTiles);
return FALSE;
}
region->tiles[region->usedTiles++] = t;
if (!t->dirty)
{
if (surface->numUpdatedTiles >= surface->gridSize)
{
if (!progressive_allocate_tile_cache(surface, surface->numUpdatedTiles + 1))
return FALSE;
}
surface->updatedTileIndices[surface->numUpdatedTiles++] = (UINT32)zIdx;
}
t->dirty = TRUE;
return TRUE;
}
INT32 progressive_create_surface_context(PROGRESSIVE_CONTEXT* progressive, UINT16 surfaceId,
UINT32 width, UINT32 height)
{
PROGRESSIVE_SURFACE_CONTEXT* surface = progressive_get_surface_data(progressive, surfaceId);
if (!surface)
{
surface = progressive_surface_context_new(surfaceId, width, height);
if (!surface)
return -1;
if (!progressive_set_surface_data(progressive, surfaceId, (void*)surface))
{
progressive_surface_context_free(surface);
return -1;
}
}
return 1;
}
int progressive_delete_surface_context(PROGRESSIVE_CONTEXT* progressive, UINT16 surfaceId)
{
progressive_set_surface_data(progressive, surfaceId, NULL);
return 1;
}
/*
* Band Offset Dimensions Size
*
* HL1 0 31x33 1023
* LH1 1023 33x31 1023
* HH1 2046 31x31 961
*
* HL2 3007 16x17 272
* LH2 3279 17x16 272
* HH2 3551 16x16 256
*
* HL3 3807 8x9 72
* LH3 3879 9x8 72
* HH3 3951 8x8 64
*
* LL3 4015 9x9 81
*/
static INLINE void progressive_rfx_idwt_x(const INT16* pLowBand, size_t nLowStep,
const INT16* pHighBand, size_t nHighStep, INT16* pDstBand,
size_t nDstStep, size_t nLowCount, size_t nHighCount,
size_t nDstCount)
{
INT16 L0 = 0;
INT16 H0 = 0;
INT16 H1 = 0;
INT16 X0 = 0;
INT16 X1 = 0;
INT16 X2 = 0;
for (size_t i = 0; i < nDstCount; i++)
{
const INT16* pL = pLowBand;
const INT16* pH = pHighBand;
INT16* pX = pDstBand;
H0 = *pH++;
L0 = *pL++;
X0 = L0 - H0;
X2 = L0 - H0;
for (size_t j = 0; j < (nHighCount - 1); j++)
{
H1 = *pH;
pH++;
L0 = *pL;
pL++;
X2 = L0 - ((H0 + H1) / 2);
X1 = ((X0 + X2) / 2) + (2 * H0);
pX[0] = X0;
pX[1] = X1;
pX += 2;
X0 = X2;
H0 = H1;
}
if (nLowCount <= (nHighCount + 1))
{
if (nLowCount <= nHighCount)
{
pX[0] = X2;
pX[1] = X2 + (2 * H0);
}
else
{
L0 = *pL;
pL++;
X0 = L0 - H0;
pX[0] = X2;
pX[1] = ((X0 + X2) / 2) + (2 * H0);
pX[2] = X0;
}
}
else
{
L0 = *pL;
pL++;
X0 = L0 - (H0 / 2);
pX[0] = X2;
pX[1] = ((X0 + X2) / 2) + (2 * H0);
pX[2] = X0;
L0 = *pL;
pL++;
pX[3] = (X0 + L0) / 2;
}
pLowBand += nLowStep;
pHighBand += nHighStep;
pDstBand += nDstStep;
}
}
static INLINE void progressive_rfx_idwt_y(const INT16* pLowBand, size_t nLowStep,
const INT16* pHighBand, size_t nHighStep, INT16* pDstBand,
size_t nDstStep, size_t nLowCount, size_t nHighCount,
size_t nDstCount)
{
INT16 L0 = 0;
INT16 H0 = 0;
INT16 H1 = 0;
INT16 X0 = 0;
INT16 X1 = 0;
INT16 X2 = 0;
for (size_t i = 0; i < nDstCount; i++)
{
const INT16* pL = pLowBand;
const INT16* pH = pHighBand;
INT16* pX = pDstBand;
H0 = *pH;
pH += nHighStep;
L0 = *pL;
pL += nLowStep;
X0 = L0 - H0;
X2 = L0 - H0;
for (size_t j = 0; j < (nHighCount - 1); j++)
{
H1 = *pH;
pH += nHighStep;
L0 = *pL;
pL += nLowStep;
X2 = L0 - ((H0 + H1) / 2);
X1 = ((X0 + X2) / 2) + (2 * H0);
*pX = X0;
pX += nDstStep;
*pX = X1;
pX += nDstStep;
X0 = X2;
H0 = H1;
}
if (nLowCount <= (nHighCount + 1))
{
if (nLowCount <= nHighCount)
{
*pX = X2;
pX += nDstStep;
*pX = X2 + (2 * H0);
}
else
{
L0 = *pL;
X0 = L0 - H0;
*pX = X2;
pX += nDstStep;
*pX = ((X0 + X2) / 2) + (2 * H0);
pX += nDstStep;
*pX = X0;
}
}
else
{
L0 = *pL;
pL += nLowStep;
X0 = L0 - (H0 / 2);
*pX = X2;
pX += nDstStep;
*pX = ((X0 + X2) / 2) + (2 * H0);
pX += nDstStep;
*pX = X0;
pX += nDstStep;
L0 = *pL;
*pX = (X0 + L0) / 2;
}
pLowBand++;
pHighBand++;
pDstBand++;
}
}
static INLINE size_t progressive_rfx_get_band_l_count(size_t level)
{
return (64 >> level) + 1;
}
static INLINE size_t progressive_rfx_get_band_h_count(size_t level)
{
if (level == 1)
return (64 >> 1) - 1;
else
return (64 + (1 << (level - 1))) >> level;
}
static INLINE void progressive_rfx_dwt_2d_decode_block(INT16* buffer, INT16* temp, size_t level)
{
size_t nDstStepX = 0;
size_t nDstStepY = 0;
INT16* HL = NULL;
INT16* LH = NULL;
INT16* HH = NULL;
INT16* LL = NULL;
INT16* L = NULL;
INT16* H = NULL;
INT16* LLx = NULL;
const size_t nBandL = progressive_rfx_get_band_l_count(level);
const size_t nBandH = progressive_rfx_get_band_h_count(level);
size_t offset = 0;
HL = &buffer[offset];
offset += (nBandH * nBandL);
LH = &buffer[offset];
offset += (nBandL * nBandH);
HH = &buffer[offset];
offset += (nBandH * nBandH);
LL = &buffer[offset];
nDstStepX = (nBandL + nBandH);
nDstStepY = (nBandL + nBandH);
offset = 0;
L = &temp[offset];
offset += (nBandL * nDstStepX);
H = &temp[offset];
LLx = &buffer[0];
/* horizontal (LL + HL -> L) */
progressive_rfx_idwt_x(LL, nBandL, HL, nBandH, L, nDstStepX, nBandL, nBandH, nBandL);
/* horizontal (LH + HH -> H) */
progressive_rfx_idwt_x(LH, nBandL, HH, nBandH, H, nDstStepX, nBandL, nBandH, nBandH);
/* vertical (L + H -> LL) */
progressive_rfx_idwt_y(L, nDstStepX, H, nDstStepX, LLx, nDstStepY, nBandL, nBandH,
nBandL + nBandH);
}
void rfx_dwt_2d_extrapolate_decode(INT16* buffer, INT16* temp)
{
WINPR_ASSERT(buffer);
WINPR_ASSERT(temp);
progressive_rfx_dwt_2d_decode_block(&buffer[3807], temp, 3);
progressive_rfx_dwt_2d_decode_block(&buffer[3007], temp, 2);
progressive_rfx_dwt_2d_decode_block(&buffer[0], temp, 1);
}
static INLINE int progressive_rfx_dwt_2d_decode(PROGRESSIVE_CONTEXT* progressive, INT16* buffer,
INT16* current, BOOL coeffDiff, BOOL extrapolate,
BOOL reverse)
{
const primitives_t* prims = primitives_get();
if (!progressive || !buffer || !current)
return -1;
INT16 dst[4096] = { 0 };
if (reverse)
memcpy(buffer, current, sizeof(dst));
else
{
if (coeffDiff)
{
prims->add_16s(buffer, current, dst, ARRAYSIZE(dst));
memcpy(current, dst, sizeof(dst));
memcpy(buffer, dst, sizeof(dst));
}
else
memcpy(current, buffer, sizeof(dst));
}
INT16* temp = (INT16*)BufferPool_Take(progressive->bufferPool, -1); /* DWT buffer */
if (!temp)
return -2;
if (!extrapolate)
{
progressive->rfx_context->dwt_2d_decode(buffer, temp);
}
else
{
WINPR_ASSERT(progressive->rfx_context->dwt_2d_extrapolate_decode);
progressive->rfx_context->dwt_2d_extrapolate_decode(buffer, temp);
}
BufferPool_Return(progressive->bufferPool, temp);
return 1;
}
static INLINE void progressive_rfx_decode_block(const primitives_t* prims, INT16* buffer,
UINT32 length, UINT32 shift)
{
if (!shift)
return;
prims->lShiftC_16s(buffer, shift, buffer, length);
}
static INLINE int progressive_rfx_decode_component(PROGRESSIVE_CONTEXT* progressive,
const RFX_COMPONENT_CODEC_QUANT* shift,
const BYTE* data, UINT32 length, INT16* buffer,
INT16* current, INT16* sign, BOOL coeffDiff,
BOOL subbandDiff, BOOL extrapolate)
{
int status = 0;
const primitives_t* prims = primitives_get();
status = progressive->rfx_context->rlgr_decode(RLGR1, data, length, buffer, 4096);
if (status < 0)
return status;
CopyMemory(sign, buffer, 4096 * 2);
if (!extrapolate)
{
rfx_differential_decode(buffer + 4032, 64);
progressive_rfx_decode_block(prims, &buffer[0], 1024, shift->HL1); /* HL1 */
progressive_rfx_decode_block(prims, &buffer[1024], 1024, shift->LH1); /* LH1 */
progressive_rfx_decode_block(prims, &buffer[2048], 1024, shift->HH1); /* HH1 */
progressive_rfx_decode_block(prims, &buffer[3072], 256, shift->HL2); /* HL2 */
progressive_rfx_decode_block(prims, &buffer[3328], 256, shift->LH2); /* LH2 */
progressive_rfx_decode_block(prims, &buffer[3584], 256, shift->HH2); /* HH2 */
progressive_rfx_decode_block(prims, &buffer[3840], 64, shift->HL3); /* HL3 */
progressive_rfx_decode_block(prims, &buffer[3904], 64, shift->LH3); /* LH3 */
progressive_rfx_decode_block(prims, &buffer[3968], 64, shift->HH3); /* HH3 */
progressive_rfx_decode_block(prims, &buffer[4032], 64, shift->LL3); /* LL3 */
}
else
{
progressive_rfx_decode_block(prims, &buffer[0], 1023, shift->HL1); /* HL1 */
progressive_rfx_decode_block(prims, &buffer[1023], 1023, shift->LH1); /* LH1 */
progressive_rfx_decode_block(prims, &buffer[2046], 961, shift->HH1); /* HH1 */
progressive_rfx_decode_block(prims, &buffer[3007], 272, shift->HL2); /* HL2 */
progressive_rfx_decode_block(prims, &buffer[3279], 272, shift->LH2); /* LH2 */
progressive_rfx_decode_block(prims, &buffer[3551], 256, shift->HH2); /* HH2 */
progressive_rfx_decode_block(prims, &buffer[3807], 72, shift->HL3); /* HL3 */
progressive_rfx_decode_block(prims, &buffer[3879], 72, shift->LH3); /* LH3 */
progressive_rfx_decode_block(prims, &buffer[3951], 64, shift->HH3); /* HH3 */
rfx_differential_decode(&buffer[4015], 81); /* LL3 */
progressive_rfx_decode_block(prims, &buffer[4015], 81, shift->LL3); /* LL3 */
}
return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
FALSE);
}
static INLINE int progressive_decompress_tile_first(PROGRESSIVE_CONTEXT* progressive,
RFX_PROGRESSIVE_TILE* tile,
PROGRESSIVE_BLOCK_REGION* region,
const PROGRESSIVE_BLOCK_CONTEXT* context)
{
int rc = 0;
BOOL diff = 0;
BOOL sub = 0;
BOOL extrapolate = 0;
BYTE* pBuffer = NULL;
INT16* pSign[3];
INT16* pSrcDst[3];
INT16* pCurrent[3];
RFX_COMPONENT_CODEC_QUANT shiftY = { 0 };
RFX_COMPONENT_CODEC_QUANT shiftCb = { 0 };
RFX_COMPONENT_CODEC_QUANT shiftCr = { 0 };
RFX_COMPONENT_CODEC_QUANT* quantY = NULL;
RFX_COMPONENT_CODEC_QUANT* quantCb = NULL;
RFX_COMPONENT_CODEC_QUANT* quantCr = NULL;
RFX_COMPONENT_CODEC_QUANT* quantProgY = NULL;
RFX_COMPONENT_CODEC_QUANT* quantProgCb = NULL;
RFX_COMPONENT_CODEC_QUANT* quantProgCr = NULL;
RFX_PROGRESSIVE_CODEC_QUANT* quantProgVal = NULL;
static const prim_size_t roi_64x64 = { 64, 64 };
const primitives_t* prims = primitives_get();
tile->pass = 1;
diff = tile->flags & RFX_TILE_DIFFERENCE;
sub = context->flags & RFX_SUBBAND_DIFFING;
extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
#if defined(WITH_DEBUG_CODECS)
WLog_Print(progressive->log, WLOG_DEBUG,
"ProgressiveTile%s: quantIdx Y: %" PRIu8 " Cb: %" PRIu8 " Cr: %" PRIu8
" xIdx: %" PRIu16 " yIdx: %" PRIu16 " flags: 0x%02" PRIX8 " quality: %" PRIu8
" yLen: %" PRIu16 " cbLen: %" PRIu16 " crLen: %" PRIu16 " tailLen: %" PRIu16 "",
(tile->blockType == PROGRESSIVE_WBT_TILE_FIRST) ? "First" : "Simple",
tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx, tile->yIdx,
tile->flags, tile->quality, tile->yLen, tile->cbLen, tile->crLen, tile->tailLen);
#endif
if (tile->quantIdxY >= region->numQuant)
{
WLog_ERR(TAG, "quantIdxY %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
return -1;
}
quantY = &(region->quantVals[tile->quantIdxY]);
if (tile->quantIdxCb >= region->numQuant)
{
WLog_ERR(TAG, "quantIdxCb %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCb,
region->numQuant);
return -1;
}
quantCb = &(region->quantVals[tile->quantIdxCb]);
if (tile->quantIdxCr >= region->numQuant)
{
WLog_ERR(TAG, "quantIdxCr %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCr,
region->numQuant);
return -1;
}
quantCr = &(region->quantVals[tile->quantIdxCr]);
if (tile->quality == 0xFF)
{
quantProgVal = &(progressive->quantProgValFull);
}
else
{
if (tile->quality >= region->numProgQuant)
{
WLog_ERR(TAG, "quality %" PRIu8 " > numProgQuant %" PRIu8, tile->quality,
region->numProgQuant);
return -1;
}
quantProgVal = &(region->quantProgVals[tile->quality]);
}
quantProgY = &(quantProgVal->yQuantValues);
quantProgCb = &(quantProgVal->cbQuantValues);
quantProgCr = &(quantProgVal->crQuantValues);
tile->yQuant = *quantY;
tile->cbQuant = *quantCb;
tile->crQuant = *quantCr;
tile->yProgQuant = *quantProgY;
tile->cbProgQuant = *quantProgCb;
tile->crProgQuant = *quantProgCr;
progressive_rfx_quant_add(quantY, quantProgY, &(tile->yBitPos));
progressive_rfx_quant_add(quantCb, quantProgCb, &(tile->cbBitPos));
progressive_rfx_quant_add(quantCr, quantProgCr, &(tile->crBitPos));
progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
progressive_rfx_quant_lsub(&shiftY, 1); /* -6 + 5 = -1 */
progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
progressive_rfx_quant_lsub(&shiftCb, 1); /* -6 + 5 = -1 */
progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
progressive_rfx_quant_lsub(&shiftCr, 1); /* -6 + 5 = -1 */
pSign[0] = (INT16*)((BYTE*)(&tile->sign[((8192 + 32) * 0) + 16])); /* Y/R buffer */
pSign[1] = (INT16*)((BYTE*)(&tile->sign[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
pSign[2] = (INT16*)((BYTE*)(&tile->sign[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
pCurrent[0] = (INT16*)((BYTE*)(&tile->current[((8192 + 32) * 0) + 16])); /* Y/R buffer */
pCurrent[1] = (INT16*)((BYTE*)(&tile->current[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
pCurrent[2] = (INT16*)((BYTE*)(&tile->current[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
pBuffer = (BYTE*)BufferPool_Take(progressive->bufferPool, -1);
pSrcDst[0] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 0) + 16])); /* Y/R buffer */
pSrcDst[1] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
pSrcDst[2] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
rc = progressive_rfx_decode_component(progressive, &shiftY, tile->yData, tile->yLen, pSrcDst[0],
pCurrent[0], pSign[0], diff, sub, extrapolate); /* Y */
if (rc < 0)
goto fail;
rc = progressive_rfx_decode_component(progressive, &shiftCb, tile->cbData, tile->cbLen,
pSrcDst[1], pCurrent[1], pSign[1], diff, sub,
extrapolate); /* Cb */
if (rc < 0)
goto fail;
rc = progressive_rfx_decode_component(progressive, &shiftCr, tile->crData, tile->crLen,
pSrcDst[2], pCurrent[2], pSign[2], diff, sub,
extrapolate); /* Cr */
if (rc < 0)
goto fail;
rc = prims->yCbCrToRGB_16s8u_P3AC4R((const INT16* const*)pSrcDst, 64 * 2, tile->data,
tile->stride, progressive->format, &roi_64x64);
fail:
BufferPool_Return(progressive->bufferPool, pBuffer);
return rc;
}
static INLINE INT16 progressive_rfx_srl_read(RFX_PROGRESSIVE_UPGRADE_STATE* state, UINT32 numBits)
{
UINT32 k = 0;
UINT32 bit = 0;
UINT32 max = 0;
UINT32 mag = 0;
UINT32 sign = 0;
wBitStream* bs = state->srl;
if (state->nz)
{
state->nz--;
return 0;
}
k = state->kp / 8;
if (!state->mode)
{
/* zero encoding */
bit = (bs->accumulator & 0x80000000) ? 1 : 0;
BitStream_Shift(bs, 1);
if (!bit)
{
/* '0' bit, nz >= (1 << k), nz = (1 << k) */
state->nz = (1 << k);
state->kp += 4;
if (state->kp > 80)
state->kp = 80;
state->nz--;
return 0;
}
else
{
/* '1' bit, nz < (1 << k), nz = next k bits */
state->nz = 0;
state->mode = 1; /* unary encoding is next */
if (k)
{
bs->mask = ((1 << k) - 1);
state->nz = ((bs->accumulator >> (32u - k)) & bs->mask);
BitStream_Shift(bs, k);
}
if (state->nz)
{
state->nz--;
return 0;
}
}
}
state->mode = 0; /* zero encoding is next */
/* unary encoding */
/* read sign bit */
sign = (bs->accumulator & 0x80000000) ? 1 : 0;
BitStream_Shift(bs, 1);
if (state->kp < 6)
state->kp = 0;
else
state->kp -= 6;
if (numBits == 1)
return sign ? -1 : 1;
mag = 1;
max = (1 << numBits) - 1;
while (mag < max)
{
bit = (bs->accumulator & 0x80000000) ? 1 : 0;
BitStream_Shift(bs, 1);
if (bit)
break;
mag++;
}
return sign ? -1 * mag : mag;
}
static INLINE int progressive_rfx_upgrade_state_finish(RFX_PROGRESSIVE_UPGRADE_STATE* state)
{
UINT32 pad = 0;
wBitStream* srl = NULL;
wBitStream* raw = NULL;
if (!state)
return -1;
srl = state->srl;
raw = state->raw;
/* Read trailing bits from RAW/SRL bit streams */
pad = (raw->position % 8) ? (8 - (raw->position % 8)) : 0;
if (pad)
BitStream_Shift(raw, pad);
pad = (srl->position % 8) ? (8 - (srl->position % 8)) : 0;
if (pad)
BitStream_Shift(srl, pad);
if (BitStream_GetRemainingLength(srl) == 8)
BitStream_Shift(srl, 8);
return 1;
}
static INLINE int progressive_rfx_upgrade_block(RFX_PROGRESSIVE_UPGRADE_STATE* state, INT16* buffer,
INT16* sign, UINT32 length, UINT32 shift,
UINT32 bitPos, UINT32 numBits)
{
INT16 input = 0;
wBitStream* raw = NULL;
if (!numBits)
return 1;
raw = state->raw;
if (!state->nonLL)
{
for (UINT32 index = 0; index < length; index++)
{
raw->mask = ((1 << numBits) - 1);
input = (INT16)((raw->accumulator >> (32 - numBits)) & raw->mask);
BitStream_Shift(raw, numBits);
buffer[index] += (input << shift);
}
return 1;
}
for (UINT32 index = 0; index < length; index++)
{
if (sign[index] > 0)
{
/* sign > 0, read from raw */
raw->mask = ((1 << numBits) - 1);
input = (INT16)((raw->accumulator >> (32 - numBits)) & raw->mask);
BitStream_Shift(raw, numBits);
}
else if (sign[index] < 0)
{
/* sign < 0, read from raw */
raw->mask = ((1 << numBits) - 1);
input = (INT16)((raw->accumulator >> (32 - numBits)) & raw->mask);
BitStream_Shift(raw, numBits);
input *= -1;
}
else
{
/* sign == 0, read from srl */
input = progressive_rfx_srl_read(state, numBits);
sign[index] = input;
}
buffer[index] += (INT16)((UINT32)input << shift);
}
return 1;
}
static INLINE int progressive_rfx_upgrade_component(
PROGRESSIVE_CONTEXT* progressive, const RFX_COMPONENT_CODEC_QUANT* shift,
const RFX_COMPONENT_CODEC_QUANT* bitPos, const RFX_COMPONENT_CODEC_QUANT* numBits,
INT16* buffer, INT16* current, INT16* sign, const BYTE* srlData, UINT32 srlLen,
const BYTE* rawData, UINT32 rawLen, BOOL coeffDiff, BOOL subbandDiff, BOOL extrapolate)
{
int rc = 0;
UINT32 aRawLen = 0;
UINT32 aSrlLen = 0;
wBitStream s_srl = { 0 };
wBitStream s_raw = { 0 };
RFX_PROGRESSIVE_UPGRADE_STATE state = { 0 };
state.kp = 8;
state.mode = 0;
state.srl = &s_srl;
state.raw = &s_raw;
BitStream_Attach(state.srl, srlData, srlLen);
BitStream_Fetch(state.srl);
BitStream_Attach(state.raw, rawData, rawLen);
BitStream_Fetch(state.raw);
state.nonLL = TRUE;
rc = progressive_rfx_upgrade_block(&state, ¤t[0], &sign[0], 1023, shift->HL1, bitPos->HL1,
numBits->HL1); /* HL1 */
if (rc < 0)
return rc;
rc = progressive_rfx_upgrade_block(&state, ¤t[1023], &sign[1023], 1023, shift->LH1,
bitPos->LH1, numBits->LH1); /* LH1 */
if (rc < 0)
return rc;
rc = progressive_rfx_upgrade_block(&state, ¤t[2046], &sign[2046], 961, shift->HH1,
bitPos->HH1, numBits->HH1); /* HH1 */
if (rc < 0)
return rc;
rc = progressive_rfx_upgrade_block(&state, ¤t[3007], &sign[3007], 272, shift->HL2,
bitPos->HL2, numBits->HL2); /* HL2 */
if (rc < 0)
return rc;
rc = progressive_rfx_upgrade_block(&state, ¤t[3279], &sign[3279], 272, shift->LH2,
bitPos->LH2, numBits->LH2); /* LH2 */
if (rc < 0)
return rc;
rc = progressive_rfx_upgrade_block(&state, ¤t[3551], &sign[3551], 256, shift->HH2,
bitPos->HH2, numBits->HH2); /* HH2 */
if (rc < 0)
return rc;
rc = progressive_rfx_upgrade_block(&state, ¤t[3807], &sign[3807], 72, shift->HL3,
bitPos->HL3, numBits->HL3); /* HL3 */
if (rc < 0)
return rc;
rc = progressive_rfx_upgrade_block(&state, ¤t[3879], &sign[3879], 72, shift->LH3,
bitPos->LH3, numBits->LH3); /* LH3 */
if (rc < 0)
return rc;
rc = progressive_rfx_upgrade_block(&state, ¤t[3951], &sign[3951], 64, shift->HH3,
bitPos->HH3, numBits->HH3); /* HH3 */
if (rc < 0)
return rc;
state.nonLL = FALSE;
rc = progressive_rfx_upgrade_block(&state, ¤t[4015], &sign[4015], 81, shift->LL3,
bitPos->LL3, numBits->LL3); /* LL3 */
if (rc < 0)
return rc;
rc = progressive_rfx_upgrade_state_finish(&state);
if (rc < 0)
return rc;
aRawLen = (state.raw->position + 7) / 8;
aSrlLen = (state.srl->position + 7) / 8;
if ((aRawLen != rawLen) || (aSrlLen != srlLen))
{
int pRawLen = 0;
int pSrlLen = 0;
if (rawLen)
pRawLen = (int)((((float)aRawLen) / ((float)rawLen)) * 100.0f);
if (srlLen)
pSrlLen = (int)((((float)aSrlLen) / ((float)srlLen)) * 100.0f);
WLog_Print(progressive->log, WLOG_WARN,
"RAW: %" PRIu32 "/%" PRIu32 " %d%% (%" PRIu32 "/%" PRIu32 ":%" PRIu32
")\tSRL: %" PRIu32 "/%" PRIu32 " %d%% (%" PRIu32 "/%" PRIu32 ":%" PRIu32 ")",
aRawLen, rawLen, pRawLen, state.raw->position, rawLen * 8,
(rawLen * 8) - state.raw->position, aSrlLen, srlLen, pSrlLen,
state.srl->position, srlLen * 8, (srlLen * 8) - state.srl->position);
return -1;
}
return progressive_rfx_dwt_2d_decode(progressive, buffer, current, coeffDiff, extrapolate,
TRUE);
}
static INLINE int progressive_decompress_tile_upgrade(PROGRESSIVE_CONTEXT* progressive,
RFX_PROGRESSIVE_TILE* tile,
PROGRESSIVE_BLOCK_REGION* region,
const PROGRESSIVE_BLOCK_CONTEXT* context)
{
int status = 0;
BOOL coeffDiff = 0;
BOOL sub = 0;
BOOL extrapolate = 0;
BYTE* pBuffer = NULL;
INT16* pSign[3] = { 0 };
INT16* pSrcDst[3] = { 0 };
INT16* pCurrent[3] = { 0 };
RFX_COMPONENT_CODEC_QUANT shiftY = { 0 };
RFX_COMPONENT_CODEC_QUANT shiftCb = { 0 };
RFX_COMPONENT_CODEC_QUANT shiftCr = { 0 };
RFX_COMPONENT_CODEC_QUANT yBitPos = { 0 };
RFX_COMPONENT_CODEC_QUANT cbBitPos = { 0 };
RFX_COMPONENT_CODEC_QUANT crBitPos = { 0 };
RFX_COMPONENT_CODEC_QUANT yNumBits = { 0 };
RFX_COMPONENT_CODEC_QUANT cbNumBits = { 0 };
RFX_COMPONENT_CODEC_QUANT crNumBits = { 0 };
RFX_COMPONENT_CODEC_QUANT* quantY = NULL;
RFX_COMPONENT_CODEC_QUANT* quantCb = NULL;
RFX_COMPONENT_CODEC_QUANT* quantCr = NULL;
RFX_COMPONENT_CODEC_QUANT* quantProgY = NULL;
RFX_COMPONENT_CODEC_QUANT* quantProgCb = NULL;
RFX_COMPONENT_CODEC_QUANT* quantProgCr = NULL;
RFX_PROGRESSIVE_CODEC_QUANT* quantProg = NULL;
static const prim_size_t roi_64x64 = { 64, 64 };
const primitives_t* prims = primitives_get();
coeffDiff = tile->flags & RFX_TILE_DIFFERENCE;
sub = context->flags & RFX_SUBBAND_DIFFING;
extrapolate = region->flags & RFX_DWT_REDUCE_EXTRAPOLATE;
tile->pass++;
#if defined(WITH_DEBUG_CODECS)
WLog_Print(progressive->log, WLOG_DEBUG,
"ProgressiveTileUpgrade: pass: %" PRIu16 " quantIdx Y: %" PRIu8 " Cb: %" PRIu8
" Cr: %" PRIu8 " xIdx: %" PRIu16 " yIdx: %" PRIu16 " quality: %" PRIu8
" ySrlLen: %" PRIu16 " yRawLen: %" PRIu16 " cbSrlLen: %" PRIu16 " cbRawLen: %" PRIu16
" crSrlLen: %" PRIu16 " crRawLen: %" PRIu16 "",
tile->pass, tile->quantIdxY, tile->quantIdxCb, tile->quantIdxCr, tile->xIdx,
tile->yIdx, tile->quality, tile->ySrlLen, tile->yRawLen, tile->cbSrlLen,
tile->cbRawLen, tile->crSrlLen, tile->crRawLen);
#endif
if (tile->quantIdxY >= region->numQuant)
{
WLog_ERR(TAG, "quantIdxY %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxY, region->numQuant);
return -1;
}
quantY = &(region->quantVals[tile->quantIdxY]);
if (tile->quantIdxCb >= region->numQuant)
{
WLog_ERR(TAG, "quantIdxCb %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCb,
region->numQuant);
return -1;
}
quantCb = &(region->quantVals[tile->quantIdxCb]);
if (tile->quantIdxCr >= region->numQuant)
{
WLog_ERR(TAG, "quantIdxCr %" PRIu8 " > numQuant %" PRIu8, tile->quantIdxCr,
region->numQuant);
return -1;
}
quantCr = &(region->quantVals[tile->quantIdxCr]);
if (tile->quality == 0xFF)
{
quantProg = &(progressive->quantProgValFull);
}
else
{
if (tile->quality >= region->numProgQuant)
{
WLog_ERR(TAG, "quality %" PRIu8 " > numProgQuant %" PRIu8, tile->quality,
region->numProgQuant);
return -1;
}
quantProg = &(region->quantProgVals[tile->quality]);
}
quantProgY = &(quantProg->yQuantValues);
quantProgCb = &(quantProg->cbQuantValues);
quantProgCr = &(quantProg->crQuantValues);
if (!progressive_rfx_quant_cmp_equal(quantY, &(tile->yQuant)))
WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantY has changed!");
if (!progressive_rfx_quant_cmp_equal(quantCb, &(tile->cbQuant)))
WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantCb has changed!");
if (!progressive_rfx_quant_cmp_equal(quantCr, &(tile->crQuant)))
WLog_Print(progressive->log, WLOG_WARN, "non-progressive quantCr has changed!");
if (!(context->flags & RFX_SUBBAND_DIFFING))
WLog_WARN(TAG, "PROGRESSIVE_BLOCK_CONTEXT::flags & RFX_SUBBAND_DIFFING not set");
progressive_rfx_quant_add(quantY, quantProgY, &yBitPos);
progressive_rfx_quant_add(quantCb, quantProgCb, &cbBitPos);
progressive_rfx_quant_add(quantCr, quantProgCr, &crBitPos);
progressive_rfx_quant_sub(&(tile->yBitPos), &yBitPos, &yNumBits);
progressive_rfx_quant_sub(&(tile->cbBitPos), &cbBitPos, &cbNumBits);
progressive_rfx_quant_sub(&(tile->crBitPos), &crBitPos, &crNumBits);
progressive_rfx_quant_add(quantY, quantProgY, &shiftY);
progressive_rfx_quant_lsub(&shiftY, 1); /* -6 + 5 = -1 */
progressive_rfx_quant_add(quantCb, quantProgCb, &shiftCb);
progressive_rfx_quant_lsub(&shiftCb, 1); /* -6 + 5 = -1 */
progressive_rfx_quant_add(quantCr, quantProgCr, &shiftCr);
progressive_rfx_quant_lsub(&shiftCr, 1); /* -6 + 5 = -1 */
tile->yBitPos = yBitPos;
tile->cbBitPos = cbBitPos;
tile->crBitPos = crBitPos;
tile->yQuant = *quantY;
tile->cbQuant = *quantCb;
tile->crQuant = *quantCr;
tile->yProgQuant = *quantProgY;
tile->cbProgQuant = *quantProgCb;
tile->crProgQuant = *quantProgCr;
pSign[0] = (INT16*)((BYTE*)(&tile->sign[((8192 + 32) * 0) + 16])); /* Y/R buffer */
pSign[1] = (INT16*)((BYTE*)(&tile->sign[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
pSign[2] = (INT16*)((BYTE*)(&tile->sign[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
pCurrent[0] = (INT16*)((BYTE*)(&tile->current[((8192 + 32) * 0) + 16])); /* Y/R buffer */
pCurrent[1] = (INT16*)((BYTE*)(&tile->current[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
pCurrent[2] = (INT16*)((BYTE*)(&tile->current[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
pBuffer = (BYTE*)BufferPool_Take(progressive->bufferPool, -1);
pSrcDst[0] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 0) + 16])); /* Y/R buffer */
pSrcDst[1] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 1) + 16])); /* Cb/G buffer */
pSrcDst[2] = (INT16*)((BYTE*)(&pBuffer[((8192 + 32) * 2) + 16])); /* Cr/B buffer */
status = progressive_rfx_upgrade_component(progressive, &shiftY, quantProgY, &yNumBits,
pSrcDst[0], pCurrent[0], pSign[0], tile->ySrlData,
tile->ySrlLen, tile->yRawData, tile->yRawLen,
coeffDiff, sub, extrapolate); /* Y */
if (status < 0)
goto fail;
status = progressive_rfx_upgrade_component(progressive, &shiftCb, quantProgCb, &cbNumBits,
pSrcDst[1], pCurrent[1], pSign[1], tile->cbSrlData,
tile->cbSrlLen, tile->cbRawData, tile->cbRawLen,
coeffDiff, sub, extrapolate); /* Cb */
if (status < 0)
goto fail;
status = progressive_rfx_upgrade_component(progressive, &shiftCr, quantProgCr, &crNumBits,
pSrcDst[2], pCurrent[2], pSign[2], tile->crSrlData,
tile->crSrlLen, tile->crRawData, tile->crRawLen,
coeffDiff, sub, extrapolate); /* Cr */
if (status < 0)
goto fail;
status = prims->yCbCrToRGB_16s8u_P3AC4R((const INT16* const*)pSrcDst, 64 * 2, tile->data,
tile->stride, progressive->format, &roi_64x64);
fail:
BufferPool_Return(progressive->bufferPool, pBuffer);
return status;
}
static INLINE BOOL progressive_tile_read_upgrade(PROGRESSIVE_CONTEXT* progressive, wStream* s,
UINT16 blockType, UINT32 blockLen,
PROGRESSIVE_SURFACE_CONTEXT* surface,
PROGRESSIVE_BLOCK_REGION* region,
const PROGRESSIVE_BLOCK_CONTEXT* context)
{
RFX_PROGRESSIVE_TILE tile = { 0 };
const size_t expect = 20;
if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
return FALSE;
tile.blockType = blockType;
tile.blockLen = blockLen;
tile.flags = 0;
Stream_Read_UINT8(s, tile.quantIdxY);
Stream_Read_UINT8(s, tile.quantIdxCb);
Stream_Read_UINT8(s, tile.quantIdxCr);
Stream_Read_UINT16(s, tile.xIdx);
Stream_Read_UINT16(s, tile.yIdx);
Stream_Read_UINT8(s, tile.quality);
Stream_Read_UINT16(s, tile.ySrlLen);
Stream_Read_UINT16(s, tile.yRawLen);
Stream_Read_UINT16(s, tile.cbSrlLen);
Stream_Read_UINT16(s, tile.cbRawLen);
Stream_Read_UINT16(s, tile.crSrlLen);
Stream_Read_UINT16(s, tile.crRawLen);
tile.ySrlData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.ySrlLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.ySrlLen);
return FALSE;
}
tile.yRawData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.yRawLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.yRawLen);
return FALSE;
}
tile.cbSrlData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.cbSrlLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
tile.cbSrlLen);
return FALSE;
}
tile.cbRawData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.cbRawLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
tile.cbRawLen);
return FALSE;
}
tile.crSrlData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.crSrlLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
tile.crSrlLen);
return FALSE;
}
tile.crRawData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.crRawLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes",
tile.crRawLen);
return FALSE;
}
return progressive_surface_tile_replace(surface, region, &tile, TRUE);
}
static INLINE BOOL progressive_tile_read(PROGRESSIVE_CONTEXT* progressive, BOOL simple, wStream* s,
UINT16 blockType, UINT32 blockLen,
PROGRESSIVE_SURFACE_CONTEXT* surface,
PROGRESSIVE_BLOCK_REGION* region,
const PROGRESSIVE_BLOCK_CONTEXT* context)
{
RFX_PROGRESSIVE_TILE tile = { 0 };
size_t expect = simple ? 16 : 17;
if (!Stream_CheckAndLogRequiredLength(TAG, s, expect))
return FALSE;
tile.blockType = blockType;
tile.blockLen = blockLen;
Stream_Read_UINT8(s, tile.quantIdxY);
Stream_Read_UINT8(s, tile.quantIdxCb);
Stream_Read_UINT8(s, tile.quantIdxCr);
Stream_Read_UINT16(s, tile.xIdx);
Stream_Read_UINT16(s, tile.yIdx);
Stream_Read_UINT8(s, tile.flags);
if (!simple)
Stream_Read_UINT8(s, tile.quality);
else
tile.quality = 0xFF;
Stream_Read_UINT16(s, tile.yLen);
Stream_Read_UINT16(s, tile.cbLen);
Stream_Read_UINT16(s, tile.crLen);
Stream_Read_UINT16(s, tile.tailLen);
tile.yData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.yLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.yLen);
return FALSE;
}
tile.cbData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.cbLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.cbLen);
return FALSE;
}
tile.crData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.crLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.crLen);
return FALSE;
}
tile.tailData = Stream_Pointer(s);
if (!Stream_SafeSeek(s, tile.tailLen))
{
WLog_Print(progressive->log, WLOG_ERROR, " Failed to seek %" PRIu32 " bytes", tile.tailLen);
return FALSE;
}
return progressive_surface_tile_replace(surface, region, &tile, FALSE);
}
typedef struct
{
PROGRESSIVE_CONTEXT* progressive;
PROGRESSIVE_BLOCK_REGION* region;
const PROGRESSIVE_BLOCK_CONTEXT* context;
RFX_PROGRESSIVE_TILE* tile;
} PROGRESSIVE_TILE_PROCESS_WORK_PARAM;
static void CALLBACK progressive_process_tiles_tile_work_callback(PTP_CALLBACK_INSTANCE instance,
void* context, PTP_WORK work)
{
PROGRESSIVE_TILE_PROCESS_WORK_PARAM* param = (PROGRESSIVE_TILE_PROCESS_WORK_PARAM*)context;
WINPR_UNUSED(instance);
WINPR_UNUSED(work);
switch (param->tile->blockType)
{
case PROGRESSIVE_WBT_TILE_SIMPLE:
case PROGRESSIVE_WBT_TILE_FIRST:
progressive_decompress_tile_first(param->progressive, param->tile, param->region,
param->context);
break;
case PROGRESSIVE_WBT_TILE_UPGRADE:
progressive_decompress_tile_upgrade(param->progressive, param->tile, param->region,
param->context);
break;
default:
WLog_Print(param->progressive->log, WLOG_ERROR, "Invalid block type %04" PRIx16 " (%s)",
param->tile->blockType,
rfx_get_progressive_block_type_string(param->tile->blockType));
break;
}
}
static INLINE SSIZE_T progressive_process_tiles(PROGRESSIVE_CONTEXT* progressive, wStream* s,
PROGRESSIVE_BLOCK_REGION* region,
PROGRESSIVE_SURFACE_CONTEXT* surface,
const PROGRESSIVE_BLOCK_CONTEXT* context)
{
int status = 0;
size_t end = 0;
const size_t start = Stream_GetPosition(s);
UINT16 index = 0;
UINT16 blockType = 0;
UINT32 blockLen = 0;
UINT32 count = 0;
PTP_WORK* work_objects = NULL;
PROGRESSIVE_TILE_PROCESS_WORK_PARAM* params = NULL;
UINT16 close_cnt = 0;
WINPR_ASSERT(progressive);
WINPR_ASSERT(region);
if (!Stream_CheckAndLogRequiredLength(TAG, s, region->tileDataSize))
return -1;
while ((Stream_GetRemainingLength(s) >= 6) &&
(region->tileDataSize > (Stream_GetPosition(s) - start)))
{
const size_t pos = Stream_GetPosition(s);
Stream_Read_UINT16(s, blockType);
Stream_Read_UINT32(s, blockLen);
#if defined(WITH_DEBUG_CODECS)
WLog_Print(progressive->log, WLOG_DEBUG, "%s",
rfx_get_progressive_block_type_string(blockType));
#endif
if (blockLen < 6)
{
WLog_Print(progressive->log, WLOG_ERROR, "Expected >= %" PRIu32 " remaining %" PRIuz, 6,
blockLen);
return -1003;
}
if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
return -1003;
switch (blockType)
{
case PROGRESSIVE_WBT_TILE_SIMPLE:
if (!progressive_tile_read(progressive, TRUE, s, blockType, blockLen, surface,
region, context))
return -1022;
break;
case PROGRESSIVE_WBT_TILE_FIRST:
if (!progressive_tile_read(progressive, FALSE, s, blockType, blockLen, surface,
region, context))
return -1027;
break;
case PROGRESSIVE_WBT_TILE_UPGRADE:
if (!progressive_tile_read_upgrade(progressive, s, blockType, blockLen, surface,
region, context))
return -1032;
break;
default:
WLog_ERR(TAG, "Invalid block type %04" PRIx16 " (%s)", blockType,
rfx_get_progressive_block_type_string(blockType));
return -1039;
}
size_t rem = Stream_GetPosition(s);
if ((rem - pos) != blockLen)
{
WLog_Print(progressive->log, WLOG_ERROR,
"Actual block read %" PRIuz " but expected %" PRIu32, rem - pos, blockLen);
return -1040;
}
count++;
}
end = Stream_GetPosition(s);
if ((end - start) != region->tileDataSize)
{
WLog_Print(progressive->log, WLOG_ERROR,
"Actual total blocks read %" PRIuz " but expected %" PRIu32, end - start,
region->tileDataSize);
return -1041;
}
if (count != region->numTiles)
{
WLog_Print(progressive->log, WLOG_WARN,
"numTiles inconsistency: actual: %" PRIu32 ", expected: %" PRIu16 "\n", count,
region->numTiles);
return -1044;
}
{
size_t tcount = 1;
if (progressive->rfx_context->priv->UseThreads)
tcount = region->numTiles;
work_objects = (PTP_WORK*)winpr_aligned_calloc(tcount, sizeof(PTP_WORK), 32);
if (!work_objects)
return -1;
}
params = (PROGRESSIVE_TILE_PROCESS_WORK_PARAM*)winpr_aligned_calloc(
region->numTiles, sizeof(PROGRESSIVE_TILE_PROCESS_WORK_PARAM), 32);
if (!params)
{
winpr_aligned_free(work_objects);
return -1;
}
for (UINT32 index = 0; index < region->numTiles; index++)
{
RFX_PROGRESSIVE_TILE* tile = region->tiles[index];
PROGRESSIVE_TILE_PROCESS_WORK_PARAM* param = ¶ms[index];
param->progressive = progressive;
param->region = region;
param->context = context;
param->tile = tile;
if (progressive->rfx_context->priv->UseThreads)
{
if (!(work_objects[index] = CreateThreadpoolWork(
progressive_process_tiles_tile_work_callback, (void*)¶ms[index],
&progressive->rfx_context->priv->ThreadPoolEnv)))
{
WLog_ERR(TAG, "CreateThreadpoolWork failed.");
status = -1;
break;
}
SubmitThreadpoolWork(work_objects[index]);
close_cnt = index + 1;
}
else
{
progressive_process_tiles_tile_work_callback(0, ¶ms[index], 0);
}
if (status < 0)
{
WLog_Print(progressive->log, WLOG_ERROR, "Failed to decompress %s at %" PRIu16,
rfx_get_progressive_block_type_string(tile->blockType), index);
goto fail;
}
}
if (status < 0)
WLog_Print(progressive->log, WLOG_ERROR,
"Failed to create ThreadpoolWork for tile %" PRIu16, index);
if (progressive->rfx_context->priv->UseThreads)
{
for (UINT32 index = 0; index < close_cnt; index++)
{
WaitForThreadpoolWorkCallbacks(work_objects[index], FALSE);
CloseThreadpoolWork(work_objects[index]);
}
}
fail:
winpr_aligned_free(work_objects);
winpr_aligned_free(params);
if (status < 0)
return -1;
return (SSIZE_T)(end - start);
}
static INLINE SSIZE_T progressive_wb_sync(PROGRESSIVE_CONTEXT* progressive, wStream* s,
UINT16 blockType, UINT32 blockLen)
{
const UINT32 magic = 0xCACCACCA;
const UINT16 version = 0x0100;
PROGRESSIVE_BLOCK_SYNC sync;
sync.blockType = blockType;
sync.blockLen = blockLen;
if (sync.blockLen != 12)
{
WLog_Print(progressive->log, WLOG_ERROR,
"PROGRESSIVE_BLOCK_SYNC::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
sync.blockLen, 12);
return -1005;
}
if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
return -1004;
#if defined(WITH_DEBUG_CODECS)
WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveSync");
#endif
Stream_Read_UINT32(s, sync.magic);
Stream_Read_UINT16(s, sync.version);
if (sync.magic != magic)
{
WLog_Print(progressive->log, WLOG_ERROR,
"PROGRESSIVE_BLOCK_SYNC::magic = 0x%08" PRIx32 " != 0x%08" PRIx32, sync.magic,
magic);
return -1005;
}
if (sync.version != 0x0100)
{
WLog_Print(progressive->log, WLOG_ERROR,
"PROGRESSIVE_BLOCK_SYNC::version = 0x%04" PRIx16 " != 0x%04" PRIu16,
sync.version, version);
return -1006;
}
if ((progressive->state & FLAG_WBT_SYNC) != 0)
WLog_WARN(TAG, "Duplicate PROGRESSIVE_BLOCK_SYNC, ignoring");
progressive->state |= FLAG_WBT_SYNC;
return 0;
}
static INLINE SSIZE_T progressive_wb_frame_begin(PROGRESSIVE_CONTEXT* progressive, wStream* s,
UINT16 blockType, UINT32 blockLen)
{
PROGRESSIVE_BLOCK_FRAME_BEGIN frameBegin;
frameBegin.blockType = blockType;
frameBegin.blockLen = blockLen;
if (frameBegin.blockLen != 12)
{
WLog_Print(progressive->log, WLOG_ERROR,
" RFX_PROGRESSIVE_FRAME_BEGIN::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
frameBegin.blockLen, 12);
return -1005;
}
if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
return -1007;
Stream_Read_UINT32(s, frameBegin.frameIndex);
Stream_Read_UINT16(s, frameBegin.regionCount);
#if defined(WITH_DEBUG_CODECS)
WLog_Print(progressive->log, WLOG_DEBUG,
"ProgressiveFrameBegin: frameIndex: %" PRIu32 " regionCount: %" PRIu16 "",
frameBegin.frameIndex, frameBegin.regionCount);
#endif
/**
* If the number of elements specified by the regionCount field is
* larger than the actual number of elements in the regions field,
* the decoder SHOULD ignore this inconsistency.
*/
if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
{
WLog_ERR(TAG, "Duplicate RFX_PROGRESSIVE_FRAME_BEGIN in stream, this is not allowed!");
return -1008;
}
if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
{
WLog_ERR(TAG, "RFX_PROGRESSIVE_FRAME_BEGIN after RFX_PROGRESSIVE_FRAME_END in stream, this "
"is not allowed!");
return -1008;
}
progressive->state |= FLAG_WBT_FRAME_BEGIN;
return 0;
}
static INLINE SSIZE_T progressive_wb_frame_end(PROGRESSIVE_CONTEXT* progressive, wStream* s,
UINT16 blockType, UINT32 blockLen)
{
PROGRESSIVE_BLOCK_FRAME_END frameEnd;
frameEnd.blockType = blockType;
frameEnd.blockLen = blockLen;
if (frameEnd.blockLen != 6)
{
WLog_Print(progressive->log, WLOG_ERROR,
" RFX_PROGRESSIVE_FRAME_END::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
frameEnd.blockLen, 6);
return -1005;
}
if (Stream_GetRemainingLength(s) != 0)
{
WLog_Print(progressive->log, WLOG_ERROR,
"ProgressiveFrameEnd short %" PRIuz ", expected %" PRIuz,
Stream_GetRemainingLength(s), 0);
return -1008;
}
#if defined(WITH_DEBUG_CODECS)
WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveFrameEnd");
#endif
if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
WLog_WARN(TAG, "RFX_PROGRESSIVE_FRAME_END before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
WLog_WARN(TAG, "Duplicate RFX_PROGRESSIVE_FRAME_END, ignoring");
progressive->state |= FLAG_WBT_FRAME_END;
return 0;
}
static INLINE SSIZE_T progressive_wb_context(PROGRESSIVE_CONTEXT* progressive, wStream* s,
UINT16 blockType, UINT32 blockLen)
{
PROGRESSIVE_BLOCK_CONTEXT* context = &progressive->context;
context->blockType = blockType;
context->blockLen = blockLen;
if (context->blockLen != 10)
{
WLog_Print(progressive->log, WLOG_ERROR,
"RFX_PROGRESSIVE_CONTEXT::blockLen = 0x%08" PRIx32 " != 0x%08" PRIx32,
context->blockLen, 10);
return -1005;
}
if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
return -1009;
Stream_Read_UINT8(s, context->ctxId);
Stream_Read_UINT16(s, context->tileSize);
Stream_Read_UINT8(s, context->flags);
if (context->ctxId != 0x00)
WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT::ctxId != 0x00: %" PRIu8, context->ctxId);
if (context->tileSize != 64)
{
WLog_ERR(TAG, "RFX_PROGRESSIVE_CONTEXT::tileSize != 0x40: %" PRIu16, context->tileSize);
return -1010;
}
if ((progressive->state & FLAG_WBT_FRAME_BEGIN) != 0)
WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_BEGIN");
if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
WLog_WARN(TAG, "RFX_PROGRESSIVE_CONTEXT received after RFX_PROGRESSIVE_FRAME_END");
if ((progressive->state & FLAG_WBT_CONTEXT) != 0)
WLog_WARN(TAG, "Duplicate RFX_PROGRESSIVE_CONTEXT received, ignoring.");
#if defined(WITH_DEBUG_CODECS)
WLog_Print(progressive->log, WLOG_DEBUG, "ProgressiveContext: flags: 0x%02" PRIX8 "",
context->flags);
#endif
progressive->state |= FLAG_WBT_CONTEXT;
return 0;
}
static INLINE SSIZE_T progressive_wb_read_region_header(PROGRESSIVE_CONTEXT* progressive,
wStream* s, UINT16 blockType,
UINT32 blockLen,
PROGRESSIVE_BLOCK_REGION* region)
{
SSIZE_T len = 0;
memset(region, 0, sizeof(PROGRESSIVE_BLOCK_REGION));
if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
return -1011;
region->blockType = blockType;
region->blockLen = blockLen;
Stream_Read_UINT8(s, region->tileSize);
Stream_Read_UINT16(s, region->numRects);
Stream_Read_UINT8(s, region->numQuant);
Stream_Read_UINT8(s, region->numProgQuant);
Stream_Read_UINT8(s, region->flags);
Stream_Read_UINT16(s, region->numTiles);
Stream_Read_UINT32(s, region->tileDataSize);
if (region->tileSize != 64)
{
WLog_Print(progressive->log, WLOG_ERROR,
"ProgressiveRegion tile size %" PRIu8 ", expected %" PRIuz, region->tileSize,
64);
return -1012;
}
if (region->numRects < 1)
{
WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion missing rect count %" PRIu16,
region->numRects);
return -1013;
}
if (region->numQuant > 7)
{
WLog_Print(progressive->log, WLOG_ERROR,
"ProgressiveRegion quant count too high %" PRIu8 ", expected < %" PRIuz,
region->numQuant, 7);
return -1014;
}
len = Stream_GetRemainingLength(s);
if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, region->numRects, 8ull))
{
WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion data short for region->rects");
return -1015;
}
len -= region->numRects * 8ULL;
if (len / 5 < region->numQuant)
{
WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion data short for region->cQuant");
return -1018;
}
len -= region->numQuant * 5ULL;
if (len / 16 < region->numProgQuant)
{
WLog_Print(progressive->log, WLOG_ERROR,
"ProgressiveRegion data short for region->cProgQuant");
return -1021;
}
len -= region->numProgQuant * 16ULL;
if (len < region->tileDataSize * 1ll)
{
WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion data short for region->tiles");
return -1024;
}
len -= region->tileDataSize;
if (len > 0)
WLog_Print(progressive->log, WLOG_WARN,
"Unused bytes detected, %" PRIuz " bytes not processed", len);
return len;
}
static INLINE SSIZE_T progressive_wb_skip_region(PROGRESSIVE_CONTEXT* progressive, wStream* s,
UINT16 blockType, UINT32 blockLen)
{
SSIZE_T rc = 0;
size_t total = 0;
PROGRESSIVE_BLOCK_REGION* region = &progressive->region;
rc = progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
if (rc < 0)
return rc;
total = (region->numRects * 8);
total += (region->numQuant * 5);
total += (region->numProgQuant * 16);
total += region->tileDataSize;
if (!Stream_SafeSeek(s, total))
return -1111;
return rc;
}
static INLINE SSIZE_T progressive_wb_region(PROGRESSIVE_CONTEXT* progressive, wStream* s,
UINT16 blockType, UINT32 blockLen,
PROGRESSIVE_SURFACE_CONTEXT* surface,
PROGRESSIVE_BLOCK_REGION* region)
{
SSIZE_T rc = -1;
UINT16 boxLeft = 0;
UINT16 boxTop = 0;
UINT16 boxRight = 0;
UINT16 boxBottom = 0;
UINT16 idxLeft = 0;
UINT16 idxTop = 0;
UINT16 idxRight = 0;
UINT16 idxBottom = 0;
const PROGRESSIVE_BLOCK_CONTEXT* context = &progressive->context;
if ((progressive->state & FLAG_WBT_FRAME_BEGIN) == 0)
{
WLog_WARN(TAG, "RFX_PROGRESSIVE_REGION before RFX_PROGRESSIVE_FRAME_BEGIN, ignoring");
return progressive_wb_skip_region(progressive, s, blockType, blockLen);
}
if ((progressive->state & FLAG_WBT_FRAME_END) != 0)
{
WLog_WARN(TAG, "RFX_PROGRESSIVE_REGION after RFX_PROGRESSIVE_FRAME_END, ignoring");
return progressive_wb_skip_region(progressive, s, blockType, blockLen);
}
progressive->state |= FLAG_WBT_REGION;
rc = progressive_wb_read_region_header(progressive, s, blockType, blockLen, region);
if (rc < 0)
return rc;
for (UINT16 index = 0; index < region->numRects; index++)
{
RFX_RECT* rect = &(region->rects[index]);
Stream_Read_UINT16(s, rect->x);
Stream_Read_UINT16(s, rect->y);
Stream_Read_UINT16(s, rect->width);
Stream_Read_UINT16(s, rect->height);
}
for (BYTE index = 0; index < region->numQuant; index++)
{
RFX_COMPONENT_CODEC_QUANT* quantVal = &(region->quantVals[index]);
progressive_component_codec_quant_read(s, quantVal);
if (!progressive_rfx_quant_lcmp_greater_equal(quantVal, 6))
{
WLog_Print(progressive->log, WLOG_ERROR,
"ProgressiveRegion region->cQuant[%" PRIu32 "] < 6", index);
return -1;
}
if (!progressive_rfx_quant_lcmp_less_equal(quantVal, 15))
{
WLog_Print(progressive->log, WLOG_ERROR,
"ProgressiveRegion region->cQuant[%" PRIu32 "] > 15", index);
return -1;
}
}
for (BYTE index = 0; index < region->numProgQuant; index++)
{
RFX_PROGRESSIVE_CODEC_QUANT* quantProgVal = &(region->quantProgVals[index]);
Stream_Read_UINT8(s, quantProgVal->quality);
progressive_component_codec_quant_read(s, &(quantProgVal->yQuantValues));
progressive_component_codec_quant_read(s, &(quantProgVal->cbQuantValues));
progressive_component_codec_quant_read(s, &(quantProgVal->crQuantValues));
}
#if defined(WITH_DEBUG_CODECS)
WLog_Print(progressive->log, WLOG_DEBUG,
"ProgressiveRegion: numRects: %" PRIu16 " numTiles: %" PRIu16
" tileDataSize: %" PRIu32 " flags: 0x%02" PRIX8 " numQuant: %" PRIu8
" numProgQuant: %" PRIu8 "",
region->numRects, region->numTiles, region->tileDataSize, region->flags,
region->numQuant, region->numProgQuant);
#endif
boxLeft = surface->gridWidth;
boxTop = surface->gridHeight;
boxRight = 0;
boxBottom = 0;
for (UINT16 index = 0; index < region->numRects; index++)
{
RFX_RECT* rect = &(region->rects[index]);
idxLeft = rect->x / 64;
idxTop = rect->y / 64;
idxRight = (rect->x + rect->width + 63) / 64;
idxBottom = (rect->y + rect->height + 63) / 64;
if (idxLeft < boxLeft)
boxLeft = idxLeft;
if (idxTop < boxTop)
boxTop = idxTop;
if (idxRight > boxRight)
boxRight = idxRight;
if (idxBottom > boxBottom)
boxBottom = idxBottom;
#if defined(WITH_DEBUG_CODECS)
WLog_Print(progressive->log, WLOG_DEBUG,
"rect[%" PRIu16 "]: x: %" PRIu16 " y: %" PRIu16 " w: %" PRIu16 " h: %" PRIu16 "",
index, rect->x, rect->y, rect->width, rect->height);
#endif
}
const SSIZE_T res = progressive_process_tiles(progressive, s, region, surface, context);
if (res < 0)
return -1;
return (size_t)rc;
}
static SSIZE_T progressive_parse_block(PROGRESSIVE_CONTEXT* progressive, wStream* s,
PROGRESSIVE_SURFACE_CONTEXT* surface,
PROGRESSIVE_BLOCK_REGION* region)
{
UINT16 blockType = 0;
UINT32 blockLen = 0;
SSIZE_T rc = -1;
wStream sub = { 0 };
WINPR_ASSERT(progressive);
if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
return -1;
Stream_Read_UINT16(s, blockType);
Stream_Read_UINT32(s, blockLen);
if (blockLen < 6)
{
WLog_WARN(TAG, "Invalid blockLen %" PRIu32 ", expected >= 6", blockLen);
return -1;
}
if (!Stream_CheckAndLogRequiredLength(TAG, s, blockLen - 6))
return -1;
Stream_StaticConstInit(&sub, Stream_Pointer(s), blockLen - 6);
Stream_Seek(s, blockLen - 6);
switch (blockType)
{
case PROGRESSIVE_WBT_SYNC:
rc = progressive_wb_sync(progressive, &sub, blockType, blockLen);
break;
case PROGRESSIVE_WBT_FRAME_BEGIN:
rc = progressive_wb_frame_begin(progressive, &sub, blockType, blockLen);
break;
case PROGRESSIVE_WBT_FRAME_END:
rc = progressive_wb_frame_end(progressive, &sub, blockType, blockLen);
break;
case PROGRESSIVE_WBT_CONTEXT:
rc = progressive_wb_context(progressive, &sub, blockType, blockLen);
break;
case PROGRESSIVE_WBT_REGION:
rc = progressive_wb_region(progressive, &sub, blockType, blockLen, surface, region);
break;
default:
WLog_Print(progressive->log, WLOG_ERROR, "Invalid block type %04" PRIx16, blockType);
return -1;
}
if (rc < 0)
return -1;
if (Stream_GetRemainingLength(&sub) > 0)
{
WLog_Print(progressive->log, WLOG_ERROR,
"block len %" PRIu32 " does not match read data %" PRIuz, blockLen,
blockLen - Stream_GetRemainingLength(&sub));
return -1;
}
return rc;
}
static BOOL update_tiles(PROGRESSIVE_CONTEXT* progressive, PROGRESSIVE_SURFACE_CONTEXT* surface,
BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
UINT32 nYDst, PROGRESSIVE_BLOCK_REGION* region, REGION16* invalidRegion)
{
BOOL rc = TRUE;
REGION16 clippingRects = { 0 };
region16_init(&clippingRects);
for (UINT32 i = 0; i < region->numRects; i++)
{
RECTANGLE_16 clippingRect = { 0 };
const RFX_RECT* rect = &(region->rects[i]);
clippingRect.left = (UINT16)nXDst + rect->x;
clippingRect.top = (UINT16)nYDst + rect->y;
clippingRect.right = clippingRect.left + rect->width;
clippingRect.bottom = clippingRect.top + rect->height;
region16_union_rect(&clippingRects, &clippingRects, &clippingRect);
}
for (UINT32 i = 0; i < surface->numUpdatedTiles; i++)
{
UINT32 nbUpdateRects = 0;
const RECTANGLE_16* updateRects = NULL;
RECTANGLE_16 updateRect = { 0 };
WINPR_ASSERT(surface->updatedTileIndices);
const UINT32 index = surface->updatedTileIndices[i];
WINPR_ASSERT(index < surface->tilesSize);
RFX_PROGRESSIVE_TILE* tile = surface->tiles[index];
WINPR_ASSERT(tile);
updateRect.left = nXDst + tile->x;
updateRect.top = nYDst + tile->y;
updateRect.right = updateRect.left + 64;
updateRect.bottom = updateRect.top + 64;
REGION16 updateRegion = { 0 };
region16_init(&updateRegion);
region16_intersect_rect(&updateRegion, &clippingRects, &updateRect);
updateRects = region16_rects(&updateRegion, &nbUpdateRects);
for (UINT32 j = 0; j < nbUpdateRects; j++)
{
const RECTANGLE_16* rect = &updateRects[j];
if (rect->left < updateRect.left)
goto fail;
const UINT32 nXSrc = rect->left - updateRect.left;
const UINT32 nYSrc = rect->top - updateRect.top;
const UINT32 width = rect->right - rect->left;
const UINT32 height = rect->bottom - rect->top;
if (rect->left + width > surface->width)
goto fail;
if (rect->top + height > surface->height)
goto fail;
rc = freerdp_image_copy(pDstData, DstFormat, nDstStep, rect->left, rect->top, width,
height, tile->data, progressive->format, tile->stride, nXSrc,
nYSrc, NULL, FREERDP_KEEP_DST_ALPHA);
if (!rc)
break;
if (invalidRegion)
region16_union_rect(invalidRegion, invalidRegion, rect);
}
region16_uninit(&updateRegion);
tile->dirty = FALSE;
}
fail:
region16_uninit(&clippingRects);
return rc;
}
INT32 progressive_decompress(PROGRESSIVE_CONTEXT* progressive, const BYTE* pSrcData, UINT32 SrcSize,
BYTE* pDstData, UINT32 DstFormat, UINT32 nDstStep, UINT32 nXDst,
UINT32 nYDst, REGION16* invalidRegion, UINT16 surfaceId,
UINT32 frameId)
{
INT32 rc = 1;
WINPR_ASSERT(progressive);
PROGRESSIVE_SURFACE_CONTEXT* surface = progressive_get_surface_data(progressive, surfaceId);
if (!surface)
{
WLog_Print(progressive->log, WLOG_ERROR, "ProgressiveRegion no surface for %" PRIu16,
surfaceId);
return -1001;
}
PROGRESSIVE_BLOCK_REGION* region = &progressive->region;
WINPR_ASSERT(region);
if (surface->frameId != frameId)
{
surface->frameId = frameId;
surface->numUpdatedTiles = 0;
}
wStream ss = { 0 };
wStream* s = Stream_StaticConstInit(&ss, pSrcData, SrcSize);
WINPR_ASSERT(s);
switch (DstFormat)
{
case PIXEL_FORMAT_RGBA32:
case PIXEL_FORMAT_RGBX32:
case PIXEL_FORMAT_BGRA32:
case PIXEL_FORMAT_BGRX32:
progressive->format = DstFormat;
break;
default:
progressive->format = PIXEL_FORMAT_XRGB32;
break;
}
const size_t start = Stream_GetPosition(s);
progressive->state = 0; /* Set state to not initialized */
while (Stream_GetRemainingLength(s) > 0)
{
if (progressive_parse_block(progressive, s, surface, region) < 0)
goto fail;
}
const size_t end = Stream_GetPosition(s);
if ((end - start) != SrcSize)
{
WLog_Print(progressive->log, WLOG_ERROR,
"total block len %" PRIuz " does not match read data %" PRIu32, end - start,
SrcSize);
rc = -1041;
goto fail;
}
if (!update_tiles(progressive, surface, pDstData, DstFormat, nDstStep, nXDst, nYDst, region,
invalidRegion))
return -2002;
fail:
return rc;
}
BOOL progressive_rfx_write_message_progressive_simple(PROGRESSIVE_CONTEXT* progressive, wStream* s,
const RFX_MESSAGE* msg)
{
RFX_CONTEXT* context = NULL;
WINPR_ASSERT(progressive);
WINPR_ASSERT(s);
WINPR_ASSERT(msg);
context = progressive->rfx_context;
return rfx_write_message_progressive_simple(context, s, msg);
}
int progressive_compress(PROGRESSIVE_CONTEXT* progressive, const BYTE* pSrcData, UINT32 SrcSize,
UINT32 SrcFormat, UINT32 Width, UINT32 Height, UINT32 ScanLine,
const REGION16* invalidRegion, BYTE** ppDstData, UINT32* pDstSize)
{
BOOL rc = FALSE;
int res = -6;
wStream* s = NULL;
UINT32 numRects = 0;
UINT32 x = 0;
UINT32 y = 0;
RFX_RECT* rects = NULL;
RFX_MESSAGE* message = NULL;
if (!progressive || !pSrcData || !ppDstData || !pDstSize)
{
return -1;
}
if (ScanLine == 0)
{
switch (SrcFormat)
{
case PIXEL_FORMAT_ABGR32:
case PIXEL_FORMAT_ARGB32:
case PIXEL_FORMAT_XBGR32:
case PIXEL_FORMAT_XRGB32:
case PIXEL_FORMAT_BGRA32:
case PIXEL_FORMAT_BGRX32:
case PIXEL_FORMAT_RGBA32:
case PIXEL_FORMAT_RGBX32:
ScanLine = Width * 4;
break;
default:
return -2;
}
}
if (SrcSize < Height * ScanLine)
return -4;
if (!invalidRegion)
{
numRects = (Width + 63) / 64;
numRects *= (Height + 63) / 64;
}
else
numRects = region16_n_rects(invalidRegion);
if (numRects == 0)
return 0;
if (!Stream_EnsureCapacity(progressive->rects, numRects * sizeof(RFX_RECT)))
return -5;
rects = (RFX_RECT*)Stream_Buffer(progressive->rects);
if (invalidRegion)
{
const RECTANGLE_16* region_rects = region16_rects(invalidRegion, NULL);
for (UINT32 x = 0; x < numRects; x++)
{
const RECTANGLE_16* r = ®ion_rects[x];
RFX_RECT* rect = &rects[x];
rect->x = r->left;
rect->y = r->top;
rect->width = r->right - r->left;
rect->height = r->bottom - r->top;
}
}
else
{
x = 0;
y = 0;
for (UINT32 i = 0; i < numRects; i++)
{
RFX_RECT* r = &rects[i];
r->x = x;
r->y = y;
r->width = MIN(64, Width - x);
r->height = MIN(64, Height - y);
if (x + 64 >= Width)
{
y += 64;
x = 0;
}
else
x += 64;
WINPR_ASSERT(r->x % 64 == 0);
WINPR_ASSERT(r->y % 64 == 0);
WINPR_ASSERT(r->width <= 64);
WINPR_ASSERT(r->height <= 64);
}
}
s = progressive->buffer;
Stream_SetPosition(s, 0);
progressive->rfx_context->mode = RLGR1;
progressive->rfx_context->width = Width;
progressive->rfx_context->height = Height;
rfx_context_set_pixel_format(progressive->rfx_context, SrcFormat);
message = rfx_encode_message(progressive->rfx_context, rects, numRects, pSrcData, Width, Height,
ScanLine);
if (!message)
{
WLog_ERR(TAG, "failed to encode rfx message");
goto fail;
}
rc = progressive_rfx_write_message_progressive_simple(progressive, s, message);
rfx_message_free(progressive->rfx_context, message);
if (!rc)
goto fail;
const size_t pos = Stream_GetPosition(s);
WINPR_ASSERT(pos <= UINT32_MAX);
*pDstSize = (UINT32)pos;
*ppDstData = Stream_Buffer(s);
res = 1;
fail:
return res;
}
BOOL progressive_context_reset(PROGRESSIVE_CONTEXT* progressive)
{
if (!progressive)
return FALSE;
return TRUE;
}
PROGRESSIVE_CONTEXT* progressive_context_new(BOOL Compressor)
{
return progressive_context_new_ex(Compressor, 0);
}
PROGRESSIVE_CONTEXT* progressive_context_new_ex(BOOL Compressor, UINT32 ThreadingFlags)
{
PROGRESSIVE_CONTEXT* progressive =
(PROGRESSIVE_CONTEXT*)winpr_aligned_calloc(1, sizeof(PROGRESSIVE_CONTEXT), 32);
if (!progressive)
return NULL;
progressive->Compressor = Compressor;
progressive->quantProgValFull.quality = 100;
progressive->log = WLog_Get(TAG);
if (!progressive->log)
goto fail;
progressive->rfx_context = rfx_context_new_ex(Compressor, ThreadingFlags);
if (!progressive->rfx_context)
goto fail;
progressive->buffer = Stream_New(NULL, 1024);
if (!progressive->buffer)
goto fail;
progressive->rects = Stream_New(NULL, 1024);
if (!progressive->rects)
goto fail;
progressive->bufferPool = BufferPool_New(TRUE, (8192 + 32) * 3, 16);
if (!progressive->bufferPool)
goto fail;
progressive->SurfaceContexts = HashTable_New(TRUE);
if (!progressive->SurfaceContexts)
goto fail;
{
wObject* obj = HashTable_ValueObject(progressive->SurfaceContexts);
WINPR_ASSERT(obj);
obj->fnObjectFree = progressive_surface_context_free;
}
return progressive;
fail:
WINPR_PRAGMA_DIAG_PUSH
WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
progressive_context_free(progressive);
WINPR_PRAGMA_DIAG_POP
return NULL;
}
void progressive_context_free(PROGRESSIVE_CONTEXT* progressive)
{
if (!progressive)
return;
Stream_Free(progressive->buffer, TRUE);
Stream_Free(progressive->rects, TRUE);
rfx_context_free(progressive->rfx_context);
BufferPool_Free(progressive->bufferPool);
HashTable_Free(progressive->SurfaceContexts);
winpr_aligned_free(progressive);
}
|