contrib: cargo: use cargo/vendored-openssl if needed
[vlc.git] / modules / video_chroma / yuvp.c
blobb2eba4fda7bb5be94150604225c07d575fe2829c
1 /*****************************************************************************
2 * yuvp.c: YUVP to YUVA/RGBA chroma converter
3 *****************************************************************************
4 * Copyright (C) 2008 VLC authors and VideoLAN
6 * Authors: Laurent Aimar < fenrir @ videolan.org >
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_filter.h>
33 #include <vlc_picture.h>
34 #include <assert.h>
36 /* TODO:
37 * Add anti-aliasing support (specially for DVD where only 4 colors are used)
40 /*****************************************************************************
41 * Module descriptor
42 *****************************************************************************/
43 static int Open ( vlc_object_t * );
44 static void Close( vlc_object_t * );
46 vlc_module_begin ()
47 set_description( N_("YUVP converter") )
48 set_capability( "video converter", 10 )
49 set_callbacks( Open, Close )
50 vlc_module_end ()
52 /****************************************************************************
53 * Local prototypes
54 ****************************************************************************/
55 static picture_t *Convert_Filter( filter_t *, picture_t * );
56 static void Convert( filter_t *, picture_t *, picture_t * );
57 static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 );
59 /*****************************************************************************
60 * Open: probe the filter and return score
61 *****************************************************************************/
62 static int Open( vlc_object_t *p_this )
64 filter_t *p_filter = (filter_t*)p_this;
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_out.video.i_chroma != VLC_CODEC_ARGB &&
72 p_filter->fmt_out.video.i_chroma != VLC_CODEC_BGRA ) ||
73 p_filter->fmt_in.video.i_width != p_filter->fmt_out.video.i_width ||
74 p_filter->fmt_in.video.i_height != p_filter->fmt_out.video.i_height ||
75 p_filter->fmt_in.video.orientation != p_filter->fmt_out.video.orientation )
77 return VLC_EGENERIC;
80 p_filter->pf_video_filter = Convert_Filter;
82 msg_Dbg( p_filter, "YUVP to %4.4s converter",
83 (const char*)&p_filter->fmt_out.video.i_chroma );
85 return VLC_SUCCESS;
88 /*****************************************************************************
89 * Close: clean up the filter
90 *****************************************************************************/
91 static void Close( vlc_object_t *p_this )
93 VLC_UNUSED(p_this );
96 /****************************************************************************
97 * Filter: the whole thing
98 ****************************************************************************/
99 VIDEO_FILTER_WRAPPER( Convert )
101 static void Convert( filter_t *p_filter, picture_t *p_source,
102 picture_t *p_dest )
104 const video_palette_t *p_yuvp = p_filter->fmt_in.video.p_palette;
106 assert( p_yuvp != NULL );
107 assert( p_filter->fmt_in.video.i_chroma == VLC_CODEC_YUVP );
108 assert( p_filter->fmt_in.video.i_width == p_filter->fmt_out.video.i_width );
109 assert( p_filter->fmt_in.video.i_height == p_filter->fmt_out.video.i_height );
111 if( p_filter->fmt_out.video.i_chroma == VLC_CODEC_YUVA )
113 for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
115 const uint8_t *p_line = &p_source->p->p_pixels[y*p_source->p->i_pitch];
116 uint8_t *p_y = &p_dest->Y_PIXELS[y*p_dest->Y_PITCH];
117 uint8_t *p_u = &p_dest->U_PIXELS[y*p_dest->U_PITCH];
118 uint8_t *p_v = &p_dest->V_PIXELS[y*p_dest->V_PITCH];
119 uint8_t *p_a = &p_dest->A_PIXELS[y*p_dest->A_PITCH];
121 for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
123 const int v = p_line[x];
125 if( v > p_yuvp->i_entries ) /* maybe assert ? */
126 continue;
128 p_y[x] = p_yuvp->palette[v][0];
129 p_u[x] = p_yuvp->palette[v][1];
130 p_v[x] = p_yuvp->palette[v][2];
131 p_a[x] = p_yuvp->palette[v][3];
135 else
137 video_palette_t rgbp;
138 int r, g, b, a;
140 switch( p_filter->fmt_out.video.i_chroma )
142 case VLC_CODEC_ARGB: r = 1, g = 2, b = 3, a = 0; break;
143 case VLC_CODEC_RGBA: r = 0, g = 1, b = 2, a = 3; break;
144 case VLC_CODEC_BGRA: r = 2, g = 1, b = 0, a = 3; break;
145 default:
146 vlc_assert_unreachable();
148 /* Create a RGBA palette */
149 rgbp.i_entries = p_yuvp->i_entries;
150 for( int i = 0; i < p_yuvp->i_entries; i++ )
152 if( p_yuvp->palette[i][3] == 0 )
154 memset( rgbp.palette[i], 0, sizeof( rgbp.palette[i] ) );
155 continue;
157 Yuv2Rgb( &rgbp.palette[i][r], &rgbp.palette[i][g], &rgbp.palette[i][b],
158 p_yuvp->palette[i][0], p_yuvp->palette[i][1], p_yuvp->palette[i][2] );
159 rgbp.palette[i][a] = p_yuvp->palette[i][3];
162 for( unsigned int y = 0; y < p_filter->fmt_in.video.i_height; y++ )
164 const uint8_t *p_line = &p_source->p->p_pixels[y*p_source->p->i_pitch];
165 uint8_t *p_pixels = &p_dest->p->p_pixels[y*p_dest->p->i_pitch];
167 for( unsigned int x = 0; x < p_filter->fmt_in.video.i_width; x++ )
169 const int v = p_line[x];
171 if( v >= rgbp.i_entries ) /* maybe assert ? */
172 continue;
174 p_pixels[4*x+0] = rgbp.palette[v][0];
175 p_pixels[4*x+1] = rgbp.palette[v][1];
176 p_pixels[4*x+2] = rgbp.palette[v][2];
177 p_pixels[4*x+3] = rgbp.palette[v][3];
184 /* FIXME copied from blend.c */
185 static inline uint8_t vlc_uint8( int v )
187 if( v > 255 )
188 return 255;
189 else if( v < 0 )
190 return 0;
191 return v;
193 static void Yuv2Rgb( uint8_t *r, uint8_t *g, uint8_t *b, int y1, int u1, int v1 )
195 /* macros used for YUV pixel conversions */
196 # define SCALEBITS 10
197 # define ONE_HALF (1 << (SCALEBITS - 1))
198 # define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
200 int y, cb, cr, r_add, g_add, b_add;
202 cb = u1 - 128;
203 cr = v1 - 128;
204 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;
205 g_add = - FIX(0.34414*255.0/224.0) * cb
206 - FIX(0.71414*255.0/224.0) * cr + ONE_HALF;
207 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;
208 y = (y1 - 16) * FIX(255.0/219.0);
209 *r = vlc_uint8( (y + r_add) >> SCALEBITS );
210 *g = vlc_uint8( (y + g_add) >> SCALEBITS );
211 *b = vlc_uint8( (y + b_add) >> SCALEBITS );
212 #undef FIX