Coverage Report

Created: 2026-08-17 07:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/LibRaw/src/decoders/sony_arw6.cpp
Line
Count
Source
1
/* -*- C++ -*-
2
 * Copyright 2026 LibRaw LLC (info@libraw.org)
3
 * Copyright (C) 2026 Jiang Yaogeng (Sony ARW6 CRAW HQ decoder;
4
 *   code generated with assistance from GLM-5)
5
 *
6
 LibRaw is free software; you can redistribute it and/or modify
7
 it under the terms of the one of two licenses as you choose:
8
9
1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
10
   (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
11
12
2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
13
   (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
14
15
 */
16
17
#include "../../internal/dcraw_defs.h"
18
19
#include <algorithm>
20
#include <stdint.h>
21
#include <string.h>
22
#include <vector>
23
24
namespace
25
{
26
const int SONY_ARW6_STREAM_OFFSET = 0x200;
27
const int SONY_ARW6_INTERNAL_BIAS = 2048;
28
const size_t SONY_ARW6_MAX_PLANE_SAMPLES = size_t(128) * 1024 * 1024;
29
const INT64 SONY_ARW6_WORKING_BYTES_PER_TILE_PIXEL = 24;
30
31
static inline int sony_arw6_align_up(int value, int multiple)
32
0
{
33
0
  return ((value + multiple - 1) / multiple) * multiple;
34
0
}
35
36
static inline INT64 sony_arw6_memory_limit_bytes(unsigned max_raw_memory_mb)
37
0
{
38
0
  return INT64(max_raw_memory_mb) * INT64(1024 * 1024);
39
0
}
40
41
static inline uint16_t sony_arw6_be16(const uchar *p)
42
0
{
43
0
  return uint16_t((uint16_t(p[0]) << 8) | p[1]);
44
0
}
45
46
static inline uint32_t sony_arw6_be32(const uchar *p)
47
0
{
48
0
  return (uint32_t(p[0]) << 24) | (uint32_t(p[1]) << 16) |
49
0
         (uint32_t(p[2]) << 8) | uint32_t(p[3]);
50
0
}
51
52
static inline uint32_t sony_arw6_le32(const uchar *p)
53
0
{
54
0
  return (uint32_t(p[3]) << 24) | (uint32_t(p[2]) << 16) |
55
0
         (uint32_t(p[1]) << 8) | uint32_t(p[0]);
56
0
}
57
58
static inline void sony_arw6_require(bool cond)
59
0
{
60
0
  if (!cond)
61
0
    throw LIBRAW_EXCEPTION_IO_CORRUPT;
62
0
}
63
64
static inline int32_t sony_arw6_floor_shift(int64_t x, int bits)
65
0
{
66
0
  if (bits <= 0)
67
0
    return int32_t(x);
68
0
  const int64_t d = int64_t(1) << bits;
69
0
  return int32_t(x >= 0 ? x >> bits : -(((-x) + d - 1) >> bits));
70
0
}
71
72
static inline int32_t sony_arw6_sign16(int32_t x)
73
0
{
74
0
  x &= 0xffff;
75
0
  return x >= 0x8000 ? x - 0x10000 : x;
76
0
}
77
78
static inline int32_t sony_arw6_edge_detail(int32_t x, bool odd_mode)
79
0
{
80
0
  const int32_t signed_half_step = x > 0 ? 1 : (x < 0 ? -1 : 0);
81
0
  const bool is_odd = (x % 2) != 0;
82
0
  const bool use_half_step =
83
0
      odd_mode ? is_odd : (x != 0 && !is_odd);
84
0
  return 2 * x + (use_half_step ? signed_half_step : 0);
85
0
}
86
87
static const ushort sony_arw6_llvc3_curve[4096] = {
88
  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
89
  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
90
  32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
91
  48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
92
  64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
93
  80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
94
  96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
95
  112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
96
  128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
97
  144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
98
  160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
99
  176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
100
  192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
101
  208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
102
  224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
103
  240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
104
  256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271,
105
  272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287,
106
  288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303,
107
  304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319,
108
  320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335,
109
  336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351,
110
  352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367,
111
  368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383,
112
  384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399,
113
  400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415,
114
  416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431,
115
  432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447,
116
  448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463,
117
  464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479,
118
  480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495,
119
  496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511,
120
  512, 513, 514, 515, 516, 517, 518, 519, 520, 521, 522, 523, 524, 525, 526, 527,
121
  528, 529, 530, 531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543,
122
  544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559,
123
  560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572, 573, 574, 575,
124
  576, 577, 578, 579, 580, 581, 582, 583, 584, 585, 586, 587, 588, 589, 590, 591,
125
  592, 593, 594, 595, 596, 597, 598, 599, 600, 601, 602, 603, 604, 605, 606, 607,
126
  608, 609, 610, 611, 612, 613, 614, 615, 616, 617, 618, 619, 620, 621, 622, 623,
127
  624, 625, 626, 627, 628, 629, 630, 631, 632, 633, 634, 635, 636, 637, 638, 639,
128
  640, 641, 642, 643, 644, 645, 646, 647, 648, 649, 650, 651, 652, 653, 654, 655,
129
  656, 657, 658, 659, 660, 661, 662, 663, 664, 665, 666, 667, 668, 669, 670, 671,
130
  672, 673, 674, 675, 676, 677, 678, 679, 680, 681, 682, 683, 684, 685, 686, 687,
131
  688, 689, 690, 691, 692, 693, 694, 695, 696, 697, 698, 699, 700, 701, 702, 703,
132
  704, 705, 706, 707, 708, 709, 710, 711, 712, 713, 714, 715, 716, 717, 718, 719,
133
  720, 721, 722, 723, 724, 725, 726, 727, 728, 729, 730, 731, 732, 733, 734, 735,
134
  736, 737, 738, 739, 740, 741, 742, 743, 744, 745, 746, 747, 748, 749, 750, 751,
135
  752, 753, 754, 755, 756, 757, 758, 759, 760, 761, 762, 763, 764, 765, 766, 767,
136
  768, 769, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 780, 781, 782, 783,
137
  784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799,
138
  800, 801, 802, 803, 804, 805, 806, 807, 808, 809, 810, 811, 812, 813, 814, 815,
139
  816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827, 828, 829, 830, 831,
140
  832, 833, 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846, 847,
141
  848, 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859, 860, 861, 862, 863,
142
  864, 865, 866, 867, 868, 869, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879,
143
  880, 881, 882, 883, 884, 885, 886, 887, 888, 889, 890, 891, 892, 893, 894, 895,
144
  896, 897, 898, 899, 900, 901, 902, 903, 904, 905, 906, 907, 908, 909, 910, 911,
145
  912, 913, 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927,
146
  928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943,
147
  944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959,
148
  960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975,
149
  976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988, 989, 990, 991,
150
  992, 993, 994, 995, 996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007,
151
  1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015, 1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
152
  1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031, 1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039,
153
  1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047, 1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055,
154
  1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063, 1064, 1065, 1066, 1067, 1068, 1069, 1070, 1071,
155
  1072, 1073, 1074, 1075, 1076, 1077, 1078, 1079, 1080, 1081, 1082, 1083, 1084, 1085, 1086, 1087,
156
  1088, 1089, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098, 1099, 1100, 1101, 1102, 1103,
157
  1104, 1105, 1106, 1107, 1108, 1109, 1110, 1111, 1112, 1113, 1114, 1115, 1116, 1117, 1118, 1119,
158
  1120, 1121, 1122, 1123, 1124, 1125, 1126, 1127, 1128, 1129, 1130, 1131, 1132, 1133, 1134, 1135,
159
  1136, 1137, 1138, 1139, 1140, 1141, 1142, 1143, 1144, 1145, 1146, 1147, 1148, 1149, 1150, 1151,
160
  1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 1162, 1163, 1164, 1165, 1166, 1167,
161
  1168, 1169, 1170, 1171, 1172, 1173, 1174, 1175, 1176, 1177, 1178, 1179, 1180, 1181, 1182, 1183,
162
  1184, 1185, 1186, 1187, 1188, 1189, 1190, 1191, 1192, 1193, 1194, 1195, 1196, 1197, 1198, 1199,
163
  1200, 1201, 1202, 1203, 1204, 1205, 1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213, 1214, 1215,
164
  1216, 1217, 1218, 1219, 1220, 1221, 1222, 1223, 1224, 1225, 1226, 1227, 1228, 1229, 1230, 1231,
165
  1232, 1233, 1234, 1235, 1236, 1237, 1238, 1239, 1240, 1241, 1242, 1243, 1244, 1245, 1246, 1247,
166
  1248, 1249, 1250, 1251, 1252, 1253, 1254, 1255, 1256, 1257, 1258, 1259, 1260, 1261, 1262, 1263,
167
  1264, 1265, 1266, 1267, 1268, 1269, 1270, 1271, 1272, 1273, 1274, 1275, 1276, 1277, 1278, 1279,
168
  1280, 1281, 1282, 1283, 1284, 1285, 1286, 1287, 1288, 1289, 1290, 1291, 1292, 1293, 1294, 1295,
169
  1296, 1297, 1298, 1299, 1300, 1301, 1302, 1303, 1304, 1305, 1306, 1307, 1308, 1309, 1310, 1311,
170
  1312, 1313, 1314, 1315, 1316, 1317, 1318, 1319, 1320, 1321, 1322, 1323, 1324, 1325, 1326, 1327,
171
  1328, 1329, 1330, 1331, 1332, 1333, 1334, 1335, 1336, 1337, 1338, 1339, 1340, 1341, 1342, 1343,
172
  1344, 1345, 1346, 1347, 1348, 1349, 1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359,
173
  1360, 1361, 1362, 1363, 1364, 1365, 1366, 1367, 1368, 1369, 1370, 1371, 1372, 1373, 1374, 1375,
174
  1376, 1377, 1378, 1379, 1380, 1381, 1382, 1383, 1384, 1385, 1386, 1387, 1388, 1389, 1390, 1391,
175
  1392, 1393, 1394, 1395, 1396, 1397, 1398, 1399, 1400, 1401, 1402, 1403, 1404, 1405, 1406, 1407,
176
  1408, 1409, 1410, 1411, 1412, 1413, 1414, 1415, 1416, 1417, 1418, 1419, 1420, 1421, 1422, 1423,
177
  1424, 1425, 1426, 1427, 1429, 1430, 1431, 1432, 1433, 1434, 1435, 1436, 1438, 1439, 1440, 1441,
178
  1442, 1443, 1444, 1445, 1446, 1447, 1448, 1449, 1450, 1451, 1452, 1453, 1455, 1456, 1457, 1458,
179
  1459, 1460, 1461, 1462, 1463, 1464, 1465, 1466, 1467, 1468, 1469, 1470, 1471, 1472, 1473, 1474,
180
  1475, 1476, 1477, 1478, 1480, 1481, 1482, 1483, 1484, 1485, 1486, 1487, 1489, 1490, 1491, 1492,
181
  1493, 1494, 1495, 1496, 1497, 1498, 1499, 1500, 1501, 1502, 1503, 1504, 1505, 1506, 1507, 1508,
182
  1509, 1510, 1511, 1512, 1513, 1514, 1515, 1516, 1517, 1518, 1519, 1520, 1522, 1523, 1524, 1525,
183
  1526, 1527, 1529, 1530, 1531, 1532, 1534, 1535, 1536, 1537, 1538, 1539, 1541, 1542, 1543, 1544,
184
  1545, 1546, 1548, 1549, 1550, 1551, 1553, 1554, 1555, 1556, 1558, 1559, 1561, 1562, 1563, 1565,
185
  1566, 1567, 1568, 1569, 1571, 1572, 1573, 1574, 1575, 1576, 1578, 1579, 1580, 1581, 1583, 1584,
186
  1585, 1586, 1588, 1589, 1590, 1591, 1593, 1594, 1595, 1596, 1598, 1599, 1601, 1602, 1603, 1605,
187
  1606, 1607, 1608, 1609, 1611, 1612, 1613, 1614, 1615, 1616, 1618, 1619, 1621, 1622, 1623, 1625,
188
  1626, 1627, 1629, 1630, 1631, 1632, 1634, 1635, 1636, 1637, 1639, 1640, 1641, 1642, 1644, 1645,
189
  1646, 1647, 1649, 1650, 1651, 1652, 1654, 1655, 1656, 1657, 1659, 1660, 1662, 1663, 1664, 1666,
190
  1667, 1668, 1670, 1671, 1673, 1674, 1675, 1677, 1678, 1680, 1681, 1683, 1685, 1686, 1688, 1689,
191
  1691, 1692, 1694, 1695, 1697, 1698, 1699, 1701, 1702, 1704, 1705, 1707, 1709, 1710, 1712, 1713,
192
  1715, 1716, 1718, 1719, 1720, 1721, 1723, 1724, 1725, 1727, 1728, 1730, 1731, 1733, 1734, 1736,
193
  1737, 1739, 1740, 1742, 1743, 1745, 1746, 1748, 1749, 1750, 1752, 1753, 1755, 1756, 1757, 1759,
194
  1760, 1762, 1763, 1765, 1767, 1768, 1770, 1771, 1773, 1774, 1776, 1777, 1779, 1780, 1781, 1783,
195
  1784, 1786, 1787, 1789, 1791, 1792, 1794, 1795, 1797, 1799, 1801, 1802, 1804, 1806, 1808, 1809,
196
  1811, 1813, 1814, 1816, 1818, 1819, 1821, 1822, 1824, 1826, 1827, 1829, 1831, 1832, 1834, 1835,
197
  1837, 1839, 1841, 1842, 1844, 1846, 1848, 1849, 1851, 1853, 1854, 1856, 1858, 1859, 1861, 1862,
198
  1864, 1866, 1868, 1869, 1871, 1873, 1875, 1876, 1878, 1880, 1881, 1883, 1885, 1886, 1888, 1889,
199
  1891, 1893, 1894, 1896, 1898, 1899, 1901, 1902, 1904, 1906, 1908, 1909, 1911, 1913, 1915, 1916,
200
  1918, 1920, 1922, 1924, 1926, 1927, 1929, 1931, 1933, 1935, 1937, 1938, 1940, 1942, 1944, 1945,
201
  1947, 1949, 1951, 1953, 1955, 1957, 1959, 1961, 1963, 1965, 1967, 1968, 1970, 1972, 1974, 1975,
202
  1977, 1979, 1981, 1983, 1985, 1986, 1988, 1990, 1992, 1994, 1996, 1998, 2000, 2001, 2003, 2005,
203
  2007, 2009, 2011, 2013, 2015, 2016, 2018, 2020, 2022, 2024, 2026, 2027, 2029, 2031, 2033, 2034,
204
  2036, 2038, 2040, 2042, 2044, 2046, 2048, 2050, 2052, 2054, 2056, 2058, 2060, 2062, 2064, 2066,
205
  2068, 2070, 2072, 2074, 2076, 2078, 2080, 2082, 2084, 2086, 2088, 2090, 2092, 2094, 2096, 2098,
206
  2100, 2102, 2104, 2106, 2109, 2111, 2113, 2115, 2117, 2119, 2121, 2123, 2125, 2127, 2129, 2131,
207
  2133, 2135, 2137, 2139, 2141, 2143, 2145, 2147, 2149, 2151, 2153, 2155, 2157, 2159, 2161, 2163,
208
  2165, 2167, 2170, 2172, 2175, 2177, 2179, 2182, 2184, 2186, 2188, 2190, 2193, 2195, 2197, 2199,
209
  2201, 2203, 2206, 2208, 2210, 2212, 2215, 2217, 2219, 2221, 2224, 2226, 2228, 2230, 2233, 2235,
210
  2237, 2239, 2242, 2244, 2247, 2249, 2251, 2254, 2256, 2258, 2261, 2263, 2265, 2267, 2270, 2272,
211
  2274, 2276, 2279, 2281, 2283, 2285, 2288, 2290, 2292, 2294, 2297, 2299, 2302, 2304, 2306, 2309,
212
  2311, 2314, 2316, 2319, 2321, 2324, 2326, 2329, 2331, 2333, 2336, 2338, 2340, 2342, 2345, 2347,
213
  2349, 2352, 2354, 2357, 2359, 2362, 2364, 2367, 2369, 2371, 2374, 2376, 2379, 2381, 2383, 2386,
214
  2388, 2390, 2393, 2395, 2398, 2400, 2402, 2405, 2407, 2410, 2412, 2415, 2417, 2420, 2422, 2425,
215
  2427, 2430, 2432, 2435, 2438, 2440, 2443, 2445, 2448, 2451, 2453, 2456, 2459, 2461, 2464, 2466,
216
  2469, 2472, 2475, 2477, 2480, 2483, 2486, 2488, 2491, 2494, 2496, 2499, 2501, 2504, 2506, 2509,
217
  2511, 2514, 2517, 2519, 2522, 2525, 2528, 2530, 2533, 2536, 2538, 2541, 2543, 2546, 2548, 2551,
218
  2553, 2556, 2559, 2561, 2564, 2567, 2570, 2572, 2575, 2578, 2581, 2584, 2587, 2589, 2592, 2595,
219
  2598, 2601, 2604, 2606, 2609, 2612, 2615, 2617, 2620, 2623, 2626, 2629, 2632, 2634, 2637, 2640,
220
  2643, 2646, 2649, 2652, 2655, 2657, 2660, 2663, 2666, 2669, 2672, 2675, 2678, 2680, 2683, 2686,
221
  2689, 2692, 2695, 2698, 2701, 2704, 2707, 2710, 2713, 2716, 2719, 2722, 2725, 2727, 2730, 2733,
222
  2736, 2739, 2742, 2745, 2749, 2752, 2755, 2758, 2761, 2764, 2767, 2770, 2773, 2776, 2779, 2782,
223
  2785, 2788, 2791, 2794, 2798, 2801, 2804, 2807, 2810, 2813, 2816, 2819, 2823, 2826, 2829, 2832,
224
  2835, 2838, 2841, 2844, 2848, 2851, 2854, 2857, 2860, 2863, 2867, 2870, 2873, 2876, 2880, 2883,
225
  2886, 2889, 2893, 2896, 2899, 2902, 2906, 2909, 2912, 2915, 2919, 2922, 2925, 2928, 2932, 2935,
226
  2938, 2941, 2945, 2948, 2952, 2955, 2958, 2962, 2965, 2968, 2972, 2975, 2979, 2982, 2985, 2989,
227
  2992, 2995, 2999, 3002, 3006, 3009, 3012, 3016, 3019, 3023, 3026, 3030, 3033, 3037, 3040, 3044,
228
  3047, 3051, 3054, 3058, 3061, 3065, 3068, 3072, 3075, 3079, 3082, 3086, 3089, 3093, 3096, 3100,
229
  3103, 3107, 3110, 3114, 3118, 3121, 3125, 3128, 3132, 3136, 3139, 3143, 3146, 3150, 3153, 3157,
230
  3160, 3164, 3168, 3171, 3175, 3179, 3183, 3186, 3190, 3194, 3197, 3201, 3205, 3208, 3212, 3215,
231
  3219, 3223, 3227, 3231, 3235, 3238, 3242, 3246, 3250, 3254, 3258, 3261, 3265, 3269, 3273, 3276,
232
  3280, 3284, 3288, 3292, 3296, 3299, 3303, 3307, 3311, 3315, 3319, 3323, 3327, 3331, 3335, 3339,
233
  3343, 3347, 3351, 3354, 3358, 3362, 3366, 3369, 3373, 3377, 3381, 3385, 3389, 3393, 3397, 3401,
234
  3405, 3409, 3413, 3417, 3422, 3426, 3430, 3434, 3438, 3442, 3446, 3450, 3455, 3459, 3463, 3467,
235
  3471, 3475, 3479, 3483, 3487, 3491, 3495, 3499, 3503, 3507, 3512, 3516, 3520, 3524, 3529, 3533,
236
  3537, 3541, 3546, 3550, 3554, 3558, 3563, 3567, 3571, 3575, 3580, 3584, 3589, 3593, 3597, 3602,
237
  3606, 3610, 3615, 3619, 3624, 3628, 3632, 3637, 3641, 3645, 3650, 3654, 3658, 3662, 3667, 3671,
238
  3675, 3680, 3684, 3689, 3693, 3698, 3702, 3707, 3711, 3716, 3720, 3725, 3729, 3734, 3738, 3743,
239
  3747, 3752, 3756, 3761, 3766, 3770, 3775, 3779, 3784, 3789, 3793, 3798, 3802, 3807, 3811, 3816,
240
  3820, 3825, 3830, 3834, 3839, 3844, 3849, 3853, 3858, 3863, 3868, 3872, 3877, 3882, 3887, 3891,
241
  3896, 3901, 3906, 3910, 3915, 3920, 3925, 3929, 3934, 3939, 3944, 3949, 3954, 3958, 3963, 3968,
242
  3973, 3978, 3983, 3988, 3993, 3997, 4002, 4007, 4012, 4017, 4022, 4027, 4032, 4037, 4042, 4047,
243
  4052, 4057, 4062, 4067, 4072, 4077, 4082, 4087, 4092, 4097, 4102, 4107, 4113, 4118, 4123, 4128,
244
  4133, 4138, 4143, 4148, 4154, 4159, 4164, 4169, 4174, 4179, 4185, 4190, 4195, 4200, 4206, 4211,
245
  4216, 4221, 4227, 4232, 4237, 4242, 4248, 4253, 4258, 4263, 4269, 4274, 4280, 4285, 4290, 4296,
246
  4301, 4306, 4312, 4317, 4323, 4328, 4333, 4339, 4344, 4350, 4355, 4361, 4366, 4372, 4377, 4383,
247
  4388, 4394, 4399, 4405, 4410, 4416, 4421, 4427, 4432, 4438, 4444, 4449, 4455, 4461, 4467, 4472,
248
  4478, 4484, 4489, 4495, 4500, 4506, 4511, 4517, 4522, 4528, 4534, 4540, 4546, 4551, 4557, 4563,
249
  4569, 4575, 4581, 4586, 4592, 4598, 4604, 4609, 4615, 4621, 4627, 4633, 4639, 4644, 4650, 4656,
250
  4662, 4668, 4674, 4680, 4686, 4692, 4698, 4704, 4710, 4716, 4722, 4728, 4734, 4740, 4746, 4752,
251
  4758, 4764, 4771, 4777, 4783, 4789, 4796, 4802, 4808, 4814, 4820, 4826, 4832, 4838, 4844, 4850,
252
  4856, 4862, 4869, 4875, 4882, 4888, 4894, 4901, 4907, 4913, 4920, 4926, 4933, 4939, 4945, 4952,
253
  4958, 4964, 4971, 4977, 4983, 4989, 4996, 5002, 5008, 5015, 5021, 5028, 5034, 5041, 5047, 5054,
254
  5060, 5067, 5073, 5080, 5087, 5093, 5100, 5106, 5113, 5120, 5126, 5133, 5140, 5146, 5153, 5159,
255
  5166, 5173, 5180, 5186, 5193, 5200, 5207, 5213, 5220, 5227, 5234, 5240, 5247, 5254, 5261, 5267,
256
  5274, 5281, 5288, 5295, 5302, 5309, 5316, 5323, 5330, 5337, 5344, 5351, 5358, 5365, 5372, 5379,
257
  5386, 5393, 5400, 5407, 5414, 5421, 5428, 5435, 5442, 5449, 5456, 5463, 5471, 5478, 5485, 5492,
258
  5499, 5506, 5514, 5521, 5528, 5535, 5543, 5550, 5557, 5564, 5572, 5579, 5587, 5594, 5601, 5609,
259
  5616, 5623, 5631, 5638, 5646, 5653, 5660, 5668, 5675, 5683, 5690, 5698, 5705, 5713, 5720, 5728,
260
  5735, 5743, 5751, 5758, 5766, 5774, 5782, 5789, 5797, 5805, 5812, 5820, 5828, 5835, 5843, 5850,
261
  5858, 5866, 5874, 5881, 5889, 5897, 5905, 5912, 5920, 5928, 5936, 5944, 5952, 5959, 5967, 5975,
262
  5983, 5991, 5999, 6007, 6015, 6023, 6031, 6039, 6047, 6055, 6063, 6071, 6079, 6087, 6095, 6103,
263
  6111, 6119, 6128, 6136, 6144, 6152, 6161, 6169, 6177, 6185, 6194, 6202, 6210, 6218, 6227, 6235,
264
  6243, 6251, 6260, 6268, 6277, 6285, 6293, 6302, 6310, 6319, 6327, 6336, 6344, 6353, 6361, 6370,
265
  6378, 6387, 6395, 6404, 6412, 6421, 6429, 6438, 6446, 6455, 6464, 6472, 6481, 6490, 6499, 6507,
266
  6516, 6525, 6534, 6543, 6552, 6560, 6569, 6578, 6587, 6596, 6605, 6613, 6622, 6631, 6640, 6648,
267
  6657, 6666, 6675, 6684, 6694, 6703, 6712, 6721, 6730, 6739, 6748, 6757, 6767, 6776, 6785, 6794,
268
  6803, 6812, 6822, 6831, 6840, 6849, 6859, 6868, 6877, 6886, 6896, 6905, 6914, 6923, 6933, 6942,
269
  6951, 6961, 6970, 6980, 6989, 6999, 7008, 7018, 7027, 7037, 7046, 7056, 7065, 7075, 7084, 7094,
270
  7103, 7113, 7123, 7132, 7142, 7152, 7162, 7171, 7181, 7191, 7201, 7210, 7220, 7230, 7240, 7249,
271
  7259, 7269, 7279, 7289, 7299, 7309, 7319, 7329, 7339, 7349, 7359, 7369, 7379, 7389, 7399, 7409,
272
  7419, 7429, 7440, 7450, 7460, 7470, 7481, 7491, 7501, 7511, 7522, 7532, 7543, 7553, 7563, 7574,
273
  7584, 7594, 7605, 7615, 7626, 7636, 7646, 7657, 7667, 7678, 7688, 7699, 7709, 7720, 7730, 7741,
274
  7751, 7762, 7772, 7783, 7794, 7804, 7815, 7825, 7836, 7847, 7858, 7869, 7880, 7890, 7901, 7912,
275
  7923, 7934, 7945, 7956, 7967, 7977, 7988, 7999, 8010, 8021, 8032, 8043, 8055, 8066, 8077, 8088,
276
  8099, 8110, 8122, 8133, 8144, 8155, 8167, 8178, 8189, 8200, 8212, 8223, 8234, 8245, 8257, 8268,
277
  8279, 8291, 8302, 8314, 8326, 8337, 8349, 8360, 8372, 8384, 8395, 8407, 8418, 8430, 8441, 8453,
278
  8464, 8476, 8488, 8500, 8512, 8523, 8535, 8547, 8559, 8571, 8583, 8595, 8607, 8618, 8630, 8642,
279
  8654, 8666, 8678, 8690, 8702, 8714, 8726, 8738, 8750, 8762, 8775, 8787, 8799, 8811, 8824, 8836,
280
  8848, 8860, 8873, 8885, 8898, 8910, 8922, 8935, 8947, 8960, 8972, 8985, 8997, 9010, 9022, 9035,
281
  9047, 9060, 9072, 9085, 9098, 9110, 9123, 9135, 9148, 9161, 9174, 9187, 9200, 9212, 9225, 9238,
282
  9251, 9264, 9277, 9290, 9303, 9316, 9329, 9342, 9355, 9368, 9381, 9394, 9408, 9421, 9434, 9447,
283
  9460, 9473, 9487, 9500, 9513, 9526, 9540, 9553, 9566, 9580, 9593, 9607, 9620, 9634, 9647, 9661,
284
  9674, 9688, 9701, 9715, 9728, 9742, 9755, 9769, 9782, 9796, 9810, 9824, 9838, 9851, 9865, 9879,
285
  9893, 9907, 9921, 9935, 9949, 9963, 9977, 9991, 10005, 10019, 10033, 10047, 10061, 10075, 10089, 10103,
286
  10117, 10131, 10146, 10160, 10175, 10189, 10203, 10218, 10232, 10247, 10261, 10276, 10290, 10305, 10319, 10334,
287
  10348, 10363, 10377, 10392, 10407, 10421, 10436, 10450, 10465, 10480, 10495, 10509, 10524, 10539, 10554, 10568,
288
  10583, 10598, 10613, 10628, 10643, 10658, 10673, 10688, 10703, 10718, 10734, 10749, 10764, 10779, 10795, 10810,
289
  10825, 10840, 10856, 10871, 10887, 10902, 10917, 10933, 10948, 10964, 10979, 10995, 11010, 11026, 11041, 11057,
290
  11072, 11088, 11104, 11120, 11136, 11151, 11167, 11183, 11199, 11215, 11231, 11247, 11263, 11278, 11294, 11310,
291
  11326, 11342, 11358, 11374, 11391, 11407, 11423, 11439, 11455, 11471, 11488, 11504, 11521, 11537, 11553, 11570,
292
  11586, 11603, 11619, 11636, 11652, 11669, 11685, 11702, 11718, 11735, 11752, 11768, 11785, 11802, 11819, 11835,
293
  11852, 11869, 11886, 11903, 11920, 11937, 11954, 11971, 11988, 12005, 12022, 12039, 12057, 12074, 12091, 12108,
294
  12125, 12142, 12160, 12177, 12195, 12212, 12229, 12247, 12264, 12282, 12299, 12317, 12335, 12352, 12370, 12387,
295
  12405, 12423, 12441, 12458, 12476, 12494, 12512, 12529, 12547, 12565, 12583, 12601, 12619, 12637, 12655, 12673,
296
  12691, 12709, 12728, 12746, 12764, 12782, 12801, 12819, 12837, 12855, 12874, 12892, 12911, 12929, 12947, 12966,
297
  12984, 13003, 13022, 13040, 13059, 13078, 13097, 13115, 13134, 13153, 13172, 13191, 13210, 13228, 13247, 13266,
298
  13285, 13304, 13323, 13342, 13362, 13381, 13400, 13419, 13438, 13457, 13477, 13496, 13516, 13535, 13554, 13574,
299
  13593, 13613, 13632, 13652, 13672, 13691, 13711, 13730, 13750, 13770, 13790, 13810, 13830, 13849, 13869, 13889,
300
  13909, 13929, 13949, 13969, 13990, 14010, 14030, 14050, 14070, 14090, 14111, 14131, 14151, 14171, 14192, 14212,
301
  14232, 14253, 14273, 14294, 14315, 14335, 14356, 14376, 14397, 14418, 14439, 14460, 14481, 14501, 14522, 14543,
302
  14564, 14585, 14606, 14627, 14648, 14669, 14690, 14711, 14732, 14753, 14775, 14796, 14818, 14839, 14860, 14882,
303
  14903, 14925, 14946, 14968, 14990, 15011, 15033, 15054, 15076, 15098, 15120, 15142, 15164, 15185, 15207, 15229,
304
  15251, 15273, 15295, 15317, 15340, 15362, 15384, 15406, 15428, 15450, 15473, 15495, 15518, 15540, 15562, 15585,
305
  15607, 15630, 15653, 15675, 15698, 15721, 15744, 15766, 15789, 15812, 15835, 15858, 15881, 15903, 15926, 15949,
306
  15972, 15995, 16019, 16042, 16065, 16088, 16112, 16135, 16158, 16182, 16205, 16229, 16253, 16276, 16300, 16323,
307
  16347, 16371, 16395, 16418, 16442, 16466, 16490, 16513, 16537, 16561, 16585, 16609, 16634, 16658, 16682, 16706,
308
  16730, 16754, 16779, 16803, 16828, 16852, 16876, 16901, 16925, 16950, 16975, 16999, 17024, 17049, 17074, 17098,
309
  17123, 17148, 17173, 17198, 17223, 17248, 17273, 17298, 17323, 17348, 17374, 17399, 17424, 17449, 17475, 17500,
310
  17525, 17551, 17576, 17602, 17628, 17653, 17679, 17704, 17730, 17756, 17782, 17808, 17834, 17860, 17886, 17912,
311
  17938, 17964, 17991, 18017, 18043, 18069, 18096, 18122, 18148, 18175, 18201, 18228, 18254, 18281, 18307, 18334,
312
  18360, 18387, 18414, 18441, 18468, 18494, 18521, 18548, 18575, 18602, 18630, 18657, 18684, 18711, 18739, 18766,
313
  18793, 18821, 18848, 18876, 18904, 18931, 18959, 18986, 19014, 19042, 19070, 19097, 19125, 19153, 19181, 19208,
314
  19236, 19264, 19293, 19321, 19349, 19377, 19406, 19434, 19462, 19491, 19519, 19548, 19577, 19605, 19634, 19662,
315
  19691, 19720, 19749, 19778, 19807, 19835, 19864, 19893, 19922, 19951, 19981, 20010, 20040, 20069, 20098, 20128,
316
  20157, 20187, 20216, 20246, 20276, 20305, 20335, 20364, 20394, 20424, 20454, 20484, 20514, 20544, 20574, 20604,
317
  20634, 20664, 20695, 20725, 20756, 20786, 20816, 20847, 20877, 20908, 20939, 20969, 21000, 21031, 21062, 21092,
318
  21123, 21154, 21185, 21216, 21247, 21278, 21309, 21340, 21371, 21403, 21434, 21466, 21497, 21529, 21560, 21592,
319
  21623, 21655, 21687, 21719, 21751, 21782, 21814, 21846, 21878, 21910, 21943, 21975, 22007, 22039, 22072, 22104,
320
  22136, 22169, 22202, 22234, 22267, 22300, 22333, 22365, 22398, 22431, 22464, 22497, 22530, 22563, 22596, 22629,
321
  22662, 22696, 22729, 22763, 22796, 22830, 22863, 22897, 22930, 22964, 22998, 23032, 23066, 23099, 23133, 23167,
322
  23201, 23235, 23270, 23304, 23338, 23372, 23407, 23441, 23475, 23510, 23545, 23579, 23614, 23649, 23684, 23718,
323
  23753, 23788, 23823, 23858, 23894, 23929, 23964, 23999, 24034, 24070, 24105, 24141, 24176, 24212, 24247, 24283,
324
  24318, 24354, 24390, 24426, 24462, 24498, 24534, 24570, 24606, 24643, 24679, 24716, 24752, 24789, 24825, 24862,
325
  24898, 24935, 24972, 25009, 25046, 25082, 25119, 25156, 25193, 25230, 25268, 25305, 25342, 25379, 25417, 25454,
326
  25491, 25529, 25567, 25605, 25643, 25680, 25718, 25756, 25794, 25832, 25871, 25909, 25947, 25985, 26024, 26062,
327
  26100, 26139, 26178, 26216, 26255, 26294, 26333, 26371, 26410, 26449, 26488, 26527, 26567, 26606, 26645, 26684,
328
  26723, 26763, 26802, 26842, 26882, 26921, 26961, 27000, 27040, 27080, 27121, 27161, 27201, 27241, 27282, 27322,
329
  27362, 27403, 27443, 27484, 27525, 27565, 27606, 27646, 27687, 27728, 27769, 27810, 27852, 27893, 27934, 27975,
330
  28016, 28058, 28099, 28141, 28183, 28224, 28266, 28307, 28349, 28391, 28433, 28475, 28518, 28560, 28602, 28644,
331
  28686, 28729, 28772, 28814, 28857, 28900, 28943, 28985, 29028, 29071, 29114, 29157, 29201, 29244, 29287, 29330,
332
  29373, 29417, 29461, 29504, 29548, 29592, 29636, 29679, 29723, 29767, 29812, 29856, 29900, 29944, 29989, 30033,
333
  30077, 30122, 30167, 30211, 30256, 30301, 30346, 30390, 30435, 30480, 30526, 30571, 30617, 30662, 30707, 30753,
334
  30798, 30844, 30890, 30936, 30982, 31027, 31073, 31119, 31165, 31212, 31258, 31305, 31351, 31398, 31444, 31491,
335
  31537, 31584, 31631, 31678, 31725, 31772, 31819, 31866, 31913, 31961, 32008, 32056, 32104, 32151, 32199, 32246,
336
  32294, 32342, 32390, 32438, 32487, 32535, 32583, 32631, 32679, 32766, 32777, 32825, 32874, 32923, 32972, 33020,
337
  33069, 33118, 33168, 33217, 33267, 33316, 33365, 33415, 33464, 33514, 33564, 33614, 33664, 33714, 33764, 33814,
338
  33864, 33915, 33965, 34016, 34067, 34117, 34168, 34218, 34269, 34320, 34371, 34422, 34474, 34525, 34576, 34627,
339
  34678, 34730, 34782, 34834, 34886, 34937, 34989, 35041, 35093, 35146, 35198, 35251, 35303, 35356, 35408, 35461,
340
  35513, 35566, 35619, 35672, 35725, 35778, 35831, 35884, 35937, 35991, 36045, 36098, 36152, 36206, 36260, 36313,
341
  36367, 36422, 36476, 36531, 36585, 36640, 36694, 36749, 36803, 36858, 36913, 36968, 37023, 37078, 37133, 37188,
342
  37243, 37299, 37355, 37410, 37466, 37522, 37578, 37633, 37689, 37746, 37802, 37859, 37915, 37972, 38028, 38085,
343
  38141, 38198, 38255, 38312, 38369, 38426, 38483, 38540, 38597, 38655, 38713, 38771, 38829, 38886, 38944, 39002
344
};
345
346
static inline ushort sony_arw6_llvc3_lut_value(int code)
347
0
{
348
0
  if (code < 0)
349
0
    code = 0;
350
0
  if (code > 4095)
351
0
    code = 4095;
352
0
  return sony_arw6_llvc3_curve[code];
353
0
}
354
355
static size_t sony_arw6_checked_plane_samples(int rows, int cols)
356
0
{
357
0
  sony_arw6_require(rows >= 0 && cols >= 0);
358
0
  const uint64_t r = uint64_t(rows);
359
0
  const uint64_t c = uint64_t(cols);
360
0
  sony_arw6_require(c == 0 || r <= uint64_t(~size_t(0)) / c);
361
0
  const uint64_t samples = r * c;
362
0
  sony_arw6_require(samples <= uint64_t(SONY_ARW6_MAX_PLANE_SAMPLES));
363
0
  return size_t(samples);
364
0
}
365
366
struct SonyArw6Plane
367
{
368
  int rows;
369
  int cols;
370
  std::vector<int32_t> data;
371
372
0
  SonyArw6Plane() : rows(0), cols(0) {}
373
  SonyArw6Plane(int r, int c)
374
0
      : rows(r), cols(c), data(sony_arw6_checked_plane_samples(r, c))
375
0
  {
376
0
  }
377
378
0
  int32_t &at(int r, int c) { return data[size_t(r) * size_t(cols) + c]; }
379
  const int32_t &at(int r, int c) const
380
0
  {
381
0
    return data[size_t(r) * size_t(cols) + c];
382
0
  }
383
0
  int32_t *row(int r) { return &data[size_t(r) * size_t(cols)]; }
384
0
  const int32_t *row(int r) const { return &data[size_t(r) * size_t(cols)]; }
385
};
386
387
struct SonyArw6DirectoryEntry
388
{
389
  int group;
390
  int index;
391
  uint32_t start;
392
  uint32_t length;
393
};
394
395
struct SonyArw6StreamInfo
396
{
397
  int index;
398
  uint32_t offset;
399
  uint32_t length;
400
  int tile_x;
401
  int tile_y;
402
  int tile_w;
403
  int tile_h;
404
  int coded_width;
405
  int coded_half_height;
406
  int logical_height;
407
};
408
409
struct SonyArw6PacketRecord
410
{
411
  uint32_t byte_length;
412
  uchar selectors[3];
413
  uint32_t payload_offset;
414
};
415
416
struct SonyArw6Packet
417
{
418
  int type;
419
  int block_count;
420
  uint32_t control_bytes;
421
  uint32_t total_bytes;
422
  const uchar *data;
423
  uint32_t length;
424
  std::vector<SonyArw6PacketRecord> records;
425
};
426
427
static uint32_t sony_arw6_stream_payload_length(const uchar *stream,
428
                                                uint32_t stream_size)
429
0
{
430
0
  sony_arw6_require(stream_size >= 0x80);
431
0
  uint32_t payload = 0;
432
0
  const int offs[5] = {0, 3, 6, 9, 12};
433
0
  for (int g = 0; g < 5; g++)
434
0
    payload +=
435
0
        ((sony_arw6_be32(stream + 0x10 + offs[g]) >> 4) & 0x0ffffff0);
436
0
  sony_arw6_require(payload <= stream_size - 0x80);
437
0
  return 0x80 + payload;
438
0
}
439
440
static bool sony_arw6_parse_stream_header(const uchar *stream,
441
                                          uint32_t stream_size,
442
                                          SonyArw6StreamInfo &info)
443
0
{
444
0
  if (stream_size < 0x80 ||
445
0
      (memcmp(stream, "A000", 4) && memcmp(stream, "0000", 4)))
446
0
    return false;
447
0
  const uint16_t word_c = sony_arw6_be16(stream + 0x0c);
448
0
  const uint16_t word_e = sony_arw6_be16(stream + 0x0e);
449
0
  const int decoded_bits = (word_c >> 4) & 0x3f;
450
0
  const int components = word_e >> 13;
451
0
  const int mode = (word_e >> 10) & 0x03;
452
0
  if (decoded_bits != 16 || components != 3 || mode != 3)
453
0
    return false;
454
0
  info.coded_width = sony_arw6_be16(stream + 8);
455
0
  info.coded_half_height = sony_arw6_be16(stream + 0x0a);
456
0
  info.logical_height = info.coded_half_height * 2;
457
0
  if (info.coded_width <= 0 || info.coded_half_height <= 0 ||
458
0
      (info.coded_width & 15))
459
0
    return false;
460
0
  return true;
461
0
}
462
463
static std::vector<SonyArw6StreamInfo>
464
sony_arw6_find_streams(const std::vector<uchar> &strip, int full_width,
465
                       int full_height)
466
0
{
467
0
  std::vector<SonyArw6StreamInfo> streams;
468
0
  if (full_width <= 0 || full_height <= 0)
469
0
    return streams;
470
0
  const uint32_t strip_size = uint32_t(strip.size());
471
0
  const uchar *base = strip_size ? &strip[0] : 0;
472
0
  if (!base || strip_size < SONY_ARW6_STREAM_OFFSET + 0x80)
473
0
    return streams;
474
475
0
  const uint32_t count = sony_arw6_le32(base);
476
0
  if (count >= 1 && count <= 16)
477
0
  {
478
0
    for (uint32_t index = 0; index < count; index++)
479
0
    {
480
0
      const uint32_t entry = 0x08 + index * 0x18;
481
0
      if (entry + 0x18 > strip_size)
482
0
      {
483
0
        streams.clear();
484
0
        break;
485
0
      }
486
0
      const uint32_t table_offset = sony_arw6_le32(base + entry);
487
0
      const int tile_x = int(sony_arw6_le32(base + entry + 0x08));
488
0
      const int tile_y = int(sony_arw6_le32(base + entry + 0x0c));
489
0
      const int entry_tile_w = int(sony_arw6_le32(base + entry + 0x10));
490
0
      const int entry_tile_h = int(sony_arw6_le32(base + entry + 0x14));
491
0
      const uint32_t start = table_offset ? table_offset : SONY_ARW6_STREAM_OFFSET;
492
0
      if (start > strip_size - 0x80)
493
0
      {
494
0
        streams.clear();
495
0
        break;
496
0
      }
497
0
      const uint32_t search_end =
498
0
          std::min(strip_size - 0x80,
499
0
                   start > UINT32_MAX - uint32_t(0x1000)
500
0
                       ? UINT32_MAX
501
0
                       : start + uint32_t(0x1000));
502
0
      bool found = false;
503
0
      SonyArw6StreamInfo info;
504
0
      for (uint32_t candidate = start; candidate <= search_end;
505
0
           candidate += 0x10)
506
0
      {
507
0
        if (!sony_arw6_parse_stream_header(base + candidate,
508
0
                                           strip_size - candidate, info))
509
0
          continue;
510
0
        uint32_t length =
511
0
            sony_arw6_stream_payload_length(base + candidate,
512
0
                                            strip_size - candidate);
513
0
        if (length <= 0x80 || candidate + length > strip_size)
514
0
          continue;
515
0
        info.index = int(index);
516
0
        info.offset = candidate;
517
0
        info.length = length;
518
0
        info.tile_x = tile_x;
519
0
        info.tile_y = tile_y;
520
0
        info.tile_w = entry_tile_w ? entry_tile_w : info.coded_width;
521
0
        info.tile_h = entry_tile_h ? entry_tile_h : info.logical_height;
522
0
        if (info.tile_x < 0 || info.tile_y < 0 ||
523
0
            info.tile_w != info.coded_width ||
524
0
            info.tile_h != info.logical_height ||
525
0
            info.tile_x > full_width - info.tile_w ||
526
0
            info.tile_y > full_height - info.tile_h)
527
0
          continue;
528
0
        streams.push_back(info);
529
0
        found = true;
530
0
        break;
531
0
      }
532
0
      if (!found)
533
0
      {
534
0
        streams.clear();
535
0
        break;
536
0
      }
537
0
    }
538
0
    if (streams.size() == count)
539
0
      return streams;
540
0
  }
541
542
0
  SonyArw6StreamInfo info;
543
0
  if (sony_arw6_parse_stream_header(base + SONY_ARW6_STREAM_OFFSET,
544
0
                                    strip_size - SONY_ARW6_STREAM_OFFSET,
545
0
                                    info))
546
0
  {
547
0
    info.index = 0;
548
0
    info.offset = SONY_ARW6_STREAM_OFFSET;
549
0
    info.length =
550
0
        sony_arw6_stream_payload_length(base + info.offset,
551
0
                                        strip_size - info.offset);
552
0
    info.tile_x = 0;
553
0
    info.tile_y = 0;
554
0
    info.tile_w = info.coded_width;
555
0
    info.tile_h = info.logical_height;
556
0
    if (info.coded_width == full_width && info.logical_height == full_height)
557
0
      streams.push_back(info);
558
0
  }
559
0
  return streams;
560
0
}
561
562
class SonyArw6HeaderBits
563
{
564
  const uchar *data_;
565
  uint32_t size_;
566
  uint32_t bitpos_;
567
568
public:
569
  SonyArw6HeaderBits(const uchar *data, uint32_t size)
570
0
      : data_(data), size_(size), bitpos_(0)
571
0
  {
572
0
  }
573
574
  uint32_t read(int nbits)
575
0
  {
576
0
    sony_arw6_require(nbits >= 0 && nbits <= 32);
577
0
    sony_arw6_require(uint64_t(bitpos_) + uint32_t(nbits) <=
578
0
                      uint64_t(size_) * 8U);
579
0
    uint32_t out = 0;
580
0
    for (int i = 0; i < nbits; i++)
581
0
    {
582
0
      const uchar byte = data_[bitpos_ >> 3];
583
0
      const uint32_t bit = (byte >> (7 - (bitpos_ & 7))) & 1;
584
0
      out = (out << 1) | bit;
585
0
      bitpos_++;
586
0
    }
587
0
    return out;
588
0
  }
589
};
590
591
class SonyArw6NativeBits
592
{
593
  const uchar *data_;
594
  uint32_t size_;
595
  int64_t ptr_;
596
  uint64_t cur_;
597
  int bit_;
598
  int words_left_;
599
600
public:
601
  int status;
602
603
  SonyArw6NativeBits(const uchar *packet, uint32_t packet_len,
604
                     uint32_t payload_offset, uint32_t byte_length)
605
0
      : data_(packet), size_(packet_len), ptr_(int64_t(payload_offset) - 8),
606
0
        cur_(0), bit_(0),
607
0
        words_left_(((((int)byte_length + 7) / 8) + 1) & ~1), status(0)
608
0
  {
609
0
  }
610
611
  void load_next_word()
612
0
  {
613
0
    words_left_--;
614
0
    if (words_left_ < 0)
615
0
    {
616
0
      status = 2;
617
0
      return;
618
0
    }
619
0
    ptr_ += 8;
620
0
    uint64_t x = 0;
621
0
    for (int i = 0; i < 8; i++)
622
0
    {
623
0
      x <<= 8;
624
0
      const int64_t pos = ptr_ + i;
625
0
      if (pos >= 0 && pos < int64_t(size_))
626
0
        x |= data_[pos];
627
0
    }
628
0
    cur_ = x;
629
0
    bit_ = 64;
630
0
  }
631
632
  uint32_t read_bits(int nbits)
633
0
  {
634
0
    sony_arw6_require(nbits >= 0 && nbits <= 32);
635
0
    uint32_t out = 0;
636
0
    int remaining = nbits;
637
0
    while (remaining > 0)
638
0
    {
639
0
      if (bit_ <= 0)
640
0
      {
641
0
        load_next_word();
642
0
        if (status)
643
0
          return out << remaining;
644
0
      }
645
0
      const int take = std::min(remaining, bit_);
646
0
      bit_ -= take;
647
0
      out = (out << take) |
648
0
            uint32_t((cur_ >> bit_) & ((uint64_t(1) << take) - 1));
649
0
      remaining -= take;
650
0
    }
651
0
    return out;
652
0
  }
653
654
  int read_unary_zeros_plus_one()
655
0
  {
656
0
    int zeros = 0;
657
0
    while (!status)
658
0
    {
659
0
      const uint32_t bit = read_bits(1);
660
0
      if (status)
661
0
        break;
662
0
      if (bit)
663
0
        return zeros + 1;
664
0
      zeros++;
665
0
    }
666
0
    return zeros + 1;
667
0
  }
668
};
669
670
static int sony_arw6_update_width(SonyArw6NativeBits &br, int wstate)
671
0
{
672
0
  if (br.read_bits(1) == 0 || br.status)
673
0
    return wstate;
674
0
  if (br.read_bits(1) == 0 || br.status)
675
0
    return wstate + br.read_unary_zeros_plus_one();
676
677
0
  for (int zeros = 0; zeros < std::max(0, wstate - 1); zeros++)
678
0
  {
679
0
    if (br.read_bits(1))
680
0
      return wstate - (zeros + 1);
681
0
  }
682
0
  return 0;
683
0
}
684
685
static int sony_arw6_read_zero_run(SonyArw6NativeBits &br, int remaining)
686
0
{
687
0
  if (remaining <= 1)
688
0
    return remaining;
689
0
  int max_prefix = 0;
690
0
  for (int x = remaining - 1; x; x >>= 1)
691
0
    max_prefix++;
692
0
  int zeros = 0;
693
0
  while (zeros < max_prefix)
694
0
  {
695
0
    if (br.read_bits(1))
696
0
      break;
697
0
    if (br.status)
698
0
      return remaining;
699
0
    zeros++;
700
0
  }
701
0
  if (zeros >= max_prefix)
702
0
    return remaining;
703
0
  const int base = 1 << zeros;
704
0
  if (base >= remaining)
705
0
    return remaining;
706
0
  const int extra = zeros ? int(br.read_bits(zeros)) : 0;
707
0
  return std::min(base + extra, remaining);
708
0
}
709
710
static void sony_arw6_decode_coeff_group(SonyArw6NativeBits &br, int nbits,
711
                                         int shift, int32_t vals[4])
712
0
{
713
0
  for (int i = 0; i < 4; i++)
714
0
  {
715
0
    uint32_t x = nbits > 0 ? br.read_bits(nbits) : 0;
716
0
    int32_t v = int32_t(x);
717
0
    if (shift > 0 && x > 0)
718
0
      v = int32_t(((uint64_t(2) * x + 1) << (shift - 1)) - (x & 1));
719
0
    vals[i] = v;
720
0
  }
721
0
}
722
723
static void sony_arw6_apply_signs(SonyArw6NativeBits &br, int32_t vals[4])
724
0
{
725
0
  for (int i = 0; i < 4; i++)
726
0
    if (vals[i] > 0)
727
0
    {
728
0
      const uint32_t bit = br.read_bits(1);
729
0
      vals[i] = vals[i] - 2 * vals[i] * int32_t(bit);
730
0
    }
731
0
}
732
733
static void sony_arw6_decode_component(SonyArw6NativeBits &br, int groups,
734
                                       int shift, int32_t *dst, int row_width)
735
0
{
736
0
  int width_state = sony_arw6_update_width(br, 0);
737
0
  int gi = 0;
738
0
  int out = 0;
739
0
  while (gi < groups)
740
0
  {
741
0
    if (br.status || width_state > 0x13)
742
0
    {
743
0
      br.status = br.status ? br.status : 1;
744
0
      while (out < row_width)
745
0
        dst[out++] = 0;
746
0
      return;
747
0
    }
748
0
    if (width_state == 0)
749
0
    {
750
0
      const int run = sony_arw6_read_zero_run(br, groups - gi);
751
0
      for (int r = 0; r < run; r++)
752
0
        for (int k = 0; k < 4 && out < row_width; k++)
753
0
          dst[out++] = 0;
754
0
      gi += run;
755
0
      if (gi >= groups)
756
0
        break;
757
0
      width_state = br.read_unary_zeros_plus_one();
758
0
      continue;
759
0
    }
760
761
0
    int32_t vals[4];
762
0
    sony_arw6_decode_coeff_group(br, width_state, shift, vals);
763
0
    const int next_width =
764
0
        gi + 1 < groups ? sony_arw6_update_width(br, width_state) : width_state;
765
0
    sony_arw6_apply_signs(br, vals);
766
0
    for (int k = 0; k < 4 && out < row_width; k++)
767
0
      dst[out++] = vals[k];
768
0
    width_state = next_width;
769
0
    gi++;
770
0
  }
771
0
  while (out < row_width)
772
0
    dst[out++] = 0;
773
0
}
774
775
static int sony_arw6_row_multiplier(int group)
776
0
{
777
0
  if (group == 4)
778
0
    return 8;
779
0
  if (group == 0)
780
0
    return 1;
781
0
  return 1 << (group - 1);
782
0
}
783
784
static int sony_arw6_infer_packet_width(int group, int packet_type,
785
                                        int mosaic_width)
786
0
{
787
0
  sony_arw6_require(mosaic_width > 0 && (mosaic_width & 15) == 0);
788
0
  if (packet_type == 1)
789
0
    return group == 4 ? mosaic_width / 2 : mosaic_width / 16;
790
0
  if (packet_type == 3 && group >= 1 && group <= 3)
791
0
    return mosaic_width / (1 << (5 - group));
792
0
  throw LIBRAW_EXCEPTION_IO_CORRUPT;
793
0
}
794
795
static int sony_arw6_expected_packet_rows(int group, int coded_height)
796
0
{
797
0
  sony_arw6_require(group >= 0 && group <= 4);
798
0
  sony_arw6_require(coded_height > 0 && coded_height <= 0x7fff);
799
0
  const int padded_height = sony_arw6_align_up(coded_height, 16);
800
0
  return (padded_height / 16 + 1) * sony_arw6_row_multiplier(group);
801
0
}
802
803
static std::vector<SonyArw6DirectoryEntry>
804
sony_arw6_parse_directory(const uchar *stream, uint32_t stream_size)
805
0
{
806
0
  sony_arw6_require(stream_size >= 0x80);
807
0
  std::vector<SonyArw6DirectoryEntry> entries;
808
0
  uint32_t group_lengths[5];
809
0
  const int offs[5] = {0, 3, 6, 9, 12};
810
0
  for (int g = 0; g < 5; g++)
811
0
    group_lengths[g] =
812
0
        ((sony_arw6_be32(stream + 0x10 + offs[g]) >> 4) & 0x0ffffff0);
813
814
0
  uint32_t pos = 0x30;
815
0
  uint32_t base = 0;
816
0
  for (int group = 0; group < 5; group++)
817
0
  {
818
0
    sony_arw6_require(pos + 0x10 <= stream_size);
819
0
    const int n_entries = stream[pos] & 0x0f;
820
0
    sony_arw6_require(n_entries >= 0 && n_entries <= 9);
821
0
    uint32_t values[9] = {0};
822
0
    for (int i = 0; i < 5; i++)
823
0
      values[i] = (sony_arw6_be32(stream + pos + offs[i]) & 0x00ffffff) << 4;
824
0
    uint32_t consumed = 0x10;
825
0
    if (n_entries >= 5)
826
0
    {
827
0
      sony_arw6_require(pos + 0x20 <= stream_size);
828
0
      for (int i = 0; i < 4; i++)
829
0
        values[5 + i] =
830
0
            (sony_arw6_be32(stream + pos + 0x10 + offs[i]) & 0x00ffffff)
831
0
            << 4;
832
0
      consumed = 0x20;
833
0
    }
834
835
0
    uint32_t local = 0;
836
0
    for (int index = 0; index < n_entries; index++)
837
0
    {
838
0
      SonyArw6DirectoryEntry e;
839
0
      e.group = group;
840
0
      e.index = index;
841
0
      sony_arw6_require(base <= UINT32_MAX - local &&
842
0
                        0x80 <= UINT32_MAX - base - local);
843
0
      e.start = 0x80 + base + local;
844
0
      e.length = values[index];
845
0
      sony_arw6_require(e.start <= stream_size &&
846
0
                        e.length <= stream_size - e.start);
847
0
      entries.push_back(e);
848
0
      sony_arw6_require(local <= UINT32_MAX - e.length);
849
0
      local += e.length;
850
0
    }
851
0
    sony_arw6_require(local <= group_lengths[group]);
852
0
    sony_arw6_require(base <= UINT32_MAX - group_lengths[group]);
853
0
    base += group_lengths[group];
854
0
    pos += consumed;
855
0
  }
856
0
  return entries;
857
0
}
858
859
static SonyArw6DirectoryEntry
860
sony_arw6_find_entry(const std::vector<SonyArw6DirectoryEntry> &entries,
861
                     int group, int index)
862
0
{
863
0
  for (size_t i = 0; i < entries.size(); i++)
864
0
    if (entries[i].group == group && entries[i].index == index)
865
0
      return entries[i];
866
0
  throw LIBRAW_EXCEPTION_IO_CORRUPT;
867
0
}
868
869
static SonyArw6Packet sony_arw6_parse_packet(const uchar *data, uint32_t length)
870
0
{
871
0
  sony_arw6_require(length >= 16);
872
0
  SonyArw6HeaderBits br(data, length);
873
0
  const uint32_t control_count = br.read(16);
874
0
  const uint32_t extra_count = br.read(24);
875
0
  const uint32_t tag4 = br.read(4);
876
0
  const uint32_t reserved4 = br.read(4);
877
0
  const uint32_t type2 = br.read(2);
878
0
  const uint32_t control_words = br.read(6);
879
0
  const uint32_t block_count = br.read(16);
880
0
  const uint32_t width_marker = br.read(8);
881
0
  const uint32_t reserved8 = br.read(8);
882
0
  for (int i = 0; i < 5; i++)
883
0
    sony_arw6_require(br.read(8) == 0);
884
885
0
  sony_arw6_require(tag4 == 4 && reserved4 == 0 && control_words == 0);
886
0
  sony_arw6_require(type2 == 1 || type2 == 3);
887
0
  sony_arw6_require(block_count > 0 && block_count <= 30000);
888
0
  sony_arw6_require(width_marker == 0x10 && reserved8 == 0);
889
890
0
  SonyArw6Packet packet;
891
0
  packet.type = int(type2);
892
0
  packet.block_count = int(block_count);
893
0
  const uint64_t control_bytes = (uint64_t(control_count) + 1U) << 4;
894
0
  const uint64_t total_bytes =
895
0
      (uint64_t(control_count) + 1U + extra_count) << 4;
896
0
  sony_arw6_require(control_bytes <= UINT32_MAX &&
897
0
                    total_bytes <= UINT32_MAX);
898
0
  packet.control_bytes = uint32_t(control_bytes);
899
0
  packet.total_bytes = uint32_t(total_bytes);
900
0
  packet.data = data;
901
0
  packet.length = length;
902
0
  sony_arw6_require(packet.total_bytes == length);
903
0
  sony_arw6_require(packet.control_bytes <= length);
904
905
0
  uint32_t cursor = packet.control_bytes;
906
0
  packet.records.reserve(block_count);
907
0
  for (uint32_t i = 0; i < block_count; i++)
908
0
  {
909
0
    SonyArw6PacketRecord rec;
910
0
    rec.byte_length = br.read(16);
911
0
    memset(rec.selectors, 0, sizeof(rec.selectors));
912
0
    for (uint32_t j = 0; j < type2; j++)
913
0
      rec.selectors[j] = uchar(br.read(4));
914
0
    rec.payload_offset = cursor;
915
0
    sony_arw6_require(rec.byte_length <= length &&
916
0
                      cursor <= length - rec.byte_length);
917
0
    cursor += rec.byte_length;
918
0
    packet.records.push_back(rec);
919
0
  }
920
0
  sony_arw6_require(cursor <= packet.total_bytes);
921
0
  return packet;
922
0
}
923
924
static std::vector<SonyArw6Plane>
925
sony_arw6_decode_packet_arrays(const uchar *stream, uint32_t stream_size,
926
                               const std::vector<SonyArw6DirectoryEntry> &dir,
927
                               int mosaic_width, int coded_height, int group,
928
                               int index)
929
0
{
930
0
  const SonyArw6DirectoryEntry e = sony_arw6_find_entry(dir, group, index);
931
0
  const SonyArw6Packet p = sony_arw6_parse_packet(stream + e.start, e.length);
932
0
  const int components = p.type == 1 ? 1 : 3;
933
0
  const int row_width = sony_arw6_infer_packet_width(group, p.type, mosaic_width);
934
0
  const int row_multiplier = sony_arw6_row_multiplier(group);
935
0
  const int rows = p.block_count * row_multiplier;
936
0
  const int expected_rows = sony_arw6_expected_packet_rows(group, coded_height);
937
0
  const int groups_per_row = (row_width + 3) / 4;
938
0
  sony_arw6_require(row_width > 0 && groups_per_row > 0 && rows > 0);
939
0
  if (rows != expected_rows)
940
0
    sony_arw6_require((coded_height & 15) &&
941
0
                      expected_rows - rows == row_multiplier);
942
0
  sony_arw6_require(size_t(p.block_count) <=
943
0
                    (stream_size / 16U + 1U)); /* weak corruption guard */
944
945
0
  std::vector<SonyArw6Plane> planes;
946
0
  planes.reserve(components);
947
0
  for (int c = 0; c < components; c++)
948
0
    planes.push_back(SonyArw6Plane(expected_rows, row_width));
949
950
0
  for (int ri = 0; ri < p.block_count; ri++)
951
0
  {
952
0
    const SonyArw6PacketRecord &rec = p.records[ri];
953
0
    SonyArw6NativeBits bits(p.data, p.length, rec.payload_offset,
954
0
                            rec.byte_length);
955
0
    for (int ci = 0; ci < components; ci++)
956
0
    {
957
0
      const int shift = ci < p.type ? rec.selectors[ci] : 0;
958
0
      for (int rm = 0; rm < row_multiplier; rm++)
959
0
      {
960
0
        int32_t *dst = planes[ci].row(ri * row_multiplier + rm);
961
0
        if (rec.byte_length <= 0)
962
0
          memset(dst, 0, sizeof(int32_t) * size_t(row_width));
963
0
        else
964
0
          sony_arw6_decode_component(bits, groups_per_row, shift, dst, row_width);
965
0
      }
966
0
    }
967
0
  }
968
0
  return planes;
969
0
}
970
971
static SonyArw6Plane sony_arw6_integrate_type1(const SonyArw6Plane &coeffs,
972
                                               int rows, int dc_offset)
973
0
{
974
0
  sony_arw6_require(rows > 0 && rows <= coeffs.rows);
975
0
  SonyArw6Plane out(rows, coeffs.cols);
976
0
  for (int y = 0; y < rows; y++)
977
0
  {
978
0
    int32_t acc = sony_arw6_sign16(coeffs.at(y, 0)) * 2;
979
0
    out.at(y, 0) = acc / 2 + dc_offset;
980
0
    for (int x = 1; x < coeffs.cols; x++)
981
0
    {
982
0
      acc += sony_arw6_sign16(coeffs.at(y, x)) * 2;
983
0
      out.at(y, x) = acc / 2 + dc_offset;
984
0
    }
985
0
  }
986
0
  return out;
987
0
}
988
989
static SonyArw6Plane sony_arw6_head_rows(const SonyArw6Plane &src, int rows)
990
0
{
991
0
  sony_arw6_require(rows >= 0 && rows <= src.rows);
992
0
  SonyArw6Plane out(rows, src.cols);
993
0
  if (rows)
994
0
    memcpy(&out.data[0], &src.data[0],
995
0
           sizeof(int32_t) * size_t(rows) * size_t(src.cols));
996
0
  return out;
997
0
}
998
999
static SonyArw6Plane sony_arw6_inv53_axis0(const SonyArw6Plane &low,
1000
                                           const SonyArw6Plane &high)
1001
0
{
1002
0
  sony_arw6_require(low.rows == high.rows && low.cols == high.cols);
1003
0
  SonyArw6Plane lo2(low.rows, low.cols);
1004
0
  for (int y = 0; y < low.rows; y++)
1005
0
    for (int x = 0; x < low.cols; x++)
1006
0
    {
1007
0
      const int32_t hp = high.at(y ? y - 1 : 0, x);
1008
0
      const int32_t hc = high.at(y, x);
1009
0
      lo2.at(y, x) = low.at(y, x) - sony_arw6_floor_shift(hp + hc + 2, 2);
1010
0
    }
1011
1012
0
  SonyArw6Plane out(low.rows * 2, low.cols);
1013
0
  for (int y = 0; y < low.rows; y++)
1014
0
    for (int x = 0; x < low.cols; x++)
1015
0
    {
1016
0
      const int32_t ln = lo2.at(y + 1 < low.rows ? y + 1 : y, x);
1017
0
      const int32_t hi2 =
1018
0
          high.at(y, x) + sony_arw6_floor_shift(lo2.at(y, x) + ln, 1);
1019
0
      out.at(y * 2, x) = lo2.at(y, x);
1020
0
      out.at(y * 2 + 1, x) = hi2;
1021
0
    }
1022
0
  return out;
1023
0
}
1024
1025
static SonyArw6Plane sony_arw6_inv53_axis1(const SonyArw6Plane &low,
1026
                                           const SonyArw6Plane &high)
1027
0
{
1028
0
  sony_arw6_require(low.rows == high.rows && low.cols == high.cols);
1029
0
  SonyArw6Plane lo2(low.rows, low.cols);
1030
0
  for (int y = 0; y < low.rows; y++)
1031
0
    for (int x = 0; x < low.cols; x++)
1032
0
    {
1033
0
      const int32_t hp = high.at(y, x ? x - 1 : 0);
1034
0
      const int32_t hc = high.at(y, x);
1035
0
      lo2.at(y, x) = low.at(y, x) - sony_arw6_floor_shift(hp + hc + 2, 2);
1036
0
    }
1037
1038
0
  SonyArw6Plane out(low.rows, low.cols * 2);
1039
0
  for (int y = 0; y < low.rows; y++)
1040
0
    for (int x = 0; x < low.cols; x++)
1041
0
    {
1042
0
      const int32_t ln = lo2.at(y, x + 1 < low.cols ? x + 1 : x);
1043
0
      const int32_t hi2 =
1044
0
          high.at(y, x) + sony_arw6_floor_shift(lo2.at(y, x) + ln, 1);
1045
0
      out.at(y, x * 2) = lo2.at(y, x);
1046
0
      out.at(y, x * 2 + 1) = hi2;
1047
0
    }
1048
0
  return out;
1049
0
}
1050
1051
static SonyArw6Plane sony_arw6_inv53_axis0_high_leading(
1052
    const SonyArw6Plane &low, const SonyArw6Plane &high)
1053
0
{
1054
0
  sony_arw6_require(low.cols == high.cols);
1055
0
  sony_arw6_require(high.rows == low.rows || high.rows == low.rows + 1);
1056
0
  SonyArw6Plane lo2(low.rows, low.cols);
1057
0
  SonyArw6Plane hi2(high.rows, high.cols);
1058
1059
0
  if (high.rows == low.rows + 1)
1060
0
  {
1061
0
    for (int y = 0; y < low.rows; y++)
1062
0
      for (int x = 0; x < low.cols; x++)
1063
0
        lo2.at(y, x) =
1064
0
            low.at(y, x) -
1065
0
            sony_arw6_floor_shift(high.at(y, x) + high.at(y + 1, x) + 2, 2);
1066
1067
0
    for (int x = 0; x < high.cols; x++)
1068
0
    {
1069
0
      hi2.at(0, x) = high.at(0, x) + lo2.at(0, x);
1070
0
      hi2.at(high.rows - 1, x) = high.at(high.rows - 1, x) +
1071
0
                                 lo2.at(low.rows - 1, x);
1072
0
    }
1073
0
    for (int y = 1; y < high.rows - 1; y++)
1074
0
      for (int x = 0; x < high.cols; x++)
1075
0
        hi2.at(y, x) = high.at(y, x) +
1076
0
                       sony_arw6_floor_shift(lo2.at(y - 1, x) +
1077
0
                                                 lo2.at(y, x),
1078
0
                                             1);
1079
1080
0
    SonyArw6Plane out(low.rows * 2 + 1, low.cols);
1081
0
    for (int y = 0; y < low.rows; y++)
1082
0
    {
1083
0
      memcpy(out.row(y * 2), hi2.row(y), sizeof(int32_t) * size_t(low.cols));
1084
0
      memcpy(out.row(y * 2 + 1), lo2.row(y),
1085
0
             sizeof(int32_t) * size_t(low.cols));
1086
0
    }
1087
0
    memcpy(out.row(out.rows - 1), hi2.row(hi2.rows - 1),
1088
0
           sizeof(int32_t) * size_t(low.cols));
1089
0
    return out;
1090
0
  }
1091
1092
0
  for (int y = 0; y < low.rows; y++)
1093
0
    for (int x = 0; x < low.cols; x++)
1094
0
    {
1095
0
      const int next_y = y + 1 < high.rows ? y + 1 : y;
1096
0
      lo2.at(y, x) =
1097
0
          low.at(y, x) -
1098
0
          sony_arw6_floor_shift(high.at(y, x) + high.at(next_y, x) + 2, 2);
1099
0
    }
1100
0
  for (int x = 0; x < high.cols; x++)
1101
0
    hi2.at(0, x) = high.at(0, x) + lo2.at(0, x);
1102
0
  for (int y = 1; y < high.rows; y++)
1103
0
    for (int x = 0; x < high.cols; x++)
1104
0
      hi2.at(y, x) =
1105
0
          high.at(y, x) +
1106
0
          sony_arw6_floor_shift(lo2.at(y - 1, x) + lo2.at(y, x), 1);
1107
1108
0
  SonyArw6Plane out(low.rows * 2, low.cols);
1109
0
  for (int y = 0; y < low.rows; y++)
1110
0
  {
1111
0
    memcpy(out.row(y * 2), hi2.row(y), sizeof(int32_t) * size_t(low.cols));
1112
0
    memcpy(out.row(y * 2 + 1), lo2.row(y),
1113
0
           sizeof(int32_t) * size_t(low.cols));
1114
0
  }
1115
0
  return out;
1116
0
}
1117
1118
static SonyArw6Plane sony_arw6_synthesize_level(const SonyArw6Plane &ll,
1119
                                                const SonyArw6Plane &sub0,
1120
                                                const SonyArw6Plane &sub1,
1121
                                                const SonyArw6Plane &sub2)
1122
0
{
1123
0
  const int h = ll.rows;
1124
0
  const int w = ll.cols;
1125
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1126
0
  sony_arw6_require(sub0.rows >= h + 1 && sub1.rows >= h + 1 &&
1127
0
                    sub2.rows >= h + 1);
1128
0
  SonyArw6Plane lh(h, w), hh(h, w);
1129
0
  for (int y = 0; y < h - 1; y++)
1130
0
    for (int x = 0; x < w; x++)
1131
0
    {
1132
0
      lh.at(y, x) = sub1.at(y + 1, x);
1133
0
      hh.at(y, x) = sub2.at(y + 1, x);
1134
0
    }
1135
0
  for (int x = 0; x < w; x++)
1136
0
  {
1137
0
    lh.at(h - 1, x) = sub0.at(h, x);
1138
0
    hh.at(h - 1, x) = sub1.at(h, x);
1139
0
  }
1140
0
  SonyArw6Plane low_horizontal = sony_arw6_inv53_axis0(ll, lh);
1141
0
  SonyArw6Plane high_horizontal =
1142
0
      sony_arw6_inv53_axis0(sony_arw6_head_rows(sub0, h), hh);
1143
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1144
0
}
1145
1146
static SonyArw6Plane sony_arw6_synthesize_guard_group1(
1147
    const SonyArw6Plane &ll, const SonyArw6Plane &sub0,
1148
    const SonyArw6Plane &sub1, const SonyArw6Plane &sub2)
1149
0
{
1150
0
  const int h = ll.rows;
1151
0
  const int w = ll.cols;
1152
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1153
0
  sony_arw6_require(sub0.rows >= h + 2 && sub1.rows >= h + 2 &&
1154
0
                    sub2.rows >= h + 2);
1155
1156
0
  SonyArw6Plane lh(h + 1, w), hh(h + 1, w), hl(h, w);
1157
0
  for (int y = 0; y < h; y++)
1158
0
    for (int x = 0; x < w; x++)
1159
0
    {
1160
0
      hl.at(y, x) = sub0.at(y + 1, x);
1161
0
      lh.at(y, x) = sub1.at(y + 1, x);
1162
0
      hh.at(y, x) = sub2.at(y + 1, x);
1163
0
    }
1164
0
  for (int x = 0; x < w; x++)
1165
0
  {
1166
0
    lh.at(h, x) = sub0.at(h + 1, x);
1167
0
    hh.at(h, x) = sub1.at(h + 1, x);
1168
0
  }
1169
1170
0
  SonyArw6Plane low_horizontal =
1171
0
      sony_arw6_inv53_axis0_high_leading(ll, lh);
1172
0
  SonyArw6Plane high_horizontal =
1173
0
      sony_arw6_inv53_axis0_high_leading(hl, hh);
1174
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1175
0
}
1176
1177
static SonyArw6Plane sony_arw6_synthesize_guard_group2(
1178
    const SonyArw6Plane &ll, const SonyArw6Plane &sub0,
1179
    const SonyArw6Plane &sub1, const SonyArw6Plane &sub2,
1180
    bool odd_edge_mode)
1181
0
{
1182
0
  const int h = ll.rows;
1183
0
  const int w = ll.cols;
1184
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1185
0
  sony_arw6_require(sub0.rows >= h + 3 && sub1.rows >= h + 1 &&
1186
0
                    sub2.rows >= h + 1);
1187
1188
0
  SonyArw6Plane hl(h, w), lh(h, w), hh(h, w);
1189
0
  for (int x = 0; x < w; x++)
1190
0
  {
1191
0
    lh.at(0, x) = sub0.at(0, x);
1192
0
    hh.at(0, x) = sony_arw6_edge_detail(sub0.at(1, x), odd_edge_mode);
1193
0
  }
1194
0
  for (int y = 0; y < h; y++)
1195
0
    for (int x = 0; x < w; x++)
1196
0
      hl.at(y, x) = sub0.at(y + 2, x);
1197
0
  for (int y = 1; y < h; y++)
1198
0
    for (int x = 0; x < w; x++)
1199
0
    {
1200
0
      lh.at(y, x) = sub1.at(y + 1, x);
1201
0
      hh.at(y, x) = sub2.at(y + 1, x);
1202
0
    }
1203
1204
0
  SonyArw6Plane low_horizontal =
1205
0
      sony_arw6_inv53_axis0_high_leading(ll, lh);
1206
0
  SonyArw6Plane high_horizontal =
1207
0
      sony_arw6_inv53_axis0_high_leading(hl, hh);
1208
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1209
0
}
1210
1211
static SonyArw6Plane sony_arw6_synthesize_guard_group3(
1212
    const SonyArw6Plane &ll, const SonyArw6Plane &sub0,
1213
    const SonyArw6Plane &sub1, const SonyArw6Plane &sub2,
1214
    bool odd_edge_mode)
1215
0
{
1216
0
  const int h = ll.rows;
1217
0
  const int w = ll.cols;
1218
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1219
0
  sony_arw6_require(sub0.rows >= h + 5 && sub1.rows >= h + 2 &&
1220
0
                    sub2.rows >= h + 2);
1221
1222
0
  SonyArw6Plane hl(h, w), lh(h, w), hh(h, w);
1223
0
  for (int x = 0; x < w; x++)
1224
0
  {
1225
0
    hl.at(0, x) = sub0.at(0, x);
1226
0
    lh.at(0, x) = sub0.at(1, x);
1227
0
    hh.at(0, x) = sony_arw6_edge_detail(sub0.at(2, x), odd_edge_mode);
1228
0
    lh.at(h - 1, x) = sub0.at(h + 3, x);
1229
0
    hh.at(h - 1, x) =
1230
0
        sony_arw6_edge_detail(sub0.at(h + 4, x), odd_edge_mode);
1231
0
  }
1232
0
  for (int y = 1; y < h; y++)
1233
0
    for (int x = 0; x < w; x++)
1234
0
      hl.at(y, x) = sub0.at(y + 3, x);
1235
0
  for (int y = 1; y < h - 1; y++)
1236
0
    for (int x = 0; x < w; x++)
1237
0
    {
1238
0
      lh.at(y, x) = sub1.at(y + 3, x);
1239
0
      hh.at(y, x) = sub2.at(y + 3, x);
1240
0
    }
1241
1242
0
  SonyArw6Plane low_horizontal = sony_arw6_inv53_axis0(ll, lh);
1243
0
  SonyArw6Plane high_horizontal = sony_arw6_inv53_axis0(hl, hh);
1244
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1245
0
}
1246
1247
static SonyArw6Plane sony_arw6_synthesize_level_stride(
1248
    const SonyArw6Plane &ll, const SonyArw6Plane &sub0,
1249
    const SonyArw6Plane &sub1, const SonyArw6Plane &sub2, int edge_rows,
1250
    bool odd_edge_mode)
1251
0
{
1252
0
  if (edge_rows == 0)
1253
0
    return sony_arw6_synthesize_level(ll, sub0, sub1, sub2);
1254
1255
0
  const int h = ll.rows;
1256
0
  const int w = ll.cols;
1257
0
  sony_arw6_require(sub0.cols == w && sub1.cols == w && sub2.cols == w);
1258
0
  sony_arw6_require(sub0.rows >= h + edge_rows * 2 &&
1259
0
                    sub1.rows >= h + edge_rows &&
1260
0
                    sub2.rows >= h);
1261
1262
0
  SonyArw6Plane hl(h, w), lh(h, w), hh(h, w);
1263
0
  for (int y = 0; y < edge_rows; y++)
1264
0
    for (int x = 0; x < w; x++)
1265
0
    {
1266
0
      hl.at(y, x) = sub0.at(y, x);
1267
0
      lh.at(y, x) = sub0.at(y + edge_rows, x);
1268
0
      hh.at(y, x) = sony_arw6_edge_detail(sub1.at(y, x), odd_edge_mode);
1269
0
    }
1270
0
  for (int y = edge_rows; y < h - edge_rows; y++)
1271
0
    for (int x = 0; x < w; x++)
1272
0
    {
1273
0
      hl.at(y, x) = sub0.at(y + edge_rows, x);
1274
0
      lh.at(y, x) = sub1.at(y + edge_rows, x);
1275
0
      hh.at(y, x) = sub2.at(y + edge_rows, x);
1276
0
    }
1277
0
  for (int y = h - edge_rows; y < h; y++)
1278
0
  {
1279
0
    const int k = y - (h - edge_rows);
1280
0
    for (int x = 0; x < w; x++)
1281
0
    {
1282
0
      hl.at(y, x) = sub0.at(h + k, x);
1283
0
      lh.at(y, x) = sub0.at(h + edge_rows + k, x);
1284
0
      hh.at(y, x) = sony_arw6_edge_detail(sub1.at(h + k, x), odd_edge_mode);
1285
0
    }
1286
0
  }
1287
1288
0
  SonyArw6Plane low_horizontal = sony_arw6_inv53_axis0(ll, lh);
1289
0
  SonyArw6Plane high_horizontal = sony_arw6_inv53_axis0(hl, hh);
1290
0
  return sony_arw6_inv53_axis1(low_horizontal, high_horizontal);
1291
0
}
1292
1293
static SonyArw6Plane sony_arw6_final_green(const SonyArw6Plane &ll,
1294
                                           const SonyArw6Plane &detail,
1295
                                           int top_rows)
1296
0
{
1297
0
  const int h = ll.rows;
1298
0
  const int w = ll.cols;
1299
0
  sony_arw6_require(top_rows >= 0 && top_rows <= 8);
1300
0
  sony_arw6_require(detail.cols == w &&
1301
0
                    detail.rows >= 8 + std::max(0, h - top_rows));
1302
1303
0
  SonyArw6Plane selected(h, w);
1304
0
  for (int y = 0; y < h; y++)
1305
0
  {
1306
0
    const int src_y = y < top_rows ? y : y + 8 - top_rows;
1307
0
    for (int x = 0; x < w; x++)
1308
0
      selected.at(y, x) = detail.at(src_y, x);
1309
0
  }
1310
1311
0
  SonyArw6Plane odd_green(h, w);
1312
0
  for (int y = 0; y < h; y++)
1313
0
  {
1314
0
    const int prev_y = y ? y - 1 : y;
1315
0
    for (int x = 0; x < w; x++)
1316
0
    {
1317
0
      int32_t pred;
1318
0
      if (x + 1 < w)
1319
0
        pred = sony_arw6_floor_shift(
1320
0
            selected.at(y, x + 1) + selected.at(prev_y, x) +
1321
0
                selected.at(y, x) + selected.at(prev_y, x + 1),
1322
0
            2);
1323
0
      else
1324
0
        pred = sony_arw6_floor_shift(
1325
0
            (selected.at(prev_y, x) + selected.at(y, x)) * 2, 2);
1326
0
      odd_green.at(y, x) =
1327
0
          sony_arw6_floor_shift(int64_t(2) * ll.at(y, x) - pred, 1);
1328
0
    }
1329
0
  }
1330
1331
0
  SonyArw6Plane even_green(h, w);
1332
0
  for (int y = 0; y < h; y++)
1333
0
  {
1334
0
    const int next_y = y + 1 < h ? y + 1 : y;
1335
0
    even_green.at(y, 0) =
1336
0
        selected.at(y, 0) +
1337
0
        sony_arw6_floor_shift((odd_green.at(y, 0) + odd_green.at(next_y, 0)) *
1338
0
                                  int64_t(2),
1339
0
                              2);
1340
0
    for (int x = 1; x < w; x++)
1341
0
      even_green.at(y, x) =
1342
0
          selected.at(y, x) +
1343
0
          sony_arw6_floor_shift(odd_green.at(y, x - 1) +
1344
0
                                    odd_green.at(next_y, x - 1) +
1345
0
                                    odd_green.at(next_y, x) +
1346
0
                                    odd_green.at(y, x),
1347
0
                                2);
1348
0
  }
1349
1350
0
  SonyArw6Plane out(h, w * 2);
1351
0
  for (int y = 0; y < h; y++)
1352
0
    for (int x = 0; x < w; x++)
1353
0
    {
1354
0
      out.at(y, x * 2) = even_green.at(y, x);
1355
0
      out.at(y, x * 2 + 1) = odd_green.at(y, x);
1356
0
    }
1357
0
  return out;
1358
0
}
1359
1360
static inline ushort sony_arw6_sample_from_signed(int32_t x)
1361
0
{
1362
0
  int code = x + SONY_ARW6_INTERNAL_BIAS;
1363
0
  if (code < 0)
1364
0
    code = 0;
1365
0
  if (code > 4095)
1366
0
    code = 4095;
1367
0
  return sony_arw6_llvc3_lut_value(code);
1368
0
}
1369
1370
static inline int32_t sony_arw6_clamp_signed_code(int32_t x)
1371
0
{
1372
0
  int code = x + SONY_ARW6_INTERNAL_BIAS;
1373
0
  if (code < 0)
1374
0
    code = 0;
1375
0
  if (code > 4095)
1376
0
    code = 4095;
1377
0
  return code - SONY_ARW6_INTERNAL_BIAS;
1378
0
}
1379
1380
struct SonyArw6DecodedTile
1381
{
1382
  SonyArw6Plane green;
1383
  SonyArw6Plane red_residual;
1384
  SonyArw6Plane blue_residual;
1385
  SonyArw6Plane full_green;
1386
};
1387
1388
static SonyArw6DecodedTile sony_arw6_decode_stream_tile(const uchar *stream,
1389
                                                        uint32_t stream_size)
1390
0
{
1391
0
  SonyArw6StreamInfo header;
1392
0
  sony_arw6_require(sony_arw6_parse_stream_header(stream, stream_size, header));
1393
0
  const std::vector<SonyArw6DirectoryEntry> dir =
1394
0
      sony_arw6_parse_directory(stream, stream_size);
1395
0
  const int coded_height = header.logical_height;
1396
0
  sony_arw6_require(header.coded_width > 0 && coded_height > 0);
1397
0
  const int padded_height = sony_arw6_align_up(coded_height, 16);
1398
0
  const bool guarded_height = coded_height != padded_height;
1399
0
  const int low_rows = padded_height / 16;
1400
0
  const int low_start = guarded_height ? 1 : 0;
1401
0
  const int low_count = low_rows - low_start;
1402
1403
0
  std::vector<SonyArw6Plane> g0 =
1404
0
      sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1405
0
                                     header.coded_width, coded_height, 0, 0);
1406
0
  SonyArw6Plane green =
1407
0
      sony_arw6_integrate_type1(g0[0], low_start + low_count, 0);
1408
0
  if (low_start)
1409
0
  {
1410
0
    SonyArw6Plane cropped(low_count, green.cols);
1411
0
    for (int y = 0; y < low_count; y++)
1412
0
      memcpy(cropped.row(y), green.row(y + low_start),
1413
0
             sizeof(int32_t) * size_t(green.cols));
1414
0
    green = cropped;
1415
0
  }
1416
1417
0
  std::vector<SonyArw6Plane> r0 =
1418
0
      sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1419
0
                                     header.coded_width, coded_height, 0, 1);
1420
0
  SonyArw6Plane red_residual =
1421
0
      sony_arw6_integrate_type1(r0[0], low_start + low_count, 0);
1422
0
  if (low_start)
1423
0
  {
1424
0
    SonyArw6Plane cropped(low_count, red_residual.cols);
1425
0
    for (int y = 0; y < low_count; y++)
1426
0
      memcpy(cropped.row(y), red_residual.row(y + low_start),
1427
0
             sizeof(int32_t) * size_t(red_residual.cols));
1428
0
    red_residual = cropped;
1429
0
  }
1430
1431
0
  std::vector<SonyArw6Plane> b0 =
1432
0
      sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1433
0
                                     header.coded_width, coded_height, 0, 2);
1434
0
  SonyArw6Plane blue_residual =
1435
0
      sony_arw6_integrate_type1(b0[0], low_start + low_count, 0);
1436
0
  if (low_start)
1437
0
  {
1438
0
    SonyArw6Plane cropped(low_count, blue_residual.cols);
1439
0
    for (int y = 0; y < low_count; y++)
1440
0
      memcpy(cropped.row(y), blue_residual.row(y + low_start),
1441
0
             sizeof(int32_t) * size_t(blue_residual.cols));
1442
0
    blue_residual = cropped;
1443
0
  }
1444
1445
0
  for (int group = 1; group <= 3; group++)
1446
0
  {
1447
0
    const int edge_rows = group == 1 ? 0 : (group == 2 ? 1 : 2);
1448
0
    const bool color_odd_edge = group == 3;
1449
1450
0
    std::vector<SonyArw6Plane> planes =
1451
0
        sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1452
0
                                       header.coded_width, coded_height, group,
1453
0
                                       0);
1454
0
    if (guarded_height)
1455
0
    {
1456
0
      if (group == 1)
1457
0
        green = sony_arw6_synthesize_guard_group1(green, planes[0],
1458
0
                                                  planes[1], planes[2]);
1459
0
      else if (group == 2)
1460
0
        green = sony_arw6_synthesize_guard_group2(green, planes[0],
1461
0
                                                  planes[1], planes[2], false);
1462
0
      else
1463
0
        green = sony_arw6_synthesize_guard_group3(green, planes[0],
1464
0
                                                  planes[1], planes[2], false);
1465
0
    }
1466
0
    else
1467
0
      green = sony_arw6_synthesize_level_stride(green, planes[0], planes[1],
1468
0
                                                planes[2], edge_rows, false);
1469
1470
0
    planes = sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1471
0
                                            header.coded_width, coded_height,
1472
0
                                            group, 1);
1473
0
    if (guarded_height)
1474
0
    {
1475
0
      if (group == 1)
1476
0
        red_residual = sony_arw6_synthesize_guard_group1(
1477
0
            red_residual, planes[0], planes[1], planes[2]);
1478
0
      else if (group == 2)
1479
0
        red_residual = sony_arw6_synthesize_guard_group2(
1480
0
            red_residual, planes[0], planes[1], planes[2], false);
1481
0
      else
1482
0
        red_residual = sony_arw6_synthesize_guard_group3(
1483
0
            red_residual, planes[0], planes[1], planes[2], color_odd_edge);
1484
0
    }
1485
0
    else
1486
0
      red_residual = sony_arw6_synthesize_level_stride(
1487
0
          red_residual, planes[0], planes[1], planes[2], edge_rows,
1488
0
          color_odd_edge);
1489
1490
0
    planes = sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1491
0
                                            header.coded_width, coded_height,
1492
0
                                            group, 2);
1493
0
    if (guarded_height)
1494
0
    {
1495
0
      if (group == 1)
1496
0
        blue_residual = sony_arw6_synthesize_guard_group1(
1497
0
            blue_residual, planes[0], planes[1], planes[2]);
1498
0
      else if (group == 2)
1499
0
        blue_residual = sony_arw6_synthesize_guard_group2(
1500
0
            blue_residual, planes[0], planes[1], planes[2], false);
1501
0
      else
1502
0
        blue_residual = sony_arw6_synthesize_guard_group3(
1503
0
            blue_residual, planes[0], planes[1], planes[2], color_odd_edge);
1504
0
    }
1505
0
    else
1506
0
      blue_residual = sony_arw6_synthesize_level_stride(
1507
0
          blue_residual, planes[0], planes[1], planes[2], edge_rows,
1508
0
          color_odd_edge);
1509
0
  }
1510
1511
0
  std::vector<SonyArw6Plane> g4 =
1512
0
      sony_arw6_decode_packet_arrays(stream, stream_size, dir,
1513
0
                                     header.coded_width, coded_height, 4, 0);
1514
0
  SonyArw6Plane full_green =
1515
0
      sony_arw6_final_green(green, g4[0], guarded_height ? 2 : 4);
1516
1517
0
  SonyArw6DecodedTile out;
1518
0
  out.green = green;
1519
0
  out.red_residual = red_residual;
1520
0
  out.blue_residual = blue_residual;
1521
0
  out.full_green = full_green;
1522
0
  return out;
1523
0
}
1524
1525
} // namespace
1526
1527
void LibRaw::sony_arw6_load_raw()
1528
0
{
1529
0
  if (raw_width <= 0 || raw_height <= 0 || (raw_width & 15) ||
1530
0
      (raw_height & 1))
1531
0
    throw LIBRAW_EXCEPTION_IO_BADFILE;
1532
0
  if (!raw_image)
1533
0
    throw LIBRAW_EXCEPTION_IO_BADFILE;
1534
0
  if (data_size < SONY_ARW6_STREAM_OFFSET + 0x80 ||
1535
0
      data_size > 1024LL * 1024LL * 1024LL)
1536
0
    throw LIBRAW_EXCEPTION_IO_CORRUPT;
1537
0
  if (data_size > INT64(imgdata.rawparams.max_raw_memory_mb) *
1538
0
                      INT64(1024 * 1024))
1539
0
    throw LIBRAW_EXCEPTION_ALLOC;
1540
1541
0
  std::vector<uchar> strip(size_t(data_size));
1542
0
  ifp->seek(data_offset, SEEK_SET);
1543
0
  const int readed = ifp->read(&strip[0], 1, size_t(data_size));
1544
0
  if (readed != data_size)
1545
0
    throw LIBRAW_EXCEPTION_IO_EOF;
1546
1547
0
  const std::vector<SonyArw6StreamInfo> streams =
1548
0
      sony_arw6_find_streams(strip, raw_width, raw_height);
1549
0
  sony_arw6_require(!streams.empty());
1550
1551
0
  INT64 max_tile_pixels = 0;
1552
0
  for (size_t si = 0; si < streams.size(); si++)
1553
0
  {
1554
0
    const SonyArw6StreamInfo &s = streams[si];
1555
0
    max_tile_pixels =
1556
0
        std::max(max_tile_pixels, INT64(s.coded_width) * s.logical_height);
1557
0
  }
1558
0
  const INT64 raw_bytes = INT64(raw_width) * raw_height *
1559
0
                          INT64(sizeof(raw_image[0]));
1560
0
  const INT64 working_bytes =
1561
0
      raw_bytes + data_size +
1562
0
      max_tile_pixels * SONY_ARW6_WORKING_BYTES_PER_TILE_PIXEL;
1563
0
  if (working_bytes > sony_arw6_memory_limit_bytes(
1564
0
                          imgdata.rawparams.max_raw_memory_mb))
1565
0
    throw LIBRAW_EXCEPTION_ALLOC;
1566
1567
0
  for (size_t si = 0; si < streams.size(); si++)
1568
0
  {
1569
0
    checkCancel();
1570
0
    const SonyArw6StreamInfo &s = streams[si];
1571
0
    sony_arw6_require(s.offset <= strip.size() &&
1572
0
                      s.length <= strip.size() - s.offset);
1573
0
    sony_arw6_require(s.tile_x >= 0 && s.tile_y >= 0 &&
1574
0
                      s.tile_w == s.coded_width &&
1575
0
                      s.tile_h == s.logical_height &&
1576
0
                      s.tile_x + s.tile_w <= raw_width &&
1577
0
                      s.tile_y + s.tile_h <= raw_height);
1578
1579
0
    SonyArw6DecodedTile tile =
1580
0
        sony_arw6_decode_stream_tile(&strip[s.offset], s.length);
1581
1582
0
    const int half_h = s.logical_height / 2;
1583
0
    const int half_w = s.coded_width / 2;
1584
0
    sony_arw6_require(tile.full_green.rows >= half_h &&
1585
0
                      tile.full_green.cols == s.coded_width);
1586
0
    sony_arw6_require(tile.green.rows >= half_h &&
1587
0
                      tile.green.cols == half_w);
1588
0
    sony_arw6_require(tile.red_residual.rows >= half_h &&
1589
0
                      tile.red_residual.cols == half_w);
1590
0
    sony_arw6_require(tile.blue_residual.rows >= half_h &&
1591
0
                      tile.blue_residual.cols == half_w);
1592
1593
0
    for (int y = 0; y < half_h; y++)
1594
0
    {
1595
0
      checkCancel();
1596
0
      ushort *row0 =
1597
0
          raw_image + size_t(s.tile_y + y * 2) * raw_width + s.tile_x;
1598
0
      ushort *row1 =
1599
0
          raw_image + size_t(s.tile_y + y * 2 + 1) * raw_width + s.tile_x;
1600
0
      for (int x = 0; x < half_w; x++)
1601
0
      {
1602
0
        const int32_t g0 =
1603
0
            sony_arw6_clamp_signed_code(tile.full_green.at(y, x * 2));
1604
0
        const int32_t g1 =
1605
0
            sony_arw6_clamp_signed_code(tile.full_green.at(y, x * 2 + 1));
1606
0
        const int32_t gavg = sony_arw6_floor_shift(int64_t(g0) + g1, 1);
1607
0
        row0[x * 2] = sony_arw6_sample_from_signed(
1608
0
            gavg + 2 * tile.red_residual.at(y, x));
1609
0
        row0[x * 2 + 1] =
1610
0
            sony_arw6_sample_from_signed(tile.full_green.at(y, x * 2 + 1));
1611
0
        row1[x * 2] =
1612
0
            sony_arw6_sample_from_signed(tile.full_green.at(y, x * 2));
1613
0
        row1[x * 2 + 1] = sony_arw6_sample_from_signed(
1614
0
            gavg + 2 * tile.blue_residual.at(y, x));
1615
0
      }
1616
0
    }
1617
0
  }
1618
0
}