Coverage Report

Created: 2026-09-14 08:00

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