]> git.sesse.net Git - x264/blob - common/macroblock.c
skip intra pred+dct+quant in cases where it's redundant (analyse vs encode)
[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 "common.h"
25
26 int x264_mb_predict_intra4x4_mode( x264_t *h, int idx )
27 {
28     const int ma = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 1];
29     const int mb = h->mb.cache.intra4x4_pred_mode[x264_scan8[idx] - 8];
30     const int m  = X264_MIN( x264_mb_pred_mode4x4_fix(ma),
31                              x264_mb_pred_mode4x4_fix(mb) );
32
33     if( m < 0 )
34         return I_PRED_4x4_DC;
35
36     return m;
37 }
38
39 int x264_mb_predict_non_zero_code( x264_t *h, int idx )
40 {
41     const int za = h->mb.cache.non_zero_count[x264_scan8[idx] - 1];
42     const int zb = h->mb.cache.non_zero_count[x264_scan8[idx] - 8];
43
44     int i_ret = za + zb;
45
46     if( i_ret < 0x80 )
47     {
48         i_ret = ( i_ret + 1 ) >> 1;
49     }
50     return i_ret & 0x7f;
51 }
52
53 int x264_mb_transform_8x8_allowed( x264_t *h )
54 {
55     // intra and skip are disallowed
56     // large partitions are allowed
57     // direct and 8x8 are conditional
58     static const uint8_t partition_tab[X264_MBTYPE_MAX] = {
59         0,0,0,0,1,2,0,2,1,1,1,1,1,1,1,1,1,2,0,
60     };
61     int p, i;
62
63     if( !h->pps->b_transform_8x8_mode )
64         return 0;
65     p = partition_tab[h->mb.i_type];
66     if( p < 2 )
67         return p;
68     else if( h->mb.i_type == B_DIRECT )
69         return h->sps->b_direct8x8_inference;
70     else if( h->mb.i_type == P_8x8 )
71     {
72         if( !(h->param.analyse.inter & X264_ANALYSE_PSUB8x8) )
73             return 1;
74         for( i=0; i<4; i++ )
75             if( h->mb.i_sub_partition[i] != D_L0_8x8 )
76                 return 0;
77         return 1;
78     }
79     else // B_8x8
80     {
81         // x264 currently doesn't use sub-8x8 B partitions, so don't check for them
82         if( h->sps->b_direct8x8_inference )
83             return 1;
84         for( i=0; i<4; i++ )
85             if( h->mb.i_sub_partition[i] == D_DIRECT_8x8 )
86                 return 0;
87         return 1;
88     }
89 }
90
91 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int mvp[2] )
92 {
93     const int i8 = x264_scan8[idx];
94     const int i_ref= h->mb.cache.ref[i_list][i8];
95     int     i_refa = h->mb.cache.ref[i_list][i8 - 1];
96     int16_t *mv_a  = h->mb.cache.mv[i_list][i8 - 1];
97     int     i_refb = h->mb.cache.ref[i_list][i8 - 8];
98     int16_t *mv_b  = h->mb.cache.mv[i_list][i8 - 8];
99     int     i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width ];
100     int16_t *mv_c  = h->mb.cache.mv[i_list][i8 - 8 + i_width];
101
102     int i_count;
103
104     if( (idx&0x03) == 3 || ( i_width == 2 && (idx&0x3) == 2 )|| i_refc == -2 )
105     {
106         i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
107         mv_c   = h->mb.cache.mv[i_list][i8 - 8 - 1];
108     }
109
110     if( h->mb.i_partition == D_16x8 )
111     {
112         if( idx == 0 && i_refb == i_ref )
113         {
114             mvp[0] = mv_b[0];
115             mvp[1] = mv_b[1];
116             return;
117         }
118         else if( idx != 0 && i_refa == i_ref )
119         {
120             mvp[0] = mv_a[0];
121             mvp[1] = mv_a[1];
122             return;
123         }
124     }
125     else if( h->mb.i_partition == D_8x16 )
126     {
127         if( idx == 0 && i_refa == i_ref )
128         {
129             mvp[0] = mv_a[0];
130             mvp[1] = mv_a[1];
131             return;
132         }
133         else if( idx != 0 && i_refc == i_ref )
134         {
135             mvp[0] = mv_c[0];
136             mvp[1] = mv_c[1];
137             return;
138         }
139     }
140
141     i_count = 0;
142     if( i_refa == i_ref ) i_count++;
143     if( i_refb == i_ref ) i_count++;
144     if( i_refc == i_ref ) i_count++;
145
146     if( i_count > 1 )
147     {
148         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
149         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
150     }
151     else if( i_count == 1 )
152     {
153         if( i_refa == i_ref )
154         {
155             mvp[0] = mv_a[0];
156             mvp[1] = mv_a[1];
157         }
158         else if( i_refb == i_ref )
159         {
160             mvp[0] = mv_b[0];
161             mvp[1] = mv_b[1];
162         }
163         else
164         {
165             mvp[0] = mv_c[0];
166             mvp[1] = mv_c[1];
167         }
168     }
169     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
170     {
171         mvp[0] = mv_a[0];
172         mvp[1] = mv_a[1];
173     }
174     else
175     {
176         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
177         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
178     }
179 }
180
181 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int mvp[2] )
182 {
183     int     i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
184     int16_t *mv_a  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
185     int     i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
186     int16_t *mv_b  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
187     int     i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
188     int16_t *mv_c  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
189
190     int i_count;
191
192     if( i_refc == -2 )
193     {
194         i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
195         mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
196     }
197
198     i_count = 0;
199     if( i_refa == i_ref ) i_count++;
200     if( i_refb == i_ref ) i_count++;
201     if( i_refc == i_ref ) i_count++;
202
203     if( i_count > 1 )
204     {
205         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
206         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
207     }
208     else if( i_count == 1 )
209     {
210         if( i_refa == i_ref )
211         {
212             mvp[0] = mv_a[0];
213             mvp[1] = mv_a[1];
214         }
215         else if( i_refb == i_ref )
216         {
217             mvp[0] = mv_b[0];
218             mvp[1] = mv_b[1];
219         }
220         else
221         {
222             mvp[0] = mv_c[0];
223             mvp[1] = mv_c[1];
224         }
225     }
226     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
227     {
228         mvp[0] = mv_a[0];
229         mvp[1] = mv_a[1];
230     }
231     else
232     {
233         mvp[0] = x264_median( mv_a[0], mv_b[0], mv_c[0] );
234         mvp[1] = x264_median( mv_a[1], mv_b[1], mv_c[1] );
235     }
236 }
237
238
239 void x264_mb_predict_mv_pskip( x264_t *h, int mv[2] )
240 {
241     int     i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
242     int     i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
243     int16_t *mv_a  = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
244     int16_t *mv_b  = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
245
246     if( i_refa == -2 || i_refb == -2 ||
247         ( i_refa == 0 && mv_a[0] == 0 && mv_a[1] == 0 ) ||
248         ( i_refb == 0 && mv_b[0] == 0 && mv_b[1] == 0 ) )
249     {
250         mv[0] = mv[1] = 0;
251     }
252     else
253     {
254         x264_mb_predict_mv_16x16( h, 0, 0, mv );
255     }
256 }
257
258 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
259 {
260     int i_mb_4x4 = 16 * h->mb.i_mb_stride * h->mb.i_mb_y + 4 * h->mb.i_mb_x;
261     int i_mb_8x8 =  4 * h->mb.i_mb_stride * h->mb.i_mb_y + 2 * h->mb.i_mb_x;
262     int i8, i4;
263     int b8x8;
264     const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
265     
266     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
267     
268     if( IS_INTRA( type_col ) )
269     {
270         x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
271         x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 0, 0, 0 );
272         x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 1, 0, 0 );
273         return 1;
274     }
275     b8x8 = h->sps->b_direct8x8_inference ||
276            (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
277
278     for( i8 = 0; i8 < 4; i8++ )
279     {
280         const int x8 = i8%2;
281         const int y8 = i8/2;
282         const int i_part_8x8 = i_mb_8x8 + x8 + y8 * h->mb.i_b8_stride;
283         const int i_ref = h->mb.map_col_to_list0[ h->fref1[0]->ref[0][ i_part_8x8 ] ];
284
285         if( i_ref >= 0 )
286         {
287             const int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
288
289             x264_macroblock_cache_ref( h, 2*x8, 2*y8, 2, 2, 0, i_ref );
290
291             if( b8x8 )
292             {
293                 const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + 3*x8 + 3*y8 * h->mb.i_b4_stride];
294                 int mv_l0[2];
295                 mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
296                 mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
297                 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, mv_l0[0], mv_l0[1] );
298                 x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
299             }
300             else
301             {
302                 for( i4 = 0; i4 < 4; i4++ )
303                 {
304                     const int x4 = i4%2 + 2*x8;
305                     const int y4 = i4/2 + 2*y8;
306                     const int16_t *mv_col = h->fref1[0]->mv[0][ i_mb_4x4 + x4 + y4 * h->mb.i_b4_stride ];
307                     int mv_l0[2];
308                     mv_l0[0] = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
309                     mv_l0[1] = ( dist_scale_factor * mv_col[1] + 128 ) >> 8;
310                     x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, mv_l0[0], mv_l0[1] );
311                     x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, mv_l0[0] - mv_col[0], mv_l0[1] - mv_col[1] );
312                 }
313             }
314         }
315         else
316         {
317             /* the collocated ref isn't in the current list0 */
318             /* FIXME: we might still be able to use direct_8x8 on some partitions */
319             /* FIXME: with B-pyramid + extensive ref list reordering
320              *   (not currently used), we would also have to check
321              *   l1mv1 like in spatial mode */
322             return 0;
323         }
324     }
325
326     if( h->param.i_threads > 1 )
327     {
328         int di = b8x8 ? 4 : 1;
329         for( i4=0; i4<16; i4+=di )
330         {
331             if( h->mb.cache.mv[0][x264_scan8[i4]][1] > h->mb.mv_max_spel[1]
332              || h->mb.cache.mv[1][x264_scan8[i4]][1] > h->mb.mv_max_spel[1] )
333             {
334 #if 0
335                 fprintf(stderr, "direct_temporal: (%d,%d) (%d,%d) > %d \n",
336                         h->mb.cache.mv[0][x264_scan8[i4]][0],
337                         h->mb.cache.mv[0][x264_scan8[i4]][1],
338                         h->mb.cache.mv[1][x264_scan8[i4]][0],
339                         h->mb.cache.mv[1][x264_scan8[i4]][1],
340                         h->mb.mv_max_spel[1]);
341 #endif
342                 return 0;
343             }
344         }
345     }
346
347     return 1;
348 }
349
350 static int x264_mb_predict_mv_direct16x16_spatial( x264_t *h )
351 {
352     int ref[2];
353     int mv[2][2];
354     int i_list;
355     int i8, i4;
356     int b8x8;
357     const int8_t *l1ref0 = &h->fref1[0]->ref[0][ h->mb.i_b8_xy ];
358     const int8_t *l1ref1 = &h->fref1[0]->ref[1][ h->mb.i_b8_xy ];
359     const int16_t (*l1mv0)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[0][ h->mb.i_b4_xy ];
360     const int16_t (*l1mv1)[2] = (const int16_t (*)[2]) &h->fref1[0]->mv[1][ h->mb.i_b4_xy ];
361     const int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
362
363     for( i_list=0; i_list<2; i_list++ )
364     {
365         int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
366         int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
367         int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
368         if( i_refc == -2 )
369             i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
370
371         ref[i_list] = i_refa;
372         if( ref[i_list] < 0 || ( i_refb < ref[i_list] && i_refb >= 0 ))
373             ref[i_list] = i_refb;
374         if( ref[i_list] < 0 || ( i_refc < ref[i_list] && i_refc >= 0 ))
375             ref[i_list] = i_refc;
376         if( ref[i_list] < 0 )
377             ref[i_list] = -1;
378     }
379
380     if( ref[0] < 0 && ref[1] < 0 )
381     {
382         ref[0] = 
383         ref[1] = 0;
384         mv[0][0] = 
385         mv[0][1] = 
386         mv[1][0] = 
387         mv[1][1] = 0;
388     }
389     else
390     {
391         for( i_list=0; i_list<2; i_list++ )
392         {
393             if( ref[i_list] >= 0 )
394                 x264_mb_predict_mv_16x16( h, i_list, ref[i_list], mv[i_list] );
395             else
396                 mv[i_list][0] = mv[i_list][1] = 0;
397         }
398     }
399
400     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, ref[0] );
401     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, ref[1] );
402     x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 0, mv[0][0], mv[0][1] );
403     x264_macroblock_cache_mv(  h, 0, 0, 4, 4, 1, mv[1][0], mv[1][1] );
404
405     if( IS_INTRA( type_col ) )
406         return 1;
407
408     if( h->param.i_threads > 1
409         && ( mv[0][1] > h->mb.mv_max_spel[1]
410           || mv[1][1] > h->mb.mv_max_spel[1] ) )
411     {
412 #if 0
413         fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
414                 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
415                 h->mb.mv_max_spel[1]);
416 #endif
417         return 0;
418     }
419
420     b8x8 = h->sps->b_direct8x8_inference ||
421            (type_col != P_8x8 && type_col != B_SKIP && type_col != B_DIRECT && type_col != B_8x8);
422
423     /* col_zero_flag */
424     for( i8=0; i8<4; i8++ )
425     {
426         const int x8 = i8%2;
427         const int y8 = i8/2;
428         const int o8 = x8 + y8 * h->mb.i_b8_stride;
429         if( l1ref0[o8] == 0 || ( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) )
430         {
431             const int16_t (*l1mv)[2] = (l1ref0[o8] == 0) ? l1mv0 : l1mv1;
432             if( b8x8 )
433             {
434                 const int16_t *mvcol = l1mv[3*x8 + 3*y8 * h->mb.i_b4_stride];
435                 if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
436                 {
437                     if( ref[0] == 0 )
438                         x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 0, 0, 0 );
439                     if( ref[1] == 0 )
440                         x264_macroblock_cache_mv( h, 2*x8, 2*y8, 2, 2, 1, 0, 0 );
441                 }
442             }
443             else
444             {
445                 for( i4=0; i4<4; i4++ )
446                 {
447                     const int x4 = i4%2 + 2*x8;
448                     const int y4 = i4/2 + 2*y8;
449                     const int16_t *mvcol = l1mv[x4 + y4 * h->mb.i_b4_stride];
450                     if( abs( mvcol[0] ) <= 1 && abs( mvcol[1] ) <= 1 )
451                     {
452                         if( ref[0] == 0 )
453                             x264_macroblock_cache_mv( h, x4, y4, 1, 1, 0, 0, 0 );
454                         if( ref[1] == 0 )
455                             x264_macroblock_cache_mv( h, x4, y4, 1, 1, 1, 0, 0 );
456                     }
457                 }
458             }
459         }
460     }
461
462     return 1;
463 }
464
465 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
466 {
467     int b_available;
468     if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
469         return 0;
470     else if( h->sh.b_direct_spatial_mv_pred )
471         b_available = x264_mb_predict_mv_direct16x16_spatial( h );
472     else
473         b_available = x264_mb_predict_mv_direct16x16_temporal( h );
474
475     if( b_changed != NULL && b_available )
476     {
477         int type_col = h->fref1[0]->mb_type[ h->mb.i_mb_xy ];
478         if( IS_INTRA(type_col) || type_col == P_SKIP )
479         {
480             *b_changed = h->mb.cache.direct_ref[0][0] != h->mb.cache.ref[0][X264_SCAN8_0]
481                       || h->mb.cache.direct_ref[1][0] != h->mb.cache.ref[1][X264_SCAN8_0]
482                       || *(uint32_t*)h->mb.cache.direct_mv[0][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[0][X264_SCAN8_0]
483                       || *(uint32_t*)h->mb.cache.direct_mv[1][X264_SCAN8_0] != *(uint32_t*)h->mb.cache.mv[1][X264_SCAN8_0];
484         }
485         else
486         {
487             int i, l;
488             *b_changed = 0;
489             for( l = 0; l < 2; l++ )
490                 for( i = 0; i < 4; i++ )
491                     *b_changed |= h->mb.cache.direct_ref[l][i] != h->mb.cache.ref[l][x264_scan8[i*4]];
492             *b_changed = *b_changed || memcmp(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
493         }
494         if( !*b_changed )
495             return b_available;
496     }
497
498     /* cache ref & mv */
499     if( b_available )
500     {
501         int i, l;
502         for( l = 0; l < 2; l++ )
503             for( i = 0; i < 4; i++ )
504                 h->mb.cache.direct_ref[l][i] = h->mb.cache.ref[l][x264_scan8[i*4]];
505         h->mc.memcpy_aligned(h->mb.cache.direct_mv, h->mb.cache.mv, sizeof(h->mb.cache.mv));
506     }
507
508     return b_available;
509 }
510
511 void x264_mb_load_mv_direct8x8( x264_t *h, int idx )
512 {
513     const int x = 2*(idx%2);
514     const int y = 2*(idx/2);
515     int l;
516     x264_macroblock_cache_ref( h, x, y, 2, 2, 0, h->mb.cache.direct_ref[0][idx] );
517     x264_macroblock_cache_ref( h, x, y, 2, 2, 1, h->mb.cache.direct_ref[1][idx] );
518     for( l = 0; l < 2; l++ )
519     {
520         *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]] =
521         *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]];
522         *(uint64_t*)h->mb.cache.mv[l][x264_scan8[idx*4]+8] =
523         *(uint64_t*)h->mb.cache.direct_mv[l][x264_scan8[idx*4]+8];
524     }
525 }
526
527 #define FIXED_SCALE 256
528
529 /* This just improves encoder performance, it's not part of the spec */
530 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int mvc[8][2], int *i_mvc )
531 {
532     int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
533     int i = 0;
534
535 #define SET_MVP(mvp) { \
536         mvc[i][0] = mvp[0]; \
537         mvc[i][1] = mvp[1]; \
538         i++; \
539     }
540
541     /* b_direct */
542     if( h->sh.i_type == SLICE_TYPE_B
543         && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
544     {
545         SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
546     }
547
548     /* spatial predictors */
549     if( h->mb.i_neighbour & MB_LEFT )
550     {
551         int i_mb_l = h->mb.i_mb_xy - 1;
552         /* skip MBs didn't go through the whole search process, so mvr is undefined */
553         if( !IS_SKIP( h->mb.type[i_mb_l] ) )
554             SET_MVP( mvr[i_mb_l] );
555     }
556     if( h->mb.i_neighbour & MB_TOP )
557     {
558         int i_mb_t = h->mb.i_mb_top_xy;
559         if( !IS_SKIP( h->mb.type[i_mb_t] ) )
560             SET_MVP( mvr[i_mb_t] );
561
562         if( h->mb.i_neighbour & MB_TOPLEFT && !IS_SKIP( h->mb.type[i_mb_t - 1] ) )
563             SET_MVP( mvr[i_mb_t-1] );
564         if( h->mb.i_mb_x < h->mb.i_mb_stride - 1 && !IS_SKIP( h->mb.type[i_mb_t + 1] ) )
565             SET_MVP( mvr[i_mb_t+1] );
566     }
567 #undef SET_MVP
568
569     /* temporal predictors */
570     /* FIXME temporal scaling w/ interlace */
571     if( h->fref0[0]->i_ref[0] > 0 && !h->sh.b_mbaff )
572     {
573         x264_frame_t *l0 = h->fref0[0];
574
575 #define SET_TMVP(dx, dy) { \
576             int i_b4 = h->mb.i_b4_xy + dx*4 + dy*4*h->mb.i_b4_stride; \
577             int i_b8 = h->mb.i_b8_xy + dx*2 + dy*2*h->mb.i_b8_stride; \
578             int ref_col = l0->ref[0][i_b8]; \
579             if( ref_col >= 0 ) \
580             { \
581                 int scale = (h->fdec->i_poc - h->fdec->ref_poc[0][i_ref]) * l0->inv_ref_poc[ref_col];\
582                 mvc[i][0] = l0->mv[0][i_b4][0] * scale / FIXED_SCALE; \
583                 mvc[i][1] = l0->mv[0][i_b4][1] * scale / FIXED_SCALE; \
584                 i++; \
585             } \
586         }
587
588         SET_TMVP(0,0);
589         if( h->mb.i_mb_x < h->sps->i_mb_width-1 )
590             SET_TMVP(1,0);
591         if( h->mb.i_mb_y < h->sps->i_mb_height-1 )
592             SET_TMVP(0,1);
593 #undef SET_TMVP
594     }
595
596     *i_mvc = i;
597 }
598
599 /* Set up a lookup table for delta pocs to reduce an IDIV to an IMUL */
600 static void setup_inverse_delta_pocs( x264_t *h )
601 {
602     int i;
603     for( i = 0; i < h->i_ref0; i++ )
604     {
605         int delta = h->fdec->i_poc - h->fref0[i]->i_poc;
606         h->fdec->inv_ref_poc[i] = (FIXED_SCALE + delta/2) / delta;
607     }
608 }
609
610 static inline void x264_mb_mc_0xywh( x264_t *h, int x, int y, int width, int height )
611 {
612     const int i8 = x264_scan8[0]+x+8*y;
613     const int i_ref = h->mb.cache.ref[0][i8];
614     const int mvx   = x264_clip3( h->mb.cache.mv[0][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
615     int       mvy   = x264_clip3( h->mb.cache.mv[0][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
616
617     h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
618                    h->mb.pic.p_fref[0][i_ref], h->mb.pic.i_stride[0],
619                    mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
620
621     // chroma is offset if MCing from a field of opposite parity
622     if( h->mb.b_interlaced & i_ref )
623         mvy += (h->mb.i_mb_y & 1)*4 - 2;
624
625     h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
626                      &h->mb.pic.p_fref[0][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
627                      mvx, mvy, 2*width, 2*height );
628
629     h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
630                      &h->mb.pic.p_fref[0][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
631                      mvx, mvy, 2*width, 2*height );
632 }
633 static inline void x264_mb_mc_1xywh( x264_t *h, int x, int y, int width, int height )
634 {
635     const int i8 = x264_scan8[0]+x+8*y;
636     const int i_ref = h->mb.cache.ref[1][i8];
637     const int mvx   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
638     int       mvy   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
639
640     h->mc.mc_luma( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE,
641                    h->mb.pic.p_fref[1][i_ref], h->mb.pic.i_stride[0],
642                    mvx + 4*4*x, mvy + 4*4*y, 4*width, 4*height );
643
644     if( h->mb.b_interlaced & i_ref )
645         mvy += (h->mb.i_mb_y & 1)*4 - 2;
646
647     h->mc.mc_chroma( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
648                      &h->mb.pic.p_fref[1][i_ref][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
649                      mvx, mvy, 2*width, 2*height );
650
651     h->mc.mc_chroma( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE,
652                      &h->mb.pic.p_fref[1][i_ref][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
653                      mvx, mvy, 2*width, 2*height );
654 }
655
656 static inline void x264_mb_mc_01xywh( x264_t *h, int x, int y, int width, int height )
657 {
658     const int i8 = x264_scan8[0]+x+8*y;
659
660     const int i_ref1 = h->mb.cache.ref[1][i8];
661     const int mvx1   = x264_clip3( h->mb.cache.mv[1][i8][0], h->mb.mv_min[0], h->mb.mv_max[0] );
662     int       mvy1   = x264_clip3( h->mb.cache.mv[1][i8][1], h->mb.mv_min[1], h->mb.mv_max[1] );
663     DECLARE_ALIGNED( uint8_t, tmp[16*16], 16 );
664     int i_mode = x264_size2pixel[height][width];
665
666     x264_mb_mc_0xywh( h, x, y, width, height );
667
668     h->mc.mc_luma( tmp, 16, h->mb.pic.p_fref[1][i_ref1], h->mb.pic.i_stride[0],
669                    mvx1 + 4*4*x, mvy1 + 4*4*y, 4*width, 4*height );
670
671     if( h->mb.b_interlaced & i_ref1 )
672         mvy1 += (h->mb.i_mb_y & 1)*4 - 2;
673
674     if( h->param.analyse.b_weighted_bipred )
675     {
676         const int i_ref0 = h->mb.cache.ref[0][i8];
677         const int weight = h->mb.bipred_weight[i_ref0][i_ref1];
678
679         h->mc.avg_weight[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16, weight );
680
681         h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
682                          mvx1, mvy1, 2*width, 2*height );
683         h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
684
685         h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
686                          mvx1, mvy1, 2*width, 2*height );
687         h->mc.avg_weight[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16, weight );
688     }
689     else
690     {
691         h->mc.avg[i_mode]( &h->mb.pic.p_fdec[0][4*y*FDEC_STRIDE+4*x], FDEC_STRIDE, tmp, 16 );
692
693         h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][4][2*y*h->mb.pic.i_stride[1]+2*x], h->mb.pic.i_stride[1],
694                          mvx1, mvy1, 2*width, 2*height );
695         h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[1][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
696
697         h->mc.mc_chroma( tmp, 16, &h->mb.pic.p_fref[1][i_ref1][5][2*y*h->mb.pic.i_stride[2]+2*x], h->mb.pic.i_stride[2],
698                          mvx1, mvy1, 2*width, 2*height );
699         h->mc.avg[i_mode+3]( &h->mb.pic.p_fdec[2][2*y*FDEC_STRIDE+2*x], FDEC_STRIDE, tmp, 16 );
700     }
701 }
702
703 static void x264_mb_mc_direct8x8( x264_t *h, int x, int y )
704 {
705     const int i8 = x264_scan8[0] + x + 8*y;
706
707     /* FIXME: optimize based on current block size, not global settings? */
708     if( h->sps->b_direct8x8_inference )
709     {
710         if( h->mb.cache.ref[0][i8] >= 0 )
711             if( h->mb.cache.ref[1][i8] >= 0 )
712                 x264_mb_mc_01xywh( h, x, y, 2, 2 );
713             else
714                 x264_mb_mc_0xywh( h, x, y, 2, 2 );
715         else
716             x264_mb_mc_1xywh( h, x, y, 2, 2 );
717     }
718     else
719     {
720         if( h->mb.cache.ref[0][i8] >= 0 )
721         {
722             if( h->mb.cache.ref[1][i8] >= 0 )
723             {
724                 x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
725                 x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
726                 x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
727                 x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
728             }
729             else
730             {
731                 x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
732                 x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
733                 x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
734                 x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
735             }
736         }
737         else
738         {
739             x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
740             x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
741             x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
742             x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
743         }
744     }
745 }
746
747 void x264_mb_mc_8x8( x264_t *h, int i8 )
748 {
749     const int x = 2*(i8&1);
750     const int y = 2*(i8>>1);
751     switch( h->mb.i_sub_partition[i8] )
752     {
753         case D_L0_8x8:
754             x264_mb_mc_0xywh( h, x, y, 2, 2 );
755             break;
756         case D_L0_8x4:
757             x264_mb_mc_0xywh( h, x, y+0, 2, 1 );
758             x264_mb_mc_0xywh( h, x, y+1, 2, 1 );
759             break;
760         case D_L0_4x8:
761             x264_mb_mc_0xywh( h, x+0, y, 1, 2 );
762             x264_mb_mc_0xywh( h, x+1, y, 1, 2 );
763             break;
764         case D_L0_4x4:
765             x264_mb_mc_0xywh( h, x+0, y+0, 1, 1 );
766             x264_mb_mc_0xywh( h, x+1, y+0, 1, 1 );
767             x264_mb_mc_0xywh( h, x+0, y+1, 1, 1 );
768             x264_mb_mc_0xywh( h, x+1, y+1, 1, 1 );
769             break;
770         case D_L1_8x8:
771             x264_mb_mc_1xywh( h, x, y, 2, 2 );
772             break;
773         case D_L1_8x4:
774             x264_mb_mc_1xywh( h, x, y+0, 2, 1 );
775             x264_mb_mc_1xywh( h, x, y+1, 2, 1 );
776             break;
777         case D_L1_4x8:
778             x264_mb_mc_1xywh( h, x+0, y, 1, 2 );
779             x264_mb_mc_1xywh( h, x+1, y, 1, 2 );
780             break;
781         case D_L1_4x4:
782             x264_mb_mc_1xywh( h, x+0, y+0, 1, 1 );
783             x264_mb_mc_1xywh( h, x+1, y+0, 1, 1 );
784             x264_mb_mc_1xywh( h, x+0, y+1, 1, 1 );
785             x264_mb_mc_1xywh( h, x+1, y+1, 1, 1 );
786             break;
787         case D_BI_8x8:
788             x264_mb_mc_01xywh( h, x, y, 2, 2 );
789             break;
790         case D_BI_8x4:
791             x264_mb_mc_01xywh( h, x, y+0, 2, 1 );
792             x264_mb_mc_01xywh( h, x, y+1, 2, 1 );
793             break;
794         case D_BI_4x8:
795             x264_mb_mc_01xywh( h, x+0, y, 1, 2 );
796             x264_mb_mc_01xywh( h, x+1, y, 1, 2 );
797             break;
798         case D_BI_4x4:
799             x264_mb_mc_01xywh( h, x+0, y+0, 1, 1 );
800             x264_mb_mc_01xywh( h, x+1, y+0, 1, 1 );
801             x264_mb_mc_01xywh( h, x+0, y+1, 1, 1 );
802             x264_mb_mc_01xywh( h, x+1, y+1, 1, 1 );
803             break;
804         case D_DIRECT_8x8:
805             x264_mb_mc_direct8x8( h, x, y );
806             break;
807     }
808 }
809
810 void x264_mb_mc( x264_t *h )
811 {
812     if( h->mb.i_type == P_L0 )
813     {
814         if( h->mb.i_partition == D_16x16 )
815         {
816             x264_mb_mc_0xywh( h, 0, 0, 4, 4 );
817         }
818         else if( h->mb.i_partition == D_16x8 )
819         {
820             x264_mb_mc_0xywh( h, 0, 0, 4, 2 );
821             x264_mb_mc_0xywh( h, 0, 2, 4, 2 );
822         }
823         else if( h->mb.i_partition == D_8x16 )
824         {
825             x264_mb_mc_0xywh( h, 0, 0, 2, 4 );
826             x264_mb_mc_0xywh( h, 2, 0, 2, 4 );
827         }
828     }
829     else if( h->mb.i_type == P_8x8 || h->mb.i_type == B_8x8 )
830     {
831         int i;
832         for( i = 0; i < 4; i++ )
833             x264_mb_mc_8x8( h, i );
834     }
835     else if( h->mb.i_type == B_SKIP || h->mb.i_type == B_DIRECT )
836     {
837         x264_mb_mc_direct8x8( h, 0, 0 );
838         x264_mb_mc_direct8x8( h, 2, 0 );
839         x264_mb_mc_direct8x8( h, 0, 2 );
840         x264_mb_mc_direct8x8( h, 2, 2 );
841     }
842     else    /* B_*x* */
843     {
844         int b_list0[2];
845         int b_list1[2];
846
847         int i;
848
849         /* init ref list utilisations */
850         for( i = 0; i < 2; i++ )
851         {
852             b_list0[i] = x264_mb_type_list0_table[h->mb.i_type][i];
853             b_list1[i] = x264_mb_type_list1_table[h->mb.i_type][i];
854         }
855         if( h->mb.i_partition == D_16x16 )
856         {
857             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 4 );
858             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 4 );
859             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 4 );
860         }
861         else if( h->mb.i_partition == D_16x8 )
862         {
863             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 4, 2 );
864             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 4, 2 );
865             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 4, 2 );
866
867             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 0, 2, 4, 2 );
868             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 0, 2, 4, 2 );
869             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 0, 2, 4, 2 );
870         }
871         else if( h->mb.i_partition == D_8x16 )
872         {
873             if( b_list0[0] && b_list1[0] ) x264_mb_mc_01xywh( h, 0, 0, 2, 4 );
874             else if( b_list0[0] )          x264_mb_mc_0xywh ( h, 0, 0, 2, 4 );
875             else if( b_list1[0] )          x264_mb_mc_1xywh ( h, 0, 0, 2, 4 );
876
877             if( b_list0[1] && b_list1[1] ) x264_mb_mc_01xywh( h, 2, 0, 2, 4 );
878             else if( b_list0[1] )          x264_mb_mc_0xywh ( h, 2, 0, 2, 4 );
879             else if( b_list1[1] )          x264_mb_mc_1xywh ( h, 2, 0, 2, 4 );
880         }
881     }
882 }
883
884 int x264_macroblock_cache_init( x264_t *h )
885 {
886     int i, j;
887     int i_mb_count = h->mb.i_mb_count;
888
889     h->mb.i_mb_stride = h->sps->i_mb_width;
890     h->mb.i_b8_stride = h->sps->i_mb_width * 2;
891     h->mb.i_b4_stride = h->sps->i_mb_width * 4;
892
893     h->mb.b_interlaced = h->param.b_interlaced;
894
895     CHECKED_MALLOC( h->mb.qp, i_mb_count * sizeof(int8_t) );
896     CHECKED_MALLOC( h->mb.cbp, i_mb_count * sizeof(int16_t) );
897     CHECKED_MALLOC( h->mb.skipbp, i_mb_count * sizeof(int8_t) );
898     CHECKED_MALLOC( h->mb.mb_transform_size, i_mb_count * sizeof(int8_t) );
899
900     /* 0 -> 3 top(4), 4 -> 6 : left(3) */
901     CHECKED_MALLOC( h->mb.intra4x4_pred_mode, i_mb_count * 7 * sizeof(int8_t) );
902
903     /* all coeffs */
904     CHECKED_MALLOC( h->mb.non_zero_count, i_mb_count * 24 * sizeof(uint8_t) );
905     CHECKED_MALLOC( h->mb.nnz_backup, h->sps->i_mb_width * 4 * 16 * sizeof(uint8_t) );
906
907     if( h->param.b_cabac )
908     {
909         CHECKED_MALLOC( h->mb.chroma_pred_mode, i_mb_count * sizeof(int8_t) );
910         CHECKED_MALLOC( h->mb.mvd[0], 2*16 * i_mb_count * sizeof(int16_t) );
911         CHECKED_MALLOC( h->mb.mvd[1], 2*16 * i_mb_count * sizeof(int16_t) );
912     }
913
914     for( i=0; i<2; i++ )
915     {
916         int i_refs = X264_MIN(16, (i ? 1 : h->param.i_frame_reference) + h->param.b_bframe_pyramid) << h->param.b_interlaced;
917         for( j=0; j < i_refs; j++ )
918             CHECKED_MALLOC( h->mb.mvr[i][j], 2 * i_mb_count * sizeof(int16_t) );
919     }
920
921     for( i=0; i<=h->param.b_interlaced; i++ )
922         for( j=0; j<3; j++ )
923         {
924             CHECKED_MALLOC( h->mb.intra_border_backup[i][j], h->fdec->i_stride[j] );
925             h->mb.intra_border_backup[i][j] += 8;
926         }
927
928     /* init with not available (for top right idx=7,15) */
929     memset( h->mb.cache.ref[0], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
930     memset( h->mb.cache.ref[1], -2, X264_SCAN8_SIZE * sizeof( int8_t ) );
931
932     return 0;
933 fail: return -1;
934 }
935 void x264_macroblock_cache_end( x264_t *h )
936 {
937     int i, j;
938     for( i=0; i<=h->param.b_interlaced; i++ )
939         for( j=0; j<3; j++ )
940             x264_free( h->mb.intra_border_backup[i][j] - 8 );
941     for( i=0; i<2; i++ )
942         for( j=0; j<32; j++ )
943             x264_free( h->mb.mvr[i][j] );
944     if( h->param.b_cabac )
945     {
946         x264_free( h->mb.chroma_pred_mode );
947         x264_free( h->mb.mvd[0] );
948         x264_free( h->mb.mvd[1] );
949     }
950     x264_free( h->mb.intra4x4_pred_mode );
951     x264_free( h->mb.non_zero_count );
952     x264_free( h->mb.nnz_backup );
953     x264_free( h->mb.mb_transform_size );
954     x264_free( h->mb.skipbp );
955     x264_free( h->mb.cbp );
956     x264_free( h->mb.qp );
957 }
958 void x264_macroblock_slice_init( x264_t *h )
959 {
960     int i, j;
961
962     h->mb.mv[0] = h->fdec->mv[0];
963     h->mb.mv[1] = h->fdec->mv[1];
964     h->mb.ref[0] = h->fdec->ref[0];
965     h->mb.ref[1] = h->fdec->ref[1];
966     h->mb.type = h->fdec->mb_type;
967
968     h->fdec->i_ref[0] = h->i_ref0;
969     h->fdec->i_ref[1] = h->i_ref1;
970     for( i = 0; i < h->i_ref0; i++ )
971         h->fdec->ref_poc[0][i] = h->fref0[i]->i_poc;
972     if( h->sh.i_type == SLICE_TYPE_B )
973     {
974         for( i = 0; i < h->i_ref1; i++ )
975             h->fdec->ref_poc[1][i] = h->fref1[i]->i_poc;
976
977         h->mb.map_col_to_list0[-1] = -1;
978         h->mb.map_col_to_list0[-2] = -2;
979         for( i = 0; i < h->fref1[0]->i_ref[0]; i++ )
980         {
981             int poc = h->fref1[0]->ref_poc[0][i];
982             h->mb.map_col_to_list0[i] = -2;
983             for( j = 0; j < h->i_ref0; j++ )
984                 if( h->fref0[j]->i_poc == poc )
985                 {
986                     h->mb.map_col_to_list0[i] = j;
987                     break;
988                 }
989         }
990     }
991     if( h->sh.i_type == SLICE_TYPE_P )
992         memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
993
994     setup_inverse_delta_pocs( h );
995 }
996
997 void x264_prefetch_fenc( x264_t *h, x264_frame_t *fenc, int i_mb_x, int i_mb_y )
998 {
999     int stride_y  = fenc->i_stride[0];
1000     int stride_uv = fenc->i_stride[1];
1001     int off_y = 16 * (i_mb_x + i_mb_y * stride_y);
1002     int off_uv = 8 * (i_mb_x + i_mb_y * stride_uv);
1003     h->mc.prefetch_fenc( fenc->plane[0]+off_y, stride_y,
1004                          fenc->plane[1+(i_mb_x&1)]+off_uv, stride_uv, i_mb_x );
1005 }
1006
1007 void x264_macroblock_cache_load( x264_t *h, int i_mb_x, int i_mb_y )
1008 {
1009     int i_mb_xy = i_mb_y * h->mb.i_mb_stride + i_mb_x;
1010     int i_mb_4x4 = 4*(i_mb_y * h->mb.i_b4_stride + i_mb_x);
1011     int i_mb_8x8 = 2*(i_mb_y * h->mb.i_b8_stride + i_mb_x);
1012     int i_top_y = i_mb_y - (1 << h->mb.b_interlaced);
1013     int i_top_xy = i_top_y * h->mb.i_mb_stride + i_mb_x;
1014     int i_top_4x4 = (4*i_top_y+3) * h->mb.i_b4_stride + 4*i_mb_x;
1015     int i_top_8x8 = (2*i_top_y+1) * h->mb.i_b8_stride + 2*i_mb_x;
1016     int i_left_xy = -1;
1017     int i_top_type = -1;    /* gcc warn */
1018     int i_left_type= -1;
1019
1020     int i;
1021
1022     assert( h->mb.i_b8_stride == 2*h->mb.i_mb_stride );
1023     assert( h->mb.i_b4_stride == 4*h->mb.i_mb_stride );
1024
1025     /* init index */
1026     h->mb.i_mb_x = i_mb_x;
1027     h->mb.i_mb_y = i_mb_y;
1028     h->mb.i_mb_xy = i_mb_xy;
1029     h->mb.i_b8_xy = i_mb_8x8;
1030     h->mb.i_b4_xy = i_mb_4x4;
1031     h->mb.i_mb_top_xy = i_top_xy;
1032     h->mb.i_neighbour = 0;
1033
1034     /* load cache */
1035     if( i_top_xy >= h->sh.i_first_mb )
1036     {
1037         h->mb.i_mb_type_top =
1038         i_top_type= h->mb.type[i_top_xy];
1039
1040         h->mb.i_neighbour |= MB_TOP;
1041
1042         /* load intra4x4 */
1043         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][0];
1044         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][1];
1045         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][2];
1046         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = h->mb.intra4x4_pred_mode[i_top_xy][3];
1047
1048         /* load non_zero_count */
1049         h->mb.cache.non_zero_count[x264_scan8[0] - 8] = h->mb.non_zero_count[i_top_xy][10];
1050         h->mb.cache.non_zero_count[x264_scan8[1] - 8] = h->mb.non_zero_count[i_top_xy][11];
1051         h->mb.cache.non_zero_count[x264_scan8[4] - 8] = h->mb.non_zero_count[i_top_xy][14];
1052         h->mb.cache.non_zero_count[x264_scan8[5] - 8] = h->mb.non_zero_count[i_top_xy][15];
1053
1054         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] = h->mb.non_zero_count[i_top_xy][16+2];
1055         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] = h->mb.non_zero_count[i_top_xy][16+3];
1056
1057         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] = h->mb.non_zero_count[i_top_xy][16+4+2];
1058         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = h->mb.non_zero_count[i_top_xy][16+4+3];
1059     }
1060     else
1061     {
1062         h->mb.i_mb_type_top = -1;
1063         
1064         /* load intra4x4 */
1065         h->mb.cache.intra4x4_pred_mode[x264_scan8[0] - 8] =
1066         h->mb.cache.intra4x4_pred_mode[x264_scan8[1] - 8] =
1067         h->mb.cache.intra4x4_pred_mode[x264_scan8[4] - 8] =
1068         h->mb.cache.intra4x4_pred_mode[x264_scan8[5] - 8] = -1;
1069
1070         /* load non_zero_count */
1071         h->mb.cache.non_zero_count[x264_scan8[0] - 8] =
1072         h->mb.cache.non_zero_count[x264_scan8[1] - 8] =
1073         h->mb.cache.non_zero_count[x264_scan8[4] - 8] =
1074         h->mb.cache.non_zero_count[x264_scan8[5] - 8] =
1075         h->mb.cache.non_zero_count[x264_scan8[16+0] - 8] =
1076         h->mb.cache.non_zero_count[x264_scan8[16+1] - 8] =
1077         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 8] =
1078         h->mb.cache.non_zero_count[x264_scan8[16+4+1] - 8] = 0x80;
1079
1080     }
1081
1082     if( i_mb_x > 0 && i_mb_xy > h->sh.i_first_mb )
1083     {
1084         i_left_xy = i_mb_xy - 1;
1085         h->mb.i_mb_type_left =
1086         i_left_type = h->mb.type[i_left_xy];
1087
1088         h->mb.i_neighbour |= MB_LEFT;
1089
1090         /* load intra4x4 */
1091         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][4];
1092         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][5];
1093         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][6];
1094         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = h->mb.intra4x4_pred_mode[i_left_xy][3];
1095
1096         /* load non_zero_count */
1097         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] = h->mb.non_zero_count[i_left_xy][5];
1098         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] = h->mb.non_zero_count[i_left_xy][7];
1099         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] = h->mb.non_zero_count[i_left_xy][13];
1100         h->mb.cache.non_zero_count[x264_scan8[10] - 1] = h->mb.non_zero_count[i_left_xy][15];
1101
1102         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] = h->mb.non_zero_count[i_left_xy][16+1];
1103         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] = h->mb.non_zero_count[i_left_xy][16+3];
1104
1105         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] = h->mb.non_zero_count[i_left_xy][16+4+1];
1106         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = h->mb.non_zero_count[i_left_xy][16+4+3];
1107     }
1108     else
1109     {
1110         h->mb.i_mb_type_left = -1;
1111
1112         h->mb.cache.intra4x4_pred_mode[x264_scan8[0 ] - 1] =
1113         h->mb.cache.intra4x4_pred_mode[x264_scan8[2 ] - 1] =
1114         h->mb.cache.intra4x4_pred_mode[x264_scan8[8 ] - 1] =
1115         h->mb.cache.intra4x4_pred_mode[x264_scan8[10] - 1] = -1;
1116
1117         /* load non_zero_count */
1118         h->mb.cache.non_zero_count[x264_scan8[0 ] - 1] =
1119         h->mb.cache.non_zero_count[x264_scan8[2 ] - 1] =
1120         h->mb.cache.non_zero_count[x264_scan8[8 ] - 1] =
1121         h->mb.cache.non_zero_count[x264_scan8[10] - 1] =
1122         h->mb.cache.non_zero_count[x264_scan8[16+0] - 1] =
1123         h->mb.cache.non_zero_count[x264_scan8[16+2] - 1] =
1124         h->mb.cache.non_zero_count[x264_scan8[16+4+0] - 1] =
1125         h->mb.cache.non_zero_count[x264_scan8[16+4+2] - 1] = 0x80;
1126     }
1127
1128     if( i_mb_x < h->sps->i_mb_width - 1 && i_top_xy + 1 >= h->sh.i_first_mb )
1129     {
1130         h->mb.i_neighbour |= MB_TOPRIGHT;
1131         h->mb.i_mb_type_topright = h->mb.type[ i_top_xy + 1 ];
1132     }
1133     else
1134         h->mb.i_mb_type_topright = -1;
1135     if( i_mb_x > 0 && i_top_xy - 1 >= h->sh.i_first_mb )
1136     {
1137         h->mb.i_neighbour |= MB_TOPLEFT;
1138         h->mb.i_mb_type_topleft = h->mb.type[ i_top_xy - 1 ];
1139     }
1140     else
1141         h->mb.i_mb_type_topleft = -1;
1142
1143     if( h->pps->b_transform_8x8_mode )
1144     {
1145         h->mb.cache.i_neighbour_transform_size =
1146             ( i_left_type >= 0 && h->mb.mb_transform_size[i_left_xy] )
1147           + ( i_top_type  >= 0 && h->mb.mb_transform_size[i_top_xy]  );
1148     }
1149
1150     if( h->sh.b_mbaff )
1151     {
1152         h->mb.pic.i_fref[0] = h->i_ref0 << h->mb.b_interlaced;
1153         h->mb.pic.i_fref[1] = h->i_ref1 << h->mb.b_interlaced;
1154         h->mb.cache.i_neighbour_interlaced =
1155             !!(h->mb.i_neighbour & MB_LEFT)
1156           + !!(h->mb.i_neighbour & MB_TOP);
1157     }
1158
1159     /* fdec:      fenc:
1160      * yyyyyyy
1161      * yYYYY      YYYY
1162      * yYYYY      YYYY
1163      * yYYYY      YYYY
1164      * yYYYY      YYYY
1165      * uuu vvv    UUVV
1166      * uUU vVV    UUVV
1167      * uUU vVV
1168      */
1169     h->mb.pic.p_fenc[0] = h->mb.pic.fenc_buf;
1170     h->mb.pic.p_fenc[1] = h->mb.pic.fenc_buf + 16*FENC_STRIDE;
1171     h->mb.pic.p_fenc[2] = h->mb.pic.fenc_buf + 16*FENC_STRIDE + 8;
1172     h->mb.pic.p_fdec[0] = h->mb.pic.fdec_buf + 2*FDEC_STRIDE;
1173     h->mb.pic.p_fdec[1] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE;
1174     h->mb.pic.p_fdec[2] = h->mb.pic.fdec_buf + 19*FDEC_STRIDE + 16;
1175
1176     /* load picture pointers */
1177     for( i = 0; i < 3; i++ )
1178     {
1179         const int w = (i == 0 ? 16 : 8);
1180         const int i_stride = h->fdec->i_stride[i];
1181         const int i_stride2 = i_stride << h->mb.b_interlaced;
1182         const int i_pix_offset = h->mb.b_interlaced
1183                                ? w * (i_mb_x + (i_mb_y&~1) * i_stride) + (i_mb_y&1) * i_stride
1184                                : w * (i_mb_x + i_mb_y * i_stride);
1185         int ref_pix_offset[2] = { i_pix_offset, i_pix_offset };
1186         const uint8_t *plane_fdec = &h->fdec->plane[i][i_pix_offset];
1187         const uint8_t *intra_fdec = &h->mb.intra_border_backup[i_mb_y & h->sh.b_mbaff][i][i_mb_x*16>>!!i];
1188         x264_frame_t **fref[2] = { h->fref0, h->fref1 };
1189         int j, k, l;
1190
1191         if( h->mb.b_interlaced )
1192             ref_pix_offset[1] += (1-2*(i_mb_y&1)) * i_stride;
1193
1194         h->mb.pic.i_stride[i] = i_stride2;
1195
1196         h->mc.copy[i?PIXEL_8x8:PIXEL_16x16]( h->mb.pic.p_fenc[i], FENC_STRIDE,
1197             &h->fenc->plane[i][i_pix_offset], i_stride2, w );
1198         memcpy( &h->mb.pic.p_fdec[i][-1-FDEC_STRIDE], intra_fdec-1, w*3/2+1 );
1199         for( j = 0; j < w; j++ )
1200             h->mb.pic.p_fdec[i][-1+j*FDEC_STRIDE] = plane_fdec[-1+j*i_stride2];
1201
1202         for( l=0; l<2; l++ )
1203         {
1204             for( j=0; j<h->mb.pic.i_fref[l]; j++ )
1205             {
1206                 h->mb.pic.p_fref[l][j][i==0 ? 0:i+3] = &fref[l][j >> h->mb.b_interlaced]->plane[i][ref_pix_offset[j&1]];
1207                 if( i == 0 )
1208                     for( k = 1; k < 4; k++ )
1209                         h->mb.pic.p_fref[l][j][k] = &fref[l][j >> h->mb.b_interlaced]->filtered[k][ref_pix_offset[j&1]];
1210             }
1211         }
1212     }
1213
1214     if( h->fdec->integral )
1215     {
1216         assert( !h->mb.b_interlaced );
1217         for( i = 0; i < h->mb.pic.i_fref[0]; i++ )
1218             h->mb.pic.p_integral[0][i] = &h->fref0[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1219         for( i = 0; i < h->mb.pic.i_fref[1]; i++ )
1220             h->mb.pic.p_integral[1][i] = &h->fref1[i]->integral[ 16 * ( i_mb_x + i_mb_y * h->fdec->i_stride[0] )];
1221     }
1222
1223     x264_prefetch_fenc( h, h->fenc, i_mb_x, i_mb_y );
1224
1225     /* load ref/mv/mvd */
1226     if( h->sh.i_type != SLICE_TYPE_I )
1227     {
1228         const int s8x8 = h->mb.i_b8_stride;
1229         const int s4x4 = h->mb.i_b4_stride;
1230
1231         int i_list;
1232
1233         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1234         {
1235             /*
1236             h->mb.cache.ref[i_list][x264_scan8[5 ]+1] =
1237             h->mb.cache.ref[i_list][x264_scan8[7 ]+1] =
1238             h->mb.cache.ref[i_list][x264_scan8[13]+1] = -2;
1239             */
1240
1241             if( h->mb.i_neighbour & MB_TOPLEFT )
1242             {
1243                 const int i8 = x264_scan8[0] - 1 - 1*8;
1244                 const int ir = i_top_8x8 - 1;
1245                 const int iv = i_top_4x4 - 1;
1246                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1247                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1248                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1249             }
1250             else
1251             {
1252                 const int i8 = x264_scan8[0] - 1 - 1*8;
1253                 h->mb.cache.ref[i_list][i8] = -2;
1254                 h->mb.cache.mv[i_list][i8][0] = 0;
1255                 h->mb.cache.mv[i_list][i8][1] = 0;
1256             }
1257
1258             if( h->mb.i_neighbour & MB_TOP )
1259             {
1260                 const int i8 = x264_scan8[0] - 8;
1261                 const int ir = i_top_8x8;
1262                 const int iv = i_top_4x4;
1263                 h->mb.cache.ref[i_list][i8+0] =
1264                 h->mb.cache.ref[i_list][i8+1] = h->mb.ref[i_list][ir + 0];
1265                 h->mb.cache.ref[i_list][i8+2] =
1266                 h->mb.cache.ref[i_list][i8+3] = h->mb.ref[i_list][ir + 1];
1267
1268                 for( i = 0; i < 4; i++ )
1269                 {
1270                     h->mb.cache.mv[i_list][i8+i][0] = h->mb.mv[i_list][iv + i][0];
1271                     h->mb.cache.mv[i_list][i8+i][1] = h->mb.mv[i_list][iv + i][1];
1272                 }
1273             }
1274             else
1275             {
1276                 const int i8 = x264_scan8[0] - 8;
1277                 for( i = 0; i < 4; i++ )
1278                 {
1279                     h->mb.cache.ref[i_list][i8+i] = -2;
1280                     h->mb.cache.mv[i_list][i8+i][0] =
1281                     h->mb.cache.mv[i_list][i8+i][1] = 0;
1282                 }
1283             }
1284
1285             if( h->mb.i_neighbour & MB_TOPRIGHT )
1286             {
1287                 const int i8 = x264_scan8[0] + 4 - 1*8;
1288                 const int ir = i_top_8x8 + 2;
1289                 const int iv = i_top_4x4 + 4;
1290                 h->mb.cache.ref[i_list][i8]  = h->mb.ref[i_list][ir];
1291                 h->mb.cache.mv[i_list][i8][0] = h->mb.mv[i_list][iv][0];
1292                 h->mb.cache.mv[i_list][i8][1] = h->mb.mv[i_list][iv][1];
1293             }
1294             else
1295             {
1296                 const int i8 = x264_scan8[0] + 4 - 1*8;
1297                 h->mb.cache.ref[i_list][i8] = -2;
1298                 h->mb.cache.mv[i_list][i8][0] = 0;
1299                 h->mb.cache.mv[i_list][i8][1] = 0;
1300             }
1301
1302             if( h->mb.i_neighbour & MB_LEFT )
1303             {
1304                 const int i8 = x264_scan8[0] - 1;
1305                 const int ir = i_mb_8x8 - 1;
1306                 const int iv = i_mb_4x4 - 1;
1307                 h->mb.cache.ref[i_list][i8+0*8] =
1308                 h->mb.cache.ref[i_list][i8+1*8] = h->mb.ref[i_list][ir + 0*s8x8];
1309                 h->mb.cache.ref[i_list][i8+2*8] =
1310                 h->mb.cache.ref[i_list][i8+3*8] = h->mb.ref[i_list][ir + 1*s8x8];
1311
1312                 for( i = 0; i < 4; i++ )
1313                 {
1314                     h->mb.cache.mv[i_list][i8+i*8][0] = h->mb.mv[i_list][iv + i*s4x4][0];
1315                     h->mb.cache.mv[i_list][i8+i*8][1] = h->mb.mv[i_list][iv + i*s4x4][1];
1316                 }
1317             }
1318             else
1319             {
1320                 const int i8 = x264_scan8[0] - 1;
1321                 for( i = 0; i < 4; i++ )
1322                 {
1323                     h->mb.cache.ref[i_list][i8+i*8] = -2;
1324                     h->mb.cache.mv[i_list][i8+i*8][0] =
1325                     h->mb.cache.mv[i_list][i8+i*8][1] = 0;
1326                 }
1327             }
1328
1329             if( h->param.b_cabac )
1330             {
1331                 if( i_top_type >= 0 )
1332                 {
1333                     const int i8 = x264_scan8[0] - 8;
1334                     const int iv = i_top_4x4;
1335                     for( i = 0; i < 4; i++ )
1336                     {
1337                         h->mb.cache.mvd[i_list][i8+i][0] = h->mb.mvd[i_list][iv + i][0];
1338                         h->mb.cache.mvd[i_list][i8+i][1] = h->mb.mvd[i_list][iv + i][1];
1339                     }
1340                 }
1341                 else
1342                 {
1343                     const int i8 = x264_scan8[0] - 8;
1344                     for( i = 0; i < 4; i++ )
1345                     {
1346                         h->mb.cache.mvd[i_list][i8+i][0] =
1347                         h->mb.cache.mvd[i_list][i8+i][1] = 0;
1348                     }
1349                 }
1350
1351                 if( i_left_type >= 0 )
1352                 {
1353                     const int i8 = x264_scan8[0] - 1;
1354                     const int iv = i_mb_4x4 - 1;
1355                     for( i = 0; i < 4; i++ )
1356                     {
1357                         h->mb.cache.mvd[i_list][i8+i*8][0] = h->mb.mvd[i_list][iv + i*s4x4][0];
1358                         h->mb.cache.mvd[i_list][i8+i*8][1] = h->mb.mvd[i_list][iv + i*s4x4][1];
1359                     }
1360                 }
1361                 else
1362                 {
1363                     const int i8 = x264_scan8[0] - 1;
1364                     for( i = 0; i < 4; i++ )
1365                     {
1366                         h->mb.cache.mvd[i_list][i8+i*8][0] =
1367                         h->mb.cache.mvd[i_list][i8+i*8][1] = 0;
1368                     }
1369                 }
1370             }
1371         }
1372
1373         /* load skip */
1374         if( h->sh.i_type == SLICE_TYPE_B && h->param.b_cabac )
1375         {
1376             memset( h->mb.cache.skip, 0, X264_SCAN8_SIZE * sizeof( int8_t ) );
1377             if( i_left_type >= 0 )
1378             {
1379                 h->mb.cache.skip[x264_scan8[0] - 1] = h->mb.skipbp[i_left_xy] & 0x2;
1380                 h->mb.cache.skip[x264_scan8[8] - 1] = h->mb.skipbp[i_left_xy] & 0x8;
1381             }
1382             if( i_top_type >= 0 )
1383             {
1384                 h->mb.cache.skip[x264_scan8[0] - 8] = h->mb.skipbp[i_top_xy] & 0x4;
1385                 h->mb.cache.skip[x264_scan8[4] - 8] = h->mb.skipbp[i_top_xy] & 0x8;
1386             }
1387         }
1388
1389         if( h->sh.i_type == SLICE_TYPE_P )
1390             x264_mb_predict_mv_pskip( h, h->mb.cache.pskip_mv );
1391     }
1392
1393     h->mb.i_neighbour4[0] =
1394     h->mb.i_neighbour8[0] = (h->mb.i_neighbour & (MB_TOP|MB_LEFT|MB_TOPLEFT))
1395                             | ((h->mb.i_neighbour & MB_TOP) ? MB_TOPRIGHT : 0);
1396     h->mb.i_neighbour4[4] =
1397     h->mb.i_neighbour4[1] = MB_LEFT | ((h->mb.i_neighbour & MB_TOP) ? (MB_TOP|MB_TOPLEFT|MB_TOPRIGHT) : 0);
1398     h->mb.i_neighbour4[2] =
1399     h->mb.i_neighbour4[8] =
1400     h->mb.i_neighbour4[10] =
1401     h->mb.i_neighbour8[2] = MB_TOP|MB_TOPRIGHT | ((h->mb.i_neighbour & MB_LEFT) ? (MB_LEFT|MB_TOPLEFT) : 0);
1402     h->mb.i_neighbour4[3] =
1403     h->mb.i_neighbour4[7] =
1404     h->mb.i_neighbour4[11] =
1405     h->mb.i_neighbour4[13] =
1406     h->mb.i_neighbour4[15] =
1407     h->mb.i_neighbour8[3] = MB_LEFT|MB_TOP|MB_TOPLEFT;
1408     h->mb.i_neighbour4[5] =
1409     h->mb.i_neighbour8[1] = MB_LEFT | (h->mb.i_neighbour & MB_TOPRIGHT)
1410                             | ((h->mb.i_neighbour & MB_TOP) ? MB_TOP|MB_TOPLEFT : 0);
1411     h->mb.i_neighbour4[6] =
1412     h->mb.i_neighbour4[9] =
1413     h->mb.i_neighbour4[12] =
1414     h->mb.i_neighbour4[14] = MB_LEFT|MB_TOP|MB_TOPLEFT|MB_TOPRIGHT;
1415 }
1416
1417 void x264_macroblock_cache_save( x264_t *h )
1418 {
1419     const int i_mb_xy = h->mb.i_mb_xy;
1420     const int i_mb_type = x264_mb_type_fix[h->mb.i_type];
1421     const int s8x8 = h->mb.i_b8_stride;
1422     const int s4x4 = h->mb.i_b4_stride;
1423     const int i_mb_4x4 = h->mb.i_b4_xy;
1424     const int i_mb_8x8 = h->mb.i_b8_xy;
1425
1426     int i;
1427
1428     for( i = 0; i < 3; i++ )
1429     {
1430         int w = i ? 8 : 16;
1431         int i_stride = h->fdec->i_stride[i];
1432         int i_stride2 = i_stride << h->mb.b_interlaced;
1433         int i_pix_offset = h->mb.b_interlaced
1434                          ? w * (h->mb.i_mb_x + (h->mb.i_mb_y&~1) * i_stride) + (h->mb.i_mb_y&1) * i_stride
1435                          : w * (h->mb.i_mb_x + h->mb.i_mb_y * i_stride);
1436         h->mc.copy[i?PIXEL_8x8:PIXEL_16x16](
1437             &h->fdec->plane[i][i_pix_offset], i_stride2,
1438             h->mb.pic.p_fdec[i], FDEC_STRIDE, w );
1439     }
1440
1441     x264_prefetch_fenc( h, h->fdec, h->mb.i_mb_x, h->mb.i_mb_y );
1442
1443     h->mb.type[i_mb_xy] = i_mb_type;
1444
1445     if( h->mb.i_type != I_16x16 && h->mb.i_cbp_luma == 0 && h->mb.i_cbp_chroma == 0 )
1446         h->mb.i_qp = h->mb.i_last_qp;
1447     h->mb.qp[i_mb_xy] = h->mb.i_qp;
1448
1449     h->mb.i_last_dqp = h->mb.i_qp - h->mb.i_last_qp;
1450     h->mb.i_last_qp = h->mb.i_qp;
1451     h->mb.i_mb_prev_xy = h->mb.i_mb_xy;
1452
1453     /* save intra4x4 */
1454     if( i_mb_type == I_4x4 )
1455     {
1456         h->mb.intra4x4_pred_mode[i_mb_xy][0] = h->mb.cache.intra4x4_pred_mode[x264_scan8[10] ];
1457         h->mb.intra4x4_pred_mode[i_mb_xy][1] = h->mb.cache.intra4x4_pred_mode[x264_scan8[11] ];
1458         h->mb.intra4x4_pred_mode[i_mb_xy][2] = h->mb.cache.intra4x4_pred_mode[x264_scan8[14] ];
1459         h->mb.intra4x4_pred_mode[i_mb_xy][3] = h->mb.cache.intra4x4_pred_mode[x264_scan8[15] ];
1460         h->mb.intra4x4_pred_mode[i_mb_xy][4] = h->mb.cache.intra4x4_pred_mode[x264_scan8[5] ];
1461         h->mb.intra4x4_pred_mode[i_mb_xy][5] = h->mb.cache.intra4x4_pred_mode[x264_scan8[7] ];
1462         h->mb.intra4x4_pred_mode[i_mb_xy][6] = h->mb.cache.intra4x4_pred_mode[x264_scan8[13] ];
1463     }
1464     else
1465     {
1466         h->mb.intra4x4_pred_mode[i_mb_xy][0] =
1467         h->mb.intra4x4_pred_mode[i_mb_xy][1] =
1468         h->mb.intra4x4_pred_mode[i_mb_xy][2] =
1469         h->mb.intra4x4_pred_mode[i_mb_xy][3] =
1470         h->mb.intra4x4_pred_mode[i_mb_xy][4] =
1471         h->mb.intra4x4_pred_mode[i_mb_xy][5] =
1472         h->mb.intra4x4_pred_mode[i_mb_xy][6] = I_PRED_4x4_DC;
1473     }
1474
1475     if( i_mb_type == I_PCM )
1476     {
1477         h->mb.cbp[i_mb_xy] = 0x72f;   /* all set */
1478         for( i = 0; i < 16 + 2*4; i++ )
1479         {
1480             h->mb.non_zero_count[i_mb_xy][i] = 16;
1481         }
1482     }
1483     else
1484     {
1485         /* save non zero count */
1486         for( i = 0; i < 16 + 2*4; i++ )
1487         {
1488             h->mb.non_zero_count[i_mb_xy][i] = h->mb.cache.non_zero_count[x264_scan8[i]];
1489         }
1490     }
1491
1492     if( h->mb.i_cbp_luma == 0 && h->mb.i_type != I_8x8 )
1493         h->mb.b_transform_8x8 = 0;
1494     h->mb.mb_transform_size[i_mb_xy] = h->mb.b_transform_8x8;
1495
1496     if( !IS_INTRA( i_mb_type ) )
1497     {
1498         int i_list;
1499         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1500         {
1501             int y,x;
1502
1503             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[0]];
1504             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] = h->mb.cache.ref[i_list][x264_scan8[4]];
1505             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[8]];
1506             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = h->mb.cache.ref[i_list][x264_scan8[12]];
1507
1508             for( y = 0; y < 4; y++ )
1509             {
1510                 for( x = 0; x < 4; x++ )
1511                 {
1512                     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];
1513                     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];
1514                 }
1515             }
1516         }
1517     }
1518     else
1519     {
1520         int i_list;
1521         for( i_list = 0; i_list < (h->sh.i_type == SLICE_TYPE_B ? 2  : 1 ); i_list++ )
1522         {
1523             int y,x;
1524
1525             h->mb.ref[i_list][i_mb_8x8+0+0*s8x8] =
1526             h->mb.ref[i_list][i_mb_8x8+1+0*s8x8] =
1527             h->mb.ref[i_list][i_mb_8x8+0+1*s8x8] =
1528             h->mb.ref[i_list][i_mb_8x8+1+1*s8x8] = -1;
1529
1530             for( y = 0; y < 4; y++ )
1531             {
1532                 for( x = 0; x < 4; x++ )
1533                 {
1534                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1535                     h->mb.mv[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1536                 }
1537             }
1538         }
1539     }
1540
1541     if( h->param.b_cabac )
1542     {
1543         if( i_mb_type == I_4x4 || i_mb_type == I_16x16 )
1544             h->mb.chroma_pred_mode[i_mb_xy] = x264_mb_pred_mode8x8c_fix[ h->mb.i_chroma_pred_mode ];
1545         else
1546             h->mb.chroma_pred_mode[i_mb_xy] = I_PRED_CHROMA_DC;
1547
1548         if( !IS_INTRA( i_mb_type ) && !IS_SKIP( i_mb_type ) && !IS_DIRECT( i_mb_type ) )
1549         {
1550             int i_list;
1551             for( i_list  = 0; i_list < 2; i_list++ )
1552             {
1553                 const int s4x4 = 4 * h->mb.i_mb_stride;
1554                 int y,x;
1555                 for( y = 0; y < 4; y++ )
1556                 {
1557                     for( x = 0; x < 4; x++ )
1558                     {
1559                         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];
1560                         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];
1561                     }
1562                 }
1563             }
1564         }
1565         else
1566         {
1567             int i_list;
1568             for( i_list  = 0; i_list < 2; i_list++ )
1569             {
1570                 const int s4x4 = 4 * h->mb.i_mb_stride;
1571                 int y,x;
1572                 for( y = 0; y < 4; y++ )
1573                 {
1574                     for( x = 0; x < 4; x++ )
1575                     {
1576                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][0] = 0;
1577                         h->mb.mvd[i_list][i_mb_4x4+x+y*s4x4][1] = 0;
1578                     }
1579                 }
1580             }
1581         }
1582         if( h->sh.i_type == SLICE_TYPE_B )
1583         {
1584             if( i_mb_type == B_SKIP || i_mb_type == B_DIRECT )
1585                 h->mb.skipbp[i_mb_xy] = 0xf;
1586             else if( i_mb_type == B_8x8 )
1587             {
1588                 int skipbp = 0;
1589                 for( i = 0; i < 4; i++ )
1590                     skipbp |= ( h->mb.i_sub_partition[i] == D_DIRECT_8x8 ) << i;
1591                 h->mb.skipbp[i_mb_xy] = skipbp;
1592             }
1593             else
1594                 h->mb.skipbp[i_mb_xy] = 0;
1595         }
1596     }
1597 }
1598
1599 void x264_macroblock_bipred_init( x264_t *h )
1600 {
1601     int i_ref0, i_ref1;
1602     for( i_ref0 = 0; i_ref0 < h->i_ref0; i_ref0++ )
1603     {
1604         int poc0 = h->fref0[i_ref0]->i_poc;
1605         for( i_ref1 = 0; i_ref1 < h->i_ref1; i_ref1++ )
1606         {
1607             int dist_scale_factor;
1608             int poc1 = h->fref1[i_ref1]->i_poc;
1609             int td = x264_clip3( poc1 - poc0, -128, 127 );
1610             if( td == 0 /* || pic0 is a long-term ref */ )
1611                 dist_scale_factor = 256;
1612             else
1613             {
1614                 int tb = x264_clip3( h->fdec->i_poc - poc0, -128, 127 );
1615                 int tx = (16384 + (abs(td) >> 1)) / td;
1616                 dist_scale_factor = x264_clip3( (tb * tx + 32) >> 6, -1024, 1023 );
1617             }
1618             h->mb.dist_scale_factor[i_ref0][i_ref1] = dist_scale_factor;
1619
1620             dist_scale_factor >>= 2;
1621             if( h->param.analyse.b_weighted_bipred
1622                   && dist_scale_factor >= -64
1623                   && dist_scale_factor <= 128 )
1624                 h->mb.bipred_weight[i_ref0][i_ref1] = 64 - dist_scale_factor;
1625             else
1626                 h->mb.bipred_weight[i_ref0][i_ref1] = 32;
1627         }
1628     }
1629     if( h->sh.b_mbaff )
1630     {
1631         for( i_ref0 = 2*h->i_ref0-1; i_ref0 >= 0; i_ref0-- )
1632             for( i_ref1 = 2*h->i_ref1-1; i_ref1 >= 0; i_ref1-- )
1633                 h->mb.bipred_weight[i_ref0][i_ref1] = h->mb.bipred_weight[i_ref0>>1][i_ref1>>1];
1634     }
1635 }