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