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 /*****************************************************************************
25 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_filter.h>
33 #include <vlc_picture.h>
37 * Add anti-aliasing support (specially for DVD where only 4 colors are used)
40 /*****************************************************************************
42 *****************************************************************************/
43 static int Open ( filter_t
* );
46 set_description( N_("YUVP converter") )
47 set_callback_video_converter( Open
, 10 )
50 /****************************************************************************
52 ****************************************************************************/
53 static void Yuv2Rgb( uint8_t *r
, uint8_t *g
, uint8_t *b
, int y1
, int u1
, int v1
);
55 VIDEO_FILTER_WRAPPER( Convert
)
57 /*****************************************************************************
58 * Open: probe the filter and return score
59 *****************************************************************************/
60 static int Open( filter_t
*p_filter
)
62 /* It only supports YUVP to YUVA/RGBA without scaling
63 * (if scaling is required another filter can do it) */
64 if( p_filter
->fmt_in
.video
.i_chroma
!= VLC_CODEC_YUVP
||
65 ( p_filter
->fmt_out
.video
.i_chroma
!= VLC_CODEC_YUVA
&&
66 p_filter
->fmt_out
.video
.i_chroma
!= VLC_CODEC_RGBA
&&
67 p_filter
->fmt_out
.video
.i_chroma
!= VLC_CODEC_ARGB
&&
68 p_filter
->fmt_out
.video
.i_chroma
!= VLC_CODEC_BGRA
) ||
69 p_filter
->fmt_in
.video
.i_width
!= p_filter
->fmt_out
.video
.i_width
||
70 p_filter
->fmt_in
.video
.i_height
!= p_filter
->fmt_out
.video
.i_height
||
71 p_filter
->fmt_in
.video
.orientation
!= p_filter
->fmt_out
.video
.orientation
)
76 p_filter
->ops
= &Convert_ops
;
78 msg_Dbg( p_filter
, "YUVP to %4.4s converter",
79 (const char*)&p_filter
->fmt_out
.video
.i_chroma
);
84 /****************************************************************************
85 * Filter: the whole thing
86 ****************************************************************************/
88 static void Convert( filter_t
*p_filter
, picture_t
*p_source
,
91 const video_palette_t
*p_yuvp
= p_filter
->fmt_in
.video
.p_palette
;
93 assert( p_yuvp
!= NULL
);
94 assert( p_filter
->fmt_in
.video
.i_chroma
== VLC_CODEC_YUVP
);
95 assert( p_filter
->fmt_in
.video
.i_width
== p_filter
->fmt_out
.video
.i_width
);
96 assert( p_filter
->fmt_in
.video
.i_height
== p_filter
->fmt_out
.video
.i_height
);
98 if( p_filter
->fmt_out
.video
.i_chroma
== VLC_CODEC_YUVA
)
100 for( unsigned int y
= 0; y
< p_filter
->fmt_in
.video
.i_height
; y
++ )
102 const uint8_t *p_line
= &p_source
->p
->p_pixels
[y
*p_source
->p
->i_pitch
];
103 uint8_t *p_y
= &p_dest
->Y_PIXELS
[y
*p_dest
->Y_PITCH
];
104 uint8_t *p_u
= &p_dest
->U_PIXELS
[y
*p_dest
->U_PITCH
];
105 uint8_t *p_v
= &p_dest
->V_PIXELS
[y
*p_dest
->V_PITCH
];
106 uint8_t *p_a
= &p_dest
->A_PIXELS
[y
*p_dest
->A_PITCH
];
108 for( unsigned int x
= 0; x
< p_filter
->fmt_in
.video
.i_width
; x
++ )
110 const int v
= p_line
[x
];
112 if( v
> p_yuvp
->i_entries
) /* maybe assert ? */
115 p_y
[x
] = p_yuvp
->palette
[v
][0];
116 p_u
[x
] = p_yuvp
->palette
[v
][1];
117 p_v
[x
] = p_yuvp
->palette
[v
][2];
118 p_a
[x
] = p_yuvp
->palette
[v
][3];
124 video_palette_t rgbp
;
127 switch( p_filter
->fmt_out
.video
.i_chroma
)
129 case VLC_CODEC_ARGB
: r
= 1, g
= 2, b
= 3, a
= 0; break;
130 case VLC_CODEC_RGBA
: r
= 0, g
= 1, b
= 2, a
= 3; break;
131 case VLC_CODEC_BGRA
: r
= 2, g
= 1, b
= 0, a
= 3; break;
133 vlc_assert_unreachable();
135 /* Create a RGBA palette */
136 rgbp
.i_entries
= p_yuvp
->i_entries
;
137 for( int i
= 0; i
< p_yuvp
->i_entries
; i
++ )
139 if( p_yuvp
->palette
[i
][3] == 0 )
141 memset( rgbp
.palette
[i
], 0, sizeof( rgbp
.palette
[i
] ) );
144 Yuv2Rgb( &rgbp
.palette
[i
][r
], &rgbp
.palette
[i
][g
], &rgbp
.palette
[i
][b
],
145 p_yuvp
->palette
[i
][0], p_yuvp
->palette
[i
][1], p_yuvp
->palette
[i
][2] );
146 rgbp
.palette
[i
][a
] = p_yuvp
->palette
[i
][3];
149 for( unsigned int y
= 0; y
< p_filter
->fmt_in
.video
.i_height
; y
++ )
151 const uint8_t *p_line
= &p_source
->p
->p_pixels
[y
*p_source
->p
->i_pitch
];
152 uint8_t *p_pixels
= &p_dest
->p
->p_pixels
[y
*p_dest
->p
->i_pitch
];
154 for( unsigned int x
= 0; x
< p_filter
->fmt_in
.video
.i_width
; x
++ )
156 const int v
= p_line
[x
];
158 if( v
>= rgbp
.i_entries
) /* maybe assert ? */
161 p_pixels
[4*x
+0] = rgbp
.palette
[v
][0];
162 p_pixels
[4*x
+1] = rgbp
.palette
[v
][1];
163 p_pixels
[4*x
+2] = rgbp
.palette
[v
][2];
164 p_pixels
[4*x
+3] = rgbp
.palette
[v
][3];
171 /* FIXME copied from blend.c */
172 static inline uint8_t vlc_uint8( int v
)
180 static void Yuv2Rgb( uint8_t *r
, uint8_t *g
, uint8_t *b
, int y1
, int u1
, int v1
)
182 /* macros used for YUV pixel conversions */
183 # define SCALEBITS 10
184 # define ONE_HALF (1 << (SCALEBITS - 1))
185 # define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
187 int y
, cb
, cr
, r_add
, g_add
, b_add
;
191 r_add
= FIX(1.40200*255.0/224.0) * cr
+ ONE_HALF
;
192 g_add
= - FIX(0.34414*255.0/224.0) * cb
193 - FIX(0.71414*255.0/224.0) * cr
+ ONE_HALF
;
194 b_add
= FIX(1.77200*255.0/224.0) * cb
+ ONE_HALF
;
195 y
= (y1
- 16) * FIX(255.0/219.0);
196 *r
= vlc_uint8( (y
+ r_add
) >> SCALEBITS
);
197 *g
= vlc_uint8( (y
+ g_add
) >> SCALEBITS
);
198 *b
= vlc_uint8( (y
+ b_add
) >> SCALEBITS
);