aboutsummaryrefslogtreecommitdiffstats
path: root/distrib/sdl-1.2.15/src/video/ps3/SDL_ps3yuv.c
blob: b1e17dae6d72ae064977746bb77c20a76dcc4f57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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
/*
 * SDL - Simple DirectMedia Layer
 * CELL BE Support for PS3 Framebuffer
 * Copyright (C) 2008, 2009 International Business Machines Corporation
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * 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
 *
 *  Martin Lowinski  <lowinski [at] de [dot] ibm [ibm] com>
 *  Dirk Herrendoerfer <d.herrendoerfer [at] de [dot] ibm [dot] com>
 *  SPE code based on research by:
 *  Rene Becker
 *  Thimo Emmerich
 */

#include "SDL_config.h"

#include "SDL_video.h"
#include "SDL_ps3video.h"
#include "SDL_ps3yuv_c.h"
#include "../SDL_yuvfuncs.h"
#include "spulibs/spu_common.h"

/* Stores the executable name */
extern spe_program_handle_t yuv2rgb_spu;
extern spe_program_handle_t bilin_scaler_spu;

int SPE_Start(_THIS, spu_data_t * spe_data);
int SPE_Stop(_THIS, spu_data_t * spe_data);
int SPE_Boot(_THIS, spu_data_t * spe_data);
int SPE_Shutdown(_THIS, spu_data_t * spe_data);
int SPE_SendMsg(_THIS, spu_data_t * spe_data, unsigned int msg);
int SPE_WaitForMsg(_THIS, spu_data_t * spe_data, unsigned int msg);
void SPE_RunContext(void *thread_argp);


/* The functions used to manipulate software video overlays */
static struct private_yuvhwfuncs ps3_yuvfuncs = {
  PS3_LockYUVOverlay,
  PS3_UnlockYUVOverlay,
  PS3_DisplayYUVOverlay,
  PS3_FreeYUVOverlay
};


struct private_yuvhwdata {
	SDL_Surface *display;
	SDL_Surface *stretch;
    volatile void * pixels __attribute__((aligned(128)));

	/* These are just so we don't have to allocate them separately */
	Uint16 pitches[3];
	Uint8 * planes[3];

	unsigned int scale;

	/* Scaled YUV picture */
	Uint8 * scaler_out __attribute__((aligned(128)));

	/* YUV2RGB converter data */
    volatile struct yuv2rgb_parms_t * converter_parms __attribute__((aligned(128)));

	/* Scaler data */
    volatile struct scale_parms_t * scaler_parms __attribute__((aligned(128)));

	Uint8 locked;
};


SDL_Overlay *PS3_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display) {
	/* Only RGB packed pixel conversion supported */
	if ((display->format->BytesPerPixel != 2) &&
			(display->format->BytesPerPixel != 3) &&
			(display->format->BytesPerPixel != 4))
	{
		SDL_SetError ("Can't use YUV data on non 16/24/32 bit surfaces");
		return NULL;
	}

	/* Double-check the requested format. We'll only support YV12 */
	switch (format) {
	    case SDL_IYUV_OVERLAY:
		case SDL_YV12_OVERLAY:
			/* Supported YUV format */
			break;
		default:
			SDL_SetError("Unsupported YUV format");
			return NULL;
	}

	SDL_Overlay* overlay;
	struct private_yuvhwdata* hwdata;

	/* Create the overlay structure */
	overlay = (SDL_Overlay *) SDL_calloc(1, sizeof(SDL_Overlay));
	if (overlay == NULL) {
		SDL_OutOfMemory();
		return NULL;
	}
	SDL_memset(overlay, 0, (sizeof *overlay));

	/* Set the basic attributes */
	overlay->format = format;
	overlay->w = width;
	overlay->h = height;
	overlay->hwdata = NULL;

	/* Set up the PS3 YUV surface function structure */
	overlay->hwfuncs = &ps3_yuvfuncs;

	/* Create the pixel data and lookup tables */
	hwdata = (struct private_yuvhwdata *) SDL_calloc(1, sizeof(struct private_yuvhwdata));
	if (hwdata == NULL) {
		SDL_OutOfMemory();
		SDL_FreeYUVOverlay(overlay);
		return NULL;
	}
	overlay->hwdata = hwdata;

	hwdata->stretch = NULL;
	hwdata->display = display;

	/* Create SPU parms structure */
	hwdata->converter_parms = (struct yuv2rgb_parms_t *) memalign(16, sizeof(struct yuv2rgb_parms_t));
	hwdata->scaler_parms = (struct scale_parms_t *) memalign(16, sizeof(struct scale_parms_t));
	if (hwdata->converter_parms == NULL || hwdata->scaler_parms == NULL) {
		SDL_FreeYUVOverlay(overlay);
		SDL_OutOfMemory();
		return(NULL);
	}

	/* Set up the SPEs */
	scaler_thread_data = (spu_data_t *) malloc(sizeof(spu_data_t));
	converter_thread_data = (spu_data_t *) malloc(sizeof(spu_data_t));
	if (converter_thread_data == NULL || scaler_thread_data == NULL) {
		SDL_FreeYUVOverlay(overlay);
		SDL_OutOfMemory();
		return(NULL);
	}

	scaler_thread_data->program = bilin_scaler_spu;
	scaler_thread_data->program_name = "bilin_scaler_spu";
	scaler_thread_data->keepalive = 0;
	scaler_thread_data->booted = 0;

	converter_thread_data->program = yuv2rgb_spu;
	converter_thread_data->program_name = "yuv2rgb_spu";
	converter_thread_data->keepalive = 1;
	converter_thread_data->booted = 0;

	SPE_Start(this, converter_thread_data);

	hwdata->pixels = (Uint8 *) memalign(16, width * height + ((width * height) >> 1));
	if (hwdata->pixels == NULL) {
		SDL_FreeYUVOverlay(overlay);
		SDL_OutOfMemory();
		return(NULL);
	}

	/* Find the pitch and offset values for the overlay */
	overlay->pitches = hwdata->pitches;
	overlay->pixels = hwdata->planes;
	switch (format) {
	    case SDL_YV12_OVERLAY:
	    case SDL_IYUV_OVERLAY:
			overlay->pitches[0] = overlay->w;
			overlay->pitches[1] = overlay->pitches[0] / 2;
			overlay->pitches[2] = overlay->pitches[0] / 2;
			overlay->pixels[0] = (Uint8 *)hwdata->pixels;
			overlay->pixels[1] = overlay->pixels[0] +
				overlay->pitches[0] * overlay->h;
			overlay->pixels[2] = overlay->pixels[1] +
				overlay->pitches[1] * overlay->h / 2;
			overlay->planes = 3;
		break;
	    default:
		/* We should never get here (caught above) */
		break;
	}

	/* We're all done.. */
	return overlay;
}


int PS3_LockYUVOverlay(_THIS, SDL_Overlay *overlay) {
	if (overlay == NULL) {
		return -1;
	}
	overlay->hwdata->locked = 1;

	return 0;
}


void PS3_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay) {
	if (overlay == NULL) {
		return;
	}
	overlay->hwdata->locked = 0;

	return;
}


int PS3_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst) {
	if ((overlay == NULL) || (overlay->hwdata == NULL)) {
		return -1;
	}

	Uint8 *lum, *Cr, *Cb;
	struct private_yuvhwdata *hwdata;
	SDL_Surface *display;

	hwdata = overlay->hwdata;
	display = hwdata->display;

	/* Do we have to scale? */
	if ((src->w != dst->w) || (src->h != dst->h) ) {
		hwdata->scale = 1;
		deprintf(1, "[PS3] We need to scale\n");
	} else {
		hwdata->scale = 0;
		deprintf(1, "[PS3] No scaling\n");
	}

	/* Find out where the various portions of the image are */
	switch (overlay->format) {
		case SDL_YV12_OVERLAY:
			lum = (Uint8 *)overlay->pixels[0];
			Cr =  (Uint8 *)overlay->pixels[1];
			Cb =  (Uint8 *)overlay->pixels[2];
			break;
		case SDL_IYUV_OVERLAY:
			lum = (Uint8 *)overlay->pixels[0];
			Cr =  (Uint8 *)overlay->pixels[2];
			Cb =  (Uint8 *)overlay->pixels[1];
			break;
		default:
			SDL_SetError("Unsupported YUV format in blit");
			return -1;
	}

	if (hwdata->scale) {
		/* Alloc mem for scaled YUV picture */
		hwdata->scaler_out = (Uint8 *) memalign(16, dst->w * dst->h + ((dst->w * dst->h) >> 1));
		if (hwdata->scaler_out == NULL) {
			SDL_FreeYUVOverlay(overlay);
			SDL_OutOfMemory();
			return -1;
		}

		/* Set parms for scaling */
		hwdata->scaler_parms->src_pixel_width = src->w;
		hwdata->scaler_parms->src_pixel_height = src->h;
		hwdata->scaler_parms->dst_pixel_width = dst->w;
		hwdata->scaler_parms->dst_pixel_height = dst->h;
		hwdata->scaler_parms->y_plane = lum;
		hwdata->scaler_parms->v_plane = Cr;
		hwdata->scaler_parms->u_plane = Cb;
		hwdata->scaler_parms->dstBuffer = hwdata->scaler_out;
		scaler_thread_data->argp = (void *)hwdata->scaler_parms;

		/* Scale the YUV overlay to given size */
		SPE_Start(this, scaler_thread_data);
		SPE_Stop(this, scaler_thread_data);

		/* Set parms for converting after scaling */
		hwdata->converter_parms->y_plane = hwdata->scaler_out;
		hwdata->converter_parms->v_plane = hwdata->scaler_out + dst->w * dst->h;
		hwdata->converter_parms->u_plane = hwdata->scaler_out + dst->w * dst->h + ((dst->w * dst->h) >> 2);
	} else {
		/* Set parms for converting */
		hwdata->converter_parms->y_plane = lum;
		hwdata->converter_parms->v_plane = Cr;
		hwdata->converter_parms->u_plane = Cb;
	}

	hwdata->converter_parms->src_pixel_width = dst->w;
	hwdata->converter_parms->src_pixel_height = dst->h;
	hwdata->converter_parms->dstBuffer = (Uint8 *) s_pixels;
	converter_thread_data->argp = (void *)hwdata->converter_parms;

	/* Convert YUV overlay to RGB */
	SPE_SendMsg(this, converter_thread_data, SPU_START);
	SPE_SendMsg(this, converter_thread_data, (unsigned int)converter_thread_data->argp);

	/* Centering */
	s_bounded_input_width = dst->w;
	s_bounded_input_height = dst->h;

	/* UpdateRects() will do the rest.. */
	SDL_UpdateRects(display, 1, dst);

	if (hwdata->scale)
		SDL_free((void *)hwdata->scaler_out);

	return 0;
}


void PS3_FreeYUVOverlay(_THIS, SDL_Overlay *overlay) {
	if (overlay == NULL) {
		return;
	}

	if (overlay->hwdata == NULL) {
		return;
	}

	struct private_yuvhwdata * hwdata;
	hwdata = overlay->hwdata;

	if (scaler_thread_data)
		SDL_free(scaler_thread_data);
	if (converter_thread_data) {
		SPE_Shutdown(this, converter_thread_data);
		SDL_free(converter_thread_data);
	}

	if (hwdata) {
		if (hwdata->pixels)
			SDL_free((void *)hwdata->pixels);
		SDL_free(hwdata);
	}
	return;
}