demux:mkv: fix wrong value reset after clean of an array
[vlc.git] / modules / video_chroma / i420_rgb16.c
blob2db649786413beb9d439ccb948254b517114d3e2
1 /*****************************************************************************
2 * i420_rgb16.c : YUV to bitmap RGB conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VLC authors and VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Damien Fouilleul <damienf@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_common.h>
29 #include <vlc_filter.h>
30 #include <vlc_picture.h>
31 #include <vlc_cpu.h>
33 #include "i420_rgb.h"
34 #include "i420_rgb_c.h"
36 /*****************************************************************************
37 * SetOffset: build offset array for conversion functions
38 *****************************************************************************
39 * This function will build an offset array used in later conversion functions.
40 * It will also set horizontal and vertical scaling indicators.
41 *****************************************************************************/
42 static void SetOffset( int i_width, int i_height, int i_pic_width,
43 int i_pic_height, bool *pb_hscale,
44 unsigned int *pi_vscale, int *p_offset )
47 * Prepare horizontal offset array
49 if( i_pic_width - i_width == 0 )
50 { /* No horizontal scaling: YUV conversion is done directly to picture */
51 *pb_hscale = 0;
53 else if( i_pic_width - i_width > 0 )
54 { /* Prepare scaling array for horizontal extension */
55 int i_scale_count = i_pic_width;
57 *pb_hscale = 1;
58 for( int i_x = i_width; i_x--; )
60 while( (i_scale_count -= i_width) > 0 )
62 *p_offset++ = 0;
64 *p_offset++ = 1;
65 i_scale_count += i_pic_width;
68 else /* if( i_pic_width - i_width < 0 ) */
69 { /* Prepare scaling array for horizontal reduction */
70 int i_scale_count = i_pic_width;
72 *pb_hscale = 1;
73 for( int i_x = i_pic_width; i_x--; )
75 *p_offset = 1;
76 while( (i_scale_count -= i_pic_width) > 0 )
78 *p_offset += 1;
80 p_offset++;
81 i_scale_count += i_width;
86 * Set vertical scaling indicator
88 if( i_pic_height - i_height == 0 )
89 *pi_vscale = 0;
90 else if( i_pic_height - i_height > 0 )
91 *pi_vscale = 1;
92 else /* if( i_pic_height - i_height < 0 ) */
93 *pi_vscale = -1;
96 /*****************************************************************************
97 * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp
98 *****************************************************************************
99 * Horizontal alignment needed:
100 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
101 * - output: 1 pixel (2 bytes), margins allowed
102 * Vertical alignment needed:
103 * - input: 2 lines (2 Y lines, 1 U/V line)
104 * - output: 1 line
105 *****************************************************************************/
107 void I420_RGB16( filter_t *p_filter, picture_t *p_src, picture_t *p_dest )
109 filter_sys_t *p_sys = p_filter->p_sys;
111 /* We got this one from the old arguments */
112 uint16_t *p_pic = (uint16_t*)p_dest->p->p_pixels;
113 uint8_t *p_y = p_src->Y_PIXELS;
114 uint8_t *p_u = p_src->U_PIXELS;
115 uint8_t *p_v = p_src->V_PIXELS;
117 bool b_hscale; /* horizontal scaling type */
118 unsigned int i_vscale; /* vertical scaling type */
119 unsigned int i_x, i_y; /* horizontal and vertical indexes */
121 int i_right_margin;
122 int i_rewind;
123 int i_scale_count; /* scale modulo counter */
124 int i_chroma_width = (p_filter->fmt_in.video.i_x_offset + p_filter->fmt_in.video.i_visible_width) / 2; /* chroma width */
125 uint16_t * p_pic_start; /* beginning of the current line for copy */
126 int i_uval, i_vval; /* U and V samples */
127 int i_red, i_green, i_blue; /* U and V modified samples */
128 uint16_t * p_yuv = p_sys->p_rgb16;
129 uint16_t * p_ybase; /* Y dependant conversion table */
131 /* Conversion buffer pointer */
132 uint16_t * p_buffer_start;
133 uint16_t * p_buffer;
135 /* Offset array pointer */
136 int * p_offset_start = p_sys->p_offset;
137 int * p_offset;
139 const int i_source_margin = p_src->p[0].i_pitch
140 - p_src->p[0].i_visible_pitch
141 - p_filter->fmt_in.video.i_x_offset;
142 const int i_source_margin_c = p_src->p[1].i_pitch
143 - p_src->p[1].i_visible_pitch
144 - ( p_filter->fmt_in.video.i_x_offset / 2 );
146 i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
147 i_rewind = (-(p_filter->fmt_in.video.i_x_offset + p_filter->fmt_in.video.i_visible_width)) & 7;
149 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
150 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
151 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
152 SetOffset( (p_filter->fmt_in.video.i_x_offset + p_filter->fmt_in.video.i_visible_width),
153 (p_filter->fmt_in.video.i_y_offset + p_filter->fmt_in.video.i_visible_height),
154 (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width),
155 (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height),
156 &b_hscale, &i_vscale, p_offset_start );
158 if(b_hscale &&
159 AllocateOrGrow(&p_sys->p_buffer, &p_sys->i_buffer_size,
160 p_filter->fmt_in.video.i_x_offset +
161 p_filter->fmt_in.video.i_visible_width,
162 p_sys->i_bytespp))
163 return;
164 else p_buffer_start = (uint16_t*)p_sys->p_buffer;
167 * Perform conversion
169 i_scale_count = ( i_vscale == 1 ) ?
170 (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height) :
171 (p_filter->fmt_in.video.i_y_offset + p_filter->fmt_in.video.i_visible_height);
172 for( i_y = 0; i_y < (p_filter->fmt_in.video.i_y_offset + p_filter->fmt_in.video.i_visible_height); i_y++ )
174 p_pic_start = p_pic;
175 p_buffer = b_hscale ? p_buffer_start : p_pic;
177 for ( i_x = (p_filter->fmt_in.video.i_x_offset + p_filter->fmt_in.video.i_visible_width) / 8; i_x--; )
179 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
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);
185 /* Here we do some unaligned reads and duplicate conversions, but
186 * at least we have all the pixels */
187 if( i_rewind )
189 p_y -= i_rewind;
190 p_u -= i_rewind >> 1;
191 p_v -= i_rewind >> 1;
192 p_buffer -= i_rewind;
194 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
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);
199 SCALE_WIDTH;
200 SCALE_HEIGHT( 420, 2 );
202 p_y += i_source_margin;
203 if( i_y % 2 )
205 p_u += i_source_margin_c;
206 p_v += i_source_margin_c;
211 /*****************************************************************************
212 * I420_RGB32: color YUV 4:2:0 to RGB 32 bpp
213 *****************************************************************************
214 * Horizontal alignment needed:
215 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
216 * - output: 1 pixel (2 bytes), margins allowed
217 * Vertical alignment needed:
218 * - input: 2 lines (2 Y lines, 1 U/V line)
219 * - output: 1 line
220 *****************************************************************************/
222 void I420_RGB32( filter_t *p_filter, picture_t *p_src, picture_t *p_dest )
224 filter_sys_t *p_sys = p_filter->p_sys;
226 /* We got this one from the old arguments */
227 uint32_t *p_pic = (uint32_t*)p_dest->p->p_pixels;
228 uint8_t *p_y = p_src->Y_PIXELS;
229 uint8_t *p_u = p_src->U_PIXELS;
230 uint8_t *p_v = p_src->V_PIXELS;
232 bool b_hscale; /* horizontal scaling type */
233 unsigned int i_vscale; /* vertical scaling type */
234 unsigned int i_x, i_y; /* horizontal and vertical indexes */
236 int i_right_margin;
237 int i_rewind;
238 int i_scale_count; /* scale modulo counter */
239 int i_chroma_width = (p_filter->fmt_in.video.i_x_offset + p_filter->fmt_in.video.i_visible_width) / 2; /* chroma width */
240 uint32_t * p_pic_start; /* beginning of the current line for copy */
241 int i_uval, i_vval; /* U and V samples */
242 int i_red, i_green, i_blue; /* U and V modified samples */
243 uint32_t * p_yuv = p_sys->p_rgb32;
244 uint32_t * p_ybase; /* Y dependant conversion table */
246 /* Conversion buffer pointer */
247 uint32_t * p_buffer_start;
248 uint32_t * p_buffer;
250 /* Offset array pointer */
251 int * p_offset_start = p_sys->p_offset;
252 int * p_offset;
254 const int i_source_margin = p_src->p[0].i_pitch
255 - p_src->p[0].i_visible_pitch
256 - p_filter->fmt_in.video.i_x_offset;
257 const int i_source_margin_c = p_src->p[1].i_pitch
258 - p_src->p[1].i_visible_pitch
259 - ( p_filter->fmt_in.video.i_x_offset / 2 );
261 i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
262 i_rewind = (-(p_filter->fmt_in.video.i_x_offset + p_filter->fmt_in.video.i_visible_width)) & 7;
264 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
265 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
266 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
267 SetOffset( (p_filter->fmt_in.video.i_x_offset + p_filter->fmt_in.video.i_visible_width),
268 (p_filter->fmt_in.video.i_y_offset + p_filter->fmt_in.video.i_visible_height),
269 (p_filter->fmt_out.video.i_x_offset + p_filter->fmt_out.video.i_visible_width),
270 (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height),
271 &b_hscale, &i_vscale, p_offset_start );
273 if(b_hscale &&
274 AllocateOrGrow(&p_sys->p_buffer, &p_sys->i_buffer_size,
275 p_filter->fmt_in.video.i_x_offset +
276 p_filter->fmt_in.video.i_visible_width,
277 p_sys->i_bytespp))
278 return;
279 else p_buffer_start = (uint32_t*)p_sys->p_buffer;
282 * Perform conversion
284 i_scale_count = ( i_vscale == 1 ) ?
285 (p_filter->fmt_out.video.i_y_offset + p_filter->fmt_out.video.i_visible_height) :
286 (p_filter->fmt_in.video.i_y_offset + p_filter->fmt_in.video.i_visible_height);
287 for( i_y = 0; i_y < (p_filter->fmt_in.video.i_y_offset + p_filter->fmt_in.video.i_visible_height); i_y++ )
289 p_pic_start = p_pic;
290 p_buffer = b_hscale ? p_buffer_start : p_pic;
292 for ( i_x = (p_filter->fmt_in.video.i_x_offset + p_filter->fmt_in.video.i_visible_width) / 8; i_x--; )
294 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
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);
300 /* Here we do some unaligned reads and duplicate conversions, but
301 * at least we have all the pixels */
302 if( i_rewind )
304 p_y -= i_rewind;
305 p_u -= i_rewind >> 1;
306 p_v -= i_rewind >> 1;
307 p_buffer -= i_rewind;
308 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
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);
313 SCALE_WIDTH;
314 SCALE_HEIGHT( 420, 4 );
316 p_y += i_source_margin;
317 if( i_y % 2 )
319 p_u += i_source_margin_c;
320 p_v += i_source_margin_c;