Skip to content

Latest commit

 

History

History
414 lines (374 loc) · 13.6 KB

SDL_yuv_sw.c

File metadata and controls

414 lines (374 loc) · 13.6 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../SDL_internal.h"
/* This is the software implementation of the YUV texture support */
#include "SDL_assert.h"
Nov 13, 2017
Nov 13, 2017
27
#include "SDL_yuv_sw_c.h"
28
29
30
31
32
33
34
35
36
37
38
39
40
SDL_SW_YUVTexture *
SDL_SW_CreateYUVTexture(Uint32 format, int w, int h)
{
SDL_SW_YUVTexture *swdata;
switch (format) {
case SDL_PIXELFORMAT_YV12:
case SDL_PIXELFORMAT_IYUV:
case SDL_PIXELFORMAT_YUY2:
case SDL_PIXELFORMAT_UYVY:
case SDL_PIXELFORMAT_YVYU:
Oct 6, 2017
Oct 6, 2017
41
42
case SDL_PIXELFORMAT_NV12:
case SDL_PIXELFORMAT_NV21:
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
break;
default:
SDL_SetError("Unsupported YUV format");
return NULL;
}
swdata = (SDL_SW_YUVTexture *) SDL_calloc(1, sizeof(*swdata));
if (!swdata) {
SDL_OutOfMemory();
return NULL;
}
swdata->format = format;
swdata->target_format = SDL_PIXELFORMAT_UNKNOWN;
swdata->w = w;
swdata->h = h;
Oct 6, 2017
Oct 6, 2017
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
{
const int sz_plane = w * h;
const int sz_plane_chroma = ((w + 1) / 2) * ((h + 1) / 2);
const int sz_plane_packed = ((w + 1) / 2) * h;
int dst_size = 0;
switch(format)
{
case SDL_PIXELFORMAT_YV12: /**< Planar mode: Y + V + U (3 planes) */
case SDL_PIXELFORMAT_IYUV: /**< Planar mode: Y + U + V (3 planes) */
dst_size = sz_plane + sz_plane_chroma + sz_plane_chroma;
break;
case SDL_PIXELFORMAT_YUY2: /**< Packed mode: Y0+U0+Y1+V0 (1 plane) */
case SDL_PIXELFORMAT_UYVY: /**< Packed mode: U0+Y0+V0+Y1 (1 plane) */
case SDL_PIXELFORMAT_YVYU: /**< Packed mode: Y0+V0+Y1+U0 (1 plane) */
dst_size = 4 * sz_plane_packed;
break;
case SDL_PIXELFORMAT_NV12: /**< Planar mode: Y + U/V interleaved (2 planes) */
case SDL_PIXELFORMAT_NV21: /**< Planar mode: Y + V/U interleaved (2 planes) */
dst_size = sz_plane + sz_plane_chroma + sz_plane_chroma;
break;
default:
SDL_assert(0 && "We should never get here (caught above)");
break;
}
swdata->pixels = (Uint8 *) SDL_malloc(dst_size);
Nov 13, 2017
Nov 13, 2017
87
88
89
90
91
if (!swdata->pixels) {
SDL_SW_DestroyYUVTexture(swdata);
SDL_OutOfMemory();
return NULL;
}
Nov 13, 2017
Nov 13, 2017
94
/* Find the pitch and offset values for the texture */
95
96
97
98
switch (format) {
case SDL_PIXELFORMAT_YV12:
case SDL_PIXELFORMAT_IYUV:
swdata->pitches[0] = w;
Oct 6, 2017
Oct 6, 2017
99
100
swdata->pitches[1] = (swdata->pitches[0] + 1) / 2;
swdata->pitches[2] = (swdata->pitches[0] + 1) / 2;
101
102
swdata->planes[0] = swdata->pixels;
swdata->planes[1] = swdata->planes[0] + swdata->pitches[0] * h;
Oct 6, 2017
Oct 6, 2017
103
swdata->planes[2] = swdata->planes[1] + swdata->pitches[1] * ((h + 1) / 2);
104
105
106
107
break;
case SDL_PIXELFORMAT_YUY2:
case SDL_PIXELFORMAT_UYVY:
case SDL_PIXELFORMAT_YVYU:
Oct 6, 2017
Oct 6, 2017
108
swdata->pitches[0] = ((w + 1) / 2) * 4;
109
110
swdata->planes[0] = swdata->pixels;
break;
Oct 6, 2017
Oct 6, 2017
111
112
113
114
115
116
117
118
119
case SDL_PIXELFORMAT_NV12:
case SDL_PIXELFORMAT_NV21:
swdata->pitches[0] = w;
swdata->pitches[1] = 2 * ((swdata->pitches[0] + 1) / 2);
swdata->planes[0] = swdata->pixels;
swdata->planes[1] = swdata->planes[0] + swdata->pitches[0] * h;
break;
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
default:
SDL_assert(0 && "We should never get here (caught above)");
break;
}
/* We're all done.. */
return (swdata);
}
int
SDL_SW_QueryYUVTexturePixels(SDL_SW_YUVTexture * swdata, void **pixels,
int *pitch)
{
*pixels = swdata->planes[0];
*pitch = swdata->pitches[0];
return 0;
}
int
SDL_SW_UpdateYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
const void *pixels, int pitch)
{
switch (swdata->format) {
case SDL_PIXELFORMAT_YV12:
case SDL_PIXELFORMAT_IYUV:
if (rect->x == 0 && rect->y == 0 &&
rect->w == swdata->w && rect->h == swdata->h) {
SDL_memcpy(swdata->pixels, pixels,
Oct 6, 2017
Oct 6, 2017
148
(swdata->h * swdata->w) + 2* ((swdata->h + 1) /2) * ((swdata->w + 1) / 2));
149
150
151
152
153
154
155
156
157
158
159
160
161
162
} else {
Uint8 *src, *dst;
int row;
size_t length;
/* Copy the Y plane */
src = (Uint8 *) pixels;
dst = swdata->pixels + rect->y * swdata->w + rect->x;
length = rect->w;
for (row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += swdata->w;
}
Oct 6, 2017
Oct 6, 2017
163
164
165
166
/* Copy the next plane */
src = (Uint8 *) pixels + rect->h * pitch;
dst = swdata->pixels + swdata->h * swdata->w;
Oct 6, 2017
Oct 6, 2017
167
168
169
dst += rect->y/2 * ((swdata->w + 1) / 2) + rect->x/2;
length = (rect->w + 1) / 2;
for (row = 0; row < (rect->h + 1)/2; ++row) {
170
SDL_memcpy(dst, src, length);
Oct 6, 2017
Oct 6, 2017
171
172
src += (pitch + 1)/2;
dst += (swdata->w + 1)/2;
173
174
175
}
/* Copy the next plane */
Oct 6, 2017
Oct 6, 2017
176
src = (Uint8 *) pixels + rect->h * pitch + ((rect->h + 1) / 2) * ((pitch + 1) / 2);
177
dst = swdata->pixels + swdata->h * swdata->w +
Oct 6, 2017
Oct 6, 2017
178
179
180
181
((swdata->h + 1)/2) * ((swdata->w+1) / 2);
dst += rect->y/2 * ((swdata->w + 1)/2) + rect->x/2;
length = (rect->w + 1) / 2;
for (row = 0; row < (rect->h + 1)/2; ++row) {
182
SDL_memcpy(dst, src, length);
Oct 6, 2017
Oct 6, 2017
183
184
src += (pitch + 1)/2;
dst += (swdata->w + 1)/2;
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
}
}
break;
case SDL_PIXELFORMAT_YUY2:
case SDL_PIXELFORMAT_UYVY:
case SDL_PIXELFORMAT_YVYU:
{
Uint8 *src, *dst;
int row;
size_t length;
src = (Uint8 *) pixels;
dst =
swdata->planes[0] + rect->y * swdata->pitches[0] +
rect->x * 2;
Oct 6, 2017
Oct 6, 2017
200
length = 4 * ((rect->w + 1) / 2);
201
202
203
204
205
206
207
for (row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += swdata->pitches[0];
}
}
break;
Oct 6, 2017
Oct 6, 2017
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
case SDL_PIXELFORMAT_NV12:
case SDL_PIXELFORMAT_NV21:
{
if (rect->x == 0 && rect->y == 0 && rect->w == swdata->w && rect->h == swdata->h) {
SDL_memcpy(swdata->pixels, pixels,
(swdata->h * swdata->w) + 2* ((swdata->h + 1) /2) * ((swdata->w + 1) / 2));
} else {
Uint8 *src, *dst;
int row;
size_t length;
/* Copy the Y plane */
src = (Uint8 *) pixels;
dst = swdata->pixels + rect->y * swdata->w + rect->x;
length = rect->w;
for (row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += swdata->w;
}
/* Copy the next plane */
src = (Uint8 *) pixels + rect->h * pitch;
dst = swdata->pixels + swdata->h * swdata->w;
dst += 2 * ((rect->y + 1)/2) * ((swdata->w + 1) / 2) + 2 * (rect->x/2);
length = 2 * ((rect->w + 1) / 2);
for (row = 0; row < (rect->h + 1)/2; ++row) {
SDL_memcpy(dst, src, length);
src += 2 * ((pitch + 1)/2);
dst += 2 * ((swdata->w + 1)/2);
}
}
}
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
}
return 0;
}
int
SDL_SW_UpdateYUVTexturePlanar(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
const Uint8 *Yplane, int Ypitch,
const Uint8 *Uplane, int Upitch,
const Uint8 *Vplane, int Vpitch)
{
const Uint8 *src;
Uint8 *dst;
int row;
size_t length;
/* Copy the Y plane */
src = Yplane;
dst = swdata->pixels + rect->y * swdata->w + rect->x;
length = rect->w;
for (row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += Ypitch;
dst += swdata->w;
}
/* Copy the U plane */
src = Uplane;
if (swdata->format == SDL_PIXELFORMAT_IYUV) {
dst = swdata->pixels + swdata->h * swdata->w;
} else {
dst = swdata->pixels + swdata->h * swdata->w +
Oct 6, 2017
Oct 6, 2017
273
((swdata->h + 1) / 2) * ((swdata->w + 1) / 2);
Oct 6, 2017
Oct 6, 2017
275
276
277
dst += rect->y/2 * ((swdata->w + 1)/2) + rect->x/2;
length = (rect->w + 1) / 2;
for (row = 0; row < (rect->h + 1)/2; ++row) {
278
279
SDL_memcpy(dst, src, length);
src += Upitch;
Oct 6, 2017
Oct 6, 2017
280
dst += (swdata->w + 1)/2;
281
282
283
284
285
286
287
288
}
/* Copy the V plane */
src = Vplane;
if (swdata->format == SDL_PIXELFORMAT_YV12) {
dst = swdata->pixels + swdata->h * swdata->w;
} else {
dst = swdata->pixels + swdata->h * swdata->w +
Oct 6, 2017
Oct 6, 2017
289
((swdata->h + 1) / 2) * ((swdata->w + 1) / 2);
Oct 6, 2017
Oct 6, 2017
291
292
293
dst += rect->y/2 * ((swdata->w + 1)/2) + rect->x/2;
length = (rect->w + 1) / 2;
for (row = 0; row < (rect->h + 1)/2; ++row) {
294
295
SDL_memcpy(dst, src, length);
src += Vpitch;
Oct 6, 2017
Oct 6, 2017
296
dst += (swdata->w + 1)/2;
297
298
299
300
301
302
303
304
305
306
307
}
return 0;
}
int
SDL_SW_LockYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
void **pixels, int *pitch)
{
switch (swdata->format) {
case SDL_PIXELFORMAT_YV12:
case SDL_PIXELFORMAT_IYUV:
Oct 6, 2017
Oct 6, 2017
308
309
case SDL_PIXELFORMAT_NV12:
case SDL_PIXELFORMAT_NV21:
310
311
312
313
if (rect
&& (rect->x != 0 || rect->y != 0 || rect->w != swdata->w
|| rect->h != swdata->h)) {
return SDL_SetError
Oct 6, 2017
Oct 6, 2017
314
("YV12, IYUV, NV12, NV21 textures only support full surface locks");
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
}
break;
}
if (rect) {
*pixels = swdata->planes[0] + rect->y * swdata->pitches[0] + rect->x * 2;
} else {
*pixels = swdata->planes[0];
}
*pitch = swdata->pitches[0];
return 0;
}
void
SDL_SW_UnlockYUVTexture(SDL_SW_YUVTexture * swdata)
{
}
int
SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture * swdata, const SDL_Rect * srcrect,
Uint32 target_format, int w, int h, void *pixels,
int pitch)
{
int stretch;
/* Make sure we're set up to display in the desired format */
Nov 13, 2017
Nov 13, 2017
341
342
343
if (target_format != swdata->target_format && swdata->display) {
SDL_FreeSurface(swdata->display);
swdata->display = NULL;
344
345
346
}
stretch = 0;
Nov 13, 2017
Nov 13, 2017
347
if (srcrect->x || srcrect->y || srcrect->w < swdata->w || srcrect->h < swdata->h) {
348
349
350
351
352
353
354
/* The source rectangle has been clipped.
Using a scratch surface is easier than adding clipped
source support to all the blitters, plus that would
slow them down in the general unclipped case.
*/
stretch = 1;
} else if ((srcrect->w != w) || (srcrect->h != h)) {
Nov 13, 2017
Nov 13, 2017
355
stretch = 1;
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
}
if (stretch) {
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
if (swdata->display) {
swdata->display->w = w;
swdata->display->h = h;
swdata->display->pixels = pixels;
swdata->display->pitch = pitch;
} else {
/* This must have succeeded in SDL_SW_SetupYUVDisplay() earlier */
SDL_PixelFormatEnumToMasks(target_format, &bpp, &Rmask, &Gmask,
&Bmask, &Amask);
swdata->display =
SDL_CreateRGBSurfaceFrom(pixels, w, h, bpp, pitch, Rmask,
Gmask, Bmask, Amask);
if (!swdata->display) {
return (-1);
}
}
if (!swdata->stretch) {
/* This must have succeeded in SDL_SW_SetupYUVDisplay() earlier */
SDL_PixelFormatEnumToMasks(target_format, &bpp, &Rmask, &Gmask,
&Bmask, &Amask);
swdata->stretch =
SDL_CreateRGBSurface(0, swdata->w, swdata->h, bpp, Rmask,
Gmask, Bmask, Amask);
if (!swdata->stretch) {
return (-1);
}
}
pixels = swdata->stretch->pixels;
pitch = swdata->stretch->pitch;
}
Nov 13, 2017
Nov 13, 2017
391
392
393
394
if (SDL_ConvertPixels(swdata->w, swdata->h, swdata->format,
swdata->planes[0], swdata->pitches[0],
target_format, pixels, pitch) < 0) {
return -1;
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
}
if (stretch) {
SDL_Rect rect = *srcrect;
SDL_SoftStretch(swdata->stretch, &rect, swdata->display, NULL);
}
return 0;
}
void
SDL_SW_DestroyYUVTexture(SDL_SW_YUVTexture * swdata)
{
if (swdata) {
SDL_free(swdata->pixels);
SDL_FreeSurface(swdata->stretch);
SDL_FreeSurface(swdata->display);
SDL_free(swdata);
}
}
/* vi: set ts=4 sw=4 expandtab: */