2 * Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "libavutil/avassert.h"
28 #define avg(a,b,c,d) (a)
31 #define avg(a,b,c,d) (((a) + (b) + 1) >> 1)
34 #define avg(a,b,c,d) (((a) + (b) + (c) + (d) + 2) >> 2)
40 #define fn3(a,b,c) a##_##c##p##b##_c
41 #define fn2(a,b,c) fn3(a,b,c)
42 #define fn(a) fn2(a, BIT_DEPTH, ss)
48 #define av_clip_pixel(x) av_clip_uint8(x)
50 #define pixel uint16_t
51 #define av_clip_pixel(x) av_clip_uintp2(x, BIT_DEPTH)
54 static void fn(yuv2rgb)(int16_t *rgb[3], ptrdiff_t rgb_stride,
55 uint8_t *_yuv[3], ptrdiff_t yuv_stride[3],
56 int w, int h, const int16_t yuv2rgb_coeffs[3][3][8],
57 const int16_t yuv_offset[8])
59 pixel **yuv = (pixel **) _yuv;
60 const pixel *yuv0 = yuv[0], *yuv1 = yuv[1], *yuv2 = yuv[2];
61 int16_t *rgb0 = rgb[0], *rgb1 = rgb[1], *rgb2 = rgb[2];
63 int cy = yuv2rgb_coeffs[0][0][0];
64 int crv = yuv2rgb_coeffs[0][2][0];
65 int cgu = yuv2rgb_coeffs[1][1][0];
66 int cgv = yuv2rgb_coeffs[1][2][0];
67 int cbu = yuv2rgb_coeffs[2][1][0];
68 const int sh = BIT_DEPTH - 1, rnd = 1 << (sh - 1);
69 const int uv_offset = 128 << (BIT_DEPTH - 8);
71 av_assert2(yuv2rgb_coeffs[0][1][0] == 0);
72 av_assert2(yuv2rgb_coeffs[2][2][0] == 0);
73 av_assert2(yuv2rgb_coeffs[1][0][0] == cy && yuv2rgb_coeffs[2][0][0] == cy);
75 w = AV_CEIL_RSHIFT(w, SS_W);
76 h = AV_CEIL_RSHIFT(h, SS_H);
77 for (y = 0; y < h; y++) {
78 for (x = 0; x < w; x++) {
79 int y00 = yuv0[x << SS_W] - yuv_offset[0];
81 int y01 = yuv0[2 * x + 1] - yuv_offset[0];
83 int y10 = yuv0[yuv_stride[0] / sizeof(pixel) + 2 * x] - yuv_offset[0];
84 int y11 = yuv0[yuv_stride[0] / sizeof(pixel) + 2 * x + 1] - yuv_offset[0];
87 int u = yuv1[x] - uv_offset, v = yuv2[x] - uv_offset;
89 rgb0[x << SS_W] = av_clip_int16((y00 * cy + crv * v + rnd) >> sh);
91 rgb0[2 * x + 1] = av_clip_int16((y01 * cy + crv * v + rnd) >> sh);
93 rgb0[2 * x + rgb_stride] = av_clip_int16((y10 * cy + crv * v + rnd) >> sh);
94 rgb0[2 * x + rgb_stride + 1] = av_clip_int16((y11 * cy + crv * v + rnd) >> sh);
98 rgb1[x << SS_W] = av_clip_int16((y00 * cy + cgu * u +
99 cgv * v + rnd) >> sh);
101 rgb1[2 * x + 1] = av_clip_int16((y01 * cy + cgu * u +
102 cgv * v + rnd) >> sh);
104 rgb1[2 * x + rgb_stride] = av_clip_int16((y10 * cy + cgu * u +
105 cgv * v + rnd) >> sh);
106 rgb1[2 * x + rgb_stride + 1] = av_clip_int16((y11 * cy + cgu * u +
107 cgv * v + rnd) >> sh);
111 rgb2[x << SS_W] = av_clip_int16((y00 * cy + cbu * u + rnd) >> sh);
113 rgb2[2 * x + 1] = av_clip_int16((y01 * cy + cbu * u + rnd) >> sh);
115 rgb2[2 * x + rgb_stride] = av_clip_int16((y10 * cy + cbu * u + rnd) >> sh);
116 rgb2[2 * x + rgb_stride + 1] = av_clip_int16((y11 * cy + cbu * u + rnd) >> sh);
121 yuv0 += (yuv_stride[0] * (1 << SS_H)) / sizeof(pixel);
122 yuv1 += yuv_stride[1] / sizeof(pixel);
123 yuv2 += yuv_stride[2] / sizeof(pixel);
124 rgb0 += rgb_stride * (1 << SS_H);
125 rgb1 += rgb_stride * (1 << SS_H);
126 rgb2 += rgb_stride * (1 << SS_H);
130 static void fn(rgb2yuv)(uint8_t *_yuv[3], ptrdiff_t yuv_stride[3],
131 int16_t *rgb[3], ptrdiff_t s,
132 int w, int h, const int16_t rgb2yuv_coeffs[3][3][8],
133 const int16_t yuv_offset[8])
135 pixel **yuv = (pixel **) _yuv;
136 pixel *yuv0 = yuv[0], *yuv1 = yuv[1], *yuv2 = yuv[2];
137 const int16_t *rgb0 = rgb[0], *rgb1 = rgb[1], *rgb2 = rgb[2];
139 const int sh = 29 - BIT_DEPTH;
140 const int rnd = 1 << (sh - 1);
141 int cry = rgb2yuv_coeffs[0][0][0];
142 int cgy = rgb2yuv_coeffs[0][1][0];
143 int cby = rgb2yuv_coeffs[0][2][0];
144 int cru = rgb2yuv_coeffs[1][0][0];
145 int cgu = rgb2yuv_coeffs[1][1][0];
146 int cburv = rgb2yuv_coeffs[1][2][0];
147 int cgv = rgb2yuv_coeffs[2][1][0];
148 int cbv = rgb2yuv_coeffs[2][2][0];
149 ptrdiff_t s0 = yuv_stride[0] / sizeof(pixel);
150 const int uv_offset = 128 << (BIT_DEPTH - 8);
152 av_assert2(rgb2yuv_coeffs[1][2][0] == rgb2yuv_coeffs[2][0][0]);
153 w = AV_CEIL_RSHIFT(w, SS_W);
154 h = AV_CEIL_RSHIFT(h, SS_H);
155 for (y = 0; y < h; y++) {
156 for (x = 0; x < w; x++) {
157 int r00 = rgb0[x << SS_W], g00 = rgb1[x << SS_W], b00 = rgb2[x << SS_W];
159 int r01 = rgb0[x * 2 + 1], g01 = rgb1[x * 2 + 1], b01 = rgb2[x * 2 + 1];
161 int r10 = rgb0[x * 2 + 0 + s], g10 = rgb1[x * 2 + 0 + s], b10 = rgb2[x * 2 + 0 + s];
162 int r11 = rgb0[x * 2 + 1 + s], g11 = rgb1[x * 2 + 1 + s], b11 = rgb2[x * 2 + 1 + s];
166 yuv0[x << SS_W] = av_clip_pixel(yuv_offset[0] +
167 ((r00 * cry + g00 * cgy +
168 b00 * cby + rnd) >> sh));
170 yuv0[x * 2 + 1] = av_clip_pixel(yuv_offset[0] +
171 ((r01 * cry + g01 * cgy +
172 b01 * cby + rnd) >> sh));
174 yuv0[x * 2 + 0 + s0] = av_clip_pixel(yuv_offset[0] +
175 ((r10 * cry + g10 * cgy +
176 b10 * cby + rnd) >> sh));
177 yuv0[x * 2 + 1 + s0] = av_clip_pixel(yuv_offset[0] +
178 ((r11 * cry + g11 * cgy +
179 b11 * cby + rnd) >> sh));
183 yuv1[x] = av_clip_pixel(uv_offset +
184 ((avg(r00, r01, r10, r11) * cru +
185 avg(g00, g01, g10, g11) * cgu +
186 avg(b00, b01, b10, b11) * cburv + rnd) >> sh));
187 yuv2[x] = av_clip_pixel(uv_offset +
188 ((avg(r00, r01, r10, r11) * cburv +
189 avg(g00, g01, g10, g11) * cgv +
190 avg(b00, b01, b10, b11) * cbv + rnd) >> sh));
193 yuv0 += s0 * (1 << SS_H);
194 yuv1 += yuv_stride[1] / sizeof(pixel);
195 yuv2 += yuv_stride[2] / sizeof(pixel);
196 rgb0 += s * (1 << SS_H);
197 rgb1 += s * (1 << SS_H);
198 rgb2 += s * (1 << SS_H);
204 #define OUT_BIT_DEPTH BIT_DEPTH
205 #define IN_BIT_DEPTH 8
206 #include "colorspacedsp_yuv2yuv_template.c"
209 #define IN_BIT_DEPTH 10
210 #include "colorspacedsp_yuv2yuv_template.c"
213 #define IN_BIT_DEPTH 12
214 #include "colorspacedsp_yuv2yuv_template.c"