/
SDL_phyuv.c
504 lines (420 loc) · 15.2 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
40
static struct private_yuvhwfuncs ph_yuvfuncs =
{
41
42
43
44
ph_LockYUVOverlay,
ph_UnlockYUVOverlay,
ph_DisplayYUVOverlay,
ph_FreeYUVOverlay
45
46
};
47
int grab_ptrs2(PgVideoChannel_t* channel, FRAMEDATA* Frame0, FRAMEDATA* Frame1)
48
{
49
50
51
52
53
int planes = 0;
/* Buffers have moved; re-obtain the pointers */
Frame0->Y = (unsigned char *)PdGetOffscreenContextPtr(channel->yplane1);
Frame1->Y = (unsigned char *)PdGetOffscreenContextPtr(channel->yplane2);
54
55
56
57
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* ph_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface* display)
72
{
73
74
75
SDL_Overlay* overlay;
struct private_yuvhwdata* hwdata;
int vidport;
76
77
78
79
80
81
int rtncode;
int planes;
int i=0;
PhPoint_t pos;
/* Create the overlay structure */
82
overlay = SDL_calloc(1, sizeof(SDL_Overlay));
83
84
85
if (overlay == NULL)
{
86
SDL_OutOfMemory();
87
return NULL;
88
89
90
91
92
93
}
/* Fill in the basic members */
overlay->format = format;
overlay->w = width;
overlay->h = height;
94
overlay->hwdata = NULL;
95
96
97
98
99
/* Set up the YUV surface function structure */
overlay->hwfuncs = &ph_yuvfuncs;
/* Create the pixel data and lookup tables */
100
hwdata = SDL_calloc(1, sizeof(struct private_yuvhwdata));
101
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)
{
114
if ((overlay->hwdata->channel = PgCreateVideoChannel(Pg_VIDEO_CHANNEL_SCALER, 0)) == NULL)
115
{
116
117
SDL_SetError("ph_CreateYUVOverlay(): Create channel failed: %s\n", strerror(errno));
SDL_FreeYUVOverlay(overlay);
118
return NULL;
119
120
121
122
}
}
123
124
overlay->hwdata->forcedredraw=0;
125
PtGetAbsPosition(window, &pos.x, &pos.y);
126
127
128
129
130
131
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;
132
overlay->hwdata->State = OVERLAY_STATE_UNINIT;
133
134
overlay->hwdata->FrameData0 = (FRAMEDATA *) SDL_calloc(1, sizeof(FRAMEDATA));
overlay->hwdata->FrameData1 = (FRAMEDATA *) SDL_calloc(1, sizeof(FRAMEDATA));
135
136
vidport = -1;
137
i=0;
138
139
140
141
overlay->hwdata->ischromakey=0;
do {
142
SDL_memset(&overlay->hwdata->caps, 0x00, sizeof(PgScalerCaps_t));
143
144
145
146
147
148
149
150
151
152
overlay->hwdata->caps.size = sizeof(PgScalerCaps_t);
rtncode = PgGetScalerCapabilities(overlay->hwdata->channel, i, &overlay->hwdata->caps);
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;
}
153
vidport=1;
154
155
156
157
158
159
160
161
162
break;
}
}
else
{
break;
}
i++;
} while(1);
163
164
165
if (vidport == -1)
166
167
168
{
SDL_SetError("No available video ports for requested format\n");
SDL_FreeYUVOverlay(overlay);
169
return NULL;
170
}
171
172
173
174
overlay->hwdata->format = format;
overlay->hwdata->props.format = format;
overlay->hwdata->props.size = sizeof(PgScalerProps_t);
175
176
overlay->hwdata->props.src_dim.w = width;
overlay->hwdata->props.src_dim.h = height;
177
178
179
180
/* overlay->hwdata->chromakey = PgGetOverlayChromaColor(); */
overlay->hwdata->chromakey = PgRGB(12, 6, 12); /* very dark pink color */
overlay->hwdata->props.color_key = overlay->hwdata->chromakey;
181
182
PhAreaToRect(&overlay->hwdata->CurrentViewPort, &overlay->hwdata->props.viewport);
183
184
overlay->hwdata->props.flags = Pg_SCALER_PROP_DOUBLE_BUFFER;
185
186
187
188
if ((overlay->hwdata->ischromakey)&&(overlay->hwdata->chromakey))
{
overlay->hwdata->props.flags |= Pg_SCALER_PROP_CHROMA_ENABLE;
189
overlay->hwdata->props.flags |= Pg_SCALER_PROP_CHROMA_SPECIFY_KEY_MASK;
190
191
192
193
194
}
else
{
overlay->hwdata->props.flags &= ~Pg_SCALER_PROP_CHROMA_ENABLE;
}
195
196
rtncode = PgConfigScalerChannel(overlay->hwdata->channel, &overlay->hwdata->props);
197
198
199
200
switch(rtncode)
{
case -1: SDL_SetError("PgConfigScalerChannel failed\n");
201
202
SDL_FreeYUVOverlay(overlay);
return NULL;
203
204
205
206
207
case 1:
case 0:
default:
break;
}
208
209
planes = grab_ptrs2(overlay->hwdata->channel, overlay->hwdata->FrameData0, overlay->hwdata->FrameData1);
210
211
if(overlay->hwdata->channel->yplane1 != NULL)
212
overlay->hwdata->YStride = overlay->hwdata->channel->yplane1->pitch;
213
if(overlay->hwdata->channel->vplane1 != NULL)
214
215
216
overlay->hwdata->UStride = overlay->hwdata->channel->vplane1->pitch;
if(overlay->hwdata->channel->uplane1 != NULL)
overlay->hwdata->VStride = overlay->hwdata->channel->uplane1->pitch;
217
218
219
220
221
222
223
224
225
226
227
/* check for the validness of all planes */
if ((overlay->hwdata->channel->yplane1 == NULL) &&
(overlay->hwdata->channel->uplane1 == NULL) &&
(overlay->hwdata->channel->vplane1 == NULL))
{
SDL_FreeYUVOverlay(overlay);
SDL_SetError("PgConfigScaler() returns all planes equal NULL\n");
return NULL;
}
/*
228
overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
229
230
231
if (overlay->hwdata->current==0)
{
232
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
233
}
234
else
235
{
236
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
237
238
239
}
*/
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
240
241
/*
242
overlay->hwdata->locked = 1;
243
*/
244
245
246
/* Find the pitch and offset values for the overlay */
overlay->planes = planes;
247
248
overlay->pitches = SDL_calloc(overlay->planes, sizeof(Uint16));
overlay->pixels = SDL_calloc(overlay->planes, sizeof(Uint8*));
249
250
251
252
253
254
if (!overlay->pitches || !overlay->pixels)
{
SDL_OutOfMemory();
SDL_FreeYUVOverlay(overlay);
return(NULL);
}
255
256
257
258
259
260
261
262
if (overlay->planes > 0)
{
overlay->pitches[0] = overlay->hwdata->channel->yplane1->pitch;
overlay->pixels[0] = overlay->hwdata->CurrentFrameData->Y;
}
if (overlay->planes > 1)
{
263
overlay->pitches[1] = overlay->hwdata->channel->vplane1->pitch;
264
265
266
267
overlay->pixels[1] = overlay->hwdata->CurrentFrameData->U;
}
if (overlay->planes > 2)
{
268
overlay->pitches[2] = overlay->hwdata->channel->uplane1->pitch;
269
270
overlay->pixels[2] = overlay->hwdata->CurrentFrameData->V;
}
271
272
273
274
overlay->hwdata->State = OVERLAY_STATE_ACTIVE;
overlay->hwdata->scaler_on = 0;
overlay->hw_overlay = 1;
275
276
277
278
current_overlay=overlay;
return overlay;
279
}
280
281
int ph_LockYUVOverlay(_THIS, SDL_Overlay* overlay)
282
283
{
if (overlay == NULL)
284
{
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 ph_UnlockYUVOverlay(_THIS, SDL_Overlay* overlay)
328
{
329
if (overlay == NULL)
330
{
331
return;
332
333
}
334
overlay->hwdata->locked = 0;
335
336
}
337
int ph_DisplayYUVOverlay(_THIS, SDL_Overlay* overlay, SDL_Rect* src, SDL_Rect* dst)
338
{
339
340
int rtncode;
PhPoint_t pos;
341
342
343
SDL_Rect backrect;
PhRect_t windowextent;
int winchanged=0;
344
345
346
if ((overlay == NULL) || (overlay->hwdata==NULL))
{
347
return -1;
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
}
if (overlay->hwdata->State == OVERLAY_STATE_UNINIT)
{
return -1;
}
PtGetAbsPosition(window, &pos.x, &pos.y);
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;
}
363
364
/* If CurrentViewPort position/size has been changed, then move/resize the viewport */
365
366
367
368
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) ||
369
370
(overlay->hwdata->scaler_on==0) || (winchanged==1) ||
(overlay->hwdata->forcedredraw==1))
371
{
372
373
374
375
376
377
378
379
380
381
382
383
if (overlay->hwdata->ischromakey==1)
{
/* restore screen behind the overlay/chroma color. */
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;
this->UpdateRects(this, 1, &backrect);
/* Draw the new rectangle of the chroma color at the viewport position */
PgSetFillColor(overlay->hwdata->chromakey);
384
PgDrawIRect(dst->x, dst->y, dst->x+dst->w-1, dst->y+dst->h-1, Pg_DRAW_FILL);
385
386
PgFlush();
}
387
388
389
overlay->hwdata->props.flags |= Pg_SCALER_PROP_SCALER_ENABLE;
overlay->hwdata->scaler_on = 1;
390
391
PhWindowQueryVisible(Ph_QUERY_CONSOLE, 0, PtWidgetRid(window), &windowextent);
392
393
394
395
overlay->hwdata->CurrentViewPort.pos.x = pos.x-windowextent.ul.x+dst->x;
overlay->hwdata->CurrentViewPort.pos.y = pos.y-windowextent.ul.y+dst->y;
overlay->hwdata->CurrentViewPort.size.w = dst->w;
overlay->hwdata->CurrentViewPort.size.h = dst->h;
396
PhAreaToRect(&overlay->hwdata->CurrentViewPort, &overlay->hwdata->props.viewport);
397
398
overlay->hwdata->CurrentViewPort.pos.x = dst->x;
overlay->hwdata->CurrentViewPort.pos.y = dst->y;
399
400
rtncode = PgConfigScalerChannel(overlay->hwdata->channel, &(overlay->hwdata->props));
401
402
403
404
switch(rtncode)
{
case -1:
405
SDL_SetError("PgConfigScalerChannel() function failed\n");
406
SDL_FreeYUVOverlay(overlay);
407
return -1;
408
409
410
411
412
413
414
415
case 1:
grab_ptrs2(overlay->hwdata->channel, overlay->hwdata->FrameData0, overlay->hwdata->FrameData1);
break;
case 0:
default:
break;
}
}
416
417
418
419
/*
if (overlay->hwdata->locked==0)
420
421
422
423
{
overlay->hwdata->current = PgNextVideoFrame(overlay->hwdata->channel);
if (overlay->hwdata->current == -1)
{
424
SDL_SetError("ph_LockYUVOverlay: PgNextFrame() failed, bailing out\n");
425
426
427
SDL_FreeYUVOverlay(overlay);
return 0;
}
428
429
if (overlay->hwdata->current == 0)
430
{
431
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData0;
432
}
433
else
434
{
435
overlay->hwdata->CurrentFrameData = overlay->hwdata->FrameData1;
436
}
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
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;
}
}
454
*/
455
456
return 0;
457
458
459
460
}
void ph_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
{
461
462
SDL_Rect backrect;
463
if (overlay == NULL)
464
{
465
return;
466
}
467
468
if (overlay->hwdata == NULL)
469
{
470
return;
471
472
473
474
475
476
477
478
479
480
}
current_overlay=NULL;
/* restore screen behind the overlay/chroma color. */
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;
this->UpdateRects(this, 1, &backrect);
481
482
483
484
485
/* 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;
PgConfigScalerChannel(overlay->hwdata->channel, &(overlay->hwdata->props));
486
487
488
overlay->hwdata->scaler_on = 0;
overlay->hwdata->State = OVERLAY_STATE_UNINIT;
489
490
491
492
493
494
495
if (overlay->hwdata->channel != NULL)
{
PgDestroyVideoChannel(overlay->hwdata->channel);
overlay->hwdata->channel = NULL;
return;
}
496
497
498
overlay->hwdata->CurrentFrameData = NULL;
499
500
SDL_free(overlay->hwdata->FrameData0);
SDL_free(overlay->hwdata->FrameData1);
501
502
overlay->hwdata->FrameData0 = NULL;
overlay->hwdata->FrameData1 = NULL;
503
SDL_free(overlay->hwdata);
504
}