]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1_pred.c
Merge commit '31d2039cb42668ebcf08248bc48bbad44aa05f49'
[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) << hpel;
174         else
175             n = scaleforsame_x(v, n, dir) << 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) << 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     dmv_x <<= 1 - s->quarter_sample;
235     dmv_y <<= 1 - s->quarter_sample;
236
237     wrap = s->b8_stride;
238     xy   = s->block_index[n];
239
240     if (s->mb_intra) {
241         s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
242         s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
243         s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
244         s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
245         if (mv1) { /* duplicate motion data for 1-MV block */
246             s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0]        = 0;
247             s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1]        = 0;
248             s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0]     = 0;
249             s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1]     = 0;
250             s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
251             s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
252             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
253             s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0]        = 0;
254             s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1]        = 0;
255             s->current_picture.motion_val[1][xy + wrap][0]                     = 0;
256             s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1]     = 0;
257             s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
258             s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
259         }
260         return;
261     }
262
263     C = s->current_picture.motion_val[dir][xy -    1 + v->blocks_off];
264     A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
265     if (mv1) {
266         if (v->field_mode && mixedmv_pic)
267             off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
268         else
269             off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
270     } else {
271         //in 4-MV mode different blocks have different B predictor position
272         switch (n) {
273         case 0:
274             off = (s->mb_x > 0) ? -1 : 1;
275             break;
276         case 1:
277             off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
278             break;
279         case 2:
280             off = 1;
281             break;
282         case 3:
283             off = -1;
284         }
285     }
286     B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
287
288     a_valid = !s->first_slice_line || (n == 2 || n == 3);
289     b_valid = a_valid && (s->mb_width > 1);
290     c_valid = s->mb_x || (n == 1 || n == 3);
291     if (v->field_mode) {
292         a_valid = a_valid && !is_intra[xy - wrap];
293         b_valid = b_valid && !is_intra[xy - wrap + off];
294         c_valid = c_valid && !is_intra[xy - 1];
295     }
296
297     if (a_valid) {
298         a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
299         num_oppfield  += a_f;
300         num_samefield += 1 - a_f;
301         field_predA[0] = A[0];
302         field_predA[1] = A[1];
303     } else {
304         field_predA[0] = field_predA[1] = 0;
305         a_f = 0;
306     }
307     if (b_valid) {
308         b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
309         num_oppfield  += b_f;
310         num_samefield += 1 - b_f;
311         field_predB[0] = B[0];
312         field_predB[1] = B[1];
313     } else {
314         field_predB[0] = field_predB[1] = 0;
315         b_f = 0;
316     }
317     if (c_valid) {
318         c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
319         num_oppfield  += c_f;
320         num_samefield += 1 - c_f;
321         field_predC[0] = C[0];
322         field_predC[1] = C[1];
323     } else {
324         field_predC[0] = field_predC[1] = 0;
325         c_f = 0;
326     }
327
328     if (v->field_mode) {
329         if (!v->numref)
330             // REFFIELD determines if the last field or the second-last field is
331             // to be used as reference
332             opposite = 1 - v->reffield;
333         else {
334             if (num_samefield <= num_oppfield)
335                 opposite = 1 - pred_flag;
336             else
337                 opposite = pred_flag;
338         }
339     } else
340         opposite = 0;
341     if (opposite) {
342         if (a_valid && !a_f) {
343             field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
344             field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
345         }
346         if (b_valid && !b_f) {
347             field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
348             field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
349         }
350         if (c_valid && !c_f) {
351             field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
352             field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
353         }
354         v->mv_f[dir][xy + v->blocks_off] = 1;
355         v->ref_field_type[dir] = !v->cur_field_type;
356     } else {
357         if (a_valid && a_f) {
358             field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
359             field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
360         }
361         if (b_valid && b_f) {
362             field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
363             field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
364         }
365         if (c_valid && c_f) {
366             field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
367             field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
368         }
369         v->mv_f[dir][xy + v->blocks_off] = 0;
370         v->ref_field_type[dir] = v->cur_field_type;
371     }
372
373     if (a_valid) {
374         px = field_predA[0];
375         py = field_predA[1];
376     } else if (c_valid) {
377         px = field_predC[0];
378         py = field_predC[1];
379     } else if (b_valid) {
380         px = field_predB[0];
381         py = field_predB[1];
382     } else {
383         px = 0;
384         py = 0;
385     }
386
387     if (num_samefield + num_oppfield > 1) {
388         px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
389         py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
390     }
391
392     /* Pullback MV as specified in 8.3.5.3.4 */
393     if (!v->field_mode) {
394         int qx, qy, X, Y;
395         int MV = mv1 ? -60 : -28;
396         qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
397         qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
398         X  = (s->mb_width  << 6) - 4;
399         Y  = (s->mb_height << 6) - 4;
400         if (qx + px < MV) px = MV - qx;
401         if (qy + py < MV) py = MV - qy;
402         if (qx + px > X) px = X - qx;
403         if (qy + py > Y) py = Y - qy;
404     }
405
406     if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
407         /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
408         hybridmv_thresh = 32;
409         if (a_valid && c_valid) {
410             if (is_intra[xy - wrap])
411                 sum = FFABS(px) + FFABS(py);
412             else
413                 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
414             if (sum > hybridmv_thresh) {
415                 if (get_bits1(&s->gb)) {     // read HYBRIDPRED bit
416                     px = field_predA[0];
417                     py = field_predA[1];
418                 } else {
419                     px = field_predC[0];
420                     py = field_predC[1];
421                 }
422             } else {
423                 if (is_intra[xy - 1])
424                     sum = FFABS(px) + FFABS(py);
425                 else
426                     sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
427                 if (sum > hybridmv_thresh) {
428                     if (get_bits1(&s->gb)) {
429                         px = field_predA[0];
430                         py = field_predA[1];
431                     } else {
432                         px = field_predC[0];
433                         py = field_predC[1];
434                     }
435                 }
436             }
437         }
438     }
439
440     if (v->field_mode && v->numref)
441         r_y >>= 1;
442     if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
443         y_bias = 1;
444     /* store MV using signed modulus of MV range defined in 4.11 */
445     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;
446     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;
447     if (mv1) { /* duplicate motion data for 1-MV block */
448         s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
449         s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
450         s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
451         s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
452         s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
453         s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
454         v->mv_f[dir][xy +    1 + v->blocks_off] = v->mv_f[dir][xy +            v->blocks_off];
455         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];
456     }
457 }
458
459 /** Predict and set motion vector for interlaced frame picture MBs
460  */
461 void ff_vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
462                           int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
463 {
464     MpegEncContext *s = &v->s;
465     int xy, wrap, off = 0;
466     int A[2], B[2], C[2];
467     int px = 0, py = 0;
468     int a_valid = 0, b_valid = 0, c_valid = 0;
469     int field_a, field_b, field_c; // 0: same, 1: opposit
470     int total_valid, num_samefield, num_oppfield;
471     int pos_c, pos_b, n_adj;
472
473     wrap = s->b8_stride;
474     xy = s->block_index[n];
475
476     if (s->mb_intra) {
477         s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
478         s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
479         s->current_picture.motion_val[1][xy][0] = 0;
480         s->current_picture.motion_val[1][xy][1] = 0;
481         if (mvn == 1) { /* duplicate motion data for 1-MV block */
482             s->current_picture.motion_val[0][xy + 1][0]        = 0;
483             s->current_picture.motion_val[0][xy + 1][1]        = 0;
484             s->current_picture.motion_val[0][xy + wrap][0]     = 0;
485             s->current_picture.motion_val[0][xy + wrap][1]     = 0;
486             s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
487             s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
488             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
489             s->current_picture.motion_val[1][xy + 1][0]        = 0;
490             s->current_picture.motion_val[1][xy + 1][1]        = 0;
491             s->current_picture.motion_val[1][xy + wrap][0]     = 0;
492             s->current_picture.motion_val[1][xy + wrap][1]     = 0;
493             s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
494             s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
495         }
496         return;
497     }
498
499     off = ((n == 0) || (n == 1)) ? 1 : -1;
500     /* predict A */
501     if (s->mb_x || (n == 1) || (n == 3)) {
502         if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
503             || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
504             A[0] = s->current_picture.motion_val[dir][xy - 1][0];
505             A[1] = s->current_picture.motion_val[dir][xy - 1][1];
506             a_valid = 1;
507         } else { // current block has frame mv and cand. has field MV (so average)
508             A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
509                     + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
510             A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
511                     + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
512             a_valid = 1;
513         }
514         if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
515             a_valid = 0;
516             A[0] = A[1] = 0;
517         }
518     } else
519         A[0] = A[1] = 0;
520     /* Predict B and C */
521     B[0] = B[1] = C[0] = C[1] = 0;
522     if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
523         if (!s->first_slice_line) {
524             if (!v->is_intra[s->mb_x - s->mb_stride]) {
525                 b_valid = 1;
526                 n_adj   = n | 2;
527                 pos_b   = s->block_index[n_adj] - 2 * wrap;
528                 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
529                     n_adj = (n & 2) | (n & 1);
530                 }
531                 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
532                 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
533                 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
534                     B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
535                     B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
536                 }
537             }
538             if (s->mb_width > 1) {
539                 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
540                     c_valid = 1;
541                     n_adj   = 2;
542                     pos_c   = s->block_index[2] - 2 * wrap + 2;
543                     if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
544                         n_adj = n & 2;
545                     }
546                     C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
547                     C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
548                     if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
549                         C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
550                         C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
551                     }
552                     if (s->mb_x == s->mb_width - 1) {
553                         if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
554                             c_valid = 1;
555                             n_adj   = 3;
556                             pos_c   = s->block_index[3] - 2 * wrap - 2;
557                             if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
558                                 n_adj = n | 1;
559                             }
560                             C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
561                             C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
562                             if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
563                                 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
564                                 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
565                             }
566                         } else
567                             c_valid = 0;
568                     }
569                 }
570             }
571         }
572     } else {
573         pos_b   = s->block_index[1];
574         b_valid = 1;
575         B[0]    = s->current_picture.motion_val[dir][pos_b][0];
576         B[1]    = s->current_picture.motion_val[dir][pos_b][1];
577         pos_c   = s->block_index[0];
578         c_valid = 1;
579         C[0]    = s->current_picture.motion_val[dir][pos_c][0];
580         C[1]    = s->current_picture.motion_val[dir][pos_c][1];
581     }
582
583     total_valid = a_valid + b_valid + c_valid;
584     // check if predictor A is out of bounds
585     if (!s->mb_x && !(n == 1 || n == 3)) {
586         A[0] = A[1] = 0;
587     }
588     // check if predictor B is out of bounds
589     if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
590         B[0] = B[1] = C[0] = C[1] = 0;
591     }
592     if (!v->blk_mv_type[xy]) {
593         if (s->mb_width == 1) {
594             px = B[0];
595             py = B[1];
596         } else {
597             if (total_valid >= 2) {
598                 px = mid_pred(A[0], B[0], C[0]);
599                 py = mid_pred(A[1], B[1], C[1]);
600             } else if (total_valid) {
601                 if      (a_valid) { px = A[0]; py = A[1]; }
602                 else if (b_valid) { px = B[0]; py = B[1]; }
603                 else              { px = C[0]; py = C[1]; }
604             }
605         }
606     } else {
607         if (a_valid)
608             field_a = (A[1] & 4) ? 1 : 0;
609         else
610             field_a = 0;
611         if (b_valid)
612             field_b = (B[1] & 4) ? 1 : 0;
613         else
614             field_b = 0;
615         if (c_valid)
616             field_c = (C[1] & 4) ? 1 : 0;
617         else
618             field_c = 0;
619
620         num_oppfield  = field_a + field_b + field_c;
621         num_samefield = total_valid - num_oppfield;
622         if (total_valid == 3) {
623             if ((num_samefield == 3) || (num_oppfield == 3)) {
624                 px = mid_pred(A[0], B[0], C[0]);
625                 py = mid_pred(A[1], B[1], C[1]);
626             } else if (num_samefield >= num_oppfield) {
627                 /* take one MV from same field set depending on priority
628                 the check for B may not be necessary */
629                 px = !field_a ? A[0] : B[0];
630                 py = !field_a ? A[1] : B[1];
631             } else {
632                 px =  field_a ? A[0] : B[0];
633                 py =  field_a ? A[1] : B[1];
634             }
635         } else if (total_valid == 2) {
636             if (num_samefield >= num_oppfield) {
637                 if (!field_a && a_valid) {
638                     px = A[0];
639                     py = A[1];
640                 } else if (!field_b && b_valid) {
641                     px = B[0];
642                     py = B[1];
643                 } else /*if (c_valid)*/ {
644                     av_assert1(c_valid);
645                     px = C[0];
646                     py = C[1];
647                 }
648             } else {
649                 if (field_a && a_valid) {
650                     px = A[0];
651                     py = A[1];
652                 } else /*if (field_b && b_valid)*/ {
653                     av_assert1(field_b && b_valid);
654                     px = B[0];
655                     py = B[1];
656                 }
657             }
658         } else if (total_valid == 1) {
659             px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
660             py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
661         }
662     }
663
664     /* store MV using signed modulus of MV range defined in 4.11 */
665     s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
666     s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
667     if (mvn == 1) { /* duplicate motion data for 1-MV block */
668         s->current_picture.motion_val[dir][xy +    1    ][0] = s->current_picture.motion_val[dir][xy][0];
669         s->current_picture.motion_val[dir][xy +    1    ][1] = s->current_picture.motion_val[dir][xy][1];
670         s->current_picture.motion_val[dir][xy + wrap    ][0] = s->current_picture.motion_val[dir][xy][0];
671         s->current_picture.motion_val[dir][xy + wrap    ][1] = s->current_picture.motion_val[dir][xy][1];
672         s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
673         s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
674     } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
675         s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
676         s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
677         s->mv[dir][n + 1][0] = s->mv[dir][n][0];
678         s->mv[dir][n + 1][1] = s->mv[dir][n][1];
679     }
680 }
681
682 void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
683                       int direct, int mvtype)
684 {
685     MpegEncContext *s = &v->s;
686     int xy, wrap, off = 0;
687     int16_t *A, *B, *C;
688     int px, py;
689     int sum;
690     int r_x, r_y;
691     const uint8_t *is_intra = v->mb_type[0];
692
693     av_assert0(!v->field_mode);
694
695     r_x = v->range_x;
696     r_y = v->range_y;
697     /* scale MV difference to be quad-pel */
698     dmv_x[0] <<= 1 - s->quarter_sample;
699     dmv_y[0] <<= 1 - s->quarter_sample;
700     dmv_x[1] <<= 1 - s->quarter_sample;
701     dmv_y[1] <<= 1 - s->quarter_sample;
702
703     wrap = s->b8_stride;
704     xy = s->block_index[0];
705
706     if (s->mb_intra) {
707         s->current_picture.motion_val[0][xy][0] =
708         s->current_picture.motion_val[0][xy][1] =
709         s->current_picture.motion_val[1][xy][0] =
710         s->current_picture.motion_val[1][xy][1] = 0;
711         return;
712     }
713         if (direct && s->next_picture_ptr->field_picture)
714             av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
715
716         s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
717         s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
718         s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
719         s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
720
721         /* Pullback predicted motion vectors as specified in 8.4.5.4 */
722         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));
723         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));
724         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));
725         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));
726     if (direct) {
727         s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
728         s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
729         s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
730         s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
731         return;
732     }
733
734     if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
735         C   = s->current_picture.motion_val[0][xy - 2];
736         A   = s->current_picture.motion_val[0][xy - wrap * 2];
737         off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
738         B   = s->current_picture.motion_val[0][xy - wrap * 2 + off];
739
740         if (!s->mb_x) C[0] = C[1] = 0;
741         if (!s->first_slice_line) { // predictor A is not out of bounds
742             if (s->mb_width == 1) {
743                 px = A[0];
744                 py = A[1];
745             } else {
746                 px = mid_pred(A[0], B[0], C[0]);
747                 py = mid_pred(A[1], B[1], C[1]);
748             }
749         } else if (s->mb_x) { // predictor C is not out of bounds
750             px = C[0];
751             py = C[1];
752         } else {
753             px = py = 0;
754         }
755         /* Pullback MV as specified in 8.3.5.3.4 */
756         {
757             int qx, qy, X, Y;
758             int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
759             int MV = 4 - (1 << sh);
760             qx = (s->mb_x << sh);
761             qy = (s->mb_y << sh);
762             X  = (s->mb_width  << sh) - 4;
763             Y  = (s->mb_height << sh) - 4;
764             if (qx + px < MV) px = MV - qx;
765             if (qy + py < MV) py = MV - qy;
766             if (qx + px > X) px = X - qx;
767             if (qy + py > Y) py = Y - qy;
768         }
769         /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
770         if (0 && !s->first_slice_line && s->mb_x) {
771             if (is_intra[xy - wrap])
772                 sum = FFABS(px) + FFABS(py);
773             else
774                 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
775             if (sum > 32) {
776                 if (get_bits1(&s->gb)) {
777                     px = A[0];
778                     py = A[1];
779                 } else {
780                     px = C[0];
781                     py = C[1];
782                 }
783             } else {
784                 if (is_intra[xy - 2])
785                     sum = FFABS(px) + FFABS(py);
786                 else
787                     sum = FFABS(px - C[0]) + FFABS(py - C[1]);
788                 if (sum > 32) {
789                     if (get_bits1(&s->gb)) {
790                         px = A[0];
791                         py = A[1];
792                     } else {
793                         px = C[0];
794                         py = C[1];
795                     }
796                 }
797             }
798         }
799         /* store MV using signed modulus of MV range defined in 4.11 */
800         s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
801         s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
802     }
803     if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
804         C   = s->current_picture.motion_val[1][xy - 2];
805         A   = s->current_picture.motion_val[1][xy - wrap * 2];
806         off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
807         B   = s->current_picture.motion_val[1][xy - wrap * 2 + off];
808
809         if (!s->mb_x)
810             C[0] = C[1] = 0;
811         if (!s->first_slice_line) { // predictor A is not out of bounds
812             if (s->mb_width == 1) {
813                 px = A[0];
814                 py = A[1];
815             } else {
816                 px = mid_pred(A[0], B[0], C[0]);
817                 py = mid_pred(A[1], B[1], C[1]);
818             }
819         } else if (s->mb_x) { // predictor C is not out of bounds
820             px = C[0];
821             py = C[1];
822         } else {
823             px = py = 0;
824         }
825         /* Pullback MV as specified in 8.3.5.3.4 */
826         {
827             int qx, qy, X, Y;
828             int sh = v->profile < PROFILE_ADVANCED ? 5 : 6;
829             int MV = 4 - (1 << sh);
830             qx = (s->mb_x << sh);
831             qy = (s->mb_y << sh);
832             X  = (s->mb_width  << sh) - 4;
833             Y  = (s->mb_height << sh) - 4;
834             if (qx + px < MV) px = MV - qx;
835             if (qy + py < MV) py = MV - qy;
836             if (qx + px > X) px = X - qx;
837             if (qy + py > Y) py = Y - qy;
838         }
839         /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
840         if (0 && !s->first_slice_line && s->mb_x) {
841             if (is_intra[xy - wrap])
842                 sum = FFABS(px) + FFABS(py);
843             else
844                 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
845             if (sum > 32) {
846                 if (get_bits1(&s->gb)) {
847                     px = A[0];
848                     py = A[1];
849                 } else {
850                     px = C[0];
851                     py = C[1];
852                 }
853             } else {
854                 if (is_intra[xy - 2])
855                     sum = FFABS(px) + FFABS(py);
856                 else
857                     sum = FFABS(px - C[0]) + FFABS(py - C[1]);
858                 if (sum > 32) {
859                     if (get_bits1(&s->gb)) {
860                         px = A[0];
861                         py = A[1];
862                     } else {
863                         px = C[0];
864                         py = C[1];
865                     }
866                 }
867             }
868         }
869         /* store MV using signed modulus of MV range defined in 4.11 */
870
871         s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
872         s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
873     }
874     s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
875     s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
876     s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
877     s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
878 }
879
880 void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y,
881                             int mv1, int *pred_flag)
882 {
883     int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
884     MpegEncContext *s = &v->s;
885     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
886
887     if (v->bmvtype == BMV_TYPE_DIRECT) {
888         int total_opp, k, f;
889         if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
890             s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
891                                       v->bfraction, 0, s->quarter_sample);
892             s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
893                                       v->bfraction, 0, s->quarter_sample);
894             s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
895                                       v->bfraction, 1, s->quarter_sample);
896             s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
897                                       v->bfraction, 1, s->quarter_sample);
898
899             total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
900                       + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
901                       + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
902                       + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
903             f = (total_opp > 2) ? 1 : 0;
904         } else {
905             s->mv[0][0][0] = s->mv[0][0][1] = 0;
906             s->mv[1][0][0] = s->mv[1][0][1] = 0;
907             f = 0;
908         }
909         v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
910         for (k = 0; k < 4; k++) {
911             s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
912             s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
913             s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
914             s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
915             v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
916             v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
917         }
918         return;
919     }
920     if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
921         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);
922         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);
923         return;
924     }
925     if (dir) { // backward
926         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);
927         if (n == 3 || mv1) {
928             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);
929         }
930     } else { // forward
931         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);
932         if (n == 3 || mv1) {
933             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);
934         }
935     }
936 }