]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
FS-7500: refactor switch_img_patch, it was made complicated than it should be
authorSeven Du <dujinfang@gmail.com>
Sun, 8 Feb 2015 01:23:47 +0000 (09:23 +0800)
committerMichael Jerris <mike@jerris.com>
Thu, 28 May 2015 17:46:59 +0000 (12:46 -0500)
src/switch_core_video.c

index 9d9b1618f91472b8d383e59bbd5f277d9560308e..bbdc40dddc0fd02d307f0c5b700d057966692d2b 100644 (file)
@@ -72,37 +72,32 @@ SWITCH_DECLARE(void) switch_img_free(switch_image_t **img)
        }
 }
 
+#ifndef MIN
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+#endif
+
 // simple implementation to patch a small img to a big IMG at position x,y
 SWITCH_DECLARE(void) switch_img_patch(switch_image_t *IMG, switch_image_t *img, int x, int y)
 {
-       int i, j, k;
-       int W = IMG->d_w;
-       int H = IMG->d_h;
-       int w = img->d_w;
-       int h = img->d_h;
+       int i, len;
 
        switch_assert(img->fmt == SWITCH_IMG_FMT_I420);
        switch_assert(IMG->fmt == SWITCH_IMG_FMT_I420);
 
-       for (i = y; i < (y + h) && i < H; i++) {
-               for (j = x; j < (x + w) && j < W; j++) {
-                       IMG->planes[0][i * IMG->stride[0] + j] = img->planes[0][(i - y) * img->stride[0] + (j - x)];
-               }
-       }
+       len = MIN(img->d_w, IMG->d_w - x);
+       if (len <= 0) return;
 
-       for (i = y; i < (y + h) && i < H; i+=4) {
-               for (j = x; j < (x + w) && j < W; j+=4) {
-                       for (k = 1; k <= 2; k++) {
-                               IMG->planes[k][i/2 * IMG->stride[k] + j/2]         = img->planes[k][(i-y)/2 * img->stride[k] + (j-x)/2];
-                               IMG->planes[k][i/2 * IMG->stride[k] + j/2 + 1]     = img->planes[k][(i-y)/2 * img->stride[k] + (j-x)/2 + 1];
-                               IMG->planes[k][(i+2)/2 * IMG->stride[k] + j/2]     = img->planes[k][(i+2-y)/2 * img->stride[k] + (j-x)/2];
-                               IMG->planes[k][(i+2)/2 * IMG->stride[k] + j/2 + 1] = img->planes[k][(i+2-y)/2 * img->stride[k] + (j-x)/2 + 1];
-                       }
-               }
+       for (i = y; i < (y + img->d_h) && i < IMG->d_h; i++) {
+               memcpy(IMG->planes[SWITCH_PLANE_Y] + IMG->stride[SWITCH_PLANE_Y] * i + x, img->planes[SWITCH_PLANE_Y] + img->stride[SWITCH_PLANE_Y] * (i - y), len);
        }
-}
 
+       len /= 2;
 
+       for (i = y; i < (y + img->d_h) && i < IMG->d_h; i += 2) {
+               memcpy(IMG->planes[SWITCH_PLANE_U] + IMG->stride[SWITCH_PLANE_U] * i / 2 + x / 2, img->planes[SWITCH_PLANE_U] + img->stride[SWITCH_PLANE_U] * (i - y) / 2, len);
+               memcpy(IMG->planes[SWITCH_PLANE_V] + IMG->stride[SWITCH_PLANE_V] * i / 2 + x / 2, img->planes[SWITCH_PLANE_V] + img->stride[SWITCH_PLANE_V] * (i - y) / 2, len);
+       }
+}
 
 SWITCH_DECLARE(void) switch_img_copy(switch_image_t *img, switch_image_t **new_img)
 {