Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * This file is part of FFmpeg. | ||
3 | * | ||
4 | * FFmpeg 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 | * FFmpeg 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 FFmpeg; if not, write to the Free Software | ||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
17 | */ | ||
18 | |||
19 | #include <assert.h> | ||
20 | #include <stdint.h> | ||
21 | #include <string.h> | ||
22 | |||
23 | #include "config.h" | ||
24 | #include "libavutil/avassert.h" | ||
25 | #include "libavutil/attributes.h" | ||
26 | #include "libavutil/imgutils.h" | ||
27 | #include "avcodec.h" | ||
28 | #include "me_cmp.h" | ||
29 | #include "mpegvideoencdsp.h" | ||
30 | |||
31 | ✗ | static int try_8x8basis_c(const int16_t rem[64], const int16_t weight[64], | |
32 | const int16_t basis[64], int scale) | ||
33 | { | ||
34 | int i; | ||
35 | ✗ | unsigned int sum = 0; | |
36 | |||
37 | ✗ | for (i = 0; i < 8 * 8; i++) { | |
38 | ✗ | int b = rem[i] + ((basis[i] * scale + | |
39 | ✗ | (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >> | |
40 | (BASIS_SHIFT - RECON_SHIFT)); | ||
41 | ✗ | int w = weight[i]; | |
42 | ✗ | b >>= RECON_SHIFT; | |
43 | av_assert2(-512 < b && b < 512); | ||
44 | |||
45 | ✗ | sum += (w * b) * (w * b) >> 4; | |
46 | } | ||
47 | ✗ | return sum >> 2; | |
48 | } | ||
49 | |||
50 | ✗ | static void add_8x8basis_c(int16_t rem[64], const int16_t basis[64], int scale) | |
51 | { | ||
52 | int i; | ||
53 | |||
54 | ✗ | for (i = 0; i < 8 * 8; i++) | |
55 | ✗ | rem[i] += (basis[i] * scale + | |
56 | ✗ | (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >> | |
57 | (BASIS_SHIFT - RECON_SHIFT); | ||
58 | ✗ | } | |
59 | |||
60 | 4341418 | static int pix_sum_c(const uint8_t *pix, ptrdiff_t line_size) | |
61 | { | ||
62 | 4341418 | int s = 0, i, j; | |
63 | |||
64 |
2/2✓ Branch 0 taken 69462688 times.
✓ Branch 1 taken 4341418 times.
|
73804106 | for (i = 0; i < 16; i++) { |
65 |
2/2✓ Branch 0 taken 138925376 times.
✓ Branch 1 taken 69462688 times.
|
208388064 | for (j = 0; j < 16; j += 8) { |
66 | 138925376 | s += pix[0]; | |
67 | 138925376 | s += pix[1]; | |
68 | 138925376 | s += pix[2]; | |
69 | 138925376 | s += pix[3]; | |
70 | 138925376 | s += pix[4]; | |
71 | 138925376 | s += pix[5]; | |
72 | 138925376 | s += pix[6]; | |
73 | 138925376 | s += pix[7]; | |
74 | 138925376 | pix += 8; | |
75 | } | ||
76 | 69462688 | pix += line_size - 16; | |
77 | } | ||
78 | 4341418 | return s; | |
79 | } | ||
80 | |||
81 | 4341418 | static int pix_norm1_c(const uint8_t *pix, ptrdiff_t line_size) | |
82 | { | ||
83 | 4341418 | int s = 0, i, j; | |
84 | 4341418 | const uint32_t *sq = ff_square_tab + 256; | |
85 | |||
86 |
2/2✓ Branch 0 taken 69462688 times.
✓ Branch 1 taken 4341418 times.
|
73804106 | for (i = 0; i < 16; i++) { |
87 |
2/2✓ Branch 0 taken 138925376 times.
✓ Branch 1 taken 69462688 times.
|
208388064 | for (j = 0; j < 16; j += 8) { |
88 | #if HAVE_FAST_64BIT | ||
89 | 138925376 | register uint64_t x = *(uint64_t *) pix; | |
90 | 138925376 | s += sq[x & 0xff]; | |
91 | 138925376 | s += sq[(x >> 8) & 0xff]; | |
92 | 138925376 | s += sq[(x >> 16) & 0xff]; | |
93 | 138925376 | s += sq[(x >> 24) & 0xff]; | |
94 | 138925376 | s += sq[(x >> 32) & 0xff]; | |
95 | 138925376 | s += sq[(x >> 40) & 0xff]; | |
96 | 138925376 | s += sq[(x >> 48) & 0xff]; | |
97 | 138925376 | s += sq[(x >> 56) & 0xff]; | |
98 | #else | ||
99 | register uint32_t x = *(uint32_t *) pix; | ||
100 | s += sq[x & 0xff]; | ||
101 | s += sq[(x >> 8) & 0xff]; | ||
102 | s += sq[(x >> 16) & 0xff]; | ||
103 | s += sq[(x >> 24) & 0xff]; | ||
104 | x = *(uint32_t *) (pix + 4); | ||
105 | s += sq[x & 0xff]; | ||
106 | s += sq[(x >> 8) & 0xff]; | ||
107 | s += sq[(x >> 16) & 0xff]; | ||
108 | s += sq[(x >> 24) & 0xff]; | ||
109 | #endif | ||
110 | 138925376 | pix += 8; | |
111 | } | ||
112 | 69462688 | pix += line_size - 16; | |
113 | } | ||
114 | 4341418 | return s; | |
115 | } | ||
116 | |||
117 | 21201 | static av_always_inline void draw_edges_lr(uint8_t *ptr, ptrdiff_t wrap, int width, int height, int w) | |
118 | { | ||
119 |
2/2✓ Branch 0 taken 2199914 times.
✓ Branch 1 taken 21201 times.
|
2221115 | for (int i = 0; i < height; i++) { |
120 | 2199914 | memset(ptr - w, ptr[0], w); | |
121 | 2199914 | memset(ptr + width, ptr[width - 1], w); | |
122 | 2199914 | ptr += wrap; | |
123 | } | ||
124 | 21201 | } | |
125 | |||
126 | /* draw the edges of width 'w' of an image of size width, height */ | ||
127 | // FIXME: Check that this is OK for MPEG-4 interlaced. | ||
128 | 21201 | static void draw_edges_8_c(uint8_t *buf, ptrdiff_t wrap, int width, int height, | |
129 | int w, int h, int sides) | ||
130 | { | ||
131 | uint8_t *last_line; | ||
132 | int i; | ||
133 | |||
134 | /* left and right */ | ||
135 |
2/2✓ Branch 0 taken 7110 times.
✓ Branch 1 taken 14091 times.
|
21201 | if (w == 16) { |
136 | 7110 | draw_edges_lr(buf, wrap, width, height, 16); | |
137 |
2/2✓ Branch 0 taken 14076 times.
✓ Branch 1 taken 15 times.
|
14091 | } else if (w == 8) { |
138 | 14076 | draw_edges_lr(buf, wrap, width, height, 8); | |
139 | } else { | ||
140 | av_assert1(w == 4); | ||
141 | 15 | draw_edges_lr(buf, wrap, width, height, 4); | |
142 | } | ||
143 | |||
144 | /* top and bottom + corners */ | ||
145 | 21201 | buf -= w; | |
146 | 21201 | last_line = buf + (height - 1) * wrap; | |
147 |
2/2✓ Branch 0 taken 13263 times.
✓ Branch 1 taken 7938 times.
|
21201 | if (sides & EDGE_TOP) |
148 |
2/2✓ Branch 0 taken 140956 times.
✓ Branch 1 taken 13263 times.
|
154219 | for (i = 0; i < h; i++) |
149 | // top | ||
150 | 140956 | memcpy(buf - (i + 1) * wrap, buf, width + w + w); | |
151 |
1/2✓ Branch 0 taken 21201 times.
✗ Branch 1 not taken.
|
21201 | if (sides & EDGE_BOTTOM) |
152 |
2/2✓ Branch 0 taken 235228 times.
✓ Branch 1 taken 21201 times.
|
256429 | for (i = 0; i < h; i++) |
153 | // bottom | ||
154 | 235228 | memcpy(last_line + (i + 1) * wrap, last_line, width + w + w); | |
155 | 21201 | } | |
156 | |||
157 | /* This wrapper function only serves to convert the stride parameters | ||
158 | * from ptrdiff_t to int for av_image_copy_plane(). */ | ||
159 | ✗ | static void copy_plane_wrapper(uint8_t *dst, ptrdiff_t dst_wrap, | |
160 | const uint8_t *src, ptrdiff_t src_wrap, | ||
161 | int width, int height) | ||
162 | { | ||
163 | ✗ | av_image_copy_plane(dst, dst_wrap, src, src_wrap, width, height); | |
164 | ✗ | } | |
165 | |||
166 | /* 2x2 -> 1x1 */ | ||
167 | ✗ | static void shrink22(uint8_t *dst, ptrdiff_t dst_wrap, | |
168 | const uint8_t *src, ptrdiff_t src_wrap, | ||
169 | int width, int height) | ||
170 | { | ||
171 | int w; | ||
172 | const uint8_t *s1, *s2; | ||
173 | uint8_t *d; | ||
174 | |||
175 | ✗ | for (; height > 0; height--) { | |
176 | ✗ | s1 = src; | |
177 | ✗ | s2 = s1 + src_wrap; | |
178 | ✗ | d = dst; | |
179 | ✗ | for (w = width; w >= 4; w -= 4) { | |
180 | ✗ | d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2; | |
181 | ✗ | d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2; | |
182 | ✗ | d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2; | |
183 | ✗ | d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2; | |
184 | ✗ | s1 += 8; | |
185 | ✗ | s2 += 8; | |
186 | ✗ | d += 4; | |
187 | } | ||
188 | ✗ | for (; w > 0; w--) { | |
189 | ✗ | d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2; | |
190 | ✗ | s1 += 2; | |
191 | ✗ | s2 += 2; | |
192 | ✗ | d++; | |
193 | } | ||
194 | ✗ | src += 2 * src_wrap; | |
195 | ✗ | dst += dst_wrap; | |
196 | } | ||
197 | ✗ | } | |
198 | |||
199 | /* 4x4 -> 1x1 */ | ||
200 | ✗ | static void shrink44(uint8_t *dst, ptrdiff_t dst_wrap, | |
201 | const uint8_t *src, ptrdiff_t src_wrap, | ||
202 | int width, int height) | ||
203 | { | ||
204 | int w; | ||
205 | const uint8_t *s1, *s2, *s3, *s4; | ||
206 | uint8_t *d; | ||
207 | |||
208 | ✗ | for (; height > 0; height--) { | |
209 | ✗ | s1 = src; | |
210 | ✗ | s2 = s1 + src_wrap; | |
211 | ✗ | s3 = s2 + src_wrap; | |
212 | ✗ | s4 = s3 + src_wrap; | |
213 | ✗ | d = dst; | |
214 | ✗ | for (w = width; w > 0; w--) { | |
215 | ✗ | d[0] = (s1[0] + s1[1] + s1[2] + s1[3] + | |
216 | ✗ | s2[0] + s2[1] + s2[2] + s2[3] + | |
217 | ✗ | s3[0] + s3[1] + s3[2] + s3[3] + | |
218 | ✗ | s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4; | |
219 | ✗ | s1 += 4; | |
220 | ✗ | s2 += 4; | |
221 | ✗ | s3 += 4; | |
222 | ✗ | s4 += 4; | |
223 | ✗ | d++; | |
224 | } | ||
225 | ✗ | src += 4 * src_wrap; | |
226 | ✗ | dst += dst_wrap; | |
227 | } | ||
228 | ✗ | } | |
229 | |||
230 | /* 8x8 -> 1x1 */ | ||
231 | ✗ | static void shrink88(uint8_t *dst, ptrdiff_t dst_wrap, | |
232 | const uint8_t *src, ptrdiff_t src_wrap, | ||
233 | int width, int height) | ||
234 | { | ||
235 | int w, i; | ||
236 | |||
237 | ✗ | for (; height > 0; height--) { | |
238 | ✗ | for(w = width;w > 0; w--) { | |
239 | ✗ | int tmp = 0; | |
240 | ✗ | for (i = 0; i < 8; i++) { | |
241 | ✗ | tmp += src[0] + src[1] + src[2] + src[3] + | |
242 | ✗ | src[4] + src[5] + src[6] + src[7]; | |
243 | ✗ | src += src_wrap; | |
244 | } | ||
245 | ✗ | *(dst++) = (tmp + 32) >> 6; | |
246 | ✗ | src += 8 - 8 * src_wrap; | |
247 | } | ||
248 | ✗ | src += 8 * src_wrap - 8 * width; | |
249 | ✗ | dst += dst_wrap - width; | |
250 | } | ||
251 | ✗ | } | |
252 | |||
253 | 375 | av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c, | |
254 | AVCodecContext *avctx) | ||
255 | { | ||
256 | 375 | c->try_8x8basis = try_8x8basis_c; | |
257 | 375 | c->add_8x8basis = add_8x8basis_c; | |
258 | |||
259 | 375 | c->shrink[0] = copy_plane_wrapper; | |
260 | 375 | c->shrink[1] = shrink22; | |
261 | 375 | c->shrink[2] = shrink44; | |
262 | 375 | c->shrink[3] = shrink88; | |
263 | |||
264 | 375 | c->pix_sum = pix_sum_c; | |
265 | 375 | c->pix_norm1 = pix_norm1_c; | |
266 | |||
267 | 375 | c->draw_edges = draw_edges_8_c; | |
268 | |||
269 | #if ARCH_AARCH64 | ||
270 | ff_mpegvideoencdsp_init_aarch64(c, avctx); | ||
271 | #elif ARCH_ARM | ||
272 | ff_mpegvideoencdsp_init_arm(c, avctx); | ||
273 | #elif ARCH_PPC | ||
274 | ff_mpegvideoencdsp_init_ppc(c, avctx); | ||
275 | #elif ARCH_RISCV | ||
276 | ff_mpegvideoencdsp_init_riscv(c, avctx); | ||
277 | #elif ARCH_X86 | ||
278 | 375 | ff_mpegvideoencdsp_init_x86(c, avctx); | |
279 | #elif ARCH_MIPS | ||
280 | ff_mpegvideoencdsp_init_mips(c, avctx); | ||
281 | #endif | ||
282 | 375 | } | |
283 |