From f23b9ed64bd4ccf249c686616dd3f51a69d285dc Mon Sep 17 00:00:00 2001 From: Oleg Oshmyan Date: Wed, 12 Mar 2014 22:25:52 +0000 Subject: Don't forget to apply \be to the first/last row/column (again) The implementation of \be was changed to xy-VSFilter's, which (like VSFilter's) reads but does not write out the first/last row/column. --- libass/ass_bitmap.c | 55 +++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/libass/ass_bitmap.c b/libass/ass_bitmap.c index 6f77a17..19c4727 100644 --- a/libass/ass_bitmap.c +++ b/libass/ass_bitmap.c @@ -649,49 +649,35 @@ void be_blur_c(uint8_t *buf, intptr_t w, unsigned char *src, *dst; memset(col_pix_buf, 0, w * sizeof(unsigned short)); memset(col_sum_buf, 0, w * sizeof(unsigned short)); - { - y = 0; - src=buf+y*stride; + y = 0; - x = 2; - old_pix = src[x-1]; - old_sum = old_pix + src[x-2]; - for ( ; x < w; x++) { - temp1 = src[x]; - temp2 = old_pix + temp1; - old_pix = temp1; - temp1 = old_sum + temp2; - old_sum = temp2; - col_pix_buf[x] = temp1; - } - } { - y = 1; src=buf+y*stride; - x = 2; + x = 1; old_pix = src[x-1]; - old_sum = old_pix + src[x-2]; + old_sum = old_pix; for ( ; x < w; x++) { temp1 = src[x]; temp2 = old_pix + temp1; old_pix = temp1; temp1 = old_sum + temp2; old_sum = temp2; - - temp2 = col_pix_buf[x] + temp1; - col_pix_buf[x] = temp1; - col_sum_buf[x] = temp2; + col_pix_buf[x-1] = temp1; + col_sum_buf[x-1] = temp1; } + temp1 = old_sum + old_pix; + col_pix_buf[x-1] = temp1; + col_sum_buf[x-1] = temp1; } - for (y = 2; y < h; y++) { + for (y++; y < h; y++) { src=buf+y*stride; dst=buf+(y-1)*stride; - x = 2; + x = 1; old_pix = src[x-1]; - old_sum = old_pix + src[x-2]; + old_sum = old_pix; for ( ; x < w; x++) { temp1 = src[x]; temp2 = old_pix + temp1; @@ -699,11 +685,22 @@ void be_blur_c(uint8_t *buf, intptr_t w, temp1 = old_sum + temp2; old_sum = temp2; - temp2 = col_pix_buf[x] + temp1; - col_pix_buf[x] = temp1; - dst[x-1] = (col_sum_buf[x] + temp2) >> 4; - col_sum_buf[x] = temp2; + temp2 = col_pix_buf[x-1] + temp1; + col_pix_buf[x-1] = temp1; + dst[x-1] = (col_sum_buf[x-1] + temp2) >> 4; + col_sum_buf[x-1] = temp2; } + temp1 = old_sum + old_pix; + temp2 = col_pix_buf[x-1] + temp1; + col_pix_buf[x-1] = temp1; + dst[x-1] = (col_sum_buf[x-1] + temp2) >> 4; + col_sum_buf[x-1] = temp2; + } + + { + dst=buf+(y-1)*stride; + for (x = 0; x < w; x++) + dst[x] = (col_sum_buf[x] + col_pix_buf[x]) >> 4; } } -- cgit v1.2.3 From 461ba65d18957aa20c686c22d4f73d930c602146 Mon Sep 17 00:00:00 2001 From: Oleg Oshmyan Date: Thu, 13 Mar 2014 03:05:11 +0000 Subject: Apply \be after \blur (like VSFilter) --- libass/ass_bitmap.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/libass/ass_bitmap.c b/libass/ass_bitmap.c index 19c4727..97fb26b 100644 --- a/libass/ass_bitmap.c +++ b/libass/ass_bitmap.c @@ -146,6 +146,20 @@ void ass_synth_blur(ASS_SynthPriv *priv_blur, int opaque_box, int be, return; } + // Apply gaussian blur + if (blur_radius > 0.0 && generate_tables(priv_blur, blur_radius)) { + if (bm_o) + ass_gauss_blur(bm_o->buffer, priv_blur->tmp, + bm_o->w, bm_o->h, bm_o->stride, + priv_blur->gt2, priv_blur->g_r, + priv_blur->g_w); + if (!bm_o || opaque_box) + ass_gauss_blur(bm_g->buffer, priv_blur->tmp, + bm_g->w, bm_g->h, bm_g->stride, + priv_blur->gt2, priv_blur->g_r, + priv_blur->g_w); + } + // Apply box blur (multiple passes, if requested) if (be) { uint16_t* tmp = priv_blur->tmp; @@ -180,20 +194,6 @@ void ass_synth_blur(ASS_SynthPriv *priv_blur, int opaque_box, int be, } } } - - // Apply gaussian blur - if (blur_radius > 0.0 && generate_tables(priv_blur, blur_radius)) { - if (bm_o) - ass_gauss_blur(bm_o->buffer, priv_blur->tmp, - bm_o->w, bm_o->h, bm_o->stride, - priv_blur->gt2, priv_blur->g_r, - priv_blur->g_w); - if (!bm_o || opaque_box) - ass_gauss_blur(bm_g->buffer, priv_blur->tmp, - bm_g->w, bm_g->h, bm_g->stride, - priv_blur->gt2, priv_blur->g_r, - priv_blur->g_w); - } } ASS_SynthPriv *ass_synth_init(double radius) -- cgit v1.2.3 From 0f367cc55086e46070e4c4cda9a1800176bed3c0 Mon Sep 17 00:00:00 2001 From: Oleg Oshmyan Date: Thu, 13 Mar 2014 03:15:18 +0000 Subject: Calculate \be using [0..64] value range (like VSFilter) To avoid banding in the output, the full [0..255] value range is restored before the last \be pass, which then uses the full range and hides the bands by virtue of being a blur. With this, our \be finally closely matches VSFilter's. The only visible difference (other than the lack of banding) is in clipping: we add proper padding and output the whole blurred image, while VSFilter does not add any padding and hence clips the blurred image too early. --- libass/ass_bitmap.c | 63 +++++++++++++++++++++++++++++++++++++++++++++-------- libass/ass_bitmap.h | 4 ++++ 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/libass/ass_bitmap.c b/libass/ass_bitmap.c index 97fb26b..5f0991d 100644 --- a/libass/ass_bitmap.c +++ b/libass/ass_bitmap.c @@ -170,13 +170,23 @@ void ass_synth_blur(ASS_SynthPriv *priv_blur, int opaque_box, int be, unsigned stride = bm_o->stride; unsigned char *buf = bm_o->buffer; if(w && h){ - while(passes--){ - memset(tmp, 0, stride * 2); - if(w < 16){ - be_blur_c(buf, w, h, stride, tmp); - }else{ - priv_blur->be_blur_func(buf, w, h, stride, tmp); + if(passes > 1){ + be_blur_pre(buf, w, h, stride); + while(--passes){ + memset(tmp, 0, stride * 2); + if(w < 16){ + be_blur_c(buf, w, h, stride, tmp); + }else{ + priv_blur->be_blur_func(buf, w, h, stride, tmp); + } } + be_blur_post(buf, w, h, stride); + } + memset(tmp, 0, stride * 2); + if(w < 16){ + be_blur_c(buf, w, h, stride, tmp); + }else{ + priv_blur->be_blur_func(buf, w, h, stride, tmp); } } } @@ -187,10 +197,16 @@ void ass_synth_blur(ASS_SynthPriv *priv_blur, int opaque_box, int be, unsigned stride = bm_g->stride; unsigned char *buf = bm_g->buffer; if(w && h){ - while(passes--){ - memset(tmp, 0, stride * 2); - priv_blur->be_blur_func(buf, w, h, stride, tmp); + if(passes > 1){ + be_blur_pre(buf, w, h, stride); + while(--passes){ + memset(tmp, 0, stride * 2); + priv_blur->be_blur_func(buf, w, h, stride, tmp); + } + be_blur_post(buf, w, h, stride); } + memset(tmp, 0, stride * 2); + priv_blur->be_blur_func(buf, w, h, stride, tmp); } } } @@ -704,6 +720,35 @@ void be_blur_c(uint8_t *buf, intptr_t w, } } +void be_blur_pre(uint8_t *buf, intptr_t w, intptr_t h, intptr_t stride) +{ + for (int y = 0; y < h; ++y) + { + for (int x = 0; x < w; ++x) + { + // This is equivalent to (value * 64 + 127) / 255 for all + // values from 0 to 256 inclusive. Assist vectorizing + // compilers by noting that all temporaries fit in 8 bits. + buf[y * stride + x] = + (uint8_t) ((buf[y * stride + x] >> 1) + 1) >> 1; + } + } +} + +void be_blur_post(uint8_t *buf, intptr_t w, intptr_t h, intptr_t stride) +{ + for (int y = 0; y < h; ++y) + { + for (int x = 0; x < w; ++x) + { + // This is equivalent to (value * 255 + 32) / 64 for all values + // from 0 to 96 inclusive, and we only care about 0 to 64. + uint8_t value = buf[y * stride + x]; + buf[y * stride + x] = (value << 2) - (value > 32); + } + } +} + int outline_to_bitmap2(ASS_Renderer *render_priv, ASS_Outline *outline, ASS_Outline *border, Bitmap **bm_g, Bitmap **bm_o) diff --git a/libass/ass_bitmap.h b/libass/ass_bitmap.h index 9a36a3e..5014f51 100644 --- a/libass/ass_bitmap.h +++ b/libass/ass_bitmap.h @@ -72,6 +72,10 @@ void ass_gauss_blur(unsigned char *buffer, unsigned *tmp2, void be_blur_c(uint8_t *buf, intptr_t w, intptr_t h, intptr_t stride, uint16_t *tmp); +void be_blur_pre(uint8_t *buf, intptr_t w, + intptr_t h, intptr_t stride); +void be_blur_post(uint8_t *buf, intptr_t w, + intptr_t h, intptr_t stride); void add_bitmaps_c(uint8_t *dst, intptr_t dst_stride, uint8_t *src, intptr_t src_stride, intptr_t height, intptr_t width); -- cgit v1.2.3 From e55955084f66971f6a268e0d0641da1f132fa979 Mon Sep 17 00:00:00 2001 From: Oleg Oshmyan Date: Mon, 9 Feb 2015 15:18:44 +0200 Subject: Add enough padding for \be to avoid clipping To avoid making bitmaps unnecessarily large, use just the necessary amount of padding for the given \be value. --- libass/ass_bitmap.c | 29 +++++++++++++++++++++++++++++ libass/ass_bitmap.h | 1 + libass/ass_render.c | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/libass/ass_bitmap.c b/libass/ass_bitmap.c index 5f0991d..286f904 100644 --- a/libass/ass_bitmap.c +++ b/libass/ass_bitmap.c @@ -749,6 +749,35 @@ void be_blur_post(uint8_t *buf, intptr_t w, intptr_t h, intptr_t stride) } } +/* + * To find these values, simulate blur on the border between two + * half-planes, one zero-filled (background) and the other filled + * with the maximum supported value (foreground). Keep incrementing + * the \be argument. The necessary padding is the distance by which + * the blurred foreground image extends beyond the original border + * and into the background. Initially it increases along with \be, + * but very soon it grinds to a halt. At some point, the blurred + * image actually reaches a stationary point and stays unchanged + * forever after, simply _shifting_ by one pixel for each \be + * step--moving in the direction of the non-zero half-plane and + * thus decreasing the necessary padding (although the large + * padding is still needed for intermediate results). In practice, + * images are finite rather than infinite like half-planes, but + * this can only decrease the required padding. Half-planes filled + * with extreme values are the theoretical limit of the worst case. + * Make sure to use the right pixel value range in the simulation! + */ +int be_padding(int be) +{ + if (be <= 3) + return be; + if (be <= 7) + return 4; + if (be <= 123) + return 5; + return FFMAX(128 - be, 0); +} + int outline_to_bitmap2(ASS_Renderer *render_priv, ASS_Outline *outline, ASS_Outline *border, Bitmap **bm_g, Bitmap **bm_o) diff --git a/libass/ass_bitmap.h b/libass/ass_bitmap.h index 5014f51..901adc7 100644 --- a/libass/ass_bitmap.h +++ b/libass/ass_bitmap.h @@ -69,6 +69,7 @@ void ass_free_bitmap(Bitmap *bm); void ass_gauss_blur(unsigned char *buffer, unsigned *tmp2, int width, int height, int stride, unsigned *m2, int r, int mwidth); +int be_padding(int be); void be_blur_c(uint8_t *buf, intptr_t w, intptr_t h, intptr_t stride, uint16_t *tmp); diff --git a/libass/ass_render.c b/libass/ass_render.c index 6140788..a02bcab 100644 --- a/libass/ass_render.c +++ b/libass/ass_render.c @@ -2257,7 +2257,7 @@ static void render_and_combine_glyphs(ASS_Renderer *render_priv, continue; } - int bbord = info->filter.be > 0 ? sqrt(2 * info->filter.be) : 0; + int bbord = be_padding(info->filter.be); int gbord = info->filter.blur > 0.0 ? FFMIN(info->filter.blur + 1, INT_MAX) : 0; int bord = FFMAX(bbord, gbord); -- cgit v1.2.3 From 6a07f564857d02d55b3549380ce033f532c14efc Mon Sep 17 00:00:00 2001 From: Oleg Oshmyan Date: Tue, 10 Feb 2015 00:26:59 +0200 Subject: Use correct types in be_blur_c Also fix a related sort-of-bug: a multiple of sizeof(uint16_t) was being added to a pointer that already pointed to uint16_t. This was not causing any harm given enough space in the buffer. Fixing the above also lets us combine the two memsets. --- libass/ass_bitmap.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/libass/ass_bitmap.c b/libass/ass_bitmap.c index 286f904..609404f 100644 --- a/libass/ass_bitmap.c +++ b/libass/ass_bitmap.c @@ -651,7 +651,7 @@ void ass_gauss_blur(unsigned char *buffer, unsigned *tmp2, } /** - * \brief Blur with [[1,2,1]. [2,4,2], [1,2,1]] kernel + * \brief Blur with [[1,2,1], [2,4,2], [1,2,1]] kernel * This blur is the same as the one employed by vsfilter. * Pure C implementation. */ @@ -659,12 +659,11 @@ void be_blur_c(uint8_t *buf, intptr_t w, intptr_t h, intptr_t stride, uint16_t *tmp) { - unsigned short *col_pix_buf = tmp; - unsigned short *col_sum_buf = tmp + w * sizeof(unsigned short); + uint16_t *col_pix_buf = tmp; + uint16_t *col_sum_buf = tmp + w; unsigned x, y, old_pix, old_sum, temp1, temp2; - unsigned char *src, *dst; - memset(col_pix_buf, 0, w * sizeof(unsigned short)); - memset(col_sum_buf, 0, w * sizeof(unsigned short)); + uint8_t *src, *dst; + memset(tmp, 0, sizeof(uint16_t) * w * 2); y = 0; { -- cgit v1.2.3 From 5c60a78f2ebdca81f590707f48cf07420292044b Mon Sep 17 00:00:00 2001 From: Oleg Oshmyan Date: Tue, 10 Feb 2015 00:37:41 +0200 Subject: Make sure the synth tmp buffer is large enough for be_blur --- libass/ass_bitmap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libass/ass_bitmap.c b/libass/ass_bitmap.c index 609404f..d1ba819 100644 --- a/libass/ass_bitmap.c +++ b/libass/ass_bitmap.c @@ -126,7 +126,8 @@ static bool resize_tmp(ASS_SynthPriv *priv, int w, int h) { if (w >= INT_MAX || (w + 1) > SIZE_MAX / 2 / sizeof(unsigned) / FFMAX(h, 1)) return false; - size_t needed = sizeof(unsigned) * (w + 1) * h; + size_t needed = FFMAX(sizeof(unsigned) * (w + 1) * h, + sizeof(uint16_t) * ass_align(32, w) * 2); if (priv->tmp && priv->tmp_allocated >= needed) return true; -- cgit v1.2.3