This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_phyuv.c
510 lines (425 loc) · 15.5 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2006 Sam Lantinga
4
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.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
/* This is the QNX Realtime Platform version of SDL YUV video overlays */
25
26
27
28
29
30
31
32
#include <errno.h>
#include <Ph.h>
#include <Pt.h>
#include "SDL_video.h"
#include "SDL_phyuv_c.h"
33
#include "../SDL_yuvfuncs.h"
34
35
#define OVERLAY_STATE_UNINIT 0
36
37
#define OVERLAY_STATE_ACTIVE 1
38
/* The functions are used to manipulate software video overlays */
39
static struct private_yuvhwfuncs ph_yuvfuncs = {
40
41
42
43
ph_LockYUVOverlay,
ph_UnlockYUVOverlay,
ph_DisplayYUVOverlay,
ph_FreeYUVOverlay
44
45
};
46
int
47
grab_ptrs2(PgVideoChannel_t * channel, FRAMEDATA * Frame0, FRAMEDATA * Frame1)
48
{
49
50
51
int planes = 0;
/* Buffers have moved; re-obtain the pointers */
52
53
54
55
56
57
Frame0->Y = (unsigned char *) PdGetOffscreenContextPtr(channel->yplane1);
Frame1->Y = (unsigned char *) PdGetOffscreenContextPtr(channel->yplane2);
Frame0->U = (unsigned char *) PdGetOffscreenContextPtr(channel->vplane1);
Frame1->U = (unsigned char *) PdGetOffscreenContextPtr(channel->vplane2);
Frame0->V = (unsigned char *) PdGetOffscreenContextPtr(channel->uplane1);
Frame1->V = (unsigned char *) PdGetOffscreenContextPtr(channel->uplane2);
58
59
60
61
62
63
if (Frame0->Y)
planes++;
if (Frame0->U)
planes++;
64
65
66
if (Frame0->V)
planes++;
67
68
return planes;
69
70
}
71
SDL_Overlay *
72
73
ph_CreateYUVOverlay(_THIS, int width, int height, Uint32 format,
SDL_Surface * display)
74
{
75
76
SDL_Overlay *overlay;
struct private_yuvhwdata *hwdata;
77
int vidport;
78
79
int rtncode;
int planes;
80
int i = 0;
81
82
83
PhPoint_t pos;
/* Create the overlay structure */
84
overlay = SDL_calloc(1, sizeof(SDL_Overlay));
85
86
if (overlay == NULL) {
87
SDL_OutOfMemory();
88
return NULL;
89
90
91
92
93
94
}
/* Fill in the basic members */
overlay->format = format;
overlay->w = width;
overlay->h = height;
95
overlay->hwdata = NULL;
96
97
98
99
100
/* Set up the YUV surface function structure */
overlay->hwfuncs = &ph_yuvfuncs;
/* Create the pixel data and lookup tables */
101
hwdata = SDL_calloc(1, sizeof(struct private_yuvhwdata));
102
103
if (hwdata == NULL) {
104
105
SDL_OutOfMemory();
SDL_FreeYUVOverlay(overlay);
106
return NULL;
107
108
}
109
110
overlay->hwdata = hwdata;
111
PhDCSetCurrent(0);
112
113
if (overlay->hwdata->channel == NULL) {
if ((overlay->hwdata->channel =
114
PgCreateVideoChannel(Pg_VIDEO_CHANNEL_SCALER, 0)) == NULL) {
115
116
SDL_SetError
("ph_CreateYUVOverlay(): Create channel failed: %s\n",
117
118
strerror(errno));
SDL_FreeYUVOverlay(overlay);
119
return NULL;
120
121
122
123
}
}
124
overlay->hwdata->forcedredraw = 0;
125
126
PtGetAbsPosition(window, &pos.x, &pos.y);
127
128
129
130
131
132
overlay->hwdata->CurrentWindowPos.x = pos.x;
overlay->hwdata->CurrentWindowPos.y = pos.y;
overlay->hwdata->CurrentViewPort.pos.x = 0;
overlay->hwdata->CurrentViewPort.pos.y = 0;
overlay->hwdata->CurrentViewPort.size.w = width;
overlay->hwdata->CurrentViewPort.size.h = height;
133
overlay->hwdata->State = OVERLAY_STATE_UNINIT;
134
overlay->hwdata->FrameData0 =
135
(FRAMEDATA *) SDL_calloc(1, sizeof(FRAMEDATA));
136
overlay->hwdata->FrameData1 =
137
(FRAMEDATA *) SDL_calloc(1, sizeof(FRAMEDATA));
138
139
vidport = -1;
140
141
142
i = 0;
overlay->hwdata->ischromakey = 0;
143
144
do {
145
146
SDL_memset(&overlay->hwdata->caps, 0x00, sizeof(PgScalerCaps_t));
overlay->hwdata->caps.size = sizeof(PgScalerCaps_t);
147
rtncode =
148
149
PgGetScalerCapabilities(overlay->hwdata->channel, i,
&overlay->hwdata->caps);
150
151
152
153
154
155
156
157
158
if (rtncode == 0) {
if (overlay->hwdata->caps.format == format) {
if ((overlay->hwdata->caps.
flags & Pg_SCALER_CAP_DST_CHROMA_KEY) ==
Pg_SCALER_CAP_DST_CHROMA_KEY) {
overlay->hwdata->ischromakey = 1;
}
vidport = 1;
break;
159
}
160
161
} else {
break;
162
163
}
i++;
164
165
}
while (1);
166
167
168
if (vidport == -1) {
169
170
SDL_SetError("No available video ports for requested format\n");
SDL_FreeYUVOverlay(overlay);
171
return NULL;
172
}
173
174
175
overlay->hwdata->format = format;
overlay->hwdata->props.format = format;
176
overlay->hwdata->props.size = sizeof(PgScalerProps_t);
177
178
overlay->hwdata->props.src_dim.w = width;
overlay->hwdata->props.src_dim.h = height;
179
180
/* overlay->hwdata->chromakey = PgGetOverlayChromaColor(); */
181
overlay->hwdata->chromakey = PgRGB(12, 6, 12); /* very dark pink color */
182
overlay->hwdata->props.color_key = overlay->hwdata->chromakey;
183
184
185
PhAreaToRect(&overlay->hwdata->CurrentViewPort,
&overlay->hwdata->props.viewport);
186
187
overlay->hwdata->props.flags = Pg_SCALER_PROP_DOUBLE_BUFFER;
188
189
if ((overlay->hwdata->ischromakey) && (overlay->hwdata->chromakey)) {
190
overlay->hwdata->props.flags |= Pg_SCALER_PROP_CHROMA_ENABLE;
191
192
193
overlay->hwdata->props.flags |=
Pg_SCALER_PROP_CHROMA_SPECIFY_KEY_MASK;
} else {
194
195
overlay->hwdata->props.flags &= ~Pg_SCALER_PROP_CHROMA_ENABLE;
}
196
197
rtncode =
198
199
PgConfigScalerChannel(overlay->hwdata->channel,
&overlay->hwdata->props);
200
201
202
switch (rtncode) {
case -1:
203
204
SDL_SetError("PgConfigScalerChannel failed\n");
SDL_FreeYUVOverlay(overlay);
205
206
207
208
209
return NULL;
case 1:
case 0:
default:
break;
210
}
211
212
planes =
213
214
grab_ptrs2(overlay->hwdata->channel, overlay->hwdata->FrameData0,
overlay->hwdata->FrameData1);
215
216
if (overlay->hwdata->channel->yplane1 != NULL)
217
overlay->hwdata->YStride = overlay->hwdata->channel->yplane1->pitch;
218
if (overlay->hwdata->channel->vplane1 != NULL)
219
overlay->hwdata->UStride = overlay->hwdata->channel->vplane1->pitch;
220
if (overlay->hwdata->channel->uplane1 != NULL)
221
overlay->hwdata->VStride = overlay->hwdata->channel->uplane1->pitch;
222
223
224
225
/* check for the validness of all planes */
if ((overlay->hwdata->channel->yplane1 == NULL) &&
(overlay->hwdata->channel->uplane1 == NULL) &&
226
(overlay->hwdata->channel->vplane1 == NULL)) {
227
228
SDL_FreeYUVOverlay(overlay);
SDL_SetError("PgConfigScaler() returns all planes equal NULL\n");
229
return NULL;
230
231
}
/*
232
overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
233
234
235
if (overlay->hwdata->current==0)
{
236
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
237
}
238
else
239
{
240
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
241
242
243
}
*/
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
244
245
/*
246
overlay->hwdata->locked = 1;
247
*/
248
249
250
/* Find the pitch and offset values for the overlay */
overlay->planes = planes;
251
252
overlay->pitches = SDL_calloc(overlay->planes, sizeof(Uint16));
overlay->pixels = SDL_calloc(overlay->planes, sizeof(Uint8 *));
253
if (!overlay->pitches || !overlay->pixels) {
254
255
SDL_OutOfMemory();
SDL_FreeYUVOverlay(overlay);
256
return (NULL);
257
}
258
259
if (overlay->planes > 0) {
260
overlay->pitches[0] = overlay->hwdata->channel->yplane1->pitch;
261
overlay->pixels[0] = overlay->hwdata->CurrentFrameData->Y;
262
}
263
if (overlay->planes > 1) {
264
overlay->pitches[1] = overlay->hwdata->channel->vplane1->pitch;
265
overlay->pixels[1] = overlay->hwdata->CurrentFrameData->U;
266
}
267
if (overlay->planes > 2) {
268
overlay->pitches[2] = overlay->hwdata->channel->uplane1->pitch;
269
overlay->pixels[2] = overlay->hwdata->CurrentFrameData->V;
270
}
271
272
273
274
overlay->hwdata->State = OVERLAY_STATE_ACTIVE;
overlay->hwdata->scaler_on = 0;
overlay->hw_overlay = 1;
275
276
current_overlay = overlay;
277
278
return overlay;
279
}
280
281
int
282
ph_LockYUVOverlay(_THIS, SDL_Overlay * overlay)
283
{
284
if (overlay == NULL) {
285
return -1;
286
}
287
288
289
290
overlay->hwdata->locked = 1;
/* overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
291
292
if (overlay->hwdata->current == -1)
{
293
SDL_SetError("ph_LockYUVOverlay: PgNextFrame() failed, bailing out\n");
294
SDL_FreeYUVOverlay(overlay);
295
return 0;
296
}
297
298
if (overlay->hwdata->current == 0)
299
{
300
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
301
}
302
else
303
{
304
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
305
}
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
if (overlay->planes > 0)
{
overlay->pitches[0] = overlay->hwdata->channel->yplane1->pitch;
overlay->pixels[0] = overlay->hwdata->CurrentFrameData->Y;
}
if (overlay->planes > 1)
{
overlay->pitches[1] = overlay->hwdata->channel->uplane1->pitch;
overlay->pixels[1] = overlay->hwdata->CurrentFrameData->U;
}
if (overlay->planes > 2)
{
overlay->pitches[2] = overlay->hwdata->channel->vplane1->pitch;
overlay->pixels[2] = overlay->hwdata->CurrentFrameData->V;
}
322
*/
323
324
return (0);
325
326
}
327
void
328
ph_UnlockYUVOverlay(_THIS, SDL_Overlay * overlay)
329
{
330
if (overlay == NULL) {
331
return;
332
333
}
334
overlay->hwdata->locked = 0;
335
336
}
337
int
338
339
ph_DisplayYUVOverlay(_THIS, SDL_Overlay * overlay, SDL_Rect * src,
SDL_Rect * dst)
340
{
341
342
int rtncode;
PhPoint_t pos;
343
344
SDL_Rect backrect;
PhRect_t windowextent;
345
int winchanged = 0;
346
347
if ((overlay == NULL) || (overlay->hwdata == NULL)) {
348
return -1;
349
350
}
351
if (overlay->hwdata->State == OVERLAY_STATE_UNINIT) {
352
353
354
return -1;
}
355
PtGetAbsPosition(window, &pos.x, &pos.y);
356
357
358
359
360
if ((pos.x != overlay->hwdata->CurrentWindowPos.x) ||
(pos.y != overlay->hwdata->CurrentWindowPos.y)) {
winchanged = 1;
overlay->hwdata->CurrentWindowPos.x = pos.x;
overlay->hwdata->CurrentWindowPos.y = pos.y;
361
}
362
363
/* If CurrentViewPort position/size has been changed, then move/resize the viewport */
364
365
366
367
if ((overlay->hwdata->CurrentViewPort.pos.x != dst->x) ||
(overlay->hwdata->CurrentViewPort.pos.y != dst->y) ||
(overlay->hwdata->CurrentViewPort.size.w != dst->w) ||
(overlay->hwdata->CurrentViewPort.size.h != dst->h) ||
368
369
(overlay->hwdata->scaler_on == 0) || (winchanged == 1) ||
(overlay->hwdata->forcedredraw == 1)) {
370
371
if (overlay->hwdata->ischromakey == 1) {
372
/* restore screen behind the overlay/chroma color. */
373
374
375
376
backrect.x = overlay->hwdata->CurrentViewPort.pos.x;
backrect.y = overlay->hwdata->CurrentViewPort.pos.y;
backrect.w = overlay->hwdata->CurrentViewPort.size.w;
backrect.h = overlay->hwdata->CurrentViewPort.size.h;
377
this->UpdateRects(this, 1, &backrect);
378
379
/* Draw the new rectangle of the chroma color at the viewport position */
380
381
382
383
PgSetFillColor(overlay->hwdata->chromakey);
PgDrawIRect(dst->x, dst->y, dst->x + dst->w - 1,
dst->y + dst->h - 1, Pg_DRAW_FILL);
PgFlush();
384
}
385
386
387
overlay->hwdata->props.flags |= Pg_SCALER_PROP_SCALER_ENABLE;
overlay->hwdata->scaler_on = 1;
388
389
390
PhWindowQueryVisible(Ph_QUERY_CONSOLE, 0, PtWidgetRid(window),
&windowextent);
391
392
393
394
overlay->hwdata->CurrentViewPort.pos.x =
pos.x - windowextent.ul.x + dst->x;
overlay->hwdata->CurrentViewPort.pos.y =
pos.y - windowextent.ul.y + dst->y;
395
396
overlay->hwdata->CurrentViewPort.size.w = dst->w;
overlay->hwdata->CurrentViewPort.size.h = dst->h;
397
398
PhAreaToRect(&overlay->hwdata->CurrentViewPort,
&overlay->hwdata->props.viewport);
399
400
overlay->hwdata->CurrentViewPort.pos.x = dst->x;
overlay->hwdata->CurrentViewPort.pos.y = dst->y;
401
402
rtncode =
403
404
PgConfigScalerChannel(overlay->hwdata->channel,
&(overlay->hwdata->props));
405
406
407
switch (rtncode) {
case -1:
408
409
SDL_SetError("PgConfigScalerChannel() function failed\n");
SDL_FreeYUVOverlay(overlay);
410
411
return -1;
case 1:
412
413
414
grab_ptrs2(overlay->hwdata->channel,
overlay->hwdata->FrameData0,
overlay->hwdata->FrameData1);
415
416
417
418
break;
case 0:
default:
break;
419
420
}
}
421
422
423
424
/*
if (overlay->hwdata->locked==0)
425
426
427
428
{
overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
if (overlay->hwdata->current == -1)
{
429
SDL_SetError("ph_LockYUVOverlay: PgNextFrame() failed, bailing out\n");
430
431
432
SDL_FreeYUVOverlay(overlay);
return 0;
}
433
434
if (overlay->hwdata->current == 0)
435
{
436
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
437
}
438
else
439
{
440
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
441
}
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
if (overlay->planes > 0)
{
overlay->pitches[0] = overlay->hwdata->channel->yplane1->pitch;
overlay->pixels[0] = overlay->hwdata->CurrentFrameData->Y;
}
if (overlay->planes > 1)
{
overlay->pitches[1] = overlay->hwdata->channel->uplane1->pitch;
overlay->pixels[1] = overlay->hwdata->CurrentFrameData->U;
}
if (overlay->planes > 2)
{
overlay->pitches[2] = overlay->hwdata->channel->vplane1->pitch;
overlay->pixels[2] = overlay->hwdata->CurrentFrameData->V;
}
}
459
*/
460
461
return 0;
462
463
}
464
void
465
ph_FreeYUVOverlay(_THIS, SDL_Overlay * overlay)
466
{
467
468
SDL_Rect backrect;
469
if (overlay == NULL) {
470
return;
471
}
472
473
if (overlay->hwdata == NULL) {
474
return;
475
476
}
477
current_overlay = NULL;
478
479
/* restore screen behind the overlay/chroma color. */
480
481
482
483
backrect.x = overlay->hwdata->CurrentViewPort.pos.x;
backrect.y = overlay->hwdata->CurrentViewPort.pos.y;
backrect.w = overlay->hwdata->CurrentViewPort.size.w;
backrect.h = overlay->hwdata->CurrentViewPort.size.h;
484
this->UpdateRects(this, 1, &backrect);
485
486
487
488
/* it is need for some buggy drivers, that can't hide overlay before */
/* freeing buffer, so we got trash on the srceen */
overlay->hwdata->props.flags &= ~Pg_SCALER_PROP_SCALER_ENABLE;
489
490
PgConfigScalerChannel(overlay->hwdata->channel,
&(overlay->hwdata->props));
491
492
493
overlay->hwdata->scaler_on = 0;
overlay->hwdata->State = OVERLAY_STATE_UNINIT;
494
495
if (overlay->hwdata->channel != NULL) {
496
PgDestroyVideoChannel(overlay->hwdata->channel);
497
498
overlay->hwdata->channel = NULL;
return;
499
500
501
}
overlay->hwdata->CurrentFrameData = NULL;
502
503
504
SDL_free(overlay->hwdata->FrameData0);
SDL_free(overlay->hwdata->FrameData1);
505
506
overlay->hwdata->FrameData0 = NULL;
overlay->hwdata->FrameData1 = NULL;
507
SDL_free(overlay->hwdata);
508
}
509
510
/* vi: set ts=4 sw=4 expandtab: */