]> git.sesse.net Git - ffmpeg/blob - libavcodec/mpegvideoencdsp.c
configure: Print list of enabled programs
[ffmpeg] / libavcodec / mpegvideoencdsp.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 <assert.h>
20 #include <stdint.h>
21 #include <string.h>
22
23 #include "config.h"
24 #include "libavutil/attributes.h"
25 #include "libavutil/imgutils.h"
26 #include "avcodec.h"
27 #include "me_cmp.h"
28 #include "mpegvideoencdsp.h"
29
30 static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
31                           int16_t basis[64], int scale)
32 {
33     int i;
34     unsigned int sum = 0;
35
36     for (i = 0; i < 8 * 8; i++) {
37         int b = rem[i] + ((basis[i] * scale +
38                            (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
39                           (BASIS_SHIFT - RECON_SHIFT));
40         int w = weight[i];
41         b >>= RECON_SHIFT;
42         assert(-512 < b && b < 512);
43
44         sum += (w * b) * (w * b) >> 4;
45     }
46     return sum >> 2;
47 }
48
49 static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
50 {
51     int i;
52
53     for (i = 0; i < 8 * 8; i++)
54         rem[i] += (basis[i] * scale +
55                    (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
56                   (BASIS_SHIFT - RECON_SHIFT);
57 }
58
59 static int pix_sum_c(uint8_t *pix, int line_size)
60 {
61     int s = 0, i, j;
62
63     for (i = 0; i < 16; i++) {
64         for (j = 0; j < 16; j += 8) {
65             s   += pix[0];
66             s   += pix[1];
67             s   += pix[2];
68             s   += pix[3];
69             s   += pix[4];
70             s   += pix[5];
71             s   += pix[6];
72             s   += pix[7];
73             pix += 8;
74         }
75         pix += line_size - 16;
76     }
77     return s;
78 }
79
80 static int pix_norm1_c(uint8_t *pix, int line_size)
81 {
82     int s = 0, i, j;
83     uint32_t *sq = ff_square_tab + 256;
84
85     for (i = 0; i < 16; i++) {
86         for (j = 0; j < 16; j += 8) {
87 #if HAVE_FAST_64BIT
88             register uint64_t x = *(uint64_t *) pix;
89             s += sq[x         & 0xff];
90             s += sq[(x >>  8) & 0xff];
91             s += sq[(x >> 16) & 0xff];
92             s += sq[(x >> 24) & 0xff];
93             s += sq[(x >> 32) & 0xff];
94             s += sq[(x >> 40) & 0xff];
95             s += sq[(x >> 48) & 0xff];
96             s += sq[(x >> 56) & 0xff];
97 #else
98             register uint32_t x = *(uint32_t *) pix;
99             s += sq[x         & 0xff];
100             s += sq[(x >>  8) & 0xff];
101             s += sq[(x >> 16) & 0xff];
102             s += sq[(x >> 24) & 0xff];
103             x  = *(uint32_t *) (pix + 4);
104             s += sq[x         & 0xff];
105             s += sq[(x >>  8) & 0xff];
106             s += sq[(x >> 16) & 0xff];
107             s += sq[(x >> 24) & 0xff];
108 #endif
109             pix += 8;
110         }
111         pix += line_size - 16;
112     }
113     return s;
114 }
115
116 /* draw the edges of width 'w' of an image of size width, height */
117 // FIXME: Check that this is OK for MPEG-4 interlaced.
118 static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
119                            int w, int h, int sides)
120 {
121     uint8_t *ptr = buf, *last_line;
122     int i;
123
124     /* left and right */
125     for (i = 0; i < height; i++) {
126         memset(ptr - w, ptr[0], w);
127         memset(ptr + width, ptr[width - 1], w);
128         ptr += wrap;
129     }
130
131     /* top and bottom + corners */
132     buf -= w;
133     last_line = buf + (height - 1) * wrap;
134     if (sides & EDGE_TOP)
135         for (i = 0; i < h; i++)
136             // top
137             memcpy(buf - (i + 1) * wrap, buf, width + w + w);
138     if (sides & EDGE_BOTTOM)
139         for (i = 0; i < h; i++)
140             // bottom
141             memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
142 }
143
144 /* 2x2 -> 1x1 */
145 static void shrink22(uint8_t *dst, int dst_wrap,
146                      const uint8_t *src, int src_wrap,
147                      int width, int height)
148 {
149     int w;
150     const uint8_t *s1, *s2;
151     uint8_t *d;
152
153     for (; height > 0; height--) {
154         s1 = src;
155         s2 = s1 + src_wrap;
156         d = dst;
157         for (w = width; w >= 4; w -= 4) {
158             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
159             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
160             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
161             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
162             s1 += 8;
163             s2 += 8;
164             d += 4;
165         }
166         for (; w > 0; w--) {
167             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
168             s1 += 2;
169             s2 += 2;
170             d++;
171         }
172         src += 2 * src_wrap;
173         dst += dst_wrap;
174     }
175 }
176
177 /* 4x4 -> 1x1 */
178 static void shrink44(uint8_t *dst, int dst_wrap,
179                      const uint8_t *src, int src_wrap,
180                      int width, int height)
181 {
182     int w;
183     const uint8_t *s1, *s2, *s3, *s4;
184     uint8_t *d;
185
186     for (; height > 0; height--) {
187         s1 = src;
188         s2 = s1 + src_wrap;
189         s3 = s2 + src_wrap;
190         s4 = s3 + src_wrap;
191         d = dst;
192         for (w = width; w > 0; w--) {
193             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
194                     s2[0] + s2[1] + s2[2] + s2[3] +
195                     s3[0] + s3[1] + s3[2] + s3[3] +
196                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
197             s1 += 4;
198             s2 += 4;
199             s3 += 4;
200             s4 += 4;
201             d++;
202         }
203         src += 4 * src_wrap;
204         dst += dst_wrap;
205     }
206 }
207
208 /* 8x8 -> 1x1 */
209 static void shrink88(uint8_t *dst, int dst_wrap,
210                      const uint8_t *src, int src_wrap,
211                      int width, int height)
212 {
213     int w, i;
214
215     for (; height > 0; height--) {
216         for(w = width;w > 0; w--) {
217             int tmp = 0;
218             for (i = 0; i < 8; i++) {
219                 tmp += src[0] + src[1] + src[2] + src[3] +
220                        src[4] + src[5] + src[6] + src[7];
221                 src += src_wrap;
222             }
223             *(dst++) = (tmp + 32) >> 6;
224             src += 8 - 8 * src_wrap;
225         }
226         src += 8 * src_wrap - 8 * width;
227         dst += dst_wrap - width;
228     }
229 }
230
231 av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c,
232                                      AVCodecContext *avctx)
233 {
234     c->try_8x8basis = try_8x8basis_c;
235     c->add_8x8basis = add_8x8basis_c;
236
237     c->shrink[0] = av_image_copy_plane;
238     c->shrink[1] = shrink22;
239     c->shrink[2] = shrink44;
240     c->shrink[3] = shrink88;
241
242     c->pix_sum   = pix_sum_c;
243     c->pix_norm1 = pix_norm1_c;
244
245     c->draw_edges = draw_edges_8_c;
246
247     if (ARCH_ARM)
248         ff_mpegvideoencdsp_init_arm(c, avctx);
249     if (ARCH_PPC)
250         ff_mpegvideoencdsp_init_ppc(c, avctx);
251     if (ARCH_X86)
252         ff_mpegvideoencdsp_init_x86(c, avctx);
253 }