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