]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideoencdsp.c
avcodec/motion_est_template: Fix map cache use in qpel_motion_search()
[ffmpeg] / libavcodec / mpegvideoencdsp.c
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(int16_t rem[64], int16_t weight[64],
32                           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], 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 static int pix_sum_c(uint8_t *pix, int line_size)
61 {
62     int s = 0, i, j;
63
64     for (i = 0; i < 16; i++) {
65         for (j = 0; j < 16; j += 8) {
66             s   += pix[0];
67             s   += pix[1];
68             s   += pix[2];
69             s   += pix[3];
70             s   += pix[4];
71             s   += pix[5];
72             s   += pix[6];
73             s   += pix[7];
74             pix += 8;
75         }
76         pix += line_size - 16;
77     }
78     return s;
79 }
80
81 static int pix_norm1_c(uint8_t *pix, int line_size)
82 {
83     int s = 0, i, j;
84     uint32_t *sq = ff_square_tab + 256;
85
86     for (i = 0; i < 16; i++) {
87         for (j = 0; j < 16; j += 8) {
88 #if 0
89             s += sq[pix[0]];
90             s += sq[pix[1]];
91             s += sq[pix[2]];
92             s += sq[pix[3]];
93             s += sq[pix[4]];
94             s += sq[pix[5]];
95             s += sq[pix[6]];
96             s += sq[pix[7]];
97 #else
98 #if HAVE_FAST_64BIT
99             register uint64_t x = *(uint64_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             s += sq[(x >> 32) & 0xff];
105             s += sq[(x >> 40) & 0xff];
106             s += sq[(x >> 48) & 0xff];
107             s += sq[(x >> 56) & 0xff];
108 #else
109             register uint32_t x = *(uint32_t *) pix;
110             s += sq[x         & 0xff];
111             s += sq[(x >>  8) & 0xff];
112             s += sq[(x >> 16) & 0xff];
113             s += sq[(x >> 24) & 0xff];
114             x  = *(uint32_t *) (pix + 4);
115             s += sq[x         & 0xff];
116             s += sq[(x >>  8) & 0xff];
117             s += sq[(x >> 16) & 0xff];
118             s += sq[(x >> 24) & 0xff];
119 #endif
120 #endif
121             pix += 8;
122         }
123         pix += line_size - 16;
124     }
125     return s;
126 }
127
128 /* draw the edges of width 'w' of an image of size width, height */
129 // FIXME: Check that this is OK for MPEG-4 interlaced.
130 static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
131                            int w, int h, int sides)
132 {
133     uint8_t *ptr = buf, *last_line;
134     int i;
135
136     /* left and right */
137     for (i = 0; i < height; i++) {
138         memset(ptr - w, ptr[0], w);
139         memset(ptr + width, ptr[width - 1], w);
140         ptr += wrap;
141     }
142
143     /* top and bottom + corners */
144     buf -= w;
145     last_line = buf + (height - 1) * wrap;
146     if (sides & EDGE_TOP)
147         for (i = 0; i < h; i++)
148             // top
149             memcpy(buf - (i + 1) * wrap, buf, width + w + w);
150     if (sides & EDGE_BOTTOM)
151         for (i = 0; i < h; i++)
152             // bottom
153             memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
154 }
155
156 /* 2x2 -> 1x1 */
157 static void shrink22(uint8_t *dst, int dst_wrap,
158                      const uint8_t *src, int src_wrap,
159                      int width, int height)
160 {
161     int w;
162     const uint8_t *s1, *s2;
163     uint8_t *d;
164
165     for (; height > 0; height--) {
166         s1 = src;
167         s2 = s1 + src_wrap;
168         d = dst;
169         for (w = width; w >= 4; w -= 4) {
170             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
171             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
172             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
173             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
174             s1 += 8;
175             s2 += 8;
176             d += 4;
177         }
178         for (; w > 0; w--) {
179             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
180             s1 += 2;
181             s2 += 2;
182             d++;
183         }
184         src += 2 * src_wrap;
185         dst += dst_wrap;
186     }
187 }
188
189 /* 4x4 -> 1x1 */
190 static void shrink44(uint8_t *dst, int dst_wrap,
191                      const uint8_t *src, int src_wrap,
192                      int width, int height)
193 {
194     int w;
195     const uint8_t *s1, *s2, *s3, *s4;
196     uint8_t *d;
197
198     for (; height > 0; height--) {
199         s1 = src;
200         s2 = s1 + src_wrap;
201         s3 = s2 + src_wrap;
202         s4 = s3 + src_wrap;
203         d = dst;
204         for (w = width; w > 0; w--) {
205             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
206                     s2[0] + s2[1] + s2[2] + s2[3] +
207                     s3[0] + s3[1] + s3[2] + s3[3] +
208                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
209             s1 += 4;
210             s2 += 4;
211             s3 += 4;
212             s4 += 4;
213             d++;
214         }
215         src += 4 * src_wrap;
216         dst += dst_wrap;
217     }
218 }
219
220 /* 8x8 -> 1x1 */
221 static void shrink88(uint8_t *dst, int dst_wrap,
222                      const uint8_t *src, int src_wrap,
223                      int width, int height)
224 {
225     int w, i;
226
227     for (; height > 0; height--) {
228         for(w = width;w > 0; w--) {
229             int tmp = 0;
230             for (i = 0; i < 8; i++) {
231                 tmp += src[0] + src[1] + src[2] + src[3] +
232                        src[4] + src[5] + src[6] + src[7];
233                 src += src_wrap;
234             }
235             *(dst++) = (tmp + 32) >> 6;
236             src += 8 - 8 * src_wrap;
237         }
238         src += 8 * src_wrap - 8 * width;
239         dst += dst_wrap - width;
240     }
241 }
242
243 av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c,
244                                      AVCodecContext *avctx)
245 {
246     c->try_8x8basis = try_8x8basis_c;
247     c->add_8x8basis = add_8x8basis_c;
248
249     c->shrink[0] = av_image_copy_plane;
250     c->shrink[1] = shrink22;
251     c->shrink[2] = shrink44;
252     c->shrink[3] = shrink88;
253
254     c->pix_sum   = pix_sum_c;
255     c->pix_norm1 = pix_norm1_c;
256
257     c->draw_edges = draw_edges_8_c;
258
259     if (ARCH_ARM)
260         ff_mpegvideoencdsp_init_arm(c, avctx);
261     if (ARCH_PPC)
262         ff_mpegvideoencdsp_init_ppc(c, avctx);
263     if (ARCH_X86)
264         ff_mpegvideoencdsp_init_x86(c, avctx);
265     if (ARCH_MIPS)
266         ff_mpegvideoencdsp_init_mips(c, avctx);
267 }