diff options
17 files changed, 1359 insertions, 4 deletions
@@ -1,2 +1,4 @@ build/ tools/ +*.m4b +playback-trace.txt @@ -4,12 +4,15 @@ This repo is simply the [YP3 Rockbox Firmware](https://github.com/jaydens-tasty- - Bookmarks for .m4b audiobooks when the player is closed, powered off, and automatically every 1m - Chapter support for .m4b audiobooks +- Long books start without freezing the controls, with a "Loading book N%" screen while the index loads +- Pulling the SD card out while running no longer crashes the player: it pauses and turns itself off; put the card back, turn it on, and the book resumes **Controls** - Tap Down+Left/Right to jump audiobooks by chapter - Hold Center then select Chapters to pick a chapter - Tap Left/Right skips 30 seconds +- Tap Up/Down changes the volume one step; hold to keep changing it ## Install @@ -30,7 +33,7 @@ sudo udevadm control --reload 3. Flash: ```sh -tools/rockslop-cli flash release/rockbox-yp3-audiobook-1.0.tar.xz +tools/rockslop-cli flash release/rockbox-yp3-audiobook-1.1.tar.xz ``` When it says `waiting for the YP3`: unplug the player, power it off (hold Center), @@ -45,9 +48,9 @@ To put the player back to stock firmware, hold *Down* and connect USB to put it Just keep the cable connected! This can get stuck at 0% even when files are moving over. The connection can be very slow. It genuinely can take 10-15 minutes with no feedback. -**ATA error: -5** +**"No SD card." and the player turns off** -There is no SD card in the player. Just flash the firmware again. +There is no SD card in the player, or it can't be read. Put the card in and turn the player on. **CLI error about not being able to mount /dev/sdb or similar** @@ -62,6 +65,9 @@ The YP3 is in download mode. There was either a flashing issue or a crash that p `scripts/build.sh` rebuilds the package from the upstream source in `vendor/` plus the changes in `patches/`. Output goes to `build/out/`. +Host tests for the M4B code: `tests/chapters/run.sh` (chapter reader, needs ffmpeg) +and `tests/m4a/run.sh BOOK.m4b` (index loader, against a book of your own). + ## License GPL v2 or later. See `LICENSE`. diff --git a/patches/0004-yp3-hold-Up-in-Now-Playing-raises-the-volume.patch b/patches/0004-yp3-hold-Up-in-Now-Playing-raises-the-volume.patch new file mode 100644 index 0000000..037c494 --- /dev/null +++ b/patches/0004-yp3-hold-Up-in-Now-Playing-raises-the-volume.patch @@ -0,0 +1,55 @@ +From 269e5050242003eff7e5c4d88402ff96e819a468 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Thu, 26 Nov 2026 13:52:50 -0500 +Subject: [PATCH 4/5] yp3: hold Up in Now Playing raises the volume + +Tapping Up/Down steps the volume, holding either keeps stepping it. Hold +Up used to stop playback (and bookmark). + +--- + apps/keymaps/keymap-yp3.c | 15 ++++++++------- + 1 file changed, 8 insertions(+), 7 deletions(-) + +diff --git a/apps/keymaps/keymap-yp3.c b/apps/keymaps/keymap-yp3.c +index 444bd47c..d69ffd01 100644 +--- a/apps/keymaps/keymap-yp3.c ++++ b/apps/keymaps/keymap-yp3.c +@@ -2,8 +2,9 @@ + /* YP3 keys: Up ("M"), Down, Left, Right, centre (Select). On every screen: + * Left back / Right in, Up/Down move (volume on playing screens), Select the + * main action, hold Select the screen's menu, hold Up a second action where +- * Up does not scroll, Up+Left home (main menu, or cancel on prompts), hold +- * Left back where Left already acts. */ ++ * Up does not scroll (except Now Playing, where it keeps raising the ++ * volume), Up+Left home (main menu, or cancel on prompts), hold Left back ++ * where Left already acts. */ + #include "config.h" + #include "action.h" + #include "button.h" +@@ -24,10 +25,10 @@ static const struct button_mapping lists[] = { + { ACTION_STD_MENU, BUTTON_UP|BUTTON_LEFT, BUTTON_NONE }, + LAST_ITEM_IN_LIST__NEXTLIST(CONTEXT_STD) + }; +-/* Now Playing: Up acts on release so that holding it can stop. Left/Right +- * tapped hop (Skip Length), held seek; Down+Left / Down+Right (the lower +- * corners) go to the previous / next chapter, so Down turns the volume +- * down on release, or the combo would lower it too. */ ++/* Now Playing: Up/Down tapped step the volume, held keep stepping it. Both ++ * act on release when tapped, or the Up+Left (menu) and Down+Left/Right ++ * (previous / next chapter) combos would change the volume too. Left/Right ++ * tapped hop (Skip Length), held seek. */ + static const struct button_mapping wps[] = { + { ACTION_WPS_PLAY, BUTTON_SELECT|BUTTON_REL, BUTTON_SELECT }, + { ACTION_WPS_CONTEXT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_SELECT }, +@@ -39,7 +40,7 @@ static const struct button_mapping wps[] = { + { ACTION_WPS_STOPSEEK, BUTTON_RIGHT|BUTTON_REL, + BUTTON_RIGHT|BUTTON_REPEAT }, + { ACTION_WPS_VOLUP, BUTTON_UP|BUTTON_REL, BUTTON_UP }, +- { ACTION_WPS_STOP, BUTTON_UP|BUTTON_REPEAT, BUTTON_UP }, ++ { ACTION_WPS_VOLUP, BUTTON_UP|BUTTON_REPEAT, BUTTON_NONE }, + #ifdef HAVE_AUDIOBOOK_CHAPTERS + { ACTION_WPS_VOLDOWN, BUTTON_DOWN|BUTTON_REL, BUTTON_DOWN }, + { ACTION_WPS_CHAPTERPREV, BUTTON_DOWN|BUTTON_LEFT, BUTTON_NONE }, +-- +2.56.1 + diff --git a/patches/0005-yp3-survive-pulling-the-SD-card-while-running.patch b/patches/0005-yp3-survive-pulling-the-SD-card-while-running.patch new file mode 100644 index 0000000..3d27689 --- /dev/null +++ b/patches/0005-yp3-survive-pulling-the-SD-card-while-running.patch @@ -0,0 +1,389 @@ +From 27005a02eebd02cf82f357917019f72db9045af7 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Thu, 26 Nov 2026 13:52:50 -0500 +Subject: [PATCH] yp3: survive pulling the SD card while running + +The driver gave up on the first failed transfer, so a card pulled and +put back stayed dead, and the next write (at power-off at the latest) +panicked in fat.c ("Could not write sector"). + +Now a failed transfer sets the card up again and retries if it is the +same card (CID). The power thread checks an idle card every half second. +A card that is gone pauses playback with a message, writes are dropped +instead of panicking, and the player powers off 4 s later. Turned on +again with the card in, it resumes the book from its last saved place. + +(Restarting with system_reboot() once the card was back left a player +dark and unresponsive; power_off() is the tested shutdown path.) + +--- + apps/misc.c | 10 ++ + firmware/common/fat.c | 9 ++ + firmware/export/config/yp3.h | 4 + + firmware/export/sd.h | 5 + + firmware/powermgmt.c | 7 + + firmware/target/arm/sl6801/yp3/sd-yp3.c | 165 ++++++++++++++++++++++-- + 6 files changed, 186 insertions(+), 14 deletions(-) + +diff --git a/apps/misc.c b/apps/misc.c +index a5762bb3..a487c050 100644 +--- a/apps/misc.c ++++ b/apps/misc.c +@@ -719,6 +719,16 @@ long default_event_handler_ex(long event, void (*callback)(void *), void *parame + unpause_action(true); + return SYS_CAR_ADAPTER_RESUME; + #endif ++#ifdef HAVE_SD_CARD_SWAP ++ case SYS_HOTSWAP_EXTRACTED: ++ /* The card was pulled (sd-yp3.c): hold the book where it is. ++ Nothing can be read or saved; the player powers off a few ++ seconds later. */ ++ if (audio_status() & AUDIO_STATUS_PLAY) ++ audio_pause(); ++ splash(3*HZ, "SD card removed. Turning off."); ++ return SYS_HOTSWAP_EXTRACTED; ++#endif + #ifdef HAVE_DEVLOG + case SYS_DEV_RUN: /* 0 for a UI ping: nothing ran */ + return dev_ui_run() ? SYS_DEV_RUN : 0; +diff --git a/firmware/common/fat.c b/firmware/common/fat.c +index 3953f46b..4523c327 100644 +--- a/firmware/common/fat.c ++++ b/firmware/common/fat.c +@@ -38,6 +38,9 @@ + #include "kernel.h" + /*#define LOGF_ENABLE*/ + #include "logf.h" ++#ifdef HAVE_SD_CARD_SWAP ++#include <errno.h> ++#endif + + #define BYTES2INT32(array, pos) \ + (((uint32_t)array[pos+0] << 0) | \ +@@ -394,6 +397,12 @@ void dc_writeback_callback(IF_MV(int volume,) sector_t sector, void *buf) + while (1) + { + int rc = storage_write_sectors(IF_MD(fat_bpb->drive,) sector, 1, buf); ++#ifdef HAVE_SD_CARD_SWAP ++ /* The card is out: nothing to write to, and the player powers off ++ before it is used again (sd-yp3.c) */ ++ if (rc == -ENODEV) ++ return; ++#endif + if (rc < 0) + { + panicf("%s() - Could not write sector %llu" +diff --git a/firmware/export/config/yp3.h b/firmware/export/config/yp3.h +index d3e5742d..ec0ccdab 100644 +--- a/firmware/export/config/yp3.h ++++ b/firmware/export/config/yp3.h +@@ -22,6 +22,10 @@ + #endif + /* Keep FAT sector aggregation within the YP3 adapter's 128-sector cap. */ + #define FAT_MAX_TRANSFER_SIZE 128 ++/* Pulling the card while running (sd-yp3.c): pushed straight back it ++ * carries on, otherwise playback pauses, writes are dropped instead of ++ * panicking, and the player powers off a few seconds later. */ ++#define HAVE_SD_CARD_SWAP + #endif + #define CONFIG_CPU SL6801 + #define CPU_FREQ 192000000 +diff --git a/firmware/export/sd.h b/firmware/export/sd.h +index 61cc513c..5cf229a2 100644 +--- a/firmware/export/sd.h ++++ b/firmware/export/sd.h +@@ -55,6 +55,11 @@ bool sd_present(IF_MD_NONVOID(int drive)); + + long sd_last_disk_activity(void); + ++#ifdef HAVE_SD_CARD_SWAP ++/* From the power thread: notice a pulled card, then power off */ ++void sd_card_poll(void); ++#endif ++ + #ifdef CONFIG_STORAGE_MULTI + int sd_num_drives(int first_drive); + #endif +diff --git a/firmware/powermgmt.c b/firmware/powermgmt.c +index 1441fa12..869de8bd 100644 +--- a/firmware/powermgmt.c ++++ b/firmware/powermgmt.c +@@ -32,6 +32,9 @@ + #include "audio.h" + #include "usb.h" + #include "powermgmt.h" ++#ifdef HAVE_SD_CARD_SWAP ++#include "sd.h" ++#endif + #include "backlight.h" + #include "lcd.h" + #include "rtc.h" +@@ -840,6 +843,10 @@ static void power_thread(void) + /* Perform target tasks */ + charging_algorithm_step(); + ++#ifdef HAVE_SD_CARD_SWAP ++ sd_card_poll(); ++#endif ++ + /* check if some idle or sleep timer wears off */ + handle_auto_poweroff(); + +diff --git a/firmware/target/arm/sl6801/yp3/sd-yp3.c b/firmware/target/arm/sl6801/yp3/sd-yp3.c +index 1ecc5844..5187ef76 100644 +--- a/firmware/target/arm/sl6801/yp3/sd-yp3.c ++++ b/firmware/target/arm/sl6801/yp3/sd-yp3.c +@@ -18,6 +18,9 @@ + #include "sd.h" + #include "sdmmc.h" + #include "storage.h" ++#ifdef HAVE_SD_CARD_SWAP ++#include "power.h" ++#endif + #include "gpio-yp3.h" + #include "mmio-sl6801.h" + +@@ -219,7 +222,10 @@ enum { + SD_E_R1 = -5, SD_E_FIFO = -6, SD_E_STATE = -7, SD_E_UNSUPPORTED = -8, + }; + +-enum sd_state { STATE_UNINIT, STATE_READY, STATE_FAILED }; ++/* REMOVED (HAVE_SD_CARD_SWAP): the card stopped answering and a fresh ++ * setup found no card, or a different one; sd_card_poll() powers the ++ * player off shortly after. */ ++enum sd_state { STATE_UNINIT, STATE_READY, STATE_FAILED, STATE_REMOVED }; + + static struct mutex sd_mutex; + static tCardInfo card_info; +@@ -228,6 +234,13 @@ static bool sd_busy; + static long last_activity; + static uint32_t card_sectors, card_rca; + static bool card_sector_addressed; ++#ifdef HAVE_SD_CARD_SWAP ++/* CID (manufacturer, product, serial) of the card set up at boot, and of ++ * the one the last cold_prepare() found */ ++static uint32_t card_cid[4], probe_cid[4]; ++/* When the card was found gone (STATE_REMOVED) */ ++static long removed_tick; ++#endif + + /* ------------------------------------------------------------------ */ + /* Bounded polling of the data-path controller state. */ +@@ -761,6 +774,11 @@ static int cold_prepare(void) + ccs = (response >> OCR_CCS_SHIFT) & 1u; + status = cold_command(2, 0, true, &response); + if (status) return status; ++#ifdef HAVE_SD_CARD_SWAP ++ /* R2, highest word at the highest response address, as the CSD */ ++ for (i = 0; i < 4u; i++) ++ probe_cid[i] = REG(SD_RESP0 + (3u - i) * 4u); ++#endif + status = cold_command(3, 0, true, &response); + if (status) return status; + rca = response >> RCA_SHIFT; /* R6: new RCA in bits 31:16 */ +@@ -830,6 +848,113 @@ static int bind_card(void) + return controller_idle() ? 0 : SD_E_STATE; + } + ++#ifdef HAVE_SD_CARD_SWAP ++/* ------------------------------------------------------------------ */ ++/* Card pulled while running. The card holds everything but the core and ++ * codecs, and Rockbox cannot unmount its system volume, so: a card that ++ * is pulled and pushed straight back during a transfer carries on (same ++ * CID, set up again); otherwise I/O stops (writes are dropped, fat.c), ++ * playback pauses with a message, and the player powers off a few seconds ++ * later, so nothing runs on with stale filesystem state. Turned on again ++ * with the card in, it resumes the book from its last saved place. ++ * Upstream gave up on the first failed transfer, and the next write ++ * (at power-off at the latest) panicked in fat.c. ++ * ++ * A first version restarted the player with system_reboot() (PMU software ++ * reset) once a card was back; on a player that left it dark and ++ * unresponsive, needing a reflash (2026-11-26). power_off() is the path ++ * every normal shutdown takes. */ ++ ++/* Idle time before the poll checks the card is still there */ ++#define CARD_POLL_IDLE_TICKS HZ ++/* Card gone to power-off: long enough to read the message (misc.c) */ ++#define CARD_REMOVED_OFF_TICKS (4*HZ) ++ ++/* Set up the card from scratch (mutex held). True if the card we booted ++ * with is ready again. */ ++static bool card_restart(void) ++{ ++ int rc = cold_prepare(); ++ if (rc == 0) ++ rc = bind_card(); ++#ifdef YP3_DEV ++ dev_log("sd restart: rc %d", rc); ++#endif ++ return rc == 0 && !memcmp(probe_cid, card_cid, sizeof(card_cid)); ++} ++ ++/* Mutex held, sd_state READY: the card stopped answering. True if it is ++ * back (retry the transfer); else it is out (or another card is in) and ++ * everyone is told. */ ++static bool card_recover(void) ++{ ++ if (card_restart()) ++ return true; ++ removed_tick = current_tick; ++ sd_state = STATE_REMOVED; ++ card_info.initialized = 0; ++ queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0); ++ return false; ++} ++ ++/* Is the card still selected and answering? (mutex held) */ ++static bool card_answers(void) ++{ ++ struct op op = { 0u, POLL_LIMIT }; ++ uint32_t response = 0u; ++ if (!controller_idle()) ++ return false; ++ return send_command(&op, CMD13_PACKED, card_rca << RCA_SHIFT, 13u, true, ++ &response) == 0; ++} ++ ++/* Power thread, every POWER_THREAD_STEP_TICKS: notice a pulled card while ++ * nothing reads or writes, and power off a few seconds after. */ ++void sd_card_poll(void) ++{ ++ if (sd_state == STATE_READY) { ++ if (sd_busy || ++ !TIME_AFTER(current_tick, last_activity + CARD_POLL_IDLE_TICKS)) ++ return; ++ mutex_lock(&sd_mutex); ++ if (sd_state == STATE_READY && !card_answers()) ++ card_recover(); ++ mutex_unlock(&sd_mutex); ++ } else if (sd_state == STATE_REMOVED && ++ TIME_AFTER(current_tick, removed_tick + CARD_REMOVED_OFF_TICKS)) { ++ /* Straight to the hardware off: the normal shutdown would save ++ * settings and bookmarks to a card that is not there. */ ++ power_off(); ++ } ++} ++#endif /* HAVE_SD_CARD_SWAP */ ++ ++/* A transfer failed (mutex held): true to retry it once, the card having ++ * been set up again. */ ++static bool transfer_failed(bool retried) ++{ ++#ifdef HAVE_SD_CARD_SWAP ++ if (!retried) ++ return card_recover(); ++#else ++ (void)retried; ++#endif ++ return false; ++} ++ ++/* After the transfers (mutex held): a failure that left the card in place ++ * leaves the controller in an unknown state. */ ++static int transfer_result(int rc) ++{ ++ if (rc == 0) ++ return 0; ++ if (sd_state == STATE_READY) { ++ sd_state = STATE_FAILED; ++ card_info.initialized = 0; ++ } ++ return sd_state == STATE_FAILED ? -EIO : -ENODEV; ++} ++ + /* ------------------------------------------------------------------ */ + /* Rockbox storage API. */ + +@@ -850,6 +975,9 @@ int sd_init(void) + sd_state = STATE_FAILED; + return -EIO; + } ++#ifdef HAVE_SD_CARD_SWAP ++ memcpy(card_cid, probe_cid, sizeof(card_cid)); ++#endif + card_info.initialized = 1; + card_info.blocksize = SD_BLOCK_SIZE; + card_info.current_bank = 0; +@@ -885,7 +1013,8 @@ int sd_read_sectors(IF_MD(int drive,) sector_t start, int count, void *buffer) + #ifdef YP3_DEV + uint32_t t0 = sl6801_us(); + #endif +- for (int i = 0; i < count && rc == 0; ) { ++ bool retried = false; ++ for (int i = 0; i < count; ) { + unsigned n; + if ((uintptr_t)out & 3u) { + n = MIN((unsigned)(count - i), BOUNCE_BLOCKS); +@@ -905,6 +1034,13 @@ int sd_read_sectors(IF_MD(int drive,) sector_t start, int count, void *buffer) + (unsigned long)REG(SD_DMA_CTRL), + (unsigned long)REG(SD_DMA_STATUS)); + #endif ++ if (rc) { ++ if (!transfer_failed(retried)) ++ break; ++ retried = true; ++ rc = 0; ++ continue; ++ } + i += n; + out += n * SD_BLOCK_SIZE; + } +@@ -916,13 +1052,9 @@ int sd_read_sectors(IF_MD(int drive,) sector_t start, int count, void *buffer) + if (rc == 0) + last_activity = current_tick; + sd_busy = false; +- if (rc) { +- /* A failed transfer leaves the controller in an unknown state. */ +- sd_state = STATE_FAILED; +- card_info.initialized = 0; +- } ++ rc = transfer_result(rc); + mutex_unlock(&sd_mutex); +- return rc ? -EIO : 0; ++ return rc; + } + + int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, +@@ -946,7 +1078,8 @@ int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, + #ifdef YP3_DEV + uint32_t t0 = sl6801_us(); + #endif +- for (int i = 0; i < count && rc == 0; ) { ++ bool retried = false; ++ for (int i = 0; i < count; ) { + unsigned n; + if ((uintptr_t)in & 3u) { + n = MIN((unsigned)(count - i), BOUNCE_BLOCKS); +@@ -963,6 +1096,13 @@ int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, + (unsigned long)REG(SD_EVENT), + (unsigned long)REG(SD_STATUS)); + #endif ++ if (rc) { ++ if (!transfer_failed(retried)) ++ break; ++ retried = true; ++ rc = 0; ++ continue; ++ } + i += n; + in += n * SD_BLOCK_SIZE; + } +@@ -973,12 +1113,9 @@ int sd_write_sectors(IF_MD(int drive,) sector_t start, int count, + if (rc == 0) + last_activity = current_tick; + sd_busy = false; +- if (rc) { +- sd_state = STATE_FAILED; +- card_info.initialized = 0; +- } ++ rc = transfer_result(rc); + mutex_unlock(&sd_mutex); +- return rc ? -EIO : 0; ++ return rc; + } + + bool sd_disk_is_active(void) +-- +2.56.1 + diff --git a/patches/0006-yp3-load-long-M4B-books-without-freezing-with-a-load.patch b/patches/0006-yp3-load-long-M4B-books-without-freezing-with-a-load.patch new file mode 100644 index 0000000..c769dd1 --- /dev/null +++ b/patches/0006-yp3-load-long-M4B-books-without-freezing-with-a-load.patch @@ -0,0 +1,322 @@ +From 37c3425cffda782c316a01a366b85e56ee8c5031 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Thu, 26 Nov 2026 19:54:31 +0000 +Subject: [PATCH 06/11] yp3: load long M4B books without freezing, with a + loading screen + +Starting a long M4B (a 4.5 h book: ~93k chunks) froze the player for a +while: Now Playing scrolled slowly, Center and the context menu did +nothing, and then playback started. The codec read the index tables +stco and stsc one 4-byte value per playback-buffer call, ~280k calls +that never yielded, and the UI thread, at the same priority and never +preempted, only ran when the codec waited for the card. + +libm4a now reads those tables a 1 KiB block at a time (~1.3k calls) and +yields after each block. It builds the same seek table (checked on the +host with tests/m4a). It also reports how far it has got through a new +codec API call, load_progress, and Now Playing shows "Loading book N%" +with a bar while a load takes more than half a second. + +--- + apps/codec_thread.c | 24 +++++++++ + apps/codec_thread.h | 1 + + apps/codecs.c | 1 + + apps/gui/wps.c | 21 +++++++- + lib/rbcodec/codecs/codecs.h | 7 ++- + lib/rbcodec/codecs/libm4a/demux.c | 85 +++++++++++++++++++++++++++---- + 6 files changed, 125 insertions(+), 14 deletions(-) + +diff --git a/apps/codec_thread.c b/apps/codec_thread.c +index 5c170362..a4f5b39c 100644 +--- a/apps/codec_thread.c ++++ b/apps/codec_thread.c +@@ -586,6 +586,28 @@ static void load_codec(const struct codec_load_info *ev_data) + } + + /* Handle Q_CODEC_RUN */ ++/* Loading screen (wps.c): how far the codec has got reading a slow ++ * track's index, -1 when it is not doing that */ ++static volatile int load_percent = -1; ++static long load_start_tick; ++ ++static void codec_load_progress_callback(int percent) ++{ ++ if (load_percent < 0) ++ load_start_tick = current_tick; ++ load_percent = percent >= 100 ? -1 : percent; ++} ++ ++/* 0-99 while a track start is reading its index and has taken longer than ++ * half a second (no flash for a short one), else -1 */ ++int codec_load_progress(void) ++{ ++ int percent = load_percent; ++ if (percent < 0 || TIME_BEFORE(current_tick, load_start_tick + HZ/2)) ++ return -1; ++ return percent; ++} ++ + static void run_codec(void) + { + bool const encoder = type_is_encoder(codec_type); +@@ -621,6 +643,7 @@ static void run_codec(void) + } + + status = codec_run_proc(); ++ load_percent = -1; /* stopped, or failed, while loading */ + #ifdef HAVE_DEVLOG + if (!encoder) + dev_hash_log(); +@@ -749,6 +772,7 @@ void INIT_ATTR codec_thread_init(void) + ci.get_command = codec_get_command_callback; + ci.loop_track = codec_loop_track_callback; + ci.strip_filesize = codec_strip_filesize_callback; ++ ci.load_progress = codec_load_progress_callback; + + /* Init threading */ + queue_init(&codec_queue, false); +diff --git a/apps/codec_thread.h b/apps/codec_thread.h +index 9916d4e7..36749228 100644 +--- a/apps/codec_thread.h ++++ b/apps/codec_thread.h +@@ -50,6 +50,7 @@ size_t codec_finish_stream(void); + #endif + void codec_unload(void); + int codec_loaded(void); ++int codec_load_progress(void); + + /* */ + +diff --git a/apps/codecs.c b/apps/codecs.c +index 8375eba9..5601f9f6 100644 +--- a/apps/codecs.c ++++ b/apps/codecs.c +@@ -163,6 +163,7 @@ struct codec_api ci = { + #ifdef HAVE_CODEC_EXTRA_BUFFER + NULL, /* codec_get_extra_buffer */ + #endif ++ NULL, /* load_progress: codec_thread_init() sets it */ + + }; + +diff --git a/apps/gui/wps.c b/apps/gui/wps.c +index 714a44a4..ebbca5fe 100644 +--- a/apps/gui/wps.c ++++ b/apps/gui/wps.c +@@ -58,6 +58,7 @@ + #include "appevents.h" + #include "viewport.h" + #include "pcmbuf.h" ++#include "codec_thread.h" + #include "option_select.h" + #include "playlist_viewer.h" + #include "wps.h" +@@ -730,6 +731,7 @@ long gui_wps_show(void) + bool update = false; + bool theme_enabled = true; + long last_left = 0, last_right = 0; ++ bool loading = false; + struct wps_state *state = get_wps_state(); + + ab_reset_markers(); +@@ -737,6 +739,7 @@ long gui_wps_show(void) + wps_state_init(); + while ( 1 ) + { ++ int load; + bool hotkey = false; + bool audio_paused = (audio_status() & AUDIO_STATUS_PAUSE)?true:false; + /* did someone else (i.e power thread) change audio pause mode? */ +@@ -767,8 +770,23 @@ long gui_wps_show(void) + gwps_enter_wps(theme_enabled); + theme_enabled = true; + } ++ else if ((load = codec_load_progress()) >= 0) ++ { ++ /* A long book's index is being read: say so instead of a ++ Now Playing that seems stuck (codec_thread.c) */ ++ if (!loading) ++ splash_progress_set_delay(0); ++ loading = true; ++ splash_progress(load, 100, "Loading book %d%%", load); ++ } + else + { ++ if (loading) ++ { ++ loading = false; ++ skin_request_full_update(WPS); ++ update = true; ++ } + gwps_caption_backlight(state); + + FOR_NB_SCREENS(i) +@@ -798,7 +816,8 @@ long gui_wps_show(void) + if (button && !IS_SYSEVENT(button) ) + storage_spin(); + +- button = skin_wait_for_action(WPS, CONTEXT_WPS|ALLOW_SOFTLOCK, HZ/5); ++ button = skin_wait_for_action(WPS, CONTEXT_WPS|ALLOW_SOFTLOCK, ++ loading ? HZ/10 : HZ/5); + + /* Exit if audio has stopped playing. This happens e.g. at end of + playlist or if using the sleep timer. */ +diff --git a/lib/rbcodec/codecs/codecs.h b/lib/rbcodec/codecs/codecs.h +index acdebecc..28f5a0f8 100644 +--- a/lib/rbcodec/codecs/codecs.h ++++ b/lib/rbcodec/codecs/codecs.h +@@ -89,9 +89,9 @@ + * any new functions "waiting" at the end of the list. + */ + #ifdef HAVE_CODEC_EXTRA_BUFFER +-#define CODEC_API_VERSION 51 /* codec_get_extra_buffer */ ++#define CODEC_API_VERSION 52 /* load_progress */ + #else +-#define CODEC_API_VERSION 50 ++#define CODEC_API_VERSION 51 /* load_progress */ + #endif + + /* reasons for calling codec main entrypoint */ +@@ -242,6 +242,9 @@ struct codec_api { + heap besides codec_get_buffer()'s; NULL when there is none */ + void *(*codec_get_extra_buffer)(size_t *size); + #endif ++ /* How far a slow track start (a long M4B's index) has got, 0-100; the ++ loading screen shows it (codec_load_progress()) */ ++ void (*load_progress)(int percent); + }; + + /* codec header */ +diff --git a/lib/rbcodec/codecs/libm4a/demux.c b/lib/rbcodec/codecs/libm4a/demux.c +index 869f2353..c4cfd92a 100644 +--- a/lib/rbcodec/codecs/libm4a/demux.c ++++ b/lib/rbcodec/codecs/libm4a/demux.c +@@ -442,11 +442,69 @@ static bool read_chunk_stsc(qtmovie_t *qtmovie, size_t chunk_len) + return true; + } + +-static void stream_read_sample_to_chunk(stream_t *stream, uint32_t *first_chunk, uint32_t *num_samples) ++/* --- stco and stsc read a block at a time --- ++ * A long audiobook has ~100k chunks. Read one 4-byte value per buffer call, ++ * the two tables took ~280k calls (a 4.5 h M4B), seconds of CPU that never ++ * yielded: the UI, at the codec's priority and never preempted, froze until ++ * the book had loaded. Now one call per block, a yield after each, and the ++ * progress reported (ci->load_progress) for the loading screen. */ ++#define TABLE_BLOCK_BYTES 1024 ++ ++static struct table_reader + { +- (*first_chunk) = stream_read_uint32(stream); +- (*num_samples) = stream_read_uint32(stream); +- stream_skip(stream, 4); ++ stream_t *stream; ++ uint32_t bytes_left; /* of the table, not yet in buf */ ++ unsigned len, i; /* bytes in buf, next to use */ ++ uint32_t done, total; /* table bytes, both tables: progress */ ++ int percent; ++ unsigned char buf[TABLE_BLOCK_BYTES]; ++} rd; ++ ++static void table_open(stream_t *stream, uint32_t bytes) ++{ ++ rd.stream = stream; ++ rd.bytes_left = bytes; ++ rd.len = rd.i = 0; ++} ++ ++static void table_progress(int percent) ++{ ++ struct codec_api *api = rd.stream->ci; ++ if (percent != rd.percent && api->load_progress) ++ api->load_progress(percent); ++ rd.percent = percent; ++} ++ ++static uint32_t table_u32(void) ++{ ++ if (rd.i + 4 > rd.len) ++ { ++ /* tables are whole words, so a block holds whole values */ ++ unsigned n = MIN(rd.bytes_left, TABLE_BLOCK_BYTES); ++ if (n < 4) ++ return 0; ++ unsigned got = rd.stream->ci->read_filebuf(rd.buf, n); ++ if (got < n) ++ memset(rd.buf + got, 0, n - got); ++ rd.bytes_left -= n; ++ rd.len = n; ++ rd.i = 0; ++ rd.done += n; ++ if (rd.total) ++ table_progress((uint64_t)rd.done * 100 / rd.total); ++ rd.stream->ci->yield(); ++ } ++ const unsigned char *b = rd.buf + rd.i; ++ rd.i += 4; ++ return (uint32_t)b[0] << 24 | (uint32_t)b[1] << 16 | ++ (uint32_t)b[2] << 8 | b[3]; ++} ++ ++static void stream_read_sample_to_chunk(uint32_t *first_chunk, uint32_t *num_samples) ++{ ++ (*first_chunk) = table_u32(); ++ (*num_samples) = table_u32(); ++ table_u32(); /* sample description index */ + } + + static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len) +@@ -513,16 +571,19 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len) + * accepted to be able to avoid allocation of the large sample_byte_size[] + * table. This reduces the memory consumption by a factor of 2 or even + * more. */ ++ /* Progress over both passes: the stco entries, then stsc's */ ++ rd.done = 0; ++ rd.total = numentries * 4 + qtmovie->res->num_sample_to_chunks * 12; ++ rd.percent = -1; ++ table_open(qtmovie->stream, numentries * 4); ++ + uint32_t idx = 0; + for (i = 0; i < numentries; ++i) + { ++ uint32_t offset = table_u32(); + if (i % accuracy_divider == 0) + { +- qtmovie->res->lookup_table[idx++].offset = stream_read_uint32(qtmovie->stream); +- } +- else +- { +- stream_skip(qtmovie->stream, 4); ++ qtmovie->res->lookup_table[idx++].offset = offset; + } + size_remaining -= 4; + } +@@ -534,7 +595,8 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len) + + int32_t current_offset = stream_tell(qtmovie->stream); + stream_seek(qtmovie->stream, qtmovie->res->sample_to_chunk_offset); +- stream_read_sample_to_chunk(qtmovie->stream, &new_first, &new_frame); ++ table_open(qtmovie->stream, qtmovie->res->num_sample_to_chunks * 12); ++ stream_read_sample_to_chunk(&new_first, &new_frame); + for (k = 1; k <= numentries; ++k) + { + for (; i <= qtmovie->res->num_sample_to_chunks; ++i) +@@ -545,7 +607,7 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len) + old_first = new_first; + old_frame = new_frame; + if (i < qtmovie->res->num_sample_to_chunks) +- stream_read_sample_to_chunk(qtmovie->stream, &new_first, &new_frame); ++ stream_read_sample_to_chunk(&new_first, &new_frame); + old_i = i; + } + +@@ -566,6 +628,7 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len) + qtmovie->res->lookup_table[idx].sample = 0; + } + ++ table_progress(100); + stream_seek(qtmovie->stream, current_offset); + if (size_remaining) + { +-- +2.56.1 + diff --git a/patches/0007-yp3-power-off-at-boot-when-there-is-no-SD-card.patch b/patches/0007-yp3-power-off-at-boot-when-there-is-no-SD-card.patch new file mode 100644 index 0000000..086494c --- /dev/null +++ b/patches/0007-yp3-power-off-at-boot-when-there-is-no-SD-card.patch @@ -0,0 +1,48 @@ +From 33293a86a06f935e44f17f36b38f586e7b0e5b33 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Thu, 26 Nov 2026 20:23:27 +0000 +Subject: [PATCH 07/11] yp3: power off at boot when there is no SD card + +Turned on without a card, the player showed "ATA error: -5", then after +a key the debug screen and a panic ("ata: -5" and a backtrace). The +panic parks the CPU until the recovery watchdog resets it (~16 s), a +dark screen that looked like a crash into download mode. + +Now it says there is no SD card, waits 5 s and powers off; with the +card back in, the next power-on boots normally. + +--- + apps/main.c | 17 +++++++++++++++++ + 1 file changed, 17 insertions(+) + +diff --git a/apps/main.c b/apps/main.c +index 1dcc992d..ca189b07 100644 +--- a/apps/main.c ++++ b/apps/main.c +@@ -626,6 +626,23 @@ static void init(void) + CHART(">storage_init"); + rc = storage_init(); + CHART("<storage_init"); ++#ifdef HAVE_SD_CARD_SWAP ++ if (rc) ++ { ++ /* No card (or none that answers): say so and power off. Upstream ++ waited for a key, showed the debug screen and panicked, and the ++ panic parked the player dark until the recovery watchdog reset ++ it (~16 s), which looked like a crash into download mode. */ ++ lcd_clear_display(); ++ lcd_puts(0, 1, "No SD card."); ++ lcd_puts(0, 3, "Put the card in,"); ++ lcd_puts(0, 4, "then turn the"); ++ lcd_puts(0, 5, "player on."); ++ lcd_update(); ++ sleep(5*HZ); ++ power_off(); ++ } ++#endif + if(rc) + { + lcd_clear_display(); +-- +2.56.1 + diff --git a/patches/0008-yp3-no-rewind-on-pause-while-a-book-is-still-loading.patch b/patches/0008-yp3-no-rewind-on-pause-while-a-book-is-still-loading.patch new file mode 100644 index 0000000..e8e8e58 --- /dev/null +++ b/patches/0008-yp3-no-rewind-on-pause-while-a-book-is-still-loading.patch @@ -0,0 +1,70 @@ +From cab74ec7312b907abd750b7ea807b2a4371e6f74 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Fri, 27 Nov 2026 00:05:54 +0000 +Subject: [PATCH 08/11] yp3: no rewind-on-pause while a book is still loading + +Booting into a long M4B with Bluetooth headphones connecting sometimes +loaded the book twice. Playback was paused during the load, and the +pause's "rewind duration on pause" (2 s, the audiobook default) seeked +to elapsed - 2 s, clamped to 0. A seek halts the codec, and halted +mid-load it was started again, reading the whole index a second time. + +Rewind on pause now only when there is something to rewind: the track +has played (elapsed > 0) and its index is not being read. + +--- + apps/codec_thread.c | 6 ++++++ + apps/codec_thread.h | 1 + + apps/gui/wps.c | 7 +++++-- + 3 files changed, 12 insertions(+), 2 deletions(-) + +diff --git a/apps/codec_thread.c b/apps/codec_thread.c +index a4f5b39c..fc6eeecf 100644 +--- a/apps/codec_thread.c ++++ b/apps/codec_thread.c +@@ -598,6 +598,12 @@ static void codec_load_progress_callback(int percent) + load_percent = percent >= 100 ? -1 : percent; + } + ++/* A track start is reading its index (load_progress), however briefly */ ++bool codec_loading(void) ++{ ++ return load_percent >= 0; ++} ++ + /* 0-99 while a track start is reading its index and has taken longer than + * half a second (no flash for a short one), else -1 */ + int codec_load_progress(void) +diff --git a/apps/codec_thread.h b/apps/codec_thread.h +index 36749228..6f0776d8 100644 +--- a/apps/codec_thread.h ++++ b/apps/codec_thread.h +@@ -51,6 +51,7 @@ size_t codec_finish_stream(void); + void codec_unload(void); + int codec_loaded(void); + int codec_load_progress(void); ++bool codec_loading(void); + + /* */ + +diff --git a/apps/gui/wps.c b/apps/gui/wps.c +index ebbca5fe..3fd19a0e 100644 +--- a/apps/gui/wps.c ++++ b/apps/gui/wps.c +@@ -140,8 +140,11 @@ void wps_do_action(enum wps_do_action_type action, bool updatewps) + { + audio_pause(); + +- if (global_settings.pause_rewind) { +- unsigned long elapsed = audio_current_track()->elapsed; ++ unsigned long elapsed = audio_current_track()->elapsed; ++ /* Nothing to rewind until the track plays: a seek while a long ++ book still loads restarts the codec, which reads the whole ++ index again (a pause during the load at boot did) */ ++ if (global_settings.pause_rewind && elapsed > 0 && !codec_loading()) { + long newpos = elapsed - (global_settings.pause_rewind * 1000); + + audio_pre_ff_rewind(); +-- +2.56.1 + diff --git a/patches/0009-yp3-no-Bookmark-saved-splash-for-automatic-bookmarks.patch b/patches/0009-yp3-no-Bookmark-saved-splash-for-automatic-bookmarks.patch new file mode 100644 index 0000000..6ef5ce4 --- /dev/null +++ b/patches/0009-yp3-no-Bookmark-saved-splash-for-automatic-bookmarks.patch @@ -0,0 +1,53 @@ +From 7da5965cf589e889e06c835b89c8ea029c5064b8 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Fri, 27 Nov 2026 00:17:17 +0000 +Subject: [PATCH 09/11] yp3: no "Bookmark saved" splash for automatic bookmarks + +Every automatic bookmark (power-off, stop, switching books) showed +"Bookmark saved" for a second, which held up the power-off. Those are +now saved quietly; a bookmark made from the menu still says so, and a +failure is still shown. + +--- + apps/bookmark.c | 11 ++++++++--- + 1 file changed, 8 insertions(+), 3 deletions(-) + +diff --git a/apps/bookmark.c b/apps/bookmark.c +index a6790c7f..ace8e203 100644 +--- a/apps/bookmark.c ++++ b/apps/bookmark.c +@@ -59,6 +59,7 @@ + #define BMARK_ASK_USER 0x1 + #define BMARK_CREATE_FILE 0x2 + #define BMARK_CHECK_IGNORE 0x4 ++#define BMARK_SHOW_SAVED 0x8 /* say "Bookmark saved": a manual bookmark */ + + /* Used to buffer bookmarks while displaying the bookmark list. */ + struct bookmark_list +@@ -681,8 +682,12 @@ static bool write_bookmark(unsigned int flags) + else + ret = false; + +- splash(HZ, ret ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS) +- : ID2P(LANG_BOOKMARK_CREATE_FAILURE)); ++ /* Automatic bookmarks (power-off, stop, another book) are saved ++ quietly: the splash held the power-off for a second each time. ++ A failure is still shown. */ ++ if (!ret || (flags & BMARK_SHOW_SAVED)) ++ splash(HZ, ret ? ID2P(LANG_BOOKMARK_CREATE_SUCCESS) ++ : ID2P(LANG_BOOKMARK_CREATE_FAILURE)); + + return ret; + } +@@ -1202,7 +1207,7 @@ bool bookmark_create_menu(void) + if (!bookmark_is_bookmarkable_state()) + save_playlist_screen(NULL); + +- return write_bookmark(BMARK_CREATE_FILE); ++ return write_bookmark(BMARK_CREATE_FILE | BMARK_SHOW_SAVED); + } + /* ----------------------------------------------------------------------- */ + /* This function acts as the load interface from the context menu. */ +-- +2.56.1 + diff --git a/patches/0010-yp3-ignore-the-power-on-key-until-it-is-released.patch b/patches/0010-yp3-ignore-the-power-on-key-until-it-is-released.patch new file mode 100644 index 0000000..7561192 --- /dev/null +++ b/patches/0010-yp3-ignore-the-power-on-key-until-it-is-released.patch @@ -0,0 +1,82 @@ +From a7863201fe90839c670bfe6373e7d88360518835 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Fri, 27 Nov 2026 00:30:35 +0000 +Subject: [PATCH 10/11] yp3: ignore the power-on key until it is released + +The player powers on with a long press of Center. Held until Rockbox +was up (~5 s), its release reached Now Playing as Center, play/pause, +and paused the book being resumed: the boots that "loaded the book +twice" were these (with rewind-on-pause seeking during the load), and +the book then stayed paused. + +A key held when the buttons start is now ignored until it is released. + +--- + apps/main.c | 15 +++++++++++++++ + firmware/export/config/yp3.h | 3 +++ + 2 files changed, 18 insertions(+) + +diff --git a/apps/main.c b/apps/main.c +index ca189b07..6034408f 100644 +--- a/apps/main.c ++++ b/apps/main.c +@@ -31,6 +31,7 @@ + #include "led.h" + #include "../kernel-internal.h" + #include "button.h" ++#include "action.h" + #include "core_keymap.h" + #include "tree.h" + #include "filetypes.h" +@@ -167,6 +168,9 @@ + // #define AUTOROCK VIEWERS_DATA_DIR"/imageviewer.rock" + // #define AUTOROCK_ARG "/jpegs/sample.jpg" + ++#ifdef IGNORE_POWER_ON_KEY ++static bool key_held_at_boot; ++#endif + static void init(void); + /* main(), and various functions called by main() and init() may be + * be INIT_ATTR. These functions must not be called after the final call +@@ -258,6 +262,12 @@ int main(void) + #endif + #ifdef HAVE_BATTLOG + battlog_start(); ++#endif ++#ifdef IGNORE_POWER_ON_KEY ++ /* Ignore the power-on key until it is released (its press and ++ release may already be queued; other events are kept) */ ++ if (key_held_at_boot) ++ action_wait_for_release(); + #endif + CHART(">root_menu"); + root_menu(); +@@ -592,6 +602,11 @@ static void init(void) + backlight_init(); + + button_init(); ++#ifdef IGNORE_POWER_ON_KEY ++ /* The key that powered the player on, if still held: Center on the ++ YP3, whose release (play/pause) paused the book while it loaded */ ++ key_held_at_boot = (button_read_device() & BUTTON_MAIN) != 0; ++#endif + + /* Don't initialize power management here if it could incorrectly + * measure battery voltage, and it's not needed for charging. */ +diff --git a/firmware/export/config/yp3.h b/firmware/export/config/yp3.h +index ec0ccdab..4624ad2d 100644 +--- a/firmware/export/config/yp3.h ++++ b/firmware/export/config/yp3.h +@@ -26,6 +26,9 @@ + * carries on, otherwise playback pauses, writes are dropped instead of + * panicking, and the player powers off a few seconds later. */ + #define HAVE_SD_CARD_SWAP ++/* The key held to power on (Center) is ignored until released: its ++ * release toggled play/pause while a resumed book loaded (apps/main.c) */ ++#define IGNORE_POWER_ON_KEY + #endif + #define CONFIG_CPU SL6801 + #define CPU_FREQ 192000000 +-- +2.56.1 + diff --git a/patches/0011-yp3-stop-a-long-book-s-load-at-once-for-USB.patch b/patches/0011-yp3-stop-a-long-book-s-load-at-once-for-USB.patch new file mode 100644 index 0000000..8fabc8f --- /dev/null +++ b/patches/0011-yp3-stop-a-long-book-s-load-at-once-for-USB.patch @@ -0,0 +1,151 @@ +From a79946f739128362227ab71864ada22a9908ea52 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Fri, 27 Nov 2026 00:31:28 +0000 +Subject: [PATCH 11/11] yp3: stop a long book's load at once for USB + +Plugging in USB while a long M4B loaded (also the flasher's reboot +into Rockbox, which resumes the book) left the computer without the +player's disk: USB storage needs playback stopped, and the stop waited +for the codec, which takes commands only once the index is read +(~6 s). The computer gave up on the unresponsive device and the +flasher reported "NO USB DISK". + +load_progress now returns whether to go on: false when a stop is +waiting in the codec queue, and libm4a then ends the load at once with +an error. Seeks and pauses still wait for the load, as before. + +--- + apps/codec_thread.c | 7 ++++++- + lib/rbcodec/codecs/codecs.h | 10 ++++++---- + lib/rbcodec/codecs/libm4a/demux.c | 19 +++++++++++++------ + 3 files changed, 25 insertions(+), 11 deletions(-) + +diff --git a/apps/codec_thread.c b/apps/codec_thread.c +index fc6eeecf..90c76192 100644 +--- a/apps/codec_thread.c ++++ b/apps/codec_thread.c +@@ -591,11 +591,16 @@ static void load_codec(const struct codec_load_info *ev_data) + static volatile int load_percent = -1; + static long load_start_tick; + +-static void codec_load_progress_callback(int percent) ++static bool codec_load_progress_callback(int percent) + { + if (load_percent < 0) + load_start_tick = current_tick; + load_percent = percent >= 100 ? -1 : percent; ++ ++ /* Stop waiting (audio_stop_playback(): USB, another track): end the ++ load rather than make it wait for the whole index */ ++ struct queue_event ev; ++ return !(queue_peek(&codec_queue, &ev) && ev.id == Q_CODEC_STOP); + } + + /* A track start is reading its index (load_progress), however briefly */ +diff --git a/lib/rbcodec/codecs/codecs.h b/lib/rbcodec/codecs/codecs.h +index 28f5a0f8..32568ad9 100644 +--- a/lib/rbcodec/codecs/codecs.h ++++ b/lib/rbcodec/codecs/codecs.h +@@ -89,9 +89,9 @@ + * any new functions "waiting" at the end of the list. + */ + #ifdef HAVE_CODEC_EXTRA_BUFFER +-#define CODEC_API_VERSION 52 /* load_progress */ ++#define CODEC_API_VERSION 53 /* load_progress returns bool */ + #else +-#define CODEC_API_VERSION 51 /* load_progress */ ++#define CODEC_API_VERSION 52 /* load_progress returns bool */ + #endif + + /* reasons for calling codec main entrypoint */ +@@ -243,8 +243,10 @@ struct codec_api { + void *(*codec_get_extra_buffer)(size_t *size); + #endif + /* How far a slow track start (a long M4B's index) has got, 0-100; the +- loading screen shows it (codec_load_progress()) */ +- void (*load_progress)(int percent); ++ loading screen shows it (codec_load_progress()). Called often (each ++ block read): false means the codec is to stop, so the load should ++ end now (return an error) */ ++ bool (*load_progress)(int percent); + }; + + /* codec header */ +diff --git a/lib/rbcodec/codecs/libm4a/demux.c b/lib/rbcodec/codecs/libm4a/demux.c +index c4cfd92a..b4c08234 100644 +--- a/lib/rbcodec/codecs/libm4a/demux.c ++++ b/lib/rbcodec/codecs/libm4a/demux.c +@@ -447,7 +447,10 @@ static bool read_chunk_stsc(qtmovie_t *qtmovie, size_t chunk_len) + * the two tables took ~280k calls (a 4.5 h M4B), seconds of CPU that never + * yielded: the UI, at the codec's priority and never preempted, froze until + * the book had loaded. Now one call per block, a yield after each, and the +- * progress reported (ci->load_progress) for the loading screen. */ ++ * progress reported (ci->load_progress) for the loading screen. A stop ++ * (USB, another book) ends the load at the next block: the codec cannot ++ * take commands until it returns, and USB waited ~6 s, long enough for ++ * the computer to give up on the player's disk. */ + #define TABLE_BLOCK_BYTES 1024 + + static struct table_reader +@@ -457,6 +460,7 @@ static struct table_reader + unsigned len, i; /* bytes in buf, next to use */ + uint32_t done, total; /* table bytes, both tables: progress */ + int percent; ++ bool stop; /* load_progress said stop: read no more */ + unsigned char buf[TABLE_BLOCK_BYTES]; + } rd; + +@@ -470,8 +474,8 @@ static void table_open(stream_t *stream, uint32_t bytes) + static void table_progress(int percent) + { + struct codec_api *api = rd.stream->ci; +- if (percent != rd.percent && api->load_progress) +- api->load_progress(percent); ++ if (api->load_progress && !api->load_progress(percent)) ++ rd.stop = true; + rd.percent = percent; + } + +@@ -481,7 +485,7 @@ static uint32_t table_u32(void) + { + /* tables are whole words, so a block holds whole values */ + unsigned n = MIN(rd.bytes_left, TABLE_BLOCK_BYTES); +- if (n < 4) ++ if (n < 4 || rd.stop) + return 0; + unsigned got = rd.stream->ci->read_filebuf(rd.buf, n); + if (got < n) +@@ -575,10 +579,11 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len) + rd.done = 0; + rd.total = numentries * 4 + qtmovie->res->num_sample_to_chunks * 12; + rd.percent = -1; ++ rd.stop = false; + table_open(qtmovie->stream, numentries * 4); + + uint32_t idx = 0; +- for (i = 0; i < numentries; ++i) ++ for (i = 0; i < numentries && !rd.stop; ++i) + { + uint32_t offset = table_u32(); + if (i % accuracy_divider == 0) +@@ -597,7 +602,7 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len) + stream_seek(qtmovie->stream, qtmovie->res->sample_to_chunk_offset); + table_open(qtmovie->stream, qtmovie->res->num_sample_to_chunks * 12); + stream_read_sample_to_chunk(&new_first, &new_frame); +- for (k = 1; k <= numentries; ++k) ++ for (k = 1; k <= numentries && !rd.stop; ++k) + { + for (; i <= qtmovie->res->num_sample_to_chunks; ++i) + { +@@ -628,6 +633,8 @@ static bool read_chunk_stco(qtmovie_t *qtmovie, size_t chunk_len) + qtmovie->res->lookup_table[idx].sample = 0; + } + ++ if (rd.stop) ++ return false; /* the codec is to stop: no use finishing */ + table_progress(100); + stream_seek(qtmovie->stream, current_offset); + if (size_remaining) +-- +2.56.1 + diff --git a/release/rockbox-yp3-audiobook-1.0.tar.xz.sha256 b/release/rockbox-yp3-audiobook-1.0.tar.xz.sha256 deleted file mode 100644 index ea41a00..0000000 --- a/release/rockbox-yp3-audiobook-1.0.tar.xz.sha256 +++ /dev/null @@ -1 +0,0 @@ -a6ff3e99d5d53a5ae95b331683e4b1e8d404f2c1b188b4fd21f43c4859ad1248 rockbox-yp3-audiobook-1.0.tar.xz diff --git a/release/rockbox-yp3-audiobook-1.0.tar.xz b/release/rockbox-yp3-audiobook-1.1.tar.xz Binary files differindex ef511ce..87ec5cc 100644 --- a/release/rockbox-yp3-audiobook-1.0.tar.xz +++ b/release/rockbox-yp3-audiobook-1.1.tar.xz diff --git a/release/rockbox-yp3-audiobook-1.1.tar.xz.sha256 b/release/rockbox-yp3-audiobook-1.1.tar.xz.sha256 new file mode 100644 index 0000000..4775672 --- /dev/null +++ b/release/rockbox-yp3-audiobook-1.1.tar.xz.sha256 @@ -0,0 +1 @@ +ae1cd0b472c61bc92feca6aa43a51a4726e4a067bd50311a2446ad20c0c37554 rockbox-yp3-audiobook-1.1.tar.xz diff --git a/tests/m4a/harness.c b/tests/m4a/harness.c new file mode 100644 index 0000000..b9c08bf --- /dev/null +++ b/tests/m4a/harness.c @@ -0,0 +1,123 @@ +/* Runs libm4a's qtmovie_read() on a file through a model of Rockbox's + * playback buffer (a FILE_DATA ring refilled from the file), and prints + * the seek table it builds plus what it cost: buffer API calls, bytes + * loaded from the card, rebuffers (a seek outside the buffered data), + * yields. */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "codecs.h" +#include "m4a.h" + +#define FILE_DATA (40 * 1024) /* TARGET_AUDIO_BUFFER_CAP - guard */ +#define FILECHUNK (32 * 1024) /* BUFFERING_DEFAULT_FILECHUNK */ + +static unsigned char *file; +static size_t win_start, win_end; /* buffered [start, end) */ +static unsigned long calls, loaded, rebuffers, yields; +static int progress_calls, last_progress = -1, progress_backwards; +static int stop_after = -1; /* STOP_AFTER: say "stop" from that call */ +static struct mp3entry id3; +static struct codec_api api; +struct codec_api *ci = &api; + +static unsigned char heap[1 << 20]; +static size_t heap_used, heap_size; +void *codec_malloc(size_t size) +{ + size = (size + 3) & ~3u; + if (heap_used + size > heap_size) return NULL; + heap_used += size; + return heap + heap_used - size; +} + +/* make [pos, pos + n) buffered, as buffering.c would */ +static void need(size_t pos, size_t n) +{ + if (pos + n > api.filesize) n = api.filesize - pos; + if (pos >= win_start && pos + n <= win_end) return; + if (pos < win_start || pos > win_end + FILECHUNK) { /* rebuffer */ + rebuffers++; + win_start = win_end = pos; + } + win_start = pos; /* consumed data is freed */ + size_t new_end = pos + FILE_DATA; + if (new_end > api.filesize) new_end = api.filesize; + loaded += new_end - win_end; + win_end = new_end; +} +static size_t h_read(void *ptr, size_t size) +{ + calls++; + if (api.curpos + size > api.filesize) size = api.filesize - api.curpos; + need(api.curpos, size); + memcpy(ptr, file + api.curpos, size); + api.curpos += size; + return size; +} +static void h_advance(size_t amount) +{ + calls++; + api.curpos += amount; + if (api.curpos > api.filesize) api.curpos = api.filesize; + if (api.curpos > win_end) need(api.curpos, 1); +} +static bool h_seek(size_t pos) +{ + calls++; + api.curpos = pos; + if (pos < win_start || pos > win_end) need(pos, 1); + return true; +} +static void h_yield(void) { yields++; } +static bool h_progress(int percent) +{ + progress_calls++; + if (percent < last_progress || percent > 100) progress_backwards++; + last_progress = percent; + return stop_after < 0 || progress_calls < stop_after; +} + +int main(int argc, char **argv) +{ + if (argc < 3) { fprintf(stderr, "usage: harness FILE HEAP_BYTES [STOP_AFTER]\n"); return 2; } + FILE *f = fopen(argv[1], "rb"); + if (!f) { perror(argv[1]); return 2; } + fseek(f, 0, SEEK_END); api.filesize = ftell(f); fseek(f, 0, SEEK_SET); + file = malloc(api.filesize); + if (fread(file, 1, api.filesize, f) != api.filesize) return 2; + heap_size = strtoul(argv[2], NULL, 0); + id3.length = 15000000; + if (argc > 3) stop_after = atoi(argv[3]); + id3.codectype = AFMT_MP4_AAC; + api.id3 = &id3; + api.read_filebuf = h_read; api.advance_buffer = h_advance; + api.seek_buffer = h_seek; api.yield = h_yield; + api.load_progress = h_progress; + + stream_t stream; demux_res_t res; + memset(&res, 0, sizeof res); + stream_create(&stream, ci); + int ok = qtmovie_read(&stream, &res); + if (stop_after >= 0) { + /* told to stop: the load must give up at once, not finish */ + printf("stop after %d: ok %d, %d progress calls, %lu buffer calls\n", + stop_after, ok, progress_calls, calls); + return ok || progress_calls != stop_after; + } + unsigned long sum = 0; + for (uint32_t i = 0; i < res.num_lookup_table; i++) + sum = sum * 31 + res.lookup_table[i].sample * 7 + res.lookup_table[i].offset; + printf("ok %d mdat_offset %d curpos %zu\n", ok, res.mdat_offset, api.curpos); + printf("table %u entries, checksum %lu, last {%u, %u}\n", + res.num_lookup_table, sum, + res.lookup_table[res.num_lookup_table - 1].sample, + res.lookup_table[res.num_lookup_table - 1].offset); + printf("stsz_offset %d stts %u heap %zu/%zu\n", res.sample_byte_sizes_offset, + res.num_time_to_samples, heap_used, heap_size); + printf("cost: %lu buffer calls, %lu KiB loaded, %lu rebuffers, %lu yields\n", + calls, loaded / 1024, rebuffers, yields); + printf("progress: %d reports, last %d, %d out of order\n", + progress_calls, last_progress, progress_backwards); + return !ok || progress_backwards; +} diff --git a/tests/m4a/run.sh b/tests/m4a/run.sh new file mode 100755 index 0000000..0a1a98e --- /dev/null +++ b/tests/m4a/run.sh @@ -0,0 +1,27 @@ +#!/bin/sh +# Host check of libm4a's index parser (lib/rbcodec/codecs/libm4a/demux.c): +# runs qtmovie_read() on an M4B through a model of the playback buffer and +# prints the seek table it builds and what it cost (buffer calls, card +# reads, rebuffers, yields). +# +# tests/m4a/run.sh FILE.m4b [HEAP_BYTES] [ROCKBOX_TREE] [STOP_AFTER] +# STOP_AFTER: load_progress says "stop" from that call on, and the load +# must give up there. +set -eu +HERE=$(cd "$(dirname "$0")" && pwd) +REPO=$(cd "$HERE/../.." && pwd) +FILE=$1 +HEAP=${2:-65536} +TREE=${3:-$REPO/build/rockbox} +STOP=${4:-} +M4A=$TREE/lib/rbcodec/codecs/libm4a +T=$(mktemp -d) +trap 'rm -rf "$T"' EXIT +# m4a.c's stream_* helpers, up to stream_create() (its seek code needs the +# whole codec API) +awk '{print} /^void stream_create/{f=1} f && /^}/{exit}' "$M4A/m4a.c" > "$T/stream.c" +gcc -O2 -g -fsanitize=address,undefined -fno-sanitize-recover=all \ + -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable \ + -I"$HERE/shim" -I"$M4A" -o "$T/harness" \ + "$HERE/harness.c" "$M4A/demux.c" "$T/stream.c" +"$T/harness" "$FILE" "$HEAP" $STOP diff --git a/tests/m4a/shim/codeclib.h b/tests/m4a/shim/codeclib.h new file mode 100644 index 0000000..1c46fd0 --- /dev/null +++ b/tests/m4a/shim/codeclib.h @@ -0,0 +1,3 @@ +#include "codecs.h" +void *codec_malloc(size_t size); +#define malloc(x) codec_malloc(x) diff --git a/tests/m4a/shim/codecs.h b/tests/m4a/shim/codecs.h new file mode 100644 index 0000000..e2215ce --- /dev/null +++ b/tests/m4a/shim/codecs.h @@ -0,0 +1,24 @@ +/* Host stand-in for the parts of codecs.h that libm4a/demux.c uses */ +#ifndef CODECS_H +#define CODECS_H +#include <stddef.h> +#include <stdint.h> +#include <stdbool.h> +#include <sys/types.h> +enum { AFMT_MP4_AAC = 1, AFMT_MP4_AAC_HE, AFMT_MP4_ALAC }; +struct mp3entry { unsigned long length; int codectype; }; +#define CODEC_SIZE (128 * 1024) /* yp3.h, near enough */ +struct codec_api { + size_t filesize, curpos; + struct mp3entry *id3; + size_t (*read_filebuf)(void *ptr, size_t size); + void (*advance_buffer)(size_t amount); + bool (*seek_buffer)(size_t newpos); + void (*yield)(void); + void (*debugf)(const char *fmt, ...); + bool (*load_progress)(int percent); +}; +extern struct codec_api *ci; +#define ROCKBOX_LITTLE_ENDIAN 1 +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#endif |
