[MPlayer-dev-eng] [PATCH] vf_ass: avoid a division
Reimar Döffinger
Reimar.Doeffinger at gmx.de
Fri Sep 17 08:30:43 CEST 2010
On Fri, Sep 17, 2010 at 08:06:04AM +0200, Reimar Döffinger wrote:
> On Thu, Sep 16, 2010 at 06:54:59PM +0200, Nicolas George wrote:
> > diff --git a/libmpcodecs/vf_ass.c b/libmpcodecs/vf_ass.c
> > index 3ff9218..6f0beff 100644
> > --- a/libmpcodecs/vf_ass.c
> > +++ b/libmpcodecs/vf_ass.c
> > @@ -317,10 +317,10 @@ static void my_draw_bitmap(struct vf_instance *vf, unsigned char *bitmap,
> > dstv = vf->priv->planes[2] + dst_x + dst_y * vf->priv->outw;
> > for (i = 0; i < bitmap_h; ++i) {
> > for (j = 0; j < bitmap_w; ++j) {
> > - unsigned k = ((unsigned) src[j]) * opacity / 255;
> > - dsty[j] = (k * y + (255 - k) * dsty[j]) / 255;
> > - dstu[j] = (k * u + (255 - k) * dstu[j]) / 255;
> > - dstv[j] = (k * v + (255 - k) * dstv[j]) / 255;
> > + unsigned k = (((unsigned) src[j]) * opacity + 255) >> 8;
> > + dsty[j] = (k * y + (255 - k) * dsty[j] + 255) >> 8;
> > + dstu[j] = (k * u + (255 - k) * dstu[j] + 255) >> 8;
> > + dstv[j] = (k * v + (255 - k) * dstv[j] + 255) >> 8;
>
> Why aren't you using the method from the original patch anymore?
> It would still remove one shift and one add and should reduce
> the rounding errors that appear with above code for small k.
Actually I think you should try something like
opacity += opacity << 8; // *258, approximates 256*256/255/255
...
unsigned k = src[j];
if (!k)
continue;
dst[j] = (k * y + (258*255*255 - k) * dsty[j] + (1 << 23)) >> 24;
Note that the constant add possibly should be tweaked a bit.
You could also use *129 instead of *258 in case the above
can overflow or you'd like to be able to use signed ints.
More information about the MPlayer-dev-eng
mailing list