]> git.sesse.net Git - vlc/blob - modules/video_filter/yuvp.c
Used VLC_CODEC_* and vlc_fourcc_GetCodec when suitable.
[vlc] / modules / video_filter / yuvp.c
1 /*****************************************************************************
2  * yuvp.c: YUVP to YUVA/RGBA chroma converter
3  *****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar < fenrir @ videolan.org >
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_vout.h>
34 #include "vlc_filter.h"
35 #include <assert.h>
36
37 /* TODO:
38  *  Add anti-aliasing support (specially for DVD where only 4 colors are used)
39  */
40
41 /*****************************************************************************
42  * Module descriptor
43  *****************************************************************************/
44 static int  Open ( vlc_object_t * );
45 static void Close( vlc_object_t * );
46
47 vlc_module_begin ()
48     set_description( N_("YUVP converter") )
49     set_capability( "video filter2", 10 )
50     set_callbacks( Open, Close )
51 vlc_module_end ()
52
53 /****************************************************************************
54  * Local prototypes
55  ****************************************************************************/
56 static picture_t *Filter( filter_t *, picture_t * );
57 static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 );
58
59 /*****************************************************************************
60  * Open: probe the filter and return score
61  *****************************************************************************/
62 static int Open( vlc_object_t *p_this )
63 {
64     filter_t *p_filter = (filter_t*)p_this;
65
66     /* It only supports YUVP to YUVA/RGBA without scaling
67      * (if scaling is required another filter can do it) */
68     if( p_filter->fmt_in.video.i_chroma != VLC_CODEC_YUVP ||
69         ( p_filter->fmt_out.video.i_chroma != VLC_CODEC_YUVA &&
70           p_filter->fmt_out.video.i_chroma != VLC_CODEC_RGBA ) ||
71         p_filter->fmt_in.video.i_width  != p_filter->fmt_out.video.i_width ||
72         p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height )
73     {
74         return VLC_EGENERIC;
75     }
76
77     p_filter->pf_video_filter = Filter;
78
79     msg_Dbg( p_filter, "YUVP to %4.4s converter",
80              (const char*)&p_filter->fmt_out.video.i_chroma );
81
82     return VLC_SUCCESS;
83 }
84
85 /*****************************************************************************
86  * Close: clean up the filter
87  *****************************************************************************/
88 static void Close( vlc_object_t *p_this )
89 {
90     VLC_UNUSED(p_this );
91 }
92
93 /****************************************************************************
94  * Filter: the whole thing
95  ****************************************************************************/
96 static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
97 {
98     picture_t *p_out;
99
100     if( !p_pic )
101         return NULL;
102
103     const video_palette_t *p_yuvp = p_filter->fmt_in.video.p_palette;
104
105     assert( p_yuvp != NULL );
106     assert( p_filter->fmt_in.video.i_chroma == VLC_CODEC_YUVP );
107     assert( p_filter->fmt_in.video.i_width == p_filter->fmt_out.video.i_width );
108     assert( p_filter->fmt_in.video.i_height == p_filter->fmt_out.video.i_height );
109
110     /* Request output picture */
111     p_out = filter_NewPicture( p_filter );
112     if( !p_out )
113     {
114         picture_Release( p_pic );
115         return NULL;
116     }
117
118     if( p_filter->fmt_out.video.i_chroma == VLC_CODEC_YUVA )
119     {
120         for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
121         {
122             const uint8_t *p_line = &p_pic->p->p_pixels[y*p_pic->p->i_pitch];
123             uint8_t *p_y = &p_out->Y_PIXELS[y*p_out->Y_PITCH];
124             uint8_t *p_u = &p_out->U_PIXELS[y*p_out->U_PITCH];
125             uint8_t *p_v = &p_out->V_PIXELS[y*p_out->V_PITCH];
126             uint8_t *p_a = &p_out->A_PIXELS[y*p_out->A_PITCH];
127
128             for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
129             {
130                 const int v = p_line[x];
131
132                 if( v > p_yuvp->i_entries )  /* maybe assert ? */
133                     continue;
134
135                 p_y[x] = p_yuvp->palette[v][0];
136                 p_u[x] = p_yuvp->palette[v][1];
137                 p_v[x] = p_yuvp->palette[v][2];
138                 p_a[x] = p_yuvp->palette[v][3];
139             }
140         }
141     }
142     else
143     {
144         assert( p_filter->fmt_out.video.i_chroma == VLC_CODEC_RGBA );
145
146         /* Create a RGBA palette */
147         video_palette_t rgbp;
148         rgbp.i_entries = p_yuvp->i_entries;
149         for( int i = 0; i < p_yuvp->i_entries; i++ )
150         {
151             Yuv2Rgb( &rgbp.palette[i][0], &rgbp.palette[i][1], &rgbp.palette[i][2],
152                      p_yuvp->palette[i][0], p_yuvp->palette[i][1], p_yuvp->palette[i][2] );
153             rgbp.palette[i][3] = p_yuvp->palette[i][3];
154         }
155
156         /* */
157         for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
158         {
159             const uint8_t *p_line = &p_pic->p->p_pixels[y*p_pic->p->i_pitch];
160             uint8_t *p_rgba = &p_out->p->p_pixels[y*p_out->p->i_pitch];
161
162             for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
163             {
164                 const int v = p_line[x];
165
166                 if( v > rgbp.i_entries )  /* maybe assert ? */
167                     continue;
168
169                 p_rgba[4*x+0] = rgbp.palette[v][0];
170                 p_rgba[4*x+1] = rgbp.palette[v][1];
171                 p_rgba[4*x+2] = rgbp.palette[v][2];
172                 p_rgba[4*x+3] = rgbp.palette[v][3];
173             }
174         }
175     }
176
177     picture_CopyProperties( p_out, p_pic );
178     picture_Release( p_pic );
179     return p_out;
180 }
181
182 /* FIXME copied from blend.c */
183 static inline uint8_t vlc_uint8( int v )
184 {
185     if( v > 255 )
186         return 255;
187     else if( v < 0 )
188         return 0;
189     return v;
190 }
191 static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 )
192 {
193     /* macros used for YUV pixel conversions */
194 #   define SCALEBITS 10
195 #   define ONE_HALF  (1 << (SCALEBITS - 1))
196 #   define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
197
198     int y, cb, cr, r_add, g_add, b_add;
199
200     cb = u1 - 128;
201     cr = v1 - 128;
202     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
203     g_add = - FIX(0.34414*255.0/224.0) * cb
204             - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
205     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
206     y = (y1 - 16) * FIX(255.0/219.0);
207     *r = vlc_uint8( (y + r_add) >> SCALEBITS );
208     *g = vlc_uint8( (y + g_add) >> SCALEBITS );
209     *b = vlc_uint8( (y + b_add) >> SCALEBITS );
210 #undef FIX
211 }