/src/x264/common/mvpred.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * mvpred.c: motion vector prediction |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2003-2025 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 | 0 | { |
32 | 0 | const int i8 = x264_scan8[idx]; |
33 | 0 | const int i_ref= h->mb.cache.ref[i_list][i8]; |
34 | 0 | int i_refa = h->mb.cache.ref[i_list][i8 - 1]; |
35 | 0 | int16_t *mv_a = h->mb.cache.mv[i_list][i8 - 1]; |
36 | 0 | int i_refb = h->mb.cache.ref[i_list][i8 - 8]; |
37 | 0 | int16_t *mv_b = h->mb.cache.mv[i_list][i8 - 8]; |
38 | 0 | int i_refc = h->mb.cache.ref[i_list][i8 - 8 + i_width]; |
39 | 0 | 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 | 0 | if( (idx&3) >= 2 + (i_width&1) || i_refc == -2 ) |
43 | 0 | { |
44 | 0 | i_refc = h->mb.cache.ref[i_list][i8 - 8 - 1]; |
45 | 0 | mv_c = h->mb.cache.mv[i_list][i8 - 8 - 1]; |
46 | |
|
47 | 0 | if( SLICE_MBAFF |
48 | 0 | && h->mb.cache.ref[i_list][x264_scan8[0]-1] != -2 |
49 | 0 | && MB_INTERLACED != h->mb.field[h->mb.i_mb_left_xy[0]] ) |
50 | 0 | { |
51 | 0 | if( idx == 2 ) |
52 | 0 | { |
53 | 0 | mv_c = h->mb.cache.topright_mv[i_list][0]; |
54 | 0 | i_refc = h->mb.cache.topright_ref[i_list][0]; |
55 | 0 | } |
56 | 0 | else if( idx == 8 ) |
57 | 0 | { |
58 | 0 | mv_c = h->mb.cache.topright_mv[i_list][1]; |
59 | 0 | i_refc = h->mb.cache.topright_ref[i_list][1]; |
60 | 0 | } |
61 | 0 | else if( idx == 10 ) |
62 | 0 | { |
63 | 0 | mv_c = h->mb.cache.topright_mv[i_list][2]; |
64 | 0 | i_refc = h->mb.cache.topright_ref[i_list][2]; |
65 | 0 | } |
66 | 0 | } |
67 | 0 | } |
68 | 0 | if( h->mb.i_partition == D_16x8 ) |
69 | 0 | { |
70 | 0 | if( idx == 0 ) |
71 | 0 | { |
72 | 0 | if( i_refb == i_ref ) |
73 | 0 | { |
74 | 0 | CP32( mvp, mv_b ); |
75 | 0 | return; |
76 | 0 | } |
77 | 0 | } |
78 | 0 | else |
79 | 0 | { |
80 | 0 | if( i_refa == i_ref ) |
81 | 0 | { |
82 | 0 | CP32( mvp, mv_a ); |
83 | 0 | return; |
84 | 0 | } |
85 | 0 | } |
86 | 0 | } |
87 | 0 | else if( h->mb.i_partition == D_8x16 ) |
88 | 0 | { |
89 | 0 | if( idx == 0 ) |
90 | 0 | { |
91 | 0 | if( i_refa == i_ref ) |
92 | 0 | { |
93 | 0 | CP32( mvp, mv_a ); |
94 | 0 | return; |
95 | 0 | } |
96 | 0 | } |
97 | 0 | else |
98 | 0 | { |
99 | 0 | if( i_refc == i_ref ) |
100 | 0 | { |
101 | 0 | CP32( mvp, mv_c ); |
102 | 0 | return; |
103 | 0 | } |
104 | 0 | } |
105 | 0 | } |
106 | | |
107 | 0 | int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref); |
108 | |
|
109 | 0 | if( i_count > 1 ) |
110 | 0 | { |
111 | 0 | median: |
112 | 0 | x264_median_mv( mvp, mv_a, mv_b, mv_c ); |
113 | 0 | } |
114 | 0 | else if( i_count == 1 ) |
115 | 0 | { |
116 | 0 | if( i_refa == i_ref ) |
117 | 0 | CP32( mvp, mv_a ); |
118 | 0 | else if( i_refb == i_ref ) |
119 | 0 | CP32( mvp, mv_b ); |
120 | 0 | else |
121 | 0 | CP32( mvp, mv_c ); |
122 | 0 | } |
123 | 0 | else if( i_refb == -2 && i_refc == -2 && i_refa != -2 ) |
124 | 0 | CP32( mvp, mv_a ); |
125 | 0 | else |
126 | 0 | goto median; |
127 | 0 | } Unexecuted instantiation: x264_8_mb_predict_mv Unexecuted instantiation: x264_10_mb_predict_mv |
128 | | |
129 | | void x264_mb_predict_mv_16x16( x264_t *h, int i_list, int i_ref, int16_t mvp[2] ) |
130 | 0 | { |
131 | 0 | int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1]; |
132 | 0 | int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1]; |
133 | 0 | int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8]; |
134 | 0 | int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8]; |
135 | 0 | int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4]; |
136 | 0 | int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4]; |
137 | 0 | if( i_refc == -2 ) |
138 | 0 | { |
139 | 0 | i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1]; |
140 | 0 | mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1]; |
141 | 0 | } |
142 | |
|
143 | 0 | int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref); |
144 | |
|
145 | 0 | if( i_count > 1 ) |
146 | 0 | { |
147 | 0 | median: |
148 | 0 | x264_median_mv( mvp, mv_a, mv_b, mv_c ); |
149 | 0 | } |
150 | 0 | else if( i_count == 1 ) |
151 | 0 | { |
152 | 0 | if( i_refa == i_ref ) |
153 | 0 | CP32( mvp, mv_a ); |
154 | 0 | else if( i_refb == i_ref ) |
155 | 0 | CP32( mvp, mv_b ); |
156 | 0 | else |
157 | 0 | CP32( mvp, mv_c ); |
158 | 0 | } |
159 | 0 | else if( i_refb == -2 && i_refc == -2 && i_refa != -2 ) |
160 | 0 | CP32( mvp, mv_a ); |
161 | 0 | else |
162 | 0 | goto median; |
163 | 0 | } Unexecuted instantiation: x264_8_mb_predict_mv_16x16 Unexecuted instantiation: x264_10_mb_predict_mv_16x16 |
164 | | |
165 | | |
166 | | void x264_mb_predict_mv_pskip( x264_t *h, int16_t mv[2] ) |
167 | 0 | { |
168 | 0 | int i_refa = h->mb.cache.ref[0][X264_SCAN8_0 - 1]; |
169 | 0 | int i_refb = h->mb.cache.ref[0][X264_SCAN8_0 - 8]; |
170 | 0 | int16_t *mv_a = h->mb.cache.mv[0][X264_SCAN8_0 - 1]; |
171 | 0 | int16_t *mv_b = h->mb.cache.mv[0][X264_SCAN8_0 - 8]; |
172 | |
|
173 | 0 | if( i_refa == -2 || i_refb == -2 || |
174 | 0 | !( (uint32_t)i_refa | M32( mv_a ) ) || |
175 | 0 | !( (uint32_t)i_refb | M32( mv_b ) ) ) |
176 | 0 | { |
177 | 0 | M32( mv ) = 0; |
178 | 0 | } |
179 | 0 | else |
180 | 0 | x264_mb_predict_mv_16x16( h, 0, 0, mv ); |
181 | 0 | } Unexecuted instantiation: x264_8_mb_predict_mv_pskip Unexecuted instantiation: x264_10_mb_predict_mv_pskip |
182 | | |
183 | | static int mb_predict_mv_direct16x16_temporal( x264_t *h ) |
184 | 0 | { |
185 | 0 | int mb_x = h->mb.i_mb_x; |
186 | 0 | int mb_y = h->mb.i_mb_y; |
187 | 0 | int mb_xy = h->mb.i_mb_xy; |
188 | 0 | int type_col[2] = { h->fref[1][0]->mb_type[mb_xy], h->fref[1][0]->mb_type[mb_xy] }; |
189 | 0 | int partition_col[2] = { h->fref[1][0]->mb_partition[mb_xy], h->fref[1][0]->mb_partition[mb_xy] }; |
190 | 0 | int preshift = MB_INTERLACED; |
191 | 0 | int postshift = MB_INTERLACED; |
192 | 0 | int offset = 1; |
193 | 0 | int yshift = 1; |
194 | 0 | h->mb.i_partition = partition_col[0]; |
195 | 0 | if( PARAM_INTERLACED && h->fref[1][0]->field[mb_xy] != MB_INTERLACED ) |
196 | 0 | { |
197 | 0 | if( MB_INTERLACED ) |
198 | 0 | { |
199 | 0 | mb_y = h->mb.i_mb_y&~1; |
200 | 0 | mb_xy = mb_x + h->mb.i_mb_stride * mb_y; |
201 | 0 | type_col[0] = h->fref[1][0]->mb_type[mb_xy]; |
202 | 0 | type_col[1] = h->fref[1][0]->mb_type[mb_xy + h->mb.i_mb_stride]; |
203 | 0 | partition_col[0] = h->fref[1][0]->mb_partition[mb_xy]; |
204 | 0 | partition_col[1] = h->fref[1][0]->mb_partition[mb_xy + h->mb.i_mb_stride]; |
205 | 0 | preshift = 0; |
206 | 0 | yshift = 0; |
207 | |
|
208 | 0 | if( (IS_INTRA(type_col[0]) || partition_col[0] == D_16x16) && |
209 | 0 | (IS_INTRA(type_col[1]) || partition_col[1] == D_16x16) && |
210 | 0 | partition_col[0] != D_8x8 ) |
211 | 0 | h->mb.i_partition = D_16x8; |
212 | 0 | else |
213 | 0 | h->mb.i_partition = D_8x8; |
214 | 0 | } |
215 | 0 | else |
216 | 0 | { |
217 | 0 | int cur_poc = h->fdec->i_poc + h->fdec->i_delta_poc[MB_INTERLACED&h->mb.i_mb_y&1]; |
218 | 0 | int col_parity = abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[0] - cur_poc) |
219 | 0 | >= abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[1] - cur_poc); |
220 | 0 | mb_y = (h->mb.i_mb_y&~1) + col_parity; |
221 | 0 | mb_xy = mb_x + h->mb.i_mb_stride * mb_y; |
222 | 0 | type_col[0] = type_col[1] = h->fref[1][0]->mb_type[mb_xy]; |
223 | 0 | partition_col[0] = partition_col[1] = h->fref[1][0]->mb_partition[mb_xy]; |
224 | 0 | preshift = 1; |
225 | 0 | yshift = 2; |
226 | 0 | h->mb.i_partition = partition_col[0]; |
227 | 0 | } |
228 | 0 | offset = 0; |
229 | 0 | } |
230 | 0 | int i_mb_4x4 = 16 * h->mb.i_mb_stride * mb_y + 4 * mb_x; |
231 | 0 | int i_mb_8x8 = 4 * h->mb.i_mb_stride * mb_y + 2 * mb_x; |
232 | |
|
233 | 0 | 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 | 0 | int max_i8 = (D_16x16 - h->mb.i_partition) + 1; |
239 | 0 | int step = (h->mb.i_partition == D_16x8) + 1; |
240 | 0 | int width = 4 >> ((D_16x16 - h->mb.i_partition)&1); |
241 | 0 | int height = 4 >> ((D_16x16 - h->mb.i_partition)>>1); |
242 | 0 | for( int i8 = 0; i8 < max_i8; i8 += step ) |
243 | 0 | { |
244 | 0 | int x8 = i8&1; |
245 | 0 | int y8 = i8>>1; |
246 | 0 | int ypart = (SLICE_MBAFF && h->fref[1][0]->field[mb_xy] != MB_INTERLACED) ? |
247 | 0 | MB_INTERLACED ? y8*6 : 2*(h->mb.i_mb_y&1) + y8 : |
248 | 0 | 3*y8; |
249 | |
|
250 | 0 | if( IS_INTRA( type_col[y8] ) ) |
251 | 0 | { |
252 | 0 | x264_macroblock_cache_ref( h, 2*x8, 2*y8, width, height, 0, 0 ); |
253 | 0 | x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, 0 ); |
254 | 0 | x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, 0 ); |
255 | 0 | continue; |
256 | 0 | } |
257 | | |
258 | 0 | int i_part_8x8 = i_mb_8x8 + x8 + (ypart>>1) * h->mb.i_b8_stride; |
259 | 0 | int i_ref1_ref = h->fref[1][0]->ref[0][i_part_8x8]; |
260 | 0 | int i_ref = (map_col_to_list0(i_ref1_ref>>preshift) * (1 << postshift)) + (offset&i_ref1_ref&MB_INTERLACED); |
261 | |
|
262 | 0 | if( i_ref >= 0 ) |
263 | 0 | { |
264 | 0 | int dist_scale_factor = h->mb.dist_scale_factor[i_ref][0]; |
265 | 0 | int16_t *mv_col = h->fref[1][0]->mv[0][i_mb_4x4 + 3*x8 + ypart * h->mb.i_b4_stride]; |
266 | 0 | int16_t mv_y = (mv_col[1] * (1 << yshift)) / 2; |
267 | 0 | int l0x = ( dist_scale_factor * mv_col[0] + 128 ) >> 8; |
268 | 0 | int l0y = ( dist_scale_factor * mv_y + 128 ) >> 8; |
269 | 0 | if( h->param.i_threads > 1 && (l0y > h->mb.mv_max_spel[1] || l0y-mv_y > h->mb.mv_max_spel[1]) ) |
270 | 0 | return 0; |
271 | 0 | x264_macroblock_cache_ref( h, 2*x8, 2*y8, width, height, 0, i_ref ); |
272 | 0 | x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, pack16to32_mask(l0x, l0y) ); |
273 | 0 | x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, pack16to32_mask(l0x-mv_col[0], l0y-mv_y) ); |
274 | 0 | } |
275 | 0 | else |
276 | 0 | { |
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 | 0 | return 0; |
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | 0 | return 1; |
287 | 0 | } |
288 | | |
289 | | static ALWAYS_INLINE int mb_predict_mv_direct16x16_spatial( x264_t *h, int b_interlaced ) |
290 | 0 | { |
291 | 0 | int8_t ref[2]; |
292 | 0 | ALIGNED_ARRAY_8( int16_t, mv,[2],[2] ); |
293 | 0 | for( int i_list = 0; i_list < 2; i_list++ ) |
294 | 0 | { |
295 | 0 | int i_refa = h->mb.cache.ref[i_list][X264_SCAN8_0 - 1]; |
296 | 0 | int16_t *mv_a = h->mb.cache.mv[i_list][X264_SCAN8_0 - 1]; |
297 | 0 | int i_refb = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8]; |
298 | 0 | int16_t *mv_b = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8]; |
299 | 0 | int i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 + 4]; |
300 | 0 | int16_t *mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 + 4]; |
301 | 0 | if( i_refc == -2 ) |
302 | 0 | { |
303 | 0 | i_refc = h->mb.cache.ref[i_list][X264_SCAN8_0 - 8 - 1]; |
304 | 0 | mv_c = h->mb.cache.mv[i_list][X264_SCAN8_0 - 8 - 1]; |
305 | 0 | } |
306 | |
|
307 | 0 | int i_ref = (int)X264_MIN3( (unsigned)i_refa, (unsigned)i_refb, (unsigned)i_refc ); |
308 | 0 | if( i_ref < 0 ) |
309 | 0 | { |
310 | 0 | i_ref = -1; |
311 | 0 | M32( mv[i_list] ) = 0; |
312 | 0 | } |
313 | 0 | else |
314 | 0 | { |
315 | | /* Same as x264_mb_predict_mv_16x16, but simplified to eliminate cases |
316 | | * not relevant to spatial direct. */ |
317 | 0 | int i_count = (i_refa == i_ref) + (i_refb == i_ref) + (i_refc == i_ref); |
318 | |
|
319 | 0 | if( i_count > 1 ) |
320 | 0 | x264_median_mv( mv[i_list], mv_a, mv_b, mv_c ); |
321 | 0 | else |
322 | 0 | { |
323 | 0 | if( i_refa == i_ref ) |
324 | 0 | CP32( mv[i_list], mv_a ); |
325 | 0 | else if( i_refb == i_ref ) |
326 | 0 | CP32( mv[i_list], mv_b ); |
327 | 0 | else |
328 | 0 | CP32( mv[i_list], mv_c ); |
329 | 0 | } |
330 | 0 | } |
331 | |
|
332 | 0 | x264_macroblock_cache_ref( h, 0, 0, 4, 4, i_list, i_ref ); |
333 | 0 | x264_macroblock_cache_mv_ptr( h, 0, 0, 4, 4, i_list, mv[i_list] ); |
334 | 0 | ref[i_list] = i_ref; |
335 | 0 | } |
336 | |
|
337 | 0 | int mb_x = h->mb.i_mb_x; |
338 | 0 | int mb_y = h->mb.i_mb_y; |
339 | 0 | int mb_xy = h->mb.i_mb_xy; |
340 | 0 | int type_col[2] = { h->fref[1][0]->mb_type[mb_xy], h->fref[1][0]->mb_type[mb_xy] }; |
341 | 0 | int partition_col[2] = { h->fref[1][0]->mb_partition[mb_xy], h->fref[1][0]->mb_partition[mb_xy] }; |
342 | 0 | h->mb.i_partition = partition_col[0]; |
343 | 0 | if( b_interlaced && h->fref[1][0]->field[mb_xy] != MB_INTERLACED ) |
344 | 0 | { |
345 | 0 | if( MB_INTERLACED ) |
346 | 0 | { |
347 | 0 | mb_y = h->mb.i_mb_y&~1; |
348 | 0 | mb_xy = mb_x + h->mb.i_mb_stride * mb_y; |
349 | 0 | type_col[0] = h->fref[1][0]->mb_type[mb_xy]; |
350 | 0 | type_col[1] = h->fref[1][0]->mb_type[mb_xy + h->mb.i_mb_stride]; |
351 | 0 | partition_col[0] = h->fref[1][0]->mb_partition[mb_xy]; |
352 | 0 | partition_col[1] = h->fref[1][0]->mb_partition[mb_xy + h->mb.i_mb_stride]; |
353 | |
|
354 | 0 | if( (IS_INTRA(type_col[0]) || partition_col[0] == D_16x16) && |
355 | 0 | (IS_INTRA(type_col[1]) || partition_col[1] == D_16x16) && |
356 | 0 | partition_col[0] != D_8x8 ) |
357 | 0 | h->mb.i_partition = D_16x8; |
358 | 0 | else |
359 | 0 | h->mb.i_partition = D_8x8; |
360 | 0 | } |
361 | 0 | else |
362 | 0 | { |
363 | 0 | int cur_poc = h->fdec->i_poc + h->fdec->i_delta_poc[MB_INTERLACED&h->mb.i_mb_y&1]; |
364 | 0 | int col_parity = abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[0] - cur_poc) |
365 | 0 | >= abs(h->fref[1][0]->i_poc + h->fref[1][0]->i_delta_poc[1] - cur_poc); |
366 | 0 | mb_y = (h->mb.i_mb_y&~1) + col_parity; |
367 | 0 | mb_xy = mb_x + h->mb.i_mb_stride * mb_y; |
368 | 0 | type_col[0] = type_col[1] = h->fref[1][0]->mb_type[mb_xy]; |
369 | 0 | partition_col[0] = partition_col[1] = h->fref[1][0]->mb_partition[mb_xy]; |
370 | 0 | h->mb.i_partition = partition_col[0]; |
371 | 0 | } |
372 | 0 | } |
373 | 0 | int i_mb_4x4 = b_interlaced ? 4 * (h->mb.i_b4_stride*mb_y + mb_x) : h->mb.i_b4_xy; |
374 | 0 | int i_mb_8x8 = b_interlaced ? 2 * (h->mb.i_b8_stride*mb_y + mb_x) : h->mb.i_b8_xy; |
375 | |
|
376 | 0 | int8_t *l1ref0 = &h->fref[1][0]->ref[0][i_mb_8x8]; |
377 | 0 | int8_t *l1ref1 = &h->fref[1][0]->ref[1][i_mb_8x8]; |
378 | 0 | int16_t (*l1mv[2])[2] = { (int16_t (*)[2]) &h->fref[1][0]->mv[0][i_mb_4x4], |
379 | 0 | (int16_t (*)[2]) &h->fref[1][0]->mv[1][i_mb_4x4] }; |
380 | |
|
381 | 0 | if( (M16( ref ) & 0x8080) == 0x8080 ) /* if( ref[0] < 0 && ref[1] < 0 ) */ |
382 | 0 | { |
383 | 0 | x264_macroblock_cache_ref( h, 0, 0, 4, 4, 0, 0 ); |
384 | 0 | x264_macroblock_cache_ref( h, 0, 0, 4, 4, 1, 0 ); |
385 | 0 | return 1; |
386 | 0 | } |
387 | | |
388 | 0 | if( h->param.i_threads > 1 |
389 | 0 | && ( mv[0][1] > h->mb.mv_max_spel[1] |
390 | 0 | || mv[1][1] > h->mb.mv_max_spel[1] ) ) |
391 | 0 | { |
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 | 0 | return 0; |
398 | 0 | } |
399 | | |
400 | 0 | if( !M64( mv ) || (!b_interlaced && IS_INTRA( type_col[0] )) || (ref[0]&&ref[1]) ) |
401 | 0 | 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 | 0 | int max_i8 = (D_16x16 - h->mb.i_partition) + 1; |
407 | 0 | int step = (h->mb.i_partition == D_16x8) + 1; |
408 | 0 | int width = 4 >> ((D_16x16 - h->mb.i_partition)&1); |
409 | 0 | int height = 4 >> ((D_16x16 - h->mb.i_partition)>>1); |
410 | | |
411 | | /* col_zero_flag */ |
412 | 0 | for( int i8 = 0; i8 < max_i8; i8 += step ) |
413 | 0 | { |
414 | 0 | const int x8 = i8&1; |
415 | 0 | const int y8 = i8>>1; |
416 | 0 | int ypart = (b_interlaced && h->fref[1][0]->field[mb_xy] != MB_INTERLACED) ? |
417 | 0 | MB_INTERLACED ? y8*6 : 2*(h->mb.i_mb_y&1) + y8 : |
418 | 0 | 3*y8; |
419 | 0 | int o8 = x8 + (ypart>>1) * h->mb.i_b8_stride; |
420 | 0 | int o4 = 3*x8 + ypart * h->mb.i_b4_stride; |
421 | |
|
422 | 0 | if( b_interlaced && IS_INTRA( type_col[y8] ) ) |
423 | 0 | continue; |
424 | | |
425 | 0 | int idx; |
426 | 0 | if( l1ref0[o8] == 0 ) |
427 | 0 | idx = 0; |
428 | 0 | else if( l1ref0[o8] < 0 && l1ref1[o8] == 0 ) |
429 | 0 | idx = 1; |
430 | 0 | else |
431 | 0 | continue; |
432 | | |
433 | 0 | if( abs( l1mv[idx][o4][0] ) <= 1 && abs( l1mv[idx][o4][1] ) <= 1 ) |
434 | 0 | { |
435 | 0 | if( ref[0] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 0, 0 ); |
436 | 0 | if( ref[1] == 0 ) x264_macroblock_cache_mv( h, 2*x8, 2*y8, width, height, 1, 0 ); |
437 | 0 | } |
438 | 0 | } |
439 | |
|
440 | 0 | return 1; |
441 | 0 | } |
442 | | |
443 | | |
444 | | static int mb_predict_mv_direct16x16_spatial_interlaced( x264_t *h ) |
445 | 0 | { |
446 | 0 | return mb_predict_mv_direct16x16_spatial( h, 1 ); |
447 | 0 | } |
448 | | |
449 | | static int mb_predict_mv_direct16x16_spatial_progressive( x264_t *h ) |
450 | 0 | { |
451 | 0 | return mb_predict_mv_direct16x16_spatial( h, 0 ); |
452 | 0 | } |
453 | | |
454 | | int x264_mb_predict_mv_direct16x16( x264_t *h, int *b_changed ) |
455 | 0 | { |
456 | 0 | int b_available; |
457 | 0 | if( h->param.analyse.i_direct_mv_pred == X264_DIRECT_PRED_NONE ) |
458 | 0 | return 0; |
459 | 0 | else if( h->sh.b_direct_spatial_mv_pred ) |
460 | 0 | { |
461 | 0 | if( SLICE_MBAFF ) |
462 | 0 | b_available = mb_predict_mv_direct16x16_spatial_interlaced( h ); |
463 | 0 | else |
464 | 0 | b_available = mb_predict_mv_direct16x16_spatial_progressive( h ); |
465 | 0 | } |
466 | 0 | else |
467 | 0 | b_available = mb_predict_mv_direct16x16_temporal( h ); |
468 | | |
469 | 0 | if( b_changed != NULL && b_available ) |
470 | 0 | { |
471 | 0 | int changed; |
472 | |
|
473 | 0 | changed = (int)(M32( h->mb.cache.direct_mv[0][0] ) ^ M32( h->mb.cache.mv[0][x264_scan8[0]] )); |
474 | 0 | changed |= (int)(M32( h->mb.cache.direct_mv[1][0] ) ^ M32( h->mb.cache.mv[1][x264_scan8[0]] )); |
475 | 0 | changed |= h->mb.cache.direct_ref[0][0] ^ h->mb.cache.ref[0][x264_scan8[0]]; |
476 | 0 | changed |= h->mb.cache.direct_ref[1][0] ^ h->mb.cache.ref[1][x264_scan8[0]]; |
477 | 0 | if( !changed && h->mb.i_partition != D_16x16 ) |
478 | 0 | { |
479 | 0 | changed |= (int)(M32( h->mb.cache.direct_mv[0][3] ) ^ M32( h->mb.cache.mv[0][x264_scan8[12]] )); |
480 | 0 | changed |= (int)(M32( h->mb.cache.direct_mv[1][3] ) ^ M32( h->mb.cache.mv[1][x264_scan8[12]] )); |
481 | 0 | changed |= h->mb.cache.direct_ref[0][3] ^ h->mb.cache.ref[0][x264_scan8[12]]; |
482 | 0 | changed |= h->mb.cache.direct_ref[1][3] ^ h->mb.cache.ref[1][x264_scan8[12]]; |
483 | 0 | } |
484 | 0 | if( !changed && h->mb.i_partition == D_8x8 ) |
485 | 0 | { |
486 | 0 | changed |= (int)(M32( h->mb.cache.direct_mv[0][1] ) ^ M32( h->mb.cache.mv[0][x264_scan8[4]] )); |
487 | 0 | changed |= (int)(M32( h->mb.cache.direct_mv[1][1] ) ^ M32( h->mb.cache.mv[1][x264_scan8[4]] )); |
488 | 0 | changed |= (int)(M32( h->mb.cache.direct_mv[0][2] ) ^ M32( h->mb.cache.mv[0][x264_scan8[8]] )); |
489 | 0 | changed |= (int)(M32( h->mb.cache.direct_mv[1][2] ) ^ M32( h->mb.cache.mv[1][x264_scan8[8]] )); |
490 | 0 | changed |= h->mb.cache.direct_ref[0][1] ^ h->mb.cache.ref[0][x264_scan8[4]]; |
491 | 0 | changed |= h->mb.cache.direct_ref[1][1] ^ h->mb.cache.ref[1][x264_scan8[4]]; |
492 | 0 | changed |= h->mb.cache.direct_ref[0][2] ^ h->mb.cache.ref[0][x264_scan8[8]]; |
493 | 0 | changed |= h->mb.cache.direct_ref[1][2] ^ h->mb.cache.ref[1][x264_scan8[8]]; |
494 | 0 | } |
495 | 0 | *b_changed = changed; |
496 | 0 | if( !changed ) |
497 | 0 | return b_available; |
498 | 0 | } |
499 | | |
500 | | /* cache ref & mv */ |
501 | 0 | if( b_available ) |
502 | 0 | for( int l = 0; l < 2; l++ ) |
503 | 0 | { |
504 | 0 | CP32( h->mb.cache.direct_mv[l][0], h->mb.cache.mv[l][x264_scan8[ 0]] ); |
505 | 0 | CP32( h->mb.cache.direct_mv[l][1], h->mb.cache.mv[l][x264_scan8[ 4]] ); |
506 | 0 | CP32( h->mb.cache.direct_mv[l][2], h->mb.cache.mv[l][x264_scan8[ 8]] ); |
507 | 0 | CP32( h->mb.cache.direct_mv[l][3], h->mb.cache.mv[l][x264_scan8[12]] ); |
508 | 0 | h->mb.cache.direct_ref[l][0] = h->mb.cache.ref[l][x264_scan8[ 0]]; |
509 | 0 | h->mb.cache.direct_ref[l][1] = h->mb.cache.ref[l][x264_scan8[ 4]]; |
510 | 0 | h->mb.cache.direct_ref[l][2] = h->mb.cache.ref[l][x264_scan8[ 8]]; |
511 | 0 | h->mb.cache.direct_ref[l][3] = h->mb.cache.ref[l][x264_scan8[12]]; |
512 | 0 | h->mb.cache.direct_partition = h->mb.i_partition; |
513 | 0 | } |
514 | |
|
515 | 0 | return b_available; |
516 | 0 | } Unexecuted instantiation: x264_8_mb_predict_mv_direct16x16 Unexecuted instantiation: x264_10_mb_predict_mv_direct16x16 |
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)[2], int *i_mvc ) |
520 | 0 | { |
521 | 0 | int16_t (*mvr)[2] = h->mb.mvr[i_list][i_ref]; |
522 | 0 | int i = 0; |
523 | |
|
524 | 0 | #define SET_MVP(mvp) \ |
525 | 0 | { \ |
526 | 0 | CP32( mvc[i], mvp ); \ |
527 | 0 | i++; \ |
528 | 0 | } |
529 | |
|
530 | 0 | #define SET_IMVP(xy) \ |
531 | 0 | if( xy >= 0 ) \ |
532 | 0 | { \ |
533 | 0 | int shift = 1 + MB_INTERLACED - h->mb.field[xy]; \ |
534 | 0 | int16_t *mvp = h->mb.mvr[i_list][i_ref<<1>>shift][xy]; \ |
535 | 0 | mvc[i][0] = mvp[0]; \ |
536 | 0 | mvc[i][1] = mvp[1]*2>>shift; \ |
537 | 0 | i++; \ |
538 | 0 | } |
539 | | |
540 | | /* b_direct */ |
541 | 0 | if( h->sh.i_type == SLICE_TYPE_B |
542 | 0 | && h->mb.cache.ref[i_list][x264_scan8[12]] == i_ref ) |
543 | 0 | { |
544 | 0 | SET_MVP( h->mb.cache.mv[i_list][x264_scan8[12]] ); |
545 | 0 | } |
546 | |
|
547 | 0 | if( i_ref == 0 && h->frames.b_have_lowres ) |
548 | 0 | { |
549 | 0 | int idx = i_list ? h->fref[1][0]->i_frame-h->fenc->i_frame-1 |
550 | 0 | : h->fenc->i_frame-h->fref[0][0]->i_frame-1; |
551 | 0 | if( idx <= h->param.i_bframe ) |
552 | 0 | { |
553 | 0 | int16_t (*lowres_mv)[2] = h->fenc->lowres_mvs[i_list][idx]; |
554 | 0 | if( lowres_mv[0][0] != 0x7fff ) |
555 | 0 | { |
556 | 0 | M32( mvc[i] ) = (M32( lowres_mv[h->mb.i_mb_xy] )*2)&0xfffeffff; |
557 | 0 | i++; |
558 | 0 | } |
559 | 0 | } |
560 | 0 | } |
561 | | |
562 | | /* spatial predictors */ |
563 | 0 | if( SLICE_MBAFF ) |
564 | 0 | { |
565 | 0 | SET_IMVP( h->mb.i_mb_left_xy[0] ); |
566 | 0 | SET_IMVP( h->mb.i_mb_top_xy ); |
567 | 0 | SET_IMVP( h->mb.i_mb_topleft_xy ); |
568 | 0 | SET_IMVP( h->mb.i_mb_topright_xy ); |
569 | 0 | } |
570 | 0 | else |
571 | 0 | { |
572 | 0 | SET_MVP( mvr[h->mb.i_mb_left_xy[0]] ); |
573 | 0 | SET_MVP( mvr[h->mb.i_mb_top_xy] ); |
574 | 0 | SET_MVP( mvr[h->mb.i_mb_topleft_xy] ); |
575 | 0 | SET_MVP( mvr[h->mb.i_mb_topright_xy] ); |
576 | 0 | } |
577 | 0 | #undef SET_IMVP |
578 | 0 | #undef SET_MVP |
579 | | |
580 | | /* temporal predictors */ |
581 | 0 | if( h->fref[0][0]->i_ref[0] > 0 ) |
582 | 0 | { |
583 | 0 | x264_frame_t *l0 = h->fref[0][0]; |
584 | 0 | int field = h->mb.i_mb_y&1; |
585 | 0 | int curpoc = h->fdec->i_poc + h->fdec->i_delta_poc[field]; |
586 | 0 | int refpoc = h->fref[i_list][i_ref>>SLICE_MBAFF]->i_poc; |
587 | 0 | refpoc += l0->i_delta_poc[field^(i_ref&1)]; |
588 | |
|
589 | 0 | #define SET_TMVP( dx, dy ) \ |
590 | 0 | { \ |
591 | 0 | int mb_index = h->mb.i_mb_xy + dx + dy*h->mb.i_mb_stride; \ |
592 | 0 | int scale = (curpoc - refpoc) * l0->inv_ref_poc[MB_INTERLACED&field]; \ |
593 | 0 | mvc[i][0] = x264_clip3( (l0->mv16x16[mb_index][0]*scale + 128) >> 8, INT16_MIN, INT16_MAX ); \ |
594 | 0 | mvc[i][1] = x264_clip3( (l0->mv16x16[mb_index][1]*scale + 128) >> 8, INT16_MIN, INT16_MAX ); \ |
595 | 0 | i++; \ |
596 | 0 | } |
597 | |
|
598 | 0 | SET_TMVP(0,0); |
599 | 0 | if( h->mb.i_mb_x < h->mb.i_mb_width-1 ) |
600 | 0 | SET_TMVP(1,0); |
601 | 0 | if( h->mb.i_mb_y < h->mb.i_mb_height-1 ) |
602 | 0 | SET_TMVP(0,1); |
603 | 0 | #undef SET_TMVP |
604 | 0 | } |
605 | |
|
606 | 0 | *i_mvc = i; |
607 | 0 | } Unexecuted instantiation: x264_8_mb_predict_mv_ref16x16 Unexecuted instantiation: x264_10_mb_predict_mv_ref16x16 |