Commit | Line | Data |
---|---|---|
72b9787e JB |
1 | /***************************************************************************** |
2 | * Copyright (C) 2013 x265 project | |
3 | * | |
4 | * Authors: Selvakumar Nithiyaruban <selvakumar@multicorewareinc.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. | |
19 | * | |
20 | * This program is also available under a commercial proprietary license. | |
21 | * For more information, contact us at license @ x265.com. | |
22 | *****************************************************************************/ | |
23 | ||
24 | #include "filters.h" | |
25 | #include "common.h" | |
26 | ||
27 | /* The dithering algorithm is based on Sierra-2-4A error diffusion. */ | |
28 | void ditherPlane(pixel *dst, int dstStride, uint16_t *src, int srcStride, | |
29 | int width, int height, int16_t *errors, int bitDepth) | |
30 | { | |
31 | const int lShift = 16 - bitDepth; | |
32 | const int rShift = 16 - bitDepth + 2; | |
33 | const int half = (1 << (16 - bitDepth + 1)); | |
34 | const int pixelMax = (1 << bitDepth) - 1; | |
35 | ||
36 | memset(errors, 0, (width + 1) * sizeof(int16_t)); | |
37 | int pitch = 1; | |
38 | for (int y = 0; y < height; y++, src += srcStride, dst += dstStride) | |
39 | { | |
40 | int16_t err = 0; | |
41 | for (int x = 0; x < width; x++) | |
42 | { | |
43 | err = err * 2 + errors[x] + errors[x + 1]; | |
44 | dst[x * pitch] = (pixel)Clip3(0, pixelMax, ((src[x * 1] << 2) + err + half) >> rShift); | |
45 | errors[x] = err = src[x * pitch] - (dst[x * pitch] << lShift); | |
46 | } | |
47 | } | |
48 | } | |
49 | ||
50 | void ditherImage(x265_picture& picIn, int picWidth, int picHeight, int16_t *errorBuf, int bitDepth) | |
51 | { | |
52 | /* This portion of code is from readFrame in x264. */ | |
53 | for (int i = 0; i < x265_cli_csps[picIn.colorSpace].planes; i++) | |
54 | { | |
55 | if ((picIn.bitDepth & 7) && (picIn.bitDepth != 16)) | |
56 | { | |
57 | /* upconvert non 16bit high depth planes to 16bit */ | |
58 | uint16_t *plane = (uint16_t*)picIn.planes[i]; | |
59 | uint32_t pixelCount = x265_picturePlaneSize(picIn.colorSpace, picWidth, picHeight, i); | |
60 | int lShift = 16 - picIn.bitDepth; | |
61 | ||
62 | /* This loop assumes width is equal to stride which | |
63 | happens to be true for file reader outputs */ | |
64 | for (uint32_t j = 0; j < pixelCount; j++) | |
65 | { | |
66 | plane[j] = plane[j] << lShift; | |
67 | } | |
68 | } | |
69 | } | |
70 | ||
71 | for (int i = 0; i < x265_cli_csps[picIn.colorSpace].planes; i++) | |
72 | { | |
73 | int height = (int)(picHeight >> x265_cli_csps[picIn.colorSpace].height[i]); | |
74 | int width = (int)(picWidth >> x265_cli_csps[picIn.colorSpace].width[i]); | |
75 | ||
76 | ditherPlane(((pixel*)picIn.planes[i]), picIn.stride[i] / sizeof(pixel), ((uint16_t*)picIn.planes[i]), | |
77 | picIn.stride[i] / 2, width, height, errorBuf, bitDepth); | |
78 | } | |
79 | } |