h264_mvpred.h 34.7 KB
Newer Older
1
/*
2
 * H.26L/H.264/AVC/JVT/14496-10/... motion vector prediction
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
23
 * @file
24
 * H.264 / AVC / MPEG-4 part10 motion vector prediction.
25 26 27
 * @author Michael Niedermayer <michaelni@gmx.at>
 */

28 29 30
#ifndef AVCODEC_H264_MVPRED_H
#define AVCODEC_H264_MVPRED_H

31 32
#include "internal.h"
#include "avcodec.h"
33
#include "h264dec.h"
34
#include "mpegutils.h"
35
#include "libavutil/avassert.h"
36 37


38
static av_always_inline int fetch_diagonal_mv(const H264Context *h, H264SliceContext *sl,
39
                                              const int16_t **C,
40 41
                                              int i, int list, int part_width)
{
42
    const int topright_ref = sl->ref_cache[list][i - 8 + part_width];
43 44 45

    /* there is no consistent mapping of mvs to neighboring locations that will
     * make mbaff happy, so we can't move all this logic to fill_caches */
46
    if (FRAME_MBAFF(h)) {
47 48
#define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4)                              \
        const int xy = XY, y4 = Y4;                                     \
49
        const int mb_type = mb_types[xy + (y4 >> 2) * h->mb_stride];    \
50 51
        if (!USES_LIST(mb_type, list))                                  \
            return LIST_NOT_USED;                                       \
52
        mv = h->cur_pic_ptr->motion_val[list][h->mb2b_xy[xy] + 3 + y4 * h->b_stride]; \
53 54
        sl->mv_cache[list][scan8[0] - 2][0] = mv[0];                     \
        sl->mv_cache[list][scan8[0] - 2][1] = mv[1] MV_OP;               \
55
        return h->cur_pic_ptr->ref_index[list][4 * xy + 1 + (y4 & ~1)] REF_OP;
56 57 58

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

65
            if (!MB_FIELD(sl) && IS_INTERLACED(sl->left_type[0])) {
66
                SET_DIAG_MV(* 2, >> 1, sl->left_mb_xy[0] + h->mb_stride,
67
                            (sl->mb_y & 1) * 2 + (i >> 5));
68
            }
69
            if (MB_FIELD(sl) && !IS_INTERLACED(sl->left_type[0])) {
70
                // left shift will turn LIST_NOT_USED into PART_NOT_AVAILABLE, but that's OK.
71
                SET_DIAG_MV(/ 2, << 1, sl->left_mb_xy[i >= 36], ((i >> 2)) & 3);
72 73 74 75 76
            }
        }
#undef SET_DIAG_MV
    }

77
    if (topright_ref != PART_NOT_AVAILABLE) {
78
        *C = sl->mv_cache[list][i - 8 + part_width];
79
        return topright_ref;
80
    } else {
81
        ff_tlog(h->avctx, "topright MV not available\n");
82

83 84
        *C = sl->mv_cache[list][i - 8 - 1];
        return sl->ref_cache[list][i - 8 - 1];
85 86 87 88
    }
}

/**
89
 * Get the predicted MV.
90 91 92 93 94
 * @param n the block index
 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
 * @param mx the x component of the predicted motion vector
 * @param my the y component of the predicted motion vector
 */
95
static av_always_inline void pred_motion(const H264Context *const h,
96 97
                                         H264SliceContext *sl,
                                         int n,
98 99 100 101
                                         int part_width, int list, int ref,
                                         int *const mx, int *const my)
{
    const int index8       = scan8[n];
102 103 104 105
    const int top_ref      = sl->ref_cache[list][index8 - 8];
    const int left_ref     = sl->ref_cache[list][index8 - 1];
    const int16_t *const A = sl->mv_cache[list][index8 - 1];
    const int16_t *const B = sl->mv_cache[list][index8 - 8];
106
    const int16_t *C;
107 108
    int diagonal_ref, match_count;

109
    av_assert2(part_width == 1 || part_width == 2 || part_width == 4);
110 111

/* mv_cache
112 113 114 115 116 117 118
 * B . . A T T T T
 * U . . L . . , .
 * U . . L . . . .
 * U . . L . . , .
 * . . . L . . . .
 */

119
    diagonal_ref = fetch_diagonal_mv(h, sl, &C, index8, list, part_width);
120
    match_count  = (diagonal_ref == ref) + (top_ref == ref) + (left_ref == ref);
121
    ff_tlog(h->avctx, "pred_motion match_count=%d\n", match_count);
122 123 124 125 126 127 128 129 130 131 132 133 134
    if (match_count > 1) { //most common
        *mx = mid_pred(A[0], B[0], C[0]);
        *my = mid_pred(A[1], B[1], C[1]);
    } else if (match_count == 1) {
        if (left_ref == ref) {
            *mx = A[0];
            *my = A[1];
        } else if (top_ref == ref) {
            *mx = B[0];
            *my = B[1];
        } else {
            *mx = C[0];
            *my = C[1];
135
        }
136 137 138 139 140 141 142 143 144
    } else {
        if (top_ref      == PART_NOT_AVAILABLE &&
            diagonal_ref == PART_NOT_AVAILABLE &&
            left_ref     != PART_NOT_AVAILABLE) {
            *mx = A[0];
            *my = A[1];
        } else {
            *mx = mid_pred(A[0], B[0], C[0]);
            *my = mid_pred(A[1], B[1], C[1]);
145 146 147
        }
    }

148
    ff_tlog(h->avctx,
149 150
            "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n",
            top_ref, B[0], B[1], diagonal_ref, C[0], C[1], left_ref,
151
            A[0], A[1], ref, *mx, *my, sl->mb_x, sl->mb_y, n, list);
152 153 154
}

/**
155
 * Get the directionally predicted 16x8 MV.
156 157 158 159
 * @param n the block index
 * @param mx the x component of the predicted motion vector
 * @param my the y component of the predicted motion vector
 */
160
static av_always_inline void pred_16x8_motion(const H264Context *const h,
161
                                              H264SliceContext *sl,
162 163 164 165
                                              int n, int list, int ref,
                                              int *const mx, int *const my)
{
    if (n == 0) {
166 167
        const int top_ref      = sl->ref_cache[list][scan8[0] - 8];
        const int16_t *const B = sl->mv_cache[list][scan8[0] - 8];
168

169
        ff_tlog(h->avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
170
                top_ref, B[0], B[1], sl->mb_x, sl->mb_y, n, list);
171 172 173 174

        if (top_ref == ref) {
            *mx = B[0];
            *my = B[1];
175 176
            return;
        }
177
    } else {
178 179
        const int left_ref     = sl->ref_cache[list][scan8[8] - 1];
        const int16_t *const A = sl->mv_cache[list][scan8[8] - 1];
180

181
        ff_tlog(h->avctx, "pred_16x8: (%2d %2d %2d) at %2d %2d %d list %d\n",
182
                left_ref, A[0], A[1], sl->mb_x, sl->mb_y, n, list);
183

184 185 186
        if (left_ref == ref) {
            *mx = A[0];
            *my = A[1];
187 188 189 190 191
            return;
        }
    }

    //RARE
192
    pred_motion(h, sl, n, 4, list, ref, mx, my);
193 194 195
}

/**
196
 * Get the directionally predicted 8x16 MV.
197 198 199 200
 * @param n the block index
 * @param mx the x component of the predicted motion vector
 * @param my the y component of the predicted motion vector
 */
201
static av_always_inline void pred_8x16_motion(const H264Context *const h,
202
                                              H264SliceContext *sl,
203 204 205 206
                                              int n, int list, int ref,
                                              int *const mx, int *const my)
{
    if (n == 0) {
207 208
        const int left_ref     = sl->ref_cache[list][scan8[0] - 1];
        const int16_t *const A = sl->mv_cache[list][scan8[0] - 1];
209

210
        ff_tlog(h->avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
211
                left_ref, A[0], A[1], sl->mb_x, sl->mb_y, n, list);
212 213 214 215

        if (left_ref == ref) {
            *mx = A[0];
            *my = A[1];
216 217
            return;
        }
218 219
    } else {
        const int16_t *C;
220 221
        int diagonal_ref;

222
        diagonal_ref = fetch_diagonal_mv(h, sl, &C, scan8[4], list, 2);
223

224
        ff_tlog(h->avctx, "pred_8x16: (%2d %2d %2d) at %2d %2d %d list %d\n",
225
                diagonal_ref, C[0], C[1], sl->mb_x, sl->mb_y, n, list);
226

227 228 229
        if (diagonal_ref == ref) {
            *mx = C[0];
            *my = C[1];
230 231 232 233 234
            return;
        }
    }

    //RARE
235
    pred_motion(h, sl, n, 2, list, ref, mx, my);
236 237
}

238
#define FIX_MV_MBAFF(type, refn, mvn, idx)      \
239
    if (FRAME_MBAFF(h)) {                       \
240
        if (MB_FIELD(sl)) {                     \
241 242 243 244 245 246 247 248 249 250 251 252 253 254
            if (!IS_INTERLACED(type)) {         \
                refn <<= 1;                     \
                AV_COPY32(mvbuf[idx], mvn);     \
                mvbuf[idx][1] /= 2;             \
                mvn = mvbuf[idx];               \
            }                                   \
        } else {                                \
            if (IS_INTERLACED(type)) {          \
                refn >>= 1;                     \
                AV_COPY32(mvbuf[idx], mvn);     \
                mvbuf[idx][1] <<= 1;            \
                mvn = mvbuf[idx];               \
            }                                   \
        }                                       \
255
    }
256

257
static av_always_inline void pred_pskip_motion(const H264Context *const h,
258
                                               H264SliceContext *sl)
259 260
{
    DECLARE_ALIGNED(4, static const int16_t, zeromv)[2] = { 0 };
261
    DECLARE_ALIGNED(4, int16_t, mvbuf)[3][2];
262 263
    int8_t *ref     = h->cur_pic.ref_index[0];
    int16_t(*mv)[2] = h->cur_pic.motion_val[0];
264
    int top_ref, left_ref, diagonal_ref, match_count, mx, my;
265 266 267
    const int16_t *A, *B, *C;
    int b_stride = h->b_stride;

268
    fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
269

270 271 272 273
    /* To avoid doing an entire fill_decode_caches, we inline the relevant
     * parts here.
     * FIXME: this is a partial duplicate of the logic in fill_decode_caches,
     * but it's faster this way.  Is there a way to avoid this duplication?
274
     */
275 276 277 278
    if (USES_LIST(sl->left_type[LTOP], 0)) {
        left_ref = ref[4 * sl->left_mb_xy[LTOP] + 1 + (sl->left_block[0] & ~1)];
        A = mv[h->mb2b_xy[sl->left_mb_xy[LTOP]] + 3 + b_stride * sl->left_block[0]];
        FIX_MV_MBAFF(sl->left_type[LTOP], left_ref, A, 0);
279
        if (!(left_ref | AV_RN32A(A)))
280
            goto zeromv;
281
    } else if (sl->left_type[LTOP]) {
282
        left_ref = LIST_NOT_USED;
283 284
        A        = zeromv;
    } else {
285
        goto zeromv;
286
    }
287

288 289 290 291
    if (USES_LIST(sl->top_type, 0)) {
        top_ref = ref[4 * sl->top_mb_xy + 2];
        B       = mv[h->mb2b_xy[sl->top_mb_xy] + 3 * b_stride];
        FIX_MV_MBAFF(sl->top_type, top_ref, B, 1);
292
        if (!(top_ref | AV_RN32A(B)))
293
            goto zeromv;
294
    } else if (sl->top_type) {
295
        top_ref = LIST_NOT_USED;
296 297
        B       = zeromv;
    } else {
298
        goto zeromv;
299 300
    }

301
    ff_tlog(h->avctx, "pred_pskip: (%d) (%d) at %2d %2d\n",
302
            top_ref, left_ref, sl->mb_x, sl->mb_y);
303

304 305 306 307 308
    if (USES_LIST(sl->topright_type, 0)) {
        diagonal_ref = ref[4 * sl->topright_mb_xy + 2];
        C = mv[h->mb2b_xy[sl->topright_mb_xy] + 3 * b_stride];
        FIX_MV_MBAFF(sl->topright_type, diagonal_ref, C, 2);
    } else if (sl->topright_type) {
309 310
        diagonal_ref = LIST_NOT_USED;
        C = zeromv;
311
    } else {
312 313 314 315 316 317 318
        if (USES_LIST(sl->topleft_type, 0)) {
            diagonal_ref = ref[4 * sl->topleft_mb_xy + 1 +
                               (sl->topleft_partition & 2)];
            C = mv[h->mb2b_xy[sl->topleft_mb_xy] + 3 + b_stride +
                   (sl->topleft_partition & 2 * b_stride)];
            FIX_MV_MBAFF(sl->topleft_type, diagonal_ref, C, 2);
        } else if (sl->topleft_type) {
319
            diagonal_ref = LIST_NOT_USED;
320 321
            C            = zeromv;
        } else {
322
            diagonal_ref = PART_NOT_AVAILABLE;
323
            C            = zeromv;
324
        }
325 326
    }

327
    match_count = !diagonal_ref + !top_ref + !left_ref;
328
    ff_tlog(h->avctx, "pred_pskip_motion match_count=%d\n", match_count);
329
    if (match_count > 1) {
330 331
        mx = mid_pred(A[0], B[0], C[0]);
        my = mid_pred(A[1], B[1], C[1]);
332 333
    } else if (match_count == 1) {
        if (!left_ref) {
334 335
            mx = A[0];
            my = A[1];
336
        } else if (!top_ref) {
337 338
            mx = B[0];
            my = B[1];
339
        } else {
340 341
            mx = C[0];
            my = C[1];
342
        }
343
    } else {
344 345
        mx = mid_pred(A[0], B[0], C[0]);
        my = mid_pred(A[1], B[1], C[1]);
346
    }
347

348
    fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8, pack16to32(mx, my), 4);
349
    return;
350

351
zeromv:
352
    fill_rectangle(sl->mv_cache[0][scan8[0]], 4, 4, 8, 0, 4);
353 354
    return;
}
355

356
static void fill_decode_neighbors(const H264Context *h, H264SliceContext *sl, int mb_type)
357
{
358
    const int mb_xy = sl->mb_xy;
359
    int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
360 361 362 363 364
    static const uint8_t left_block_options[4][32] = {
        { 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 },
        { 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 },
        { 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 },
        { 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 }
365 366
    };

367
    sl->topleft_partition = -1;
368

369
    top_xy = mb_xy - (h->mb_stride << MB_FIELD(sl));
370 371 372 373

    /* Wow, what a mess, why didn't they simplify the interlacing & intra
     * stuff, I can't imagine that these complex rules are worth it. */

374 375 376
    topleft_xy    = top_xy - 1;
    topright_xy   = top_xy + 1;
    left_xy[LBOT] = left_xy[LTOP] = mb_xy - 1;
377
    sl->left_block = left_block_options[0];
378
    if (FRAME_MBAFF(h)) {
379
        const int left_mb_field_flag = IS_INTERLACED(h->cur_pic.mb_type[mb_xy - 1]);
380
        const int curr_mb_field_flag = IS_INTERLACED(mb_type);
381
        if (sl->mb_y & 1) {
382
            if (left_mb_field_flag != curr_mb_field_flag) {
383
                left_xy[LBOT] = left_xy[LTOP] = mb_xy - h->mb_stride - 1;
384
                if (curr_mb_field_flag) {
385
                    left_xy[LBOT] += h->mb_stride;
386
                    sl->left_block  = left_block_options[3];
387
                } else {
388
                    topleft_xy += h->mb_stride;
389 390
                    /* take top left mv from the middle of the mb, as opposed
                     * to all other modes which use the bottom right partition */
391 392
                    sl->topleft_partition = 0;
                    sl->left_block        = left_block_options[1];
393 394
                }
            }
395 396
        } else {
            if (curr_mb_field_flag) {
397 398 399
                topleft_xy  += h->mb_stride & (((h->cur_pic.mb_type[top_xy - 1] >> 7) & 1) - 1);
                topright_xy += h->mb_stride & (((h->cur_pic.mb_type[top_xy + 1] >> 7) & 1) - 1);
                top_xy      += h->mb_stride & (((h->cur_pic.mb_type[top_xy]     >> 7) & 1) - 1);
400 401 402
            }
            if (left_mb_field_flag != curr_mb_field_flag) {
                if (curr_mb_field_flag) {
403
                    left_xy[LBOT] += h->mb_stride;
404
                    sl->left_block  = left_block_options[3];
405
                } else {
406
                    sl->left_block = left_block_options[2];
407 408 409 410 411
                }
            }
        }
    }

412 413 414 415 416
    sl->topleft_mb_xy    = topleft_xy;
    sl->top_mb_xy        = top_xy;
    sl->topright_mb_xy   = topright_xy;
    sl->left_mb_xy[LTOP] = left_xy[LTOP];
    sl->left_mb_xy[LBOT] = left_xy[LBOT];
417 418
    //FIXME do we need all in the context?

419 420 421 422 423
    sl->topleft_type    = h->cur_pic.mb_type[topleft_xy];
    sl->top_type        = h->cur_pic.mb_type[top_xy];
    sl->topright_type   = h->cur_pic.mb_type[topright_xy];
    sl->left_type[LTOP] = h->cur_pic.mb_type[left_xy[LTOP]];
    sl->left_type[LBOT] = h->cur_pic.mb_type[left_xy[LBOT]];
424

425
    if (FMO) {
426
        if (h->slice_table[topleft_xy] != sl->slice_num)
427
            sl->topleft_type = 0;
428
        if (h->slice_table[top_xy] != sl->slice_num)
429
            sl->top_type = 0;
430
        if (h->slice_table[left_xy[LTOP]] != sl->slice_num)
431
            sl->left_type[LTOP] = sl->left_type[LBOT] = 0;
432
    } else {
433
        if (h->slice_table[topleft_xy] != sl->slice_num) {
434
            sl->topleft_type = 0;
435
            if (h->slice_table[top_xy] != sl->slice_num)
436
                sl->top_type = 0;
437
            if (h->slice_table[left_xy[LTOP]] != sl->slice_num)
438
                sl->left_type[LTOP] = sl->left_type[LBOT] = 0;
439 440
        }
    }
441
    if (h->slice_table[topright_xy] != sl->slice_num)
442
        sl->topright_type = 0;
443 444
}

445
static void fill_decode_caches(const H264Context *h, H264SliceContext *sl, int mb_type)
446
{
447 448
    int topleft_xy, top_xy, topright_xy, left_xy[LEFT_MBS];
    int topleft_type, top_type, topright_type, left_type[LEFT_MBS];
449
    const uint8_t *left_block = sl->left_block;
450 451 452 453
    int i;
    uint8_t *nnz;
    uint8_t *nnz_cache;

454 455 456 457 458 459 460 461 462 463
    topleft_xy      = sl->topleft_mb_xy;
    top_xy          = sl->top_mb_xy;
    topright_xy     = sl->topright_mb_xy;
    left_xy[LTOP]   = sl->left_mb_xy[LTOP];
    left_xy[LBOT]   = sl->left_mb_xy[LBOT];
    topleft_type    = sl->topleft_type;
    top_type        = sl->top_type;
    topright_type   = sl->topright_type;
    left_type[LTOP] = sl->left_type[LTOP];
    left_type[LBOT] = sl->left_type[LBOT];
464 465 466

    if (!IS_SKIP(mb_type)) {
        if (IS_INTRA(mb_type)) {
467
            int type_mask = h->ps.pps->constrained_intra_pred ? IS_INTRA(-1) : -1;
468 469 470 471
            sl->topleft_samples_available     =
                sl->top_samples_available     =
                    sl->left_samples_available = 0xFFFF;
            sl->topright_samples_available     = 0xEEEA;
472 473

            if (!(top_type & type_mask)) {
474 475 476
                sl->topleft_samples_available  = 0xB3FF;
                sl->top_samples_available      = 0x33FF;
                sl->topright_samples_available = 0x26EA;
477
            }
478 479 480
            if (IS_INTERLACED(mb_type) != IS_INTERLACED(left_type[LTOP])) {
                if (IS_INTERLACED(mb_type)) {
                    if (!(left_type[LTOP] & type_mask)) {
481 482
                        sl->topleft_samples_available &= 0xDFFF;
                        sl->left_samples_available    &= 0x5FFF;
483
                    }
484
                    if (!(left_type[LBOT] & type_mask)) {
485 486
                        sl->topleft_samples_available &= 0xFF5F;
                        sl->left_samples_available    &= 0xFF5F;
487
                    }
488
                } else {
489
                    int left_typei = h->cur_pic.mb_type[left_xy[LTOP] + h->mb_stride];
490

491
                    av_assert2(left_xy[LTOP] == left_xy[LBOT]);
492
                    if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
493 494
                        sl->topleft_samples_available &= 0xDF5F;
                        sl->left_samples_available    &= 0x5F5F;
495 496
                    }
                }
497 498
            } else {
                if (!(left_type[LTOP] & type_mask)) {
499 500
                    sl->topleft_samples_available &= 0xDF5F;
                    sl->left_samples_available    &= 0x5F5F;
501 502 503
                }
            }

504
            if (!(topleft_type & type_mask))
505
                sl->topleft_samples_available &= 0x7FFF;
506

507
            if (!(topright_type & type_mask))
508
                sl->topright_samples_available &= 0xFBFF;
509

510 511
            if (IS_INTRA4x4(mb_type)) {
                if (IS_INTRA4x4(top_type)) {
512
                    AV_COPY32(sl->intra4x4_pred_mode_cache + 4 + 8 * 0, sl->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
513
                } else {
514 515 516 517
                    sl->intra4x4_pred_mode_cache[4 + 8 * 0] =
                    sl->intra4x4_pred_mode_cache[5 + 8 * 0] =
                    sl->intra4x4_pred_mode_cache[6 + 8 * 0] =
                    sl->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);
518
                }
519 520
                for (i = 0; i < 2; i++) {
                    if (IS_INTRA4x4(left_type[LEFT(i)])) {
521 522 523
                        int8_t *mode = sl->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
                        sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];
                        sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];
524
                    } else {
525 526
                        sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =
                        sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = 2 - 3 * !(left_type[LEFT(i)] & type_mask);
527 528 529 530 531
                    }
                }
            }
        }

532 533 534 535 536 537 538 539 540 541
        /*
         * 0 . T T. T T T T
         * 1 L . .L . . . .
         * 2 L . .L . . . .
         * 3 . T TL . . . .
         * 4 L . .L . . . .
         * 5 L . .. . . . .
         */
        /* FIXME: constraint_intra_pred & partitioning & nnz
         * (let us hope this is just a typo in the spec) */
542
        nnz_cache = sl->non_zero_count_cache;
543 544 545
        if (top_type) {
            nnz = h->non_zero_count[top_xy];
            AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
546
            if (!h->chroma_y_shift) {
547 548 549 550 551 552 553
                AV_COPY32(&nnz_cache[4 + 8 *  5], &nnz[4 *  7]);
                AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
            } else {
                AV_COPY32(&nnz_cache[4 + 8 *  5], &nnz[4 * 5]);
                AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
            }
        } else {
554
            uint32_t top_empty = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 0x40404040;
555 556 557
            AV_WN32A(&nnz_cache[4 + 8 *  0], top_empty);
            AV_WN32A(&nnz_cache[4 + 8 *  5], top_empty);
            AV_WN32A(&nnz_cache[4 + 8 * 10], top_empty);
558 559
        }

560 561 562 563 564
        for (i = 0; i < 2; i++) {
            if (left_type[LEFT(i)]) {
                nnz = h->non_zero_count[left_xy[LEFT(i)]];
                nnz_cache[3 + 8 * 1 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i]];
                nnz_cache[3 + 8 * 2 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i]];
565
                if (CHROMA444(h)) {
566 567 568 569
                    nnz_cache[3 + 8 *  6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 4 * 4];
                    nnz_cache[3 + 8 *  7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 4 * 4];
                    nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] + 8 * 4];
                    nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] + 8 * 4];
570
                } else if (CHROMA422(h)) {
571 572 573 574 575 576 577 578 579 580 581 582 583 584
                    nnz_cache[3 + 8 *  6 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 4 * 4];
                    nnz_cache[3 + 8 *  7 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 4 * 4];
                    nnz_cache[3 + 8 * 11 + 2 * 8 * i] = nnz[left_block[8 + 0 + 2 * i] - 2 + 8 * 4];
                    nnz_cache[3 + 8 * 12 + 2 * 8 * i] = nnz[left_block[8 + 1 + 2 * i] - 2 + 8 * 4];
                } else {
                    nnz_cache[3 + 8 *  6 + 8 * i] = nnz[left_block[8 + 4 + 2 * i]];
                    nnz_cache[3 + 8 * 11 + 8 * i] = nnz[left_block[8 + 5 + 2 * i]];
                }
            } else {
                nnz_cache[3 + 8 *  1 + 2 * 8 * i] =
                nnz_cache[3 + 8 *  2 + 2 * 8 * i] =
                nnz_cache[3 + 8 *  6 + 2 * 8 * i] =
                nnz_cache[3 + 8 *  7 + 2 * 8 * i] =
                nnz_cache[3 + 8 * 11 + 2 * 8 * i] =
585
                nnz_cache[3 + 8 * 12 + 2 * 8 * i] = CABAC(h) && !IS_INTRA(mb_type) ? 0 : 64;
586 587 588
            }
        }

589
        if (CABAC(h)) {
590 591
            // top_cbp
            if (top_type)
592
                sl->top_cbp = h->cbp_table[top_xy];
593
            else
594
                sl->top_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
595 596
            // left_cbp
            if (left_type[LTOP]) {
597
                sl->left_cbp =   (h->cbp_table[left_xy[LTOP]] & 0x7F0) |
598 599 600
                               ((h->cbp_table[left_xy[LTOP]] >> (left_block[0] & (~1))) & 2) |
                              (((h->cbp_table[left_xy[LBOT]] >> (left_block[2] & (~1))) & 2) << 2);
            } else {
601
                sl->left_cbp = IS_INTRA(mb_type) ? 0x7CF : 0x00F;
602
            }
603 604 605
        }
    }

606
    if (IS_INTER(mb_type) || (IS_DIRECT(mb_type) && sl->direct_spatial_mv_pred)) {
607 608
        int list;
        int b_stride = h->b_stride;
609
        for (list = 0; list < sl->list_count; list++) {
610
            int8_t *ref_cache = &sl->ref_cache[list][scan8[0]];
611
            int8_t *ref       = h->cur_pic.ref_index[list];
612
            int16_t(*mv_cache)[2] = &sl->mv_cache[list][scan8[0]];
613
            int16_t(*mv)[2]       = h->cur_pic.motion_val[list];
614
            if (!USES_LIST(mb_type, list))
615
                continue;
616
            av_assert2(!(IS_DIRECT(mb_type) && !sl->direct_spatial_mv_pred));
617

618 619 620 621 622 623 624 625 626 627 628
            if (USES_LIST(top_type, list)) {
                const int b_xy = h->mb2b_xy[top_xy] + 3 * b_stride;
                AV_COPY128(mv_cache[0 - 1 * 8], mv[b_xy + 0]);
                ref_cache[0 - 1 * 8] =
                ref_cache[1 - 1 * 8] = ref[4 * top_xy + 2];
                ref_cache[2 - 1 * 8] =
                ref_cache[3 - 1 * 8] = ref[4 * top_xy + 3];
            } else {
                AV_ZERO128(mv_cache[0 - 1 * 8]);
                AV_WN32A(&ref_cache[0 - 1 * 8],
                         ((top_type ? LIST_NOT_USED : PART_NOT_AVAILABLE) & 0xFF) * 0x01010101u);
629 630
            }

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
            if (mb_type & (MB_TYPE_16x8 | MB_TYPE_8x8)) {
                for (i = 0; i < 2; i++) {
                    int cache_idx = -1 + i * 2 * 8;
                    if (USES_LIST(left_type[LEFT(i)], list)) {
                        const int b_xy  = h->mb2b_xy[left_xy[LEFT(i)]] + 3;
                        const int b8_xy = 4 * left_xy[LEFT(i)] + 1;
                        AV_COPY32(mv_cache[cache_idx],
                                  mv[b_xy + b_stride * left_block[0 + i * 2]]);
                        AV_COPY32(mv_cache[cache_idx + 8],
                                  mv[b_xy + b_stride * left_block[1 + i * 2]]);
                        ref_cache[cache_idx]     = ref[b8_xy + (left_block[0 + i * 2] & ~1)];
                        ref_cache[cache_idx + 8] = ref[b8_xy + (left_block[1 + i * 2] & ~1)];
                    } else {
                        AV_ZERO32(mv_cache[cache_idx]);
                        AV_ZERO32(mv_cache[cache_idx + 8]);
                        ref_cache[cache_idx]     =
                        ref_cache[cache_idx + 8] = (left_type[LEFT(i)]) ? LIST_NOT_USED
                                                                        : PART_NOT_AVAILABLE;
                    }
650
                }
651 652 653 654 655 656 657
            } else {
                if (USES_LIST(left_type[LTOP], list)) {
                    const int b_xy  = h->mb2b_xy[left_xy[LTOP]] + 3;
                    const int b8_xy = 4 * left_xy[LTOP] + 1;
                    AV_COPY32(mv_cache[-1], mv[b_xy + b_stride * left_block[0]]);
                    ref_cache[-1] = ref[b8_xy + (left_block[0] & ~1)];
                } else {
658
                    AV_ZERO32(mv_cache[-1]);
659 660
                    ref_cache[-1] = left_type[LTOP] ? LIST_NOT_USED
                                                    : PART_NOT_AVAILABLE;
661 662 663
                }
            }

664 665 666 667 668 669 670 671
            if (USES_LIST(topright_type, list)) {
                const int b_xy = h->mb2b_xy[topright_xy] + 3 * b_stride;
                AV_COPY32(mv_cache[4 - 1 * 8], mv[b_xy]);
                ref_cache[4 - 1 * 8] = ref[4 * topright_xy + 2];
            } else {
                AV_ZERO32(mv_cache[4 - 1 * 8]);
                ref_cache[4 - 1 * 8] = topright_type ? LIST_NOT_USED
                                                     : PART_NOT_AVAILABLE;
672
            }
673
            if(ref_cache[2 - 1*8] < 0 || ref_cache[4 - 1 * 8] < 0) {
674 675
                if (USES_LIST(topleft_type, list)) {
                    const int b_xy  = h->mb2b_xy[topleft_xy] + 3 + b_stride +
676 677
                                      (sl->topleft_partition & 2 * b_stride);
                    const int b8_xy = 4 * topleft_xy + 1 + (sl->topleft_partition & 2);
678 679 680 681 682 683
                    AV_COPY32(mv_cache[-1 - 1 * 8], mv[b_xy]);
                    ref_cache[-1 - 1 * 8] = ref[b8_xy];
                } else {
                    AV_ZERO32(mv_cache[-1 - 1 * 8]);
                    ref_cache[-1 - 1 * 8] = topleft_type ? LIST_NOT_USED
                                                         : PART_NOT_AVAILABLE;
684 685 686
                }
            }

687
            if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF(h))
688 689
                continue;

690
            if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
691
                uint8_t(*mvd_cache)[2]   = &sl->mvd_cache[list][scan8[0]];
692
                uint8_t(*mvd)[2]         = sl->mvd_table[list];
693 694 695 696 697
                ref_cache[2 + 8 * 0] =
                ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;
                AV_ZERO32(mv_cache[2 + 8 * 0]);
                AV_ZERO32(mv_cache[2 + 8 * 2]);

698
                if (CABAC(h)) {
699 700 701 702 703
                    if (USES_LIST(top_type, list)) {
                        const int b_xy = h->mb2br_xy[top_xy];
                        AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
                    } else {
                        AV_ZERO64(mvd_cache[0 - 1 * 8]);
704
                    }
705 706 707 708 709 710 711
                    if (USES_LIST(left_type[LTOP], list)) {
                        const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;
                        AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
                        AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
                    } else {
                        AV_ZERO16(mvd_cache[-1 + 0 * 8]);
                        AV_ZERO16(mvd_cache[-1 + 1 * 8]);
712
                    }
713 714 715 716 717 718 719
                    if (USES_LIST(left_type[LBOT], list)) {
                        const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;
                        AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
                        AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
                    } else {
                        AV_ZERO16(mvd_cache[-1 + 2 * 8]);
                        AV_ZERO16(mvd_cache[-1 + 3 * 8]);
720
                    }
721 722
                    AV_ZERO16(mvd_cache[2 + 8 * 0]);
                    AV_ZERO16(mvd_cache[2 + 8 * 2]);
723
                    if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
724
                        uint8_t *direct_cache = &sl->direct_cache[scan8[0]];
725
                        uint8_t *direct_table = h->direct_table;
726 727 728 729 730 731 732 733 734 735 736 737
                        fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);

                        if (IS_DIRECT(top_type)) {
                            AV_WN32A(&direct_cache[-1 * 8],
                                     0x01010101u * (MB_TYPE_DIRECT2 >> 1));
                        } else if (IS_8X8(top_type)) {
                            int b8_xy = 4 * top_xy;
                            direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
                            direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
                        } else {
                            AV_WN32A(&direct_cache[-1 * 8],
                                     0x01010101 * (MB_TYPE_16x16 >> 1));
738 739
                        }

740 741 742 743
                        if (IS_DIRECT(left_type[LTOP]))
                            direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;
                        else if (IS_8X8(left_type[LTOP]))
                            direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];
744
                        else
745
                            direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;
746

747 748 749 750
                        if (IS_DIRECT(left_type[LBOT]))
                            direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;
                        else if (IS_8X8(left_type[LBOT]))
                            direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
751
                        else
752
                            direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;
753 754 755
                    }
                }
            }
756 757 758 759 760 761 762 763 764 765 766 767 768

#define MAP_MVS                                                         \
    MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type)                         \
    MAP_F2F(scan8[0] + 0 - 1 * 8, top_type)                             \
    MAP_F2F(scan8[0] + 1 - 1 * 8, top_type)                             \
    MAP_F2F(scan8[0] + 2 - 1 * 8, top_type)                             \
    MAP_F2F(scan8[0] + 3 - 1 * 8, top_type)                             \
    MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type)                        \
    MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP])                      \
    MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP])                      \
    MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT])                      \
    MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])

769
            if (FRAME_MBAFF(h)) {
770
                if (MB_FIELD(sl)) {
771 772

#define MAP_F2F(idx, mb_type)                                           \
773
    if (!IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) {     \
774
        sl->ref_cache[list][idx]     *= 2;                              \
775
        sl->mv_cache[list][idx][1]   /= 2;                              \
776
        sl->mvd_cache[list][idx][1] >>= 1;                              \
777 778
    }

779
                    MAP_MVS
780 781
                } else {

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

790 791 792 793 794 795 796
                    MAP_MVS
#undef MAP_F2F
                }
            }
        }
    }

797
    sl->neighbor_transform_size = !!IS_8x8DCT(top_type) + !!IS_8x8DCT(left_type[LTOP]);
798 799
}

800 801 802
/**
 * decodes a P_SKIP or B_SKIP macroblock
 */
803
static void av_unused decode_mb_skip(const H264Context *h, H264SliceContext *sl)
804
{
805
    const int mb_xy = sl->mb_xy;
806
    int mb_type     = 0;
807 808 809

    memset(h->non_zero_count[mb_xy], 0, 48);

810
    if (MB_FIELD(sl))
811
        mb_type |= MB_TYPE_INTERLACED;
812

813
    if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
814
        // just for fill_caches. pred_direct_motion will set the real mb_type
815
        mb_type |= MB_TYPE_L0L1 | MB_TYPE_DIRECT2 | MB_TYPE_SKIP;
816
        if (sl->direct_spatial_mv_pred) {
817 818
            fill_decode_neighbors(h, sl, mb_type);
            fill_decode_caches(h, sl, mb_type); //FIXME check what is needed and what not ...
819
        }
820
        ff_h264_pred_direct_motion(h, sl, &mb_type);
821 822 823
        mb_type |= MB_TYPE_SKIP;
    } else {
        mb_type |= MB_TYPE_16x16 | MB_TYPE_P0L0 | MB_TYPE_P1L0 | MB_TYPE_SKIP;
824

825 826
        fill_decode_neighbors(h, sl, mb_type);
        pred_pskip_motion(h, sl);
827 828
    }

829
    write_back_motion(h, sl, mb_type);
830
    h->cur_pic.mb_type[mb_xy]      = mb_type;
831
    h->cur_pic.qscale_table[mb_xy] = sl->qscale;
832
    h->slice_table[mb_xy]          = sl->slice_num;
833
    sl->prev_mb_skipped            = 1;
834 835
}

836
#endif /* AVCODEC_H264_MVPRED_H */