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