vf_boxblur: Templatize blur{8,16}

Reviewed-by: Michael Niedermayer <michael@niedermayer.cc>
This commit is contained in:
Timothy Gu 2015-11-01 10:20:58 -08:00
parent c03044c86a
commit dee7440531
1 changed files with 44 additions and 66 deletions

View File

@ -204,75 +204,53 @@ static int config_input(AVFilterLink *inlink)
return 0; return 0;
} }
static inline void blur8(uint8_t *dst, int dst_step, const uint8_t *src, int src_step, /* Naive boxblur would sum source pixels from x-radius .. x+radius
int len, int radius) * for destination pixel x. That would be O(radius*width).
{ * If you now look at what source pixels represent 2 consecutive
/* Naive boxblur would sum source pixels from x-radius .. x+radius * output pixels, then you see they are almost identical and only
* for destination pixel x. That would be O(radius*width). * differ by 2 pixels, like:
* If you now look at what source pixels represent 2 consecutive * src0 111111111
* output pixels, then you see they are almost identical and only * dst0 1
* differ by 2 pixels, like: * src1 111111111
* src0 111111111 * dst1 1
* dst0 1 * src0-src1 1 -1
* src1 111111111 * so when you know one output pixel you can find the next by just adding
* dst1 1 * and subtracting 1 input pixel.
* src0-src1 1 -1 * The following code adopts this faster variant.
* so when you know one output pixel you can find the next by just adding */
* and subtracting 1 input pixel. #define BLUR(type, depth) \
* The following code adopts this faster variant. static inline void blur ## depth(type *dst, int dst_step, const type *src, \
*/ int src_step, int len, int radius) \
const int length = radius*2 + 1; { \
const int inv = ((1<<16) + length/2)/length; const int length = radius*2 + 1; \
int x, sum = src[radius*src_step]; const int inv = ((1<<16) + length/2)/length; \
int x, sum = src[radius*src_step]; \
for (x = 0; x < radius; x++) \
sum += src[x*src_step]<<1; for (x = 0; x < radius; x++) \
sum += src[x*src_step]<<1; \
sum = sum*inv + (1<<15); \
sum = sum*inv + (1<<15); \
for (x = 0; x <= radius; x++) { \
sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv; for (x = 0; x <= radius; x++) { \
dst[x*dst_step] = sum>>16; sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv; \
} dst[x*dst_step] = sum>>16; \
} \
for (; x < len-radius; x++) { \
sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv; for (; x < len-radius; x++) { \
dst[x*dst_step] = sum >>16; sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv; \
} dst[x*dst_step] = sum >>16; \
} \
for (; x < len; x++) { \
sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv; for (; x < len; x++) { \
dst[x*dst_step] = sum>>16; sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv; \
} dst[x*dst_step] = sum>>16; \
} \
} }
static inline void blur16(uint16_t *dst, int dst_step, const uint16_t *src, int src_step, BLUR(uint8_t, 8)
int len, int radius) BLUR(uint16_t, 16)
{
const int length = radius*2 + 1;
const int inv = ((1<<16) + length/2)/length;
int x, sum = src[radius*src_step];
for (x = 0; x < radius; x++) #undef BLUR
sum += src[x*src_step]<<1;
sum = sum*inv + (1<<15);
for (x = 0; x <= radius; x++) {
sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv;
dst[x*dst_step] = sum>>16;
}
for (; x < len-radius; x++) {
sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv;
dst[x*dst_step] = sum >>16;
}
for (; x < len; x++) {
sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv;
dst[x*dst_step] = sum>>16;
}
}
static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step, static inline void blur(uint8_t *dst, int dst_step, const uint8_t *src, int src_step,
int len, int radius, int pixsize) int len, int radius, int pixsize)