This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_phyuv.c
509 lines (424 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
47
int
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
72
73
SDL_Overlay *
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
114
115
116
117
if (overlay->hwdata->channel == NULL) {
if ((overlay->hwdata->channel =
PgCreateVideoChannel(Pg_VIDEO_CHANNEL_SCALER, 0)) == NULL) {
SDL_SetError
("ph_CreateYUVOverlay(): Create channel failed: %s\n",
strerror(errno));
118
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
135
136
137
overlay->hwdata->FrameData0 =
(FRAMEDATA *) SDL_calloc(1, sizeof(FRAMEDATA));
overlay->hwdata->FrameData1 =
(FRAMEDATA *) SDL_calloc(1, sizeof(FRAMEDATA));
138
139
vidport = -1;
140
141
142
i = 0;
overlay->hwdata->ischromakey = 0;
143
144
do {
145
SDL_memset(&overlay->hwdata->caps, 0x00, sizeof(PgScalerCaps_t));
146
overlay->hwdata->caps.size = sizeof(PgScalerCaps_t);
147
148
149
150
151
rtncode =
PgGetScalerCapabilities(overlay->hwdata->channel, i,
&overlay->hwdata->caps);
if (rtncode == 0) {
if (overlay->hwdata->caps.format == format) {
152
153
if ((overlay->hwdata->
caps.flags & Pg_SCALER_CAP_DST_CHROMA_KEY) ==
154
155
156
157
158
Pg_SCALER_CAP_DST_CHROMA_KEY) {
overlay->hwdata->ischromakey = 1;
}
vidport = 1;
break;
159
}
160
161
} else {
break;
162
163
}
i++;
164
} while (1);
165
166
167
if (vidport == -1) {
168
169
SDL_SetError("No available video ports for requested format\n");
SDL_FreeYUVOverlay(overlay);
170
return NULL;
171
}
172
173
174
175
overlay->hwdata->format = format;
overlay->hwdata->props.format = format;
overlay->hwdata->props.size = sizeof(PgScalerProps_t);
176
177
overlay->hwdata->props.src_dim.w = width;
overlay->hwdata->props.src_dim.h = height;
178
179
/* overlay->hwdata->chromakey = PgGetOverlayChromaColor(); */
180
overlay->hwdata->chromakey = PgRGB(12, 6, 12); /* very dark pink color */
181
overlay->hwdata->props.color_key = overlay->hwdata->chromakey;
182
183
184
PhAreaToRect(&overlay->hwdata->CurrentViewPort,
&overlay->hwdata->props.viewport);
185
186
overlay->hwdata->props.flags = Pg_SCALER_PROP_DOUBLE_BUFFER;
187
188
if ((overlay->hwdata->ischromakey) && (overlay->hwdata->chromakey)) {
189
overlay->hwdata->props.flags |= Pg_SCALER_PROP_CHROMA_ENABLE;
190
191
192
overlay->hwdata->props.flags |=
Pg_SCALER_PROP_CHROMA_SPECIFY_KEY_MASK;
} else {
193
194
overlay->hwdata->props.flags &= ~Pg_SCALER_PROP_CHROMA_ENABLE;
}
195
196
197
198
rtncode =
PgConfigScalerChannel(overlay->hwdata->channel,
&overlay->hwdata->props);
199
200
201
202
203
204
205
206
207
208
switch (rtncode) {
case -1:
SDL_SetError("PgConfigScalerChannel failed\n");
SDL_FreeYUVOverlay(overlay);
return NULL;
case 1:
case 0:
default:
break;
209
}
210
211
212
213
planes =
grab_ptrs2(overlay->hwdata->channel, overlay->hwdata->FrameData0,
overlay->hwdata->FrameData1);
214
215
if (overlay->hwdata->channel->yplane1 != NULL)
216
overlay->hwdata->YStride = overlay->hwdata->channel->yplane1->pitch;
217
if (overlay->hwdata->channel->vplane1 != NULL)
218
overlay->hwdata->UStride = overlay->hwdata->channel->vplane1->pitch;
219
if (overlay->hwdata->channel->uplane1 != NULL)
220
overlay->hwdata->VStride = overlay->hwdata->channel->uplane1->pitch;
221
222
223
224
/* check for the validness of all planes */
if ((overlay->hwdata->channel->yplane1 == NULL) &&
(overlay->hwdata->channel->uplane1 == NULL) &&
225
226
227
228
(overlay->hwdata->channel->vplane1 == NULL)) {
SDL_FreeYUVOverlay(overlay);
SDL_SetError("PgConfigScaler() returns all planes equal NULL\n");
return NULL;
229
230
}
/*
231
overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
232
233
234
if (overlay->hwdata->current==0)
{
235
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
236
}
237
else
238
{
239
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
240
241
242
}
*/
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
243
244
/*
245
overlay->hwdata->locked = 1;
246
*/
247
248
249
/* Find the pitch and offset values for the overlay */
overlay->planes = planes;
250
overlay->pitches = SDL_calloc(overlay->planes, sizeof(Uint16));
251
252
overlay->pixels = SDL_calloc(overlay->planes, sizeof(Uint8 *));
if (!overlay->pitches || !overlay->pixels) {
253
254
SDL_OutOfMemory();
SDL_FreeYUVOverlay(overlay);
255
return (NULL);
256
}
257
258
if (overlay->planes > 0) {
259
overlay->pitches[0] = overlay->hwdata->channel->yplane1->pitch;
260
overlay->pixels[0] = overlay->hwdata->CurrentFrameData->Y;
261
}
262
if (overlay->planes > 1) {
263
overlay->pitches[1] = overlay->hwdata->channel->vplane1->pitch;
264
overlay->pixels[1] = overlay->hwdata->CurrentFrameData->U;
265
}
266
if (overlay->planes > 2) {
267
overlay->pitches[2] = overlay->hwdata->channel->uplane1->pitch;
268
overlay->pixels[2] = overlay->hwdata->CurrentFrameData->V;
269
}
270
271
272
273
overlay->hwdata->State = OVERLAY_STATE_ACTIVE;
overlay->hwdata->scaler_on = 0;
overlay->hw_overlay = 1;
274
275
current_overlay = overlay;
276
277
return overlay;
278
}
279
280
281
int
ph_LockYUVOverlay(_THIS, SDL_Overlay * overlay)
282
{
283
if (overlay == NULL) {
284
return -1;
285
}
286
287
288
289
overlay->hwdata->locked = 1;
/* overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
290
291
if (overlay->hwdata->current == -1)
{
292
SDL_SetError("ph_LockYUVOverlay: PgNextFrame() failed, bailing out\n");
293
SDL_FreeYUVOverlay(overlay);
294
return 0;
295
}
296
297
if (overlay->hwdata->current == 0)
298
{
299
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
300
}
301
else
302
{
303
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
304
}
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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;
}
321
*/
322
323
return (0);
324
325
}
326
327
void
ph_UnlockYUVOverlay(_THIS, SDL_Overlay * overlay)
328
{
329
if (overlay == NULL) {
330
return;
331
332
}
333
overlay->hwdata->locked = 0;
334
335
}
336
337
338
int
ph_DisplayYUVOverlay(_THIS, SDL_Overlay * overlay, SDL_Rect * src,
SDL_Rect * dst)
339
{
340
341
int rtncode;
PhPoint_t pos;
342
343
SDL_Rect backrect;
PhRect_t windowextent;
344
int winchanged = 0;
345
346
if ((overlay == NULL) || (overlay->hwdata == NULL)) {
347
return -1;
348
349
}
350
if (overlay->hwdata->State == OVERLAY_STATE_UNINIT) {
351
352
353
354
return -1;
}
PtGetAbsPosition(window, &pos.x, &pos.y);
355
356
357
358
359
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;
360
}
361
362
/* If CurrentViewPort position/size has been changed, then move/resize the viewport */
363
364
365
366
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) ||
367
368
(overlay->hwdata->scaler_on == 0) || (winchanged == 1) ||
(overlay->hwdata->forcedredraw == 1)) {
369
370
if (overlay->hwdata->ischromakey == 1) {
371
/* restore screen behind the overlay/chroma color. */
372
373
374
375
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;
376
377
378
379
this->UpdateRects(this, 1, &backrect);
/* Draw the new rectangle of the chroma color at the viewport position */
PgSetFillColor(overlay->hwdata->chromakey);
380
381
PgDrawIRect(dst->x, dst->y, dst->x + dst->w - 1,
dst->y + dst->h - 1, Pg_DRAW_FILL);
382
383
PgFlush();
}
384
385
386
overlay->hwdata->props.flags |= Pg_SCALER_PROP_SCALER_ENABLE;
overlay->hwdata->scaler_on = 1;
387
388
389
390
391
392
393
PhWindowQueryVisible(Ph_QUERY_CONSOLE, 0, PtWidgetRid(window),
&windowextent);
overlay->hwdata->CurrentViewPort.pos.x =
pos.x - windowextent.ul.x + dst->x;
overlay->hwdata->CurrentViewPort.pos.y =
pos.y - windowextent.ul.y + dst->y;
394
395
overlay->hwdata->CurrentViewPort.size.w = dst->w;
overlay->hwdata->CurrentViewPort.size.h = dst->h;
396
397
PhAreaToRect(&overlay->hwdata->CurrentViewPort,
&overlay->hwdata->props.viewport);
398
399
overlay->hwdata->CurrentViewPort.pos.x = dst->x;
overlay->hwdata->CurrentViewPort.pos.y = dst->y;
400
401
402
403
rtncode =
PgConfigScalerChannel(overlay->hwdata->channel,
&(overlay->hwdata->props));
404
405
406
407
408
409
410
411
412
413
414
415
416
417
switch (rtncode) {
case -1:
SDL_SetError("PgConfigScalerChannel() function failed\n");
SDL_FreeYUVOverlay(overlay);
return -1;
case 1:
grab_ptrs2(overlay->hwdata->channel,
overlay->hwdata->FrameData0,
overlay->hwdata->FrameData1);
break;
case 0:
default:
break;
418
419
}
}
420
421
422
423
/*
if (overlay->hwdata->locked==0)
424
425
426
427
{
overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
if (overlay->hwdata->current == -1)
{
428
SDL_SetError("ph_LockYUVOverlay: PgNextFrame() failed, bailing out\n");
429
430
431
SDL_FreeYUVOverlay(overlay);
return 0;
}
432
433
if (overlay->hwdata->current == 0)
434
{
435
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
436
}
437
else
438
{
439
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
440
}
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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;
}
}
458
*/
459
460
return 0;
461
462
}
463
464
void
ph_FreeYUVOverlay(_THIS, SDL_Overlay * overlay)
465
{
466
467
SDL_Rect backrect;
468
if (overlay == NULL) {
469
return;
470
}
471
472
if (overlay->hwdata == NULL) {
473
return;
474
475
}
476
current_overlay = NULL;
477
478
/* restore screen behind the overlay/chroma color. */
479
480
481
482
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;
483
this->UpdateRects(this, 1, &backrect);
484
485
486
487
/* 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;
488
489
PgConfigScalerChannel(overlay->hwdata->channel,
&(overlay->hwdata->props));
490
491
492
overlay->hwdata->scaler_on = 0;
overlay->hwdata->State = OVERLAY_STATE_UNINIT;
493
494
if (overlay->hwdata->channel != NULL) {
495
496
497
PgDestroyVideoChannel(overlay->hwdata->channel);
overlay->hwdata->channel = NULL;
return;
498
499
500
}
overlay->hwdata->CurrentFrameData = NULL;
501
502
503
SDL_free(overlay->hwdata->FrameData0);
SDL_free(overlay->hwdata->FrameData1);
504
505
overlay->hwdata->FrameData0 = NULL;
overlay->hwdata->FrameData1 = NULL;
506
SDL_free(overlay->hwdata);
507
}
508
509
/* vi: set ts=4 sw=4 expandtab: */