jpegdec reimplementation (#912)

* add jpegdec reimplementation

* reduce work memory

* fix color space

* jpegdec: cleanup results to use atmosphere style

* fix outdated comments, correct do/while bug

Co-authored-by: Michael Scire <SciresM@gmail.com>
This commit is contained in:
HookedBehemoth
2020-04-20 11:07:37 +02:00
committed by GitHub
parent b39b6f0d5b
commit 90d754f920
12 changed files with 735 additions and 1 deletions

View File

@@ -0,0 +1,148 @@
/*
* 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

@@ -0,0 +1,43 @@
/*
* 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

@@ -0,0 +1,79 @@
/*
* 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

@@ -0,0 +1,34 @@
/*
* 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

@@ -0,0 +1,103 @@
/*
* 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"
extern "C" {
extern u32 __start__;
u32 __nx_applet_type = AppletType_None;
#define INNER_HEAP_SIZE 0x18000
size_t nx_inner_heap_size = INNER_HEAP_SIZE;
char nx_inner_heap[INNER_HEAP_SIZE];
void __libnx_initheap(void);
void __appInit(void);
void __appExit(void);
/* Exception handling. */
alignas(16) u8 __nx_exception_stack[ams::os::MemoryPageSize];
u64 __nx_exception_stack_size = sizeof(__nx_exception_stack);
void __libnx_exception_handler(ThreadExceptionDump *ctx);
}
namespace ams {
ncm::ProgramId CurrentProgramId = ncm::SystemProgramId::JpegDec;
namespace result {
bool CallFatalOnResultAssertion = true;
}
}
using namespace ams;
void __libnx_exception_handler(ThreadExceptionDump *ctx) {
ams::CrashHandler(ctx);
}
void __libnx_initheap(void) {
void* addr = nx_inner_heap;
size_t size = nx_inner_heap_size;
/* Newlib */
extern char* fake_heap_start;
extern char* fake_heap_end;
fake_heap_start = (char*)addr;
fake_heap_end = (char*)addr + size;
}
void __appInit(void) {
hos::InitializeForStratosphere();
ams::CheckApiVersion();
}
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. */
os::SetThreadNamePointer(os::GetCurrentThread(), AMS_GET_SYSTEM_THREAD_NAME(jpegdec, Main));
/* Official jpegdec changes its thread priority to 21 in main. */
/* This is because older versions of the sysmodule had priority 20 in npdm. */
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));
/* Loop forever, servicing our services. */
g_server_manager.LoopProcess();
/* Cleanup */
return 0;
}