]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1_pred.c
Merge commit '9485cce6d55baf547e92ef1f54cad117f2a38287'
[ffmpeg] / libavcodec / vc1_pred.c
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(VC1Context *v, int n /* MV */, int dir)
37 {
38     int scaledvalue, refdist;
39     int scalesame1, scalesame2;
40     int scalezone1_x, zone1offset_x;
41     int table_index = dir ^ v->second_field;
42
43     if (v->s.pict_type != AV_PICTURE_TYPE_B)
44         refdist = v->refdist;
45     else
46         refdist = dir ? v->brfd : v->frfd;
47     if (refdist > 3)
48         refdist = 3;
49     scalesame1    = ff_vc1_field_mvpred_scales[table_index][1][refdist];
50     scalesame2    = ff_vc1_field_mvpred_scales[table_index][2][refdist];
51     scalezone1_x  = ff_vc1_field_mvpred_scales[table_index][3][refdist];
52     zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
53
54     if (FFABS(n) > 255)
55         scaledvalue = n;
56     else {
57         if (FFABS(n) < scalezone1_x)
58             scaledvalue = (n * scalesame1) >> 8;
59         else {
60             if (n < 0)
61                 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
62             else
63                 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
64         }
65     }
66     return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
67 }
68
69 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
70 {
71     int scaledvalue, refdist;
72     int scalesame1, scalesame2;
73     int scalezone1_y, zone1offset_y;
74     int table_index = dir ^ v->second_field;
75
76     if (v->s.pict_type != AV_PICTURE_TYPE_B)
77         refdist = v->refdist;
78     else
79         refdist = dir ? v->brfd : v->frfd;
80     if (refdist > 3)
81         refdist = 3;
82     scalesame1    = ff_vc1_field_mvpred_scales[table_index][1][refdist];
83     scalesame2    = ff_vc1_field_mvpred_scales[table_index][2][refdist];
84     scalezone1_y  = ff_vc1_field_mvpred_scales[table_index][4][refdist];
85     zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
86
87     if (FFABS(n) > 63)
88         scaledvalue = n;
89     else {
90         if (FFABS(n) < scalezone1_y)
91             scaledvalue = (n * scalesame1) >> 8;
92         else {
93             if (n < 0)
94                 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
95             else
96                 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
97         }
98     }
99
100     if (v->cur_field_type && !v->ref_field_type[dir])
101         return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
102     else
103         return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
104 }
105
106 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
107 {
108     int scalezone1_x, zone1offset_x;
109     int scaleopp1, scaleopp2, brfd;
110     int scaledvalue;
111
112     brfd = FFMIN(v->brfd, 3);
113     scalezone1_x  = ff_vc1_b_field_mvpred_scales[3][brfd];
114     zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
115     scaleopp1     = ff_vc1_b_field_mvpred_scales[1][brfd];
116     scaleopp2     = ff_vc1_b_field_mvpred_scales[2][brfd];
117
118     if (FFABS(n) > 255)
119         scaledvalue = n;
120     else {
121         if (FFABS(n) < scalezone1_x)
122             scaledvalue = (n * scaleopp1) >> 8;
123         else {
124             if (n < 0)
125                 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
126             else
127                 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
128         }
129     }
130     return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
131 }
132
133 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
134 {
135     int scalezone1_y, zone1offset_y;
136     int scaleopp1, scaleopp2, brfd;
137     int scaledvalue;
138
139     brfd = FFMIN(v->brfd, 3);
140     scalezone1_y  = ff_vc1_b_field_mvpred_scales[4][brfd];
141     zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
142     scaleopp1     = ff_vc1_b_field_mvpred_scales[1][brfd];
143     scaleopp2     = ff_vc1_b_field_mvpred_scales[2][brfd];
144
145     if (FFABS(n) > 63)
146         scaledvalue = n;
147     else {
148         if (FFABS(n) < scalezone1_y)
149             scaledvalue = (n * scaleopp1) >> 8;
150         else {
151             if (n < 0)
152                 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
153             else
154                 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
155         }
156     }
157     if (v->cur_field_type && !v->ref_field_type[dir]) {
158         return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
159     } else {
160         return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
161     }
162 }
163
164 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
165                                          int dim, int dir)
166 {
167     int brfd, scalesame;
168     int hpel = 1 - v->s.quarter_sample;
169
170     n >>= hpel;
171     if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
172         if (dim)
173             n = scaleforsame_y(v, i, n, dir) * (1 << hpel);
174         else
175             n = scaleforsame_x(v, n, dir) * (1 << hpel);
176         return n;
177     }
178     brfd      = FFMIN(v->brfd, 3);
179     scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
180
181     n = (n * scalesame >> 8) << hpel;
182     return n;
183 }
184
185 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
186                                         int dim, int dir)
187 {
188     int refdist, scaleopp;
189     int hpel = 1 - v->s.quarter_sample;
190
191     n >>= hpel;
192     if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
193         if (dim)
194             n = scaleforopp_y(v, n, dir) << hpel;
195         else
196             n = scaleforopp_x(v, n) << hpel;
197         return n;
198     }
199     if (v->s.pict_type != AV_PICTURE_TYPE_B)
200         refdist = FFMIN(v->refdist, 3);
201     else
202         refdist = dir ? v->brfd : v->frfd;
203     scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
204
205     n = (n * scaleopp >> 8) * (1 << hpel);
206     return n;
207 }
208
209 /** Predict and set motion vector
210  */
211 void ff_vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
212                     int mv1, int r_x, int r_y, uint8_t* is_intra,
213                     int pred_flag, int dir)
214 {
215     MpegEncContext *s = &v->s;
216     int xy, wrap, off = 0;
217     int16_t *A, *B, *C;
218     int px, py;
219     int sum;
220     int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
221     int opposite, a_f, b_f, c_f;
222     int16_t field_predA[2];
223     int16_t field_predB[2];
224     int16_t field_predC[2];
225     int a_valid, b_valid, c_valid;
226     int hybridmv_thresh, y_bias = 0;
227
228     if (v->mv_mode == MV_PMODE_MIXED_MV ||
229         ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
230         mixedmv_pic = 1;
231     else
232         mixedmv_pic = 0;
233     /* scale MV difference to be quad-pel */
234     if (!s->quarter_sample) {
235         dmv_x *= 2;
236         dmv_y *= 2;
237     }
238
239     wrap = s->b8_stride;
240     xy   = s->block_index[n];
241
242     if (s->mb_intra) {
243         s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
244         s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
245         s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
246         s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
247         if (mv1) { /* duplicate motion data for 1-MV block */
248             s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0]        = 0;
249             s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1]        = 0;
250             s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0]     = 0;
251             s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1]     = 0;
252             s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
253             s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
254             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
255             s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0]        = 0;
256             s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1]        = 0;
257             s->current_picture.motion_val[1][xy + wrap + v->blocks_off][0]     = 0;
258             s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1]     = 0;
259             s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
260             s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
261         }
262         return;
263     }
264
265     a_valid = !s->first_slice_line || (n == 2 || n == 3);
266     b_valid = a_valid;
267     c_valid = s->mb_x || (n == 1 || n == 3);
268     if (mv1) {
269         if (v->field_mode && mixedmv_pic)
270             off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
271         else
272             off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
273         b_valid = b_valid && s->mb_width > 1;
274     } else {
275         //in 4-MV mode different blocks have different B predictor position
276         switch (n) {
277         case 0:
278             if (v->res_rtm_flag)
279                 off = s->mb_x ? -1 : 1;
280             else
281                 off = s->mb_x ? -1 : 2 * s->mb_width - wrap - 1;
282             break;
283         case 1:
284             off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
285             break;
286         case 2:
287             off = 1;
288             break;
289         case 3:
290             off = -1;
291         }
292         if (v->field_mode && s->mb_width == 1)
293             b_valid = b_valid && c_valid;
294     }
295
296     if (v->field_mode) {
297         a_valid = a_valid && !is_intra[xy - wrap];
298         b_valid = b_valid && !is_intra[xy - wrap + off];
299         c_valid = c_valid && !is_intra[xy - 1];
300     }
301
302     if (a_valid) {
303         A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
304         a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
305         num_oppfield  += a_f;
306         num_samefield += 1 - a_f;
307         field_predA[0] = A[0];
308         field_predA[1] = A[1];
309     } else {
310         field_predA[0] = field_predA[1] = 0;
311         a_f = 0;
312     }
313     if (b_valid) {
314         B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
315         b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
316         num_oppfield  += b_f;
317         num_samefield += 1 - b_f;
318         field_predB[0] = B[0];
319         field_predB[1] = B[1];
320     } else {
321         field_predB[0] = field_predB[1] = 0;
322         b_f = 0;
323     }
324     if (c_valid) {
325         C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
326         c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
327         num_oppfield  += c_f;
328         num_samefield += 1 - c_f;
329         field_predC[0] = C[0];
330         field_predC[1] = C[1];
331     } else {
332         field_predC[0] = field_predC[1] = 0;
333         c_f = 0;
334     }
335
336     if (v->field_mode) {
337         if (!v->numref)
338             // REFFIELD determines if the last field or the second-last field is
339             // to be used as reference
340             opposite = 1 - v->reffield;
341         else {
342             if (num_samefield <= num_oppfield)
343                 opposite = 1 - pred_flag;
344             else
345                 opposite = pred_flag;
346         }
347     } else
348         opposite = 0;
349     if (opposite) {
350         v->mv_f[dir][xy + v->blocks_off] = 1;
351         v->ref_field_type[dir] = !v->cur_field_type;
352         if (a_valid && !a_f) {
353             field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
354             field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
355         }
356         if (b_valid && !b_f) {
357             field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
358             field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
359         }
360         if (c_valid && !c_f) {
361             field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
362             field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
363         }
364     } else {
365         v->mv_f[dir][xy + v->blocks_off] = 0;
366         v->ref_field_type[dir] = v->cur_field_type;
367         if (a_valid && a_f) {
368             field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
369             field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
370         }
371         if (b_valid && b_f) {
372             field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
373             field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
374         }
375         if (c_valid && c_f) {
376             field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
377             field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
378         }
379     }
380
381     if (a_valid) {
382         px = field_predA[0];
383         py = field_predA[1];
384     } else if (c_valid) {
385         px = field_predC[0];
386         py = field_predC[1];
387     } else if (b_valid) {
388         px = field_predB[0];
389         py = field_predB[1];
390     } else {
391         px = 0;
392         py = 0;
393     }
394
395     if (num_samefield + num_oppfield > 1) {
396         px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
397         py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
398     }
399
400     /* Pullback MV as specified in 8.3.5.3.4 */
401     if (!v->field_mode) {
402         int qx, qy, X, Y;
403         int MV = mv1 ? -60 : -28;
404         qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
405         qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
406         X  = (s->mb_width  << 6) - 4;
407         Y  = (s->mb_height << 6) - 4;
408         if (qx + px < MV) px = MV - qx;
409         if (qy + py < MV) py = MV - qy;
410         if (qx + px > X) px = X - qx;
411         if (qy + py > Y) py = Y - qy;
412     }
413
414     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         hybridmv_thresh = 32;
417         if (a_valid && c_valid) {
418             if (is_intra[xy - wrap])
419                 sum = FFABS(px) + FFABS(py);
420             else
421                 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
422             if (sum > hybridmv_thresh) {
423                 if (get_bits1(&s->gb)) {     // read HYBRIDPRED bit
424                     px = field_predA[0];
425                     py = field_predA[1];
426                 } else {
427                     px = field_predC[0];
428                     py = field_predC[1];
429                 }
430             } else {
431                 if (is_intra[xy - 1])
432                     sum = FFABS(px) + FFABS(py);
433                 else
434                     sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
435                 if (sum > hybridmv_thresh) {
436                     if (get_bits1(&s->gb)) {
437                         px = field_predA[0];
438                         py = field_predA[1];
439                     } else {
440                         px = field_predC[0];
441                         py = field_predC[1];
442                     }
443                 }
444             }
445         }
446     }
447
448     if (v->field_mode && v->numref)
449         r_y >>= 1;
450     if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
451         y_bias = 1;
452     /* store MV using signed modulus of MV range defined in 4.11 */
453     s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
454     s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
455     if (mv1) { /* duplicate motion data for 1-MV block */
456         s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
457         s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
458         s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
459         s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
460         s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
461         s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
462         v->mv_f[dir][xy +    1 + v->blocks_off] = v->mv_f[dir][xy +            v->blocks_off];
463         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     }
465 }
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, uint8_t* is_intra, int dir)
471 {
472     MpegEncContext *s = &v->s;
473     int xy, wrap, off = 0;
474     int A[2], B[2], C[2];
475     int px = 0, py = 0;
476     int a_valid = 0, b_valid = 0, c_valid = 0;
477     int field_a, field_b, field_c; // 0: same, 1: opposite
478     int total_valid, num_samefield, num_oppfield;
479     int pos_c, pos_b, n_adj;
480
481     wrap = s->b8_stride;
482     xy = s->block_index[n];
483
484     if (s->mb_intra) {
485         s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
486         s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
487         s->current_picture.motion_val[1][xy][0] = 0;
488         s->current_picture.motion_val[1][xy][1] = 0;
489         if (mvn == 1) { /* duplicate motion data for 1-MV block */
490             s->current_picture.motion_val[0][xy + 1][0]        = 0;
491             s->current_picture.motion_val[0][xy + 1][1]        = 0;
492             s->current_picture.motion_val[0][xy + wrap][0]     = 0;
493             s->current_picture.motion_val[0][xy + wrap][1]     = 0;
494             s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
495             s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
496             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
497             s->current_picture.motion_val[1][xy + 1][0]        = 0;
498             s->current_picture.motion_val[1][xy + 1][1]        = 0;
499             s->current_picture.motion_val[1][xy + wrap][0]     = 0;
500             s->current_picture.motion_val[1][xy + wrap][1]     = 0;
501             s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
502             s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
503         }
504         return;
505     }
506
507     off = ((n == 0) || (n == 1)) ? 1 : -1;
508     /* predict A */
509     if (s->mb_x || (n == 1) || (n == 3)) {
510         if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
511             || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
512             A[0] = s->current_picture.motion_val[dir][xy - 1][0];
513             A[1] = s->current_picture.motion_val[dir][xy - 1][1];
514             a_valid = 1;
515         } else { // current block has frame mv and cand. has field MV (so average)
516             A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
517                     + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
518             A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
519                     + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
520             a_valid = 1;
521         }
522         if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
523             a_valid = 0;
524             A[0] = A[1] = 0;
525         }
526     } else
527         A[0] = A[1] = 0;
528     /* Predict B and C */
529     B[0] = B[1] = C[0] = C[1] = 0;
530     if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
531         if (!s->first_slice_line) {
532             if (!v->is_intra[s->mb_x - s->mb_stride]) {
533                 b_valid = 1;
534                 n_adj   = n | 2;
535                 pos_b   = s->block_index[n_adj] - 2 * wrap;
536                 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
537                     n_adj = (n & 2) | (n & 1);
538                 }
539                 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
540                 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
541                 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
542                     B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
543                     B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
544                 }
545             }
546             if (s->mb_width > 1) {
547                 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
548                     c_valid = 1;
549                     n_adj   = 2;
550                     pos_c   = s->block_index[2] - 2 * wrap + 2;
551                     if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
552                         n_adj = n & 2;
553                     }
554                     C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
555                     C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
556                     if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
557                         C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
558                         C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
559                     }
560                     if (s->mb_x == s->mb_width - 1) {
561                         if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
562                             c_valid = 1;
563                             n_adj   = 3;
564                             pos_c   = s->block_index[3] - 2 * wrap - 2;
565                             if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
566                                 n_adj = n | 1;
567                             }
568                             C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
569                             C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
570                             if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
571                                 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
572                                 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
573                             }
574                         } else
575                             c_valid = 0;
576                     }
577                 }
578             }
579         }
580     } else {
581         pos_b   = s->block_index[1];
582         b_valid = 1;
583         B[0]    = s->current_picture.motion_val[dir][pos_b][0];
584         B[1]    = s->current_picture.motion_val[dir][pos_b][1];
585         pos_c   = s->block_index[0];
586         c_valid = 1;
587         C[0]    = s->current_picture.motion_val[dir][pos_c][0];
588         C[1]    = s->current_picture.motion_val[dir][pos_c][1];
589     }
590
591     total_valid = a_valid + b_valid + c_valid;
592     // check if predictor A is out of bounds
593     if (!s->mb_x && !(n == 1 || n == 3)) {
594         A[0] = A[1] = 0;
595     }
596     // check if predictor B is out of bounds
597     if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
598         B[0] = B[1] = C[0] = C[1] = 0;
599     }
600     if (!v->blk_mv_type[xy]) {
601         if (s->mb_width == 1) {
602             px = B[0];
603             py = B[1];
604         } else {
605             if (total_valid >= 2) {
606                 px = mid_pred(A[0], B[0], C[0]);
607                 py = mid_pred(A[1], B[1], C[1]);
608             } else if (total_valid) {
609                 if      (a_valid) { px = A[0]; py = A[1]; }
610                 else if (b_valid) { px = B[0]; py = B[1]; }
611                 else              { px = C[0]; py = C[1]; }
612             }
613         }
614     } else {
615         if (a_valid)
616             field_a = (A[1] & 4) ? 1 : 0;
617         else
618             field_a = 0;
619         if (b_valid)
620             field_b = (B[1] & 4) ? 1 : 0;
621         else
622             field_b = 0;
623         if (c_valid)
624             field_c = (C[1] & 4) ? 1 : 0;
625         else
626             field_c = 0;
627
628         num_oppfield  = field_a + field_b + field_c;
629         num_samefield = total_valid - num_oppfield;
630         if (total_valid == 3) {
631             if ((num_samefield == 3) || (num_oppfield == 3)) {
632                 px = mid_pred(A[0], B[0], C[0]);
633                 py = mid_pred(A[1], B[1], C[1]);
634             } 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                 px = !field_a ? A[0] : B[0];
638                 py = !field_a ? A[1] : B[1];
639             } else {
640                 px =  field_a ? A[0] : B[0];
641                 py =  field_a ? A[1] : B[1];
642             }
643         } else if (total_valid == 2) {
644             if (num_samefield >= num_oppfield) {
645                 if (!field_a && a_valid) {
646                     px = A[0];
647                     py = A[1];
648                 } else if (!field_b && b_valid) {
649                     px = B[0];
650                     py = B[1];
651                 } else /*if (c_valid)*/ {
652                     av_assert1(c_valid);
653                     px = C[0];
654                     py = C[1];
655                 }
656             } else {
657                 if (field_a && a_valid) {
658                     px = A[0];
659                     py = A[1];
660                 } else /*if (field_b && b_valid)*/ {
661                     av_assert1(field_b && b_valid);
662                     px = B[0];
663                     py = B[1];
664                 }
665             }
666         } else if (total_valid == 1) {
667             px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
668             py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
669         }
670     }
671
672     /* store MV using signed modulus of MV range defined in 4.11 */
673     s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
674     s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
675     if (mvn == 1) { /* duplicate motion data for 1-MV block */
676         s->current_picture.motion_val[dir][xy +    1    ][0] = s->current_picture.motion_val[dir][xy][0];
677         s->current_picture.motion_val[dir][xy +    1    ][1] = s->current_picture.motion_val[dir][xy][1];
678         s->current_picture.motion_val[dir][xy + wrap    ][0] = s->current_picture.motion_val[dir][xy][0];
679         s->current_picture.motion_val[dir][xy + wrap    ][1] = s->current_picture.motion_val[dir][xy][1];
680         s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
681         s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
682     } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
683         s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
684         s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
685         s->mv[dir][n + 1][0] = s->mv[dir][n][0];
686         s->mv[dir][n + 1][1] = s->mv[dir][n][1];
687     }
688 }
689
690 void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
691                       int direct, int mvtype)
692 {
693     MpegEncContext *s = &v->s;
694     int xy, wrap, off = 0;
695     int16_t *A, *B, *C;
696     int px, py;
697     int sum;
698     int r_x, r_y;
699     const uint8_t *is_intra = v->mb_type[0];
700
701     av_assert0(!v->field_mode);
702
703     r_x = v->range_x;
704     r_y = v->range_y;
705     /* scale MV difference to be quad-pel */
706     if (!s->quarter_sample) {
707         dmv_x[0] *= 2;
708         dmv_y[0] *= 2;
709         dmv_x[1] *= 2;
710         dmv_y[1] *= 2;
711     }
712
713     wrap = s->b8_stride;
714     xy = s->block_index[0];
715
716     if (s->mb_intra) {
717         s->current_picture.motion_val[0][xy][0] =
718         s->current_picture.motion_val[0][xy][1] =
719         s->current_picture.motion_val[1][xy][0] =
720         s->current_picture.motion_val[1][xy][1] = 0;
721         return;
722     }
723         if (direct && s->next_picture_ptr->field_picture)
724             av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
725
726         s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
727         s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
728         s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
729         s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
730
731         /* Pullback predicted motion vectors as specified in 8.4.5.4 */
732         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));
733         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));
734         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));
735         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));
736     if (direct) {
737         s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
738         s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
739         s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
740         s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
741         return;
742     }
743
744     if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
745         C   = s->current_picture.motion_val[0][xy - 2];
746         A   = s->current_picture.motion_val[0][xy - wrap * 2];
747         off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
748         B   = s->current_picture.motion_val[0][xy - wrap * 2 + off];
749
750         if (!s->mb_x) C[0] = C[1] = 0;
751         if (!s->first_slice_line) { // predictor A is not out of bounds
752             if (s->mb_width == 1) {
753                 px = A[0];
754                 py = A[1];
755             } else {
756                 px = mid_pred(A[0], B[0], C[0]);
757                 py = mid_pred(A[1], B[1], C[1]);
758             }
759         } else if (s->mb_x) { // predictor C is not out of bounds
760             px = C[0];
761             py = C[1];
762         } else {
763             px = py = 0;
764         }
765         /* Pullback MV as specified in 8.3.5.3.4 */
766         {
767             int qx, qy, X, Y;
768             int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
769             int MV = 4 - (1 << sh);
770             qx = (s->mb_x << sh);
771             qy = (s->mb_y << sh);
772             X  = (s->mb_width  << sh) - 4;
773             Y  = (s->mb_height << sh) - 4;
774             if (qx + px < MV) px = MV - qx;
775             if (qy + py < MV) py = MV - qy;
776             if (qx + px > X) px = X - qx;
777             if (qy + py > Y) py = Y - qy;
778         }
779         /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
780         if (0 && !s->first_slice_line && s->mb_x) {
781             if (is_intra[xy - wrap])
782                 sum = FFABS(px) + FFABS(py);
783             else
784                 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
785             if (sum > 32) {
786                 if (get_bits1(&s->gb)) {
787                     px = A[0];
788                     py = A[1];
789                 } else {
790                     px = C[0];
791                     py = C[1];
792                 }
793             } else {
794                 if (is_intra[xy - 2])
795                     sum = FFABS(px) + FFABS(py);
796                 else
797                     sum = FFABS(px - C[0]) + FFABS(py - C[1]);
798                 if (sum > 32) {
799                     if (get_bits1(&s->gb)) {
800                         px = A[0];
801                         py = A[1];
802                     } else {
803                         px = C[0];
804                         py = C[1];
805                     }
806                 }
807             }
808         }
809         /* store MV using signed modulus of MV range defined in 4.11 */
810         s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
811         s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
812     }
813     if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
814         C   = s->current_picture.motion_val[1][xy - 2];
815         A   = s->current_picture.motion_val[1][xy - wrap * 2];
816         off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
817         B   = s->current_picture.motion_val[1][xy - wrap * 2 + off];
818
819         if (!s->mb_x)
820             C[0] = C[1] = 0;
821         if (!s->first_slice_line) { // predictor A is not out of bounds
822             if (s->mb_width == 1) {
823                 px = A[0];
824                 py = A[1];
825             } else {
826                 px = mid_pred(A[0], B[0], C[0]);
827                 py = mid_pred(A[1], B[1], C[1]);
828             }
829         } else if (s->mb_x) { // predictor C is not out of bounds
830             px = C[0];
831             py = C[1];
832         } else {
833             px = py = 0;
834         }
835         /* Pullback MV as specified in 8.3.5.3.4 */
836         {
837             int qx, qy, X, Y;
838             int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
839             int MV = 4 - (1 << sh);
840             qx = (s->mb_x << sh);
841             qy = (s->mb_y << sh);
842             X  = (s->mb_width  << sh) - 4;
843             Y  = (s->mb_height << sh) - 4;
844             if (qx + px < MV) px = MV - qx;
845             if (qy + py < MV) py = MV - qy;
846             if (qx + px > X) px = X - qx;
847             if (qy + py > Y) py = Y - qy;
848         }
849         /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
850         if (0 && !s->first_slice_line && s->mb_x) {
851             if (is_intra[xy - wrap])
852                 sum = FFABS(px) + FFABS(py);
853             else
854                 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
855             if (sum > 32) {
856                 if (get_bits1(&s->gb)) {
857                     px = A[0];
858                     py = A[1];
859                 } else {
860                     px = C[0];
861                     py = C[1];
862                 }
863             } else {
864                 if (is_intra[xy - 2])
865                     sum = FFABS(px) + FFABS(py);
866                 else
867                     sum = FFABS(px - C[0]) + FFABS(py - C[1]);
868                 if (sum > 32) {
869                     if (get_bits1(&s->gb)) {
870                         px = A[0];
871                         py = A[1];
872                     } else {
873                         px = C[0];
874                         py = C[1];
875                     }
876                 }
877             }
878         }
879         /* store MV using signed modulus of MV range defined in 4.11 */
880
881         s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
882         s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
883     }
884     s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
885     s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
886     s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
887     s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
888 }
889
890 void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y,
891                             int mv1, int *pred_flag)
892 {
893     int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
894     MpegEncContext *s = &v->s;
895     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
896
897     if (v->bmvtype == BMV_TYPE_DIRECT) {
898         int total_opp, k, f;
899         if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
900             s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
901                                       v->bfraction, 0, s->quarter_sample);
902             s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
903                                       v->bfraction, 0, s->quarter_sample);
904             s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
905                                       v->bfraction, 1, s->quarter_sample);
906             s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
907                                       v->bfraction, 1, s->quarter_sample);
908
909             total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
910                       + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
911                       + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
912                       + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
913             f = (total_opp > 2) ? 1 : 0;
914         } else {
915             s->mv[0][0][0] = s->mv[0][0][1] = 0;
916             s->mv[1][0][0] = s->mv[1][0][1] = 0;
917             f = 0;
918         }
919         v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
920         for (k = 0; k < 4; k++) {
921             s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
922             s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
923             s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
924             s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
925             v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
926             v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
927         }
928         return;
929     }
930     if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
931         ff_vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0],   1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
932         ff_vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1],   1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
933         return;
934     }
935     if (dir) { // backward
936         ff_vc1_pred_mv(v, n, dmv_x[1], dmv_y[1], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
937         if (n == 3 || mv1) {
938             ff_vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0],   1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
939         }
940     } else { // forward
941         ff_vc1_pred_mv(v, n, dmv_x[0], dmv_y[0], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
942         if (n == 3 || mv1) {
943             ff_vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1],   1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
944         }
945     }
946 }