Path: blob/master/thirdparty/libwebp/src/enc/picture_csp_enc.c
9917 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 <stdlib.h>15#include <math.h>1617#include "sharpyuv/sharpyuv.h"18#include "sharpyuv/sharpyuv_csp.h"19#include "src/enc/vp8i_enc.h"20#include "src/utils/random_utils.h"21#include "src/utils/utils.h"22#include "src/dsp/dsp.h"23#include "src/dsp/lossless.h"24#include "src/dsp/yuv.h"25#include "src/dsp/cpu.h"2627#if defined(WEBP_USE_THREAD) && !defined(_WIN32)28#include <pthread.h>29#endif3031// Uncomment to disable gamma-compression during RGB->U/V averaging32#define USE_GAMMA_COMPRESSION3334// If defined, use table to compute x / alpha.35#define USE_INVERSE_ALPHA_TABLE3637#ifdef WORDS_BIGENDIAN38// uint32_t 0xff000000 is 0xff,00,00,00 in memory39#define CHANNEL_OFFSET(i) (i)40#else41// uint32_t 0xff000000 is 0x00,00,00,ff in memory42#define CHANNEL_OFFSET(i) (3-(i))43#endif4445#define ALPHA_OFFSET CHANNEL_OFFSET(0)4647//------------------------------------------------------------------------------48// Detection of non-trivial transparency4950// Returns true if alpha[] has non-0xff values.51static int CheckNonOpaque(const uint8_t* alpha, int width, int height,52int x_step, int y_step) {53if (alpha == NULL) return 0;54WebPInitAlphaProcessing();55if (x_step == 1) {56for (; height-- > 0; alpha += y_step) {57if (WebPHasAlpha8b(alpha, width)) return 1;58}59} else {60for (; height-- > 0; alpha += y_step) {61if (WebPHasAlpha32b(alpha, width)) return 1;62}63}64return 0;65}6667// Checking for the presence of non-opaque alpha.68int WebPPictureHasTransparency(const WebPPicture* picture) {69if (picture == NULL) return 0;70if (picture->use_argb) {71if (picture->argb != NULL) {72return CheckNonOpaque((const uint8_t*)picture->argb + ALPHA_OFFSET,73picture->width, picture->height,744, picture->argb_stride * sizeof(*picture->argb));75}76return 0;77}78return CheckNonOpaque(picture->a, picture->width, picture->height,791, picture->a_stride);80}8182//------------------------------------------------------------------------------83// Code for gamma correction8485#if defined(USE_GAMMA_COMPRESSION)8687// Gamma correction compensates loss of resolution during chroma subsampling.88#define GAMMA_FIX 12 // fixed-point precision for linear values89#define GAMMA_TAB_FIX 7 // fixed-point fractional bits precision90#define GAMMA_TAB_SIZE (1 << (GAMMA_FIX - GAMMA_TAB_FIX))91static const double kGamma = 0.80;92static const int kGammaScale = ((1 << GAMMA_FIX) - 1);93static const int kGammaTabScale = (1 << GAMMA_TAB_FIX);94static const int kGammaTabRounder = (1 << GAMMA_TAB_FIX >> 1);9596static int kLinearToGammaTab[GAMMA_TAB_SIZE + 1];97static uint16_t kGammaToLinearTab[256];98static volatile int kGammaTablesOk = 0;99static void InitGammaTables(void);100extern VP8CPUInfo VP8GetCPUInfo;101102WEBP_DSP_INIT_FUNC(InitGammaTables) {103if (!kGammaTablesOk) {104int v;105const double scale = (double)(1 << GAMMA_TAB_FIX) / kGammaScale;106const double norm = 1. / 255.;107for (v = 0; v <= 255; ++v) {108kGammaToLinearTab[v] =109(uint16_t)(pow(norm * v, kGamma) * kGammaScale + .5);110}111for (v = 0; v <= GAMMA_TAB_SIZE; ++v) {112kLinearToGammaTab[v] = (int)(255. * pow(scale * v, 1. / kGamma) + .5);113}114kGammaTablesOk = 1;115}116}117118static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) {119return kGammaToLinearTab[v];120}121122static WEBP_INLINE int Interpolate(int v) {123const int tab_pos = v >> (GAMMA_TAB_FIX + 2); // integer part124const int x = v & ((kGammaTabScale << 2) - 1); // fractional part125const int v0 = kLinearToGammaTab[tab_pos];126const int v1 = kLinearToGammaTab[tab_pos + 1];127const int y = v1 * x + v0 * ((kGammaTabScale << 2) - x); // interpolate128assert(tab_pos + 1 < GAMMA_TAB_SIZE + 1);129return y;130}131132// Convert a linear value 'v' to YUV_FIX+2 fixed-point precision133// U/V value, suitable for RGBToU/V calls.134static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {135const int y = Interpolate(base_value << shift); // final uplifted value136return (y + kGammaTabRounder) >> GAMMA_TAB_FIX; // descale137}138139#else140141static void InitGammaTables(void) {}142static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) { return v; }143static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {144return (int)(base_value << shift);145}146147#endif // USE_GAMMA_COMPRESSION148149//------------------------------------------------------------------------------150// RGB -> YUV conversion151152static int RGBToY(int r, int g, int b, VP8Random* const rg) {153return (rg == NULL) ? VP8RGBToY(r, g, b, YUV_HALF)154: VP8RGBToY(r, g, b, VP8RandomBits(rg, YUV_FIX));155}156157static int RGBToU(int r, int g, int b, VP8Random* const rg) {158return (rg == NULL) ? VP8RGBToU(r, g, b, YUV_HALF << 2)159: VP8RGBToU(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));160}161162static int RGBToV(int r, int g, int b, VP8Random* const rg) {163return (rg == NULL) ? VP8RGBToV(r, g, b, YUV_HALF << 2)164: VP8RGBToV(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));165}166167//------------------------------------------------------------------------------168// Sharp RGB->YUV conversion169170static const int kMinDimensionIterativeConversion = 4;171172//------------------------------------------------------------------------------173// Main function174175static int PreprocessARGB(const uint8_t* r_ptr,176const uint8_t* g_ptr,177const uint8_t* b_ptr,178int step, int rgb_stride,179WebPPicture* const picture) {180const int ok = SharpYuvConvert(181r_ptr, g_ptr, b_ptr, step, rgb_stride, /*rgb_bit_depth=*/8,182picture->y, picture->y_stride, picture->u, picture->uv_stride, picture->v,183picture->uv_stride, /*yuv_bit_depth=*/8, picture->width,184picture->height, SharpYuvGetConversionMatrix(kSharpYuvMatrixWebp));185if (!ok) {186return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);187}188return ok;189}190191//------------------------------------------------------------------------------192// "Fast" regular RGB->YUV193194#define SUM4(ptr, step) LinearToGamma( \195GammaToLinear((ptr)[0]) + \196GammaToLinear((ptr)[(step)]) + \197GammaToLinear((ptr)[rgb_stride]) + \198GammaToLinear((ptr)[rgb_stride + (step)]), 0) \199200#define SUM2(ptr) \201LinearToGamma(GammaToLinear((ptr)[0]) + GammaToLinear((ptr)[rgb_stride]), 1)202203#define SUM2ALPHA(ptr) ((ptr)[0] + (ptr)[rgb_stride])204#define SUM4ALPHA(ptr) (SUM2ALPHA(ptr) + SUM2ALPHA((ptr) + 4))205206#if defined(USE_INVERSE_ALPHA_TABLE)207208static const int kAlphaFix = 19;209// Following table is (1 << kAlphaFix) / a. The (v * kInvAlpha[a]) >> kAlphaFix210// formula is then equal to v / a in most (99.6%) cases. Note that this table211// and constant are adjusted very tightly to fit 32b arithmetic.212// In particular, they use the fact that the operands for 'v / a' are actually213// derived as v = (a0.p0 + a1.p1 + a2.p2 + a3.p3) and a = a0 + a1 + a2 + a3214// with ai in [0..255] and pi in [0..1<<GAMMA_FIX). The constraint to avoid215// overflow is: GAMMA_FIX + kAlphaFix <= 31.216static const uint32_t kInvAlpha[4 * 0xff + 1] = {2170, /* alpha = 0 */218524288, 262144, 174762, 131072, 104857, 87381, 74898, 65536,21958254, 52428, 47662, 43690, 40329, 37449, 34952, 32768,22030840, 29127, 27594, 26214, 24966, 23831, 22795, 21845,22120971, 20164, 19418, 18724, 18078, 17476, 16912, 16384,22215887, 15420, 14979, 14563, 14169, 13797, 13443, 13107,22312787, 12483, 12192, 11915, 11650, 11397, 11155, 10922,22410699, 10485, 10280, 10082, 9892, 9709, 9532, 9362,2259198, 9039, 8886, 8738, 8594, 8456, 8322, 8192,2268065, 7943, 7825, 7710, 7598, 7489, 7384, 7281,2277182, 7084, 6990, 6898, 6808, 6721, 6636, 6553,2286472, 6393, 6316, 6241, 6168, 6096, 6026, 5957,2295890, 5825, 5761, 5698, 5637, 5577, 5518, 5461,2305405, 5349, 5295, 5242, 5190, 5140, 5090, 5041,2314993, 4946, 4899, 4854, 4809, 4766, 4723, 4681,2324639, 4599, 4559, 4519, 4481, 4443, 4405, 4369,2334332, 4297, 4262, 4228, 4194, 4161, 4128, 4096,2344064, 4032, 4002, 3971, 3942, 3912, 3883, 3855,2353826, 3799, 3771, 3744, 3718, 3692, 3666, 3640,2363615, 3591, 3566, 3542, 3518, 3495, 3472, 3449,2373426, 3404, 3382, 3360, 3339, 3318, 3297, 3276,2383256, 3236, 3216, 3196, 3177, 3158, 3139, 3120,2393102, 3084, 3066, 3048, 3030, 3013, 2995, 2978,2402962, 2945, 2928, 2912, 2896, 2880, 2864, 2849,2412833, 2818, 2803, 2788, 2774, 2759, 2744, 2730,2422716, 2702, 2688, 2674, 2661, 2647, 2634, 2621,2432608, 2595, 2582, 2570, 2557, 2545, 2532, 2520,2442508, 2496, 2484, 2473, 2461, 2449, 2438, 2427,2452416, 2404, 2394, 2383, 2372, 2361, 2351, 2340,2462330, 2319, 2309, 2299, 2289, 2279, 2269, 2259,2472250, 2240, 2231, 2221, 2212, 2202, 2193, 2184,2482175, 2166, 2157, 2148, 2139, 2131, 2122, 2114,2492105, 2097, 2088, 2080, 2072, 2064, 2056, 2048,2502040, 2032, 2024, 2016, 2008, 2001, 1993, 1985,2511978, 1971, 1963, 1956, 1949, 1941, 1934, 1927,2521920, 1913, 1906, 1899, 1892, 1885, 1879, 1872,2531865, 1859, 1852, 1846, 1839, 1833, 1826, 1820,2541814, 1807, 1801, 1795, 1789, 1783, 1777, 1771,2551765, 1759, 1753, 1747, 1741, 1736, 1730, 1724,2561718, 1713, 1707, 1702, 1696, 1691, 1685, 1680,2571675, 1669, 1664, 1659, 1653, 1648, 1643, 1638,2581633, 1628, 1623, 1618, 1613, 1608, 1603, 1598,2591593, 1588, 1583, 1579, 1574, 1569, 1565, 1560,2601555, 1551, 1546, 1542, 1537, 1533, 1528, 1524,2611519, 1515, 1510, 1506, 1502, 1497, 1493, 1489,2621485, 1481, 1476, 1472, 1468, 1464, 1460, 1456,2631452, 1448, 1444, 1440, 1436, 1432, 1428, 1424,2641420, 1416, 1413, 1409, 1405, 1401, 1398, 1394,2651390, 1387, 1383, 1379, 1376, 1372, 1368, 1365,2661361, 1358, 1354, 1351, 1347, 1344, 1340, 1337,2671334, 1330, 1327, 1323, 1320, 1317, 1314, 1310,2681307, 1304, 1300, 1297, 1294, 1291, 1288, 1285,2691281, 1278, 1275, 1272, 1269, 1266, 1263, 1260,2701257, 1254, 1251, 1248, 1245, 1242, 1239, 1236,2711233, 1230, 1227, 1224, 1222, 1219, 1216, 1213,2721210, 1208, 1205, 1202, 1199, 1197, 1194, 1191,2731188, 1186, 1183, 1180, 1178, 1175, 1172, 1170,2741167, 1165, 1162, 1159, 1157, 1154, 1152, 1149,2751147, 1144, 1142, 1139, 1137, 1134, 1132, 1129,2761127, 1125, 1122, 1120, 1117, 1115, 1113, 1110,2771108, 1106, 1103, 1101, 1099, 1096, 1094, 1092,2781089, 1087, 1085, 1083, 1081, 1078, 1076, 1074,2791072, 1069, 1067, 1065, 1063, 1061, 1059, 1057,2801054, 1052, 1050, 1048, 1046, 1044, 1042, 1040,2811038, 1036, 1034, 1032, 1030, 1028, 1026, 1024,2821022, 1020, 1018, 1016, 1014, 1012, 1010, 1008,2831006, 1004, 1002, 1000, 998, 996, 994, 992,284991, 989, 987, 985, 983, 981, 979, 978,285976, 974, 972, 970, 969, 967, 965, 963,286961, 960, 958, 956, 954, 953, 951, 949,287948, 946, 944, 942, 941, 939, 937, 936,288934, 932, 931, 929, 927, 926, 924, 923,289921, 919, 918, 916, 914, 913, 911, 910,290908, 907, 905, 903, 902, 900, 899, 897,291896, 894, 893, 891, 890, 888, 887, 885,292884, 882, 881, 879, 878, 876, 875, 873,293872, 870, 869, 868, 866, 865, 863, 862,294860, 859, 858, 856, 855, 853, 852, 851,295849, 848, 846, 845, 844, 842, 841, 840,296838, 837, 836, 834, 833, 832, 830, 829,297828, 826, 825, 824, 823, 821, 820, 819,298817, 816, 815, 814, 812, 811, 810, 809,299807, 806, 805, 804, 802, 801, 800, 799,300798, 796, 795, 794, 793, 791, 790, 789,301788, 787, 786, 784, 783, 782, 781, 780,302779, 777, 776, 775, 774, 773, 772, 771,303769, 768, 767, 766, 765, 764, 763, 762,304760, 759, 758, 757, 756, 755, 754, 753,305752, 751, 750, 748, 747, 746, 745, 744,306743, 742, 741, 740, 739, 738, 737, 736,307735, 734, 733, 732, 731, 730, 729, 728,308727, 726, 725, 724, 723, 722, 721, 720,309719, 718, 717, 716, 715, 714, 713, 712,310711, 710, 709, 708, 707, 706, 705, 704,311703, 702, 701, 700, 699, 699, 698, 697,312696, 695, 694, 693, 692, 691, 690, 689,313688, 688, 687, 686, 685, 684, 683, 682,314681, 680, 680, 679, 678, 677, 676, 675,315674, 673, 673, 672, 671, 670, 669, 668,316667, 667, 666, 665, 664, 663, 662, 661,317661, 660, 659, 658, 657, 657, 656, 655,318654, 653, 652, 652, 651, 650, 649, 648,319648, 647, 646, 645, 644, 644, 643, 642,320641, 640, 640, 639, 638, 637, 637, 636,321635, 634, 633, 633, 632, 631, 630, 630,322629, 628, 627, 627, 626, 625, 624, 624,323623, 622, 621, 621, 620, 619, 618, 618,324617, 616, 616, 615, 614, 613, 613, 612,325611, 611, 610, 609, 608, 608, 607, 606,326606, 605, 604, 604, 603, 602, 601, 601,327600, 599, 599, 598, 597, 597, 596, 595,328595, 594, 593, 593, 592, 591, 591, 590,329589, 589, 588, 587, 587, 586, 585, 585,330584, 583, 583, 582, 581, 581, 580, 579,331579, 578, 578, 577, 576, 576, 575, 574,332574, 573, 572, 572, 571, 571, 570, 569,333569, 568, 568, 567, 566, 566, 565, 564,334564, 563, 563, 562, 561, 561, 560, 560,335559, 558, 558, 557, 557, 556, 555, 555,336554, 554, 553, 553, 552, 551, 551, 550,337550, 549, 548, 548, 547, 547, 546, 546,338545, 544, 544, 543, 543, 542, 542, 541,339541, 540, 539, 539, 538, 538, 537, 537,340536, 536, 535, 534, 534, 533, 533, 532,341532, 531, 531, 530, 530, 529, 529, 528,342527, 527, 526, 526, 525, 525, 524, 524,343523, 523, 522, 522, 521, 521, 520, 520,344519, 519, 518, 518, 517, 517, 516, 516,345515, 515, 514, 514346};347348// Note that LinearToGamma() expects the values to be premultiplied by 4,349// so we incorporate this factor 4 inside the DIVIDE_BY_ALPHA macro directly.350#define DIVIDE_BY_ALPHA(sum, a) (((sum) * kInvAlpha[(a)]) >> (kAlphaFix - 2))351352#else353354#define DIVIDE_BY_ALPHA(sum, a) (4 * (sum) / (a))355356#endif // USE_INVERSE_ALPHA_TABLE357358static WEBP_INLINE int LinearToGammaWeighted(const uint8_t* src,359const uint8_t* a_ptr,360uint32_t total_a, int step,361int rgb_stride) {362const uint32_t sum =363a_ptr[0] * GammaToLinear(src[0]) +364a_ptr[step] * GammaToLinear(src[step]) +365a_ptr[rgb_stride] * GammaToLinear(src[rgb_stride]) +366a_ptr[rgb_stride + step] * GammaToLinear(src[rgb_stride + step]);367assert(total_a > 0 && total_a <= 4 * 0xff);368#if defined(USE_INVERSE_ALPHA_TABLE)369assert((uint64_t)sum * kInvAlpha[total_a] < ((uint64_t)1 << 32));370#endif371return LinearToGamma(DIVIDE_BY_ALPHA(sum, total_a), 0);372}373374static WEBP_INLINE void ConvertRowToY(const uint8_t* const r_ptr,375const uint8_t* const g_ptr,376const uint8_t* const b_ptr,377int step,378uint8_t* const dst_y,379int width,380VP8Random* const rg) {381int i, j;382for (i = 0, j = 0; i < width; i += 1, j += step) {383dst_y[i] = RGBToY(r_ptr[j], g_ptr[j], b_ptr[j], rg);384}385}386387static WEBP_INLINE void AccumulateRGBA(const uint8_t* const r_ptr,388const uint8_t* const g_ptr,389const uint8_t* const b_ptr,390const uint8_t* const a_ptr,391int rgb_stride,392uint16_t* dst, int width) {393int i, j;394// we loop over 2x2 blocks and produce one R/G/B/A value for each.395for (i = 0, j = 0; i < (width >> 1); i += 1, j += 2 * 4, dst += 4) {396const uint32_t a = SUM4ALPHA(a_ptr + j);397int r, g, b;398if (a == 4 * 0xff || a == 0) {399r = SUM4(r_ptr + j, 4);400g = SUM4(g_ptr + j, 4);401b = SUM4(b_ptr + j, 4);402} else {403r = LinearToGammaWeighted(r_ptr + j, a_ptr + j, a, 4, rgb_stride);404g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 4, rgb_stride);405b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 4, rgb_stride);406}407dst[0] = r;408dst[1] = g;409dst[2] = b;410dst[3] = a;411}412if (width & 1) {413const uint32_t a = 2u * SUM2ALPHA(a_ptr + j);414int r, g, b;415if (a == 4 * 0xff || a == 0) {416r = SUM2(r_ptr + j);417g = SUM2(g_ptr + j);418b = SUM2(b_ptr + j);419} else {420r = LinearToGammaWeighted(r_ptr + j, a_ptr + j, a, 0, rgb_stride);421g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 0, rgb_stride);422b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 0, rgb_stride);423}424dst[0] = r;425dst[1] = g;426dst[2] = b;427dst[3] = a;428}429}430431static WEBP_INLINE void AccumulateRGB(const uint8_t* const r_ptr,432const uint8_t* const g_ptr,433const uint8_t* const b_ptr,434int step, int rgb_stride,435uint16_t* dst, int width) {436int i, j;437for (i = 0, j = 0; i < (width >> 1); i += 1, j += 2 * step, dst += 4) {438dst[0] = SUM4(r_ptr + j, step);439dst[1] = SUM4(g_ptr + j, step);440dst[2] = SUM4(b_ptr + j, step);441// MemorySanitizer may raise false positives with data that passes through442// RGBA32PackedToPlanar_16b_SSE41() due to incorrect modeling of shuffles.443// See https://crbug.com/webp/573.444#ifdef WEBP_MSAN445dst[3] = 0;446#endif447}448if (width & 1) {449dst[0] = SUM2(r_ptr + j);450dst[1] = SUM2(g_ptr + j);451dst[2] = SUM2(b_ptr + j);452#ifdef WEBP_MSAN453dst[3] = 0;454#endif455}456}457458static WEBP_INLINE void ConvertRowsToUV(const uint16_t* rgb,459uint8_t* const dst_u,460uint8_t* const dst_v,461int width,462VP8Random* const rg) {463int i;464for (i = 0; i < width; i += 1, rgb += 4) {465const int r = rgb[0], g = rgb[1], b = rgb[2];466dst_u[i] = RGBToU(r, g, b, rg);467dst_v[i] = RGBToV(r, g, b, rg);468}469}470471extern void SharpYuvInit(VP8CPUInfo cpu_info_func);472473static int ImportYUVAFromRGBA(const uint8_t* r_ptr,474const uint8_t* g_ptr,475const uint8_t* b_ptr,476const uint8_t* a_ptr,477int step, // bytes per pixel478int rgb_stride, // bytes per scanline479float dithering,480int use_iterative_conversion,481WebPPicture* const picture) {482int y;483const int width = picture->width;484const int height = picture->height;485const int has_alpha = CheckNonOpaque(a_ptr, width, height, step, rgb_stride);486const int is_rgb = (r_ptr < b_ptr); // otherwise it's bgr487488picture->colorspace = has_alpha ? WEBP_YUV420A : WEBP_YUV420;489picture->use_argb = 0;490491// disable smart conversion if source is too small (overkill).492if (width < kMinDimensionIterativeConversion ||493height < kMinDimensionIterativeConversion) {494use_iterative_conversion = 0;495}496497if (!WebPPictureAllocYUVA(picture)) {498return 0;499}500if (has_alpha) {501assert(step == 4);502#if defined(USE_GAMMA_COMPRESSION) && defined(USE_INVERSE_ALPHA_TABLE)503assert(kAlphaFix + GAMMA_FIX <= 31);504#endif505}506507if (use_iterative_conversion) {508SharpYuvInit(VP8GetCPUInfo);509if (!PreprocessARGB(r_ptr, g_ptr, b_ptr, step, rgb_stride, picture)) {510return 0;511}512if (has_alpha) {513WebPExtractAlpha(a_ptr, rgb_stride, width, height,514picture->a, picture->a_stride);515}516} else {517const int uv_width = (width + 1) >> 1;518int use_dsp = (step == 3); // use special function in this case519// temporary storage for accumulated R/G/B values during conversion to U/V520uint16_t* const tmp_rgb =521(uint16_t*)WebPSafeMalloc(4 * uv_width, sizeof(*tmp_rgb));522uint8_t* dst_y = picture->y;523uint8_t* dst_u = picture->u;524uint8_t* dst_v = picture->v;525uint8_t* dst_a = picture->a;526527VP8Random base_rg;528VP8Random* rg = NULL;529if (dithering > 0.) {530VP8InitRandom(&base_rg, dithering);531rg = &base_rg;532use_dsp = 0; // can't use dsp in this case533}534WebPInitConvertARGBToYUV();535InitGammaTables();536537if (tmp_rgb == NULL) {538return WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);539}540541// Downsample Y/U/V planes, two rows at a time542for (y = 0; y < (height >> 1); ++y) {543int rows_have_alpha = has_alpha;544if (use_dsp) {545if (is_rgb) {546WebPConvertRGB24ToY(r_ptr, dst_y, width);547WebPConvertRGB24ToY(r_ptr + rgb_stride,548dst_y + picture->y_stride, width);549} else {550WebPConvertBGR24ToY(b_ptr, dst_y, width);551WebPConvertBGR24ToY(b_ptr + rgb_stride,552dst_y + picture->y_stride, width);553}554} else {555ConvertRowToY(r_ptr, g_ptr, b_ptr, step, dst_y, width, rg);556ConvertRowToY(r_ptr + rgb_stride,557g_ptr + rgb_stride,558b_ptr + rgb_stride, step,559dst_y + picture->y_stride, width, rg);560}561dst_y += 2 * picture->y_stride;562if (has_alpha) {563rows_have_alpha &= !WebPExtractAlpha(a_ptr, rgb_stride, width, 2,564dst_a, picture->a_stride);565dst_a += 2 * picture->a_stride;566}567// Collect averaged R/G/B(/A)568if (!rows_have_alpha) {569AccumulateRGB(r_ptr, g_ptr, b_ptr, step, rgb_stride, tmp_rgb, width);570} else {571AccumulateRGBA(r_ptr, g_ptr, b_ptr, a_ptr, rgb_stride, tmp_rgb, width);572}573// Convert to U/V574if (rg == NULL) {575WebPConvertRGBA32ToUV(tmp_rgb, dst_u, dst_v, uv_width);576} else {577ConvertRowsToUV(tmp_rgb, dst_u, dst_v, uv_width, rg);578}579dst_u += picture->uv_stride;580dst_v += picture->uv_stride;581r_ptr += 2 * rgb_stride;582b_ptr += 2 * rgb_stride;583g_ptr += 2 * rgb_stride;584if (has_alpha) a_ptr += 2 * rgb_stride;585}586if (height & 1) { // extra last row587int row_has_alpha = has_alpha;588if (use_dsp) {589if (r_ptr < b_ptr) {590WebPConvertRGB24ToY(r_ptr, dst_y, width);591} else {592WebPConvertBGR24ToY(b_ptr, dst_y, width);593}594} else {595ConvertRowToY(r_ptr, g_ptr, b_ptr, step, dst_y, width, rg);596}597if (row_has_alpha) {598row_has_alpha &= !WebPExtractAlpha(a_ptr, 0, width, 1, dst_a, 0);599}600// Collect averaged R/G/B(/A)601if (!row_has_alpha) {602// Collect averaged R/G/B603AccumulateRGB(r_ptr, g_ptr, b_ptr, step, /* rgb_stride = */ 0,604tmp_rgb, width);605} else {606AccumulateRGBA(r_ptr, g_ptr, b_ptr, a_ptr, /* rgb_stride = */ 0,607tmp_rgb, width);608}609if (rg == NULL) {610WebPConvertRGBA32ToUV(tmp_rgb, dst_u, dst_v, uv_width);611} else {612ConvertRowsToUV(tmp_rgb, dst_u, dst_v, uv_width, rg);613}614}615WebPSafeFree(tmp_rgb);616}617return 1;618}619620#undef SUM4621#undef SUM2622#undef SUM4ALPHA623#undef SUM2ALPHA624625//------------------------------------------------------------------------------626// call for ARGB->YUVA conversion627628static int PictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace,629float dithering, int use_iterative_conversion) {630if (picture == NULL) return 0;631if (picture->argb == NULL) {632return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);633} else if ((colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {634return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);635} else {636const uint8_t* const argb = (const uint8_t*)picture->argb;637const uint8_t* const a = argb + CHANNEL_OFFSET(0);638const uint8_t* const r = argb + CHANNEL_OFFSET(1);639const uint8_t* const g = argb + CHANNEL_OFFSET(2);640const uint8_t* const b = argb + CHANNEL_OFFSET(3);641642picture->colorspace = WEBP_YUV420;643return ImportYUVAFromRGBA(r, g, b, a, 4, 4 * picture->argb_stride,644dithering, use_iterative_conversion, picture);645}646}647648int WebPPictureARGBToYUVADithered(WebPPicture* picture, WebPEncCSP colorspace,649float dithering) {650return PictureARGBToYUVA(picture, colorspace, dithering, 0);651}652653int WebPPictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace) {654return PictureARGBToYUVA(picture, colorspace, 0.f, 0);655}656657int WebPPictureSharpARGBToYUVA(WebPPicture* picture) {658return PictureARGBToYUVA(picture, WEBP_YUV420, 0.f, 1);659}660// for backward compatibility661int WebPPictureSmartARGBToYUVA(WebPPicture* picture) {662return WebPPictureSharpARGBToYUVA(picture);663}664665//------------------------------------------------------------------------------666// call for YUVA -> ARGB conversion667668int WebPPictureYUVAToARGB(WebPPicture* picture) {669if (picture == NULL) return 0;670if (picture->y == NULL || picture->u == NULL || picture->v == NULL) {671return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);672}673if ((picture->colorspace & WEBP_CSP_ALPHA_BIT) && picture->a == NULL) {674return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);675}676if ((picture->colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {677return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);678}679// Allocate a new argb buffer (discarding the previous one).680if (!WebPPictureAllocARGB(picture)) return 0;681picture->use_argb = 1;682683// Convert684{685int y;686const int width = picture->width;687const int height = picture->height;688const int argb_stride = 4 * picture->argb_stride;689uint8_t* dst = (uint8_t*)picture->argb;690const uint8_t* cur_u = picture->u, *cur_v = picture->v, *cur_y = picture->y;691WebPUpsampleLinePairFunc upsample =692WebPGetLinePairConverter(ALPHA_OFFSET > 0);693694// First row, with replicated top samples.695upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);696cur_y += picture->y_stride;697dst += argb_stride;698// Center rows.699for (y = 1; y + 1 < height; y += 2) {700const uint8_t* const top_u = cur_u;701const uint8_t* const top_v = cur_v;702cur_u += picture->uv_stride;703cur_v += picture->uv_stride;704upsample(cur_y, cur_y + picture->y_stride, top_u, top_v, cur_u, cur_v,705dst, dst + argb_stride, width);706cur_y += 2 * picture->y_stride;707dst += 2 * argb_stride;708}709// Last row (if needed), with replicated bottom samples.710if (height > 1 && !(height & 1)) {711upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);712}713// Insert alpha values if needed, in replacement for the default 0xff ones.714if (picture->colorspace & WEBP_CSP_ALPHA_BIT) {715for (y = 0; y < height; ++y) {716uint32_t* const argb_dst = picture->argb + y * picture->argb_stride;717const uint8_t* const src = picture->a + y * picture->a_stride;718int x;719for (x = 0; x < width; ++x) {720argb_dst[x] = (argb_dst[x] & 0x00ffffffu) | ((uint32_t)src[x] << 24);721}722}723}724}725return 1;726}727728//------------------------------------------------------------------------------729// automatic import / conversion730731static int Import(WebPPicture* const picture,732const uint8_t* rgb, int rgb_stride,733int step, int swap_rb, int import_alpha) {734int y;735// swap_rb -> b,g,r,a , !swap_rb -> r,g,b,a736const uint8_t* r_ptr = rgb + (swap_rb ? 2 : 0);737const uint8_t* g_ptr = rgb + 1;738const uint8_t* b_ptr = rgb + (swap_rb ? 0 : 2);739const int width = picture->width;740const int height = picture->height;741742if (abs(rgb_stride) < (import_alpha ? 4 : 3) * width) return 0;743744if (!picture->use_argb) {745const uint8_t* a_ptr = import_alpha ? rgb + 3 : NULL;746return ImportYUVAFromRGBA(r_ptr, g_ptr, b_ptr, a_ptr, step, rgb_stride,7470.f /* no dithering */, 0, picture);748}749if (!WebPPictureAlloc(picture)) return 0;750751VP8LDspInit();752WebPInitAlphaProcessing();753754if (import_alpha) {755// dst[] byte order is {a,r,g,b} for big-endian, {b,g,r,a} for little endian756uint32_t* dst = picture->argb;757const int do_copy = (ALPHA_OFFSET == 3) && swap_rb;758assert(step == 4);759if (do_copy) {760for (y = 0; y < height; ++y) {761memcpy(dst, rgb, width * 4);762rgb += rgb_stride;763dst += picture->argb_stride;764}765} else {766for (y = 0; y < height; ++y) {767#ifdef WORDS_BIGENDIAN768// BGRA or RGBA input order.769const uint8_t* a_ptr = rgb + 3;770WebPPackARGB(a_ptr, r_ptr, g_ptr, b_ptr, width, dst);771r_ptr += rgb_stride;772g_ptr += rgb_stride;773b_ptr += rgb_stride;774#else775// RGBA input order. Need to swap R and B.776VP8LConvertBGRAToRGBA((const uint32_t*)rgb, width, (uint8_t*)dst);777#endif778rgb += rgb_stride;779dst += picture->argb_stride;780}781}782} else {783uint32_t* dst = picture->argb;784assert(step >= 3);785for (y = 0; y < height; ++y) {786WebPPackRGB(r_ptr, g_ptr, b_ptr, width, step, dst);787r_ptr += rgb_stride;788g_ptr += rgb_stride;789b_ptr += rgb_stride;790dst += picture->argb_stride;791}792}793return 1;794}795796// Public API797798#if !defined(WEBP_REDUCE_CSP)799800int WebPPictureImportBGR(WebPPicture* picture,801const uint8_t* bgr, int bgr_stride) {802return (picture != NULL && bgr != NULL)803? Import(picture, bgr, bgr_stride, 3, 1, 0)804: 0;805}806807int WebPPictureImportBGRA(WebPPicture* picture,808const uint8_t* bgra, int bgra_stride) {809return (picture != NULL && bgra != NULL)810? Import(picture, bgra, bgra_stride, 4, 1, 1)811: 0;812}813814815int WebPPictureImportBGRX(WebPPicture* picture,816const uint8_t* bgrx, int bgrx_stride) {817return (picture != NULL && bgrx != NULL)818? Import(picture, bgrx, bgrx_stride, 4, 1, 0)819: 0;820}821822#endif // WEBP_REDUCE_CSP823824int WebPPictureImportRGB(WebPPicture* picture,825const uint8_t* rgb, int rgb_stride) {826return (picture != NULL && rgb != NULL)827? Import(picture, rgb, rgb_stride, 3, 0, 0)828: 0;829}830831int WebPPictureImportRGBA(WebPPicture* picture,832const uint8_t* rgba, int rgba_stride) {833return (picture != NULL && rgba != NULL)834? Import(picture, rgba, rgba_stride, 4, 0, 1)835: 0;836}837838int WebPPictureImportRGBX(WebPPicture* picture,839const uint8_t* rgbx, int rgbx_stride) {840return (picture != NULL && rgbx != NULL)841? Import(picture, rgbx, rgbx_stride, 4, 0, 0)842: 0;843}844845//------------------------------------------------------------------------------846847848