GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/h264_mvpred.h Lines: 413 417 99.0 %
Date: 2019-11-18 18:00:01 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
37
38
17646623
static av_always_inline int fetch_diagonal_mv(const H264Context *h, H264SliceContext *sl,
39
                                              const int16_t **C,
40
                                              int i, int list, int part_width)
41
{
42
17646623
    const int topright_ref = sl->ref_cache[list][i - 8 + part_width];
43
44
    /* there is no consistent mapping of mvs to neighboring locations that will
45
     * make mbaff happy, so we can't move all this logic to fill_caches */
46
17646623
    if (FRAME_MBAFF(h)) {
47
#define SET_DIAG_MV(MV_OP, REF_OP, XY, Y4)                              \
48
        const int xy = XY, y4 = Y4;                                     \
49
        const int mb_type = mb_types[xy + (y4 >> 2) * h->mb_stride];    \
50
        if (!USES_LIST(mb_type, list))                                  \
51
            return LIST_NOT_USED;                                       \
52
        mv = h->cur_pic_ptr->motion_val[list][h->mb2b_xy[xy] + 3 + y4 * h->b_stride]; \
53
        sl->mv_cache[list][scan8[0] - 2][0] = mv[0];                     \
54
        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
2891570
        if (topright_ref == PART_NOT_AVAILABLE
58

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

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

162038
            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
35582
                SET_DIAG_MV(/ 2, *2, sl->left_mb_xy[i >= 36], ((i >> 2)) & 3);
72
            }
73
        }
74
#undef SET_DIAG_MV
75
    }
76
77
17584401
    if (topright_ref != PART_NOT_AVAILABLE) {
78
11056342
        *C = sl->mv_cache[list][i - 8 + part_width];
79
11056342
        return topright_ref;
80
    } else {
81
        ff_tlog(h->avctx, "topright MV not available\n");
82
83
6528059
        *C = sl->mv_cache[list][i - 8 - 1];
84
6528059
        return sl->ref_cache[list][i - 8 - 1];
85
    }
86
}
87
88
/**
89
 * Get the predicted MV.
90
 * @param n the block index
91
 * @param part_width the width of the partition (4, 8,16) -> (1, 2, 4)
92
 * @param mx the x component of the predicted motion vector
93
 * @param my the y component of the predicted motion vector
94
 */
95
17054490
static av_always_inline void pred_motion(const H264Context *const h,
96
                                         H264SliceContext *sl,
97
                                         int n,
98
                                         int part_width, int list, int ref,
99
                                         int *const mx, int *const my)
100
{
101
17054490
    const int index8       = scan8[n];
102
17054490
    const int top_ref      = sl->ref_cache[list][index8 - 8];
103
17054490
    const int left_ref     = sl->ref_cache[list][index8 - 1];
104
17054490
    const int16_t *const A = sl->mv_cache[list][index8 - 1];
105
17054490
    const int16_t *const B = sl->mv_cache[list][index8 - 8];
106
    const int16_t *C;
107
    int diagonal_ref, match_count;
108
109
    av_assert2(part_width == 1 || part_width == 2 || part_width == 4);
110
111
/* mv_cache
112
 * B . . A T T T T
113
 * U . . L . . , .
114
 * U . . L . . . .
115
 * U . . L . . , .
116
 * . . . L . . . .
117
 */
118
119
17054490
    diagonal_ref = fetch_diagonal_mv(h, sl, &C, index8, list, part_width);
120
17054490
    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
17054490
    if (match_count > 1) { //most common
123
12588757
        *mx = mid_pred(A[0], B[0], C[0]);
124
12588757
        *my = mid_pred(A[1], B[1], C[1]);
125
4465733
    } else if (match_count == 1) {
126
3034988
        if (left_ref == ref) {
127
1909957
            *mx = A[0];
128
1909957
            *my = A[1];
129
1125031
        } else if (top_ref == ref) {
130
808753
            *mx = B[0];
131
808753
            *my = B[1];
132
        } else {
133
316278
            *mx = C[0];
134
316278
            *my = C[1];
135
        }
136
    } else {
137

1430745
        if (top_ref      == PART_NOT_AVAILABLE &&
138
232907
            diagonal_ref == PART_NOT_AVAILABLE &&
139
            left_ref     != PART_NOT_AVAILABLE) {
140
207905
            *mx = A[0];
141
207905
            *my = A[1];
142
        } else {
143
1222840
            *mx = mid_pred(A[0], B[0], C[0]);
144
1222840
            *my = mid_pred(A[1], B[1], C[1]);
145
        }
146
    }
147
148
    ff_tlog(h->avctx,
149
            "pred_motion (%2d %2d %2d) (%2d %2d %2d) (%2d %2d %2d) -> (%2d %2d %2d) at %2d %2d %d list %d\n",
150
            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
17054490
}
153
154
/**
155
 * Get the directionally predicted 16x8 MV.
156
 * @param n the block index
157
 * @param mx the x component of the predicted motion vector
158
 * @param my the y component of the predicted motion vector
159
 */
160
1340909
static av_always_inline void pred_16x8_motion(const H264Context *const h,
161
                                              H264SliceContext *sl,
162
                                              int n, int list, int ref,
163
                                              int *const mx, int *const my)
164
{
165
1340909
    if (n == 0) {
166
669614
        const int top_ref      = sl->ref_cache[list][scan8[0] - 8];
167
669614
        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
669614
        if (top_ref == ref) {
173
378816
            *mx = B[0];
174
378816
            *my = B[1];
175
378816
            return;
176
        }
177
    } else {
178
671295
        const int left_ref     = sl->ref_cache[list][scan8[8] - 1];
179
671295
        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
671295
        if (left_ref == ref) {
185
449450
            *mx = A[0];
186
449450
            *my = A[1];
187
449450
            return;
188
        }
189
    }
190
191
    //RARE
192
512643
    pred_motion(h, sl, n, 4, list, ref, mx, my);
193
}
194
195
/**
196
 * Get the directionally predicted 8x16 MV.
197
 * @param n the block index
198
 * @param mx the x component of the predicted motion vector
199
 * @param my the y component of the predicted motion vector
200
 */
201
1185583
static av_always_inline void pred_8x16_motion(const H264Context *const h,
202
                                              H264SliceContext *sl,
203
                                              int n, int list, int ref,
204
                                              int *const mx, int *const my)
205
{
206
1185583
    if (n == 0) {
207
593450
        const int left_ref     = sl->ref_cache[list][scan8[0] - 1];
208
593450
        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
593450
        if (left_ref == ref) {
214
369830
            *mx = A[0];
215
369830
            *my = A[1];
216
369830
            return;
217
        }
218
    } else {
219
        const int16_t *C;
220
        int diagonal_ref;
221
222
592133
        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
592133
        if (diagonal_ref == ref) {
228
337331
            *mx = C[0];
229
337331
            *my = C[1];
230
337331
            return;
231
        }
232
    }
233
234
    //RARE
235
478422
    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
            if (!IS_INTERLACED(type)) {         \
242
                refn <<= 1;                     \
243
                AV_COPY32(mvbuf[idx], mvn);     \
244
                mvbuf[idx][1] /= 2;             \
245
                mvn = mvbuf[idx];               \
246
            }                                   \
247
        } else {                                \
248
            if (IS_INTERLACED(type)) {          \
249
                refn >>= 1;                     \
250
                AV_COPY32(mvbuf[idx], mvn);     \
251
                mvbuf[idx][1] *= 2;             \
252
                mvn = mvbuf[idx];               \
253
            }                                   \
254
        }                                       \
255
    }
256
257
1392846
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
1392846
    int8_t *ref     = h->cur_pic.ref_index[0];
263
1392846
    int16_t(*mv)[2] = h->cur_pic.motion_val[0];
264
    int top_ref, left_ref, diagonal_ref, match_count, mx, my;
265
    const int16_t *A, *B, *C;
266
1392846
    int b_stride = h->b_stride;
267
268
1392846
    fill_rectangle(&sl->ref_cache[0][scan8[0]], 4, 4, 8, 0, 1);
269
270
    /* To avoid doing an entire fill_decode_caches, we inline the relevant
271
     * parts here.
272
     * FIXME: this is a partial duplicate of the logic in fill_decode_caches,
273
     * but it's faster this way.  Is there a way to avoid this duplication?
274
     */
275
1392846
    if (USES_LIST(sl->left_type[LTOP], 0)) {
276
1315199
        left_ref = ref[4 * sl->left_mb_xy[LTOP] + 1 + (sl->left_block[0] & ~1)];
277
1315199
        A = mv[h->mb2b_xy[sl->left_mb_xy[LTOP]] + 3 + b_stride * sl->left_block[0]];
278


1315199
        FIX_MV_MBAFF(sl->left_type[LTOP], left_ref, A, 0);
279
1315199
        if (!(left_ref | AV_RN32A(A)))
280
1038708
            goto zeromv;
281
77647
    } else if (sl->left_type[LTOP]) {
282
31538
        left_ref = LIST_NOT_USED;
283
31538
        A        = zeromv;
284
    } else {
285
46109
        goto zeromv;
286
    }
287
288
308029
    if (USES_LIST(sl->top_type, 0)) {
289
279184
        top_ref = ref[4 * sl->top_mb_xy + 2];
290
279184
        B       = mv[h->mb2b_xy[sl->top_mb_xy] + 3 * b_stride];
291


279184
        FIX_MV_MBAFF(sl->top_type, top_ref, B, 1);
292
279184
        if (!(top_ref | AV_RN32A(B)))
293
46324
            goto zeromv;
294
28845
    } else if (sl->top_type) {
295
19032
        top_ref = LIST_NOT_USED;
296
19032
        B       = zeromv;
297
    } else {
298
9813
        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
251892
    if (USES_LIST(sl->topright_type, 0)) {
305
213321
        diagonal_ref = ref[4 * sl->topright_mb_xy + 2];
306
213321
        C = mv[h->mb2b_xy[sl->topright_mb_xy] + 3 * b_stride];
307


213321
        FIX_MV_MBAFF(sl->topright_type, diagonal_ref, C, 2);
308
38571
    } else if (sl->topright_type) {
309
18754
        diagonal_ref = LIST_NOT_USED;
310
18754
        C = zeromv;
311
    } else {
312
19817
        if (USES_LIST(sl->topleft_type, 0)) {
313
18834
            diagonal_ref = ref[4 * sl->topleft_mb_xy + 1 +
314
18834
                               (sl->topleft_partition & 2)];
315
18834
            C = mv[h->mb2b_xy[sl->topleft_mb_xy] + 3 + b_stride +
316
18834
                   (sl->topleft_partition & 2 * b_stride)];
317


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

101547
                    if (!((left_typei & type_mask) && (left_type[LTOP] & type_mask))) {
493
53
                        sl->topleft_samples_available &= 0xDF5F;
494
53
                        sl->left_samples_available    &= 0x5F5F;
495
                    }
496
                }
497
            } else {
498
3526548
                if (!(left_type[LTOP] & type_mask)) {
499
64339
                    sl->topleft_samples_available &= 0xDF5F;
500
64339
                    sl->left_samples_available    &= 0x5F5F;
501
                }
502
            }
503
504
3738535
            if (!(topleft_type & type_mask))
505
340107
                sl->topleft_samples_available &= 0x7FFF;
506
507
3738535
            if (!(topright_type & type_mask))
508
706460
                sl->topright_samples_available &= 0xFBFF;
509
510
3738535
            if (IS_INTRA4x4(mb_type)) {
511
2883951
                if (IS_INTRA4x4(top_type)) {
512
2156056
                    AV_COPY32(sl->intra4x4_pred_mode_cache + 4 + 8 * 0, sl->intra4x4_pred_mode + h->mb2br_xy[top_xy]);
513
                } else {
514
727895
                    sl->intra4x4_pred_mode_cache[4 + 8 * 0] =
515
727895
                    sl->intra4x4_pred_mode_cache[5 + 8 * 0] =
516
727895
                    sl->intra4x4_pred_mode_cache[6 + 8 * 0] =
517
727895
                    sl->intra4x4_pred_mode_cache[7 + 8 * 0] = 2 - 3 * !(top_type & type_mask);
518
                }
519
8651853
                for (i = 0; i < 2; i++) {
520
5767902
                    if (IS_INTRA4x4(left_type[LEFT(i)])) {
521
4622356
                        int8_t *mode = sl->intra4x4_pred_mode + h->mb2br_xy[left_xy[LEFT(i)]];
522
4622356
                        sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] = mode[6 - left_block[0 + 2 * i]];
523
4622356
                        sl->intra4x4_pred_mode_cache[3 + 8 * 2 + 2 * 8 * i] = mode[6 - left_block[1 + 2 * i]];
524
                    } else {
525
1145546
                        sl->intra4x4_pred_mode_cache[3 + 8 * 1 + 2 * 8 * i] =
526
1145546
                        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
         * 0 . T T. T T T T
534
         * 1 L . .L . . . .
535
         * 2 L . .L . . . .
536
         * 3 . T TL . . . .
537
         * 4 L . .L . . . .
538
         * 5 L . .. . . . .
539
         */
540
        /* FIXME: constraint_intra_pred & partitioning & nnz
541
         * (let us hope this is just a typo in the spec) */
542
9855693
        nnz_cache = sl->non_zero_count_cache;
543
9855693
        if (top_type) {
544
9049759
            nnz = h->non_zero_count[top_xy];
545
9049759
            AV_COPY32(&nnz_cache[4 + 8 * 0], &nnz[4 * 3]);
546
9049759
            if (!h->chroma_y_shift) {
547
854774
                AV_COPY32(&nnz_cache[4 + 8 *  5], &nnz[4 *  7]);
548
854774
                AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 11]);
549
            } else {
550
8194985
                AV_COPY32(&nnz_cache[4 + 8 *  5], &nnz[4 * 5]);
551
8194985
                AV_COPY32(&nnz_cache[4 + 8 * 10], &nnz[4 * 9]);
552
            }
553
        } else {
554

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

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

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

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

10542630
            if ((mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2)) && !FRAME_MBAFF(h))
688
3377944
                continue;
689
690
7164686
            if (!(mb_type & (MB_TYPE_SKIP | MB_TYPE_DIRECT2))) {
691
6660070
                uint8_t(*mvd_cache)[2]   = &sl->mvd_cache[list][scan8[0]];
692
6660070
                uint8_t(*mvd)[2]         = sl->mvd_table[list];
693
6660070
                ref_cache[2 + 8 * 0] =
694
6660070
                ref_cache[2 + 8 * 2] = PART_NOT_AVAILABLE;
695
6660070
                AV_ZERO32(mv_cache[2 + 8 * 0]);
696
6660070
                AV_ZERO32(mv_cache[2 + 8 * 2]);
697
698
                if (CABAC(h)) {
699
4348778
                    if (USES_LIST(top_type, list)) {
700
3444807
                        const int b_xy = h->mb2br_xy[top_xy];
701
3444807
                        AV_COPY64(mvd_cache[0 - 1 * 8], mvd[b_xy + 0]);
702
                    } else {
703
903971
                        AV_ZERO64(mvd_cache[0 - 1 * 8]);
704
                    }
705
4348778
                    if (USES_LIST(left_type[LTOP], list)) {
706
3658055
                        const int b_xy = h->mb2br_xy[left_xy[LTOP]] + 6;
707
3658055
                        AV_COPY16(mvd_cache[-1 + 0 * 8], mvd[b_xy - left_block[0]]);
708
3658055
                        AV_COPY16(mvd_cache[-1 + 1 * 8], mvd[b_xy - left_block[1]]);
709
                    } else {
710
690723
                        AV_ZERO16(mvd_cache[-1 + 0 * 8]);
711
690723
                        AV_ZERO16(mvd_cache[-1 + 1 * 8]);
712
                    }
713
4348778
                    if (USES_LIST(left_type[LBOT], list)) {
714
3657855
                        const int b_xy = h->mb2br_xy[left_xy[LBOT]] + 6;
715
3657855
                        AV_COPY16(mvd_cache[-1 + 2 * 8], mvd[b_xy - left_block[2]]);
716
3657855
                        AV_COPY16(mvd_cache[-1 + 3 * 8], mvd[b_xy - left_block[3]]);
717
                    } else {
718
690923
                        AV_ZERO16(mvd_cache[-1 + 2 * 8]);
719
690923
                        AV_ZERO16(mvd_cache[-1 + 3 * 8]);
720
                    }
721
4348778
                    AV_ZERO16(mvd_cache[2 + 8 * 0]);
722
4348778
                    AV_ZERO16(mvd_cache[2 + 8 * 2]);
723
4348778
                    if (sl->slice_type_nos == AV_PICTURE_TYPE_B) {
724
2518558
                        uint8_t *direct_cache = &sl->direct_cache[scan8[0]];
725
2518558
                        uint8_t *direct_table = h->direct_table;
726
2518558
                        fill_rectangle(direct_cache, 4, 4, 8, MB_TYPE_16x16 >> 1, 1);
727
728
2518558
                        if (IS_DIRECT(top_type)) {
729
381262
                            AV_WN32A(&direct_cache[-1 * 8],
730
                                     0x01010101u * (MB_TYPE_DIRECT2 >> 1));
731
2137296
                        } else if (IS_8X8(top_type)) {
732
610301
                            int b8_xy = 4 * top_xy;
733
610301
                            direct_cache[0 - 1 * 8] = direct_table[b8_xy + 2];
734
610301
                            direct_cache[2 - 1 * 8] = direct_table[b8_xy + 3];
735
                        } else {
736
1526995
                            AV_WN32A(&direct_cache[-1 * 8],
737
                                     0x01010101 * (MB_TYPE_16x16 >> 1));
738
                        }
739
740
2518558
                        if (IS_DIRECT(left_type[LTOP]))
741
376850
                            direct_cache[-1 + 0 * 8] = MB_TYPE_DIRECT2 >> 1;
742
2141708
                        else if (IS_8X8(left_type[LTOP]))
743
645258
                            direct_cache[-1 + 0 * 8] = direct_table[4 * left_xy[LTOP] + 1 + (left_block[0] & ~1)];
744
                        else
745
1496450
                            direct_cache[-1 + 0 * 8] = MB_TYPE_16x16 >> 1;
746
747
2518558
                        if (IS_DIRECT(left_type[LBOT]))
748
375965
                            direct_cache[-1 + 2 * 8] = MB_TYPE_DIRECT2 >> 1;
749
2142593
                        else if (IS_8X8(left_type[LBOT]))
750
647347
                            direct_cache[-1 + 2 * 8] = direct_table[4 * left_xy[LBOT] + 1 + (left_block[2] & ~1)];
751
                        else
752
1495246
                            direct_cache[-1 + 2 * 8] = MB_TYPE_16x16 >> 1;
753
                    }
754
                }
755
            }
756
757
#define MAP_MVS                                                         \
758
    MAP_F2F(scan8[0] - 1 - 1 * 8, topleft_type)                         \
759
    MAP_F2F(scan8[0] + 0 - 1 * 8, top_type)                             \
760
    MAP_F2F(scan8[0] + 1 - 1 * 8, top_type)                             \
761
    MAP_F2F(scan8[0] + 2 - 1 * 8, top_type)                             \
762
    MAP_F2F(scan8[0] + 3 - 1 * 8, top_type)                             \
763
    MAP_F2F(scan8[0] + 4 - 1 * 8, topright_type)                        \
764
    MAP_F2F(scan8[0] - 1 + 0 * 8, left_type[LTOP])                      \
765
    MAP_F2F(scan8[0] - 1 + 1 * 8, left_type[LTOP])                      \
766
    MAP_F2F(scan8[0] - 1 + 2 * 8, left_type[LBOT])                      \
767
    MAP_F2F(scan8[0] - 1 + 3 * 8, left_type[LBOT])
768
769
7164686
            if (FRAME_MBAFF(h)) {
770
1583802
                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










543998
                    MAP_MVS
780
                } else {
781
782
#undef MAP_F2F
783
#define MAP_F2F(idx, mb_type)                                           \
784
    if (IS_INTERLACED(mb_type) && sl->ref_cache[list][idx] >= 0) {      \
785
        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










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