2 * Bayer-to-RGB/YV12 template
3 * Copyright (c) 2011-2014 Peter Ross <pross@xvid.org>
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #if defined(BAYER_BGGR) || defined(BAYER_GBRG)
27 #if defined(BAYER_RGGB) || defined(BAYER_GRBG)
34 #define BAYER_READ(x) (x)
35 #define BAYER_SIZEOF 1
38 #if defined(BAYER_16LE)
39 #define BAYER_READ(x) AV_RL16(&(x))
40 #define BAYER_SIZEOF 2
43 #if defined(BAYER_16BE)
44 #define BAYER_READ(x) AV_RB16(&(x))
45 #define BAYER_SIZEOF 2
49 #define S(y, x) BAYER_READ(src[(y)*src_stride + BAYER_SIZEOF*(x)])
50 #define T(y, x) (unsigned int)S(y, x)
51 #define R(y, x) dst[(y)*dst_stride + (x)*3 + BAYER_R]
52 #define G(y, x) dst[(y)*dst_stride + (x)*3 + BAYER_G]
53 #define B(y, x) dst[(y)*dst_stride + (x)*3 + BAYER_B]
55 #if defined(BAYER_BGGR) || defined(BAYER_RGGB)
56 #define BAYER_TO_RGB24_COPY \
60 R(1, 0) = S(1, 1) >> BAYER_SHIFT; \
62 G(0, 1) = S(0, 1) >> BAYER_SHIFT; \
64 G(1, 1) = (T(0, 1) + T(1, 0)) >> (1 + BAYER_SHIFT); \
65 G(1, 0) = S(1, 0) >> BAYER_SHIFT; \
70 B(1, 0) = S(0, 0) >> BAYER_SHIFT;
71 #define BAYER_TO_RGB24_INTERPOLATE \
72 R(0, 0) = (T(-1, -1) + T(-1, 1) + T(1, -1) + T(1, 1)) >> (2 + BAYER_SHIFT); \
73 G(0, 0) = (T(-1, 0) + T( 0, -1) + T(0, 1) + T(1, 0)) >> (2 + BAYER_SHIFT); \
74 B(0, 0) = S(0, 0) >> BAYER_SHIFT; \
76 R(0, 1) = (T(-1, 1) + T(1, 1)) >> (1 + BAYER_SHIFT); \
77 G(0, 1) = S(0, 1) >> BAYER_SHIFT; \
78 B(0, 1) = (T(0, 0) + T(0, 2)) >> (1 + BAYER_SHIFT); \
80 R(1, 0) = (T(1, -1) + T(1, 1)) >> (1 + BAYER_SHIFT); \
81 G(1, 0) = S(1, 0) >> BAYER_SHIFT; \
82 B(1, 0) = (T(0, 0) + T(2, 0)) >> (1 + BAYER_SHIFT); \
84 R(1, 1) = S(1, 1) >> BAYER_SHIFT; \
85 G(1, 1) = (T(0, 1) + T(1, 0) + T(1, 2) + T(2, 1)) >> (2 + BAYER_SHIFT); \
86 B(1, 1) = (T(0, 0) + T(0, 2) + T(2, 0) + T(2, 2)) >> (2 + BAYER_SHIFT);
88 #define BAYER_TO_RGB24_COPY \
92 R(1, 0) = S(1, 0) >> BAYER_SHIFT; \
94 G(0, 0) = S(0, 0) >> BAYER_SHIFT; \
95 G(1, 1) = S(1, 1) >> BAYER_SHIFT; \
97 G(1, 0) = (T(0, 0) + T(1, 1)) >> (1 + BAYER_SHIFT); \
102 B(1, 0) = S(0, 1) >> BAYER_SHIFT;
103 #define BAYER_TO_RGB24_INTERPOLATE \
104 R(0, 0) = (T(-1, 0) + T(1, 0)) >> (1 + BAYER_SHIFT); \
105 G(0, 0) = S(0, 0) >> BAYER_SHIFT; \
106 B(0, 0) = (T(0, -1) + T(0, 1)) >> (1 + BAYER_SHIFT); \
108 R(0, 1) = (T(-1, 0) + T(-1, 2) + T(1, 0) + T(1, 2)) >> (2 + BAYER_SHIFT); \
109 G(0, 1) = (T(-1, 1) + T(0, 0) + T(0, 2) + T(1, 1)) >> (2 + BAYER_SHIFT); \
110 B(0, 1) = S(0, 1) >> BAYER_SHIFT; \
112 R(1, 0) = S(1, 0) >> BAYER_SHIFT; \
113 G(1, 0) = (T(0, 0) + T(1, -1) + T(1, 1) + T(2, 0)) >> (2 + BAYER_SHIFT); \
114 B(1, 0) = (T(0, -1) + T(0, 1) + T(2, -1) + T(2, 1)) >> (2 + BAYER_SHIFT); \
116 R(1, 1) = (T(1, 0) + T(1, 2)) >> (1 + BAYER_SHIFT); \
117 G(1, 1) = S(1, 1) >> BAYER_SHIFT; \
118 B(1, 1) = (T(0, 1) + T(2, 1)) >> (1 + BAYER_SHIFT);
121 #if defined(BAYER_BGGR) || defined(BAYER_RGGB)
122 #define BAYER_TO_RGB48_COPY \
130 G(1, 1) = (T(0, 1) + T(1, 0)) >> 1; \
137 #define BAYER_TO_RGB48_INTERPOLATE \
138 R(0, 0) = (T(-1, -1) + T(-1, 1) + T(1, -1) + T(1, 1)) >> 2; \
139 G(0, 0) = (T(-1, 0) + T( 0, -1) + T(0, 1) + T(1, 0)) >> 2; \
142 R(0, 1) = (T(-1, 1) + T(1, 1)) >> 1; \
144 B(0, 1) = (T(0, 0) + T(0, 2)) >> 1; \
146 R(1, 0) = (T(1, -1) + T(1, 1)) >> 1; \
148 B(1, 0) = (T(0, 0) + T(2, 0)) >> 1; \
151 G(1, 1) = (T(0, 1) + T(1, 0) + T(1, 2) + T(2, 1)) >> 2; \
152 B(1, 1) = (T(0, 0) + T(0, 2) + T(2, 0) + T(2, 2)) >> 2;
154 #define BAYER_TO_RGB48_COPY \
163 G(1, 0) = (T(0, 0) + T(1, 1)) >> 1; \
169 #define BAYER_TO_RGB48_INTERPOLATE \
170 R(0, 0) = (T(-1, 0) + T(1, 0)) >> 1; \
172 B(0, 0) = (T(0, -1) + T(0, 1)) >> 1; \
174 R(0, 1) = (T(-1, 0) + T(-1, 2) + T(1, 0) + T(1, 2)) >> 2; \
175 G(0, 1) = (T(-1, 1) + T(0, 0) + T(0, 2) + T(1, 1)) >> 2; \
179 G(1, 0) = (T(0, 0) + T(1, -1) + T(1, 1) + T(2, 0)) >> 2; \
180 B(1, 0) = (T(0, -1) + T(0, 1) + T(2, -1) + T(2, 1)) >> 2; \
182 R(1, 1) = (T(1, 0) + T(1, 2)) >> 1; \
184 B(1, 1) = (T(0, 1) + T(2, 1)) >> 1;
188 * invoke ff_rgb24toyv12 for 2x2 pixels
190 #define rgb24toyv12_2x2(src, dstY, dstU, dstV, luma_stride, src_stride, rgb2yuv) \
191 ff_rgb24toyv12(src, dstY, dstV, dstU, 2, 2, luma_stride, 0, src_stride, rgb2yuv)
193 static void BAYER_RENAME(rgb24_copy)(const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int width)
196 for (i = 0 ; i < width; i+= 2) {
198 src += 2 * BAYER_SIZEOF;
203 static void BAYER_RENAME(rgb24_interpolate)(const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int width)
208 src += 2 * BAYER_SIZEOF;
211 for (i = 2 ; i < width - 2; i+= 2) {
212 BAYER_TO_RGB24_INTERPOLATE
213 src += 2 * BAYER_SIZEOF;
222 static void BAYER_RENAME(rgb48_copy)(const uint8_t *src, int src_stride, uint8_t *ddst, int dst_stride, int width)
224 uint16_t *dst = (uint16_t *)ddst;
228 for (i = 0 ; i < width; i+= 2) {
230 src += 2 * BAYER_SIZEOF;
235 static void BAYER_RENAME(rgb48_interpolate)(const uint8_t *src, int src_stride, uint8_t *ddst, int dst_stride, int width)
237 uint16_t *dst = (uint16_t *)ddst;
242 src += 2 * BAYER_SIZEOF;
245 for (i = 2 ; i < width - 2; i+= 2) {
246 BAYER_TO_RGB48_INTERPOLATE
247 src += 2 * BAYER_SIZEOF;
256 static void BAYER_RENAME(yv12_copy)(const uint8_t *src, int src_stride, uint8_t *dstY, uint8_t *dstU, uint8_t *dstV, int luma_stride, int width, int32_t *rgb2yuv)
259 const int dst_stride = 6;
261 for (i = 0 ; i < width; i+= 2) {
263 rgb24toyv12_2x2(dst, dstY, dstU, dstV, luma_stride, dst_stride, rgb2yuv);
264 src += 2 * BAYER_SIZEOF;
271 static void BAYER_RENAME(yv12_interpolate)(const uint8_t *src, int src_stride, uint8_t *dstY, uint8_t *dstU, uint8_t *dstV, int luma_stride, int width, int32_t *rgb2yuv)
274 const int dst_stride = 6;
278 rgb24toyv12_2x2(dst, dstY, dstU, dstV, luma_stride, dst_stride, rgb2yuv);
279 src += 2 * BAYER_SIZEOF;
284 for (i = 2 ; i < width - 2; i+= 2) {
285 BAYER_TO_RGB24_INTERPOLATE
286 rgb24toyv12_2x2(dst, dstY, dstU, dstV, luma_stride, dst_stride, rgb2yuv);
287 src += 2 * BAYER_SIZEOF;
295 rgb24toyv12_2x2(dst, dstY, dstU, dstV, luma_stride, dst_stride, rgb2yuv);
304 #undef BAYER_TO_RGB24_COPY
305 #undef BAYER_TO_RGB24_INTERPOLATE
306 #undef BAYER_TO_RGB48_COPY
307 #undef BAYER_TO_RGB48_INTERPOLATE
318 #if defined(BAYER_BGGR)
321 #if defined(BAYER_RGGB)
324 #if defined(BAYER_GBRG)
327 #if defined(BAYER_GRBG)
333 #if defined(BAYER_16LE)
336 #if defined(BAYER_16BE)