Hi folks, The attached C source file implements a new video filter which attempts to remove alpha-blended logos by reversing the alpha blend to recover the original colour. To use it, the user must create a template in the form of a PNG image with an alpha channel which gives the filter the alpha value and colour used at each point in the logo. Since it uses libpng to decode the template, the filter is only included if libpng is present. This template is easy to create if the logo is all white: just wait until a frame where the screen is completely black and use the luminance of the logo on that frame as the alpha channel in an all-white PNG image. If the logo also features black and other colours it's more tricky as you need to extract each colour separately and merge them together into a single template. A tool could be made to automate much of this, but I've not written it yet. I just created the templates manually in a graphics program from frames dumped to disk. I tested this with the logos from TheWB and ABC, but of course I can't supply samples because of trademark/copyright issues. The filter currently only operates on RGB-type images, which means it's not really useable from mplayer since the accelerated vo devices usually want YUV images. It's more useful for preprocessing near-raw video with mencoder before applying a more "aggressive" codec. Applying the filter to a video which has already been heavily compressed can cause some weird effects due to the resolution difference under the watermark, especially in the presence of hardframedrop. I've not tested the ARGB and ABGR formats because I couldn't find any output devices/codecs that actually support them. I'm a bit unsure about the image format stuff because BGRA and RGBA both seem to be the same, for some reason. See also the comments at the start of the source file for more notes and caveats. This is the first time I've delved into mplayer's innards, so let me know if I've done anything wrong with regard to code style as well as API. All the best, Martin Atkins Index: Makefile =================================================================== RCS file: /cvsroot/mplayer/main/libmpcodecs/Makefile,v retrieving revision 1.138 diff -u -r1.138 Makefile --- libmpcodecs/Makefile 29 Dec 2004 19:51:56 -0000 1.138 +++ libmpcodecs/Makefile 19 Feb 2005 18:33:55 -0000 @@ -33,6 +33,7 @@ ifeq ($(PNG),yes) VIDEO_SRCS += vd_mpng.c +VFILTER_SRCS += vf_deblendlogo.c endif ifeq ($(JPEG),yes) Index: vf.c =================================================================== RCS file: /cvsroot/mplayer/main/libmpcodecs/vf.c,v retrieving revision 1.111 diff -u -r1.111 vf.c --- libmpcodecsvf.c 16 Feb 2005 23:46:59 -0000 1.111 +++ libmpcodecsvf.c 19 Feb 2005 18:33:56 -0000 @@ -95,6 +95,9 @@ extern vf_info_t vf_info_divtc; extern vf_info_t vf_info_harddup; extern vf_info_t vf_info_softskip; +#ifdef HAVE_PNG +extern vf_info_t vf_info_deblendlogo; +#endif // list of available filters: static vf_info_t* filter_list[]={ @@ -179,6 +182,9 @@ &vf_info_divtc, &vf_info_harddup, &vf_info_softskip, +#ifdef HAVE_PNG + &vf_info_deblendlogo, +#endif NULL }; /* * vf_deblendlogo.c v0.1 * * Removes alpha-blended TV station logos by reversing the alpha * blending process based on a template. * * Some artifacts are still left due to the colour resolution lost * in the alpha-blending process, but the watermark is much less * noticable. * * Usage: -vf deblendlogo=x:y:template.png * Where template.png is a PNG file containing an RGBA image * representing the logo template. x and y define where on the * image the template is to be placed. * * There are several caveats with this filter: * * It can sometimes be hard to get a good template image. * For logos which are all white, you can use a frame from * your video where the picture has faded to black. However, * if the logo features black elements you need to find * a frame which has solid white behind the logo. Fortunately, * for most channels you will only have to find the template * once as long as your video resolution does not change. * * The filter makes no attempt to detect portions of the * input video where the logo is not present. For these portions * a negative watermark will be rendered onto the image. * Personally, I prefer having a dark watermark for a short * period to having a light watermark for the majority of a * show, but your mileage may vary. * * Compression artifacts as well as the loss of colour depth * caused by the original alpha-blend can cause a small * amount of distortion where the logo was. This is unavoidable. * You can reduce the distortion by doing the logo removal * before applying "clever" compression algorithms. * * If the logo you are trying to remove has opaque or near-opaque * elements, you may like to try the "delogo" filter instead. * */ /* This filter requires libpng */ #include "../config.h" #ifdef HAVE_PNG #include <stdio.h> #include <stdlib.h> #include <string.h> #include <png.h> #include "mp_image.h" #include "../mp_msg.h" #include "vf.h" #include "img_format.h" #include "../libvo/fastmemcpy.h" #define DEBLENDLOGO_NAME "deblendlogo" #define DEBLENDLOGO_MODNAME "vf_" DEBLENDLOGO_NAME struct vf_priv_s { int x, y, tw, th; unsigned char* tmpl; }; /* This filter is very picky about what formats it accepts, because it needs to be able to get at RGBA data to work on it with the RGBA template. In future, perhaps it should also be extended to work on YUV formats */ static int query_format(struct vf_instance_s* vf, unsigned int fmt) { switch (fmt) { case IMGFMT_ABGR: case IMGFMT_BGRA: case IMGFMT_ARGB: case IMGFMT_RGBA: return vf_next_query_format(vf, fmt); default: return 0; } } static int config(struct vf_instance_s* vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt) { if (vf->priv->x < 0 || vf->priv->y < 0 || vf->priv->x + vf->priv->tw > width || vf->priv->y + vf->priv->th > height) { mp_msg(MSGT_VFILTER,MSGL_WARN,DEBLENDLOGO_MODNAME ": The template is hanging out of the video frame!\n"); } return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt); } static int put_image(struct vf_instance_s* vf, mp_image_t* mpi) { mp_image_t* dmpi; unsigned int bpp = mpi->bpp / 8; unsigned int x, y, red, green, blue; struct vf_priv_s* priv = (struct vf_priv_s*)vf->priv; dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE, mpi->w, mpi->h); memcpy_pic(dmpi->planes[0],mpi->planes[0],mpi->w*bpp, mpi->h, dmpi->stride[0],mpi->stride[0]); switch (dmpi->imgfmt) { case IMGFMT_ABGR: { red = 1; green = 2; blue = 3; } break; case IMGFMT_BGRA: { red = 0; green = 1; blue = 2; } break; case IMGFMT_ARGB: { red = 1; green = 2; blue = 3; } break; case IMGFMT_RGBA: { red = 0; green = 1; blue = 2; } break; default: { mp_msg(MSGT_VFILTER,MSGL_ERR,DEBLENDLOGO_MODNAME ": Unsupported image format.\n"); return vf_next_put_image(vf, dmpi); } } /* The equation used here is just a re-arranged version of that used to do alpha blending in the first place. w: watermark r,g,b (from the template) s: source r,g,b (the result) b: blended r,g,b (the video data) a: alpha (from the template) s = b - (w * a) ----------- (for each component) 1 - a vpos: memory location in the video image tpos: memory location in the template image */ for (x = 0; x < priv->tw; x += 1) { for (y = 0; y < priv->th; y += 1) { int vpos = ((y+priv->y) * dmpi->stride[0]) + ((x+priv->x) * 4); int tpos = ((y * priv->tw) + x) * 4; double alpha = ((double)priv->tmpl[tpos + 3]) / 255.0; double wr = priv->tmpl[tpos], wg = priv->tmpl[tpos + 1], wb = priv->tmpl[tpos + 2]; double br = dmpi->planes[0][vpos+red], bg = dmpi->planes[0][vpos+green], bb = dmpi->planes[0][vpos+blue]; double sr,sg,sb; if (alpha == 0) continue; if (alpha == 255) continue; sr = ((br - (wr * alpha)) / (1.0 - alpha)); sg = ((bg - (wg * alpha)) / (1.0 - alpha)); sb = ((bb - (wb * alpha)) / (1.0 - alpha)); if (sr < 0.0) sr = 0.0; if (sg < 0.0) sg = 0.0; if (sb < 0.0) sb = 0.0; dmpi->planes[0][vpos+red] = sr; dmpi->planes[0][vpos+green] = sg; dmpi->planes[0][vpos+blue] = sb; } } return vf_next_put_image(vf, dmpi); } static void uninit(struct vf_instance_s* vf) { if (vf->priv->tmpl != 0) free(vf->priv->tmpl); vf->priv->tmpl = 0; if (vf->priv != 0) free(vf->priv); vf->priv = 0; } static int open(vf_instance_t* vf, char* args) { vf->config = config; vf->uninit = uninit; vf->query_format = query_format; vf->put_image = put_image; vf->priv = malloc(sizeof(struct vf_priv_s)); vf->priv->x = 0; vf->priv->y = 0; vf->priv->tw = -1; vf->priv->th = -1; vf->priv->tmpl = 0; if (args) { char tmplname[1000]; char png_cookie[8]; png_structp png_ptr; png_infop info_ptr; png_infop end_info; png_bytepp img_rows; int filelen, i; FILE* tmplfile = 0; int result = sscanf(args, "%d:%d:%s", &vf->priv->x, &vf->priv->y, tmplname); if (result != 3) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Invalid arguments. Usage: deblendlogo=x:y:templatefile\n"); return 0; } if (! (tmplfile = fopen(tmplname, "rw"))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to load template file %s\n", tmplname); return 0; } fread(png_cookie, 1, sizeof(png_cookie), tmplfile); if (png_sig_cmp(png_cookie, 0, sizeof(png_cookie))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Given template file is not a PNG image\n"); fclose(tmplfile); return 0; } fseek(tmplfile, 0, 0); if (! (png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate libpng read struct\n"); fclose(tmplfile); return 0; } if (! (info_ptr = png_create_info_struct(png_ptr))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate libpng info struct\n"); png_destroy_read_struct(&png_ptr, 0, 0); fclose(tmplfile); return 0; } if (! (end_info = png_create_info_struct(png_ptr))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate libpng end info struct\n"); png_destroy_read_struct(&png_ptr, &info_ptr, 0); fclose(tmplfile); return 0; } #define DEBLEND_CLEANUP { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); \ fclose(tmplfile); return 0; } /* FIXME: All of this PNG reading stuff should really be ready to catch a longjmp, but that only matters if the template image is corrupt, which should happen rarely. Right now it'll just cause mplayer to exit with an error message. */ png_init_io(png_ptr, tmplfile); png_read_info(png_ptr, info_ptr); if (png_get_color_type(png_ptr,info_ptr) != PNG_COLOR_TYPE_RGB_ALPHA) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Template image must be an RGB image with an alpha channel\n"); DEBLEND_CLEANUP; return 0; } if (png_get_bit_depth(png_ptr,info_ptr) == 16) { png_set_strip_16(png_ptr); } vf->priv->tw = png_get_image_width(png_ptr, info_ptr); vf->priv->th = png_get_image_height(png_ptr, info_ptr); if (! (vf->priv->tmpl = (char*)malloc(4 * vf->priv->tw * vf->priv->th))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate memory for template image\n"); DEBLEND_CLEANUP; return 0; } if (! (img_rows = (png_bytepp)malloc(sizeof(png_bytep) * vf->priv->th))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate memory\n"); DEBLEND_CLEANUP; return 0; } for (i = 0; i < vf->priv->th; i++) { img_rows[i] = vf->priv->tmpl + (i * vf->priv->tw * 4); } png_read_image(png_ptr, img_rows); png_read_end(png_ptr, end_info); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); } else { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Invalid arguments. Usage: deblendlogo=x:y:templatefile\n"); return 0; } return 1; } vf_info_t vf_info_deblendlogo = { "remove alpha-blended station logos", DEBLENDLOGO_NAME, "Martin Atkins", "", open, NULL }; #endif
Martin Atkins wrote:
Hi folks,
The attached C source file implements a new video filter which attempts to remove alpha-blended logos by reversing the alpha blend to recover the original colour. To use it, the user must create a template in the form of a PNG image with an alpha channel which gives the filter the alpha value and colour used at each point in the logo. Since it uses libpng to decode the template, the filter is only included if libpng is present.
[snip]
The filter currently only operates on RGB-type images, which means it's not really useable from mplayer since the accelerated vo devices usually want YUV images.
[snip] As an experiment, I added rudimentary support for YV12 using the SwScaler to convert back and forth between it and BGRA. Not a brilliant solution, but it still manages acceptable realtime performance on my Pentium 3 computer here, so I think it's acceptable. The format filter could perhaps have handled the conversion, but it's only a few extra lines of code and it makes the filter easier to use with YUV sources and YUV output devices. This new version also fixes a bug where the filter would crash (divide by zero) if there was a completely opaque pixel in the template. There seems to be a strange bug with the YV12->BGRA->YV12 path where a green line is introduced with some video sources at the bottom of the image. I can't figure out what's causing it, but some discussion on the #mplayerdev IRC channel led to the conclusion that it was a bug in the scaler. Still, if anyone can see that I'm calling it wrong or something, please let me know. The attached file is the new filter implementation. The patch included in my last message will still work to add the filter to mplayer when built. All the best, Martin Atkins /* * vf_deblendlogo.c v0.1 * * Removes alpha-blended TV station logos by reversing the alpha * blending process based on a template. * * Some artifacts are still left due to the colour resolution lost * in the alpha-blending process, but the watermark is much less * noticable. * * Usage: -vf deblendlogo=x:y:template.png * Where template.png is a PNG file containing an RGBA image * representing the logo template. x and y define where on the * image the template is to be placed. * * There are several caveats with this filter: * * It can sometimes be hard to get a good template image. * For logos which are all white, you can use a frame from * your video where the picture has faded to black. However, * if the logo features black elements you need to find * a frame which has solid white behind the logo. Fortunately, * for most channels you will only have to find the template * once as long as your video resolution does not change. * * The filter makes no attempt to detect portions of the * input video where the logo is not present. For these portions * a negative watermark will be rendered onto the image. * Personally, I prefer having a dark watermark for a short * period to having a light watermark for the majority of a * show, but your mileage may vary. * * Compression artifacts as well as the loss of colour depth * caused by the original alpha-blend can cause a small * amount of distortion where the logo was. This is unavoidable. * You can reduce the distortion by doing the logo removal * before applying "clever" compression algorithms. * * If the logo you are trying to remove has opaque or near-opaque * elements, you may like to try the "delogo" filter instead. * */ /* This filter requires libpng */ #include "../config.h" #ifdef HAVE_PNG #include <stdio.h> #include <stdlib.h> #include <string.h> #include <png.h> #include <inttypes.h> #include "mp_image.h" #include "../mp_msg.h" #include "vf.h" #include "img_format.h" #include "../postproc/swscale.h" #include "../libvo/fastmemcpy.h" #define DEBLENDLOGO_NAME "deblendlogo" #define DEBLENDLOGO_MODNAME "vf_" DEBLENDLOGO_NAME struct vf_priv_s { int x, y, tw, th; unsigned char* tmpl; struct SwsContext* yv12torgb; struct SwsContext* rgbtoyv12; }; static int query_format(struct vf_instance_s* vf, unsigned int fmt) { switch (fmt) { case IMGFMT_ABGR: case IMGFMT_BGRA: case IMGFMT_ARGB: case IMGFMT_RGBA: case IMGFMT_YV12: return vf_next_query_format(vf, fmt); default: return 0; } } static int config(struct vf_instance_s* vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt) { if (vf->priv->x < 0 || vf->priv->y < 0 || vf->priv->x + vf->priv->tw > width || vf->priv->y + vf->priv->th > height) { mp_msg(MSGT_VFILTER,MSGL_WARN,DEBLENDLOGO_MODNAME ": The template is hanging out of the video frame!\n"); } /* If YV12 format is selected, we must set up the SwScaler conversion contexts */ if (outfmt == IMGFMT_YV12) { vf->priv->yv12torgb = sws_getContext(width, height, IMGFMT_YV12, width, height, IMGFMT_BGRA, 0, NULL, NULL, NULL); vf->priv->rgbtoyv12 = sws_getContext(width, height, IMGFMT_BGRA, width, height, IMGFMT_YV12, 0, NULL, NULL, NULL); } return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt); } static int put_image(struct vf_instance_s* vf, mp_image_t* mpi) { mp_image_t* dmpi; unsigned int bpp = mpi->bpp / 8; unsigned int x, y, red, green, blue; struct vf_priv_s* priv = (struct vf_priv_s*)vf->priv; struct SwsContext *scalectx; if (mpi->imgfmt == IMGFMT_YV12) { dmpi = vf_get_image(vf->next, IMGFMT_BGRA, MP_IMGTYPE_EXPORT, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE, mpi->w, mpi->h); dmpi->stride[0] = mpi->w * 4; dmpi->planes[0] = malloc(dmpi->stride[0] * mpi->h); sws_scale(priv->yv12torgb, mpi->planes, mpi->stride, 0, mpi->h, dmpi->planes, dmpi->stride); } else { dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE, mpi->w, mpi->h); memcpy_pic(dmpi->planes[0],mpi->planes[0],mpi->w*bpp, mpi->h, dmpi->stride[0],mpi->stride[0]); } switch (dmpi->imgfmt) { case IMGFMT_ABGR: { red = 1; green = 2; blue = 3; } break; case IMGFMT_BGRA: { red = 0; green = 1; blue = 2; } break; case IMGFMT_ARGB: { red = 1; green = 2; blue = 3; } break; case IMGFMT_RGBA: { red = 0; green = 1; blue = 2; } break; default: { static int shownerror; if (shownerror != 1) { mp_msg(MSGT_VFILTER,MSGL_ERR,DEBLENDLOGO_MODNAME ": Unsupported image format (%s).\n", vo_format_name(dmpi->imgfmt)); shownerror = 1; } return vf_next_put_image(vf, dmpi); } } /* The equation used here is just a re-arranged version of that used to do alpha blending in the first place. w: watermark r,g,b (from the template) s: source r,g,b (the result) b: blended r,g,b (the video data) a: alpha (from the template) s = b - (w * a) ----------- (for each component) 1 - a vpos: memory location in the video image tpos: memory location in the template image */ for (x = 0; x < priv->tw; x++) { for (y = 0; y < priv->th; y++) { int vpos = ((y+priv->y) * dmpi->stride[0]) + ((x+priv->x) * 4); int tpos = ((y * priv->tw) + x) * 4; double alpha = ((double)priv->tmpl[tpos + 3]) / 255.0; double wr = priv->tmpl[tpos], wg = priv->tmpl[tpos + 1], wb = priv->tmpl[tpos + 2]; double br = dmpi->planes[0][vpos+red], bg = dmpi->planes[0][vpos+green], bb = dmpi->planes[0][vpos+blue]; double sr,sg,sb; if (alpha == 0.0 || alpha == 1.0) continue; sr = ((br - (wr * alpha)) / (1.0 - alpha)); sg = ((bg - (wg * alpha)) / (1.0 - alpha)); sb = ((bb - (wb * alpha)) / (1.0 - alpha)); if (sr < 0.0) sr = 0.0; if (sg < 0.0) sg = 0.0; if (sb < 0.0) sb = 0.0; dmpi->planes[0][vpos+red] = sr; dmpi->planes[0][vpos+green] = sg; dmpi->planes[0][vpos+blue] = sb; } } if (mpi->imgfmt == IMGFMT_YV12) { /* Convert back to YV12 */ mp_image_t* tdmpi = dmpi; dmpi = vf_get_image(vf->next, IMGFMT_YV12, MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE, tdmpi->w, tdmpi->h); sws_scale(priv->rgbtoyv12, tdmpi->planes, tdmpi->stride, 0, tdmpi->h, dmpi->planes, dmpi->stride); free(tdmpi->planes[0]); tdmpi->planes[0] = 0; } return vf_next_put_image(vf, dmpi); } static void uninit(struct vf_instance_s* vf) { if (vf->priv->tmpl != 0) free(vf->priv->tmpl); vf->priv->tmpl = 0; if (vf->priv->yv12torgb != 0) sws_freeContext(vf->priv->yv12torgb); if (vf->priv->rgbtoyv12 != 0) sws_freeContext(vf->priv->rgbtoyv12); vf->priv->yv12torgb = 0; vf->priv->rgbtoyv12 = 0; if (vf->priv != 0) free(vf->priv); vf->priv = 0; } static int open(vf_instance_t* vf, char* args) { vf->config = config; vf->uninit = uninit; vf->query_format = query_format; vf->put_image = put_image; vf->priv = malloc(sizeof(struct vf_priv_s)); vf->priv->x = 0; vf->priv->y = 0; vf->priv->tw = -1; vf->priv->th = -1; vf->priv->tmpl = 0; vf->priv->yv12torgb = 0; vf->priv->rgbtoyv12 = 0; if (args) { char tmplname[1000]; char png_cookie[8]; png_structp png_ptr; png_infop info_ptr; png_infop end_info; png_bytepp img_rows; int filelen, i; FILE* tmplfile = 0; int result = sscanf(args, "%d:%d:%s", &vf->priv->x, &vf->priv->y, tmplname); if (result != 3) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Invalid arguments. Usage: deblendlogo=x:y:templatefile\n"); return 0; } if (! (tmplfile = fopen(tmplname, "rw"))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to load template file %s\n", tmplname); return 0; } fread(png_cookie, 1, sizeof(png_cookie), tmplfile); if (png_sig_cmp(png_cookie, 0, sizeof(png_cookie))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Given template file is not a PNG image\n"); fclose(tmplfile); return 0; } fseek(tmplfile, 0, 0); if (! (png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate libpng read struct\n"); fclose(tmplfile); return 0; } if (! (info_ptr = png_create_info_struct(png_ptr))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate libpng info struct\n"); png_destroy_read_struct(&png_ptr, 0, 0); fclose(tmplfile); return 0; } if (! (end_info = png_create_info_struct(png_ptr))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate libpng end info struct\n"); png_destroy_read_struct(&png_ptr, &info_ptr, 0); fclose(tmplfile); return 0; } #define DEBLEND_CLEANUP { png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); \ fclose(tmplfile); return 0; } /* FIXME: All of this PNG reading stuff should really be ready to catch a longjmp, but that only matters if the template image is corrupt, which should happen rarely. Right now it'll just cause mplayer to exit with an error message. */ png_init_io(png_ptr, tmplfile); png_read_info(png_ptr, info_ptr); if (png_get_color_type(png_ptr,info_ptr) != PNG_COLOR_TYPE_RGB_ALPHA) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Template image must be an RGB image with an alpha channel\n"); DEBLEND_CLEANUP; return 0; } if (png_get_bit_depth(png_ptr,info_ptr) == 16) { png_set_strip_16(png_ptr); } vf->priv->tw = png_get_image_width(png_ptr, info_ptr); vf->priv->th = png_get_image_height(png_ptr, info_ptr); if (! (vf->priv->tmpl = (char*)malloc(4 * vf->priv->tw * vf->priv->th))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate memory for template image\n"); DEBLEND_CLEANUP; return 0; } if (! (img_rows = (png_bytepp)malloc(sizeof(png_bytep) * vf->priv->th))) { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Failed to allocate memory\n"); DEBLEND_CLEANUP; return 0; } for (i = 0; i < vf->priv->th; i++) { img_rows[i] = vf->priv->tmpl + (i * vf->priv->tw * 4); } png_read_image(png_ptr, img_rows); png_read_end(png_ptr, end_info); png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); } else { mp_msg(MSGT_VFILTER, MSGL_ERR, DEBLENDLOGO_MODNAME ": Invalid arguments. Usage: deblendlogo=x:y:templatefile\n"); return 0; } return 1; } vf_info_t vf_info_deblendlogo = { "remove alpha-blended station logos", DEBLENDLOGO_NAME, "Martin Atkins", "", open, NULL }; #endif
On Tuesday 22 February 2005 14:57, Martin Atkins wrote:
There seems to be a strange bug with the YV12->BGRA->YV12 path where a green line is introduced with some video sources at the bottom of the image. I can't figure out what's causing it, but some discussion on the #mplayerdev IRC channel led to the conclusion that it was a bug in the scaler. Still, if anyone can see that I'm calling it wrong or something, please let me know. I dunno about the calling, TBH, I didn't even look at the code, but it gives me a green line too. :( So it's not swscaler or uneven res, something is wrong.. It's a bit of a stopper, better figure it out...
I dunno much about the filter chain, but I believe it works in a way where each filter says which color format it wants, and what color format it can give, and the filter chain takes care of the rest. That's how the 'format' filter works AFAIK, since the 'format' filter alone really doesn't do anything, all conversion is done in swscale. So can't you make your filter say it only wants RGBA and only gives out RGBA?...
The attached file is the new filter implementation. The patch included in my last message will still work to add the filter to mplayer when built. Other than that, amazing filter!! See for yourself. http://dividedsky.net/~ods15/snapshot0.png
- ods15
On Tuesday 22 February 2005 14:57, Martin Atkins wrote:
There seems to be a strange bug with the YV12->BGRA->YV12 path where a green line is introduced with some video sources at the bottom of the image. I can't figure out what's causing it, but some discussion on the #mplayerdev IRC channel led to the conclusion that it was a bug in the scaler. Still, if anyone can see that I'm calling it wrong or something, please let me know.
I dunno about the calling, TBH, I didn't even look at the code, but it gives me a green line too. :( So it's not swscaler or uneven res, something is wrong.. It's a bit of a stopper, better figure it out... It's worse than I thought. :/ It's not just the green line, the entire image has gone down in quality - it's as if someone used nearest neighboor scale to half the size, and then again to twice the size. (i.e., 75% of the pixels were completely lost.) I did find this, using these params, there is no quality defects, no green
On Wednesday 23 February 2005 00:58, Oded Shimon wrote: line problem, nothing. Absolute perfection: -vf scale=,format=fmt=rgba,deblendlogo=730:409:chan3-logo.png,scale=,format=fmt=yv12 If I get some time I might look into it and fix the filter. Generally color converting at all is (a little) lossy, so if it's possible in some way to make this filter work in YUV without converting anything, it would be awesome. I think it is possible, as far as I can tell, alpha works just as well on YUV as it does on RGB. All you need to do is first convert the input png image to YUV instead of RGB (and store the alpha separately or something). - ods15
On Wed, Feb 23, 2005 at 01:21:28AM +0200, Oded Shimon wrote:
It's worse than I thought. :/ It's not just the green line, the entire image has gone down in quality - it's as if someone used nearest neighboor scale to half the size, and then again to twice the size. (i.e., 75% of the pixels were completely lost.)
That sounds exactly like RGBA->YV12->RGBA. You scale the chroma to half-size...
I did find this, using these params, there is no quality defects, no green line problem, nothing. Absolute perfection: -vf scale=,format=fmt=rgba,deblendlogo=730:409:chan3-logo.png,scale=,format=fmt=yv12
If I get some time I might look into it and fix the filter. Generally color converting at all is (a little) lossy, so if it's possible in some way to make this filter work in YUV without converting anything, it would be awesome. I think it is possible, as far as I can tell, alpha works just as well on YUV as it does on RGB. All you need to do is first convert the input png image to YUV instead of RGB (and store the alpha separately or something).
I agree. Using swscale inside the filter is a BAD idea. You should either support it or let the automatic conversions handle it. Faking YV12 support is not helpful at all. --Joey -- "I eat donuts for dying children." --Andrew
Joey Parrish wrote:
On Wed, Feb 23, 2005 at 01:21:28AM +0200, Oded Shimon wrote:
If I get some time I might look into it and fix the filter. Generally color converting at all is (a little) lossy, so if it's possible in some way to make this filter work in YUV without converting anything, it would be awesome. I think it is possible, as far as I can tell, alpha works just as well on YUV as it does on RGB. All you need to do is first convert the input png image to YUV instead of RGB (and store the alpha separately or something).
I agree. Using swscale inside the filter is a BAD idea. You should either support it or let the automatic conversions handle it. Faking YV12 support is not helpful at all.
My original patch declared that it only supported the RGB formats, but mplayer didn't automatically do the necessary conversions to output to YUV drivers/formats, and at the time I didn't know how to work the format filter. It added the scale filter to convert to BGRA, but since the xv driver can't support BGRA it didn't work. Doing the alpha calculations directly on the YV12 data would certainly be ideal, but I don't know enough about the YUV colourspace to do that. My second patch was just a workaround since I got feedback that without YUV support it was of little use, since few file formats use the RGB colourspace. If someone who knows more about YUV than me can make the reverse alpha blend work on YV12 then that would definitely be a better solution. Alternatively, my original patch without the YV12 "support" is complete except for a small bug with the checking to ignore pixels where the template is completely transparent or opaque, where I compared against 255 instead of 1.0 causing a crash when opaque pixels appear in the template. With that fixed, that code will do exactly the same as my more recent version but without the YV12->RGB->YV12 path. All the best, Martin Atkins
On Thursday 24 February 2005 02:10, Martin Atkins wrote:
If someone who knows more about YUV than me can make the reverse alpha blend work on YV12 then that would definitely be a better solution. I just finished it. :)
I'm cleaning up the code a bit, I'll probably send a patch tommorrow - ods15
On Wednesday 23 February 2005 01:21, Oded Shimon wrote:
If I get some time I might look into it and fix the filter. Generally color converting at all is (a little) lossy, so if it's possible in some way to make this filter work in YUV without converting anything, it would be awesome. I think it is possible, as far as I can tell, alpha works just as well on YUV as it does on RGB. All you need to do is first convert the input png image to YUV instead of RGB (and store the alpha separately or something).
I rewrote most of the filter, and added YUV support using the exact same reverse alpha used for RGB (treating the YUV channels as RGB channels). This worked great on black and dark scenes, but for some reason it started completely messing up on bright red or bright blue - the reverse alpha area becomes TOO red. I figured a simple-minded equation for the YUV channels didn't work like that, so I worked out the whole math of converting the YUV to RGB, reversing alpha, and then converting to YUV, simplified, and ended up with the exact same equation I already had, :/ Meaning the original equation DOES work on YUV, it's just because of the clipping that it apparently sucks. 2 possible options: Disable YV12 altogether for the filter, let the user do 'scale=,format=rgba' before the filter. Allow YV12, and convert only the necessary area to RGBA and back. anyone has an idea for a third better option?... http://dividedsky.net/~ods15/snapshot2.png - ods15
On Wednesday 23 February 2005 21:21, Oded Shimon wrote:
2 possible options: Disable YV12 altogether for the filter, let the user do 'scale=,format=rgba' before the filter. Allow YV12, and convert only the necessary area to RGBA and back. Funny thing is, this doesn't even work.
Does anyone know th _REAL_ YUV <-> RGB convertion equations?... They are NOT what is written in DOCS/tech/colorspaces.txt The same 3 pixels, using the equations ion that file: 102 70 59 123 76 68 141 83 70 And using the swscaler: 91 65 61 104 78 74 114 89 84 (yes I know there are 2 sets of equations in that file, neither gave these results though.) Obviously much different. It gets worse the higher it gets. In values less than 70, they are nearly identical. swscaler is extremely hard to read, and I have a feeling the equations used to make that are not written in there anyway, as they are MMX/SSE/whatever stuff. - ods15
On Wed, Feb 23, 2005 at 11:15:14PM +0200, Oded Shimon wrote:
Does anyone know th _REAL_ YUV <-> RGB convertion equations?... They are NOT what is written in DOCS/tech/colorspaces.txt
recently i used these formulas: #define Kr 0.2126 #define Kb 0.0722 #define L(r,g,b) (Kr * r + Kb * b + (1 - Kr - Kb) * g) #define YU(r,g,b) \ (((int)(219*L(r,g,b)/255 + 16.5)) | \ (((int)(112*(b - L(r,g,b))/((1-Kb)*255) + 128.5)) << 8)) #define YV(r,g,b) \ (((int)(219*L(r,g,b)/255 + 16.5)) | \ (((int)(112*(r - L(r,g,b))/((1-Kr)*255) + 128.5)) << 8)) #define YUY2(r,g,b) (YU(r,g,b) | (YV(r,g,b) << 16)) which will probably result in one of the number sets mentioned in the file. i've got this from some directshow docu on msdn ... -- Chaos, panic, and disorder - my work here is done.
On Wednesday 23 February 2005 23:26, Oswald Buddenhagen wrote:
On Wed, Feb 23, 2005 at 11:15:14PM +0200, Oded Shimon wrote:
Does anyone know th _REAL_ YUV <-> RGB convertion equations?... They are NOT what is written in DOCS/tech/colorspaces.txt
recently i used these formulas:
#define Kr 0.2126 #define Kb 0.0722
#define L(r,g,b) (Kr * r + Kb * b + (1 - Kr - Kb) * g) #define YU(r,g,b) \ (((int)(219*L(r,g,b)/255 + 16.5)) | \ (((int)(112*(b - L(r,g,b))/((1-Kb)*255) + 128.5)) << 8)) #define YV(r,g,b) \ (((int)(219*L(r,g,b)/255 + 16.5)) | \ (((int)(112*(r - L(r,g,b))/((1-Kr)*255) + 128.5)) << 8)) #define YUY2(r,g,b) (YU(r,g,b) | (YV(r,g,b) << 16))
which will probably result in one of the number sets mentioned in the file. i've got this from some directshow docu on msdn ... Actually, no, these numbers are much different.. I'm gonna try them out...
On Wednesday 23 February 2005 23:29, Ivo wrote:
On Wednesday 23 February 2005 22:15, Oded Shimon wrote:
Does anyone know th _REAL_ YUV <-> RGB convertion equations?... They are NOT what is written in DOCS/tech/colorspaces.txt
http://en.wikipedia.org/wiki/YUV
These are different, so maybe you could try those and see what happens? Check again, those are the same numbers as the second method in DOCS/tech/colorspaces
- ods15
On Wednesday 23 February 2005 22:37, Oded Shimon wrote:
Check again, those are the same numbers as the second method in DOCS/tech/colorspaces
For Y it is, but not for U and V. --Ivo
On Wednesday 23 February 2005 23:43, Ivo wrote:
On Wednesday 23 February 2005 22:37, Oded Shimon wrote:
Check again, those are the same numbers as the second method in DOCS/tech/colorspaces
For Y it is, but not for U and V. My bad, you're right, didn't notice. I'm a bit overflooded with different equations now, I'll try them one at a time and say which of em (if any) is the best. :)
- ods15
On Wednesday 23 February 2005 22:15, Oded Shimon wrote:
Does anyone know th _REAL_ YUV <-> RGB convertion equations?... They are NOT what is written in DOCS/tech/colorspaces.txt
http://en.wikipedia.org/wiki/YUV These are different, so maybe you could try those and see what happens? --Ivo
Oded Shimon wrote:
On Wednesday 23 February 2005 21:21, Oded Shimon wrote:
2 possible options: Disable YV12 altogether for the filter, let the user do 'scale=,format=rgba' before the filter. Allow YV12, and convert only the necessary area to RGBA and back.
Funny thing is, this doesn't even work.
Does anyone know th _REAL_ YUV <-> RGB convertion equations?... They are NOT what is written in DOCS/tech/colorspaces.txt
What about the ecuations used in libavcodec? imgconvert.c lines 995 and 1052. -- "El verdadero valor esta en hacer sin testigos, lo que podrÃa hacerse delante de todo el mundo" The danger lies not in the machine itself but in the user's failure to envision the full consequences of the instructions he gives to it." Neal Stephenson What difference does it make to the dead, the orphans and the homeless, whether the mad destruction is wrought under the name of totalitarianism or the holy name of liberty or democracy? Mahatma Gandhi (1869 - 1948), "Non-Violence in Peace and War"
On Wed, 23 Feb 2005, [UTF-8] Julián Nuñez wrote:
What about the ecuations used in libavcodec? imgconvert.c lines 995 and 1052.
The macros RGB_TO_[YUV] and the _CCIR versions use the same formula as the first one in DOCS/tech/colorspaces.txt. The non-CCIR versions use full range value, the _CCIR versions have been scaled. If you look at the colorspace faq, the Equation 2 for question C-28 is the same formula as RGB_TO_[YUV]. The equation for CCIR601 values is derived from it for questions C-29 and C-30, ending up with the first equation in DOCS/tech/colorspaces.txt.
On Wed, 23 Feb 2005, Oded Shimon wrote:
On Wednesday 23 February 2005 21:21, Oded Shimon wrote:
2 possible options: Disable YV12 altogether for the filter, let the user do 'scale=,format=rgba' before the filter. Allow YV12, and convert only the necessary area to RGBA and back. Funny thing is, this doesn't even work.
Does anyone know th _REAL_ YUV <-> RGB convertion equations?... They are NOT what is written in DOCS/tech/colorspaces.txt
There are two kinds of YUV that normally get used. One is full range where Y, U, and V each go from 0 to 255. JPEG files use this. The other, specified by rec 601, has a limited range. Y ranges from 16 to 235, U and V from 16 to 240. This is used by MPEG, MJPEG, and a lot of other video codecs I'm assuming. I suggest reading the colorspace-faq, which goes into much detail. Look at the equation for question C-30, "HOW DO I ENCODE Y'CBCR COMPONENTS FROM COMPUTER R'G'B' ?" That equation, which I believe is the correct one, is the same as the first equation in DOCS/tech/colorspaces.txt (if you scale down by a factor of 256). It's also the same as the defines BY, BV, BU, GY, GU, etc. in swscale.c (around line 100).
On Wed, Feb 23, 2005 at 09:21:02PM +0200, Oded Shimon wrote:
I rewrote most of the filter, and added YUV support using the exact same reverse alpha used for RGB (treating the YUV channels as RGB channels).
Note that the U and V channels are not unsigned integers like R, G, B or Y, but signed values with bias. This makes alpha-arithmetic quite different: you must de-bias them before multiplication. -- Ville
participants (8)
-
Ivo -
Joey Parrish -
Julián Nuñez -
Martin Atkins -
Oded Shimon -
Oswald Buddenhagen -
Trent Piepho -
Ville Saari