|
| 1 | +/*! |
| 2 | + * libwebp's sharp RGB to YUV 4:2:0 conversion (sharpyuv/sharpyuv.c, sharpyuv_dsp.c, sharpyuv_gamma.c), ported for 8 |
| 3 | + * bit RGB to 8 bit YUV with the WebP matrix: luma and chroma are chosen iteratively so that the picture upsampled |
| 4 | + * again matches the original better, sharper at color edges than averaging the chroma. Copyright (c) 2022 Google |
| 5 | + * Inc., BSD-3-Clause License (see THIRD_PARTY_NOTICES.md) |
| 6 | + */ |
| 7 | + |
| 8 | +const ITERATIONS = 4; |
| 9 | +// RGB is 8 bits, the work is done in 10 (2 more bits of precision, sharpyuv's GetPrecisionShift) |
| 10 | +const SHIFT = 2, BITS = 10, MAX = (1 << BITS) - 1; |
| 11 | + |
| 12 | +// the gamma curve (libwebp calls it sRGB: Rec. 709's), as 16 bit linear values from 10 bit ones and back by a 513 |
| 13 | +// entry table |
| 14 | +const TO_LINEAR = new Uint32Array(1024 + 2), FROM_LINEAR = new Uint32Array(512 + 2); |
| 15 | +{ |
| 16 | + const a = 0.09929682680944, thresh = 0.018053968510807, gamma = 1 / 0.45; |
| 17 | + for (let v = 0; v <= 1024; v++) { |
| 18 | + const g = v / 1024; |
| 19 | + TO_LINEAR[v] = Math.trunc((g <= thresh * 4.5 ? g / 4.5 : Math.pow((g + a) / (1 + a), gamma)) * 65536 + 0.5); |
| 20 | + } |
| 21 | + TO_LINEAR[1025] = TO_LINEAR[1024]; |
| 22 | + for (let v = 0; v <= 512; v++) { |
| 23 | + const g = v / 512; |
| 24 | + FROM_LINEAR[v] = Math.trunc(65536 * (g <= thresh ? 4.5 * g : (1 + a) * Math.pow(g, 1 / gamma) - a) + 0.5); |
| 25 | + } |
| 26 | + FROM_LINEAR[513] = FROM_LINEAR[512]; |
| 27 | +} |
| 28 | + |
| 29 | +const toLinear = (v: number): number => TO_LINEAR[v]; |
| 30 | + |
| 31 | +function fromLinear(value: number): number { |
| 32 | + const pos = value >>> 7, x = value - (pos << 7); |
| 33 | + const v0 = FROM_LINEAR[pos] >>> 6, v1 = FROM_LINEAR[pos + 1] >>> 6; |
| 34 | + return v0 + (((v1 - v0) * x + 64) >>> 7); |
| 35 | +} |
| 36 | + |
| 37 | +// (the values can take more than 32 bits) |
| 38 | +const gray = (r: number, g: number, b: number): number => Math.floor((13933 * r + 46871 * g + 4732 * b + 32768) / 65536); |
| 39 | +const clip = (v: number): number => v < 0 ? 0 : v > MAX ? MAX : v; |
| 40 | + |
| 41 | +// the gamma compressed luma of a row of R, G and B planes (w each) |
| 42 | +function updateW(src: Uint16Array, s: number, dst: Uint16Array, d: number, w: number): void { |
| 43 | + for (let i = 0; i < w; i++) dst[d + i] = fromLinear(gray(toLinear(src[s + i]), toLinear(src[s + w + i]), toLinear(src[s + 2 * w + i]))); |
| 44 | +} |
| 45 | + |
| 46 | +// the average of 4 gamma compressed values, in linear light |
| 47 | +const scaleDown = (a: number, b: number, c: number, d: number): number => fromLinear((toLinear(a) + toLinear(b) + toLinear(c) + toLinear(d) + 2) >> 2); |
| 48 | + |
| 49 | +// the chroma (R, G and B minus their luma) of 2 rows of R, G and B planes (2 uvWidth each) into 3 planes of uvWidth |
| 50 | +function updateChroma(src: Uint16Array, s1: number, s2: number, dst: Int16Array, d: number, uvWidth: number): void { |
| 51 | + const w = 2 * uvWidth; |
| 52 | + for (let i = 0; i < uvWidth; i++) { |
| 53 | + const o = 2 * i; |
| 54 | + const r = scaleDown(src[s1 + o], src[s1 + o + 1], src[s2 + o], src[s2 + o + 1]); |
| 55 | + const g = scaleDown(src[s1 + w + o], src[s1 + w + o + 1], src[s2 + w + o], src[s2 + w + o + 1]); |
| 56 | + const b = scaleDown(src[s1 + 2 * w + o], src[s1 + 2 * w + o + 1], src[s2 + 2 * w + o], src[s2 + 2 * w + o + 1]); |
| 57 | + const W = gray(r, g, b); |
| 58 | + dst[d + i] = r - W; |
| 59 | + dst[d + uvWidth + i] = g - W; |
| 60 | + dst[d + 2 * uvWidth + i] = b - W; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function storeGray(rgb: Uint16Array, s: number, y: Uint16Array, d: number, w: number): void { |
| 65 | + for (let i = 0; i < w; i++) y[d + i] = gray(rgb[s + i], rgb[s + w + i], rgb[s + 2 * w + i]); |
| 66 | +} |
| 67 | + |
| 68 | +// a row of RGBA as R, G and B planes (w each, w even: an odd width's last pixel again), 10 bit |
| 69 | +function importRow(rgba: Uint8Array, o: number, width: number, w: number, dst: Uint16Array, d: number): void { |
| 70 | + for (let i = 0; i < width; i++, o += 4) { |
| 71 | + dst[d + i] = rgba[o] << SHIFT; |
| 72 | + dst[d + w + i] = rgba[o + 1] << SHIFT; |
| 73 | + dst[d + 2 * w + i] = rgba[o + 2] << SHIFT; |
| 74 | + } |
| 75 | + if (width & 1) for (let c = 0; c < 3; c++) dst[d + c * w + width] = dst[d + c * w + width - 1]; |
| 76 | +} |
| 77 | + |
| 78 | +// the RGB of 2 rows (into out at o1 and o2) from their luma and the chroma upsampled (cur, the rows' chroma, with |
| 79 | +// prev and next, the rows above and below): sharpyuv's InterpolateTwoRows |
| 80 | +function interpolateTwoRows(bestY: Uint16Array, y: number, uv: Int16Array, prev: number, cur: number, next: number, w: number, out: Uint16Array, o1: number, o2: number): void { |
| 81 | + const uvWidth = w >> 1, len = (w - 1) >> 1; |
| 82 | + const filter2 = (a: number, b: number, w0: number): number => clip(((a * 3 + b + 2) >> 2) + w0); |
| 83 | + const filterRow = (a: number, b: number, by: number, o: number): void => { |
| 84 | + for (let i = 0; i < len; i++, a++, b++) { |
| 85 | + const v0 = (uv[a] * 9 + uv[a + 1] * 3 + uv[b] * 3 + uv[b + 1] + 8) >> 4; |
| 86 | + const v1 = (uv[a + 1] * 9 + uv[a] * 3 + uv[b + 1] * 3 + uv[b] + 8) >> 4; |
| 87 | + out[o + 2 * i] = clip(bestY[by + 2 * i] + v0); |
| 88 | + out[o + 2 * i + 1] = clip(bestY[by + 2 * i + 1] + v1); |
| 89 | + } |
| 90 | + }; |
| 91 | + for (let k = 0; k < 3; k++) { |
| 92 | + out[o1] = filter2(uv[cur], uv[prev], bestY[y]); |
| 93 | + out[o2] = filter2(uv[cur], uv[next], bestY[y + w]); |
| 94 | + filterRow(cur, prev, y + 1, o1 + 1); |
| 95 | + filterRow(cur, next, y + w + 1, o2 + 1); |
| 96 | + if (!(w & 1)) { |
| 97 | + out[o1 + w - 1] = filter2(uv[cur + uvWidth - 1], uv[prev + uvWidth - 1], bestY[y + w - 1]); |
| 98 | + out[o2 + w - 1] = filter2(uv[cur + uvWidth - 1], uv[next + uvWidth - 1], bestY[y + 2 * w - 1]); |
| 99 | + } |
| 100 | + o1 += w; |
| 101 | + o2 += w; |
| 102 | + prev += uvWidth; |
| 103 | + cur += uvWidth; |
| 104 | + next += uvWidth; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// the WebP matrix (limited range Rec. 601) for 10 bit RGB: Y, U and V of R, G and B, rounded |
| 109 | +const yuv = (r: number, g: number, b: number, c0: number, c1: number, c2: number, offset: number): number => |
| 110 | + (c0 * r + c1 * g + c2 * b + (offset << 18) + (1 << 17)) >> 18; |
| 111 | +const clip8 = (v: number): number => v < 0 ? 0 : v > 255 ? 255 : v; |
| 112 | + |
| 113 | +/** |
| 114 | + * The luma and chroma of RGBA pixels (their alpha isn't used) by libwebp's sharp conversion (at least 4x4 pixels, as |
| 115 | + * libwebp uses it): y has width x height values, u and v (width + 1) / 2 x (height + 1) / 2 |
| 116 | + */ |
| 117 | +export function sharpYUV(rgba: Uint8Array, width: number, height: number, y: Uint8Array, u: Uint8Array, v: Uint8Array): void { |
| 118 | + const w = (width + 1) & ~1, h = (height + 1) & ~1, uvWidth = w >> 1, uvHeight = h >> 1; |
| 119 | + const tmp = new Uint16Array(w * 6), bestY = new Uint16Array(w * h), targetY = new Uint16Array(w * h), bestRGBY = new Uint16Array(w * 2); |
| 120 | + const bestUV = new Int16Array(uvWidth * 3 * uvHeight), targetUV = new Int16Array(uvWidth * 3 * uvHeight), bestRGBUV = new Int16Array(uvWidth * 3); |
| 121 | + const stride = width * 4; |
| 122 | + |
| 123 | + // the start: luma of the original, chroma averaged in linear light |
| 124 | + for (let j = 0, uvRow = 0; j < height; j += 2, uvRow += 3 * uvWidth) { |
| 125 | + importRow(rgba, j * stride, width, w, tmp, 0); |
| 126 | + if (j < height - 1) importRow(rgba, (j + 1) * stride, width, w, tmp, 3 * w); |
| 127 | + else tmp.copyWithin(3 * w, 0, 3 * w); |
| 128 | + storeGray(tmp, 0, bestY, j * w, w); |
| 129 | + storeGray(tmp, 3 * w, bestY, (j + 1) * w, w); |
| 130 | + updateW(tmp, 0, targetY, j * w, w); |
| 131 | + updateW(tmp, 3 * w, targetY, (j + 1) * w, w); |
| 132 | + updateChroma(tmp, 0, 3 * w, targetUV, uvRow, uvWidth); |
| 133 | + bestUV.set(targetUV.subarray(uvRow, uvRow + 3 * uvWidth), uvRow); |
| 134 | + } |
| 135 | + |
| 136 | + // the luma and chroma move by how much the upsampled picture's differ from the target's |
| 137 | + const threshold = Math.trunc(3 * w * h); |
| 138 | + let previousSum = Infinity; |
| 139 | + for (let iteration = 0; iteration < ITERATIONS; iteration++) { |
| 140 | + let cur = 0, prev = 0, sum = 0; |
| 141 | + for (let j = 0; j < h; j += 2) { |
| 142 | + const next = cur + (j < h - 2 ? 3 * uvWidth : 0); |
| 143 | + interpolateTwoRows(bestY, j * w, bestUV, prev, cur, next, w, tmp, 0, 3 * w); |
| 144 | + prev = cur; |
| 145 | + cur = next; |
| 146 | + updateW(tmp, 0, bestRGBY, 0, w); |
| 147 | + updateW(tmp, 3 * w, bestRGBY, w, w); |
| 148 | + updateChroma(tmp, 0, 3 * w, bestRGBUV, 0, uvWidth); |
| 149 | + for (let i = 0; i < 2 * w; i++) { |
| 150 | + const diff = targetY[j * w + i] - bestRGBY[i]; |
| 151 | + bestY[j * w + i] = clip(bestY[j * w + i] + diff); |
| 152 | + sum += Math.abs(diff); |
| 153 | + } |
| 154 | + const uvRow = (j >> 1) * 3 * uvWidth; |
| 155 | + for (let i = 0; i < 3 * uvWidth; i++) bestUV[uvRow + i] += targetUV[uvRow + i] - bestRGBUV[i]; |
| 156 | + } |
| 157 | + if (iteration > 0 && (sum < threshold || sum > previousSum)) break; |
| 158 | + previousSum = sum; |
| 159 | + } |
| 160 | + |
| 161 | + // to YUV |
| 162 | + for (let j = 0; j < height; j++) { |
| 163 | + const uvRow = (j >> 1) * 3 * uvWidth; |
| 164 | + for (let i = 0; i < width; i++) { |
| 165 | + const W = bestY[j * w + i], o = uvRow + (i >> 1); |
| 166 | + const r = bestUV[o] + W, g = bestUV[o + uvWidth] + W, b = bestUV[o + 2 * uvWidth] + W; |
| 167 | + y[j * width + i] = clip8(yuv(r, g, b, 16839, 33059, 6420, 16)); |
| 168 | + } |
| 169 | + } |
| 170 | + for (let j = 0; j < uvHeight; j++) |
| 171 | + for (let i = 0; i < uvWidth; i++) { |
| 172 | + const o = j * 3 * uvWidth + i; |
| 173 | + const r = bestUV[o], g = bestUV[o + uvWidth], b = bestUV[o + 2 * uvWidth]; |
| 174 | + u[j * uvWidth + i] = clip8(yuv(r, g, b, -9719, -19081, 28800, 128)); |
| 175 | + v[j * uvWidth + i] = clip8(yuv(r, g, b, 28800, -24116, -4684, 128)); |
| 176 | + } |
| 177 | +} |
0 commit comments