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