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