1 /*
2 * Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "libavutil/avassert.h"
22
23 #undef opixel
24 #define opixel pixel
25
26 #undef ipixel
27 #if IN_BIT_DEPTH == 8
28 #define ipixel uint8_t
29 #else
30 #define ipixel uint16_t
31 #endif
32
33 #undef fn
34 #undef fn2
35 #undef fn3
36 #define fn3(a,b,c,d) a##_##d##p##b##to##c##_c
37 #define fn2(a,b,c,d) fn3(a,b,c,d)
38 #define fn(a) fn2(a, IN_BIT_DEPTH, OUT_BIT_DEPTH, ss)
39
fn(yuv2yuv)40 static void fn(yuv2yuv)(uint8_t *_dst[3], const ptrdiff_t dst_stride[3],
41 uint8_t *_src[3], const ptrdiff_t src_stride[3],
42 int w, int h, const int16_t c[3][3][8],
43 const int16_t yuv_offset[2][8])
44 {
45 opixel **dst = (opixel **) _dst;
46 ipixel **src = (ipixel **) _src;
47 const ipixel *src0 = src[0], *src1 = src[1], *src2 = src[2];
48 opixel *dst0 = dst[0], *dst1 = dst[1], *dst2 = dst[2];
49 int y, x;
50 const int sh = 14 + IN_BIT_DEPTH - OUT_BIT_DEPTH;
51 const int rnd = 1 << (sh - 1);
52 int y_off_in = yuv_offset[0][0];
53 int y_off_out = yuv_offset[1][0] << sh;
54 const int uv_off_in = 128 << (IN_BIT_DEPTH - 8);
55 const int uv_off_out = rnd + (128 << (OUT_BIT_DEPTH - 8 + sh));
56 int cyy = c[0][0][0], cyu = c[0][1][0], cyv = c[0][2][0];
57 int cuu = c[1][1][0], cuv = c[1][2][0], cvu = c[2][1][0], cvv = c[2][2][0];
58
59 av_assert2(c[1][0][0] == 0);
60 av_assert2(c[2][0][0] == 0);
61 w = AV_CEIL_RSHIFT(w, SS_W);
62 h = AV_CEIL_RSHIFT(h, SS_H);
63 for (y = 0; y < h; y++) {
64 for (x = 0; x < w; x++) {
65 int y00 = src0[x << SS_W] - y_off_in;
66 #if SS_W == 1
67 int y01 = src0[2 * x + 1] - y_off_in;
68 #if SS_H == 1
69 int y10 = src0[src_stride[0] / sizeof(ipixel) + 2 * x] - y_off_in;
70 int y11 = src0[src_stride[0] / sizeof(ipixel) + 2 * x + 1] - y_off_in;
71 #endif
72 #endif
73 int u = src1[x] - uv_off_in, v = src2[x] - uv_off_in;
74 int uv_val = cyu * u + cyv * v + rnd + y_off_out;
75
76 dst0[x << SS_W] = av_clip_pixel((cyy * y00 + uv_val) >> sh);
77 #if SS_W == 1
78 dst0[x * 2 + 1] = av_clip_pixel((cyy * y01 + uv_val) >> sh);
79 #if SS_H == 1
80 dst0[x * 2 + 0 + dst_stride[0] / sizeof(opixel)] =
81 av_clip_pixel((cyy * y10 + uv_val) >> sh);
82 dst0[x * 2 + 1 + dst_stride[0] / sizeof(opixel)] =
83 av_clip_pixel((cyy * y11 + uv_val) >> sh);
84 #endif
85 #endif
86
87 dst1[x] = av_clip_pixel((u * cuu + v * cuv + uv_off_out) >> sh);
88 dst2[x] = av_clip_pixel((u * cvu + v * cvv + uv_off_out) >> sh);
89 }
90
91 dst0 += (dst_stride[0] * (1 << SS_H)) / sizeof(opixel);
92 dst1 += dst_stride[1] / sizeof(opixel);
93 dst2 += dst_stride[2] / sizeof(opixel);
94 src0 += (src_stride[0] * (1 << SS_H)) / sizeof(ipixel);
95 src1 += src_stride[1] / sizeof(ipixel);
96 src2 += src_stride[2] / sizeof(ipixel);
97 }
98 }
99