| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
#include <stdlib.h> |
| 19 |
|
| 20 |
#include "src/dsp/lossless_common.h" |
| 21 |
#include "src/utils/utils.h" |
| 22 |
#include "src/enc/vp8li_enc.h" |
| 23 |
|
| 24 |
#if (WEBP_NEAR_LOSSLESS == 1) |
| 25 |
|
| 26 |
#define MIN_DIM_FOR_NEAR_LOSSLESS 64 |
| 27 |
#define MAX_LIMIT_BITS 5 |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
static uint32_t FindClosestDiscretized(uint32_t a, int bits) { |
| 32 |
const uint32_t mask = (1u << bits) - 1; |
| 33 |
const uint32_t biased = a + (mask >> 1) + ((a >> bits) & 1); |
| 34 |
assert(bits > 0); |
| 35 |
if (biased > 0xff) return 0xff; |
| 36 |
return biased & ~mask; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
static uint32_t ClosestDiscretizedArgb(uint32_t a, int bits) { |
| 41 |
return |
| 42 |
(FindClosestDiscretized(a >> 24, bits) << 24) | |
| 43 |
(FindClosestDiscretized((a >> 16) & 0xff, bits) << 16) | |
| 44 |
(FindClosestDiscretized((a >> 8) & 0xff, bits) << 8) | |
| 45 |
(FindClosestDiscretized(a & 0xff, bits)); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static int IsNear(uint32_t a, uint32_t b, int limit) { |
| 51 |
int k; |
| 52 |
for (k = 0; k < 4; ++k) { |
| 53 |
const int delta = |
| 54 |
(int)((a >> (k * 8)) & 0xff) - (int)((b >> (k * 8)) & 0xff); |
| 55 |
if (delta >= limit || delta <= -limit) { |
| 56 |
return 0; |
| 57 |
} |
| 58 |
} |
| 59 |
return 1; |
| 60 |
} |
| 61 |
|
| 62 |
static int IsSmooth(const uint32_t* const prev_row, |
| 63 |
const uint32_t* const curr_row, |
| 64 |
const uint32_t* const next_row, |
| 65 |
int ix, int limit) { |
| 66 |
|
| 67 |
return (IsNear(curr_row[ix], curr_row[ix - 1], limit) && |
| 68 |
IsNear(curr_row[ix], curr_row[ix + 1], limit) && |
| 69 |
IsNear(curr_row[ix], prev_row[ix], limit) && |
| 70 |
IsNear(curr_row[ix], next_row[ix], limit)); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
static void NearLossless(int xsize, int ysize, const uint32_t* argb_src, |
| 75 |
int stride, int limit_bits, uint32_t* copy_buffer, |
| 76 |
uint32_t* argb_dst) { |
| 77 |
int x, y; |
| 78 |
const int limit = 1 << limit_bits; |
| 79 |
uint32_t* prev_row = copy_buffer; |
| 80 |
uint32_t* curr_row = prev_row + xsize; |
| 81 |
uint32_t* next_row = curr_row + xsize; |
| 82 |
memcpy(curr_row, argb_src, xsize * sizeof(argb_src[0])); |
| 83 |
memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0])); |
| 84 |
|
| 85 |
for (y = 0; y < ysize; ++y, argb_src += stride, argb_dst += xsize) { |
| 86 |
if (y == 0 || y == ysize - 1) { |
| 87 |
memcpy(argb_dst, argb_src, xsize * sizeof(argb_src[0])); |
| 88 |
} else { |
| 89 |
memcpy(next_row, argb_src + stride, xsize * sizeof(argb_src[0])); |
| 90 |
argb_dst[0] = argb_src[0]; |
| 91 |
argb_dst[xsize - 1] = argb_src[xsize - 1]; |
| 92 |
for (x = 1; x < xsize - 1; ++x) { |
| 93 |
if (IsSmooth(prev_row, curr_row, next_row, x, limit)) { |
| 94 |
argb_dst[x] = curr_row[x]; |
| 95 |
} else { |
| 96 |
argb_dst[x] = ClosestDiscretizedArgb(curr_row[x], limit_bits); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
{ |
| 101 |
|
| 102 |
uint32_t* const temp = prev_row; |
| 103 |
prev_row = curr_row; |
| 104 |
curr_row = next_row; |
| 105 |
next_row = temp; |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
int VP8ApplyNearLossless(const WebPPicture* const picture, int quality, |
| 111 |
uint32_t* const argb_dst) { |
| 112 |
int i; |
| 113 |
const int xsize = picture->width; |
| 114 |
const int ysize = picture->height; |
| 115 |
const int stride = picture->argb_stride; |
| 116 |
uint32_t* const copy_buffer = |
| 117 |
(uint32_t*)WebPSafeMalloc(xsize * 3, sizeof(*copy_buffer)); |
| 118 |
const int limit_bits = VP8LNearLosslessBits(quality); |
| 119 |
assert(argb_dst != NULL); |
| 120 |
assert(limit_bits > 0); |
| 121 |
assert(limit_bits <= MAX_LIMIT_BITS); |
| 122 |
if (copy_buffer == NULL) { |
| 123 |
return 0; |
| 124 |
} |
| 125 |
|
| 126 |
if ((xsize < MIN_DIM_FOR_NEAR_LOSSLESS && |
| 127 |
ysize < MIN_DIM_FOR_NEAR_LOSSLESS) || |
| 128 |
ysize < 3) { |
| 129 |
for (i = 0; i < ysize; ++i) { |
| 130 |
memcpy(argb_dst + i * xsize, picture->argb + i * picture->argb_stride, |
| 131 |
xsize * sizeof(*argb_dst)); |
| 132 |
} |
| 133 |
WebPSafeFree(copy_buffer); |
| 134 |
return 1; |
| 135 |
} |
| 136 |
|
| 137 |
NearLossless(xsize, ysize, picture->argb, stride, limit_bits, copy_buffer, |
| 138 |
argb_dst); |
| 139 |
for (i = limit_bits - 1; i != 0; --i) { |
| 140 |
NearLossless(xsize, ysize, argb_dst, xsize, i, copy_buffer, argb_dst); |
| 141 |
} |
| 142 |
WebPSafeFree(copy_buffer); |
| 143 |
return 1; |
| 144 |
} |
| 145 |
#else |
| 146 |
|
| 147 |
|
| 148 |
extern void VP8LNearLosslessStub(void); |
| 149 |
void VP8LNearLosslessStub(void) {} |
| 150 |
|
| 151 |
#endif |