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