/src/ffmpeg/libavcodec/lossless_videodsp.c
Line | Count | Source |
1 | | /* |
2 | | * Lossless video DSP utils |
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 "config.h" |
22 | | #include "lossless_videodsp.h" |
23 | | #include "libavcodec/mathops.h" |
24 | | |
25 | | // 0x7f7f7f7f or 0x7f7f7f7f7f7f7f7f or whatever, depending on the cpu's native arithmetic size |
26 | 1.26G | #define pb_7f (~0UL / 255 * 0x7f) |
27 | 632M | #define pb_80 (~0UL / 255 * 0x80) |
28 | | |
29 | | static void add_bytes_c(uint8_t *dst, uint8_t *src, ptrdiff_t w) |
30 | 16.9M | { |
31 | 16.9M | long i; |
32 | | |
33 | 649M | for (i = 0; i <= w - (int) sizeof(long); i += sizeof(long)) { |
34 | 632M | long a = *(long *) (src + i); |
35 | 632M | long b = *(long *) (dst + i); |
36 | 632M | *(long *) (dst + i) = ((a & pb_7f) + (b & pb_7f)) ^ ((a ^ b) & pb_80); |
37 | 632M | } |
38 | 50.6M | for (; i < w; i++) |
39 | 33.7M | dst[i + 0] += src[i + 0]; |
40 | 16.9M | } |
41 | | |
42 | | static void add_median_pred_c(uint8_t *dst, const uint8_t *src1, |
43 | | const uint8_t *diff, ptrdiff_t w, |
44 | | int *left, int *left_top) |
45 | 27.7M | { |
46 | 27.7M | int i; |
47 | 27.7M | uint8_t l, lt; |
48 | | |
49 | 27.7M | l = *left; |
50 | 27.7M | lt = *left_top; |
51 | | |
52 | 1.33G | for (i = 0; i < w; i++) { |
53 | 1.31G | l = mid_pred(l, src1[i], (l + src1[i] - lt) & 0xFF) + diff[i]; |
54 | 1.31G | lt = src1[i]; |
55 | 1.31G | dst[i] = l; |
56 | 1.31G | } |
57 | | |
58 | 27.7M | *left = l; |
59 | 27.7M | *left_top = lt; |
60 | 27.7M | } |
61 | | |
62 | | static int add_left_pred_c(uint8_t *dst, const uint8_t *src, ptrdiff_t w, |
63 | | int acc) |
64 | 3.19M | { |
65 | 3.19M | int i; |
66 | | |
67 | 194M | for (i = 0; i < w - 1; i++) { |
68 | 191M | acc += src[i]; |
69 | 191M | dst[i] = acc; |
70 | 191M | i++; |
71 | 191M | acc += src[i]; |
72 | 191M | dst[i] = acc; |
73 | 191M | } |
74 | | |
75 | 4.72M | for (; i < w; i++) { |
76 | 1.52M | acc += src[i]; |
77 | 1.52M | dst[i] = acc; |
78 | 1.52M | } |
79 | | |
80 | 3.19M | return acc; |
81 | 3.19M | } |
82 | | |
83 | 2.82M | static int add_left_pred_int16_c(uint16_t *dst, const uint16_t *src, unsigned mask, ptrdiff_t w, unsigned acc){ |
84 | 2.82M | int i; |
85 | | |
86 | 1.00G | for(i=0; i<w-1; i++){ |
87 | 1.00G | acc+= src[i]; |
88 | 1.00G | dst[i]= acc &= mask; |
89 | 1.00G | i++; |
90 | 1.00G | acc+= src[i]; |
91 | 1.00G | dst[i]= acc &= mask; |
92 | 1.00G | } |
93 | | |
94 | 4.21M | for(; i<w; i++){ |
95 | 1.38M | acc+= src[i]; |
96 | 1.38M | dst[i]= acc &= mask; |
97 | 1.38M | } |
98 | | |
99 | 2.82M | return acc; |
100 | 2.82M | } |
101 | | |
102 | 23.2M | static void add_gradient_pred_c(uint8_t *src, const ptrdiff_t stride, const ptrdiff_t width){ |
103 | 23.2M | int A, B, C, i; |
104 | | |
105 | 2.94G | for (i = 0; i < width; i++) { |
106 | 2.92G | A = src[i - stride]; |
107 | 2.92G | B = src[i - (stride + 1)]; |
108 | 2.92G | C = src[i - 1]; |
109 | 2.92G | src[i] = (A - B + C + src[i]) & 0xFF; |
110 | 2.92G | } |
111 | 23.2M | } |
112 | | |
113 | | void ff_llviddsp_init(LLVidDSPContext *c) |
114 | 15.0k | { |
115 | 15.0k | c->add_bytes = add_bytes_c; |
116 | 15.0k | c->add_median_pred = add_median_pred_c; |
117 | 15.0k | c->add_left_pred = add_left_pred_c; |
118 | | |
119 | 15.0k | c->add_left_pred_int16 = add_left_pred_int16_c; |
120 | 15.0k | c->add_gradient_pred = add_gradient_pred_c; |
121 | | |
122 | | #if ARCH_PPC |
123 | | ff_llviddsp_init_ppc(c); |
124 | | #elif ARCH_RISCV |
125 | | ff_llviddsp_init_riscv(c); |
126 | | #elif ARCH_X86 |
127 | | ff_llviddsp_init_x86(c); |
128 | 15.0k | #endif |
129 | 15.0k | } |