Path: blob/master/thirdparty/libwebp/src/enc/picture_csp_enc.c
21869 views
// Copyright 2014 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// WebPPicture utils for colorspace conversion10//11// Author: Skal ([email protected])1213#include <assert.h>14#include <math.h>15#include <stdlib.h>16#include <string.h>1718#include "sharpyuv/sharpyuv.h"19#include "sharpyuv/sharpyuv_csp.h"20#include "src/dsp/cpu.h"21#include "src/dsp/dsp.h"22#include "src/dsp/lossless.h"23#include "src/dsp/yuv.h"24#include "src/enc/vp8i_enc.h"25#include "src/utils/random_utils.h"26#include "src/utils/utils.h"27#include "src/webp/encode.h"28#include "src/webp/types.h"2930#if defined(WEBP_USE_THREAD) && !defined(_WIN32)31#include <pthread.h>32#endif3334// Uncomment to disable gamma-compression during RGB->U/V averaging35#define USE_GAMMA_COMPRESSION3637// If defined, use table to compute x / alpha.38#define USE_INVERSE_ALPHA_TABLE3940#ifdef WORDS_BIGENDIAN41// uint32_t 0xff000000 is 0xff,00,00,00 in memory42#define CHANNEL_OFFSET(i) (i)43#else44// uint32_t 0xff000000 is 0x00,00,00,ff in memory45#define CHANNEL_OFFSET(i) (3-(i))46#endif4748#define ALPHA_OFFSET CHANNEL_OFFSET(0)4950//------------------------------------------------------------------------------51// Detection of non-trivial transparency5253// Returns true if alpha[] has non-0xff values.54static int CheckNonOpaque(const uint8_t* alpha, int width, int height,55int x_step, int y_step) {56if (alpha == NULL) return 0;57WebPInitAlphaProcessing();58if (x_step == 1) {59for (; height-- > 0; alpha += y_step) {60if (WebPHasAlpha8b(alpha, width)) return 1;61}62} else {63for (; height-- > 0; alpha += y_step) {64if (WebPHasAlpha32b(alpha, width)) return 1;65}66}67return 0;68}6970// Checking for the presence of non-opaque alpha.71int WebPPictureHasTransparency(const WebPPicture* picture) {72if (picture == NULL) return 0;73if (picture->use_argb) {74if (picture->argb != NULL) {75return CheckNonOpaque((const uint8_t*)picture->argb + ALPHA_OFFSET,76picture->width, picture->height,774, picture->argb_stride * sizeof(*picture->argb));78}79return 0;80}81return CheckNonOpaque(picture->a, picture->width, picture->height,821, picture->a_stride);83}8485//------------------------------------------------------------------------------86// Code for gamma correction8788#if defined(USE_GAMMA_COMPRESSION)8990// Gamma correction compensates loss of resolution during chroma subsampling.91#define GAMMA_FIX 12 // fixed-point precision for linear values92#define GAMMA_TAB_FIX 7 // fixed-point fractional bits precision93#define GAMMA_TAB_SIZE (1 << (GAMMA_FIX - GAMMA_TAB_FIX))94static const double kGamma = 0.80;95static const int kGammaScale = ((1 << GAMMA_FIX) - 1);96static const int kGammaTabScale = (1 << GAMMA_TAB_FIX);97static const int kGammaTabRounder = (1 << GAMMA_TAB_FIX >> 1);9899static int kLinearToGammaTab[GAMMA_TAB_SIZE + 1];100static uint16_t kGammaToLinearTab[256];101static volatile int kGammaTablesOk = 0;102static void InitGammaTables(void);103extern VP8CPUInfo VP8GetCPUInfo;104105WEBP_DSP_INIT_FUNC(InitGammaTables) {106if (!kGammaTablesOk) {107int v;108const double scale = (double)(1 << GAMMA_TAB_FIX) / kGammaScale;109const double norm = 1. / 255.;110for (v = 0; v <= 255; ++v) {111kGammaToLinearTab[v] =112(uint16_t)(pow(norm * v, kGamma) * kGammaScale + .5);113}114for (v = 0; v <= GAMMA_TAB_SIZE; ++v) {115kLinearToGammaTab[v] = (int)(255. * pow(scale * v, 1. / kGamma) + .5);116}117kGammaTablesOk = 1;118}119}120121static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) {122return kGammaToLinearTab[v];123}124125static WEBP_INLINE int Interpolate(int v) {126const int tab_pos = v >> (GAMMA_TAB_FIX + 2); // integer part127const int x = v & ((kGammaTabScale << 2) - 1); // fractional part128const int v0 = kLinearToGammaTab[tab_pos];129const int v1 = kLinearToGammaTab[tab_pos + 1];130const int y = v1 * x + v0 * ((kGammaTabScale << 2) - x); // interpolate131assert(tab_pos + 1 < GAMMA_TAB_SIZE + 1);132return y;133}134135// Convert a linear value 'v' to YUV_FIX+2 fixed-point precision136// U/V value, suitable for RGBToU/V calls.137static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {138const int y = Interpolate(base_value << shift); // final uplifted value139return (y + kGammaTabRounder) >> GAMMA_TAB_FIX; // descale140}141142#else143144static void InitGammaTables(void) {}145static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) { return v; }146static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {147return (int)(base_value << shift);148}149150#endif // USE_GAMMA_COMPRESSION151152//------------------------------------------------------------------------------153// RGB -> YUV conversion154155static int RGBToY(int r, int g, int b, VP8Random* const rg) {156return (rg == NULL) ? VP8RGBToY(r, g, b, YUV_HALF)157: VP8RGBToY(r, g, b, VP8RandomBits(rg, YUV_FIX));158}159160static int RGBToU(int r, int g, int b, VP8Random* const rg) {161return (rg == NULL) ? VP8RGBToU(r, g, b, YUV_HALF << 2)162: VP8RGBToU(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));163}164165static int RGBToV(int r, int g, int b, VP8Random* const rg) {166return (rg == NULL) ? VP8RGBToV(r, g, b, YUV_HALF << 2)167: VP8RGBToV(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));168}169170//------------------------------------------------------------------------------171// Sharp RGB->YUV conversion172173static const int kMinDimensionIterativeConversion = 4;174175//------------------------------------------------------------------------------176// Main function177178static int PreprocessARGB(const uint8_t* r_ptr,179const uint8_t* g_ptr,180const uint8_t* b_ptr,181int step, int rgb_stride,182WebPPicture* const picture) {183const int ok = SharpYuvConvert(184r_ptr, g_ptr, b_ptr, step, rgb_stride, /*rgb_bit_depth=*/8,185picture->y, picture->y_stride, picture->u, picture->uv_stride, picture->v,186picture->uv_stride, /*yuv_bit_depth=*/8, picture->width,187picture->height, SharpYuvGetConversionMatrix(kSharpYuvMatrixWebp));188if (!ok) {189return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);190}191return ok;192}193194//------------------------------------------------------------------------------195// "Fast" regular RGB->YUV196197#define SUM4(ptr, step) LinearToGamma( \198GammaToLinear((ptr)[0]) + \199GammaToLinear((ptr)[(step)]) + \200GammaToLinear((ptr)[rgb_stride]) + \201GammaToLinear((ptr)[rgb_stride + (step)]), 0) \202203#define SUM2(ptr) \204LinearToGamma(GammaToLinear((ptr)[0]) + GammaToLinear((ptr)[rgb_stride]), 1)205206#define SUM2ALPHA(ptr) ((ptr)[0] + (ptr)[rgb_stride])207#define SUM4ALPHA(ptr) (SUM2ALPHA(ptr) + SUM2ALPHA((ptr) + 4))208209#if defined(USE_INVERSE_ALPHA_TABLE)210211static const int kAlphaFix = 19;212// Following table is (1 << kAlphaFix) / a. The (v * kInvAlpha[a]) >> kAlphaFix213// formula is then equal to v / a in most (99.6%) cases. Note that this table214// and constant are adjusted very tightly to fit 32b arithmetic.215// In particular, they use the fact that the operands for 'v / a' are actually216// derived as v = (a0.p0 + a1.p1 + a2.p2 + a3.p3) and a = a0 + a1 + a2 + a3217// with ai in [0..255] and pi in [0..1<<GAMMA_FIX). The constraint to avoid218// overflow is: GAMMA_FIX + kAlphaFix <= 31.219static const uint32_t kInvAlpha[4 * 0xff + 1] = {2200, /* alpha = 0 */221524288, 262144, 174762, 131072, 104857, 87381, 74898, 65536,22258254, 52428, 47662, 43690, 40329, 37449, 34952, 32768,22330840, 29127, 27594, 26214, 24966, 23831, 22795, 21845,22420971, 20164, 19418, 18724, 18078, 17476, 16912, 16384,22515887, 15420, 14979, 14563, 14169, 13797, 13443, 13107,22612787, 12483, 12192, 11915, 11650, 11397, 11155, 10922,22710699, 10485, 10280, 10082, 9892, 9709, 9532, 9362,2289198, 9039, 8886, 8738, 8594, 8456, 8322, 8192,2298065, 7943, 7825, 7710, 7598, 7489, 7384, 7281,2307182, 7084, 6990, 6898, 6808, 6721, 6636, 6553,2316472, 6393, 6316, 6241, 6168, 6096, 6026, 5957,2325890, 5825, 5761, 5698, 5637, 5577, 5518, 5461,2335405, 5349, 5295, 5242, 5190, 5140, 5090, 5041,2344993, 4946, 4899, 4854, 4809, 4766, 4723, 4681,2354639, 4599, 4559, 4519, 4481, 4443, 4405, 4369,2364332, 4297, 4262, 4228, 4194, 4161, 4128, 4096,2374064, 4032, 4002, 3971, 3942, 3912, 3883, 3855,2383826, 3799, 3771, 3744, 3718, 3692, 3666, 3640,2393615, 3591, 3566, 3542, 3518, 3495, 3472, 3449,2403426, 3404, 3382, 3360, 3339, 3318, 3297, 3276,2413256, 3236, 3216, 3196, 3177, 3158, 3139, 3120,2423102, 3084, 3066, 3048, 3030, 3013, 2995, 2978,2432962, 2945, 2928, 2912, 2896, 2880, 2864, 2849,2442833, 2818, 2803, 2788, 2774, 2759, 2744, 2730,2452716, 2702, 2688, 2674, 2661, 2647, 2634, 2621,2462608, 2595, 2582, 2570, 2557, 2545, 2532, 2520,2472508, 2496, 2484, 2473, 2461, 2449, 2438, 2427,2482416, 2404, 2394, 2383, 2372, 2361, 2351, 2340,2492330, 2319, 2309, 2299, 2289, 2279, 2269, 2259,2502250, 2240, 2231, 2221, 2212, 2202, 2193, 2184,2512175, 2166, 2157, 2148, 2139, 2131, 2122, 2114,2522105, 2097, 2088, 2080, 2072, 2064, 2056, 2048,2532040, 2032, 2024, 2016, 2008, 2001, 1993, 1985,2541978, 1971, 1963, 1956, 1949, 1941, 1934, 1927,2551920, 1913, 1906, 1899, 1892, 1885, 1879, 1872,2561865, 1859, 1852, 1846, 1839, 1833, 1826, 1820,2571814, 1807, 1801, 1795, 1789, 1783, 1777, 1771,2581765, 1759, 1753, 1747, 1741, 1736, 1730, 1724,2591718, 1713, 1707, 1702, 1696, 1691, 1685, 1680,2601675, 1669, 1664, 1659, 1653, 1648, 1643, 1638,2611633, 1628, 1623, 1618, 1613, 1608, 1603, 1598,2621593, 1588, 1583, 1579, 1574, 1569, 1565, 1560,2631555, 1551, 1546, 1542, 1537, 1533, 1528, 1524,2641519, 1515, 1510, 1506, 1502, 1497, 1493, 1489,2651485, 1481, 1476, 1472, 1468, 1464, 1460, 1456,2661452, 1448, 1444, 1440, 1436, 1432, 1428, 1424,2671420, 1416, 1413, 1409, 1405, 1401, 1398, 1394,2681390, 1387, 1383, 1379, 1376, 1372, 1368, 1365,2691361, 1358, 1354, 1351, 1347, 1344, 1340, 1337,2701334, 1330, 1327, 1323, 1320, 1317, 1314, 1310,2711307, 1304, 1300, 1297, 1294, 1291, 1288, 1285,2721281, 1278, 1275, 1272, 1269, 1266, 1263, 1260,2731257, 1254, 1251, 1248, 1245, 1242, 1239, 1236,2741233, 1230, 1227, 1224, 1222, 1219, 1216, 1213,2751210, 1208, 1205, 1202, 1199, 1197, 1194, 1191,2761188, 1186, 1183, 1180, 1178, 1175, 1172, 1170,2771167, 1165, 1162, 1159, 1157, 1154, 1152, 1149,2781147, 1144, 1142, 1139, 1137, 1134, 1132, 1129,2791127, 1125, 1122, 1120, 1117, 1115, 1113, 1110,2801108, 1106, 1103, 1101, 1099, 1096, 1094, 1092,2811089, 1087, 1085, 1083, 1081, 1078, 1076, 1074,2821072, 1069, 1067, 1065, 1063, 1061, 1059, 1057,2831054, 1052, 1050, 1048, 1046, 1044, 1042, 1040,2841038, 1036, 1034, 1032, 1030, 1028, 1026, 1024,2851022, 1020, 1018, 1016, 1014, 1012, 1010, 1008,2861006, 1004, 1002, 1000, 998, 996, 994, 992,287991, 989, 987, 985, 983, 981, 979, 978,288976, 974, 972, 970, 969, 967, 965, 963,289961, 960, 958, 956, 954, 953, 951, 949,290948, 946, 944, 942, 941, 939, 937, 936,291934, 932, 931, 929, 927, 926, 924, 923,292921, 919, 918, 916, 914, 913, 911, 910,293908, 907, 905, 903, 902, 900, 899, 897,294896, 894, 893, 891, 890, 888, 887, 885,295884, 882, 881, 879, 878, 876, 875, 873,296872, 870, 869, 868, 866, 865, 863, 862,297860, 859, 858, 856, 855, 853, 852, 851,298849, 848, 846, 845, 844, 842, 841, 840,299838, 837, 836, 834, 833, 832, 830, 829,300828, 826, 825, 824, 823, 821, 820, 819,301817, 816, 815, 814, 812, 811, 810, 809,302807, 806, 805, 804, 802, 801, 800, 799,303798, 796, 795, 794, 793, 791, 790, 789,304788, 787, 786, 784, 783, 782, 781, 780,305779, 777, 776, 775, 774, 773, 772, 771,306769, 768, 767, 766, 765, 764, 763, 762,307760, 759, 758, 757, 756, 755, 754, 753,308752, 751, 750, 748, 747, 746, 745, 744,309743, 742, 741, 740, 739, 738, 737, 736,310735, 734, 733, 732, 731, 730, 729, 728,311727, 726, 725, 724, 723, 722, 721, 720,312719, 718, 717, 716, 715, 714, 713, 712,313711, 710, 709, 708, 707, 706, 705, 704,314703, 702, 701, 700, 699, 699, 698, 697,315696, 695, 694, 693, 692, 691, 690, 689,316688, 688, 687, 686, 685, 684, 683, 682,317681, 680, 680, 679, 678, 677, 676, 675,318674, 673, 673, 672, 671, 670, 669, 668,319667, 667, 666, 665, 664, 663, 662, 661,320661, 660, 659, 658, 657, 657, 656, 655,321654, 653, 652, 652, 651, 650, 649, 648,322648, 647, 646, 645, 644, 644, 643, 642,323641, 640, 640, 639, 638, 637, 637, 636,324635, 634, 633, 633, 632, 631, 630, 630,325629, 628, 627, 627, 626, 625, 624, 624,326623, 622, 621, 621, 620, 619, 618, 618,327617, 616, 616, 615, 614, 613, 613, 612,328611, 611, 610, 609, 608, 608, 607, 606,329606, 605, 604, 604, 603, 602, 601, 601,330600, 599, 599, 598, 597, 597, 596, 595,331595, 594, 593, 593, 592, 591, 591, 590,332589, 589, 588, 587, 587, 586, 585, 585,333584, 583, 583, 582, 581, 581, 580, 579,334579, 578, 578, 577, 576, 576, 575, 574,335574, 573, 572, 572, 571, 571, 570, 569,336569, 568, 568, 567, 566, 566, 565, 564,337564, 563, 563, 562, 561, 561, 560, 560,338559, 558, 558, 557, 557, 556, 555, 555,339554, 554, 553, 553, 552, 551, 551, 550,340550, 549, 548, 548, 547, 547, 546, 546,341545, 544, 544, 543, 543, 542, 542, 541,342541, 540, 539, 539, 538, 538, 537, 537,343536, 536, 535, 534, 534, 533, 533, 532,344532, 531, 531, 530, 530, 529, 529, 528,345527, 527, 526, 526, 525, 525, 524, 524,346523, 523, 522, 522, 521, 521, 520, 520,347519, 519, 518, 518, 517, 517, 516, 516,348515, 515, 514, 514349};350351// Note that LinearToGamma() expects the values to be premultiplied by 4,352// so we incorporate this factor 4 inside the DIVIDE_BY_ALPHA macro directly.353#define DIVIDE_BY_ALPHA(sum, a) (((sum) * kInvAlpha[(a)]) >> (kAlphaFix - 2))354355#else356357#define DIVIDE_BY_ALPHA(sum, a) (4 * (sum) / (a))358359#endif // USE_INVERSE_ALPHA_TABLE360361static WEBP_INLINE int LinearToGammaWeighted(const uint8_t* src,362const uint8_t* a_ptr,363uint32_t total_a, int step,364int rgb_stride) {365const uint32_t sum =366a_ptr[0] * GammaToLinear(src[0]) +367a_ptr[step] * GammaToLinear(src[step]) +368a_ptr[rgb_stride] * GammaToLinear(src[rgb_stride]) +369a_ptr[rgb_stride + step] * GammaToLinear(src[rgb_stride + step]);370assert(total_a > 0 && total_a <= 4 * 0xff);371#if defined(USE_INVERSE_ALPHA_TABLE)372assert((uint64_t)sum * kInvAlpha[total_a] < ((uint64_t)1 << 32));373#endif374return LinearToGamma(DIVIDE_BY_ALPHA(sum, total_a), 0);375}376377static WEBP_INLINE void ConvertRowToY(const uint8_t* const r_ptr,378const uint8_t* const g_ptr,379const uint8_t* const b_ptr,380int step,381uint8_t* const dst_y,382int width,383VP8Random* const rg) {384int i, j;385for (i = 0, j = 0; i < width; i += 1, j += step) {386dst_y[i] = RGBToY(r_ptr[j], g_ptr[j], b_ptr[j], rg);387}388}389390static WEBP_INLINE void AccumulateRGBA(const uint8_t* const r_ptr,391const uint8_t* const g_ptr,392const uint8_t* const b_ptr,393const uint8_t* const a_ptr,394int rgb_stride,395uint16_t* dst, int width) {396int i, j;397// we loop over 2x2 blocks and produce one R/G/B/A value for each.398for (i = 0, j = 0; i < (width >> 1); i += 1, j += 2 * 4, dst += 4) {399const uint32_t a = SUM4ALPHA(a_ptr + j);400int r, g, b;401if (a == 4 * 0xff || a == 0) {402r = SUM4(r_ptr + j, 4);403g = SUM4(g_ptr + j, 4);404b = SUM4(b_ptr + j, 4);405} else {406r = LinearToGammaWeighted(r_ptr + j, a_ptr + j, a, 4, rgb_stride);407g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 4, rgb_stride);408b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 4, rgb_stride);409}410dst[0] = r;411dst[1] = g;412dst[2] = b;413dst[3] = a;414}415if (width & 1) {416const uint32_t a = 2u * SUM2ALPHA(a_ptr + j);417int r, g, b;418if (a == 4 * 0xff || a == 0) {419r = SUM2(r_ptr + j);420g = SUM2(g_ptr + j);421b = SUM2(b_ptr + j);422} else {423r = LinearToGammaWeighted(r_ptr + j, a_ptr + j, a, 0, rgb_stride);424g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 0, rgb_stride);425b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 0, rgb_stride);426}427dst[0] = r;428dst[1] = g;429dst[2] = b;430dst[3] = a;431}432}433434static WEBP_INLINE void AccumulateRGB(const uint8_t* const r_ptr,435const uint8_t* const g_ptr,436const uint8_t* const b_ptr,437int step, int rgb_stride,438uint16_t* dst, int width) {439int i, j;440for (i = 0, j = 0; i < (width >> 1); i += 1, j += 2 * step, dst += 4) {441dst[0] = SUM4(r_ptr + j, step);442dst[1] = SUM4(g_ptr + j, step);443dst[2] = SUM4(b_ptr + j, step);444// MemorySanitizer may raise false positives with data that passes through445// RGBA32PackedToPlanar_16b_SSE41() due to incorrect modeling of shuffles.446// See https://crbug.com/webp/573.447#ifdef WEBP_MSAN448dst[3] = 0;449#endif450}451if (width & 1) {452dst[0] = SUM2(r_ptr + j);453dst[1] = SUM2(g_ptr + j);454dst[2] = SUM2(b_ptr + j);455#ifdef WEBP_MSAN456dst[3] = 0;457#endif458}459}460461static WEBP_INLINE void ConvertRowsToUV(const uint16_t* rgb,462uint8_t* const dst_u,463uint8_t* const dst_v,464int width,465VP8Random* const rg) {466int i;467for (i = 0; i < width; i += 1, rgb += 4) {468const int r = rgb[0], g = rgb[1], b = rgb[2];469dst_u[i] = RGBToU(r, g, b, rg);470dst_v[i] = RGBToV(r, g, b, rg);471}472}473474extern void SharpYuvInit(VP8CPUInfo cpu_info_func);475476static int ImportYUVAFromRGBA(const uint8_t* r_ptr,477const uint8_t* g_ptr,478const uint8_t* b_ptr,479const uint8_t* a_ptr,480int step, // bytes per pixel481int rgb_stride, // bytes per scanline482float dithering,483int use_iterative_conversion,484WebPPicture* const picture) {485int y;486const int width = picture->width;487const int height = picture->height;488const int has_alpha = CheckNonOpaque(a_ptr, width, height, step, rgb_stride);489const int is_rgb = (r_ptr < b_ptr); // otherwise it's bgr490491picture->colorspace = has_alpha ? WEBP_YUV420A : WEBP_YUV420;492picture->use_argb = 0;493494// disable smart conversion if source is too small (overkill).495if (width < kMinDimensionIterativeConversion ||496height < kMinDimensionIterativeConversion) {497use_iterative_conversion = 0;498}499500if (!WebPPictureAllocYUVA(picture)) {501return 0;502}503if (has_alpha) {504assert(step == 4);505#if defined(USE_GAMMA_COMPRESSION) && defined(USE_INVERSE_ALPHA_TABLE)506assert(kAlphaFix + GAMMA_FIX <= 31);507#endif508}509510if (use_iterative_conversion) {511SharpYuvInit(VP8GetCPUInfo);512if (!PreprocessARGB(r_ptr, g_ptr, b_ptr, step, rgb_stride, picture)) {513return 0;514}515if (has_alpha) {516WebPExtractAlpha(a_ptr, rgb_stride, width, height,517picture->a, picture->a_stride);518}519} else {520const int uv_width = (width + 1) >> 1;521int use_dsp = (step == 3); // use special function in this case522// temporary storage for accumulated R/G/B values during conversion to U/V523uint16_t* const tmp_rgb =524(uint16_t*)WebPSafeMalloc(4 * uv_width, sizeof(*tmp_rgb));525uint8_t* dst_y = picture->y;526uint8_t* dst_u = picture->u;527uint8_t* dst_v = picture->v;528uint8_t* dst_a = picture->a;529530VP8Random base_rg;531VP8Random* rg = NULL;532if (dithering > 0.) {533VP8InitRandom(&base_rg, dithering);534rg = &base_rg;535use_dsp = 0; // can't use dsp in this case536}537WebPInitConvertARGBToYUV();538InitGammaTables();539540if (tmp_rgb == NULL) {541return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);542}543544// Downsample Y/U/V planes, two rows at a time545for (y = 0; y < (height >> 1); ++y) {546int rows_have_alpha = has_alpha;547if (use_dsp) {548if (is_rgb) {549WebPConvertRGB24ToY(r_ptr, dst_y, width);550WebPConvertRGB24ToY(r_ptr + rgb_stride,551dst_y + picture->y_stride, width);552} else {553WebPConvertBGR24ToY(b_ptr, dst_y, width);554WebPConvertBGR24ToY(b_ptr + rgb_stride,555dst_y + picture->y_stride, width);556}557} else {558ConvertRowToY(r_ptr, g_ptr, b_ptr, step, dst_y, width, rg);559ConvertRowToY(r_ptr + rgb_stride,560g_ptr + rgb_stride,561b_ptr + rgb_stride, step,562dst_y + picture->y_stride, width, rg);563}564dst_y += 2 * picture->y_stride;565if (has_alpha) {566rows_have_alpha &= !WebPExtractAlpha(a_ptr, rgb_stride, width, 2,567dst_a, picture->a_stride);568dst_a += 2 * picture->a_stride;569}570// Collect averaged R/G/B(/A)571if (!rows_have_alpha) {572AccumulateRGB(r_ptr, g_ptr, b_ptr, step, rgb_stride, tmp_rgb, width);573} else {574AccumulateRGBA(r_ptr, g_ptr, b_ptr, a_ptr, rgb_stride, tmp_rgb, width);575}576// Convert to U/V577if (rg == NULL) {578WebPConvertRGBA32ToUV(tmp_rgb, dst_u, dst_v, uv_width);579} else {580ConvertRowsToUV(tmp_rgb, dst_u, dst_v, uv_width, rg);581}582dst_u += picture->uv_stride;583dst_v += picture->uv_stride;584r_ptr += 2 * rgb_stride;585b_ptr += 2 * rgb_stride;586g_ptr += 2 * rgb_stride;587if (has_alpha) a_ptr += 2 * rgb_stride;588}589if (height & 1) { // extra last row590int row_has_alpha = has_alpha;591if (use_dsp) {592if (r_ptr < b_ptr) {593WebPConvertRGB24ToY(r_ptr, dst_y, width);594} else {595WebPConvertBGR24ToY(b_ptr, dst_y, width);596}597} else {598ConvertRowToY(r_ptr, g_ptr, b_ptr, step, dst_y, width, rg);599}600if (row_has_alpha) {601row_has_alpha &= !WebPExtractAlpha(a_ptr, 0, width, 1, dst_a, 0);602}603// Collect averaged R/G/B(/A)604if (!row_has_alpha) {605// Collect averaged R/G/B606AccumulateRGB(r_ptr, g_ptr, b_ptr, step, /* rgb_stride = */ 0,607tmp_rgb, width);608} else {609AccumulateRGBA(r_ptr, g_ptr, b_ptr, a_ptr, /* rgb_stride = */ 0,610tmp_rgb, width);611}612if (rg == NULL) {613WebPConvertRGBA32ToUV(tmp_rgb, dst_u, dst_v, uv_width);614} else {615ConvertRowsToUV(tmp_rgb, dst_u, dst_v, uv_width, rg);616}617}618WebPSafeFree(tmp_rgb);619}620return 1;621}622623#undef SUM4624#undef SUM2625#undef SUM4ALPHA626#undef SUM2ALPHA627628//------------------------------------------------------------------------------629// call for ARGB->YUVA conversion630631static int PictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace,632float dithering, int use_iterative_conversion) {633if (picture == NULL) return 0;634if (picture->argb == NULL) {635return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);636} else if ((colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {637return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);638} else {639const uint8_t* const argb = (const uint8_t*)picture->argb;640const uint8_t* const a = argb + CHANNEL_OFFSET(0);641const uint8_t* const r = argb + CHANNEL_OFFSET(1);642const uint8_t* const g = argb + CHANNEL_OFFSET(2);643const uint8_t* const b = argb + CHANNEL_OFFSET(3);644645picture->colorspace = WEBP_YUV420;646return ImportYUVAFromRGBA(r, g, b, a, 4, 4 * picture->argb_stride,647dithering, use_iterative_conversion, picture);648}649}650651int WebPPictureARGBToYUVADithered(WebPPicture* picture, WebPEncCSP colorspace,652float dithering) {653return PictureARGBToYUVA(picture, colorspace, dithering, 0);654}655656int WebPPictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace) {657return PictureARGBToYUVA(picture, colorspace, 0.f, 0);658}659660int WebPPictureSharpARGBToYUVA(WebPPicture* picture) {661return PictureARGBToYUVA(picture, WEBP_YUV420, 0.f, 1);662}663// for backward compatibility664int WebPPictureSmartARGBToYUVA(WebPPicture* picture) {665return WebPPictureSharpARGBToYUVA(picture);666}667668//------------------------------------------------------------------------------669// call for YUVA -> ARGB conversion670671int WebPPictureYUVAToARGB(WebPPicture* picture) {672if (picture == NULL) return 0;673if (picture->y == NULL || picture->u == NULL || picture->v == NULL) {674return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);675}676if ((picture->colorspace & WEBP_CSP_ALPHA_BIT) && picture->a == NULL) {677return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);678}679if ((picture->colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {680return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);681}682// Allocate a new argb buffer (discarding the previous one).683if (!WebPPictureAllocARGB(picture)) return 0;684picture->use_argb = 1;685686// Convert687{688int y;689const int width = picture->width;690const int height = picture->height;691const int argb_stride = 4 * picture->argb_stride;692uint8_t* dst = (uint8_t*)picture->argb;693const uint8_t* cur_u = picture->u, *cur_v = picture->v, *cur_y = picture->y;694WebPUpsampleLinePairFunc upsample =695WebPGetLinePairConverter(ALPHA_OFFSET > 0);696697// First row, with replicated top samples.698upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);699cur_y += picture->y_stride;700dst += argb_stride;701// Center rows.702for (y = 1; y + 1 < height; y += 2) {703const uint8_t* const top_u = cur_u;704const uint8_t* const top_v = cur_v;705cur_u += picture->uv_stride;706cur_v += picture->uv_stride;707upsample(cur_y, cur_y + picture->y_stride, top_u, top_v, cur_u, cur_v,708dst, dst + argb_stride, width);709cur_y += 2 * picture->y_stride;710dst += 2 * argb_stride;711}712// Last row (if needed), with replicated bottom samples.713if (height > 1 && !(height & 1)) {714upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);715}716// Insert alpha values if needed, in replacement for the default 0xff ones.717if (picture->colorspace & WEBP_CSP_ALPHA_BIT) {718for (y = 0; y < height; ++y) {719uint32_t* const argb_dst = picture->argb + y * picture->argb_stride;720const uint8_t* const src = picture->a + y * picture->a_stride;721int x;722for (x = 0; x < width; ++x) {723argb_dst[x] = (argb_dst[x] & 0x00ffffffu) | ((uint32_t)src[x] << 24);724}725}726}727}728return 1;729}730731//------------------------------------------------------------------------------732// automatic import / conversion733734static int Import(WebPPicture* const picture,735const uint8_t* rgb, int rgb_stride,736int step, int swap_rb, int import_alpha) {737int y;738// swap_rb -> b,g,r,a , !swap_rb -> r,g,b,a739const uint8_t* r_ptr = rgb + (swap_rb ? 2 : 0);740const uint8_t* g_ptr = rgb + 1;741const uint8_t* b_ptr = rgb + (swap_rb ? 0 : 2);742const int width = picture->width;743const int height = picture->height;744745if (abs(rgb_stride) < (import_alpha ? 4 : 3) * width) return 0;746747if (!picture->use_argb) {748const uint8_t* a_ptr = import_alpha ? rgb + 3 : NULL;749return ImportYUVAFromRGBA(r_ptr, g_ptr, b_ptr, a_ptr, step, rgb_stride,7500.f /* no dithering */, 0, picture);751}752if (!WebPPictureAlloc(picture)) return 0;753754VP8LDspInit();755WebPInitAlphaProcessing();756757if (import_alpha) {758// dst[] byte order is {a,r,g,b} for big-endian, {b,g,r,a} for little endian759uint32_t* dst = picture->argb;760const int do_copy = (ALPHA_OFFSET == 3) && swap_rb;761assert(step == 4);762if (do_copy) {763for (y = 0; y < height; ++y) {764memcpy(dst, rgb, width * 4);765rgb += rgb_stride;766dst += picture->argb_stride;767}768} else {769for (y = 0; y < height; ++y) {770#ifdef WORDS_BIGENDIAN771// BGRA or RGBA input order.772const uint8_t* a_ptr = rgb + 3;773WebPPackARGB(a_ptr, r_ptr, g_ptr, b_ptr, width, dst);774r_ptr += rgb_stride;775g_ptr += rgb_stride;776b_ptr += rgb_stride;777#else778// RGBA input order. Need to swap R and B.779VP8LConvertBGRAToRGBA((const uint32_t*)rgb, width, (uint8_t*)dst);780#endif781rgb += rgb_stride;782dst += picture->argb_stride;783}784}785} else {786uint32_t* dst = picture->argb;787assert(step >= 3);788for (y = 0; y < height; ++y) {789WebPPackRGB(r_ptr, g_ptr, b_ptr, width, step, dst);790r_ptr += rgb_stride;791g_ptr += rgb_stride;792b_ptr += rgb_stride;793dst += picture->argb_stride;794}795}796return 1;797}798799// Public API800801#if !defined(WEBP_REDUCE_CSP)802803int WebPPictureImportBGR(WebPPicture* picture,804const uint8_t* bgr, int bgr_stride) {805return (picture != NULL && bgr != NULL)806? Import(picture, bgr, bgr_stride, 3, 1, 0)807: 0;808}809810int WebPPictureImportBGRA(WebPPicture* picture,811const uint8_t* bgra, int bgra_stride) {812return (picture != NULL && bgra != NULL)813? Import(picture, bgra, bgra_stride, 4, 1, 1)814: 0;815}816817818int WebPPictureImportBGRX(WebPPicture* picture,819const uint8_t* bgrx, int bgrx_stride) {820return (picture != NULL && bgrx != NULL)821? Import(picture, bgrx, bgrx_stride, 4, 1, 0)822: 0;823}824825#endif // WEBP_REDUCE_CSP826827int WebPPictureImportRGB(WebPPicture* picture,828const uint8_t* rgb, int rgb_stride) {829return (picture != NULL && rgb != NULL)830? Import(picture, rgb, rgb_stride, 3, 0, 0)831: 0;832}833834int WebPPictureImportRGBA(WebPPicture* picture,835const uint8_t* rgba, int rgba_stride) {836return (picture != NULL && rgba != NULL)837? Import(picture, rgba, rgba_stride, 4, 0, 1)838: 0;839}840841int WebPPictureImportRGBX(WebPPicture* picture,842const uint8_t* rgbx, int rgbx_stride) {843return (picture != NULL && rgbx != NULL)844? Import(picture, rgbx, rgbx_stride, 4, 0, 0)845: 0;846}847848//------------------------------------------------------------------------------849850851