+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * src/avx_color.c - Color format conversion and alpha blending |
| 7 | */ |
| 8 | |
| 9 | #include "avx_color.h" |
| 10 | #include "avx_simd.h" |
| 11 | |
| 12 | void avx_color_uyvy_to_rgba(const uint8_t *src, uint8_t *dst, int width, int height) { |
| 13 | avx_simd_uyvy_to_rgba(src, dst, width, height); |
| 14 | } |
| 15 | |
| 16 | void avx_color_uyvy_to_bgra(const uint8_t *src, uint8_t *dst, int width, int height) { |
| 17 | avx_simd_uyvy_to_bgra(src, dst, width, height); |
| 18 | } |
| 19 | |
| 20 | void avx_color_rgba_to_uyvy(const uint8_t *src, uint8_t *dst, int width, int height) { |
| 21 | avx_simd_rgba_to_uyvy(src, dst, width, height); |
| 22 | } |
| 23 | |
| 24 | void avx_color_bgra_to_uyvy(const uint8_t *src, uint8_t *dst, int width, int height) { |
| 25 | avx_simd_bgra_to_uyvy(src, dst, width, height); |
| 26 | } |
| 27 | |
| 28 | void avx_color_rgba_to_bgra(const uint8_t *src, uint8_t *dst, int width, int height) { |
| 29 | avx_simd_rgba_to_bgra(src, dst, width, height); |
| 30 | } |
| 31 | |
| 32 | void avx_color_alpha_blend_rgba(const uint8_t *fg, const uint8_t *bg, uint8_t *out, int width, int height) { |
| 33 | avx_simd_alpha_blend_rgba(fg, bg, out, width, height); |
| 34 | } |
| 35 | |
| 36 | /* Unpacks 16-bit planar P216 to 8-bit packed UYVY, discarding lower 8 bits of precision. */ |
| 37 | void avx_color_p216_to_uyvy(const uint8_t *src, uint8_t *dst, int width, int height) { |
| 38 | if (!src || !dst || width <= 0 || height <= 0) return; |
| 39 | const uint16_t *src_y = (const uint16_t*)src; |
| 40 | const uint16_t *src_uv = (const uint16_t*)(src + (size_t)width * height * 2); |
| 41 | size_t num_pairs = ((size_t)width * height) / 2; |
| 42 | |
| 43 | for (size_t i = 0; i < num_pairs; ++i) { |
| 44 | uint8_t y0 = (uint8_t)(src_y[2 * i + 0] >> 8); |
| 45 | uint8_t y1 = (uint8_t)(src_y[2 * i + 1] >> 8); |
| 46 | uint8_t u = (uint8_t)(src_uv[2 * i + 0] >> 8); |
| 47 | uint8_t v = (uint8_t)(src_uv[2 * i + 1] >> 8); |
| 48 | |
| 49 | dst[4 * i + 0] = u; |
| 50 | dst[4 * i + 1] = y0; |
| 51 | dst[4 * i + 2] = v; |
| 52 | dst[4 * i + 3] = y1; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /* Expands 8-bit packed UYVY to 16-bit planar P216, replicating 8-bit values into both bytes. */ |
| 57 | void avx_color_uyvy_to_p216(const uint8_t *src, uint8_t *dst, int width, int height) { |
| 58 | if (!src || !dst || width <= 0 || height <= 0) return; |
| 59 | uint16_t *dst_y = (uint16_t*)dst; |
| 60 | uint16_t *dst_uv = (uint16_t*)(dst + (size_t)width * height * 2); |
| 61 | size_t num_pairs = ((size_t)width * height) / 2; |
| 62 | |
| 63 | for (size_t i = 0; i < num_pairs; ++i) { |
| 64 | uint8_t u = src[4 * i + 0]; |
| 65 | uint8_t y0 = src[4 * i + 1]; |
| 66 | uint8_t v = src[4 * i + 2]; |
| 67 | uint8_t y1 = src[4 * i + 3]; |
| 68 | |
| 69 | dst_y[2 * i + 0] = (uint16_t)((y0 << 8) | y0); |
| 70 | dst_y[2 * i + 1] = (uint16_t)((y1 << 8) | y1); |
| 71 | dst_uv[2 * i + 0] = (uint16_t)((u << 8) | u); |
| 72 | dst_uv[2 * i + 1] = (uint16_t)((v << 8) | v); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void avx_color_p216_to_rgba(const uint8_t *src, uint8_t *dst, int width, int height) { |
| 77 | if (!src || !dst || width <= 0 || height <= 0) return; |
| 78 | const uint16_t *src_y = (const uint16_t*)src; |
| 79 | const uint16_t *src_uv = (const uint16_t*)(src + (size_t)width * height * 2); |
| 80 | size_t num_pairs = ((size_t)width * height) / 2; |
| 81 | |
| 82 | for (size_t i = 0; i < num_pairs; ++i) { |
| 83 | int u = src_uv[2 * i + 0] >> 8; |
| 84 | int y0 = src_y[2 * i + 0] >> 8; |
| 85 | int v = src_uv[2 * i + 1] >> 8; |
| 86 | int y1 = src_y[2 * i + 1] >> 8; |
| 87 | |
| 88 | int c0 = y0 - 16; |
| 89 | int c1 = y1 - 16; |
| 90 | int d_u = u - 128; |
| 91 | int e_v = v - 128; |
| 92 | |
| 93 | int r0 = (298 * c0 + 409 * e_v + 128) >> 8; |
| 94 | int g0 = (298 * c0 - 100 * d_u - 208 * e_v + 128) >> 8; |
| 95 | int b0 = (298 * c0 + 516 * d_u + 128) >> 8; |
| 96 | |
| 97 | int r1 = (298 * c1 + 409 * e_v + 128) >> 8; |
| 98 | int g1 = (298 * c1 - 100 * d_u - 208 * e_v + 128) >> 8; |
| 99 | int b1 = (298 * c1 + 516 * d_u + 128) >> 8; |
| 100 | |
| 101 | dst[8 * i + 0] = (uint8_t)(r0 < 0 ? 0 : (r0 > 255 ? 255 : r0)); |
| 102 | dst[8 * i + 1] = (uint8_t)(g0 < 0 ? 0 : (g0 > 255 ? 255 : g0)); |
| 103 | dst[8 * i + 2] = (uint8_t)(b0 < 0 ? 0 : (b0 > 255 ? 255 : b0)); |
| 104 | dst[8 * i + 3] = 255; |
| 105 | |
| 106 | dst[8 * i + 4] = (uint8_t)(r1 < 0 ? 0 : (r1 > 255 ? 255 : r1)); |
| 107 | dst[8 * i + 5] = (uint8_t)(g1 < 0 ? 0 : (g1 > 255 ? 255 : g1)); |
| 108 | dst[8 * i + 6] = (uint8_t)(b1 < 0 ? 0 : (b1 > 255 ? 255 : b1)); |
| 109 | dst[8 * i + 7] = 255; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | * Converts 4:2:2:4 UYVA to 32-bit RGBA, converting UYVY colorspace and copying |
| 115 | * independent 8-bit alpha plane directly into destination alpha channel. |
| 116 | */ |
| 117 | void avx_color_uyva_to_rgba(const uint8_t *uyvy, const uint8_t *alpha, uint8_t *dst, int width, int height) { |
| 118 | if (!uyvy || !dst || width <= 0 || height <= 0) return; |
| 119 | avx_color_uyvy_to_rgba(uyvy, dst, width, height); |
| 120 | if (alpha) { |
| 121 | size_t total = (size_t)width * height; |
| 122 | for (size_t i = 0; i < total; ++i) { |
| 123 | dst[i * 4 + 3] = alpha[i]; |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void avx_color_uyva_to_bgra(const uint8_t *uyvy, const uint8_t *alpha, uint8_t *dst, int width, int height) { |
| 129 | if (!uyvy || !dst || width <= 0 || height <= 0) return; |
| 130 | avx_color_uyvy_to_bgra(uyvy, dst, width, height); |
| 131 | if (alpha) { |
| 132 | size_t total = (size_t)width * height; |
| 133 | for (size_t i = 0; i < total; ++i) { |
| 134 | dst[i * 4 + 3] = alpha[i]; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void avx_color_rgba_to_uyva(const uint8_t *src, uint8_t *uyvy, uint8_t *alpha, int width, int height) { |
| 140 | if (!src || !uyvy || width <= 0 || height <= 0) return; |
| 141 | avx_color_rgba_to_uyvy(src, uyvy, width, height); |
| 142 | if (alpha) { |
| 143 | size_t total = (size_t)width * height; |
| 144 | for (size_t i = 0; i < total; ++i) { |
| 145 | alpha[i] = src[i * 4 + 3]; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Upsamples bi-planar NV12 (4:2:0) to packed UYVY (4:2:2), vertically duplicating |
| 152 | * interleaved UV chroma samples across adjacent even/odd scanlines. |
| 153 | */ |
| 154 | void avx_color_nv12_to_uyvy(const uint8_t *y_src, size_t y_stride, |
| 155 | const uint8_t *uv_src, size_t uv_stride, |
| 156 | uint8_t *dst, size_t dst_stride, |
| 157 | int width, int height) |
| 158 | { |
| 159 | if (!y_src || !uv_src || !dst || width <= 0 || height <= 0) return; |
| 160 | if (y_stride == 0) y_stride = (size_t)width; |
| 161 | if (uv_stride == 0) uv_stride = (size_t)width; |
| 162 | if (dst_stride == 0) dst_stride = (size_t)width * 2; |
| 163 | |
| 164 | int half_h = (height + 1) / 2; |
| 165 | int half_w = (width + 1) / 2; |
| 166 | |
| 167 | for (int y2 = 0; y2 < half_h; ++y2) { |
| 168 | int y0 = y2 * 2; |
| 169 | int y1 = y0 + 1; |
| 170 | const uint8_t *y_row0 = y_src + (size_t)y0 * y_stride; |
| 171 | const uint8_t *y_row1 = (y1 < height) ? (y_src + (size_t)y1 * y_stride) : y_row0; |
| 172 | const uint8_t *uv_row = uv_src + (size_t)y2 * uv_stride; |
| 173 | uint8_t *dst_row0 = dst + (size_t)y0 * dst_stride; |
| 174 | uint8_t *dst_row1 = (y1 < height) ? (dst + (size_t)y1 * dst_stride) : NULL; |
| 175 | |
| 176 | for (int x2 = 0; x2 < half_w; ++x2) { |
| 177 | uint8_t u = uv_row[2 * x2 + 0]; |
| 178 | uint8_t v = uv_row[2 * x2 + 1]; |
| 179 | uint8_t y00 = y_row0[2 * x2 + 0]; |
| 180 | uint8_t y01 = (2 * x2 + 1 < width) ? y_row0[2 * x2 + 1] : y00; |
| 181 | |
| 182 | dst_row0[4 * x2 + 0] = u; |
| 183 | dst_row0[4 * x2 + 1] = y00; |
| 184 | dst_row0[4 * x2 + 2] = v; |
| 185 | dst_row0[4 * x2 + 3] = y01; |
| 186 | |
| 187 | if (dst_row1) { |
| 188 | uint8_t y10 = y_row1[2 * x2 + 0]; |
| 189 | uint8_t y11 = (2 * x2 + 1 < width) ? y_row1[2 * x2 + 1] : y10; |
| 190 | dst_row1[4 * x2 + 0] = u; |
| 191 | dst_row1[4 * x2 + 1] = y10; |
| 192 | dst_row1[4 * x2 + 2] = v; |
| 193 | dst_row1[4 * x2 + 3] = y11; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static inline uint8_t avx_clamp_u8(int val) { |
| 200 | return (uint8_t)(val < 0 ? 0 : (val > 255 ? 255 : val)); |
| 201 | } |
| 202 | |
| 203 | static void avx_color_nv12_to_rgb32(const uint8_t *y_src, size_t y_stride, |
| 204 | const uint8_t *uv_src, size_t uv_stride, |
| 205 | uint8_t *dst, size_t dst_stride, |
| 206 | int width, int height, |
| 207 | bool is_bgra) |
| 208 | { |
| 209 | if (!y_src || !uv_src || !dst || width <= 0 || height <= 0) return; |
| 210 | if (y_stride == 0) y_stride = (size_t)width; |
| 211 | if (uv_stride == 0) uv_stride = (size_t)width; |
| 212 | if (dst_stride == 0) dst_stride = (size_t)width * 4; |
| 213 | |
| 214 | int half_h = (height + 1) / 2; |
| 215 | int half_w = (width + 1) / 2; |
| 216 | const int r_off = is_bgra ? 2 : 0; |
| 217 | const int b_off = is_bgra ? 0 : 2; |
| 218 | |
| 219 | for (int y2 = 0; y2 < half_h; ++y2) { |
| 220 | int y0 = y2 * 2; |
| 221 | int y1 = y0 + 1; |
| 222 | const uint8_t *y_row0 = y_src + (size_t)y0 * y_stride; |
| 223 | const uint8_t *y_row1 = (y1 < height) ? (y_src + (size_t)y1 * y_stride) : y_row0; |
| 224 | const uint8_t *uv_row = uv_src + (size_t)y2 * uv_stride; |
| 225 | uint8_t *dst_row0 = dst + (size_t)y0 * dst_stride; |
| 226 | uint8_t *dst_row1 = (y1 < height) ? (dst + (size_t)y1 * dst_stride) : NULL; |
| 227 | |
| 228 | for (int x2 = 0; x2 < half_w; ++x2) { |
| 229 | int u = uv_row[2 * x2 + 0]; |
| 230 | int v = uv_row[2 * x2 + 1]; |
| 231 | int d_u = u - 128; |
| 232 | int e_v = v - 128; |
| 233 | |
| 234 | /* Row 0, pixel 0 */ |
| 235 | int c00 = y_row0[2 * x2 + 0] - 16; |
| 236 | dst_row0[8 * x2 + r_off] = avx_clamp_u8((298 * c00 + 409 * e_v + 128) >> 8); |
| 237 | dst_row0[8 * x2 + 1] = avx_clamp_u8((298 * c00 - 100 * d_u - 208 * e_v + 128) >> 8); |
| 238 | dst_row0[8 * x2 + b_off] = avx_clamp_u8((298 * c00 + 516 * d_u + 128) >> 8); |
| 239 | dst_row0[8 * x2 + 3] = 255; |
| 240 | |
| 241 | /* Row 0, pixel 1 */ |
| 242 | if (2 * x2 + 1 < width) { |
| 243 | int c01 = y_row0[2 * x2 + 1] - 16; |
| 244 | dst_row0[8 * x2 + 4 + r_off] = avx_clamp_u8((298 * c01 + 409 * e_v + 128) >> 8); |
| 245 | dst_row0[8 * x2 + 5] = avx_clamp_u8((298 * c01 - 100 * d_u - 208 * e_v + 128) >> 8); |
| 246 | dst_row0[8 * x2 + 4 + b_off] = avx_clamp_u8((298 * c01 + 516 * d_u + 128) >> 8); |
| 247 | dst_row0[8 * x2 + 7] = 255; |
| 248 | } |
| 249 | |
| 250 | /* Row 1, pixels 0 and 1 */ |
| 251 | if (dst_row1) { |
| 252 | int c10 = y_row1[2 * x2 + 0] - 16; |
| 253 | dst_row1[8 * x2 + r_off] = avx_clamp_u8((298 * c10 + 409 * e_v + 128) >> 8); |
| 254 | dst_row1[8 * x2 + 1] = avx_clamp_u8((298 * c10 - 100 * d_u - 208 * e_v + 128) >> 8); |
| 255 | dst_row1[8 * x2 + b_off] = avx_clamp_u8((298 * c10 + 516 * d_u + 128) >> 8); |
| 256 | dst_row1[8 * x2 + 3] = 255; |
| 257 | |
| 258 | if (2 * x2 + 1 < width) { |
| 259 | int c11 = y_row1[2 * x2 + 1] - 16; |
| 260 | dst_row1[8 * x2 + 4 + r_off] = avx_clamp_u8((298 * c11 + 409 * e_v + 128) >> 8); |
| 261 | dst_row1[8 * x2 + 5] = avx_clamp_u8((298 * c11 - 100 * d_u - 208 * e_v + 128) >> 8); |
| 262 | dst_row1[8 * x2 + 4 + b_off] = avx_clamp_u8((298 * c11 + 516 * d_u + 128) >> 8); |
| 263 | dst_row1[8 * x2 + 7] = 255; |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | void avx_color_nv12_to_rgba(const uint8_t *y_src, size_t y_stride, |
| 271 | const uint8_t *uv_src, size_t uv_stride, |
| 272 | uint8_t *dst, size_t dst_stride, |
| 273 | int width, int height) |
| 274 | { |
| 275 | avx_color_nv12_to_rgb32(y_src, y_stride, uv_src, uv_stride, dst, dst_stride, width, height, false); |
| 276 | } |
| 277 | |
| 278 | void avx_color_nv12_to_bgra(const uint8_t *y_src, size_t y_stride, |
| 279 | const uint8_t *uv_src, size_t uv_stride, |
| 280 | uint8_t *dst, size_t dst_stride, |
| 281 | int width, int height) |
| 282 | { |
| 283 | avx_color_nv12_to_rgb32(y_src, y_stride, uv_src, uv_stride, dst, dst_stride, width, height, true); |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * Upsamples tri-planar I420 (4:2:0) to packed UYVY (4:2:2), vertically duplicating |
| 288 | * planar U and V chroma samples across adjacent even/odd scanlines. |
| 289 | */ |
| 290 | void avx_color_i420_to_uyvy(const uint8_t *y_src, size_t y_stride, |
| 291 | const uint8_t *u_src, size_t u_stride, |
| 292 | const uint8_t *v_src, size_t v_stride, |
| 293 | uint8_t *dst, size_t dst_stride, |
| 294 | int width, int height) |
| 295 | { |
| 296 | if (!y_src || !u_src || !v_src || !dst || width <= 0 || height <= 0) return; |
| 297 | if (y_stride == 0) y_stride = (size_t)width; |
| 298 | if (u_stride == 0) u_stride = (size_t)(width + 1) / 2; |
| 299 | if (v_stride == 0) v_stride = (size_t)(width + 1) / 2; |
| 300 | if (dst_stride == 0) dst_stride = (size_t)width * 2; |
| 301 | |
| 302 | int half_h = (height + 1) / 2; |
| 303 | int half_w = (width + 1) / 2; |
| 304 | |
| 305 | for (int y2 = 0; y2 < half_h; ++y2) { |
| 306 | int y0 = y2 * 2; |
| 307 | int y1 = y0 + 1; |
| 308 | const uint8_t *y_row0 = y_src + (size_t)y0 * y_stride; |
| 309 | const uint8_t *y_row1 = (y1 < height) ? (y_src + (size_t)y1 * y_stride) : y_row0; |
| 310 | const uint8_t *u_row = u_src + (size_t)y2 * u_stride; |
| 311 | const uint8_t *v_row = v_src + (size_t)y2 * v_stride; |
| 312 | uint8_t *dst_row0 = dst + (size_t)y0 * dst_stride; |
| 313 | uint8_t *dst_row1 = (y1 < height) ? (dst + (size_t)y1 * dst_stride) : NULL; |
| 314 | |
| 315 | for (int x2 = 0; x2 < half_w; ++x2) { |
| 316 | uint8_t u = u_row[x2]; |
| 317 | uint8_t v = v_row[x2]; |
| 318 | uint8_t y00 = y_row0[2 * x2 + 0]; |
| 319 | uint8_t y01 = (2 * x2 + 1 < width) ? y_row0[2 * x2 + 1] : y00; |
| 320 | |
| 321 | dst_row0[4 * x2 + 0] = u; |
| 322 | dst_row0[4 * x2 + 1] = y00; |
| 323 | dst_row0[4 * x2 + 2] = v; |
| 324 | dst_row0[4 * x2 + 3] = y01; |
| 325 | |
| 326 | if (dst_row1) { |
| 327 | uint8_t y10 = y_row1[2 * x2 + 0]; |
| 328 | uint8_t y11 = (2 * x2 + 1 < width) ? y_row1[2 * x2 + 1] : y10; |
| 329 | dst_row1[4 * x2 + 0] = u; |
| 330 | dst_row1[4 * x2 + 1] = y10; |
| 331 | dst_row1[4 * x2 + 2] = v; |
| 332 | dst_row1[4 * x2 + 3] = y11; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void avx_color_i420_to_rgba(const uint8_t *y_src, size_t y_stride, |
| 339 | const uint8_t *u_src, size_t u_stride, |
| 340 | const uint8_t *v_src, size_t v_stride, |
| 341 | uint8_t *dst, size_t dst_stride, |
| 342 | int width, int height) |
| 343 | { |
| 344 | if (!y_src || !u_src || !v_src || !dst || width <= 0 || height <= 0) return; |
| 345 | if (y_stride == 0) y_stride = (size_t)width; |
| 346 | if (u_stride == 0) u_stride = (size_t)(width + 1) / 2; |
| 347 | if (v_stride == 0) v_stride = (size_t)(width + 1) / 2; |
| 348 | if (dst_stride == 0) dst_stride = (size_t)width * 4; |
| 349 | |
| 350 | int half_h = (height + 1) / 2; |
| 351 | int half_w = (width + 1) / 2; |
| 352 | |
| 353 | for (int y2 = 0; y2 < half_h; ++y2) { |
| 354 | int y0 = y2 * 2; |
| 355 | int y1 = y0 + 1; |
| 356 | const uint8_t *y_row0 = y_src + (size_t)y0 * y_stride; |
| 357 | const uint8_t *y_row1 = (y1 < height) ? (y_src + (size_t)y1 * y_stride) : y_row0; |
| 358 | const uint8_t *u_row = u_src + (size_t)y2 * u_stride; |
| 359 | const uint8_t *v_row = v_src + (size_t)y2 * v_stride; |
| 360 | uint8_t *dst_row0 = dst + (size_t)y0 * dst_stride; |
| 361 | uint8_t *dst_row1 = (y1 < height) ? (dst + (size_t)y1 * dst_stride) : NULL; |
| 362 | |
| 363 | for (int x2 = 0; x2 < half_w; ++x2) { |
| 364 | int u = u_row[x2]; |
| 365 | int v = v_row[x2]; |
| 366 | int d_u = u - 128; |
| 367 | int e_v = v - 128; |
| 368 | |
| 369 | /* Row 0, pixel 0 */ |
| 370 | int c00 = y_row0[2 * x2 + 0] - 16; |
| 371 | int r00 = (298 * c00 + 409 * e_v + 128) >> 8; |
| 372 | int g00 = (298 * c00 - 100 * d_u - 208 * e_v + 128) >> 8; |
| 373 | int b00 = (298 * c00 + 516 * d_u + 128) >> 8; |
| 374 | dst_row0[8 * x2 + 0] = (uint8_t)(r00 < 0 ? 0 : (r00 > 255 ? 255 : r00)); |
| 375 | dst_row0[8 * x2 + 1] = (uint8_t)(g00 < 0 ? 0 : (g00 > 255 ? 255 : g00)); |
| 376 | dst_row0[8 * x2 + 2] = (uint8_t)(b00 < 0 ? 0 : (b00 > 255 ? 255 : b00)); |
| 377 | dst_row0[8 * x2 + 3] = 255; |
| 378 | |
| 379 | /* Row 0, pixel 1 */ |
| 380 | if (2 * x2 + 1 < width) { |
| 381 | int c01 = y_row0[2 * x2 + 1] - 16; |
| 382 | int r01 = (298 * c01 + 409 * e_v + 128) >> 8; |
| 383 | int g01 = (298 * c01 - 100 * d_u - 208 * e_v + 128) >> 8; |
| 384 | int b01 = (298 * c01 + 516 * d_u + 128) >> 8; |
| 385 | dst_row0[8 * x2 + 4] = (uint8_t)(r01 < 0 ? 0 : (r01 > 255 ? 255 : r01)); |
| 386 | dst_row0[8 * x2 + 5] = (uint8_t)(g01 < 0 ? 0 : (g01 > 255 ? 255 : g01)); |
| 387 | dst_row0[8 * x2 + 6] = (uint8_t)(b01 < 0 ? 0 : (b01 > 255 ? 255 : b01)); |
| 388 | dst_row0[8 * x2 + 7] = 255; |
| 389 | } |
| 390 | |
| 391 | /* Row 1, pixels 0 and 1 */ |
| 392 | if (dst_row1) { |
| 393 | int c10 = y_row1[2 * x2 + 0] - 16; |
| 394 | int r10 = (298 * c10 + 409 * e_v + 128) >> 8; |
| 395 | int g10 = (298 * c10 - 100 * d_u - 208 * e_v + 128) >> 8; |
| 396 | int b10 = (298 * c10 + 516 * d_u + 128) >> 8; |
| 397 | dst_row1[8 * x2 + 0] = (uint8_t)(r10 < 0 ? 0 : (r10 > 255 ? 255 : r10)); |
| 398 | dst_row1[8 * x2 + 1] = (uint8_t)(g10 < 0 ? 0 : (g10 > 255 ? 255 : g10)); |
| 399 | dst_row1[8 * x2 + 2] = (uint8_t)(b10 < 0 ? 0 : (b10 > 255 ? 255 : b10)); |
| 400 | dst_row1[8 * x2 + 3] = 255; |
| 401 | |
| 402 | if (2 * x2 + 1 < width) { |
| 403 | int c11 = y_row1[2 * x2 + 1] - 16; |
| 404 | int r11 = (298 * c11 + 409 * e_v + 128) >> 8; |
| 405 | int g11 = (298 * c11 - 100 * d_u - 208 * e_v + 128) >> 8; |
| 406 | int b11 = (298 * c11 + 516 * d_u + 128) >> 8; |
| 407 | dst_row1[8 * x2 + 4] = (uint8_t)(r11 < 0 ? 0 : (r11 > 255 ? 255 : r11)); |
| 408 | dst_row1[8 * x2 + 5] = (uint8_t)(g11 < 0 ? 0 : (g11 > 255 ? 255 : g11)); |
| 409 | dst_row1[8 * x2 + 6] = (uint8_t)(b11 < 0 ? 0 : (b11 > 255 ? 255 : b11)); |
| 410 | dst_row1[8 * x2 + 7] = 255; |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Downsamples packed UYVY (4:2:2) to bi-planar NV12 (4:2:0), vertically averaging |
| 419 | * chroma pairs across two scanlines into interleaved UV planes. |
| 420 | */ |
| 421 | void avx_color_uyvy_to_nv12(const uint8_t *src, size_t src_stride, |
| 422 | uint8_t *y_dst, size_t y_stride, |
| 423 | uint8_t *uv_dst, size_t uv_stride, |
| 424 | int width, int height) |
| 425 | { |
| 426 | if (!src || !y_dst || !uv_dst || width <= 0 || height <= 0) return; |
| 427 | if (src_stride == 0) src_stride = (size_t)width * 2; |
| 428 | if (y_stride == 0) y_stride = (size_t)width; |
| 429 | if (uv_stride == 0) uv_stride = (size_t)width; |
| 430 | |
| 431 | int half_h = (height + 1) / 2; |
| 432 | int half_w = (width + 1) / 2; |
| 433 | |
| 434 | for (int y2 = 0; y2 < half_h; ++y2) { |
| 435 | int y0 = y2 * 2; |
| 436 | int y1 = y0 + 1; |
| 437 | const uint8_t *src_row0 = src + (size_t)y0 * src_stride; |
| 438 | const uint8_t *src_row1 = (y1 < height) ? (src + (size_t)y1 * src_stride) : src_row0; |
| 439 | uint8_t *y_out0 = y_dst + (size_t)y0 * y_stride; |
| 440 | uint8_t *y_out1 = (y1 < height) ? (y_dst + (size_t)y1 * y_stride) : NULL; |
| 441 | uint8_t *uv_out = uv_dst + (size_t)y2 * uv_stride; |
| 442 | |
| 443 | for (int x2 = 0; x2 < half_w; ++x2) { |
| 444 | uint8_t u0 = src_row0[4 * x2 + 0]; |
| 445 | uint8_t y00 = src_row0[4 * x2 + 1]; |
| 446 | uint8_t v0 = src_row0[4 * x2 + 2]; |
| 447 | uint8_t y01 = src_row0[4 * x2 + 3]; |
| 448 | |
| 449 | y_out0[2 * x2 + 0] = y00; |
| 450 | if (2 * x2 + 1 < width) y_out0[2 * x2 + 1] = y01; |
| 451 | |
| 452 | uint8_t u1 = u0; |
| 453 | uint8_t v1 = v0; |
| 454 | if (y_out1) { |
| 455 | u1 = src_row1[4 * x2 + 0]; |
| 456 | uint8_t y10 = src_row1[4 * x2 + 1]; |
| 457 | v1 = src_row1[4 * x2 + 2]; |
| 458 | uint8_t y11 = src_row1[4 * x2 + 3]; |
| 459 | |
| 460 | y_out1[2 * x2 + 0] = y10; |
| 461 | if (2 * x2 + 1 < width) y_out1[2 * x2 + 1] = y11; |
| 462 | } |
| 463 | |
| 464 | uv_out[2 * x2 + 0] = (uint8_t)(((int)u0 + (int)u1) / 2); |
| 465 | uv_out[2 * x2 + 1] = (uint8_t)(((int)v0 + (int)v1) / 2); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | void avx_color_uyvy_to_i420(const uint8_t *src, size_t src_stride, |
| 471 | uint8_t *y_dst, size_t y_stride, |
| 472 | uint8_t *u_dst, size_t u_stride, |
| 473 | uint8_t *v_dst, size_t v_stride, |
| 474 | int width, int height) |
| 475 | { |
| 476 | if (!src || !y_dst || !u_dst || !v_dst || width <= 0 || height <= 0) return; |
| 477 | if (src_stride == 0) src_stride = (size_t)width * 2; |
| 478 | if (y_stride == 0) y_stride = (size_t)width; |
| 479 | if (u_stride == 0) u_stride = (size_t)(width + 1) / 2; |
| 480 | if (v_stride == 0) v_stride = (size_t)(width + 1) / 2; |
| 481 | |
| 482 | int half_h = (height + 1) / 2; |
| 483 | int half_w = (width + 1) / 2; |
| 484 | |
| 485 | for (int y2 = 0; y2 < half_h; ++y2) { |
| 486 | int y0 = y2 * 2; |
| 487 | int y1 = y0 + 1; |
| 488 | const uint8_t *src_row0 = src + (size_t)y0 * src_stride; |
| 489 | const uint8_t *src_row1 = (y1 < height) ? (src + (size_t)y1 * src_stride) : src_row0; |
| 490 | uint8_t *y_out0 = y_dst + (size_t)y0 * y_stride; |
| 491 | uint8_t *y_out1 = (y1 < height) ? (y_dst + (size_t)y1 * y_stride) : NULL; |
| 492 | uint8_t *u_out = u_dst + (size_t)y2 * u_stride; |
| 493 | uint8_t *v_out = v_dst + (size_t)y2 * v_stride; |
| 494 | |
| 495 | for (int x2 = 0; x2 < half_w; ++x2) { |
| 496 | uint8_t u0 = src_row0[4 * x2 + 0]; |
| 497 | uint8_t y00 = src_row0[4 * x2 + 1]; |
| 498 | uint8_t v0 = src_row0[4 * x2 + 2]; |
| 499 | uint8_t y01 = src_row0[4 * x2 + 3]; |
| 500 | |
| 501 | y_out0[2 * x2 + 0] = y00; |
| 502 | if (2 * x2 + 1 < width) y_out0[2 * x2 + 1] = y01; |
| 503 | |
| 504 | uint8_t u1 = u0; |
| 505 | uint8_t v1 = v0; |
| 506 | if (y_out1) { |
| 507 | u1 = src_row1[4 * x2 + 0]; |
| 508 | uint8_t y10 = src_row1[4 * x2 + 1]; |
| 509 | v1 = src_row1[4 * x2 + 2]; |
| 510 | uint8_t y11 = src_row1[4 * x2 + 3]; |
| 511 | |
| 512 | y_out1[2 * x2 + 0] = y10; |
| 513 | if (2 * x2 + 1 < width) y_out1[2 * x2 + 1] = y11; |
| 514 | } |
| 515 | |
| 516 | u_out[x2] = (uint8_t)(((int)u0 + (int)u1) / 2); |
| 517 | v_out[x2] = (uint8_t)(((int)v0 + (int)v1) / 2); |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void avx_color_uyvy_to_yv12(const uint8_t *src, size_t src_stride, |
| 523 | uint8_t *y_dst, size_t y_stride, |
| 524 | uint8_t *v_dst, size_t v_stride, |
| 525 | uint8_t *u_dst, size_t u_stride, |
| 526 | int width, int height) |
| 527 | { |
| 528 | /* YV12 is identical to I420 with U and V destinations swapped */ |
| 529 | avx_color_uyvy_to_i420(src, src_stride, y_dst, y_stride, u_dst, u_stride, v_dst, v_stride, width, height); |
| 530 | } |
| 531 | |
| 532 | void avx_color_yv12_to_uyvy(const uint8_t *y_src, size_t y_stride, |
| 533 | const uint8_t *v_src, size_t v_stride, |
| 534 | const uint8_t *u_src, size_t u_stride, |
| 535 | uint8_t *dst, size_t dst_stride, |
| 536 | int width, int height) |
| 537 | { |
| 538 | /* YV12 has V as plane 1 and U as plane 2 */ |
| 539 | avx_color_i420_to_uyvy(y_src, y_stride, u_src, u_stride, v_src, v_stride, dst, dst_stride, width, height); |
| 540 | } |
| 541 | |
| 542 | void avx_color_yv12_to_rgba(const uint8_t *y_src, size_t y_stride, |
| 543 | const uint8_t *v_src, size_t v_stride, |
| 544 | const uint8_t *u_src, size_t u_stride, |
| 545 | uint8_t *dst, size_t dst_stride, |
| 546 | int width, int height) |
| 547 | { |
| 548 | /* YV12 has V as plane 1 and U as plane 2 */ |
| 549 | avx_color_i420_to_rgba(y_src, y_stride, u_src, u_stride, v_src, v_stride, dst, dst_stride, width, height); |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Downsamples 32-bit RGBA to bi-planar NV12 (4:2:0) with 2x2 box filtering for chroma |
| 554 | * and BT.601 studio swing clamping (Y in [16..235], UV in [16..240]). |
| 555 | */ |
| 556 | void avx_color_rgba_to_nv12(const uint8_t *src, size_t src_stride, |
| 557 | uint8_t *y_dst, size_t y_stride, |
| 558 | uint8_t *uv_dst, size_t uv_stride, |
| 559 | int width, int height) |
| 560 | { |
| 561 | if (!src || !y_dst || !uv_dst || width <= 0 || height <= 0) return; |
| 562 | if (src_stride == 0) src_stride = (size_t)width * 4; |
| 563 | if (y_stride == 0) y_stride = (size_t)width; |
| 564 | if (uv_stride == 0) uv_stride = (size_t)width; |
| 565 | |
| 566 | int half_h = (height + 1) / 2; |
| 567 | int half_w = (width + 1) / 2; |
| 568 | |
| 569 | for (int y2 = 0; y2 < half_h; ++y2) { |
| 570 | int y0 = y2 * 2; |
| 571 | int y1 = y0 + 1; |
| 572 | const uint8_t *src_row0 = src + (size_t)y0 * src_stride; |
| 573 | const uint8_t *src_row1 = (y1 < height) ? (src + (size_t)y1 * src_stride) : src_row0; |
| 574 | uint8_t *y_out0 = y_dst + (size_t)y0 * y_stride; |
| 575 | uint8_t *y_out1 = (y1 < height) ? (y_dst + (size_t)y1 * y_stride) : NULL; |
| 576 | uint8_t *uv_out = uv_dst + (size_t)y2 * uv_stride; |
| 577 | |
| 578 | for (int x2 = 0; x2 < half_w; ++x2) { |
| 579 | int x0 = x2 * 2; |
| 580 | int x1 = x0 + 1; |
| 581 | |
| 582 | int r00 = src_row0[4 * x0 + 0]; |
| 583 | int g00 = src_row0[4 * x0 + 1]; |
| 584 | int b00 = src_row0[4 * x0 + 2]; |
| 585 | |
| 586 | int r01 = r00, g01 = g00, b01 = b00; |
| 587 | if (x1 < width) { |
| 588 | r01 = src_row0[4 * x1 + 0]; |
| 589 | g01 = src_row0[4 * x1 + 1]; |
| 590 | b01 = src_row0[4 * x1 + 2]; |
| 591 | } |
| 592 | |
| 593 | int r10 = r00, g10 = g00, b10 = b00; |
| 594 | int r11 = r01, g11 = g01, b11 = b01; |
| 595 | if (y_out1) { |
| 596 | r10 = src_row1[4 * x0 + 0]; |
| 597 | g10 = src_row1[4 * x0 + 1]; |
| 598 | b10 = src_row1[4 * x0 + 2]; |
| 599 | if (x1 < width) { |
| 600 | r11 = src_row1[4 * x1 + 0]; |
| 601 | g11 = src_row1[4 * x1 + 1]; |
| 602 | b11 = src_row1[4 * x1 + 2]; |
| 603 | } else { |
| 604 | r11 = r10; g11 = g10; b11 = b10; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | int y_val00 = ((66 * r00 + 129 * g00 + 25 * b00 + 128) >> 8) + 16; |
| 609 | y_out0[x0] = (uint8_t)(y_val00 < 0 ? 0 : (y_val00 > 255 ? 255 : y_val00)); |
| 610 | if (x1 < width) { |
| 611 | int y_val01 = ((66 * r01 + 129 * g01 + 25 * b01 + 128) >> 8) + 16; |
| 612 | y_out0[x1] = (uint8_t)(y_val01 < 0 ? 0 : (y_val01 > 255 ? 255 : y_val01)); |
| 613 | } |
| 614 | |
| 615 | if (y_out1) { |
| 616 | int y_val10 = ((66 * r10 + 129 * g10 + 25 * b10 + 128) >> 8) + 16; |
| 617 | y_out1[x0] = (uint8_t)(y_val10 < 0 ? 0 : (y_val10 > 255 ? 255 : y_val10)); |
| 618 | if (x1 < width) { |
| 619 | int y_val11 = ((66 * r11 + 129 * g11 + 25 * b11 + 128) >> 8) + 16; |
| 620 | y_out1[x1] = (uint8_t)(y_val11 < 0 ? 0 : (y_val11 > 255 ? 255 : y_val11)); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | int r_avg = (r00 + r01 + r10 + r11 + 2) >> 2; |
| 625 | int g_avg = (g00 + g01 + g10 + g11 + 2) >> 2; |
| 626 | int b_avg = (b00 + b01 + b10 + b11 + 2) >> 2; |
| 627 | |
| 628 | int u_val = ((-38 * r_avg - 74 * g_avg + 112 * b_avg + 128) >> 8) + 128; |
| 629 | int v_val = ((112 * r_avg - 94 * g_avg - 18 * b_avg + 128) >> 8) + 128; |
| 630 | |
| 631 | uv_out[2 * x2 + 0] = (uint8_t)(u_val < 0 ? 0 : (u_val > 255 ? 255 : u_val)); |
| 632 | uv_out[2 * x2 + 1] = (uint8_t)(v_val < 0 ? 0 : (v_val > 255 ? 255 : v_val)); |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Downsamples 32-bit RGBA to tri-planar I420 (4:2:0) with 2x2 box filtering for chroma |
| 639 | * and BT.601 studio swing clamping. |
| 640 | */ |
| 641 | void avx_color_rgba_to_i420(const uint8_t *src, size_t src_stride, |
| 642 | uint8_t *y_dst, size_t y_stride, |
| 643 | uint8_t *u_dst, size_t u_stride, |
| 644 | uint8_t *v_dst, size_t v_stride, |
| 645 | int width, int height) |
| 646 | { |
| 647 | if (!src || !y_dst || !u_dst || !v_dst || width <= 0 || height <= 0) return; |
| 648 | if (src_stride == 0) src_stride = (size_t)width * 4; |
| 649 | if (y_stride == 0) y_stride = (size_t)width; |
| 650 | if (u_stride == 0) u_stride = (size_t)(width + 1) / 2; |
| 651 | if (v_stride == 0) v_stride = (size_t)(width + 1) / 2; |
| 652 | |
| 653 | int half_h = (height + 1) / 2; |
| 654 | int half_w = (width + 1) / 2; |
| 655 | |
| 656 | for (int y2 = 0; y2 < half_h; ++y2) { |
| 657 | int y0 = y2 * 2; |
| 658 | int y1 = y0 + 1; |
| 659 | const uint8_t *src_row0 = src + (size_t)y0 * src_stride; |
| 660 | const uint8_t *src_row1 = (y1 < height) ? (src + (size_t)y1 * src_stride) : src_row0; |
| 661 | uint8_t *y_out0 = y_dst + (size_t)y0 * y_stride; |
| 662 | uint8_t *y_out1 = (y1 < height) ? (y_dst + (size_t)y1 * y_stride) : NULL; |
| 663 | uint8_t *u_out = u_dst + (size_t)y2 * u_stride; |
| 664 | uint8_t *v_out = v_dst + (size_t)y2 * v_stride; |
| 665 | |
| 666 | for (int x2 = 0; x2 < half_w; ++x2) { |
| 667 | int x0 = x2 * 2; |
| 668 | int x1 = x0 + 1; |
| 669 | |
| 670 | int r00 = src_row0[4 * x0 + 0]; |
| 671 | int g00 = src_row0[4 * x0 + 1]; |
| 672 | int b00 = src_row0[4 * x0 + 2]; |
| 673 | |
| 674 | int r01 = r00, g01 = g00, b01 = b00; |
| 675 | if (x1 < width) { |
| 676 | r01 = src_row0[4 * x1 + 0]; |
| 677 | g01 = src_row0[4 * x1 + 1]; |
| 678 | b01 = src_row0[4 * x1 + 2]; |
| 679 | } |
| 680 | |
| 681 | int r10 = r00, g10 = g00, b10 = b00; |
| 682 | int r11 = r01, g11 = g01, b11 = b01; |
| 683 | if (y_out1) { |
| 684 | r10 = src_row1[4 * x0 + 0]; |
| 685 | g10 = src_row1[4 * x0 + 1]; |
| 686 | b10 = src_row1[4 * x0 + 2]; |
| 687 | if (x1 < width) { |
| 688 | r11 = src_row1[4 * x1 + 0]; |
| 689 | g11 = src_row1[4 * x1 + 1]; |
| 690 | b11 = src_row1[4 * x1 + 2]; |
| 691 | } else { |
| 692 | r11 = r10; g11 = g10; b11 = b10; |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | int y_val00 = ((66 * r00 + 129 * g00 + 25 * b00 + 128) >> 8) + 16; |
| 697 | y_out0[x0] = (uint8_t)(y_val00 < 0 ? 0 : (y_val00 > 255 ? 255 : y_val00)); |
| 698 | if (x1 < width) { |
| 699 | int y_val01 = ((66 * r01 + 129 * g01 + 25 * b01 + 128) >> 8) + 16; |
| 700 | y_out0[x1] = (uint8_t)(y_val01 < 0 ? 0 : (y_val01 > 255 ? 255 : y_val01)); |
| 701 | } |
| 702 | |
| 703 | if (y_out1) { |
| 704 | int y_val10 = ((66 * r10 + 129 * g10 + 25 * b10 + 128) >> 8) + 16; |
| 705 | y_out1[x0] = (uint8_t)(y_val10 < 0 ? 0 : (y_val10 > 255 ? 255 : y_val10)); |
| 706 | if (x1 < width) { |
| 707 | int y_val11 = ((66 * r11 + 129 * g11 + 25 * b11 + 128) >> 8) + 16; |
| 708 | y_out1[x1] = (uint8_t)(y_val11 < 0 ? 0 : (y_val11 > 255 ? 255 : y_val11)); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | int r_avg = (r00 + r01 + r10 + r11 + 2) >> 2; |
| 713 | int g_avg = (g00 + g01 + g10 + g11 + 2) >> 2; |
| 714 | int b_avg = (b00 + b01 + b10 + b11 + 2) >> 2; |
| 715 | |
| 716 | int u_val = ((-38 * r_avg - 74 * g_avg + 112 * b_avg + 128) >> 8) + 128; |
| 717 | int v_val = ((112 * r_avg - 94 * g_avg - 18 * b_avg + 128) >> 8) + 128; |
| 718 | |
| 719 | u_out[x2] = (uint8_t)(u_val < 0 ? 0 : (u_val > 255 ? 255 : u_val)); |
| 720 | v_out[x2] = (uint8_t)(v_val < 0 ? 0 : (v_val > 255 ? 255 : v_val)); |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | void avx_color_rgba_to_yv12(const uint8_t *src, size_t src_stride, |
| 726 | uint8_t *y_dst, size_t y_stride, |
| 727 | uint8_t *v_dst, size_t v_stride, |
| 728 | uint8_t *u_dst, size_t u_stride, |
| 729 | int width, int height) |
| 730 | { |
| 731 | /* YV12 has V as plane 1 and U as plane 2 */ |
| 732 | avx_color_rgba_to_i420(src, src_stride, y_dst, y_stride, u_dst, u_stride, v_dst, v_stride, width, height); |
| 733 | } |