]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_direct.c
avcodec/dcadec: use a constant instead of assuming every compiler can optimize pow...
[ffmpeg] / libavcodec / h264_direct.c
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
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 "avcodec.h"
30 #include "h264.h"
31 #include "mpegutils.h"
32 #include "rectangle.h"
33 #include "thread.h"
34
35 #include <assert.h>
36
37
38 static int get_scale_factor(H264Context * const h, int poc, int poc1, int i){
39     int poc0 = h->ref_list[0][i].poc;
40     int td = av_clip(poc1 - poc0, -128, 127);
41     if(td == 0 || h->ref_list[0][i].long_ref){
42         return 256;
43     }else{
44         int tb = av_clip(poc - poc0, -128, 127);
45         int tx = (16384 + (FFABS(td) >> 1)) / td;
46         return av_clip((tb*tx + 32) >> 6, -1024, 1023);
47     }
48 }
49
50 void ff_h264_direct_dist_scale_factor(H264Context * const h){
51     const int poc = h->cur_pic_ptr->field_poc[h->picture_structure == PICT_BOTTOM_FIELD];
52     const int poc1 = h->ref_list[1][0].poc;
53     int i, field;
54
55     if (FRAME_MBAFF(h))
56         for (field = 0; field < 2; field++){
57             const int poc  = h->cur_pic_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] =
61                     get_scale_factor(h, poc, poc1, i+16);
62         }
63
64     for (i = 0; i < h->ref_count[0]; i++){
65         h->dist_scale_factor[i] = get_scale_factor(h, poc, poc1, i);
66     }
67 }
68
69 static void fill_colmap(H264Context *h, int map[2][16+32], int list, int field, int colfield, int mbafi){
70     H264Picture * const ref1 = &h->ref_list[1][0];
71     int j, old_ref, rfield;
72     int start= mbafi ? 16                      : 0;
73     int end  = mbafi ? 16+2*h->ref_count[0]    : h->ref_count[0];
74     int interl= mbafi || h->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 is not needed
86                 poc= (poc&~3) + rfield + 1;
87
88             for(j=start; j<end; j++){
89                 if (4 * h->ref_list[0][j].frame_num + (h->ref_list[0][j].reference & 3) == poc) {
90                     int cur_ref= mbafi ? (j-16)^field : j;
91                     if (ref1->mbaff)
92                         map[list][2 * old_ref + (rfield^field) + 16] = cur_ref;
93                     if(rfield == field || !interl)
94                         map[list][old_ref] = cur_ref;
95                     break;
96                 }
97             }
98         }
99     }
100 }
101
102 void ff_h264_direct_ref_list_init(H264Context * const h){
103     H264Picture * const ref1 = &h->ref_list[1][0];
104     H264Picture * const cur = h->cur_pic_ptr;
105     int list, j, field;
106     int sidx= (h->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(h->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(h);
121
122     h->col_fieldoff= 0;
123     if(h->picture_structure == PICT_FRAME){
124         int cur_poc = h->cur_pic_ptr->poc;
125         int *col_poc = h->ref_list[1]->field_poc;
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 (!(h->picture_structure & h->ref_list[1][0].reference) && !h->ref_list[1][0].mbaff) { // FL -> FL & differ parity
129         h->col_fieldoff = 2 * h->ref_list[1][0].reference - 3;
130     }
131
132     if (h->slice_type_nos != AV_PICTURE_TYPE_B || h->direct_spatial_mv_pred)
133         return;
134
135     for(list=0; list<2; list++){
136         fill_colmap(h, h->map_col_to_list0, list, sidx, ref1sidx, 0);
137         if (FRAME_MBAFF(h))
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
143 static void await_reference_mb_row(H264Context * const h, H264Picture *ref, int mb_y)
144 {
145     int ref_field = ref->reference - 1;
146     int ref_field_picture = ref->field_picture;
147     int ref_height = 16*h->mb_height >> ref_field_picture;
148
149     if(!HAVE_THREADS || !(h->avctx->active_thread_type&FF_THREAD_FRAME))
150         return;
151
152     //FIXME it can be safe to access mb stuff
153     //even if pixels aren't deblocked yet
154
155     ff_thread_await_progress(&ref->tf,
156                              FFMIN(16 * mb_y >> ref_field_picture, ref_height - 1),
157                              ref_field_picture && ref_field);
158 }
159
160 static void pred_spatial_direct_motion(H264Context * const h, int *mb_type){
161     int b8_stride = 2;
162     int b4_stride = h->b_stride;
163     int mb_xy = h->mb_xy, mb_y = h->mb_y;
164     int mb_type_col[2];
165     const int16_t (*l1mv0)[2], (*l1mv1)[2];
166     const int8_t *l1ref0, *l1ref1;
167     const int is_b8x8 = IS_8X8(*mb_type);
168     unsigned int sub_mb_type= MB_TYPE_L0L1;
169     int i8, i4;
170     int ref[2];
171     int mv[2];
172     int list;
173
174     assert(h->ref_list[1][0].reference & 3);
175
176     await_reference_mb_row(h, &h->ref_list[1][0], h->mb_y + !!IS_INTERLACED(*mb_type));
177
178 #define MB_TYPE_16x16_OR_INTRA (MB_TYPE_16x16|MB_TYPE_INTRA4x4|MB_TYPE_INTRA16x16|MB_TYPE_INTRA_PCM)
179
180
181     /* ref = min(neighbors) */
182     for(list=0; list<2; list++){
183         int left_ref = h->ref_cache[list][scan8[0] - 1];
184         int top_ref  = h->ref_cache[list][scan8[0] - 8];
185         int refc = h->ref_cache[list][scan8[0] - 8 + 4];
186         const int16_t *C= h->mv_cache[list][ scan8[0] - 8 + 4];
187         if(refc == PART_NOT_AVAILABLE){
188             refc = h->ref_cache[list][scan8[0] - 8 - 1];
189             C    = h-> mv_cache[list][scan8[0] - 8 - 1];
190         }
191         ref[list] = FFMIN3((unsigned)left_ref, (unsigned)top_ref, (unsigned)refc);
192         if(ref[list] >= 0){
193             //this is just pred_motion() but with the cases removed that cannot happen for direct blocks
194             const int16_t * const A= h->mv_cache[list][ scan8[0] - 1 ];
195             const int16_t * const B= h->mv_cache[list][ scan8[0] - 8 ];
196
197             int match_count= (left_ref==ref[list]) + (top_ref==ref[list]) + (refc==ref[list]);
198             if(match_count > 1){ //most common
199                 mv[list]= pack16to32(mid_pred(A[0], B[0], C[0]),
200                                      mid_pred(A[1], B[1], C[1]) );
201             }else {
202                 assert(match_count==1);
203                 if(left_ref==ref[list]){
204                     mv[list]= AV_RN32A(A);
205                 }else if(top_ref==ref[list]){
206                     mv[list]= AV_RN32A(B);
207                 }else{
208                     mv[list]= AV_RN32A(C);
209                 }
210             }
211             av_assert2(ref[list] < (h->ref_count[list] << !!FRAME_MBAFF(h)));
212         }else{
213             int mask= ~(MB_TYPE_L0 << (2*list));
214             mv[list] = 0;
215             ref[list] = -1;
216             if(!is_b8x8)
217                 *mb_type &= mask;
218             sub_mb_type &= mask;
219         }
220     }
221     if(ref[0] < 0 && ref[1] < 0){
222         ref[0] = ref[1] = 0;
223         if(!is_b8x8)
224             *mb_type |= MB_TYPE_L0L1;
225         sub_mb_type |= MB_TYPE_L0L1;
226     }
227
228     if(!(is_b8x8|mv[0]|mv[1])){
229         fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
230         fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
231         fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
232         fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, 0, 4);
233         *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;
234         return;
235     }
236
237     if (IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
238         if (!IS_INTERLACED(*mb_type)) {                          //     AFR/FR    -> AFL/FL
239             mb_y = (h->mb_y&~1) + h->col_parity;
240             mb_xy= h->mb_x + ((h->mb_y&~1) + h->col_parity)*h->mb_stride;
241             b8_stride = 0;
242         }else{
243             mb_y  += h->col_fieldoff;
244             mb_xy += h->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
245         }
246         goto single_col;
247     }else{                                               // AFL/AFR/FR/FL -> AFR/FR
248         if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
249             mb_y = h->mb_y&~1;
250             mb_xy= h->mb_x + (h->mb_y&~1)*h->mb_stride;
251             mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
252             mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
253             b8_stride = 2+4*h->mb_stride;
254             b4_stride *= 6;
255             if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
256                 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
257                 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
258             }
259
260             sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
261             if(    (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
262                 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
263                 && !is_b8x8){
264                 *mb_type   |= MB_TYPE_16x8 |MB_TYPE_DIRECT2; /* B_16x8 */
265             }else{
266                 *mb_type   |= MB_TYPE_8x8;
267             }
268         }else{                                           //     AFR/FR    -> AFR/FR
269 single_col:
270             mb_type_col[0] =
271             mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
272
273             sub_mb_type |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
274             if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
275                 *mb_type   |= MB_TYPE_16x16|MB_TYPE_DIRECT2; /* B_16x16 */
276             }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
277                 *mb_type   |= MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
278             }else{
279                 if(!h->sps.direct_8x8_inference_flag){
280                     /* FIXME save sub mb types from previous frames (or derive from MVs)
281                     * so we know exactly what block size to use */
282                     sub_mb_type += (MB_TYPE_8x8-MB_TYPE_16x16); /* B_SUB_4x4 */
283                 }
284                 *mb_type   |= MB_TYPE_8x8;
285             }
286         }
287     }
288
289     await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
290
291     l1mv0  = (void*)&h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
292     l1mv1  = (void*)&h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
293     l1ref0 = &h->ref_list[1][0].ref_index [0][4 * mb_xy];
294     l1ref1 = &h->ref_list[1][0].ref_index [1][4 * mb_xy];
295     if(!b8_stride){
296         if(h->mb_y&1){
297             l1ref0 += 2;
298             l1ref1 += 2;
299             l1mv0  +=  2*b4_stride;
300             l1mv1  +=  2*b4_stride;
301         }
302     }
303
304
305         if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
306             int n=0;
307             for(i8=0; i8<4; i8++){
308                 int x8 = i8&1;
309                 int y8 = i8>>1;
310                 int xy8 = x8+y8*b8_stride;
311                 int xy4 = 3*x8+y8*b4_stride;
312                 int a,b;
313
314                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
315                     continue;
316                 h->sub_mb_type[i8] = sub_mb_type;
317
318                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
319                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
320                 if(!IS_INTRA(mb_type_col[y8]) && !h->ref_list[1][0].long_ref
321                    && (   (l1ref0[xy8] == 0 && FFABS(l1mv0[xy4][0]) <= 1 && FFABS(l1mv0[xy4][1]) <= 1)
322                        || (l1ref0[xy8]  < 0 && l1ref1[xy8] == 0 && FFABS(l1mv1[xy4][0]) <= 1 && FFABS(l1mv1[xy4][1]) <= 1))){
323                     a=b=0;
324                     if(ref[0] > 0)
325                         a= mv[0];
326                     if(ref[1] > 0)
327                         b= mv[1];
328                     n++;
329                 }else{
330                     a= mv[0];
331                     b= mv[1];
332                 }
333                 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, a, 4);
334                 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, b, 4);
335             }
336             if(!is_b8x8 && !(n&3))
337                 *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;
338         }else if(IS_16X16(*mb_type)){
339             int a,b;
340
341             fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, (uint8_t)ref[0], 1);
342             fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, (uint8_t)ref[1], 1);
343             if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref
344                && (   (l1ref0[0] == 0 && FFABS(l1mv0[0][0]) <= 1 && FFABS(l1mv0[0][1]) <= 1)
345                    || (l1ref0[0]  < 0 && l1ref1[0] == 0 && FFABS(l1mv1[0][0]) <= 1 && FFABS(l1mv1[0][1]) <= 1
346                        && h->x264_build>33U))){
347                 a=b=0;
348                 if(ref[0] > 0)
349                     a= mv[0];
350                 if(ref[1] > 0)
351                     b= mv[1];
352             }else{
353                 a= mv[0];
354                 b= mv[1];
355             }
356             fill_rectangle(&h->mv_cache[0][scan8[0]], 4, 4, 8, a, 4);
357             fill_rectangle(&h->mv_cache[1][scan8[0]], 4, 4, 8, b, 4);
358         }else{
359             int n=0;
360             for(i8=0; i8<4; i8++){
361                 const int x8 = i8&1;
362                 const int y8 = i8>>1;
363
364                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
365                     continue;
366                 h->sub_mb_type[i8] = sub_mb_type;
367
368                 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, mv[0], 4);
369                 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, mv[1], 4);
370                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[0], 1);
371                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, (uint8_t)ref[1], 1);
372
373                 assert(b8_stride==2);
374                 /* col_zero_flag */
375                 if(!IS_INTRA(mb_type_col[0]) && !h->ref_list[1][0].long_ref && (   l1ref0[i8] == 0
376                                               || (l1ref0[i8] < 0 && l1ref1[i8] == 0
377                                                   && h->x264_build>33U))){
378                     const int16_t (*l1mv)[2]= l1ref0[i8] == 0 ? l1mv0 : l1mv1;
379                     if(IS_SUB_8X8(sub_mb_type)){
380                         const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
381                         if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
382                             if(ref[0] == 0)
383                                 fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
384                             if(ref[1] == 0)
385                                 fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
386                             n+=4;
387                         }
388                     }else{
389                         int m=0;
390                     for(i4=0; i4<4; i4++){
391                         const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
392                         if(FFABS(mv_col[0]) <= 1 && FFABS(mv_col[1]) <= 1){
393                             if(ref[0] == 0)
394                                 AV_ZERO32(h->mv_cache[0][scan8[i8*4+i4]]);
395                             if(ref[1] == 0)
396                                 AV_ZERO32(h->mv_cache[1][scan8[i8*4+i4]]);
397                             m++;
398                         }
399                     }
400                     if(!(m&3))
401                         h->sub_mb_type[i8]+= MB_TYPE_16x16 - MB_TYPE_8x8;
402                     n+=m;
403                     }
404                 }
405             }
406             if(!is_b8x8 && !(n&15))
407                 *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;
408         }
409 }
410
411 static void pred_temp_direct_motion(H264Context * const h, int *mb_type){
412     int b8_stride = 2;
413     int b4_stride = h->b_stride;
414     int mb_xy = h->mb_xy, mb_y = h->mb_y;
415     int mb_type_col[2];
416     const int16_t (*l1mv0)[2], (*l1mv1)[2];
417     const int8_t *l1ref0, *l1ref1;
418     const int is_b8x8 = IS_8X8(*mb_type);
419     unsigned int sub_mb_type;
420     int i8, i4;
421
422     assert(h->ref_list[1][0].reference & 3);
423
424     await_reference_mb_row(h, &h->ref_list[1][0], h->mb_y + !!IS_INTERLACED(*mb_type));
425
426     if (IS_INTERLACED(h->ref_list[1][0].mb_type[mb_xy])) { // AFL/AFR/FR/FL -> AFL/FL
427         if (!IS_INTERLACED(*mb_type)) {                          //     AFR/FR    -> AFL/FL
428             mb_y = (h->mb_y&~1) + h->col_parity;
429             mb_xy= h->mb_x + ((h->mb_y&~1) + h->col_parity)*h->mb_stride;
430             b8_stride = 0;
431         }else{
432             mb_y  += h->col_fieldoff;
433             mb_xy += h->mb_stride*h->col_fieldoff; // non zero for FL -> FL & differ parity
434         }
435         goto single_col;
436     }else{                                               // AFL/AFR/FR/FL -> AFR/FR
437         if(IS_INTERLACED(*mb_type)){                     // AFL       /FL -> AFR/FR
438             mb_y = h->mb_y&~1;
439             mb_xy= h->mb_x + (h->mb_y&~1)*h->mb_stride;
440             mb_type_col[0] = h->ref_list[1][0].mb_type[mb_xy];
441             mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy + h->mb_stride];
442             b8_stride = 2+4*h->mb_stride;
443             b4_stride *= 6;
444             if (IS_INTERLACED(mb_type_col[0]) != IS_INTERLACED(mb_type_col[1])) {
445                 mb_type_col[0] &= ~MB_TYPE_INTERLACED;
446                 mb_type_col[1] &= ~MB_TYPE_INTERLACED;
447             }
448
449             sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
450
451             if(    (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)
452                 && (mb_type_col[1] & MB_TYPE_16x16_OR_INTRA)
453                 && !is_b8x8){
454                 *mb_type   |= MB_TYPE_16x8 |MB_TYPE_L0L1|MB_TYPE_DIRECT2; /* B_16x8 */
455             }else{
456                 *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
457             }
458         }else{                                           //     AFR/FR    -> AFR/FR
459 single_col:
460             mb_type_col[0] =
461             mb_type_col[1] = h->ref_list[1][0].mb_type[mb_xy];
462
463             sub_mb_type = MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_8x8 */
464             if(!is_b8x8 && (mb_type_col[0] & MB_TYPE_16x16_OR_INTRA)){
465                 *mb_type   |= MB_TYPE_16x16|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_16x16 */
466             }else if(!is_b8x8 && (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16))){
467                 *mb_type   |= MB_TYPE_L0L1|MB_TYPE_DIRECT2 | (mb_type_col[0] & (MB_TYPE_16x8|MB_TYPE_8x16));
468             }else{
469                 if(!h->sps.direct_8x8_inference_flag){
470                     /* FIXME save sub mb types from previous frames (or derive from MVs)
471                     * so we know exactly what block size to use */
472                     sub_mb_type = MB_TYPE_8x8|MB_TYPE_P0L0|MB_TYPE_P0L1|MB_TYPE_DIRECT2; /* B_SUB_4x4 */
473                 }
474                 *mb_type   |= MB_TYPE_8x8|MB_TYPE_L0L1;
475             }
476         }
477     }
478
479     await_reference_mb_row(h, &h->ref_list[1][0], mb_y);
480
481     l1mv0  = (void*)&h->ref_list[1][0].motion_val[0][h->mb2b_xy [mb_xy]];
482     l1mv1  = (void*)&h->ref_list[1][0].motion_val[1][h->mb2b_xy [mb_xy]];
483     l1ref0 = &h->ref_list[1][0].ref_index [0][4 * mb_xy];
484     l1ref1 = &h->ref_list[1][0].ref_index [1][4 * mb_xy];
485     if(!b8_stride){
486         if(h->mb_y&1){
487             l1ref0 += 2;
488             l1ref1 += 2;
489             l1mv0  +=  2*b4_stride;
490             l1mv1  +=  2*b4_stride;
491         }
492     }
493
494     {
495         const int *map_col_to_list0[2] = {h->map_col_to_list0[0], h->map_col_to_list0[1]};
496         const int *dist_scale_factor = h->dist_scale_factor;
497         int ref_offset;
498
499         if (FRAME_MBAFF(h) && IS_INTERLACED(*mb_type)) {
500             map_col_to_list0[0] = h->map_col_to_list0_field[h->mb_y&1][0];
501             map_col_to_list0[1] = h->map_col_to_list0_field[h->mb_y&1][1];
502             dist_scale_factor   =h->dist_scale_factor_field[h->mb_y&1];
503         }
504         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
505
506         if(IS_INTERLACED(*mb_type) != IS_INTERLACED(mb_type_col[0])){
507             int y_shift  = 2*!IS_INTERLACED(*mb_type);
508             assert(h->sps.direct_8x8_inference_flag);
509
510             for(i8=0; i8<4; i8++){
511                 const int x8 = i8&1;
512                 const int y8 = i8>>1;
513                 int ref0, scale;
514                 const int16_t (*l1mv)[2]= l1mv0;
515
516                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
517                     continue;
518                 h->sub_mb_type[i8] = sub_mb_type;
519
520                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
521                 if(IS_INTRA(mb_type_col[y8])){
522                     fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
523                     fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
524                     fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
525                     continue;
526                 }
527
528                 ref0 = l1ref0[x8 + y8*b8_stride];
529                 if(ref0 >= 0)
530                     ref0 = map_col_to_list0[0][ref0 + ref_offset];
531                 else{
532                     ref0 = map_col_to_list0[1][l1ref1[x8 + y8*b8_stride] + ref_offset];
533                     l1mv= l1mv1;
534                 }
535                 scale = dist_scale_factor[ref0];
536                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
537
538                 {
539                     const int16_t *mv_col = l1mv[x8*3 + y8*b4_stride];
540                     int my_col = (mv_col[1]<<y_shift)/2;
541                     int mx = (scale * mv_col[0] + 128) >> 8;
542                     int my = (scale * my_col + 128) >> 8;
543                     fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
544                     fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-my_col), 4);
545                 }
546             }
547             return;
548         }
549
550         /* one-to-one mv scaling */
551
552         if(IS_16X16(*mb_type)){
553             int ref, mv0, mv1;
554
555             fill_rectangle(&h->ref_cache[1][scan8[0]], 4, 4, 8, 0, 1);
556             if(IS_INTRA(mb_type_col[0])){
557                 ref=mv0=mv1=0;
558             }else{
559                 const int ref0 = l1ref0[0] >= 0 ? map_col_to_list0[0][l1ref0[0] + ref_offset]
560                                                 : map_col_to_list0[1][l1ref1[0] + ref_offset];
561                 const int scale = dist_scale_factor[ref0];
562                 const int16_t *mv_col = l1ref0[0] >= 0 ? l1mv0[0] : l1mv1[0];
563                 int mv_l0[2];
564                 mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
565                 mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
566                 ref= ref0;
567                 mv0= pack16to32(mv_l0[0],mv_l0[1]);
568                 mv1= pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]);
569             }
570             fill_rectangle(&h->ref_cache[0][scan8[0]], 4, 4, 8, ref, 1);
571             fill_rectangle(&h-> mv_cache[0][scan8[0]], 4, 4, 8, mv0, 4);
572             fill_rectangle(&h-> mv_cache[1][scan8[0]], 4, 4, 8, mv1, 4);
573         }else{
574             for(i8=0; i8<4; i8++){
575                 const int x8 = i8&1;
576                 const int y8 = i8>>1;
577                 int ref0, scale;
578                 const int16_t (*l1mv)[2]= l1mv0;
579
580                 if(is_b8x8 && !IS_DIRECT(h->sub_mb_type[i8]))
581                     continue;
582                 h->sub_mb_type[i8] = sub_mb_type;
583                 fill_rectangle(&h->ref_cache[1][scan8[i8*4]], 2, 2, 8, 0, 1);
584                 if(IS_INTRA(mb_type_col[0])){
585                     fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, 0, 1);
586                     fill_rectangle(&h-> mv_cache[0][scan8[i8*4]], 2, 2, 8, 0, 4);
587                     fill_rectangle(&h-> mv_cache[1][scan8[i8*4]], 2, 2, 8, 0, 4);
588                     continue;
589                 }
590
591                 assert(b8_stride == 2);
592                 ref0 = l1ref0[i8];
593                 if(ref0 >= 0)
594                     ref0 = map_col_to_list0[0][ref0 + ref_offset];
595                 else{
596                     ref0 = map_col_to_list0[1][l1ref1[i8] + ref_offset];
597                     l1mv= l1mv1;
598                 }
599                 scale = dist_scale_factor[ref0];
600
601                 fill_rectangle(&h->ref_cache[0][scan8[i8*4]], 2, 2, 8, ref0, 1);
602                 if(IS_SUB_8X8(sub_mb_type)){
603                     const int16_t *mv_col = l1mv[x8*3 + y8*3*b4_stride];
604                     int mx = (scale * mv_col[0] + 128) >> 8;
605                     int my = (scale * mv_col[1] + 128) >> 8;
606                     fill_rectangle(&h->mv_cache[0][scan8[i8*4]], 2, 2, 8, pack16to32(mx,my), 4);
607                     fill_rectangle(&h->mv_cache[1][scan8[i8*4]], 2, 2, 8, pack16to32(mx-mv_col[0],my-mv_col[1]), 4);
608                 }else
609                 for(i4=0; i4<4; i4++){
610                     const int16_t *mv_col = l1mv[x8*2 + (i4&1) + (y8*2 + (i4>>1))*b4_stride];
611                     int16_t *mv_l0 = h->mv_cache[0][scan8[i8*4+i4]];
612                     mv_l0[0] = (scale * mv_col[0] + 128) >> 8;
613                     mv_l0[1] = (scale * mv_col[1] + 128) >> 8;
614                     AV_WN32A(h->mv_cache[1][scan8[i8*4+i4]],
615                         pack16to32(mv_l0[0]-mv_col[0],mv_l0[1]-mv_col[1]));
616                 }
617             }
618         }
619     }
620 }
621
622 void ff_h264_pred_direct_motion(H264Context * const h, int *mb_type){
623     if(h->direct_spatial_mv_pred){
624         pred_spatial_direct_motion(h, mb_type);
625     }else{
626         pred_temp_direct_motion(h, mb_type);
627     }
628 }