src/video/SDL_yuv_sw.c
changeset 0 74212992fb08
child 9 a1c15fa4abb9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/SDL_yuv_sw.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,1313 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* This is the software implementation of the YUV video overlay support */
    1.32 +
    1.33 +/* This code was derived from code carrying the following copyright notices:
    1.34 +
    1.35 + * Copyright (c) 1995 The Regents of the University of California.
    1.36 + * All rights reserved.
    1.37 + * 
    1.38 + * Permission to use, copy, modify, and distribute this software and its
    1.39 + * documentation for any purpose, without fee, and without written agreement is
    1.40 + * hereby granted, provided that the above copyright notice and the following
    1.41 + * two paragraphs appear in all copies of this software.
    1.42 + * 
    1.43 + * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
    1.44 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
    1.45 + * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
    1.46 + * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.47 + * 
    1.48 + * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
    1.49 + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    1.50 + * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
    1.51 + * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
    1.52 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
    1.53 +
    1.54 + * Copyright (c) 1995 Erik Corry
    1.55 + * All rights reserved.
    1.56 + * 
    1.57 + * Permission to use, copy, modify, and distribute this software and its
    1.58 + * documentation for any purpose, without fee, and without written agreement is
    1.59 + * hereby granted, provided that the above copyright notice and the following
    1.60 + * two paragraphs appear in all copies of this software.
    1.61 + * 
    1.62 + * IN NO EVENT SHALL ERIK CORRY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
    1.63 + * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
    1.64 + * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIK CORRY HAS BEEN ADVISED
    1.65 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.66 + * 
    1.67 + * ERIK CORRY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
    1.68 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    1.69 + * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
    1.70 + * BASIS, AND ERIK CORRY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
    1.71 + * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
    1.72 +
    1.73 + * Portions of this software Copyright (c) 1995 Brown University.
    1.74 + * All rights reserved.
    1.75 + * 
    1.76 + * Permission to use, copy, modify, and distribute this software and its
    1.77 + * documentation for any purpose, without fee, and without written agreement
    1.78 + * is hereby granted, provided that the above copyright notice and the
    1.79 + * following two paragraphs appear in all copies of this software.
    1.80 + * 
    1.81 + * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
    1.82 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
    1.83 + * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
    1.84 + * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.85 + * 
    1.86 + * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
    1.87 + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    1.88 + * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
    1.89 + * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
    1.90 + * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
    1.91 + */
    1.92 +
    1.93 +#include <stdlib.h>
    1.94 +#include <string.h>
    1.95 +
    1.96 +#include "SDL_error.h"
    1.97 +#include "SDL_video.h"
    1.98 +#include "SDL_stretch_c.h"
    1.99 +#include "SDL_yuvfuncs.h"
   1.100 +#include "SDL_yuv_sw_c.h"
   1.101 +
   1.102 +/* Function to check the CPU flags */
   1.103 +#define MMX_CPU		0x800000
   1.104 +#ifdef USE_ASMBLIT
   1.105 +#define CPU_Flags()	Hermes_X86_CPU()
   1.106 +#else
   1.107 +#define CPU_Flags()	0L
   1.108 +#endif
   1.109 +
   1.110 +#ifdef USE_ASMBLIT
   1.111 +#define X86_ASSEMBLER
   1.112 +#define HermesConverterInterface	void
   1.113 +#define HermesClearInterface		void
   1.114 +#define STACKCALL
   1.115 +typedef Uint32 int32;
   1.116 +
   1.117 +#include "HeadX86.h"
   1.118 +#endif
   1.119 +
   1.120 +/* The functions used to manipulate software video overlays */
   1.121 +static struct private_yuvhwfuncs sw_yuvfuncs = {
   1.122 +	SDL_LockYUV_SW,
   1.123 +	SDL_UnlockYUV_SW,
   1.124 +	SDL_DisplayYUV_SW,
   1.125 +	SDL_FreeYUV_SW
   1.126 +};
   1.127 +
   1.128 +/* RGB conversion lookup tables */
   1.129 +struct private_yuvhwdata {
   1.130 +	SDL_Surface *stretch;
   1.131 +	SDL_Surface *display;
   1.132 +	Uint8 *pixels;
   1.133 +	int *colortab;
   1.134 +	Uint32 *rgb_2_pix;
   1.135 +	void (*Display1X)(int *colortab, Uint32 *rgb_2_pix,
   1.136 +                          unsigned char *lum, unsigned char *cr,
   1.137 +                          unsigned char *cb, unsigned char *out,
   1.138 +                          int rows, int cols, int mod );
   1.139 +	void (*Display2X)(int *colortab, Uint32 *rgb_2_pix,
   1.140 +	                  unsigned char *lum, unsigned char *cr,
   1.141 +                          unsigned char *cb, unsigned char *out,
   1.142 +                          int rows, int cols, int mod );
   1.143 +
   1.144 +	/* These are just so we don't have to allocate them separately */
   1.145 +	Uint16 pitches[3];
   1.146 +	Uint8 *planes[3];
   1.147 +};
   1.148 +
   1.149 +
   1.150 +/* The colorspace conversion functions */
   1.151 +
   1.152 +extern void Color565DitherYV12MMX1X( int *colortab, Uint32 *rgb_2_pix,
   1.153 +                                     unsigned char *lum, unsigned char *cr,
   1.154 +                                     unsigned char *cb, unsigned char *out,
   1.155 +                                     int rows, int cols, int mod );
   1.156 +extern void ColorRGBDitherYV12MMX1X( int *colortab, Uint32 *rgb_2_pix,
   1.157 +                                     unsigned char *lum, unsigned char *cr,
   1.158 +                                     unsigned char *cb, unsigned char *out,
   1.159 +                                     int rows, int cols, int mod );
   1.160 +
   1.161 +static void Color16DitherYV12Mod1X( int *colortab, Uint32 *rgb_2_pix,
   1.162 +                                    unsigned char *lum, unsigned char *cr,
   1.163 +                                    unsigned char *cb, unsigned char *out,
   1.164 +                                    int rows, int cols, int mod )
   1.165 +{
   1.166 +    unsigned short* row1;
   1.167 +    unsigned short* row2;
   1.168 +    unsigned char* lum2;
   1.169 +    int x, y;
   1.170 +    int cr_r;
   1.171 +    int crb_g;
   1.172 +    int cb_b;
   1.173 +    int cols_2 = cols / 2;
   1.174 +
   1.175 +    row1 = (unsigned short*) out;
   1.176 +    row2 = row1 + cols + mod;
   1.177 +    lum2 = lum + cols;
   1.178 +
   1.179 +    mod += cols + mod;
   1.180 +
   1.181 +    y = rows / 2;
   1.182 +    while( y-- )
   1.183 +    {
   1.184 +        x = cols_2;
   1.185 +        while( x-- )
   1.186 +        {
   1.187 +            register int L;
   1.188 +
   1.189 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.190 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.191 +                               + colortab[ *cb + 2*256 ];
   1.192 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.193 +            ++cr; ++cb;
   1.194 +
   1.195 +            L = *lum++;
   1.196 +            *row1++ = (rgb_2_pix[ L + cr_r ] |
   1.197 +                       rgb_2_pix[ L + crb_g ] |
   1.198 +                       rgb_2_pix[ L + cb_b ]);
   1.199 +
   1.200 +            L = *lum++;
   1.201 +            *row1++ = (rgb_2_pix[ L + cr_r ] |
   1.202 +                       rgb_2_pix[ L + crb_g ] |
   1.203 +                       rgb_2_pix[ L + cb_b ]);
   1.204 +
   1.205 +
   1.206 +            /* Now, do second row.  */
   1.207 +
   1.208 +            L = *lum2++;
   1.209 +            *row2++ = (rgb_2_pix[ L + cr_r ] |
   1.210 +                       rgb_2_pix[ L + crb_g ] |
   1.211 +                       rgb_2_pix[ L + cb_b ]);
   1.212 +
   1.213 +            L = *lum2++;
   1.214 +            *row2++ = (rgb_2_pix[ L + cr_r ] |
   1.215 +                       rgb_2_pix[ L + crb_g ] |
   1.216 +                       rgb_2_pix[ L + cb_b ]);
   1.217 +        }
   1.218 +
   1.219 +        /*
   1.220 +         * These values are at the start of the next line, (due
   1.221 +         * to the ++'s above),but they need to be at the start
   1.222 +         * of the line after that.
   1.223 +         */
   1.224 +        lum  += cols;
   1.225 +        lum2 += cols;
   1.226 +        row1 += mod;
   1.227 +        row2 += mod;
   1.228 +    }
   1.229 +}
   1.230 +
   1.231 +static void Color24DitherYV12Mod1X( int *colortab, Uint32 *rgb_2_pix,
   1.232 +                                    unsigned char *lum, unsigned char *cr,
   1.233 +                                    unsigned char *cb, unsigned char *out,
   1.234 +                                    int rows, int cols, int mod )
   1.235 +{
   1.236 +    unsigned int value;
   1.237 +    unsigned char* row1;
   1.238 +    unsigned char* row2;
   1.239 +    unsigned char* lum2;
   1.240 +    int x, y;
   1.241 +    int cr_r;
   1.242 +    int crb_g;
   1.243 +    int cb_b;
   1.244 +    int cols_2 = cols / 2;
   1.245 +
   1.246 +    row1 = out;
   1.247 +    row2 = row1 + cols*3 + mod*3;
   1.248 +    lum2 = lum + cols;
   1.249 +
   1.250 +    mod += cols + mod;
   1.251 +    mod *= 3;
   1.252 +
   1.253 +    y = rows / 2;
   1.254 +    while( y-- )
   1.255 +    {
   1.256 +        x = cols_2;
   1.257 +        while( x-- )
   1.258 +        {
   1.259 +            register int L;
   1.260 +
   1.261 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.262 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.263 +                               + colortab[ *cb + 2*256 ];
   1.264 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.265 +            ++cr; ++cb;
   1.266 +
   1.267 +            L = *lum++;
   1.268 +            value = (rgb_2_pix[ L + cr_r ] |
   1.269 +                     rgb_2_pix[ L + crb_g ] |
   1.270 +                     rgb_2_pix[ L + cb_b ]);
   1.271 +            *row1++ = (value      ) & 0xFF;
   1.272 +            *row1++ = (value >>  8) & 0xFF;
   1.273 +            *row1++ = (value >> 16) & 0xFF;
   1.274 +
   1.275 +            L = *lum++;
   1.276 +            value = (rgb_2_pix[ L + cr_r ] |
   1.277 +                     rgb_2_pix[ L + crb_g ] |
   1.278 +                     rgb_2_pix[ L + cb_b ]);
   1.279 +            *row1++ = (value      ) & 0xFF;
   1.280 +            *row1++ = (value >>  8) & 0xFF;
   1.281 +            *row1++ = (value >> 16) & 0xFF;
   1.282 +
   1.283 +
   1.284 +            /* Now, do second row.  */
   1.285 +
   1.286 +            L = *lum2++;
   1.287 +            value = (rgb_2_pix[ L + cr_r ] |
   1.288 +                     rgb_2_pix[ L + crb_g ] |
   1.289 +                     rgb_2_pix[ L + cb_b ]);
   1.290 +            *row2++ = (value      ) & 0xFF;
   1.291 +            *row2++ = (value >>  8) & 0xFF;
   1.292 +            *row2++ = (value >> 16) & 0xFF;
   1.293 +
   1.294 +            L = *lum2++;
   1.295 +            value = (rgb_2_pix[ L + cr_r ] |
   1.296 +                     rgb_2_pix[ L + crb_g ] |
   1.297 +                     rgb_2_pix[ L + cb_b ]);
   1.298 +            *row2++ = (value      ) & 0xFF;
   1.299 +            *row2++ = (value >>  8) & 0xFF;
   1.300 +            *row2++ = (value >> 16) & 0xFF;
   1.301 +        }
   1.302 +
   1.303 +        /*
   1.304 +         * These values are at the start of the next line, (due
   1.305 +         * to the ++'s above),but they need to be at the start
   1.306 +         * of the line after that.
   1.307 +         */
   1.308 +        lum  += cols;
   1.309 +        lum2 += cols;
   1.310 +        row1 += mod;
   1.311 +        row2 += mod;
   1.312 +    }
   1.313 +}
   1.314 +
   1.315 +static void Color32DitherYV12Mod1X( int *colortab, Uint32 *rgb_2_pix,
   1.316 +                                    unsigned char *lum, unsigned char *cr,
   1.317 +                                    unsigned char *cb, unsigned char *out,
   1.318 +                                    int rows, int cols, int mod )
   1.319 +{
   1.320 +    unsigned int* row1;
   1.321 +    unsigned int* row2;
   1.322 +    unsigned char* lum2;
   1.323 +    int x, y;
   1.324 +    int cr_r;
   1.325 +    int crb_g;
   1.326 +    int cb_b;
   1.327 +    int cols_2 = cols / 2;
   1.328 +
   1.329 +    row1 = (unsigned int*) out;
   1.330 +    row2 = row1 + cols + mod;
   1.331 +    lum2 = lum + cols;
   1.332 +
   1.333 +    mod += cols + mod;
   1.334 +
   1.335 +    y = rows / 2;
   1.336 +    while( y-- )
   1.337 +    {
   1.338 +        x = cols_2;
   1.339 +        while( x-- )
   1.340 +        {
   1.341 +            register int L;
   1.342 +
   1.343 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.344 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.345 +                               + colortab[ *cb + 2*256 ];
   1.346 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.347 +            ++cr; ++cb;
   1.348 +
   1.349 +            L = *lum++;
   1.350 +            *row1++ = (rgb_2_pix[ L + cr_r ] |
   1.351 +                       rgb_2_pix[ L + crb_g ] |
   1.352 +                       rgb_2_pix[ L + cb_b ]);
   1.353 +
   1.354 +            L = *lum++;
   1.355 +            *row1++ = (rgb_2_pix[ L + cr_r ] |
   1.356 +                       rgb_2_pix[ L + crb_g ] |
   1.357 +                       rgb_2_pix[ L + cb_b ]);
   1.358 +
   1.359 +
   1.360 +            /* Now, do second row.  */
   1.361 +
   1.362 +            L = *lum2++;
   1.363 +            *row2++ = (rgb_2_pix[ L + cr_r ] |
   1.364 +                       rgb_2_pix[ L + crb_g ] |
   1.365 +                       rgb_2_pix[ L + cb_b ]);
   1.366 +
   1.367 +            L = *lum2++;
   1.368 +            *row2++ = (rgb_2_pix[ L + cr_r ] |
   1.369 +                       rgb_2_pix[ L + crb_g ] |
   1.370 +                       rgb_2_pix[ L + cb_b ]);
   1.371 +        }
   1.372 +
   1.373 +        /*
   1.374 +         * These values are at the start of the next line, (due
   1.375 +         * to the ++'s above),but they need to be at the start
   1.376 +         * of the line after that.
   1.377 +         */
   1.378 +        lum  += cols;
   1.379 +        lum2 += cols;
   1.380 +        row1 += mod;
   1.381 +        row2 += mod;
   1.382 +    }
   1.383 +}
   1.384 +
   1.385 +/*
   1.386 + * In this function I make use of a nasty trick. The tables have the lower
   1.387 + * 16 bits replicated in the upper 16. This means I can write ints and get
   1.388 + * the horisontal doubling for free (almost).
   1.389 + */
   1.390 +static void Color16DitherYV12Mod2X( int *colortab, Uint32 *rgb_2_pix,
   1.391 +                                    unsigned char *lum, unsigned char *cr,
   1.392 +                                    unsigned char *cb, unsigned char *out,
   1.393 +                                    int rows, int cols, int mod )
   1.394 +{
   1.395 +    unsigned int* row1 = (unsigned int*) out;
   1.396 +    const int next_row = cols+(mod/2);
   1.397 +    unsigned int* row2 = row1 + 2*next_row;
   1.398 +    unsigned char* lum2;
   1.399 +    int x, y;
   1.400 +    int cr_r;
   1.401 +    int crb_g;
   1.402 +    int cb_b;
   1.403 +    int cols_2 = cols / 2;
   1.404 +
   1.405 +    lum2 = lum + cols;
   1.406 +
   1.407 +    mod = (next_row * 3) + (mod/2);
   1.408 +
   1.409 +    y = rows / 2;
   1.410 +    while( y-- )
   1.411 +    {
   1.412 +        x = cols_2;
   1.413 +        while( x-- )
   1.414 +        {
   1.415 +            register int L;
   1.416 +
   1.417 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.418 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.419 +                               + colortab[ *cb + 2*256 ];
   1.420 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.421 +            ++cr; ++cb;
   1.422 +
   1.423 +            L = *lum++;
   1.424 +            row1[0] = row1[next_row] = (rgb_2_pix[ L + cr_r ] |
   1.425 +                                        rgb_2_pix[ L + crb_g ] |
   1.426 +                                        rgb_2_pix[ L + cb_b ]);
   1.427 +            row1++;
   1.428 +
   1.429 +            L = *lum++;
   1.430 +            row1[0] = row1[next_row] = (rgb_2_pix[ L + cr_r ] |
   1.431 +                                        rgb_2_pix[ L + crb_g ] |
   1.432 +                                        rgb_2_pix[ L + cb_b ]);
   1.433 +            row1++;
   1.434 +
   1.435 +
   1.436 +            /* Now, do second row. */
   1.437 +
   1.438 +            L = *lum2++;
   1.439 +            row2[0] = row2[next_row] = (rgb_2_pix[ L + cr_r ] |
   1.440 +                                        rgb_2_pix[ L + crb_g ] |
   1.441 +                                        rgb_2_pix[ L + cb_b ]);
   1.442 +            row2++;
   1.443 +
   1.444 +            L = *lum2++;
   1.445 +            row2[0] = row2[next_row] = (rgb_2_pix[ L + cr_r ] |
   1.446 +                                        rgb_2_pix[ L + crb_g ] |
   1.447 +                                        rgb_2_pix[ L + cb_b ]);
   1.448 +            row2++;
   1.449 +        }
   1.450 +
   1.451 +        /*
   1.452 +         * These values are at the start of the next line, (due
   1.453 +         * to the ++'s above),but they need to be at the start
   1.454 +         * of the line after that.
   1.455 +         */
   1.456 +        lum  += cols;
   1.457 +        lum2 += cols;
   1.458 +        row1 += mod;
   1.459 +        row2 += mod;
   1.460 +    }
   1.461 +}
   1.462 +
   1.463 +static void Color24DitherYV12Mod2X( int *colortab, Uint32 *rgb_2_pix,
   1.464 +                                    unsigned char *lum, unsigned char *cr,
   1.465 +                                    unsigned char *cb, unsigned char *out,
   1.466 +                                    int rows, int cols, int mod )
   1.467 +{
   1.468 +    unsigned int value;
   1.469 +    unsigned char* row1 = out;
   1.470 +    const int next_row = (cols*2 + mod) * 3;
   1.471 +    unsigned char* row2 = row1 + 2*next_row;
   1.472 +    unsigned char* lum2;
   1.473 +    int x, y;
   1.474 +    int cr_r;
   1.475 +    int crb_g;
   1.476 +    int cb_b;
   1.477 +    int cols_2 = cols / 2;
   1.478 +
   1.479 +    lum2 = lum + cols;
   1.480 +
   1.481 +    mod = next_row*3 + mod*3;
   1.482 +
   1.483 +    y = rows / 2;
   1.484 +    while( y-- )
   1.485 +    {
   1.486 +        x = cols_2;
   1.487 +        while( x-- )
   1.488 +        {
   1.489 +            register int L;
   1.490 +
   1.491 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.492 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.493 +                               + colortab[ *cb + 2*256 ];
   1.494 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.495 +            ++cr; ++cb;
   1.496 +
   1.497 +            L = *lum++;
   1.498 +            value = (rgb_2_pix[ L + cr_r ] |
   1.499 +                     rgb_2_pix[ L + crb_g ] |
   1.500 +                     rgb_2_pix[ L + cb_b ]);
   1.501 +            row1[0+0] = row1[3+0] = row1[next_row+0] = row1[next_row+3+0] =
   1.502 +                     (value      ) & 0xFF;
   1.503 +            row1[0+1] = row1[3+1] = row1[next_row+1] = row1[next_row+3+1] =
   1.504 +                     (value >>  8) & 0xFF;
   1.505 +            row1[0+2] = row1[3+2] = row1[next_row+2] = row1[next_row+3+2] =
   1.506 +                     (value >> 16) & 0xFF;
   1.507 +            row1 += 2*3;
   1.508 +
   1.509 +            L = *lum++;
   1.510 +            value = (rgb_2_pix[ L + cr_r ] |
   1.511 +                     rgb_2_pix[ L + crb_g ] |
   1.512 +                     rgb_2_pix[ L + cb_b ]);
   1.513 +            row1[0+0] = row1[3+0] = row1[next_row+0] = row1[next_row+3+0] =
   1.514 +                     (value      ) & 0xFF;
   1.515 +            row1[0+1] = row1[3+1] = row1[next_row+1] = row1[next_row+3+1] =
   1.516 +                     (value >>  8) & 0xFF;
   1.517 +            row1[0+2] = row1[3+2] = row1[next_row+2] = row1[next_row+3+2] =
   1.518 +                     (value >> 16) & 0xFF;
   1.519 +            row1 += 2*3;
   1.520 +
   1.521 +
   1.522 +            /* Now, do second row. */
   1.523 +
   1.524 +            L = *lum2++;
   1.525 +            value = (rgb_2_pix[ L + cr_r ] |
   1.526 +                     rgb_2_pix[ L + crb_g ] |
   1.527 +                     rgb_2_pix[ L + cb_b ]);
   1.528 +            row2[0+0] = row2[3+0] = row2[next_row+0] = row2[next_row+3+0] =
   1.529 +                     (value      ) & 0xFF;
   1.530 +            row2[0+1] = row2[3+1] = row2[next_row+1] = row2[next_row+3+1] =
   1.531 +                     (value >>  8) & 0xFF;
   1.532 +            row2[0+2] = row2[3+2] = row2[next_row+2] = row2[next_row+3+2] =
   1.533 +                     (value >> 16) & 0xFF;
   1.534 +            row2 += 2*3;
   1.535 +
   1.536 +            L = *lum2++;
   1.537 +            value = (rgb_2_pix[ L + cr_r ] |
   1.538 +                     rgb_2_pix[ L + crb_g ] |
   1.539 +                     rgb_2_pix[ L + cb_b ]);
   1.540 +            row2[0+0] = row2[3+0] = row2[next_row+0] = row2[next_row+3+0] =
   1.541 +                     (value      ) & 0xFF;
   1.542 +            row2[0+1] = row2[3+1] = row2[next_row+1] = row2[next_row+3+1] =
   1.543 +                     (value >>  8) & 0xFF;
   1.544 +            row2[0+2] = row2[3+2] = row2[next_row+2] = row2[next_row+3+2] =
   1.545 +                     (value >> 16) & 0xFF;
   1.546 +            row2 += 2*3;
   1.547 +        }
   1.548 +
   1.549 +        /*
   1.550 +         * These values are at the start of the next line, (due
   1.551 +         * to the ++'s above),but they need to be at the start
   1.552 +         * of the line after that.
   1.553 +         */
   1.554 +        lum  += cols;
   1.555 +        lum2 += cols;
   1.556 +        row1 += mod;
   1.557 +        row2 += mod;
   1.558 +    }
   1.559 +}
   1.560 +
   1.561 +static void Color32DitherYV12Mod2X( int *colortab, Uint32 *rgb_2_pix,
   1.562 +                                    unsigned char *lum, unsigned char *cr,
   1.563 +                                    unsigned char *cb, unsigned char *out,
   1.564 +                                    int rows, int cols, int mod )
   1.565 +{
   1.566 +    unsigned int* row1 = (unsigned int*) out;
   1.567 +    const int next_row = cols*2+mod;
   1.568 +    unsigned int* row2 = row1 + 2*next_row;
   1.569 +    unsigned char* lum2;
   1.570 +    int x, y;
   1.571 +    int cr_r;
   1.572 +    int crb_g;
   1.573 +    int cb_b;
   1.574 +    int cols_2 = cols / 2;
   1.575 +
   1.576 +    lum2 = lum + cols;
   1.577 +
   1.578 +    mod = (next_row * 3) + mod;
   1.579 +
   1.580 +    y = rows / 2;
   1.581 +    while( y-- )
   1.582 +    {
   1.583 +        x = cols_2;
   1.584 +        while( x-- )
   1.585 +        {
   1.586 +            register int L;
   1.587 +
   1.588 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.589 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.590 +                               + colortab[ *cb + 2*256 ];
   1.591 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.592 +            ++cr; ++cb;
   1.593 +
   1.594 +            L = *lum++;
   1.595 +            row1[0] = row1[1] = row1[next_row] = row1[next_row+1] =
   1.596 +                                       (rgb_2_pix[ L + cr_r ] |
   1.597 +                                        rgb_2_pix[ L + crb_g ] |
   1.598 +                                        rgb_2_pix[ L + cb_b ]);
   1.599 +            row1 += 2;
   1.600 +
   1.601 +            L = *lum++;
   1.602 +            row1[0] = row1[1] = row1[next_row] = row1[next_row+1] =
   1.603 +                                       (rgb_2_pix[ L + cr_r ] |
   1.604 +                                        rgb_2_pix[ L + crb_g ] |
   1.605 +                                        rgb_2_pix[ L + cb_b ]);
   1.606 +            row1 += 2;
   1.607 +
   1.608 +
   1.609 +            /* Now, do second row. */
   1.610 +
   1.611 +            L = *lum2++;
   1.612 +            row2[0] = row2[1] = row2[next_row] = row2[next_row+1] =
   1.613 +                                       (rgb_2_pix[ L + cr_r ] |
   1.614 +                                        rgb_2_pix[ L + crb_g ] |
   1.615 +                                        rgb_2_pix[ L + cb_b ]);
   1.616 +            row2 += 2;
   1.617 +
   1.618 +            L = *lum2++;
   1.619 +            row2[0] = row2[1] = row2[next_row] = row2[next_row+1] =
   1.620 +                                       (rgb_2_pix[ L + cr_r ] |
   1.621 +                                        rgb_2_pix[ L + crb_g ] |
   1.622 +                                        rgb_2_pix[ L + cb_b ]);
   1.623 +            row2 += 2;
   1.624 +        }
   1.625 +
   1.626 +        /*
   1.627 +         * These values are at the start of the next line, (due
   1.628 +         * to the ++'s above),but they need to be at the start
   1.629 +         * of the line after that.
   1.630 +         */
   1.631 +        lum  += cols;
   1.632 +        lum2 += cols;
   1.633 +        row1 += mod;
   1.634 +        row2 += mod;
   1.635 +    }
   1.636 +}
   1.637 +
   1.638 +static void Color16DitherYUY2Mod1X( int *colortab, Uint32 *rgb_2_pix,
   1.639 +                                    unsigned char *lum, unsigned char *cr,
   1.640 +                                    unsigned char *cb, unsigned char *out,
   1.641 +                                    int rows, int cols, int mod )
   1.642 +{
   1.643 +    unsigned short* row;
   1.644 +    int x, y;
   1.645 +    int cr_r;
   1.646 +    int crb_g;
   1.647 +    int cb_b;
   1.648 +    int cols_2 = cols / 2;
   1.649 +
   1.650 +    row = (unsigned short*) out;
   1.651 +
   1.652 +    y = rows;
   1.653 +    while( y-- )
   1.654 +    {
   1.655 +        x = cols_2;
   1.656 +        while( x-- )
   1.657 +        {
   1.658 +            register int L;
   1.659 +
   1.660 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.661 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.662 +                               + colortab[ *cb + 2*256 ];
   1.663 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.664 +            cr += 4; cb += 4;
   1.665 +
   1.666 +            L = *lum; lum += 2;
   1.667 +            *row++ = (rgb_2_pix[ L + cr_r ] |
   1.668 +                       rgb_2_pix[ L + crb_g ] |
   1.669 +                       rgb_2_pix[ L + cb_b ]);
   1.670 +
   1.671 +            L = *lum; lum += 2;
   1.672 +            *row++ = (rgb_2_pix[ L + cr_r ] |
   1.673 +                       rgb_2_pix[ L + crb_g ] |
   1.674 +                       rgb_2_pix[ L + cb_b ]);
   1.675 +
   1.676 +        }
   1.677 +
   1.678 +        row += mod;
   1.679 +    }
   1.680 +}
   1.681 +
   1.682 +static void Color24DitherYUY2Mod1X( int *colortab, Uint32 *rgb_2_pix,
   1.683 +                                    unsigned char *lum, unsigned char *cr,
   1.684 +                                    unsigned char *cb, unsigned char *out,
   1.685 +                                    int rows, int cols, int mod )
   1.686 +{
   1.687 +    unsigned int value;
   1.688 +    unsigned char* row;
   1.689 +    int x, y;
   1.690 +    int cr_r;
   1.691 +    int crb_g;
   1.692 +    int cb_b;
   1.693 +    int cols_2 = cols / 2;
   1.694 +
   1.695 +    row = (unsigned char*) out;
   1.696 +    mod *= 3;
   1.697 +    y = rows;
   1.698 +    while( y-- )
   1.699 +    {
   1.700 +        x = cols_2;
   1.701 +        while( x-- )
   1.702 +        {
   1.703 +            register int L;
   1.704 +
   1.705 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.706 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.707 +                               + colortab[ *cb + 2*256 ];
   1.708 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.709 +            cr += 4; cb += 4;
   1.710 +
   1.711 +            L = *lum; lum += 2;
   1.712 +            value = (rgb_2_pix[ L + cr_r ] |
   1.713 +                     rgb_2_pix[ L + crb_g ] |
   1.714 +                     rgb_2_pix[ L + cb_b ]);
   1.715 +            *row++ = (value      ) & 0xFF;
   1.716 +            *row++ = (value >>  8) & 0xFF;
   1.717 +            *row++ = (value >> 16) & 0xFF;
   1.718 +
   1.719 +            L = *lum; lum += 2;
   1.720 +            value = (rgb_2_pix[ L + cr_r ] |
   1.721 +                     rgb_2_pix[ L + crb_g ] |
   1.722 +                     rgb_2_pix[ L + cb_b ]);
   1.723 +            *row++ = (value      ) & 0xFF;
   1.724 +            *row++ = (value >>  8) & 0xFF;
   1.725 +            *row++ = (value >> 16) & 0xFF;
   1.726 +
   1.727 +        }
   1.728 +        row += mod;
   1.729 +    }
   1.730 +}
   1.731 +
   1.732 +static void Color32DitherYUY2Mod1X( int *colortab, Uint32 *rgb_2_pix,
   1.733 +                                    unsigned char *lum, unsigned char *cr,
   1.734 +                                    unsigned char *cb, unsigned char *out,
   1.735 +                                    int rows, int cols, int mod )
   1.736 +{
   1.737 +    unsigned int* row;
   1.738 +    int x, y;
   1.739 +    int cr_r;
   1.740 +    int crb_g;
   1.741 +    int cb_b;
   1.742 +    int cols_2 = cols / 2;
   1.743 +
   1.744 +    row = (unsigned int*) out;
   1.745 +    y = rows;
   1.746 +    while( y-- )
   1.747 +    {
   1.748 +        x = cols_2;
   1.749 +        while( x-- )
   1.750 +        {
   1.751 +            register int L;
   1.752 +
   1.753 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.754 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.755 +                               + colortab[ *cb + 2*256 ];
   1.756 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.757 +            cr += 4; cb += 4;
   1.758 +
   1.759 +            L = *lum; lum += 2;
   1.760 +            *row++ = (rgb_2_pix[ L + cr_r ] |
   1.761 +                       rgb_2_pix[ L + crb_g ] |
   1.762 +                       rgb_2_pix[ L + cb_b ]);
   1.763 +
   1.764 +            L = *lum; lum += 2;
   1.765 +            *row++ = (rgb_2_pix[ L + cr_r ] |
   1.766 +                       rgb_2_pix[ L + crb_g ] |
   1.767 +                       rgb_2_pix[ L + cb_b ]);
   1.768 +
   1.769 +
   1.770 +        }
   1.771 +        row += mod;
   1.772 +    }
   1.773 +}
   1.774 +
   1.775 +/*
   1.776 + * In this function I make use of a nasty trick. The tables have the lower
   1.777 + * 16 bits replicated in the upper 16. This means I can write ints and get
   1.778 + * the horisontal doubling for free (almost).
   1.779 + */
   1.780 +static void Color16DitherYUY2Mod2X( int *colortab, Uint32 *rgb_2_pix,
   1.781 +                                    unsigned char *lum, unsigned char *cr,
   1.782 +                                    unsigned char *cb, unsigned char *out,
   1.783 +                                    int rows, int cols, int mod )
   1.784 +{
   1.785 +    unsigned int* row = (unsigned int*) out;
   1.786 +    const int next_row = cols+(mod/2);
   1.787 +    int x, y;
   1.788 +    int cr_r;
   1.789 +    int crb_g;
   1.790 +    int cb_b;
   1.791 +    int cols_2 = cols / 2;
   1.792 +
   1.793 +    y = rows;
   1.794 +    while( y-- )
   1.795 +    {
   1.796 +        x = cols_2;
   1.797 +        while( x-- )
   1.798 +        {
   1.799 +            register int L;
   1.800 +
   1.801 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.802 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.803 +                               + colortab[ *cb + 2*256 ];
   1.804 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.805 +            cr += 4; cb += 4;
   1.806 +
   1.807 +            L = *lum; lum += 2;
   1.808 +            row[0] = row[next_row] = (rgb_2_pix[ L + cr_r ] |
   1.809 +                                        rgb_2_pix[ L + crb_g ] |
   1.810 +                                        rgb_2_pix[ L + cb_b ]);
   1.811 +            row++;
   1.812 +
   1.813 +            L = *lum; lum += 2;
   1.814 +            row[0] = row[next_row] = (rgb_2_pix[ L + cr_r ] |
   1.815 +                                        rgb_2_pix[ L + crb_g ] |
   1.816 +                                        rgb_2_pix[ L + cb_b ]);
   1.817 +            row++;
   1.818 +
   1.819 +        }
   1.820 +        row += next_row;
   1.821 +    }
   1.822 +}
   1.823 +
   1.824 +static void Color24DitherYUY2Mod2X( int *colortab, Uint32 *rgb_2_pix,
   1.825 +                                    unsigned char *lum, unsigned char *cr,
   1.826 +                                    unsigned char *cb, unsigned char *out,
   1.827 +                                    int rows, int cols, int mod )
   1.828 +{
   1.829 +    unsigned int value;
   1.830 +    unsigned char* row = out;
   1.831 +    const int next_row = (cols*2 + mod) * 3;
   1.832 +    int x, y;
   1.833 +    int cr_r;
   1.834 +    int crb_g;
   1.835 +    int cb_b;
   1.836 +    int cols_2 = cols / 2;
   1.837 +    y = rows;
   1.838 +    while( y-- )
   1.839 +    {
   1.840 +        x = cols_2;
   1.841 +        while( x-- )
   1.842 +        {
   1.843 +            register int L;
   1.844 +
   1.845 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.846 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.847 +                               + colortab[ *cb + 2*256 ];
   1.848 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.849 +            cr += 4; cb += 4;
   1.850 +
   1.851 +            L = *lum; lum += 2;
   1.852 +            value = (rgb_2_pix[ L + cr_r ] |
   1.853 +                     rgb_2_pix[ L + crb_g ] |
   1.854 +                     rgb_2_pix[ L + cb_b ]);
   1.855 +            row[0+0] = row[3+0] = row[next_row+0] = row[next_row+3+0] =
   1.856 +                     (value      ) & 0xFF;
   1.857 +            row[0+1] = row[3+1] = row[next_row+1] = row[next_row+3+1] =
   1.858 +                     (value >>  8) & 0xFF;
   1.859 +            row[0+2] = row[3+2] = row[next_row+2] = row[next_row+3+2] =
   1.860 +                     (value >> 16) & 0xFF;
   1.861 +            row += 2*3;
   1.862 +
   1.863 +            L = *lum; lum += 2;
   1.864 +            value = (rgb_2_pix[ L + cr_r ] |
   1.865 +                     rgb_2_pix[ L + crb_g ] |
   1.866 +                     rgb_2_pix[ L + cb_b ]);
   1.867 +            row[0+0] = row[3+0] = row[next_row+0] = row[next_row+3+0] =
   1.868 +                     (value      ) & 0xFF;
   1.869 +            row[0+1] = row[3+1] = row[next_row+1] = row[next_row+3+1] =
   1.870 +                     (value >>  8) & 0xFF;
   1.871 +            row[0+2] = row[3+2] = row[next_row+2] = row[next_row+3+2] =
   1.872 +                     (value >> 16) & 0xFF;
   1.873 +            row += 2*3;
   1.874 +
   1.875 +        }
   1.876 +        row += next_row;
   1.877 +    }
   1.878 +}
   1.879 +
   1.880 +static void Color32DitherYUY2Mod2X( int *colortab, Uint32 *rgb_2_pix,
   1.881 +                                    unsigned char *lum, unsigned char *cr,
   1.882 +                                    unsigned char *cb, unsigned char *out,
   1.883 +                                    int rows, int cols, int mod )
   1.884 +{
   1.885 +    unsigned int* row = (unsigned int*) out;
   1.886 +    const int next_row = cols*2+mod;
   1.887 +    int x, y;
   1.888 +    int cr_r;
   1.889 +    int crb_g;
   1.890 +    int cb_b;
   1.891 +    int cols_2 = cols / 2;
   1.892 +    mod+=mod;
   1.893 +    y = rows;
   1.894 +    while( y-- )
   1.895 +    {
   1.896 +        x = cols_2;
   1.897 +        while( x-- )
   1.898 +        {
   1.899 +            register int L;
   1.900 +
   1.901 +            cr_r   = 0*768+256 + colortab[ *cr + 0*256 ];
   1.902 +            crb_g  = 1*768+256 + colortab[ *cr + 1*256 ]
   1.903 +                               + colortab[ *cb + 2*256 ];
   1.904 +            cb_b   = 2*768+256 + colortab[ *cb + 3*256 ];
   1.905 +            cr += 4; cb += 4;
   1.906 +
   1.907 +            L = *lum; lum += 2;
   1.908 +            row[0] = row[1] = row[next_row] = row[next_row+1] =
   1.909 +                                       (rgb_2_pix[ L + cr_r ] |
   1.910 +                                        rgb_2_pix[ L + crb_g ] |
   1.911 +                                        rgb_2_pix[ L + cb_b ]);
   1.912 +            row += 2;
   1.913 +
   1.914 +            L = *lum; lum += 2;
   1.915 +            row[0] = row[1] = row[next_row] = row[next_row+1] =
   1.916 +                                       (rgb_2_pix[ L + cr_r ] |
   1.917 +                                        rgb_2_pix[ L + crb_g ] |
   1.918 +                                        rgb_2_pix[ L + cb_b ]);
   1.919 +            row += 2;
   1.920 +
   1.921 +
   1.922 +        }
   1.923 +
   1.924 +        row += next_row;
   1.925 +    }
   1.926 +}
   1.927 +
   1.928 +/*
   1.929 + * How many 1 bits are there in the Uint32.
   1.930 + * Low performance, do not call often.
   1.931 + */
   1.932 +static int number_of_bits_set( Uint32 a )
   1.933 +{
   1.934 +    if(!a) return 0;
   1.935 +    if(a & 1) return 1 + number_of_bits_set(a >> 1);
   1.936 +    return(number_of_bits_set(a >> 1));
   1.937 +}
   1.938 +
   1.939 +/*
   1.940 + * How many 0 bits are there at least significant end of Uint32.
   1.941 + * Low performance, do not call often.
   1.942 + */
   1.943 +static int free_bits_at_bottom( Uint32 a )
   1.944 +{
   1.945 +      /* assume char is 8 bits */
   1.946 +    if(!a) return sizeof(Uint32) * 8;
   1.947 +    if(((Sint32)a) & 1l) return 0;
   1.948 +    return 1 + free_bits_at_bottom ( a >> 1);
   1.949 +}
   1.950 +
   1.951 +
   1.952 +SDL_Overlay *SDL_CreateYUV_SW(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
   1.953 +{
   1.954 +	SDL_Overlay *overlay;
   1.955 +	struct private_yuvhwdata *swdata;
   1.956 +	int *Cr_r_tab;
   1.957 +	int *Cr_g_tab;
   1.958 +	int *Cb_g_tab;
   1.959 +	int *Cb_b_tab;
   1.960 +	Uint32 *r_2_pix_alloc;
   1.961 +	Uint32 *g_2_pix_alloc;
   1.962 +	Uint32 *b_2_pix_alloc;
   1.963 +	int i, cpu_mmx;
   1.964 +	int CR, CB;
   1.965 +	Uint32 Rmask, Gmask, Bmask;
   1.966 +
   1.967 +	/* Only RGB packed pixel conversion supported */
   1.968 +	if ( (display->format->BytesPerPixel != 2) &&
   1.969 +	     (display->format->BytesPerPixel != 3) &&
   1.970 +	     (display->format->BytesPerPixel != 4) ) {
   1.971 +		SDL_SetError("Can't use YUV data on non 16/24/32 bit surfaces");
   1.972 +		return(NULL);
   1.973 +	}
   1.974 +
   1.975 +	/* Verify that we support the format */
   1.976 +	switch (format) {
   1.977 +	    case SDL_YV12_OVERLAY:
   1.978 +	    case SDL_IYUV_OVERLAY:
   1.979 +	    case SDL_YUY2_OVERLAY:
   1.980 +	    case SDL_UYVY_OVERLAY:
   1.981 +	    case SDL_YVYU_OVERLAY:
   1.982 +		break;
   1.983 +	    default:
   1.984 +		SDL_SetError("Unsupported YUV format");
   1.985 +		return(NULL);
   1.986 +	}
   1.987 +
   1.988 +	/* Create the overlay structure */
   1.989 +	overlay = (SDL_Overlay *)malloc(sizeof *overlay);
   1.990 +	if ( overlay == NULL ) {
   1.991 +		SDL_OutOfMemory();
   1.992 +		return(NULL);
   1.993 +	}
   1.994 +	memset(overlay, 0, (sizeof *overlay));
   1.995 +
   1.996 +	/* Fill in the basic members */
   1.997 +	overlay->format = format;
   1.998 +	overlay->w = width;
   1.999 +	overlay->h = height;
  1.1000 +
  1.1001 +	/* Set up the YUV surface function structure */
  1.1002 +	overlay->hwfuncs = &sw_yuvfuncs;
  1.1003 +
  1.1004 +	/* Create the pixel data and lookup tables */
  1.1005 +	swdata = (struct private_yuvhwdata *)malloc(sizeof *swdata);
  1.1006 +	overlay->hwdata = swdata;
  1.1007 +	if ( swdata == NULL ) {
  1.1008 +		SDL_OutOfMemory();
  1.1009 +		SDL_FreeYUVOverlay(overlay);
  1.1010 +		return(NULL);
  1.1011 +	}
  1.1012 +	swdata->stretch = NULL;
  1.1013 +	swdata->display = display;
  1.1014 +	swdata->pixels = (Uint8 *) malloc(width*height*2);
  1.1015 +	swdata->colortab = (int *)malloc(4*256*sizeof(int));
  1.1016 +	Cr_r_tab = &swdata->colortab[0*256];
  1.1017 +	Cr_g_tab = &swdata->colortab[1*256];
  1.1018 +	Cb_g_tab = &swdata->colortab[2*256];
  1.1019 +	Cb_b_tab = &swdata->colortab[3*256];
  1.1020 +	swdata->rgb_2_pix = (Uint32 *)malloc(3*768*sizeof(Uint32));
  1.1021 +	r_2_pix_alloc = &swdata->rgb_2_pix[0*768];
  1.1022 +	g_2_pix_alloc = &swdata->rgb_2_pix[1*768];
  1.1023 +	b_2_pix_alloc = &swdata->rgb_2_pix[2*768];
  1.1024 +	if ( ! swdata->pixels || ! swdata->colortab || ! swdata->rgb_2_pix ) {
  1.1025 +		SDL_OutOfMemory();
  1.1026 +		SDL_FreeYUVOverlay(overlay);
  1.1027 +		return(NULL);
  1.1028 +	}
  1.1029 +
  1.1030 +	/* Generate the tables for the display surface */
  1.1031 +	for (i=0; i<256; i++) {
  1.1032 +		/* Gamma correction (luminescence table) and chroma correction
  1.1033 +		   would be done here.  See the Berkeley mpeg_play sources.
  1.1034 +		*/
  1.1035 +		CB = CR = (i-128);
  1.1036 +		Cr_r_tab[i] = (int) ( (0.419/0.299) * CR);
  1.1037 +		Cr_g_tab[i] = (int) (-(0.299/0.419) * CR);
  1.1038 +		Cb_g_tab[i] = (int) (-(0.114/0.331) * CB); 
  1.1039 +		Cb_b_tab[i] = (int) ( (0.587/0.331) * CB);
  1.1040 +	}
  1.1041 +
  1.1042 +	/* 
  1.1043 +	 * Set up entries 0-255 in rgb-to-pixel value tables.
  1.1044 +	 */
  1.1045 +	Rmask = display->format->Rmask;
  1.1046 +	Gmask = display->format->Gmask;
  1.1047 +	Bmask = display->format->Bmask;
  1.1048 +	for ( i=0; i<256; ++i ) {
  1.1049 +		r_2_pix_alloc[i+256] = i >> (8 - number_of_bits_set(Rmask));
  1.1050 +		r_2_pix_alloc[i+256] <<= free_bits_at_bottom(Rmask);
  1.1051 +		g_2_pix_alloc[i+256] = i >> (8 - number_of_bits_set(Gmask));
  1.1052 +		g_2_pix_alloc[i+256] <<= free_bits_at_bottom(Gmask);
  1.1053 +		b_2_pix_alloc[i+256] = i >> (8 - number_of_bits_set(Bmask));
  1.1054 +		b_2_pix_alloc[i+256] <<= free_bits_at_bottom(Bmask);
  1.1055 +	}
  1.1056 +
  1.1057 +	/*
  1.1058 +	 * If we have 16-bit output depth, then we double the value
  1.1059 +	 * in the top word. This means that we can write out both
  1.1060 +	 * pixels in the pixel doubling mode with one op. It is 
  1.1061 +	 * harmless in the normal case as storing a 32-bit value
  1.1062 +	 * through a short pointer will lose the top bits anyway.
  1.1063 +	 */
  1.1064 +	if( display->format->BytesPerPixel == 2 ) {
  1.1065 +		for ( i=0; i<256; ++i ) {
  1.1066 +			r_2_pix_alloc[i+256] |= (r_2_pix_alloc[i+256]) << 16;
  1.1067 +			g_2_pix_alloc[i+256] |= (g_2_pix_alloc[i+256]) << 16;
  1.1068 +			b_2_pix_alloc[i+256] |= (b_2_pix_alloc[i+256]) << 16;
  1.1069 +		}
  1.1070 +	}
  1.1071 +
  1.1072 +	/*
  1.1073 +	 * Spread out the values we have to the rest of the array so that
  1.1074 +	 * we do not need to check for overflow.
  1.1075 +	 */
  1.1076 +	for ( i=0; i<256; ++i ) {
  1.1077 +		r_2_pix_alloc[i] = r_2_pix_alloc[256];
  1.1078 +		r_2_pix_alloc[i+512] = r_2_pix_alloc[511];
  1.1079 +		g_2_pix_alloc[i] = g_2_pix_alloc[256];
  1.1080 +		g_2_pix_alloc[i+512] = g_2_pix_alloc[511];
  1.1081 +		b_2_pix_alloc[i] = b_2_pix_alloc[256];
  1.1082 +		b_2_pix_alloc[i+512] = b_2_pix_alloc[511];
  1.1083 +	}
  1.1084 +
  1.1085 +	/* You have chosen wisely... */
  1.1086 +	switch (format) {
  1.1087 +	    case SDL_YV12_OVERLAY:
  1.1088 +	    case SDL_IYUV_OVERLAY:
  1.1089 +		cpu_mmx = CPU_Flags() & MMX_CPU;
  1.1090 +		if ( display->format->BytesPerPixel == 2 ) {
  1.1091 +#if defined(i386) && defined(__GNUC__) && defined(USE_ASMBLIT)
  1.1092 +			/* inline assembly functions */
  1.1093 +			if ( cpu_mmx && (Rmask == 0xF800) &&
  1.1094 +			                (Gmask == 0x07E0) &&
  1.1095 +				        (Bmask == 0x001F) &&
  1.1096 +			                (width & 15) == 0) {
  1.1097 +/*printf("Using MMX 16-bit 565 dither\n");*/
  1.1098 +				swdata->Display1X = Color565DitherYV12MMX1X;
  1.1099 +			} else {
  1.1100 +/*printf("Using C 16-bit dither\n");*/
  1.1101 +				swdata->Display1X = Color16DitherYV12Mod1X;
  1.1102 +			}
  1.1103 +#else
  1.1104 +			swdata->Display1X = Color16DitherYV12Mod1X;
  1.1105 +#endif
  1.1106 +			swdata->Display2X = Color16DitherYV12Mod2X;
  1.1107 +		}
  1.1108 +		if ( display->format->BytesPerPixel == 3 ) {
  1.1109 +			swdata->Display1X = Color24DitherYV12Mod1X;
  1.1110 +			swdata->Display2X = Color24DitherYV12Mod2X;
  1.1111 +		}
  1.1112 +		if ( display->format->BytesPerPixel == 4 ) {
  1.1113 +#if defined(i386) && defined(__GNUC__) && defined(USE_ASMBLIT)
  1.1114 +			/* inline assembly functions */
  1.1115 +			if ( cpu_mmx && (Rmask == 0x00FF0000) &&
  1.1116 +			                (Gmask == 0x0000FF00) &&
  1.1117 +				        (Bmask == 0x000000FF) && 
  1.1118 +			                (width & 15) == 0) {
  1.1119 +/*printf("Using MMX 32-bit dither\n");*/
  1.1120 +				swdata->Display1X = ColorRGBDitherYV12MMX1X;
  1.1121 +			} else {
  1.1122 +/*printf("Using C 32-bit dither\n");*/
  1.1123 +				swdata->Display1X = Color32DitherYV12Mod1X;
  1.1124 +			}
  1.1125 +#else
  1.1126 +			swdata->Display1X = Color32DitherYV12Mod1X;
  1.1127 +#endif
  1.1128 +			swdata->Display2X = Color32DitherYV12Mod2X;
  1.1129 +		}
  1.1130 +		break;
  1.1131 +	    case SDL_YUY2_OVERLAY:
  1.1132 +	    case SDL_UYVY_OVERLAY:
  1.1133 +	    case SDL_YVYU_OVERLAY:
  1.1134 +		if ( display->format->BytesPerPixel == 2 ) {
  1.1135 +			swdata->Display1X = Color16DitherYUY2Mod1X;
  1.1136 +			swdata->Display2X = Color16DitherYUY2Mod2X;
  1.1137 +		}
  1.1138 +		if ( display->format->BytesPerPixel == 3 ) {
  1.1139 +			swdata->Display1X = Color24DitherYUY2Mod1X;
  1.1140 +			swdata->Display2X = Color24DitherYUY2Mod2X;
  1.1141 +		}
  1.1142 +		if ( display->format->BytesPerPixel == 4 ) {
  1.1143 +			swdata->Display1X = Color32DitherYUY2Mod1X;
  1.1144 +			swdata->Display2X = Color32DitherYUY2Mod2X;
  1.1145 +		}
  1.1146 +		break;
  1.1147 +	    default:
  1.1148 +		/* We should never get here (caught above) */
  1.1149 +		break;
  1.1150 +	}
  1.1151 +
  1.1152 +	/* Find the pitch and offset values for the overlay */
  1.1153 +	overlay->pitches = swdata->pitches;
  1.1154 +	overlay->pixels = swdata->planes;
  1.1155 +	switch (format) {
  1.1156 +	    case SDL_YV12_OVERLAY:
  1.1157 +	    case SDL_IYUV_OVERLAY:
  1.1158 +		overlay->pitches[0] = overlay->w;
  1.1159 +		overlay->pitches[1] = overlay->pitches[0] / 2;
  1.1160 +		overlay->pitches[2] = overlay->pitches[0] / 2;
  1.1161 +	        overlay->pixels[0] = swdata->pixels;
  1.1162 +	        overlay->pixels[1] = overlay->pixels[0] +
  1.1163 +		                     overlay->pitches[0] * overlay->h;
  1.1164 +	        overlay->pixels[2] = overlay->pixels[1] +
  1.1165 +		                     overlay->pitches[1] * overlay->h / 2;
  1.1166 +		overlay->planes = 3;
  1.1167 +		break;
  1.1168 +	    case SDL_YUY2_OVERLAY:
  1.1169 +	    case SDL_UYVY_OVERLAY:
  1.1170 +	    case SDL_YVYU_OVERLAY:
  1.1171 +		overlay->pitches[0] = overlay->w*2;
  1.1172 +	        overlay->pixels[0] = swdata->pixels;
  1.1173 +		overlay->planes = 1;
  1.1174 +		break;
  1.1175 +	    default:
  1.1176 +		/* We should never get here (caught above) */
  1.1177 +		break;
  1.1178 +	}
  1.1179 +
  1.1180 +	/* We're all done.. */
  1.1181 +	return(overlay);
  1.1182 +}
  1.1183 +
  1.1184 +int SDL_LockYUV_SW(_THIS, SDL_Overlay *overlay)
  1.1185 +{
  1.1186 +	return(0);
  1.1187 +}
  1.1188 +
  1.1189 +void SDL_UnlockYUV_SW(_THIS, SDL_Overlay *overlay)
  1.1190 +{
  1.1191 +	return;
  1.1192 +}
  1.1193 +
  1.1194 +int SDL_DisplayYUV_SW(_THIS, SDL_Overlay *overlay, SDL_Rect *dstrect)
  1.1195 +{
  1.1196 +	struct private_yuvhwdata *swdata;
  1.1197 +	SDL_Surface *stretch;
  1.1198 +	SDL_Surface *display;
  1.1199 +	int scale_2x;
  1.1200 +	Uint8 *lum, *Cr, *Cb;
  1.1201 +	Uint8 *dst;
  1.1202 +	int mod;
  1.1203 +
  1.1204 +	swdata = overlay->hwdata;
  1.1205 +	scale_2x = 0;
  1.1206 +	stretch = 0;
  1.1207 +	if ( (overlay->w != dstrect->w) || (overlay->h != dstrect->h) ) {
  1.1208 +		if ( (dstrect->w == 2*overlay->w) &&
  1.1209 +		     (dstrect->h == 2*overlay->h) ) {
  1.1210 +			scale_2x = 1;
  1.1211 +		} else {
  1.1212 +			if ( ! swdata->stretch ) {
  1.1213 +				display = swdata->display;
  1.1214 +				swdata->stretch = SDL_CreateRGBSurface(
  1.1215 +					SDL_SWSURFACE,
  1.1216 +					overlay->w, overlay->h,
  1.1217 +					display->format->BitsPerPixel,
  1.1218 +					display->format->Rmask,
  1.1219 +					display->format->Gmask,
  1.1220 +					display->format->Bmask, 0);
  1.1221 +				if ( ! swdata->stretch ) {
  1.1222 +					return(-1);
  1.1223 +				}
  1.1224 +			}
  1.1225 +			stretch = swdata->stretch;
  1.1226 +		}
  1.1227 +	}
  1.1228 +
  1.1229 +	if ( stretch ) {
  1.1230 +		display = stretch;
  1.1231 +	} else {
  1.1232 +		display = swdata->display;
  1.1233 +	}
  1.1234 +	switch (overlay->format) {
  1.1235 +	    case SDL_YV12_OVERLAY:
  1.1236 +		lum = overlay->pixels[0];
  1.1237 +		Cr =  overlay->pixels[1];
  1.1238 +		Cb =  overlay->pixels[2];
  1.1239 +		break;
  1.1240 +	    case SDL_IYUV_OVERLAY:
  1.1241 +		lum = overlay->pixels[0];
  1.1242 +		Cr =  overlay->pixels[2];
  1.1243 +		Cb =  overlay->pixels[1];
  1.1244 +		break;
  1.1245 +	    case SDL_YUY2_OVERLAY:
  1.1246 +		lum = overlay->pixels[0];
  1.1247 +		Cr = lum + 3;
  1.1248 +		Cb = lum + 1;
  1.1249 +		break;
  1.1250 +	    case SDL_UYVY_OVERLAY:
  1.1251 +		lum = overlay->pixels[0]+1;
  1.1252 +		Cr = lum + 1;
  1.1253 +		Cb = lum - 1;
  1.1254 +		break;
  1.1255 +	    case SDL_YVYU_OVERLAY:
  1.1256 +		lum = overlay->pixels[0];
  1.1257 +		Cr = lum + 1;
  1.1258 +		Cb = lum + 3;
  1.1259 +		break;
  1.1260 +	    default:
  1.1261 +		SDL_SetError("Unsupported YUV format in blit (??)");
  1.1262 +		return(-1);
  1.1263 +	}
  1.1264 +	if ( SDL_MUSTLOCK(display) ) {
  1.1265 +        	if ( SDL_LockSurface(display) < 0 ) {
  1.1266 +			return(-1);
  1.1267 +		}
  1.1268 +	}
  1.1269 +	if ( stretch ) {
  1.1270 +		dst = (Uint8 *)stretch->pixels;
  1.1271 +	} else {
  1.1272 +		dst = (Uint8 *)display->pixels
  1.1273 +			+ dstrect->x * display->format->BytesPerPixel
  1.1274 +			+ dstrect->y * display->pitch;
  1.1275 +	}
  1.1276 +	mod = (display->pitch / display->format->BytesPerPixel);
  1.1277 +
  1.1278 +	if ( scale_2x ) {
  1.1279 +		mod -= (overlay->w * 2);
  1.1280 +		swdata->Display2X(swdata->colortab, swdata->rgb_2_pix,
  1.1281 +		                  lum, Cr, Cb, dst, overlay->h, overlay->w,mod);
  1.1282 +	} else {
  1.1283 +		mod -= overlay->w;
  1.1284 +		swdata->Display1X(swdata->colortab, swdata->rgb_2_pix,
  1.1285 +		                  lum, Cr, Cb, dst, overlay->h, overlay->w,mod);
  1.1286 +	}
  1.1287 +	if ( SDL_MUSTLOCK(display) ) {
  1.1288 +		SDL_UnlockSurface(display);
  1.1289 +	}
  1.1290 +	if ( stretch ) {
  1.1291 +		display = swdata->display;
  1.1292 +		SDL_SoftStretch(stretch, NULL, display, dstrect);
  1.1293 +	}
  1.1294 +	SDL_UpdateRects(display, 1, dstrect);
  1.1295 +
  1.1296 +	return(0);
  1.1297 +}
  1.1298 +
  1.1299 +void SDL_FreeYUV_SW(_THIS, SDL_Overlay *overlay)
  1.1300 +{
  1.1301 +	struct private_yuvhwdata *swdata;
  1.1302 +
  1.1303 +	swdata = overlay->hwdata;
  1.1304 +	if ( swdata ) {
  1.1305 +		if ( swdata->pixels ) {
  1.1306 +			free(swdata->pixels);
  1.1307 +		}
  1.1308 +		if ( swdata->colortab ) {
  1.1309 +			free(swdata->colortab);
  1.1310 +		}
  1.1311 +		if ( swdata->rgb_2_pix ) {
  1.1312 +			free(swdata->rgb_2_pix);
  1.1313 +		}
  1.1314 +		free(swdata);
  1.1315 +	}
  1.1316 +}