yuy2_i420.c 13 KB
Newer Older
1 2 3
/*****************************************************************************
 * yuy2_i420.c : Packed YUV 4:2:2 to Planar YUV conversion module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2007 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Antoine Cellerier <dionoea at videolan dot 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
#include <vlc_common.h>
33
#include <vlc_plugin.h>
34
#include <vlc_filter.h>
35
#include <vlc_picture.h>
36

37
#define SRC_FOURCC "YUY2,YUNV,YVYU,UYVY,UYNV,Y422"
38 39 40 41 42 43 44
#define DEST_FOURCC  "I420"

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

45 46 47
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 * );
48

49 50 51 52
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 * );

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

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

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

77 78
    if( p_filter->fmt_in.video.i_width != (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width)
     || p_filter->fmt_in.video.i_height != (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height)
79
     || p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation)
80 81
        return -1;

82
    switch( p_filter->fmt_out.video.i_chroma )
83
    {
84
        case VLC_CODEC_I420:
85
            switch( p_filter->fmt_in.video.i_chroma )
86
            {
87
                case VLC_CODEC_YUYV:
88
                    p_filter->pf_video_filter = YUY2_I420_Filter;
89 90
                    break;

91
                case VLC_CODEC_YVYU:
92
                    p_filter->pf_video_filter = YVYU_I420_Filter;
93 94
                    break;

95
                case VLC_CODEC_UYVY:
96
                    p_filter->pf_video_filter = UYVY_I420_Filter;
97 98 99 100 101 102 103 104 105 106 107 108 109 110
                    break;

                default:
                    return -1;
            }
            break;

        default:
            return -1;
    }
    return 0;
}

/* Following functions are local */
111 112 113
VIDEO_FILTER_WRAPPER( YUY2_I420 )
VIDEO_FILTER_WRAPPER( YVYU_I420 )
VIDEO_FILTER_WRAPPER( UYVY_I420 )
114 115 116 117

/*****************************************************************************
 * YUY2_I420: packed YUY2 4:2:2 to planar YUV 4:2:0
 *****************************************************************************/
118 119
static void YUY2_I420( filter_t *p_filter, picture_t *p_source,
                                           picture_t *p_dest )
120 121 122 123 124 125 126 127 128 129
{
    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
130 131
                                 - p_dest->p[0].i_visible_pitch
                                 - p_filter->fmt_out.video.i_x_offset;
132
    const int i_dest_margin_c = p_dest->p[1].i_pitch
133 134
                                 - p_dest->p[1].i_visible_pitch
                                 - ( p_filter->fmt_out.video.i_x_offset / 2 );
135
    const int i_source_margin = p_source->p->i_pitch
136 137
                               - p_source->p->i_visible_pitch
                               - ( p_filter->fmt_in.video.i_x_offset * 2 );
138

139
    bool b_skip = false;
140

141
    for( i_y = (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height) ; i_y-- ; )
142 143 144
    {
        if( b_skip )
        {
145
            for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
146 147 148 149 150 151 152 153 154
            {
    #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 );
            }
155
            for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
156 157 158 159 160 161
            {
                C_YUYV_YUV422_skip( p_line, p_y, p_u, p_v );
            }
        }
        else
        {
162
            for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
163 164 165 166 167 168 169 170 171
            {
    #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 );
            }
172
            for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
173 174 175
            {
                C_YUYV_YUV422( p_line, p_y, p_u, p_v );
            }
176 177
            p_u += i_dest_margin_c;
            p_v += i_dest_margin_c;
178 179 180 181 182 183 184 185 186 187 188
        }
        p_line += i_source_margin;
        p_y += i_dest_margin;

        b_skip = !b_skip;
    }
}

/*****************************************************************************
 * YVYU_I420: packed YVYU 4:2:2 to planar YUV 4:2:0
 *****************************************************************************/
189 190
static void YVYU_I420( filter_t *p_filter, picture_t *p_source,
                                           picture_t *p_dest )
191 192 193 194 195 196 197 198 199 200
{
    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
201 202
                                 - p_dest->p[0].i_visible_pitch
                                 - p_filter->fmt_out.video.i_x_offset;
203
    const int i_dest_margin_c = p_dest->p[1].i_pitch
204 205
                                 - p_dest->p[1].i_visible_pitch
                                 - ( p_filter->fmt_out.video.i_x_offset / 2 );
206
    const int i_source_margin = p_source->p->i_pitch
207 208
                               - p_source->p->i_visible_pitch
                               - ( p_filter->fmt_in.video.i_x_offset * 2 );
209

210
    bool b_skip = false;
211

212
    for( i_y = (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height) ; i_y-- ; )
213 214 215
    {
        if( b_skip )
        {
216
            for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
217 218 219 220 221 222 223 224 225
            {
    #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 );
            }
226
            for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
227 228 229 230 231 232
            {
                C_YVYU_YUV422_skip( p_line, p_y, p_u, p_v );
            }
        }
        else
        {
233
            for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) / 8 ; i_x-- ; )
234 235 236 237 238 239 240 241 242
            {
    #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 );
            }
243
            for( i_x = ( (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
244 245 246
            {
                C_YVYU_YUV422( p_line, p_y, p_u, p_v );
            }
247 248
            p_u += i_dest_margin_c;
            p_v += i_dest_margin_c;
249 250 251 252 253 254 255 256 257 258 259
        }
        p_line += i_source_margin;
        p_y += i_dest_margin;

        b_skip = !b_skip;
    }
}

/*****************************************************************************
 * UYVY_I420: packed UYVY 4:2:2 to planar YUV 4:2:0
 *****************************************************************************/
260 261
static void UYVY_I420( filter_t *p_filter, picture_t *p_source,
                                           picture_t *p_dest )
262 263 264 265 266 267 268 269 270 271
{
    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
272 273
                                 - p_dest->p[0].i_visible_pitch
                                 - p_filter->fmt_out.video.i_x_offset;
274
    const int i_dest_margin_c = p_dest->p[1].i_pitch
275 276
                                 - p_dest->p[1].i_visible_pitch
                                 - ( p_filter->fmt_out.video.i_x_offset / 2 );
277
    const int i_source_margin = p_source->p->i_pitch
278 279
                               - p_source->p->i_visible_pitch
                               - ( p_filter->fmt_in.video.i_x_offset * 2 );
280

281
    bool b_skip = false;
282

283
    for( i_y = (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height) ; i_y-- ; )
284 285 286
    {
        if( b_skip )
        {
287
            for( i_x = (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_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_x_offset + p_filter->fmt_out.video.i_visible_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_x_offset + p_filter->fmt_out.video.i_visible_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_x_offset + p_filter->fmt_out.video.i_visible_width) % 8 ) / 2; i_x-- ; )
315 316 317
            {
                C_UYVY_YUV422( p_line, p_y, p_u, p_v );
            }
318
            p_y += i_dest_margin;
319 320 321 322 323 324 325 326
        }
        p_line += i_source_margin;
        p_u += i_dest_margin_c;
        p_v += i_dest_margin_c;

        b_skip = !b_skip;
    }
}