* Make it possible to extract any color component from the video. (Red, Green and...
authorAntoine Cellerier <dionoea@videolan.org>
Mon, 18 Dec 2006 21:08:11 +0000 (21:08 +0000)
committerAntoine Cellerier <dionoea@videolan.org>
Mon, 18 Dec 2006 21:08:11 +0000 (21:08 +0000)
modules/video_filter/extract.c

index 7140813..b4c4901 100644 (file)
@@ -40,11 +40,22 @@ static void Destroy     ( vlc_object_t * );
 
 static picture_t *Filter( filter_t *, picture_t * );
 
+static void get_red_from_yuv420( picture_t *, picture_t *, int, int, int );
+static void get_green_from_yuv420( picture_t *, picture_t *, int, int, int );
+static void get_blue_from_yuv420( picture_t *, picture_t *, int, int, int );
+static void get_red_from_yuv422( picture_t *, picture_t *, int, int, int );
+static void get_green_from_yuv422( picture_t *, picture_t *, int, int, int );
+static void get_blue_from_yuv422( picture_t *, picture_t *, int, int, int );
+static void make_projection_matrix( int color, int *matrix );
+static void get_custom_from_yuv420( picture_t *, picture_t *, int, int, int, int * );
+static void get_custom_from_yuv422( picture_t *, picture_t *, int, int, int, int * );
+
+
 #define COMPONENT_TEXT N_("RGB component to extract")
 #define COMPONENT_LONGTEXT N_("RGB component to extract. 0 for Red, 1 for Green and 2 for Blue.")
 #define FILTER_PREFIX "extract-"
 
-static int pi_component_values[] = { 0, 1, 2 };
+static int pi_component_values[] = { 0xFF0000, 0x00FF00, 0x0000FF };
 static const char *ppsz_component_descriptions[] = { "Red", "Green", "Blue" };
 
 /*****************************************************************************
@@ -58,8 +69,8 @@ vlc_module_begin();
     set_capability( "video filter2", 0 );
     add_shortcut( "extract" );
 
-    add_integer( FILTER_PREFIX "component", 0, NULL,
-                 COMPONENT_TEXT, COMPONENT_LONGTEXT, VLC_FALSE );
+    add_integer_with_range( FILTER_PREFIX "component", 0xFF0000, 1, 0xFFFFFF,
+                 NULL, COMPONENT_TEXT, COMPONENT_LONGTEXT, VLC_FALSE );
         change_integer_list( pi_component_values, ppsz_component_descriptions, 0 );
 
     set_callbacks( Create, Destroy );
@@ -69,10 +80,11 @@ static const char *ppsz_filter_options[] = {
     "component", NULL
 };
 
-enum { RED, GREEN, BLUE };
+enum { RED=0xFF0000, GREEN=0x00FF00, BLUE=0x0000FF };
 struct filter_sys_t
 {
     int i_color;
+    int *projection_matrix;
 };
 
 /*****************************************************************************
@@ -89,6 +101,13 @@ static int Create( vlc_object_t *p_this )
         msg_Err( p_filter, "out of memory" );
         return VLC_ENOMEM;
     }
+    p_filter->p_sys->projection_matrix = malloc( 9 * sizeof( int ) );
+    if( !p_filter->p_sys->projection_matrix )
+    {
+        free( p_filter->p_sys );
+        msg_Err( p_filter, "out of memory" );
+        return VLC_ENOMEM;
+    }
 
     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
                        p_filter->p_cfg );
@@ -97,6 +116,17 @@ static int Create( vlc_object_t *p_this )
                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
     p_filter->p_sys->i_color = var_GetInteger( p_filter,
                                                FILTER_PREFIX "component" );
+    switch( p_filter->p_sys->i_color )
+    {
+        case RED:
+        case GREEN:
+        case BLUE:
+            break;
+        default:
+            make_projection_matrix( p_filter->p_sys->i_color,
+                                    p_filter->p_sys->projection_matrix );
+            break;
+    }
 
     p_filter->pf_video_filter = Filter;
 
@@ -110,19 +140,13 @@ static void Destroy( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t *)p_this;
 
+    free( p_filter->p_sys->projection_matrix );
     free( p_filter->p_sys );
 }
 
 /*****************************************************************************
  * Render
  *****************************************************************************/
-static void get_red_from_yuv420( picture_t *, picture_t *, int, int, int );
-static void get_green_from_yuv420( picture_t *, picture_t *, int, int, int );
-static void get_blue_from_yuv420( picture_t *, picture_t *, int, int, int );
-static void get_red_from_yuv422( picture_t *, picture_t *, int, int, int );
-static void get_green_from_yuv422( picture_t *, picture_t *, int, int, int );
-static void get_blue_from_yuv422( picture_t *, picture_t *, int, int, int );
-
 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 {
     picture_t *p_outpic;
@@ -158,6 +182,11 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
                     get_blue_from_yuv420( p_pic, p_outpic,
                                           Y_PLANE, U_PLANE, V_PLANE );
                     break;
+                default:
+                    get_custom_from_yuv420( p_pic, p_outpic,
+                                            Y_PLANE, U_PLANE, V_PLANE,
+                                            p_filter->p_sys->projection_matrix);
+                    break;
             }
             break;
 
@@ -177,6 +206,11 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
                     get_blue_from_yuv422( p_pic, p_outpic,
                                           Y_PLANE, U_PLANE, V_PLANE );
                     break;
+                default:
+                    get_custom_from_yuv422( p_pic, p_outpic,
+                                            Y_PLANE, U_PLANE, V_PLANE,
+                                            p_filter->p_sys->projection_matrix);
+                    break;
             }
             break;
 
@@ -211,6 +245,159 @@ inline uint8_t crop( int a )
 #define U 128
 #define V 128
 
+static void mmult( double *res, double *a, double *b )
+{
+    int i, j, k;
+    for( i = 0; i < 3; i++ )
+    {
+        for( j = 0; j < 3; j++ )
+        {
+            res[ i*3 + j ] = 0.;
+            for( k = 0; k < 3; k++ )
+            {
+                res[ i*3 + j ] += a[ i*3 + k ] * b[ k*3 + j ];
+            }
+        }
+    }
+}
+static void make_projection_matrix( int color, int *matrix )
+{
+    double left_matrix[9] =
+        {  76.24500,  149.68500,  29.07000,
+          -43.02765,  -84.47235, 127.50000,
+          127.50000, -106.76534, -20.73466 };
+    double right_matrix[9] =
+        { 257.00392,   0.00000,  360.31950,
+          257.00392, -88.44438, -183.53583,
+          257.00392, 455.41095,    0.00000 };
+    double red = ((double)(( 0xFF0000 & color )>>16))/255.;
+    double green = ((double)(( 0x00FF00 & color )>>8))/255.;
+    double blue = ((double)( 0x0000FF & color ))/255.;
+    double norm = sqrt( red*red + green*green + blue*blue );
+    red /= norm;
+    green /= norm;
+    blue /= norm;
+    /* XXX: We might still need to norm the rgb_matrix */
+    double rgb_matrix[9] =
+        { red*red,    red*green,   red*blue,
+          red*green,  green*green, green*blue,
+          red*blue,   green*blue,  blue*blue };
+    double result1[9];
+    double result[9];
+    int i;
+    printf( "%f\n", red );
+    printf( "%f\n", green );
+    printf( "%f\n", blue );
+    mmult( result1, rgb_matrix, right_matrix );
+    mmult( result, left_matrix, result1 );
+    for( i = 0; i < 9; i++ )
+    {
+        matrix[i] = (int)result[i];
+        printf( "%d ", matrix[i] );
+        if( i%3 == 2 ) printf( "\n" );
+    }
+}
+static void get_custom_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
+                                    int yp, int up, int vp, int *m )
+{
+    uint8_t *y1in = p_inpic->p[yp].p_pixels;
+    uint8_t *y2in;
+    uint8_t *uin  = p_inpic->p[up].p_pixels;
+    uint8_t *vin  = p_inpic->p[vp].p_pixels;
+
+    uint8_t *y1out = p_outpic->p[yp].p_pixels;
+    uint8_t *y2out;
+    uint8_t *uout  = p_outpic->p[up].p_pixels;
+    uint8_t *vout  = p_outpic->p[vp].p_pixels;
+
+    const int i_pitch = p_inpic->p[yp].i_pitch;
+    const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
+    const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
+
+    const int i_uv_pitch = p_inpic->p[up].i_pitch;
+    const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
+
+    const uint8_t *yend = y1in + i_visible_lines * i_pitch;
+    while( y1in < yend )
+    {
+        const uint8_t *y1end = y1in + i_visible_pitch;
+        y2in  = y1in + i_pitch;
+        y2out = y1out + i_pitch;
+        while( y1in < y1end )
+        {
+/*
+38470   -13239  -27473
+-21710  7471    15504
+-27439  9443    19595
+*/
+            *uout++ = crop( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
+                      / 65536 + U );
+            *vout++ = crop( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
+                      / 65536 + V );
+            *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
+                       / 65536 );
+            *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
+                       / 65536 );
+            *y2out++ = crop( (*y2in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
+                       / 65536 );
+            *y2out++ = crop( (*y2in++ * m[0] + (*uin++ - U) * m[1] + (*vin++ -V) * m[2])
+                       / 65536 );
+        }
+        y1in  += 2*i_pitch - i_visible_pitch;
+        y1out += 2*i_pitch - i_visible_pitch;
+        uin   += i_uv_pitch - i_uv_visible_pitch;
+        uout  += i_uv_pitch - i_uv_visible_pitch;
+        vin   += i_uv_pitch - i_uv_visible_pitch;
+        vout  += i_uv_pitch - i_uv_visible_pitch;
+    }
+}
+static void get_custom_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
+                                    int yp, int up, int vp, int *m )
+{
+    uint8_t *y1in = p_inpic->p[yp].p_pixels;
+    uint8_t *uin  = p_inpic->p[up].p_pixels;
+    uint8_t *vin  = p_inpic->p[vp].p_pixels;
+
+    uint8_t *y1out = p_outpic->p[yp].p_pixels;
+    uint8_t *uout  = p_outpic->p[up].p_pixels;
+    uint8_t *vout  = p_outpic->p[vp].p_pixels;
+
+    const int i_pitch = p_inpic->p[yp].i_pitch;
+    const int i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
+    const int i_visible_lines = p_inpic->p[yp].i_visible_lines;
+
+    const int i_uv_pitch = p_inpic->p[up].i_pitch;
+    const int i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
+
+    const uint8_t *yend = y1in + i_visible_lines * i_pitch;
+    while( y1in < yend )
+    {
+        const uint8_t *y1end = y1in + i_visible_pitch;
+        while( y1in < y1end )
+        {
+/*
+38470   -13239  -27473
+-21710  7471    15504
+-27439  9443    19595
+*/
+            *uout++ = crop( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
+                      / 65536 + U );
+            *vout++ = crop( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
+                      / 65536 + V );
+            *y1out++ = crop( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
+                       / 65536 );
+            *y1out++ = crop( (*y1in++ * m[0] + (*uin++ -U) * m[1] + (*vin++ -V) * m[2])
+                       / 65536 );
+        }
+        y1in  += 2*i_pitch - i_visible_pitch;
+        y1out += 2*i_pitch - i_visible_pitch;
+        uin   += i_uv_pitch - i_uv_visible_pitch;
+        uout  += i_uv_pitch - i_uv_visible_pitch;
+        vin   += i_uv_pitch - i_uv_visible_pitch;
+        vout  += i_uv_pitch - i_uv_visible_pitch;
+    }
+}
+
 static void get_red_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
                                  int yp, int up, int vp )
 {
@@ -307,7 +494,7 @@ static void get_green_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
                        / 65536 );
             *y2out++ = crop( (*y2in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
                        / 65536 );
-            *y2out++ = crop( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -128) * -27473)
+            *y2out++ = crop( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -V) * -27473)
                        / 65536 );
         }
         y1in  += 2*i_pitch - i_visible_pitch;