2 * This file is part of Libav.
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.
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.
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
24 #include "libavutil/attributes.h"
25 #include "libavutil/imgutils.h"
28 #include "mpegvideoencdsp.h"
30 static int try_8x8basis_c(int16_t rem[64], int16_t weight[64],
31 int16_t basis[64], int scale)
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));
42 assert(-512 < b && b < 512);
44 sum += (w * b) * (w * b) >> 4;
49 static void add_8x8basis_c(int16_t rem[64], int16_t basis[64], int scale)
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);
59 static int pix_sum_c(uint8_t *pix, int line_size)
63 for (i = 0; i < 16; i++) {
64 for (j = 0; j < 16; j += 8) {
75 pix += line_size - 16;
80 static int pix_norm1_c(uint8_t *pix, int line_size)
83 uint32_t *sq = ff_square_tab + 256;
85 for (i = 0; i < 16; i++) {
86 for (j = 0; j < 16; j += 8) {
98 register uint64_t x = *(uint64_t *) pix;
100 s += sq[(x >> 8) & 0xff];
101 s += sq[(x >> 16) & 0xff];
102 s += sq[(x >> 24) & 0xff];
103 s += sq[(x >> 32) & 0xff];
104 s += sq[(x >> 40) & 0xff];
105 s += sq[(x >> 48) & 0xff];
106 s += sq[(x >> 56) & 0xff];
108 register uint32_t x = *(uint32_t *) pix;
110 s += sq[(x >> 8) & 0xff];
111 s += sq[(x >> 16) & 0xff];
112 s += sq[(x >> 24) & 0xff];
113 x = *(uint32_t *) (pix + 4);
115 s += sq[(x >> 8) & 0xff];
116 s += sq[(x >> 16) & 0xff];
117 s += sq[(x >> 24) & 0xff];
122 pix += line_size - 16;
127 /* draw the edges of width 'w' of an image of size width, height */
128 // FIXME: Check that this is OK for MPEG-4 interlaced.
129 static void draw_edges_8_c(uint8_t *buf, int wrap, int width, int height,
130 int w, int h, int sides)
132 uint8_t *ptr = buf, *last_line;
136 for (i = 0; i < height; i++) {
137 memset(ptr - w, ptr[0], w);
138 memset(ptr + width, ptr[width - 1], w);
142 /* top and bottom + corners */
144 last_line = buf + (height - 1) * wrap;
145 if (sides & EDGE_TOP)
146 for (i = 0; i < h; i++)
148 memcpy(buf - (i + 1) * wrap, buf, width + w + w);
149 if (sides & EDGE_BOTTOM)
150 for (i = 0; i < h; i++)
152 memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
156 static void shrink22(uint8_t *dst, int dst_wrap,
157 const uint8_t *src, int src_wrap,
158 int width, int height)
161 const uint8_t *s1, *s2;
164 for (; height > 0; height--) {
168 for (w = width; w >= 4; w -= 4) {
169 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
170 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
171 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
172 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
178 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
189 static void shrink44(uint8_t *dst, int dst_wrap,
190 const uint8_t *src, int src_wrap,
191 int width, int height)
194 const uint8_t *s1, *s2, *s3, *s4;
197 for (; height > 0; height--) {
203 for (w = width; w > 0; w--) {
204 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
205 s2[0] + s2[1] + s2[2] + s2[3] +
206 s3[0] + s3[1] + s3[2] + s3[3] +
207 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
220 static void shrink88(uint8_t *dst, int dst_wrap,
221 const uint8_t *src, int src_wrap,
222 int width, int height)
226 for (; height > 0; height--) {
227 for(w = width;w > 0; w--) {
229 for (i = 0; i < 8; i++) {
230 tmp += src[0] + src[1] + src[2] + src[3] +
231 src[4] + src[5] + src[6] + src[7];
234 *(dst++) = (tmp + 32) >> 6;
235 src += 8 - 8 * src_wrap;
237 src += 8 * src_wrap - 8 * width;
238 dst += dst_wrap - width;
242 av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c,
243 AVCodecContext *avctx)
245 c->try_8x8basis = try_8x8basis_c;
246 c->add_8x8basis = add_8x8basis_c;
248 c->shrink[0] = av_image_copy_plane;
249 c->shrink[1] = shrink22;
250 c->shrink[2] = shrink44;
251 c->shrink[3] = shrink88;
253 c->pix_sum = pix_sum_c;
254 c->pix_norm1 = pix_norm1_c;
256 c->draw_edges = draw_edges_8_c;
259 ff_mpegvideoencdsp_init_arm(c, avctx);
261 ff_mpegvideoencdsp_init_ppc(c, avctx);
263 ff_mpegvideoencdsp_init_x86(c, avctx);