]> git.sesse.net Git - x264/blob - common/mvpred.c
Add speedcontrol file.
[x264] / common / mvpred.c
1 /*****************************************************************************
2  * mvpred.c: motion vector prediction
3  *****************************************************************************
4  * Copyright (C) 2003-2016 x264 project
5  *
6  * Authors: Loren Merritt <lorenm@u.washington.edu>
7  *          Fiona Glaser <fiona@x264.com>
8  *          Laurent Aimar <fenrir@via.ecp.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
23  *
24  * This program is also available under a commercial proprietary license.
25  * For more information, contact us at licensing@x264.com.
26  *****************************************************************************/
27
28 #include "common.h"
29
30 void x264_mb_predict_mv( x264_t *h, int i_list, int idx, int i_width, int16_t mvp[2] )
31 {
32     const int i8 = x264_scan8[idx];
33     const int i_ref= h->mb.cache.ref[i_list][i8];
34     int     i_refa = h->mb.cache.ref[i_list][i8 - 1];
35     int16_t *mv_a  = h->mb.cache.mv[i_list][i8 - 1];
36     int     i_refb = h->mb.cache.ref[i_list][i8 - 8];
37     int16_t *mv_b  = h->mb.cache.mv[i_list][i8 - 8];
38     int     i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width];
39     int16_t *mv_c  = h->mb.cache.mv[i_list][i8 - 8 + i_width];
40
41     // Partitions not yet reached in scan order are unavailable.
42     if( (idx&3) >= 2 + (i_width&1) || i_refc == -2 )
43     {
44         i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1];
45         mv_c   = h->mb.cache.mv[i_list][i8 - 8 - 1];
46
47         if( SLICE_MBAFF
48             && h->mb.cache.ref[i_list][x264_scan8[0]-1] != -2
49             && MB_INTERLACED != h->mb.field[h->mb.i_mb_left_xy[0]] )
50         {
51             if( idx == 2 )
52             {
53                 mv_c = h->mb.cache.topright_mv[i_list][0];
54                 i_refc = h->mb.cache.topright_ref[i_list][0];
55             }
56             else if( idx == 8 )
57             {
58                 mv_c = h->mb.cache.topright_mv[i_list][1];
59                 i_refc = h->mb.cache.topright_ref[i_list][1];
60             }
61             else if( idx == 10 )
62             {
63                 mv_c = h->mb.cache.topright_mv[i_list][2];
64                 i_refc = h->mb.cache.topright_ref[i_list][2];
65             }
66         }
67     }
68     if( h->mb.i_partition == D_16x8 )
69     {
70         if( idx == 0 )
71         {
72             if( i_refb == i_ref )
73             {
74                 CP32( mvp, mv_b );
75                 return;
76             }
77         }
78         else
79         {
80             if( i_refa == i_ref )
81             {
82                 CP32( mvp, mv_a );
83                 return;
84             }
85         }
86     }
87     else if( h->mb.i_partition == D_8x16 )
88     {
89         if( idx == 0 )
90         {
91             if( i_refa == i_ref )
92             {
93                 CP32( mvp, mv_a );
94                 return;
95             }
96         }
97         else
98         {
99             if( i_refc == i_ref )
100             {
101                 CP32( mvp, mv_c );
102                 return;
103             }
104         }
105     }
106
107     int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
108
109     if( i_count > 1 )
110     {
111 median:
112         x264_median_mv( mvp, mv_a, mv_b, mv_c );
113     }
114     else if( i_count == 1 )
115     {
116         if( i_refa == i_ref )
117             CP32( mvp, mv_a );
118         else if( i_refb == i_ref )
119             CP32( mvp, mv_b );
120         else
121             CP32( mvp, mv_c );
122     }
123     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
124         CP32( mvp, mv_a );
125     else
126         goto median;
127 }
128
129 void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] )
130 {
131     int     i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
132     int16_t *mv_a  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
133     int     i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
134     int16_t *mv_b  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
135     int     i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
136     int16_t *mv_c  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
137     if( i_refc == -2 )
138     {
139         i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
140         mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
141     }
142
143     int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
144
145     if( i_count > 1 )
146     {
147 median:
148         x264_median_mv( mvp, mv_a, mv_b, mv_c );
149     }
150     else if( i_count == 1 )
151     {
152         if( i_refa == i_ref )
153             CP32( mvp, mv_a );
154         else if( i_refb == i_ref )
155             CP32( mvp, mv_b );
156         else
157             CP32( mvp, mv_c );
158     }
159     else if( i_refb == -2 && i_refc == -2 && i_refa != -2 )
160         CP32( mvp, mv_a );
161     else
162         goto median;
163 }
164
165
166 void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] )
167 {
168     int     i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1];
169     int     i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8];
170     int16_t *mv_a  = h->mb.cache.mv[0][X264_SCAN8_0 - 1];
171     int16_t *mv_b  = h->mb.cache.mv[0][X264_SCAN8_0 - 8];
172
173     if( i_refa == -2 || i_refb == -2 ||
174         !( i_refa | M32( mv_a ) ) ||
175         !( i_refb | M32( mv_b ) ) )
176     {
177         M32( mv ) = 0;
178     }
179     else
180         x264_mb_predict_mv_16x16( h, 0, 0, mv );
181 }
182
183 static int x264_mb_predict_mv_direct16x16_temporal( x264_t *h )
184 {
185     int mb_x = h->mb.i_mb_x;
186     int mb_y = h->mb.i_mb_y;
187     int mb_xy = h->mb.i_mb_xy;
188     int type_col[2] = { h->fref[1][0]->mb_type[mb_xy], h->fref[1][0]->mb_type[mb_xy] };
189     int partition_col[2] = { h->fref[1][0]->mb_partition[mb_xy], h->fref[1][0]->mb_partition[mb_xy] };
190     int preshift = MB_INTERLACED;
191     int postshift = MB_INTERLACED;
192     int offset = 1;
193     int yshift = 1;
194     h->mb.i_partition = partition_col[0];
195     if( PARAM_INTERLACED && h->fref[1][0]->field[mb_xy] != MB_INTERLACED )
196     {
197         if( MB_INTERLACED )
198         {
199             mb_y = h->mb.i_mb_y&~1;
200             mb_xy = mb_x + h->mb.i_mb_stride * mb_y;
201             type_col[0] = h->fref[1][0]->mb_type[mb_xy];
202             type_col[1] = h->fref[1][0]->mb_type[mb_xy + h->mb.i_mb_stride];
203             partition_col[0] = h->fref[1][0]->mb_partition[mb_xy];
204             partition_col[1] = h->fref[1][0]->mb_partition[mb_xy + h->mb.i_mb_stride];
205             preshift = 0;
206             yshift = 0;
207
208             if( (IS_INTRA(type_col[0]) || partition_col[0] == D_16x16) &&
209                 (IS_INTRA(type_col[1]) || partition_col[1] == D_16x16) &&
210                 partition_col[0] != D_8x8 )
211                 h->mb.i_partition = D_16x8;
212             else
213                 h->mb.i_partition = D_8x8;
214         }
215         else
216         {
217             int cur_poc = h->fdec->i_poc + h->fdec->i_delta_poc[MB_INTERLACED&h->mb.i_mb_y&1];
218             int col_parity = abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[0] - cur_poc)
219                           >= abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[1] - cur_poc);
220             mb_y = (h->mb.i_mb_y&~1) + col_parity;
221             mb_xy = mb_x + h->mb.i_mb_stride * mb_y;
222             type_col[0] = type_col[1] = h->fref[1][0]->mb_type[mb_xy];
223             partition_col[0] = partition_col[1] = h->fref[1][0]->mb_partition[mb_xy];
224             preshift = 1;
225             yshift = 2;
226             h->mb.i_partition = partition_col[0];
227         }
228         offset = 0;
229     }
230     int i_mb_4x4 = 16 * h->mb.i_mb_stride * mb_y + 4 * mb_x;
231     int i_mb_8x8 =  4 * h->mb.i_mb_stride * mb_y + 2 * mb_x;
232
233     x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
234
235     /* Don't do any checks other than the ones we have to, based
236      * on the size of the colocated partitions.
237      * Depends on the enum order: D_8x8, D_16x8, D_8x16, D_16x16 */
238     int max_i8 = (D_16x16 - h->mb.i_partition) + 1;
239     int step = (h->mb.i_partition == D_16x8) + 1;
240     int width = 4 >> ((D_16x16 - h->mb.i_partition)&1);
241     int height = 4 >> ((D_16x16 - h->mb.i_partition)>>1);
242     for( int i8 = 0; i8 < max_i8; i8 += step )
243     {
244         int x8 = i8&1;
245         int y8 = i8>>1;
246         int ypart = (SLICE_MBAFF && h->fref[1][0]->field[mb_xy] != MB_INTERLACED) ?
247                     MB_INTERLACED ? y8*6 : 2*(h->mb.i_mb_y&1) + y8 :
248                     3*y8;
249
250         if( IS_INTRA( type_col[y8] ) )
251         {
252             x264_macroblock_cache_ref( h, 2*x8, 2*y8, width, height, 0, 0 );
253             x264_macroblock_cache_mv(  h, 2*x8, 2*y8, width, height, 0, 0 );
254             x264_macroblock_cache_mv(  h, 2*x8, 2*y8, width, height, 1, 0 );
255             continue;
256         }
257
258         int i_part_8x8 = i_mb_8x8 + x8 + (ypart>>1) * h->mb.i_b8_stride;
259         int i_ref1_ref = h->fref[1][0]->ref[0][i_part_8x8];
260         int i_ref = (map_col_to_list0(i_ref1_ref>>preshift) << postshift) + (offset&i_ref1_ref&MB_INTERLACED);
261
262         if( i_ref >= 0 )
263         {
264             int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0];
265             int16_t *mv_col = h->fref[1][0]->mv[0][i_mb_4x4 + 3*x8 + ypart * h->mb.i_b4_stride];
266             int16_t mv_y = (mv_col[1]<<yshift)/2;
267             int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8;
268             int l0y = ( dist_scale_factor * mv_y + 128 ) >> 8;
269             if( h->param.i_threads > 1 && (l0y > h->mb.mv_max_spel[1] || l0y-mv_y > h->mb.mv_max_spel[1]) )
270                 return 0;
271             x264_macroblock_cache_ref( h, 2*x8, 2*y8, width, height, 0, i_ref );
272             x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, pack16to32_mask(l0x, l0y) );
273             x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_y) );
274         }
275         else
276         {
277             /* the collocated ref isn't in the current list0 */
278             /* FIXME: we might still be able to use direct_8x8 on some partitions */
279             /* FIXME: with B-pyramid + extensive ref list reordering
280              *   (not currently used), we would also have to check
281              *   l1mv1 like in spatial mode */
282             return 0;
283         }
284     }
285
286     return 1;
287 }
288
289 static ALWAYS_INLINE int x264_mb_predict_mv_direct16x16_spatial( x264_t *h, int b_interlaced )
290 {
291     int8_t ref[2];
292     ALIGNED_ARRAY_8( int16_t, mv,[2],[2] );
293     for( int i_list = 0; i_list < 2; i_list++ )
294     {
295         int     i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1];
296         int16_t *mv_a  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1];
297         int     i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8];
298         int16_t *mv_b  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8];
299         int     i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4];
300         int16_t *mv_c  = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4];
301         if( i_refc == -2 )
302         {
303             i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1];
304             mv_c   = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1];
305         }
306
307         int i_ref = X264_MIN3( (unsigned)i_refa, (unsigned)i_refb, (unsigned)i_refc );
308         if( i_ref < 0 )
309         {
310             i_ref = -1;
311             M32( mv[i_list] ) = 0;
312         }
313         else
314         {
315             /* Same as x264_mb_predict_mv_16x16, but simplified to eliminate cases
316              * not relevant to spatial direct. */
317             int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref);
318
319             if( i_count > 1 )
320                 x264_median_mv( mv[i_list], mv_a, mv_b, mv_c );
321             else
322             {
323                 if( i_refa == i_ref )
324                     CP32( mv[i_list], mv_a );
325                 else if( i_refb == i_ref )
326                     CP32( mv[i_list], mv_b );
327                 else
328                     CP32( mv[i_list], mv_c );
329             }
330         }
331
332         x264_macroblock_cache_ref( h, 0, 0, 4, 4, i_list, i_ref );
333         x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, i_list, mv[i_list] );
334         ref[i_list] = i_ref;
335     }
336
337     int mb_x = h->mb.i_mb_x;
338     int mb_y = h->mb.i_mb_y;
339     int mb_xy = h->mb.i_mb_xy;
340     int type_col[2] = { h->fref[1][0]->mb_type[mb_xy], h->fref[1][0]->mb_type[mb_xy] };
341     int partition_col[2] = { h->fref[1][0]->mb_partition[mb_xy], h->fref[1][0]->mb_partition[mb_xy] };
342     h->mb.i_partition = partition_col[0];
343     if( b_interlaced && h->fref[1][0]->field[mb_xy] != MB_INTERLACED )
344     {
345         if( MB_INTERLACED )
346         {
347             mb_y = h->mb.i_mb_y&~1;
348             mb_xy = mb_x + h->mb.i_mb_stride * mb_y;
349             type_col[0] = h->fref[1][0]->mb_type[mb_xy];
350             type_col[1] = h->fref[1][0]->mb_type[mb_xy + h->mb.i_mb_stride];
351             partition_col[0] = h->fref[1][0]->mb_partition[mb_xy];
352             partition_col[1] = h->fref[1][0]->mb_partition[mb_xy + h->mb.i_mb_stride];
353
354             if( (IS_INTRA(type_col[0]) || partition_col[0] == D_16x16) &&
355                 (IS_INTRA(type_col[1]) || partition_col[1] == D_16x16) &&
356                 partition_col[0] != D_8x8 )
357                 h->mb.i_partition = D_16x8;
358             else
359                 h->mb.i_partition = D_8x8;
360         }
361         else
362         {
363             int cur_poc = h->fdec->i_poc + h->fdec->i_delta_poc[MB_INTERLACED&h->mb.i_mb_y&1];
364             int col_parity = abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[0] - cur_poc)
365                           >= abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[1] - cur_poc);
366             mb_y = (h->mb.i_mb_y&~1) + col_parity;
367             mb_xy = mb_x + h->mb.i_mb_stride * mb_y;
368             type_col[0] = type_col[1] = h->fref[1][0]->mb_type[mb_xy];
369             partition_col[0] = partition_col[1] = h->fref[1][0]->mb_partition[mb_xy];
370             h->mb.i_partition = partition_col[0];
371         }
372     }
373     int i_mb_4x4 = b_interlaced ? 4 * (h->mb.i_b4_stride*mb_y + mb_x) : h->mb.i_b4_xy ;
374     int i_mb_8x8 = b_interlaced ? 2 * (h->mb.i_b8_stride*mb_y + mb_x) : h->mb.i_b8_xy ;
375
376     int8_t *l1ref0 = &h->fref[1][0]->ref[0][i_mb_8x8];
377     int8_t *l1ref1 = &h->fref[1][0]->ref[1][i_mb_8x8];
378     int16_t (*l1mv[2])[2] = { (int16_t (*)[2]) &h->fref[1][0]->mv[0][i_mb_4x4],
379                               (int16_t (*)[2]) &h->fref[1][0]->mv[1][i_mb_4x4] };
380
381     if( (M16( ref ) & 0x8080) == 0x8080 ) /* if( ref[0] < 0 && ref[1] < 0 ) */
382     {
383         x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 );
384         x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 );
385         return 1;
386     }
387
388     if( h->param.i_threads > 1
389         && ( mv[0][1] > h->mb.mv_max_spel[1]
390           || mv[1][1] > h->mb.mv_max_spel[1] ) )
391     {
392 #if 0
393         fprintf(stderr, "direct_spatial: (%d,%d) (%d,%d) > %d \n",
394                 mv[0][0], mv[0][1], mv[1][0], mv[1][1],
395                 h->mb.mv_max_spel[1]);
396 #endif
397         return 0;
398     }
399
400     if( !M64( mv ) || (!b_interlaced && IS_INTRA( type_col[0] )) || (ref[0]&&ref[1]) )
401         return 1;
402
403     /* Don't do any checks other than the ones we have to, based
404      * on the size of the colocated partitions.
405      * Depends on the enum order: D_8x8, D_16x8, D_8x16, D_16x16 */
406     int max_i8 = (D_16x16 - h->mb.i_partition) + 1;
407     int step = (h->mb.i_partition == D_16x8) + 1;
408     int width = 4 >> ((D_16x16 - h->mb.i_partition)&1);
409     int height = 4 >> ((D_16x16 - h->mb.i_partition)>>1);
410
411     /* col_zero_flag */
412     for( int i8 = 0; i8 < max_i8; i8 += step )
413     {
414         const int x8 = i8&1;
415         const int y8 = i8>>1;
416         int ypart = (b_interlaced && h->fref[1][0]->field[mb_xy] != MB_INTERLACED) ?
417                     MB_INTERLACED ? y8*6 : 2*(h->mb.i_mb_y&1) + y8 :
418                     3*y8;
419         int o8 = x8 + (ypart>>1) * h->mb.i_b8_stride;
420         int o4 = 3*x8 + ypart * h->mb.i_b4_stride;
421
422         if( b_interlaced && IS_INTRA( type_col[y8] ) )
423             continue;
424
425         int idx;
426         if( l1ref0[o8] == 0 )
427             idx = 0;
428         else if( l1ref0[o8] < 0 && l1ref1[o8] == 0 )
429             idx = 1;
430         else
431             continue;
432
433         if( abs( l1mv[idx][o4][0] ) <= 1 && abs( l1mv[idx][o4][1] ) <= 1 )
434         {
435             if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, 0 );
436             if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, 0 );
437         }
438     }
439
440     return 1;
441 }
442
443
444 static int x264_mb_predict_mv_direct16x16_spatial_interlaced( x264_t *h )
445 {
446     return x264_mb_predict_mv_direct16x16_spatial( h, 1 );
447 }
448
449 static int x264_mb_predict_mv_direct16x16_spatial_progressive( x264_t *h )
450 {
451     return x264_mb_predict_mv_direct16x16_spatial( h, 0 );
452 }
453
454 int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed )
455 {
456     int b_available;
457     if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE )
458         return 0;
459     else if( h->sh.b_direct_spatial_mv_pred )
460     {
461         if( SLICE_MBAFF )
462             b_available = x264_mb_predict_mv_direct16x16_spatial_interlaced( h );
463         else
464             b_available = x264_mb_predict_mv_direct16x16_spatial_progressive( h );
465     }
466     else
467         b_available = x264_mb_predict_mv_direct16x16_temporal( h );
468
469     if( b_changed != NULL && b_available )
470     {
471         int changed;
472
473         changed  = M32( h->mb.cache.direct_mv[0][0] ) ^ M32( h->mb.cache.mv[0][x264_scan8[0]] );
474         changed |= M32( h->mb.cache.direct_mv[1][0] ) ^ M32( h->mb.cache.mv[1][x264_scan8[0]] );
475         changed |= h->mb.cache.direct_ref[0][0] ^ h->mb.cache.ref[0][x264_scan8[0]];
476         changed |= h->mb.cache.direct_ref[1][0] ^ h->mb.cache.ref[1][x264_scan8[0]];
477         if( !changed && h->mb.i_partition != D_16x16 )
478         {
479             changed |= M32( h->mb.cache.direct_mv[0][3] ) ^ M32( h->mb.cache.mv[0][x264_scan8[12]] );
480             changed |= M32( h->mb.cache.direct_mv[1][3] ) ^ M32( h->mb.cache.mv[1][x264_scan8[12]] );
481             changed |= h->mb.cache.direct_ref[0][3] ^ h->mb.cache.ref[0][x264_scan8[12]];
482             changed |= h->mb.cache.direct_ref[1][3] ^ h->mb.cache.ref[1][x264_scan8[12]];
483         }
484         if( !changed && h->mb.i_partition == D_8x8 )
485         {
486             changed |= M32( h->mb.cache.direct_mv[0][1] ) ^ M32( h->mb.cache.mv[0][x264_scan8[4]] );
487             changed |= M32( h->mb.cache.direct_mv[1][1] ) ^ M32( h->mb.cache.mv[1][x264_scan8[4]] );
488             changed |= M32( h->mb.cache.direct_mv[0][2] ) ^ M32( h->mb.cache.mv[0][x264_scan8[8]] );
489             changed |= M32( h->mb.cache.direct_mv[1][2] ) ^ M32( h->mb.cache.mv[1][x264_scan8[8]] );
490             changed |= h->mb.cache.direct_ref[0][1] ^ h->mb.cache.ref[0][x264_scan8[4]];
491             changed |= h->mb.cache.direct_ref[1][1] ^ h->mb.cache.ref[1][x264_scan8[4]];
492             changed |= h->mb.cache.direct_ref[0][2] ^ h->mb.cache.ref[0][x264_scan8[8]];
493             changed |= h->mb.cache.direct_ref[1][2] ^ h->mb.cache.ref[1][x264_scan8[8]];
494         }
495         *b_changed = changed;
496         if( !changed )
497             return b_available;
498     }
499
500     /* cache ref & mv */
501     if( b_available )
502         for( int l = 0; l < 2; l++ )
503         {
504             CP32( h->mb.cache.direct_mv[l][0], h->mb.cache.mv[l][x264_scan8[ 0]] );
505             CP32( h->mb.cache.direct_mv[l][1], h->mb.cache.mv[l][x264_scan8[ 4]] );
506             CP32( h->mb.cache.direct_mv[l][2], h->mb.cache.mv[l][x264_scan8[ 8]] );
507             CP32( h->mb.cache.direct_mv[l][3], h->mb.cache.mv[l][x264_scan8[12]] );
508             h->mb.cache.direct_ref[l][0] = h->mb.cache.ref[l][x264_scan8[ 0]];
509             h->mb.cache.direct_ref[l][1] = h->mb.cache.ref[l][x264_scan8[ 4]];
510             h->mb.cache.direct_ref[l][2] = h->mb.cache.ref[l][x264_scan8[ 8]];
511             h->mb.cache.direct_ref[l][3] = h->mb.cache.ref[l][x264_scan8[12]];
512             h->mb.cache.direct_partition = h->mb.i_partition;
513         }
514
515     return b_available;
516 }
517
518 /* This just improves encoder performance, it's not part of the spec */
519 void x264_mb_predict_mv_ref16x16( x264_t *h, int i_list, int i_ref, int16_t mvc[9][2], int *i_mvc )
520 {
521     int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref];
522     int i = 0;
523
524 #define SET_MVP(mvp) \
525     { \
526         CP32( mvc[i], mvp ); \
527         i++; \
528     }
529
530 #define SET_IMVP(xy) \
531     if( xy >= 0 ) \
532     { \
533         int shift = 1 + MB_INTERLACED - h->mb.field[xy]; \
534         int16_t *mvp = h->mb.mvr[i_list][i_ref<<1>>shift][xy]; \
535         mvc[i][0] = mvp[0]; \
536         mvc[i][1] = mvp[1]<<1>>shift; \
537         i++; \
538     }
539
540     /* b_direct */
541     if( h->sh.i_type == SLICE_TYPE_B
542         && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref )
543     {
544         SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] );
545     }
546
547     if( i_ref == 0 && h->frames.b_have_lowres )
548     {
549         int idx = i_list ? h->fref[1][0]->i_frame-h->fenc->i_frame-1
550                          : h->fenc->i_frame-h->fref[0][0]->i_frame-1;
551         if( idx <= h->param.i_bframe )
552         {
553             int16_t (*lowres_mv)[2] = h->fenc->lowres_mvs[i_list][idx];
554             if( lowres_mv[0][0] != 0x7fff )
555             {
556                 M32( mvc[i] ) = (M32( lowres_mv[h->mb.i_mb_xy] )*2)&0xfffeffff;
557                 i++;
558             }
559         }
560     }
561
562     /* spatial predictors */
563     if( SLICE_MBAFF )
564     {
565         SET_IMVP( h->mb.i_mb_left_xy[0] );
566         SET_IMVP( h->mb.i_mb_top_xy );
567         SET_IMVP( h->mb.i_mb_topleft_xy );
568         SET_IMVP( h->mb.i_mb_topright_xy );
569     }
570     else
571     {
572         SET_MVP( mvr[h->mb.i_mb_left_xy[0]] );
573         SET_MVP( mvr[h->mb.i_mb_top_xy] );
574         SET_MVP( mvr[h->mb.i_mb_topleft_xy] );
575         SET_MVP( mvr[h->mb.i_mb_topright_xy] );
576     }
577 #undef SET_IMVP
578 #undef SET_MVP
579
580     /* temporal predictors */
581     if( h->fref[0][0]->i_ref[0] > 0 )
582     {
583         x264_frame_t *l0 = h->fref[0][0];
584         int field = h->mb.i_mb_y&1;
585         int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field];
586         int refpoc = h->fref[i_list][i_ref>>SLICE_MBAFF]->i_poc;
587         refpoc += l0->i_delta_poc[field^(i_ref&1)];
588
589 #define SET_TMVP( dx, dy ) \
590         { \
591             int mb_index = h->mb.i_mb_xy + dx + dy*h->mb.i_mb_stride; \
592             int scale = (curpoc - refpoc) * l0->inv_ref_poc[MB_INTERLACED&field]; \
593             mvc[i][0] = (l0->mv16x16[mb_index][0]*scale + 128) >> 8; \
594             mvc[i][1] = (l0->mv16x16[mb_index][1]*scale + 128) >> 8; \
595             i++; \
596         }
597
598         SET_TMVP(0,0);
599         if( h->mb.i_mb_x < h->mb.i_mb_width-1 )
600             SET_TMVP(1,0);
601         if( h->mb.i_mb_y < h->mb.i_mb_height-1 )
602             SET_TMVP(0,1);
603 #undef SET_TMVP
604     }
605
606     *i_mvc = i;
607 }