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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 # include "i420_rgb_mmx.h"
40 static void SetOffset( int, int, int, int, vlc_bool_t *, int *, int * );
42 #if defined (MODULE_NAME_IS_i420_rgb)
43 /*****************************************************************************
44 * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp with dithering
45 *****************************************************************************
46 * Horizontal alignment needed:
47 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
48 * - output: 1 pixel (2 bytes), margins allowed
49 * Vertical alignment needed:
50 * - input: 2 lines (2 Y lines, 1 U/V line)
52 *****************************************************************************/
53 void E_(I420_RGB16_dithering)( vout_thread_t *p_vout, picture_t *p_src,
56 /* We got this one from the old arguments */
57 uint16_t *p_pic = (uint16_t*)p_dest->p->p_pixels;
58 uint8_t *p_y = p_src->Y_PIXELS;
59 uint8_t *p_u = p_src->U_PIXELS;
60 uint8_t *p_v = p_src->V_PIXELS;
62 vlc_bool_t b_hscale; /* horizontal scaling type */
63 int i_vscale; /* vertical scaling type */
64 unsigned int i_x, i_y; /* horizontal and vertical indexes */
65 unsigned int i_real_y; /* y % 4 */
69 int i_scale_count; /* scale modulo counter */
70 int i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
71 uint16_t * p_pic_start; /* beginning of the current line for copy */
72 int i_uval, i_vval; /* U and V samples */
73 int i_red, i_green, i_blue; /* U and V modified samples */
74 uint16_t * p_yuv = p_vout->chroma.p_sys->p_rgb16;
75 uint16_t * p_ybase; /* Y dependant conversion table */
77 /* Conversion buffer pointer */
78 uint16_t * p_buffer_start = (uint16_t*)p_vout->chroma.p_sys->p_buffer;
81 /* Offset array pointer */
82 int * p_offset_start = p_vout->chroma.p_sys->p_offset;
85 const int i_source_margin = p_src->p[0].i_pitch
86 - p_src->p[0].i_visible_pitch;
87 const int i_source_margin_c = p_src->p[1].i_pitch
88 - p_src->p[1].i_visible_pitch;
90 /* The dithering matrices */
91 int dither10[4] = { 0x0, 0x8, 0x2, 0xa };
92 int dither11[4] = { 0xc, 0x4, 0xe, 0x6 };
93 int dither12[4] = { 0x3, 0xb, 0x1, 0x9 };
94 int dither13[4] = { 0xf, 0x7, 0xd, 0x5 };
96 for(i_x = 0; i_x < 4; i_x++)
98 dither10[i_x] = dither10[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
99 dither11[i_x] = dither11[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
100 dither12[i_x] = dither12[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
101 dither13[i_x] = dither13[i_x] << (SHIFT - 4 + p_vout->output.i_rrshift);
104 i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
106 if( p_vout->render.i_width & 7 )
108 i_rewind = 8 - ( p_vout->render.i_width & 7 );
115 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
116 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
117 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
118 SetOffset( p_vout->render.i_width, p_vout->render.i_height,
119 p_vout->output.i_width, p_vout->output.i_height,
120 &b_hscale, &i_vscale, p_offset_start );
125 i_scale_count = ( i_vscale == 1 ) ?
126 p_vout->output.i_height : p_vout->render.i_height;
127 for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
129 i_real_y = i_y & 0x3;
131 p_buffer = b_hscale ? p_buffer_start : p_pic;
133 for ( i_x = p_vout->render.i_width / 8; i_x--; )
135 int *p_dither = dither10;
136 CONVERT_YUV_PIXEL_DITHER(2);
138 CONVERT_Y_PIXEL_DITHER(2);
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);
153 /* Here we do some unaligned reads and duplicate conversions, but
154 * at least we have all the pixels */
157 int *p_dither = dither10;
159 p_u -= i_rewind >> 1;
160 p_v -= i_rewind >> 1;
161 p_buffer -= i_rewind;
162 CONVERT_YUV_PIXEL_DITHER(2);
164 CONVERT_Y_PIXEL_DITHER(2);
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);
179 SCALE_HEIGHT( 420, 2 );
181 p_y += i_source_margin;
184 p_u += i_source_margin_c;
185 p_v += i_source_margin_c;
191 /*****************************************************************************
192 * I420_RGB16: color YUV 4:2:0 to RGB 16 bpp
193 *****************************************************************************
194 * Horizontal alignment needed:
195 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
196 * - output: 1 pixel (2 bytes), margins allowed
197 * Vertical alignment needed:
198 * - input: 2 lines (2 Y lines, 1 U/V line)
200 *****************************************************************************/
201 void E_(I420_RGB16)( vout_thread_t *p_vout, picture_t *p_src,
204 /* We got this one from the old arguments */
205 uint16_t *p_pic = (uint16_t*)p_dest->p->p_pixels;
206 uint8_t *p_y = p_src->Y_PIXELS;
207 uint8_t *p_u = p_src->U_PIXELS;
208 uint8_t *p_v = p_src->V_PIXELS;
210 vlc_bool_t b_hscale; /* horizontal scaling type */
211 unsigned int i_vscale; /* vertical scaling type */
212 unsigned int i_x, i_y; /* horizontal and vertical indexes */
216 int i_scale_count; /* scale modulo counter */
217 int i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
218 uint16_t * p_pic_start; /* beginning of the current line for copy */
219 #if defined (MODULE_NAME_IS_i420_rgb)
220 int i_uval, i_vval; /* U and V samples */
221 int i_red, i_green, i_blue; /* U and V modified samples */
222 uint16_t * p_yuv = p_vout->chroma.p_sys->p_rgb16;
223 uint16_t * p_ybase; /* Y dependant conversion table */
226 /* Conversion buffer pointer */
227 uint16_t * p_buffer_start = (uint16_t*)p_vout->chroma.p_sys->p_buffer;
230 /* Offset array pointer */
231 int * p_offset_start = p_vout->chroma.p_sys->p_offset;
234 const int i_source_margin = p_src->p[0].i_pitch
235 - p_src->p[0].i_visible_pitch;
236 const int i_source_margin_c = p_src->p[1].i_pitch
237 - p_src->p[1].i_visible_pitch;
239 i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
241 if( p_vout->render.i_width & 7 )
243 i_rewind = 8 - ( p_vout->render.i_width & 7 );
250 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
251 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
252 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
253 SetOffset( p_vout->render.i_width, p_vout->render.i_height,
254 p_vout->output.i_width, p_vout->output.i_height,
255 &b_hscale, &i_vscale, p_offset_start );
260 i_scale_count = ( i_vscale == 1 ) ?
261 p_vout->output.i_height : p_vout->render.i_height;
262 for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
265 p_buffer = b_hscale ? p_buffer_start : p_pic;
267 #if defined (MODULE_NAME_IS_i420_rgb)
268 for ( i_x = p_vout->render.i_width / 8; i_x--; )
270 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
271 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
272 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
273 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
275 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
276 if( p_vout->output.i_rmask == 0x7c00 )
279 for ( i_x = p_vout->render.i_width / 8; i_x--; )
282 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
288 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
299 for ( i_x = p_vout->render.i_width / 8; i_x--; )
302 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
308 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
318 /* Here we do some unaligned reads and duplicate conversions, but
319 * at least we have all the pixels */
323 p_u -= i_rewind >> 1;
324 p_v -= i_rewind >> 1;
325 p_buffer -= i_rewind;
326 #if defined (MODULE_NAME_IS_i420_rgb)
327 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
328 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
329 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
330 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
331 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
333 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
335 if( p_vout->output.i_rmask == 0x7c00 )
342 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
351 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
361 SCALE_HEIGHT( 420, 2 );
363 p_y += i_source_margin;
366 p_u += i_source_margin_c;
367 p_v += i_source_margin_c;
372 /*****************************************************************************
373 * I420_RGB32: color YUV 4:2:0 to RGB 32 bpp
374 *****************************************************************************
375 * Horizontal alignment needed:
376 * - input: 8 pixels (8 Y bytes, 4 U/V bytes), margins not allowed
377 * - output: 1 pixel (2 bytes), margins allowed
378 * Vertical alignment needed:
379 * - input: 2 lines (2 Y lines, 1 U/V line)
381 *****************************************************************************/
382 void E_(I420_RGB32)( vout_thread_t *p_vout, picture_t *p_src,
385 /* We got this one from the old arguments */
386 uint32_t *p_pic = (uint32_t*)p_dest->p->p_pixels;
387 uint8_t *p_y = p_src->Y_PIXELS;
388 uint8_t *p_u = p_src->U_PIXELS;
389 uint8_t *p_v = p_src->V_PIXELS;
391 vlc_bool_t b_hscale; /* horizontal scaling type */
392 unsigned int i_vscale; /* vertical scaling type */
393 unsigned int i_x, i_y; /* horizontal and vertical indexes */
397 int i_scale_count; /* scale modulo counter */
398 int i_chroma_width = p_vout->render.i_width / 2; /* chroma width */
399 uint32_t * p_pic_start; /* beginning of the current line for copy */
400 #if defined (MODULE_NAME_IS_i420_rgb)
401 int i_uval, i_vval; /* U and V samples */
402 int i_red, i_green, i_blue; /* U and V modified samples */
403 uint32_t * p_yuv = p_vout->chroma.p_sys->p_rgb32;
404 uint32_t * p_ybase; /* Y dependant conversion table */
407 /* Conversion buffer pointer */
408 uint32_t * p_buffer_start = (uint32_t*)p_vout->chroma.p_sys->p_buffer;
411 /* Offset array pointer */
412 int * p_offset_start = p_vout->chroma.p_sys->p_offset;
415 const int i_source_margin = p_src->p[0].i_pitch
416 - p_src->p[0].i_visible_pitch;
417 const int i_source_margin_c = p_src->p[1].i_pitch
418 - p_src->p[1].i_visible_pitch;
420 i_right_margin = p_dest->p->i_pitch - p_dest->p->i_visible_pitch;
422 if( p_vout->render.i_width & 7 )
424 i_rewind = 8 - ( p_vout->render.i_width & 7 );
431 /* Rule: when a picture of size (x1,y1) with aspect ratio r1 is rendered
432 * on a picture of size (x2,y2) with aspect ratio r2, if x1 grows to x1'
433 * then y1 grows to y1' = x1' * y2/x2 * r2/r1 */
434 SetOffset( p_vout->render.i_width, p_vout->render.i_height,
435 p_vout->output.i_width, p_vout->output.i_height,
436 &b_hscale, &i_vscale, p_offset_start );
441 i_scale_count = ( i_vscale == 1 ) ?
442 p_vout->output.i_height : p_vout->render.i_height;
443 for( i_y = 0; i_y < p_vout->render.i_height; i_y++ )
446 p_buffer = b_hscale ? p_buffer_start : p_pic;
448 for ( i_x = p_vout->render.i_width / 8; i_x--; )
450 #if defined (MODULE_NAME_IS_i420_rgb)
451 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
452 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
453 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
454 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
455 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
457 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
463 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
472 /* Here we do some unaligned reads and duplicate conversions, but
473 * at least we have all the pixels */
477 p_u -= i_rewind >> 1;
478 p_v -= i_rewind >> 1;
479 p_buffer -= i_rewind;
480 #if defined (MODULE_NAME_IS_i420_rgb)
481 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
482 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
483 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
484 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
485 #elif defined (MODULE_NAME_IS_i420_rgb_mmx)
487 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
493 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer) );
502 SCALE_HEIGHT( 420, 4 );
504 p_y += i_source_margin;
507 p_u += i_source_margin_c;
508 p_v += i_source_margin_c;
513 /* Following functions are local */
515 /*****************************************************************************
516 * SetOffset: build offset array for conversion functions
517 *****************************************************************************
518 * This function will build an offset array used in later conversion functions.
519 * It will also set horizontal and vertical scaling indicators.
520 *****************************************************************************/
521 static void SetOffset( int i_width, int i_height, int i_pic_width,
522 int i_pic_height, vlc_bool_t *pb_hscale,
523 int *pi_vscale, int *p_offset )
525 int i_x; /* x position in destination */
526 int i_scale_count; /* modulo counter */
529 * Prepare horizontal offset array
531 if( i_pic_width - i_width == 0 )
533 /* No horizontal scaling: YUV conversion is done directly to picture */
536 else if( i_pic_width - i_width > 0 )
538 /* Prepare scaling array for horizontal extension */
540 i_scale_count = i_pic_width;
541 for( i_x = i_width; i_x--; )
543 while( (i_scale_count -= i_width) > 0 )
548 i_scale_count += i_pic_width;
551 else /* if( i_pic_width - i_width < 0 ) */
553 /* Prepare scaling array for horizontal reduction */
555 i_scale_count = i_width;
556 for( i_x = i_pic_width; i_x--; )
559 while( (i_scale_count -= i_pic_width) > 0 )
564 i_scale_count += i_width;
569 * Set vertical scaling indicator
571 if( i_pic_height - i_height == 0 )
575 else if( i_pic_height - i_height > 0 )
579 else /* if( i_pic_height - i_height < 0 ) */