extract.c 28.7 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 51 52 53 54 55
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 );
56
static void make_projection_matrix( filter_t *, int color, int *matrix );
57 58
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 * );
Antoine Cellerier's avatar
Antoine Cellerier committed
59
static void get_custom_from_packedyuv422( picture_t *, picture_t *, int * );
60 61


62 63 64 65
#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-"

66 67 68
static const int pi_component_values[] = { 0xFF0000, 0x00FF00, 0x0000FF };
static const char *const ppsz_component_descriptions[] = {
    "Red", "Green", "Blue" };
69 70 71 72

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
73 74 75 76 77
vlc_module_begin ()
    set_description( N_("Extract RGB component video filter") )
    set_shortname( N_("Extract" ))
    set_category( CAT_VIDEO )
    set_subcategory( SUBCAT_VIDEO_VFILTER )
78
    set_capability( "video filter", 0 )
79
    add_shortcut( "extract" )
80

81
    add_integer_with_range( FILTER_PREFIX "component", 0xFF0000, 1, 0xFFFFFF,
82
                            COMPONENT_TEXT, COMPONENT_LONGTEXT, false )
83
        change_integer_list( pi_component_values, ppsz_component_descriptions )
84

85 86
    set_callbacks( Create, Destroy )
vlc_module_end ()
87

88
static const char *const ppsz_filter_options[] = {
89 90 91
    "component", NULL
};

92
enum { RED=0xFF0000, GREEN=0x00FF00, BLUE=0x0000FF };
93 94
struct filter_sys_t
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
95
    vlc_mutex_t lock;
96
    int *projection_matrix;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
97
    uint32_t i_color;
98 99 100 101 102 103 104 105 106
};

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

Antoine Cellerier's avatar
Antoine Cellerier committed
107 108
    switch( p_filter->fmt_in.video.i_chroma )
    {
109 110 111
        case VLC_CODEC_I420:
        case VLC_CODEC_J420:
        case VLC_CODEC_YV12:
Antoine Cellerier's avatar
Antoine Cellerier committed
112

113 114
        case VLC_CODEC_I422:
        case VLC_CODEC_J422:
Antoine Cellerier's avatar
Antoine Cellerier committed
115 116 117 118 119 120

        CASE_PACKED_YUV_422
            break;

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

126 127 128 129
    /* Allocate structure */
    p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
    if( p_filter->p_sys == NULL )
        return VLC_ENOMEM;
130 131 132 133 134 135
    p_filter->p_sys->projection_matrix = malloc( 9 * sizeof( int ) );
    if( !p_filter->p_sys->projection_matrix )
    {
        free( p_filter->p_sys );
        return VLC_ENOMEM;
    }
136 137 138 139

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

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

    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
160
    filter_sys_t *p_sys = p_filter->p_sys;
161

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

/*****************************************************************************
 * 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
175
    filter_sys_t *p_sys = p_filter->p_sys;
176 177 178

    if( !p_pic ) return NULL;

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

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
186
    vlc_mutex_lock( &p_sys->lock );
187 188
    switch( p_pic->format.i_chroma )
    {
189 190 191
        case VLC_CODEC_I420:
        case VLC_CODEC_J420:
        case VLC_CODEC_YV12:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
192
            switch( p_sys->i_color )
193 194 195 196 197 198 199 200 201 202 203 204 205
            {
                case RED:
                    get_red_from_yuv420( p_pic, p_outpic,
                                         Y_PLANE, U_PLANE, V_PLANE );
                    break;
                case GREEN:
                    get_green_from_yuv420( p_pic, p_outpic,
                                           Y_PLANE, U_PLANE, V_PLANE );
                    break;
                case BLUE:
                    get_blue_from_yuv420( p_pic, p_outpic,
                                          Y_PLANE, U_PLANE, V_PLANE );
                    break;
206 207 208
                default:
                    get_custom_from_yuv420( p_pic, p_outpic,
                                            Y_PLANE, U_PLANE, V_PLANE,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
209
                                            p_sys->projection_matrix);
210
                    break;
211 212 213
            }
            break;

214 215
        case VLC_CODEC_I422:
        case VLC_CODEC_J422:
216 217 218 219 220 221 222 223 224 225 226 227 228 229
            switch( p_filter->p_sys->i_color )
            {
                case RED:
                    get_red_from_yuv422( p_pic, p_outpic,
                                         Y_PLANE, U_PLANE, V_PLANE );
                    break;
                case GREEN:
                    get_green_from_yuv422( p_pic, p_outpic,
                                           Y_PLANE, U_PLANE, V_PLANE );
                    break;
                case BLUE:
                    get_blue_from_yuv422( p_pic, p_outpic,
                                          Y_PLANE, U_PLANE, V_PLANE );
                    break;
230 231 232
                default:
                    get_custom_from_yuv422( p_pic, p_outpic,
                                            Y_PLANE, U_PLANE, V_PLANE,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
233
                                            p_sys->projection_matrix);
234
                    break;
235 236 237
            }
            break;

Antoine Cellerier's avatar
Antoine Cellerier committed
238 239
        CASE_PACKED_YUV_422
            get_custom_from_packedyuv422( p_pic, p_outpic,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
240
                                          p_sys->projection_matrix );
Antoine Cellerier's avatar
Antoine Cellerier committed
241 242
            break;

243
        default:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
244
            vlc_mutex_unlock( &p_sys->lock );
245
            msg_Warn( p_filter, "Unsupported input chroma (%4.4s)",
246
                      (char*)&(p_pic->format.i_chroma) );
247
            picture_Release( p_pic );
248 249
            return NULL;
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
250
    vlc_mutex_unlock( &p_sys->lock );
251

252
    return CopyInfoAndRelease( p_outpic, p_pic );
253 254 255 256 257
}

#define U 128
#define V 128

258 259
static void mmult( double *res, double *a, double *b )
{
260
    for( int i = 0; i < 3; i++ )
261
    {
262
        for( int j = 0; j < 3; j++ )
263 264
        {
            res[ i*3 + j ] = 0.;
265
            for( int k = 0; k < 3; k++ )
266 267 268 269 270 271
            {
                res[ i*3 + j ] += a[ i*3 + k ] * b[ k*3 + j ];
            }
        }
    }
}
272
static void make_projection_matrix( filter_t *p_filter, int color, int *matrix )
273 274 275 276 277 278 279 280 281 282 283 284 285
{
    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 );
286 287 288 289 290 291
    if( norm > 0 )
    {
        red /= norm;
        green /= norm;
        blue /= norm;
    }
292 293 294 295 296 297 298
    /* 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];
299 300 301
    msg_Dbg( p_filter, "red: %f", red );
    msg_Dbg( p_filter, "green: %f", green );
    msg_Dbg( p_filter, "blue: %f", blue );
302 303
    mmult( result1, rgb_matrix, right_matrix );
    mmult( result, left_matrix, result1 );
304
    for( int i = 0; i < 9; i++ )
305 306 307
    {
        matrix[i] = (int)result[i];
    }
308 309 310 311
    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] );
312
}
313

314 315 316 317 318 319 320 321 322 323 324 325 326
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;

Laurent Aimar's avatar
Laurent Aimar committed
327 328 329
    const int i_in_pitch  = p_inpic->p[yp].i_pitch;
    const int i_out_pitch = p_outpic->p[yp].i_pitch;

330 331 332 333
    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_visible_pitch = p_inpic->p[up].i_visible_pitch;

Laurent Aimar's avatar
Laurent Aimar committed
334
    const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
335 336 337
    while( y1in < yend )
    {
        const uint8_t *y1end = y1in + i_visible_pitch;
Laurent Aimar's avatar
Laurent Aimar committed
338 339
        y2in  = y1in + i_in_pitch;
        y2out = y1out + i_out_pitch;
340 341
        while( y1in < y1end )
        {
342
            *uout++ = vlc_uint8( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
343
                      / 65536 + U );
344
            *vout++ = vlc_uint8( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
345
                      / 65536 + V );
346
            *y1out++ = vlc_uint8( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
347
                       / 65536 );
348
            *y1out++ = vlc_uint8( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
349
                       / 65536 );
350
            *y2out++ = vlc_uint8( (*y2in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
351
                       / 65536 );
352
            *y2out++ = vlc_uint8( (*y2in++ * m[0] + (*uin++ - U) * m[1] + (*vin++ -V) * m[2])
353 354
                       / 65536 );
        }
Laurent Aimar's avatar
Laurent Aimar committed
355 356 357 358 359 360
        y1in  += 2*i_in_pitch  - i_visible_pitch;
        y1out += 2*i_out_pitch - i_visible_pitch;
        uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
        uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
        vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
        vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
361 362 363 364 365 366 367 368 369 370 371 372 373
    }
}
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;

Laurent Aimar's avatar
Laurent Aimar committed
374 375 376
    const int i_in_pitch  = p_inpic->p[yp].i_pitch;
    const int i_out_pitch = p_outpic->p[yp].i_pitch;

377 378 379 380
    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_visible_pitch = p_inpic->p[up].i_visible_pitch;

Laurent Aimar's avatar
Laurent Aimar committed
381
    const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
382 383 384 385 386
    while( y1in < yend )
    {
        const uint8_t *y1end = y1in + i_visible_pitch;
        while( y1in < y1end )
        {
387
            *uout++ = vlc_uint8( (*y1in * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
388
                      / 65536 + U );
389
            *vout++ = vlc_uint8( (*y1in * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
390
                      / 65536 + V );
391
            *y1out++ = vlc_uint8( (*y1in++ * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
392
                       / 65536 );
393
            *y1out++ = vlc_uint8( (*y1in++ * m[0] + (*uin++ -U) * m[1] + (*vin++ -V) * m[2])
394 395
                       / 65536 );
        }
Laurent Aimar's avatar
Laurent Aimar committed
396 397 398 399 400 401
        y1in  += i_in_pitch  - i_visible_pitch;
        y1out += i_out_pitch - i_visible_pitch;
        uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
        uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
        vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
        vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
402 403 404
    }
}

Antoine Cellerier's avatar
Antoine Cellerier committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
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
422 423
    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
424 425 426
    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
427
    const uint8_t *yend = yin + i_visible_lines * i_in_pitch;
Antoine Cellerier's avatar
Antoine Cellerier committed
428 429 430 431 432
    while( yin < yend )
    {
        const uint8_t *ylend = yin + i_visible_pitch;
        while( yin < ylend )
        {
433
            *uout = vlc_uint8( (*yin * m[3] + (*uin-U) * m[4] + (*vin-V) * m[5])
Antoine Cellerier's avatar
Antoine Cellerier committed
434 435
                      / 65536 + U );
            uout += 4;
436
            *vout = vlc_uint8( (*yin * m[6] + (*uin-U) * m[7] + (*vin-V) * m[8])
Antoine Cellerier's avatar
Antoine Cellerier committed
437 438
                     / 65536 + V );
            vout += 4;
439
            *yout = vlc_uint8( (*yin * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
Antoine Cellerier's avatar
Antoine Cellerier committed
440 441 442
                       / 65536 );
            yin  += 2;
            yout += 2;
443
            *yout = vlc_uint8( (*yin * m[0] + (*uin-U) * m[1] + (*vin-V) * m[2])
Antoine Cellerier's avatar
Antoine Cellerier committed
444 445 446 447 448 449
                       / 65536 );
            yin  += 2;
            yout += 2;
            uin  += 4;
            vin  += 4;
        }
Laurent Aimar's avatar
Laurent Aimar committed
450 451 452 453 454 455
        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
456 457 458
    }
}

459 460 461 462 463 464 465 466 467 468 469 470
static void get_red_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
                                 int yp, int up, int vp )
{
    uint8_t *y1in = p_inpic->p[yp].p_pixels;
    uint8_t *y2in;
    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;

Laurent Aimar's avatar
Laurent Aimar committed
471 472 473
    const int i_in_pitch  = p_inpic->p[yp].i_pitch;
    const int i_out_pitch = p_outpic->p[yp].i_pitch;

474 475 476 477
    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_visible_pitch = p_inpic->p[up].i_visible_pitch;

Laurent Aimar's avatar
Laurent Aimar committed
478
    const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
479 480 481
    while( y1in < yend )
    {
        const uint8_t *y1end = y1in + i_visible_pitch;
Laurent Aimar's avatar
Laurent Aimar committed
482 483
        y2in  = y1in + i_in_pitch;
        y2out = y1out + i_out_pitch;
484 485 486 487 488 489 490
        while( y1in < y1end )
        {
/*
19595   0   27473
-11058  0   -15504
32768   0   45941
*/
491
            *uout++ = vlc_uint8( (*y1in * -11058 + (*vin - V) * -15504)
492
                      / 65536 + U );
493
            *vout++ = vlc_uint8( (*y1in * 32768 + (*vin - V) * 45941)
494
                      / 65536 + V );
495
            *y1out++ = vlc_uint8( (*y1in++ * 19595 + (*vin - V) * 27473)
496
                       / 65536 );
497
            *y1out++ = vlc_uint8( (*y1in++ * 19595 + (*vin - V) * 27473)
498
                       / 65536 );
499
            *y2out++ = vlc_uint8( (*y2in++ * 19594 + (*vin - V) * 27473)
500
                       / 65536 );
501
            *y2out++ = vlc_uint8( (*y2in++ * 19594 + (*vin++ - V) * 27473)
502 503
                       / 65536 );
        }
Laurent Aimar's avatar
Laurent Aimar committed
504 505 506 507 508
        y1in  += 2*i_in_pitch  - i_visible_pitch;
        y1out += 2*i_out_pitch - i_visible_pitch;
        uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
        vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
        vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    }
}

static void get_green_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
                                 int yp, int up, int vp )
{
    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;

Laurent Aimar's avatar
Laurent Aimar committed
525 526 527
    const int i_in_pitch  = p_inpic->p[yp].i_pitch;
    const int i_out_pitch = p_outpic->p[yp].i_pitch;

528 529 530 531 532
    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_visible_pitch = p_inpic->p[up].i_visible_pitch;

Laurent Aimar's avatar
Laurent Aimar committed
533
    const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
534 535 536
    while( y1in < yend )
    {
        const uint8_t *y1end = y1in + i_visible_pitch;
Laurent Aimar's avatar
Laurent Aimar committed
537 538
        y2in  = y1in + i_in_pitch;
        y2out = y1out + i_out_pitch;
539 540 541 542 543 544 545
        while( y1in < y1end )
        {
/*
38470   -13239  -27473
-21710  7471    15504
-27439  9443    19595
*/
546
            *uout++ = vlc_uint8( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
547
                      / 65536 + U );
548
            *vout++ = vlc_uint8( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
549
                      / 65536 + V );
550
            *y1out++ = vlc_uint8( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
551
                       / 65536 );
552
            *y1out++ = vlc_uint8( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
553
                       / 65536 );
554
            *y2out++ = vlc_uint8( (*y2in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
555
                       / 65536 );
556
            *y2out++ = vlc_uint8( (*y2in++ * 38470 + (*uin++ - U) * -13239 + (*vin++ -V) * -27473)
557 558
                       / 65536 );
        }
Laurent Aimar's avatar
Laurent Aimar committed
559 560 561 562 563 564
        y1in  += 2*i_in_pitch  - i_visible_pitch;
        y1out += 2*i_out_pitch - i_visible_pitch;
        uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
        uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
        vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
        vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    }
}

static void get_blue_from_yuv420( picture_t *p_inpic, picture_t *p_outpic,
                                 int yp, int up, int vp )
{
    uint8_t *y1in = p_inpic->p[yp].p_pixels;
    uint8_t *y2in;
    uint8_t *uin  = p_inpic->p[up].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;

Laurent Aimar's avatar
Laurent Aimar committed
580 581 582
    const int i_in_pitch  = p_inpic->p[yp].i_pitch;
    const int i_out_pitch = p_outpic->p[yp].i_pitch;

583 584 585 586
    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_visible_pitch = p_inpic->p[up].i_visible_pitch;

Laurent Aimar's avatar
Laurent Aimar committed
587
    const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
588 589 590
    while( y1in < yend )
    {
        const uint8_t *y1end = y1in + i_visible_pitch;
Laurent Aimar's avatar
Laurent Aimar committed
591 592
        y2in  = y1in + i_in_pitch;
        y2out = y1out + i_out_pitch;
593 594 595 596 597 598 599
        while( y1in < y1end )
        {
/*
7471    13239   0
32768   58065   0
-5329   -9443   0
*/
600
            *uout++ = vlc_uint8( (*y1in* 32768 + (*uin - U) * 58065 )
601
                      / 65536 + U );
602
            *vout++ = vlc_uint8( (*y1in * -5329 + (*uin - U) * -9443 )
603
                      / 65536 + V );
604
            *y1out++ = vlc_uint8( (*y1in++ * 7471 + (*uin - U) * 13239 )
605
                       / 65536 );
606
            *y1out++ = vlc_uint8( (*y1in++ * 7471 + (*uin - U) * 13239 )
607
                       / 65536 );
608
            *y2out++ = vlc_uint8( (*y2in++ * 7471 + (*uin - U) * 13239 )
609
                       / 65536 );
610
            *y2out++ = vlc_uint8( (*y2in++ * 7471 + (*uin++ - U) * 13239 )
611 612
                       / 65536 );
        }
Laurent Aimar's avatar
Laurent Aimar committed
613 614 615 616 617
        y1in  += 2*i_in_pitch  - i_visible_pitch;
        y1out += 2*i_out_pitch - i_visible_pitch;
        uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
        uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
        vout  += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
618 619 620 621 622 623 624 625 626 627 628 629 630
    }
}

static void get_red_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
                                 int yp, int up, int vp )
{
    uint8_t *y1in = p_inpic->p[yp].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;

Laurent Aimar's avatar
Laurent Aimar committed
631 632 633
    const int i_in_pitch = p_inpic->p[yp].i_pitch;
    const int i_out_pitch = p_inpic->p[yp].i_pitch;

634 635 636 637
    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_visible_pitch = p_inpic->p[up].i_visible_pitch;

Laurent Aimar's avatar
Laurent Aimar committed
638
    const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
639 640 641 642 643 644 645 646 647 648
    while( y1in < yend )
    {
        const uint8_t *y1end = y1in + i_visible_pitch;
        while( y1in < y1end )
        {
/*
19595   0   27473
-11058  0   -15504
32768   0   45941
*/
649
            *uout++ = vlc_uint8( (*y1in * -11058 + (*vin - V) * -15504)
650
                      / 65536 + U );
651
            *vout++ = vlc_uint8( (*y1in * 32768 + (*vin - V) * 45941)
652
                      / 65536 + V );
653
            *y1out++ = vlc_uint8( (*y1in++ * 19595 + (*vin - V) * 27473)
654
                       / 65536 );
655
            *y1out++ = vlc_uint8( (*y1in++ * 19595 + (*vin++ - V) * 27473)
656 657
                       / 65536 );
        }
Laurent Aimar's avatar
Laurent Aimar committed
658 659 660 661 662
        y1in  += i_in_pitch  - i_visible_pitch;
        y1out += i_out_pitch - i_visible_pitch;
        uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
        vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
        vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
663 664 665 666 667 668 669 670 671 672 673 674 675 676
    }
}

static void get_green_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
                                   int yp, int up, int vp )
{
    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;

Laurent Aimar's avatar
Laurent Aimar committed
677 678 679
    const int i_in_pitch  = p_inpic->p[yp].i_pitch;
    const int i_out_pitch = p_outpic->p[yp].i_pitch;

680 681 682 683
    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_visible_pitch = p_inpic->p[up].i_visible_pitch;

Laurent Aimar's avatar
Laurent Aimar committed
684
    const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
685 686 687 688 689 690 691 692 693 694
    while( y1in < yend )
    {
        const uint8_t *y1end = y1in + i_visible_pitch;
        while( y1in < y1end )
        {
/*
38470   -13239  -27473
-21710  7471    15504
-27439  9443    19595
*/
695
            *uout++ = vlc_uint8( (*y1in * -21710 + (*uin-U) * 7471 + (*vin-V) * 15504)
696
                      / 65536 + U );
697
            *vout++ = vlc_uint8( (*y1in * -27439 + (*uin-U) * 9443 + (*vin-V) * 19595)
698
                      / 65536 + V );
699
            *y1out++ = vlc_uint8( (*y1in++ * 38470 + (*uin-U) * -13239 + (*vin-V) * -27473)
700
                       / 65536 );
701
            *y1out++ = vlc_uint8( (*y1in++ * 38470 + (*uin++-U) * -13239 + (*vin++-V) * -27473)
702 703
                       / 65536 );
        }
Laurent Aimar's avatar
Laurent Aimar committed
704 705 706 707 708 709
        y1in  += i_in_pitch  - i_visible_pitch;
        y1out += i_out_pitch - i_visible_pitch;
        uin   += p_inpic->p[up].i_pitch  - i_uv_visible_pitch;
        uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
        vin   += p_inpic->p[vp].i_pitch  - i_uv_visible_pitch;
        vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
710 711 712 713 714 715 716 717 718 719 720 721 722
    }
}

static void get_blue_from_yuv422( picture_t *p_inpic, picture_t *p_outpic,
                                 int yp, int up, int vp )
{
    uint8_t *y1in = p_inpic->p[yp].p_pixels;
    uint8_t *uin  = p_inpic->p[up].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;

Laurent Aimar's avatar
Laurent Aimar committed
723 724 725
    const int i_in_pitch  = p_inpic->p[yp].i_pitch;
    const int i_out_pitch = p_outpic->p[yp].i_pitch;

726 727 728 729
    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_visible_pitch = p_inpic->p[up].i_visible_pitch;

Laurent Aimar's avatar
Laurent Aimar committed
730
    const uint8_t *yend = y1in + i_visible_lines * i_in_pitch;
731 732 733 734 735 736 737 738 739 740
    while( y1in < yend )
    {
        const uint8_t *y1end = y1in + i_visible_pitch;
        while( y1in < y1end )
        {
/*
7471    13239   0
32768   58065   0
-5329   -9443   0
*/
741
            *uout++ = vlc_uint8( (*y1in* 32768 + (*uin - U) * 58065 )
742
                      / 65536 + U );
743
            *vout++ = vlc_uint8( (*y1in * -5329 + (*uin - U) * -9443 )
744
                      / 65536 + V );
745
            *y1out++ = vlc_uint8( (*y1in++ * 7471 + (*uin - U) * 13239 )
746
                       / 65536 );
747
            *y1out++ = vlc_uint8( (*y1in++ * 7471 + (*uin++ - U) * 13239 )
748 749
                       / 65536 );
        }
Laurent Aimar's avatar
Laurent Aimar committed
750 751 752 753 754
        y1in  += i_in_pitch  - i_visible_pitch;
        y1out += i_out_pitch - i_visible_pitch;
        uin   += p_inpic->p[up].i_pitch - i_uv_visible_pitch;
        uout  += p_outpic->p[up].i_pitch - i_uv_visible_pitch;
        vout  += p_outpic->p[vp].i_pitch - i_uv_visible_pitch;
755 756
    }
}
757 758 759 760 761

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
762
    VLC_UNUSED(oldval);
763 764
    filter_sys_t *p_sys = (filter_sys_t *)p_data;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
765
    vlc_mutex_lock( &p_sys->lock );
766 767 768
    if( !strcmp( psz_var, FILTER_PREFIX "component" ) )
    {
        p_sys->i_color = newval.i_int;
Antoine Cellerier's avatar
Antoine Cellerier committed
769 770 771
        /* 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 );
772 773 774 775 776
    }
    else
    {
        msg_Warn( p_this, "Unknown callback command." );
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
777
    vlc_mutex_unlock( &p_sys->lock );
778 779
    return VLC_SUCCESS;
}