yuy2_i420.c 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*****************************************************************************
 * yuy2_i420.c : Packed YUV 4:2:2 to Planar YUV conversion module for vlc
 *****************************************************************************
 * Copyright (C) 2007 the VideoLAN team
 * $Id$
 *
 * Authors: 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
 * 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
 *****************************************************************************/

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

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
34
#include <vlc_filter.h>
35 36 37 38 39 40 41 42 43 44
#include <vlc_vout.h>

#define SRC_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422,cyuv"
#define DEST_FOURCC  "I420"

/*****************************************************************************
 * Local and extern prototypes.
 *****************************************************************************/
static int  Activate ( vlc_object_t * );

45 46 47 48
static void YUY2_I420           ( filter_t *, picture_t *, picture_t * );
static void YVYU_I420           ( filter_t *, picture_t *, picture_t * );
static void UYVY_I420           ( filter_t *, picture_t *, picture_t * );
static void cyuv_I420           ( filter_t *, picture_t *, picture_t * );
49

50 51 52 53 54
static picture_t *YUY2_I420_Filter    ( filter_t *, picture_t * );
static picture_t *YVYU_I420_Filter    ( filter_t *, picture_t * );
static picture_t *UYVY_I420_Filter    ( filter_t *, picture_t * );
static picture_t *cyuv_I420_Filter    ( filter_t *, picture_t * );

55 56 57 58
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
59
    set_description( N_("Conversions from " SRC_FOURCC " to " DEST_FOURCC) );
60
    set_capability( "video filter2", 80 );
61 62 63 64 65 66 67 68 69 70
    set_callbacks( Activate, NULL );
vlc_module_end();

/*****************************************************************************
 * Activate: allocate a chroma function
 *****************************************************************************
 * This function allocates and initializes a chroma function
 *****************************************************************************/
static int Activate( vlc_object_t *p_this )
{
71
    filter_t *p_filter = (filter_t *)p_this;
72

73 74
    if( p_filter->fmt_in.video.i_width & 1
     || p_filter->fmt_in.video.i_height & 1 )
75 76 77 78
    {
        return -1;
    }

79 80 81 82
    if( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width
     || p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height )
        return -1;

83
    switch( p_filter->fmt_out.video.i_chroma )
84 85
    {
        case VLC_FOURCC('I','4','2','0'):
86
            switch( p_filter->fmt_in.video.i_chroma )
87 88 89
            {
                case VLC_FOURCC('Y','U','Y','2'):
                case VLC_FOURCC('Y','U','N','V'):
90
                    p_filter->pf_video_filter = YUY2_I420_Filter;
91 92 93
                    break;

                case VLC_FOURCC('Y','V','Y','U'):
94
                    p_filter->pf_video_filter = YVYU_I420_Filter;
95 96 97 98 99
                    break;

                case VLC_FOURCC('U','Y','V','Y'):
                case VLC_FOURCC('U','Y','N','V'):
                case VLC_FOURCC('Y','4','2','2'):
100
                    p_filter->pf_video_filter = UYVY_I420_Filter;
101 102 103
                    break;

                case VLC_FOURCC('c','y','u','v'):
104
                    p_filter->pf_video_filter = cyuv_I420_Filter;
105 106 107 108 109 110 111 112 113 114 115 116 117 118
                    break;

                default:
                    return -1;
            }
            break;

        default:
            return -1;
    }
    return 0;
}

/* Following functions are local */
119 120 121 122
VIDEO_FILTER_WRAPPER( YUY2_I420 )
VIDEO_FILTER_WRAPPER( YVYU_I420 )
VIDEO_FILTER_WRAPPER( UYVY_I420 )
VIDEO_FILTER_WRAPPER( cyuv_I420 )
123 124 125 126

/*****************************************************************************
 * YUY2_I420: packed YUY2 4:2:2 to planar YUV 4:2:0
 *****************************************************************************/
127 128
static void YUY2_I420( filter_t *p_filter, picture_t *p_source,
                                           picture_t *p_dest )
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
{
    uint8_t *p_line = p_source->p->p_pixels;

    uint8_t *p_y = p_dest->Y_PIXELS;
    uint8_t *p_u = p_dest->U_PIXELS;
    uint8_t *p_v = p_dest->V_PIXELS;

    int i_x, i_y;

    const int i_dest_margin = p_dest->p[0].i_pitch
                                 - p_dest->p[0].i_visible_pitch;
    const int i_dest_margin_c = p_dest->p[1].i_pitch
                                 - p_dest->p[1].i_visible_pitch;
    const int i_source_margin = p_source->p->i_pitch
                               - p_source->p->i_visible_pitch;

145
    bool b_skip = false;
146

147
    for( i_y = p_filter->fmt_out.video.i_height ; i_y-- ; )
148 149 150
    {
        if( b_skip )
        {
151
            for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
152 153 154 155 156 157 158 159 160
            {
    #define C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v )      \
                *p_y++ = *p_line++; p_line++; \
                *p_y++ = *p_line++; p_line++
                C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
                C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
                C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
                C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
            }
161
            for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
162 163 164 165 166 167
            {
                C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
            }
        }
        else
        {
168
            for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
169 170 171 172 173 174 175 176 177
            {
    #define C_YUYV_YUV422( p_line, p_y, p_u, p_v )      \
                *p_y++ = *p_line++; *p_u++ = *p_line++; \
                *p_y++ = *p_line++; *p_v++ = *p_line++
                C_YUYV_YUV422( p_line, p_y, p_u, p_v );
                C_YUYV_YUV422( p_line, p_y, p_u, p_v );
                C_YUYV_YUV422( p_line, p_y, p_u, p_v );
                C_YUYV_YUV422( p_line, p_y, p_u, p_v );
            }
178
            for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
            {
                C_YUYV_YUV422( p_line, p_y, p_u, p_v );
            }
        }
        p_line += i_source_margin;
        p_y += i_dest_margin;
        p_u += i_dest_margin_c;
        p_v += i_dest_margin_c;

        b_skip = !b_skip;
    }
}

/*****************************************************************************
 * YVYU_I420: packed YVYU 4:2:2 to planar YUV 4:2:0
 *****************************************************************************/
195 196
static void YVYU_I420( filter_t *p_filter, picture_t *p_source,
                                           picture_t *p_dest )
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
{
    uint8_t *p_line = p_source->p->p_pixels;

    uint8_t *p_y = p_dest->Y_PIXELS;
    uint8_t *p_u = p_dest->U_PIXELS;
    uint8_t *p_v = p_dest->V_PIXELS;

    int i_x, i_y;

    const int i_dest_margin = p_dest->p[0].i_pitch
                                 - p_dest->p[0].i_visible_pitch;
    const int i_dest_margin_c = p_dest->p[1].i_pitch
                                 - p_dest->p[1].i_visible_pitch;
    const int i_source_margin = p_source->p->i_pitch
                               - p_source->p->i_visible_pitch;

213
    bool b_skip = false;
214

215
    for( i_y = p_filter->fmt_out.video.i_height ; i_y-- ; )
216 217 218
    {
        if( b_skip )
        {
219
            for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
220 221 222 223 224 225 226 227 228
            {
    #define C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v )      \
                *p_y++ = *p_line++; p_line++; \
                *p_y++ = *p_line++; p_line++
                C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
                C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
                C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
                C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
            }
229
            for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
230 231 232 233 234 235
            {
                C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
            }
        }
        else
        {
236
            for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
237 238 239 240 241 242 243 244 245
            {
    #define C_YVYU_YUV422( p_line, p_y, p_u, p_v )      \
                *p_y++ = *p_line++; *p_v++ = *p_line++; \
                *p_y++ = *p_line++; *p_u++ = *p_line++
                C_YVYU_YUV422( p_line, p_y, p_u, p_v );
                C_YVYU_YUV422( p_line, p_y, p_u, p_v );
                C_YVYU_YUV422( p_line, p_y, p_u, p_v );
                C_YVYU_YUV422( p_line, p_y, p_u, p_v );
            }
246
            for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
            {
                C_YVYU_YUV422( p_line, p_y, p_u, p_v );
            }
        }
        p_line += i_source_margin;
        p_y += i_dest_margin;
        p_u += i_dest_margin_c;
        p_v += i_dest_margin_c;

        b_skip = !b_skip;
    }
}

/*****************************************************************************
 * UYVY_I420: packed UYVY 4:2:2 to planar YUV 4:2:0
 *****************************************************************************/
263 264
static void UYVY_I420( filter_t *p_filter, picture_t *p_source,
                                           picture_t *p_dest )
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
{
    uint8_t *p_line = p_source->p->p_pixels;

    uint8_t *p_y = p_dest->Y_PIXELS;
    uint8_t *p_u = p_dest->U_PIXELS;
    uint8_t *p_v = p_dest->V_PIXELS;

    int i_x, i_y;

    const int i_dest_margin = p_dest->p[0].i_pitch
                                 - p_dest->p[0].i_visible_pitch;
    const int i_dest_margin_c = p_dest->p[1].i_pitch
                                 - p_dest->p[1].i_visible_pitch;
    const int i_source_margin = p_source->p->i_pitch
                               - p_source->p->i_visible_pitch;

281
    bool b_skip = false;
282

283
    for( i_y = p_filter->fmt_out.video.i_height ; i_y-- ; )
284 285 286
    {
        if( b_skip )
        {
287
            for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
288 289 290 291 292 293 294 295 296
            {
    #define C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v )      \
                *p_u++ = *p_line++; p_line++; \
                *p_v++ = *p_line++; p_line++
                C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
                C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
                C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
                C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
            }
297
            for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
298 299 300 301 302 303
            {
                C_UYVY_YUV422_skip( p_line, p_y, p_u, p_v );
            }
        }
        else
        {
304
            for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
305 306 307 308 309 310 311 312 313
            {
    #define C_UYVY_YUV422( p_line, p_y, p_u, p_v )      \
                *p_u++ = *p_line++; *p_y++ = *p_line++; \
                *p_v++ = *p_line++; *p_y++ = *p_line++
                C_UYVY_YUV422( p_line, p_y, p_u, p_v );
                C_UYVY_YUV422( p_line, p_y, p_u, p_v );
                C_UYVY_YUV422( p_line, p_y, p_u, p_v );
                C_UYVY_YUV422( p_line, p_y, p_u, p_v );
            }
314
            for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
            {
                C_UYVY_YUV422( p_line, p_y, p_u, p_v );
            }
        }
        p_line += i_source_margin;
        p_y += i_dest_margin;
        p_u += i_dest_margin_c;
        p_v += i_dest_margin_c;

        b_skip = !b_skip;
    }
}

/*****************************************************************************
 * cyuv_I420: upside-down packed UYVY 4:2:2 to planar YUV 4:2:0
 * FIXME
 *****************************************************************************/
332 333
static void cyuv_I420( filter_t *p_filter, picture_t *p_source,
                                           picture_t *p_dest )
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
{
    uint8_t *p_line = p_source->p->p_pixels;

    uint8_t *p_y = p_dest->Y_PIXELS;
    uint8_t *p_u = p_dest->U_PIXELS;
    uint8_t *p_v = p_dest->V_PIXELS;

    int i_x, i_y;

    const int i_dest_margin = p_dest->p[0].i_pitch
                                 - p_dest->p[0].i_visible_pitch;
    const int i_dest_margin_c = p_dest->p[1].i_pitch
                                 - p_dest->p[1].i_visible_pitch;
    const int i_source_margin = p_source->p->i_pitch
                               - p_source->p->i_visible_pitch;

350
    bool b_skip = false;
351

352
    for( i_y = p_filter->fmt_out.video.i_height ; i_y-- ; )
353 354 355
    {
        if( b_skip )
        {
356
            for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
357 358 359 360 361 362 363 364 365
            {
    #define C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v )      \
                *p_y++ = *p_line++; p_line++; \
                *p_y++ = *p_line++; p_line++
                C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
                C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
                C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
                C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
            }
366
            for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
367 368 369 370 371 372
            {
                C_cyuv_YUV422_skip( p_line, p_y, p_u, p_v );
            }
        }
        else
        {
373
            for( i_x = p_filter->fmt_out.video.i_width / 8 ; i_x-- ; )
374 375 376 377 378 379 380 381 382
            {
    #define C_cyuv_YUV422( p_line, p_y, p_u, p_v )      \
                *p_y++ = *p_line++; *p_v++ = *p_line++; \
                *p_y++ = *p_line++; *p_u++ = *p_line++
                C_cyuv_YUV422( p_line, p_y, p_u, p_v );
                C_cyuv_YUV422( p_line, p_y, p_u, p_v );
                C_cyuv_YUV422( p_line, p_y, p_u, p_v );
                C_cyuv_YUV422( p_line, p_y, p_u, p_v );
            }
383
            for( i_x = ( p_filter->fmt_out.video.i_width % 8 ) / 2; i_x-- ; )
384 385 386 387 388 389 390 391 392 393 394 395
            {
                C_cyuv_YUV422( p_line, p_y, p_u, p_v );
            }
        }
        p_line += i_source_margin;
        p_y += i_dest_margin;
        p_u += i_dest_margin_c;
        p_v += i_dest_margin_c;

        b_skip = !b_skip;
    }
}