extract.c 15 KB
Newer Older
1 2 3
/*****************************************************************************
 * extract.c : Extract RGB components
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2000-2006 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea .t videolan d@t org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
9 10 11
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
12 13 14 15
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20 21
 * You should have received a copy of the GNU Lesser 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.
22 23 24 25 26 27
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

28 29 30 31
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

32 33
#include <math.h>

34
#include <vlc_common.h>
35
#include <vlc_plugin.h>
Rémi Duraffort's avatar
Rémi Duraffort committed
36
#include <vlc_filter.h>
37
#include <vlc_picture.h>
Antoine Cellerier's avatar
Antoine Cellerier committed
38
#include "filter_picture.h"
39 40 41 42 43 44 45 46

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Create      ( vlc_object_t * );
static void Destroy     ( vlc_object_t * );

static picture_t *Filter( filter_t *, picture_t * );
47 48
static int ExtractCallback( vlc_object_t *, char const *,
                            vlc_value_t, vlc_value_t, void * );
49

50
static void make_projection_matrix( filter_t *, int color, int *matrix );
51
static void get_custom_from_yuv( picture_t *, picture_t *, int const, int const, int const, int const * );
Antoine Cellerier's avatar
Antoine Cellerier committed
52
static void get_custom_from_packedyuv422( picture_t *, picture_t *, int * );
53 54


55 56 57 58
#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-"

59 60 61
static const int pi_component_values[] = { 0xFF0000, 0x00FF00, 0x0000FF };
static const char *const ppsz_component_descriptions[] = {
    "Red", "Green", "Blue" };
62 63 64 65

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
66 67 68 69 70
vlc_module_begin ()
    set_description( N_("Extract RGB component video filter") )
    set_shortname( N_("Extract" ))
    set_category( CAT_VIDEO )
    set_subcategory( SUBCAT_VIDEO_VFILTER )
71
    set_capability( "video filter", 0 )
72
    add_shortcut( "extract" )
73

74
    add_integer_with_range( FILTER_PREFIX "component", 0xFF0000, 1, 0xFFFFFF,
75
                            COMPONENT_TEXT, COMPONENT_LONGTEXT, false )
76
        change_integer_list( pi_component_values, ppsz_component_descriptions )
77

78 79
    set_callbacks( Create, Destroy )
vlc_module_end ()
80

81
static const char *const ppsz_filter_options[] = {
82 83 84
    "component", NULL
};

85
enum { RED=0xFF0000, GREEN=0x00FF00, BLUE=0x0000FF };
86 87
struct filter_sys_t
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
88
    vlc_mutex_t lock;
89
    int *projection_matrix;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
90
    uint32_t i_color;
91 92 93 94 95 96 97 98 99
};

/*****************************************************************************
 * Create
 *****************************************************************************/
static int Create( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;

Antoine Cellerier's avatar
Antoine Cellerier committed
100 101
    switch( p_filter->fmt_in.video.i_chroma )
    {
102
        case VLC_CODEC_I420:
103 104
        case VLC_CODEC_I420_10L:
        case VLC_CODEC_I420_10B:
105 106
        case VLC_CODEC_J420:
        case VLC_CODEC_YV12:
Antoine Cellerier's avatar
Antoine Cellerier committed
107

108 109
        case VLC_CODEC_I422:
        case VLC_CODEC_J422:
Antoine Cellerier's avatar
Antoine Cellerier committed
110 111 112 113 114 115

        CASE_PACKED_YUV_422
            break;

        default:
            /* We only want planar YUV 4:2:0 or 4:2:2 */
116
            msg_Err( p_filter, "Unsupported input chroma (%4.4s)",
Antoine Cellerier's avatar
Antoine Cellerier committed
117 118 119 120
                     (char*)&(p_filter->fmt_in.video.i_chroma) );
            return VLC_EGENERIC;
    }

121
    /* Allocate structure */
122 123
    filter_sys_t *p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_sys == NULL )
124
        return VLC_ENOMEM;
125 126 127 128
    p_filter->p_sys = p_sys;

    p_sys->projection_matrix = malloc( 9 * sizeof( int ) );
    if( !p_sys->projection_matrix )
129
    {
130
        free( p_sys );
131 132
        return VLC_ENOMEM;
    }
133 134 135 136

    config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
                       p_filter->p_cfg );

137
    p_sys->i_color = var_CreateGetIntegerCommand( p_filter,
138
                                               FILTER_PREFIX "component" );
Antoine Cellerier's avatar
Antoine Cellerier committed
139
    /* Matrix won't be used for RED, GREEN or BLUE in planar formats */
140 141 142
    make_projection_matrix( p_filter, p_sys->i_color,
                            p_sys->projection_matrix );
    vlc_mutex_init( &p_sys->lock );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
143
    var_AddCallback( p_filter, FILTER_PREFIX "component",
144
                     ExtractCallback, p_sys );
145 146 147 148 149 150 151 152 153 154 155 156

    p_filter->pf_video_filter = Filter;

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Destroy
 *****************************************************************************/
static void Destroy( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t *)p_this;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
157
    filter_sys_t *p_sys = p_filter->p_sys;
158

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
159 160 161 162 163
    var_DelCallback( p_filter, FILTER_PREFIX "component", ExtractCallback,
                     p_sys );
    vlc_mutex_destroy( &p_sys->lock );
    free( p_sys->projection_matrix );
    free( p_sys );
164 165 166 167 168 169 170 171
}

/*****************************************************************************
 * Render
 *****************************************************************************/
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
    picture_t *p_outpic;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
172
    filter_sys_t *p_sys = p_filter->p_sys;
173 174 175

    if( !p_pic ) return NULL;

Laurent Aimar's avatar
Laurent Aimar committed
176
    p_outpic = filter_NewPicture( p_filter );
177 178
    if( !p_outpic )
    {
179
        picture_Release( p_pic );
180 181 182
        return NULL;
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
183
    vlc_mutex_lock( &p_sys->lock );
184 185
    switch( p_pic->format.i_chroma )
    {
186
    case VLC_CODEC_I420:
187 188
    case VLC_CODEC_I420_10L:
    case VLC_CODEC_I420_10B:
189 190 191 192
    case VLC_CODEC_J420:
    case VLC_CODEC_YV12:
    case VLC_CODEC_I422:
    case VLC_CODEC_J422:
193
        get_custom_from_yuv( p_pic, p_outpic, Y_PLANE, U_PLANE, V_PLANE, p_sys->projection_matrix );
194
        break;
195

Antoine Cellerier's avatar
Antoine Cellerier committed
196
        CASE_PACKED_YUV_422
197
            get_custom_from_packedyuv422( p_pic, p_outpic, p_sys->projection_matrix );
Antoine Cellerier's avatar
Antoine Cellerier committed
198 199
            break;

200 201 202 203 204 205
    default:
        vlc_mutex_unlock( &p_sys->lock );
        msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
                  (char*)&(p_pic->format.i_chroma) );
        picture_Release( p_pic );
        return NULL;
206
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
207
    vlc_mutex_unlock( &p_sys->lock );
208

209
    return CopyInfoAndRelease( p_outpic, p_pic );
210 211
}

212 213 214 215 216
#define U8 128
#define V8 128

#define U10 512
#define V10 512
217

218 219
static void mmult( double *res, double *a, double *b )
{
220
    for( int i = 0; i < 3; i++ )
221
    {
222
        for( int j = 0; j < 3; j++ )
223 224
        {
            res[ i*3 + j ] = 0.;
225
            for( int k = 0; k < 3; k++ )
226 227 228 229 230 231
            {
                res[ i*3 + j ] += a[ i*3 + k ] * b[ k*3 + j ];
            }
        }
    }
}
232
static void make_projection_matrix( filter_t *p_filter, int color, int *matrix )
233 234 235 236 237 238 239 240 241 242 243 244 245
{
    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 );
246 247 248 249 250 251
    if( norm > 0 )
    {
        red /= norm;
        green /= norm;
        blue /= norm;
    }
252 253 254 255 256 257 258
    /* 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];
259 260 261
    msg_Dbg( p_filter, "red: %f", red );
    msg_Dbg( p_filter, "green: %f", green );
    msg_Dbg( p_filter, "blue: %f", blue );
262 263
    mmult( result1, rgb_matrix, right_matrix );
    mmult( result, left_matrix, result1 );
264
    for( int i = 0; i < 9; i++ )
265 266 267
    {
        matrix[i] = (int)result[i];
    }
268 269 270 271
    msg_Dbg( p_filter, "Projection matrix:" );
    msg_Dbg( p_filter, "%6d %6d %6d", matrix[0], matrix[1], matrix[2] );
    msg_Dbg( p_filter, "%6d %6d %6d", matrix[3], matrix[4], matrix[5] );
    msg_Dbg( p_filter, "%6d %6d %6d", matrix[6], matrix[7], matrix[8] );
272
}
273

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
#define IS_YUV_10BITS(fmt) (fmt == VLC_CODEC_I420_10L || fmt == VLC_CODEC_I420_10B)

#define GET_CUSTOM_PIX() \
    do \
    { \
        val = (*y_in[0] * m[3] + (**u_in - u) * m[4] + (**v_in - v) * m[5]) / 65536 + u; \
        *(*u_out)++ = VLC_CLIP( val, 0, maxval ); \
        val = (*y_in[0] * m[6] + (**u_in - u) * m[7] + (**v_in - v) * m[8]) / 65536 + v; \
        *(*v_out)++ = VLC_CLIP( val, 0, maxval ); \
        val = (*y_in[0]++ * m[0] + (**u_in - u) * m[1] + (**v_in - v) * m[2]) / 65536; \
        *y_out[0]++ = VLC_CLIP( val, 0, maxval ); \
        val = (*y_in[0]++ * m[0] + (**u_in - u) * m[1] + (**v_in - v) * m[2]) / 65536; \
        *y_out[0]++ = VLC_CLIP( val, 0, maxval ); \
        val = (*y_in[1]++ * m[0] + (**u_in - u) * m[1] + (**v_in - v) * m[2]) / 65536; \
        *y_out[1]++ = VLC_CLIP( val, 0, maxval ); \
        val = (*y_in[1]++ * m[0] + (*(*u_in)++ - u) * m[1] + (*(*v_in)++ - v) * m[2]) / 65536; \
        *y_out[1]++ = VLC_CLIP( val, 0, maxval ); \
    } while (0);

static inline void
get_custom_pix_8b( uint8_t *y_in[2], uint8_t *y_out[2],
                   uint8_t **u_in, uint8_t **u_out,
                   uint8_t **v_in, uint8_t **v_out,
                   uint16_t const u, uint16_t const v,
                   int const *m, int maxval )
{
    uint8_t val;
    GET_CUSTOM_PIX();
}

static inline void
get_custom_pix_10b( uint16_t *y_in[2], uint16_t *y_out[2],
                    uint16_t **u_in, uint16_t **u_out,
                    uint16_t **v_in, uint16_t **v_out,
                    uint16_t const u, uint16_t const v,
                    int const *m, int maxval )
{
    uint16_t val;
    GET_CUSTOM_PIX();
}

315 316 317
static void
get_custom_from_yuv( picture_t *p_inpic, picture_t *p_outpic,
                     int const yp, int const up, int const vp, int const *m )
318
{
319 320 321 322 323 324 325 326 327 328 329 330 331 332
    int const   i_in_pitch  = p_inpic->p[yp].i_pitch;
    int const   i_out_pitch = p_outpic->p[yp].i_pitch;
    int const   i_visible_pitch = p_inpic->p[yp].i_visible_pitch;
    int const   i_visible_lines = p_inpic->p[yp].i_visible_lines;
    int const   i_uv_visible_pitch = p_inpic->p[up].i_visible_pitch;
    uint8_t     *y_in[2] = { p_inpic->p[yp].p_pixels };
    uint8_t     *u_in = p_inpic->p[up].p_pixels;
    uint8_t     *v_in = p_inpic->p[vp].p_pixels;
    uint8_t     *y_out[2] = { p_outpic->p[yp].p_pixels };
    uint8_t     *u_out = p_outpic->p[up].p_pixels;
    uint8_t     *v_out = p_outpic->p[vp].p_pixels;
    uint8_t *const y_end = y_in[0] + i_visible_lines * i_in_pitch;

    while (y_in[0] < y_end)
333
    {
334 335 336
        y_in[1] = y_in[0] + i_in_pitch;
        y_out[1] = y_out[0] + i_out_pitch;
        for (uint8_t *const y_row_end = y_in[0] + i_visible_pitch; y_in[0] < y_row_end; )
337
        {
338 339 340 341 342 343 344
            !IS_YUV_10BITS(p_inpic->format.i_chroma)
                ? get_custom_pix_8b(y_in, y_out, &u_in, &u_out, &v_in, &v_out, U8,
                                 V8, m, 255)
                : get_custom_pix_10b((uint16_t **)y_in, (uint16_t **)y_out,
                                  (uint16_t **)&u_in, (uint16_t **)&u_out,
                                  (uint16_t **)&v_in, (uint16_t **)&v_out, U10,
                                  V10, m, 1023);
345
        }
346 347 348 349 350 351
        y_in[0] += 2 * i_in_pitch - i_visible_pitch;
        y_out[0] += 2 * i_out_pitch - i_visible_pitch;
        u_in += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
        u_out += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
        v_in += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
        v_out += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
352 353 354
    }
}

Antoine Cellerier's avatar
Antoine Cellerier committed
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
static void get_custom_from_packedyuv422( picture_t *p_inpic,
                                          picture_t *p_outpic,
                                          int *m )
{
    int i_y_offset, i_u_offset, i_v_offset;
    if( GetPackedYuvOffsets( p_inpic->format.i_chroma, &i_y_offset,
                         &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
        return;

    uint8_t *yin = p_inpic->p->p_pixels + i_y_offset;
    uint8_t *uin = p_inpic->p->p_pixels + i_u_offset;
    uint8_t *vin = p_inpic->p->p_pixels + i_v_offset;

    uint8_t *yout = p_outpic->p->p_pixels + i_y_offset;
    uint8_t *uout = p_outpic->p->p_pixels + i_u_offset;
    uint8_t *vout = p_outpic->p->p_pixels + i_v_offset;

Laurent Aimar's avatar
Laurent Aimar committed
372 373
    const int i_in_pitch  = p_inpic->p->i_pitch;
    const int i_out_pitch = p_outpic->p->i_pitch;
Antoine Cellerier's avatar
Antoine Cellerier committed
374 375 376
    const int i_visible_pitch = p_inpic->p->i_visible_pitch;
    const int i_visible_lines = p_inpic->p->i_visible_lines;

Laurent Aimar's avatar
Laurent Aimar committed
377
    const uint8_t *yend = yin + i_visible_lines * i_in_pitch;
Antoine Cellerier's avatar
Antoine Cellerier committed
378 379 380 381 382
    while( yin < yend )
    {
        const uint8_t *ylend = yin + i_visible_pitch;
        while( yin < ylend )
        {
383
            *uout = vlc_uint8( (*yin * m[3] + (*uin-U8) * m[4] + (*vin-V8) * m[5]) / 65536 + U8 );
Antoine Cellerier's avatar
Antoine Cellerier committed
384
            uout += 4;
385
            *vout = vlc_uint8( (*yin * m[6] + (*uin-U8) * m[7] + (*vin-V8) * m[8]) / 65536 + V8 );
Antoine Cellerier's avatar
Antoine Cellerier committed
386
            vout += 4;
387
            *yout = vlc_uint8( (*yin * m[0] + (*uin-U8) * m[1] + (*vin-V8) * m[2]) / 65536 );
Antoine Cellerier's avatar
Antoine Cellerier committed
388 389
            yin  += 2;
            yout += 2;
390
            *yout = vlc_uint8( (*yin * m[0] + (*uin-U8) * m[1] + (*vin-V8) * m[2]) / 65536 );
Antoine Cellerier's avatar
Antoine Cellerier committed
391 392 393 394 395
            yin  += 2;
            yout += 2;
            uin  += 4;
            vin  += 4;
        }
Laurent Aimar's avatar
Laurent Aimar committed
396 397 398 399 400 401
        yin  += i_in_pitch  - i_visible_pitch;
        yout += i_out_pitch - i_visible_pitch;
        uin  += i_in_pitch  - i_visible_pitch;
        uout += i_out_pitch - i_visible_pitch;
        vin  += i_in_pitch  - i_visible_pitch;
        vout += i_out_pitch - i_visible_pitch;
Antoine Cellerier's avatar
Antoine Cellerier committed
402 403 404
    }
}

405 406 407 408
static int ExtractCallback( vlc_object_t *p_this, char const *psz_var,
                            vlc_value_t oldval, vlc_value_t newval,
                            void *p_data )
{
Rafaël Carré's avatar
Rafaël Carré committed
409
    VLC_UNUSED(oldval);
410 411
    filter_sys_t *p_sys = (filter_sys_t *)p_data;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
412
    vlc_mutex_lock( &p_sys->lock );
413 414 415
    if( !strcmp( psz_var, FILTER_PREFIX "component" ) )
    {
        p_sys->i_color = newval.i_int;
Antoine Cellerier's avatar
Antoine Cellerier committed
416 417 418
        /* Matrix won't be used for RED, GREEN or BLUE in planar formats */
        make_projection_matrix( (filter_t *)p_this, p_sys->i_color,
                                p_sys->projection_matrix );
419 420 421 422 423
    }
    else
    {
        msg_Warn( p_this, "Unknown callback command." );
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
424
    vlc_mutex_unlock( &p_sys->lock );
425 426
    return VLC_SUCCESS;
}