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