|
| 1 | +From 2c8d963dfc0b967e6c78259ba0a99185b27206d8 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Dirk Farin < [email protected]> |
| 3 | +Date: Mon, 12 Jul 2021 14:10:20 +0200 |
| 4 | +Subject: [PATCH] Op_RGB24_32_to_YCbCr: move chroma 4:2:0 sampling position to |
| 5 | + center (#521) |
| 6 | + |
| 7 | +--- |
| 8 | + libheif/heif_colorconversion.cc | 166 +++++++++++++++++++++++++++++--- |
| 9 | + 1 file changed, 150 insertions(+), 16 deletions(-) |
| 10 | + |
| 11 | +diff --git a/libheif/heif_colorconversion.cc b/libheif/heif_colorconversion.cc |
| 12 | +index 7e420912..8803ffc8 100644 |
| 13 | +--- a/libheif/heif_colorconversion.cc |
| 14 | ++++ b/libheif/heif_colorconversion.cc |
| 15 | +@@ -34,6 +34,9 @@ using namespace heif; |
| 16 | + #define DEBUG_ME 0 |
| 17 | + #define DEBUG_PIPELINE_CREATION 0 |
| 18 | + |
| 19 | ++#define USE_CENTER_CHROMA_422 0 |
| 20 | ++ |
| 21 | ++ |
| 22 | + std::ostream& operator<<(std::ostream& ostr, heif_colorspace c) |
| 23 | + { |
| 24 | + switch (c) { |
| 25 | +@@ -1944,6 +1947,25 @@ static inline uint8_t clip_f_u8(float fx) |
| 26 | + } |
| 27 | + |
| 28 | + |
| 29 | ++inline void set_chroma_pixels(uint8_t* out_cb, uint8_t* out_cr, |
| 30 | ++ uint8_t r, uint8_t g, uint8_t b, |
| 31 | ++ const RGB_to_YCbCr_coefficients& coeffs, |
| 32 | ++ bool full_range_flag) |
| 33 | ++{ |
| 34 | ++ float cb = r * coeffs.c[1][0] + g * coeffs.c[1][1] + b * coeffs.c[1][2]; |
| 35 | ++ float cr = r * coeffs.c[2][0] + g * coeffs.c[2][1] + b * coeffs.c[2][2]; |
| 36 | ++ |
| 37 | ++ if (full_range_flag) { |
| 38 | ++ *out_cb = clip_f_u8(cb + 128); |
| 39 | ++ *out_cr = clip_f_u8(cr + 128); |
| 40 | ++ } |
| 41 | ++ else { |
| 42 | ++ *out_cb = (uint8_t) clip_f_u8(cb * 0.875f + 128.0f); |
| 43 | ++ *out_cr = (uint8_t) clip_f_u8(cr * 0.875f + 128.0f); |
| 44 | ++ } |
| 45 | ++} |
| 46 | ++ |
| 47 | ++ |
| 48 | + std::shared_ptr<HeifPixelImage> |
| 49 | + Op_RGB24_32_to_YCbCr::convert_colorspace(const std::shared_ptr<const HeifPixelImage>& input, |
| 50 | + ColorState target_state, |
| 51 | +@@ -2024,30 +2046,142 @@ Op_RGB24_32_to_YCbCr::convert_colorspace(const std::shared_ptr<const HeifPixelIm |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +- for (int y = 0; y < height; y += chromaSubV) { |
| 56 | +- const uint8_t* p = &in_p[y * in_stride]; |
| 57 | ++ if (chromaSubH == 1 && chromaSubV == 1) { |
| 58 | ++ // chroma 4:4:4 |
| 59 | + |
| 60 | +- for (int x = 0; x < width; x += chromaSubH) { |
| 61 | +- uint8_t r = p[0]; |
| 62 | +- uint8_t g = p[1]; |
| 63 | +- uint8_t b = p[2]; |
| 64 | +- p += bytes_per_pixel * chromaSubH; |
| 65 | ++ for (int y = 0; y < height; y++) { |
| 66 | ++ const uint8_t* p = &in_p[y * in_stride]; |
| 67 | + |
| 68 | +- float cb = r * coeffs.c[1][0] + g * coeffs.c[1][1] + b * coeffs.c[1][2]; |
| 69 | +- float cr = r * coeffs.c[2][0] + g * coeffs.c[2][1] + b * coeffs.c[2][2]; |
| 70 | ++ for (int x = 0; x < width; x++) { |
| 71 | ++ uint8_t r = p[0]; |
| 72 | ++ uint8_t g = p[1]; |
| 73 | ++ uint8_t b = p[2]; |
| 74 | ++ p += bytes_per_pixel; |
| 75 | ++ |
| 76 | ++ set_chroma_pixels(out_cb + y * out_cb_stride + x, |
| 77 | ++ out_cr + y * out_cr_stride + x, |
| 78 | ++ r, g, b, |
| 79 | ++ coeffs, full_range_flag); |
| 80 | ++ } |
| 81 | ++ } |
| 82 | ++ } |
| 83 | ++ else if (chromaSubH == 2 && chromaSubV == 2) { |
| 84 | ++ // chroma 4:2:0 |
| 85 | + |
| 86 | +- if (full_range_flag) { |
| 87 | +- out_cb[(y / chromaSubV) * out_cb_stride + (x / chromaSubH)] = clip_f_u8(cb + 128); |
| 88 | +- out_cr[(y / chromaSubV) * out_cr_stride + (x / chromaSubH)] = clip_f_u8(cr + 128); |
| 89 | ++ for (int y = 0; y < (height & ~1); y += 2) { |
| 90 | ++ const uint8_t* p = &in_p[y * in_stride]; |
| 91 | ++ |
| 92 | ++ for (int x = 0; x < (width & ~1); x += 2) { |
| 93 | ++ uint8_t r = uint8_t((p[0] + p[bytes_per_pixel + 0] + p[in_stride + 0] + p[bytes_per_pixel + in_stride + 0]) / 4); |
| 94 | ++ uint8_t g = uint8_t((p[1] + p[bytes_per_pixel + 1] + p[in_stride + 1] + p[bytes_per_pixel + in_stride + 1]) / 4); |
| 95 | ++ uint8_t b = uint8_t((p[2] + p[bytes_per_pixel + 2] + p[in_stride + 2] + p[bytes_per_pixel + in_stride + 2]) / 4); |
| 96 | ++ |
| 97 | ++ p += bytes_per_pixel * 2; |
| 98 | ++ |
| 99 | ++ set_chroma_pixels(out_cb + (y / 2) * out_cb_stride + (x / 2), |
| 100 | ++ out_cr + (y / 2) * out_cr_stride + (x / 2), |
| 101 | ++ r, g, b, |
| 102 | ++ coeffs, full_range_flag); |
| 103 | + } |
| 104 | +- else { |
| 105 | +- out_cb[(y / chromaSubV) * out_cb_stride + (x / chromaSubH)] = (uint8_t) clip_f_u8(cb * 0.875f + 128.0f); |
| 106 | +- out_cr[(y / chromaSubV) * out_cr_stride + (x / chromaSubH)] = (uint8_t) clip_f_u8(cr * 0.875f + 128.0f); |
| 107 | ++ } |
| 108 | ++ |
| 109 | ++ // 4:2:0 right column (if odd width) |
| 110 | ++ if (width & 1) { |
| 111 | ++ int x = width - 1; |
| 112 | ++ const uint8_t* p = &in_p[x * bytes_per_pixel]; |
| 113 | ++ |
| 114 | ++ for (int y = 0; y < height; y += 2) { |
| 115 | ++ uint8_t r, g, b; |
| 116 | ++ if (y + 1 < height) { |
| 117 | ++ r = uint8_t((p[0] + p[in_stride + 0]) / 2); |
| 118 | ++ g = uint8_t((p[1] + p[in_stride + 1]) / 2); |
| 119 | ++ b = uint8_t((p[2] + p[in_stride + 2]) / 2); |
| 120 | ++ } |
| 121 | ++ else { |
| 122 | ++ r = p[0]; |
| 123 | ++ g = p[1]; |
| 124 | ++ b = p[2]; |
| 125 | ++ } |
| 126 | ++ |
| 127 | ++ set_chroma_pixels(out_cb + (y / 2) * out_cb_stride + (x / 2), |
| 128 | ++ out_cr + (y / 2) * out_cr_stride + (x / 2), |
| 129 | ++ r, g, b, |
| 130 | ++ coeffs, full_range_flag); |
| 131 | ++ |
| 132 | ++ p += in_stride * 2; |
| 133 | ++ } |
| 134 | ++ } |
| 135 | ++ |
| 136 | ++ // 4:2:0 bottom row (if odd height) |
| 137 | ++ if (height & 1) { |
| 138 | ++ int y = height - 1; |
| 139 | ++ const uint8_t* p = &in_p[y * in_stride]; |
| 140 | ++ |
| 141 | ++ for (int x = 0; x < width; x += 2) { |
| 142 | ++ uint8_t r, g, b; |
| 143 | ++ if (x + 1 < width) { |
| 144 | ++ r = uint8_t((p[0] + p[bytes_per_pixel + 0]) / 2); |
| 145 | ++ g = uint8_t((p[1] + p[bytes_per_pixel + 1]) / 2); |
| 146 | ++ b = uint8_t((p[2] + p[bytes_per_pixel + 2]) / 2); |
| 147 | ++ } |
| 148 | ++ else { |
| 149 | ++ r = p[0]; |
| 150 | ++ g = p[1]; |
| 151 | ++ b = p[2]; |
| 152 | ++ } |
| 153 | ++ |
| 154 | ++ set_chroma_pixels(out_cb + (y / 2) * out_cb_stride + (x / 2), |
| 155 | ++ out_cr + (y / 2) * out_cr_stride + (x / 2), |
| 156 | ++ r, g, b, |
| 157 | ++ coeffs, full_range_flag); |
| 158 | ++ |
| 159 | ++ p += bytes_per_pixel * 2; |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | ++ else if (chromaSubH == 2 && chromaSubV == 1) { |
| 164 | ++ // chroma 4:2:2 |
| 165 | ++ |
| 166 | ++ for (int y = 0; y < height; y++) { |
| 167 | ++ const uint8_t* p = &in_p[y * in_stride]; |
| 168 | ++ |
| 169 | ++ for (int x = 0; x < width; x += 2) { |
| 170 | ++ uint8_t r, g, b; |
| 171 | ++ |
| 172 | ++ // TODO: it still is an open question where the 'correct' chroma sample positions are for 4:2:2 |
| 173 | ++ // Since 4:2:2 is primarily used for video content and as there is no way to signal center position for h.265, |
| 174 | ++ // we currently use left-aligned sampling. See the discussion here: https://github.com/strukturag/libheif/issues/521 |
| 175 | ++#if USE_CENTER_CHROMA_422 |
| 176 | ++ if (x + 1 < width) { |
| 177 | ++ r = uint8_t((p[0] + p[bytes_per_pixel + 0]) / 2); |
| 178 | ++ g = uint8_t((p[1] + p[bytes_per_pixel + 1]) / 2); |
| 179 | ++ b = uint8_t((p[2] + p[bytes_per_pixel + 2]) / 2); |
| 180 | ++ } |
| 181 | ++ else { |
| 182 | ++ r = p[0]; |
| 183 | ++ g = p[1]; |
| 184 | ++ b = p[2]; |
| 185 | ++ } |
| 186 | ++#else |
| 187 | ++ r = p[0]; |
| 188 | ++ g = p[1]; |
| 189 | ++ b = p[2]; |
| 190 | ++#endif |
| 191 | ++ |
| 192 | ++ p += bytes_per_pixel * 2; |
| 193 | ++ |
| 194 | ++ set_chroma_pixels(out_cb + y * out_cb_stride + (x / 2), |
| 195 | ++ out_cr + y * out_cr_stride + (x / 2), |
| 196 | ++ r, g, b, |
| 197 | ++ coeffs, full_range_flag); |
| 198 | ++ } |
| 199 | ++ } |
| 200 | ++ } |
| 201 | ++ |
| 202 | + |
| 203 | + if (has_alpha) { |
| 204 | ++ assert(bytes_per_pixel == 4); |
| 205 | ++ |
| 206 | + for (int y = 0; y < height; y++) { |
| 207 | + for (int x = 0; x < width; x++) { |
| 208 | + uint8_t a = in_p[y * in_stride + x * 4 + 3]; |
| 209 | +@@ -2460,7 +2594,7 @@ Op_to_sdr_planes::convert_colorspace(const std::shared_ptr<const HeifPixelImage> |
| 210 | + if (input->has_channel(channel)) { |
| 211 | + int input_bits = input->get_bits_per_pixel(channel); |
| 212 | + |
| 213 | +- if (input_bits>8) { |
| 214 | ++ if (input_bits > 8) { |
| 215 | + int width = input->get_width(channel); |
| 216 | + int height = input->get_height(channel); |
| 217 | + outimg->add_plane(channel, width, height, 8); |
0 commit comments