]> git.sesse.net Git - x264/blob - common/mc.c
15d4498dc977daf427adb783ac919dc5f1f98077
[x264] / common / mc.c
1 /*****************************************************************************
2  * mc.c: h264 encoder library (Motion Compensation)
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: mc.c,v 1.1 2004/06/03 19:27:07 fenrir Exp $
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_STDINT_H
25 #include <stdint.h>
26 #else
27 #include <inttypes.h>
28 #endif
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdarg.h>
33
34 #include "x264.h"
35
36 #include "pixel.h"
37 #include "mc.h"
38 #include "clip1.h"
39 #include "frame.h"
40
41 #ifdef HAVE_MMXEXT
42 #include "i386/mc.h"
43 #endif
44 #ifdef ARCH_PPC
45 #include "ppc/mc.h"
46 #endif
47
48
49 static inline int x264_tapfilter( uint8_t *pix, int i_pix_next )
50 {
51     return pix[-2*i_pix_next] - 5*pix[-1*i_pix_next] + 20*(pix[0] + pix[1*i_pix_next]) - 5*pix[ 2*i_pix_next] + pix[ 3*i_pix_next];
52 }
53 static inline int x264_tapfilter1( uint8_t *pix )
54 {
55     return pix[-2] - 5*pix[-1] + 20*(pix[0] + pix[1]) - 5*pix[ 2] + pix[ 3];
56 }
57
58 static inline void pixel_avg( uint8_t *dst,  int i_dst_stride,
59                               uint8_t *src1, int i_src1_stride,
60                               uint8_t *src2, int i_src2_stride,
61                               int i_width, int i_height )
62 {
63     int x, y;
64     for( y = 0; y < i_height; y++ )
65     {
66         for( x = 0; x < i_width; x++ )
67         {
68             dst[x] = ( src1[x] + src2[x] + 1 ) >> 1;
69         }
70         dst  += i_dst_stride;
71         src1 += i_src1_stride;
72         src2 += i_src2_stride;
73     }
74 }
75
76 static inline void pixel_avg_wxh( uint8_t *dst, int i_dst, uint8_t *src, int i_src, int width, int height )
77 {
78     int x, y;
79     for( y = 0; y < height; y++ )
80     {
81         for( x = 0; x < width; x++ )
82         {
83             dst[x] = ( dst[x] + src[x] + 1 ) >> 1;
84         }
85         dst += i_dst;
86         src += i_src;
87     }
88 }
89
90 #define PIXEL_AVG_C( name, width, height ) \
91 static void name( uint8_t *pix1, int i_stride_pix1, \
92                   uint8_t *pix2, int i_stride_pix2 ) \
93 { \
94     pixel_avg_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, width, height ); \
95 }
96 PIXEL_AVG_C( pixel_avg_16x16, 16, 16 )
97 PIXEL_AVG_C( pixel_avg_16x8,  16, 8 )
98 PIXEL_AVG_C( pixel_avg_8x16,  8, 16 )
99 PIXEL_AVG_C( pixel_avg_8x8,   8, 8 )
100 PIXEL_AVG_C( pixel_avg_8x4,   8, 4 )
101 PIXEL_AVG_C( pixel_avg_4x8,   4, 8 )
102 PIXEL_AVG_C( pixel_avg_4x4,   4, 4 )
103 PIXEL_AVG_C( pixel_avg_4x2,   4, 2 )
104 PIXEL_AVG_C( pixel_avg_2x4,   2, 4 )
105 PIXEL_AVG_C( pixel_avg_2x2,   2, 2 )
106
107
108 /* Implicit weighted bipred only:
109  * assumes log2_denom = 5, offset = 0, weight1 + weight2 = 64 */
110 #define op_scale2(x) dst[x] = x264_clip_uint8( (dst[x]*i_weight1 + src[x]*i_weight2 + (1<<5)) >> 6 )
111 static inline void pixel_avg_weight_wxh( uint8_t *dst, int i_dst, uint8_t *src, int i_src, int width, int height, int i_weight1 ){
112     int y;
113     const int i_weight2 = 64 - i_weight1;
114     for(y=0; y<height; y++, dst += i_dst, src += i_src){
115         op_scale2(0);
116         op_scale2(1);
117         if(width==2) continue;
118         op_scale2(2);
119         op_scale2(3);
120         if(width==4) continue;
121         op_scale2(4);
122         op_scale2(5);
123         op_scale2(6);
124         op_scale2(7);
125         if(width==8) continue;
126         op_scale2(8);
127         op_scale2(9);
128         op_scale2(10);
129         op_scale2(11);
130         op_scale2(12);
131         op_scale2(13);
132         op_scale2(14);
133         op_scale2(15);
134     }
135 }
136
137 #define PIXEL_AVG_WEIGHT_C( width, height ) \
138 static void pixel_avg_weight_##width##x##height( \
139                 uint8_t *pix1, int i_stride_pix1, \
140                 uint8_t *pix2, int i_stride_pix2, int i_weight1 ) \
141 { \
142     pixel_avg_weight_wxh( pix1, i_stride_pix1, pix2, i_stride_pix2, width, height, i_weight1 ); \
143 }
144
145 PIXEL_AVG_WEIGHT_C(16,16)
146 PIXEL_AVG_WEIGHT_C(16,8)
147 PIXEL_AVG_WEIGHT_C(8,16)
148 PIXEL_AVG_WEIGHT_C(8,8)
149 PIXEL_AVG_WEIGHT_C(8,4)
150 PIXEL_AVG_WEIGHT_C(4,8)
151 PIXEL_AVG_WEIGHT_C(4,4)
152 PIXEL_AVG_WEIGHT_C(4,2)
153 PIXEL_AVG_WEIGHT_C(2,4)
154 PIXEL_AVG_WEIGHT_C(2,2)
155 #undef op_scale2
156 #undef PIXEL_AVG_WEIGHT_C
157
158 typedef void (*pf_mc_t)(uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height );
159
160 static void mc_copy( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
161 {
162     int y;
163
164     for( y = 0; y < i_height; y++ )
165     {
166         memcpy( dst, src, i_width );
167
168         src += i_src_stride;
169         dst += i_dst_stride;
170     }
171 }
172 static inline void mc_hh( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
173 {
174     int x, y;
175
176     for( y = 0; y < i_height; y++ )
177     {
178         for( x = 0; x < i_width; x++ )
179         {
180             dst[x] = x264_mc_clip1( ( x264_tapfilter1( &src[x] ) + 16 ) >> 5 );
181         }
182         src += i_src_stride;
183         dst += i_dst_stride;
184     }
185 }
186 static inline void mc_hv( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
187 {
188     int x, y;
189
190     for( y = 0; y < i_height; y++ )
191     {
192         for( x = 0; x < i_width; x++ )
193         {
194             dst[x] = x264_mc_clip1( ( x264_tapfilter( &src[x], i_src_stride ) + 16 ) >> 5 );
195         }
196         src += i_src_stride;
197         dst += i_dst_stride;
198     }
199 }
200 static inline void mc_hc( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
201 {
202     uint8_t *out;
203     uint8_t *pix;
204     int x, y;
205
206     for( x = 0; x < i_width; x++ )
207     {
208         int tap[6];
209
210         pix = &src[x];
211         out = &dst[x];
212
213         tap[0] = x264_tapfilter1( &pix[-2*i_src_stride] );
214         tap[1] = x264_tapfilter1( &pix[-1*i_src_stride] );
215         tap[2] = x264_tapfilter1( &pix[ 0*i_src_stride] );
216         tap[3] = x264_tapfilter1( &pix[ 1*i_src_stride] );
217         tap[4] = x264_tapfilter1( &pix[ 2*i_src_stride] );
218
219         for( y = 0; y < i_height; y++ )
220         {
221             tap[5] = x264_tapfilter1( &pix[ 3*i_src_stride] );
222
223             *out = x264_mc_clip1( ( tap[0] - 5*tap[1] + 20 * tap[2] + 20 * tap[3] -5*tap[4] + tap[5] + 512 ) >> 10 );
224
225             /* Next line */
226             pix += i_src_stride;
227             out += i_dst_stride;
228             tap[0] = tap[1];
229             tap[1] = tap[2];
230             tap[2] = tap[3];
231             tap[3] = tap[4];
232             tap[4] = tap[5];
233         }
234     }
235 }
236
237 #if 0
238 /* mc I+H */
239 static void mc_xy10( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
240 {
241     uint8_t tmp[16*16];
242     mc_hh( src, i_src_stride, tmp, i_width, i_width, i_height );
243     pixel_avg( dst, i_dst_stride, src, i_src_stride, tmp, i_width, i_width, i_height );
244 }
245 static void mc_xy30( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
246 {
247     uint8_t tmp[16*16];
248     mc_hh( src, i_src_stride, tmp, i_width, i_width, i_height );
249     pixel_avg( dst, i_dst_stride, src+1, i_src_stride, tmp, i_width, i_width, i_height );
250 }
251 /* mc I+V */
252 static void mc_xy01( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
253 {
254     uint8_t tmp[16*16];
255     mc_hv( src, i_src_stride, tmp, i_width, i_width, i_height );
256     pixel_avg( dst, i_dst_stride, src, i_src_stride, tmp, i_width, i_width, i_height );
257 }
258 static void mc_xy03( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
259 {
260     uint8_t tmp[16*16];
261     mc_hv( src, i_src_stride, tmp, i_width, i_width, i_height );
262     pixel_avg( dst, i_dst_stride, src+i_src_stride, i_src_stride, tmp, i_width, i_width, i_height );
263 }
264 /* H+V */
265 static void mc_xy11( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
266 {
267     uint8_t tmp1[16*16];
268     uint8_t tmp2[16*16];
269
270     mc_hv( src, i_src_stride, tmp1, i_width, i_width, i_height );
271     mc_hh( src, i_src_stride, tmp2, i_width, i_width, i_height );
272     pixel_avg( dst, i_dst_stride, tmp1, i_width, tmp2, i_width, i_width, i_height );
273 }
274 static void mc_xy31( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
275 {
276     uint8_t tmp1[16*16];
277     uint8_t tmp2[16*16];
278
279     mc_hv( src+1, i_src_stride, tmp1, i_width, i_width, i_height );
280     mc_hh( src,   i_src_stride, tmp2, i_width, i_width, i_height );
281     pixel_avg( dst, i_dst_stride, tmp1, i_width, tmp2, i_width, i_width, i_height );
282 }
283 static void mc_xy13( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
284 {
285     uint8_t tmp1[16*16];
286     uint8_t tmp2[16*16];
287
288     mc_hv( src,              i_src_stride, tmp1, i_width, i_width, i_height );
289     mc_hh( src+i_src_stride, i_src_stride, tmp2, i_width, i_width, i_height );
290     pixel_avg( dst, i_dst_stride, tmp1, i_width, tmp2, i_width, i_width, i_height );
291 }
292 static void mc_xy33( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
293 {
294     uint8_t tmp1[16*16];
295     uint8_t tmp2[16*16];
296
297     mc_hv( src+1,            i_src_stride, tmp1, i_width, i_width, i_height );
298     mc_hh( src+i_src_stride, i_src_stride, tmp2, i_width, i_width, i_height );
299     pixel_avg( dst, i_dst_stride, tmp1, i_width, tmp2, i_width, i_width, i_height );
300 }
301 static void mc_xy21( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
302 {
303     uint8_t tmp1[16*16];
304     uint8_t tmp2[16*16];
305
306     mc_hc( src, i_src_stride, tmp1, i_width, i_width, i_height );
307     mc_hh( src, i_src_stride, tmp2, i_width, i_width, i_height );
308     pixel_avg( dst, i_dst_stride, tmp1, i_width, tmp2, i_width, i_width, i_height );
309 }
310 static void mc_xy12( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
311 {
312     uint8_t tmp1[16*16];
313     uint8_t tmp2[16*16];
314
315     mc_hc( src, i_src_stride, tmp1, i_width, i_width, i_height );
316     mc_hv( src, i_src_stride, tmp2, i_width, i_width, i_height );
317     pixel_avg( dst, i_dst_stride, tmp1, i_width, tmp2, i_width, i_width, i_height );
318 }
319 static void mc_xy32( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
320 {
321     uint8_t tmp1[16*16];
322     uint8_t tmp2[16*16];
323
324     mc_hc( src,   i_src_stride, tmp1, i_width, i_width, i_height );
325     mc_hv( src+1, i_src_stride, tmp2, i_width, i_width, i_height );
326     pixel_avg( dst, i_dst_stride, tmp1, i_width, tmp2, i_width, i_width, i_height );
327 }
328 static void mc_xy23( uint8_t *src, int i_src_stride, uint8_t *dst, int i_dst_stride, int i_width, int i_height )
329 {
330     uint8_t tmp1[16*16];
331     uint8_t tmp2[16*16];
332
333     mc_hc( src,              i_src_stride, tmp1, i_width, i_width, i_height );
334     mc_hh( src+i_src_stride, i_src_stride, tmp2, i_width, i_width, i_height );
335     pixel_avg( dst, i_dst_stride, tmp1, i_width, tmp2, i_width, i_width, i_height );
336 }
337
338 static void motion_compensation_luma( uint8_t *src, int i_src_stride,
339                                       uint8_t *dst, int i_dst_stride,
340                                       int mvx,int mvy,
341                                       int i_width, int i_height )
342 {
343     static pf_mc_t pf_mc[4][4] =    /*XXX [dqy][dqx] */
344     {
345         { mc_copy,  mc_xy10,    mc_hh,      mc_xy30 },
346         { mc_xy01,  mc_xy11,    mc_xy21,    mc_xy31 },
347         { mc_hv,    mc_xy12,    mc_hc,      mc_xy32 },
348         { mc_xy03,  mc_xy13,    mc_xy23,    mc_xy33 },
349     };
350
351     src += (mvy >> 2) * i_src_stride + (mvx >> 2);
352     pf_mc[mvy&0x03][mvx&0x03]( src, i_src_stride, dst, i_dst_stride, i_width, i_height );
353 }
354 #endif
355
356 static void mc_luma( uint8_t *src[4], int i_src_stride,
357                      uint8_t *dst,    int i_dst_stride,
358                      int mvx,int mvy,
359                      int i_width, int i_height )
360 {
361     uint8_t *src1, *src2;
362
363     int correction = (mvx&1) && (mvy&1) && ((mvx&2) ^ (mvy&2));
364     int hpel1x = mvx>>1;
365     int hpel1y = (mvy+1-correction)>>1;
366     int filter1 = (hpel1x & 1) + ( (hpel1y & 1) << 1 );
367
368     src1 = src[filter1] + (hpel1y >> 1) * i_src_stride + (hpel1x >> 1);
369
370     if ( (mvx|mvy) & 1 ) /* qpel interpolation needed */
371     {
372         int hpel2x = (mvx+1)>>1;
373         int hpel2y = (mvy+correction)>>1;
374         int filter2 = (hpel2x & 1) + ( (hpel2y & 1) <<1 );
375
376         src2 = src[filter2] + (hpel2y >> 1) * i_src_stride + (hpel2x >> 1);
377     
378         pixel_avg( dst, i_dst_stride, src1, i_src_stride,
379                    src2, i_src_stride, i_width, i_height );
380     }
381     else
382     {
383         mc_copy( src1, i_src_stride, dst, i_dst_stride, i_width, i_height );
384     }
385 }
386
387 static uint8_t *get_ref( uint8_t *src[4], int i_src_stride,
388                          uint8_t *dst,    int * i_dst_stride,
389                          int mvx,int mvy,
390                          int i_width, int i_height )
391 {
392     uint8_t *src1, *src2;
393
394     int correction = (mvx&1) && (mvy&1) && ((mvx&2) ^ (mvy&2));
395     int hpel1x = mvx>>1;
396     int hpel1y = (mvy+1-correction)>>1;
397     int filter1 = (hpel1x & 1) + ( (hpel1y & 1) << 1 );
398
399     src1 = src[filter1] + (hpel1y >> 1) * i_src_stride + (hpel1x >> 1);
400
401     if ( (mvx|mvy) & 1 ) /* qpel interpolation needed */
402     {
403         int hpel2x = (mvx+1)>>1;
404         int hpel2y = (mvy+correction)>>1;
405         int filter2 = (hpel2x & 1) + ( (hpel2y & 1) <<1 );
406
407         src2 = src[filter2] + (hpel2y >> 1) * i_src_stride + (hpel2x >> 1);
408     
409         pixel_avg( dst, *i_dst_stride, src1, i_src_stride,
410                    src2, i_src_stride, i_width, i_height );
411
412         return dst;
413     }
414     else
415     {
416         *i_dst_stride = i_src_stride;
417         return src1;
418     }
419 }
420
421 /* full chroma mc (ie until 1/8 pixel)*/
422 static void motion_compensation_chroma( uint8_t *src, int i_src_stride,
423                                         uint8_t *dst, int i_dst_stride,
424                                         int mvx, int mvy,
425                                         int i_width, int i_height )
426 {
427     uint8_t *srcp;
428     int x, y;
429
430     const int d8x = mvx&0x07;
431     const int d8y = mvy&0x07;
432
433     const int cA = (8-d8x)*(8-d8y);
434     const int cB = d8x    *(8-d8y);
435     const int cC = (8-d8x)*d8y;
436     const int cD = d8x    *d8y;
437
438     src  += (mvy >> 3) * i_src_stride + (mvx >> 3);
439     srcp = &src[i_src_stride];
440
441     for( y = 0; y < i_height; y++ )
442     {
443         for( x = 0; x < i_width; x++ )
444         {
445             dst[x] = ( cA*src[x]  + cB*src[x+1] +
446                        cC*srcp[x] + cD*srcp[x+1] + 32 ) >> 6;
447         }
448         dst  += i_dst_stride;
449
450         src   = srcp;
451         srcp += i_src_stride;
452     }
453 }
454
455 #ifdef HAVE_MMXEXT
456 static void motion_compensation_chroma_sse( uint8_t *src, int i_src_stride,
457                                         uint8_t *dst, int i_dst_stride,
458                                         int mvx, int mvy,
459                                         int i_width, int i_height )
460 {
461     if (i_width == 2) {
462         motion_compensation_chroma(src, i_src_stride, dst, i_dst_stride,
463                                    mvx, mvy, i_width, i_height);
464     } else {
465         const int d8x = mvx&0x07;
466         const int d8y = mvy&0x07;
467         
468         src  += (mvy >> 3) * i_src_stride + (mvx >> 3);
469         
470         x264_mc_chroma_sse(src, i_src_stride, dst, i_dst_stride,
471                               d8x, d8y, i_height, i_width);
472     }
473 }
474 #endif
475
476 void x264_mc_init( int cpu, x264_mc_functions_t *pf )
477 {
478     pf->mc_luma   = mc_luma;
479     pf->get_ref   = get_ref;
480     pf->mc_chroma = motion_compensation_chroma;
481
482     pf->avg[PIXEL_16x16]= pixel_avg_16x16;
483     pf->avg[PIXEL_16x8] = pixel_avg_16x8;
484     pf->avg[PIXEL_8x16] = pixel_avg_8x16;
485     pf->avg[PIXEL_8x8]  = pixel_avg_8x8;
486     pf->avg[PIXEL_8x4]  = pixel_avg_8x4;
487     pf->avg[PIXEL_4x8]  = pixel_avg_4x8;
488     pf->avg[PIXEL_4x4]  = pixel_avg_4x4;
489     pf->avg[PIXEL_4x2]  = pixel_avg_4x2;
490     pf->avg[PIXEL_2x4]  = pixel_avg_2x4;
491     pf->avg[PIXEL_2x2]  = pixel_avg_2x2;
492     
493     pf->avg_weight[PIXEL_16x16]= pixel_avg_weight_16x16;
494     pf->avg_weight[PIXEL_16x8] = pixel_avg_weight_16x8;
495     pf->avg_weight[PIXEL_8x16] = pixel_avg_weight_8x16;
496     pf->avg_weight[PIXEL_8x8]  = pixel_avg_weight_8x8;
497     pf->avg_weight[PIXEL_8x4]  = pixel_avg_weight_8x4;
498     pf->avg_weight[PIXEL_4x8]  = pixel_avg_weight_4x8;
499     pf->avg_weight[PIXEL_4x4]  = pixel_avg_weight_4x4;
500     pf->avg_weight[PIXEL_4x2]  = pixel_avg_weight_4x2;
501     pf->avg_weight[PIXEL_2x4]  = pixel_avg_weight_2x4;
502     pf->avg_weight[PIXEL_2x2]  = pixel_avg_weight_2x2;
503
504 #ifdef HAVE_MMXEXT
505     if( cpu&X264_CPU_MMXEXT ) {
506         x264_mc_mmxext_init( pf );
507         pf->mc_chroma = motion_compensation_chroma_sse;
508     }
509 #endif
510 #ifdef HAVE_SSE2
511     if( cpu&X264_CPU_SSE2 )
512         x264_mc_sse2_init( pf );
513 #endif
514 #ifdef ARCH_PPC
515     if( cpu&X264_CPU_ALTIVEC )
516         x264_mc_altivec_init( pf );
517 #endif
518 }
519
520 #if 0
521 void get_funcs_mmx(pf_mc_t*, pf_mc_t*, pf_mc_t*);
522 void get_funcs_sse2(pf_mc_t*, pf_mc_t*, pf_mc_t*);
523 #endif
524
525 extern void x264_horizontal_filter_mmxext( uint8_t *dst, int i_dst_stride,
526                                            uint8_t *src, int i_src_stride,
527                                            int i_width, int i_height );
528 extern void x264_center_filter_mmxext( uint8_t *dst1, int i_dst1_stride,
529                                        uint8_t *dst2, int i_dst2_stride,
530                                        uint8_t *src, int i_src_stride,
531                                        int i_width, int i_height );
532
533 void x264_frame_filter( int cpu, x264_frame_t *frame )
534 {
535     const int x_inc = 16, y_inc = 16;
536     const int stride = frame->i_stride[0];
537     int x, y;
538
539     pf_mc_t int_h = mc_hh;
540     pf_mc_t int_v = mc_hv;
541     pf_mc_t int_hv = mc_hc;
542
543 #if 0
544 #ifdef HAVE_MMXEXT
545     if( cpu&X264_CPU_MMXEXT )
546         get_funcs_mmx(&int_h, &int_v, &int_hv);
547 #endif
548
549 #ifdef HAVE_SSE2
550     if( cpu&X264_CPU_SSE2 )
551         get_funcs_sse2(&int_h, &int_v, &int_hv);
552 #endif
553 #endif
554
555 #ifdef HAVE_MMXEXT
556     if ( cpu & X264_CPU_MMXEXT )
557     {
558         x264_horizontal_filter_mmxext(frame->filtered[1] - 8 * stride - 8, stride,
559             frame->plane[0] - 8 * stride - 8, stride,
560             stride - 48, frame->i_lines[0] + 16);
561         x264_center_filter_mmxext(frame->filtered[2] - 8 * stride - 8, stride,
562             frame->filtered[3] - 8 * stride - 8, stride,
563             frame->plane[0] - 8 * stride - 8, stride,
564             stride - 48, frame->i_lines[0] + 16);
565     }
566     else
567 #endif
568     {
569         for( y = -8; y < frame->i_lines[0]+8; y += y_inc )
570         {
571             uint8_t *p_in = frame->plane[0] + y * stride - 8;
572             uint8_t *p_h  = frame->filtered[1] + y * stride - 8;
573             uint8_t *p_v  = frame->filtered[2] + y * stride - 8;
574             uint8_t *p_hv = frame->filtered[3] + y * stride - 8;
575             for( x = -8; x < stride - 64 + 8; x += x_inc )
576             {
577                 int_h(  p_in, stride, p_h,  stride, x_inc, y_inc );
578                 int_v(  p_in, stride, p_v,  stride, x_inc, y_inc );
579                 int_hv( p_in, stride, p_hv, stride, x_inc, y_inc );
580
581                 p_h += x_inc;
582                 p_v += x_inc;
583                 p_hv += x_inc;
584                 p_in += x_inc;
585             }
586         }
587     }
588 }
589
590 void x264_frame_init_lowres( int cpu, x264_frame_t *frame )
591 {
592     // FIXME: tapfilter?
593     const int i_stride = frame->i_stride[0];
594     const int i_stride2 = frame->i_stride_lowres;
595     const int i_width2 = i_stride2 - 64;
596     int x, y, i;
597     for( y = 0; y < frame->i_lines_lowres - 1; y++ )
598     {
599         uint8_t *src0 = &frame->plane[0][2*y*i_stride];
600         uint8_t *src1 = src0+i_stride;
601         uint8_t *src2 = src1+i_stride;
602         uint8_t *dst0 = &frame->lowres[0][y*i_stride2];
603         uint8_t *dsth = &frame->lowres[1][y*i_stride2];
604         uint8_t *dstv = &frame->lowres[2][y*i_stride2];
605         uint8_t *dstc = &frame->lowres[3][y*i_stride2];
606         for( x = 0; x < i_width2 - 1; x++ )
607         {
608             dst0[x] = (src0[2*x  ] + src0[2*x+1] + src1[2*x  ] + src1[2*x+1] + 2) >> 2;
609             dsth[x] = (src0[2*x+1] + src0[2*x+2] + src1[2*x+1] + src1[2*x+2] + 2) >> 2;
610             dstv[x] = (src1[2*x  ] + src1[2*x+1] + src2[2*x  ] + src2[2*x+1] + 2) >> 2;
611             dstc[x] = (src1[2*x+1] + src1[2*x+2] + src2[2*x+1] + src2[2*x+2] + 2) >> 2;
612         }
613         dst0[x] = (src0[2*x  ] + src0[2*x+1] + src1[2*x  ] + src1[2*x+1] + 2) >> 2;
614         dstv[x] = (src1[2*x  ] + src1[2*x+1] + src2[2*x  ] + src2[2*x+1] + 2) >> 2;
615         dsth[x] = (src0[2*x+1] + src1[2*x+1] + 1) >> 1;
616         dstc[x] = (src1[2*x+1] + src2[2*x+1] + 1) >> 1;
617     }
618     for( i = 0; i < 4; i++ )
619         memcpy( &frame->lowres[i][y*i_stride2], &frame->lowres[i][(y-1)*i_stride2], i_width2 );
620
621     for( y = 0; y < 16; y++ )
622         for( x = 0; x < 16; x++ )
623             frame->i_cost_est[x][y] = -1;
624
625     x264_frame_expand_border_lowres( frame );
626 }
627