b5a714d072f94a0cedca7950fe34d57878e55f20
[libav.git] / libavcodec / huffyuvdsp.c
1 /*
2 * This file is part of Libav.
3 *
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <stdint.h>
20
21 #include "config.h"
22 #include "libavutil/attributes.h"
23 #include "mathops.h"
24 #include "huffyuvdsp.h"
25
26 // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size
27 #define pb_7f (~0UL / 255 * 0x7f)
28 #define pb_80 (~0UL / 255 * 0x80)
29
30 static void add_bytes_c(uint8_t *dst, uint8_t *src, int w)
31 {
32 long i;
33
34 for (i = 0; i <= w - (int) sizeof(long); i += sizeof(long)) {
35 long a = *(long *) (src + i);
36 long b = *(long *) (dst + i);
37 *(long *) (dst + i) = ((a & pb_7f) + (b & pb_7f)) ^ ((a ^ b) & pb_80);
38 }
39 for (; i < w; i++)
40 dst[i + 0] += src[i + 0];
41 }
42
43 static void add_hfyu_median_pred_c(uint8_t *dst, const uint8_t *src1,
44 const uint8_t *diff, int w,
45 int *left, int *left_top)
46 {
47 int i;
48 uint8_t l, lt;
49
50 l = *left;
51 lt = *left_top;
52
53 for (i = 0; i < w; i++) {
54 l = mid_pred(l, src1[i], (l + src1[i] - lt) & 0xFF) + diff[i];
55 lt = src1[i];
56 dst[i] = l;
57 }
58
59 *left = l;
60 *left_top = lt;
61 }
62
63 static int add_hfyu_left_pred_c(uint8_t *dst, const uint8_t *src, int w,
64 int acc)
65 {
66 int i;
67
68 for (i = 0; i < w - 1; i++) {
69 acc += src[i];
70 dst[i] = acc;
71 i++;
72 acc += src[i];
73 dst[i] = acc;
74 }
75
76 for (; i < w; i++) {
77 acc += src[i];
78 dst[i] = acc;
79 }
80
81 return acc;
82 }
83
84 #if HAVE_BIGENDIAN
85 #define B 3
86 #define G 2
87 #define R 1
88 #define A 0
89 #else
90 #define B 0
91 #define G 1
92 #define R 2
93 #define A 3
94 #endif
95 static void add_hfyu_left_pred_bgr32_c(uint8_t *dst, const uint8_t *src,
96 int w, int *red, int *green,
97 int *blue, int *alpha)
98 {
99 int i, r = *red, g = *green, b = *blue, a = *alpha;
100
101 for (i = 0; i < w; i++) {
102 b += src[4 * i + B];
103 g += src[4 * i + G];
104 r += src[4 * i + R];
105 a += src[4 * i + A];
106
107 dst[4 * i + B] = b;
108 dst[4 * i + G] = g;
109 dst[4 * i + R] = r;
110 dst[4 * i + A] = a;
111 }
112
113 *red = r;
114 *green = g;
115 *blue = b;
116 *alpha = a;
117 }
118 #undef B
119 #undef G
120 #undef R
121 #undef A
122
123 av_cold void ff_huffyuvdsp_init(HuffYUVDSPContext *c)
124 {
125 c->add_bytes = add_bytes_c;
126 c->add_hfyu_median_pred = add_hfyu_median_pred_c;
127 c->add_hfyu_left_pred = add_hfyu_left_pred_c;
128 c->add_hfyu_left_pred_bgr32 = add_hfyu_left_pred_bgr32_c;
129
130 if (ARCH_X86)
131 ff_huffyuvdsp_init_x86(c);
132 }