src/video/SDL_yuv_sw.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 14 Jul 2007 07:28:45 +0000
changeset 2197 2ff40f30af31
parent 2172 bef26cfc8f79
child 2781 5651642f4a78
permissions -rw-r--r--
Merged r3261:3262 from branches/SDL-1.2: MMX/YUV with __OPTIMIZE__.

"Mac OS X/x86 won't build the MMX/YUV inline assembly without optimizations
enabled (not enough registers), so for now, we only build it if we see
the __OPTIMIZE__ #define, which GCC provides when you build at -O1 or higher."
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@1895
    24
/* This is the software implementation of the YUV texture support */
slouken@0
    25
slouken@0
    26
/* This code was derived from code carrying the following copyright notices:
slouken@0
    27
slouken@0
    28
 * Copyright (c) 1995 The Regents of the University of California.
slouken@0
    29
 * All rights reserved.
slouken@0
    30
 * 
slouken@0
    31
 * Permission to use, copy, modify, and distribute this software and its
slouken@0
    32
 * documentation for any purpose, without fee, and without written agreement is
slouken@0
    33
 * hereby granted, provided that the above copyright notice and the following
slouken@0
    34
 * two paragraphs appear in all copies of this software.
slouken@0
    35
 * 
slouken@0
    36
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
slouken@0
    37
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
slouken@0
    38
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
slouken@0
    39
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
slouken@0
    40
 * 
slouken@0
    41
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
slouken@0
    42
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
slouken@0
    43
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
slouken@0
    44
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
slouken@0
    45
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
slouken@0
    46
slouken@0
    47
 * Copyright (c) 1995 Erik Corry
slouken@0
    48
 * All rights reserved.
slouken@0
    49
 * 
slouken@0
    50
 * Permission to use, copy, modify, and distribute this software and its
slouken@0
    51
 * documentation for any purpose, without fee, and without written agreement is
slouken@0
    52
 * hereby granted, provided that the above copyright notice and the following
slouken@0
    53
 * two paragraphs appear in all copies of this software.
slouken@0
    54
 * 
slouken@0
    55
 * IN NO EVENT SHALL ERIK CORRY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
slouken@0
    56
 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
slouken@0
    57
 * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIK CORRY HAS BEEN ADVISED
slouken@0
    58
 * OF THE POSSIBILITY OF SUCH DAMAGE.
slouken@0
    59
 * 
slouken@0
    60
 * ERIK CORRY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
slouken@0
    61
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
slouken@0
    62
 * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
slouken@0
    63
 * BASIS, AND ERIK CORRY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
slouken@0
    64
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
slouken@0
    65
slouken@0
    66
 * Portions of this software Copyright (c) 1995 Brown University.
slouken@0
    67
 * All rights reserved.
slouken@0
    68
 * 
slouken@0
    69
 * Permission to use, copy, modify, and distribute this software and its
slouken@0
    70
 * documentation for any purpose, without fee, and without written agreement
slouken@0
    71
 * is hereby granted, provided that the above copyright notice and the
slouken@0
    72
 * following two paragraphs appear in all copies of this software.
slouken@0
    73
 * 
slouken@0
    74
 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
slouken@0
    75
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
slouken@0
    76
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
slouken@0
    77
 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
slouken@0
    78
 * 
slouken@0
    79
 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
slouken@0
    80
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
slouken@0
    81
 * PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
slouken@0
    82
 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
slouken@0
    83
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
slouken@0
    84
 */
slouken@0
    85
slouken@0
    86
#include "SDL_video.h"
slouken@739
    87
#include "SDL_cpuinfo.h"
slouken@0
    88
#include "SDL_stretch_c.h"
slouken@0
    89
#include "SDL_yuv_sw_c.h"
slouken@0
    90
slouken@1895
    91
slouken@1895
    92
struct SDL_SW_YUVTexture
slouken@1895
    93
{
slouken@1895
    94
    SDL_Texture *texture;
slouken@1895
    95
slouken@1895
    96
    Uint32 target_format;
slouken@1895
    97
    Uint8 *pixels;
slouken@1895
    98
    int *colortab;
slouken@1895
    99
    Uint32 *rgb_2_pix;
slouken@1895
   100
    void (*Display1X) (int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   101
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   102
                       unsigned char *cb, unsigned char *out,
slouken@1895
   103
                       int rows, int cols, int mod);
slouken@1895
   104
    void (*Display2X) (int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   105
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   106
                       unsigned char *cb, unsigned char *out,
slouken@1895
   107
                       int rows, int cols, int mod);
slouken@1895
   108
slouken@1895
   109
    /* These are just so we don't have to allocate them separately */
slouken@1895
   110
    Uint16 pitches[3];
slouken@1895
   111
    Uint8 *planes[3];
slouken@1895
   112
slouken@1895
   113
    /* This is a temporary surface in case we have to stretch copy */
slouken@1895
   114
    SDL_Surface *stretch;
slouken@1895
   115
    SDL_Surface *display;
slouken@0
   116
};
slouken@0
   117
slouken@0
   118
/* The colorspace conversion functions */
slouken@0
   119
icculus@2197
   120
#if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
slouken@1895
   121
extern void Color565DitherYV12MMX1X(int *colortab, Uint32 * rgb_2_pix,
slouken@0
   122
                                    unsigned char *lum, unsigned char *cr,
slouken@0
   123
                                    unsigned char *cb, unsigned char *out,
slouken@1895
   124
                                    int rows, int cols, int mod);
slouken@1895
   125
extern void ColorRGBDitherYV12MMX1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   126
                                    unsigned char *lum, unsigned char *cr,
slouken@1895
   127
                                    unsigned char *cb, unsigned char *out,
slouken@1895
   128
                                    int rows, int cols, int mod);
slouken@1895
   129
#endif
slouken@1895
   130
slouken@1895
   131
static void
slouken@1895
   132
Color16DitherYV12Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   133
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   134
                       unsigned char *cb, unsigned char *out,
slouken@1895
   135
                       int rows, int cols, int mod)
slouken@0
   136
{
slouken@1895
   137
    unsigned short *row1;
slouken@1895
   138
    unsigned short *row2;
slouken@1895
   139
    unsigned char *lum2;
slouken@0
   140
    int x, y;
slouken@0
   141
    int cr_r;
slouken@0
   142
    int crb_g;
slouken@0
   143
    int cb_b;
slouken@0
   144
    int cols_2 = cols / 2;
slouken@0
   145
slouken@1895
   146
    row1 = (unsigned short *) out;
slouken@0
   147
    row2 = row1 + cols + mod;
slouken@0
   148
    lum2 = lum + cols;
slouken@0
   149
slouken@0
   150
    mod += cols + mod;
slouken@0
   151
slouken@0
   152
    y = rows / 2;
slouken@1895
   153
    while (y--) {
slouken@0
   154
        x = cols_2;
slouken@1895
   155
        while (x--) {
slouken@0
   156
            register int L;
slouken@0
   157
slouken@1895
   158
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   159
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   160
                + colortab[*cb + 2 * 256];
slouken@1895
   161
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   162
            ++cr;
slouken@1895
   163
            ++cb;
slouken@0
   164
slouken@0
   165
            L = *lum++;
slouken@1895
   166
            *row1++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   167
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   168
                                        rgb_2_pix[L + cb_b]);
slouken@0
   169
slouken@0
   170
            L = *lum++;
slouken@1895
   171
            *row1++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   172
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   173
                                        rgb_2_pix[L + cb_b]);
slouken@0
   174
slouken@0
   175
slouken@0
   176
            /* Now, do second row.  */
slouken@0
   177
slouken@0
   178
            L = *lum2++;
slouken@1895
   179
            *row2++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   180
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   181
                                        rgb_2_pix[L + cb_b]);
slouken@0
   182
slouken@0
   183
            L = *lum2++;
slouken@1895
   184
            *row2++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   185
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   186
                                        rgb_2_pix[L + cb_b]);
slouken@0
   187
        }
slouken@0
   188
slouken@0
   189
        /*
slouken@0
   190
         * These values are at the start of the next line, (due
slouken@0
   191
         * to the ++'s above),but they need to be at the start
slouken@0
   192
         * of the line after that.
slouken@0
   193
         */
slouken@1895
   194
        lum += cols;
slouken@0
   195
        lum2 += cols;
slouken@0
   196
        row1 += mod;
slouken@0
   197
        row2 += mod;
slouken@0
   198
    }
slouken@0
   199
}
slouken@0
   200
slouken@1895
   201
static void
slouken@1895
   202
Color24DitherYV12Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   203
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   204
                       unsigned char *cb, unsigned char *out,
slouken@1895
   205
                       int rows, int cols, int mod)
slouken@0
   206
{
slouken@0
   207
    unsigned int value;
slouken@1895
   208
    unsigned char *row1;
slouken@1895
   209
    unsigned char *row2;
slouken@1895
   210
    unsigned char *lum2;
slouken@0
   211
    int x, y;
slouken@0
   212
    int cr_r;
slouken@0
   213
    int crb_g;
slouken@0
   214
    int cb_b;
slouken@0
   215
    int cols_2 = cols / 2;
slouken@0
   216
slouken@0
   217
    row1 = out;
slouken@1895
   218
    row2 = row1 + cols * 3 + mod * 3;
slouken@0
   219
    lum2 = lum + cols;
slouken@0
   220
slouken@0
   221
    mod += cols + mod;
slouken@0
   222
    mod *= 3;
slouken@0
   223
slouken@0
   224
    y = rows / 2;
slouken@1895
   225
    while (y--) {
slouken@0
   226
        x = cols_2;
slouken@1895
   227
        while (x--) {
slouken@0
   228
            register int L;
slouken@0
   229
slouken@1895
   230
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   231
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   232
                + colortab[*cb + 2 * 256];
slouken@1895
   233
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   234
            ++cr;
slouken@1895
   235
            ++cb;
slouken@0
   236
slouken@0
   237
            L = *lum++;
slouken@1895
   238
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   239
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   240
            *row1++ = (value) & 0xFF;
slouken@1895
   241
            *row1++ = (value >> 8) & 0xFF;
slouken@0
   242
            *row1++ = (value >> 16) & 0xFF;
slouken@0
   243
slouken@0
   244
            L = *lum++;
slouken@1895
   245
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   246
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   247
            *row1++ = (value) & 0xFF;
slouken@1895
   248
            *row1++ = (value >> 8) & 0xFF;
slouken@0
   249
            *row1++ = (value >> 16) & 0xFF;
slouken@0
   250
slouken@0
   251
slouken@0
   252
            /* Now, do second row.  */
slouken@0
   253
slouken@0
   254
            L = *lum2++;
slouken@1895
   255
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   256
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   257
            *row2++ = (value) & 0xFF;
slouken@1895
   258
            *row2++ = (value >> 8) & 0xFF;
slouken@0
   259
            *row2++ = (value >> 16) & 0xFF;
slouken@0
   260
slouken@0
   261
            L = *lum2++;
slouken@1895
   262
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   263
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   264
            *row2++ = (value) & 0xFF;
slouken@1895
   265
            *row2++ = (value >> 8) & 0xFF;
slouken@0
   266
            *row2++ = (value >> 16) & 0xFF;
slouken@0
   267
        }
slouken@0
   268
slouken@0
   269
        /*
slouken@0
   270
         * These values are at the start of the next line, (due
slouken@0
   271
         * to the ++'s above),but they need to be at the start
slouken@0
   272
         * of the line after that.
slouken@0
   273
         */
slouken@1895
   274
        lum += cols;
slouken@0
   275
        lum2 += cols;
slouken@0
   276
        row1 += mod;
slouken@0
   277
        row2 += mod;
slouken@0
   278
    }
slouken@0
   279
}
slouken@0
   280
slouken@1895
   281
static void
slouken@1895
   282
Color32DitherYV12Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   283
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   284
                       unsigned char *cb, unsigned char *out,
slouken@1895
   285
                       int rows, int cols, int mod)
slouken@0
   286
{
slouken@1895
   287
    unsigned int *row1;
slouken@1895
   288
    unsigned int *row2;
slouken@1895
   289
    unsigned char *lum2;
slouken@0
   290
    int x, y;
slouken@0
   291
    int cr_r;
slouken@0
   292
    int crb_g;
slouken@0
   293
    int cb_b;
slouken@0
   294
    int cols_2 = cols / 2;
slouken@0
   295
slouken@1895
   296
    row1 = (unsigned int *) out;
slouken@0
   297
    row2 = row1 + cols + mod;
slouken@0
   298
    lum2 = lum + cols;
slouken@0
   299
slouken@0
   300
    mod += cols + mod;
slouken@0
   301
slouken@0
   302
    y = rows / 2;
slouken@1895
   303
    while (y--) {
slouken@0
   304
        x = cols_2;
slouken@1895
   305
        while (x--) {
slouken@0
   306
            register int L;
slouken@0
   307
slouken@1895
   308
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   309
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   310
                + colortab[*cb + 2 * 256];
slouken@1895
   311
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   312
            ++cr;
slouken@1895
   313
            ++cb;
slouken@0
   314
slouken@0
   315
            L = *lum++;
slouken@1895
   316
            *row1++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   317
                       rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   318
slouken@0
   319
            L = *lum++;
slouken@1895
   320
            *row1++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   321
                       rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   322
slouken@0
   323
slouken@0
   324
            /* Now, do second row.  */
slouken@0
   325
slouken@0
   326
            L = *lum2++;
slouken@1895
   327
            *row2++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   328
                       rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   329
slouken@0
   330
            L = *lum2++;
slouken@1895
   331
            *row2++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   332
                       rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   333
        }
slouken@0
   334
slouken@0
   335
        /*
slouken@0
   336
         * These values are at the start of the next line, (due
slouken@0
   337
         * to the ++'s above),but they need to be at the start
slouken@0
   338
         * of the line after that.
slouken@0
   339
         */
slouken@1895
   340
        lum += cols;
slouken@0
   341
        lum2 += cols;
slouken@0
   342
        row1 += mod;
slouken@0
   343
        row2 += mod;
slouken@0
   344
    }
slouken@0
   345
}
slouken@0
   346
slouken@0
   347
/*
slouken@0
   348
 * In this function I make use of a nasty trick. The tables have the lower
slouken@0
   349
 * 16 bits replicated in the upper 16. This means I can write ints and get
slouken@0
   350
 * the horisontal doubling for free (almost).
slouken@0
   351
 */
slouken@1895
   352
static void
slouken@1895
   353
Color16DitherYV12Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   354
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   355
                       unsigned char *cb, unsigned char *out,
slouken@1895
   356
                       int rows, int cols, int mod)
slouken@0
   357
{
slouken@1895
   358
    unsigned int *row1 = (unsigned int *) out;
slouken@1895
   359
    const int next_row = cols + (mod / 2);
slouken@1895
   360
    unsigned int *row2 = row1 + 2 * next_row;
slouken@1895
   361
    unsigned char *lum2;
slouken@0
   362
    int x, y;
slouken@0
   363
    int cr_r;
slouken@0
   364
    int crb_g;
slouken@0
   365
    int cb_b;
slouken@0
   366
    int cols_2 = cols / 2;
slouken@0
   367
slouken@0
   368
    lum2 = lum + cols;
slouken@0
   369
slouken@1895
   370
    mod = (next_row * 3) + (mod / 2);
slouken@0
   371
slouken@0
   372
    y = rows / 2;
slouken@1895
   373
    while (y--) {
slouken@0
   374
        x = cols_2;
slouken@1895
   375
        while (x--) {
slouken@0
   376
            register int L;
slouken@0
   377
slouken@1895
   378
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   379
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   380
                + colortab[*cb + 2 * 256];
slouken@1895
   381
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   382
            ++cr;
slouken@1895
   383
            ++cb;
slouken@0
   384
slouken@0
   385
            L = *lum++;
slouken@1895
   386
            row1[0] = row1[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   387
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   388
                                        rgb_2_pix[L + cb_b]);
slouken@0
   389
            row1++;
slouken@0
   390
slouken@0
   391
            L = *lum++;
slouken@1895
   392
            row1[0] = row1[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   393
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   394
                                        rgb_2_pix[L + cb_b]);
slouken@0
   395
            row1++;
slouken@0
   396
slouken@0
   397
slouken@0
   398
            /* Now, do second row. */
slouken@0
   399
slouken@0
   400
            L = *lum2++;
slouken@1895
   401
            row2[0] = row2[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   402
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   403
                                        rgb_2_pix[L + cb_b]);
slouken@0
   404
            row2++;
slouken@0
   405
slouken@0
   406
            L = *lum2++;
slouken@1895
   407
            row2[0] = row2[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   408
                                        rgb_2_pix[L + crb_g] |
slouken@1895
   409
                                        rgb_2_pix[L + cb_b]);
slouken@0
   410
            row2++;
slouken@0
   411
        }
slouken@0
   412
slouken@0
   413
        /*
slouken@0
   414
         * These values are at the start of the next line, (due
slouken@0
   415
         * to the ++'s above),but they need to be at the start
slouken@0
   416
         * of the line after that.
slouken@0
   417
         */
slouken@1895
   418
        lum += cols;
slouken@0
   419
        lum2 += cols;
slouken@0
   420
        row1 += mod;
slouken@0
   421
        row2 += mod;
slouken@0
   422
    }
slouken@0
   423
}
slouken@0
   424
slouken@1895
   425
static void
slouken@1895
   426
Color24DitherYV12Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   427
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   428
                       unsigned char *cb, unsigned char *out,
slouken@1895
   429
                       int rows, int cols, int mod)
slouken@0
   430
{
slouken@0
   431
    unsigned int value;
slouken@1895
   432
    unsigned char *row1 = out;
slouken@1895
   433
    const int next_row = (cols * 2 + mod) * 3;
slouken@1895
   434
    unsigned char *row2 = row1 + 2 * next_row;
slouken@1895
   435
    unsigned char *lum2;
slouken@0
   436
    int x, y;
slouken@0
   437
    int cr_r;
slouken@0
   438
    int crb_g;
slouken@0
   439
    int cb_b;
slouken@0
   440
    int cols_2 = cols / 2;
slouken@0
   441
slouken@0
   442
    lum2 = lum + cols;
slouken@0
   443
slouken@1895
   444
    mod = next_row * 3 + mod * 3;
slouken@0
   445
slouken@0
   446
    y = rows / 2;
slouken@1895
   447
    while (y--) {
slouken@0
   448
        x = cols_2;
slouken@1895
   449
        while (x--) {
slouken@0
   450
            register int L;
slouken@0
   451
slouken@1895
   452
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   453
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   454
                + colortab[*cb + 2 * 256];
slouken@1895
   455
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   456
            ++cr;
slouken@1895
   457
            ++cb;
slouken@0
   458
slouken@0
   459
            L = *lum++;
slouken@1895
   460
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   461
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   462
            row1[0 + 0] = row1[3 + 0] = row1[next_row + 0] =
slouken@1895
   463
                row1[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   464
            row1[0 + 1] = row1[3 + 1] = row1[next_row + 1] =
slouken@1895
   465
                row1[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   466
            row1[0 + 2] = row1[3 + 2] = row1[next_row + 2] =
slouken@1895
   467
                row1[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   468
            row1 += 2 * 3;
slouken@0
   469
slouken@0
   470
            L = *lum++;
slouken@1895
   471
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   472
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   473
            row1[0 + 0] = row1[3 + 0] = row1[next_row + 0] =
slouken@1895
   474
                row1[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   475
            row1[0 + 1] = row1[3 + 1] = row1[next_row + 1] =
slouken@1895
   476
                row1[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   477
            row1[0 + 2] = row1[3 + 2] = row1[next_row + 2] =
slouken@1895
   478
                row1[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   479
            row1 += 2 * 3;
slouken@0
   480
slouken@0
   481
slouken@0
   482
            /* Now, do second row. */
slouken@0
   483
slouken@0
   484
            L = *lum2++;
slouken@1895
   485
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   486
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   487
            row2[0 + 0] = row2[3 + 0] = row2[next_row + 0] =
slouken@1895
   488
                row2[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   489
            row2[0 + 1] = row2[3 + 1] = row2[next_row + 1] =
slouken@1895
   490
                row2[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   491
            row2[0 + 2] = row2[3 + 2] = row2[next_row + 2] =
slouken@1895
   492
                row2[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   493
            row2 += 2 * 3;
slouken@0
   494
slouken@0
   495
            L = *lum2++;
slouken@1895
   496
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   497
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   498
            row2[0 + 0] = row2[3 + 0] = row2[next_row + 0] =
slouken@1895
   499
                row2[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   500
            row2[0 + 1] = row2[3 + 1] = row2[next_row + 1] =
slouken@1895
   501
                row2[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   502
            row2[0 + 2] = row2[3 + 2] = row2[next_row + 2] =
slouken@1895
   503
                row2[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   504
            row2 += 2 * 3;
slouken@0
   505
        }
slouken@0
   506
slouken@0
   507
        /*
slouken@0
   508
         * These values are at the start of the next line, (due
slouken@0
   509
         * to the ++'s above),but they need to be at the start
slouken@0
   510
         * of the line after that.
slouken@0
   511
         */
slouken@1895
   512
        lum += cols;
slouken@0
   513
        lum2 += cols;
slouken@0
   514
        row1 += mod;
slouken@0
   515
        row2 += mod;
slouken@0
   516
    }
slouken@0
   517
}
slouken@0
   518
slouken@1895
   519
static void
slouken@1895
   520
Color32DitherYV12Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   521
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   522
                       unsigned char *cb, unsigned char *out,
slouken@1895
   523
                       int rows, int cols, int mod)
slouken@0
   524
{
slouken@1895
   525
    unsigned int *row1 = (unsigned int *) out;
slouken@1895
   526
    const int next_row = cols * 2 + mod;
slouken@1895
   527
    unsigned int *row2 = row1 + 2 * next_row;
slouken@1895
   528
    unsigned char *lum2;
slouken@0
   529
    int x, y;
slouken@0
   530
    int cr_r;
slouken@0
   531
    int crb_g;
slouken@0
   532
    int cb_b;
slouken@0
   533
    int cols_2 = cols / 2;
slouken@0
   534
slouken@0
   535
    lum2 = lum + cols;
slouken@0
   536
slouken@0
   537
    mod = (next_row * 3) + mod;
slouken@0
   538
slouken@0
   539
    y = rows / 2;
slouken@1895
   540
    while (y--) {
slouken@0
   541
        x = cols_2;
slouken@1895
   542
        while (x--) {
slouken@0
   543
            register int L;
slouken@0
   544
slouken@1895
   545
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   546
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   547
                + colortab[*cb + 2 * 256];
slouken@1895
   548
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   549
            ++cr;
slouken@1895
   550
            ++cb;
slouken@0
   551
slouken@0
   552
            L = *lum++;
slouken@1895
   553
            row1[0] = row1[1] = row1[next_row] = row1[next_row + 1] =
slouken@1895
   554
                (rgb_2_pix[L + cr_r] |
slouken@1895
   555
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   556
            row1 += 2;
slouken@0
   557
slouken@0
   558
            L = *lum++;
slouken@1895
   559
            row1[0] = row1[1] = row1[next_row] = row1[next_row + 1] =
slouken@1895
   560
                (rgb_2_pix[L + cr_r] |
slouken@1895
   561
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   562
            row1 += 2;
slouken@0
   563
slouken@0
   564
slouken@0
   565
            /* Now, do second row. */
slouken@0
   566
slouken@0
   567
            L = *lum2++;
slouken@1895
   568
            row2[0] = row2[1] = row2[next_row] = row2[next_row + 1] =
slouken@1895
   569
                (rgb_2_pix[L + cr_r] |
slouken@1895
   570
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   571
            row2 += 2;
slouken@0
   572
slouken@0
   573
            L = *lum2++;
slouken@1895
   574
            row2[0] = row2[1] = row2[next_row] = row2[next_row + 1] =
slouken@1895
   575
                (rgb_2_pix[L + cr_r] |
slouken@1895
   576
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   577
            row2 += 2;
slouken@0
   578
        }
slouken@0
   579
slouken@0
   580
        /*
slouken@0
   581
         * These values are at the start of the next line, (due
slouken@0
   582
         * to the ++'s above),but they need to be at the start
slouken@0
   583
         * of the line after that.
slouken@0
   584
         */
slouken@1895
   585
        lum += cols;
slouken@0
   586
        lum2 += cols;
slouken@0
   587
        row1 += mod;
slouken@0
   588
        row2 += mod;
slouken@0
   589
    }
slouken@0
   590
}
slouken@0
   591
slouken@1895
   592
static void
slouken@1895
   593
Color16DitherYUY2Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   594
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   595
                       unsigned char *cb, unsigned char *out,
slouken@1895
   596
                       int rows, int cols, int mod)
slouken@0
   597
{
slouken@1895
   598
    unsigned short *row;
slouken@0
   599
    int x, y;
slouken@0
   600
    int cr_r;
slouken@0
   601
    int crb_g;
slouken@0
   602
    int cb_b;
slouken@0
   603
    int cols_2 = cols / 2;
slouken@0
   604
slouken@1895
   605
    row = (unsigned short *) out;
slouken@0
   606
slouken@0
   607
    y = rows;
slouken@1895
   608
    while (y--) {
slouken@0
   609
        x = cols_2;
slouken@1895
   610
        while (x--) {
slouken@0
   611
            register int L;
slouken@0
   612
slouken@1895
   613
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   614
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   615
                + colortab[*cb + 2 * 256];
slouken@1895
   616
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   617
            cr += 4;
slouken@1895
   618
            cb += 4;
slouken@0
   619
slouken@1895
   620
            L = *lum;
slouken@1895
   621
            lum += 2;
slouken@1895
   622
            *row++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   623
                                       rgb_2_pix[L + crb_g] |
slouken@1895
   624
                                       rgb_2_pix[L + cb_b]);
slouken@0
   625
slouken@1895
   626
            L = *lum;
slouken@1895
   627
            lum += 2;
slouken@1895
   628
            *row++ = (unsigned short) (rgb_2_pix[L + cr_r] |
slouken@1895
   629
                                       rgb_2_pix[L + crb_g] |
slouken@1895
   630
                                       rgb_2_pix[L + cb_b]);
slouken@0
   631
slouken@0
   632
        }
slouken@0
   633
slouken@0
   634
        row += mod;
slouken@0
   635
    }
slouken@0
   636
}
slouken@0
   637
slouken@1895
   638
static void
slouken@1895
   639
Color24DitherYUY2Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   640
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   641
                       unsigned char *cb, unsigned char *out,
slouken@1895
   642
                       int rows, int cols, int mod)
slouken@0
   643
{
slouken@0
   644
    unsigned int value;
slouken@1895
   645
    unsigned char *row;
slouken@0
   646
    int x, y;
slouken@0
   647
    int cr_r;
slouken@0
   648
    int crb_g;
slouken@0
   649
    int cb_b;
slouken@0
   650
    int cols_2 = cols / 2;
slouken@0
   651
slouken@1895
   652
    row = (unsigned char *) out;
slouken@0
   653
    mod *= 3;
slouken@0
   654
    y = rows;
slouken@1895
   655
    while (y--) {
slouken@0
   656
        x = cols_2;
slouken@1895
   657
        while (x--) {
slouken@0
   658
            register int L;
slouken@0
   659
slouken@1895
   660
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   661
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   662
                + colortab[*cb + 2 * 256];
slouken@1895
   663
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   664
            cr += 4;
slouken@1895
   665
            cb += 4;
slouken@0
   666
slouken@1895
   667
            L = *lum;
slouken@1895
   668
            lum += 2;
slouken@1895
   669
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   670
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   671
            *row++ = (value) & 0xFF;
slouken@1895
   672
            *row++ = (value >> 8) & 0xFF;
slouken@0
   673
            *row++ = (value >> 16) & 0xFF;
slouken@0
   674
slouken@1895
   675
            L = *lum;
slouken@1895
   676
            lum += 2;
slouken@1895
   677
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   678
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   679
            *row++ = (value) & 0xFF;
slouken@1895
   680
            *row++ = (value >> 8) & 0xFF;
slouken@0
   681
            *row++ = (value >> 16) & 0xFF;
slouken@0
   682
slouken@0
   683
        }
slouken@0
   684
        row += mod;
slouken@0
   685
    }
slouken@0
   686
}
slouken@0
   687
slouken@1895
   688
static void
slouken@1895
   689
Color32DitherYUY2Mod1X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   690
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   691
                       unsigned char *cb, unsigned char *out,
slouken@1895
   692
                       int rows, int cols, int mod)
slouken@0
   693
{
slouken@1895
   694
    unsigned int *row;
slouken@0
   695
    int x, y;
slouken@0
   696
    int cr_r;
slouken@0
   697
    int crb_g;
slouken@0
   698
    int cb_b;
slouken@0
   699
    int cols_2 = cols / 2;
slouken@0
   700
slouken@1895
   701
    row = (unsigned int *) out;
slouken@0
   702
    y = rows;
slouken@1895
   703
    while (y--) {
slouken@0
   704
        x = cols_2;
slouken@1895
   705
        while (x--) {
slouken@0
   706
            register int L;
slouken@0
   707
slouken@1895
   708
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   709
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   710
                + colortab[*cb + 2 * 256];
slouken@1895
   711
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   712
            cr += 4;
slouken@1895
   713
            cb += 4;
slouken@0
   714
slouken@1895
   715
            L = *lum;
slouken@1895
   716
            lum += 2;
slouken@1895
   717
            *row++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   718
                      rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   719
slouken@1895
   720
            L = *lum;
slouken@1895
   721
            lum += 2;
slouken@1895
   722
            *row++ = (rgb_2_pix[L + cr_r] |
slouken@1895
   723
                      rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   724
slouken@0
   725
slouken@0
   726
        }
slouken@0
   727
        row += mod;
slouken@0
   728
    }
slouken@0
   729
}
slouken@0
   730
slouken@0
   731
/*
slouken@0
   732
 * In this function I make use of a nasty trick. The tables have the lower
slouken@0
   733
 * 16 bits replicated in the upper 16. This means I can write ints and get
slouken@0
   734
 * the horisontal doubling for free (almost).
slouken@0
   735
 */
slouken@1895
   736
static void
slouken@1895
   737
Color16DitherYUY2Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   738
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   739
                       unsigned char *cb, unsigned char *out,
slouken@1895
   740
                       int rows, int cols, int mod)
slouken@0
   741
{
slouken@1895
   742
    unsigned int *row = (unsigned int *) out;
slouken@1895
   743
    const int next_row = cols + (mod / 2);
slouken@0
   744
    int x, y;
slouken@0
   745
    int cr_r;
slouken@0
   746
    int crb_g;
slouken@0
   747
    int cb_b;
slouken@0
   748
    int cols_2 = cols / 2;
slouken@0
   749
slouken@0
   750
    y = rows;
slouken@1895
   751
    while (y--) {
slouken@0
   752
        x = cols_2;
slouken@1895
   753
        while (x--) {
slouken@0
   754
            register int L;
slouken@0
   755
slouken@1895
   756
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   757
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   758
                + colortab[*cb + 2 * 256];
slouken@1895
   759
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   760
            cr += 4;
slouken@1895
   761
            cb += 4;
slouken@0
   762
slouken@1895
   763
            L = *lum;
slouken@1895
   764
            lum += 2;
slouken@1895
   765
            row[0] = row[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   766
                                      rgb_2_pix[L + crb_g] |
slouken@1895
   767
                                      rgb_2_pix[L + cb_b]);
slouken@0
   768
            row++;
slouken@0
   769
slouken@1895
   770
            L = *lum;
slouken@1895
   771
            lum += 2;
slouken@1895
   772
            row[0] = row[next_row] = (rgb_2_pix[L + cr_r] |
slouken@1895
   773
                                      rgb_2_pix[L + crb_g] |
slouken@1895
   774
                                      rgb_2_pix[L + cb_b]);
slouken@0
   775
            row++;
slouken@0
   776
slouken@0
   777
        }
slouken@0
   778
        row += next_row;
slouken@0
   779
    }
slouken@0
   780
}
slouken@0
   781
slouken@1895
   782
static void
slouken@1895
   783
Color24DitherYUY2Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   784
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   785
                       unsigned char *cb, unsigned char *out,
slouken@1895
   786
                       int rows, int cols, int mod)
slouken@0
   787
{
slouken@0
   788
    unsigned int value;
slouken@1895
   789
    unsigned char *row = out;
slouken@1895
   790
    const int next_row = (cols * 2 + mod) * 3;
slouken@0
   791
    int x, y;
slouken@0
   792
    int cr_r;
slouken@0
   793
    int crb_g;
slouken@0
   794
    int cb_b;
slouken@0
   795
    int cols_2 = cols / 2;
slouken@0
   796
    y = rows;
slouken@1895
   797
    while (y--) {
slouken@0
   798
        x = cols_2;
slouken@1895
   799
        while (x--) {
slouken@0
   800
            register int L;
slouken@0
   801
slouken@1895
   802
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   803
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   804
                + colortab[*cb + 2 * 256];
slouken@1895
   805
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   806
            cr += 4;
slouken@1895
   807
            cb += 4;
slouken@0
   808
slouken@1895
   809
            L = *lum;
slouken@1895
   810
            lum += 2;
slouken@1895
   811
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   812
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   813
            row[0 + 0] = row[3 + 0] = row[next_row + 0] =
slouken@1895
   814
                row[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   815
            row[0 + 1] = row[3 + 1] = row[next_row + 1] =
slouken@1895
   816
                row[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   817
            row[0 + 2] = row[3 + 2] = row[next_row + 2] =
slouken@1895
   818
                row[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   819
            row += 2 * 3;
slouken@0
   820
slouken@1895
   821
            L = *lum;
slouken@1895
   822
            lum += 2;
slouken@1895
   823
            value = (rgb_2_pix[L + cr_r] |
slouken@1895
   824
                     rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@1895
   825
            row[0 + 0] = row[3 + 0] = row[next_row + 0] =
slouken@1895
   826
                row[next_row + 3 + 0] = (value) & 0xFF;
slouken@1895
   827
            row[0 + 1] = row[3 + 1] = row[next_row + 1] =
slouken@1895
   828
                row[next_row + 3 + 1] = (value >> 8) & 0xFF;
slouken@1895
   829
            row[0 + 2] = row[3 + 2] = row[next_row + 2] =
slouken@1895
   830
                row[next_row + 3 + 2] = (value >> 16) & 0xFF;
slouken@1895
   831
            row += 2 * 3;
slouken@0
   832
slouken@0
   833
        }
slouken@0
   834
        row += next_row;
slouken@0
   835
    }
slouken@0
   836
}
slouken@0
   837
slouken@1895
   838
static void
slouken@1895
   839
Color32DitherYUY2Mod2X(int *colortab, Uint32 * rgb_2_pix,
slouken@1895
   840
                       unsigned char *lum, unsigned char *cr,
slouken@1895
   841
                       unsigned char *cb, unsigned char *out,
slouken@1895
   842
                       int rows, int cols, int mod)
slouken@0
   843
{
slouken@1895
   844
    unsigned int *row = (unsigned int *) out;
slouken@1895
   845
    const int next_row = cols * 2 + mod;
slouken@0
   846
    int x, y;
slouken@0
   847
    int cr_r;
slouken@0
   848
    int crb_g;
slouken@0
   849
    int cb_b;
slouken@0
   850
    int cols_2 = cols / 2;
slouken@1895
   851
    mod += mod;
slouken@0
   852
    y = rows;
slouken@1895
   853
    while (y--) {
slouken@0
   854
        x = cols_2;
slouken@1895
   855
        while (x--) {
slouken@0
   856
            register int L;
slouken@0
   857
slouken@1895
   858
            cr_r = 0 * 768 + 256 + colortab[*cr + 0 * 256];
slouken@1895
   859
            crb_g = 1 * 768 + 256 + colortab[*cr + 1 * 256]
slouken@1895
   860
                + colortab[*cb + 2 * 256];
slouken@1895
   861
            cb_b = 2 * 768 + 256 + colortab[*cb + 3 * 256];
slouken@1895
   862
            cr += 4;
slouken@1895
   863
            cb += 4;
slouken@0
   864
slouken@1895
   865
            L = *lum;
slouken@1895
   866
            lum += 2;
slouken@1895
   867
            row[0] = row[1] = row[next_row] = row[next_row + 1] =
slouken@1895
   868
                (rgb_2_pix[L + cr_r] |
slouken@1895
   869
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   870
            row += 2;
slouken@0
   871
slouken@1895
   872
            L = *lum;
slouken@1895
   873
            lum += 2;
slouken@1895
   874
            row[0] = row[1] = row[next_row] = row[next_row + 1] =
slouken@1895
   875
                (rgb_2_pix[L + cr_r] |
slouken@1895
   876
                 rgb_2_pix[L + crb_g] | rgb_2_pix[L + cb_b]);
slouken@0
   877
            row += 2;
slouken@0
   878
slouken@0
   879
slouken@0
   880
        }
slouken@0
   881
slouken@0
   882
        row += next_row;
slouken@0
   883
    }
slouken@0
   884
}
slouken@0
   885
slouken@0
   886
/*
slouken@0
   887
 * How many 1 bits are there in the Uint32.
slouken@0
   888
 * Low performance, do not call often.
slouken@0
   889
 */
slouken@1895
   890
static int
slouken@1895
   891
number_of_bits_set(Uint32 a)
slouken@0
   892
{
slouken@1895
   893
    if (!a)
slouken@1895
   894
        return 0;
slouken@1895
   895
    if (a & 1)
slouken@1895
   896
        return 1 + number_of_bits_set(a >> 1);
slouken@1895
   897
    return (number_of_bits_set(a >> 1));
slouken@0
   898
}
slouken@0
   899
slouken@0
   900
/*
slouken@0
   901
 * How many 0 bits are there at least significant end of Uint32.
slouken@0
   902
 * Low performance, do not call often.
slouken@0
   903
 */
slouken@1895
   904
static int
slouken@1895
   905
free_bits_at_bottom(Uint32 a)
slouken@0
   906
{
slouken@1895
   907
    /* assume char is 8 bits */
slouken@1895
   908
    if (!a)
slouken@1895
   909
        return sizeof(Uint32) * 8;
slouken@1895
   910
    if (((Sint32) a) & 1l)
slouken@1895
   911
        return 0;
slouken@1895
   912
    return 1 + free_bits_at_bottom(a >> 1);
slouken@0
   913
}
slouken@0
   914
slouken@1895
   915
static int
slouken@1895
   916
SDL_SW_SetupYUVDisplay(SDL_SW_YUVTexture * swdata, Uint32 target_format)
slouken@1895
   917
{
slouken@1895
   918
    Uint32 *r_2_pix_alloc;
slouken@1895
   919
    Uint32 *g_2_pix_alloc;
slouken@1895
   920
    Uint32 *b_2_pix_alloc;
slouken@1895
   921
    int i;
slouken@1895
   922
    int bpp;
slouken@1895
   923
    Uint32 Rmask, Gmask, Bmask, Amask;
slouken@0
   924
slouken@1895
   925
    if (!SDL_PixelFormatEnumToMasks
slouken@1895
   926
        (target_format, &bpp, &Rmask, &Gmask, &Bmask, &Amask) || bpp < 15) {
slouken@1895
   927
        SDL_SetError("Unsupported YUV destination format");
slouken@1895
   928
        return -1;
slouken@1895
   929
    }
slouken@0
   930
slouken@1895
   931
    swdata->target_format = target_format;
slouken@1895
   932
    r_2_pix_alloc = &swdata->rgb_2_pix[0 * 768];
slouken@1895
   933
    g_2_pix_alloc = &swdata->rgb_2_pix[1 * 768];
slouken@1895
   934
    b_2_pix_alloc = &swdata->rgb_2_pix[2 * 768];
slouken@0
   935
slouken@1895
   936
    /* 
slouken@1895
   937
     * Set up entries 0-255 in rgb-to-pixel value tables.
slouken@1895
   938
     */
slouken@1895
   939
    for (i = 0; i < 256; ++i) {
slouken@1895
   940
        r_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Rmask));
slouken@1895
   941
        r_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Rmask);
slouken@1895
   942
        g_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Gmask));
slouken@1895
   943
        g_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Gmask);
slouken@1895
   944
        b_2_pix_alloc[i + 256] = i >> (8 - number_of_bits_set(Bmask));
slouken@1895
   945
        b_2_pix_alloc[i + 256] <<= free_bits_at_bottom(Bmask);
slouken@1895
   946
    }
slouken@0
   947
slouken@1895
   948
    /*
slouken@1895
   949
     * If we have 16-bit output depth, then we double the value
slouken@1895
   950
     * in the top word. This means that we can write out both
slouken@1895
   951
     * pixels in the pixel doubling mode with one op. It is 
slouken@1895
   952
     * harmless in the normal case as storing a 32-bit value
slouken@1895
   953
     * through a short pointer will lose the top bits anyway.
slouken@1895
   954
     */
slouken@1895
   955
    if (SDL_BYTESPERPIXEL(target_format) == 2) {
slouken@1895
   956
        for (i = 0; i < 256; ++i) {
slouken@1895
   957
            r_2_pix_alloc[i + 256] |= (r_2_pix_alloc[i + 256]) << 16;
slouken@1895
   958
            g_2_pix_alloc[i + 256] |= (g_2_pix_alloc[i + 256]) << 16;
slouken@1895
   959
            b_2_pix_alloc[i + 256] |= (b_2_pix_alloc[i + 256]) << 16;
slouken@1895
   960
        }
slouken@1895
   961
    }
slouken@0
   962
slouken@1895
   963
    /*
slouken@1895
   964
     * Spread out the values we have to the rest of the array so that
slouken@1895
   965
     * we do not need to check for overflow.
slouken@1895
   966
     */
slouken@1895
   967
    for (i = 0; i < 256; ++i) {
slouken@1895
   968
        r_2_pix_alloc[i] = r_2_pix_alloc[256];
slouken@1895
   969
        r_2_pix_alloc[i + 512] = r_2_pix_alloc[511];
slouken@1895
   970
        g_2_pix_alloc[i] = g_2_pix_alloc[256];
slouken@1895
   971
        g_2_pix_alloc[i + 512] = g_2_pix_alloc[511];
slouken@1895
   972
        b_2_pix_alloc[i] = b_2_pix_alloc[256];
slouken@1895
   973
        b_2_pix_alloc[i + 512] = b_2_pix_alloc[511];
slouken@1895
   974
    }
slouken@0
   975
slouken@1895
   976
    /* You have chosen wisely... */
slouken@1895
   977
    switch (swdata->texture->format) {
slouken@1965
   978
    case SDL_PIXELFORMAT_YV12:
slouken@1965
   979
    case SDL_PIXELFORMAT_IYUV:
slouken@1895
   980
        if (SDL_BYTESPERPIXEL(target_format) == 2) {
icculus@2197
   981
#if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
slouken@1895
   982
            /* inline assembly functions */
slouken@1895
   983
            if (SDL_HasMMX() && (Rmask == 0xF800) &&
slouken@2172
   984
                (Gmask == 0x07E0) && (Bmask == 0x001F)
slouken@2172
   985
                && (swdata->texture->w & 15) == 0) {
slouken@1895
   986
/*printf("Using MMX 16-bit 565 dither\n");*/
slouken@1895
   987
                swdata->Display1X = Color565DitherYV12MMX1X;
slouken@1895
   988
            } else {
slouken@1895
   989
/*printf("Using C 16-bit dither\n");*/
slouken@1895
   990
                swdata->Display1X = Color16DitherYV12Mod1X;
slouken@1895
   991
            }
slouken@1895
   992
#else
slouken@1895
   993
            swdata->Display1X = Color16DitherYV12Mod1X;
slouken@1895
   994
#endif
slouken@1895
   995
            swdata->Display2X = Color16DitherYV12Mod2X;
slouken@1895
   996
        }
slouken@1895
   997
        if (SDL_BYTESPERPIXEL(target_format) == 3) {
slouken@1895
   998
            swdata->Display1X = Color24DitherYV12Mod1X;
slouken@1895
   999
            swdata->Display2X = Color24DitherYV12Mod2X;
slouken@1895
  1000
        }
slouken@1895
  1001
        if (SDL_BYTESPERPIXEL(target_format) == 4) {
icculus@2197
  1002
#if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
slouken@1895
  1003
            /* inline assembly functions */
slouken@1895
  1004
            if (SDL_HasMMX() && (Rmask == 0x00FF0000) &&
slouken@1895
  1005
                (Gmask == 0x0000FF00) &&
slouken@2172
  1006
                (Bmask == 0x000000FF) && (swdata->texture->w & 15) == 0) {
slouken@1895
  1007
/*printf("Using MMX 32-bit dither\n");*/
slouken@1895
  1008
                swdata->Display1X = ColorRGBDitherYV12MMX1X;
slouken@1895
  1009
            } else {
slouken@1895
  1010
/*printf("Using C 32-bit dither\n");*/
slouken@1895
  1011
                swdata->Display1X = Color32DitherYV12Mod1X;
slouken@1895
  1012
            }
slouken@1895
  1013
#else
slouken@1895
  1014
            swdata->Display1X = Color32DitherYV12Mod1X;
slouken@1895
  1015
#endif
slouken@1895
  1016
            swdata->Display2X = Color32DitherYV12Mod2X;
slouken@1895
  1017
        }
slouken@1895
  1018
        break;
slouken@1965
  1019
    case SDL_PIXELFORMAT_YUY2:
slouken@1965
  1020
    case SDL_PIXELFORMAT_UYVY:
slouken@1965
  1021
    case SDL_PIXELFORMAT_YVYU:
slouken@1895
  1022
        if (SDL_BYTESPERPIXEL(target_format) == 2) {
slouken@1895
  1023
            swdata->Display1X = Color16DitherYUY2Mod1X;
slouken@1895
  1024
            swdata->Display2X = Color16DitherYUY2Mod2X;
slouken@1895
  1025
        }
slouken@1895
  1026
        if (SDL_BYTESPERPIXEL(target_format) == 3) {
slouken@1895
  1027
            swdata->Display1X = Color24DitherYUY2Mod1X;
slouken@1895
  1028
            swdata->Display2X = Color24DitherYUY2Mod2X;
slouken@1895
  1029
        }
slouken@1895
  1030
        if (SDL_BYTESPERPIXEL(target_format) == 4) {
slouken@1895
  1031
            swdata->Display1X = Color32DitherYUY2Mod1X;
slouken@1895
  1032
            swdata->Display2X = Color32DitherYUY2Mod2X;
slouken@1895
  1033
        }
slouken@1895
  1034
        break;
slouken@1895
  1035
    default:
slouken@1895
  1036
        /* We should never get here (caught above) */
slouken@1895
  1037
        break;
slouken@1895
  1038
    }
slouken@0
  1039
slouken@1895
  1040
    if (swdata->display) {
slouken@1895
  1041
        SDL_FreeSurface(swdata->display);
slouken@1895
  1042
        swdata->display = NULL;
slouken@1895
  1043
    }
slouken@1895
  1044
    return 0;
slouken@0
  1045
}
slouken@0
  1046
slouken@1895
  1047
SDL_SW_YUVTexture *
slouken@1895
  1048
SDL_SW_CreateYUVTexture(SDL_Texture * texture)
slouken@0
  1049
{
slouken@1895
  1050
    SDL_SW_YUVTexture *swdata;
slouken@1895
  1051
    int *Cr_r_tab;
slouken@1895
  1052
    int *Cr_g_tab;
slouken@1895
  1053
    int *Cb_g_tab;
slouken@1895
  1054
    int *Cb_b_tab;
slouken@1895
  1055
    int i;
slouken@1895
  1056
    int CR, CB;
slouken@1895
  1057
slouken@1920
  1058
    swdata = (SDL_SW_YUVTexture *) SDL_calloc(1, sizeof(*swdata));
slouken@1895
  1059
    if (!swdata) {
slouken@1895
  1060
        SDL_OutOfMemory();
slouken@1895
  1061
        return NULL;
slouken@1895
  1062
    }
slouken@1895
  1063
slouken@1895
  1064
    switch (texture->format) {
slouken@1965
  1065
    case SDL_PIXELFORMAT_YV12:
slouken@1965
  1066
    case SDL_PIXELFORMAT_IYUV:
slouken@1965
  1067
    case SDL_PIXELFORMAT_YUY2:
slouken@1965
  1068
    case SDL_PIXELFORMAT_UYVY:
slouken@1965
  1069
    case SDL_PIXELFORMAT_YVYU:
slouken@1895
  1070
        break;
slouken@1895
  1071
    default:
slouken@1895
  1072
        SDL_SetError("Unsupported YUV format");
slouken@1895
  1073
        return NULL;
slouken@1895
  1074
    }
slouken@1895
  1075
slouken@1895
  1076
    swdata->texture = texture;
slouken@1965
  1077
    swdata->target_format = SDL_PIXELFORMAT_UNKNOWN;
slouken@1895
  1078
    swdata->pixels = (Uint8 *) SDL_malloc(texture->w * texture->h * 2);
slouken@1895
  1079
    swdata->colortab = (int *) SDL_malloc(4 * 256 * sizeof(int));
slouken@1895
  1080
    swdata->rgb_2_pix = (Uint32 *) SDL_malloc(3 * 768 * sizeof(Uint32));
slouken@1895
  1081
    if (!swdata->pixels || !swdata->colortab || !swdata->rgb_2_pix) {
slouken@1895
  1082
        SDL_OutOfMemory();
slouken@1895
  1083
        SDL_SW_DestroyYUVTexture(swdata);
slouken@1895
  1084
        return NULL;
slouken@1895
  1085
    }
slouken@1895
  1086
slouken@1895
  1087
    /* Generate the tables for the display surface */
slouken@1895
  1088
    Cr_r_tab = &swdata->colortab[0 * 256];
slouken@1895
  1089
    Cr_g_tab = &swdata->colortab[1 * 256];
slouken@1895
  1090
    Cb_g_tab = &swdata->colortab[2 * 256];
slouken@1895
  1091
    Cb_b_tab = &swdata->colortab[3 * 256];
slouken@1895
  1092
    for (i = 0; i < 256; i++) {
slouken@1895
  1093
        /* Gamma correction (luminescence table) and chroma correction
slouken@1895
  1094
           would be done here.  See the Berkeley mpeg_play sources.
slouken@1895
  1095
         */
slouken@1895
  1096
        CB = CR = (i - 128);
slouken@1895
  1097
        Cr_r_tab[i] = (int) ((0.419 / 0.299) * CR);
slouken@1895
  1098
        Cr_g_tab[i] = (int) (-(0.299 / 0.419) * CR);
slouken@1895
  1099
        Cb_g_tab[i] = (int) (-(0.114 / 0.331) * CB);
slouken@1895
  1100
        Cb_b_tab[i] = (int) ((0.587 / 0.331) * CB);
slouken@1895
  1101
    }
slouken@1895
  1102
slouken@1895
  1103
    /* Find the pitch and offset values for the overlay */
slouken@1895
  1104
    switch (texture->format) {
slouken@1965
  1105
    case SDL_PIXELFORMAT_YV12:
slouken@1965
  1106
    case SDL_PIXELFORMAT_IYUV:
slouken@1895
  1107
        swdata->pitches[0] = texture->w;
slouken@1895
  1108
        swdata->pitches[1] = swdata->pitches[0] / 2;
slouken@1895
  1109
        swdata->pitches[2] = swdata->pitches[0] / 2;
slouken@1895
  1110
        swdata->planes[0] = swdata->pixels;
slouken@1895
  1111
        swdata->planes[1] =
slouken@1895
  1112
            swdata->planes[0] + swdata->pitches[0] * texture->h;
slouken@1895
  1113
        swdata->planes[2] =
slouken@1895
  1114
            swdata->planes[1] + swdata->pitches[1] * texture->h / 2;
slouken@1895
  1115
        break;
slouken@1965
  1116
    case SDL_PIXELFORMAT_YUY2:
slouken@1965
  1117
    case SDL_PIXELFORMAT_UYVY:
slouken@1965
  1118
    case SDL_PIXELFORMAT_YVYU:
slouken@1895
  1119
        swdata->pitches[0] = texture->w * 2;
slouken@1895
  1120
        swdata->planes[0] = swdata->pixels;
slouken@1895
  1121
        break;
slouken@1895
  1122
    default:
slouken@1895
  1123
        /* We should never get here (caught above) */
slouken@1895
  1124
        break;
slouken@1895
  1125
    }
slouken@1895
  1126
slouken@1895
  1127
    /* We're all done.. */
slouken@1895
  1128
    return (swdata);
slouken@0
  1129
}
slouken@0
  1130
slouken@1895
  1131
int
slouken@1895
  1132
SDL_SW_QueryYUVTexturePixels(SDL_SW_YUVTexture * swdata, void **pixels,
slouken@1895
  1133
                             int *pitch)
slouken@0
  1134
{
slouken@1895
  1135
    *pixels = swdata->planes[0];
slouken@1895
  1136
    *pitch = swdata->pitches[0];
slouken@1895
  1137
    return 0;
slouken@0
  1138
}
slouken@0
  1139
slouken@1895
  1140
int
slouken@1895
  1141
SDL_SW_UpdateYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
slouken@1895
  1142
                        const void *pixels, int pitch)
slouken@0
  1143
{
slouken@1895
  1144
    SDL_Texture *texture = swdata->texture;
slouken@0
  1145
slouken@1895
  1146
    switch (texture->format) {
slouken@1965
  1147
    case SDL_PIXELFORMAT_YV12:
slouken@1965
  1148
    case SDL_PIXELFORMAT_IYUV:
slouken@1895
  1149
        if (rect
slouken@1895
  1150
            && (rect->x != 0 || rect->y != 0 || rect->w != texture->w
slouken@1895
  1151
                || rect->h != texture->h)) {
slouken@1895
  1152
            SDL_SetError
slouken@1895
  1153
                ("YV12 and IYUV textures only support full surface updates");
slouken@1895
  1154
            return -1;
slouken@1895
  1155
        }
slouken@1895
  1156
        SDL_memcpy(swdata->pixels, pixels, texture->h * texture->w * 2);
slouken@1895
  1157
        break;
slouken@1965
  1158
    case SDL_PIXELFORMAT_YUY2:
slouken@1965
  1159
    case SDL_PIXELFORMAT_UYVY:
slouken@1965
  1160
    case SDL_PIXELFORMAT_YVYU:
slouken@1895
  1161
        {
slouken@1895
  1162
            Uint8 *src, *dst;
slouken@1895
  1163
            int row;
slouken@1895
  1164
            size_t length;
slouken@0
  1165
slouken@1895
  1166
            src = (Uint8 *) pixels;
slouken@1895
  1167
            dst =
slouken@1895
  1168
                swdata->planes[0] + rect->y * swdata->pitches[0] +
slouken@1895
  1169
                rect->x * 2;
slouken@1895
  1170
            length = rect->w * 2;
slouken@1895
  1171
            for (row = 0; row < rect->h; ++row) {
slouken@1895
  1172
                SDL_memcpy(dst, src, length);
slouken@1895
  1173
                src += pitch;
slouken@1895
  1174
                dst += swdata->pitches[0];
slouken@1895
  1175
            }
slouken@1895
  1176
        }
slouken@1895
  1177
        break;
slouken@1895
  1178
    }
slouken@1895
  1179
    return 0;
slouken@0
  1180
}
slouken@0
  1181
slouken@1895
  1182
int
slouken@1895
  1183
SDL_SW_LockYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
slouken@1895
  1184
                      int markDirty, void **pixels, int *pitch)
slouken@0
  1185
{
slouken@1895
  1186
    SDL_Texture *texture = swdata->texture;
slouken@0
  1187
slouken@1895
  1188
    switch (texture->format) {
slouken@1965
  1189
    case SDL_PIXELFORMAT_YV12:
slouken@1965
  1190
    case SDL_PIXELFORMAT_IYUV:
slouken@1895
  1191
        if (rect
slouken@1895
  1192
            && (rect->x != 0 || rect->y != 0 || rect->w != texture->w
slouken@1895
  1193
                || rect->h != texture->h)) {
slouken@1895
  1194
            SDL_SetError
slouken@1895
  1195
                ("YV12 and IYUV textures only support full surface locks");
slouken@1895
  1196
            return -1;
slouken@1895
  1197
        }
slouken@1895
  1198
        break;
slouken@1895
  1199
    }
slouken@1895
  1200
slouken@1895
  1201
    *pixels = swdata->planes[0] + rect->y * swdata->pitches[0] + rect->x * 2;
slouken@1895
  1202
    *pitch = swdata->pitches[0];
slouken@1895
  1203
    return 0;
slouken@0
  1204
}
slouken@1895
  1205
slouken@1895
  1206
void
slouken@1895
  1207
SDL_SW_UnlockYUVTexture(SDL_SW_YUVTexture * swdata)
slouken@1895
  1208
{
slouken@1895
  1209
}
slouken@1895
  1210
slouken@1895
  1211
int
slouken@1895
  1212
SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture * swdata, const SDL_Rect * srcrect,
slouken@1895
  1213
                    Uint32 target_format, int w, int h, void *pixels,
slouken@1895
  1214
                    int pitch)
slouken@1895
  1215
{
slouken@1895
  1216
    SDL_Texture *texture = swdata->texture;
slouken@1895
  1217
    int stretch;
slouken@1895
  1218
    int scale_2x;
slouken@1895
  1219
    Uint8 *lum, *Cr, *Cb;
slouken@1895
  1220
    int mod;
slouken@1895
  1221
slouken@1895
  1222
    /* Make sure we're set up to display in the desired format */
slouken@1895
  1223
    if (target_format != swdata->target_format) {
slouken@1895
  1224
        if (SDL_SW_SetupYUVDisplay(swdata, target_format) < 0) {
slouken@1895
  1225
            return -1;
slouken@1895
  1226
        }
slouken@1895
  1227
    }
slouken@1895
  1228
slouken@1895
  1229
    stretch = 0;
slouken@1895
  1230
    scale_2x = 0;
slouken@1895
  1231
    if (srcrect->x || srcrect->y || srcrect->w < texture->w
slouken@1895
  1232
        || srcrect->h < texture->h) {
slouken@1895
  1233
        /* The source rectangle has been clipped.
slouken@1895
  1234
           Using a scratch surface is easier than adding clipped
slouken@1895
  1235
           source support to all the blitters, plus that would
slouken@1895
  1236
           slow them down in the general unclipped case.
slouken@1895
  1237
         */
slouken@1895
  1238
        stretch = 1;
slouken@1895
  1239
    } else if ((srcrect->w != w) || (srcrect->h != h)) {
slouken@1895
  1240
        if ((w == 2 * srcrect->w) && (h == 2 * srcrect->h)) {
slouken@1895
  1241
            scale_2x = 1;
slouken@1895
  1242
        } else {
slouken@1895
  1243
            stretch = 1;
slouken@1895
  1244
        }
slouken@1895
  1245
    }
slouken@1895
  1246
    if (stretch) {
slouken@1895
  1247
        int bpp;
slouken@1895
  1248
        Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
  1249
slouken@1895
  1250
        if (swdata->display) {
slouken@1895
  1251
            swdata->display->w = w;
slouken@1895
  1252
            swdata->display->h = h;
slouken@1895
  1253
            swdata->display->pixels = pixels;
slouken@1895
  1254
            swdata->display->pitch = pitch;
slouken@1895
  1255
        } else {
slouken@1895
  1256
            /* This must have succeeded in SDL_SW_SetupYUVDisplay() earlier */
slouken@1895
  1257
            SDL_PixelFormatEnumToMasks(target_format, &bpp, &Rmask, &Gmask,
slouken@1895
  1258
                                       &Bmask, &Amask);
slouken@1895
  1259
            swdata->display =
slouken@1895
  1260
                SDL_CreateRGBSurfaceFrom(pixels, w, h, bpp, pitch, Rmask,
slouken@1895
  1261
                                         Gmask, Bmask, Amask);
slouken@1895
  1262
            if (!swdata->display) {
slouken@1895
  1263
                return (-1);
slouken@1895
  1264
            }
slouken@1895
  1265
        }
slouken@1895
  1266
        if (!swdata->stretch) {
slouken@1895
  1267
            /* This must have succeeded in SDL_SW_SetupYUVDisplay() earlier */
slouken@1895
  1268
            SDL_PixelFormatEnumToMasks(target_format, &bpp, &Rmask, &Gmask,
slouken@1895
  1269
                                       &Bmask, &Amask);
slouken@1895
  1270
            swdata->stretch =
slouken@1895
  1271
                SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask,
slouken@1895
  1272
                                     Gmask, Bmask, Amask);
slouken@1895
  1273
            if (!swdata->stretch) {
slouken@1895
  1274
                return (-1);
slouken@1895
  1275
            }
slouken@1895
  1276
        }
slouken@1895
  1277
        pixels = swdata->stretch->pixels;
slouken@1895
  1278
        pitch = swdata->stretch->pitch;
slouken@1895
  1279
    }
slouken@1895
  1280
    switch (texture->format) {
slouken@1965
  1281
    case SDL_PIXELFORMAT_YV12:
slouken@1895
  1282
        lum = swdata->planes[0];
slouken@1895
  1283
        Cr = swdata->planes[1];
slouken@1895
  1284
        Cb = swdata->planes[2];
slouken@1895
  1285
        break;
slouken@1965
  1286
    case SDL_PIXELFORMAT_IYUV:
slouken@1895
  1287
        lum = swdata->planes[0];
slouken@1895
  1288
        Cr = swdata->planes[2];
slouken@1895
  1289
        Cb = swdata->planes[1];
slouken@1895
  1290
        break;
slouken@1965
  1291
    case SDL_PIXELFORMAT_YUY2:
slouken@1895
  1292
        lum = swdata->planes[0];
slouken@1895
  1293
        Cr = lum + 3;
slouken@1895
  1294
        Cb = lum + 1;
slouken@1895
  1295
        break;
slouken@1965
  1296
    case SDL_PIXELFORMAT_UYVY:
slouken@1895
  1297
        lum = swdata->planes[0] + 1;
slouken@1895
  1298
        Cr = lum + 1;
slouken@1895
  1299
        Cb = lum - 1;
slouken@1895
  1300
        break;
slouken@1965
  1301
    case SDL_PIXELFORMAT_YVYU:
slouken@1895
  1302
        lum = swdata->planes[0];
slouken@1895
  1303
        Cr = lum + 1;
slouken@1895
  1304
        Cb = lum + 3;
slouken@1895
  1305
        break;
slouken@1895
  1306
    default:
slouken@1895
  1307
        SDL_SetError("Unsupported YUV format in copy");
slouken@1895
  1308
        return (-1);
slouken@1895
  1309
    }
slouken@1895
  1310
    mod = (pitch / SDL_BYTESPERPIXEL(target_format));
slouken@1895
  1311
slouken@1895
  1312
    if (scale_2x) {
slouken@1895
  1313
        mod -= (texture->w * 2);
slouken@1895
  1314
        swdata->Display2X(swdata->colortab, swdata->rgb_2_pix,
slouken@1895
  1315
                          lum, Cr, Cb, pixels, texture->h, texture->w, mod);
slouken@1895
  1316
    } else {
slouken@1895
  1317
        mod -= texture->w;
slouken@1895
  1318
        swdata->Display1X(swdata->colortab, swdata->rgb_2_pix,
slouken@1895
  1319
                          lum, Cr, Cb, pixels, texture->h, texture->w, mod);
slouken@1895
  1320
    }
slouken@1895
  1321
    if (stretch) {
slouken@1895
  1322
        SDL_Rect rect = *srcrect;
slouken@1895
  1323
        SDL_SoftStretch(swdata->stretch, &rect, swdata->display, NULL);
slouken@1895
  1324
    }
slouken@1895
  1325
    return 0;
slouken@1895
  1326
}
slouken@1895
  1327
slouken@1895
  1328
void
slouken@1895
  1329
SDL_SW_DestroyYUVTexture(SDL_SW_YUVTexture * swdata)
slouken@1895
  1330
{
slouken@1895
  1331
    if (swdata) {
slouken@1895
  1332
        if (swdata->pixels) {
slouken@1895
  1333
            SDL_free(swdata->pixels);
slouken@1895
  1334
        }
slouken@1895
  1335
        if (swdata->colortab) {
slouken@1895
  1336
            SDL_free(swdata->colortab);
slouken@1895
  1337
        }
slouken@1895
  1338
        if (swdata->rgb_2_pix) {
slouken@1895
  1339
            SDL_free(swdata->rgb_2_pix);
slouken@1895
  1340
        }
slouken@1895
  1341
        if (swdata->stretch) {
slouken@1895
  1342
            SDL_FreeSurface(swdata->stretch);
slouken@1895
  1343
        }
slouken@1895
  1344
        if (swdata->display) {
slouken@1895
  1345
            SDL_FreeSurface(swdata->display);
slouken@1895
  1346
        }
slouken@1895
  1347
        SDL_free(swdata);
slouken@1895
  1348
    }
slouken@1895
  1349
}
slouken@1895
  1350
slouken@1895
  1351
/* vi: set ts=4 sw=4 expandtab: */