GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/h264_mvpred.h Lines: 413 417 99.0 %
Date: 2021-04-20 15:25:36 Branches: 347 356 97.5 %

Line Branch Exec Source
1
/*
2
 * H.26L/H.264/AVC/JVT/14496-10/... motion vector prediction
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 motion vector prediction.
25
 * @author Michael Niedermayer <michaelni@gmx.at>
26
 */
27
28
#ifndef AVCODEC_H264_MVPRED_H
29
#define AVCODEC_H264_MVPRED_H
30
31
#include "internal.h"
32
#include "avcodec.h"
33
#include "h264dec.h"
34
#include "mpegutils.h"
35
#include "libavutil/avassert.h"
36
#include "libavutil/mem_internal.h"
37
38
39
17840472
static av_always_inline int fetch_diagonal_mv(const H264Context *h, H264SliceContext *sl,
40
                                              const int16_t **C,
41
                                              int i, int list, int part_width)
42
{
43
17840472
    const int topright_ref = sl->ref_cache[list][i - 8 + part_width];
44
45
    /* there is no consistent mapping of mvs to neighboring locations that will
46
     * make mbaff happy, so we can't move all this logic to fill_caches */
47
17840472
    if (FRAME_MBAFF(h)) {
48
#define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4)                              \
49
        const int xy = XY, y4 = Y4;                                     \
50
        const int mb_type = mb_types[xy + (y4 >> 2) * h->mb_stride];    \
51
        if (!USES_LIST(mb_type, list))                                  \
52
            return LIST_NOT_USED;                                       \
53
        mv = h->cur_pic_ptr->motion_val[list][h->mb2b_xy[xy] + 3 + y4 * h->b_stride]; \
54
        sl->mv_cache[list][scan8[0] - 2][0] = mv[0];                     \
55
        sl->mv_cache[list][scan8[0] - 2][1] = mv[1] MV_OP;               \
56
        return h->cur_pic_ptr->ref_index[list][4 * xy + 1 + (y4 & ~1)] REF_OP;
57
58
2891570
        if (topright_ref == PART_NOT_AVAILABLE
59

1265392
            && i >= scan8[0] + 8 && (i & 7) == 4
60
196840
            && sl->ref_cache[list][scan8[0] - 1] != PART_NOT_AVAILABLE) {
61
188678
            const uint32_t *mb_types = h->cur_pic_ptr->mb_type;
62
            const int16_t *mv;
63
188678
            AV_ZERO32(sl->mv_cache[list][scan8[0] - 2]);
64
188678
            *C = sl->mv_cache[list][scan8[0] - 2];
65
66

188678
            if (!MB_FIELD(sl) && IS_INTERLACED(sl->left_type[0])) {
67
26640
                SET_DIAG_MV(* 2, >> 1, sl->left_mb_xy[0] + h->mb_stride,
68
                            (sl->mb_y & 1) * 2 + (i >> 5));
69
            }
70

162038
            if (MB_FIELD(sl) && !IS_INTERLACED(sl->left_type[0])) {
71
                // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
72
35582
                SET_DIAG_MV(/ 2, *2, sl->left_mb_xy[i >= 36], ((i >> 2)) & 3);
73
            }
74
        }
75
#undef SET_DIAG_MV
76
    }
77
78
17778250
    if (topright_ref != PART_NOT_AVAILABLE) {
79
11240252
        *C = sl->mv_cache[list][i - 8 + part_width];
80
11240252
        return topright_ref;
81
    } else {
82
        ff_tlog(h->avctx, "topright MV not available\n");
83
84
6537998
        *C = sl->mv_cache[list][i - 8 - 1];
85
6537998
        return sl->ref_cache[list][i - 8 - 1];
86
    }
87
}
88
89
/**
90
 * Get the predicted MV.
91
 * @param n the block index
92
 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
93
 * @param mx the x component of the predicted motion vector
94
 * @param my the y component of the predicted motion vector
95
 */
96
17248106
static av_always_inline void pred_motion(const H264Context *const h,
97
                                         H264SliceContext *sl,
98
                                         int n,
99
                                         int part_width, int list, int ref,
100
                                         int *const mx, int *const my)
101
{
102
17248106
    const int index8       = scan8[n];
103
17248106
    const int top_ref      = sl->ref_cache[list][index8 - 8];
104
17248106
    const int left_ref     = sl->ref_cache[list][index8 - 1];
105
17248106
    const int16_t *const A = sl->mv_cache[list][index8 - 1];
106
17248106
    const int16_t *const B = sl->mv_cache[list][index8 - 8];
107
    const int16_t *C;
108
    int diagonal_ref, match_count;
109
110
    av_assert2(part_width == 1 || part_width == 2 || part_width == 4);
111
112
/* mv_cache
113
 * B . . A T T T T
114
 * U . . L . . , .
115
 * U . . L . . . .
116
 * U . . L . . , .
117
 * . . . L . . . .
118
 */
119
120
17248106
    diagonal_ref = fetch_diagonal_mv(h, sl, &C, index8, list, part_width);
121
17248106
    match_count  = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
122
    ff_tlog(h->avctx, "pred_motion match_count=%d\n", match_count);
123
17248106
    if (match_count > 1) { //most common
124
12772869
        *mx = mid_pred(A[0], B[0], C[0]);
125
12772869
        *my = mid_pred(A[1], B[1], C[1]);
126
4475237
    } else if (match_count == 1) {
127
3042116
        if (left_ref == ref) {
128
1916457
            *mx = A[0];
129
1916457
            *my = A[1];
130
1125659
        } else if (top_ref == ref) {
131
809106
            *mx = B[0];
132
809106
            *my = B[1];
133
        } else {
134
316553
            *mx = C[0];
135
316553
            *my = C[1];
136
        }
137
    } else {
138

1433121
        if (top_ref      == PART_NOT_AVAILABLE &&
139
233058
            diagonal_ref == PART_NOT_AVAILABLE &&
140
            left_ref     != PART_NOT_AVAILABLE) {
141
208011
            *mx = A[0];
142
208011
            *my = A[1];
143
        } else {
144
1225110
            *mx = mid_pred(A[0], B[0], C[0]);
145
1225110
            *my = mid_pred(A[1], B[1], C[1]);
146
        }
147
    }
148
149
    ff_tlog(h->avctx,
150
            "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n",
151
            top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref,
152
            A[0], A[1], ref, *mx, *my, sl->mb_x, sl->mb_y, n, list);
153
17248106
}
154
155
/**
156
 * Get the directionally predicted 16x8 MV.
157
 * @param n the block index
158
 * @param mx the x component of the predicted motion vector
159
 * @param my the y component of the predicted motion vector
160
 */
161
1341285
static av_always_inline void pred_16x8_motion(const H264Context *const h,
162
                                              H264SliceContext *sl,
163
                                              int n, int list, int ref,
164
                                              int *const mx, int *const my)
165
{
166
1341285
    if (n == 0) {
167
669802
        const int top_ref      = sl->ref_cache[list][scan8[0] - 8];
168
669802
        const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
169
170
        ff_tlog(h->avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
171
                top_ref, B[0], B[1], sl->mb_x, sl->mb_y, n, list);
172
173
669802
        if (top_ref == ref) {
174
378948
            *mx = B[0];
175
378948
            *my = B[1];
176
378948
            return;
177
        }
178
    } else {
179
671483
        const int left_ref     = sl->ref_cache[list][scan8[8] - 1];
180
671483
        const int16_t *const A = sl->mv_cache[list][scan8[8] - 1];
181
182
        ff_tlog(h->avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
183
                left_ref, A[0], A[1], sl->mb_x, sl->mb_y, n, list);
184
185
671483
        if (left_ref == ref) {
186
449579
            *mx = A[0];
187
449579
            *my = A[1];
188
449579
            return;
189
        }
190
    }
191
192
    //RARE
193
512758
    pred_motion(h, sl, n, 4, list, ref, mx, my);
194
}
195
196
/**
197
 * Get the directionally predicted 8x16 MV.
198
 * @param n the block index
199
 * @param mx the x component of the predicted motion vector
200
 * @param my the y component of the predicted motion vector
201
 */
202
1186049
static av_always_inline void pred_8x16_motion(const H264Context *const h,
203
                                              H264SliceContext *sl,
204
                                              int n, int list, int ref,
205
                                              int *const mx, int *const my)
206
{
207
1186049
    if (n == 0) {
208
593683
        const int left_ref     = sl->ref_cache[list][scan8[0] - 1];
209
593683
        const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
210
211
        ff_tlog(h->avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
212
                left_ref, A[0], A[1], sl->mb_x, sl->mb_y, n, list);
213
214
593683
        if (left_ref == ref) {
215
369982
            *mx = A[0];
216
369982
            *my = A[1];
217
369982
            return;
218
        }
219
    } else {
220
        const int16_t *C;
221
        int diagonal_ref;
222
223
592366
        diagonal_ref = fetch_diagonal_mv(h, sl, &C, scan8[4], list, 2);
224
225
        ff_tlog(h->avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
226
                diagonal_ref, C[0], C[1], sl->mb_x, sl->mb_y, n, list);
227
228
592366
        if (diagonal_ref == ref) {
229
337481
            *mx = C[0];
230
337481
            *my = C[1];
231
337481
            return;
232
        }
233
    }
234
235
    //RARE
236
478586
    pred_motion(h, sl, n, 2, list, ref, mx, my);
237
}
238
239
#define FIX_MV_MBAFF(type, refn, mvn, idx)      \
240
    if (FRAME_MBAFF(h)) {                       \
241
        if (MB_FIELD(sl)) {                     \
242
            if (!IS_INTERLACED(type)) {         \
243
                refn <<= 1;                     \
244
                AV_COPY32(mvbuf[idx], mvn);     \
245
                mvbuf[idx][1] /= 2;             \
246
                mvn = mvbuf[idx];               \
247
            }                                   \
248
        } else {                                \
249
            if (IS_INTERLACED(type)) {          \
250
                refn >>= 1;                     \
251
                AV_COPY32(mvbuf[idx], mvn);     \
252
                mvbuf[idx][1] *= 2;             \
253
                mvn = mvbuf[idx];               \
254
            }                                   \
255
        }                                       \
256
    }
257
258
1402514
static av_always_inline void pred_pskip_motion(const H264Context *const h,
259
                                               H264SliceContext *sl)
260
{
261
    DECLARE_ALIGNED(4, static const int16_t, zeromv)[2] = { 0 };
262
    DECLARE_ALIGNED(4, int16_t, mvbuf)[3][2];
263
1402514
    int8_t *ref     = h->cur_pic.ref_index[0];
264
1402514
    int16_t(*mv)[2] = h->cur_pic.motion_val[0];
265
    int top_ref, left_ref, diagonal_ref, match_count, mx, my;
266
    const int16_t *A, *B, *C;
267
1402514
    int b_stride = h->b_stride;
268
269
1402514
    fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
270
271
    /* To avoid doing an entire fill_decode_caches, we inline the relevant
272
     * parts here.
273
     * FIXME: this is a partial duplicate of the logic in fill_decode_caches,
274
     * but it's faster this way.  Is there a way to avoid this duplication?
275
     */
276
1402514
    if (USES_LIST(sl->left_type[LTOP], 0)) {
277
1324843
        left_ref = ref[4 * sl->left_mb_xy[LTOP] + 1 + (sl->left_block[0] & ~1)];
278
1324843
        A = mv[h->mb2b_xy[sl->left_mb_xy[LTOP]] + 3 + b_stride * sl->left_block[0]];
279


1324843
        FIX_MV_MBAFF(sl->left_type[LTOP], left_ref, A, 0);
280
1324843
        if (!(left_ref | AV_RN32A(A)))
281
1047224
            goto zeromv;
282
77671
    } else if (sl->left_type[LTOP]) {
283
31577
        left_ref = LIST_NOT_USED;
284
31577
        A        = zeromv;
285
    } else {
286
46094
        goto zeromv;
287
    }
288
289
309196
    if (USES_LIST(sl->top_type, 0)) {
290
280294
        top_ref = ref[4 * sl->top_mb_xy + 2];
291
280294
        B       = mv[h->mb2b_xy[sl->top_mb_xy] + 3 * b_stride];
292


280294
        FIX_MV_MBAFF(sl->top_type, top_ref, B, 1);
293
280294
        if (!(top_ref | AV_RN32A(B)))
294
46616
            goto zeromv;
295
28902
    } else if (sl->top_type) {
296
19074
        top_ref = LIST_NOT_USED;
297
19074
        B       = zeromv;
298
    } else {
299
9828
        goto zeromv;
300
    }
301
302
    ff_tlog(h->avctx, "pred_pskip: (%d) (%d) at %2d %2d\n",
303
            top_ref, left_ref, sl->mb_x, sl->mb_y);
304
305
252752
    if (USES_LIST(sl->topright_type, 0)) {
306
214125
        diagonal_ref = ref[4 * sl->topright_mb_xy + 2];
307
214125
        C = mv[h->mb2b_xy[sl->topright_mb_xy] + 3 * b_stride];
308


214125
        FIX_MV_MBAFF(sl->topright_type, diagonal_ref, C, 2);
309
38627
    } else if (sl->topright_type) {
310
18789
        diagonal_ref = LIST_NOT_USED;
311
18789
        C = zeromv;
312
    } else {
313
19838
        if (USES_LIST(sl->topleft_type, 0)) {
314
18855
            diagonal_ref = ref[4 * sl->topleft_mb_xy + 1 +
315
18855
                               (sl->topleft_partition & 2)];
316
18855
            C = mv[h->mb2b_xy[sl->topleft_mb_xy] + 3 + b_stride +
317
18855
                   (sl->topleft_partition & 2 * b_stride)];
318


18855
            FIX_MV_MBAFF(sl->topleft_type, diagonal_ref, C, 2);
319
983
        } else if (sl->topleft_type) {
320
983
            diagonal_ref = LIST_NOT_USED;
321
983
            C            = zeromv;
322
        } else {
323
            diagonal_ref = PART_NOT_AVAILABLE;
324
            C            = zeromv;
325
        }
326
    }
327
328
252752
    match_count = !diagonal_ref + !top_ref + !left_ref;
329
    ff_tlog(h->avctx, "pred_pskip_motion match_count=%d\n", match_count);
330
252752
    if (match_count > 1) {
331
210473
        mx = mid_pred(A[0], B[0], C[0]);
332
210473
        my = mid_pred(A[1], B[1], C[1]);
333
42279
    } else if (match_count == 1) {
334
36183
        if (!left_ref) {
335
17568
            mx = A[0];
336
17568
            my = A[1];
337
18615
        } else if (!top_ref) {
338
8127
            mx = B[0];
339
8127
            my = B[1];
340
        } else {
341
10488
            mx = C[0];
342
10488
            my = C[1];
343
        }
344
    } else {
345
6096
        mx = mid_pred(A[0], B[0], C[0]);
346
6096
        my = mid_pred(A[1], B[1], C[1]);
347
    }
348
349
252752
    fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx, my), 4);
350
252752
    return;
351
352
1149762
zeromv:
353
1149762
    fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
354
1149762
    return;
355
}
356
357
13542630
static void fill_decode_neighbors(const H264Context *h, H264SliceContext *sl, int mb_type)
358
{
359
13542630
    const int mb_xy = sl->mb_xy;
360
    int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
361
    static const uint8_t left_block_options[4][32] = {
362
        { 0, 1, 2, 3, 7, 10, 8, 11, 3 + 0 * 4, 3 + 1 * 4, 3 + 2 * 4, 3 + 3 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 5 * 4, 1 + 9 * 4 },
363
        { 2, 2, 3, 3, 8, 11, 8, 11, 3 + 2 * 4, 3 + 2 * 4, 3 + 3 * 4, 3 + 3 * 4, 1 + 5 * 4, 1 + 9 * 4, 1 + 5 * 4, 1 + 9 * 4 },
364
        { 0, 0, 1, 1, 7, 10, 7, 10, 3 + 0 * 4, 3 + 0 * 4, 3 + 1 * 4, 3 + 1 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 },
365
        { 0, 2, 0, 2, 7, 10, 7, 10, 3 + 0 * 4, 3 + 2 * 4, 3 + 0 * 4, 3 + 2 * 4, 1 + 4 * 4, 1 + 8 * 4, 1 + 4 * 4, 1 + 8 * 4 }
366
    };
367
368
13542630
    sl->topleft_partition = -1;
369
370
13542630
    top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
371
372
    /* Wow, what a mess, why didn't they simplify the interlacing & intra
373
     * stuff, I can't imagine that these complex rules are worth it. */
374
375
13542630
    topleft_xy    = top_xy - 1;
376
13542630
    topright_xy   = top_xy + 1;
377
13542630
    left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
378
13542630
    sl->left_block = left_block_options[0];
379
13542630
    if (FRAME_MBAFF(h)) {
380
2095408
        const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
381
2095408
        const int curr_mb_field_flag = IS_INTERLACED(mb_type);
382
2095408
        if (sl->mb_y & 1) {
383
1047592
            if (left_mb_field_flag != curr_mb_field_flag) {
384
247902
                left_xy[LBOT] = left_xy[LTOP] = mb_xy - h->mb_stride - 1;
385
247902
                if (curr_mb_field_flag) {
386
127965
                    left_xy[LBOT] += h->mb_stride;
387
127965
                    sl->left_block  = left_block_options[3];
388
                } else {
389
119937
                    topleft_xy += h->mb_stride;
390
                    /* take top left mv from the middle of the mb, as opposed
391
                     * to all other modes which use the bottom right partition */
392
119937
                    sl->topleft_partition = 0;
393
119937
                    sl->left_block        = left_block_options[1];
394
                }
395
            }
396
        } else {
397
1047816
            if (curr_mb_field_flag) {
398
305129
                topleft_xy  += h->mb_stride & (((h->cur_pic.mb_type[top_xy - 1] >> 7) & 1) - 1);
399
305129
                topright_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy + 1] >> 7) & 1) - 1);
400
305129
                top_xy      += h->mb_stride & (((h->cur_pic.mb_type[top_xy]     >> 7) & 1) - 1);
401
            }
402
1047816
            if (left_mb_field_flag != curr_mb_field_flag) {
403
248076
                if (curr_mb_field_flag) {
404
127938
                    left_xy[LBOT] += h->mb_stride;
405
127938
                    sl->left_block  = left_block_options[3];
406
                } else {
407
120138
                    sl->left_block = left_block_options[2];
408
                }
409
            }
410
        }
411
    }
412
413
13542630
    sl->topleft_mb_xy    = topleft_xy;
414
13542630
    sl->top_mb_xy        = top_xy;
415
13542630
    sl->topright_mb_xy   = topright_xy;
416
13542630
    sl->left_mb_xy[LTOP] = left_xy[LTOP];
417
13542630
    sl->left_mb_xy[LBOT] = left_xy[LBOT];
418
    //FIXME do we need all in the context?
419
420
13542630
    sl->topleft_type    = h->cur_pic.mb_type[topleft_xy];
421
13542630
    sl->top_type        = h->cur_pic.mb_type[top_xy];
422
13542630
    sl->topright_type   = h->cur_pic.mb_type[topright_xy];
423
13542630
    sl->left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
424
13542630
    sl->left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
425
426
    if (FMO) {
427
        if (h->slice_table[topleft_xy] != sl->slice_num)
428
            sl->topleft_type = 0;
429
        if (h->slice_table[top_xy] != sl->slice_num)
430
            sl->top_type = 0;
431
        if (h->slice_table[left_xy[LTOP]] != sl->slice_num)
432
            sl->left_type[LTOP] = sl->left_type[LBOT] = 0;
433
    } else {
434
13542630
        if (h->slice_table[topleft_xy] != sl->slice_num) {
435
1350913
            sl->topleft_type = 0;
436
1350913
            if (h->slice_table[top_xy] != sl->slice_num)
437
1004667
                sl->top_type = 0;
438
1350913
            if (h->slice_table[left_xy[LTOP]] != sl->slice_num)
439
380191
                sl->left_type[LTOP] = sl->left_type[LBOT] = 0;
440
        }
441
    }
442
13542630
    if (h->slice_table[topright_xy] != sl->slice_num)
443
2072581
        sl->topright_type = 0;
444
13542630
}
445
446
12134457
static void fill_decode_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
447
{
448
    int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
449
    int topleft_type, top_type, topright_type, left_type[LEFT_MBS];
450
12134457
    const uint8_t *left_block = sl->left_block;
451
    int i;
452
    uint8_t *nnz;
453
    uint8_t *nnz_cache;
454
455
12134457
    topleft_xy      = sl->topleft_mb_xy;
456
12134457
    top_xy          = sl->top_mb_xy;
457
12134457
    topright_xy     = sl->topright_mb_xy;
458
12134457
    left_xy[LTOP]   = sl->left_mb_xy[LTOP];
459
12134457
    left_xy[LBOT]   = sl->left_mb_xy[LBOT];
460
12134457
    topleft_type    = sl->topleft_type;
461
12134457
    top_type        = sl->top_type;
462
12134457
    topright_type   = sl->topright_type;
463
12134457
    left_type[LTOP] = sl->left_type[LTOP];
464
12134457
    left_type[LBOT] = sl->left_type[LBOT];
465
466
12134457
    if (!IS_SKIP(mb_type)) {
467
10137018
        if (IS_INTRA(mb_type)) {
468
3811333
            int type_mask = h->ps.pps->constrained_intra_pred ? IS_INTRA(-1) : -1;
469
3811333
            sl->topleft_samples_available     =
470
3811333
                sl->top_samples_available     =
471
3811333
                    sl->left_samples_available = 0xFFFF;
472
3811333
            sl->topright_samples_available     = 0xEEEA;
473
474
3811333
            if (!(top_type & type_mask)) {
475
270267
                sl->topleft_samples_available  = 0xB3FF;
476
270267
                sl->top_samples_available      = 0x33FF;
477
270267
                sl->topright_samples_available = 0x26EA;
478
            }
479
3811333
            if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {
480
212225
                if (IS_INTERLACED(mb_type)) {
481
110678
                    if (!(left_type[LTOP] & type_mask)) {
482
15866
                        sl->topleft_samples_available &= 0xDFFF;
483
15866
                        sl->left_samples_available    &= 0x5FFF;
484
                    }
485
110678
                    if (!(left_type[LBOT] & type_mask)) {
486
15871
                        sl->topleft_samples_available &= 0xFF5F;
487
15871
                        sl->left_samples_available    &= 0xFF5F;
488
                    }
489
                } else {
490
101547
                    int left_typei = h->cur_pic.mb_type[left_xy[LTOP] + h->mb_stride];
491
492
                    av_assert2(left_xy[LTOP] == left_xy[LBOT]);
493

101547
                    if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
494
53
                        sl->topleft_samples_available &= 0xDF5F;
495
53
                        sl->left_samples_available    &= 0x5F5F;
496
                    }
497
                }
498
            } else {
499
3599108
                if (!(left_type[LTOP] & type_mask)) {
500
64707
                    sl->topleft_samples_available &= 0xDF5F;
501
64707
                    sl->left_samples_available    &= 0x5F5F;
502
                }
503
            }
504
505
3811333
            if (!(topleft_type & type_mask))
506
342163
                sl->topleft_samples_available &= 0x7FFF;
507
508
3811333
            if (!(topright_type & type_mask))
509
708509
                sl->topright_samples_available &= 0xFBFF;
510
511
3811333
            if (IS_INTRA4x4(mb_type)) {
512
2956505
                if (IS_INTRA4x4(top_type)) {
513
2224286
                    AV_COPY32(sl->intra4x4_pred_mode_cache + 4 + 8 * 0, sl->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
514
                } else {
515
732219
                    sl->intra4x4_pred_mode_cache[4 + 8 * 0] =
516
732219
                    sl->intra4x4_pred_mode_cache[5 + 8 * 0] =
517
732219
                    sl->intra4x4_pred_mode_cache[6 + 8 * 0] =
518
732219
                    sl->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);
519
                }
520
8869515
                for (i = 0; i < 2; i++) {
521
5913010
                    if (IS_INTRA4x4(left_type[LEFT(i)])) {
522
4761238
                        int8_t *mode = sl->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
523
4761238
                        sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];
524
4761238
                        sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];
525
                    } else {
526
1151772
                        sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =
527
1151772
                        sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);
528
                    }
529
                }
530
            }
531
        }
532
533
        /*
534
         * 0 . T T. T T T T
535
         * 1 L . .L . . . .
536
         * 2 L . .L . . . .
537
         * 3 . T TL . . . .
538
         * 4 L . .L . . . .
539
         * 5 L . .. . . . .
540
         */
541
        /* FIXME: constraint_intra_pred & partitioning & nnz
542
         * (let us hope this is just a typo in the spec) */
543
10137018
        nnz_cache = sl->non_zero_count_cache;
544
10137018
        if (top_type) {
545
9324481
            nnz = h->non_zero_count[top_xy];
546
9324481
            AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
547
9324481
            if (!h->chroma_y_shift) {
548
894974
                AV_COPY32(&nnz_cache[4 + 8 *  5], &nnz[4 *  7]);
549
894974
                AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
550
            } else {
551
8429507
                AV_COPY32(&nnz_cache[4 + 8 *  5], &nnz[4 * 5]);
552
8429507
                AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
553
            }
554
        } else {
555

812537
            uint32_t top_empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 0x40404040;
556
812537
            AV_WN32A(&nnz_cache[4 + 8 *  0], top_empty);
557
812537
            AV_WN32A(&nnz_cache[4 + 8 *  5], top_empty);
558
812537
            AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
559
        }
560
561
30411054
        for (i = 0; i < 2; i++) {
562
20274036
            if (left_type[LEFT(i)]) {
563
19680992
                nnz = h->non_zero_count[left_xy[LEFT(i)]];
564
19680992
                nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
565
19680992
                nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
566
19680992
                if (CHROMA444(h)) {
567
271160
                    nnz_cache[3 + 8 *  6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
568
271160
                    nnz_cache[3 + 8 *  7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
569
271160
                    nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
570
271160
                    nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
571
19409832
                } else if (CHROMA422(h)) {
572
1619188
                    nnz_cache[3 + 8 *  6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
573
1619188
                    nnz_cache[3 + 8 *  7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
574
1619188
                    nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
575
1619188
                    nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];
576
                } else {
577
17790644
                    nnz_cache[3 + 8 *  6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];
578
17790644
                    nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];
579
                }
580
            } else {
581
593044
                nnz_cache[3 + 8 *  1 + 2 * 8 * i] =
582
593044
                nnz_cache[3 + 8 *  2 + 2 * 8 * i] =
583
593044
                nnz_cache[3 + 8 *  6 + 2 * 8 * i] =
584
593044
                nnz_cache[3 + 8 *  7 + 2 * 8 * i] =
585
593044
                nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
586

593044
                nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 64;
587
            }
588
        }
589
590
        if (CABAC(h)) {
591
            // top_cbp
592
6663328
            if (top_type)
593
6193705
                sl->top_cbp = h->cbp_table[top_xy];
594
            else
595
469623
                sl->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
596
            // left_cbp
597
6663328
            if (left_type[LTOP]) {
598
6493655
                sl->left_cbp =   (h->cbp_table[left_xy[LTOP]] & 0x7F0) |
599
6493655
                               ((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |
600
6493655
                              (((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
601
            } else {
602
169673
                sl->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
603
            }
604
        }
605
    }
606
607

12134457
    if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && sl->direct_spatial_mv_pred)) {
608
        int list;
609
8259164
        int b_stride = h->b_stride;
610
21415408
        for (list = 0; list < sl->list_count; list++) {
611
13156244
            int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
612
13156244
            int8_t *ref       = h->cur_pic.ref_index[list];
613
13156244
            int16_t(*mv_cache)[2] = &sl->mv_cache[list][scan8[0]];
614
13156244
            int16_t(*mv)[2]       = h->cur_pic.motion_val[list];
615
13156244
            if (!USES_LIST(mb_type, list))
616
1251861
                continue;
617
            av_assert2(!(IS_DIRECT(mb_type) && !sl->direct_spatial_mv_pred));
618
619
11904383
            if (USES_LIST(top_type, list)) {
620
9659597
                const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
621
9659597
                AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);
622
9659597
                ref_cache[0 - 1 * 8] =
623
9659597
                ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];
624
9659597
                ref_cache[2 - 1 * 8] =
625
9659597
                ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];
626
            } else {
627
2244786
                AV_ZERO128(mv_cache[0 - 1 * 8]);
628
2244786
                AV_WN32A(&ref_cache[0 - 1 * 8],
629
                         ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);
630
            }
631
632
11904383
            if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {
633
8162940
                for (i = 0; i < 2; i++) {
634
5441960
                    int cache_idx = -1 + i * 2 * 8;
635
5441960
                    if (USES_LIST(left_type[LEFT(i)], list)) {
636
4653384
                        const int b_xy  = h->mb2b_xy[left_xy[LEFT(i)]] + 3;
637
4653384
                        const int b8_xy = 4 * left_xy[LEFT(i)] + 1;
638
4653384
                        AV_COPY32(mv_cache[cache_idx],
639
                                  mv[b_xy + b_stride * left_block[0 + i * 2]]);
640
4653384
                        AV_COPY32(mv_cache[cache_idx + 8],
641
                                  mv[b_xy + b_stride * left_block[1 + i * 2]]);
642
4653384
                        ref_cache[cache_idx]     = ref[b8_xy + (left_block[0 + i * 2] & ~1)];
643
4653384
                        ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];
644
                    } else {
645
788576
                        AV_ZERO32(mv_cache[cache_idx]);
646
788576
                        AV_ZERO32(mv_cache[cache_idx + 8]);
647
788576
                        ref_cache[cache_idx]     =
648
788576
                        ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED
649
                                                                        : PART_NOT_AVAILABLE;
650
                    }
651
                }
652
            } else {
653
9183403
                if (USES_LIST(left_type[LTOP], list)) {
654
7908410
                    const int b_xy  = h->mb2b_xy[left_xy[LTOP]] + 3;
655
7908410
                    const int b8_xy = 4 * left_xy[LTOP] + 1;
656
7908410
                    AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);
657
7908410
                    ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];
658
                } else {
659
1274993
                    AV_ZERO32(mv_cache[-1]);
660
1274993
                    ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED
661
                                                    : PART_NOT_AVAILABLE;
662
                }
663
            }
664
665
11904383
            if (USES_LIST(topright_type, list)) {
666
8900972
                const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;
667
8900972
                AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);
668
8900972
                ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];
669
            } else {
670
3003411
                AV_ZERO32(mv_cache[4 - 1 * 8]);
671
3003411
                ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED
672
                                                     : PART_NOT_AVAILABLE;
673
            }
674

11904383
            if(ref_cache[2 - 1*8] < 0 || ref_cache[4 - 1 * 8] < 0) {
675
3667973
                if (USES_LIST(topleft_type, list)) {
676
1674650
                    const int b_xy  = h->mb2b_xy[topleft_xy] + 3 + b_stride +
677
1674650
                                      (sl->topleft_partition & 2 * b_stride);
678
1674650
                    const int b8_xy = 4 * topleft_xy + 1 + (sl->topleft_partition & 2);
679
1674650
                    AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);
680
1674650
                    ref_cache[-1 - 1 * 8] = ref[b8_xy];
681
                } else {
682
1993323
                    AV_ZERO32(mv_cache[-1 - 1 * 8]);
683
1993323
                    ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED
684
                                                         : PART_NOT_AVAILABLE;
685
                }
686
            }
687
688

11904383
            if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF(h))
689
4556736
                continue;
690
691
7347647
            if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
692
6843031
                uint8_t(*mvd_cache)[2]   = &sl->mvd_cache[list][scan8[0]];
693
6843031
                uint8_t(*mvd)[2]         = sl->mvd_table[list];
694
6843031
                ref_cache[2 + 8 * 0] =
695
6843031
                ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;
696
6843031
                AV_ZERO32(mv_cache[2 + 8 * 0]);
697
6843031
                AV_ZERO32(mv_cache[2 + 8 * 2]);
698
699
                if (CABAC(h)) {
700
4530832
                    if (USES_LIST(top_type, list)) {
701
3620860
                        const int b_xy = h->mb2br_xy[top_xy];
702
3620860
                        AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
703
                    } else {
704
909972
                        AV_ZERO64(mvd_cache[0 - 1 * 8]);
705
                    }
706
4530832
                    if (USES_LIST(left_type[LTOP], list)) {
707
3837224
                        const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;
708
3837224
                        AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
709
3837224
                        AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
710
                    } else {
711
693608
                        AV_ZERO16(mvd_cache[-1 + 0 * 8]);
712
693608
                        AV_ZERO16(mvd_cache[-1 + 1 * 8]);
713
                    }
714
4530832
                    if (USES_LIST(left_type[LBOT], list)) {
715
3837024
                        const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;
716
3837024
                        AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
717
3837024
                        AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
718
                    } else {
719
693808
                        AV_ZERO16(mvd_cache[-1 + 2 * 8]);
720
693808
                        AV_ZERO16(mvd_cache[-1 + 3 * 8]);
721
                    }
722
4530832
                    AV_ZERO16(mvd_cache[2 + 8 * 0]);
723
4530832
                    AV_ZERO16(mvd_cache[2 + 8 * 2]);
724
4530832
                    if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
725
2536394
                        uint8_t *direct_cache = &sl->direct_cache[scan8[0]];
726
2536394
                        uint8_t *direct_table = h->direct_table;
727
2536394
                        fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);
728
729
2536394
                        if (IS_DIRECT(top_type)) {
730
395572
                            AV_WN32A(&direct_cache[-1 * 8],
731
                                     0x01010101u * (MB_TYPE_DIRECT2 >> 1));
732
2140822
                        } else if (IS_8X8(top_type)) {
733
610350
                            int b8_xy = 4 * top_xy;
734
610350
                            direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
735
610350
                            direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
736
                        } else {
737
1530472
                            AV_WN32A(&direct_cache[-1 * 8],
738
                                     0x01010101 * (MB_TYPE_16x16 >> 1));
739
                        }
740
741
2536394
                        if (IS_DIRECT(left_type[LTOP]))
742
390852
                            direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;
743
2145542
                        else if (IS_8X8(left_type[LTOP]))
744
645309
                            direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];
745
                        else
746
1500233
                            direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;
747
748
2536394
                        if (IS_DIRECT(left_type[LBOT]))
749
389967
                            direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;
750
2146427
                        else if (IS_8X8(left_type[LBOT]))
751
647398
                            direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
752
                        else
753
1499029
                            direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;
754
                    }
755
                }
756
            }
757
758
#define MAP_MVS                                                         \
759
    MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type)                         \
760
    MAP_F2F(scan8[0] + 0 - 1 * 8, top_type)                             \
761
    MAP_F2F(scan8[0] + 1 - 1 * 8, top_type)                             \
762
    MAP_F2F(scan8[0] + 2 - 1 * 8, top_type)                             \
763
    MAP_F2F(scan8[0] + 3 - 1 * 8, top_type)                             \
764
    MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type)                        \
765
    MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP])                      \
766
    MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP])                      \
767
    MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT])                      \
768
    MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
769
770
7347647
            if (FRAME_MBAFF(h)) {
771
1583802
                if (MB_FIELD(sl)) {
772
773
#define MAP_F2F(idx, mb_type)                                           \
774
    if (!IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) {     \
775
        sl->ref_cache[list][idx]     *= 2;                              \
776
        sl->mv_cache[list][idx][1]   /= 2;                              \
777
        sl->mvd_cache[list][idx][1] >>= 1;                              \
778
    }
779
780










543998
                    MAP_MVS
781
                } else {
782
783
#undef MAP_F2F
784
#define MAP_F2F(idx, mb_type)                                           \
785
    if (IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) {      \
786
        sl->ref_cache[list][idx]    >>= 1;                              \
787
        sl->mv_cache[list][idx][1]   *= 2;                              \
788
        sl->mvd_cache[list][idx][1] <<= 1;                              \
789
    }
790
791










1039804
                    MAP_MVS
792
#undef MAP_F2F
793
                }
794
            }
795
        }
796
    }
797
798
12134457
    sl->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);
799
12134457
}
800
801
/**
802
 * decodes a P_SKIP or B_SKIP macroblock
803
 */
804
3536019
static void av_unused decode_mb_skip(const H264Context *h, H264SliceContext *sl)
805
{
806
3536019
    const int mb_xy = sl->mb_xy;
807
3536019
    int mb_type     = 0;
808
809
3536019
    memset(h->non_zero_count[mb_xy], 0, 48);
810
811
3536019
    if (MB_FIELD(sl))
812
1460599
        mb_type |= MB_TYPE_INTERLACED;
813
814
3536019
    if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
815
        // just for fill_caches. pred_direct_motion will set the real mb_type
816
2133505
        mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 | MB_TYPE_SKIP;
817
2133505
        if (sl->direct_spatial_mv_pred) {
818
1997439
            fill_decode_neighbors(h, sl, mb_type);
819
1997439
            fill_decode_caches(h, sl, mb_type); //FIXME check what is needed and what not ...
820
        }
821
2133505
        ff_h264_pred_direct_motion(h, sl, &mb_type);
822
2133505
        mb_type |= MB_TYPE_SKIP;
823
    } else {
824
1402514
        mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P1L0 | MB_TYPE_SKIP;
825
826
1402514
        fill_decode_neighbors(h, sl, mb_type);
827
1402514
        pred_pskip_motion(h, sl);
828
    }
829
830
3536019
    write_back_motion(h, sl, mb_type);
831
3536019
    h->cur_pic.mb_type[mb_xy]      = mb_type;
832
3536019
    h->cur_pic.qscale_table[mb_xy] = sl->qscale;
833
3536019
    h->slice_table[mb_xy]          = sl->slice_num;
834
3536019
    sl->prev_mb_skipped            = 1;
835
3536019
}
836
837
#endif /* AVCODEC_H264_MVPRED_H */