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 filter_sys_t
*p_sys
= p_filter
->p_sys
;
112 /* We got this one from the old arguments */
113 uint16_t *p_pic
= (uint16_t*)p_dest
->p
->p_pixels
;
114 uint8_t *p_y
= p_src
->Y_PIXELS
;
115 uint8_t *p_u
= p_src
->U_PIXELS
;
116 uint8_t *p_v
= p_src
->V_PIXELS
;
118 bool b_hscale
; /* horizontal scaling type */
119 unsigned int i_vscale
; /* vertical scaling type */
120 unsigned int i_x
, i_y
; /* horizontal and vertical indexes */
124 int i_scale_count
; /* scale modulo counter */
125 int i_chroma_width
= (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
) / 2; /* chroma width */
126 uint16_t * p_pic_start
; /* beginning of the current line for copy */
127 int i_uval
, i_vval
; /* U and V samples */
128 int i_red
, i_green
, i_blue
; /* U and V modified samples */
129 uint16_t * p_yuv
= p_sys
->p_rgb16
;
130 uint16_t * p_ybase
; /* Y dependant conversion table */
132 /* Conversion buffer pointer */
133 uint16_t * p_buffer_start
;
136 /* Offset array pointer */
137 int * p_offset_start
= p_sys
->p_offset
;
140 const int i_source_margin
= p_src
->p
[0].i_pitch
141 - p_src
->p
[0].i_visible_pitch
142 - p_filter
->fmt_in
.video
.i_x_offset
;
143 const int i_source_margin_c
= p_src
->p
[1].i_pitch
144 - p_src
->p
[1].i_visible_pitch
145 - ( p_filter
->fmt_in
.video
.i_x_offset
/ 2 );
147 i_right_margin
= p_dest
->p
->i_pitch
- p_dest
->p
->i_visible_pitch
;
148 i_rewind
= (-(p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
)) & 7;
150 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
151 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
152 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
153 SetOffset( (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
),
154 (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
),
155 (p_filter
->fmt_out
.video
.i_x_offset
+ p_filter
->fmt_out
.video
.i_visible_width
),
156 (p_filter
->fmt_out
.video
.i_y_offset
+ p_filter
->fmt_out
.video
.i_visible_height
),
157 &b_hscale
, &i_vscale
, p_offset_start
);
160 AllocateOrGrow(&p_sys
->p_buffer
, &p_sys
->i_buffer_size
,
161 p_filter
->fmt_in
.video
.i_x_offset
+
162 p_filter
->fmt_in
.video
.i_visible_width
,
165 else p_buffer_start
= (uint16_t*)p_sys
->p_buffer
;
170 i_scale_count
= ( i_vscale
== 1 ) ?
171 (p_filter
->fmt_out
.video
.i_y_offset
+ p_filter
->fmt_out
.video
.i_visible_height
) :
172 (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
);
173 for( i_y
= 0; i_y
< (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
); i_y
++ )
176 p_buffer
= b_hscale
? p_buffer_start
: p_pic
;
178 for ( i_x
= (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
) / 8; i_x
--; )
180 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
181 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
182 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
183 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
186 /* Here we do some unaligned reads and duplicate conversions, but
187 * at least we have all the pixels */
191 p_u
-= i_rewind
>> 1;
192 p_v
-= i_rewind
>> 1;
193 p_buffer
-= i_rewind
;
195 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
196 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
197 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
198 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
201 SCALE_HEIGHT( 420, 2 );
203 p_y
+= i_source_margin
;
206 p_u
+= i_source_margin_c
;
207 p_v
+= i_source_margin_c
;
212 /*****************************************************************************
213 * I420_RGB32: color YUV 4:2:0 to RGB 32 bpp
214 *****************************************************************************
215 * Horizontal alignment needed:
216 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
217 * - output: 1 pixel (2 bytes), margins allowed
218 * Vertical alignment needed:
219 * - input: 2 lines (2 Y lines, 1 U/V line)
221 *****************************************************************************/
223 void I420_RGB32( filter_t
*p_filter
, picture_t
*p_src
, picture_t
*p_dest
)
225 filter_sys_t
*p_sys
= p_filter
->p_sys
;
227 /* We got this one from the old arguments */
228 uint32_t *p_pic
= (uint32_t*)p_dest
->p
->p_pixels
;
229 uint8_t *p_y
= p_src
->Y_PIXELS
;
230 uint8_t *p_u
= p_src
->U_PIXELS
;
231 uint8_t *p_v
= p_src
->V_PIXELS
;
233 bool b_hscale
; /* horizontal scaling type */
234 unsigned int i_vscale
; /* vertical scaling type */
235 unsigned int i_x
, i_y
; /* horizontal and vertical indexes */
239 int i_scale_count
; /* scale modulo counter */
240 int i_chroma_width
= (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
) / 2; /* chroma width */
241 uint32_t * p_pic_start
; /* beginning of the current line for copy */
242 int i_uval
, i_vval
; /* U and V samples */
243 int i_red
, i_green
, i_blue
; /* U and V modified samples */
244 uint32_t * p_yuv
= p_sys
->p_rgb32
;
245 uint32_t * p_ybase
; /* Y dependant conversion table */
247 /* Conversion buffer pointer */
248 uint32_t * p_buffer_start
;
251 /* Offset array pointer */
252 int * p_offset_start
= p_sys
->p_offset
;
255 const int i_source_margin
= p_src
->p
[0].i_pitch
256 - p_src
->p
[0].i_visible_pitch
257 - p_filter
->fmt_in
.video
.i_x_offset
;
258 const int i_source_margin_c
= p_src
->p
[1].i_pitch
259 - p_src
->p
[1].i_visible_pitch
260 - ( p_filter
->fmt_in
.video
.i_x_offset
/ 2 );
262 i_right_margin
= p_dest
->p
->i_pitch
- p_dest
->p
->i_visible_pitch
;
263 i_rewind
= (-(p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
)) & 7;
265 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
266 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
267 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
268 SetOffset( (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
),
269 (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
),
270 (p_filter
->fmt_out
.video
.i_x_offset
+ p_filter
->fmt_out
.video
.i_visible_width
),
271 (p_filter
->fmt_out
.video
.i_y_offset
+ p_filter
->fmt_out
.video
.i_visible_height
),
272 &b_hscale
, &i_vscale
, p_offset_start
);
275 AllocateOrGrow(&p_sys
->p_buffer
, &p_sys
->i_buffer_size
,
276 p_filter
->fmt_in
.video
.i_x_offset
+
277 p_filter
->fmt_in
.video
.i_visible_width
,
280 else p_buffer_start
= (uint32_t*)p_sys
->p_buffer
;
285 i_scale_count
= ( i_vscale
== 1 ) ?
286 (p_filter
->fmt_out
.video
.i_y_offset
+ p_filter
->fmt_out
.video
.i_visible_height
) :
287 (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
);
288 for( i_y
= 0; i_y
< (p_filter
->fmt_in
.video
.i_y_offset
+ p_filter
->fmt_in
.video
.i_visible_height
); i_y
++ )
291 p_buffer
= b_hscale
? p_buffer_start
: p_pic
;
293 for ( i_x
= (p_filter
->fmt_in
.video
.i_x_offset
+ p_filter
->fmt_in
.video
.i_visible_width
) / 8; i_x
--; )
295 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
296 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
297 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
298 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
301 /* Here we do some unaligned reads and duplicate conversions, but
302 * at least we have all the pixels */
306 p_u
-= i_rewind
>> 1;
307 p_v
-= i_rewind
>> 1;
308 p_buffer
-= i_rewind
;
309 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
310 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
311 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
312 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
315 SCALE_HEIGHT( 420, 4 );
317 p_y
+= i_source_margin
;
320 p_u
+= i_source_margin_c
;
321 p_v
+= i_source_margin_c
;