aboutsummaryrefslogtreecommitdiff
path: root/tests/m4a/harness.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/m4a/harness.c')
-rw-r--r--tests/m4a/harness.c123
1 files changed, 123 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;
+}