diff options
| author | YP3 Audiobook <noreply@example.invalid> | 2026-11-27 00:51:12 +0000 |
|---|---|---|
| committer | YP3 Audiobook <noreply@example.invalid> | 2026-11-27 00:51:12 +0000 |
| commit | 7a06b5d2d7ac329dc5cba794b019ba185b9b09e2 (patch) | |
| tree | fac537bb029a8532aadec97816292c3074b9e608 /patches/0006-yp3-load-long-M4B-books-without-freezing-with-a-load.patch | |
| parent | 4b670f7ac2635de6d6583d603d97f0bcfbec6733 (diff) | |
| download | yp3-audiobook-firmware-7a06b5d2d7ac329dc5cba794b019ba185b9b09e2.tar.gz | |
Release 1.1
- Hold Up/Down to keep changing the volume
- Pulling the SD card powers the player off instead of crashing; with no
card at power-on it says so and turns off
- Long M4B books load without freezing the controls, with a
"Loading book N%" screen
- The Center press that powers the player on no longer pauses the book
as it resumes (which also made it load twice)
- No "Bookmark saved" pause at power-off
- USB disk shows up right away while a book loads (flasher's
"NO USB DISK")
- tests/m4a: host test of the M4B index loader
Diffstat (limited to 'patches/0006-yp3-load-long-M4B-books-without-freezing-with-a-load.patch')
| -rw-r--r-- | patches/0006-yp3-load-long-M4B-books-without-freezing-with-a-load.patch | 322 |
1 files changed, 322 insertions, 0 deletions
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 + |
