3 * Copyright (c) 2000, 2001 Fabrice Bellard
4 * Copyright (c) 2002-2004 Michael Niedermayer <michaelni@gmx.at>
6 * gmc & q-pel & 32/64 bit based MC by Michael Niedermayer <michaelni@gmx.at>
8 * This file is part of Libav.
10 * Libav is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * Libav is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with Libav; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include "bit_depth_template.c"
32 /* draw the edges of width 'w' of an image of size width, height */
33 //FIXME check that this is ok for mpeg4 interlaced
34 static void FUNCC(draw_edges)(uint8_t *_buf, int _wrap, int width, int height, int w, int h, int sides)
36 pixel *buf = (pixel*)_buf;
37 int wrap = _wrap / sizeof(pixel);
38 pixel *ptr, *last_line;
43 for(i=0;i<height;i++) {
46 for (j = 0; j < w; j++) {
48 ptr[j+width] = ptr[width-1];
51 memset(ptr - w, ptr[0], w);
52 memset(ptr + width, ptr[width-1], w);
57 /* top and bottom + corners */
59 last_line = buf + (height - 1) * wrap;
61 for(i = 0; i < h; i++)
62 memcpy(buf - (i + 1) * wrap, buf, (width + w + w) * sizeof(pixel)); // top
63 if (sides & EDGE_BOTTOM)
64 for (i = 0; i < h; i++)
65 memcpy(last_line + (i + 1) * wrap, last_line, (width + w + w) * sizeof(pixel)); // bottom
68 #define DCTELEM_FUNCS(dctcoef, suffix) \
69 static void FUNCC(get_pixels ## suffix)(int16_t *restrict _block, \
70 const uint8_t *_pixels, \
73 const pixel *pixels = (const pixel *) _pixels; \
74 dctcoef *restrict block = (dctcoef *) _block; \
77 /* read the pixels */ \
79 block[0] = pixels[0]; \
80 block[1] = pixels[1]; \
81 block[2] = pixels[2]; \
82 block[3] = pixels[3]; \
83 block[4] = pixels[4]; \
84 block[5] = pixels[5]; \
85 block[6] = pixels[6]; \
86 block[7] = pixels[7]; \
87 pixels += line_size / sizeof(pixel); \
92 static void FUNCC(add_pixels8 ## suffix)(uint8_t *restrict _pixels, \
97 pixel *restrict pixels = (pixel *restrict)_pixels; \
98 dctcoef *block = (dctcoef*)_block; \
99 line_size /= sizeof(pixel); \
102 pixels[0] += block[0]; \
103 pixels[1] += block[1]; \
104 pixels[2] += block[2]; \
105 pixels[3] += block[3]; \
106 pixels[4] += block[4]; \
107 pixels[5] += block[5]; \
108 pixels[6] += block[6]; \
109 pixels[7] += block[7]; \
110 pixels += line_size; \
115 static void FUNCC(add_pixels4 ## suffix)(uint8_t *restrict _pixels, \
120 pixel *restrict pixels = (pixel *restrict)_pixels; \
121 dctcoef *block = (dctcoef*)_block; \
122 line_size /= sizeof(pixel); \
125 pixels[0] += block[0]; \
126 pixels[1] += block[1]; \
127 pixels[2] += block[2]; \
128 pixels[3] += block[3]; \
129 pixels += line_size; \
134 static void FUNCC(clear_block ## suffix)(int16_t *block) \
136 memset(block, 0, sizeof(dctcoef)*64); \
140 * memset(blocks, 0, sizeof(int16_t)*6*64) \
142 static void FUNCC(clear_blocks ## suffix)(int16_t *blocks) \
144 memset(blocks, 0, sizeof(dctcoef)*6*64); \
147 DCTELEM_FUNCS(int16_t, _16)
149 DCTELEM_FUNCS(dctcoef, _32)
152 #include "hpel_template.c"
154 #define PIXOP2(OPNAME, OP) \
155 static inline void FUNC(OPNAME ## _no_rnd_pixels8_l2)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, \
156 int src_stride1, int src_stride2, int h){\
160 a= AV_RN4P(&src1[i*src_stride1 ]);\
161 b= AV_RN4P(&src2[i*src_stride2 ]);\
162 OP(*((pixel4*)&dst[i*dst_stride ]), no_rnd_avg_pixel4(a, b));\
163 a= AV_RN4P(&src1[i*src_stride1+4*sizeof(pixel)]);\
164 b= AV_RN4P(&src2[i*src_stride2+4*sizeof(pixel)]);\
165 OP(*((pixel4*)&dst[i*dst_stride+4*sizeof(pixel)]), no_rnd_avg_pixel4(a, b));\
169 static inline void FUNC(OPNAME ## _no_rnd_pixels16_l2)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, int dst_stride, \
170 int src_stride1, int src_stride2, int h){\
171 FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst , src1 , src2 , dst_stride, src_stride1, src_stride2, h);\
172 FUNC(OPNAME ## _no_rnd_pixels8_l2)(dst+8*sizeof(pixel), src1+8*sizeof(pixel), src2+8*sizeof(pixel), dst_stride, src_stride1, src_stride2, h);\
175 static inline void FUNCC(OPNAME ## _no_rnd_pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
176 FUNC(OPNAME ## _no_rnd_pixels8_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
179 static inline void FUNCC(OPNAME ## _pixels8_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
180 FUNC(OPNAME ## _pixels8_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
183 static inline void FUNCC(OPNAME ## _no_rnd_pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
184 FUNC(OPNAME ## _no_rnd_pixels8_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
187 static inline void FUNCC(OPNAME ## _pixels8_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
188 FUNC(OPNAME ## _pixels8_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
191 static inline void FUNC(OPNAME ## _pixels8_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
192 int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
193 /* FIXME HIGH BIT DEPTH */\
196 uint32_t a, b, c, d, l0, l1, h0, h1;\
197 a= AV_RN32(&src1[i*src_stride1]);\
198 b= AV_RN32(&src2[i*src_stride2]);\
199 c= AV_RN32(&src3[i*src_stride3]);\
200 d= AV_RN32(&src4[i*src_stride4]);\
201 l0= (a&0x03030303UL)\
204 h0= ((a&0xFCFCFCFCUL)>>2)\
205 + ((b&0xFCFCFCFCUL)>>2);\
206 l1= (c&0x03030303UL)\
208 h1= ((c&0xFCFCFCFCUL)>>2)\
209 + ((d&0xFCFCFCFCUL)>>2);\
210 OP(*((uint32_t*)&dst[i*dst_stride]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
211 a= AV_RN32(&src1[i*src_stride1+4]);\
212 b= AV_RN32(&src2[i*src_stride2+4]);\
213 c= AV_RN32(&src3[i*src_stride3+4]);\
214 d= AV_RN32(&src4[i*src_stride4+4]);\
215 l0= (a&0x03030303UL)\
218 h0= ((a&0xFCFCFCFCUL)>>2)\
219 + ((b&0xFCFCFCFCUL)>>2);\
220 l1= (c&0x03030303UL)\
222 h1= ((c&0xFCFCFCFCUL)>>2)\
223 + ((d&0xFCFCFCFCUL)>>2);\
224 OP(*((uint32_t*)&dst[i*dst_stride+4]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
228 static inline void FUNCC(OPNAME ## _pixels4_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
229 FUNC(OPNAME ## _pixels4_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
232 static inline void FUNCC(OPNAME ## _pixels4_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
233 FUNC(OPNAME ## _pixels4_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
236 static inline void FUNCC(OPNAME ## _pixels2_x2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
237 FUNC(OPNAME ## _pixels2_l2)(block, pixels, pixels+sizeof(pixel), line_size, line_size, line_size, h);\
240 static inline void FUNCC(OPNAME ## _pixels2_y2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h){\
241 FUNC(OPNAME ## _pixels2_l2)(block, pixels, pixels+line_size, line_size, line_size, line_size, h);\
244 static inline void FUNC(OPNAME ## _no_rnd_pixels8_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
245 int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
246 /* FIXME HIGH BIT DEPTH*/\
249 uint32_t a, b, c, d, l0, l1, h0, h1;\
250 a= AV_RN32(&src1[i*src_stride1]);\
251 b= AV_RN32(&src2[i*src_stride2]);\
252 c= AV_RN32(&src3[i*src_stride3]);\
253 d= AV_RN32(&src4[i*src_stride4]);\
254 l0= (a&0x03030303UL)\
257 h0= ((a&0xFCFCFCFCUL)>>2)\
258 + ((b&0xFCFCFCFCUL)>>2);\
259 l1= (c&0x03030303UL)\
261 h1= ((c&0xFCFCFCFCUL)>>2)\
262 + ((d&0xFCFCFCFCUL)>>2);\
263 OP(*((uint32_t*)&dst[i*dst_stride]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
264 a= AV_RN32(&src1[i*src_stride1+4]);\
265 b= AV_RN32(&src2[i*src_stride2+4]);\
266 c= AV_RN32(&src3[i*src_stride3+4]);\
267 d= AV_RN32(&src4[i*src_stride4+4]);\
268 l0= (a&0x03030303UL)\
271 h0= ((a&0xFCFCFCFCUL)>>2)\
272 + ((b&0xFCFCFCFCUL)>>2);\
273 l1= (c&0x03030303UL)\
275 h1= ((c&0xFCFCFCFCUL)>>2)\
276 + ((d&0xFCFCFCFCUL)>>2);\
277 OP(*((uint32_t*)&dst[i*dst_stride+4]), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
280 static inline void FUNC(OPNAME ## _pixels16_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
281 int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
282 FUNC(OPNAME ## _pixels8_l4)(dst , src1 , src2 , src3 , src4 , dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
283 FUNC(OPNAME ## _pixels8_l4)(dst+8*sizeof(pixel), src1+8*sizeof(pixel), src2+8*sizeof(pixel), src3+8*sizeof(pixel), src4+8*sizeof(pixel), dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
285 static inline void FUNC(OPNAME ## _no_rnd_pixels16_l4)(uint8_t *dst, const uint8_t *src1, const uint8_t *src2, const uint8_t *src3, const uint8_t *src4,\
286 int dst_stride, int src_stride1, int src_stride2,int src_stride3,int src_stride4, int h){\
287 FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst , src1 , src2 , src3 , src4 , dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
288 FUNC(OPNAME ## _no_rnd_pixels8_l4)(dst+8*sizeof(pixel), src1+8*sizeof(pixel), src2+8*sizeof(pixel), src3+8*sizeof(pixel), src4+8*sizeof(pixel), dst_stride, src_stride1, src_stride2, src_stride3, src_stride4, h);\
291 static inline void FUNCC(OPNAME ## _pixels2_xy2)(uint8_t *_block, const uint8_t *_pixels, ptrdiff_t line_size, int h)\
293 int i, a0, b0, a1, b1;\
294 pixel *block = (pixel*)_block;\
295 const pixel *pixels = (const pixel*)_pixels;\
296 line_size /= sizeof(pixel);\
303 for(i=0; i<h; i+=2){\
309 block[0]= (a1+a0)>>2; /* FIXME non put */\
310 block[1]= (b1+b0)>>2;\
320 block[0]= (a1+a0)>>2;\
321 block[1]= (b1+b0)>>2;\
327 static inline void FUNCC(OPNAME ## _pixels4_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
329 /* FIXME HIGH BIT DEPTH */\
331 const uint32_t a= AV_RN32(pixels );\
332 const uint32_t b= AV_RN32(pixels+1);\
333 uint32_t l0= (a&0x03030303UL)\
336 uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
337 + ((b&0xFCFCFCFCUL)>>2);\
341 for(i=0; i<h; i+=2){\
342 uint32_t a= AV_RN32(pixels );\
343 uint32_t b= AV_RN32(pixels+1);\
344 l1= (a&0x03030303UL)\
346 h1= ((a&0xFCFCFCFCUL)>>2)\
347 + ((b&0xFCFCFCFCUL)>>2);\
348 OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
351 a= AV_RN32(pixels );\
352 b= AV_RN32(pixels+1);\
353 l0= (a&0x03030303UL)\
356 h0= ((a&0xFCFCFCFCUL)>>2)\
357 + ((b&0xFCFCFCFCUL)>>2);\
358 OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
364 static inline void FUNCC(OPNAME ## _pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
366 /* FIXME HIGH BIT DEPTH */\
370 const uint32_t a= AV_RN32(pixels );\
371 const uint32_t b= AV_RN32(pixels+1);\
372 uint32_t l0= (a&0x03030303UL)\
375 uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
376 + ((b&0xFCFCFCFCUL)>>2);\
380 for(i=0; i<h; i+=2){\
381 uint32_t a= AV_RN32(pixels );\
382 uint32_t b= AV_RN32(pixels+1);\
383 l1= (a&0x03030303UL)\
385 h1= ((a&0xFCFCFCFCUL)>>2)\
386 + ((b&0xFCFCFCFCUL)>>2);\
387 OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
390 a= AV_RN32(pixels );\
391 b= AV_RN32(pixels+1);\
392 l0= (a&0x03030303UL)\
395 h0= ((a&0xFCFCFCFCUL)>>2)\
396 + ((b&0xFCFCFCFCUL)>>2);\
397 OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
401 pixels+=4-line_size*(h+1);\
402 block +=4-line_size*h;\
406 static inline void FUNCC(OPNAME ## _no_rnd_pixels8_xy2)(uint8_t *block, const uint8_t *pixels, ptrdiff_t line_size, int h)\
408 /* FIXME HIGH BIT DEPTH */\
412 const uint32_t a= AV_RN32(pixels );\
413 const uint32_t b= AV_RN32(pixels+1);\
414 uint32_t l0= (a&0x03030303UL)\
417 uint32_t h0= ((a&0xFCFCFCFCUL)>>2)\
418 + ((b&0xFCFCFCFCUL)>>2);\
422 for(i=0; i<h; i+=2){\
423 uint32_t a= AV_RN32(pixels );\
424 uint32_t b= AV_RN32(pixels+1);\
425 l1= (a&0x03030303UL)\
427 h1= ((a&0xFCFCFCFCUL)>>2)\
428 + ((b&0xFCFCFCFCUL)>>2);\
429 OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
432 a= AV_RN32(pixels );\
433 b= AV_RN32(pixels+1);\
434 l0= (a&0x03030303UL)\
437 h0= ((a&0xFCFCFCFCUL)>>2)\
438 + ((b&0xFCFCFCFCUL)>>2);\
439 OP(*((uint32_t*)block), h0+h1+(((l0+l1)>>2)&0x0F0F0F0FUL));\
443 pixels+=4-line_size*(h+1);\
444 block +=4-line_size*h;\
448 CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_x2) , FUNCC(OPNAME ## _pixels8_x2) , 8*sizeof(pixel))\
449 CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_y2) , FUNCC(OPNAME ## _pixels8_y2) , 8*sizeof(pixel))\
450 CALL_2X_PIXELS(FUNCC(OPNAME ## _pixels16_xy2), FUNCC(OPNAME ## _pixels8_xy2), 8*sizeof(pixel))\
451 av_unused CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16) , FUNCC(OPNAME ## _pixels8) , 8*sizeof(pixel))\
452 CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_x2) , FUNCC(OPNAME ## _no_rnd_pixels8_x2) , 8*sizeof(pixel))\
453 CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_y2) , FUNCC(OPNAME ## _no_rnd_pixels8_y2) , 8*sizeof(pixel))\
454 CALL_2X_PIXELS(FUNCC(OPNAME ## _no_rnd_pixels16_xy2), FUNCC(OPNAME ## _no_rnd_pixels8_xy2), 8*sizeof(pixel))\
456 #define op_avg(a, b) a = rnd_avg_pixel4(a, b)
457 #define op_put(a, b) a = b
459 #define put_no_rnd_pixels8_8_c put_pixels8_8_c
466 void FUNCC(ff_put_pixels8x8)(uint8_t *dst, uint8_t *src, int stride) {
467 FUNCC(put_pixels8)(dst, src, stride, 8);
469 void FUNCC(ff_avg_pixels8x8)(uint8_t *dst, uint8_t *src, int stride) {
470 FUNCC(avg_pixels8)(dst, src, stride, 8);
472 void FUNCC(ff_put_pixels16x16)(uint8_t *dst, uint8_t *src, int stride) {
473 FUNCC(put_pixels16)(dst, src, stride, 16);
475 void FUNCC(ff_avg_pixels16x16)(uint8_t *dst, uint8_t *src, int stride) {
476 FUNCC(avg_pixels16)(dst, src, stride, 16);