mirror of
https://github.com/webmproject/libwebp.git
synced 2025-07-13 06:24:27 +02:00
Added implementation for various lossless functions
- VP8LEncAnalyze, EvalAndApplySubtractGreen, ApplyPredictFilter, ApplyCrossColorFilter - Added palette handling and transform buffer management in VP8LEncodeImage() - Add Transforms (subtract Green, Predict, cross_color) to dsp/lossless.c. These are more-or-less copied from src/lossless code. After this Change, will implement the EncodeImageInternal() method. Change-Id: Idf71f803c24b3b5ae3b5079b15e019721784611d
This commit is contained in:
@ -14,107 +14,7 @@
|
||||
|
||||
#include "./backward_references.h"
|
||||
#include "./histogram.h"
|
||||
|
||||
// A lookup table for small values of log(int) to be used in entropy
|
||||
// computation.
|
||||
//
|
||||
// ", ".join(["%.16ff" % x for x in [0.0]+[log(x) for x in range(1, 256)]])
|
||||
static const float kLogTable[] = {
|
||||
0.0000000000000000f, 0.0000000000000000f, 0.6931471805599453f,
|
||||
1.0986122886681098f, 1.3862943611198906f, 1.6094379124341003f,
|
||||
1.7917594692280550f, 1.9459101490553132f, 2.0794415416798357f,
|
||||
2.1972245773362196f, 2.3025850929940459f, 2.3978952727983707f,
|
||||
2.4849066497880004f, 2.5649493574615367f, 2.6390573296152584f,
|
||||
2.7080502011022101f, 2.7725887222397811f, 2.8332133440562162f,
|
||||
2.8903717578961645f, 2.9444389791664403f, 2.9957322735539909f,
|
||||
3.0445224377234230f, 3.0910424533583161f, 3.1354942159291497f,
|
||||
3.1780538303479458f, 3.2188758248682006f, 3.2580965380214821f,
|
||||
3.2958368660043291f, 3.3322045101752038f, 3.3672958299864741f,
|
||||
3.4011973816621555f, 3.4339872044851463f, 3.4657359027997265f,
|
||||
3.4965075614664802f, 3.5263605246161616f, 3.5553480614894135f,
|
||||
3.5835189384561099f, 3.6109179126442243f, 3.6375861597263857f,
|
||||
3.6635616461296463f, 3.6888794541139363f, 3.7135720667043080f,
|
||||
3.7376696182833684f, 3.7612001156935624f, 3.7841896339182610f,
|
||||
3.8066624897703196f, 3.8286413964890951f, 3.8501476017100584f,
|
||||
3.8712010109078911f, 3.8918202981106265f, 3.9120230054281460f,
|
||||
3.9318256327243257f, 3.9512437185814275f, 3.9702919135521220f,
|
||||
3.9889840465642745f, 4.0073331852324712f, 4.0253516907351496f,
|
||||
4.0430512678345503f, 4.0604430105464191f, 4.0775374439057197f,
|
||||
4.0943445622221004f, 4.1108738641733114f, 4.1271343850450917f,
|
||||
4.1431347263915326f, 4.1588830833596715f, 4.1743872698956368f,
|
||||
4.1896547420264252f, 4.2046926193909657f, 4.2195077051761070f,
|
||||
4.2341065045972597f, 4.2484952420493594f, 4.2626798770413155f,
|
||||
4.2766661190160553f, 4.2904594411483910f, 4.3040650932041702f,
|
||||
4.3174881135363101f, 4.3307333402863311f, 4.3438054218536841f,
|
||||
4.3567088266895917f, 4.3694478524670215f, 4.3820266346738812f,
|
||||
4.3944491546724391f, 4.4067192472642533f, 4.4188406077965983f,
|
||||
4.4308167988433134f, 4.4426512564903167f, 4.4543472962535073f,
|
||||
4.4659081186545837f, 4.4773368144782069f, 4.4886363697321396f,
|
||||
4.4998096703302650f, 4.5108595065168497f, 4.5217885770490405f,
|
||||
4.5325994931532563f, 4.5432947822700038f, 4.5538768916005408f,
|
||||
4.5643481914678361f, 4.5747109785033828f, 4.5849674786705723f,
|
||||
4.5951198501345898f, 4.6051701859880918f, 4.6151205168412597f,
|
||||
4.6249728132842707f, 4.6347289882296359f, 4.6443908991413725f,
|
||||
4.6539603501575231f, 4.6634390941120669f, 4.6728288344619058f,
|
||||
4.6821312271242199f, 4.6913478822291435f, 4.7004803657924166f,
|
||||
4.7095302013123339f, 4.7184988712950942f, 4.7273878187123408f,
|
||||
4.7361984483944957f, 4.7449321283632502f, 4.7535901911063645f,
|
||||
4.7621739347977563f, 4.7706846244656651f, 4.7791234931115296f,
|
||||
4.7874917427820458f, 4.7957905455967413f, 4.8040210447332568f,
|
||||
4.8121843553724171f, 4.8202815656050371f, 4.8283137373023015f,
|
||||
4.8362819069514780f, 4.8441870864585912f, 4.8520302639196169f,
|
||||
4.8598124043616719f, 4.8675344504555822f, 4.8751973232011512f,
|
||||
4.8828019225863706f, 4.8903491282217537f, 4.8978397999509111f,
|
||||
4.9052747784384296f, 4.9126548857360524f, 4.9199809258281251f,
|
||||
4.9272536851572051f, 4.9344739331306915f, 4.9416424226093039f,
|
||||
4.9487598903781684f, 4.9558270576012609f, 4.9628446302599070f,
|
||||
4.9698132995760007f, 4.9767337424205742f, 4.9836066217083363f,
|
||||
4.9904325867787360f, 4.9972122737641147f, 5.0039463059454592f,
|
||||
5.0106352940962555f, 5.0172798368149243f, 5.0238805208462765f,
|
||||
5.0304379213924353f, 5.0369526024136295f, 5.0434251169192468f,
|
||||
5.0498560072495371f, 5.0562458053483077f, 5.0625950330269669f,
|
||||
5.0689042022202315f, 5.0751738152338266f, 5.0814043649844631f,
|
||||
5.0875963352323836f, 5.0937502008067623f, 5.0998664278241987f,
|
||||
5.1059454739005803f, 5.1119877883565437f, 5.1179938124167554f,
|
||||
5.1239639794032588f, 5.1298987149230735f, 5.1357984370502621f,
|
||||
5.1416635565026603f, 5.1474944768134527f, 5.1532915944977793f,
|
||||
5.1590552992145291f, 5.1647859739235145f, 5.1704839950381514f,
|
||||
5.1761497325738288f, 5.1817835502920850f, 5.1873858058407549f,
|
||||
5.1929568508902104f, 5.1984970312658261f, 5.2040066870767951f,
|
||||
5.2094861528414214f, 5.2149357576089859f, 5.2203558250783244f,
|
||||
5.2257466737132017f, 5.2311086168545868f, 5.2364419628299492f,
|
||||
5.2417470150596426f, 5.2470240721604862f, 5.2522734280466299f,
|
||||
5.2574953720277815f, 5.2626901889048856f, 5.2678581590633282f,
|
||||
5.2729995585637468f, 5.2781146592305168f, 5.2832037287379885f,
|
||||
5.2882670306945352f, 5.2933048247244923f, 5.2983173665480363f,
|
||||
5.3033049080590757f, 5.3082676974012051f, 5.3132059790417872f,
|
||||
5.3181199938442161f, 5.3230099791384085f, 5.3278761687895813f,
|
||||
5.3327187932653688f, 5.3375380797013179f, 5.3423342519648109f,
|
||||
5.3471075307174685f, 5.3518581334760666f, 5.3565862746720123f,
|
||||
5.3612921657094255f, 5.3659760150218512f, 5.3706380281276624f,
|
||||
5.3752784076841653f, 5.3798973535404597f, 5.3844950627890888f,
|
||||
5.3890717298165010f, 5.3936275463523620f, 5.3981627015177525f,
|
||||
5.4026773818722793f, 5.4071717714601188f, 5.4116460518550396f,
|
||||
5.4161004022044201f, 5.4205349992722862f, 5.4249500174814029f,
|
||||
5.4293456289544411f, 5.4337220035542400f, 5.4380793089231956f,
|
||||
5.4424177105217932f, 5.4467373716663099f, 5.4510384535657002f,
|
||||
5.4553211153577017f, 5.4595855141441589f, 5.4638318050256105f,
|
||||
5.4680601411351315f, 5.4722706736714750f, 5.4764635519315110f,
|
||||
5.4806389233419912f, 5.4847969334906548f, 5.4889377261566867f,
|
||||
5.4930614433405482f, 5.4971682252932021f, 5.5012582105447274f,
|
||||
5.5053315359323625f, 5.5093883366279774f, 5.5134287461649825f,
|
||||
5.5174528964647074f, 5.5214609178622460f, 5.5254529391317835f,
|
||||
5.5294290875114234f, 5.5333894887275203f, 5.5373342670185366f,
|
||||
5.5412635451584258f,
|
||||
};
|
||||
|
||||
// Faster logarithm for small integers, with the property of log(0) == 0.
|
||||
static WEBP_INLINE double FastLog(int v) {
|
||||
if (v < (int)(sizeof(kLogTable) / sizeof(kLogTable[0]))) {
|
||||
return kLogTable[v];
|
||||
}
|
||||
return log(v);
|
||||
}
|
||||
#include "../dsp/lossless.h"
|
||||
|
||||
void VP8LConvertPopulationCountTableToBitEstimates(
|
||||
int num_symbols,
|
||||
@ -176,21 +76,6 @@ void VP8LHistogramCreate(VP8LHistogram* const p,
|
||||
}
|
||||
}
|
||||
|
||||
double VP8LShannonEntropy(const int* const array, int n) {
|
||||
int i;
|
||||
double retval = 0;
|
||||
int sum = 0;
|
||||
for (i = 0; i < n; ++i) {
|
||||
if (array[i] != 0) {
|
||||
sum += array[i];
|
||||
retval += array[i] * FastLog(array[i]);
|
||||
}
|
||||
}
|
||||
retval -= sum * FastLog(sum);
|
||||
retval *= -1.4426950408889634; // 1.0 / -FastLog(2);
|
||||
return retval;
|
||||
}
|
||||
|
||||
static double BitsEntropy(const int* const array, int n) {
|
||||
double retval = 0;
|
||||
int sum = 0;
|
||||
@ -202,13 +87,13 @@ static double BitsEntropy(const int* const array, int n) {
|
||||
if (array[i] != 0) {
|
||||
sum += array[i];
|
||||
++nonzeros;
|
||||
retval += array[i] * FastLog(array[i]);
|
||||
retval += array[i] * VP8LFastLog(array[i]);
|
||||
if (max_val < array[i]) {
|
||||
max_val = array[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
retval -= sum * FastLog(sum);
|
||||
retval -= sum * VP8LFastLog(sum);
|
||||
retval *= -1.4426950408889634; // 1.0 / -Log(2);
|
||||
mix = 0.627;
|
||||
if (nonzeros < 5) {
|
||||
|
@ -47,7 +47,7 @@ static WEBP_INLINE void VP8LHistogramClear(VP8LHistogram* const p) {
|
||||
}
|
||||
|
||||
static WEBP_INLINE void VP8LHistogramInit(VP8LHistogram* const p,
|
||||
int palette_code_bits) {
|
||||
int palette_code_bits) {
|
||||
p->palette_code_bits_ = palette_code_bits;
|
||||
VP8LHistogramClear(p);
|
||||
}
|
||||
@ -118,8 +118,6 @@ static WEBP_INLINE int VP8LHistogramNumCodes(const VP8LHistogram* const p) {
|
||||
void VP8LConvertPopulationCountTableToBitEstimates(
|
||||
int n, const int* const population_counts, double* const output);
|
||||
|
||||
double VP8LShannonEntropy(const int* const array, int n);
|
||||
|
||||
// Build a 2d image of histograms, subresolutioned by (1 << histobits) to
|
||||
// the original image.
|
||||
int VP8LHistogramBuildImage(int xsize, int ysize,
|
||||
|
339
src/enc/vp8l.c
339
src/enc/vp8l.c
@ -13,8 +13,11 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "./backward_references.h"
|
||||
#include "./vp8enci.h"
|
||||
#include "./vp8li.h"
|
||||
#include "../dsp/lossless.h"
|
||||
#include "../utils/bit_writer.h"
|
||||
|
||||
#if defined(__cplusplus) || defined(c_plusplus)
|
||||
@ -23,37 +26,223 @@ extern "C" {
|
||||
|
||||
static const uint32_t kImageSizeBits = 14;
|
||||
|
||||
static int Uint32Order(const void* p1, const void* p2) {
|
||||
const uint32_t a = *(const uint32_t*)p1;
|
||||
const uint32_t b = *(const uint32_t*)p2;
|
||||
if (a < b) {
|
||||
return -1;
|
||||
}
|
||||
if (a == b) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int CreatePalette256(const uint32_t* const argb, int num_pix,
|
||||
uint32_t* const palette, int* const palette_size) {
|
||||
int i, key;
|
||||
int current_size = 0;
|
||||
uint8_t in_use[MAX_PALETTE_SIZE * 4];
|
||||
uint32_t colors[MAX_PALETTE_SIZE * 4];
|
||||
static const uint32_t kHashMul = 0x1e35a7bd;
|
||||
|
||||
memset(in_use, 0, sizeof(in_use));
|
||||
key = (kHashMul * argb[0]) >> PALETTE_KEY_RIGHT_SHIFT;
|
||||
colors[key] = argb[0];
|
||||
in_use[key] = 1;
|
||||
++current_size;
|
||||
|
||||
for (i = 1; i < num_pix; ++i) {
|
||||
if (argb[i] == argb[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
key = (kHashMul * argb[i]) >> PALETTE_KEY_RIGHT_SHIFT;
|
||||
while (1) {
|
||||
if (!in_use[key]) {
|
||||
colors[key] = argb[i];
|
||||
in_use[key] = 1;
|
||||
++current_size;
|
||||
if (current_size > MAX_PALETTE_SIZE) {
|
||||
return 0;
|
||||
}
|
||||
break;
|
||||
} else if (colors[key] == argb[i]) {
|
||||
// The color is already there.
|
||||
break;
|
||||
} else {
|
||||
// Some other color sits there.
|
||||
// Do linear conflict resolution.
|
||||
++key;
|
||||
key &= 0x3ff; // key for 1K buffer.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*palette_size = 0;
|
||||
for (i = 0; i < (int)sizeof(in_use); ++i) {
|
||||
if (in_use[i]) {
|
||||
palette[*palette_size] = colors[i];
|
||||
++(*palette_size);
|
||||
}
|
||||
}
|
||||
|
||||
qsort(palette, *palette_size, sizeof(*palette), Uint32Order);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int AnalyzeEntropy(const uint32_t const *argb, int xsize, int ysize,
|
||||
int* nonpredicted_bits, int* predicted_bits) {
|
||||
int i;
|
||||
uint32_t pix_diff;
|
||||
VP8LHistogram* nonpredicted = NULL;
|
||||
VP8LHistogram* predicted = (VP8LHistogram*)malloc(2 * sizeof(*predicted));
|
||||
if (predicted == NULL) return 0;
|
||||
nonpredicted = predicted + sizeof(*predicted);
|
||||
|
||||
VP8LHistogramInit(predicted, 0);
|
||||
VP8LHistogramInit(nonpredicted, 0);
|
||||
for (i = 1; i < xsize * ysize; ++i) {
|
||||
if ((argb[i] == argb[i - 1]) ||
|
||||
(i >= xsize && argb[i] == argb[i - xsize])) {
|
||||
continue;
|
||||
}
|
||||
VP8LHistogramAddSinglePixOrCopy(nonpredicted,
|
||||
PixOrCopyCreateLiteral(argb[i]));
|
||||
pix_diff = VP8LSubPixels(argb[i], argb[i - 1]);
|
||||
VP8LHistogramAddSinglePixOrCopy(predicted,
|
||||
PixOrCopyCreateLiteral(pix_diff));
|
||||
}
|
||||
*nonpredicted_bits = (int)VP8LHistogramEstimateBitsBulk(nonpredicted);
|
||||
*predicted_bits = (int)VP8LHistogramEstimateBitsBulk(predicted);
|
||||
free(predicted);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int VP8LEncAnalyze(VP8LEncoder* const enc) {
|
||||
(void)enc;
|
||||
const WebPPicture* const pic = enc->pic_;
|
||||
int non_pred_entropy, pred_entropy;
|
||||
int is_photograph = 0;
|
||||
assert(pic && pic->argb);
|
||||
|
||||
if (!AnalyzeEntropy(pic->argb, pic->width, pic->height,
|
||||
&non_pred_entropy, &pred_entropy)) {
|
||||
return 0;
|
||||
}
|
||||
is_photograph =
|
||||
pred_entropy < (non_pred_entropy - (non_pred_entropy >> 3));
|
||||
|
||||
if (is_photograph) {
|
||||
enc->use_predict_ = 1;
|
||||
enc->use_cross_color_ = 1;
|
||||
}
|
||||
|
||||
enc->use_palette_ = CreatePalette256(pic->argb, pic->width * pic->height,
|
||||
enc->palette_, &enc->palette_size_);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int EncodeImageInternal(VP8LEncoder* const enc) {
|
||||
(void)enc;
|
||||
// Bundles multiple (2, 4 or 8) pixels into a single pixel.
|
||||
// Returns the new xsize.
|
||||
static void BundleColorMap(const uint32_t* const argb,
|
||||
int width, int height, int xbits,
|
||||
uint32_t* bundled_argb, int xs) {
|
||||
int x, y;
|
||||
const int bit_depth = 1 << (3 - xbits);
|
||||
uint32_t code = 0;
|
||||
|
||||
for (y = 0; y < height; ++y) {
|
||||
for (x = 0; x < width; ++x) {
|
||||
const int xsub = x & ((1 << xbits) - 1);
|
||||
if (xsub == 0) {
|
||||
code = 0;
|
||||
}
|
||||
code |= (argb[y * width + x] & 0xff00) << (bit_depth * xsub);
|
||||
bundled_argb[y * xs + (x >> xbits)] = 0xff000000 | code;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int EncodeImageInternal(VP8LBitWriter* const bw,
|
||||
const uint32_t* const argb,
|
||||
int width, int height, int quality,
|
||||
int cache_bits, int histogram_bits) {
|
||||
(void)bw;
|
||||
(void)argb;
|
||||
(void)width;
|
||||
(void)height;
|
||||
(void)quality;
|
||||
(void)cache_bits;
|
||||
(void)histogram_bits;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int CreatePalette(VP8LEncoder* const enc) {
|
||||
(void)enc;
|
||||
static int EvalAndApplySubtractGreen(VP8LBitWriter* const bw,
|
||||
VP8LEncoder* const enc,
|
||||
int width, int height) {
|
||||
int i;
|
||||
VP8LHistogram* before = NULL;
|
||||
// Check if it would be a good idea to subtract green from red and blue.
|
||||
VP8LHistogram* after = (VP8LHistogram*)malloc(2 * sizeof(*after));
|
||||
if (after == NULL) return 0;
|
||||
before = after + sizeof(*after);
|
||||
|
||||
VP8LHistogramInit(before, 1);
|
||||
VP8LHistogramInit(after, 1);
|
||||
for (i = 0; i < width * height; ++i) {
|
||||
// We only impact entropy in red and blue components, don't bother
|
||||
// to look at others.
|
||||
const uint32_t c = enc->argb_[i];
|
||||
const int green = (c >> 8) & 0xff;
|
||||
++(before->red_[(c >> 16) & 0xff]);
|
||||
++(before->blue_[c & 0xff]);
|
||||
++(after->red_[((c >> 16) - green) & 0xff]);
|
||||
++(after->blue_[(c - green) & 0xff]);
|
||||
}
|
||||
// Check if subtracting green yields low entropy.
|
||||
if (VP8LHistogramEstimateBits(after) < VP8LHistogramEstimateBits(before)) {
|
||||
VP8LWriteBits(bw, 1, 1);
|
||||
VP8LWriteBits(bw, 2, 2);
|
||||
VP8LSubtractGreenFromBlueAndRed(enc->argb_, width * height);
|
||||
}
|
||||
free(after);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void EvalSubtractGreen(VP8LEncoder* const enc) {
|
||||
(void)enc;
|
||||
}
|
||||
static int ApplyPredictFilter(VP8LBitWriter* const bw,
|
||||
VP8LEncoder* const enc,
|
||||
int width, int height, int quality) {
|
||||
const int pred_bits = enc->transform_bits_;
|
||||
const int transform_width = VP8LSubSampleSize(width, pred_bits);
|
||||
const int transform_height = VP8LSubSampleSize(height, pred_bits);
|
||||
|
||||
static int ApplyPredictFilter(VP8LEncoder* const enc) {
|
||||
(void)enc;
|
||||
VP8LResidualImage(width, height, pred_bits, enc->argb_, enc->transform_data_);
|
||||
VP8LWriteBits(bw, 1, 1);
|
||||
VP8LWriteBits(bw, 2, 0);
|
||||
VP8LWriteBits(bw, 4, pred_bits);
|
||||
if (!EncodeImageInternal(bw, enc->transform_data_,
|
||||
transform_width, transform_height, quality, 0, 0)) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ApplyCrossColorFilter(VP8LEncoder* const enc) {
|
||||
(void)enc;
|
||||
return 1;
|
||||
}
|
||||
static int ApplyCrossColorFilter(VP8LBitWriter* const bw,
|
||||
VP8LEncoder* const enc,
|
||||
int width, int height, int quality) {
|
||||
const int ccolor_transform_bits = enc->transform_bits_;
|
||||
const int transform_width = VP8LSubSampleSize(width, ccolor_transform_bits);
|
||||
const int transform_height = VP8LSubSampleSize(height, ccolor_transform_bits);
|
||||
|
||||
static void EvalColorCache(VP8LEncoder* const enc) {
|
||||
(void)enc;
|
||||
VP8LColorSpaceTransform(width, height, ccolor_transform_bits, quality,
|
||||
enc->argb_, enc->transform_data_);
|
||||
VP8LWriteBits(bw, 1, 1);
|
||||
VP8LWriteBits(bw, 2, 1);
|
||||
VP8LWriteBits(bw, 4, ccolor_transform_bits);
|
||||
if (!EncodeImageInternal(bw, enc->transform_data_,
|
||||
transform_width, transform_height, quality, 0, 0)) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void PutLE32(uint8_t* const data, uint32_t val) {
|
||||
@ -125,10 +314,15 @@ static VP8LEncoder* InitVP8LEncoder(const WebPConfig* const config,
|
||||
WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
|
||||
return NULL;
|
||||
}
|
||||
memset(enc, 0, sizeof(*enc));
|
||||
|
||||
enc->pic_ = picture;
|
||||
enc->use_lz77_ = 1;
|
||||
enc->palette_bits_ = 7;
|
||||
|
||||
enc->argb_ = NULL;
|
||||
enc->width_ = picture->width;
|
||||
|
||||
// TODO: Use config.quality to initialize histo_bits_ and transform_bits_.
|
||||
enc->histo_bits_ = 4;
|
||||
enc->transform_bits_ = 4;
|
||||
@ -150,9 +344,32 @@ static void DeleteVP8LEncoder(VP8LEncoder* enc) {
|
||||
free(enc);
|
||||
}
|
||||
|
||||
static WebPEncodingError AllocateEncodeBuffer(VP8LEncoder* const enc,
|
||||
int height, int width) {
|
||||
WebPEncodingError err = VP8_ENC_OK;
|
||||
const size_t image_size = height * width;
|
||||
const size_t transform_data_size =
|
||||
VP8LSubSampleSize(height, enc->transform_bits_) *
|
||||
VP8LSubSampleSize(width, enc->transform_bits_);
|
||||
const size_t total_size = image_size + transform_data_size;
|
||||
enc->argb_ = (uint32_t*)malloc(total_size * sizeof(*enc->argb_));
|
||||
if (enc->argb_ == NULL) {
|
||||
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
||||
goto Error;
|
||||
}
|
||||
enc->transform_data_ = enc->argb_ + image_size;
|
||||
|
||||
Error:
|
||||
return err;
|
||||
}
|
||||
|
||||
int VP8LEncodeImage(const WebPConfig* const config,
|
||||
WebPPicture* const picture) {
|
||||
int i;
|
||||
int ok = 0;
|
||||
int use_color_cache = 1;
|
||||
int cache_bits = 7;
|
||||
int width, height, quality;
|
||||
VP8LEncoder* enc = NULL;
|
||||
WebPEncodingError err = VP8_ENC_OK;
|
||||
VP8LBitWriter bw;
|
||||
@ -169,42 +386,110 @@ int VP8LEncodeImage(const WebPConfig* const config,
|
||||
err = VP8_ENC_ERROR_NULL_PARAMETER;
|
||||
goto Error;
|
||||
}
|
||||
width = picture->width;
|
||||
height = picture->height;
|
||||
quality = config->quality;
|
||||
|
||||
VP8LBitWriterInit(&bw, (picture->width * picture->height) >> 1);
|
||||
VP8LBitWriterInit(&bw, (width * height) >> 1);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Analyze image (entropy, num_palettes etc)
|
||||
|
||||
if (!VP8LEncAnalyze(enc)) goto Error;
|
||||
|
||||
if (enc->use_palette_) {
|
||||
CreatePalette(enc);
|
||||
if (!VP8LEncAnalyze(enc)) {
|
||||
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
||||
goto Error;
|
||||
}
|
||||
|
||||
// Write image size.
|
||||
WriteImageSize(enc, &bw);
|
||||
|
||||
if (enc->use_palette_) {
|
||||
uint32_t* argb = picture->argb;
|
||||
const uint32_t* const palette = enc->palette_;
|
||||
const int palette_size = enc->palette_size_;
|
||||
uint32_t argb_palette[MAX_PALETTE_SIZE];
|
||||
|
||||
for (i = 0; i < width * height; ++i) {
|
||||
int k;
|
||||
for (k = 0; k < palette_size; ++k) {
|
||||
if (argb[i] == palette[k]) {
|
||||
argb_palette[i] = 0xff000000 | (k << 8);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
VP8LWriteBits(&bw, 1, 1);
|
||||
VP8LWriteBits(&bw, 2, 3);
|
||||
VP8LWriteBits(&bw, 8, palette_size - 1);
|
||||
for (i = palette_size - 1; i >= 1; --i) {
|
||||
argb_palette[i] = VP8LSubPixels(palette[i], palette[i - 1]);
|
||||
}
|
||||
if (!EncodeImageInternal(&bw, argb_palette, palette_size, 1, quality,
|
||||
0, 0)) {
|
||||
goto Error;
|
||||
}
|
||||
use_color_cache = 0;
|
||||
if (palette_size <= 16) {
|
||||
int xbits = 1;
|
||||
if (palette_size <= 2) {
|
||||
xbits = 3;
|
||||
} else if (palette_size <= 4) {
|
||||
xbits = 2;
|
||||
}
|
||||
|
||||
// Image can be packed (multiple pixels per uint32).
|
||||
enc->width_ = VP8LSubSampleSize(width, xbits);
|
||||
err = AllocateEncodeBuffer(enc, height, enc->width_);
|
||||
if (err != VP8_ENC_OK) goto Error;
|
||||
BundleColorMap(argb, width, height, xbits, enc->argb_, enc->width_);
|
||||
}
|
||||
}
|
||||
|
||||
// In case image is not packed.
|
||||
if (enc->argb_ == NULL) {
|
||||
const size_t image_size = height * enc->width_;
|
||||
err = AllocateEncodeBuffer(enc, height, enc->width_);
|
||||
if (err != VP8_ENC_OK) goto Error;
|
||||
memcpy(enc->argb_, picture->argb, image_size * sizeof(*enc->argb_));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Apply transforms and write transform data.
|
||||
|
||||
EvalSubtractGreen(enc);
|
||||
if (!EvalAndApplySubtractGreen(&bw, enc, enc->width_, height)) {
|
||||
err = VP8_ENC_ERROR_OUT_OF_MEMORY;
|
||||
goto Error;
|
||||
}
|
||||
|
||||
if (enc->use_predict_) {
|
||||
if (!ApplyPredictFilter(enc)) goto Error;
|
||||
if (!ApplyPredictFilter(&bw, enc, enc->width_, height, quality)) {
|
||||
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
||||
goto Error;
|
||||
}
|
||||
}
|
||||
|
||||
if (enc->use_cross_color_) {
|
||||
if (!ApplyCrossColorFilter(enc)) goto Error;
|
||||
if (!ApplyCrossColorFilter(&bw, enc, enc->width_, height, quality)) {
|
||||
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
||||
goto Error;
|
||||
}
|
||||
}
|
||||
|
||||
if (enc->use_color_cache) {
|
||||
EvalColorCache(enc);
|
||||
if (use_color_cache) {
|
||||
if (quality > 25) {
|
||||
if (!VP8LCalculateEstimateForPaletteSize(enc->argb_, enc->width_, height,
|
||||
&cache_bits)) {
|
||||
err = VP8_ENC_ERROR_INVALID_CONFIGURATION;
|
||||
goto Error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Encode and write the transformed image.
|
||||
|
||||
ok = EncodeImageInternal(enc);
|
||||
ok = EncodeImageInternal(&bw, enc->argb_, enc->width_, height,
|
||||
quality, cache_bits, enc->histo_bits_);
|
||||
if (!ok) goto Error;
|
||||
|
||||
err = WriteImage(enc, &bw);
|
||||
|
@ -12,6 +12,7 @@
|
||||
#ifndef WEBP_ENC_VP8LI_H_
|
||||
#define WEBP_ENC_VP8LI_H_
|
||||
|
||||
#include "./histogram.h"
|
||||
#include "../webp/encode.h"
|
||||
#include "../utils/bit_writer.h"
|
||||
|
||||
@ -27,9 +28,16 @@ extern "C" {
|
||||
#define SIGNATURE_SIZE 1
|
||||
#define LOSSLESS_MAGIC_BYTE 0x64
|
||||
|
||||
#define MAX_PALETTE_SIZE 256
|
||||
#define PALETTE_KEY_RIGHT_SHIFT 22 // Key for 1K buffer.
|
||||
|
||||
typedef struct {
|
||||
const WebPConfig* config_; // user configuration and parameters
|
||||
WebPPicture* pic_; // input / output picture
|
||||
WebPPicture* pic_; // input picture.
|
||||
|
||||
uint32_t* argb_; // Transformed argb image data.
|
||||
uint32_t* transform_data_; // Scratch memory for transform data.
|
||||
int width_; // Packed image width.
|
||||
|
||||
// Encoding parameters derived from quality parameter.
|
||||
int use_lz77_;
|
||||
@ -38,13 +46,11 @@ typedef struct {
|
||||
int transform_bits_;
|
||||
|
||||
// Encoding parameters derived from image characteristics.
|
||||
int predicted_bits_;
|
||||
int non_predicted_bits_;
|
||||
int use_palette_;
|
||||
int num_palette_colors;
|
||||
int use_predict_;
|
||||
int use_cross_color_;
|
||||
int use_color_cache;
|
||||
int use_predict_;
|
||||
int use_palette_;
|
||||
int palette_size_;
|
||||
uint32_t palette_[MAX_PALETTE_SIZE];
|
||||
} VP8LEncoder;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user