2006-11-11 14:33:43 +01:00
|
|
|
/*****************************************************************************
|
2010-01-01 21:58:09 +01:00
|
|
|
* colorthres.c: Threshold color based on similarity to reference color
|
2006-11-11 14:33:43 +01:00
|
|
|
*****************************************************************************
|
2009-06-30 19:36:29 +02:00
|
|
|
* Copyright (C) 2000-2009 the VideoLAN team
|
2006-11-11 14:33:43 +01:00
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Authors: Sigmund Augdal <dnumgis@videolan.org>
|
2010-01-04 23:23:19 +01:00
|
|
|
* Antoine Cellerier <dionoea at videolan dot org>
|
2006-11-11 14:33:43 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Preamble
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2008-01-23 22:50:58 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2008-06-24 00:19:07 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
2008-05-31 20:49:57 +02:00
|
|
|
#include <vlc_common.h>
|
2008-05-08 17:30:33 +02:00
|
|
|
#include <vlc_plugin.h>
|
2006-11-26 15:20:34 +01:00
|
|
|
#include <vlc_sout.h>
|
2006-11-11 14:33:43 +01:00
|
|
|
|
2009-06-29 16:03:26 +02:00
|
|
|
#include <vlc_filter.h>
|
2008-05-31 17:04:01 +02:00
|
|
|
#include "filter_picture.h"
|
2006-11-11 14:33:43 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Local prototypes
|
|
|
|
*****************************************************************************/
|
|
|
|
static int Create ( vlc_object_t * );
|
|
|
|
static void Destroy ( vlc_object_t * );
|
|
|
|
|
|
|
|
static picture_t *Filter( filter_t *, picture_t * );
|
2010-01-04 23:23:19 +01:00
|
|
|
static picture_t *FilterPacked( filter_t *, picture_t * );
|
2006-11-11 14:33:43 +01:00
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Module descriptor
|
|
|
|
*****************************************************************************/
|
|
|
|
#define COLOR_TEXT N_("Color")
|
|
|
|
#define COLOR_LONGTEXT N_("Colors similar to this will be kept, others will be "\
|
|
|
|
"grayscaled. This must be an hexadecimal (like HTML colors). The first two "\
|
|
|
|
"chars are for red, then green, then blue. #000000 = black, #FF0000 = red,"\
|
|
|
|
" #00FF00 = green, #FFFF00 = yellow (red + green), #FFFFFF = white" )
|
2010-01-01 21:58:09 +01:00
|
|
|
#define COLOR_HELP N_("Select one color in the video")
|
2008-05-27 21:16:52 +02:00
|
|
|
static const int pi_color_values[] = {
|
2006-11-26 18:49:17 +01:00
|
|
|
0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x0000FF00, 0x000000FF, 0x0000FFFF };
|
2006-11-11 14:33:43 +01:00
|
|
|
|
2008-05-27 21:16:52 +02:00
|
|
|
static const char *const ppsz_color_descriptions[] = {
|
2006-11-11 14:33:43 +01:00
|
|
|
N_("Red"), N_("Fuchsia"), N_("Yellow"), N_("Lime"), N_("Blue"), N_("Aqua") };
|
|
|
|
|
2007-03-31 23:25:36 +02:00
|
|
|
#define CFG_PREFIX "colorthres-"
|
2006-11-11 14:33:43 +01:00
|
|
|
|
2008-10-29 20:59:01 +01:00
|
|
|
vlc_module_begin ()
|
|
|
|
set_description( N_("Color threshold filter") )
|
|
|
|
set_shortname( N_("Color threshold" ))
|
2010-01-01 21:58:09 +01:00
|
|
|
set_help(COLOR_HELP)
|
2008-10-29 20:59:01 +01:00
|
|
|
set_category( CAT_VIDEO )
|
|
|
|
set_subcategory( SUBCAT_VIDEO_VFILTER )
|
|
|
|
set_capability( "video filter2", 0 )
|
2007-03-31 23:25:36 +02:00
|
|
|
add_integer( CFG_PREFIX "color", 0x00FF0000, NULL, COLOR_TEXT,
|
2009-01-27 22:22:05 +01:00
|
|
|
COLOR_LONGTEXT, false )
|
2009-01-28 20:48:19 +01:00
|
|
|
change_integer_list( pi_color_values, ppsz_color_descriptions, NULL )
|
2007-06-15 00:14:12 +02:00
|
|
|
add_integer( CFG_PREFIX "saturationthres", 20, NULL,
|
2009-01-28 22:05:52 +01:00
|
|
|
N_("Saturaton threshold"), "", false )
|
2007-09-10 20:56:52 +02:00
|
|
|
add_integer( CFG_PREFIX "similaritythres", 15, NULL,
|
2009-01-28 22:05:52 +01:00
|
|
|
N_("Similarity threshold"), "", false )
|
2008-10-29 20:59:01 +01:00
|
|
|
set_callbacks( Create, Destroy )
|
|
|
|
vlc_module_end ()
|
2006-11-11 14:33:43 +01:00
|
|
|
|
2008-05-27 21:16:52 +02:00
|
|
|
static const char *const ppsz_filter_options[] = {
|
2010-02-05 12:09:25 +01:00
|
|
|
"color", "saturationthres", "similaritythres", NULL
|
2007-03-31 23:25:36 +02:00
|
|
|
};
|
|
|
|
|
2009-06-30 19:36:29 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
* callback prototypes
|
|
|
|
*****************************************************************************/
|
|
|
|
static int FilterCallback( vlc_object_t *, char const *,
|
|
|
|
vlc_value_t, vlc_value_t, void * );
|
|
|
|
|
|
|
|
|
2006-11-11 14:33:43 +01:00
|
|
|
/*****************************************************************************
|
|
|
|
* filter_sys_t: adjust filter method descriptor
|
|
|
|
*****************************************************************************/
|
|
|
|
struct filter_sys_t
|
|
|
|
{
|
2009-06-30 19:36:29 +02:00
|
|
|
int i_simthres;
|
|
|
|
int i_satthres;
|
|
|
|
int i_color;
|
|
|
|
vlc_mutex_t lock;
|
2006-11-11 14:33:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Create: allocates adjust video thread output method
|
|
|
|
*****************************************************************************
|
|
|
|
* This function allocates and initializes a adjust vout method.
|
|
|
|
*****************************************************************************/
|
|
|
|
static int Create( vlc_object_t *p_this )
|
|
|
|
{
|
|
|
|
filter_t *p_filter = (filter_t *)p_this;
|
2009-06-30 19:36:29 +02:00
|
|
|
filter_sys_t *p_sys;
|
2006-11-11 14:33:43 +01:00
|
|
|
|
2008-05-31 17:04:01 +02:00
|
|
|
switch( p_filter->fmt_in.video.i_chroma )
|
2006-11-11 14:33:43 +01:00
|
|
|
{
|
2008-05-31 17:04:01 +02:00
|
|
|
CASE_PLANAR_YUV
|
2010-01-04 23:23:19 +01:00
|
|
|
p_filter->pf_video_filter = Filter;
|
|
|
|
break;
|
|
|
|
|
|
|
|
CASE_PACKED_YUV_422
|
|
|
|
p_filter->pf_video_filter = FilterPacked;
|
2008-05-31 17:04:01 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2010-01-04 23:05:36 +01:00
|
|
|
msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
|
2008-05-31 17:04:01 +02:00
|
|
|
(char*)&(p_filter->fmt_in.video.i_chroma) );
|
|
|
|
return VLC_EGENERIC;
|
2006-11-11 14:33:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
|
|
|
|
{
|
|
|
|
msg_Err( p_filter, "Input and output chromas don't match" );
|
|
|
|
return VLC_EGENERIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate structure */
|
2009-06-30 19:36:29 +02:00
|
|
|
p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
|
2006-11-11 14:33:43 +01:00
|
|
|
if( p_filter->p_sys == NULL )
|
|
|
|
return VLC_ENOMEM;
|
|
|
|
|
2009-06-30 19:36:29 +02:00
|
|
|
config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
|
|
|
|
p_filter->p_cfg );
|
|
|
|
p_sys->i_color = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "color" );
|
|
|
|
p_sys->i_simthres = var_CreateGetIntegerCommand( p_filter,
|
|
|
|
CFG_PREFIX "similaritythres" );
|
|
|
|
p_sys->i_satthres = var_CreateGetIntegerCommand( p_filter,
|
|
|
|
CFG_PREFIX "saturationthres" );
|
|
|
|
|
|
|
|
vlc_mutex_init( &p_sys->lock );
|
|
|
|
|
|
|
|
var_AddCallback( p_filter, CFG_PREFIX "color", FilterCallback, NULL );
|
|
|
|
var_AddCallback( p_filter, CFG_PREFIX "similaritythres", FilterCallback, NULL );
|
|
|
|
var_AddCallback( p_filter, CFG_PREFIX "saturationthres", FilterCallback, NULL );
|
|
|
|
|
2006-11-11 14:33:43 +01:00
|
|
|
return VLC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Destroy: destroy adjust video thread output method
|
|
|
|
*****************************************************************************
|
|
|
|
* Terminate an output method created by adjustCreateOutputMethod
|
|
|
|
*****************************************************************************/
|
|
|
|
static void Destroy( vlc_object_t *p_this )
|
|
|
|
{
|
|
|
|
filter_t *p_filter = (filter_t *)p_this;
|
2009-06-30 19:36:29 +02:00
|
|
|
|
|
|
|
var_DelCallback( p_filter, CFG_PREFIX "color", FilterCallback, NULL );
|
|
|
|
var_DelCallback( p_filter, CFG_PREFIX "similaritythres", FilterCallback, NULL );
|
|
|
|
var_DelCallback( p_filter, CFG_PREFIX "saturationthres", FilterCallback, NULL );
|
|
|
|
|
|
|
|
vlc_mutex_destroy( &p_filter->p_sys->lock );
|
2006-11-11 14:33:43 +01:00
|
|
|
free( p_filter->p_sys );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************
|
|
|
|
* Render: displays previously rendered output
|
|
|
|
*****************************************************************************
|
|
|
|
* This function send the currently rendered image to adjust modified image,
|
|
|
|
* waits until it is displayed and switch the two rendering buffers, preparing
|
|
|
|
* next frame.
|
|
|
|
*****************************************************************************/
|
|
|
|
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
|
|
|
|
{
|
|
|
|
picture_t *p_outpic;
|
2009-06-30 19:36:29 +02:00
|
|
|
filter_sys_t *p_sys = p_filter->p_sys;
|
2006-11-11 14:33:43 +01:00
|
|
|
uint8_t *p_in_y, *p_in_u, *p_in_v, *p_in_end_u;
|
|
|
|
uint8_t *p_out_y, *p_out_u, *p_out_v;
|
2009-06-30 19:36:29 +02:00
|
|
|
|
|
|
|
vlc_mutex_lock( &p_sys->lock );
|
|
|
|
int i_simthres = p_sys->i_simthres;
|
|
|
|
int i_satthres = p_sys->i_satthres;
|
|
|
|
int i_color = p_sys->i_color;
|
|
|
|
vlc_mutex_unlock( &p_sys->lock );
|
2006-11-11 14:33:43 +01:00
|
|
|
|
|
|
|
if( !p_pic ) return NULL;
|
|
|
|
|
2008-07-19 15:41:59 +02:00
|
|
|
p_outpic = filter_NewPicture( p_filter );
|
2006-11-11 14:33:43 +01:00
|
|
|
if( !p_outpic )
|
|
|
|
{
|
2008-07-18 21:18:36 +02:00
|
|
|
picture_Release( p_pic );
|
2006-11-11 14:33:43 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_in_u = p_pic->p[U_PLANE].p_pixels;
|
|
|
|
p_in_v = p_pic->p[V_PLANE].p_pixels;
|
|
|
|
p_in_y = p_pic->p[Y_PLANE].p_pixels;
|
|
|
|
p_in_end_u = p_in_u + p_pic->p[U_PLANE].i_visible_lines
|
|
|
|
* p_pic->p[U_PLANE].i_pitch - 8;
|
|
|
|
|
|
|
|
p_out_y = p_outpic->p[Y_PLANE].p_pixels;
|
|
|
|
p_out_u = p_outpic->p[U_PLANE].p_pixels;
|
|
|
|
p_out_v = p_outpic->p[V_PLANE].p_pixels;
|
|
|
|
|
|
|
|
/* Create grayscale version of input */
|
2008-05-03 16:16:59 +02:00
|
|
|
vlc_memcpy( p_out_y, p_in_y, p_pic->p[Y_PLANE].i_visible_lines
|
2006-11-26 18:29:57 +01:00
|
|
|
* p_pic->p[Y_PLANE].i_pitch - 8 );
|
2008-05-03 16:16:59 +02:00
|
|
|
vlc_memset( p_out_u, 0x80, p_pic->p[U_PLANE].i_visible_lines
|
2006-11-26 18:29:57 +01:00
|
|
|
* p_pic->p[U_PLANE].i_pitch - 8 );
|
2008-05-03 16:16:59 +02:00
|
|
|
vlc_memset( p_out_v, 0x80, p_pic->p[U_PLANE].i_visible_lines
|
2006-11-26 18:29:57 +01:00
|
|
|
* p_pic->p[U_PLANE].i_pitch - 8 );
|
|
|
|
|
2006-11-11 14:33:43 +01:00
|
|
|
/*
|
|
|
|
* Do the U and V planes
|
|
|
|
*/
|
|
|
|
int i_red = ( i_color & 0xFF0000 ) >> 16;
|
|
|
|
int i_green = ( i_color & 0xFF00 ) >> 8;
|
|
|
|
int i_blue = i_color & 0xFF;
|
|
|
|
int i_u = (int8_t)(( -38 * i_red - 74 * i_green +
|
|
|
|
112 * i_blue + 128) >> 8) + 128;
|
|
|
|
int i_v = (int8_t)(( 112 * i_red - 94 * i_green -
|
|
|
|
18 * i_blue + 128) >> 8) + 128;
|
|
|
|
int refu = i_u - 0x80; /*bright red*/
|
|
|
|
int refv = i_v - 0x80;
|
|
|
|
int reflength = sqrt(refu*refu+refv*refv);
|
|
|
|
|
|
|
|
while( p_in_u < p_in_end_u ) {
|
|
|
|
/* Length of color vector */
|
|
|
|
int inu = (*p_in_u) - 0x80;
|
|
|
|
int inv = (*p_in_v) - 0x80;
|
|
|
|
int length = sqrt(inu*inu+inv*inv);
|
|
|
|
|
|
|
|
int diffu = refu * length - inu *reflength;
|
|
|
|
int diffv = refv * length - inv *reflength;
|
|
|
|
long long int difflen2=diffu*diffu;
|
|
|
|
difflen2 +=diffv*diffv;
|
|
|
|
long long int thres = length*reflength;
|
|
|
|
thres *= thres;
|
|
|
|
if( length > i_satthres && (difflen2*i_simthres< thres ) ) {
|
|
|
|
*p_out_u = *p_in_u;
|
|
|
|
*p_out_v = *p_in_v;
|
|
|
|
// fprintf(stderr,"keeping color %d %d\n", length, difflen2);
|
|
|
|
}
|
|
|
|
p_in_u++;
|
|
|
|
p_in_v++;
|
|
|
|
p_out_u++;
|
|
|
|
p_out_v++;
|
|
|
|
}
|
|
|
|
|
2008-06-04 20:19:05 +02:00
|
|
|
return CopyInfoAndRelease( p_outpic, p_pic );
|
2006-11-11 14:33:43 +01:00
|
|
|
}
|
2009-06-30 19:36:29 +02:00
|
|
|
|
2010-01-04 23:23:19 +01:00
|
|
|
static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
|
|
|
|
{
|
|
|
|
picture_t *p_outpic;
|
|
|
|
filter_sys_t *p_sys = p_filter->p_sys;
|
|
|
|
uint8_t *p_in_y, *p_in_u, *p_in_v, *p_in_end_u;
|
|
|
|
uint8_t *p_out_y, *p_out_u, *p_out_v;
|
|
|
|
|
|
|
|
vlc_mutex_lock( &p_sys->lock );
|
|
|
|
int i_simthres = p_sys->i_simthres;
|
|
|
|
int i_satthres = p_sys->i_satthres;
|
|
|
|
int i_color = p_sys->i_color;
|
|
|
|
vlc_mutex_unlock( &p_sys->lock );
|
|
|
|
|
|
|
|
if( !p_pic ) return NULL;
|
|
|
|
|
|
|
|
p_outpic = filter_NewPicture( p_filter );
|
|
|
|
if( !p_outpic )
|
|
|
|
{
|
|
|
|
picture_Release( p_pic );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int i_y_offset, i_u_offset, i_v_offset;
|
|
|
|
GetPackedYuvOffsets( p_filter->fmt_in.video.i_chroma,
|
|
|
|
&i_y_offset, &i_u_offset, &i_v_offset );
|
|
|
|
p_in_y = p_pic->p->p_pixels+i_y_offset;
|
|
|
|
p_in_u = p_pic->p->p_pixels+i_u_offset;
|
|
|
|
p_in_v = p_pic->p->p_pixels+i_v_offset;
|
|
|
|
p_in_end_u = p_in_u + p_pic->p->i_visible_lines
|
|
|
|
* p_pic->p->i_pitch - 8;
|
|
|
|
|
|
|
|
p_out_y = p_outpic->p->p_pixels+i_y_offset;
|
|
|
|
p_out_u = p_outpic->p->p_pixels+i_u_offset;
|
|
|
|
p_out_v = p_outpic->p->p_pixels+i_v_offset;
|
|
|
|
|
|
|
|
/* Create grayscale version of input */
|
|
|
|
vlc_memcpy( p_outpic->p->p_pixels, p_pic->p->p_pixels,
|
|
|
|
p_pic->p->i_visible_lines * p_pic->p->i_pitch - 8 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the U and V planes
|
|
|
|
*/
|
|
|
|
int i_red = ( i_color & 0xFF0000 ) >> 16;
|
|
|
|
int i_green = ( i_color & 0xFF00 ) >> 8;
|
|
|
|
int i_blue = i_color & 0xFF;
|
|
|
|
int i_u = (int8_t)(( -38 * i_red - 74 * i_green +
|
|
|
|
112 * i_blue + 128) >> 8) + 128;
|
|
|
|
int i_v = (int8_t)(( 112 * i_red - 94 * i_green -
|
|
|
|
18 * i_blue + 128) >> 8) + 128;
|
|
|
|
int refu = i_u - 0x80; /*bright red*/
|
|
|
|
int refv = i_v - 0x80;
|
|
|
|
int reflength = sqrt(refu*refu+refv*refv);
|
|
|
|
|
|
|
|
while( p_in_u < p_in_end_u ) {
|
|
|
|
/* Length of color vector */
|
|
|
|
int inu = (*p_in_u) - 0x80;
|
|
|
|
int inv = (*p_in_v) - 0x80;
|
|
|
|
int length = sqrt(inu*inu+inv*inv);
|
|
|
|
|
|
|
|
int diffu = refu * length - inu *reflength;
|
|
|
|
int diffv = refv * length - inv *reflength;
|
|
|
|
long long int difflen2=diffu*diffu;
|
|
|
|
difflen2 +=diffv*diffv;
|
|
|
|
long long int thres = length*reflength;
|
|
|
|
thres *= thres;
|
|
|
|
if( length > i_satthres && (difflen2*i_simthres< thres ) ) {
|
|
|
|
*p_out_u = *p_in_u;
|
|
|
|
*p_out_v = *p_in_v;
|
|
|
|
// fprintf(stderr,"keeping color %d %d\n", length, difflen2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*p_out_u = 0x80;
|
|
|
|
*p_out_v = 0x80;
|
|
|
|
}
|
|
|
|
p_in_u+=4;
|
|
|
|
p_in_v+=4;
|
|
|
|
p_out_u+=4;
|
|
|
|
p_out_v+=4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CopyInfoAndRelease( p_outpic, p_pic );
|
|
|
|
}
|
|
|
|
|
2009-06-30 19:36:29 +02:00
|
|
|
static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
|
|
|
|
vlc_value_t oldval, vlc_value_t newval, void *p_data )
|
|
|
|
{
|
|
|
|
(void)oldval; (void)p_data;
|
|
|
|
filter_t *p_filter = (filter_t*)p_this;
|
|
|
|
filter_sys_t *p_sys = p_filter->p_sys;
|
|
|
|
|
|
|
|
if( !strcmp( psz_var, CFG_PREFIX "color" ) )
|
|
|
|
{
|
|
|
|
vlc_mutex_lock( &p_sys->lock );
|
|
|
|
p_sys->i_color = newval.i_int;
|
|
|
|
vlc_mutex_unlock( &p_sys->lock );
|
|
|
|
}
|
|
|
|
else if( !strcmp( psz_var, CFG_PREFIX "similaritythres" ) )
|
|
|
|
{
|
|
|
|
vlc_mutex_lock( &p_sys->lock );
|
|
|
|
p_sys->i_simthres = newval.i_int;
|
|
|
|
vlc_mutex_unlock( &p_sys->lock );
|
|
|
|
}
|
|
|
|
else /* CFG_PREFIX "saturationthres" */
|
|
|
|
{
|
|
|
|
vlc_mutex_lock( &p_sys->lock );
|
|
|
|
p_sys->i_satthres = newval.i_int;
|
|
|
|
vlc_mutex_unlock( &p_sys->lock );
|
|
|
|
}
|
|
|
|
|
|
|
|
return VLC_SUCCESS;
|
|
|
|
}
|