19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is the Playstation 2 implementation of YUV video overlays */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/mman.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <asm/page.h>		/* For definition of PAGE_SIZE */
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_gsyuv_c.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_yuvfuncs.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The maximum number of 16x16 pixel block converted at once */
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define MAX_MACROBLOCKS	1024	/* 2^10 macroblocks at once */
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The functions used to manipulate video overlays */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct private_yuvhwfuncs gs_yuvfuncs = {
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GS_LockYUVOverlay,
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GS_UnlockYUVOverlay,
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GS_DisplayYUVOverlay,
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GS_FreeYUVOverlay
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct private_yuvhwdata {
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int ipu_fd;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *pixels;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int macroblocks;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int dma_len;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	caddr_t dma_mem;
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	caddr_t ipu_imem;
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	caddr_t ipu_omem;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	caddr_t dma_tags;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned long long *stretch_x1y1;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned long long *stretch_x2y2;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_plist plist;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* These are just so we don't have to allocate them separately */
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 pitches[3];
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 *planes[3];
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int power_of_2(int value)
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int shift;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( shift = 0; (1<<shift) < value; ++shift ) {
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Keep looking */ ;
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(shift);
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Overlay *GS_CreateYUVOverlay(_THIS, int width, int height, Uint32 format, SDL_Surface *display)
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Overlay *overlay;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct private_yuvhwdata *hwdata;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int map_offset;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned long long *tags;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	caddr_t base;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int bpp;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int fbp, fbw, psm;
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int x, y, w, h;
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int pnum;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_packet *packet;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_packet tex_packet;
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We can only decode blocks of 16x16 pixels */
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (width & 15) || (height & 15) ) {
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Overlay width/height must be multiples of 16");
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Make sure the image isn't too large for a single DMA transfer */
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ((width/16) * (height/16)) > MAX_MACROBLOCKS ) {
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Overlay too large (maximum size: %d pixels)",
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		             MAX_MACROBLOCKS * 16 * 16);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Double-check the requested format.  For simplicity, we'll only
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   support planar YUV formats.
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (format) {
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_YV12_OVERLAY:
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_IYUV_OVERLAY:
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Supported planar YUV format */
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unsupported YUV format");
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the overlay structure */
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay = (SDL_Overlay *)SDL_malloc(sizeof *overlay);
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( overlay == NULL ) {
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(overlay, 0, (sizeof *overlay));
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Fill in the basic members */
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->format = format;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->w = width;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->h = height;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the YUV surface function structure */
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwfuncs = &gs_yuvfuncs;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hw_overlay = 1;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the pixel data */
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata = (struct private_yuvhwdata *)SDL_malloc(sizeof *hwdata);
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->hwdata = hwdata;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata == NULL ) {
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeYUVOverlay(overlay);
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->ipu_fd = -1;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->pixels = (Uint8 *)SDL_malloc(width*height*2);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata->pixels == NULL ) {
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeYUVOverlay(overlay);
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->macroblocks = (width/16) * (height/16);
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Find the pitch and offset values for the overlay */
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->pitches = hwdata->pitches;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	overlay->pixels = hwdata->planes;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (format) {
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_YV12_OVERLAY:
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_IYUV_OVERLAY:
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->pitches[0] = overlay->w;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->pitches[1] = overlay->pitches[0] / 2;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->pitches[2] = overlay->pitches[0] / 2;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        overlay->pixels[0] = hwdata->pixels;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        overlay->pixels[1] = overlay->pixels[0] +
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                     overlay->pitches[0] * overlay->h;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        overlay->pixels[2] = overlay->pixels[1] +
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		                     overlay->pitches[1] * overlay->h / 2;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		overlay->planes = 3;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* We should never get here (caught above) */
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Theoretically we could support several concurrent decode
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   streams queueing up on the same file descriptor, but for
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   simplicity we'll support only one.  Opening the IPU more
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   than once will fail with EBUSY.
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->ipu_fd = open("/dev/ps2ipu", O_RDWR);
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata->ipu_fd < 0 ) {
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeYUVOverlay(overlay);
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Playstation 2 IPU busy");
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate a DMA area for pixel conversion */
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bpp = this->screen->format->BytesPerPixel;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	map_offset = (mapped_len + (sysconf(_SC_PAGESIZE) - 1)) & ~(sysconf(_SC_PAGESIZE) - 1);
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->dma_len = hwdata->macroblocks * (16 * 16 + 8 * 8 + 8 * 8) +
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                  width * height * bpp +
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                  hwdata->macroblocks * (16 * sizeof(long long)) +
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                  12 * sizeof(long long);
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->dma_mem = mmap(0, hwdata->dma_len, PROT_READ|PROT_WRITE,
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                       MAP_SHARED, memory_fd, map_offset);
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata->dma_mem == MAP_FAILED ) {
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		hwdata->ipu_imem = (caddr_t)0;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeYUVOverlay(overlay);
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to map %d bytes for DMA", hwdata->dma_len);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->ipu_imem = hwdata->dma_mem;
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->ipu_omem = hwdata->ipu_imem +
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                   hwdata->macroblocks * (16 * 16 + 8 * 8 + 8 * 8);
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->dma_tags = hwdata->ipu_omem + width * height * bpp;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate memory for the DMA packets */
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->plist.num = hwdata->macroblocks * 4 + 1;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->plist.packet = (struct ps2_packet *)SDL_malloc(
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	                       hwdata->plist.num*sizeof(struct ps2_packet));
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! hwdata->plist.packet ) {
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeYUVOverlay(overlay);
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	pnum = 0;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet = hwdata->plist.packet;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the tags to send the image to the screen */
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags = (unsigned long long *)hwdata->dma_tags;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	base = hwdata->ipu_omem;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fbp = screen_image.fbp;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fbw = screen_image.fbw;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	psm = screen_image.psm;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	y = screen_image.y + screen_image.h;	/* Offscreen video memory */
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( h=height/16; h; --h ) {
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		x = 0;			/* Visible video memory */
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( w=width/16; w; --w ) {
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* The head tag */
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			packet[pnum].ptr = &tags[0];
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			packet[pnum].len = 10 * sizeof(*tags);
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			++pnum;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[0] = 4 | (1LL << 60);	/* GIFtag */
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[1] = 0x0e;			/* A+D */
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[2] = ((unsigned long long)fbp << 32) |
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			          ((unsigned long long)fbw << 48) |
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			          ((unsigned long long)psm << 56);
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[3] = PS2_GS_BITBLTBUF;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[4] = ((unsigned long long)x << 32) |
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			          ((unsigned long long)y << 48);
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[5] = PS2_GS_TRXPOS;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[6] = (unsigned long long)16 |
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			          ((unsigned long long)16 << 32);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[7] = PS2_GS_TRXREG;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[8] = 0;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[9] = PS2_GS_TRXDIR;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Now the actual image data */
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			packet[pnum].ptr = &tags[10];
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			packet[pnum].len = 2 * sizeof(*tags);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			++pnum;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[10] = ((16*16*bpp) >> 4) | (2LL << 58);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[11] = 0;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			packet[pnum].ptr = (void *)base;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			packet[pnum].len = 16 * 16 * bpp;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			++pnum;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			packet[pnum].ptr = &tags[12];
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			packet[pnum].len = 2 * sizeof(*tags);
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			++pnum;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[12] = (0 >> 4) | (1 << 15) | (2LL << 58);
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags[13] = 0;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			tags += 16;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			base += 16 * 16 * bpp;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			x += 16;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		y += 16;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the texture memory area for the video */
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tex_packet.ptr = tags;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tex_packet.len = 8 * sizeof(*tags);
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[0] = 3 | (1LL << 60);	/* GIFtag */
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[1] = 0x0e;			/* A+D */
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[2] = ((screen_image.y + screen_image.h) * screen_image.w) / 64 +
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          ((unsigned long long)fbw << 14) +
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          ((unsigned long long)psm << 20) +
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          ((unsigned long long)power_of_2(width) << 26) +
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          ((unsigned long long)power_of_2(height) << 30) +
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          ((unsigned long long)1 << 34) +
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          ((unsigned long long)1 << 35);
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[3] = PS2_GS_TEX0_1;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[4] = (1 << 5) + (1 << 6);
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[5] = PS2_GS_TEX1_1;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[6] = 0;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[7] = PS2_GS_TEXFLUSH;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl(console_fd, PS2IOC_SEND, &tex_packet);
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up the tags for scaling the image */
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet[pnum].ptr = tags;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet[pnum].len = 12 * sizeof(*tags);
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	++pnum;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[0] = 5 | (1LL << 60);	/* GIFtag */
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[1] = 0x0e;			/* A+D */
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[2] = 6 + (1 << 4) + (1 << 8);
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[3] = PS2_GS_PRIM;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[4] = ((unsigned long long)0 * 16) +
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	           (((unsigned long long)0 * 16) << 16);
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[5] = PS2_GS_UV;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[6] = 0; /* X1, Y1 */
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[7] = PS2_GS_XYZ2;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->stretch_x1y1 = &tags[6];
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[8] = ((unsigned long long)overlay->w * 16) +
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	           (((unsigned long long)overlay->h * 16) << 16);
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[9] = PS2_GS_UV;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[10] = 0; /* X2, Y2 */
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	tags[11] = PS2_GS_XYZ2;
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata->stretch_x2y2 = &tags[10];
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're all done.. */
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(overlay);
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint GS_LockYUVOverlay(_THIS, SDL_Overlay *overlay)
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid GS_UnlockYUVOverlay(_THIS, SDL_Overlay *overlay)
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint GS_DisplayYUVOverlay(_THIS, SDL_Overlay *overlay, SDL_Rect *src, SDL_Rect *dst)
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct private_yuvhwdata *hwdata;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	__u32 cmd;
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct ps2_packet packet;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int h, w, i;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 *lum, *Cr, *Cb;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int lum_pitch;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int crb_pitch;
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 *lum_src, *Cr_src, *Cb_src;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint32 *srcp, *dstp;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned int x, y;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Surface *screen;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Find out where the various portions of the image are */
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata = overlay->hwdata;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch (overlay->format) {
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_YV12_OVERLAY:
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lum = (Uint32 *)overlay->pixels[0];
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Cr =  (Uint32 *)overlay->pixels[1];
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Cb =  (Uint32 *)overlay->pixels[2];
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case SDL_IYUV_OVERLAY:
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lum = (Uint32 *)overlay->pixels[0];
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Cr =  (Uint32 *)overlay->pixels[2];
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Cb =  (Uint32 *)overlay->pixels[1];
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default:
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unsupported YUV format in blit (?)");
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	dstp = (Uint32 *)hwdata->ipu_imem;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	lum_pitch = overlay->w/4;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	crb_pitch = (overlay->w/2)/4;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Copy blocks of 16x16 pixels to the DMA area */
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( h=overlay->h/16; h; --h ) {
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lum_src = lum;
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Cr_src = Cr;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Cb_src = Cb;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( w=overlay->w/16; w; --w ) {
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcp = lum_src;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; i<16; ++i ) {
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp[0] = srcp[0];
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp[1] = srcp[1];
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp[2] = srcp[2];
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp[3] = srcp[3];
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srcp += lum_pitch;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp += 4;
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcp = Cb_src;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; i<8; ++i ) {
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp[0] = srcp[0];
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp[1] = srcp[1];
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srcp += crb_pitch;
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp += 2;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			srcp = Cr_src;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for ( i=0; i<8; ++i ) {
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp[0] = srcp[0];
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp[1] = srcp[1];
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				srcp += crb_pitch;
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				dstp += 2;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			lum_src += 16 / 4;
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Cb_src += 8 / 4;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			Cr_src += 8 / 4;
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lum += lum_pitch * 16;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Cr += crb_pitch * 8;
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Cb += crb_pitch * 8;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Send the macroblock data to the IPU */
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_YUV
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Sending data to IPU..\n");
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet.ptr = hwdata->ipu_imem;
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet.len = hwdata->macroblocks * (16 * 16 + 8 * 8 + 8 * 8);
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl(hwdata->ipu_fd, PS2IOC_SENDA, &packet);
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Trigger the DMA to the IPU for conversion */
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_YUV
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Trigging conversion command\n");
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cmd = (7 << 28) + hwdata->macroblocks;
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen_image.psm == PS2_GS_PSMCT16 ) {
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		cmd += (1 << 27) +	/* Output RGB 555 */
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       (1 << 26);	/* Dither output */
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl(hwdata->ipu_fd, PS2IOC_SIPUCMD, &cmd);
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Retrieve the converted image from the IPU */
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_YUV
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Retrieving data from IPU..\n");
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet.ptr = hwdata->ipu_omem;
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	packet.len = overlay->w * overlay->h *
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	             this->screen->format->BytesPerPixel;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl(hwdata->ipu_fd, PS2IOC_RECV, &packet);
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_YUV
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Copying image to screen..\n");
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Wait for previous DMA to complete */
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ioctl(console_fd, PS2IOC_SENDQCT, 1);
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Send the current image to the screen and scale it */
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	screen = this->screen;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	x = (unsigned int)dst->x;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	y = (unsigned int)dst->y;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( screen->offset ) {
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		x += (screen->offset % screen->pitch) /
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     screen->format->BytesPerPixel;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		y += (screen->offset / screen->pitch);
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	y += screen_image.y;
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*hwdata->stretch_x1y1 = (x * 16) + ((y * 16) << 16);
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	x += (unsigned int)dst->w;
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	y += (unsigned int)dst->h;
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*hwdata->stretch_x2y2 = (x * 16) + ((y * 16) << 16);
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return ioctl(console_fd, PS2IOC_SENDL, &hwdata->plist);
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid GS_FreeYUVOverlay(_THIS, SDL_Overlay *overlay)
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct private_yuvhwdata *hwdata;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hwdata = overlay->hwdata;
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( hwdata ) {
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->ipu_fd >= 0 ) {
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			close(hwdata->ipu_fd);
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->dma_mem ) {
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			munmap(hwdata->dma_mem, hwdata->dma_len);
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->plist.packet ) {
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(hwdata->plist.packet);
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( hwdata->pixels ) {
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(hwdata->pixels);
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(hwdata);
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
462