From eba9396473ab53090f7ba0ea81a637bb9377479e Mon Sep 17 00:00:00 2001 From: kub Date: Fri, 16 Apr 2021 21:31:30 +0200 Subject: [PATCH] mcd, add dr_mp3 playback support --- .gitmodules | 3 ++ Makefile | 3 +- platform/common/dr_libs | 1 + platform/common/mp3_drmp3.c | 65 +++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 160000 platform/common/dr_libs create mode 100644 platform/common/mp3_drmp3.c diff --git a/.gitmodules b/.gitmodules index 23cc3b36..309bcd18 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "platform/common/minimp3"] path = platform/common/minimp3 url = https://github.com/lieff/minimp3 +[submodule "platform/common/dr_libs"] + path = platform/common/dr_libs + url = https://github.com/mackron/dr_libs diff --git a/Makefile b/Makefile index cf1824d5..b0cc83ec 100644 --- a/Makefile +++ b/Makefile @@ -245,7 +245,8 @@ OBJS += platform/common/mp3_helix.o else ifeq "$(HAVE_LIBAVCODEC)" "1" OBJS += platform/common/mp3_libavcodec.o else -OBJS += platform/common/mp3_minimp3.o +#OBJS += platform/common/mp3_minimp3.o +OBJS += platform/common/mp3_drmp3.o endif endif diff --git a/platform/common/dr_libs b/platform/common/dr_libs new file mode 160000 index 00000000..343aa923 --- /dev/null +++ b/platform/common/dr_libs @@ -0,0 +1 @@ +Subproject commit 343aa923439e59e7a9f7726f70edc77a4500bdec diff --git a/platform/common/mp3_drmp3.c b/platform/common/mp3_drmp3.c new file mode 100644 index 00000000..54d3496d --- /dev/null +++ b/platform/common/mp3_drmp3.c @@ -0,0 +1,65 @@ +/* + * MP3 decoding using dr_mp3 + * (C) kub, 2020 + * + * This work is licensed under the terms of MAME license. + * See COPYING file in the top-level directory. + */ + +#include + +#include +#define DR_MP3_IMPLEMENTATION +#include "dr_libs/dr_mp3.h" +#include "mp3.h" + +static drmp3dec mp3dec; +static unsigned char mp3_input_buffer[2 * 1024]; + +int mp3dec_start(FILE *f, int fpos_start) +{ + drmp3dec_init(&mp3dec); + return 0; +} + +int mp3dec_decode(FILE *f, int *file_pos, int file_len) +{ + drmp3dec_frame_info info; + unsigned char *readPtr; + int bytesLeft; + int offset; // mp3 frame offset from readPtr + int len; + int retry = 3; + + do + { + if (*file_pos >= file_len) + return 1; /* EOF, nothing to do */ + + fseek(f, *file_pos, SEEK_SET); + bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), f); + + offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft); + if (offset < 0) { + lprintf("find_sync_word (%i/%i) err %i\n", + *file_pos, file_len, offset); + *file_pos = file_len; + return 1; // EOF + } + *file_pos += offset; + readPtr = mp3_input_buffer + offset; + bytesLeft -= offset; + + len = drmp3dec_decode_frame(&mp3dec, readPtr, bytesLeft, cdda_out_buffer, &info); + if (len > 0) // retrieved decoded data + *file_pos += info.frame_bytes; + else if (info.frame_bytes > 0) // no output but input consumed? + *file_pos += 1; // try to skip frame + else if (offset == 0) // bad frame? + *file_pos += 1; // try resyncing + // else // truncated frame, try more data + } + while (len <= 0 && --retry > 0); + + return len <= 0; +} -- 2.39.5