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