-
- lum = dst->data[0] + rect->y * dst->linesize[0];
- cb = dst->data[1] + (rect->y >> 1) * dst->linesize[1];
- cr = dst->data[2] + (rect->y >> 1) * dst->linesize[2];
-
- width2 = (rect->w + 1) >> 1;
- skip2 = rect->x >> 1;
+ int dstx, dsty, dstw, dsth;
+
+ dstx = FFMIN(FFMAX(rect->x, 0), imgw);
+ dstw = FFMIN(FFMAX(rect->w, 0), imgw - dstx);
+ dsty = FFMIN(FFMAX(rect->y, 0), imgh);
+ dsth = FFMIN(FFMAX(rect->h, 0), imgh - dsty);
+ lum = dst->data[0] + dsty * dst->linesize[0];
+ cb = dst->data[1] + (dsty >> 1) * dst->linesize[1];
+ cr = dst->data[2] + (dsty >> 1) * dst->linesize[2];
+
+ width2 = (dstw + 1) >> 1;
+ skip2 = dstx >> 1;