aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorYP3 Audiobook <noreply@example.invalid>2026-11-27 00:51:12 +0000
committerYP3 Audiobook <noreply@example.invalid>2026-11-27 00:51:12 +0000
commit7a06b5d2d7ac329dc5cba794b019ba185b9b09e2 (patch)
treefac537bb029a8532aadec97816292c3074b9e608 /tests
parent4b670f7ac2635de6d6583d603d97f0bcfbec6733 (diff)
downloadyp3-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 'tests')
-rw-r--r--tests/m4a/harness.c123
-rwxr-xr-xtests/m4a/run.sh27
-rw-r--r--tests/m4a/shim/codeclib.h3
-rw-r--r--tests/m4a/shim/codecs.h24
4 files changed, 177 insertions, 0 deletions
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