Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/vc1_pred.c
Line
Count
Source
1
/*
2
 * VC-1 and WMV3 decoder
3
 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4
 * Copyright (c) 2006-2007 Konstantin Shishkov
5
 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6
 *
7
 * This file is part of FFmpeg.
8
 *
9
 * FFmpeg is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation; either
12
 * version 2.1 of the License, or (at your option) any later version.
13
 *
14
 * FFmpeg is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 * Lesser General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Lesser General Public
20
 * License along with FFmpeg; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
 */
23
24
/**
25
 * @file
26
 * VC-1 and WMV3 block decoding routines
27
 */
28
29
#include "mathops.h"
30
#include "mpegutils.h"
31
#include "mpegvideo.h"
32
#include "vc1.h"
33
#include "vc1_pred.h"
34
#include "vc1data.h"
35
36
static av_always_inline int scaleforsame_x(const VC1Context *v, int n /* MV */, int dir)
37
1.12M
{
38
1.12M
    int scaledvalue, refdist;
39
1.12M
    int scalesame1, scalesame2;
40
1.12M
    int scalezone1_x, zone1offset_x;
41
1.12M
    int table_index = dir ^ v->second_field;
42
43
1.12M
    if (v->s.pict_type != AV_PICTURE_TYPE_B)
44
471k
        refdist = v->refdist;
45
655k
    else
46
655k
        refdist = dir ? v->brfd : v->frfd;
47
1.12M
    if (refdist > 3)
48
3.08k
        refdist = 3;
49
1.12M
    scalesame1    = ff_vc1_field_mvpred_scales[table_index][1][refdist];
50
1.12M
    scalesame2    = ff_vc1_field_mvpred_scales[table_index][2][refdist];
51
1.12M
    scalezone1_x  = ff_vc1_field_mvpred_scales[table_index][3][refdist];
52
1.12M
    zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
53
54
1.12M
    if (FFABS(n) > 255)
55
25.9k
        scaledvalue = n;
56
1.10M
    else {
57
1.10M
        if (FFABS(n) < scalezone1_x)
58
939k
            scaledvalue = (n * scalesame1) >> 8;
59
162k
        else {
60
162k
            if (n < 0)
61
60.0k
                scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
62
102k
            else
63
102k
                scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
64
162k
        }
65
1.10M
    }
66
1.12M
    return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
67
1.12M
}
68
69
static av_always_inline int scaleforsame_y(const VC1Context *v, int n /* MV */, int dir)
70
1.12M
{
71
1.12M
    int scaledvalue, refdist;
72
1.12M
    int scalesame1, scalesame2;
73
1.12M
    int scalezone1_y, zone1offset_y;
74
1.12M
    int table_index = dir ^ v->second_field;
75
76
1.12M
    if (v->s.pict_type != AV_PICTURE_TYPE_B)
77
471k
        refdist = v->refdist;
78
655k
    else
79
655k
        refdist = dir ? v->brfd : v->frfd;
80
1.12M
    if (refdist > 3)
81
3.08k
        refdist = 3;
82
1.12M
    scalesame1    = ff_vc1_field_mvpred_scales[table_index][1][refdist];
83
1.12M
    scalesame2    = ff_vc1_field_mvpred_scales[table_index][2][refdist];
84
1.12M
    scalezone1_y  = ff_vc1_field_mvpred_scales[table_index][4][refdist];
85
1.12M
    zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
86
87
1.12M
    if (FFABS(n) > 63)
88
36.0k
        scaledvalue = n;
89
1.09M
    else {
90
1.09M
        if (FFABS(n) < scalezone1_y)
91
898k
            scaledvalue = (n * scalesame1) >> 8;
92
192k
        else {
93
192k
            if (n < 0)
94
54.2k
                scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
95
138k
            else
96
138k
                scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
97
192k
        }
98
1.09M
    }
99
100
1.12M
    if (v->cur_field_type && !v->ref_field_type[dir])
101
0
        return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
102
1.12M
    else
103
1.12M
        return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
104
1.12M
}
105
106
static av_always_inline int scaleforopp_x(const VC1Context *v, int n /* MV */)
107
56.0k
{
108
56.0k
    int scalezone1_x, zone1offset_x;
109
56.0k
    int scaleopp1, scaleopp2, brfd;
110
56.0k
    int scaledvalue;
111
112
56.0k
    brfd = FFMIN(v->brfd, 3);
113
56.0k
    scalezone1_x  = ff_vc1_b_field_mvpred_scales[3][brfd];
114
56.0k
    zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
115
56.0k
    scaleopp1     = ff_vc1_b_field_mvpred_scales[1][brfd];
116
56.0k
    scaleopp2     = ff_vc1_b_field_mvpred_scales[2][brfd];
117
118
56.0k
    if (FFABS(n) > 255)
119
1.72k
        scaledvalue = n;
120
54.3k
    else {
121
54.3k
        if (FFABS(n) < scalezone1_x)
122
44.0k
            scaledvalue = (n * scaleopp1) >> 8;
123
10.2k
        else {
124
10.2k
            if (n < 0)
125
5.79k
                scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
126
4.41k
            else
127
4.41k
                scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
128
10.2k
        }
129
54.3k
    }
130
56.0k
    return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
131
56.0k
}
132
133
static av_always_inline int scaleforopp_y(const VC1Context *v, int n /* MV */, int dir)
134
56.0k
{
135
56.0k
    int scalezone1_y, zone1offset_y;
136
56.0k
    int scaleopp1, scaleopp2, brfd;
137
56.0k
    int scaledvalue;
138
139
56.0k
    brfd = FFMIN(v->brfd, 3);
140
56.0k
    scalezone1_y  = ff_vc1_b_field_mvpred_scales[4][brfd];
141
56.0k
    zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
142
56.0k
    scaleopp1     = ff_vc1_b_field_mvpred_scales[1][brfd];
143
56.0k
    scaleopp2     = ff_vc1_b_field_mvpred_scales[2][brfd];
144
145
56.0k
    if (FFABS(n) > 63)
146
2.53k
        scaledvalue = n;
147
53.5k
    else {
148
53.5k
        if (FFABS(n) < scalezone1_y)
149
37.1k
            scaledvalue = (n * scaleopp1) >> 8;
150
16.3k
        else {
151
16.3k
            if (n < 0)
152
6.55k
                scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
153
9.75k
            else
154
9.75k
                scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
155
16.3k
        }
156
53.5k
    }
157
56.0k
    if (v->cur_field_type && !v->ref_field_type[dir]) {
158
14.3k
        return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
159
41.7k
    } else {
160
41.7k
        return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
161
41.7k
    }
162
56.0k
}
163
164
static av_always_inline int scaleforsame(const VC1Context *v, int n /* MV */,
165
                                         int dim, int dir)
166
2.36M
{
167
2.36M
    int brfd, scalesame;
168
2.36M
    int hpel = 1 - v->s.quarter_sample;
169
170
2.36M
    n >>= hpel;
171
2.36M
    if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
172
2.25M
        if (dim)
173
1.12M
            n = scaleforsame_y(v, n, dir) * (1 << hpel);
174
1.12M
        else
175
1.12M
            n = scaleforsame_x(v, n, dir) * (1 << hpel);
176
2.25M
        return n;
177
2.25M
    }
178
104k
    brfd      = FFMIN(v->brfd, 3);
179
104k
    scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
180
181
104k
    n = (n * scalesame >> 8) * (1 << hpel);
182
104k
    return n;
183
2.36M
}
184
185
static av_always_inline int scaleforopp(const VC1Context *v, int n /* MV */,
186
                                        int dim, int dir)
187
2.52M
{
188
2.52M
    int refdist, scaleopp;
189
2.52M
    int hpel = 1 - v->s.quarter_sample;
190
191
2.52M
    n >>= hpel;
192
2.52M
    if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
193
112k
        if (dim)
194
56.0k
            n = scaleforopp_y(v, n, dir) * (1 << hpel);
195
56.0k
        else
196
56.0k
            n = scaleforopp_x(v, n)      * (1 << hpel);
197
112k
        return n;
198
112k
    }
199
2.40M
    if (v->s.pict_type != AV_PICTURE_TYPE_B)
200
906k
        refdist = v->refdist;
201
1.50M
    else
202
1.50M
        refdist = dir ? v->brfd : v->frfd;
203
2.40M
    refdist = FFMIN(refdist, 3);
204
2.40M
    scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
205
206
2.40M
    n = (n * scaleopp >> 8) * (1 << hpel);
207
2.40M
    return n;
208
2.52M
}
209
210
/** Predict and set motion vector
211
 */
212
void ff_vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
213
                    int mv1, int r_x, int r_y, uint8_t* is_intra,
214
                    int pred_flag, int dir)
215
34.5M
{
216
34.5M
    MpegEncContext *s = &v->s;
217
34.5M
    int xy, wrap, off = 0;
218
34.5M
    int px, py;
219
34.5M
    int sum;
220
34.5M
    int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
221
34.5M
    int opposite, a_f, b_f, c_f;
222
34.5M
    int16_t field_predA[2];
223
34.5M
    int16_t field_predB[2];
224
34.5M
    int16_t field_predC[2];
225
34.5M
    int a_valid, b_valid, c_valid;
226
34.5M
    int hybridmv_thresh, y_bias = 0;
227
228
34.5M
    if (v->mv_mode == MV_PMODE_MIXED_MV ||
229
13.9M
        ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
230
27.6M
        mixedmv_pic = 1;
231
6.88M
    else
232
6.88M
        mixedmv_pic = 0;
233
    /* scale MV difference to be quad-pel */
234
34.5M
    if (!s->quarter_sample) {
235
5.56M
        dmv_x *= 2;
236
5.56M
        dmv_y *= 2;
237
5.56M
    }
238
239
34.5M
    wrap = s->b8_stride;
240
34.5M
    xy   = s->block_index[n];
241
242
34.5M
    if (s->mb_intra) {
243
1.17M
        s->mv[0][n][0] = s->cur_pic.motion_val[0][xy + v->blocks_off][0] = 0;
244
1.17M
        s->mv[0][n][1] = s->cur_pic.motion_val[0][xy + v->blocks_off][1] = 0;
245
1.17M
        s->cur_pic.motion_val[1][xy + v->blocks_off][0] = 0;
246
1.17M
        s->cur_pic.motion_val[1][xy + v->blocks_off][1] = 0;
247
1.17M
        if (mv1) { /* duplicate motion data for 1-MV block */
248
457k
            s->cur_pic.motion_val[0][xy + 1 + v->blocks_off][0]        = 0;
249
457k
            s->cur_pic.motion_val[0][xy + 1 + v->blocks_off][1]        = 0;
250
457k
            s->cur_pic.motion_val[0][xy + wrap + v->blocks_off][0]     = 0;
251
457k
            s->cur_pic.motion_val[0][xy + wrap + v->blocks_off][1]     = 0;
252
457k
            s->cur_pic.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
253
457k
            s->cur_pic.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
254
457k
            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
255
457k
            s->cur_pic.motion_val[1][xy + 1 + v->blocks_off][0]        = 0;
256
457k
            s->cur_pic.motion_val[1][xy + 1 + v->blocks_off][1]        = 0;
257
457k
            s->cur_pic.motion_val[1][xy + wrap + v->blocks_off][0]     = 0;
258
457k
            s->cur_pic.motion_val[1][xy + wrap + v->blocks_off][1]     = 0;
259
457k
            s->cur_pic.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
260
457k
            s->cur_pic.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
261
457k
        }
262
1.17M
        return;
263
1.17M
    }
264
265
33.3M
    a_valid = !s->first_slice_line || (n == 2 || n == 3);
266
33.3M
    b_valid = a_valid;
267
33.3M
    c_valid = s->mb_x || (n == 1 || n == 3);
268
33.3M
    if (mv1) {
269
12.6M
        if (v->field_mode && mixedmv_pic)
270
1.53M
            off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
271
11.0M
        else
272
11.0M
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
273
12.6M
        b_valid = b_valid && s->mb_width > 1;
274
20.7M
    } else {
275
        //in 4-MV mode different blocks have different B predictor position
276
20.7M
        switch (n) {
277
5.22M
        case 0:
278
5.22M
            if (v->res_rtm_flag)
279
2.83M
                off = s->mb_x ? -1 : 1;
280
2.38M
            else
281
2.38M
                off = s->mb_x ? -1 : 2 * s->mb_width - wrap - 1;
282
5.22M
            break;
283
5.24M
        case 1:
284
5.24M
            off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
285
5.24M
            break;
286
5.24M
        case 2:
287
5.24M
            off = 1;
288
5.24M
            break;
289
5.05M
        case 3:
290
5.05M
            off = -1;
291
20.7M
        }
292
20.7M
        if (v->field_mode && s->mb_width == 1)
293
30.1k
            b_valid = b_valid && c_valid;
294
20.7M
    }
295
296
33.3M
    if (v->field_mode) {
297
4.27M
        a_valid = a_valid && !is_intra[xy - wrap];
298
4.27M
        b_valid = b_valid && !is_intra[xy - wrap + off];
299
4.27M
        c_valid = c_valid && !is_intra[xy - 1];
300
4.27M
    }
301
302
33.3M
    if (a_valid) {
303
29.9M
        const int16_t *A = s->cur_pic.motion_val[dir][xy - wrap + v->blocks_off];
304
29.9M
        a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
305
29.9M
        num_oppfield  += a_f;
306
29.9M
        num_samefield += 1 - a_f;
307
29.9M
        field_predA[0] = A[0];
308
29.9M
        field_predA[1] = A[1];
309
29.9M
    } else {
310
3.38M
        field_predA[0] = field_predA[1] = 0;
311
3.38M
        a_f = 0;
312
3.38M
    }
313
33.3M
    if (b_valid) {
314
29.5M
        const int16_t *B = s->cur_pic.motion_val[dir][xy - wrap + off + v->blocks_off];
315
29.5M
        b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
316
29.5M
        num_oppfield  += b_f;
317
29.5M
        num_samefield += 1 - b_f;
318
29.5M
        field_predB[0] = B[0];
319
29.5M
        field_predB[1] = B[1];
320
29.5M
    } else {
321
3.82M
        field_predB[0] = field_predB[1] = 0;
322
3.82M
        b_f = 0;
323
3.82M
    }
324
33.3M
    if (c_valid) {
325
31.3M
        const int16_t *C = s->cur_pic.motion_val[dir][xy - 1 + v->blocks_off];
326
31.3M
        c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
327
31.3M
        num_oppfield  += c_f;
328
31.3M
        num_samefield += 1 - c_f;
329
31.3M
        field_predC[0] = C[0];
330
31.3M
        field_predC[1] = C[1];
331
31.3M
    } else {
332
1.99M
        field_predC[0] = field_predC[1] = 0;
333
1.99M
        c_f = 0;
334
1.99M
    }
335
336
33.3M
    if (v->field_mode) {
337
4.27M
        if (!v->numref)
338
            // REFFIELD determines if the last field or the second-last field is
339
            // to be used as reference
340
373k
            opposite = 1 - v->reffield;
341
3.89M
        else {
342
3.89M
            if (num_samefield <= num_oppfield)
343
2.14M
                opposite = 1 - pred_flag;
344
1.75M
            else
345
1.75M
                opposite = pred_flag;
346
3.89M
        }
347
4.27M
    } else
348
29.0M
        opposite = 0;
349
33.3M
    if (opposite) {
350
2.25M
        v->mv_f[dir][xy + v->blocks_off] = 1;
351
2.25M
        v->ref_field_type[dir] = !v->cur_field_type;
352
2.25M
        if (a_valid && !a_f) {
353
353k
            field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
354
353k
            field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
355
353k
        }
356
2.25M
        if (b_valid && !b_f) {
357
441k
            field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
358
441k
            field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
359
441k
        }
360
2.25M
        if (c_valid && !c_f) {
361
464k
            field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
362
464k
            field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
363
464k
        }
364
31.1M
    } else {
365
31.1M
        v->mv_f[dir][xy + v->blocks_off] = 0;
366
31.1M
        v->ref_field_type[dir] = v->cur_field_type;
367
31.1M
        if (a_valid && a_f) {
368
337k
            field_predA[0] = scaleforsame(v, field_predA[0], 0, dir);
369
337k
            field_predA[1] = scaleforsame(v, field_predA[1], 1, dir);
370
337k
        }
371
31.1M
        if (b_valid && b_f) {
372
404k
            field_predB[0] = scaleforsame(v, field_predB[0], 0, dir);
373
404k
            field_predB[1] = scaleforsame(v, field_predB[1], 1, dir);
374
404k
        }
375
31.1M
        if (c_valid && c_f) {
376
437k
            field_predC[0] = scaleforsame(v, field_predC[0], 0, dir);
377
437k
            field_predC[1] = scaleforsame(v, field_predC[1], 1, dir);
378
437k
        }
379
31.1M
    }
380
381
33.3M
    if (a_valid) {
382
29.9M
        px = field_predA[0];
383
29.9M
        py = field_predA[1];
384
29.9M
    } else if (c_valid) {
385
3.13M
        px = field_predC[0];
386
3.13M
        py = field_predC[1];
387
3.13M
    } else if (b_valid) {
388
23.4k
        px = field_predB[0];
389
23.4k
        py = field_predB[1];
390
230k
    } else {
391
230k
        px = 0;
392
230k
        py = 0;
393
230k
    }
394
395
33.3M
    if (num_samefield + num_oppfield > 1) {
396
29.6M
        px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
397
29.6M
        py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
398
29.6M
    }
399
400
    /* Pullback MV as specified in 8.3.5.3.4 */
401
33.3M
    if (!v->field_mode) {
402
29.0M
        int qx, qy, X, Y;
403
29.0M
        int MV = mv1 ? -60 : -28;
404
29.0M
        qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
405
29.0M
        qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
406
29.0M
        X  = (s->mb_width  << 6) - 4;
407
29.0M
        Y  = (s->mb_height << 6) - 4;
408
29.0M
        if (qx + px < MV) px = MV - qx;
409
29.0M
        if (qy + py < MV) py = MV - qy;
410
29.0M
        if (qx + px > X) px = X - qx;
411
29.0M
        if (qy + py > Y) py = Y - qy;
412
29.0M
    }
413
414
33.3M
    if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
415
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
416
30.7M
        hybridmv_thresh = 32;
417
30.7M
        if (a_valid && c_valid) {
418
26.2M
            if (is_intra[xy - wrap])
419
927k
                sum = FFABS(px) + FFABS(py);
420
25.3M
            else
421
25.3M
                sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
422
26.2M
            if (sum > hybridmv_thresh) {
423
1.61M
                if (get_bits1(&v->gb)) {     // read HYBRIDPRED bit
424
886k
                    px = field_predA[0];
425
886k
                    py = field_predA[1];
426
886k
                } else {
427
723k
                    px = field_predC[0];
428
723k
                    py = field_predC[1];
429
723k
                }
430
24.6M
            } else {
431
24.6M
                if (is_intra[xy - 1])
432
847k
                    sum = FFABS(px) + FFABS(py);
433
23.8M
                else
434
23.8M
                    sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
435
24.6M
                if (sum > hybridmv_thresh) {
436
3.29M
                    if (get_bits1(&v->gb)) {
437
1.53M
                        px = field_predA[0];
438
1.53M
                        py = field_predA[1];
439
1.75M
                    } else {
440
1.75M
                        px = field_predC[0];
441
1.75M
                        py = field_predC[1];
442
1.75M
                    }
443
3.29M
                }
444
24.6M
            }
445
26.2M
        }
446
30.7M
    }
447
448
33.3M
    if (v->field_mode && v->numref)
449
3.89M
        r_y >>= 1;
450
33.3M
    if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
451
1.77M
        y_bias = 1;
452
    /* store MV using signed modulus of MV range defined in 4.11 */
453
33.3M
    s->mv[dir][n][0] = s->cur_pic.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
454
33.3M
    s->mv[dir][n][1] = s->cur_pic.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
455
33.3M
    if (mv1) { /* duplicate motion data for 1-MV block */
456
12.6M
        s->cur_pic.motion_val[dir][xy +    1 +     v->blocks_off][0] = s->cur_pic.motion_val[dir][xy + v->blocks_off][0];
457
12.6M
        s->cur_pic.motion_val[dir][xy +    1 +     v->blocks_off][1] = s->cur_pic.motion_val[dir][xy + v->blocks_off][1];
458
12.6M
        s->cur_pic.motion_val[dir][xy + wrap +     v->blocks_off][0] = s->cur_pic.motion_val[dir][xy + v->blocks_off][0];
459
12.6M
        s->cur_pic.motion_val[dir][xy + wrap +     v->blocks_off][1] = s->cur_pic.motion_val[dir][xy + v->blocks_off][1];
460
12.6M
        s->cur_pic.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->cur_pic.motion_val[dir][xy + v->blocks_off][0];
461
12.6M
        s->cur_pic.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->cur_pic.motion_val[dir][xy + v->blocks_off][1];
462
12.6M
        v->mv_f[dir][xy +    1 + v->blocks_off] = v->mv_f[dir][xy +            v->blocks_off];
463
12.6M
        v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
464
12.6M
    }
465
33.3M
}
466
467
/** Predict and set motion vector for interlaced frame picture MBs
468
 */
469
void ff_vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
470
                          int mvn, int r_x, int r_y, int dir)
471
31.6M
{
472
31.6M
    MpegEncContext *s = &v->s;
473
31.6M
    int xy, wrap, off = 0;
474
31.6M
    int A[2], B[2], C[2];
475
31.6M
    int px = 0, py = 0;
476
31.6M
    int a_valid = 0, b_valid = 0, c_valid = 0;
477
31.6M
    int field_a, field_b, field_c; // 0: same, 1: opposite
478
31.6M
    int total_valid, num_samefield, num_oppfield;
479
31.6M
    int pos_c, pos_b, n_adj;
480
481
31.6M
    wrap = s->b8_stride;
482
31.6M
    xy = s->block_index[n];
483
484
31.6M
    if (s->mb_intra) {
485
0
        s->mv[0][n][0] = s->cur_pic.motion_val[0][xy][0] = 0;
486
0
        s->mv[0][n][1] = s->cur_pic.motion_val[0][xy][1] = 0;
487
0
        s->cur_pic.motion_val[1][xy][0] = 0;
488
0
        s->cur_pic.motion_val[1][xy][1] = 0;
489
0
        if (mvn == 1) { /* duplicate motion data for 1-MV block */
490
0
            s->cur_pic.motion_val[0][xy + 1][0]        = 0;
491
0
            s->cur_pic.motion_val[0][xy + 1][1]        = 0;
492
0
            s->cur_pic.motion_val[0][xy + wrap][0]     = 0;
493
0
            s->cur_pic.motion_val[0][xy + wrap][1]     = 0;
494
0
            s->cur_pic.motion_val[0][xy + wrap + 1][0] = 0;
495
0
            s->cur_pic.motion_val[0][xy + wrap + 1][1] = 0;
496
0
            v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
497
0
            s->cur_pic.motion_val[1][xy + 1][0]        = 0;
498
0
            s->cur_pic.motion_val[1][xy + 1][1]        = 0;
499
0
            s->cur_pic.motion_val[1][xy + wrap][0]     = 0;
500
0
            s->cur_pic.motion_val[1][xy + wrap][1]     = 0;
501
0
            s->cur_pic.motion_val[1][xy + wrap + 1][0] = 0;
502
0
            s->cur_pic.motion_val[1][xy + wrap + 1][1] = 0;
503
0
        }
504
0
        return;
505
0
    }
506
507
31.6M
    off = ((n == 0) || (n == 1)) ? 1 : -1;
508
    /* predict A */
509
31.6M
    if (s->mb_x || (n == 1) || (n == 3)) {
510
29.1M
        if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
511
23.0M
            || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
512
23.0M
            A[0] = s->cur_pic.motion_val[dir][xy - 1][0];
513
23.0M
            A[1] = s->cur_pic.motion_val[dir][xy - 1][1];
514
23.0M
            a_valid = 1;
515
23.0M
        } else { // current block has frame mv and cand. has field MV (so average)
516
6.08M
            A[0] = (s->cur_pic.motion_val[dir][xy - 1][0]
517
6.08M
                    + s->cur_pic.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
518
6.08M
            A[1] = (s->cur_pic.motion_val[dir][xy - 1][1]
519
6.08M
                    + s->cur_pic.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
520
6.08M
            a_valid = 1;
521
6.08M
        }
522
29.1M
        if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
523
344k
            a_valid = 0;
524
344k
            A[0] = A[1] = 0;
525
344k
        }
526
29.1M
    } else
527
2.46M
        A[0] = A[1] = 0;
528
    /* Predict B and C */
529
31.6M
    B[0] = B[1] = C[0] = C[1] = 0;
530
31.6M
    if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
531
31.5M
        if (!s->first_slice_line) {
532
27.5M
            if (!v->is_intra[s->mb_x - s->mb_stride]) {
533
27.2M
                b_valid = 1;
534
27.2M
                n_adj   = n | 2;
535
27.2M
                pos_b   = s->block_index[n_adj] - 2 * wrap;
536
27.2M
                if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
537
8.46M
                    n_adj = (n & 2) | (n & 1);
538
8.46M
                }
539
27.2M
                B[0] = s->cur_pic.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
540
27.2M
                B[1] = s->cur_pic.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
541
27.2M
                if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
542
5.69M
                    B[0] = (B[0] + s->cur_pic.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
543
5.69M
                    B[1] = (B[1] + s->cur_pic.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
544
5.69M
                }
545
27.2M
            }
546
27.5M
            if (s->mb_width > 1) {
547
27.3M
                if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
548
27.0M
                    c_valid = 1;
549
27.0M
                    n_adj   = 2;
550
27.0M
                    pos_c   = s->block_index[2] - 2 * wrap + 2;
551
27.0M
                    if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
552
7.55M
                        n_adj = n & 2;
553
7.55M
                    }
554
27.0M
                    C[0] = s->cur_pic.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
555
27.0M
                    C[1] = s->cur_pic.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
556
27.0M
                    if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
557
5.39M
                        C[0] = (1 + C[0] + (s->cur_pic.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
558
5.39M
                        C[1] = (1 + C[1] + (s->cur_pic.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
559
5.39M
                    }
560
27.0M
                    if (s->mb_x == s->mb_width - 1) {
561
1.78M
                        if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
562
1.76M
                            c_valid = 1;
563
1.76M
                            n_adj   = 3;
564
1.76M
                            pos_c   = s->block_index[3] - 2 * wrap - 2;
565
1.76M
                            if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
566
507k
                                n_adj = n | 1;
567
507k
                            }
568
1.76M
                            C[0] = s->cur_pic.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
569
1.76M
                            C[1] = s->cur_pic.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
570
1.76M
                            if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
571
330k
                                C[0] = (1 + C[0] + s->cur_pic.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
572
330k
                                C[1] = (1 + C[1] + s->cur_pic.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
573
330k
                            }
574
1.76M
                        } else
575
14.7k
                            c_valid = 0;
576
1.78M
                    }
577
27.0M
                }
578
27.3M
            }
579
27.5M
        }
580
31.5M
    } else {
581
127k
        pos_b   = s->block_index[1];
582
127k
        b_valid = 1;
583
127k
        B[0]    = s->cur_pic.motion_val[dir][pos_b][0];
584
127k
        B[1]    = s->cur_pic.motion_val[dir][pos_b][1];
585
127k
        pos_c   = s->block_index[0];
586
127k
        c_valid = 1;
587
127k
        C[0]    = s->cur_pic.motion_val[dir][pos_c][0];
588
127k
        C[1]    = s->cur_pic.motion_val[dir][pos_c][1];
589
127k
    }
590
591
31.6M
    total_valid = a_valid + b_valid + c_valid;
592
    // check if predictor A is out of bounds
593
31.6M
    if (!s->mb_x && !(n == 1 || n == 3)) {
594
2.46M
        A[0] = A[1] = 0;
595
2.46M
    }
596
    // check if predictor B is out of bounds
597
31.6M
    if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
598
3.98M
        B[0] = B[1] = C[0] = C[1] = 0;
599
3.98M
    }
600
31.6M
    if (!v->blk_mv_type[xy]) {
601
15.5M
        if (s->mb_width == 1) {
602
112k
            px = B[0];
603
112k
            py = B[1];
604
15.3M
        } else {
605
15.3M
            if (total_valid >= 2) {
606
13.4M
                px = mid_pred(A[0], B[0], C[0]);
607
13.4M
                py = mid_pred(A[1], B[1], C[1]);
608
13.4M
            } else if (total_valid) {
609
1.85M
                if      (a_valid) { px = A[0]; py = A[1]; }
610
25.2k
                else if (b_valid) { px = B[0]; py = B[1]; }
611
13.5k
                else              { px = C[0]; py = C[1]; }
612
1.85M
            }
613
15.3M
        }
614
16.1M
    } else {
615
16.1M
        if (a_valid)
616
14.7M
            field_a = (A[1] & 4) ? 1 : 0;
617
1.36M
        else
618
1.36M
            field_a = 0;
619
16.1M
        if (b_valid)
620
13.8M
            field_b = (B[1] & 4) ? 1 : 0;
621
2.24M
        else
622
2.24M
            field_b = 0;
623
16.1M
        if (c_valid)
624
13.8M
            field_c = (C[1] & 4) ? 1 : 0;
625
2.32M
        else
626
2.32M
            field_c = 0;
627
628
16.1M
        num_oppfield  = field_a + field_b + field_c;
629
16.1M
        num_samefield = total_valid - num_oppfield;
630
16.1M
        if (total_valid == 3) {
631
12.4M
            if ((num_samefield == 3) || (num_oppfield == 3)) {
632
8.45M
                px = mid_pred(A[0], B[0], C[0]);
633
8.45M
                py = mid_pred(A[1], B[1], C[1]);
634
8.45M
            } else if (num_samefield >= num_oppfield) {
635
                /* take one MV from same field set depending on priority
636
                the check for B may not be necessary */
637
2.25M
                px = !field_a ? A[0] : B[0];
638
2.25M
                py = !field_a ? A[1] : B[1];
639
2.25M
            } else {
640
1.76M
                px =  field_a ? A[0] : B[0];
641
1.76M
                py =  field_a ? A[1] : B[1];
642
1.76M
            }
643
12.4M
        } else if (total_valid == 2) {
644
1.49M
            if (num_samefield >= num_oppfield) {
645
1.30M
                if (!field_a && a_valid) {
646
234k
                    px = A[0];
647
234k
                    py = A[1];
648
1.06M
                } else if (!field_b && b_valid) {
649
941k
                    px = B[0];
650
941k
                    py = B[1];
651
941k
                } else /*if (c_valid)*/ {
652
123k
                    av_assert1(c_valid);
653
123k
                    px = C[0];
654
123k
                    py = C[1];
655
123k
                }
656
1.30M
            } else {
657
193k
                if (field_a && a_valid) {
658
58.3k
                    px = A[0];
659
58.3k
                    py = A[1];
660
134k
                } else /*if (field_b && b_valid)*/ {
661
134k
                    av_assert1(field_b && b_valid);
662
134k
                    px = B[0];
663
134k
                    py = B[1];
664
134k
                }
665
193k
            }
666
2.16M
        } else if (total_valid == 1) {
667
2.06M
            px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
668
2.06M
            py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
669
2.06M
        }
670
16.1M
    }
671
672
    /* store MV using signed modulus of MV range defined in 4.11 */
673
31.6M
    s->mv[dir][n][0] = s->cur_pic.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
674
31.6M
    s->mv[dir][n][1] = s->cur_pic.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
675
31.6M
    if (mvn == 1) { /* duplicate motion data for 1-MV block */
676
15.2M
        s->cur_pic.motion_val[dir][xy +    1    ][0] = s->cur_pic.motion_val[dir][xy][0];
677
15.2M
        s->cur_pic.motion_val[dir][xy +    1    ][1] = s->cur_pic.motion_val[dir][xy][1];
678
15.2M
        s->cur_pic.motion_val[dir][xy + wrap    ][0] = s->cur_pic.motion_val[dir][xy][0];
679
15.2M
        s->cur_pic.motion_val[dir][xy + wrap    ][1] = s->cur_pic.motion_val[dir][xy][1];
680
15.2M
        s->cur_pic.motion_val[dir][xy + wrap + 1][0] = s->cur_pic.motion_val[dir][xy][0];
681
15.2M
        s->cur_pic.motion_val[dir][xy + wrap + 1][1] = s->cur_pic.motion_val[dir][xy][1];
682
16.3M
    } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
683
15.0M
        s->cur_pic.motion_val[dir][xy + 1][0] = s->cur_pic.motion_val[dir][xy][0];
684
15.0M
        s->cur_pic.motion_val[dir][xy + 1][1] = s->cur_pic.motion_val[dir][xy][1];
685
15.0M
        s->mv[dir][n + 1][0] = s->mv[dir][n][0];
686
15.0M
        s->mv[dir][n + 1][1] = s->mv[dir][n][1];
687
15.0M
    }
688
31.6M
}
689
690
void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
691
                      int direct, int mvtype)
692
6.59M
{
693
6.59M
    MpegEncContext *s = &v->s;
694
6.59M
    int xy, wrap;
695
6.59M
    int px, py;
696
6.59M
    int sum;
697
6.59M
    int r_x, r_y;
698
6.59M
    const uint8_t *is_intra = v->mb_type;
699
700
6.59M
    av_assert0(!v->field_mode);
701
702
6.59M
    r_x = v->range_x;
703
6.59M
    r_y = v->range_y;
704
    /* scale MV difference to be quad-pel */
705
6.59M
    if (!s->quarter_sample) {
706
3.91M
        dmv_x[0] *= 2;
707
3.91M
        dmv_y[0] *= 2;
708
3.91M
        dmv_x[1] *= 2;
709
3.91M
        dmv_y[1] *= 2;
710
3.91M
    }
711
712
6.59M
    wrap = s->b8_stride;
713
6.59M
    xy = s->block_index[0];
714
715
6.59M
    if (s->mb_intra) {
716
278k
        s->cur_pic.motion_val[0][xy][0] =
717
278k
        s->cur_pic.motion_val[0][xy][1] =
718
278k
        s->cur_pic.motion_val[1][xy][0] =
719
278k
        s->cur_pic.motion_val[1][xy][1] = 0;
720
278k
        return;
721
278k
    }
722
6.31M
    if (direct && s->next_pic.ptr->field_picture)
723
3.37k
        av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
724
725
6.31M
    s->mv[0][0][0] = scale_mv(s->next_pic.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
726
6.31M
    s->mv[0][0][1] = scale_mv(s->next_pic.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
727
6.31M
    s->mv[1][0][0] = scale_mv(s->next_pic.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
728
6.31M
    s->mv[1][0][1] = scale_mv(s->next_pic.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
729
730
    /* Pullback predicted motion vectors as specified in 8.4.5.4 */
731
6.31M
    s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width  << 6) - 4 - (s->mb_x << 6));
732
6.31M
    s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
733
6.31M
    s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width  << 6) - 4 - (s->mb_x << 6));
734
6.31M
    s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
735
6.31M
    if (direct) {
736
2.27M
        s->cur_pic.motion_val[0][xy][0] = s->mv[0][0][0];
737
2.27M
        s->cur_pic.motion_val[0][xy][1] = s->mv[0][0][1];
738
2.27M
        s->cur_pic.motion_val[1][xy][0] = s->mv[1][0][0];
739
2.27M
        s->cur_pic.motion_val[1][xy][1] = s->mv[1][0][1];
740
2.27M
        return;
741
2.27M
    }
742
743
4.04M
    if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
744
2.39M
        int16_t       *C = s->cur_pic.motion_val[0][xy - 2];
745
2.39M
        const int16_t *A = s->cur_pic.motion_val[0][xy - wrap * 2];
746
2.39M
        int off          = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
747
2.39M
        const int16_t *B = s->cur_pic.motion_val[0][xy - wrap * 2 + off];
748
749
2.39M
        if (!s->mb_x) C[0] = C[1] = 0;
750
2.39M
        if (!s->first_slice_line) { // predictor A is not out of bounds
751
2.11M
            if (s->mb_width == 1) {
752
25.2k
                px = A[0];
753
25.2k
                py = A[1];
754
2.09M
            } else {
755
2.09M
                px = mid_pred(A[0], B[0], C[0]);
756
2.09M
                py = mid_pred(A[1], B[1], C[1]);
757
2.09M
            }
758
2.11M
        } else if (s->mb_x) { // predictor C is not out of bounds
759
266k
            px = C[0];
760
266k
            py = C[1];
761
266k
        } else {
762
14.0k
            px = py = 0;
763
14.0k
        }
764
        /* Pullback MV as specified in 8.3.5.3.4 */
765
2.39M
        {
766
2.39M
            int qx, qy, X, Y;
767
2.39M
            int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
768
2.39M
            int MV = 4 - (1 << sh);
769
2.39M
            qx = (s->mb_x << sh);
770
2.39M
            qy = (s->mb_y << sh);
771
2.39M
            X  = (s->mb_width  << sh) - 4;
772
2.39M
            Y  = (s->mb_height << sh) - 4;
773
2.39M
            if (qx + px < MV) px = MV - qx;
774
2.39M
            if (qy + py < MV) py = MV - qy;
775
2.39M
            if (qx + px > X) px = X - qx;
776
2.39M
            if (qy + py > Y) py = Y - qy;
777
2.39M
        }
778
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
779
2.39M
        if (0 && !s->first_slice_line && s->mb_x) {
780
0
            if (is_intra[xy - wrap])
781
0
                sum = FFABS(px) + FFABS(py);
782
0
            else
783
0
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
784
0
            if (sum > 32) {
785
0
                if (get_bits1(&v->gb)) {
786
0
                    px = A[0];
787
0
                    py = A[1];
788
0
                } else {
789
0
                    px = C[0];
790
0
                    py = C[1];
791
0
                }
792
0
            } else {
793
0
                if (is_intra[xy - 2])
794
0
                    sum = FFABS(px) + FFABS(py);
795
0
                else
796
0
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
797
0
                if (sum > 32) {
798
0
                    if (get_bits1(&v->gb)) {
799
0
                        px = A[0];
800
0
                        py = A[1];
801
0
                    } else {
802
0
                        px = C[0];
803
0
                        py = C[1];
804
0
                    }
805
0
                }
806
0
            }
807
0
        }
808
        /* store MV using signed modulus of MV range defined in 4.11 */
809
2.39M
        s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
810
2.39M
        s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
811
2.39M
    }
812
4.04M
    if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
813
2.57M
        int16_t       *C = s->cur_pic.motion_val[1][xy - 2];
814
2.57M
        const int16_t *A = s->cur_pic.motion_val[1][xy - wrap * 2];
815
2.57M
        int off          = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
816
2.57M
        const int16_t *B = s->cur_pic.motion_val[1][xy - wrap * 2 + off];
817
818
2.57M
        if (!s->mb_x)
819
114k
            C[0] = C[1] = 0;
820
2.57M
        if (!s->first_slice_line) { // predictor A is not out of bounds
821
2.35M
            if (s->mb_width == 1) {
822
30.2k
                px = A[0];
823
30.2k
                py = A[1];
824
2.32M
            } else {
825
2.32M
                px = mid_pred(A[0], B[0], C[0]);
826
2.32M
                py = mid_pred(A[1], B[1], C[1]);
827
2.32M
            }
828
2.35M
        } else if (s->mb_x) { // predictor C is not out of bounds
829
212k
            px = C[0];
830
212k
            py = C[1];
831
212k
        } else {
832
14.3k
            px = py = 0;
833
14.3k
        }
834
        /* Pullback MV as specified in 8.3.5.3.4 */
835
2.57M
        {
836
2.57M
            int qx, qy, X, Y;
837
2.57M
            int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
838
2.57M
            int MV = 4 - (1 << sh);
839
2.57M
            qx = (s->mb_x << sh);
840
2.57M
            qy = (s->mb_y << sh);
841
2.57M
            X  = (s->mb_width  << sh) - 4;
842
2.57M
            Y  = (s->mb_height << sh) - 4;
843
2.57M
            if (qx + px < MV) px = MV - qx;
844
2.57M
            if (qy + py < MV) py = MV - qy;
845
2.57M
            if (qx + px > X) px = X - qx;
846
2.57M
            if (qy + py > Y) py = Y - qy;
847
2.57M
        }
848
        /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
849
2.57M
        if (0 && !s->first_slice_line && s->mb_x) {
850
0
            if (is_intra[xy - wrap])
851
0
                sum = FFABS(px) + FFABS(py);
852
0
            else
853
0
                sum = FFABS(px - A[0]) + FFABS(py - A[1]);
854
0
            if (sum > 32) {
855
0
                if (get_bits1(&v->gb)) {
856
0
                    px = A[0];
857
0
                    py = A[1];
858
0
                } else {
859
0
                    px = C[0];
860
0
                    py = C[1];
861
0
                }
862
0
            } else {
863
0
                if (is_intra[xy - 2])
864
0
                    sum = FFABS(px) + FFABS(py);
865
0
                else
866
0
                    sum = FFABS(px - C[0]) + FFABS(py - C[1]);
867
0
                if (sum > 32) {
868
0
                    if (get_bits1(&v->gb)) {
869
0
                        px = A[0];
870
0
                        py = A[1];
871
0
                    } else {
872
0
                        px = C[0];
873
0
                        py = C[1];
874
0
                    }
875
0
                }
876
0
            }
877
0
        }
878
        /* store MV using signed modulus of MV range defined in 4.11 */
879
880
2.57M
        s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
881
2.57M
        s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
882
2.57M
    }
883
4.04M
    s->cur_pic.motion_val[0][xy][0] = s->mv[0][0][0];
884
4.04M
    s->cur_pic.motion_val[0][xy][1] = s->mv[0][0][1];
885
4.04M
    s->cur_pic.motion_val[1][xy][0] = s->mv[1][0][0];
886
4.04M
    s->cur_pic.motion_val[1][xy][1] = s->mv[1][0][1];
887
4.04M
}
888
889
void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y,
890
                            int mv1, int *pred_flag)
891
1.61M
{
892
1.61M
    int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
893
1.61M
    MpegEncContext *s = &v->s;
894
1.61M
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
895
896
1.61M
    if (v->bmvtype == BMV_TYPE_DIRECT) {
897
11.5k
        int total_opp, k, f;
898
11.5k
        if (s->next_pic.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
899
10.9k
            s->mv[0][0][0] = scale_mv(s->next_pic.motion_val[1][s->block_index[0] + v->blocks_off][0],
900
10.9k
                                      v->bfraction, 0, s->quarter_sample);
901
10.9k
            s->mv[0][0][1] = scale_mv(s->next_pic.motion_val[1][s->block_index[0] + v->blocks_off][1],
902
10.9k
                                      v->bfraction, 0, s->quarter_sample);
903
10.9k
            s->mv[1][0][0] = scale_mv(s->next_pic.motion_val[1][s->block_index[0] + v->blocks_off][0],
904
10.9k
                                      v->bfraction, 1, s->quarter_sample);
905
10.9k
            s->mv[1][0][1] = scale_mv(s->next_pic.motion_val[1][s->block_index[0] + v->blocks_off][1],
906
10.9k
                                      v->bfraction, 1, s->quarter_sample);
907
908
10.9k
            total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
909
10.9k
                      + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
910
10.9k
                      + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
911
10.9k
                      + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
912
10.9k
            f = (total_opp > 2) ? 1 : 0;
913
10.9k
        } else {
914
567
            s->mv[0][0][0] = s->mv[0][0][1] = 0;
915
567
            s->mv[1][0][0] = s->mv[1][0][1] = 0;
916
567
            f = 0;
917
567
        }
918
11.5k
        v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
919
57.5k
        for (k = 0; k < 4; k++) {
920
46.0k
            s->cur_pic.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
921
46.0k
            s->cur_pic.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
922
46.0k
            s->cur_pic.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
923
46.0k
            s->cur_pic.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
924
46.0k
            v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
925
46.0k
            v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
926
46.0k
        }
927
11.5k
        return;
928
11.5k
    }
929
1.60M
    if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
930
108k
        ff_vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0],   1, v->range_x, v->range_y, v->mb_type, pred_flag[0], 0);
931
108k
        ff_vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1],   1, v->range_x, v->range_y, v->mb_type, pred_flag[1], 1);
932
108k
        return;
933
108k
    }
934
1.49M
    if (dir) { // backward
935
770k
        ff_vc1_pred_mv(v, n, dmv_x[1], dmv_y[1], mv1, v->range_x, v->range_y, v->mb_type, pred_flag[1], 1);
936
770k
        if (n == 3 || mv1) {
937
413k
            ff_vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0],   1, v->range_x, v->range_y, v->mb_type, 0, 0);
938
413k
        }
939
770k
    } else { // forward
940
728k
        ff_vc1_pred_mv(v, n, dmv_x[0], dmv_y[0], mv1, v->range_x, v->range_y, v->mb_type, pred_flag[0], 0);
941
728k
        if (n == 3 || mv1) {
942
507k
            ff_vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1],   1, v->range_x, v->range_y, v->mb_type, 0, 1);
943
507k
        }
944
728k
    }
945
1.49M
}