yuvp.c 8.04 KB
Newer Older
1 2 3
/*****************************************************************************
 * yuvp.c: YUVP to YUVA/RGBA chroma converter
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2008 VLC authors and VideoLAN
5 6 7 8
 * $Id$
 *
 * Authors: Laurent Aimar < fenrir @ videolan.org >
 *
Jean-Baptiste Kempf's avatar
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
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
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 28 29 30 31 32
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

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

/* TODO:
 *  Add anti-aliasing support (specially for DVD where only 4 colors are used)
 */

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

47 48
vlc_module_begin ()
    set_description( N_("YUVP converter") )
49
    set_capability( "video converter", 10 )
50 51
    set_callbacks( Open, Close )
vlc_module_end ()
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

/****************************************************************************
 * Local prototypes
 ****************************************************************************/
static picture_t *Filter( filter_t *, picture_t * );
static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 );

/*****************************************************************************
 * Open: probe the filter and return score
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    filter_t *p_filter = (filter_t*)p_this;

    /* It only supports YUVP to YUVA/RGBA without scaling
     * (if scaling is required another filter can do it) */
68 69
    if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVP ||
        ( p_filter->fmt_out.video.i_chroma != VLC_CODEC_YUVA &&
70 71
          p_filter->fmt_out.video.i_chroma != VLC_CODEC_RGBA &&
          p_filter->fmt_out.video.i_chroma != VLC_CODEC_ARGB ) ||
72
        p_filter->fmt_in.video.i_width  != p_filter->fmt_out.video.i_width ||
73 74
        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 )
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    {
        return VLC_EGENERIC;
    }

    p_filter->pf_video_filter = Filter;

    msg_Dbg( p_filter, "YUVP to %4.4s converter",
             (const char*)&p_filter->fmt_out.video.i_chroma );

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: clean up the filter
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    VLC_UNUSED(p_this );
}

/****************************************************************************
 * Filter: the whole thing
 ****************************************************************************/
static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
{
    picture_t *p_out;

    if( !p_pic )
        return NULL;

    const video_palette_t *p_yuvp = p_filter->fmt_in.video.p_palette;

    assert( p_yuvp != NULL );
108
    assert( p_filter->fmt_in.video.i_chroma == VLC_CODEC_YUVP );
109 110 111 112 113 114 115 116 117 118 119
    assert( p_filter->fmt_in.video.i_width == p_filter->fmt_out.video.i_width );
    assert( p_filter->fmt_in.video.i_height == p_filter->fmt_out.video.i_height );

    /* Request output picture */
    p_out = filter_NewPicture( p_filter );
    if( !p_out )
    {
        picture_Release( p_pic );
        return NULL;
    }

120
    if( p_filter->fmt_out.video.i_chroma == VLC_CODEC_YUVA )
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    {
        for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
        {
            const uint8_t *p_line = &p_pic->p->p_pixels[y*p_pic->p->i_pitch];
            uint8_t *p_y = &p_out->Y_PIXELS[y*p_out->Y_PITCH];
            uint8_t *p_u = &p_out->U_PIXELS[y*p_out->U_PITCH];
            uint8_t *p_v = &p_out->V_PIXELS[y*p_out->V_PITCH];
            uint8_t *p_a = &p_out->A_PIXELS[y*p_out->A_PITCH];

            for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
            {
                const int v = p_line[x];

                if( v > p_yuvp->i_entries )  /* maybe assert ? */
                    continue;

                p_y[x] = p_yuvp->palette[v][0];
                p_u[x] = p_yuvp->palette[v][1];
                p_v[x] = p_yuvp->palette[v][2];
                p_a[x] = p_yuvp->palette[v][3];
            }
        }
    }
144 145 146 147
    else
    {
        video_palette_t rgbp;

148 149
        assert( p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ||
                p_filter->fmt_out.video.i_chroma == VLC_CODEC_RGBA );
150 151
        /* Create a RGBA palette */
        rgbp.i_entries = p_yuvp->i_entries;
152 153 154 155
        const uint8_t r = p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ? 1 : 0;
        const uint8_t g = p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ? 2 : 1;
        const uint8_t b = p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ? 3 : 2;
        const uint8_t a = p_filter->fmt_out.video.i_chroma == VLC_CODEC_ARGB ? 0 : 3;
156 157
        for( int i = 0; i < p_yuvp->i_entries; i++ )
        {
158 159 160 161 162
            if( p_yuvp->palette[i][3] == 0 )
            {
                memset( rgbp.palette[i], 0, sizeof( rgbp.palette[i] ) );
                continue;
            }
163
            Yuv2Rgb( &rgbp.palette[i][r], &rgbp.palette[i][g], &rgbp.palette[i][b],
164
                     p_yuvp->palette[i][0], p_yuvp->palette[i][1], p_yuvp->palette[i][2] );
165
            rgbp.palette[i][a] = p_yuvp->palette[i][3];
166 167 168 169 170
        }

        for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
        {
            const uint8_t *p_line = &p_pic->p->p_pixels[y*p_pic->p->i_pitch];
171
            uint8_t *p_pixels = &p_out->p->p_pixels[y*p_out->p->i_pitch];
172 173 174 175 176 177 178 179

            for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
            {
                const int v = p_line[x];

                if( v >= rgbp.i_entries )  /* maybe assert ? */
                    continue;

180 181 182 183
                p_pixels[4*x+0] = rgbp.palette[v][0];
                p_pixels[4*x+1] = rgbp.palette[v][1];
                p_pixels[4*x+2] = rgbp.palette[v][2];
                p_pixels[4*x+3] = rgbp.palette[v][3];
184 185 186 187
            }
        }

    }
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

    picture_CopyProperties( p_out, p_pic );
    picture_Release( p_pic );
    return p_out;
}

/* FIXME copied from blend.c */
static inline uint8_t vlc_uint8( int v )
{
    if( v > 255 )
        return 255;
    else if( v < 0 )
        return 0;
    return v;
}
static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 )
{
    /* macros used for YUV pixel conversions */
#   define SCALEBITS 10
#   define ONE_HALF  (1 << (SCALEBITS - 1))
#   define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))

    int y, cb, cr, r_add, g_add, b_add;

    cb = u1 - 128;
    cr = v1 - 128;
    r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
    g_add = - FIX(0.34414*255.0/224.0) * cb
            - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
    b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
    y = (y1 - 16) * FIX(255.0/219.0);
    *r = vlc_uint8( (y + r_add) >> SCALEBITS );
    *g = vlc_uint8( (y + g_add) >> SCALEBITS );
    *b = vlc_uint8( (y + b_add) >> SCALEBITS );
#undef FIX
}