annotate src/video/SDL_yuv.c @ 4354:1e191391e68d SDL-1.2

Use MacOS line endings for Mac resource files
author Sam Lantinga <slouken@libsdl.org>
date Sun, 18 Oct 2009 16:23:12 +0000
parents a1b03ba2fcd0
children
rev   line source
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
1 /*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
4159
a1b03ba2fcd0 Updated copyright date
Sam Lantinga <slouken@libsdl.org>
parents: 4076
diff changeset
3 Copyright (C) 1997-2009 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
6 modify it under the terms of the GNU Lesser General Public
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
13 Lesser General Public License for more details.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
15 You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
16 License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
19 Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 81
diff changeset
20 slouken@libsdl.org
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
21 */
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
22 #include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
24 /* This is the implementation of the YUV video surface support */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
25
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
26 #include "SDL_video.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
27 #include "SDL_sysvideo.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28 #include "SDL_yuvfuncs.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
29 #include "SDL_yuv_sw_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
31
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
32 SDL_Overlay *SDL_CreateYUVOverlay(int w, int h, Uint32 format,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
33 SDL_Surface *display)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
34 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
35 SDL_VideoDevice *video = current_video;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
36 SDL_VideoDevice *this = current_video;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
37 const char *yuv_hwaccel;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
38 SDL_Overlay *overlay;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
39
760
cf9dd3aa6756 Oops, we only want to fail creation if the display surface is an OpenGL surface.
Sam Lantinga <slouken@libsdl.org>
parents: 660
diff changeset
40 if ( (display->flags & SDL_OPENGL) == SDL_OPENGL ) {
660
73440ac574a2 You can't create a YUV overlay in OpenGL mode
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
41 SDL_SetError("YUV overlays are not supported in OpenGL mode");
73440ac574a2 You can't create a YUV overlay in OpenGL mode
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
42 return NULL;
73440ac574a2 You can't create a YUV overlay in OpenGL mode
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
43 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
44
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
45 /* Display directly on video surface, if possible */
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
46 if ( SDL_getenv("SDL_VIDEO_YUV_DIRECT") ) {
81
1a2723474f12 Added the SDL_VIDEO_YUV_DIRECT hack for better performance when the
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
47 if ( (display == SDL_PublicSurface) &&
1a2723474f12 Added the SDL_VIDEO_YUV_DIRECT hack for better performance when the
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
48 ((SDL_VideoSurface->format->BytesPerPixel == 2) ||
1a2723474f12 Added the SDL_VIDEO_YUV_DIRECT hack for better performance when the
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
49 (SDL_VideoSurface->format->BytesPerPixel == 4)) ) {
1a2723474f12 Added the SDL_VIDEO_YUV_DIRECT hack for better performance when the
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
50 display = SDL_VideoSurface;
1a2723474f12 Added the SDL_VIDEO_YUV_DIRECT hack for better performance when the
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
51 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
52 }
660
73440ac574a2 You can't create a YUV overlay in OpenGL mode
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
53 overlay = NULL;
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1330
diff changeset
54 yuv_hwaccel = SDL_getenv("SDL_VIDEO_YUV_HWACCEL");
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
55 if ( ((display == SDL_VideoSurface) && video->CreateYUVOverlay) &&
1341
d02b552e5304 Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents: 1338
diff changeset
56 (!yuv_hwaccel || (SDL_atoi(yuv_hwaccel) > 0)) ) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
57 overlay = video->CreateYUVOverlay(this, w, h, format, display);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
58 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
59 /* If hardware YUV overlay failed ... */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
60 if ( overlay == NULL ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
61 overlay = SDL_CreateYUV_SW(this, w, h, format, display);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
62 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
63 return overlay;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
64 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
65
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
66 int SDL_LockYUVOverlay(SDL_Overlay *overlay)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
67 {
4076
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
68 if ( overlay == NULL ) {
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
69 SDL_SetError("Passed NULL overlay");
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
70 return -1;
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
71 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
72 return overlay->hwfuncs->Lock(current_video, overlay);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
73 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
74
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
75 void SDL_UnlockYUVOverlay(SDL_Overlay *overlay)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
76 {
4076
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
77 if ( overlay == NULL ) {
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
78 return;
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
79 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
80 overlay->hwfuncs->Unlock(current_video, overlay);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
81 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
82
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
83 int SDL_DisplayYUVOverlay(SDL_Overlay *overlay, SDL_Rect *dstrect)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
84 {
1643
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
85 SDL_Rect src, dst;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
86 int srcx, srcy, srcw, srch;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
87 int dstx, dsty, dstw, dsth;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
88
4076
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
89 if ( overlay == NULL || dstrect == NULL ) {
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
90 SDL_SetError("Passed NULL overlay or dstrect");
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
91 return -1;
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
92 }
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
93
1643
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
94 /* Clip the rectangle to the screen area */
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
95 srcx = 0;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
96 srcy = 0;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
97 srcw = overlay->w;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
98 srch = overlay->h;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
99 dstx = dstrect->x;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
100 dsty = dstrect->y;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
101 dstw = dstrect->w;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
102 dsth = dstrect->h;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
103 if ( dstx < 0 ) {
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
104 srcw += (dstx * overlay->w) / dstrect->w;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
105 dstw += dstx;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
106 srcx -= (dstx * overlay->w) / dstrect->w;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
107 dstx = 0;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
108 }
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
109 if ( (dstx+dstw) > current_video->screen->w ) {
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
110 int extra = (dstx+dstw - current_video->screen->w);
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
111 srcw -= (extra * overlay->w) / dstrect->w;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
112 dstw -= extra;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
113 }
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
114 if ( dsty < 0 ) {
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
115 srch += (dsty * overlay->h) / dstrect->h;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
116 dsth += dsty;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
117 srcy -= (dsty * overlay->h) / dstrect->h;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
118 dsty = 0;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
119 }
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
120 if ( (dsty+dsth) > current_video->screen->h ) {
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
121 int extra = (dsty+dsth - current_video->screen->h);
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
122 srch -= (extra * overlay->h) / dstrect->h;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
123 dsth -= extra;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
124 }
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
125 if ( srcw <= 0 || srch <= 0 ||
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
126 srch <= 0 || dsth <= 0 ) {
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
127 return 0;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
128 }
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
129 /* Ugh, I can't wait for SDL_Rect to be int values */
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
130 src.x = srcx;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
131 src.y = srcy;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
132 src.w = srcw;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
133 src.h = srch;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
134 dst.x = dstx;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
135 dst.y = dsty;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
136 dst.w = dstw;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
137 dst.h = dsth;
51038e80ae59 More general fix for bug #189
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
138 return overlay->hwfuncs->Display(current_video, overlay, &src, &dst);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
139 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
140
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
141 void SDL_FreeYUVOverlay(SDL_Overlay *overlay)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
142 {
4076
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
143 if ( overlay == NULL ) {
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
144 return;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
145 }
4076
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
146 if ( overlay->hwfuncs ) {
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
147 overlay->hwfuncs->FreeHW(current_video, overlay);
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
148 }
9a7c62bbc8b3 Fixed bug #457
Sam Lantinga <slouken@libsdl.org>
parents: 1643
diff changeset
149 SDL_free(overlay);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
150 }