]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/colorthres.c
Remove useless <errno.h> inclusions
[vlc] / modules / video_filter / colorthres.c
index e89a211a72934fa765814d43c02064d13370b058..3ab6d44f3f0738ce7a061aaef99619a3fdfe432f 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
- * colorthres.c: Theshold color based on similarity to reference color
+ * colorthres.c: Threshold color based on similarity to reference color
  *****************************************************************************
  * Copyright (C) 2000-2009 the VideoLAN team
  * $Id$
  *
  * Authors: Sigmund Augdal <dnumgis@videolan.org>
+ *          Antoine Cellerier <dionoea at videolan dot org>
  *
  * 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
@@ -29,7 +30,6 @@
 # include "config.h"
 #endif
 
-#include <errno.h>
 #include <math.h>
 
 #include <vlc_common.h>
@@ -46,6 +46,7 @@ static int  Create    ( vlc_object_t * );
 static void Destroy   ( vlc_object_t * );
 
 static picture_t *Filter( filter_t *, picture_t * );
+static picture_t *FilterPacked( filter_t *, picture_t * );
 
 /*****************************************************************************
  * Module descriptor
@@ -55,6 +56,7 @@ static picture_t *Filter( filter_t *, picture_t * );
     "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" )
+#define COLOR_HELP N_("Select one color in the video")
 static const int pi_color_values[] = {
   0x00FF0000, 0x00FF00FF, 0x00FFFF00, 0x0000FF00, 0x000000FF, 0x0000FFFF };
 
@@ -66,6 +68,7 @@ static const char *const ppsz_color_descriptions[] = {
 vlc_module_begin ()
     set_description( N_("Color threshold filter") )
     set_shortname( N_("Color threshold" ))
+    set_help(COLOR_HELP)
     set_category( CAT_VIDEO )
     set_subcategory( SUBCAT_VIDEO_VFILTER )
     set_capability( "video filter2", 0 )
@@ -114,10 +117,15 @@ static int Create( vlc_object_t *p_this )
     switch( p_filter->fmt_in.video.i_chroma )
     {
         CASE_PLANAR_YUV
+            p_filter->pf_video_filter = Filter;
+            break;
+
+        CASE_PACKED_YUV_422
+            p_filter->pf_video_filter = FilterPacked;
             break;
 
         default:
-            msg_Err( p_filter, "Unsupported input chroma (%4s)",
+            msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
                      (char*)&(p_filter->fmt_in.video.i_chroma) );
             return VLC_EGENERIC;
     }
@@ -147,8 +155,6 @@ static int Create( vlc_object_t *p_this )
     var_AddCallback( p_filter, CFG_PREFIX "similaritythres", FilterCallback, NULL );
     var_AddCallback( p_filter, CFG_PREFIX "saturationthres", FilterCallback, NULL );
 
-    p_filter->pf_video_filter = Filter;
-
     return VLC_SUCCESS;
 }
 
@@ -256,6 +262,90 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
     return CopyInfoAndRelease( p_outpic, p_pic );
 }
 
+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 );
+}
+
 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
                             vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {