grey_yuv.c 6.85 KB
Newer Older
1 2 3
/*****************************************************************************
 * grey_yuv.c : grayscale to others conversion module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2007, 2008 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Sam Hocevar <sam@zoy.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 38 39 40 41 42 43 44

#define SRC_FOURCC  "GREY"
#define DEST_FOURCC "I420,YUY2"

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

45 46
static void GREY_I420( filter_t *, picture_t *, picture_t * );
static void GREY_YUY2( filter_t *, picture_t *, picture_t * );
47

48 49 50
static picture_t *GREY_I420_Filter( filter_t *, picture_t * );
static picture_t *GREY_YUY2_Filter( filter_t *, picture_t * );

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

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

69 70
    if( p_filter->fmt_out.video.i_width & 1
     || p_filter->fmt_out.video.i_height & 1 )
71 72 73 74
    {
        return -1;
    }

75
    if( p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width
76 77
       || p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height
       || p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
78 79
        return -1;

80
    switch( p_filter->fmt_in.video.i_chroma )
81
    {
82
        case VLC_CODEC_GREY:
83
            switch( p_filter->fmt_out.video.i_chroma )
84
            {
85
                case VLC_CODEC_I420:
86
                    p_filter->pf_video_filter = GREY_I420_Filter;
87
                    break;
88
                case VLC_CODEC_YUYV:
89
                    p_filter->pf_video_filter = GREY_YUY2_Filter;
90 91 92 93 94 95 96 97 98 99 100 101 102
                    break;
                default:
                    return -1;
            }
            break;

        default:
            return -1;
    }

    return 0;
}

103 104 105
VIDEO_FILTER_WRAPPER( GREY_I420 )
VIDEO_FILTER_WRAPPER( GREY_YUY2 )

106 107 108 109 110
/* Following functions are local */

/*****************************************************************************
 * GREY_I420: 8-bit grayscale to planar YUV 4:2:0
 *****************************************************************************/
111 112
static void GREY_I420( filter_t *p_filter, picture_t *p_source,
                                           picture_t *p_dest )
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
{
    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_source_margin = p_source->p->i_pitch
                                 - p_source->p->i_visible_pitch;
    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;

128
    for( i_y = p_filter->fmt_in.video.i_height / 2; i_y-- ; )
129 130 131 132 133 134 135 136
    {
        memset(p_u, 0x80, p_dest->p[1].i_visible_pitch);
        p_u += i_dest_margin_c;

        memset(p_v, 0x80, p_dest->p[1].i_visible_pitch);
        p_v += i_dest_margin_c;
    }

137
    for( i_y = p_filter->fmt_in.video.i_height; i_y-- ; )
138
    {
139
        for( i_x = p_filter->fmt_in.video.i_width / 8; i_x-- ; )
140 141 142 143 144 145 146
        {
            *p_y++ = *p_line++; *p_y++ = *p_line++;
            *p_y++ = *p_line++; *p_y++ = *p_line++;
            *p_y++ = *p_line++; *p_y++ = *p_line++;
            *p_y++ = *p_line++; *p_y++ = *p_line++;
        }

147
        for( i_x = p_filter->fmt_in.video.i_width % 8; i_x-- ; )
148 149 150 151 152 153 154 155 156 157 158 159
        {
            *p_y++ = *p_line++;
        }

        p_line += i_source_margin;
        p_y += i_dest_margin;
    }
}

/*****************************************************************************
 * GREY_YUY2: 8-bit grayscale to packed YUY2
 *****************************************************************************/
160 161
static void GREY_YUY2( filter_t *p_filter, picture_t *p_source,
                                           picture_t *p_dest )
162 163 164 165 166 167 168 169 170 171 172
{
    uint8_t *p_in = p_source->p->p_pixels;
    uint8_t *p_out = p_dest->p->p_pixels;

    int i_x, i_y;

    const int i_source_margin = p_source->p->i_pitch
                                 - p_source->p->i_visible_pitch;
    const int i_dest_margin = p_dest->p->i_pitch
                               - p_dest->p->i_visible_pitch;

173
    for( i_y = p_filter->fmt_out.video.i_height; i_y-- ; )
174
    {
175
        for( i_x = p_filter->fmt_out.video.i_width / 8; i_x-- ; )
176 177 178 179 180 181 182 183 184 185 186
        {
            *p_out++ = *p_in++; *p_out++ = 0x80;
            *p_out++ = *p_in++; *p_out++ = 0x80;
            *p_out++ = *p_in++; *p_out++ = 0x80;
            *p_out++ = *p_in++; *p_out++ = 0x80;
            *p_out++ = *p_in++; *p_out++ = 0x80;
            *p_out++ = *p_in++; *p_out++ = 0x80;
            *p_out++ = *p_in++; *p_out++ = 0x80;
            *p_out++ = *p_in++; *p_out++ = 0x80;
        }

187
        for( i_x = (p_filter->fmt_out.video.i_width % 8) / 2; i_x-- ; )
188 189 190 191 192 193 194 195 196 197
        {
            *p_out++ = *p_in++; *p_out++ = 0x80;
            *p_out++ = *p_in++; *p_out++ = 0x80;
        }

        p_in += i_source_margin;
        p_out += i_dest_margin;
    }
}