From 2842dedc21e62dff8fb06482df067a744febb5a5 Mon Sep 17 00:00:00 2001 From: Seven Du Date: Sun, 8 Feb 2015 09:23:47 +0800 Subject: [PATCH] FS-7500: refactor switch_img_patch, it was made complicated than it should be --- src/switch_core_video.c | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/switch_core_video.c b/src/switch_core_video.c index 9d9b1618f9..bbdc40dddc 100644 --- a/src/switch_core_video.c +++ b/src/switch_core_video.c @@ -72,38 +72,33 @@ 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 + 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); } - 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]; - } - } + 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) { int i = 0;