1 /*****************************************************************************
2 * i420_rgb16.c : YUV to bitmap RGB conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VLC authors and VideoLAN
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Damien Fouilleul <damienf@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
29 #include <vlc_common.h>
30 #include <vlc_filter.h>
31 #include <vlc_picture.h>
35 #include "i420_rgb_c.h"
37 /*****************************************************************************
38 * SetOffset: build offset array for conversion functions
39 *****************************************************************************
40 * This function will build an offset array used in later conversion functions.
41 * It will also set horizontal and vertical scaling indicators.
42 *****************************************************************************/
43 static void SetOffset( int i_width
, int i_height
, int i_pic_width
,
44 int i_pic_height
, bool *pb_hscale
,
45 unsigned int *pi_vscale
, int *p_offset
)
48 * Prepare horizontal offset array
50 if( i_pic_width
- i_width
== 0 )
51 { /* No horizontal scaling: YUV conversion is done directly to picture */
54 else if( i_pic_width
- i_width
> 0 )
55 { /* Prepare scaling array for horizontal extension */
56 int i_scale_count
= i_pic_width
;
59 for( int i_x
= i_width
; i_x
--; )
61 while( (i_scale_count
-= i_width
) > 0 )
66 i_scale_count
+= i_pic_width
;
69 else /* if( i_pic_width - i_width < 0 ) */
70 { /* Prepare scaling array for horizontal reduction */
71 int i_scale_count
= i_pic_width
;
74 for( int i_x
= i_pic_width
; i_x
--; )
77 while( (i_scale_count
-= i_pic_width
) > 0 )
82 i_scale_count
+= i_width
;
87 * Set vertical scaling indicator
89 if( i_pic_height
- i_height
== 0 )
91 else if( i_pic_height
- i_height
> 0 )
93 else /* if( i_pic_height - i_height < 0 ) */
97 /*****************************************************************************
98 * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp
99 *****************************************************************************
100 * Horizontal alignment needed:
101 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
102 * - output: 1 pixel (2 bytes), margins allowed
103 * Vertical alignment needed:
104 * - input: 2 lines (2 Y lines, 1 U/V line)
106 *****************************************************************************/
108 void I420_RGB16( filter_t
*p_filter
, picture_t
*p_src
, picture_t
*p_dest
)
110 /* We got this one from the old arguments */
111 uint16_t *p_pic
= (uint16_t*)p_dest
->p
->p_pixels
;
112 uint8_t *p_y
= p_src
->Y_PIXELS
;
113 uint8_t *p_u
= p_src
->U_PIXELS
;
114 uint8_t *p_v
= p_src
->V_PIXELS
;
116 bool b_hscale
; /* horizontal scaling type */
117 unsigned int i_vscale
; /* vertical scaling type */
118 unsigned int i_x
, i_y
; /* horizontal and vertical indexes */
122 int i_scale_count
; /* scale modulo counter */
123 int i_chroma_width
= (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
) / 2; /* chroma width */
124 uint16_t * p_pic_start
; /* beginning of the current line for copy */
125 int i_uval
, i_vval
; /* U and V samples */
126 int i_red
, i_green
, i_blue
; /* U and V modified samples */
127 uint16_t * p_yuv
= p_filter
->p_sys
->p_rgb16
;
128 uint16_t * p_ybase
; /* Y dependant conversion table */
130 /* Conversion buffer pointer */
131 uint16_t * p_buffer_start
= (uint16_t*)p_filter
->p_sys
->p_buffer
;
134 /* Offset array pointer */
135 int * p_offset_start
= p_filter
->p_sys
->p_offset
;
138 const int i_source_margin
= p_src
->p
[0].i_pitch
139 - p_src
->p
[0].i_visible_pitch
140 - p_filter
->fmt_in
.video
.i_x_offset
;
141 const int i_source_margin_c
= p_src
->p
[1].i_pitch
142 - p_src
->p
[1].i_visible_pitch
143 - ( p_filter
->fmt_in
.video
.i_x_offset
/ 2 );
145 i_right_margin
= p_dest
->p
->i_pitch
- p_dest
->p
->i_visible_pitch
;
146 i_rewind
= (-(p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
)) & 7;
148 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
149 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
150 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
151 SetOffset( (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
),
152 (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
),
153 (p_filter
->fmt_out
.video
.i_x_offset
+ p_filter
->fmt_out
.video
.i_visible_width
),
154 (p_filter
->fmt_out
.video
.i_y_offset
+ p_filter
->fmt_out
.video
.i_visible_height
),
155 &b_hscale
, &i_vscale
, p_offset_start
);
160 i_scale_count
= ( i_vscale
== 1 ) ?
161 (p_filter
->fmt_out
.video
.i_y_offset
+ p_filter
->fmt_out
.video
.i_visible_height
) :
162 (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
);
163 for( i_y
= 0; i_y
< (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
); i_y
++ )
166 p_buffer
= b_hscale
? p_buffer_start
: p_pic
;
168 for ( i_x
= (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
) / 8; i_x
--; )
170 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
171 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
172 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
173 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
176 /* Here we do some unaligned reads and duplicate conversions, but
177 * at least we have all the pixels */
181 p_u
-= i_rewind
>> 1;
182 p_v
-= i_rewind
>> 1;
183 p_buffer
-= i_rewind
;
185 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
186 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
187 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
188 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
191 SCALE_HEIGHT( 420, 2 );
193 p_y
+= i_source_margin
;
196 p_u
+= i_source_margin_c
;
197 p_v
+= i_source_margin_c
;
202 /*****************************************************************************
203 * I420_RGB32: color YUV 4:2:0 to RGB 32 bpp
204 *****************************************************************************
205 * Horizontal alignment needed:
206 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
207 * - output: 1 pixel (2 bytes), margins allowed
208 * Vertical alignment needed:
209 * - input: 2 lines (2 Y lines, 1 U/V line)
211 *****************************************************************************/
213 void I420_RGB32( filter_t
*p_filter
, picture_t
*p_src
, picture_t
*p_dest
)
215 /* We got this one from the old arguments */
216 uint32_t *p_pic
= (uint32_t*)p_dest
->p
->p_pixels
;
217 uint8_t *p_y
= p_src
->Y_PIXELS
;
218 uint8_t *p_u
= p_src
->U_PIXELS
;
219 uint8_t *p_v
= p_src
->V_PIXELS
;
221 bool b_hscale
; /* horizontal scaling type */
222 unsigned int i_vscale
; /* vertical scaling type */
223 unsigned int i_x
, i_y
; /* horizontal and vertical indexes */
227 int i_scale_count
; /* scale modulo counter */
228 int i_chroma_width
= (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
) / 2; /* chroma width */
229 uint32_t * p_pic_start
; /* beginning of the current line for copy */
230 int i_uval
, i_vval
; /* U and V samples */
231 int i_red
, i_green
, i_blue
; /* U and V modified samples */
232 uint32_t * p_yuv
= p_filter
->p_sys
->p_rgb32
;
233 uint32_t * p_ybase
; /* Y dependant conversion table */
235 /* Conversion buffer pointer */
236 uint32_t * p_buffer_start
= (uint32_t*)p_filter
->p_sys
->p_buffer
;
239 /* Offset array pointer */
240 int * p_offset_start
= p_filter
->p_sys
->p_offset
;
243 const int i_source_margin
= p_src
->p
[0].i_pitch
244 - p_src
->p
[0].i_visible_pitch
245 - p_filter
->fmt_in
.video
.i_x_offset
;
246 const int i_source_margin_c
= p_src
->p
[1].i_pitch
247 - p_src
->p
[1].i_visible_pitch
248 - ( p_filter
->fmt_in
.video
.i_x_offset
/ 2 );
250 i_right_margin
= p_dest
->p
->i_pitch
- p_dest
->p
->i_visible_pitch
;
251 i_rewind
= (-(p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
)) & 7;
253 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
254 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
255 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
256 SetOffset( (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
),
257 (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
),
258 (p_filter
->fmt_out
.video
.i_x_offset
+ p_filter
->fmt_out
.video
.i_visible_width
),
259 (p_filter
->fmt_out
.video
.i_y_offset
+ p_filter
->fmt_out
.video
.i_visible_height
),
260 &b_hscale
, &i_vscale
, p_offset_start
);
265 i_scale_count
= ( i_vscale
== 1 ) ?
266 (p_filter
->fmt_out
.video
.i_y_offset
+ p_filter
->fmt_out
.video
.i_visible_height
) :
267 (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
);
268 for( i_y
= 0; i_y
< (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
); i_y
++ )
271 p_buffer
= b_hscale
? p_buffer_start
: p_pic
;
273 for ( i_x
= (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
) / 8; i_x
--; )
275 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
276 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
277 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
278 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
281 /* Here we do some unaligned reads and duplicate conversions, but
282 * at least we have all the pixels */
286 p_u
-= i_rewind
>> 1;
287 p_v
-= i_rewind
>> 1;
288 p_buffer
-= i_rewind
;
289 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
290 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
291 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
292 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
295 SCALE_HEIGHT( 420, 4 );
297 p_y
+= i_source_margin
;
300 p_u
+= i_source_margin_c
;
301 p_v
+= i_source_margin_c
;