1 /*****************************************************************************
2 * i420_rgb16.c : YUV to bitmap RGB conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: i420_rgb16.c,v 1.6.2.1 2002/06/01 10:43:54 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <string.h> /* strerror() */
29 #include <stdlib.h> /* malloc(), free() */
31 #include <videolan/vlc.h>
34 #include "video_output.h"
37 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
38 # include "i420_rgb_c.h"
39 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
40 # include "i420_rgb_mmx.h"
43 static void SetOffset( int, int, int, int, boolean_t *, int *, int * );
45 /*****************************************************************************
46 * I420_RGB15: color YUV 4:2:0 to RGB 15 bpp
47 *****************************************************************************
48 * Horizontal alignment needed:
49 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
50 * - output: 1 pixel (2 bytes), margins allowed
51 * Vertical alignment needed:
52 * - input: 2 lines (2 Y lines, 1 U/V line)
54 *****************************************************************************/
55 void _M( I420_RGB15 )( vout_thread_t *p_vout, picture_t *p_src,
58 /* We got this one from the old arguments */
59 u16 *p_pic = (u16*)p_dest->p->p_pixels;
60 u8 *p_y = p_src->Y_PIXELS;
61 u8 *p_u = p_src->U_PIXELS;
62 u8 *p_v = p_src->V_PIXELS;
64 boolean_t b_hscale; /* horizontal scaling type */
65 int i_vscale; /* vertical scaling type */
66 int i_x, i_y; /* horizontal and vertical indexes */
69 int i_scale_count; /* scale modulo counter */
70 int i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
71 u16 * p_pic_start; /* beginning of the current line for copy */
72 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
73 int i_uval, i_vval; /* U and V samples */
74 int i_red, i_green, i_blue; /* U and V modified samples */
75 u16 * p_yuv = p_vout->chroma.p_sys->p_rgb16;
76 u16 * p_ybase; /* Y dependant conversion table */
79 /* Conversion buffer pointer */
80 u16 * p_buffer_start = (u16*)p_vout->chroma.p_sys->p_buffer;
83 /* Offset array pointer */
84 int * p_offset_start = p_vout->chroma.p_sys->p_offset;
87 if( p_dest->p->b_margin )
89 i_right_margin = (p_dest->p->i_pitch - p_dest->p->i_visible_bytes) / 2;
96 if( p_vout->render.i_width & 7 )
98 i_rewind = 8 - ( p_vout->render.i_width & 7 );
105 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
106 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
107 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
108 SetOffset( p_vout->render.i_width, p_vout->render.i_height,
109 p_vout->output.i_width, p_vout->output.i_height,
110 &b_hscale, &i_vscale, p_offset_start );
115 i_scale_count = ( i_vscale == 1 ) ?
116 p_vout->output.i_height : p_vout->render.i_height;
117 for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
120 p_buffer = b_hscale ? p_buffer_start : p_pic;
122 for ( i_x = p_vout->render.i_width / 8; i_x--; )
124 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
125 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
126 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
127 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
128 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
129 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
131 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
137 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
146 /* Here we do some unaligned reads and duplicate conversions, but
147 * at least we have all the pixels */
151 p_u -= i_rewind >> 1;
152 p_v -= i_rewind >> 1;
153 p_buffer -= i_rewind;
154 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
155 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
156 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
157 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
158 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
159 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
161 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
167 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
176 SCALE_HEIGHT( 420, 2 );
180 /*****************************************************************************
181 * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp
182 *****************************************************************************
183 * Horizontal alignment needed:
184 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
185 * - output: 1 pixel (2 bytes), margins allowed
186 * Vertical alignment needed:
187 * - input: 2 lines (2 Y lines, 1 U/V line)
189 *****************************************************************************/
190 void _M( I420_RGB16 )( vout_thread_t *p_vout, picture_t *p_src,
193 /* We got this one from the old arguments */
194 u16 *p_pic = (u16*)p_dest->p->p_pixels;
195 u8 *p_y = p_src->Y_PIXELS;
196 u8 *p_u = p_src->U_PIXELS;
197 u8 *p_v = p_src->V_PIXELS;
199 boolean_t b_hscale; /* horizontal scaling type */
200 int i_vscale; /* vertical scaling type */
201 int i_x, i_y; /* horizontal and vertical indexes */
204 int i_scale_count; /* scale modulo counter */
205 int i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
206 u16 * p_pic_start; /* beginning of the current line for copy */
207 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
208 int i_uval, i_vval; /* U and V samples */
209 int i_red, i_green, i_blue; /* U and V modified samples */
210 u16 * p_yuv = p_vout->chroma.p_sys->p_rgb16;
211 u16 * p_ybase; /* Y dependant conversion table */
214 /* Conversion buffer pointer */
215 u16 * p_buffer_start = (u16*)p_vout->chroma.p_sys->p_buffer;
218 /* Offset array pointer */
219 int * p_offset_start = p_vout->chroma.p_sys->p_offset;
222 if( p_dest->p->b_margin )
224 i_right_margin = (p_dest->p->i_pitch - p_dest->p->i_visible_bytes) / 2;
231 if( p_vout->render.i_width & 7 )
233 i_rewind = 8 - ( p_vout->render.i_width & 7 );
240 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
241 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
242 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
243 SetOffset( p_vout->render.i_width, p_vout->render.i_height,
244 p_vout->output.i_width, p_vout->output.i_height,
245 &b_hscale, &i_vscale, p_offset_start );
250 i_scale_count = ( i_vscale == 1 ) ?
251 p_vout->output.i_height : p_vout->render.i_height;
252 for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
255 p_buffer = b_hscale ? p_buffer_start : p_pic;
257 for ( i_x = p_vout->render.i_width / 8; i_x--; )
259 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
260 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
261 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
262 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
263 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
264 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
266 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
272 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
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 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
290 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
291 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
292 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
293 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
294 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
296 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
302 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
311 SCALE_HEIGHT( 420, 2 );
315 /*****************************************************************************
316 * I420_RGB32: color YUV 4:2:0 to RGB 32 bpp
317 *****************************************************************************
318 * Horizontal alignment needed:
319 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
320 * - output: 1 pixel (2 bytes), margins allowed
321 * Vertical alignment needed:
322 * - input: 2 lines (2 Y lines, 1 U/V line)
324 *****************************************************************************/
325 void _M( I420_RGB32 )( vout_thread_t *p_vout, picture_t *p_src,
328 /* We got this one from the old arguments */
329 u32 *p_pic = (u32*)p_dest->p->p_pixels;
330 u8 *p_y = p_src->Y_PIXELS;
331 u8 *p_u = p_src->U_PIXELS;
332 u8 *p_v = p_src->V_PIXELS;
334 boolean_t b_hscale; /* horizontal scaling type */
335 int i_vscale; /* vertical scaling type */
336 int i_x, i_y; /* horizontal and vertical indexes */
339 int i_scale_count; /* scale modulo counter */
340 int i_chroma_width = p_vout->render.i_width / 4; /* chroma width */
341 u32 * p_pic_start; /* beginning of the current line for copy */
342 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
343 int i_uval, i_vval; /* U and V samples */
344 int i_red, i_green, i_blue; /* U and V modified samples */
345 u32 * p_yuv = p_vout->chroma.p_sys->p_rgb32;
346 u32 * p_ybase; /* Y dependant conversion table */
349 /* Conversion buffer pointer */
350 u32 * p_buffer_start = (u32*)p_vout->chroma.p_sys->p_buffer;
353 /* Offset array pointer */
354 int * p_offset_start = p_vout->chroma.p_sys->p_offset;
357 if( p_dest->p->b_margin )
359 i_right_margin = (p_dest->p->i_pitch - p_dest->p->i_visible_bytes) / 2;
366 if( p_vout->render.i_width & 7 )
368 i_rewind = 8 - ( p_vout->render.i_width & 7 );
375 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
376 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
377 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
378 SetOffset( p_vout->render.i_width, p_vout->render.i_height,
379 p_vout->output.i_width, p_vout->output.i_height,
380 &b_hscale, &i_vscale, p_offset_start );
385 i_scale_count = ( i_vscale == 1 ) ?
386 p_vout->output.i_height : p_vout->render.i_height;
387 for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
390 p_buffer = b_hscale ? p_buffer_start : p_pic;
392 for ( i_x = p_vout->render.i_width / 8; i_x--; )
394 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
395 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
396 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
397 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
398 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
399 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
401 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
407 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
416 /* Here we do some unaligned reads and duplicate conversions, but
417 * at least we have all the pixels */
421 p_u -= i_rewind >> 1;
422 p_v -= i_rewind >> 1;
423 p_buffer -= i_rewind;
424 #if defined (MODULE_NAME_IS_chroma_i420_rgb)
425 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
426 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
427 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
428 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
429 #elif defined (MODULE_NAME_IS_chroma_i420_rgb_mmx)
431 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
437 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
446 SCALE_HEIGHT( 420, 4 );
450 /* Following functions are local */
452 /*****************************************************************************
453 * SetOffset: build offset array for conversion functions
454 *****************************************************************************
455 * This function will build an offset array used in later conversion functions.
456 * It will also set horizontal and vertical scaling indicators.
457 *****************************************************************************/
458 static void SetOffset( int i_width, int i_height, int i_pic_width,
459 int i_pic_height, boolean_t *pb_hscale,
460 int *pi_vscale, int *p_offset )
462 int i_x; /* x position in destination */
463 int i_scale_count; /* modulo counter */
466 * Prepare horizontal offset array
468 if( i_pic_width - i_width == 0 )
470 /* No horizontal scaling: YUV conversion is done directly to picture */
473 else if( i_pic_width - i_width > 0 )
475 /* Prepare scaling array for horizontal extension */
477 i_scale_count = i_pic_width;
478 for( i_x = i_width; i_x--; )
480 while( (i_scale_count -= i_width) > 0 )
485 i_scale_count += i_pic_width;
488 else /* if( i_pic_width - i_width < 0 ) */
490 /* Prepare scaling array for horizontal reduction */
492 i_scale_count = i_width;
493 for( i_x = i_pic_width; i_x--; )
496 while( (i_scale_count -= i_pic_width) > 0 )
501 i_scale_count += i_width;
506 * Set vertical scaling indicator
508 if( i_pic_height - i_height == 0 )
512 else if( i_pic_height - i_height > 0 )
516 else /* if( i_pic_height - i_height < 0 ) */