jpegdec: update to reflect 17.0.0 changes

This commit is contained in:
Michael Scire 2023-10-18 02:31:26 -07:00
parent 7f61dfdb8d
commit a84f725e21
6 changed files with 354 additions and 3 deletions

View file

@ -84,7 +84,7 @@ dist-no-debug: package3 $(CURRENT_DIRECTORY)/$(ATMOSPHERE_OUT_DIR)
mkdir -p $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000034
mkdir -p $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000036
mkdir -p $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000037
#mkdir -p $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/010000000000003c
mkdir -p $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/010000000000003c
mkdir -p $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000042
mkdir -p $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000420
mkdir -p $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/010000000000b240
@ -98,7 +98,7 @@ dist-no-debug: package3 $(CURRENT_DIRECTORY)/$(ATMOSPHERE_OUT_DIR)
cp stratosphere/fatal/$(ATMOSPHERE_OUT_DIR)/fatal.nsp $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000034/exefs.nsp
cp stratosphere/creport/$(ATMOSPHERE_OUT_DIR)/creport.nsp $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000036/exefs.nsp
cp stratosphere/ro/$(ATMOSPHERE_OUT_DIR)/ro.nsp $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000037/exefs.nsp
#cp stratosphere/jpegdec/$(ATMOSPHERE_OUT_DIR)/jpegdec.nsp $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/010000000000003c/exefs.nsp
cp stratosphere/jpegdec/$(ATMOSPHERE_OUT_DIR)/jpegdec.nsp $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/010000000000003c/exefs.nsp
cp stratosphere/pgl/$(ATMOSPHERE_OUT_DIR)/pgl.nsp $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000042/exefs.nsp
cp stratosphere/LogManager/$(ATMOSPHERE_OUT_DIR)/LogManager.nsp $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/0100000000000420/exefs.nsp
cp stratosphere/htc/$(ATMOSPHERE_OUT_DIR)/htc.nsp $(DIST_DIR)/stratosphere_romfs/atmosphere/contents/010000000000b240/exefs.nsp

View file

@ -16,11 +16,16 @@
#include <stratosphere.hpp>
#include "decodersrv_decoder_server_object.hpp"
#include "../jpeg/decodersrv_software_jpeg_decoder.hpp"
#include "../jpeg/decodersrv_software_jpeg_shrinker.hpp"
namespace ams::capsrv::server {
namespace {
constexpr const int JpegShrinkQualities[] = {
98, 95, 90, 80, 70, 60, 50, 40, 30, 20, 10, 0
};
Result DecodeJpegImpl(void *dst, size_t dst_size, const void *src_jpeg, size_t src_jpeg_size, u32 width, u32 height, const ScreenShotDecodeOption &option, void *work, size_t work_size) {
/* Clear the work memory. */
std::memset(work, 0, work_size);
@ -67,6 +72,61 @@ namespace ams::capsrv::server {
R_SUCCEED();
}
Result ShrinkJpegImpl(u64 *out_size, void *dst, size_t dst_size, const void *src_jpeg, size_t src_jpeg_size, u32 width, u32 height, const ScreenShotDecodeOption &option, void *work, size_t work_size) {
/* Validate parameters. */
R_UNLESS(util::IsAligned(width, 0x10), capsrv::ResultAlbumOutOfRange());
R_UNLESS(util::IsAligned(height, 0x4), capsrv::ResultAlbumOutOfRange());
R_UNLESS(dst != nullptr, capsrv::ResultInternalJpegOutBufferShortage());
R_UNLESS(dst_size != 0, capsrv::ResultAlbumReadBufferShortage());
R_UNLESS(src_jpeg != nullptr, capsrv::ResultAlbumInvalidFileData());
R_UNLESS(src_jpeg_size != 0, capsrv::ResultAlbumInvalidFileData());
/* Create the input. */
const jpeg::SoftwareJpegShrinkerInput shrink_input = {
.jpeg = src_jpeg,
.jpeg_size = src_jpeg_size,
.width = width,
.height = height,
.fancy_upsampling = option.HasJpegDecoderFlag(ScreenShotDecoderFlag_EnableFancyUpsampling),
.block_smoothing = option.HasJpegDecoderFlag(ScreenShotDecoderFlag_EnableBlockSmoothing),
};
/* Create the output. */
u64 shrunk_size = 0;
s32 shrunk_width = 0, shrunk_height = 0;
jpeg::SoftwareJpegShrinkerOutput shrink_output = {
.out_size = std::addressof(shrunk_size),
.out_width = std::addressof(shrunk_width),
.out_height = std::addressof(shrunk_height),
.dst = dst,
.dst_size = dst_size,
};
/* Try to shrink the jpeg at various quality levels. */
for (auto quality : JpegShrinkQualities) {
/* Shrink at the current quality. */
R_TRY_CATCH(jpeg::SoftwareJpegShrinker::ShrinkRgba8(shrink_output, shrink_input, quality, work, work_size)) {
/* If the output buffer isn't large enough to fit the output, we should try at a lower quality. */
R_CATCH(capsrv::ResultInternalJpegOutBufferShortage) {
continue;
}
/* Nintendo doesn't catch this result, but our lack of work buffer use makes me think this may be necessary. */
R_CATCH(capsrv::ResultInternalJpegWorkMemoryShortage) {
continue;
}
} R_END_TRY_CATCH;
/* Write the output size. */
*out_size = shrunk_size;
R_SUCCEED();
}
/* Nintendo aborts if no quality succeeds. */
AMS_ABORT("ShrinkJpeg should succeed before this point\n");
}
}
Result DecoderControlService::DecodeJpeg(const ams::sf::OutNonSecureBuffer &out, const ams::sf::InBuffer &in, u32 width, u32 height, const ScreenShotDecodeOption &option) {
@ -78,4 +138,13 @@ namespace ams::capsrv::server {
R_RETURN(DecodeJpegImpl(out.GetPointer(), out.GetSize(), in.GetPointer(), in.GetSize(), width, height, option, work, work_size));
}
Result DecoderControlService::ShrinkJpeg(ams::sf::Out<u64> out_size, const ams::sf::OutNonSecureBuffer &out, const ams::sf::InBuffer &in, u32 width, u32 height, const capsrv::ScreenShotDecodeOption &option) {
/* Get the work buffer. */
void *work = g_work_memory.jpeg_decoder_memory;
size_t work_size = sizeof(g_work_memory.jpeg_decoder_memory);
/* Call the shrink implementation. */
R_RETURN(ShrinkJpegImpl(out_size.GetPointer(), out.GetPointer(), out.GetSize(), in.GetPointer(), in.GetSize(), width, height, option, work, work_size));
}
}

View file

@ -17,7 +17,8 @@
#include <stratosphere.hpp>
#define AMS_CAPSRV_DECODER_CONTROL_SERVICE_INTERFACE_INFO(C, H) \
AMS_SF_METHOD_INFO(C, H, 3001, Result, DecodeJpeg, (const ams::sf::OutNonSecureBuffer &out, const ams::sf::InBuffer &in, u32 width, u32 height, const capsrv::ScreenShotDecodeOption &option), (out, in, width, height, option))
AMS_SF_METHOD_INFO(C, H, 3001, Result, DecodeJpeg, (const ams::sf::OutNonSecureBuffer &out, const ams::sf::InBuffer &in, u32 width, u32 height, const capsrv::ScreenShotDecodeOption &option), (out, in, width, height, option)) \
AMS_SF_METHOD_INFO(C, H, 4001, Result, ShrinkJpeg, (ams::sf::Out<u64> out_size, const ams::sf::OutNonSecureBuffer &out, const ams::sf::InBuffer &in, u32 width, u32 height, const capsrv::ScreenShotDecodeOption &option), (out_size, out, in, width, height, option))
AMS_SF_DEFINE_INTERFACE(ams::capsrv::sf, IDecoderControlService, AMS_CAPSRV_DECODER_CONTROL_SERVICE_INTERFACE_INFO, 0xD168E90B)
@ -26,6 +27,7 @@ namespace ams::capsrv::server {
class DecoderControlService final {
public:
Result DecodeJpeg(const ams::sf::OutNonSecureBuffer &out, const ams::sf::InBuffer &in, u32 width, u32 height, const ScreenShotDecodeOption &option);
Result ShrinkJpeg(ams::sf::Out<u64> out_size, const ams::sf::OutNonSecureBuffer &out, const ams::sf::InBuffer &in, u32 width, u32 height, const capsrv::ScreenShotDecodeOption &option);
};
static_assert(capsrv::sf::IsIDecoderControlService<DecoderControlService>);

View file

@ -0,0 +1,236 @@
/*
* Copyright (c) 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 <stratosphere.hpp>
#include "decodersrv_software_jpeg_shrinker.hpp"
#include "capsrv_server_jpeg_library_types.hpp"
#include "capsrv_server_jpeg_error_handler.hpp"
namespace ams::capsrv::server::jpeg {
#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 s32 ImageSizeHorizonalUnit = 0x10;
constexpr s32 ImageSizeVerticalUnitDecode = 0x4;
constexpr s32 ImageSizeVerticalUnitEncode = 0x8;
constexpr s32 RgbColorComponentCount = 3;
constexpr s32 RgbaColorComponentCount = 4;
Result GetRgbBufferSize(size_t *out_size, size_t *out_stride, s32 width, size_t work_size) {
/* Calculate the space we need and verify we have enough. */
const size_t rgb_width = util::AlignUp(static_cast<size_t>(width), ImageSizeHorizonalUnit);
const size_t rgb_stride = rgb_width * RgbColorComponentCount;
const size_t rgb_size = rgb_stride * ImageSizeVerticalUnitEncode;
R_UNLESS(work_size >= rgb_size, capsrv::ResultInternalJpegWorkMemoryShortage());
/* Return the output to the caller. */
*out_size = rgb_size;
*out_stride = rgb_stride;
R_SUCCEED();
}
void SetupEncodingParameter(JpegLibraryType::jpeg_compress_struct *cinfo, const SoftwareJpegShrinkerInput &input, int quality) {
/* Set parameters. */
cinfo->image_width = static_cast<JpegLibraryType::JDIMENSION>(input.width / 2);
cinfo->image_height = static_cast<JpegLibraryType::JDIMENSION>(input.height / 2);
cinfo->input_components = RgbColorComponentCount;
cinfo->in_color_space = JpegLibraryType::J_COLOR_SPACE::JCS_RGB;
/* Set defaults/color space. */
jpeg_set_defaults(cinfo);
jpeg_set_colorspace(cinfo, JpegLibraryType::J_COLOR_SPACE::JCS_YCbCr);
/* Configure sampling. */
/* libjpeg-turbo doesn't actually have this field, as of now. */
/* cinfo->do_fancy_downsampling = false; */
cinfo->comp_info[0].h_samp_factor = 2;
cinfo->comp_info[0].v_samp_factor = 1;
cinfo->comp_info[1].h_samp_factor = 1;
cinfo->comp_info[1].v_samp_factor = 1;
cinfo->comp_info[2].h_samp_factor = 1;
cinfo->comp_info[2].v_samp_factor = 1;
/* Set the quality. */
jpeg_set_quality(cinfo, quality, true);
/* Configure remaining parameters. */
cinfo->dct_method = JpegLibraryType::J_DCT_METHOD::JDCT_ISLOW;
cinfo->optimize_coding = false;
cinfo->write_JFIF_header = true;
}
}
Result SoftwareJpegShrinker::ShrinkRgba8(SoftwareJpegShrinkerOutput &output, const SoftwareJpegShrinkerInput &input, int quality, void *work, size_t work_size) {
CAPSRV_ABORT_UNLESS(util::IsAligned(input.width, ImageSizeHorizonalUnit));
CAPSRV_ABORT_UNLESS(util::IsAligned(input.height, ImageSizeVerticalUnitDecode));
const u32 shrunk_width = input.width / 2;
const u32 shrunk_height = input.height / 2;
CAPSRV_ABORT_UNLESS(util::IsAligned(input.width, ImageSizeHorizonalUnit));
CAPSRV_ABORT_UNLESS(output.dst != nullptr);
CAPSRV_ABORT_UNLESS(output.out_width != nullptr);
CAPSRV_ABORT_UNLESS(output.out_height != nullptr);
/* Determine work buffer extents. */
char *work_start = static_cast<char *>(work);
char *work_end = work_start + work_size;
/* Determine the buffer extents for our linebuffers. */
u8 *rgb_buffer = static_cast<u8 *>(static_cast<void *>(work_start));
size_t rgb_buffer_size;
size_t rgb_buffer_stride;
R_TRY(GetRgbBufferSize(std::addressof(rgb_buffer_size), std::addressof(rgb_buffer_stride), input.width, work_size));
/* The start of the workbuffer is reserved for linebuffer space. */
work_start += rgb_buffer_size;
/* Create our compression structures. */
JpegLibraryType::jpeg_decompress_struct dcinfo = {};
JpegLibraryType::jpeg_compress_struct ecinfo = {};
/* Here nintendo creates a work buffer structure containing work_start + work_size. */
/* This seems to be a custom patch for/to libjpeg-turbo. */
/* It would be desirable for us to mimic this, because it gives Nintendo strong */
/* fixed memory usage guarantees. */
/* TODO: Determine if it is feasible for us to recreate this ourselves, */
/* Either by adding support to the devkitPro libjpeg-turbo portlib or otherwise. */
AMS_UNUSED(work_end);
/* Create our error managers. */
JpegErrorHandler jerr_dc = { .result = ResultSuccess(), };
JpegErrorHandler jerr_ec = { .result = ResultSuccess(), };
jerr_dc.error_exit = JpegErrorHandler::HandleError,
jerr_ec.error_exit = JpegErrorHandler::HandleError,
/* Link our error managers to our compression structures. */
dcinfo.err = jpeg_std_error(std::addressof(jerr_dc));
ecinfo.err = jpeg_std_error(std::addressof(jerr_ec));
/* Use setjmp, so that on error our handler will longjmp to return an error result. */
if (setjmp(jerr_ec.jmp_buf) == 0) {
if (setjmp(jerr_dc.jmp_buf) == 0) {
/* Create our decompressor. */
jpeg_create_decompress(std::addressof(dcinfo));
ON_SCOPE_EXIT { jpeg_destroy_decompress(std::addressof(dcinfo)); };
/* Setup our memory reader, ensure the header is correct. */
jpeg_mem_src(std::addressof(dcinfo), const_cast<unsigned char *>(static_cast<const unsigned char *>(input.jpeg)), input.jpeg_size);
R_UNLESS(jpeg_read_header(std::addressof(dcinfo), true) == JPEG_HEADER_OK, capsrv::ResultAlbumInvalidFileData());
/* Ensure width and height are correct. */
R_UNLESS(dcinfo.image_width == input.width, capsrv::ResultAlbumInvalidFileData());
R_UNLESS(dcinfo.image_height == input.height, capsrv::ResultAlbumInvalidFileData());
/* Set output parameters. */
dcinfo.out_color_space = JpegLibraryType::J_COLOR_SPACE::JCS_RGB;
dcinfo.dct_method = JpegLibraryType::J_DCT_METHOD::JDCT_ISLOW;
dcinfo.do_fancy_upsampling = input.fancy_upsampling;
dcinfo.do_block_smoothing = input.block_smoothing;
dcinfo.scale_num = 1;
dcinfo.scale_denom = 2;
/* Start decompression. */
R_UNLESS(jpeg_start_decompress(std::addressof(dcinfo)) == TRUE, capsrv::ResultAlbumInvalidFileData());
/* Check the parameters. */
CAPSRV_ASSERT(dcinfo.output_width == shrunk_width);
CAPSRV_ASSERT(dcinfo.output_height == shrunk_height);
CAPSRV_ASSERT(dcinfo.out_color_components == RgbColorComponentCount);
CAPSRV_ASSERT(dcinfo.output_components == RgbColorComponentCount);
/* Create our compressor. */
jpeg_create_compress(std::addressof(ecinfo));
ON_SCOPE_EXIT { jpeg_destroy_compress(std::addressof(ecinfo)); };
/* Setup our memory writer. */
unsigned long out_size = static_cast<unsigned long>(output.dst_size);
jpeg_mem_dest(std::addressof(ecinfo), reinterpret_cast<unsigned char **>(std::addressof(output.dst)), std::addressof(out_size));
/* Setup the encoding parameters. */
SetupEncodingParameter(std::addressof(ecinfo), input, quality);
/* Start compression. */
jpeg_start_compress(std::addressof(ecinfo), true);
/* Parse the scanlines. */
{
/* Create our linebuffer structure. */
JpegLibraryType::JSAMPROW linebuffers[ImageSizeVerticalUnitEncode] = {};
for (int i = 0; i < ImageSizeVerticalUnitEncode; i++) {
linebuffers[i] = rgb_buffer + rgb_buffer_stride * i;
}
/* While we still have scanlines, parse! */
while (dcinfo.output_scanline < shrunk_height) {
/* Determine remaining scanlines. */
const auto remaining_scanlines = shrunk_height - dcinfo.output_scanline;
const auto cur_max_scanlines = std::min<s32>(remaining_scanlines, ImageSizeVerticalUnitEncode);
/* If we have scanlines to decode, try to do so. */
auto writable_scanlines = 0;
while (writable_scanlines < cur_max_scanlines) {
const auto decoded = jpeg_read_scanlines(std::addressof(dcinfo), linebuffers + writable_scanlines, ImageSizeVerticalUnitDecode);
CAPSRV_ASSERT(decoded <= ImageSizeVerticalUnitDecode / 2);
writable_scanlines += decoded;
}
/* If we have scanlines to write, try to do so. */
jpeg_write_scanlines(std::addressof(ecinfo), linebuffers, writable_scanlines);
}
}
/* Finish the decompression. */
R_UNLESS(jpeg_finish_decompress(std::addressof(dcinfo)) == TRUE, capsrv::ResultAlbumInvalidFileData());
/* Finish the compression. */
jpeg_finish_compress(std::addressof(ecinfo));
/* Set the output size. */
*output.out_size = out_size;
} else {
/* Some unknown error was caught by our handler. */
R_THROW(capsrv::ResultAlbumInvalidFileData());
}
} else {
/* Return the encoding result. */
R_THROW(jerr_ec.result);
}
/* Write the size we decoded to output. */
*output.out_width = static_cast<s32>(dcinfo.output_width);
*output.out_width = static_cast<s32>(dcinfo.output_height);
R_SUCCEED();
}
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 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::capsrv::server::jpeg {
struct SoftwareJpegShrinkerInput {
const void *jpeg;
size_t jpeg_size;
u32 width;
u32 height;
bool fancy_upsampling;
bool block_smoothing;
};
struct SoftwareJpegShrinkerOutput {
u64 *out_size;
s32 *out_width;
s32 *out_height;
void *dst;
size_t dst_size;
};
class SoftwareJpegShrinker {
public:
static Result ShrinkRgba8(SoftwareJpegShrinkerOutput &output, const SoftwareJpegShrinkerInput &input, int quality, void *work, size_t work_size);
};
}

View file

@ -54,6 +54,7 @@ namespace ams::capsrv {
R_DEFINE_ERROR_RANGE(InternalError, 1024, 2047);
R_DEFINE_ERROR_RESULT(InternalJpegEncoderError, 1210);
R_DEFINE_ERROR_RESULT(InternalJpegOutBufferShortage, 1211);
R_DEFINE_ERROR_RESULT(InternalJpegWorkMemoryShortage, 1212);
R_DEFINE_ERROR_RANGE(InternalFileDataVerificationError, 1300, 1399);