From 8c71794d6d4545291a2c47b573ecbf427e2d0a3f Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Tue, 25 Jul 2023 19:20:29 +0200 Subject: [PATCH] PNG decoder + reduced number of directory entries + external libs header cleanup --- .gitmodules | 6 ++ Makefile | 20 +++-- src/libs/libspng | 1 + src/libs/miniz | 1 + src/libs/miniz_export.h | 3 + src/menu/menu.c | 6 +- src/menu/menu_state.h | 2 +- src/menu/{mp3player.c => mp3_player.c} | 8 +- src/menu/{mp3player.h => mp3_player.h} | 4 +- src/menu/png_decoder.c | 100 +++++++++++++++++++++++++ src/menu/png_decoder.h | 27 +++++++ src/menu/settings.c | 6 +- src/menu/views/music_player.c | 2 +- 13 files changed, 167 insertions(+), 19 deletions(-) create mode 160000 src/libs/libspng create mode 160000 src/libs/miniz create mode 100644 src/libs/miniz_export.h rename src/menu/{mp3player.c => mp3_player.c} (98%) rename src/menu/{mp3player.h => mp3_player.h} (95%) create mode 100644 src/menu/png_decoder.c create mode 100644 src/menu/png_decoder.h diff --git a/.gitmodules b/.gitmodules index 7515a8fd..5c78ba20 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,9 +2,15 @@ path = libdragon url = https://github.com/DragonMinded/libdragon branch = unstable +[submodule "src/libs/libspng"] + path = src/libs/libspng + url = https://github.com/randy408/libspng.git [submodule "src/libs/mini.c"] path = src/libs/mini.c url = https://github.com/univrsal/mini.c.git [submodule "src/libs/minimp3"] path = src/libs/minimp3 url = https://github.com/lieff/minimp3.git +[submodule "src/libs/miniz"] + path = src/libs/miniz + url = https://github.com/richgel999/miniz.git diff --git a/Makefile b/Makefile index b7f7cf4b..02d6b7e1 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ OUTPUT_DIR = output include $(N64_INST)/include/n64.mk -N64_CFLAGS += -iquote $(SOURCE_DIR) $(FLAGS) +N64_CFLAGS += -iquote $(SOURCE_DIR) -I $(SOURCE_DIR)/libs $(FLAGS) N64_LDFLAGS += --wrap asset_load SRCS = \ @@ -20,12 +20,18 @@ SRCS = \ flashcart/flashcart.c \ flashcart/sc64/sc64_internal.c \ flashcart/sc64/sc64.c \ + libs/libspng/spng/spng.c \ libs/mini.c/src/mini.c \ + libs/miniz/miniz_tdef.c \ + libs/miniz/miniz_tinfl.c \ + libs/miniz/miniz_zip.c \ + libs/miniz/miniz.c \ menu/actions.c \ menu/assets.c \ menu/menu.c \ - menu/mp3player.c \ + menu/mp3_player.c \ menu/path.c \ + menu/png_decoder.c \ menu/rom_database.c \ menu/settings.c \ menu/views/browser.c \ @@ -44,7 +50,13 @@ SRCS = \ ASSETS = \ FiraMono-Bold.ttf -$(BUILD_DIR)/FiraMono-Bold.o: MKFONT_FLAGS+=-c 0 --size 16 -r 20-7F -r 2000-206F -r 2190-21FF +OBJS = $(addprefix $(BUILD_DIR)/, $(addsuffix .o,$(basename $(SRCS) $(ASSETS)))) +MINIZ_OBJS = $(filter $(BUILD_DIR)/libs/miniz/%.o,$(OBJS)) +SPNG_OBJS = $(filter $(BUILD_DIR)/libs/libspng/%.o,$(OBJS)) + +$(MINIZ_OBJS): N64_CFLAGS+=-DMINIZ_NO_TIME -fcompare-debug-second +$(SPNG_OBJS): N64_CFLAGS+=-isystem $(SOURCE_DIR)/libs/miniz -DSPNG_USE_MINIZ -fcompare-debug-second +$(BUILD_DIR)/FiraMono-Bold.o: MKFONT_FLAGS+=-c 0 --size 16 -r 20-7F -r 2000-206F $(BUILD_DIR)/%.o: $(ASSETS_DIR)/%.ttf @echo " [FONT] $@" @@ -52,8 +64,6 @@ $(BUILD_DIR)/%.o: $(ASSETS_DIR)/%.ttf @$(N64_OBJCOPY) -I binary -O elf32-bigmips -B mips4300 $(basename $<).font64 $@ @rm $(basename $<).font64 -OBJS = $(addprefix $(BUILD_DIR)/, $(addsuffix .o,$(basename $(SRCS) $(ASSETS)))) - $(BUILD_DIR)/$(PROJECT_NAME).elf: $(OBJS) $(PROJECT_NAME).z64: N64_ROM_TITLE=$(PROJECT_NAME) diff --git a/src/libs/libspng b/src/libs/libspng new file mode 160000 index 00000000..e5c1fc47 --- /dev/null +++ b/src/libs/libspng @@ -0,0 +1 @@ +Subproject commit e5c1fc470fceaca08b8c30dc40768c28b82b9e12 diff --git a/src/libs/miniz b/src/libs/miniz new file mode 160000 index 00000000..9ae305f6 --- /dev/null +++ b/src/libs/miniz @@ -0,0 +1 @@ +Subproject commit 9ae305f6e109f8f1fbd2130458f1ee6197269b3b diff --git a/src/libs/miniz_export.h b/src/libs/miniz_export.h new file mode 100644 index 00000000..4c908d5b --- /dev/null +++ b/src/libs/miniz_export.h @@ -0,0 +1,3 @@ +#ifndef MINIZ_EXPORT +#define MINIZ_EXPORT +#endif diff --git a/src/menu/menu.c b/src/menu/menu.c index 72c9f2f6..b0146400 100644 --- a/src/menu/menu.c +++ b/src/menu/menu.c @@ -8,7 +8,7 @@ #include "flashcart/flashcart.h" #include "menu_state.h" #include "menu.h" -#include "mp3player.h" +#include "mp3_player.h" #include "settings.h" #include "utils/fs.h" #include "views/views.h" @@ -89,7 +89,7 @@ void menu_run (boot_params_t *boot_params) { int audio_buffer_length = audio_get_buffer_length(); - while (!boot_pending && (exception_reset_time() == 0)) { + while (!boot_pending && (exception_reset_time() < RESET_TIME_LENGTH)) { surface_t *display = display_try_get(); if (display != NULL) { @@ -188,7 +188,7 @@ void menu_run (boot_params_t *boot_params) { } } - if (audio_can_write()) { + while (audio_can_write()) { short *audio_buffer = audio_write_begin(); mixer_poll(audio_buffer, audio_buffer_length); audio_write_end(); diff --git a/src/menu/menu_state.h b/src/menu/menu_state.h index ac8baa31..c4761ec3 100644 --- a/src/menu/menu_state.h +++ b/src/menu/menu_state.h @@ -14,7 +14,7 @@ #include "settings.h" -#define BROWSER_LIST_SIZE 10000 +#define BROWSER_LIST_SIZE 2048 /** @brief Menu mode enumeration */ diff --git a/src/menu/mp3player.c b/src/menu/mp3_player.c similarity index 98% rename from src/menu/mp3player.c rename to src/menu/mp3_player.c index 5e6e8c7a..b50ecff7 100644 --- a/src/menu/mp3player.c +++ b/src/menu/mp3_player.c @@ -1,12 +1,12 @@ #include #include -#include "mp3player.h" +#include "mp3_player.h" #include "utils/utils.h" #define MINIMP3_IMPLEMENTATION -#include "libs/minimp3/minimp3.h" -#include "libs/minimp3/minimp3_ex.h" +#include +#include #define MIXER_CHANNEL (0) @@ -22,7 +22,7 @@ typedef struct { mp3dec_t dec; mp3dec_frame_info_t info; - uint8_t buffer[24 * 1024]; + uint8_t buffer[16 * 1024]; uint8_t *buffer_ptr; size_t buffer_left; diff --git a/src/menu/mp3player.h b/src/menu/mp3_player.h similarity index 95% rename from src/menu/mp3player.h rename to src/menu/mp3_player.h index add0dbfd..858050ff 100644 --- a/src/menu/mp3player.h +++ b/src/menu/mp3_player.h @@ -4,8 +4,8 @@ * @ingroup menu */ -#ifndef MP3PLAYER_H__ -#define MP3PLAYER_H__ +#ifndef MP3_PLAYER_H__ +#define MP3_PLAYER_H__ #include diff --git a/src/menu/png_decoder.c b/src/menu/png_decoder.c new file mode 100644 index 00000000..0a9e229a --- /dev/null +++ b/src/menu/png_decoder.c @@ -0,0 +1,100 @@ +#include + +#include "path.h" +#include "png_decoder.h" + + +png_err_t png_decode (char *path, surface_t *image, int max_width, int max_height) { + spng_ctx *ctx; + enum spng_errno err = SPNG_OK; + path_t *file_path; + FILE *file; + size_t image_size; + struct spng_ihdr ihdr; + uint8_t *row_buffer; + uint16_t *image_buffer; + + image->buffer = NULL; + + if ((ctx = spng_ctx_new(SPNG_CTX_IGNORE_ADLER32)) == NULL) { + return PNG_ERR_OUT_OF_MEM; + } + + if (spng_set_crc_action(ctx, SPNG_CRC_USE, SPNG_CRC_USE) != SPNG_OK) { + spng_ctx_free(ctx); + return PNG_ERR_INT; + } + + if (spng_set_image_limits(ctx, max_width, max_height) != SPNG_OK) { + spng_ctx_free(ctx); + return PNG_ERR_INT; + } + + file_path = path_init("sd:/"); + path_append(file_path, path); + if ((file = fopen(path_get(file_path), "r")) == NULL) { + spng_ctx_free(ctx); + path_free(file_path); + return PNG_ERR_NO_FILE; + } + path_free(file_path); + + if (spng_set_png_file(ctx, file) != SPNG_OK) { + spng_ctx_free(ctx); + fclose(file); + return PNG_ERR_INT; + } + + if (spng_decoded_image_size(ctx, SPNG_FMT_RGB8, &image_size) != SPNG_OK) { + spng_ctx_free(ctx); + fclose(file); + return PNG_ERR_BAD_FILE; + } + + if (spng_decode_image(ctx, NULL, image_size, SPNG_FMT_RGB8, SPNG_DECODE_PROGRESSIVE) != SPNG_OK) { + spng_ctx_free(ctx); + fclose(file); + return PNG_ERR_BAD_FILE; + } + + if (spng_get_ihdr(ctx, &ihdr) != SPNG_OK) { + spng_ctx_free(ctx); + fclose(file); + return PNG_ERR_BAD_FILE; + } + + *image = surface_alloc(FMT_RGBA16, ihdr.width, ihdr.height); + image_buffer = image->buffer; + if (image_buffer == NULL) { + spng_ctx_free(ctx); + fclose(file); + return PNG_ERR_OUT_OF_MEM; + } + + if ((row_buffer = malloc(ihdr.width * 3)) == NULL) { + spng_ctx_free(ctx); + fclose(file); + surface_free(image); + return PNG_ERR_OUT_OF_MEM; + } + + while ((err = spng_decode_row(ctx, row_buffer, ihdr.width * 3)) == SPNG_OK) { + for (int i = 0; i < ihdr.width * 3; i += 3) { + uint8_t r = row_buffer[i + 0] >> 3; + uint8_t g = row_buffer[i + 1] >> 3; + uint8_t b = row_buffer[i + 2] >> 3; + *image_buffer++ = (r << 11) | (g << 6) | (b << 1) | 1; + } + } + + spng_ctx_free(ctx); + free(row_buffer); + fclose(file); + + if ((err != SPNG_OK) && (err != SPNG_EOI)) { + surface_free(image); + return PNG_ERR_BAD_FILE; + } + + return PNG_OK; +} diff --git a/src/menu/png_decoder.h b/src/menu/png_decoder.h new file mode 100644 index 00000000..186a4d42 --- /dev/null +++ b/src/menu/png_decoder.h @@ -0,0 +1,27 @@ +/** + * @file png.h + * @brief PNG decoder + * @ingroup menu + */ + +#ifndef PNG_DECODER_H__ +#define PNG_DECODER_H__ + + +#include + + +/** @brief PNG decoder errors */ +typedef enum { + PNG_OK, + PNG_ERR_INT, + PNG_ERR_OUT_OF_MEM, + PNG_ERR_NO_FILE, + PNG_ERR_BAD_FILE, +} png_err_t; + + +png_err_t png_decode (char *path, surface_t *image, int max_width, int max_height); + + +#endif diff --git a/src/menu/settings.c b/src/menu/settings.c index 70729eb5..6a6ccbc4 100644 --- a/src/menu/settings.c +++ b/src/menu/settings.c @@ -1,6 +1,6 @@ #include +#include -#include "libs/mini.c/src/mini.h" #include "settings.h" #include "utils/fs.h" @@ -20,7 +20,7 @@ void settings_load (settings_t *settings) { settings_save(&init); } - mini_t *ini = mini_try_load("sd:/"SETTINGS_FILE_PATH); + mini_t *ini = mini_try_load("sd:/" SETTINGS_FILE_PATH); settings->pal60 = mini_get_bool(ini, "menu", "pal60", init.pal60); settings->show_hidden_files = mini_get_bool(ini, "menu", "show_hidden_files", init.show_hidden_files); @@ -30,7 +30,7 @@ void settings_load (settings_t *settings) { } void settings_save (settings_t *settings) { - mini_t *ini = mini_create("sd:/"SETTINGS_FILE_PATH); + mini_t *ini = mini_create("sd:/" SETTINGS_FILE_PATH); mini_set_bool(ini, "menu", "pal60", settings->pal60); mini_set_bool(ini, "menu", "show_hidden_files", settings->show_hidden_files); diff --git a/src/menu/views/music_player.c b/src/menu/views/music_player.c index 3bf8e785..096ddfc6 100644 --- a/src/menu/views/music_player.c +++ b/src/menu/views/music_player.c @@ -1,6 +1,6 @@ #include -#include "../mp3player.h" +#include "../mp3_player.h" #include "fragments/fragments.h" #include "views.h"