2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* This is the software implementation of the YUV video overlay support */
26 /* This code was derived from code carrying the following copyright notices:
28 * Copyright (c) 1995 The Regents of the University of California.
29 * All rights reserved.
31 * Permission to use, copy, modify, and distribute this software and its
32 * documentation for any purpose, without fee, and without written agreement is
33 * hereby granted, provided that the above copyright notice and the following
34 * two paragraphs appear in all copies of this software.
36 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
37 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
38 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
39 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
42 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
43 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
44 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
45 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
47 * Copyright (c) 1995 Erik Corry
48 * All rights reserved.
50 * Permission to use, copy, modify, and distribute this software and its
51 * documentation for any purpose, without fee, and without written agreement is
52 * hereby granted, provided that the above copyright notice and the following
53 * two paragraphs appear in all copies of this software.
55 * IN NO EVENT SHALL ERIK CORRY BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
56 * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
57 * THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIK CORRY HAS BEEN ADVISED
58 * OF THE POSSIBILITY OF SUCH DAMAGE.
60 * ERIK CORRY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
61 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
62 * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
63 * BASIS, AND ERIK CORRY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
64 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
66 * Portions of this software Copyright (c) 1995 Brown University.
67 * All rights reserved.
69 * Permission to use, copy, modify, and distribute this software and its
70 * documentation for any purpose, without fee, and without written agreement
71 * is hereby granted, provided that the above copyright notice and the
72 * following two paragraphs appear in all copies of this software.
74 * IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE TO ANY PARTY FOR
75 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
76 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF BROWN
77 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
79 * BROWN UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
80 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
81 * PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS"
82 * BASIS, AND BROWN UNIVERSITY HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
83 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
86 #include "SDL_video.h"
87 #include "SDL_cpuinfo.h"
88 #include "SDL_stretch_c.h"
89 #include "SDL_yuvfuncs.h"
90 #include "SDL_yuv_sw_c.h"
92 /* The functions used to manipulate software video overlays */
93 static struct private_yuvhwfuncs sw_yuvfuncs = {
100 /* RGB conversion lookup tables */
101 struct private_yuvhwdata {
102 SDL_Surface *stretch;
103 SDL_Surface *display;
107 void (*Display1X)(int *colortab, Uint32 *rgb_2_pix,
108 unsigned char *lum, unsigned char *cr,
109 unsigned char *cb, unsigned char *out,
110 int rows, int cols, int mod );
111 void (*Display2X)(int *colortab, Uint32 *rgb_2_pix,
112 unsigned char *lum, unsigned char *cr,
113 unsigned char *cb, unsigned char *out,
114 int rows, int cols, int mod );
116 /* These are just so we don't have to allocate them separately */
122 /* The colorspace conversion functions */
124 #if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
125 extern void Color565DitherYV12MMX1X( int *colortab, Uint32 *rgb_2_pix,
126 unsigned char *lum, unsigned char *cr,
127 unsigned char *cb, unsigned char *out,
128 int rows, int cols, int mod );
129 extern void ColorRGBDitherYV12MMX1X( int *colortab, Uint32 *rgb_2_pix,
130 unsigned char *lum, unsigned char *cr,
131 unsigned char *cb, unsigned char *out,
132 int rows, int cols, int mod );
135 static void Color16DitherYV12Mod1X( int *colortab, Uint32 *rgb_2_pix,
136 unsigned char *lum, unsigned char *cr,
137 unsigned char *cb, unsigned char *out,
138 int rows, int cols, int mod )
140 unsigned short* row1;
141 unsigned short* row2;
147 int cols_2 = cols / 2;
149 row1 = (unsigned short*) out;
150 row2 = row1 + cols + mod;
163 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
164 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
165 + colortab[ *cb + 2*256 ];
166 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
170 *row1++ = (unsigned short)(rgb_2_pix[ L + cr_r ] |
171 rgb_2_pix[ L + crb_g ] |
172 rgb_2_pix[ L + cb_b ]);
175 *row1++ = (unsigned short)(rgb_2_pix[ L + cr_r ] |
176 rgb_2_pix[ L + crb_g ] |
177 rgb_2_pix[ L + cb_b ]);
180 /* Now, do second row. */
183 *row2++ = (unsigned short)(rgb_2_pix[ L + cr_r ] |
184 rgb_2_pix[ L + crb_g ] |
185 rgb_2_pix[ L + cb_b ]);
188 *row2++ = (unsigned short)(rgb_2_pix[ L + cr_r ] |
189 rgb_2_pix[ L + crb_g ] |
190 rgb_2_pix[ L + cb_b ]);
194 * These values are at the start of the next line, (due
195 * to the ++'s above),but they need to be at the start
196 * of the line after that.
205 static void Color24DitherYV12Mod1X( int *colortab, Uint32 *rgb_2_pix,
206 unsigned char *lum, unsigned char *cr,
207 unsigned char *cb, unsigned char *out,
208 int rows, int cols, int mod )
218 int cols_2 = cols / 2;
221 row2 = row1 + cols*3 + mod*3;
235 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
236 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
237 + colortab[ *cb + 2*256 ];
238 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
242 value = (rgb_2_pix[ L + cr_r ] |
243 rgb_2_pix[ L + crb_g ] |
244 rgb_2_pix[ L + cb_b ]);
245 *row1++ = (value ) & 0xFF;
246 *row1++ = (value >> 8) & 0xFF;
247 *row1++ = (value >> 16) & 0xFF;
250 value = (rgb_2_pix[ L + cr_r ] |
251 rgb_2_pix[ L + crb_g ] |
252 rgb_2_pix[ L + cb_b ]);
253 *row1++ = (value ) & 0xFF;
254 *row1++ = (value >> 8) & 0xFF;
255 *row1++ = (value >> 16) & 0xFF;
258 /* Now, do second row. */
261 value = (rgb_2_pix[ L + cr_r ] |
262 rgb_2_pix[ L + crb_g ] |
263 rgb_2_pix[ L + cb_b ]);
264 *row2++ = (value ) & 0xFF;
265 *row2++ = (value >> 8) & 0xFF;
266 *row2++ = (value >> 16) & 0xFF;
269 value = (rgb_2_pix[ L + cr_r ] |
270 rgb_2_pix[ L + crb_g ] |
271 rgb_2_pix[ L + cb_b ]);
272 *row2++ = (value ) & 0xFF;
273 *row2++ = (value >> 8) & 0xFF;
274 *row2++ = (value >> 16) & 0xFF;
278 * These values are at the start of the next line, (due
279 * to the ++'s above),but they need to be at the start
280 * of the line after that.
289 static void Color32DitherYV12Mod1X( int *colortab, Uint32 *rgb_2_pix,
290 unsigned char *lum, unsigned char *cr,
291 unsigned char *cb, unsigned char *out,
292 int rows, int cols, int mod )
301 int cols_2 = cols / 2;
303 row1 = (unsigned int*) out;
304 row2 = row1 + cols + mod;
317 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
318 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
319 + colortab[ *cb + 2*256 ];
320 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
324 *row1++ = (rgb_2_pix[ L + cr_r ] |
325 rgb_2_pix[ L + crb_g ] |
326 rgb_2_pix[ L + cb_b ]);
329 *row1++ = (rgb_2_pix[ L + cr_r ] |
330 rgb_2_pix[ L + crb_g ] |
331 rgb_2_pix[ L + cb_b ]);
334 /* Now, do second row. */
337 *row2++ = (rgb_2_pix[ L + cr_r ] |
338 rgb_2_pix[ L + crb_g ] |
339 rgb_2_pix[ L + cb_b ]);
342 *row2++ = (rgb_2_pix[ L + cr_r ] |
343 rgb_2_pix[ L + crb_g ] |
344 rgb_2_pix[ L + cb_b ]);
348 * These values are at the start of the next line, (due
349 * to the ++'s above),but they need to be at the start
350 * of the line after that.
360 * In this function I make use of a nasty trick. The tables have the lower
361 * 16 bits replicated in the upper 16. This means I can write ints and get
362 * the horisontal doubling for free (almost).
364 static void Color16DitherYV12Mod2X( int *colortab, Uint32 *rgb_2_pix,
365 unsigned char *lum, unsigned char *cr,
366 unsigned char *cb, unsigned char *out,
367 int rows, int cols, int mod )
369 unsigned int* row1 = (unsigned int*) out;
370 const int next_row = cols+(mod/2);
371 unsigned int* row2 = row1 + 2*next_row;
377 int cols_2 = cols / 2;
381 mod = (next_row * 3) + (mod/2);
391 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
392 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
393 + colortab[ *cb + 2*256 ];
394 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
398 row1[0] = row1[next_row] = (rgb_2_pix[ L + cr_r ] |
399 rgb_2_pix[ L + crb_g ] |
400 rgb_2_pix[ L + cb_b ]);
404 row1[0] = row1[next_row] = (rgb_2_pix[ L + cr_r ] |
405 rgb_2_pix[ L + crb_g ] |
406 rgb_2_pix[ L + cb_b ]);
410 /* Now, do second row. */
413 row2[0] = row2[next_row] = (rgb_2_pix[ L + cr_r ] |
414 rgb_2_pix[ L + crb_g ] |
415 rgb_2_pix[ L + cb_b ]);
419 row2[0] = row2[next_row] = (rgb_2_pix[ L + cr_r ] |
420 rgb_2_pix[ L + crb_g ] |
421 rgb_2_pix[ L + cb_b ]);
426 * These values are at the start of the next line, (due
427 * to the ++'s above),but they need to be at the start
428 * of the line after that.
437 static void Color24DitherYV12Mod2X( int *colortab, Uint32 *rgb_2_pix,
438 unsigned char *lum, unsigned char *cr,
439 unsigned char *cb, unsigned char *out,
440 int rows, int cols, int mod )
443 unsigned char* row1 = out;
444 const int next_row = (cols*2 + mod) * 3;
445 unsigned char* row2 = row1 + 2*next_row;
451 int cols_2 = cols / 2;
455 mod = next_row*3 + mod*3;
465 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
466 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
467 + colortab[ *cb + 2*256 ];
468 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
472 value = (rgb_2_pix[ L + cr_r ] |
473 rgb_2_pix[ L + crb_g ] |
474 rgb_2_pix[ L + cb_b ]);
475 row1[0+0] = row1[3+0] = row1[next_row+0] = row1[next_row+3+0] =
477 row1[0+1] = row1[3+1] = row1[next_row+1] = row1[next_row+3+1] =
479 row1[0+2] = row1[3+2] = row1[next_row+2] = row1[next_row+3+2] =
480 (value >> 16) & 0xFF;
484 value = (rgb_2_pix[ L + cr_r ] |
485 rgb_2_pix[ L + crb_g ] |
486 rgb_2_pix[ L + cb_b ]);
487 row1[0+0] = row1[3+0] = row1[next_row+0] = row1[next_row+3+0] =
489 row1[0+1] = row1[3+1] = row1[next_row+1] = row1[next_row+3+1] =
491 row1[0+2] = row1[3+2] = row1[next_row+2] = row1[next_row+3+2] =
492 (value >> 16) & 0xFF;
496 /* Now, do second row. */
499 value = (rgb_2_pix[ L + cr_r ] |
500 rgb_2_pix[ L + crb_g ] |
501 rgb_2_pix[ L + cb_b ]);
502 row2[0+0] = row2[3+0] = row2[next_row+0] = row2[next_row+3+0] =
504 row2[0+1] = row2[3+1] = row2[next_row+1] = row2[next_row+3+1] =
506 row2[0+2] = row2[3+2] = row2[next_row+2] = row2[next_row+3+2] =
507 (value >> 16) & 0xFF;
511 value = (rgb_2_pix[ L + cr_r ] |
512 rgb_2_pix[ L + crb_g ] |
513 rgb_2_pix[ L + cb_b ]);
514 row2[0+0] = row2[3+0] = row2[next_row+0] = row2[next_row+3+0] =
516 row2[0+1] = row2[3+1] = row2[next_row+1] = row2[next_row+3+1] =
518 row2[0+2] = row2[3+2] = row2[next_row+2] = row2[next_row+3+2] =
519 (value >> 16) & 0xFF;
524 * These values are at the start of the next line, (due
525 * to the ++'s above),but they need to be at the start
526 * of the line after that.
535 static void Color32DitherYV12Mod2X( int *colortab, Uint32 *rgb_2_pix,
536 unsigned char *lum, unsigned char *cr,
537 unsigned char *cb, unsigned char *out,
538 int rows, int cols, int mod )
540 unsigned int* row1 = (unsigned int*) out;
541 const int next_row = cols*2+mod;
542 unsigned int* row2 = row1 + 2*next_row;
548 int cols_2 = cols / 2;
552 mod = (next_row * 3) + mod;
562 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
563 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
564 + colortab[ *cb + 2*256 ];
565 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
569 row1[0] = row1[1] = row1[next_row] = row1[next_row+1] =
570 (rgb_2_pix[ L + cr_r ] |
571 rgb_2_pix[ L + crb_g ] |
572 rgb_2_pix[ L + cb_b ]);
576 row1[0] = row1[1] = row1[next_row] = row1[next_row+1] =
577 (rgb_2_pix[ L + cr_r ] |
578 rgb_2_pix[ L + crb_g ] |
579 rgb_2_pix[ L + cb_b ]);
583 /* Now, do second row. */
586 row2[0] = row2[1] = row2[next_row] = row2[next_row+1] =
587 (rgb_2_pix[ L + cr_r ] |
588 rgb_2_pix[ L + crb_g ] |
589 rgb_2_pix[ L + cb_b ]);
593 row2[0] = row2[1] = row2[next_row] = row2[next_row+1] =
594 (rgb_2_pix[ L + cr_r ] |
595 rgb_2_pix[ L + crb_g ] |
596 rgb_2_pix[ L + cb_b ]);
601 * These values are at the start of the next line, (due
602 * to the ++'s above),but they need to be at the start
603 * of the line after that.
612 static void Color16DitherYUY2Mod1X( int *colortab, Uint32 *rgb_2_pix,
613 unsigned char *lum, unsigned char *cr,
614 unsigned char *cb, unsigned char *out,
615 int rows, int cols, int mod )
622 int cols_2 = cols / 2;
624 row = (unsigned short*) out;
634 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
635 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
636 + colortab[ *cb + 2*256 ];
637 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
641 *row++ = (unsigned short)(rgb_2_pix[ L + cr_r ] |
642 rgb_2_pix[ L + crb_g ] |
643 rgb_2_pix[ L + cb_b ]);
646 *row++ = (unsigned short)(rgb_2_pix[ L + cr_r ] |
647 rgb_2_pix[ L + crb_g ] |
648 rgb_2_pix[ L + cb_b ]);
656 static void Color24DitherYUY2Mod1X( int *colortab, Uint32 *rgb_2_pix,
657 unsigned char *lum, unsigned char *cr,
658 unsigned char *cb, unsigned char *out,
659 int rows, int cols, int mod )
667 int cols_2 = cols / 2;
669 row = (unsigned char*) out;
679 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
680 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
681 + colortab[ *cb + 2*256 ];
682 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
686 value = (rgb_2_pix[ L + cr_r ] |
687 rgb_2_pix[ L + crb_g ] |
688 rgb_2_pix[ L + cb_b ]);
689 *row++ = (value ) & 0xFF;
690 *row++ = (value >> 8) & 0xFF;
691 *row++ = (value >> 16) & 0xFF;
694 value = (rgb_2_pix[ L + cr_r ] |
695 rgb_2_pix[ L + crb_g ] |
696 rgb_2_pix[ L + cb_b ]);
697 *row++ = (value ) & 0xFF;
698 *row++ = (value >> 8) & 0xFF;
699 *row++ = (value >> 16) & 0xFF;
706 static void Color32DitherYUY2Mod1X( int *colortab, Uint32 *rgb_2_pix,
707 unsigned char *lum, unsigned char *cr,
708 unsigned char *cb, unsigned char *out,
709 int rows, int cols, int mod )
716 int cols_2 = cols / 2;
718 row = (unsigned int*) out;
727 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
728 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
729 + colortab[ *cb + 2*256 ];
730 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
734 *row++ = (rgb_2_pix[ L + cr_r ] |
735 rgb_2_pix[ L + crb_g ] |
736 rgb_2_pix[ L + cb_b ]);
739 *row++ = (rgb_2_pix[ L + cr_r ] |
740 rgb_2_pix[ L + crb_g ] |
741 rgb_2_pix[ L + cb_b ]);
750 * In this function I make use of a nasty trick. The tables have the lower
751 * 16 bits replicated in the upper 16. This means I can write ints and get
752 * the horisontal doubling for free (almost).
754 static void Color16DitherYUY2Mod2X( int *colortab, Uint32 *rgb_2_pix,
755 unsigned char *lum, unsigned char *cr,
756 unsigned char *cb, unsigned char *out,
757 int rows, int cols, int mod )
759 unsigned int* row = (unsigned int*) out;
760 const int next_row = cols+(mod/2);
765 int cols_2 = cols / 2;
775 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
776 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
777 + colortab[ *cb + 2*256 ];
778 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
782 row[0] = row[next_row] = (rgb_2_pix[ L + cr_r ] |
783 rgb_2_pix[ L + crb_g ] |
784 rgb_2_pix[ L + cb_b ]);
788 row[0] = row[next_row] = (rgb_2_pix[ L + cr_r ] |
789 rgb_2_pix[ L + crb_g ] |
790 rgb_2_pix[ L + cb_b ]);
798 static void Color24DitherYUY2Mod2X( int *colortab, Uint32 *rgb_2_pix,
799 unsigned char *lum, unsigned char *cr,
800 unsigned char *cb, unsigned char *out,
801 int rows, int cols, int mod )
804 unsigned char* row = out;
805 const int next_row = (cols*2 + mod) * 3;
810 int cols_2 = cols / 2;
819 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
820 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
821 + colortab[ *cb + 2*256 ];
822 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
826 value = (rgb_2_pix[ L + cr_r ] |
827 rgb_2_pix[ L + crb_g ] |
828 rgb_2_pix[ L + cb_b ]);
829 row[0+0] = row[3+0] = row[next_row+0] = row[next_row+3+0] =
831 row[0+1] = row[3+1] = row[next_row+1] = row[next_row+3+1] =
833 row[0+2] = row[3+2] = row[next_row+2] = row[next_row+3+2] =
834 (value >> 16) & 0xFF;
838 value = (rgb_2_pix[ L + cr_r ] |
839 rgb_2_pix[ L + crb_g ] |
840 rgb_2_pix[ L + cb_b ]);
841 row[0+0] = row[3+0] = row[next_row+0] = row[next_row+3+0] =
843 row[0+1] = row[3+1] = row[next_row+1] = row[next_row+3+1] =
845 row[0+2] = row[3+2] = row[next_row+2] = row[next_row+3+2] =
846 (value >> 16) & 0xFF;
854 static void Color32DitherYUY2Mod2X( int *colortab, Uint32 *rgb_2_pix,
855 unsigned char *lum, unsigned char *cr,
856 unsigned char *cb, unsigned char *out,
857 int rows, int cols, int mod )
859 unsigned int* row = (unsigned int*) out;
860 const int next_row = cols*2+mod;
865 int cols_2 = cols / 2;
875 cr_r = 0*768+256 + colortab[ *cr + 0*256 ];
876 crb_g = 1*768+256 + colortab[ *cr + 1*256 ]
877 + colortab[ *cb + 2*256 ];
878 cb_b = 2*768+256 + colortab[ *cb + 3*256 ];
882 row[0] = row[1] = row[next_row] = row[next_row+1] =
883 (rgb_2_pix[ L + cr_r ] |
884 rgb_2_pix[ L + crb_g ] |
885 rgb_2_pix[ L + cb_b ]);
889 row[0] = row[1] = row[next_row] = row[next_row+1] =
890 (rgb_2_pix[ L + cr_r ] |
891 rgb_2_pix[ L + crb_g ] |
892 rgb_2_pix[ L + cb_b ]);
903 * How many 1 bits are there in the Uint32.
904 * Low performance, do not call often.
906 static int number_of_bits_set( Uint32 a )
909 if(a & 1) return 1 + number_of_bits_set(a >> 1);
910 return(number_of_bits_set(a >> 1));
914 * How many 0 bits are there at least significant end of Uint32.
915 * Low performance, do not call often.
917 static int free_bits_at_bottom( Uint32 a )
919 /* assume char is 8 bits */
920 if(!a) return sizeof(Uint32) * 8;
921 if(((Sint32)a) & 1l) return 0;
922 return 1 + free_bits_at_bottom ( a >> 1);
926 SDL_Overlay *SDL_CreateYUV_SW(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
928 SDL_Overlay *overlay;
929 struct private_yuvhwdata *swdata;
934 Uint32 *r_2_pix_alloc;
935 Uint32 *g_2_pix_alloc;
936 Uint32 *b_2_pix_alloc;
939 Uint32 Rmask, Gmask, Bmask;
941 /* Only RGB packed pixel conversion supported */
942 if ( (display->format->BytesPerPixel != 2) &&
943 (display->format->BytesPerPixel != 3) &&
944 (display->format->BytesPerPixel != 4) ) {
945 SDL_SetError("Can't use YUV data on non 16/24/32 bit surfaces");
949 /* Verify that we support the format */
951 case SDL_YV12_OVERLAY:
952 case SDL_IYUV_OVERLAY:
953 case SDL_YUY2_OVERLAY:
954 case SDL_UYVY_OVERLAY:
955 case SDL_YVYU_OVERLAY:
958 SDL_SetError("Unsupported YUV format");
962 /* Create the overlay structure */
963 overlay = (SDL_Overlay *)SDL_malloc(sizeof *overlay);
964 if ( overlay == NULL ) {
968 SDL_memset(overlay, 0, (sizeof *overlay));
970 /* Fill in the basic members */
971 overlay->format = format;
975 /* Set up the YUV surface function structure */
976 overlay->hwfuncs = &sw_yuvfuncs;
978 /* Create the pixel data and lookup tables */
979 swdata = (struct private_yuvhwdata *)SDL_malloc(sizeof *swdata);
980 overlay->hwdata = swdata;
981 if ( swdata == NULL ) {
983 SDL_FreeYUVOverlay(overlay);
986 swdata->stretch = NULL;
987 swdata->display = display;
988 swdata->pixels = (Uint8 *) SDL_malloc(width*height*2);
989 swdata->colortab = (int *)SDL_malloc(4*256*sizeof(int));
990 Cr_r_tab = &swdata->colortab[0*256];
991 Cr_g_tab = &swdata->colortab[1*256];
992 Cb_g_tab = &swdata->colortab[2*256];
993 Cb_b_tab = &swdata->colortab[3*256];
994 swdata->rgb_2_pix = (Uint32 *)SDL_malloc(3*768*sizeof(Uint32));
995 r_2_pix_alloc = &swdata->rgb_2_pix[0*768];
996 g_2_pix_alloc = &swdata->rgb_2_pix[1*768];
997 b_2_pix_alloc = &swdata->rgb_2_pix[2*768];
998 if ( ! swdata->pixels || ! swdata->colortab || ! swdata->rgb_2_pix ) {
1000 SDL_FreeYUVOverlay(overlay);
1004 /* Generate the tables for the display surface */
1005 for (i=0; i<256; i++) {
1006 /* Gamma correction (luminescence table) and chroma correction
1007 would be done here. See the Berkeley mpeg_play sources.
1010 Cr_r_tab[i] = (int) ( (0.419/0.299) * CR);
1011 Cr_g_tab[i] = (int) (-(0.299/0.419) * CR);
1012 Cb_g_tab[i] = (int) (-(0.114/0.331) * CB);
1013 Cb_b_tab[i] = (int) ( (0.587/0.331) * CB);
1017 * Set up entries 0-255 in rgb-to-pixel value tables.
1019 Rmask = display->format->Rmask;
1020 Gmask = display->format->Gmask;
1021 Bmask = display->format->Bmask;
1022 for ( i=0; i<256; ++i ) {
1023 r_2_pix_alloc[i+256] = i >> (8 - number_of_bits_set(Rmask));
1024 r_2_pix_alloc[i+256] <<= free_bits_at_bottom(Rmask);
1025 g_2_pix_alloc[i+256] = i >> (8 - number_of_bits_set(Gmask));
1026 g_2_pix_alloc[i+256] <<= free_bits_at_bottom(Gmask);
1027 b_2_pix_alloc[i+256] = i >> (8 - number_of_bits_set(Bmask));
1028 b_2_pix_alloc[i+256] <<= free_bits_at_bottom(Bmask);
1032 * If we have 16-bit output depth, then we double the value
1033 * in the top word. This means that we can write out both
1034 * pixels in the pixel doubling mode with one op. It is
1035 * harmless in the normal case as storing a 32-bit value
1036 * through a short pointer will lose the top bits anyway.
1038 if( display->format->BytesPerPixel == 2 ) {
1039 for ( i=0; i<256; ++i ) {
1040 r_2_pix_alloc[i+256] |= (r_2_pix_alloc[i+256]) << 16;
1041 g_2_pix_alloc[i+256] |= (g_2_pix_alloc[i+256]) << 16;
1042 b_2_pix_alloc[i+256] |= (b_2_pix_alloc[i+256]) << 16;
1047 * Spread out the values we have to the rest of the array so that
1048 * we do not need to check for overflow.
1050 for ( i=0; i<256; ++i ) {
1051 r_2_pix_alloc[i] = r_2_pix_alloc[256];
1052 r_2_pix_alloc[i+512] = r_2_pix_alloc[511];
1053 g_2_pix_alloc[i] = g_2_pix_alloc[256];
1054 g_2_pix_alloc[i+512] = g_2_pix_alloc[511];
1055 b_2_pix_alloc[i] = b_2_pix_alloc[256];
1056 b_2_pix_alloc[i+512] = b_2_pix_alloc[511];
1059 /* You have chosen wisely... */
1061 case SDL_YV12_OVERLAY:
1062 case SDL_IYUV_OVERLAY:
1063 if ( display->format->BytesPerPixel == 2 ) {
1064 #if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
1065 /* inline assembly functions */
1066 if ( SDL_HasMMX() && (Rmask == 0xF800) &&
1067 (Gmask == 0x07E0) &&
1068 (Bmask == 0x001F) &&
1069 (width & 15) == 0) {
1070 /*printf("Using MMX 16-bit 565 dither\n");*/
1071 swdata->Display1X = Color565DitherYV12MMX1X;
1073 /*printf("Using C 16-bit dither\n");*/
1074 swdata->Display1X = Color16DitherYV12Mod1X;
1077 swdata->Display1X = Color16DitherYV12Mod1X;
1079 swdata->Display2X = Color16DitherYV12Mod2X;
1081 if ( display->format->BytesPerPixel == 3 ) {
1082 swdata->Display1X = Color24DitherYV12Mod1X;
1083 swdata->Display2X = Color24DitherYV12Mod2X;
1085 if ( display->format->BytesPerPixel == 4 ) {
1086 #if (__GNUC__ > 2) && defined(__i386__) && __OPTIMIZE__ && SDL_ASSEMBLY_ROUTINES
1087 /* inline assembly functions */
1088 if ( SDL_HasMMX() && (Rmask == 0x00FF0000) &&
1089 (Gmask == 0x0000FF00) &&
1090 (Bmask == 0x000000FF) &&
1091 (width & 15) == 0) {
1092 /*printf("Using MMX 32-bit dither\n");*/
1093 swdata->Display1X = ColorRGBDitherYV12MMX1X;
1095 /*printf("Using C 32-bit dither\n");*/
1096 swdata->Display1X = Color32DitherYV12Mod1X;
1099 swdata->Display1X = Color32DitherYV12Mod1X;
1101 swdata->Display2X = Color32DitherYV12Mod2X;
1104 case SDL_YUY2_OVERLAY:
1105 case SDL_UYVY_OVERLAY:
1106 case SDL_YVYU_OVERLAY:
1107 if ( display->format->BytesPerPixel == 2 ) {
1108 swdata->Display1X = Color16DitherYUY2Mod1X;
1109 swdata->Display2X = Color16DitherYUY2Mod2X;
1111 if ( display->format->BytesPerPixel == 3 ) {
1112 swdata->Display1X = Color24DitherYUY2Mod1X;
1113 swdata->Display2X = Color24DitherYUY2Mod2X;
1115 if ( display->format->BytesPerPixel == 4 ) {
1116 swdata->Display1X = Color32DitherYUY2Mod1X;
1117 swdata->Display2X = Color32DitherYUY2Mod2X;
1121 /* We should never get here (caught above) */
1125 /* Find the pitch and offset values for the overlay */
1126 overlay->pitches = swdata->pitches;
1127 overlay->pixels = swdata->planes;
1129 case SDL_YV12_OVERLAY:
1130 case SDL_IYUV_OVERLAY:
1131 overlay->pitches[0] = overlay->w;
1132 overlay->pitches[1] = overlay->pitches[0] / 2;
1133 overlay->pitches[2] = overlay->pitches[0] / 2;
1134 overlay->pixels[0] = swdata->pixels;
1135 overlay->pixels[1] = overlay->pixels[0] +
1136 overlay->pitches[0] * overlay->h;
1137 overlay->pixels[2] = overlay->pixels[1] +
1138 overlay->pitches[1] * overlay->h / 2;
1139 overlay->planes = 3;
1141 case SDL_YUY2_OVERLAY:
1142 case SDL_UYVY_OVERLAY:
1143 case SDL_YVYU_OVERLAY:
1144 overlay->pitches[0] = overlay->w*2;
1145 overlay->pixels[0] = swdata->pixels;
1146 overlay->planes = 1;
1149 /* We should never get here (caught above) */
1153 /* We're all done.. */
1157 int SDL_LockYUV_SW(_THIS, SDL_Overlay *overlay)
1162 void SDL_UnlockYUV_SW(_THIS, SDL_Overlay *overlay)
1167 int SDL_DisplayYUV_SW(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst)
1169 struct private_yuvhwdata *swdata;
1172 SDL_Surface *display;
1173 Uint8 *lum, *Cr, *Cb;
1177 swdata = overlay->hwdata;
1180 if ( src->x || src->y || src->w < overlay->w || src->h < overlay->h ) {
1181 /* The source rectangle has been clipped.
1182 Using a scratch surface is easier than adding clipped
1183 source support to all the blitters, plus that would
1184 slow them down in the general unclipped case.
1187 } else if ( (src->w != dst->w) || (src->h != dst->h) ) {
1188 if ( (dst->w == 2*src->w) &&
1189 (dst->h == 2*src->h) ) {
1196 if ( ! swdata->stretch ) {
1197 display = swdata->display;
1198 swdata->stretch = SDL_CreateRGBSurface(
1200 overlay->w, overlay->h,
1201 display->format->BitsPerPixel,
1202 display->format->Rmask,
1203 display->format->Gmask,
1204 display->format->Bmask, 0);
1205 if ( ! swdata->stretch ) {
1209 display = swdata->stretch;
1211 display = swdata->display;
1213 switch (overlay->format) {
1214 case SDL_YV12_OVERLAY:
1215 lum = overlay->pixels[0];
1216 Cr = overlay->pixels[1];
1217 Cb = overlay->pixels[2];
1219 case SDL_IYUV_OVERLAY:
1220 lum = overlay->pixels[0];
1221 Cr = overlay->pixels[2];
1222 Cb = overlay->pixels[1];
1224 case SDL_YUY2_OVERLAY:
1225 lum = overlay->pixels[0];
1229 case SDL_UYVY_OVERLAY:
1230 lum = overlay->pixels[0]+1;
1234 case SDL_YVYU_OVERLAY:
1235 lum = overlay->pixels[0];
1240 SDL_SetError("Unsupported YUV format in blit");
1243 if ( SDL_MUSTLOCK(display) ) {
1244 if ( SDL_LockSurface(display) < 0 ) {
1249 dstp = (Uint8 *)swdata->stretch->pixels;
1251 dstp = (Uint8 *)display->pixels
1252 + dst->x * display->format->BytesPerPixel
1253 + dst->y * display->pitch;
1255 mod = (display->pitch / display->format->BytesPerPixel);
1258 mod -= (overlay->w * 2);
1259 swdata->Display2X(swdata->colortab, swdata->rgb_2_pix,
1260 lum, Cr, Cb, dstp, overlay->h, overlay->w, mod);
1263 swdata->Display1X(swdata->colortab, swdata->rgb_2_pix,
1264 lum, Cr, Cb, dstp, overlay->h, overlay->w, mod);
1266 if ( SDL_MUSTLOCK(display) ) {
1267 SDL_UnlockSurface(display);
1270 display = swdata->display;
1271 SDL_SoftStretch(swdata->stretch, src, display, dst);
1273 SDL_UpdateRects(display, 1, dst);
1278 void SDL_FreeYUV_SW(_THIS, SDL_Overlay *overlay)
1280 struct private_yuvhwdata *swdata;
1282 swdata = overlay->hwdata;
1284 if ( swdata->stretch ) {
1285 SDL_FreeSurface(swdata->stretch);
1287 if ( swdata->pixels ) {
1288 SDL_free(swdata->pixels);
1290 if ( swdata->colortab ) {
1291 SDL_free(swdata->colortab);
1293 if ( swdata->rgb_2_pix ) {
1294 SDL_free(swdata->rgb_2_pix);
1297 overlay->hwdata = NULL;