LCOV - code coverage report
Current view: top level - libavcodec - vc1_pred.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 530 567 93.5 %
Date: 2017-12-18 20:14:19 Functions: 10 10 100.0 %

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

Generated by: LCOV version 1.13