Ticket #1641: bug_1641v2.diff

File bug_1641v2.diff, 4.4 KB (added by quetschke@…, 16 years ago)

New patch for cropdetect enhancement

  • libmpcodecs/vf_cropdetect.c

     
    7575static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
    7676    mp_image_t *dmpi;
    7777    int bpp=mpi->bpp/8;
    78     int w,h,x,y,shrink_by;
     78    int w,h,x,y,x2,y2,shrink_by;
    7979
    8080    // hope we'll get DR buffer:
    8181    dmpi=vf_get_image(vf->next,mpi->imgfmt,
     
    9292    dmpi->height=mpi->height;
    9393
    9494if(++vf->priv->fno>0){  // ignore first 2 frames - they may be empty
     95    int round_sig = 1;
    9596
    9697    // Reset the crop area every reset_count frames, if reset_count is > 0
    9798    if(vf->priv->reset_count > 0 && vf->priv->fno > vf->priv->reset_count){
     
    102103        vf->priv->fno=1;
    103104    }
    104105
    105     for(y=0;y<vf->priv->y1;y++){
    106         if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
     106    // Do not check line 0 - CC information might confuse the result
     107    for(y=1;y<vf->priv->y1;y++){
     108       if(checkline(mpi->planes[0]+mpi->stride[0]*y,bpp,mpi->w,bpp)>vf->priv->limit){
     109            if(y == 1) // If line 1 is not empty assume line 0 is used as well
     110                y = 0;
    107111            vf->priv->y1=y;
    108112            break;
    109113        }
     
    130134        }
    131135    }
    132136
    133     // round x and y (up), important for yuv colorspaces
    134     // make sure they stay rounded!
    135     x=(vf->priv->x1+1)&(~1);
    136     y=(vf->priv->y1+1)&(~1);
     137    if (vf->priv->round < 0)
     138        round_sig = -1;
    137139
    138     w = vf->priv->x2 - x + 1;
    139     h = vf->priv->y2 - y + 1;
     140    x = vf->priv->x1;
     141    y = vf->priv->y1;
     142    x2 = vf->priv->x2;
     143    y2 = vf->priv->y2;
    140144
    141     // w and h must be divisible by 2 as well because of yuv
    142     // colorspace problems.
    143     if (vf->priv->round <= 1)
    144       vf->priv->round = 16;
    145     if (vf->priv->round % 2)
    146       vf->priv->round *= 2;
     145    // round x and y, important for yuv colorspaces
     146    if (x%2)
     147        x += round_sig;
     148    if (y%2)
     149        y += round_sig;
     150    // and x2 and y2 are rounded to an odd number (this ensures
     151    // w and h being even numbers
     152    if (x2%2 == 0)
     153        x += round_sig;
     154    if (y2%2 == 0)
     155        y += round_sig;
    147156
    148     shrink_by = w % vf->priv->round;
     157    w = x2 - x + 1;
     158    h = y2 - y + 1;
     159
     160    // w and h must be divisible by 2 because of yuv colorspace problems.
     161    // This is always true as round is even number != 0
     162
     163    // Modulus with negative divisor is implementation-defined, avoid
     164    // problems.
     165    shrink_by = w % abs(vf->priv->round);
     166    if (shrink_by && vf->priv->round < 0)
     167        shrink_by += vf->priv->round;
     168
    149169    w -= shrink_by;
    150     x += (shrink_by / 2 + 1) & ~1;
     170    if (w>mpi->w) {
     171        printf("\nBUG w: %d  w0: %d\n",w,mpi->w);
     172        w = mpi->w;
     173    }
     174    if (w<0) {
     175        printf("\nBUG w: %d  w0: %d\n",w,mpi->w);
     176        w = 0;
     177    }
     178    x += shrink_by / 2;
     179    // Necessary, x can go negative for shrink_by < 0
     180    if (x<0)
     181        x = 0;
     182    // Sanity check
     183    if (x+w>mpi->w) {
     184        x = mpi->w-w;
     185    }
    151186
    152     shrink_by = h % vf->priv->round;
     187    shrink_by = h % abs(vf->priv->round);
     188    if (shrink_by && vf->priv->round < 0)
     189        shrink_by += vf->priv->round;
     190
    153191    h -= shrink_by;
    154     y += (shrink_by / 2 + 1) & ~1;
     192    if (h>mpi->h) {
     193        printf("\nBUG h: %d  h0: %d\n",h,mpi->h);
     194        h = mpi->h;
     195    }
     196    if (h<0) {
     197        printf("\nBUG h: %d  h0: %d\n",h,mpi->h);
     198        h = 0;
     199    }
     200    y += shrink_by / 2;
     201    // Necessary, y can go negative for shrink_by < 0
     202    if (y<0)
     203        y = 0;
     204    // Sanity check
     205    if (y+h>mpi->h) {
     206        y = mpi->h-h;
     207    }
    155208
     209    // make sure x and y stay rounded to even number
     210    x = (x%2 ? x+round_sig : x);
     211    y = (y%2 ? y+round_sig : y);
     212
    156213    mp_msg(MSGT_VFILTER, MSGL_INFO, MSGTR_MPCODECS_CropArea,
    157214        vf->priv->x1,vf->priv->x2,
    158215        vf->priv->y1,vf->priv->y2,
     
    175232//===========================================================================//
    176233
    177234static int vf_open(vf_instance_t *vf, char *args){
     235    int round_sig = 1;
    178236    vf->config=config;
    179237    vf->put_image=put_image;
    180238    vf->query_format=query_format;
     
    186244    &vf->priv->limit,
    187245    &vf->priv->round,
    188246    &vf->priv->reset_count);
     247
     248    if (vf->priv->round < 0)
     249        round_sig = -1;
     250
     251    if (abs(vf->priv->round) <= 1)
     252      vf->priv->round = 16*round_sig;
     253    if (vf->priv->round % 2)
     254        vf->priv->round += round_sig; // Increase by one, keep sign
     255
    189256    return 1;
    190257}
    191258