]> git.sesse.net Git - x264/blob - core/macroblock.c
* all: re-import of the CVS.
[x264] / core / macroblock.c
1 /*****************************************************************************
2  * macroblock.c: h264 encoder library
3  *****************************************************************************
4  * Copyright (C) 2003 Laurent Aimar
5  * $Id: macroblock.c,v 1.1 2004/06/03 19:27:06 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 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdint.h>
28
29 #include "common.h"
30 #include "macroblock.h"
31
32 static const uint8_t block_idx_x[16] =
33 {
34     0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3
35 };
36 static const uint8_t block_idx_y[16] =
37 {
38     0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3
39 };
40 static const uint8_t block_idx_xy[4][4] =
41 {
42     { 0, 2, 8,  10},
43     { 1, 3, 9,  11},
44     { 4, 6, 12, 14},
45     { 5, 7, 13, 15}
46 };
47
48 static const int dequant_mf[6][4][4] =
49 {
50     { {10, 13, 10, 13}, {13, 16, 13, 16}, {10, 13, 10, 13}, {13, 16, 13, 16} },
51     { {11, 14, 11, 14}, {14, 18, 14, 18}, {11, 14, 11, 14}, {14, 18, 14, 18} },
52     { {13, 16, 13, 16}, {16, 20, 16, 20}, {13, 16, 13, 16}, {16, 20, 16, 20} },
53     { {14, 18, 14, 18}, {18, 23, 18, 23}, {14, 18, 14, 18}, {18, 23, 18, 23} },
54     { {16, 20, 16, 20}, {20, 25, 20, 25}, {16, 20, 16, 20}, {20, 25, 20, 25} },
55     { {18, 23, 18, 23}, {23, 29, 23, 29}, {18, 23, 18, 23}, {23, 29, 23, 29} }
56 };
57
58 #if 0
59 static const int i_chroma_qp_table[52] =
60 {
61      0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
62     10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
63     20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
64     29, 30, 31, 32, 32, 33, 34, 34, 35, 35,
65     36, 36, 37, 37, 37, 38, 38, 38, 39, 39,
66     39, 39
67 };
68 #endif
69
70 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
71 {
72     const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
73     const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
74     const int m  = X264_MIN( ma, mb );
75
76     if( m < 0 )
77         return I_PRED_4x4_DC;
78
79     return m;
80 }
81
82 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
83 {
84     const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
85     const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
86
87     int i_ret = za + zb;
88
89     if( i_ret < 0x80 )
90     {
91         i_ret = ( i_ret + 1 ) >> 1;
92     }
93     return i_ret & 0x7f;
94 }
95
96 /****************************************************************************
97  * Scan and Quant functions
98  ****************************************************************************/
99 void x264_mb_dequant_2x2_dc( int16_t dct[2][2], int i_qscale )
100 {
101     const int i_qbits = i_qscale/6 - 1;
102
103     if( i_qbits >= 0 )
104     {
105         const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
106
107         dct[0][0] = dct[0][0] * i_dmf;
108         dct[0][1] = dct[0][1] * i_dmf;
109         dct[1][0] = dct[1][0] * i_dmf;
110         dct[1][1] = dct[1][1] * i_dmf;
111     }
112     else
113     {
114         const int i_dmf = dequant_mf[i_qscale%6][0][0];
115
116         dct[0][0] = ( dct[0][0] * i_dmf ) >> 1;
117         dct[0][1] = ( dct[0][1] * i_dmf ) >> 1;
118         dct[1][0] = ( dct[1][0] * i_dmf ) >> 1;
119         dct[1][1] = ( dct[1][1] * i_dmf ) >> 1;
120     }
121 }
122
123 void x264_mb_dequant_4x4_dc( int16_t dct[4][4], int i_qscale )
124 {
125     const int i_qbits = i_qscale/6 - 2;
126     int x,y;
127
128     if( i_qbits >= 0 )
129     {
130         const int i_dmf = dequant_mf[i_qscale%6][0][0] << i_qbits;
131
132         for( y = 0; y < 4; y++ )
133         {
134             for( x = 0; x < 4; x++ )
135             {
136                 dct[y][x] = dct[y][x] * i_dmf;
137             }
138         }
139     }
140     else
141     {
142         const int i_dmf = dequant_mf[i_qscale%6][0][0];
143         const int f = 1 << ( 1 + i_qbits );
144
145         for( y = 0; y < 4; y++ )
146         {
147             for( x = 0; x < 4; x++ )
148             {
149                 dct[y][x] = ( dct[y][x] * i_dmf + f ) >> (-i_qbits);
150             }
151         }
152     }
153 }
154
155 void x264_mb_dequant_4x4( int16_t dct[4][4], int i_qscale )
156 {
157     const int i_mf = i_qscale%6;
158     const int i_qbits = i_qscale/6;
159     int y;
160
161     for( y = 0; y < 4; y++ )
162     {
163         dct[y][0] = ( dct[y][0] * dequant_mf[i_mf][y][0] ) << i_qbits;
164         dct[y][1] = ( dct[y][1] * dequant_mf[i_mf][y][1] ) << i_qbits;
165         dct[y][2] = ( dct[y][2] * dequant_mf[i_mf][y][2] ) << i_qbits;
166         dct[y][3] = ( dct[y][3] * dequant_mf[i_mf][y][3] ) << i_qbits;
167     }
168 }
169
170 static inline int x264_median( int a, int b, int c )
171 {
172     int min = a, max =a;
173     if( b < min )
174         min = b;
175     else
176         max = b;    /* no need to do 'b > max' (more consuming than always doing affectation) */
177
178     if( c < min )
179         min = c;
180     else if( c > max )
181         max = c;
182
183     return a + b + c - min - max;
184 }
185
186 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
187 {
188     const int i8 = x264_scan8[idx];
189     const int i_ref= h->mb.cache.ref[i_list][i8];
190     int     i_refa = h->mb.cache.ref[i_list][i8 - 1];
191     int16_t *mv_a  = h->mb.cache.mv[i_list][i8 - 1];
192     int     i_refb = h->mb.cache.ref[i_list][i8 - 8];
193     int16_t *mv_b  = h->mb.cache.mv[i_list][i8 - 8];
194     int     i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
195     int16_t *mv_c  = h->mb.cache.mv[i_list][i8 - 8 + i_width];
196
197     int i_count;
198
199     if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
200     {
201         i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
202         mv_c   = h->mb.cache.mv[i_list][i8 - 8 - 1];
203     }
204
205     if( h->mb.i_partition == D_16x8 )
206     {
207         if( idx == 0 && i_refb == i_ref )
208         {
209             mvp[0] = mv_b[0];
210             mvp[1] = mv_b[1];
211             return;
212         }
213         else if( idx != 0 && i_refa == i_ref )
214         {
215             mvp[0] = mv_a[0];
216             mvp[1] = mv_a[1];
217             return;
218         }
219     }
220     else if( h->mb.i_partition == D_8x16 )
221     {
222         if( idx == 0 && i_refa == i_ref )
223         {
224             mvp[0] = mv_a[0];
225             mvp[1] = mv_a[1];
226             return;
227         }
228         else if( idx != 0 && i_refc == i_ref )
229         {
230             mvp[0] = mv_c[0];
231             mvp[1] = mv_c[1];
232             return;
233         }
234     }
235
236     i_count = 0;
237     if( i_refa == i_ref ) i_count++;
238     if( i_refb == i_ref ) i_count++;
239     if( i_refc == i_ref ) i_count++;
240
241     if( i_count > 1 )
242     {
243         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
244         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
245     }
246     else if( i_count == 1 )
247     {
248         if( i_refa == i_ref )
249         {
250             mvp[0] = mv_a[0];
251             mvp[1] = mv_a[1];
252         }
253         else if( i_refb == i_ref )
254         {
255             mvp[0] = mv_b[0];
256             mvp[1] = mv_b[1];
257         }
258         else
259         {
260             mvp[0] = mv_c[0];
261             mvp[1] = mv_c[1];
262         }
263     }
264     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
265     {
266         mvp[0] = mv_a[0];
267         mvp[1] = mv_a[1];
268     }
269     else
270     {
271         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
272         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
273     }
274 }
275
276 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
277 {
278     int     i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
279     int16_t *mv_a  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
280     int     i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
281     int16_t *mv_b  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
282     int     i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
283     int16_t *mv_c  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
284
285     int i_count;
286
287     if( i_refc == -2 )
288     {
289         i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
290         mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
291     }
292
293     i_count = 0;
294     if( i_refa == i_ref ) i_count++;
295     if( i_refb == i_ref ) i_count++;
296     if( i_refc == i_ref ) i_count++;
297
298     if( i_count > 1 )
299     {
300         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
301         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
302     }
303     else if( i_count == 1 )
304     {
305         if( i_refa == i_ref )
306         {
307             mvp[0] = mv_a[0];
308             mvp[1] = mv_a[1];
309         }
310         else if( i_refb == i_ref )
311         {
312             mvp[0] = mv_b[0];
313             mvp[1] = mv_b[1];
314         }
315         else
316         {
317             mvp[0] = mv_c[0];
318             mvp[1] = mv_c[1];
319         }
320     }
321     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
322     {
323         mvp[0] = mv_a[0];
324         mvp[1] = mv_a[1];
325     }
326     else
327     {
328         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
329         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
330     }
331 }
332
333
334 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
335 {
336     int     i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
337     int     i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
338     int16_t *mv_a  = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
339     int16_t *mv_b  = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
340
341     if( i_refa == -2 || i_refb == -2 ||
342         ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
343         ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
344     {
345         mv[0] = mv[1] = 0;
346     }
347     else
348     {
349         x264_mb_predict_mv_16x16( h, 0, 0, mv );
350     }
351 }
352
353 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
354 {
355     const int i8 = x264_scan8[0]+x+8*y;
356     const int i_ref = h->mb.cache.ref[0][i8];
357     const int mvx   = h->mb.cache.mv[0][i8][0];
358     const int mvy   = h->mb.cache.mv[0][i8][1];
359
360     h->mc[MC_LUMA]( &h->mb.pic.p_fref[0][i_ref][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
361                     &h->mb.pic.p_fdec[0][4*y * h->mb.pic.i_stride[0]+4*x],           h->mb.pic.i_stride[0],
362                     mvx, mvy, 4*width, 4*height );
363
364     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
365                       &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x],           h->mb.pic.i_stride[1],
366                       mvx, mvy, 2*width, 2*height );
367
368     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
369                       &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x],           h->mb.pic.i_stride[2],
370                       mvx, mvy, 2*width, 2*height );
371 }
372 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
373 {
374     const int i8 = x264_scan8[0]+x+8*y;
375     const int i_ref = h->mb.cache.ref[1][i8];
376     const int mvx   = h->mb.cache.mv[1][i8][0];
377     const int mvy   = h->mb.cache.mv[1][i8][1];
378
379     h->mc[MC_LUMA]( &h->mb.pic.p_fref[1][i_ref][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
380                     &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x],            h->mb.pic.i_stride[0],
381                     mvx, mvy, 4*width, 4*height );
382
383     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
384                       &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x],           h->mb.pic.i_stride[1],
385                       mvx, mvy, 2*width, 2*height );
386
387     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
388                       &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x],           h->mb.pic.i_stride[2],
389                       mvx, mvy, 2*width, 2*height );
390 }
391
392 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
393 {
394     const int i8 = x264_scan8[0]+x+8*y;
395
396     const int i_ref0 = h->mb.cache.ref[0][i8];
397     const int mvx0   = h->mb.cache.mv[0][i8][0];
398     const int mvy0   = h->mb.cache.mv[0][i8][1];
399
400     const int i_ref1 = h->mb.cache.ref[1][i8];
401     const int mvx1   = h->mb.cache.mv[1][i8][0];
402     const int mvy1   = h->mb.cache.mv[1][i8][1];
403     DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
404     int     i_mode = 0;
405
406     if( width == 4 && height == 4 ) i_mode = PIXEL_16x16;
407     else if( width == 4 && height == 2 ) i_mode = PIXEL_16x8;
408     else if( width == 2 && height == 4 ) i_mode = PIXEL_8x16;
409     else if( width == 2 && height == 2 ) i_mode = PIXEL_8x8;
410     else if( width == 2 && height == 1 ) i_mode = PIXEL_8x4;
411     else if( width == 1 && height == 2 ) i_mode = PIXEL_4x8;
412     else if( width == 1 && height == 1 ) i_mode = PIXEL_4x4;
413
414     h->mc[MC_LUMA]( &h->mb.pic.p_fref[0][i_ref0][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
415                     &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x],             h->mb.pic.i_stride[0],
416                     mvx0, mvy0, 4*width, 4*height );
417     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref0][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
418                       &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x],            h->mb.pic.i_stride[1],
419                       mvx0, mvy0, 2*width, 2*height );
420     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[0][i_ref0][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
421                       &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x],            h->mb.pic.i_stride[2],
422                       mvx0, mvy0, 2*width, 2*height );
423
424
425     h->mc[MC_LUMA]( &h->mb.pic.p_fref[1][i_ref1][0][4*y * h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0],
426                     tmp, 16, mvx1, mvy1, 4*width, 4*height );
427     h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y *h->mb.pic.i_stride[0]+4*x], h->mb.pic.i_stride[0], tmp, 16 );
428
429     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref1][1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
430                       tmp, 16, mvx1, mvy1, 2*width, 2*height );
431     h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[1][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1], tmp, 16 );
432
433     h->mc[MC_CHROMA]( &h->mb.pic.p_fref[1][i_ref1][2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
434                       tmp, 16, mvx1, mvy1, 2*width, 2*height );
435     h->pixf.avg[i_mode]( &h->mb.pic.p_fdec[2][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2], tmp, 16 );
436 }
437
438
439 void x264_mb_mc( x264_t *h )
440 {
441     if( h->mb.i_type == P_L0 )
442     {
443         if( h->mb.i_partition == D_16x16 )
444         {
445             x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
446         }
447         else if( h->mb.i_partition == D_16x8 )
448         {
449             x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
450             x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
451         }
452         else if( h->mb.i_partition == D_8x16 )
453         {
454             x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
455             x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
456         }
457     }
458     else if( h->mb.i_type == P_8x8 )
459     {
460         int i;
461         for( i = 0; i < 4; i++ )
462         {
463             const int x = 2*(i%2);
464             const int y = 2*(i/2);
465             switch( h->mb.i_sub_partition[i] )
466             {
467                 case D_L0_8x8:
468                     x264_mb_mc_0xywh( h, x, y, 2, 2 );
469                     break;
470                 case D_L0_8x4:
471                     x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
472                     x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
473                     break;
474                 case D_L0_4x8:
475                     x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
476                     x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
477                     break;
478                 case D_L0_4x4:
479                     x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
480                     x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
481                     x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
482                     x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
483                     break;
484             }
485         }
486     }
487     else if( h->mb.i_type == B_8x8 || h->mb.i_type == B_DIRECT )
488     {
489         fprintf( stderr, "mc_luma with unsupported mb\n" );
490         return;
491     }
492     else    /* B_*x* */
493     {
494         int b_list0[2];
495         int b_list1[2];
496
497         int i;
498
499         /* init ref list utilisations */
500         for( i = 0; i < 2; i++ )
501         {
502             b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
503             b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
504         }
505         if( h->mb.i_partition == D_16x16 )
506         {
507             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
508             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
509             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
510         }
511         else if( h->mb.i_partition == D_16x8 )
512         {
513             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
514             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
515             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
516
517             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
518             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
519             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
520         }
521         else if( h->mb.i_partition == D_8x16 )
522         {
523             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
524             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
525             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
526
527             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
528             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
529             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
530         }
531     }
532 }
533
534 void x264_macroblock_cache_init( x264_t *h )
535 {
536     int i_mb_count  = h->sps->i_mb_width * h->sps->i_mb_height;
537
538     h->mb.i_mb_stride = h->sps->i_mb_width;
539
540     h->mb.type= x264_malloc( i_mb_count * sizeof( int8_t) );
541     h->mb.qp  = x264_malloc( i_mb_count * sizeof( int8_t) );
542     h->mb.cbp = x264_malloc( i_mb_count * sizeof( int16_t) );
543
544     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
545     h->mb.intra4x4_pred_mode = x264_malloc( i_mb_count * 7 * sizeof( int8_t ) );
546
547     /* all coeffs */
548     h->mb.non_zero_count = x264_malloc( i_mb_count * 24 * sizeof( uint8_t ) );
549
550     h->mb.mv[0]  = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
551     h->mb.mv[1]  = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
552     h->mb.ref[0] = x264_malloc( 4 * i_mb_count * sizeof( int16_t ) );
553     h->mb.ref[1] = x264_malloc( 4 * i_mb_count * sizeof( int16_t ) );
554
555     if( h->param.b_cabac )
556     {
557         h->mb.chroma_pred_mode = x264_malloc( i_mb_count * sizeof( int8_t) );
558         h->mb.mvd[0] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
559         h->mb.mvd[1] = x264_malloc( 2*16 * i_mb_count * sizeof( int16_t ) );
560     }
561
562     /* init with not avaiable (for top right idx=7,15) */
563     memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
564     memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
565 }
566 void x264_macroblock_cache_end( x264_t *h )
567 {
568     if( h->param.b_cabac )
569     {
570         x264_free( h->mb.chroma_pred_mode );
571         x264_free( h->mb.mvd[0] );
572         x264_free( h->mb.mvd[1] );
573     }
574     x264_free( h->mb.mv[0] );
575     x264_free( h->mb.mv[1] );
576     x264_free( h->mb.ref[0] );
577     x264_free( h->mb.ref[1] );
578     x264_free( h->mb.intra4x4_pred_mode );
579     x264_free( h->mb.non_zero_count );
580     x264_free( h->mb.cbp );
581     x264_free( h->mb.qp );
582     x264_free( h->mb.type );
583 }
584
585
586 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
587 {
588     const int i_mb_4x4 = 16 * h->mb.i_mb_stride *i_mb_y + 4 * i_mb_x;
589     const int i_mb_8x8 =  4 * h->mb.i_mb_stride *i_mb_y + 2 * i_mb_x;
590
591     int i_top_xy = -1;
592     int i_left_xy = -1;
593     int i_top_type = -1;    /* gcc warn */
594     int i_left_type= -1;
595
596     int i;
597
598     /* init index */
599     h->mb.i_mb_x = i_mb_x;
600     h->mb.i_mb_y = i_mb_y;
601     h->mb.i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
602     h->mb.i_neighbour = 0;
603
604     /* load picture pointers */
605     for( i = 0; i < 3; i++ )
606     {
607         const int w = (i == 0 ? 16 : 8);
608         const int i_stride = h->fdec->i_stride[i];
609         int   j;
610
611         h->mb.pic.i_stride[i] = i_stride;
612
613         h->mb.pic.p_fenc[i] = &h->fenc->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
614
615         h->mb.pic.p_fdec[i] = &h->fdec->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
616
617         for( j = 0; j < h->i_ref0; j++ )
618         {
619             h->mb.pic.p_fref[0][j][i] = &h->fref0[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
620         }
621         for( j = 0; j < h->i_ref1; j++ )
622         {
623             h->mb.pic.p_fref[1][j][i] = &h->fref1[j]->plane[i][ w * ( i_mb_x + i_mb_y * i_stride )];
624         }
625     }
626
627     /* load cache */
628     if( i_mb_y > 0 )
629     {
630         i_top_xy  = h->mb.i_mb_xy - h->mb.i_mb_stride;
631         i_top_type= h->mb.type[i_top_xy];
632
633         h->mb.i_neighbour |= MB_TOP;
634
635         /* load intra4x4 */
636         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
637         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
638         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
639         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
640
641         /* load non_zero_count */
642         h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
643         h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
644         h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
645         h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
646
647         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
648         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
649
650         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
651         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
652     }
653     else
654     {
655         /* load intra4x4 */
656         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
657         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
658         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
659         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
660
661         /* load non_zero_count */
662         h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
663         h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
664         h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
665         h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
666         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
667         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
668         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
669         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
670
671     }
672
673     if( i_mb_x > 0 )
674     {
675         i_left_xy  = h->mb.i_mb_xy - 1;
676         i_left_type= h->mb.type[i_left_xy];
677
678         h->mb.i_neighbour |= MB_LEFT;
679
680         /* load intra4x4 */
681         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
682         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
683         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
684         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
685
686         /* load non_zero_count */
687         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
688         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
689         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
690         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
691
692         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
693         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
694
695         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
696         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
697     }
698     else
699     {
700         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
701         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
702         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
703         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
704
705         /* load non_zero_count */
706         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
707         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
708         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
709         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
710         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
711         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
712         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
713         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
714     }
715
716     if( i_mb_y > 0 && i_mb_x < h->sps->i_mb_width - 1 )
717     {
718         h->mb.i_neighbour |= MB_TOPRIGHT;
719     }
720
721     /* load ref/mv/mvd */
722     if( h->sh.i_type != SLICE_TYPE_I )
723     {
724         int s8x8 = 2 * h->mb.i_mb_stride;
725         int s4x4 = 4 * h->mb.i_mb_stride;
726
727         int i_top_left_xy   = -1;
728         int i_top_right_xy  = -1;
729
730         int i_list;
731
732         if( h->mb.i_mb_y > 0 && h->mb.i_mb_x > 0 )
733         {
734             i_top_left_xy   = i_top_xy - 1;
735         }
736         if( h->mb.i_mb_y > 0 && h->mb.i_mb_x < h->sps->i_mb_width - 1 )
737         {
738             i_top_right_xy = i_top_xy + 1;
739         }
740
741         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1  : 2 ); i_list++ )
742         {
743             /*
744             h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
745             h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
746             h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
747             */
748
749             if( i_top_left_xy >= 0 )
750             {
751                 const int i8 = x264_scan8[0] - 1 - 1*8;
752                 const int ir = i_mb_8x8 - s8x8 - 1;
753                 const int iv = i_mb_4x4 - s4x4 - 1;
754                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
755                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
756                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
757             }
758             else
759             {
760                 const int i8 = x264_scan8[0] - 1 - 1*8;
761                 h->mb.cache.ref[i_list][i8] = -2;
762                 h->mb.cache.mv[i_list][i8][0] = 0;
763                 h->mb.cache.mv[i_list][i8][1] = 0;
764             }
765
766             if( i_top_xy >= 0 )
767             {
768                 const int i8 = x264_scan8[0] - 8;
769                 const int ir = i_mb_8x8 - s8x8;
770                 const int iv = i_mb_4x4 - s4x4;
771
772                 h->mb.cache.ref[i_list][i8+0] =
773                 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
774                 h->mb.cache.ref[i_list][i8+2] =
775                 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
776
777                 for( i = 0; i < 4; i++ )
778                 {
779                     h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
780                     h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
781                 }
782             }
783             else
784             {
785                 const int i8 = x264_scan8[0] - 8;
786                 for( i = 0; i < 4; i++ )
787                 {
788                     h->mb.cache.ref[i_list][i8+i] = -2;
789                     h->mb.cache.mv[i_list][i8+i][0] =
790                     h->mb.cache.mv[i_list][i8+i][1] = 0;
791                 }
792             }
793
794             if( i_top_right_xy >= 0 )
795             {
796                 const int i8 = x264_scan8[0] + 4 - 1*8;
797                 const int ir = i_mb_8x8 - s8x8 + 2;
798                 const int iv = i_mb_4x4 - s4x4 + 4;
799
800                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
801                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
802                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
803             }
804             else
805             {
806                 const int i8 = x264_scan8[0] + 4 - 1*8;
807                 h->mb.cache.ref[i_list][i8] = -2;
808                 h->mb.cache.mv[i_list][i8][0] = 0;
809                 h->mb.cache.mv[i_list][i8][1] = 0;
810             }
811
812             if( i_left_xy >= 0 )
813             {
814                 const int i8 = x264_scan8[0] - 1;
815                 const int ir = i_mb_8x8 - 1;
816                 const int iv = i_mb_4x4 - 1;
817
818                 h->mb.cache.ref[i_list][i8+0*8] =
819                 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
820                 h->mb.cache.ref[i_list][i8+2*8] =
821                 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
822
823                 for( i = 0; i < 4; i++ )
824                 {
825                     h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
826                     h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
827                 }
828             }
829             else
830             {
831                 const int i8 = x264_scan8[0] - 1;
832                 for( i = 0; i < 4; i++ )
833                 {
834                     h->mb.cache.ref[i_list][i8+i*8] = -2;
835                     h->mb.cache.mv[i_list][i8+i*8][0] =
836                     h->mb.cache.mv[i_list][i8+i*8][1] = 0;
837                 }
838             }
839
840             if( h->param.b_cabac )
841             {
842                 if( i_top_xy >= 0 )
843                 {
844                     const int i8 = x264_scan8[0] - 8;
845                     const int iv = i_mb_4x4 - s4x4;
846                     for( i = 0; i < 4; i++ )
847                     {
848                         h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
849                         h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
850                     }
851                 }
852                 else
853                 {
854                     const int i8 = x264_scan8[0] - 8;
855                     for( i = 0; i < 4; i++ )
856                     {
857                         h->mb.cache.mvd[i_list][i8+i][0] =
858                         h->mb.cache.mvd[i_list][i8+i][1] = 0;
859                     }
860                 }
861
862                 if( i_left_xy >= 0 )
863                 {
864                     const int i8 = x264_scan8[0] - 1;
865                     const int iv = i_mb_4x4 - 1;
866                     for( i = 0; i < 4; i++ )
867                     {
868                         h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
869                         h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
870                     }
871                 }
872                 else
873                 {
874                     const int i8 = x264_scan8[0] - 1;
875                     for( i = 0; i < 4; i++ )
876                     {
877                         h->mb.cache.mvd[i_list][i8+i*8][0] =
878                         h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
879                     }
880                 }
881             }
882         }
883     }
884 }
885
886 void x264_macroblock_cache_save( x264_t *h )
887 {
888     const int i_mb_xy = h->mb.i_mb_xy;
889     const int i_mb_type = h->mb.i_type;
890     const int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
891     const int i_mb_8x8 =  4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
892
893     int i;
894
895     h->mb.i_last_dqp = h->mb.qp[i_mb_xy] - h->mb.i_last_qp;
896     h->mb.i_last_qp = h->mb.qp[i_mb_xy];
897
898     /* save intra4x4 */
899     if( i_mb_type == I_4x4 )
900     {
901         h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
902         h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
903         h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
904         h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
905         h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
906         h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
907         h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
908     }
909     else
910     {
911         h->mb.intra4x4_pred_mode[i_mb_xy][0] =
912         h->mb.intra4x4_pred_mode[i_mb_xy][1] =
913         h->mb.intra4x4_pred_mode[i_mb_xy][2] =
914         h->mb.intra4x4_pred_mode[i_mb_xy][3] =
915         h->mb.intra4x4_pred_mode[i_mb_xy][4] =
916         h->mb.intra4x4_pred_mode[i_mb_xy][5] =
917         h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
918     }
919
920     if( i_mb_type == I_PCM )
921     {
922         h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
923         for( i = 0; i < 16 + 2*4; i++ )
924         {
925             h->mb.non_zero_count[i_mb_xy][i] = 16;
926         }
927     }
928     else
929     {
930         /* save non zero count */
931         for( i = 0; i < 16 + 2*4; i++ )
932         {
933             h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
934         }
935     }
936
937     if( !IS_INTRA( i_mb_type ) )
938     {
939         int i_list;
940         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1  : 2 ); i_list++ )
941         {
942             const int s8x8 = 2 * h->mb.i_mb_stride;
943             const int s4x4 = 4 * h->mb.i_mb_stride;
944             int y,x;
945
946             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
947             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
948             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
949             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
950
951             for( y = 0; y < 4; y++ )
952             {
953                 for( x = 0; x < 4; x++ )
954                 {
955                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][0];
956                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mv[i_list][x264_scan8[0]+x+8*y][1];
957                 }
958             }
959         }
960     }
961     else
962     {
963         int i_list;
964         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_P ? 1  : 2 ); i_list++ )
965         {
966             const int s8x8 = 2 * h->mb.i_mb_stride;
967             const int s4x4 = 4 * h->mb.i_mb_stride;
968             int y,x;
969
970             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
971             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
972             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
973             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
974
975             for( y = 0; y < 4; y++ )
976             {
977                 for( x = 0; x < 4; x++ )
978                 {
979                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
980                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
981                 }
982             }
983         }
984     }
985
986     if( h->param.b_cabac )
987     {
988         if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
989             h->mb.chroma_pred_mode[i_mb_xy] = h->mb.i_chroma_pred_mode;
990         else
991             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
992
993         if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) )
994         {
995             int i_list;
996             for( i_list  = 0; i_list < 2; i_list++ )
997             {
998                 const int s4x4 = 4 * h->mb.i_mb_stride;
999                 int y,x;
1000                 for( y = 0; y < 4; y++ )
1001                 {
1002                     for( x = 0; x < 4; x++ )
1003                     {
1004                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][0];
1005                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = h->mb.cache.mvd[i_list][x264_scan8[0]+x+8*y][1];
1006                     }
1007                 }
1008             }
1009         }
1010         else
1011         {
1012             int i_list;
1013             for( i_list  = 0; i_list < 2; i_list++ )
1014             {
1015                 const int s4x4 = 4 * h->mb.i_mb_stride;
1016                 int y,x;
1017                 for( y = 0; y < 4; y++ )
1018                 {
1019                     for( x = 0; x < 4; x++ )
1020                     {
1021                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1022                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1023                     }
1024                 }
1025             }
1026         }
1027     }
1028 }
1029