ppsspp/Core/HLE/sceJpeg.cpp

367 lines
11 KiB
C++
Raw Normal View History

2013-05-15 11:49:34 +00:00
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program 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 General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "native/ext/jpge/jpgd.h"
#include "Common/Common.h"
#include "Common/ChunkFile.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/FunctionWrappers.h"
#include "Core/MemMap.h"
#include "Core/Reporting.h"
#include "Core/HLE/sceMpeg.h"
2013-05-15 11:49:34 +00:00
//Uncomment if you want to dump JPEGs loaded through sceJpeg to a file
//#define JPEG_DEBUG
#ifdef JPEG_DEBUG
#include "ext/xxhash.h"
#endif
2013-09-18 16:02:49 +00:00
#include <algorithm>
2013-07-05 08:55:57 +00:00
static int mjpegWidth, mjpegHeight;
void __JpegInit() {
mjpegWidth = 0;
mjpegHeight = 0;
}
void __JpegDoState(PointerWrap &p) {
auto s = p.Section("sceJpeg", 1);
if (!s)
return;
2013-07-05 08:55:57 +00:00
p.Do(mjpegWidth);
p.Do(mjpegHeight);
}
2013-07-04 09:55:06 +00:00
u32 convertYCbCrToABGR (int y, int cb, int cr) {
2013-09-18 10:19:08 +00:00
//see http://en.wikipedia.org/wiki/Yuv#Y.27UV444_to_RGB888_conversion for more information.
cb = cb - 128;
cr = cr - 128;
int r = y + cr + (cr >> 2) + (cr >> 3) + (cr >> 5);
int g = y - ((cb >> 2) + (cb >> 4) + (cb >> 5)) - ((cr >> 1) + (cr >> 3) + (cr >> 4) + (cr >> 5));
int b = y + cb + (cb >> 1) + (cb >> 2) + (cb >> 6);
// check rgb value.
if (r > 0xFF) r = 0xFF; if(r < 0) r = 0;
if (g > 0xFF) g = 0xFF; if(g < 0) g = 0;
if (b > 0xFF) b = 0xFF; if(b < 0) b = 0;
2013-10-04 16:04:51 +00:00
return 0xFF000000 | (b << 16) | (g << 8) | (r << 0);
}
2013-10-04 17:18:13 +00:00
void __JpegCsc(u32 imageAddr, u32 yCbCrAddr, int widthHeight, int bufferWidth) {
int height = widthHeight & 0xFFF;
int width = (widthHeight >> 16) & 0xFFF;
int lineWidth = std::min(width, bufferWidth);
int skipEndOfLine = std::max(0, bufferWidth - lineWidth);
u32 *imageBuffer = (u32*)Memory::GetPointer(imageAddr);
int sizeY = width * height;
int sizeCb = sizeY >> 2;
u8 *Y = (u8*)Memory::GetPointer(yCbCrAddr);
u8 *Cb = Y + sizeY;
u8 *Cr = Cb + sizeCb;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; x += 4) {
u8 y0 = Y[x + 0];
u8 y1 = Y[x + 1];
u8 y2 = Y[x + 2];
u8 y3 = Y[x + 3];
u8 cb = *Cb++;
u8 cr = *Cr++;
// Convert to ABGR
u32 abgr0 = convertYCbCrToABGR(y0, cb, cr);
u32 abgr1 = convertYCbCrToABGR(y1, cb, cr);
u32 abgr2 = convertYCbCrToABGR(y2, cb, cr);
u32 abgr3 = convertYCbCrToABGR(y3, cb, cr);
// Write ABGR
imageBuffer[x + 0] = abgr0;
imageBuffer[x + 1] = abgr1;
imageBuffer[x + 2] = abgr2;
imageBuffer[x + 3] = abgr3;
}
Y += width;
imageBuffer += width;
imageBuffer += skipEndOfLine;
}
2013-10-04 17:18:13 +00:00
}
int sceJpegMJpegCsc(u32 imageAddr, u32 yCbCrAddr, int widthHeight, int bufferWidth)
{
__JpegCsc(imageAddr, yCbCrAddr, widthHeight, bufferWidth);
2014-02-10 03:29:43 +00:00
DEBUG_LOG(ME, "sceJpegMJpegCsc(%i, %i, %i, %i)", imageAddr, yCbCrAddr, widthHeight, bufferWidth);
2013-10-04 17:18:13 +00:00
return 0;
}
int sceJpegDecodeMJpeg(u32 jpegAddr, int jpegSize, u32 imageAddr, int dhtMode)
{
ERROR_LOG_REPORT(ME, "UNIMPL sceJpegDecodeMJpeg(%i, %i, %i, %i)", jpegAddr, jpegSize, imageAddr, dhtMode);
2013-10-04 17:18:13 +00:00
return 0;
}
int sceJpegDeleteMJpeg()
{
2013-10-17 03:57:55 +00:00
WARN_LOG(ME, "sceJpegDeleteMJpeg()");
2013-10-04 17:18:13 +00:00
return 0;
}
int sceJpegDecodeMJpegSuccessively(u32 jpegAddr, int jpegSize, u32 imageAddr, int dhtMode)
{
ERROR_LOG_REPORT(ME, "UNIMPL sceJpegDecodeMJpegSuccessively(%i, %i, %i, %i)", jpegAddr, jpegSize, imageAddr, dhtMode);
2013-10-04 17:18:13 +00:00
return 0;
}
int sceJpegCsc(u32 imageAddr, u32 yCbCrAddr, int widthHeight, int bufferWidth, int colourInfo)
{
if (bufferWidth < 0 || widthHeight < 0){
WARN_LOG(ME, "sceJpegCsc(%i, %i, %i, %i, %i)", imageAddr, yCbCrAddr, widthHeight, bufferWidth, colourInfo);
return ERROR_JPEG_INVALID_VALUE;
}
2013-10-04 17:18:13 +00:00
__JpegCsc(imageAddr, yCbCrAddr, widthHeight, bufferWidth);
2014-02-10 03:29:43 +00:00
2013-10-17 03:57:55 +00:00
DEBUG_LOG(ME, "sceJpegCsc(%i, %i, %i, %i, %i)", imageAddr, yCbCrAddr, widthHeight, bufferWidth, colourInfo);
2013-05-15 11:49:34 +00:00
return 0;
}
int sceJpegFinishMJpeg()
{
2013-10-17 03:57:55 +00:00
WARN_LOG(ME, "sceJpegFinishMJpeg()");
2013-05-15 11:49:34 +00:00
return 0;
}
int getYCbCrBufferSize(int w, int h) {
2013-07-04 09:55:06 +00:00
// Return necessary buffer size for conversion: 12 bits per pixel
return ((w * h) >> 1) * 3;
2013-05-15 11:49:34 +00:00
}
int __JpegGetOutputInfo(u32 jpegAddr, int jpegSize, u32 colourInfoAddr) {
u8 *buf = Memory::GetPointer(jpegAddr);
int width, height, actual_components;
unsigned char *jpegBuf = jpgd::decompress_jpeg_image_from_memory(buf, jpegSize, &width, &height, &actual_components, 3);
2013-07-04 09:55:06 +00:00
if (actual_components != 3) {
// The assumption that the image was RGB was wrong...
// Try again.
int components = actual_components;
jpegBuf = jpgd::decompress_jpeg_image_from_memory(buf, jpegSize, &width, &height, &actual_components, components);
}
if (jpegBuf == NULL) {
ERROR_LOG(ME, "sceJpegGetOutputInfo: Bad JPEG data");
return getYCbCrBufferSize(0, 0);
2013-07-04 09:55:06 +00:00
}
// Buffer to store info about the color space in use.
// - Bits 24 to 32 (Always empty): 0x00
// - Bits 16 to 24 (Color mode): 0x00 (Unknown), 0x01 (Greyscale) or 0x02 (YCbCr)
// - Bits 8 to 16 (Vertical chroma subsampling value): 0x00, 0x01 or 0x02
// - Bits 0 to 8 (Horizontal chroma subsampling value): 0x00, 0x01 or 0x02
if (Memory::IsValidAddress(colourInfoAddr)) {
Memory::Write_U32(0x00020202, colourInfoAddr);
}
2013-07-04 09:55:06 +00:00
#ifdef JPEG_DEBUG
char jpeg_fname[256];
u8 *jpegBuf = Memory::GetPointer(jpegAddr);
uint32 jpeg_xxhash = XXH32((const char *)jpegBuf, jpegSize, 0xC0108888);
sprintf(jpeg_fname, "Jpeg\\%X.jpg", jpeg_xxhash);
2013-07-04 09:55:06 +00:00
FILE *wfp = fopen(jpeg_fname, "wb");
2013-10-04 16:04:51 +00:00
if (!wfp) {
_wmkdir(L"Jpeg\\");
wfp = fopen(jpeg_fname, "wb");
}
2013-07-04 09:55:06 +00:00
fwrite(jpegBuf, 1, jpegSize, wfp);
fclose(wfp);
#endif //JPEG_DEBUG
return getYCbCrBufferSize(width, height);
2013-07-04 09:55:06 +00:00
}
int sceJpegGetOutputInfo(u32 jpegAddr, int jpegSize, u32 colourInfoAddr, int dhtMode)
2013-07-04 09:55:06 +00:00
{
if (!Memory::IsValidAddress(jpegAddr)) {
ERROR_LOG(ME, "sceJpegGetOutputInfo: Bad JPEG address 0x%08x", jpegAddr);
return getYCbCrBufferSize(0, 0);
}
2014-02-10 03:29:43 +00:00
DEBUG_LOG(ME, "sceJpegGetOutputInfo(%i, %i, %i, %i)", jpegAddr, jpegSize, colourInfoAddr, dhtMode);
return __JpegGetOutputInfo(jpegAddr, jpegSize, colourInfoAddr);
}
int getWidthHeight(int width, int height) {
2013-07-04 09:55:06 +00:00
return (width << 16) | height;
}
2013-10-04 17:04:41 +00:00
u32 convertRGBToYCbCr(u32 rgb) {
//see http://en.wikipedia.org/wiki/Yuv#Y.27UV444_to_RGB888_conversion for more information.
u8 r = (rgb >> 16) & 0xFF;
u8 g = (rgb >> 8) & 0xFF;
u8 b = (rgb >> 0) & 0xFF;
int y = 0.299f * r + 0.587f * g + 0.114f * b + 0;
int cb = -0.169f * r - 0.331f * g + 0.499f * b + 128.0f;
int cr = 0.499f * r - 0.418f * g - 0.0813f * b + 128.0f;
// check yCbCr value
if ( y > 0xFF) y = 0xFF; if ( y < 0) y = 0;
if (cb > 0xFF) cb = 0xFF; if (cb < 0) cb = 0;
if (cr > 0xFF) cr = 0xFF; if (cr < 0) cr = 0;
return (y << 16) | (cb << 8) | cr;
}
int __JpegConvertRGBToYCbCr (const void *data, u32 bufferOutputAddr, int width, int height) {
2013-10-04 18:52:33 +00:00
u24_be *imageBuffer = (u24_be*)data;
2013-10-04 17:04:41 +00:00
int sizeY = width * height;
int sizeCb = sizeY >> 2;
u8 *Y = (u8*)Memory::GetPointer(bufferOutputAddr);
u8 *Cb = Y + sizeY;
u8 *Cr = Cb + sizeCb;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; x += 4) {
u32 abgr0 = imageBuffer[x + 0];
u32 abgr1 = imageBuffer[x + 1];
u32 abgr2 = imageBuffer[x + 2];
u32 abgr3 = imageBuffer[x + 3];
u32 yCbCr0 = convertRGBToYCbCr(abgr0);
u32 yCbCr1 = convertRGBToYCbCr(abgr1);
u32 yCbCr2 = convertRGBToYCbCr(abgr2);
u32 yCbCr3 = convertRGBToYCbCr(abgr3);
Y[x + 0] = (yCbCr0 >> 16) & 0xFF;
Y[x + 1] = (yCbCr1 >> 16) & 0xFF;
Y[x + 2] = (yCbCr2 >> 16) & 0xFF;
Y[x + 3] = (yCbCr3 >> 16) & 0xFF;
*Cb++ = (yCbCr0 >> 8) & 0xFF;
*Cr++ = yCbCr0 & 0xFF;
}
imageBuffer += width;
Y += width ;
}
return (width << 16) | height;
}
int __JpegDecodeMJpegYCbCr(u32 jpegAddr, int jpegSize, u32 yCbCrAddr) {
u8 *buf = Memory::GetPointer(jpegAddr);
int width, height, actual_components;
unsigned char *jpegBuf = jpgd::decompress_jpeg_image_from_memory(buf, jpegSize, &width, &height, &actual_components, 3);
if (actual_components != 3) {
// The assumption that the image was RGB was wrong...
// Try again.
int components = actual_components;
jpegBuf = jpgd::decompress_jpeg_image_from_memory(buf, jpegSize, &width, &height, &actual_components, components);
2013-07-04 09:55:06 +00:00
}
if (jpegBuf == NULL) {
return getWidthHeight(0, 0);
}
if (actual_components == 3) {
__JpegConvertRGBToYCbCr(jpegBuf, yCbCrAddr, width, height);
}
2013-07-04 09:55:06 +00:00
// TODO: There's more...
return getWidthHeight(width, height);
2013-05-15 11:49:34 +00:00
}
int sceJpegDecodeMJpegYCbCr(u32 jpegAddr, int jpegSize, u32 yCbCrAddr, int yCbCrSize, int dhtMode)
{
if (!Memory::IsValidAddress(jpegAddr)) {
ERROR_LOG(ME, "sceJpegDecodeMJpegYCbCr: Bad JPEG address 0x%08x", jpegAddr);
return getWidthHeight(0, 0);
}
2014-02-10 03:29:43 +00:00
DEBUG_LOG(ME, "sceJpegDecodeMJpegYCbCr(%i, %i, %i, %i, %i)", jpegAddr, jpegSize, yCbCrAddr, yCbCrSize, dhtMode);
return __JpegDecodeMJpegYCbCr(jpegAddr, jpegSize, yCbCrAddr);
}
int sceJpegDecodeMJpegYCbCrSuccessively(u32 jpegAddr, int jpegSize, u32 yCbCrAddr, int yCbCrSize, int dhtMode)
{
if (!Memory::IsValidAddress(jpegAddr)) {
ERROR_LOG(ME, "sceJpegDecodeMJpegYCbCrSuccessively: Bad JPEG address 0x%08x", jpegAddr);
return getWidthHeight(0, 0);
}
2014-02-10 03:29:43 +00:00
DEBUG_LOG(ME, "sceJpegDecodeMJpegYCbCrSuccessively(%i, %i, %i, %i, %i)", jpegAddr, jpegSize, yCbCrAddr, yCbCrSize, dhtMode);
// Do as same way as sceJpegDecodeMJpegYCbCr() but with smaller block size
return __JpegDecodeMJpegYCbCr(jpegAddr, jpegSize, yCbCrAddr);
}
2013-05-15 11:49:34 +00:00
int sceJpeg_9B36444C()
{
ERROR_LOG_REPORT(ME, "UNIMPL sceJpeg_9B36444C()");
2013-05-15 11:49:34 +00:00
return 0;
}
2013-07-04 09:55:06 +00:00
int sceJpegCreateMJpeg(int width, int height)
2013-05-15 11:49:34 +00:00
{
2013-07-05 08:55:57 +00:00
mjpegWidth = width;
mjpegHeight = height;
2013-07-04 09:55:06 +00:00
INFO_LOG(ME, "sceJpegCreateMJpeg(%i, %i)", width, height);
2013-05-15 11:49:34 +00:00
return 0;
}
int sceJpegInitMJpeg()
{
2013-10-17 03:57:55 +00:00
WARN_LOG(ME, "sceJpegInitMJpeg()");
2013-07-04 09:55:06 +00:00
return 0;
}
2013-12-03 07:36:55 +00:00
int sceJpegMJpegCscWithColorOption()
2013-07-04 09:55:06 +00:00
{
ERROR_LOG_REPORT(ME, "UNIMPL sceJpegMJpegCscWithColorOption()");
2013-05-15 11:49:34 +00:00
return 0;
}
2014-02-10 03:29:43 +00:00
int sceJpegDecompressAllImage()
{
ERROR_LOG_REPORT(ME, "UNIMPL sceJpegDecompressAllImage()");
2014-02-10 03:29:43 +00:00
return 0;
}
2013-05-15 11:49:34 +00:00
const HLEFunction sceJpeg[] =
{
{0x0425B986, WrapI_V<sceJpegDecompressAllImage>, "sceJpegDecompressAllImage"},
{0x04B5AE02, WrapI_UUII<sceJpegMJpegCsc>, "sceJpegMJpegCsc"},
{0x04B93CEF, WrapI_UIUI<sceJpegDecodeMJpeg>, "sceJpegDecodeMJpeg"},
{0x227662D7, WrapI_UIUII<sceJpegDecodeMJpegYCbCrSuccessively>, "sceJpegDecodeMJpegYCbCrSuccessively"},
2013-05-15 11:49:34 +00:00
{0x48B602B7, WrapI_V<sceJpegDeleteMJpeg>, "sceJpegDeleteMJpeg"},
{0x64B6F978, WrapI_UIUI<sceJpegDecodeMJpegSuccessively>, "sceJpegDecodeMJpegSuccessively"},
{0x67F0ED84, WrapI_UUIII<sceJpegCsc>, "sceJpegCsc"},
2013-05-15 11:49:34 +00:00
{0x7D2F3D7F, WrapI_V<sceJpegFinishMJpeg>, "sceJpegFinishMJpeg"},
2013-07-04 09:55:06 +00:00
{0x8F2BB012, WrapI_UIUI<sceJpegGetOutputInfo>, "sceJpegGetOutputInfo"},
{0x91EED83C, WrapI_UIUII<sceJpegDecodeMJpegYCbCr>, "sceJpegDecodeMJpegYCbCr"},
2013-05-15 11:49:34 +00:00
{0x9B36444C, WrapI_V<sceJpeg_9B36444C>, "sceJpeg_9B36444C"},
2013-07-04 09:55:06 +00:00
{0x9D47469C, WrapI_II<sceJpegCreateMJpeg>, "sceJpegCreateMJpeg"},
2013-05-15 11:49:34 +00:00
{0xAC9E70E6, WrapI_V<sceJpegInitMJpeg>, "sceJpegInitMJpeg"},
2013-12-03 07:36:55 +00:00
{0xa06a75c4, WrapI_V<sceJpegMJpegCscWithColorOption>, "sceJpegMJpegCscWithColorOption"},
2013-05-15 11:49:34 +00:00
};
void Register_sceJpeg()
{
RegisterModule("sceJpeg", ARRAY_SIZE(sceJpeg), sceJpeg);
}