PNG decoder + reduced number of directory entries + external libs header cleanup

This commit is contained in:
Mateusz Faderewski 2023-07-25 19:20:29 +02:00
parent 7a348ea6ce
commit 8c71794d6d
13 changed files with 167 additions and 19 deletions

6
.gitmodules vendored
View File

@ -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

View File

@ -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)

1
src/libs/libspng Submodule

@ -0,0 +1 @@
Subproject commit e5c1fc470fceaca08b8c30dc40768c28b82b9e12

1
src/libs/miniz Submodule

@ -0,0 +1 @@
Subproject commit 9ae305f6e109f8f1fbd2130458f1ee6197269b3b

3
src/libs/miniz_export.h Normal file
View File

@ -0,0 +1,3 @@
#ifndef MINIZ_EXPORT
#define MINIZ_EXPORT
#endif

View File

@ -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();

View File

@ -14,7 +14,7 @@
#include "settings.h"
#define BROWSER_LIST_SIZE 10000
#define BROWSER_LIST_SIZE 2048
/** @brief Menu mode enumeration */

View File

@ -1,12 +1,12 @@
#include <fatfs/ff.h>
#include <libdragon.h>
#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 <minimp3/minimp3_ex.h>
#include <minimp3/minimp3.h>
#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;

View File

@ -4,8 +4,8 @@
* @ingroup menu
*/
#ifndef MP3PLAYER_H__
#define MP3PLAYER_H__
#ifndef MP3_PLAYER_H__
#define MP3_PLAYER_H__
#include <stdbool.h>

100
src/menu/png_decoder.c Normal file
View File

@ -0,0 +1,100 @@
#include <libspng/spng/spng.h>
#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;
}

27
src/menu/png_decoder.h Normal file
View File

@ -0,0 +1,27 @@
/**
* @file png.h
* @brief PNG decoder
* @ingroup menu
*/
#ifndef PNG_DECODER_H__
#define PNG_DECODER_H__
#include <surface.h>
/** @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

View File

@ -1,6 +1,6 @@
#include <libdragon.h>
#include <mini.c/src/mini.h>
#include "libs/mini.c/src/mini.h"
#include "settings.h"
#include "utils/fs.h"

View File

@ -1,6 +1,6 @@
#include <libdragon.h>
#include "../mp3player.h"
#include "../mp3_player.h"
#include "fragments/fragments.h"
#include "views.h"