Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: third_party/libwebp/dsp/cost.c

Issue 1546003002: libwebp: update to 0.5.0 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase around clang-cl fix Created 4 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2011 Google Inc. All Rights Reserved. 1 // Copyright 2014 Google Inc. All Rights Reserved.
2 // 2 //
3 // Use of this source code is governed by a BSD-style license 3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source 4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found 5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may 6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree. 7 // be found in the AUTHORS file in the root of the source tree.
8 // ----------------------------------------------------------------------------- 8 // -----------------------------------------------------------------------------
9 // 9 //
10 // Cost tables for level and modes
11 //
12 // Author: Skal (pascal.massimino@gmail.com) 10 // Author: Skal (pascal.massimino@gmail.com)
13 11
14 #include "./cost.h" 12 #include "./dsp.h"
13 #include "../enc/cost.h"
15 14
16 //------------------------------------------------------------------------------ 15 //------------------------------------------------------------------------------
17 // Boolean-cost cost table 16 // Boolean-cost cost table
18 17
19 const uint16_t VP8EntropyCost[256] = { 18 const uint16_t VP8EntropyCost[256] = {
20 1792, 1792, 1792, 1536, 1536, 1408, 1366, 1280, 1280, 1216, 19 1792, 1792, 1792, 1536, 1536, 1408, 1366, 1280, 1280, 1216,
21 1178, 1152, 1110, 1076, 1061, 1024, 1024, 992, 968, 951, 20 1178, 1152, 1110, 1076, 1061, 1024, 1024, 992, 968, 951,
22 939, 911, 896, 878, 871, 854, 838, 820, 811, 794, 21 939, 911, 896, 878, 871, 854, 838, 820, 811, 794,
23 786, 768, 768, 752, 740, 732, 720, 709, 704, 690, 22 786, 768, 768, 752, 740, 732, 720, 709, 704, 690,
24 683, 672, 666, 655, 647, 640, 631, 622, 615, 607, 23 683, 672, 666, 655, 647, 640, 631, 622, 615, 607,
(...skipping 16 matching lines...) Expand all
41 74, 73, 71, 69, 67, 66, 64, 63, 61, 59, 40 74, 73, 71, 69, 67, 66, 64, 63, 61, 59,
42 57, 55, 54, 52, 51, 49, 47, 46, 44, 43, 41 57, 55, 54, 52, 51, 49, 47, 46, 44, 43,
43 41, 40, 38, 36, 35, 33, 32, 30, 29, 27, 42 41, 40, 38, 36, 35, 33, 32, 30, 29, 27,
44 25, 24, 22, 21, 19, 18, 16, 15, 13, 12, 43 25, 24, 22, 21, 19, 18, 16, 15, 13, 12,
45 10, 9, 7, 6, 4, 3 44 10, 9, 7, 6, 4, 3
46 }; 45 };
47 46
48 //------------------------------------------------------------------------------ 47 //------------------------------------------------------------------------------
49 // Level cost tables 48 // Level cost tables
50 49
51 // For each given level, the following table gives the pattern of contexts to
52 // use for coding it (in [][0]) as well as the bit value to use for each
53 // context (in [][1]).
54 const uint16_t VP8LevelCodes[MAX_VARIABLE_LEVEL][2] = {
55 {0x001, 0x000}, {0x007, 0x001}, {0x00f, 0x005},
56 {0x00f, 0x00d}, {0x033, 0x003}, {0x033, 0x003}, {0x033, 0x023},
57 {0x033, 0x023}, {0x033, 0x023}, {0x033, 0x023}, {0x0d3, 0x013},
58 {0x0d3, 0x013}, {0x0d3, 0x013}, {0x0d3, 0x013}, {0x0d3, 0x013},
59 {0x0d3, 0x013}, {0x0d3, 0x013}, {0x0d3, 0x013}, {0x0d3, 0x093},
60 {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093},
61 {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093},
62 {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093},
63 {0x0d3, 0x093}, {0x0d3, 0x093}, {0x0d3, 0x093}, {0x153, 0x053},
64 {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
65 {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
66 {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
67 {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
68 {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
69 {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
70 {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053},
71 {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x053}, {0x153, 0x153}
72 };
73
74 // fixed costs for coding levels, deduce from the coding tree. 50 // fixed costs for coding levels, deduce from the coding tree.
75 // This is only the part that doesn't depend on the probability state. 51 // This is only the part that doesn't depend on the probability state.
76 const uint16_t VP8LevelFixedCosts[MAX_LEVEL + 1] = { 52 const uint16_t VP8LevelFixedCosts[MAX_LEVEL + 1] = {
77 0, 256, 256, 256, 256, 432, 618, 630, 53 0, 256, 256, 256, 256, 432, 618, 630,
78 731, 640, 640, 828, 901, 948, 1021, 1101, 54 731, 640, 640, 828, 901, 948, 1021, 1101,
79 1174, 1221, 1294, 1042, 1085, 1115, 1158, 1202, 55 1174, 1221, 1294, 1042, 1085, 1115, 1158, 1202,
80 1245, 1275, 1318, 1337, 1380, 1410, 1453, 1497, 56 1245, 1275, 1318, 1337, 1380, 1410, 1453, 1497,
81 1540, 1570, 1613, 1280, 1295, 1317, 1332, 1358, 57 1540, 1570, 1613, 1280, 1295, 1317, 1332, 1358,
82 1373, 1395, 1410, 1454, 1469, 1491, 1506, 1532, 58 1373, 1395, 1410, 1454, 1469, 1491, 1506, 1532,
83 1547, 1569, 1584, 1601, 1616, 1638, 1653, 1679, 59 1547, 1569, 1584, 1601, 1616, 1638, 1653, 1679,
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 7395, 7404, 7410, 7205, 7211, 7220, 7226, 7237, 301 7395, 7404, 7410, 7205, 7211, 7220, 7226, 7237,
326 7243, 7252, 7258, 7278, 7284, 7293, 7299, 7310, 302 7243, 7252, 7258, 7278, 7284, 7293, 7299, 7310,
327 7316, 7325, 7331, 7356, 7362, 7371, 7377, 7388, 303 7316, 7325, 7331, 7356, 7362, 7371, 7377, 7388,
328 7394, 7403, 7409, 7429, 7435, 7444, 7450, 7461, 304 7394, 7403, 7409, 7429, 7435, 7444, 7450, 7461,
329 7467, 7476, 7482, 7505, 7511, 7520, 7526, 7537, 305 7467, 7476, 7482, 7505, 7511, 7520, 7526, 7537,
330 7543, 7552, 7558, 7578, 7584, 7593, 7599, 7610, 306 7543, 7552, 7558, 7578, 7584, 7593, 7599, 7610,
331 7616, 7625, 7631, 7656, 7662, 7671, 7677, 7688, 307 7616, 7625, 7631, 7656, 7662, 7671, 7677, 7688,
332 7694, 7703, 7709, 7729, 7735, 7744, 7750, 7761 308 7694, 7703, 7709, 7729, 7735, 7744, 7750, 7761
333 }; 309 };
334 310
335 static int VariableLevelCost(int level, const uint8_t probas[NUM_PROBAS]) { 311 //------------------------------------------------------------------------------
336 int pattern = VP8LevelCodes[level - 1][0]; 312 // Tables for level coding
337 int bits = VP8LevelCodes[level - 1][1];
338 int cost = 0;
339 int i;
340 for (i = 2; pattern; ++i) {
341 if (pattern & 1) {
342 cost += VP8BitCost(bits & 1, probas[i]);
343 }
344 bits >>= 1;
345 pattern >>= 1;
346 }
347 return cost;
348 }
349 313
350 //------------------------------------------------------------------------------ 314 const uint8_t VP8EncBands[16 + 1] = {
351 // Pre-calc level costs once for all 315 0, 1, 2, 3, 6, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7,
352 316 0 // sentinel
353 void VP8CalculateLevelCosts(VP8Proba* const proba) {
354 int ctype, band, ctx;
355
356 if (!proba->dirty_) return; // nothing to do.
357
358 for (ctype = 0; ctype < NUM_TYPES; ++ctype) {
359 for (band = 0; band < NUM_BANDS; ++band) {
360 for (ctx = 0; ctx < NUM_CTX; ++ctx) {
361 const uint8_t* const p = proba->coeffs_[ctype][band][ctx];
362 uint16_t* const table = proba->level_cost_[ctype][band][ctx];
363 const int cost0 = (ctx > 0) ? VP8BitCost(1, p[0]) : 0;
364 const int cost_base = VP8BitCost(1, p[1]) + cost0;
365 int v;
366 table[0] = VP8BitCost(0, p[1]) + cost0;
367 for (v = 1; v <= MAX_VARIABLE_LEVEL; ++v) {
368 table[v] = cost_base + VariableLevelCost(v, p);
369 }
370 // Starting at level 67 and up, the variable part of the cost is
371 // actually constant.
372 }
373 }
374 }
375 proba->dirty_ = 0;
376 }
377
378 //------------------------------------------------------------------------------
379 // Mode cost tables.
380
381 // These are the fixed probabilities (in the coding trees) turned into bit-cost
382 // by calling VP8BitCost().
383 const uint16_t VP8FixedCostsUV[4] = { 302, 984, 439, 642 };
384 // note: these values include the fixed VP8BitCost(1, 145) mode selection cost.
385 const uint16_t VP8FixedCostsI16[4] = { 663, 919, 872, 919 };
386 const uint16_t VP8FixedCostsI4[NUM_BMODES][NUM_BMODES][NUM_BMODES] = {
387 { { 40, 1151, 1723, 1874, 2103, 2019, 1628, 1777, 2226, 2137 },
388 { 192, 469, 1296, 1308, 1849, 1794, 1781, 1703, 1713, 1522 },
389 { 142, 910, 762, 1684, 1849, 1576, 1460, 1305, 1801, 1657 },
390 { 559, 641, 1370, 421, 1182, 1569, 1612, 1725, 863, 1007 },
391 { 299, 1059, 1256, 1108, 636, 1068, 1581, 1883, 869, 1142 },
392 { 277, 1111, 707, 1362, 1089, 672, 1603, 1541, 1545, 1291 },
393 { 214, 781, 1609, 1303, 1632, 2229, 726, 1560, 1713, 918 },
394 { 152, 1037, 1046, 1759, 1983, 2174, 1358, 742, 1740, 1390 },
395 { 512, 1046, 1420, 753, 752, 1297, 1486, 1613, 460, 1207 },
396 { 424, 827, 1362, 719, 1462, 1202, 1199, 1476, 1199, 538 } },
397 { { 240, 402, 1134, 1491, 1659, 1505, 1517, 1555, 1979, 2099 },
398 { 467, 242, 960, 1232, 1714, 1620, 1834, 1570, 1676, 1391 },
399 { 500, 455, 463, 1507, 1699, 1282, 1564, 982, 2114, 2114 },
400 { 672, 643, 1372, 331, 1589, 1667, 1453, 1938, 996, 876 },
401 { 458, 783, 1037, 911, 738, 968, 1165, 1518, 859, 1033 },
402 { 504, 815, 504, 1139, 1219, 719, 1506, 1085, 1268, 1268 },
403 { 333, 630, 1445, 1239, 1883, 3672, 799, 1548, 1865, 598 },
404 { 399, 644, 746, 1342, 1856, 1350, 1493, 613, 1855, 1015 },
405 { 622, 749, 1205, 608, 1066, 1408, 1290, 1406, 546, 971 },
406 { 500, 753, 1041, 668, 1230, 1617, 1297, 1425, 1383, 523 } },
407 { { 394, 553, 523, 1502, 1536, 981, 1608, 1142, 1666, 2181 },
408 { 655, 430, 375, 1411, 1861, 1220, 1677, 1135, 1978, 1553 },
409 { 690, 640, 245, 1954, 2070, 1194, 1528, 982, 1972, 2232 },
410 { 559, 834, 741, 867, 1131, 980, 1225, 852, 1092, 784 },
411 { 690, 875, 516, 959, 673, 894, 1056, 1190, 1528, 1126 },
412 { 740, 951, 384, 1277, 1177, 492, 1579, 1155, 1846, 1513 },
413 { 323, 775, 1062, 1776, 3062, 1274, 813, 1188, 1372, 655 },
414 { 488, 971, 484, 1767, 1515, 1775, 1115, 503, 1539, 1461 },
415 { 740, 1006, 998, 709, 851, 1230, 1337, 788, 741, 721 },
416 { 522, 1073, 573, 1045, 1346, 887, 1046, 1146, 1203, 697 } },
417 { { 105, 864, 1442, 1009, 1934, 1840, 1519, 1920, 1673, 1579 },
418 { 534, 305, 1193, 683, 1388, 2164, 1802, 1894, 1264, 1170 },
419 { 305, 518, 877, 1108, 1426, 3215, 1425, 1064, 1320, 1242 },
420 { 683, 732, 1927, 257, 1493, 2048, 1858, 1552, 1055, 947 },
421 { 394, 814, 1024, 660, 959, 1556, 1282, 1289, 893, 1047 },
422 { 528, 615, 996, 940, 1201, 635, 1094, 2515, 803, 1358 },
423 { 347, 614, 1609, 1187, 3133, 1345, 1007, 1339, 1017, 667 },
424 { 218, 740, 878, 1605, 3650, 3650, 1345, 758, 1357, 1617 },
425 { 672, 750, 1541, 558, 1257, 1599, 1870, 2135, 402, 1087 },
426 { 592, 684, 1161, 430, 1092, 1497, 1475, 1489, 1095, 822 } },
427 { { 228, 1056, 1059, 1368, 752, 982, 1512, 1518, 987, 1782 },
428 { 494, 514, 818, 942, 965, 892, 1610, 1356, 1048, 1363 },
429 { 512, 648, 591, 1042, 761, 991, 1196, 1454, 1309, 1463 },
430 { 683, 749, 1043, 676, 841, 1396, 1133, 1138, 654, 939 },
431 { 622, 1101, 1126, 994, 361, 1077, 1203, 1318, 877, 1219 },
432 { 631, 1068, 857, 1650, 651, 477, 1650, 1419, 828, 1170 },
433 { 555, 727, 1068, 1335, 3127, 1339, 820, 1331, 1077, 429 },
434 { 504, 879, 624, 1398, 889, 889, 1392, 808, 891, 1406 },
435 { 683, 1602, 1289, 977, 578, 983, 1280, 1708, 406, 1122 },
436 { 399, 865, 1433, 1070, 1072, 764, 968, 1477, 1223, 678 } },
437 { { 333, 760, 935, 1638, 1010, 529, 1646, 1410, 1472, 2219 },
438 { 512, 494, 750, 1160, 1215, 610, 1870, 1868, 1628, 1169 },
439 { 572, 646, 492, 1934, 1208, 603, 1580, 1099, 1398, 1995 },
440 { 786, 789, 942, 581, 1018, 951, 1599, 1207, 731, 768 },
441 { 690, 1015, 672, 1078, 582, 504, 1693, 1438, 1108, 2897 },
442 { 768, 1267, 571, 2005, 1243, 244, 2881, 1380, 1786, 1453 },
443 { 452, 899, 1293, 903, 1311, 3100, 465, 1311, 1319, 813 },
444 { 394, 927, 942, 1103, 1358, 1104, 946, 593, 1363, 1109 },
445 { 559, 1005, 1007, 1016, 658, 1173, 1021, 1164, 623, 1028 },
446 { 564, 796, 632, 1005, 1014, 863, 2316, 1268, 938, 764 } },
447 { { 266, 606, 1098, 1228, 1497, 1243, 948, 1030, 1734, 1461 },
448 { 366, 585, 901, 1060, 1407, 1247, 876, 1134, 1620, 1054 },
449 { 452, 565, 542, 1729, 1479, 1479, 1016, 886, 2938, 1150 },
450 { 555, 1088, 1533, 950, 1354, 895, 834, 1019, 1021, 496 },
451 { 704, 815, 1193, 971, 973, 640, 1217, 2214, 832, 578 },
452 { 672, 1245, 579, 871, 875, 774, 872, 1273, 1027, 949 },
453 { 296, 1134, 2050, 1784, 1636, 3425, 442, 1550, 2076, 722 },
454 { 342, 982, 1259, 1846, 1848, 1848, 622, 568, 1847, 1052 },
455 { 555, 1064, 1304, 828, 746, 1343, 1075, 1329, 1078, 494 },
456 { 288, 1167, 1285, 1174, 1639, 1639, 833, 2254, 1304, 509 } },
457 { { 342, 719, 767, 1866, 1757, 1270, 1246, 550, 1746, 2151 },
458 { 483, 653, 694, 1509, 1459, 1410, 1218, 507, 1914, 1266 },
459 { 488, 757, 447, 2979, 1813, 1268, 1654, 539, 1849, 2109 },
460 { 522, 1097, 1085, 851, 1365, 1111, 851, 901, 961, 605 },
461 { 709, 716, 841, 728, 736, 945, 941, 862, 2845, 1057 },
462 { 512, 1323, 500, 1336, 1083, 681, 1342, 717, 1604, 1350 },
463 { 452, 1155, 1372, 1900, 1501, 3290, 311, 944, 1919, 922 },
464 { 403, 1520, 977, 2132, 1733, 3522, 1076, 276, 3335, 1547 },
465 { 559, 1374, 1101, 615, 673, 2462, 974, 795, 984, 984 },
466 { 547, 1122, 1062, 812, 1410, 951, 1140, 622, 1268, 651 } },
467 { { 165, 982, 1235, 938, 1334, 1366, 1659, 1578, 964, 1612 },
468 { 592, 422, 925, 847, 1139, 1112, 1387, 2036, 861, 1041 },
469 { 403, 837, 732, 770, 941, 1658, 1250, 809, 1407, 1407 },
470 { 896, 874, 1071, 381, 1568, 1722, 1437, 2192, 480, 1035 },
471 { 640, 1098, 1012, 1032, 684, 1382, 1581, 2106, 416, 865 },
472 { 559, 1005, 819, 914, 710, 770, 1418, 920, 838, 1435 },
473 { 415, 1258, 1245, 870, 1278, 3067, 770, 1021, 1287, 522 },
474 { 406, 990, 601, 1009, 1265, 1265, 1267, 759, 1017, 1277 },
475 { 968, 1182, 1329, 788, 1032, 1292, 1705, 1714, 203, 1403 },
476 { 732, 877, 1279, 471, 901, 1161, 1545, 1294, 755, 755 } },
477 { { 111, 931, 1378, 1185, 1933, 1648, 1148, 1714, 1873, 1307 },
478 { 406, 414, 1030, 1023, 1910, 1404, 1313, 1647, 1509, 793 },
479 { 342, 640, 575, 1088, 1241, 1349, 1161, 1350, 1756, 1502 },
480 { 559, 766, 1185, 357, 1682, 1428, 1329, 1897, 1219, 802 },
481 { 473, 909, 1164, 771, 719, 2508, 1427, 1432, 722, 782 },
482 { 342, 892, 785, 1145, 1150, 794, 1296, 1550, 973, 1057 },
483 { 208, 1036, 1326, 1343, 1606, 3395, 815, 1455, 1618, 712 },
484 { 228, 928, 890, 1046, 3499, 1711, 994, 829, 1720, 1318 },
485 { 768, 724, 1058, 636, 991, 1075, 1319, 1324, 616, 825 },
486 { 305, 1167, 1358, 899, 1587, 1587, 987, 1988, 1332, 501 } }
487 }; 317 };
488 318
489 //------------------------------------------------------------------------------ 319 //------------------------------------------------------------------------------
490 // Mode costs 320 // Mode costs
491 321
492 static int GetResidualCost(int ctx0, const VP8Residual* const res) { 322 static int GetResidualCost(int ctx0, const VP8Residual* const res) {
493 int n = res->first; 323 int n = res->first;
494 // should be prob[VP8EncBands[n]], but it's equivalent for n=0 or 1 324 // should be prob[VP8EncBands[n]], but it's equivalent for n=0 or 1
495 const int p0 = res->prob[n][ctx0][0]; 325 const int p0 = res->prob[n][ctx0][0];
496 const uint16_t* t = res->cost[n][ctx0]; 326 CostArrayPtr const costs = res->costs;
327 const uint16_t* t = costs[n][ctx0];
497 // bit_cost(1, p0) is already incorporated in t[] tables, but only if ctx != 0 328 // bit_cost(1, p0) is already incorporated in t[] tables, but only if ctx != 0
498 // (as required by the syntax). For ctx0 == 0, we need to add it here or it'll 329 // (as required by the syntax). For ctx0 == 0, we need to add it here or it'll
499 // be missing during the loop. 330 // be missing during the loop.
500 int cost = (ctx0 == 0) ? VP8BitCost(1, p0) : 0; 331 int cost = (ctx0 == 0) ? VP8BitCost(1, p0) : 0;
501 332
502 if (res->last < 0) { 333 if (res->last < 0) {
503 return VP8BitCost(0, p0); 334 return VP8BitCost(0, p0);
504 } 335 }
505 for (; n < res->last; ++n) { 336 for (; n < res->last; ++n) {
506 const int v = abs(res->coeffs[n]); 337 const int v = abs(res->coeffs[n]);
507 const int b = VP8EncBands[n + 1];
508 const int ctx = (v >= 2) ? 2 : v; 338 const int ctx = (v >= 2) ? 2 : v;
509 cost += VP8LevelCost(t, v); 339 cost += VP8LevelCost(t, v);
510 t = res->cost[b][ctx]; 340 t = costs[n + 1][ctx];
511 } 341 }
512 // Last coefficient is always non-zero 342 // Last coefficient is always non-zero
513 { 343 {
514 const int v = abs(res->coeffs[n]); 344 const int v = abs(res->coeffs[n]);
515 assert(v != 0); 345 assert(v != 0);
516 cost += VP8LevelCost(t, v); 346 cost += VP8LevelCost(t, v);
517 if (n < 15) { 347 if (n < 15) {
518 const int b = VP8EncBands[n + 1]; 348 const int b = VP8EncBands[n + 1];
519 const int ctx = (v == 1) ? 1 : 2; 349 const int ctx = (v == 1) ? 1 : 2;
520 const int last_p0 = res->prob[b][ctx][0]; 350 const int last_p0 = res->prob[b][ctx][0];
521 cost += VP8BitCost(0, last_p0); 351 cost += VP8BitCost(0, last_p0);
522 } 352 }
523 } 353 }
524 return cost; 354 return cost;
525 } 355 }
526 356
527 //------------------------------------------------------------------------------
528 // init function
529
530 #if defined(WEBP_USE_MIPS32)
531 extern int VP8GetResidualCostMIPS32(int ctx0, const VP8Residual* const res);
532 #endif // WEBP_USE_MIPS32
533
534 // TODO(skal): this, and GetResidualCost(), should probably go somewhere
535 // under src/dsp/ at some point.
536 VP8GetResidualCostFunc VP8GetResidualCost;
537
538 void VP8GetResidualCostInit(void) {
539 VP8GetResidualCost = GetResidualCost;
540 if (VP8GetCPUInfo != NULL) {
541 #if defined(WEBP_USE_MIPS32)
542 if (VP8GetCPUInfo(kMIPS32)) {
543 VP8GetResidualCost = VP8GetResidualCostMIPS32;
544 }
545 #endif
546 }
547 }
548
549 //------------------------------------------------------------------------------
550 // helper functions for residuals struct VP8Residual.
551
552 void VP8InitResidual(int first, int coeff_type,
553 VP8Encoder* const enc, VP8Residual* const res) {
554 res->coeff_type = coeff_type;
555 res->prob = enc->proba_.coeffs_[coeff_type];
556 res->stats = enc->proba_.stats_[coeff_type];
557 res->cost = enc->proba_.level_cost_[coeff_type];
558 res->first = first;
559 }
560
561 static void SetResidualCoeffs(const int16_t* const coeffs, 357 static void SetResidualCoeffs(const int16_t* const coeffs,
562 VP8Residual* const res) { 358 VP8Residual* const res) {
563 int n; 359 int n;
564 res->last = -1; 360 res->last = -1;
565 assert(res->first == 0 || coeffs[0] == 0); 361 assert(res->first == 0 || coeffs[0] == 0);
566 for (n = 15; n >= 0; --n) { 362 for (n = 15; n >= 0; --n) {
567 if (coeffs[n]) { 363 if (coeffs[n]) {
568 res->last = n; 364 res->last = n;
569 break; 365 break;
570 } 366 }
571 } 367 }
572 res->coeffs = coeffs; 368 res->coeffs = coeffs;
573 } 369 }
574 370
575 //------------------------------------------------------------------------------ 371 //------------------------------------------------------------------------------
576 // init function 372 // init function
577 373
578 #if defined(WEBP_USE_SSE2) 374 VP8GetResidualCostFunc VP8GetResidualCost;
579 extern void VP8SetResidualCoeffsSSE2(const int16_t* const coeffs,
580 VP8Residual* const res);
581 #endif // WEBP_USE_SSE2
582
583 VP8SetResidualCoeffsFunc VP8SetResidualCoeffs; 375 VP8SetResidualCoeffsFunc VP8SetResidualCoeffs;
584 376
585 void VP8SetResidualCoeffsInit(void) { 377 extern void VP8EncDspCostInitMIPS32(void);
378 extern void VP8EncDspCostInitMIPSdspR2(void);
379 extern void VP8EncDspCostInitSSE2(void);
380
381 static volatile VP8CPUInfo cost_last_cpuinfo_used =
382 (VP8CPUInfo)&cost_last_cpuinfo_used;
383
384 WEBP_TSAN_IGNORE_FUNCTION void VP8EncDspCostInit(void) {
385 if (cost_last_cpuinfo_used == VP8GetCPUInfo) return;
386
387 VP8GetResidualCost = GetResidualCost;
586 VP8SetResidualCoeffs = SetResidualCoeffs; 388 VP8SetResidualCoeffs = SetResidualCoeffs;
389
390 // If defined, use CPUInfo() to overwrite some pointers with faster versions.
587 if (VP8GetCPUInfo != NULL) { 391 if (VP8GetCPUInfo != NULL) {
392 #if defined(WEBP_USE_MIPS32)
393 if (VP8GetCPUInfo(kMIPS32)) {
394 VP8EncDspCostInitMIPS32();
395 }
396 #endif
397 #if defined(WEBP_USE_MIPS_DSP_R2)
398 if (VP8GetCPUInfo(kMIPSdspR2)) {
399 VP8EncDspCostInitMIPSdspR2();
400 }
401 #endif
588 #if defined(WEBP_USE_SSE2) 402 #if defined(WEBP_USE_SSE2)
589 if (VP8GetCPUInfo(kSSE2)) { 403 if (VP8GetCPUInfo(kSSE2)) {
590 VP8SetResidualCoeffs = VP8SetResidualCoeffsSSE2; 404 VP8EncDspCostInitSSE2();
591 } 405 }
592 #endif 406 #endif
593 } 407 }
408
409 cost_last_cpuinfo_used = VP8GetCPUInfo;
594 } 410 }
595 411
596 //------------------------------------------------------------------------------ 412 //------------------------------------------------------------------------------
597 // Mode costs
598
599 int VP8GetCostLuma4(VP8EncIterator* const it, const int16_t levels[16]) {
600 const int x = (it->i4_ & 3), y = (it->i4_ >> 2);
601 VP8Residual res;
602 VP8Encoder* const enc = it->enc_;
603 int R = 0;
604 int ctx;
605
606 VP8InitResidual(0, 3, enc, &res);
607 ctx = it->top_nz_[x] + it->left_nz_[y];
608 VP8SetResidualCoeffs(levels, &res);
609 R += VP8GetResidualCost(ctx, &res);
610 return R;
611 }
612
613 int VP8GetCostLuma16(VP8EncIterator* const it, const VP8ModeScore* const rd) {
614 VP8Residual res;
615 VP8Encoder* const enc = it->enc_;
616 int x, y;
617 int R = 0;
618
619 VP8IteratorNzToBytes(it); // re-import the non-zero context
620
621 // DC
622 VP8InitResidual(0, 1, enc, &res);
623 VP8SetResidualCoeffs(rd->y_dc_levels, &res);
624 R += VP8GetResidualCost(it->top_nz_[8] + it->left_nz_[8], &res);
625
626 // AC
627 VP8InitResidual(1, 0, enc, &res);
628 for (y = 0; y < 4; ++y) {
629 for (x = 0; x < 4; ++x) {
630 const int ctx = it->top_nz_[x] + it->left_nz_[y];
631 VP8SetResidualCoeffs(rd->y_ac_levels[x + y * 4], &res);
632 R += VP8GetResidualCost(ctx, &res);
633 it->top_nz_[x] = it->left_nz_[y] = (res.last >= 0);
634 }
635 }
636 return R;
637 }
638
639 int VP8GetCostUV(VP8EncIterator* const it, const VP8ModeScore* const rd) {
640 VP8Residual res;
641 VP8Encoder* const enc = it->enc_;
642 int ch, x, y;
643 int R = 0;
644
645 VP8IteratorNzToBytes(it); // re-import the non-zero context
646
647 VP8InitResidual(0, 2, enc, &res);
648 for (ch = 0; ch <= 2; ch += 2) {
649 for (y = 0; y < 2; ++y) {
650 for (x = 0; x < 2; ++x) {
651 const int ctx = it->top_nz_[4 + ch + x] + it->left_nz_[4 + ch + y];
652 VP8SetResidualCoeffs(rd->uv_levels[ch * 2 + x + y * 2], &res);
653 R += VP8GetResidualCost(ctx, &res);
654 it->top_nz_[4 + ch + x] = it->left_nz_[4 + ch + y] = (res.last >= 0);
655 }
656 }
657 }
658 return R;
659 }
660
661
662 //------------------------------------------------------------------------------
663 // Recording of token probabilities.
664
665 // Record proba context used
666 static int Record(int bit, proba_t* const stats) {
667 proba_t p = *stats;
668 if (p >= 0xffff0000u) { // an overflow is inbound.
669 p = ((p + 1u) >> 1) & 0x7fff7fffu; // -> divide the stats by 2.
670 }
671 // record bit count (lower 16 bits) and increment total count (upper 16 bits).
672 p += 0x00010000u + bit;
673 *stats = p;
674 return bit;
675 }
676
677 // We keep the table-free variant around for reference, in case.
678 #define USE_LEVEL_CODE_TABLE
679
680 // Simulate block coding, but only record statistics.
681 // Note: no need to record the fixed probas.
682 int VP8RecordCoeffs(int ctx, const VP8Residual* const res) {
683 int n = res->first;
684 // should be stats[VP8EncBands[n]], but it's equivalent for n=0 or 1
685 proba_t* s = res->stats[n][ctx];
686 if (res->last < 0) {
687 Record(0, s + 0);
688 return 0;
689 }
690 while (n <= res->last) {
691 int v;
692 Record(1, s + 0); // order of record doesn't matter
693 while ((v = res->coeffs[n++]) == 0) {
694 Record(0, s + 1);
695 s = res->stats[VP8EncBands[n]][0];
696 }
697 Record(1, s + 1);
698 if (!Record(2u < (unsigned int)(v + 1), s + 2)) { // v = -1 or 1
699 s = res->stats[VP8EncBands[n]][1];
700 } else {
701 v = abs(v);
702 #if !defined(USE_LEVEL_CODE_TABLE)
703 if (!Record(v > 4, s + 3)) {
704 if (Record(v != 2, s + 4))
705 Record(v == 4, s + 5);
706 } else if (!Record(v > 10, s + 6)) {
707 Record(v > 6, s + 7);
708 } else if (!Record((v >= 3 + (8 << 2)), s + 8)) {
709 Record((v >= 3 + (8 << 1)), s + 9);
710 } else {
711 Record((v >= 3 + (8 << 3)), s + 10);
712 }
713 #else
714 if (v > MAX_VARIABLE_LEVEL) {
715 v = MAX_VARIABLE_LEVEL;
716 }
717
718 {
719 const int bits = VP8LevelCodes[v - 1][1];
720 int pattern = VP8LevelCodes[v - 1][0];
721 int i;
722 for (i = 0; (pattern >>= 1) != 0; ++i) {
723 const int mask = 2 << i;
724 if (pattern & 1) Record(!!(bits & mask), s + 3 + i);
725 }
726 }
727 #endif
728 s = res->stats[VP8EncBands[n]][2];
729 }
730 }
731 if (n < 16) Record(0, s + 0);
732 return 1;
733 }
734
735 //------------------------------------------------------------------------------
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698