1 /*****************************************************************************
2 * i420_rgb16.c : YUV to bitmap RGB conversion module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <string.h> /* strerror() */
28 #include <stdlib.h> /* malloc(), free() */
34 #if defined (MODULE_NAME_IS_i420_rgb)
35 # include "i420_rgb_c.h"
36 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
37 # if defined(HAVE_MMX_INTRINSICS)
38 # include <mmintrin.h>
40 # include "i420_rgb_mmx.h"
43 static void SetOffset( int, int, int, int, vlc_bool_t *,
44 unsigned int *, int * );
46 #if defined (MODULE_NAME_IS_i420_rgb)
47 /*****************************************************************************
48 * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp with dithering
49 *****************************************************************************
50 * Horizontal alignment needed:
51 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
52 * - output: 1 pixel (2 bytes), margins allowed
53 * Vertical alignment needed:
54 * - input: 2 lines (2 Y lines, 1 U/V line)
56 *****************************************************************************/
57 void E_(I420_RGB16_dithering)( vout_thread_t *p_vout, picture_t *p_src,
60 /* We got this one from the old arguments */
61 uint16_t *p_pic = (uint16_t*)p_dest->p->p_pixels;
62 uint8_t *p_y = p_src->Y_PIXELS;
63 uint8_t *p_u = p_src->U_PIXELS;
64 uint8_t *p_v = p_src->V_PIXELS;
66 vlc_bool_t b_hscale; /* horizontal scaling type */
67 unsigned int i_vscale; /* vertical scaling type */
68 unsigned int i_x, i_y; /* horizontal and vertical indexes */
69 unsigned int i_real_y; /* y % 4 */
73 int i_scale_count; /* scale modulo counter */
74 int i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
75 uint16_t * p_pic_start; /* beginning of the current line for copy */
76 int i_uval, i_vval; /* U and V samples */
77 int i_red, i_green, i_blue; /* U and V modified samples */
78 uint16_t * p_yuv = p_vout->chroma.p_sys->p_rgb16;
79 uint16_t * p_ybase; /* Y dependant conversion table */
81 /* Conversion buffer pointer */
82 uint16_t * p_buffer_start = (uint16_t*)p_vout->chroma.p_sys->p_buffer;
85 /* Offset array pointer */
86 int * p_offset_start = p_vout->chroma.p_sys->p_offset;
89 const int i_source_margin = p_src->p[0].i_pitch
90 - p_src->p[0].i_visible_pitch;
91 const int i_source_margin_c = p_src->p[1].i_pitch
92 - p_src->p[1].i_visible_pitch;
94 /* The dithering matrices */
95 int dither10[4] = { 0x0, 0x8, 0x2, 0xa };
96 int dither11[4] = { 0xc, 0x4, 0xe, 0x6 };
97 int dither12[4] = { 0x3, 0xb, 0x1, 0x9 };
98 int dither13[4] = { 0xf, 0x7, 0xd, 0x5 };
100 for(i_x = 0; i_x < 4; i_x++)
102 dither10[i_x] = dither10[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
103 dither11[i_x] = dither11[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
104 dither12[i_x] = dither12[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
105 dither13[i_x] = dither13[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
108 i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
110 if( p_vout->render.i_width & 7 )
112 i_rewind = 8 - ( p_vout->render.i_width & 7 );
119 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
120 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
121 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
122 SetOffset( p_vout->render.i_width, p_vout->render.i_height,
123 p_vout->output.i_width, p_vout->output.i_height,
124 &b_hscale, &i_vscale, p_offset_start );
129 i_scale_count = ( i_vscale == 1 ) ?
130 p_vout->output.i_height : p_vout->render.i_height;
131 for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
133 i_real_y = i_y & 0x3;
135 p_buffer = b_hscale ? p_buffer_start : p_pic;
137 for ( i_x = p_vout->render.i_width / 8; i_x--; )
139 int *p_dither = dither10;
140 CONVERT_YUV_PIXEL_DITHER(2);
142 CONVERT_Y_PIXEL_DITHER(2);
144 CONVERT_YUV_PIXEL_DITHER(2);
146 CONVERT_Y_PIXEL_DITHER(2);
148 CONVERT_YUV_PIXEL_DITHER(2);
150 CONVERT_Y_PIXEL_DITHER(2);
152 CONVERT_YUV_PIXEL_DITHER(2);
154 CONVERT_Y_PIXEL_DITHER(2);
157 /* Here we do some unaligned reads and duplicate conversions, but
158 * at least we have all the pixels */
161 int *p_dither = dither10;
163 p_u -= i_rewind >> 1;
164 p_v -= i_rewind >> 1;
165 p_buffer -= i_rewind;
166 CONVERT_YUV_PIXEL_DITHER(2);
168 CONVERT_Y_PIXEL_DITHER(2);
170 CONVERT_YUV_PIXEL_DITHER(2);
172 CONVERT_Y_PIXEL_DITHER(2);
174 CONVERT_YUV_PIXEL_DITHER(2);
176 CONVERT_Y_PIXEL_DITHER(2);
178 CONVERT_YUV_PIXEL_DITHER(2);
180 CONVERT_Y_PIXEL_DITHER(2);
183 SCALE_HEIGHT( 420, 2 );
185 p_y += i_source_margin;
188 p_u += i_source_margin_c;
189 p_v += i_source_margin_c;
195 /*****************************************************************************
196 * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp
197 *****************************************************************************
198 * Horizontal alignment needed:
199 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
200 * - output: 1 pixel (2 bytes), margins allowed
201 * Vertical alignment needed:
202 * - input: 2 lines (2 Y lines, 1 U/V line)
204 *****************************************************************************/
205 void E_(I420_RGB16)( vout_thread_t *p_vout, picture_t *p_src,
208 /* We got this one from the old arguments */
209 uint16_t *p_pic = (uint16_t*)p_dest->p->p_pixels;
210 uint8_t *p_y = p_src->Y_PIXELS;
211 uint8_t *p_u = p_src->U_PIXELS;
212 uint8_t *p_v = p_src->V_PIXELS;
214 vlc_bool_t b_hscale; /* horizontal scaling type */
215 unsigned int i_vscale; /* vertical scaling type */
216 unsigned int i_x, i_y; /* horizontal and vertical indexes */
220 int i_scale_count; /* scale modulo counter */
221 int i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
222 uint16_t * p_pic_start; /* beginning of the current line for copy */
223 #if defined (MODULE_NAME_IS_i420_rgb)
224 int i_uval, i_vval; /* U and V samples */
225 int i_red, i_green, i_blue; /* U and V modified samples */
226 uint16_t * p_yuv = p_vout->chroma.p_sys->p_rgb16;
227 uint16_t * p_ybase; /* Y dependant conversion table */
230 /* Conversion buffer pointer */
231 uint16_t * p_buffer_start = (uint16_t*)p_vout->chroma.p_sys->p_buffer;
234 /* Offset array pointer */
235 int * p_offset_start = p_vout->chroma.p_sys->p_offset;
238 const int i_source_margin = p_src->p[0].i_pitch
239 - p_src->p[0].i_visible_pitch;
240 const int i_source_margin_c = p_src->p[1].i_pitch
241 - p_src->p[1].i_visible_pitch;
243 i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
245 if( p_vout->render.i_width & 7 )
247 i_rewind = 8 - ( p_vout->render.i_width & 7 );
254 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
255 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
256 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
257 SetOffset( p_vout->render.i_width, p_vout->render.i_height,
258 p_vout->output.i_width, p_vout->output.i_height,
259 &b_hscale, &i_vscale, p_offset_start );
264 i_scale_count = ( i_vscale == 1 ) ?
265 p_vout->output.i_height : p_vout->render.i_height;
266 for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
269 p_buffer = b_hscale ? p_buffer_start : p_pic;
271 #if defined (MODULE_NAME_IS_i420_rgb)
272 for ( i_x = p_vout->render.i_width / 8; i_x--; )
274 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
275 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
276 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
277 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
279 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
280 if( p_vout->output.i_rmask == 0x7c00 )
283 for ( i_x = p_vout->render.i_width / 8; i_x--; )
285 # if defined (HAVE_MMX_INTRINSICS)
286 __m64 mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7;
294 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
296 __asm__( ".p2align 3"
300 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
312 for ( i_x = p_vout->render.i_width / 8; i_x--; )
314 # if defined (HAVE_MMX_INTRINSICS)
315 __m64 mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7;
323 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
325 __asm__( ".p2align 3"
329 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
340 /* Here we do some unaligned reads and duplicate conversions, but
341 * at least we have all the pixels */
344 #if defined (MODULE_NAME_IS_i420_rgb_mmx)
345 # if defined (HAVE_MMX_INTRINSICS)
346 __m64 mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7;
351 p_u -= i_rewind >> 1;
352 p_v -= i_rewind >> 1;
353 p_buffer -= i_rewind;
354 #if defined (MODULE_NAME_IS_i420_rgb)
355 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
356 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
357 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
358 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
359 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
361 # if defined (HAVE_MMX_INTRINSICS)
365 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
368 if( p_vout->output.i_rmask == 0x7c00 )
371 # if defined (HAVE_MMX_INTRINSICS)
376 __asm__( ".p2align 3"
380 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
385 # if defined (HAVE_MMX_INTRINSICS)
391 __asm__( ".p2align 3"
395 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
406 SCALE_HEIGHT( 420, 2 );
408 p_y += i_source_margin;
411 p_u += i_source_margin_c;
412 p_v += i_source_margin_c;
417 /*****************************************************************************
418 * I420_RGB32: color YUV 4:2:0 to RGB 32 bpp
419 *****************************************************************************
420 * Horizontal alignment needed:
421 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
422 * - output: 1 pixel (2 bytes), margins allowed
423 * Vertical alignment needed:
424 * - input: 2 lines (2 Y lines, 1 U/V line)
426 *****************************************************************************/
427 void E_(I420_RGB32)( vout_thread_t *p_vout, picture_t *p_src,
430 /* We got this one from the old arguments */
431 uint32_t *p_pic = (uint32_t*)p_dest->p->p_pixels;
432 uint8_t *p_y = p_src->Y_PIXELS;
433 uint8_t *p_u = p_src->U_PIXELS;
434 uint8_t *p_v = p_src->V_PIXELS;
436 vlc_bool_t b_hscale; /* horizontal scaling type */
437 unsigned int i_vscale; /* vertical scaling type */
438 unsigned int i_x, i_y; /* horizontal and vertical indexes */
442 int i_scale_count; /* scale modulo counter */
443 int i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
444 uint32_t * p_pic_start; /* beginning of the current line for copy */
445 #if defined (MODULE_NAME_IS_i420_rgb)
446 int i_uval, i_vval; /* U and V samples */
447 int i_red, i_green, i_blue; /* U and V modified samples */
448 uint32_t * p_yuv = p_vout->chroma.p_sys->p_rgb32;
449 uint32_t * p_ybase; /* Y dependant conversion table */
452 /* Conversion buffer pointer */
453 uint32_t * p_buffer_start = (uint32_t*)p_vout->chroma.p_sys->p_buffer;
456 /* Offset array pointer */
457 int * p_offset_start = p_vout->chroma.p_sys->p_offset;
460 const int i_source_margin = p_src->p[0].i_pitch
461 - p_src->p[0].i_visible_pitch;
462 const int i_source_margin_c = p_src->p[1].i_pitch
463 - p_src->p[1].i_visible_pitch;
465 i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
467 if( p_vout->render.i_width & 7 )
469 i_rewind = 8 - ( p_vout->render.i_width & 7 );
476 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
477 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
478 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
479 SetOffset( p_vout->render.i_width, p_vout->render.i_height,
480 p_vout->output.i_width, p_vout->output.i_height,
481 &b_hscale, &i_vscale, p_offset_start );
486 i_scale_count = ( i_vscale == 1 ) ?
487 p_vout->output.i_height : p_vout->render.i_height;
488 for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
491 p_buffer = b_hscale ? p_buffer_start : p_pic;
493 for ( i_x = p_vout->render.i_width / 8; i_x--; )
495 #if defined (MODULE_NAME_IS_i420_rgb)
496 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
497 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
498 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
499 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
500 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
501 # if defined (HAVE_MMX_INTRINSICS)
502 __m64 mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7;
510 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
512 __asm__( ".p2align 3"
516 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
526 /* Here we do some unaligned reads and duplicate conversions, but
527 * at least we have all the pixels */
530 #if defined (MODULE_NAME_IS_i420_rgb_mmx)
531 # if defined (HAVE_MMX_INTRINSICS)
532 __m64 mm0, mm1, mm2, mm3, mm4, mm5, mm6, mm7;
537 p_u -= i_rewind >> 1;
538 p_v -= i_rewind >> 1;
539 p_buffer -= i_rewind;
540 #if defined (MODULE_NAME_IS_i420_rgb)
541 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
542 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
543 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
544 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
545 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
546 # if defined (HAVE_MMX_INTRINSICS)
553 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
555 __asm__( ".p2align 3"
559 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
569 SCALE_HEIGHT( 420, 4 );
571 p_y += i_source_margin;
574 p_u += i_source_margin_c;
575 p_v += i_source_margin_c;
580 /* Following functions are local */
582 /*****************************************************************************
583 * SetOffset: build offset array for conversion functions
584 *****************************************************************************
585 * This function will build an offset array used in later conversion functions.
586 * It will also set horizontal and vertical scaling indicators.
587 *****************************************************************************/
588 static void SetOffset( int i_width, int i_height, int i_pic_width,
589 int i_pic_height, vlc_bool_t *pb_hscale,
590 unsigned int *pi_vscale, int *p_offset )
592 int i_x; /* x position in destination */
593 int i_scale_count; /* modulo counter */
596 * Prepare horizontal offset array
598 if( i_pic_width - i_width == 0 )
600 /* No horizontal scaling: YUV conversion is done directly to picture */
603 else if( i_pic_width - i_width > 0 )
605 /* Prepare scaling array for horizontal extension */
607 i_scale_count = i_pic_width;
608 for( i_x = i_width; i_x--; )
610 while( (i_scale_count -= i_width) > 0 )
615 i_scale_count += i_pic_width;
618 else /* if( i_pic_width - i_width < 0 ) */
620 /* Prepare scaling array for horizontal reduction */
622 i_scale_count = i_width;
623 for( i_x = i_pic_width; i_x--; )
626 while( (i_scale_count -= i_pic_width) > 0 )
631 i_scale_count += i_width;
636 * Set vertical scaling indicator
638 if( i_pic_height - i_height == 0 )
642 else if( i_pic_height - i_height > 0 )
646 else /* if( i_pic_height - i_height < 0 ) */