Split spatial and temporal direct MV generation.
[libav.git] / libavcodec / h264_direct.c
CommitLineData
943f69a6
MN
1/*
2 * H.26L/H.264/AVC/JVT/14496-10/... direct mb/block decoding
3 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22/**
23 * @file libavcodec/h264_direct.c
24 * H.264 / AVC / MPEG4 part10 direct mb/block decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
27
28#include "internal.h"
29#include "dsputil.h"
30#include "avcodec.h"
31#include "mpegvideo.h"
32#include "h264.h"
33#include "rectangle.h"
34
943f69a6
MN
35//#undef NDEBUG
36#include <assert.h>
37
38
39static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
40 int poc0 = h->ref_list[0][i].poc;
41 int td = av_clip(poc1 - poc0, -128, 127);
42 if(td == 0 || h->ref_list[0][i].long_ref){
43 return 256;
44 }else{
45 int tb = av_clip(poc - poc0, -128, 127);
46 int tx = (16384 + (FFABS(td) >> 1)) / td;
47 return av_clip((tb*tx + 32) >> 6, -1024, 1023);
48 }
49}
50
51void ff_h264_direct_dist_scale_factor(H264Context * const h){
52 MpegEncContext * const s = &h->s;
53 const int poc = h->s.current_picture_ptr->field_poc[ s->picture_structure == PICT_BOTTOM_FIELD ];
54 const int poc1 = h->ref_list[1][0].poc;
55 int i, field;
56 for(field=0; field<2; field++){
57 const int poc = h->s.current_picture_ptr->field_poc[field];
58 const int poc1 = h->ref_list[1][0].field_poc[field];
59 for(i=0; i < 2*h->ref_count[0]; i++)
60 h->dist_scale_factor_field[field][i^field] = get_scale_factor(h, poc, poc1, i+16);
61 }
62
63 for(i=0; i<h->ref_count[0]; i++){
64 h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
65 }
66}
67
68static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
69 MpegEncContext * const s = &h->s;
70 Picture * const ref1 = &h->ref_list[1][0];
71 int j, old_ref, rfield;
72 int start= mbafi ? 16 : 0;
488dba70 73 int end = mbafi ? 16+2*h->ref_count[0] : h->ref_count[0];
943f69a6
MN
74 int interl= mbafi || s->picture_structure != PICT_FRAME;
75
76 /* bogus; fills in for missing frames */
77 memset(map[list], 0, sizeof(map[list]));
78
79 for(rfield=0; rfield<2; rfield++){
80 for(old_ref=0; old_ref<ref1->ref_count[colfield][list]; old_ref++){
81 int poc = ref1->ref_poc[colfield][list][old_ref];
82
83 if (!interl)
84 poc |= 3;
85 else if( interl && (poc&3) == 3) //FIXME store all MBAFF references so this isnt needed
86 poc= (poc&~3) + rfield + 1;
87
88 for(j=start; j<end; j++){
488dba70 89 if(4*h->ref_list[0][j].frame_num + (h->ref_list[0][j].reference&3) == poc){
943f69a6
MN
90 int cur_ref= mbafi ? (j-16)^field : j;
91 map[list][2*old_ref + (rfield^field) + 16] = cur_ref;
488dba70 92 if(rfield == field || !interl)
943f69a6
MN
93 map[list][old_ref] = cur_ref;
94 break;
95 }
96 }
97 }
98 }
99}
100
101void ff_h264_direct_ref_list_init(H264Context * const h){
102 MpegEncContext * const s = &h->s;
103 Picture * const ref1 = &h->ref_list[1][0];
104 Picture * const cur = s->current_picture_ptr;
105 int list, j, field;
106 int sidx= (s->picture_structure&1)^1;
107 int ref1sidx= (ref1->reference&1)^1;
108
109 for(list=0; list<2; list++){
110 cur->ref_count[sidx][list] = h->ref_count[list];
111 for(j=0; j<h->ref_count[list]; j++)
112 cur->ref_poc[sidx][list][j] = 4*h->ref_list[list][j].frame_num + (h->ref_list[list][j].reference&3);
113 }
114
115 if(s->picture_structure == PICT_FRAME){
116 memcpy(cur->ref_count[1], cur->ref_count[0], sizeof(cur->ref_count[0]));
117 memcpy(cur->ref_poc [1], cur->ref_poc [0], sizeof(cur->ref_poc [0]));
118 }
119
120 cur->mbaff= FRAME_MBAFF;
121
0a8dca87 122 h->col_fieldoff= 0;
d88ea354
MN
123 if(s->picture_structure == PICT_FRAME){
124 int cur_poc = s->current_picture_ptr->poc;
125 int *col_poc = h->ref_list[1]->field_poc;
0a8dca87
MN
126 h->col_parity= (FFABS(col_poc[0] - cur_poc) >= FFABS(col_poc[1] - cur_poc));
127 ref1sidx=sidx= h->col_parity;
128 }else if(!(s->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff){ // FL -> FL & differ parity
129 h->col_fieldoff= s->mb_stride*(2*(h->ref_list[1][0].reference) - 3);
d88ea354
MN
130 }
131
0a8dca87
MN
132 if(cur->pict_type != FF_B_TYPE || h->direct_spatial_mv_pred)
133 return;
134
943f69a6
MN
135 for(list=0; list<2; list++){
136 fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
33a6c0c9 137 if(FRAME_MBAFF)
943f69a6
MN
138 for(field=0; field<2; field++)
139 fill_colmap(h, h->map_col_to_list0_field[field], list, field, field, 1);
140 }
141}
142
edb4df43 143static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
943f69a6
MN
144 MpegEncContext * const s = &h->s;
145 int b8_stride = h->b8_stride;
146 int b4_stride = h->b_stride;
147 int mb_xy = h->mb_xy;
148 int mb_type_col[2];
149 const int16_t (*l1mv0)[2], (*l1mv1)[2];
150 const int8_t *l1ref0, *l1ref1;
151 const int is_b8x8 = IS_8X8(*mb_type);
152 unsigned int sub_mb_type;
153 int i8, i4;
154
155 assert(h->ref_list[1][0].reference&3);
156
157#define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
158
159 if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
160 if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL
0a8dca87 161 mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
943f69a6 162 b8_stride = 0;
0a8dca87
MN
163 }else{
164 mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
943f69a6
MN
165 }
166 goto single_col;
167 }else{ // AFL/AFR/FR/FL -> AFR/FR
168 if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
169 mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
170 mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
171 mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
172 b8_stride *= 3;
173 b4_stride *= 6;
668fd142 174
e8d964fa 175 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
943f69a6
MN
176 if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
177 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
178 && !is_b8x8){
943f69a6
MN
179 *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
180 }else{
943f69a6
MN
181 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
182 }
183 }else{ // AFR/FR -> AFR/FR
184single_col:
185 mb_type_col[0] =
186 mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
daaf6e3e
MN
187
188 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
189 if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
943f69a6 190 *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
464baa66 191 }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
464baa66 192 *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
943f69a6 193 }else{
daaf6e3e
MN
194 if(!h->sps.direct_8x8_inference_flag){
195 /* FIXME save sub mb types from previous frames (or derive from MVs)
196 * so we know exactly what block size to use */
197 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
198 }
943f69a6
MN
199 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
200 }
201 }
202 }
203
204 l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
205 l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
206 l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];
207 l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];
208 if(!b8_stride){
209 if(s->mb_y&1){
210 l1ref0 += h->b8_stride;
211 l1ref1 += h->b8_stride;
212 l1mv0 += 2*b4_stride;
213 l1mv1 += 2*b4_stride;
214 }
215 }
216
edb4df43 217 {
943f69a6 218 int ref[2];
828cfff6 219 int mv[2];
943f69a6
MN
220 int list;
221
943f69a6
MN
222 /* ref = min(neighbors) */
223 for(list=0; list<2; list++){
35c8b9c0
MN
224 int left_ref = h->ref_cache[list][scan8[0] - 1];
225 int top_ref = h->ref_cache[list][scan8[0] - 8];
943f69a6 226 int refc = h->ref_cache[list][scan8[0] - 8 + 4];
35c8b9c0
MN
227 const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
228 if(refc == PART_NOT_AVAILABLE){
943f69a6 229 refc = h->ref_cache[list][scan8[0] - 8 - 1];
35c8b9c0
MN
230 C = h-> mv_cache[list][scan8[0] - 8 - 1];
231 }
232 ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
f28a2656 233 if(ref[list] >= 0){
35c8b9c0
MN
234 //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
235 const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
236 const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
237
238 int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
239 if(match_count > 1){ //most common
828cfff6
MN
240 mv[list]= (mid_pred(A[0], B[0], C[0])&0xFFFF)
241 +(mid_pred(A[1], B[1], C[1])<<16);
35c8b9c0
MN
242 }else {
243 assert(match_count==1);
244 if(left_ref==ref[list]){
828cfff6 245 mv[list]= *(uint32_t*)A;
35c8b9c0 246 }else if(top_ref==ref[list]){
828cfff6 247 mv[list]= *(uint32_t*)B;
35c8b9c0 248 }else{
828cfff6 249 mv[list]= *(uint32_t*)C;
35c8b9c0
MN
250 }
251 }
f28a2656
MN
252 }else{
253 int mask= ~(MB_TYPE_L0 << (2*list));
828cfff6 254 mv[list] = 0;
943f69a6 255 ref[list] = -1;
f28a2656
MN
256 if(!is_b8x8)
257 *mb_type &= mask;
258 sub_mb_type &= mask;
259 }
943f69a6 260 }
943f69a6
MN
261 if(ref[0] < 0 && ref[1] < 0){
262 ref[0] = ref[1] = 0;
943f69a6 263 if(!is_b8x8)
f28a2656
MN
264 *mb_type |= MB_TYPE_L0L1;
265 sub_mb_type |= MB_TYPE_L0L1;
943f69a6
MN
266 }
267
812bfd7b
MN
268 if(!is_b8x8 && (mv[0]|mv[1]) == 0){
269 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
270 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
271 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
272 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
273 *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
274 }else if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
1be49ce5 275 int n=0;
943f69a6
MN
276 for(i8=0; i8<4; i8++){
277 int x8 = i8&1;
278 int y8 = i8>>1;
279 int xy8 = x8+y8*b8_stride;
280 int xy4 = 3*x8+y8*b4_stride;
3b9e61e4 281 int a,b;
943f69a6
MN
282
283 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
284 continue;
285 h->sub_mb_type[i8] = sub_mb_type;
286
287 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
288 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
8dcbf4c7 289 if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
943f69a6
MN
290 && ( (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
291 || (l1ref0[xy8] < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
3b9e61e4 292 a=b=0;
943f69a6 293 if(ref[0] > 0)
828cfff6 294 a= mv[0];
943f69a6 295 if(ref[1] > 0)
828cfff6 296 b= mv[1];
1be49ce5 297 n++;
943f69a6 298 }else{
828cfff6
MN
299 a= mv[0];
300 b= mv[1];
943f69a6
MN
301 }
302 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
303 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
304 }
1be49ce5
MN
305 if(!is_b8x8 && !(n&3))
306 *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
943f69a6 307 }else if(IS_16X16(*mb_type)){
3b9e61e4 308 int a,b;
943f69a6
MN
309
310 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
311 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
8dcbf4c7 312 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
943f69a6
MN
313 && ( (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
314 || (l1ref0[0] < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
055a6aa7 315 && h->x264_build>33U))){
3b9e61e4 316 a=b=0;
943f69a6 317 if(ref[0] > 0)
828cfff6 318 a= mv[0];
943f69a6 319 if(ref[1] > 0)
828cfff6 320 b= mv[1];
943f69a6 321 }else{
828cfff6
MN
322 a= mv[0];
323 b= mv[1];
943f69a6
MN
324 }
325 fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
326 fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
327 }else{
3babc711 328 int n=0;
943f69a6
MN
329 for(i8=0; i8<4; i8++){
330 const int x8 = i8&1;
331 const int y8 = i8>>1;
332
333 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
334 continue;
335 h->sub_mb_type[i8] = sub_mb_type;
336
828cfff6
MN
337 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
338 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
943f69a6
MN
339 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
340 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
341
342 /* col_zero_flag */
8dcbf4c7 343 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && ( l1ref0[x8 + y8*b8_stride] == 0
943f69a6 344 || (l1ref0[x8 + y8*b8_stride] < 0 && l1ref1[x8 + y8*b8_stride] == 0
055a6aa7 345 && h->x264_build>33U))){
943f69a6
MN
346 const int16_t (*l1mv)[2]= l1ref0[x8 + y8*b8_stride] == 0 ? l1mv0 : l1mv1;
347 if(IS_SUB_8X8(sub_mb_type)){
348 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
349 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
350 if(ref[0] == 0)
351 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
352 if(ref[1] == 0)
353 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
3babc711 354 n+=4;
943f69a6 355 }
8b9eb2df
MN
356 }else{
357 int m=0;
943f69a6
MN
358 for(i4=0; i4<4; i4++){
359 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
360 if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
361 if(ref[0] == 0)
362 *(uint32_t*)h->mv_cache[0][scan8[i8*4+i4]] = 0;
363 if(ref[1] == 0)
364 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] = 0;
8b9eb2df 365 m++;
943f69a6
MN
366 }
367 }
8b9eb2df
MN
368 if(!(m&3))
369 h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
3babc711 370 n+=m;
8b9eb2df 371 }
943f69a6
MN
372 }
373 }
3babc711
MN
374 if(!is_b8x8 && !(n&15))
375 *mb_type= (*mb_type & ~(MB_TYPE_8x8|MB_TYPE_16x8|MB_TYPE_8x16|MB_TYPE_P1L0|MB_TYPE_P1L1))|MB_TYPE_16x16|MB_TYPE_DIRECT2;
943f69a6 376 }
edb4df43
MN
377 }
378}
379
380static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
381 MpegEncContext * const s = &h->s;
382 int b8_stride = h->b8_stride;
383 int b4_stride = h->b_stride;
384 int mb_xy = h->mb_xy;
385 int mb_type_col[2];
386 const int16_t (*l1mv0)[2], (*l1mv1)[2];
387 const int8_t *l1ref0, *l1ref1;
388 const int is_b8x8 = IS_8X8(*mb_type);
389 unsigned int sub_mb_type;
390 int i8, i4;
391
392 assert(h->ref_list[1][0].reference&3);
393
394 if(IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])){ // AFL/AFR/FR/FL -> AFL/FL
395 if(!IS_INTERLACED(*mb_type)){ // AFR/FR -> AFL/FL
396 mb_xy= s->mb_x + ((s->mb_y&~1) + h->col_parity)*s->mb_stride;
397 b8_stride = 0;
398 }else{
399 mb_xy += h->col_fieldoff; // non zero for FL -> FL & differ parity
400 }
401 goto single_col;
402 }else{ // AFL/AFR/FR/FL -> AFR/FR
403 if(IS_INTERLACED(*mb_type)){ // AFL /FL -> AFR/FR
404 mb_xy= s->mb_x + (s->mb_y&~1)*s->mb_stride;
405 mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
406 mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + s->mb_stride];
407 b8_stride *= 3;
408 b4_stride *= 6;
409
410 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
411
412 if( (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
413 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
414 && !is_b8x8){
415 *mb_type |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
416 }else{
417 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
418 }
419 }else{ // AFR/FR -> AFR/FR
420single_col:
421 mb_type_col[0] =
422 mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
423
424 sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
425 if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
426 *mb_type |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
427 }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
428 *mb_type |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
429 }else{
430 if(!h->sps.direct_8x8_inference_flag){
431 /* FIXME save sub mb types from previous frames (or derive from MVs)
432 * so we know exactly what block size to use */
433 sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
434 }
435 *mb_type |= MB_TYPE_8x8|MB_TYPE_L0L1;
436 }
437 }
438 }
439
440 l1mv0 = &h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
441 l1mv1 = &h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
442 l1ref0 = &h->ref_list[1][0].ref_index [0][h->mb2b8_xy[mb_xy]];
443 l1ref1 = &h->ref_list[1][0].ref_index [1][h->mb2b8_xy[mb_xy]];
444 if(!b8_stride){
445 if(s->mb_y&1){
446 l1ref0 += h->b8_stride;
447 l1ref1 += h->b8_stride;
448 l1mv0 += 2*b4_stride;
449 l1mv1 += 2*b4_stride;
450 }
451 }
452
453 {
943f69a6
MN
454 const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
455 const int *dist_scale_factor = h->dist_scale_factor;
542d4a75 456 int ref_offset;
943f69a6
MN
457
458 if(FRAME_MBAFF && IS_INTERLACED(*mb_type)){
459 map_col_to_list0[0] = h->map_col_to_list0_field[s->mb_y&1][0];
460 map_col_to_list0[1] = h->map_col_to_list0_field[s->mb_y&1][1];
461 dist_scale_factor =h->dist_scale_factor_field[s->mb_y&1];
462 }
542d4a75 463 ref_offset = (h->ref_list[1][0].mbaff<<4) & (mb_type_col[0]>>3); //if(h->ref_list[1][0].mbaff && IS_INTERLACED(mb_type_col[0])) ref_offset=16 else 0
943f69a6
MN
464
465 if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
943f69a6 466 int y_shift = 2*!IS_INTERLACED(*mb_type);
5aa1d759 467 assert(h->sps.direct_8x8_inference_flag);
943f69a6
MN
468
469 for(i8=0; i8<4; i8++){
470 const int x8 = i8&1;
471 const int y8 = i8>>1;
472 int ref0, scale;
473 const int16_t (*l1mv)[2]= l1mv0;
474
475 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
476 continue;
477 h->sub_mb_type[i8] = sub_mb_type;
478
479 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
480 if(IS_INTRA(mb_type_col[y8])){
481 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
482 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
483 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
484 continue;
485 }
486
487 ref0 = l1ref0[x8 + y8*b8_stride];
488 if(ref0 >= 0)
489 ref0 = map_col_to_list0[0][ref0 + ref_offset];
490 else{
491 ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
492 l1mv= l1mv1;
493 }
494 scale = dist_scale_factor[ref0];
495 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
496
497 {
498 const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
499 int my_col = (mv_col[1]<<y_shift)/2;
500 int mx = (scale * mv_col[0] + 128) >> 8;
501 int my = (scale * my_col + 128) >> 8;
502 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
503 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
504 }
505 }
506 return;
507 }
508
509 /* one-to-one mv scaling */
510
511 if(IS_16X16(*mb_type)){
512 int ref, mv0, mv1;
513
514 fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
515 if(IS_INTRA(mb_type_col[0])){
516 ref=mv0=mv1=0;
517 }else{
518 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
519 : map_col_to_list0[1][l1ref1[0] + ref_offset];
520 const int scale = dist_scale_factor[ref0];
521 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
522 int mv_l0[2];
523 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
524 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
525 ref= ref0;
526 mv0= pack16to32(mv_l0[0],mv_l0[1]);
527 mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
528 }
529 fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
530 fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
531 fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
532 }else{
533 for(i8=0; i8<4; i8++){
534 const int x8 = i8&1;
535 const int y8 = i8>>1;
536 int ref0, scale;
537 const int16_t (*l1mv)[2]= l1mv0;
538
539 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
540 continue;
541 h->sub_mb_type[i8] = sub_mb_type;
542 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
543 if(IS_INTRA(mb_type_col[0])){
544 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
545 fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
546 fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
547 continue;
548 }
549
50f8e985 550 ref0 = l1ref0[x8 + y8*b8_stride];
943f69a6 551 if(ref0 >= 0)
50f8e985 552 ref0 = map_col_to_list0[0][ref0 + ref_offset];
943f69a6
MN
553 else{
554 ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
555 l1mv= l1mv1;
556 }
557 scale = dist_scale_factor[ref0];
558
559 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
560 if(IS_SUB_8X8(sub_mb_type)){
561 const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
562 int mx = (scale * mv_col[0] + 128) >> 8;
563 int my = (scale * mv_col[1] + 128) >> 8;
564 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
565 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
566 }else
567 for(i4=0; i4<4; i4++){
568 const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
569 int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
570 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
571 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
572 *(uint32_t*)h->mv_cache[1][scan8[i8*4+i4]] =
573 pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
574 }
575 }
576 }
577 }
578}
edb4df43
MN
579
580void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
581 if(h->direct_spatial_mv_pred){
582 pred_spatial_direct_motion(h, mb_type);
583 }else{
584 pred_temp_direct_motion(h, mb_type);
585 }
586}