refactor jpegdec implementation into libstrat (thanks again, Behemoth!)

This commit is contained in:
Michael Scire
2020-04-20 04:37:08 -07:00
parent 90d754f920
commit 6eb77e69c4
23 changed files with 780 additions and 261 deletions

View File

@@ -1,148 +0,0 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "jpegdec_turbo.hpp"
#include <jpeglib.h>
namespace ams::jpegdec::impl {
#define CAPSRV_ABORT_UNLESS(expr) do { \
const bool __capsrv_assert_res = (expr); \
AMS_ASSERT(__capsrv_assert_res); \
AMS_ABORT_UNLESS(__capsrv_assert_res); \
} while (0)
#define CAPSRV_ASSERT(expr) do { \
const bool __capsrv_assert_res = (expr); \
AMS_ASSERT(__capsrv_assert_res); \
R_UNLESS(__capsrv_assert_res, capsrv::ResultAlbumError()); \
} while (0)
namespace {
constexpr size_t LinebufferCount = 4;
constexpr size_t ColorComponents = 3;
constexpr int ImageSizeHorizonalUnit = 0x10;
constexpr int ImageSizeVerticalUnit = 0x4;
struct RGB {
u8 r, g, b;
};
struct RGBX {
u8 r, g, b, x;
};
void JpegErrorExit(j_common_ptr cinfo) {
/* ? */
}
}
Result DecodeJpeg(DecodeOutput &out, const DecodeInput &in, u8 *work, size_t work_size) {
CAPSRV_ABORT_UNLESS(util::IsAligned(in.width, ImageSizeHorizonalUnit));
CAPSRV_ABORT_UNLESS(util::IsAligned(in.height, ImageSizeVerticalUnit));
CAPSRV_ABORT_UNLESS(out.bmp != nullptr);
CAPSRV_ABORT_UNLESS(out.bmp_size >= 4 * in.width * in.height);
CAPSRV_ABORT_UNLESS(out.width != nullptr);
CAPSRV_ABORT_UNLESS(out.height != nullptr);
const size_t linebuffer_size = ColorComponents * in.width;
const size_t total_linebuffer_size = LinebufferCount * linebuffer_size;
R_UNLESS(work_size >= total_linebuffer_size, capsrv::ResultInternalJpegWorkMemoryShortage());
jpeg_decompress_struct cinfo;
std::memset(&cinfo, 0, sizeof(cinfo));
jpeg_error_mgr jerr;
std::memset(&jerr, 0, sizeof(jerr));
cinfo.err = jpeg_std_error(&jerr);
jerr.error_exit = JpegErrorExit;
/* TODO: Here Nintendo uses setjmp, on longjmp to error ResultAlbumInvalidFileData is returned. */
jpeg_create_decompress(&cinfo);
ON_SCOPE_EXIT {
jpeg_destroy_decompress(&cinfo);
};
jpeg_mem_src(&cinfo, in.jpeg, in.jpeg_size);
R_UNLESS(jpeg_read_header(&cinfo, true) == JPEG_HEADER_OK, capsrv::ResultAlbumInvalidFileData());
R_UNLESS(cinfo.image_width == in.width, capsrv::ResultAlbumInvalidFileData());
R_UNLESS(cinfo.image_height == in.height, capsrv::ResultAlbumInvalidFileData());
cinfo.out_color_space = JCS_RGB;
cinfo.dct_method = JDCT_ISLOW;
cinfo.do_fancy_upsampling = in.fancy_upsampling;
cinfo.do_block_smoothing = in.block_smoothing;
R_UNLESS(jpeg_start_decompress(&cinfo) == TRUE, capsrv::ResultAlbumInvalidFileData());
CAPSRV_ASSERT(cinfo.output_width == in.width);
CAPSRV_ASSERT(cinfo.output_height == in.height);
CAPSRV_ASSERT(cinfo.out_color_components == ColorComponents);
CAPSRV_ASSERT(cinfo.output_components == ColorComponents);
/* Pointer to output. */
RGBX *bmp = reinterpret_cast<RGBX *>(out.bmp);
/* Decode 4 lines at once. */
u8 *linebuffer[4] = {
work + 0 * linebuffer_size,
work + 1 * linebuffer_size,
work + 2 * linebuffer_size,
work + 3 * linebuffer_size,
};
/* While we still have scanlines, parse! */
while (cinfo.output_scanline < cinfo.output_height) {
/* Decode scanlines. */
int parsed = jpeg_read_scanlines(&cinfo, linebuffer, 4);
CAPSRV_ASSERT(parsed <= ImageSizeVerticalUnit);
/* Line by line */
for (int index = 0; index < parsed; index++) {
u8 *buffer = linebuffer[index];
const RGB* rgb = reinterpret_cast<RGB *>(buffer);
for (u32 i = 0; i < in.width; i++) {
/* Fill output. */
bmp->r = rgb->r;
bmp->g = rgb->g;
bmp->b = rgb->b;
bmp->x = 0xFF;
/* Traverse buffer. */
bmp++;
rgb++;
}
}
}
R_UNLESS(jpeg_finish_decompress(&cinfo) == TRUE, capsrv::ResultAlbumInvalidFileData());
*out.width = cinfo.output_width;
*out.height = cinfo.output_height;
return ResultSuccess();
}
}

View File

@@ -1,43 +0,0 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::jpegdec::impl {
struct DecodeInput {
const u8 *jpeg;
size_t jpeg_size;
u32 width;
u32 height;
bool fancy_upsampling;
bool block_smoothing;
};
struct DecodeOutput {
u32 *width;
u32 *height;
u8 *bmp;
size_t bmp_size;
};
struct Dimensions {
u32 width, height;
};
Result DecodeJpeg(DecodeOutput &out, const DecodeInput &in, u8 *work, size_t work_size);
}

View File

@@ -1,79 +0,0 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "jpegdec_decode_service.hpp"
#include "impl/jpegdec_turbo.hpp"
namespace ams::jpegdec {
namespace {
/* Enough for four linebuffers RGB. */
u8 g_workmem[0x3C00];
}
Result DecodeService::DecodeJpeg(const sf::OutNonSecureBuffer &out, const sf::InBuffer &in, u32 width, u32 height, const CapsScreenShotDecodeOption &opts) {
u8 *bmp = out.GetPointer();
size_t bmp_size = out.GetSize();
const u8 *jpeg = in.GetPointer();
size_t jpeg_size = in.GetSize();
/* Clear the work memory and out buffer. */
std::memset(g_workmem, 0, sizeof(g_workmem));
std::memset(bmp, 0, bmp_size);
/* Clear output memory on decode failure. */
auto clear_guard = SCOPE_GUARD { std::memset(bmp, 0, bmp_size); };
R_UNLESS(util::IsAligned(width, 0x10), capsrv::ResultAlbumOutOfRange());
R_UNLESS(util::IsAligned(height, 0x4), capsrv::ResultAlbumOutOfRange());
R_UNLESS(bmp != nullptr, capsrv::ResultAlbumReadBufferShortage());
R_UNLESS(bmp_size >= 4 * width * height, capsrv::ResultAlbumReadBufferShortage());
R_UNLESS(jpeg != nullptr, capsrv::ResultAlbumInvalidFileData());
R_UNLESS(jpeg_size != 0, capsrv::ResultAlbumInvalidFileData());
impl::DecodeInput decode_input = {
.jpeg = jpeg,
.jpeg_size = jpeg_size,
.width = width,
.height = height,
.fancy_upsampling = bool(opts.fancy_upsampling),
.block_smoothing = bool(opts.block_smoothing),
};
/* Official software ignores output written to this struct. */
impl::Dimensions dims = {};
impl::DecodeOutput decode_output = {
.width = &dims.width,
.height = &dims.height,
.bmp = bmp,
.bmp_size = bmp_size,
};
/* Decode the jpeg. */
R_TRY(impl::DecodeJpeg(decode_output, decode_input, g_workmem, sizeof(g_workmem)));
clear_guard.Cancel();
/* Clear the work memory. */
std::memset(g_workmem, 0, sizeof(g_workmem));
return ResultSuccess();
}
}

View File

@@ -1,34 +0,0 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope 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 for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::jpegdec {
class DecodeService final : public sf::IServiceObject {
protected:
enum class CommandId {
DecodeJpeg = 3001,
};
public:
/* Actual commands. */
virtual Result DecodeJpeg(const sf::OutNonSecureBuffer &out, const sf::InBuffer &in, u32 width, u32 height, const CapsScreenShotDecodeOption &opts);
public:
DEFINE_SERVICE_DISPATCH_TABLE {
MAKE_SERVICE_COMMAND_META(DecodeJpeg)
};
};
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
* Copyright (c) 2018-2020 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
@@ -13,7 +13,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "jpegdec_decode_service.hpp"
#include <stratosphere.hpp>
extern "C" {
extern u32 __start__;
@@ -72,16 +72,6 @@ void __appExit(void) {
/* ... */
}
namespace {
constexpr size_t NumServers = 1;
sf::hipc::ServerManager<NumServers> g_server_manager;
/* NOTE: Official code only allows for one session. */
constexpr sm::ServiceName DecodeServiceName = sm::ServiceName::Encode("caps:dc");
constexpr size_t DecodeMaxSessions = 2;
}
int main(int argc, char **argv)
{
/* Set thread name. */
@@ -92,11 +82,14 @@ int main(int argc, char **argv)
os::ChangeThreadPriority(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_PRIORITY(jpegdec, Main));
AMS_ASSERT(os::GetThreadPriority(os::GetCurrentThread()) == AMS_GET_SYSTEM_THREAD_PRIORITY(jpegdec, Main));
/* Create service. */
R_ASSERT(g_server_manager.RegisterServer<jpegdec::DecodeService>(DecodeServiceName, DecodeMaxSessions));
/* Initialize the capsrv library. */
R_ABORT_UNLESS(capsrv::server::InitializeForDecoderServer());
/* Loop forever, servicing our services. */
g_server_manager.LoopProcess();
/* Service the decoder server. */
capsrv::server::DecoderControlServerThreadFunction(nullptr);
/* Finalize the capsrv library. */
capsrv::server::FinalizeForDecoderServer();
/* Cleanup */
return 0;