1 /*****************************************************************************
2 * video_yuv.c: YUV transformation functions
3 * Provides functions to perform the YUV conversion. The functions provided here
4 * are a complete and portable C implementation, and may be replaced in certain
5 * case by optimized functions.
6 *****************************************************************************
7 * Copyright (C) 1999, 2000 VideoLAN
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
31 #include <math.h> /* exp(), pow() */
32 #include <errno.h> /* ENOMEM */
33 #include <stdlib.h> /* free() */
34 #include <string.h> /* strerror() */
42 #include "video_output.h"
43 #include "video_yuv.h"
47 /*****************************************************************************
49 *****************************************************************************/
51 /* Margins and offsets in conversion tables - Margins are used in case a RGB
52 * RGB conversion would give a value outside the 0-255 range. Offsets have been
53 * calculated to avoid using the same cache line for 2 tables. conversion tables
54 * are 2*MARGIN + 256 long and stores pixels.*/
55 #define RED_MARGIN 178
56 #define GREEN_MARGIN 135
57 #define BLUE_MARGIN 224
58 #define RED_OFFSET 1501 /* 1323 to 1935 */
59 #define GREEN_OFFSET 135 /* 0 to 526 */
60 #define BLUE_OFFSET 818 /* 594 to 1298 */
61 #define RGB_TABLE_SIZE 1935 /* total table size */
63 #define GRAY_MARGIN 384
64 #define GRAY_TABLE_SIZE 1024 /* total table size */
66 #define PALETTE_TABLE_SIZE 2176 /* YUV -> 8bpp palette lookup table */
68 /* macros used for YUV pixel conversions */
70 #define U_GREEN_COEF ((int)(-0.391 * (1<<SHIFT) / 1.164))
71 #define U_BLUE_COEF ((int)(2.018 * (1<<SHIFT) / 1.164))
72 #define V_RED_COEF ((int)(1.596 * (1<<SHIFT) / 1.164))
73 #define V_GREEN_COEF ((int)(-0.813 * (1<<SHIFT) / 1.164))
77 /* hope these constant values are cache line aligned */
78 static unsigned long long mmx_80w = 0x0080008000800080;
79 static unsigned long long mmx_10w = 0x1010101010101010;
80 static unsigned long long mmx_00ffw = 0x00ff00ff00ff00ff;
81 static unsigned long long mmx_Y_coeff = 0x253f253f253f253f;
83 /* hope these constant values are cache line aligned */
84 static unsigned long long mmx_U_green = 0xf37df37df37df37d;
85 static unsigned long long mmx_U_blue = 0x4093409340934093;
86 static unsigned long long mmx_V_red = 0x3312331233123312;
87 static unsigned long long mmx_V_green = 0xe5fce5fce5fce5fc;
89 /* hope these constant values are cache line aligned */
90 static unsigned long long mmx_redmask = 0xf8f8f8f8f8f8f8f8;
91 static unsigned long long mmx_grnmask = 0xfcfcfcfcfcfcfcfc;
92 static unsigned long long mmx_grnshift = 0x03;
93 static unsigned long long mmx_blueshift = 0x03;
96 /*****************************************************************************
98 *****************************************************************************/
99 static void SetGammaTable ( int *pi_table, double f_gamma );
100 static void SetYUV ( vout_thread_t *p_vout );
101 static void SetOffset ( int i_width, int i_height, int i_pic_width, int i_pic_height,
102 boolean_t *pb_h_scaling, int *pi_v_scaling, int *p_offset );
104 static void ConvertY4Gray8 ( p_vout_thread_t p_vout, u8 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
105 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
106 int i_matrix_coefficients );
107 static void ConvertY4Gray16 ( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
108 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
109 int i_matrix_coefficients );
110 static void ConvertY4Gray24 ( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
111 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
112 int i_matrix_coefficients );
113 static void ConvertY4Gray32 ( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
114 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
115 int i_matrix_coefficients );
116 static void ConvertYUV420RGB8 ( p_vout_thread_t p_vout, u8 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
117 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
118 int i_matrix_coefficients );
119 static void ConvertYUV422RGB8 ( p_vout_thread_t p_vout, u8 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
120 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
121 int i_matrix_coefficients );
122 static void ConvertYUV444RGB8 ( p_vout_thread_t p_vout, u8 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
123 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
124 int i_matrix_coefficients );
125 static void ConvertYUV420RGB16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
126 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
127 int i_matrix_coefficients );
128 static void ConvertYUV422RGB16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
129 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
130 int i_matrix_coefficients );
131 static void ConvertYUV444RGB16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
132 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
133 int i_matrix_coefficients );
134 static void ConvertYUV420RGB24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
135 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
136 int i_matrix_coefficients );
137 static void ConvertYUV422RGB24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
138 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
139 int i_matrix_coefficients );
140 static void ConvertYUV444RGB24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
141 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
142 int i_matrix_coefficients );
143 static void ConvertYUV420RGB32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
144 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
145 int i_matrix_coefficients );
146 static void ConvertYUV422RGB32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
147 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
148 int i_matrix_coefficients );
149 static void ConvertYUV444RGB32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
150 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
151 int i_matrix_coefficients );
153 /*****************************************************************************
154 * CONVERT_YUV_PIXEL, CONVERT_Y_PIXEL: pixel conversion blocks
155 *****************************************************************************
156 * These conversion routines are used by YUV conversion functions.
157 * conversion are made from p_y, p_u, p_v, which are modified, to p_buffer,
158 * which is also modified.
159 *****************************************************************************/
160 #define CONVERT_Y_PIXEL( BPP ) \
161 /* Only Y sample is present */ \
162 p_ybase = p_yuv + *p_y++; \
163 *p_buffer++ = p_ybase[RED_OFFSET-((V_RED_COEF*128)>>SHIFT) + i_red] | \
164 p_ybase[GREEN_OFFSET-(((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) \
165 + i_green ] | p_ybase[BLUE_OFFSET-((U_BLUE_COEF*128)>>SHIFT) + i_blue];
167 #define CONVERT_YUV_PIXEL( BPP ) \
168 /* Y, U and V samples are present */ \
171 i_red = (V_RED_COEF * i_vval) >> SHIFT; \
172 i_green = (U_GREEN_COEF * i_uval + V_GREEN_COEF * i_vval) >> SHIFT; \
173 i_blue = (U_BLUE_COEF * i_uval) >> SHIFT; \
174 CONVERT_Y_PIXEL( BPP ) \
176 /*****************************************************************************
177 * CONVERT_4YUV_PIXELS, CONVERT_4YUV_PIXELS_SCALE: dither 4 pixels in 8 bpp
178 *****************************************************************************
179 * These macros dither 4 pixels in 8 bpp, with or without horiz. scaling
180 *****************************************************************************/
181 #define CONVERT_4YUV_PIXELS( CHROMA ) \
182 *p_pic++ = p_lookup[ \
183 (((*p_y++ + dither10[i_real_y]) >> 4) << 7) \
184 + ((*p_u + dither20[i_real_y]) >> 5) * 9 \
185 + ((*p_v + dither20[i_real_y]) >> 5) ]; \
186 *p_pic++ = p_lookup[ \
187 (((*p_y++ + dither11[i_real_y]) >> 4) << 7) \
188 + ((*p_u++ + dither21[i_real_y]) >> 5) * 9 \
189 + ((*p_v++ + dither21[i_real_y]) >> 5) ]; \
190 *p_pic++ = p_lookup[ \
191 (((*p_y++ + dither12[i_real_y]) >> 4) << 7) \
192 + ((*p_u + dither22[i_real_y]) >> 5) * 9 \
193 + ((*p_v + dither22[i_real_y]) >> 5) ]; \
194 *p_pic++ = p_lookup[ \
195 (((*p_y++ + dither13[i_real_y]) >> 4) << 7) \
196 + ((*p_u++ + dither23[i_real_y]) >> 5) * 9 \
197 + ((*p_v++ + dither23[i_real_y]) >> 5) ]; \
199 #define CONVERT_4YUV_PIXELS_SCALE( CHROMA ) \
200 *p_pic++ = p_lookup[ \
201 (((*p_y + dither10[i_real_y]) >> 4) << 7) \
202 + ((*p_u + dither20[i_real_y]) >> 5) * 9 \
203 + ((*p_v + dither20[i_real_y]) >> 5) ]; \
204 b_jump_uv += *p_offset; \
206 p_u += *p_offset & b_jump_uv; \
207 p_v += *p_offset++ & b_jump_uv; \
208 *p_pic++ = p_lookup[ \
209 (((*p_y + dither11[i_real_y]) >> 4) << 7) \
210 + ((*p_u + dither21[i_real_y]) >> 5) * 9 \
211 + ((*p_v + dither21[i_real_y]) >> 5) ]; \
212 b_jump_uv += *p_offset; \
214 p_u += *p_offset & b_jump_uv; \
215 p_v += *p_offset++ & b_jump_uv; \
216 *p_pic++ = p_lookup[ \
217 (((*p_y + dither12[i_real_y]) >> 4) << 7) \
218 + ((*p_u + dither22[i_real_y]) >> 5) * 9 \
219 + ((*p_v + dither22[i_real_y])
\f>> 5) ]; \
220 b_jump_uv += *p_offset; \
222 p_u += *p_offset & b_jump_uv; \
223 p_v += *p_offset++ & b_jump_uv; \
224 *p_pic++ = p_lookup[ \
225 (((*p_y + dither13[i_real_y]) >> 4) << 7) \
226 + ((*p_u + dither23[i_real_y]) >> 5) * 9 \
227 + ((*p_v + dither23[i_real_y]) >> 5) ]; \
228 b_jump_uv += *p_offset; \
230 p_u += *p_offset & b_jump_uv; \
231 p_v += *p_offset++ & b_jump_uv; \
233 /*****************************************************************************
234 * SCALE_WIDTH: scale a line horizontally
235 *****************************************************************************
236 * This macro scales a line using rendering buffer and offset array. It works
237 * for 1, 2 and 4 Bpp.
238 *****************************************************************************/
239 #define SCALE_WIDTH \
240 if( b_horizontal_scaling ) \
242 /* Horizontal scaling, conversion has been done to buffer. \
243 * Rewind buffer and offset, then copy and scale line */ \
244 p_buffer = p_buffer_start; \
245 p_offset = p_offset_start; \
246 for( i_x = i_pic_width / 16; i_x--; ) \
248 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
249 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
250 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
251 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
252 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
253 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
254 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
255 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
256 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
257 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
258 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
259 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
260 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
261 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
262 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
263 *p_pic++ = *p_buffer; p_buffer += *p_offset++; \
265 p_pic += i_pic_line_width; \
269 /* No scaling, conversion has been done directly in picture memory. \
270 * Increment of picture pointer to end of line is still needed */ \
271 p_pic += i_pic_width + i_pic_line_width; \
275 /*****************************************************************************
276 * SCALE_WIDTH_DITHER: scale a line horizontally for dithered 8 bpp
277 *****************************************************************************
278 * This macro scales a line using an offset array.
279 *****************************************************************************/
280 #define SCALE_WIDTH_DITHER( CHROMA ) \
281 if( b_horizontal_scaling ) \
283 /* Horizontal scaling, but we can't use a buffer due to dither */ \
284 p_offset = p_offset_start; \
286 for( i_x = i_pic_width / 16; i_x--; ) \
288 CONVERT_4YUV_PIXELS_SCALE( CHROMA ) \
289 CONVERT_4YUV_PIXELS_SCALE( CHROMA ) \
290 CONVERT_4YUV_PIXELS_SCALE( CHROMA ) \
291 CONVERT_4YUV_PIXELS_SCALE( CHROMA ) \
296 for( i_x = i_width / 16; i_x--; ) \
298 CONVERT_4YUV_PIXELS( CHROMA ) \
299 CONVERT_4YUV_PIXELS( CHROMA ) \
300 CONVERT_4YUV_PIXELS( CHROMA ) \
301 CONVERT_4YUV_PIXELS( CHROMA ) \
304 /* Increment of picture pointer to end of line is still needed */ \
305 p_pic += i_pic_line_width; \
306 i_real_y = (i_real_y + 1) & 0x3; \
308 /*****************************************************************************
309 * SCALE_HEIGHT: handle vertical scaling
310 *****************************************************************************
311 * This macro handle vertical scaling for a picture. CHROMA may be 420, 422 or
312 * 444 for RGB conversion, or 400 for gray conversion. It works for 1, 2, 3
314 *****************************************************************************/
315 #define SCALE_HEIGHT( CHROMA, BPP ) \
317 /* If line is odd, rewind 4:2:0 U and V samples */ \
318 if( ((CHROMA == 420) || (CHROMA == 422)) && !(i_y & 0x1) ) \
320 p_u -= i_chroma_width; \
321 p_v -= i_chroma_width; \
325 * Handle vertical scaling. The current line can be copied or next one \
328 switch( i_vertical_scaling ) \
330 case -1: /* vertical scaling factor is < 1 */ \
333 while( (i_scale_count -= i_pic_height) >= 0 ) \
335 /* Height reduction: skip next source line */ \
337 if( (CHROMA == 420) || (CHROMA == 422) ) \
339 if( (i_scale_count -= i_pic_height) >= 0 ) \
343 p_u += i_chroma_width; \
344 p_v += i_chroma_width; \
353 else if( CHROMA == 444 ) \
363 if( CHROMA == 420 || CHROMA == 422 ) \
365 p_u -= i_chroma_width; \
366 p_v -= i_chroma_width; \
368 while( (i_scale_count -= i_pic_height) >= 0 ) \
370 /* Height reduction: skip next source line */ \
372 if( (CHROMA == 420) || (CHROMA == 422) ) \
374 p_u += i_chroma_width; \
375 p_v += i_chroma_width; \
376 if( (i_scale_count -= i_pic_height) >= 0 ) \
388 else if( CHROMA == 444 ) \
396 i_scale_count += i_height; \
398 case 1: /* vertical scaling factor is > 1 */ \
399 while( (i_scale_count -= i_height) > 0 ) \
401 /* Height increment: copy previous picture line */ \
402 for( i_x = i_pic_width >> 4; i_x--; ) \
404 *(((u64 *) p_pic)++) = *(((u64 *) p_pic_start)++ ); \
405 *(((u64 *) p_pic)++) = *(((u64 *) p_pic_start)++ ); \
406 if( BPP > 1 ) /* 2, 3, 4 Bpp */ \
408 *(((u64 *) p_pic)++) = *(((u64 *) p_pic_start)++ ); \
409 *(((u64 *) p_pic)++) = *(((u64 *) p_pic_start)++ ); \
411 if( BPP > 2 ) /* 3, 4 Bpp */ \
413 *(((u64 *) p_pic)++) = *(((u64 *) p_pic_start)++ ); \
414 *(((u64 *) p_pic)++) = *(((u64 *) p_pic_start)++ ); \
416 if( BPP > 3 ) /* 4 Bpp */ \
418 *(((u64 *) p_pic)++) = *(((u64 *) p_pic_start)++ ); \
419 *(((u64 *) p_pic)++) = *(((u64 *) p_pic_start)++ ); \
422 p_pic += i_pic_line_width; \
423 p_pic_start += i_pic_line_width; \
425 i_scale_count += i_pic_height; \
429 /*****************************************************************************
430 * SCALE_HEIGHT_DITHER: handle vertical scaling for dithered 8 bpp
431 *****************************************************************************
432 * This macro handles vertical scaling for a picture. CHROMA may be 420, 422 or
433 * 444 for RGB conversion, or 400 for gray conversion.
434 *****************************************************************************/
435 #define SCALE_HEIGHT_DITHER( CHROMA ) \
437 /* If line is odd, rewind 4:2:0 U and V samples */ \
438 if( ((CHROMA == 420) || (CHROMA == 422)) && !(i_y & 0x1) ) \
440 p_u -= i_chroma_width; \
441 p_v -= i_chroma_width; \
445 * Handle vertical scaling. The current line can be copied or next one \
448 switch( i_vertical_scaling ) \
450 case -1: /* vertical scaling factor is < 1 */ \
451 while( (i_scale_count -= i_pic_height) >= 0 ) \
453 /* Height reduction: skip next source line */ \
456 if( (CHROMA == 420) || (CHROMA == 422) ) \
460 p_u += i_chroma_width; \
461 p_v += i_chroma_width; \
464 else if( CHROMA == 444 ) \
470 i_scale_count += i_height; \
472 case 1: /* vertical scaling factor is > 1 */ \
473 while( (i_scale_count -= i_height) > 0 ) \
475 SCALE_WIDTH_DITHER( CHROMA ); \
477 p_u -= i_chroma_width; \
478 p_v -= i_chroma_width; \
479 p_pic += i_pic_line_width; \
481 i_scale_count += i_pic_height; \
485 /*****************************************************************************
486 * vout_InitYUV: allocate and initialize translations tables
487 *****************************************************************************
488 * This function will allocate memory to store translation tables, depending
489 * of the screen depth.
490 *****************************************************************************/
491 int vout_InitYUV( vout_thread_t *p_vout )
493 size_t tables_size; /* tables size, in bytes */
495 /* Computes tables size - 3 Bpp use 32 bits pixel entries in tables */
496 switch( p_vout->i_bytes_per_pixel )
499 tables_size = sizeof( u8 ) * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : PALETTE_TABLE_SIZE);
502 tables_size = sizeof( u16 ) * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
507 tables_size = sizeof( u32 ) * (p_vout->b_grayscale ? GRAY_TABLE_SIZE : RGB_TABLE_SIZE);
511 /* Allocate memory */
512 p_vout->yuv.p_base = malloc( tables_size );
513 if( p_vout->yuv.p_base == NULL )
515 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
519 /* Allocate memory for conversion buffer and offset array */
520 p_vout->yuv.p_buffer = malloc( VOUT_MAX_WIDTH * p_vout->i_bytes_per_pixel );
521 if( p_vout->yuv.p_buffer == NULL )
523 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
524 free( p_vout->yuv.p_base );
527 p_vout->yuv.p_offset = malloc( p_vout->i_width * sizeof( int ) );
528 if( p_vout->yuv.p_offset == NULL )
530 intf_ErrMsg("error: %s\n", strerror(ENOMEM));
531 free( p_vout->yuv.p_base );
532 free( p_vout->yuv.p_buffer );
536 /* Initialize tables */
541 /*****************************************************************************
542 * vout_ResetTables: re-initialize translations tables
543 *****************************************************************************
544 * This function will initialize the tables allocated by vout_CreateTables and
545 * set functions pointers.
546 *****************************************************************************/
547 int vout_ResetYUV( vout_thread_t *p_vout )
549 vout_EndYUV( p_vout );
550 return( vout_InitYUV( p_vout ) );
553 /*****************************************************************************
554 * vout_EndYUV: destroy translations tables
555 *****************************************************************************
556 * Free memory allocated by vout_CreateTables.
557 *****************************************************************************/
558 void vout_EndYUV( vout_thread_t *p_vout )
560 free( p_vout->yuv.p_base );
561 free( p_vout->yuv.p_buffer );
562 free( p_vout->yuv.p_offset );
565 /* following functions are local */
567 /*****************************************************************************
568 * SetGammaTable: return intensity table transformed by gamma curve.
569 *****************************************************************************
570 * pi_table is a table of 256 entries from 0 to 255.
571 *****************************************************************************/
572 static void SetGammaTable( int *pi_table, double f_gamma )
574 int i_y; /* base intensity */
576 /* Use exp(gamma) instead of gamma */
577 f_gamma = exp( f_gamma );
579 /* Build gamma table */
580 for( i_y = 0; i_y < 256; i_y++ )
582 pi_table[ i_y ] = pow( (double)i_y / 256, f_gamma ) * 256;
586 /*****************************************************************************
587 * SetYUV: compute tables and set function pointers
588 + *****************************************************************************/
589 static void SetYUV( vout_thread_t *p_vout )
591 int pi_gamma[256]; /* gamma table */
592 int i_index; /* index in tables */
594 /* Build gamma table */
595 SetGammaTable( pi_gamma, p_vout->f_gamma );
598 * Set pointers and build YUV tables
600 if( p_vout->b_grayscale )
602 /* Grayscale: build gray table */
603 switch( p_vout->i_bytes_per_pixel )
607 u16 bright[256], transp[256];
609 p_vout->yuv.yuv.p_gray8 = (u8 *)p_vout->yuv.p_base + GRAY_MARGIN;
610 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
612 p_vout->yuv.yuv.p_gray8[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
613 p_vout->yuv.yuv.p_gray8[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
615 for( i_index = 0; i_index < 256; i_index++)
617 p_vout->yuv.yuv.p_gray8[ i_index ] = pi_gamma[ i_index ];
618 bright[ i_index ] = i_index << 8;
619 transp[ i_index ] = 0;
621 /* the colors have been allocated, we can set the palette */
622 p_vout->p_set_palette( p_vout, bright, bright, bright, transp );
623 p_vout->i_white_pixel = 0xff;
624 p_vout->i_black_pixel = 0x00;
625 p_vout->i_gray_pixel = 0x44;
626 p_vout->i_blue_pixel = 0x3b;
631 p_vout->yuv.yuv.p_gray16 = (u16 *)p_vout->yuv.p_base + GRAY_MARGIN;
632 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
634 p_vout->yuv.yuv.p_gray16[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
635 p_vout->yuv.yuv.p_gray16[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
637 for( i_index = 0; i_index < 256; i_index++)
639 p_vout->yuv.yuv.p_gray16[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
644 p_vout->yuv.yuv.p_gray32 = (u32 *)p_vout->yuv.p_base + GRAY_MARGIN;
645 for( i_index = 0; i_index < GRAY_MARGIN; i_index++ )
647 p_vout->yuv.yuv.p_gray32[ -i_index ] = RGB2PIXEL( p_vout, pi_gamma[0], pi_gamma[0], pi_gamma[0] );
648 p_vout->yuv.yuv.p_gray32[ 256 + i_index ] = RGB2PIXEL( p_vout, pi_gamma[255], pi_gamma[255], pi_gamma[255] );
650 for( i_index = 0; i_index < 256; i_index++)
652 p_vout->yuv.yuv.p_gray32[ i_index ] = RGB2PIXEL( p_vout, pi_gamma[i_index], pi_gamma[i_index], pi_gamma[i_index] );
659 /* Color: build red, green and blue tables */
660 switch( p_vout->i_bytes_per_pixel )
666 #define CLIP( x ) ( ((x < 0) ? 0 : (x > 255) ? 255 : x) << 8 )
672 u16 red[256], green[256], blue[256], transp[256];
673 unsigned char lookup[PALETTE_TABLE_SIZE];
675 p_vout->yuv.yuv.p_rgb8 = (u8 *)p_vout->yuv.p_base;
677 /* this loop calculates the intersection of an YUV box
678 * and the RGB cube. */
679 for ( y = 0; y <= 256; y += 16 )
681 for ( u = 0; u <= 256; u += 32 )
682 for ( v = 0; v <= 256; v += 32 )
684 uvr = (V_RED_COEF*(v-128)) >> SHIFT;
685 uvg = (U_GREEN_COEF*(u-128) + V_GREEN_COEF*(v-128)) >> SHIFT;
686 uvb = (U_BLUE_COEF*(u-128)) >> SHIFT;
691 if( r >= RGB_MIN && g >= RGB_MIN && b >= RGB_MIN
692 && r <= RGB_MAX && g <= RGB_MAX && b <= RGB_MAX )
694 /* this one should never happen unless someone fscked up my code */
695 if(j == 256) { intf_ErrMsg( "vout error: no colors left to build palette\n" ); break; }
697 /* clip the colors */
699 green[j] = CLIP( g );
705 p_vout->yuv.yuv.p_rgb8[i++] = j;
711 p_vout->yuv.yuv.p_rgb8[i++] = 0;
717 /* the colors have been allocated, we can set the palette */
718 /* there will eventually be a way to know which colors
719 * couldn't be allocated and try to find a replacement */
720 p_vout->p_set_palette( p_vout, red, green, blue, transp );
722 p_vout->i_white_pixel = 0xff;
723 p_vout->i_black_pixel = 0x00;
724 p_vout->i_gray_pixel = 0x44;
725 p_vout->i_blue_pixel = 0x3b;
728 /* this loop allocates colors that got outside
730 for ( y = 0; y <= 256; y += 16 )
732 for ( u = 0; u <= 256; u += 32 )
733 for ( v = 0; v <= 256; v += 32 )
736 int dist, mindist = 100000000;
738 if( lookup[i] || y==0)
745 for( u2 = 0; u2 <= 256; u2 += 32 )
746 for( v2 = 0; v2 <= 256; v2 += 32 )
748 j = ((y>>4)<<7) + (u2>>5)*9 + (v2>>5);
749 dist = (u-u2)*(u-u2) + (v-v2)*(v-v2);
751 /* find the nearest color */
754 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
759 /* find the nearest color */
760 if( dist + 128 < mindist )
762 p_vout->yuv.yuv.p_rgb8[i] = p_vout->yuv.yuv.p_rgb8[j];
763 mindist = dist + 128;
774 p_vout->yuv.yuv.p_rgb16 = (u16 *)p_vout->yuv.p_base;
775 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
777 p_vout->yuv.yuv.p_rgb16[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
778 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
780 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
782 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
783 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
785 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
787 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
788 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
790 for( i_index = 0; i_index < 256; i_index++ )
792 p_vout->yuv.yuv.p_rgb16[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
793 p_vout->yuv.yuv.p_rgb16[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
794 p_vout->yuv.yuv.p_rgb16[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
799 p_vout->yuv.yuv.p_rgb32 = (u32 *)p_vout->yuv.p_base;
800 for( i_index = 0; i_index < RED_MARGIN; i_index++ )
802 p_vout->yuv.yuv.p_rgb32[RED_OFFSET - RED_MARGIN + i_index] = RGB2PIXEL( p_vout, pi_gamma[0], 0, 0 );
803 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, pi_gamma[255], 0, 0 );
805 for( i_index = 0; i_index < GREEN_MARGIN; i_index++ )
807 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET - GREEN_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[0], 0 );
808 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + 256 + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[255], 0 );
810 for( i_index = 0; i_index < BLUE_MARGIN; i_index++ )
812 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET - BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[0] );
813 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + BLUE_MARGIN + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[255] );
815 for( i_index = 0; i_index < 256; i_index++ )
817 p_vout->yuv.yuv.p_rgb32[RED_OFFSET + i_index] = RGB2PIXEL( p_vout, pi_gamma[ i_index ], 0, 0 );
818 p_vout->yuv.yuv.p_rgb32[GREEN_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, pi_gamma[ i_index ], 0 );
819 p_vout->yuv.yuv.p_rgb32[BLUE_OFFSET + i_index] = RGB2PIXEL( p_vout, 0, 0, pi_gamma[ i_index ] );
826 * Set functions pointers
828 if( p_vout->b_grayscale )
831 switch( p_vout->i_bytes_per_pixel )
834 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray8;
835 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray8;
836 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray8;
839 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray16;
840 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray16;
841 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray16;
844 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray24;
845 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray24;
846 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray24;
849 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertY4Gray32;
850 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertY4Gray32;
851 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertY4Gray32;
858 switch( p_vout->i_bytes_per_pixel )
861 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB8;
862 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB8;
863 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB8;
866 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB16;
867 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB16;
868 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB16;
871 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB24;
872 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB24;
873 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB24;
876 p_vout->yuv.p_Convert420 = (vout_yuv_convert_t *) ConvertYUV420RGB32;
877 p_vout->yuv.p_Convert422 = (vout_yuv_convert_t *) ConvertYUV422RGB32;
878 p_vout->yuv.p_Convert444 = (vout_yuv_convert_t *) ConvertYUV444RGB32;
884 /*****************************************************************************
885 * SetOffset: build offset array for conversion functions
886 *****************************************************************************
887 * This function will build an offset array used in later conversion functions.
888 * It will also set horizontal and vertical scaling indicators.
889 *****************************************************************************/
890 static void SetOffset( int i_width, int i_height, int i_pic_width, int i_pic_height,
891 boolean_t *pb_h_scaling, int *pi_v_scaling, int *p_offset )
893 int i_x; /* x position in destination */
894 int i_scale_count; /* modulo counter */
897 * Prepare horizontal offset array
899 if( i_pic_width - i_width > 0 )
901 /* Prepare scaling array for horizontal extension */
903 i_scale_count = i_pic_width;
904 for( i_x = i_width; i_x--; )
906 while( (i_scale_count -= i_width) > 0 )
911 i_scale_count += i_pic_width;
914 else if( i_pic_width - i_width < 0 )
916 /* Prepare scaling array for horizontal reduction */
918 i_scale_count = i_pic_width;
919 for( i_x = i_pic_width; i_x--; )
922 while( (i_scale_count -= i_pic_width) >= 0 )
927 i_scale_count += i_width;
932 /* No horizontal scaling: YUV conversion is done directly to picture */
937 * Set vertical scaling indicator
939 if( i_pic_height - i_height > 0 )
943 else if( i_pic_height - i_height < 0 )
953 /*****************************************************************************
954 * ConvertY4Gray8: grayscale YUV 4:x:x to RGB 8 bpp
955 *****************************************************************************/
956 static void ConvertY4Gray8( p_vout_thread_t p_vout, u8 *p_pic, yuv_data_t *p_y,
957 yuv_data_t *p_u, yuv_data_t *p_v, int i_width,
958 int i_height, int i_pic_width, int i_pic_height,
959 int i_pic_line_width, int i_matrix_coefficients )
961 boolean_t b_horizontal_scaling; /* horizontal scaling type */
962 int i_vertical_scaling; /* vertical scaling type */
963 int i_x, i_y; /* horizontal and vertical indexes */
964 int i_scale_count; /* scale modulo counter */
965 int i_chroma_width; /* chroma width, not used */
966 u8 * p_gray; /* base conversion table */
967 u8 * p_pic_start; /* beginning of the current line for copy */
968 u8 * p_buffer_start; /* conversion buffer start */
969 u8 * p_buffer; /* conversion buffer pointer */
970 int * p_offset_start; /* offset array start */
971 int * p_offset; /* offset array pointer */
974 * Initialize some values - i_pic_line_width will store the line skip
976 i_pic_line_width -= i_pic_width;
977 p_gray = p_vout->yuv.yuv.p_gray8;
978 p_buffer_start = p_vout->yuv.p_buffer;
979 p_offset_start = p_vout->yuv.p_offset;
980 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
981 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
986 i_scale_count = i_pic_height;
987 for( i_y = 0; i_y < i_height; i_y++ )
989 /* Mark beginnning of line for possible later line copy, and initialize
992 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
994 /* Do YUV conversion to buffer - YUV picture is always formed of 16
995 * pixels wide blocks */
996 for( i_x = i_width / 16; i_x--; )
998 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
999 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1000 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1001 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1002 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1003 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1004 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1005 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1008 /* Do horizontal and vertical scaling */
1010 SCALE_HEIGHT(400, 1);
1014 /*****************************************************************************
1015 * ConvertY4Gray16: grayscale YUV 4:x:x to RGB 2 Bpp
1016 *****************************************************************************/
1017 static void ConvertY4Gray16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1018 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1019 int i_matrix_coefficients )
1021 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1022 int i_vertical_scaling; /* vertical scaling type */
1023 int i_x, i_y; /* horizontal and vertical indexes */
1024 int i_scale_count; /* scale modulo counter */
1025 int i_chroma_width; /* chroma width, not used */
1026 u16 * p_gray; /* base conversion table */
1027 u16 * p_pic_start; /* beginning of the current line for copy */
1028 u16 * p_buffer_start; /* conversion buffer start */
1029 u16 * p_buffer; /* conversion buffer pointer */
1030 int * p_offset_start; /* offset array start */
1031 int * p_offset; /* offset array pointer */
1034 * Initialize some values - i_pic_line_width will store the line skip
1036 i_pic_line_width -= i_pic_width;
1037 p_gray = p_vout->yuv.yuv.p_gray16;
1038 p_buffer_start = p_vout->yuv.p_buffer;
1039 p_offset_start = p_vout->yuv.p_offset;
1040 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1041 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1044 * Perform conversion
1046 i_scale_count = i_pic_height;
1047 for( i_y = 0; i_y < i_height; i_y++ )
1049 /* Mark beginnning of line for possible later line copy, and initialize
1051 p_pic_start = p_pic;
1052 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1054 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1055 * pixels wide blocks */
1056 for( i_x = i_width / 16; i_x--; )
1058 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1059 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1060 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1061 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1062 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1063 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1064 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1065 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1068 /* Do horizontal and vertical scaling */
1070 SCALE_HEIGHT(400, 2);
1074 /*****************************************************************************
1075 * ConvertY4Gray24: grayscale YUV 4:x:x to RGB 3 Bpp
1076 *****************************************************************************/
1077 static void ConvertY4Gray24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1078 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1079 int i_matrix_coefficients )
1084 /*****************************************************************************
1085 * ConvertY4Gray32: grayscale YUV 4:x:x to RGB 4 Bpp
1086 *****************************************************************************/
1087 static void ConvertY4Gray32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1088 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1089 int i_matrix_coefficients )
1091 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1092 int i_vertical_scaling; /* vertical scaling type */
1093 int i_x, i_y; /* horizontal and vertical indexes */
1094 int i_scale_count; /* scale modulo counter */
1095 int i_chroma_width; /* chroma width, not used */
1096 u32 * p_gray; /* base conversion table */
1097 u32 * p_pic_start; /* beginning of the current line for copy */
1098 u32 * p_buffer_start; /* conversion buffer start */
1099 u32 * p_buffer; /* conversion buffer pointer */
1100 int * p_offset_start; /* offset array start */
1101 int * p_offset; /* offset array pointer */
1104 * Initialize some values - i_pic_line_width will store the line skip
1106 i_pic_line_width -= i_pic_width;
1107 p_gray = p_vout->yuv.yuv.p_gray32;
1108 p_buffer_start = p_vout->yuv.p_buffer;
1109 p_offset_start = p_vout->yuv.p_offset;
1110 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1111 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1114 * Perform conversion
1116 i_scale_count = i_pic_height;
1117 for( i_y = 0; i_y < i_height; i_y++ )
1119 /* Mark beginnning of line for possible later line copy, and initialize
1121 p_pic_start = p_pic;
1122 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1124 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1125 * pixels wide blocks */
1126 for( i_x = i_width / 16; i_x--; )
1128 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1129 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1130 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1131 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1132 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1133 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1134 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1135 *p_buffer++ = p_gray[ *p_y++ ]; *p_buffer++ = p_gray[ *p_y++ ];
1138 /* Do horizontal and vertical scaling */
1140 SCALE_HEIGHT(400, 4);
1144 /*****************************************************************************
1145 * ConvertYUV420RGB8: color YUV 4:2:0 to RGB 8 bpp
1146 *****************************************************************************/
1147 static void ConvertYUV420RGB8( p_vout_thread_t p_vout, u8 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1148 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1149 int i_matrix_coefficients )
1151 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1152 int i_vertical_scaling; /* vertical scaling type */
1153 int i_x, i_y; /* horizontal and vertical indexes */
1154 int i_scale_count; /* scale modulo counter */
1155 int b_jump_uv; /* should we jump u and v ? */
1156 int i_real_y; /* y % 4 */
1157 u8 * p_lookup; /* lookup table */
1158 int i_chroma_width; /* chroma width */
1159 int * p_offset_start; /* offset array start */
1160 int * p_offset; /* offset array pointer */
1162 int dither10[4] = { 0x0, 0x8, 0x2, 0xa };
1163 int dither11[4] = { 0xc, 0x4, 0xe, 0x6 };
1164 int dither12[4] = { 0x3, 0xb, 0x1, 0x9 };
1165 int dither13[4] = { 0xf, 0x7, 0xd, 0x5 };
1167 int dither20[4] = { 0x0, 0x10, 0x4, 0x14 };
1168 int dither21[4] = { 0x18, 0x8, 0x1c, 0xc };
1169 int dither22[4] = { 0x6, 0x16, 0x2, 0x12 };
1170 int dither23[4] = { 0x1e, 0xe, 0x1a, 0xa };
1173 /* other matrices that can be interesting, either for debugging or for
1174 * various effects */
1175 int dither[4][4] = { { 0, 8, 2, 10 }, { 12, 4, 14, 16 }, { 3, 11, 1, 9}, {15, 7, 13, 5} };
1176 int dither[4][4] = { { 7, 8, 0, 15 }, { 0, 15, 8, 7 }, { 7, 0, 15, 8 }, { 15, 7, 8, 0 } };
1177 int dither[4][4] = { { 0, 15, 0, 15 }, { 15, 0, 15, 0 }, { 0, 15, 0, 15 }, { 15, 0, 15, 0 } };
1178 int dither[4][4] = { { 15, 15, 0, 0 }, { 15, 15, 0, 0 }, { 0, 0, 15, 15 }, { 0, 0, 15, 15 } };
1179 int dither[4][4] = { { 8, 8, 8, 8 }, { 8, 8, 8, 8 }, { 8, 8, 8, 8 }, { 8, 8, 8, 8 } };
1180 int dither[4][4] = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, { 12, 13, 14, 15 } };
1184 * Initialize some values - i_pic_line_width will store the line skip
1186 i_pic_line_width -= i_pic_width;
1187 i_chroma_width = i_width / 2;
1188 p_offset_start = p_vout->yuv.p_offset;
1189 p_lookup = p_vout->yuv.p_base;
1190 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1191 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1194 * Perform conversion
1196 i_scale_count = i_pic_height;
1198 for( i_y = 0; i_y < i_height; i_y++ )
1200 /* Do horizontal and vertical scaling */
1201 SCALE_WIDTH_DITHER( 420 );
1202 SCALE_HEIGHT_DITHER( 420 );
1206 /*****************************************************************************
1207 * ConvertYUV422RGB8: color YUV 4:2:2 to RGB 8 bpp
1208 *****************************************************************************/
1209 static void ConvertYUV422RGB8( p_vout_thread_t p_vout, u8 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1210 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1211 int i_matrix_coefficients )
1213 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1214 int i_vertical_scaling; /* vertical scaling type */
1215 int i_x, i_y; /* horizontal and vertical indexes */
1216 int i_scale_count; /* scale modulo counter */
1217 int i_uval, i_vval; /* U and V samples */
1218 int i_red, i_green, i_blue; /* U and V modified samples */
1219 int i_chroma_width; /* chroma width */
1220 u8 * p_yuv; /* base conversion table */
1221 u8 * p_ybase; /* Y dependant conversion table */
1222 u8 * p_pic_start; /* beginning of the current line for copy */
1223 u8 * p_buffer_start; /* conversion buffer start */
1224 u8 * p_buffer; /* conversion buffer pointer */
1225 int * p_offset_start; /* offset array start */
1226 int * p_offset; /* offset array pointer */
1229 * Initialize some values - i_pic_line_width will store the line skip
1231 i_pic_line_width -= i_pic_width;
1232 i_chroma_width = i_width / 2;
1233 p_yuv = p_vout->yuv.yuv.p_rgb8;
1234 p_buffer_start = p_vout->yuv.p_buffer;
1235 p_offset_start = p_vout->yuv.p_offset;
1236 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1237 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1240 * Perform conversion
1242 i_scale_count = i_pic_height;
1243 for( i_y = 0; i_y < i_height; i_y++ )
1245 /* Mark beginnning of line for possible later line copy, and initialize
1247 p_pic_start = p_pic;
1248 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1250 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1251 * pixels wide blocks */
1252 for( i_x = i_width / 16; i_x--; )
1254 CONVERT_YUV_PIXEL(1); CONVERT_Y_PIXEL(1);
1255 CONVERT_YUV_PIXEL(1); CONVERT_Y_PIXEL(1);
1256 CONVERT_YUV_PIXEL(1); CONVERT_Y_PIXEL(1);
1257 CONVERT_YUV_PIXEL(1); CONVERT_Y_PIXEL(1);
1258 CONVERT_YUV_PIXEL(1); CONVERT_Y_PIXEL(1);
1259 CONVERT_YUV_PIXEL(1); CONVERT_Y_PIXEL(1);
1260 CONVERT_YUV_PIXEL(1); CONVERT_Y_PIXEL(1);
1261 CONVERT_YUV_PIXEL(1); CONVERT_Y_PIXEL(1);
1264 /* Do horizontal and vertical scaling */
1266 SCALE_HEIGHT(422, 1);
1270 /*****************************************************************************
1271 * ConvertYUV444RGB8: color YUV 4:4:4 to RGB 8 bpp
1272 *****************************************************************************/
1273 static void ConvertYUV444RGB8( p_vout_thread_t p_vout, u8 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1274 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1275 int i_matrix_coefficients )
1277 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1278 int i_vertical_scaling; /* vertical scaling type */
1279 int i_x, i_y; /* horizontal and vertical indexes */
1280 int i_scale_count; /* scale modulo counter */
1281 int i_uval, i_vval; /* U and V samples */
1282 int i_red, i_green, i_blue; /* U and V modified samples */
1283 int i_chroma_width; /* chroma width, not used */
1284 u8 * p_yuv; /* base conversion table */
1285 u8 * p_ybase; /* Y dependant conversion table */
1286 u8 * p_pic_start; /* beginning of the current line for copy */
1287 u8 * p_buffer_start; /* conversion buffer start */
1288 u8 * p_buffer; /* conversion buffer pointer */
1289 int * p_offset_start; /* offset array start */
1290 int * p_offset; /* offset array pointer */
1293 * Initialize some values - i_pic_line_width will store the line skip
1295 i_pic_line_width -= i_pic_width;
1296 p_yuv = p_vout->yuv.yuv.p_rgb8;
1297 p_buffer_start = p_vout->yuv.p_buffer;
1298 p_offset_start = p_vout->yuv.p_offset;
1299 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1300 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1303 * Perform conversion
1305 i_scale_count = i_pic_height;
1306 for( i_y = 0; i_y < i_height; i_y++ )
1308 /* Mark beginnning of line for possible later line copy, and initialize
1310 p_pic_start = p_pic;
1311 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1313 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1314 * pixels wide blocks */
1315 for( i_x = i_width / 16; i_x--; )
1317 CONVERT_YUV_PIXEL(1); CONVERT_YUV_PIXEL(1);
1318 CONVERT_YUV_PIXEL(1); CONVERT_YUV_PIXEL(1);
1319 CONVERT_YUV_PIXEL(1); CONVERT_YUV_PIXEL(1);
1320 CONVERT_YUV_PIXEL(1); CONVERT_YUV_PIXEL(1);
1321 CONVERT_YUV_PIXEL(1); CONVERT_YUV_PIXEL(1);
1322 CONVERT_YUV_PIXEL(1); CONVERT_YUV_PIXEL(1);
1323 CONVERT_YUV_PIXEL(1); CONVERT_YUV_PIXEL(1);
1324 CONVERT_YUV_PIXEL(1); CONVERT_YUV_PIXEL(1);
1327 /* Do horizontal and vertical scaling */
1329 SCALE_HEIGHT(444, 1);
1333 /*****************************************************************************
1334 * ConvertYUV420RGB16: color YUV 4:2:0 to RGB 2 Bpp
1335 *****************************************************************************/
1336 static void ConvertYUV420RGB16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1337 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1338 int i_matrix_coefficients )
1342 int i_chroma_width, i_chroma_skip; /* width and eol for chroma */
1344 i_chroma_width = i_width / 2;
1345 i_chroma_skip = i_skip / 2;
1346 ConvertYUV420RGB16MMX( p_y, p_u, p_v, i_width, i_height,
1347 (i_width + i_skip) * sizeof( yuv_data_t ),
1348 (i_chroma_width + i_chroma_skip) * sizeof( yuv_data_t),
1349 i_scale, (u8 *)p_pic, 0, 0, (i_width + i_pic_eol) * sizeof( u16 ),
1350 p_vout->i_screen_depth == 15 );
1352 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1353 int i_vertical_scaling; /* vertical scaling type */
1354 int i_x, i_y; /* horizontal and vertical indexes */
1355 int i_scale_count; /* scale modulo counter */
1356 int i_uval, i_vval; /* U and V samples */
1357 int i_red, i_green, i_blue; /* U and V modified samples */
1358 int i_chroma_width; /* chroma width */
1359 u16 * p_yuv; /* base conversion table */
1360 u16 * p_ybase; /* Y dependant conversion table */
1361 u16 * p_pic_start; /* beginning of the current line for copy */
1362 u16 * p_buffer_start; /* conversion buffer start */
1363 u16 * p_buffer; /* conversion buffer pointer */
1364 int * p_offset_start; /* offset array start */
1365 int * p_offset; /* offset array pointer */
1368 * Initialize some values - i_pic_line_width will store the line skip
1370 i_pic_line_width -= i_pic_width;
1371 i_chroma_width = i_width / 2;
1372 p_yuv = p_vout->yuv.yuv.p_rgb16;
1373 p_buffer_start = p_vout->yuv.p_buffer;
1374 p_offset_start = p_vout->yuv.p_offset;
1375 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1376 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1379 * Perform conversion
1381 i_scale_count = i_pic_height;
1382 for( i_y = 0; i_y < i_height; i_y++ )
1384 /* Mark beginnning of line for possible later line copy, and initialize
1386 p_pic_start = p_pic;
1387 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1392 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1393 * pixels wide blocks */
1394 for( i_x = i_width / 16; i_x--; )
1396 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1397 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1398 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1399 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1400 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1401 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1402 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1403 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1406 SCALE_HEIGHT(420, 2);
1410 for ( i_x = i_width / 8; i_x--; )
1413 "movd (%1), %%mm0 # Load 4 Cb 00 00 00 00 u3 u2 u1 u0\n\t"
1414 "movd (%2), %%mm1 # Load 4 Cr 00 00 00 00 v3 v2 v1 v0\n\t"
1415 "pxor %%mm4, %%mm4 # zero mm4\n\t"
1416 "movq (%0), %%mm6 # Load 8 Y Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0\n\t"
1417 //"movl $0, (%3) # cache preload for image\n\t"
1418 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer));
1422 /* Do the multiply part of the conversion for even and odd pixels,
1424 * mm0 -> Cblue, mm1 -> Cred, mm2 -> Cgreen even pixels,
1425 * mm3 -> Cblue, mm4 -> Cred, mm5 -> Cgreen odd pixels,
1426 * mm6 -> Y even, mm7 -> Y odd */
1427 /* convert the chroma part */
1428 "punpcklbw %%mm4, %%mm0 # scatter 4 Cb 00 u3 00 u2 00 u1 00 u0\n\t"
1429 "punpcklbw %%mm4, %%mm1 # scatter 4 Cr 00 v3 00 v2 00 v1 00 v0\n\t"
1430 "psubsw mmx_80w, %%mm0 # Cb -= 128\n\t"
1431 "psubsw mmx_80w, %%mm1 # Cr -= 128\n\t"
1432 "psllw $3, %%mm0 # Promote precision\n\t"
1433 "psllw $3, %%mm1 # Promote precision\n\t"
1434 "movq %%mm0, %%mm2 # Copy 4 Cb 00 u3 00 u2 00 u1 00 u0\n\t"
1435 "movq %%mm1, %%mm3 # Copy 4 Cr 00 v3 00 v2 00 v1 00 v0\n\t"
1436 "pmulhw mmx_U_green, %%mm2# Mul Cb with green coeff -> Cb green\n\t"
1437 "pmulhw mmx_V_green, %%mm3# Mul Cr with green coeff -> Cr green\n\t"
1438 "pmulhw mmx_U_blue, %%mm0 # Mul Cb -> Cblue 00 b3 00 b2 00 b1 00 b0\n\t"
1439 "pmulhw mmx_V_red, %%mm1 # Mul Cr -> Cred 00 r3 00 r2 00 r1 00 r0\n\t"
1440 "paddsw %%mm3, %%mm2 # Cb green + Cr green -> Cgreen\n\t"
1441 /* convert the luma part */
1442 "psubusb mmx_10w, %%mm6 # Y -= 16\n\t"
1443 "movq %%mm6, %%mm7 # Copy 8 Y Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0\n\t"
1444 "pand mmx_00ffw, %%mm6 # get Y even 00 Y6 00 Y4 00 Y2 00 Y0\n\t"
1445 "psrlw $8, %%mm7 # get Y odd 00 Y7 00 Y5 00 Y3 00 Y1\n\t"
1446 "psllw $3, %%mm6 # Promote precision\n\t"
1447 "psllw $3, %%mm7 # Promote precision\n\t"
1448 "pmulhw mmx_Y_coeff, %%mm6# Mul 4 Y even 00 y6 00 y4 00 y2 00 y0\n\t"
1449 "pmulhw mmx_Y_coeff, %%mm7# Mul 4 Y odd 00 y7 00 y5 00 y3 00 y1\n\t"
1450 /* Do the addition part of the conversion for even and odd pixels,
1452 * mm0 -> Cblue, mm1 -> Cred, mm2 -> Cgreen even pixels,
1453 * mm3 -> Cblue, mm4 -> Cred, mm5 -> Cgreen odd pixels,
1454 * mm6 -> Y even, mm7 -> Y odd */ /* Do horizontal and vertical scaling */
1455 "movq %%mm0, %%mm3 # Copy Cblue\n\t"
1456 "movq %%mm1, %%mm4 # Copy Cred\n\t"
1457 "movq %%mm2, %%mm5 # Copy Cgreen\n\t"
1458 "paddsw %%mm6, %%mm0 # Y even + Cblue 00 B6 00 B4 00 B2 00 B0\n\t"
1459 "paddsw %%mm7, %%mm3 # Y odd + Cblue 00 B7 00 B5 00 B3 00 B1\n\t"
1460 "paddsw %%mm6, %%mm1 # Y even + Cred 00 R6 00 R4 00 R2 00 R0\n\t"
1461 "paddsw %%mm7, %%mm4 # Y odd + Cred 00 R7 00 R5 00 R3 00 R1\n\t"
1462 "paddsw %%mm6, %%mm2 # Y even + Cgreen 00 G6 00 G4 00 G2 00 G0\n\t"
1463 "paddsw %%mm7, %%mm5 # Y odd + Cgreen 00 G7 00 G5 00 G3 00 G1\n\t"
1464 /* Limit RGB even to 0..255 */
1465 "packuswb %%mm0, %%mm0 # B6 B4 B2 B0 | B6 B4 B2 B0\n\t"
1466 "packuswb %%mm1, %%mm1 # R6 R4 R2 R0 | R6 R4 R2 R0\n\t"
1467 "packuswb %%mm2, %%mm2 # G6 G4 G2 G0 | G6 G4 G2 G0\n\t"
1468 /* Limit RGB odd to 0..255 */
1469 "packuswb %%mm3, %%mm3 # B7 B5 B3 B1 | B7 B5 B3 B1\n\t"
1470 "packuswb %%mm4, %%mm4 # R7 R5 R3 R1 | R7 R5 R3 R1\n\t"
1471 "packuswb %%mm5, %%mm5 # G7 G5 G3 G1 | G7 G5 G3 G1\n\t"
1472 /* Interleave RGB even and odd */
1473 "punpcklbw %%mm3, %%mm0 # B7 B6 B5 B4 B3 B2 B1 B0\n\t"
1474 "punpcklbw %%mm4, %%mm1 # R7 R6 R5 R4 R3 R2 R1 R0\n\t"
1475 "punpcklbw %%mm5, %%mm2 # G7 G6 G5 G4 G3 G2 G1 G0\n\t"
1476 /* mask unneeded bits off */
1477 "pand mmx_redmask, %%mm0# b7b6b5b4 b3_0_0_0 b7b6b5b4 b3_0_0_0\n\t"
1478 "pand mmx_grnmask, %%mm2# g7g6g5g4 g3g2_0_0 g7g6g5g4 g3g2_0_0\n\t"
1479 "pand mmx_redmask, %%mm1# r7r6r5r4 r3_0_0_0 r7r6r5r4 r3_0_0_0\n\t"
1480 "psrlw mmx_blueshift,%%mm0#0_0_0_b7 b6b5b4b3 0_0_0_b7 b6b5b4b3\n\t"
1481 "pxor %%mm4, %%mm4 # zero mm4\n\t"
1482 "movq %%mm0, %%mm5 # Copy B7-B0\n\t"
1483 "movq %%mm2, %%mm7 # Copy G7-G0\n\t"
1484 /* convert rgb24 plane to rgb16 pack for pixel 0-3 */
1485 "punpcklbw %%mm4, %%mm2 # 0_0_0_0 0_0_0_0 g7g6g5g4 g3g2_0_0\n\t"
1486 "punpcklbw %%mm1, %%mm0 # r7r6r5r4 r3_0_0_0 0_0_0_b7 b6b5b4b3\n\t"
1487 "psllw mmx_blueshift,%%mm2# 0_0_0_0 0_g7g6g5 g4g3g2_0 0_0_0_0\n\t"
1488 "por %%mm2, %%mm0 # r7r6r5r4 r3g7g6g5 g4g3g2b7 b6b5b4b3\n\t"
1489 "movq 8(%0), %%mm6 # Load 8 Y Y7 Y6 Y5 Y4 Y3 Y2 Y1 Y0\n\t"
1490 "movq %%mm0, (%3) # store pixel 0-3\n\t"
1491 /* convert rgb24 plane to rgb16 pack for pixel 0-3 */
1492 "punpckhbw %%mm4, %%mm7 # 0_0_0_0 0_0_0_0 g7g6g5g4 g3g2_0_0\n\t"
1493 "punpckhbw %%mm1, %%mm5 # r7r6r5r4 r3_0_0_0 0_0_0_b7 b6b5b4b3\n\t"
1494 "psllw mmx_blueshift,%%mm7# 0_0_0_0 0_g7g6g5 g4g3g2_0 0_0_0_0\n\t"
1495 "movd 4(%1), %%mm0 # Load 4 Cb 00 00 00 00 u3 u2 u1 u0\n\t"
1496 "por %%mm7, %%mm5 # r7r6r5r4 r3g7g6g5 g4g3g2b7 b6b5b4b3\n\t"
1497 "movd 4(%2), %%mm1 # Load 4 Cr 00 00 00 00 v3 v2 v1 v0\n\t"
1498 "movq %%mm5, 8(%3) # store pixel 4-7\n\t"
1499 : : "r" (p_y), "r" (p_u), "r" (p_v), "r" (p_buffer));
1507 SCALE_HEIGHT(420, 2);
1509 __asm__ ("emms\n\t");
1513 /*****************************************************************************
1514 * ConvertYUV422RGB16: color YUV 4:2:2 to RGB 2 Bpp
1515 *****************************************************************************/
1516 static void ConvertYUV422RGB16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1517 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1518 int i_matrix_coefficients )
1520 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1521 int i_vertical_scaling; /* vertical scaling type */
1522 int i_x, i_y; /* horizontal and vertical indexes */
1523 int i_scale_count; /* scale modulo counter */
1524 int i_uval, i_vval; /* U and V samples */
1525 int i_red, i_green, i_blue; /* U and V modified samples */
1526 int i_chroma_width; /* chroma width */
1527 u16 * p_yuv; /* base conversion table */
1528 u16 * p_ybase; /* Y dependant conversion table */
1529 u16 * p_pic_start; /* beginning of the current line for copy */
1530 u16 * p_buffer_start; /* conversion buffer start */
1531 u16 * p_buffer; /* conversion buffer pointer */
1532 int * p_offset_start; /* offset array start */
1533 int * p_offset; /* offset array pointer */
1536 * Initialize some values - i_pic_line_width will store the line skip
1538 i_pic_line_width -= i_pic_width;
1539 i_chroma_width = i_width / 2;
1540 p_yuv = p_vout->yuv.yuv.p_rgb16;
1541 p_buffer_start = p_vout->yuv.p_buffer;
1542 p_offset_start = p_vout->yuv.p_offset;
1543 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1544 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1547 * Perform conversion
1549 i_scale_count = i_pic_height;
1550 for( i_y = 0; i_y < i_height; i_y++ )
1552 /* Mark beginnning of line for possible later line copy, and initialize
1554 p_pic_start = p_pic;
1555 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1557 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1558 * pixels wide blocks */
1559 for( i_x = i_width / 16; i_x--; )
1561 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1562 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1563 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1564 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1565 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1566 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1567 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1568 CONVERT_YUV_PIXEL(2); CONVERT_Y_PIXEL(2);
1571 /* Do horizontal and vertical scaling */
1573 SCALE_HEIGHT(422, 2);
1577 /*****************************************************************************
1578 * ConvertYUV444RGB16: color YUV 4:4:4 to RGB 2 Bpp
1579 *****************************************************************************/
1580 static void ConvertYUV444RGB16( p_vout_thread_t p_vout, u16 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1581 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1582 int i_matrix_coefficients )
1584 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1585 int i_vertical_scaling; /* vertical scaling type */
1586 int i_x, i_y; /* horizontal and vertical indexes */
1587 int i_scale_count; /* scale modulo counter */
1588 int i_uval, i_vval; /* U and V samples */
1589 int i_red, i_green, i_blue; /* U and V modified samples */
1590 int i_chroma_width; /* chroma width, not used */
1591 u16 * p_yuv; /* base conversion table */
1592 u16 * p_ybase; /* Y dependant conversion table */
1593 u16 * p_pic_start; /* beginning of the current line for copy */
1594 u16 * p_buffer_start; /* conversion buffer start */
1595 u16 * p_buffer; /* conversion buffer pointer */
1596 int * p_offset_start; /* offset array start */
1597 int * p_offset; /* offset array pointer */
1600 * Initialize some values - i_pic_line_width will store the line skip
1602 i_pic_line_width -= i_pic_width;
1603 p_yuv = p_vout->yuv.yuv.p_rgb16;
1604 p_buffer_start = p_vout->yuv.p_buffer;
1605 p_offset_start = p_vout->yuv.p_offset;
1606 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1607 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1610 * Perform conversion
1612 i_scale_count = i_pic_height;
1613 for( i_y = 0; i_y < i_height; i_y++ )
1615 /* Mark beginnning of line for possible later line copy, and initialize
1617 p_pic_start = p_pic;
1618 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1620 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1621 * pixels wide blocks */
1622 for( i_x = i_width / 16; i_x--; )
1624 CONVERT_YUV_PIXEL(2); CONVERT_YUV_PIXEL(2);
1625 CONVERT_YUV_PIXEL(2); CONVERT_YUV_PIXEL(2);
1626 CONVERT_YUV_PIXEL(2); CONVERT_YUV_PIXEL(2);
1627 CONVERT_YUV_PIXEL(2); CONVERT_YUV_PIXEL(2);
1628 CONVERT_YUV_PIXEL(2); CONVERT_YUV_PIXEL(2);
1629 CONVERT_YUV_PIXEL(2); CONVERT_YUV_PIXEL(2);
1630 CONVERT_YUV_PIXEL(2); CONVERT_YUV_PIXEL(2);
1631 CONVERT_YUV_PIXEL(2); CONVERT_YUV_PIXEL(2);
1634 /* Do horizontal and vertical scaling */
1636 SCALE_HEIGHT(444, 2);
1640 /*****************************************************************************
1641 * ConvertYUV420RGB24: color YUV 4:2:0 to RGB 3 Bpp
1642 *****************************************************************************/
1643 static void ConvertYUV420RGB24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1644 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1645 int i_matrix_coefficients )
1650 /*****************************************************************************
1651 * ConvertYUV422RGB24: color YUV 4:2:2 to RGB 3 Bpp
1652 *****************************************************************************/
1653 static void ConvertYUV422RGB24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1654 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1655 int i_matrix_coefficients )
1660 /*****************************************************************************
1661 * ConvertYUV444RGB24: color YUV 4:4:4 to RGB 3 Bpp
1662 *****************************************************************************/
1663 static void ConvertYUV444RGB24( p_vout_thread_t p_vout, void *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1664 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1665 int i_matrix_coefficients )
1670 /*****************************************************************************
1671 * ConvertYUV420RGB32: color YUV 4:2:0 to RGB 4 Bpp
1672 *****************************************************************************/
1673 static void ConvertYUV420RGB32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1674 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1675 int i_matrix_coefficients )
1677 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1678 int i_vertical_scaling; /* vertical scaling type */
1679 int i_x, i_y; /* horizontal and vertical indexes */
1680 int i_scale_count; /* scale modulo counter */
1681 int i_uval, i_vval; /* U and V samples */
1682 int i_red, i_green, i_blue; /* U and V modified samples */
1683 int i_chroma_width; /* chroma width */
1684 u32 * p_yuv; /* base conversion table */
1685 u32 * p_ybase; /* Y dependant conversion table */
1686 u32 * p_pic_start; /* beginning of the current line for copy */
1687 u32 * p_buffer_start; /* conversion buffer start */
1688 u32 * p_buffer; /* conversion buffer pointer */
1689 int * p_offset_start; /* offset array start */
1690 int * p_offset; /* offset array pointer */
1693 * Initialize some values - i_pic_line_width will store the line skip
1695 i_pic_line_width -= i_pic_width;
1696 i_chroma_width = i_width / 2;
1697 p_yuv = p_vout->yuv.yuv.p_rgb32;
1698 p_buffer_start = p_vout->yuv.p_buffer;
1699 p_offset_start = p_vout->yuv.p_offset;
1700 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1701 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1704 * Perform conversion
1706 i_scale_count = i_pic_height;
1707 for( i_y = 0; i_y < i_height; i_y++ )
1709 /* Mark beginnning of line for possible later line copy, and initialize
1711 p_pic_start = p_pic;
1712 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1714 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1715 * pixels wide blocks */
1716 for( i_x = i_width / 16; i_x--; )
1718 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1719 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1720 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1721 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1722 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1723 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1724 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1725 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1728 /* Do horizontal and vertical scaling */
1730 SCALE_HEIGHT(420, 4);
1734 /*****************************************************************************
1735 * ConvertYUV422RGB32: color YUV 4:2:2 to RGB 4 Bpp
1736 *****************************************************************************/
1737 static void ConvertYUV422RGB32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1738 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1739 int i_matrix_coefficients )
1741 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1742 int i_vertical_scaling; /* vertical scaling type */
1743 int i_x, i_y; /* horizontal and vertical indexes */
1744 int i_scale_count; /* scale modulo counter */
1745 int i_uval, i_vval; /* U and V samples */
1746 int i_red, i_green, i_blue; /* U and V modified samples */
1747 int i_chroma_width; /* chroma width */
1748 u32 * p_yuv; /* base conversion table */
1749 u32 * p_ybase; /* Y dependant conversion table */
1750 u32 * p_pic_start; /* beginning of the current line for copy */
1751 u32 * p_buffer_start; /* conversion buffer start */
1752 u32 * p_buffer; /* conversion buffer pointer */
1753 int * p_offset_start; /* offset array start */
1754 int * p_offset; /* offset array pointer */
1757 * Initialize some values - i_pic_line_width will store the line skip
1759 i_pic_line_width -= i_pic_width;
1760 i_chroma_width = i_width / 2;
1761 p_yuv = p_vout->yuv.yuv.p_rgb32;
1762 p_buffer_start = p_vout->yuv.p_buffer;
1763 p_offset_start = p_vout->yuv.p_offset;
1764 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1765 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1768 * Perform conversion
1770 i_scale_count = i_pic_height;
1771 for( i_y = 0; i_y < i_height; i_y++ )
1773 /* Mark beginnning of line for possible later line copy, and initialize
1775 p_pic_start = p_pic;
1776 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1778 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1779 * pixels wide blocks */
1780 for( i_x = i_width / 16; i_x--; )
1782 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1783 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1784 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1785 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1786 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1787 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1788 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1789 CONVERT_YUV_PIXEL(4); CONVERT_Y_PIXEL(4);
1792 /* Do horizontal and vertical scaling */
1794 SCALE_HEIGHT(422, 4);
1798 /*****************************************************************************
1799 * ConvertYUV444RGB32: color YUV 4:4:4 to RGB 4 Bpp
1800 *****************************************************************************/
1801 static void ConvertYUV444RGB32( p_vout_thread_t p_vout, u32 *p_pic, yuv_data_t *p_y, yuv_data_t *p_u, yuv_data_t *p_v,
1802 int i_width, int i_height, int i_pic_width, int i_pic_height, int i_pic_line_width,
1803 int i_matrix_coefficients )
1805 boolean_t b_horizontal_scaling; /* horizontal scaling type */
1806 int i_vertical_scaling; /* vertical scaling type */
1807 int i_x, i_y; /* horizontal and vertical indexes */
1808 int i_scale_count; /* scale modulo counter */
1809 int i_uval, i_vval; /* U and V samples */
1810 int i_red, i_green, i_blue; /* U and V modified samples */
1811 int i_chroma_width; /* chroma width, not used */
1812 u32 * p_yuv; /* base conversion table */
1813 u32 * p_ybase; /* Y dependant conversion table */
1814 u32 * p_pic_start; /* beginning of the current line for copy */
1815 u32 * p_buffer_start; /* conversion buffer start */
1816 u32 * p_buffer; /* conversion buffer pointer */
1817 int * p_offset_start; /* offset array start */
1818 int * p_offset; /* offset array pointer */
1821 * Initialize some values - i_pic_line_width will store the line skip
1823 i_pic_line_width -= i_pic_width;
1824 p_yuv = p_vout->yuv.yuv.p_rgb32;
1825 p_buffer_start = p_vout->yuv.p_buffer;
1826 p_offset_start = p_vout->yuv.p_offset;
1827 SetOffset( i_width, i_height, i_pic_width, i_pic_height,
1828 &b_horizontal_scaling, &i_vertical_scaling, p_offset_start );
1831 * Perform conversion
1833 i_scale_count = i_pic_height;
1834 for( i_y = 0; i_y < i_height; i_y++ )
1836 /* Mark beginnning of line for possible later line copy, and initialize
1838 p_pic_start = p_pic;
1839 p_buffer = b_horizontal_scaling ? p_buffer_start : p_pic;
1841 /* Do YUV conversion to buffer - YUV picture is always formed of 16
1842 * pixels wide blocks */
1843 for( i_x = i_width / 16; i_x--; )
1845 CONVERT_YUV_PIXEL(4); CONVERT_YUV_PIXEL(4);
1846 CONVERT_YUV_PIXEL(4); CONVERT_YUV_PIXEL(4);
1847 CONVERT_YUV_PIXEL(4); CONVERT_YUV_PIXEL(4);
1848 CONVERT_YUV_PIXEL(4); CONVERT_YUV_PIXEL(4);
1849 CONVERT_YUV_PIXEL(4); CONVERT_YUV_PIXEL(4);
1850 CONVERT_YUV_PIXEL(4); CONVERT_YUV_PIXEL(4);
1851 CONVERT_YUV_PIXEL(4); CONVERT_YUV_PIXEL(4);
1852 CONVERT_YUV_PIXEL(4); CONVERT_YUV_PIXEL(4);
1855 /* Do horizontal and vertical scaling */
1857 SCALE_HEIGHT(444, 4);
1861 /*-------------------- walken code follows ----------------------------------*/
1864 * YUV to RGB routines.
1866 * these routines calculate r, g and b values from each pixel's y, u and v.
1867 * these r, g an b values are then passed thru a table lookup to take the
1868 * gamma curve into account and find the corresponding pixel value.
1870 * the table must store more than 3*256 values because of the possibility
1871 * of overflow in the yuv->rgb calculation. actually the calculated r,g,b
1872 * values are in the following intervals :
1873 * -176 to 255+176 for red
1874 * -133 to 255+133 for green
1875 * -222 to 255+222 for blue
1877 * If the input y,u,v values are right, the r,g,b results are not expected
1878 * to move out of the 0 to 255 interval but who knows what will happen in
1881 * the red, green and blue conversion tables are stored in a single 1935-entry
1882 * array. The respective positions of each component in the array have been
1883 * calculated to minimize the cache interactions of the 3 tables.
1888 static void yuvToRgb24 (unsigned char * Y,
1889 unsigned char * U, unsigned char * V,
1890 char * dest, int table[1935], int width)
1905 uvRed = (V_RED_COEF*v) >> SHIFT;
1906 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1907 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1909 tableY = table + *(Y++);
1910 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1911 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1913 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1915 *(dest++) = tmp24 >> 8;
1916 *(dest++) = tmp24 >> 16;
1918 tableY = table + *(Y++);
1919 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1920 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1922 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1924 *(dest++) = tmp24 >> 8;
1925 *(dest++) = tmp24 >> 16;
1929 uvRed = (V_RED_COEF*v) >> SHIFT;
1930 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1931 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1933 tableY = table + *(Y++);
1934 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1935 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1937 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1939 *(dest++) = tmp24 >> 8;
1940 *(dest++) = tmp24 >> 16;
1942 tableY = table + *(Y++);
1943 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1944 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1946 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1948 *(dest++) = tmp24 >> 8;
1949 *(dest++) = tmp24 >> 16;
1953 uvRed = (V_RED_COEF*v) >> SHIFT;
1954 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1955 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1957 tableY = table + *(Y++);
1958 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1959 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1961 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1963 *(dest++) = tmp24 >> 8;
1964 *(dest++) = tmp24 >> 16;
1966 tableY = table + *(Y++);
1967 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1968 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1970 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1972 *(dest++) = tmp24 >> 8;
1973 *(dest++) = tmp24 >> 16;
1977 uvRed = (V_RED_COEF*v) >> SHIFT;
1978 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
1979 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
1981 tableY = table + *(Y++);
1982 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1983 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1985 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1987 *(dest++) = tmp24 >> 8;
1988 *(dest++) = tmp24 >> 16;
1990 tableY = table + *(Y++);
1991 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
1992 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
1994 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
1996 *(dest++) = tmp24 >> 8;
1997 *(dest++) = tmp24 >> 16;
2000 i = (width & 7) >> 1;
2004 uvRed = (V_RED_COEF*v) >> SHIFT;
2005 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
2006 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
2008 tableY = table + *(Y++);
2009 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
2010 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
2012 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
2014 *(dest++) = tmp24 >> 8;
2015 *(dest++) = tmp24 >> 16;
2017 tableY = table + *(Y++);
2018 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
2019 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
2021 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
2023 *(dest++) = tmp24 >> 8;
2024 *(dest++) = tmp24 >> 16;
2030 uvRed = (V_RED_COEF*v) >> SHIFT;
2031 uvGreen = (U_GREEN_COEF*u + V_GREEN_COEF*v) >> SHIFT;
2032 uvBlue = (U_BLUE_COEF*u) >> SHIFT;
2034 tableY = table + *(Y++);
2035 tmp24 = (tableY [1501 - ((V_RED_COEF*128)>>SHIFT) + uvRed] |
2036 tableY [135 - (((U_GREEN_COEF+V_GREEN_COEF)*128)>>SHIFT) +
2038 tableY [818 - ((U_BLUE_COEF*128)>>SHIFT) + uvBlue]);
2040 *(dest++) = tmp24 >> 8;
2041 *(dest++) = tmp24 >> 16;