diff options
29 files changed, 2358 insertions, 0 deletions
diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..9ba24bd --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +*.tar.xz binary +*.bin binary diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3515553 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +tools/ @@ -0,0 +1,280 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS diff --git a/README.md b/README.md new file mode 100644 index 0000000..1437dd2 --- /dev/null +++ b/README.md @@ -0,0 +1,379 @@ +# YP3 Audiobook Rockbox + +Rockbox for the **YP3** MP3 player (SmartLink SL6801, 128x160 screen, five +buttons), set up as a portable **audiobook player**. It is the unofficial +YP3 port of Rockbox +([jaydens-tasty-slop/rockbox](https://github.com/jaydens-tasty-slop/rockbox)) +with three patches that make `.m4b` audiobooks work the way you'd expect. + +Ready-to-flash firmware is in [`release/`](release/). It is installed with +[rockslop](https://github.com/jaydens-tasty-slop/rockslop), the YP3 port's +flasher, which you download separately ([Get rockslop](#1-get-rockslop)). + +> **Unofficial and experimental.** Neither this nor the YP3 port it is built +> on has had any review beyond testing on real players. Flashing can brick a +> player. Every flash backs up the stock firmware first, and the player's +> download mode is in its boot ROM, which flashing cannot touch. + +## Contents + +- [What it does](#what-it-does) +- [Controls](#controls) +- [Installing](#installing) +- [Adding audiobooks](#adding-audiobooks) +- [Power on and off](#power-on-and-off) +- [Troubleshooting](#troubleshooting) +- [Updating, and going back to stock](#updating-and-going-back-to-stock) +- [Known issues](#known-issues) +- [Building from source](#building-from-source) +- [Repository layout](#repository-layout) +- [Versions and sources](#versions-and-sources) +- [Licences and credits](#licences-and-credits) + +## What it does + +- **Plays `.m4b`** (and `.m4a`/`.mp4`/MP3 etc.). AAC, HE-AAC and mono are + all handled, and so are books with the index (`moov`) at the end of the + file. +- **Keeps your place.** + - The position is saved when you pause and every minute while playing, + so a crash, a RESET or a flat battery costs you at most a minute. + (Stock Rockbox only saved it when a file started or stopped, which + could lose hours in a single-file book.) + - Power-off saves it, and power-on goes straight back into the book, + rewound 5 s. +- **Each book has its own bookmark.** Starting another book saves your place + in the current one first. Picking a book from Files resumes it where you + left off, even with several books in one folder. + - The main menu's **Recent Bookmarks** lists the books you're partway + through. +- **Chapters inside `.m4b` files** (Nero `chpl` lists and iTunes/Audible + QuickTime chapter tracks): + - Down+Left / Down+Right jump to the previous / next chapter. + - On the Now Playing screen, the title line shows the chapter and "Next + Track" shows the next chapter. The track number reads like `3/24`. + - The progress bar has a tick at each chapter. + - Hold Centre → **Chapters** lists them with start times. Pick one to + jump there. + - A book split into one file per chapter works the same way, with files + as chapters. +- **Tapping Left/Right jumps 30 s** and never leaves the book. + +## Controls + +Now Playing screen: + +| Button | Tap | Hold | +|---|---|---| +| Centre | play / pause | menu (~0.3 s), **Chapters** first; power off (3 s) | +| Left / Right | back / forward 30 s | seek, speeding up | +| Down+Left / Down+Right (press both together) | previous / next chapter | — | +| Up | volume up | stop (saves a bookmark) | +| Down | volume down | volume down, repeating | +| Up+Left | main menu (works on every screen) | — | + +- **Previous chapter** restarts the current chapter, or goes to the one + before if you're in the first 3 s of it. +- **Next chapter** in the last chapter beeps instead of starting another + book. + +In menus and lists: Up/Down move, Right or Centre selects, Left goes back, +and Up+Left goes to the main menu. + +These are only defaults, changed under Settings → Playback Settings: +- Skip Length 30 s +- Prevent Track Skipping on +- rewind on resume 5 s and on unpause 2 s +- bookmarks created and loaded automatically +- Start Screen "Resume Playback" + +## Installing + +You need: + +- A YP3, charged. The flasher refuses below 3.3 V. +- A **microSD card, SDHC or SDXC** (4 GB or more; old ≤2 GB SD 1.x cards + fail), **formatted FAT32 with an MBR partition table.** Rockbox can't read + exFAT, which most 64 GB+ cards come with. +- A data USB cable, and a Linux x86-64 computer with udisks2 + (`udisksctl`). For Windows or macOS, use the rockslop GUI instead (below) + with **USE A FILE** and the same package. + +### 1. Get rockslop + +rockslop is the flasher: it backs up the player, writes the firmware, and +copies Rockbox's files to the card. Its source and releases are at +https://github.com/jaydens-tasty-slop/rockslop (site: +https://rockslop.jayd.ml). + +This firmware was flashed and tested with the command-line version +**`20261122_011647-8715cfc`**. Download that one into `tools/` (ignored by +git) and check it: + +```sh +mkdir -p tools +curl -fL -o tools/rockslop-cli \ + https://github.com/jaydens-tasty-slop/rockslop/releases/download/20261122_011647-8715cfc/rockslop-cli-linux-x86_64 +echo "60a138210817bdfa25383d8bbaaa3b6a625740fe696561887385d3cb7e17fb36 tools/rockslop-cli" | sha256sum -c +chmod +x tools/rockslop-cli +tools/rockslop-cli --version # rockslop-cli 20261122_011647-8715cfc +``` + +- **Newer rockslop releases** should work too: rockslop refuses a package + in a format it can't read, and says so. +- **If that release is gone,** build the CLI from source at commit + `8715cfc99d392de62dd30ce06dc9703098e22c0f` (Rust plus the ALSA headers; + see rockslop's README): `cargo build --release`, which gives + `target/release/rockslop-cli`. +- **Windows and macOS:** use the GUI (`rockslop.exe` / `Rockslop.app`) from + the same [releases page](https://github.com/jaydens-tasty-slop/rockslop/releases). + Pick **USE A FILE** and choose this repository's package. The steps on the + player are the same. + +### 2. One-time computer setup + +Let your user reach the player over USB without root, with rockslop's udev +rule: + +```sh +sudo tee /etc/udev/rules.d/70-rockslop.rules >/dev/null <<'EOF' +SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="301a", ATTR{idProduct}=="2800", MODE="0660", TAG+="uaccess" +SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="301a", ATTR{idProduct}=="2801", MODE="0660", TAG+="uaccess" +SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="10d6", ATTR{idProduct}=="10d6", MODE="0660", TAG+="uaccess" +SUBSYSTEM=="usb", ENV{DEVTYPE}=="usb_device", ATTR{idVendor}=="10d6", ATTR{idProduct}=="b00b", MODE="0660", TAG+="uaccess" +EOF +sudo udevadm control --reload +``` + +That is rockslop's `packaging/udev/70-rockslop.rules`. Reconnect the player +afterwards if it was plugged in. Then check the firmware package: + +```sh +(cd release && sha256sum -c *.sha256) # must say OK +tools/rockslop-cli check release/rockbox-yp3-audiobook-1.0.tar.xz +``` + +### 3. Prepare the card + +Format the card FAT32 **before** flashing (this erases it), in a card reader +or later through the player. Replace `/dev/sdX` with the card after checking +`lsblk`: + +```sh +lsblk -o NAME,SIZE,MODEL # find the card; be sure +sudo wipefs -a /dev/sdX +sudo parted -s /dev/sdX mklabel msdos mkpart primary fat32 1MiB 100% +sudo mkfs.vfat -F 32 -n AUDIOBOOKS /dev/sdX1 +``` + +Then put the card **in the player**. + +### 4. Flash + +```sh +tools/rockslop-cli flash release/rockbox-yp3-audiobook-1.0.tar.xz +``` + +1. The first time on a computer, it shows a waiver. Type `NO BACKSIES`. +2. When it prints `waiting for the YP3`, put the player in **download mode**: + 1. Unplug the USB cable. + 2. Power the player off: hold Centre until it goes off. It won't power + off while plugged in. + 3. **Hold Down**, plug in the USB cable, and release Down when the + terminal prints `YP3 found in download mode`. The screen may stay + dark; that's normal. + 4. If it isn't found, unplug, then hold Down and tap **RESET** on the + right side. You can also run `tools/rockslop-cli status`. +3. Answer `y` to `Back up and flash YP3 ...?`. +4. Wait for `done: unplug the player`. It runs through these stages by + itself: + 1. Backs up the whole flash, to `~/.local/share/rockslop/backups/`. + 2. Writes and checks the firmware. + 3. Reboots into Rockbox. + 4. Copies about 105 MB (1,835 files) to the card and reads it all back. + + The card copy is slow: the player's USB runs at about 0.5–1 MB/s and + most of the files are small. The progress bar can sit at 0% for minutes + while files are still being written. +5. Unplug. Rockbox is running. + +**Keep the stock backup safe.** Copy +`~/.local/share/rockslop/backups/yp3-*-stock.bin` and its `.sha256` +somewhere other than this computer. (Don't commit it to a public +repository: it's the maker's firmware.) The flash also leaves a copy on +the card, at `.rockbox/yp3-*-stock.bin`. + +## Adding audiobooks + +Put `.m4b` files anywhere on the card except `.rockbox/`. Each book can have +its own folder, or several can share one. Then pick a book from **Files** on +the player. + +- **Card reader (recommended):** fast. +- **Through the player:** with Rockbox running, plug it in and it appears as + a USB disk. This runs at about 0.5–1 MB/s, so a 500 MB book takes around + 10 minutes. If no disk appears, set Settings → General Settings → System → + USB → USB Mode to **Mass Storage**. + +Book title, author and chapter names come from the file's tags. For +chapters, `.m4b` files made by most tools work (ffmpeg, iTunes, Audible +converters, m4b-tool). + +## Power on and off + +- **Power on:** on battery, hold Centre **about 3–4 s**, until the Rockbox + logo appears. Release earlier and power drops again. +- **Power off:** hold Centre 3 s in Rockbox. The position is saved. +- With USB plugged in, the player doesn't power off; it restarts. + +## Troubleshooting + +| Symptom | Cause and fix | +|---|---| +| `ATA error: -5`, then the screen goes dark | **No card** in the player, a card not clicked in fully, or an old SD 1.x card. Insert a working SDHC/SDXC card. Nothing needs reflashing. | +| Flash ends with `E502: could not mount the player's card: ... not a mountable filesystem` | The card has no FAT32 partition (blank or exFAT). Leave the player plugged in on its USB screen, format the card as in [Prepare the card](#3-prepare-the-card) (it shows up as "Rockbox Internal Storage"), then run `tools/rockslop-cli sync release/rockbox-yp3-audiobook-1.0.tar.xz`. | +| Sync seems stuck at 0% | It isn't. Many small files over slow USB; watch the card fill with `du -sh /run/media/$USER/*`. Allow about 5–10 minutes. | +| Won't power on, or only a **pure white screen** that ignores every button; plugged in without holding anything, `status` says `download mode` | The power chip still has its one-shot "boot to download mode" flag set, which can happen after flashing or an unclean power-off. Rerun the `flash` command with the player in download mode: it writes nothing if the firmware matches, and reboots into Rockbox properly. Then power off **from Rockbox** (hold Centre 3 s). Holding Centre about 10 s (the power chip's own reset) can also clear it. | +| `NO USB DISK from Rockbox` | Settings → General Settings → System → USB → USB Mode → **Mass Storage**, or unplug, hold any key, and plug in again. | +| Only the card step failed | The firmware is installed. With Rockbox running and plugged in: `tools/rockslop-cli sync release/rockbox-yp3-audiobook-1.0.tar.xz` | +| The new defaults don't apply | Settings saved by an earlier Rockbox on the card win. Use Settings → General Settings → Reset Settings, or set them by hand. | + +The flasher logs to `~/.local/share/rockslop/rockslop.log`, and every +rockslop error code is explained in its +[ERRORS.md](https://github.com/jaydens-tasty-slop/rockslop/blob/8715cfc99d392de62dd30ce06dc9703098e22c0f/ERRORS.md). + +## Updating, and going back to stock + +- **Reflash or update:** flash the new package the same way. Only sectors + that differ are written, and only card files that differ are copied. +- **Stock firmware:** put the player in download mode and run + `tools/rockslop-cli restore yp3`. It uses the newest stock + backup on this computer, or on the card, or the image you name: + `restore yp3 path/to/yp3-...-stock.bin`. +- **The upstream YP3 port without these patches, for comparison:** + `tools/rockslop-cli flash yp3`. This downloads its latest + release from GitHub. + +## Known issues + +- **Volume steps are tiny.** Each tap of Up/Down changes the volume by + 0.5 dB (142 steps from silent to full), which is hard to hear. Holding + Down repeats, but holding Up is Stop. The same is true of the upstream + port. +- At most **255 chapters** per file are kept. The last one then runs to the + end of the book. +- Chapters in **MP3** audiobooks (ID3 `CHAP` frames) are not read. MP3 + books split into one file per chapter work as chapters. +- Elapsed and remaining times are for the whole file, not the chapter. +- When a book ends, playback carries on into the next file in its folder. +- Files over 4 GB (MP4 `co64`) are not supported. + +## Building from source + +`scripts/build.sh` rebuilds the package from the files in this repository: +1. Unpacks the upstream source snapshot. +2. Applies `patches/`. +3. Runs the upstream release script, with the vendored theme assets and + rockslop's packager, `packaging/mkpkg.py`. + +It downloads two things: +- **The Arm GNU toolchain** (gcc-arm-none-eabi 10.3-2021.10, about 150 MB), + checked against its pinned sha256 and cached in `~/.cache/yp3-audiobook` + (or `$YP3_CACHE`). +- **rockslop's source** at the pinned commit, `git clone`d for `mkpkg.py`. + To use a copy you already have instead, set + `MKPKG=/path/to/rockslop/packaging/mkpkg.py`. + +```sh +# Arch: base-devel git perl python curl bzip2 xz zip +# Debian/Ubuntu: build-essential git perl python3 curl bzip2 xz-utils zip +scripts/build.sh audiobook-1.1 +tools/rockslop-cli check build/out/rockbox-yp3-audiobook-1.1.tar.xz +``` + +It takes about 5–10 minutes. A handful of big game plugins never fit and are +skipped; the script lists them. Clone the repository to a **short path**: +the upstream build fails with "Argument list too long" from deep +directories, and the script refuses paths it knows are too long. + +The script works in `build/` (ignored by git, safe to delete): the patched +source is `build/rockbox`, and the package lands in `build/out/`. + +To change the firmware, edit `build/rockbox` (a git repository with the +patches as commits), commit, then regenerate the patches: + +```sh +rm patches/*.patch +git -C build/rockbox format-patch -o "$PWD/patches" "$(git -C build/rockbox rev-list --max-parents=0 HEAD)".. +``` + +**Chapter reader tests** run on the computer (gcc and ffmpeg). They build +`apps/chapters.c` with AddressSanitizer and test it against generated +audiobooks: + +```sh +tests/chapters/run.sh # after scripts/build.sh, or: run.sh path/to/patched/tree +``` + +**Releasing a new version:** build in a container, so the package has no +paths from your computer in it. The compiler records the build directory +in some game plugins and in the debug map. Mounted at `/src`, the paths +are the same for everyone: + +```sh +podman run --rm -v "$PWD":/src:z -v "$HOME/.cache/yp3-audiobook":/cache:z \ + -e YP3_CACHE=/cache -w /src docker.io/library/debian:bookworm sh -c ' + apt-get update && apt-get install -y --no-install-recommends \ + build-essential git perl python3 curl bzip2 xz-utils zip ca-certificates && + git config --global --add safe.directory "*" && + scripts/build.sh audiobook-X.Y' +``` + +(`docker` works the same way. Create `~/.cache/yp3-audiobook` first.) Then: + +1. Test it on a player. +2. Copy the package to `release/` as `rockbox-yp3-audiobook-X.Y.tar.xz` and + write its checksum: `(cd release && sha256sum rockbox-yp3-audiobook-X.Y.tar.xz > rockbox-yp3-audiobook-X.Y.tar.xz.sha256)`. +3. Commit and tag `vX.Y`. + +Remove old packages from `release/` if you like; they stay in git history. + +## Repository layout + +``` +release/ tested firmware packages for rockslop, with .sha256 +tools/ (not in git) where you download rockslop-cli +patches/ the audiobook changes, against upstream 822b9f5e +scripts/ build.sh +tests/ chapters/: host tests of the M4B chapter reader +vendor/ source snapshots and assets the build uses (SHA256SUMS): + rockbox-822b9f5e.tar.xz upstream YP3 Rockbox source + yp3-assets-1.tar.xz themes and models upstream adds +LICENSE GNU GPL v2 +``` + +## Versions and sources + +| Part | Version | Upstream | +|---|---|---| +| Firmware package `release/rockbox-yp3-audiobook-1.0.tar.xz` | `audiobook-1.0`; the boot screen shows `0e6e29726d-261125` | upstream + `patches/`, built in a container (see [Building from source](#building-from-source)) | +| YP3 Rockbox port | commit `822b9f5e999122bc25738fc9746bb7a17bb48fa6` (2026-11-21) | https://github.com/jaydens-tasty-slop/rockbox | +| rockslop (flasher; `mkpkg.py` for builds) | CLI release `20261122_011647-8715cfc`, commit `8715cfc99d392de62dd30ce06dc9703098e22c0f`, Linux CLI sha256 `60a13821…17fb36` | https://github.com/jaydens-tasty-slop/rockslop ([site](https://rockslop.jayd.ml)) | +| Theme/model assets | `yp3-assets-1` | rockbox repo release `yp3-assets-1` | +| Toolchain | gcc-arm-none-eabi 10.3-2021.10, sha256 `97dbb4f0…16c9a3` | developer.arm.com | + +Building with `scripts/build.sh` gives the same firmware as `release/`, +anywhere. Only the paths recorded in some game plugins and in the debug +map differ, unless it is built in the container as above. + +## Licences and credits + +- **Rockbox and the YP3 port:** GNU GPL v2 or later (`LICENSE`). Plugins, + themes and fonts carry their own licences, which are installed on the card + in `.rockbox/licenses/`. +- **rockslop** (not included; linked above): GPL-2.0-or-later. +- **The audiobook patches** (`patches/`) are under the same licence as + Rockbox. + +Credit goes to the Rockbox project, the YP3 port and rockslop by +jaydens-tasty-slop, and the authors listed in their CREDITS files. diff --git a/patches/0001-yp3-audiobook-resume-save-on-pause-periodically-per-.patch b/patches/0001-yp3-audiobook-resume-save-on-pause-periodically-per-.patch new file mode 100644 index 0000000..007e110 --- /dev/null +++ b/patches/0001-yp3-audiobook-resume-save-on-pause-periodically-per-.patch @@ -0,0 +1,386 @@ +From 7482c8a3508e68ccd705d12e2291d402bb011351 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Wed, 25 Nov 2026 20:11:27 +0000 +Subject: [PATCH 1/3] yp3: audiobook resume (save on pause/periodically, + per-file bookmarks, defaults) + +--- + apps/bookmark.c | 87 +++++++++++++++++++++++++++++++++--- + apps/bookmark.h | 3 ++ + apps/filetree.c | 9 ++++ + apps/playback.c | 30 +++++++++++++ + apps/settings.c | 12 +++++ + apps/settings.h | 3 ++ + apps/settings_list.c | 38 +++++++++++++--- + firmware/export/config/yp3.h | 7 +++ + 8 files changed, 177 insertions(+), 12 deletions(-) + +diff --git a/apps/bookmark.c b/apps/bookmark.c +index 4bb97a15..a6790c7f 100644 +--- a/apps/bookmark.c ++++ b/apps/bookmark.c +@@ -144,7 +144,8 @@ static long long_token(const char **s) + /*-------------------------------------------------------------------------*/ + static bool bookmark_get_playlist_and_track_hash(const char *bookmark, + uint32_t *pl_hash, +- uint32_t *track_hash) ++ uint32_t *track_hash, ++ bool per_track) + { + *pl_hash = 0; + *track_hash = 0; +@@ -163,7 +164,7 @@ static bool bookmark_get_playlist_and_track_hash(const char *bookmark, + track = pl_end + 1; + get_hash(pl_start, pl_hash, pl_len); + +- if (global_settings.usemrb == BOOKMARK_ONE_PER_TRACK) ++ if (per_track) + { + get_hash(track, track_hash, strlen(track)); + } +@@ -322,12 +323,26 @@ static bool add_bookmark(const char* bookmark_file_name, + || (global_settings.usemrb == BOOKMARK_ONE_PER_TRACK))) + { + +- if (bookmark_get_playlist_and_track_hash(bookmark, &pl_hash, &pl_track_hash)) ++ bool per_track = (global_settings.usemrb == BOOKMARK_ONE_PER_TRACK); ++ if (bookmark_get_playlist_and_track_hash(bookmark, &pl_hash, ++ &pl_track_hash, per_track)) + { + comp_playlist = true; +- comp_track = (global_settings.usemrb == BOOKMARK_ONE_PER_TRACK); ++ comp_track = per_track; + } + } ++#ifdef HAVE_AUDIOBOOK_RESUME ++ /* A folder's bookmark file keeps one bookmark per file: the newest ++ place in each book. Upstream adds one on every stop and keeps them ++ all, and autoload took the newest whichever file was picked. */ ++ else if (!most_recent && ++ bookmark_get_playlist_and_track_hash(bookmark, &pl_hash, ++ &pl_track_hash, true)) ++ { ++ comp_playlist = true; ++ comp_track = true; ++ } ++#endif + + logf("adding bookmark to %s [%s]", fnamebuf, bookmark); + /* Writing the new bookmark to the begining of the temp file */ +@@ -357,7 +372,8 @@ static bool add_bookmark(const char* bookmark_file_name, + if (comp_playlist) + { + if (bookmark_get_playlist_and_track_hash(global_temp_buffer, +- &bm_pl_hash, &bm_pl_track_hash)) ++ &bm_pl_hash, &bm_pl_track_hash, ++ comp_track)) + { + equal = (pl_hash == bm_pl_hash); + if (equal && comp_track) +@@ -1333,6 +1349,67 @@ int bookmark_autoload(const char* file) + } + } + ++#ifdef HAVE_AUDIOBOOK_RESUME ++/* The track a bookmark line names, without its folder. */ ++static const char *bookmark_track_name(const char *bookmark) ++{ ++ const char *track = strrchr(bookmark, ';'); ++ const char *slash; ++ ++ if (!track) ++ return NULL; ++ track++; ++ slash = strrchr(track, '/'); ++ return slash ? slash + 1 : track; ++} ++ ++/* ----------------------------------------------------------------------- */ ++/* Autoload for a file picked in the browser: resume that file at its own */ ++/* bookmark in its folder's bookmark file, so each book in a folder keeps */ ++/* its place. A file with no bookmark there plays from its start. */ ++/* Autoload "ask" keeps the upstream list. */ ++/* ----------------------------------------------------------------------- */ ++int bookmark_autoload_track(const char* dir, const char* track_path) ++{ ++ static char line[MAX_BOOKMARK_SIZE]; ++ char bm_filename[MAX_PATH]; ++ const char *want; ++ bool found = false; ++ int fd; ++ ++ if (global_settings.autoloadbookmark != BOOKMARK_YES) ++ return bookmark_autoload(dir); ++ ++ if (!generate_bookmark_file_name(bm_filename, sizeof(bm_filename), dir, -1)) ++ return BOOKMARK_DONT_RESUME; ++ ++ fd = open(bm_filename, O_RDONLY); ++ if (fd < 0) ++ return BOOKMARK_DONT_RESUME; ++ ++ want = strrchr(track_path, '/'); ++ want = want ? want + 1 : track_path; ++ ++ /* newest first */ ++ while (read_line(fd, line, sizeof(line)) > 0) ++ { ++ const char *track = bookmark_track_name(line); ++ if (track && !strcmp(track, want) && ++ parse_bookmark(NULL, 0, line, NULL, false)) ++ { ++ found = true; ++ break; ++ } ++ } ++ close(fd); ++ ++ if (!found) ++ return BOOKMARK_DONT_RESUME; ++ logf("autoload %s from %s", want, bm_filename); ++ return play_bookmark(line) ? BOOKMARK_DO_RESUME : BOOKMARK_DONT_RESUME; ++} ++#endif /* HAVE_AUDIOBOOK_RESUME */ ++ + /* ----------------------------------------------------------------------- */ + /* This function loads the bookmark information into the resume memory. */ + /* This is an interface function. */ +diff --git a/apps/bookmark.h b/apps/bookmark.h +index 192e577c..392488d7 100644 +--- a/apps/bookmark.h ++++ b/apps/bookmark.h +@@ -40,6 +40,9 @@ bool bookmark_autobookmark(bool prompt_ok); + bool bookmark_create_menu(void); + bool bookmark_mrb_load(void); + int bookmark_autoload(const char* file); ++#ifdef HAVE_AUDIOBOOK_RESUME ++int bookmark_autoload_track(const char* dir, const char* track_path); ++#endif + bool bookmark_load(const char* file, bool autoload); + bool bookmark_exists(void); + bool bookmark_is_bookmarkable_state(void); +diff --git a/apps/filetree.c b/apps/filetree.c +index efbdbf72..e11036df 100644 +--- a/apps/filetree.c ++++ b/apps/filetree.c +@@ -610,7 +610,16 @@ int ft_enter(struct tree_context* c) + + case FILE_ATTR_AUDIO: + { ++#ifdef HAVE_AUDIOBOOK_RESUME ++ /* Picking a file replaces what is playing: keep that ++ book's place first (upstream only bookmarks on stop), ++ then resume the picked file at its own bookmark. */ ++ if (audio_status() && !global_settings.party_mode) ++ bookmark_autobookmark(false); ++ int res = bookmark_autoload_track(c->currdir, buf); ++#else + int res = bookmark_autoload(c->currdir); ++#endif + if (res == BOOKMARK_CANCEL || res == BOOKMARK_DO_RESUME) + break; + +diff --git a/apps/playback.c b/apps/playback.c +index 5e09ae3e..bc5f91c4 100644 +--- a/apps/playback.c ++++ b/apps/playback.c +@@ -1807,6 +1807,25 @@ static void audio_playlist_track_change(void) + playlist_update_resume_info(id3); + } + ++#ifdef HAVE_AUDIOBOOK_RESUME ++static long audiobook_next_save_tick; ++ ++/* Store the playing position as the resume point and have it written out ++ soon. Upstream refreshes it only at track changes and stop, so in a ++ long single-file audiobook a crash or dead battery went back to the ++ start of the file. */ ++static void audiobook_save_position(void) ++{ ++ struct mp3entry *id3 = valid_mp3entry(id3_get(PLAYING_ID3)); ++ ++ audiobook_next_save_tick = current_tick + AUDIOBOOK_RESUME_SAVE_TICKS; ++ if (!id3 || !id3->elapsed) ++ return; ++ playlist_update_resume_info(id3); ++ status_save_now(); ++} ++#endif /* HAVE_AUDIOBOOK_RESUME */ ++ + /* Change the data for the next track and send the event */ + static void audio_update_and_announce_next_track(const struct mp3entry *id3_next) + { +@@ -3694,6 +3713,11 @@ static void audio_on_pause(bool pause) + play_status = pause ? PLAY_PAUSED : PLAY_PLAYING; + iap_on_play_status(play_status); + ++#ifdef HAVE_AUDIOBOOK_RESUME ++ if (pause && !ff_rw_mode) ++ audiobook_save_position(); ++#endif ++ + if (!pause && codec_skip_pending) + { + /* Actually do the skip that is due - resets the status flag */ +@@ -4202,6 +4226,12 @@ void audio_playback_handler(struct queue_event *ev) + break; + } /* end switch */ + ++#ifdef HAVE_AUDIOBOOK_RESUME ++ if (play_status == PLAY_PLAYING && !ff_rw_mode && ++ TIME_AFTER(current_tick, audiobook_next_save_tick)) ++ audiobook_save_position(); ++#endif ++ + switch (filling) + { + /* Active states */ +diff --git a/apps/settings.c b/apps/settings.c +index ef77a799..62620cc8 100644 +--- a/apps/settings.c ++++ b/apps/settings.c +@@ -748,6 +748,18 @@ void status_save(bool force) + register_storage_idle_func(flush_global_status_callback); + } + ++#ifdef HAVE_AUDIOBOOK_RESUME ++/* Write the resume point at the next storage idle, skipping the 15 minute ++ * throttle: playback calls this on pause and every ++ * AUDIOBOOK_RESUME_SAVE_TICKS while playing, so a crash, RESET or a dead ++ * battery loses at most that much of a long single-file audiobook. */ ++void status_save_now(void) ++{ ++ next_status_update_tick = current_tick - 1; ++ register_storage_idle_func(flush_global_status_callback); ++} ++#endif ++ + int settings_save(void) + { + logf("%s", __func__); +diff --git a/apps/settings.h b/apps/settings.h +index a67981dc..9a6b67ef 100644 +--- a/apps/settings.h ++++ b/apps/settings.h +@@ -262,6 +262,9 @@ void settings_load(void) INIT_ATTR; + bool settings_load_config(const char* file, bool apply); + + void status_save(bool force); ++#ifdef HAVE_AUDIOBOOK_RESUME ++void status_save_now(void); ++#endif + int settings_save(void); + + /* defines for the options paramater */ +diff --git a/apps/settings_list.c b/apps/settings_list.c +index e0eda5e7..4f5e2f58 100644 +--- a/apps/settings_list.c ++++ b/apps/settings_list.c +@@ -324,6 +324,22 @@ static const char off[] = "off"; + static const char off_on[] = "off,on"; + static const char off_on_ask[] = "off,on,ask"; + static const char off_number_spell[] = "off,number,spell"; ++/* Defaults for an audiobook player (HAVE_AUDIOBOOK_RESUME): bookmark every ++ * book on stop, power-off and switching books, resume a picked file at its ++ * bookmark, start in the book that was playing, rewind a little on resume, ++ * and step 30 s on Left/Right. */ ++#ifdef HAVE_AUDIOBOOK_RESUME ++#define AUDIOBOOK_DEFAULT(audiobook, upstream) (audiobook) ++#else ++#define AUDIOBOOK_DEFAULT(audiobook, upstream) (upstream) ++#endif ++/* "start in screen": previous, root, files, [db,] wps... */ ++#ifdef HAVE_TAGCACHE ++#define AUDIOBOOK_START_WPS 4 ++#else ++#define AUDIOBOOK_START_WPS 3 ++#endif ++ + static const int timeout_sec_common[] = {-1,0,1,2,3,4,5,6,7,8,9,10,15,20,25,30, + 45,60,90,120,180,240,300,600,900,1200, + 1500,1800,2700,3600,4500,5400,6300,7200}; +@@ -1706,7 +1722,8 @@ const struct settings_list settings[] = { + ID2P(LANG_OFF), ID2P(LANG_ON), ID2P(LANG_ASK)), + /* bookmarks */ + CHOICE_SETTING(0, autocreatebookmark, LANG_BOOKMARK_SETTINGS_AUTOCREATE, +- BOOKMARK_NO, "autocreate bookmarks", ++ AUDIOBOOK_DEFAULT(BOOKMARK_YES, BOOKMARK_NO), ++ "autocreate bookmarks", + "off,on,ask,recent only - on,recent only - ask", NULL, 5, + ID2P(LANG_SET_BOOL_NO), ID2P(LANG_SET_BOOL_YES), + ID2P(LANG_ASK), ID2P(LANG_BOOKMARK_SETTINGS_RECENT_ONLY_YES), +@@ -1714,11 +1731,13 @@ const struct settings_list settings[] = { + OFFON_SETTING(0, autoupdatebookmark, LANG_BOOKMARK_SETTINGS_AUTOUPDATE, + false, "autoupdate bookmarks", NULL), + CHOICE_SETTING(0, autoloadbookmark, LANG_BOOKMARK_SETTINGS_AUTOLOAD, +- BOOKMARK_NO, "autoload bookmarks", off_on_ask, NULL, 3, ++ AUDIOBOOK_DEFAULT(BOOKMARK_YES, BOOKMARK_NO), ++ "autoload bookmarks", off_on_ask, NULL, 3, + ID2P(LANG_SET_BOOL_NO), ID2P(LANG_SET_BOOL_YES), + ID2P(LANG_ASK)), + CHOICE_SETTING(0, usemrb, LANG_BOOKMARK_SETTINGS_MAINTAIN_RECENT_BOOKMARKS, +- BOOKMARK_NO, "use most-recent-bookmarks", ++ AUDIOBOOK_DEFAULT(BOOKMARK_ONE_PER_TRACK, BOOKMARK_NO), ++ "use most-recent-bookmarks", + "off,on,unique only,one per track", NULL, 4, ID2P(LANG_SET_BOOL_NO), + ID2P(LANG_SET_BOOL_YES), + ID2P(LANG_BOOKMARK_SETTINGS_ONE_PER_PLAYLIST), +@@ -2226,7 +2245,8 @@ const struct settings_list settings[] = { + OFFON_SETTING(0, bt_autoconnect, LANG_BLUETOOTH_AUTOCONNECT, true, + "bluetooth auto-connect", NULL), + #endif +- INT_SETTING(F_TIME_SETTING, pause_rewind, LANG_PAUSE_REWIND, 0, ++ INT_SETTING(F_TIME_SETTING, pause_rewind, LANG_PAUSE_REWIND, ++ AUDIOBOOK_DEFAULT(2, 0), + "rewind duration on pause", UNIT_SEC, 0, 15, 1, + formatter_time_unit_0_is_off, getlang_time_unit_0_is_off, NULL), + #if CONFIG_TUNER +@@ -2287,12 +2307,15 @@ const struct settings_list settings[] = { + NULL), + #endif + TABLE_SETTING_LIST(F_TIME_SETTING | F_ALLOW_ARBITRARY_VALS, skip_length, +- LANG_SKIP_LENGTH, 0, "skip length", ++ LANG_SKIP_LENGTH, AUDIOBOOK_DEFAULT(30, 0), "skip length", + "outro,track", + UNIT_SEC, formatter_time_unit_0_is_skip_track, + getlang_time_unit_0_is_skip_track, NULL, + 25, timeout_sec_common), +- CHOICE_SETTING(F_CB_ON_SELECT_ONLY, start_in_screen, LANG_START_SCREEN, 1, ++ CHOICE_SETTING(F_CB_ON_SELECT_ONLY, start_in_screen, LANG_START_SCREEN, ++ /* audiobook: "Resume Playback" (main menu without ++ anything to resume) */ ++ AUDIOBOOK_DEFAULT(AUDIOBOOK_START_WPS, 1), + "start in screen", "previous,root,files," + #ifdef HAVE_TAGCACHE + #define START_DB_COUNT 1 +@@ -2561,7 +2584,8 @@ const struct settings_list settings[] = { + + #endif /* HAVE_HOTKEY */ + +- INT_SETTING(F_TIME_SETTING, resume_rewind, LANG_RESUME_REWIND, 0, ++ INT_SETTING(F_TIME_SETTING, resume_rewind, LANG_RESUME_REWIND, ++ AUDIOBOOK_DEFAULT(5, 0), + "resume rewind", UNIT_SEC, 0, 60, 5, + formatter_time_unit_0_is_off, getlang_time_unit_0_is_off, NULL), + CUSTOM_SETTING(0, root_menu_customized, +diff --git a/firmware/export/config/yp3.h b/firmware/export/config/yp3.h +index d43aa9ed..07614b6b 100644 +--- a/firmware/export/config/yp3.h ++++ b/firmware/export/config/yp3.h +@@ -250,6 +250,13 @@ + * a whole 40 KiB frame of heap, and cabbiev2's two left playback without + * its buffer (OOM panic). Skins keep their colours, fonts and icons. */ + #define HAVE_NO_BACKDROP_FILES ++/* Audiobook player: the resume point is written on pause and every ++ * AUDIOBOOK_RESUME_SAVE_TICKS while playing (not just at track changes and ++ * stop), each book in a folder keeps its own bookmark, switching books ++ * bookmarks the one playing, and the defaults bookmark and resume ++ * (apps/playback.c, bookmark.c, filetree.c, settings_list.c). */ ++#define HAVE_AUDIOBOOK_RESUME ++#define AUDIOBOOK_RESUME_SAVE_TICKS (60 * HZ) + /* No surround: its 31 KB delay buffer comes out of the playback buffer, + * which then stayed shrunk (stutter until a reset) */ + #define HAVE_NO_SURROUND +-- +2.56.1 + diff --git a/patches/0002-yp3-M4B-chapters-Down-Left-Right-chapter-title-marke.patch b/patches/0002-yp3-M4B-chapters-Down-Left-Right-chapter-title-marke.patch new file mode 100644 index 0000000..11b3410 --- /dev/null +++ b/patches/0002-yp3-M4B-chapters-Down-Left-Right-chapter-title-marke.patch @@ -0,0 +1,968 @@ +From 648bf22824c28ed4c432bf5312d25082ef8d2364 Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Wed, 25 Nov 2026 20:28:42 +0000 +Subject: [PATCH 2/3] yp3: M4B chapters (Down+Left/Right, chapter title, + markers, list) + +--- + apps/SOURCES | 3 + + apps/action.h | 4 + + apps/chapters.c | 644 ++++++++++++++++++++++++++++ + apps/chapters.h | 38 ++ + apps/gui/skin_engine/skin_display.c | 11 + + apps/gui/skin_engine/skin_tokens.c | 30 ++ + apps/gui/wps.c | 10 + + apps/keymaps/keymap-yp3.c | 11 +- + apps/onplay.c | 22 + + apps/settings_list.c | 4 +- + firmware/export/config/yp3.h | 5 + + 11 files changed, 780 insertions(+), 2 deletions(-) + create mode 100644 apps/chapters.c + create mode 100644 apps/chapters.h + +diff --git a/apps/SOURCES b/apps/SOURCES +index af692315..ed0f5be7 100644 +--- a/apps/SOURCES ++++ b/apps/SOURCES +@@ -7,6 +7,9 @@ alarm_menu.c + #endif + abrepeat.c + bookmark.c ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++chapters.c ++#endif + core_keymap.c + debug_menu.c + filetypes.c +diff --git a/apps/action.h b/apps/action.h +index 03a3a084..c382838a 100644 +--- a/apps/action.h ++++ b/apps/action.h +@@ -191,6 +191,10 @@ enum { + ACTION_WPS_ABSETB_NEXTDIR, /* you shouldnt want to change dir in ab-mode */ + ACTION_WPS_ABRESET, + ACTION_WPS_HOTKEY, ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++ ACTION_WPS_CHAPTERPREV, ++ ACTION_WPS_CHAPTERNEXT, ++#endif + + /* list and tree page up/down */ + ACTION_LISTTREE_PGUP,/* optional */ +diff --git a/apps/chapters.c b/apps/chapters.c +new file mode 100644 +index 00000000..9b483653 +--- /dev/null ++++ b/apps/chapters.c +@@ -0,0 +1,644 @@ ++/* SPDX-License-Identifier: GPL-2.0-or-later */ ++/* Audiobook chapters of the playing file. ++ * ++ * MP4/M4B files carry chapters one of two ways, often both: ++ * - a Nero 'chpl' atom in moov/udta (ffmpeg writes it): up to 255 ++ * entries of a start time in 100 ns units and a title; ++ * - a QuickTime chapter track: the audio track's tref/chap names a text ++ * track whose samples are the titles (a 16-bit length, then the text) ++ * and whose sample times are the starts (iTunes, Audible conversions). ++ * 'chpl' is used when present, else the chapter track. ++ * ++ * Only the start times and where each title is in the file are kept, for ++ * the one track loaded (8 bytes a chapter, CHAPTERS_MAX at most): titles ++ * are read from the file when shown. Everything here runs in the UI ++ * thread (it opens files). */ ++ ++#include <stdio.h> ++#include <string.h> ++#include "string-extra.h" ++#include <stdint.h> ++#include "config.h" ++#include "system.h" ++#include "file.h" ++#include "rbunicode.h" ++#include "metadata.h" ++#include "audio.h" ++#include "playback.h" ++#include "playlist.h" ++#include "misc.h" ++#include "screen_access.h" ++#include "list.h" ++#include "root_menu.h" ++#include "logf.h" ++#include "chapters.h" ++ ++#define CHAPTERS_MAX 255 ++#define TITLE_MAX 96 /* bytes of a title read from the file */ ++ ++#define FOURCC(a,b,c,d) (((uint32_t)(a) << 24) | ((b) << 16) | ((c) << 8) | (d)) ++ ++static uint32_t ch_start[CHAPTERS_MAX]; /* ms */ ++static uint32_t ch_title[CHAPTERS_MAX]; /* file offset of the title's length ++ field; 0: no title */ ++static int ch_count; ++static bool ch_qt_titles; /* 16-bit title lengths, else 8 */ ++static char ch_path[MAX_PATH]; /* the file they are from */ ++static int ch_last_index = -1; /* chapters_changed() */ ++static int ch_title_index = -1; /* cur_title holds this one */ ++static char cur_title[TITLE_MAX + 1]; ++ ++/* --- a small buffered reader: the atoms are walked with many tiny reads --- */ ++ ++static struct ++{ ++ int fd; ++ uint32_t pos; /* file offset of buf[0] */ ++ int len, i; ++ bool err; ++ unsigned char buf[256]; ++} rd; ++ ++static void rd_seek(uint32_t off) ++{ ++ if (rd.len && off >= rd.pos && off < rd.pos + rd.len) ++ { ++ rd.i = off - rd.pos; ++ return; ++ } ++ if (lseek(rd.fd, off, SEEK_SET) != (off_t)off) ++ rd.err = true; ++ rd.pos = off; ++ rd.len = rd.i = 0; ++} ++ ++static unsigned rd_u8(void) ++{ ++ if (rd.i >= rd.len) ++ { ++ rd.pos += rd.len; ++ rd.i = 0; ++ rd.len = read(rd.fd, rd.buf, sizeof(rd.buf)); ++ if (rd.len <= 0) ++ { ++ rd.len = 0; ++ rd.err = true; ++ return 0; ++ } ++ } ++ return rd.buf[rd.i++]; ++} ++ ++static uint32_t rd_u16(void) ++{ ++ uint32_t v = rd_u8() << 8; ++ return v | rd_u8(); ++} ++ ++static uint32_t rd_u32(void) ++{ ++ uint32_t v = rd_u16() << 16; ++ return v | rd_u16(); ++} ++ ++static uint64_t rd_u64(void) ++{ ++ uint64_t v = (uint64_t)rd_u32() << 32; ++ return v | rd_u32(); ++} ++ ++static uint32_t rd_tell(void) ++{ ++ return rd.pos + rd.i; ++} ++ ++/* --- atoms --- */ ++ ++struct atom ++{ ++ uint32_t type; ++ uint32_t data; /* offset of its contents */ ++ uint32_t end; /* offset just past it */ ++}; ++ ++/* The atom at *pos, if it lies within end; *pos moves past it. */ ++static bool atom_next(uint32_t *pos, uint32_t end, struct atom *a) ++{ ++ uint64_t size; ++ uint32_t hdr = 8; ++ ++ if (rd.err || *pos >= end || end - *pos < 8) ++ return false; ++ rd_seek(*pos); ++ size = rd_u32(); ++ a->type = rd_u32(); ++ if (size == 1) ++ { ++ size = rd_u64(); ++ hdr = 16; ++ } ++ else if (size == 0) ++ size = end - *pos; /* to the end of its parent */ ++ if (rd.err || size < hdr || size > (uint64_t)(end - *pos)) ++ return false; ++ a->data = *pos + hdr; ++ a->end = *pos + (uint32_t)size; ++ *pos = a->end; ++ return true; ++} ++ ++static bool atom_find(uint32_t start, uint32_t end, uint32_t type, ++ struct atom *a) ++{ ++ uint32_t pos = start; ++ while (atom_next(&pos, end, a)) ++ if (a->type == type) ++ return true; ++ return false; ++} ++ ++/* A path of nested atoms, e.g. "mdia" "minf" "stbl". */ ++static bool atom_path(const struct atom *parent, const uint32_t *types, ++ int n, struct atom *a) ++{ ++ uint32_t start = parent->data, end = parent->end; ++ for (int i = 0; i < n; i++) ++ { ++ if (!atom_find(start, end, types[i], a)) ++ return false; ++ start = a->data; ++ end = a->end; ++ } ++ return true; ++} ++ ++/* --- Nero chapters: moov/udta/chpl --- */ ++ ++static int read_chpl(const struct atom *chpl) ++{ ++ int n, count = 0; ++ ++ /* version (1 byte), flags (3), version 1: 4 reserved, count (1) */ ++ rd_seek(chpl->data); ++ rd_seek(chpl->data + (rd_u8() != 0 ? 8 : 4)); ++ n = rd_u8(); ++ for (int i = 0; i < n && count < CHAPTERS_MAX; i++) ++ { ++ uint64_t start = rd_u64(); /* 100 ns units */ ++ uint32_t title = rd_tell(); ++ uint32_t len = rd_u8(); ++ if (rd.err || title + 1 + len > chpl->end) ++ break; ++ rd_seek(title + 1 + len); ++ ch_start[count] = (uint32_t)(start / 10000); ++ ch_title[count] = title; ++ count++; ++ } ++ ch_qt_titles = false; ++ return count; ++} ++ ++/* --- QuickTime chapter track --- */ ++ ++static uint32_t trak_id(const struct atom *trak) ++{ ++ struct atom a; ++ if (!atom_find(trak->data, trak->end, FOURCC('t','k','h','d'), &a)) ++ return 0; ++ rd_seek(a.data); ++ if (rd_u8() == 1) ++ rd_seek(a.data + 4 + 16); /* 64-bit creation/modification times */ ++ else ++ rd_seek(a.data + 4 + 8); ++ return rd_u32(); ++} ++ ++/* The track the audio track names as its chapters (tref/chap), 0: none */ ++static uint32_t chapter_track_id(const struct atom *moov) ++{ ++ uint32_t pos = moov->data; ++ struct atom trak, a; ++ static const uint32_t chap_path[] = ++ { FOURCC('t','r','e','f'), FOURCC('c','h','a','p') }; ++ ++ while (atom_next(&pos, moov->end, &trak)) ++ { ++ if (trak.type != FOURCC('t','r','a','k')) ++ continue; ++ if (atom_path(&trak, chap_path, 2, &a) && a.end - a.data >= 4) ++ { ++ rd_seek(a.data); ++ return rd_u32(); ++ } ++ } ++ return 0; ++} ++ ++static int read_chapter_track(const struct atom *trak) ++{ ++ static const uint32_t mdhd_path[] = ++ { FOURCC('m','d','i','a'), FOURCC('m','d','h','d') }; ++ static const uint32_t stbl_path[] = ++ { FOURCC('m','d','i','a'), FOURCC('m','i','n','f'), ++ FOURCC('s','t','b','l') }; ++ struct atom mdhd, stbl, stts, stsc, stsz, stco; ++ uint32_t timescale, n_stts, n_stsc, n_chunks, n_sizes, fixed_size; ++ bool co64; ++ int count = 0; ++ ++ if (!atom_path(trak, mdhd_path, 2, &mdhd) || ++ !atom_path(trak, stbl_path, 3, &stbl)) ++ return 0; ++ rd_seek(mdhd.data); ++ rd_seek(mdhd.data + (rd_u8() == 1 ? 4 + 16 : 4 + 8)); ++ timescale = rd_u32(); ++ ++ if (!atom_find(stbl.data, stbl.end, FOURCC('s','t','t','s'), &stts) || ++ !atom_find(stbl.data, stbl.end, FOURCC('s','t','s','c'), &stsc) || ++ !atom_find(stbl.data, stbl.end, FOURCC('s','t','s','z'), &stsz)) ++ return 0; ++ co64 = !atom_find(stbl.data, stbl.end, FOURCC('s','t','c','o'), &stco); ++ if (co64 && ++ !atom_find(stbl.data, stbl.end, FOURCC('c','o','6','4'), &stco)) ++ return 0; ++ if (!timescale || rd.err) ++ return 0; ++ ++ /* start times: the sample durations added up */ ++ rd_seek(stts.data + 4); ++ n_stts = rd_u32(); ++ uint64_t t = 0; ++ for (uint32_t e = 0; e < n_stts && count < CHAPTERS_MAX && !rd.err; e++) ++ { ++ uint32_t n = rd_u32(), delta = rd_u32(); ++ for (uint32_t k = 0; k < n && count < CHAPTERS_MAX; k++) ++ { ++ ch_start[count++] = (uint32_t)(t * 1000 / timescale); ++ t += delta; ++ } ++ } ++ ++ /* where each sample (title) is: chunks from stsc/stco, sizes from stsz */ ++ rd_seek(stsz.data + 4); ++ fixed_size = rd_u32(); ++ n_sizes = rd_u32(); ++ rd_seek(stsc.data + 4); ++ n_stsc = rd_u32(); ++ rd_seek(stco.data + 4); ++ n_chunks = rd_u32(); ++ if (rd.err || !n_stsc) ++ return 0; ++ if (!fixed_size && (uint32_t)count > n_sizes) ++ count = n_sizes; ++ ++ /* stsc: runs of chunks (first chunk, samples per chunk, description) */ ++ uint32_t stsc_i = 0, per_chunk = 0, next_first = 0; ++ int s = 0; ++ for (uint32_t chunk = 1; chunk <= n_chunks && s < count; chunk++) ++ { ++ while (chunk >= next_first && stsc_i < n_stsc) ++ { ++ /* entry stsc_i starts here: take its count, find the next */ ++ rd_seek(stsc.data + 8 + 12 * stsc_i + 4); ++ per_chunk = rd_u32(); ++ stsc_i++; ++ if (stsc_i < n_stsc) ++ { ++ rd_seek(stsc.data + 8 + 12 * stsc_i); ++ next_first = rd_u32(); ++ } ++ else ++ next_first = UINT32_MAX; ++ } ++ ++ uint64_t off; ++ if (co64) ++ { ++ rd_seek(stco.data + 8 + 8 * (chunk - 1)); ++ off = rd_u64(); ++ } ++ else ++ { ++ rd_seek(stco.data + 8 + 4 * (chunk - 1)); ++ off = rd_u32(); ++ } ++ for (uint32_t k = 0; k < per_chunk && s < count; k++, s++) ++ { ++ uint32_t size = fixed_size; ++ if (!size) ++ { ++ rd_seek(stsz.data + 12 + 4 * s); ++ size = rd_u32(); ++ } ++ ch_title[s] = (off < UINT32_MAX && size >= 2) ? (uint32_t)off : 0; ++ off += size; ++ } ++ if (rd.err) ++ return 0; ++ } ++ for (; s < count; s++) ++ ch_title[s] = 0; ++ ch_qt_titles = true; ++ return count; ++} ++ ++static int read_chapters(const char *path) ++{ ++ struct atom moov, a; ++ uint32_t pos = 0, size; ++ int count = 0; ++ ++ memset(&rd, 0, sizeof(rd)); ++ rd.fd = open(path, O_RDONLY); ++ if (rd.fd < 0) ++ return 0; ++ off_t fsize = ffilesize(rd.fd); ++ size = fsize > 0 ? (uint32_t)fsize : 0; ++ ++ /* the top level: mdat is skipped by its size, moov may come after it */ ++ bool found = false; ++ while (atom_next(&pos, size, &moov)) ++ { ++ if (moov.type == FOURCC('m','o','o','v')) ++ { ++ found = true; ++ break; ++ } ++ } ++ ++ if (found) ++ { ++ static const uint32_t chpl_path[] = ++ { FOURCC('u','d','t','a'), FOURCC('c','h','p','l') }; ++ if (atom_path(&moov, chpl_path, 2, &a)) ++ count = read_chpl(&a); ++ ++ if (count == 0) ++ { ++ uint32_t id = chapter_track_id(&moov); ++ uint32_t p = moov.data; ++ struct atom trak; ++ while (id && atom_next(&p, moov.end, &trak)) ++ { ++ if (trak.type == FOURCC('t','r','a','k') && trak_id(&trak) == id) ++ { ++ count = read_chapter_track(&trak); ++ break; ++ } ++ } ++ } ++ } ++ close(rd.fd); ++ ++ /* sane: from 0, in order */ ++ for (int i = 1; i < count; i++) ++ { ++ if (ch_start[i] < ch_start[i - 1]) ++ { ++ count = i; ++ break; ++ } ++ } ++ if (count > 0) ++ ch_start[0] = 0; ++ logf("chapters: %d in %s", count, path); ++ return count; ++} ++ ++/* --- interface --- */ ++ ++int chapters_update(const struct mp3entry *id3) ++{ ++ if (!id3 || !id3->path[0]) ++ return 0; ++ if (!strcmp(id3->path, ch_path)) ++ return ch_count; ++ ++ strmemccpy(ch_path, id3->path, sizeof(ch_path)); ++ ch_count = 0; ++ ch_last_index = -1; ++ ch_title_index = -1; ++ switch (id3->codectype) ++ { ++ case AFMT_MP4_AAC: ++ case AFMT_MP4_AAC_HE: ++ case AFMT_MP4_ALAC: ++ ch_count = read_chapters(id3->path); ++ break; ++ default: ++ break; ++ } ++ return ch_count; ++} ++ ++int chapters_index(unsigned long elapsed) ++{ ++ int lo = 0, hi = ch_count - 1; ++ if (ch_count <= 0) ++ return -1; ++ while (lo < hi) /* the last chapter starting at or before elapsed */ ++ { ++ int mid = (lo + hi + 1) / 2; ++ if (ch_start[mid] <= elapsed) ++ lo = mid; ++ else ++ hi = mid - 1; ++ } ++ return lo; ++} ++ ++unsigned long chapters_start(int i) ++{ ++ return (i >= 0 && i < ch_count) ? ch_start[i] : 0; ++} ++ ++/* Cut a UTF-8 string after its last whole character. */ ++static void utf8_trim(char *s, size_t len) ++{ ++ size_t i = len; ++ while (i > 0 && ((unsigned char)s[i - 1] & 0xc0) == 0x80) ++ i--; /* continuation bytes */ ++ if (i > 0 && ((unsigned char)s[i - 1] & 0x80)) ++ { ++ unsigned char lead = s[i - 1]; ++ size_t need = lead >= 0xf0 ? 4 : lead >= 0xe0 ? 3 : 2; ++ if (len - (i - 1) < need) ++ len = i - 1; /* a cut-off last character */ ++ } ++ s[len] = '\0'; ++} ++ ++const char *chapters_title(int i, char *buf, size_t size) ++{ ++ static unsigned char raw[TITLE_MAX]; ++ int fd; ++ ++ if (!size) ++ return buf; ++ buf[0] = '\0'; ++ if (i >= 0 && i < ch_count && ch_title[i] && ++ (fd = open(ch_path, O_RDONLY)) >= 0) ++ { ++ unsigned char lenbuf[2]; ++ int lenbytes = ch_qt_titles ? 2 : 1; ++ if (lseek(fd, ch_title[i], SEEK_SET) == (off_t)ch_title[i] && ++ read(fd, lenbuf, lenbytes) == lenbytes) ++ { ++ int len = ch_qt_titles ? (lenbuf[0] << 8 | lenbuf[1]) : lenbuf[0]; ++ len = MIN(len, (int)sizeof(raw)); ++ len = read(fd, raw, len); ++ if (len >= 2 && ((raw[0] == 0xfe && raw[1] == 0xff) || ++ (raw[0] == 0xff && raw[1] == 0xfe))) ++ { ++ /* UTF-16 with a byte order mark */ ++ bool be = raw[0] == 0xfe; ++ unsigned char *out = (unsigned char *)buf; ++ unsigned char *end = out + size - 1; ++ for (int k = 2; k + 1 < len; k += 2) ++ { ++ unsigned long c = be ? (raw[k] << 8 | raw[k + 1]) ++ : (raw[k + 1] << 8 | raw[k]); ++ if (c >= 0xd800 && c < 0xe000) ++ c = '?'; /* no surrogate pairs here */ ++ if (end - out < 3) ++ break; ++ out = utf8encode(c, out); ++ } ++ *out = '\0'; ++ } ++ else if (len > 0) ++ { ++ len = MIN(len, (int)size - 1); ++ memcpy(buf, raw, len); ++ utf8_trim(buf, len); ++ } ++ } ++ close(fd); ++ } ++ /* trim spaces and line ends some tools leave */ ++ size_t n = strlen(buf); ++ while (n && (buf[n - 1] == ' ' || buf[n - 1] == '\r' || buf[n - 1] == '\n')) ++ buf[--n] = '\0'; ++ if (!buf[0]) ++ snprintf(buf, size, "Chapter %d", i + 1); ++ return buf; ++} ++ ++const char *chapters_current_title(const struct mp3entry *id3) ++{ ++ if (chapters_update(id3) <= 0) ++ return NULL; ++ int i = chapters_index(id3->elapsed); ++ if (i != ch_title_index) ++ { ++ chapters_title(i, cur_title, sizeof(cur_title)); ++ ch_title_index = i; ++ } ++ return cur_title; ++} ++ ++bool chapters_changed(const struct mp3entry *id3) ++{ ++ if (chapters_update(id3) <= 0) ++ return false; ++ int i = chapters_index(id3->elapsed); ++ if (i == ch_last_index) ++ return false; ++ ch_last_index = i; ++ return true; ++} ++ ++void chapters_draw_markers(struct screen *display, unsigned long length, ++ int x, int y, int w, int h) ++{ ++ unsigned long secs = length / 1000; ++ if (ch_count < 2 || !secs) ++ return; ++ display->set_drawmode(DRMODE_COMPLEMENT); ++ for (int i = 1; i < ch_count; i++) ++ { ++ unsigned long at = ch_start[i] / 1000; ++ if (at >= secs) ++ break; ++ display->vline(x + (int)((unsigned long long)w * at / secs), y, y + h - 1); ++ } ++} ++ ++static void seek_to(unsigned long ms) ++{ ++ audio_pre_ff_rewind(); ++ audio_ff_rewind(ms); ++} ++ ++void chapters_skip(const struct mp3entry *id3, int direction, ++ unsigned long threshold) ++{ ++ if (!id3) ++ return; ++ ++ if (chapters_update(id3) > 0) ++ { ++ unsigned long elapsed = id3->elapsed; ++ int i = chapters_index(elapsed); ++ if (direction < 0) ++ { ++ /* restart this chapter, or the one before at its start */ ++ if (elapsed < ch_start[i] + threshold && i > 0) ++ i--; ++ seek_to(ch_start[i]); ++ } ++ else if (i + 1 < ch_count) ++ seek_to(ch_start[i + 1]); ++ else ++ system_sound_play(SOUND_TRACK_NO_MORE); /* stay in this book */ ++ return; ++ } ++ ++ /* no chapters inside: a book in several files, one per chapter */ ++ if (direction < 0) ++ { ++ if (id3->elapsed < threshold && playlist_check(-1)) ++ audio_prev(); ++ else ++ seek_to(0); ++ } ++ else if (playlist_check(1)) ++ audio_next(); ++ else ++ system_sound_play(SOUND_TRACK_NO_MORE); ++} ++ ++static const char *chapter_list_name(int item, void *data, ++ char *buf, size_t size) ++{ ++ (void)data; ++ char title[TITLE_MAX + 1]; ++ unsigned long s = ch_start[item] / 1000; ++ chapters_title(item, title, sizeof(title)); ++ if (s >= 3600) ++ snprintf(buf, size, "%lu:%02lu:%02lu %s", ++ s / 3600, s / 60 % 60, s % 60, title); ++ else ++ snprintf(buf, size, "%lu:%02lu %s", s / 60, s % 60, title); ++ return buf; ++} ++ ++int chapters_browse(void) ++{ ++ struct mp3entry *id3 = audio_current_track(); ++ struct simplelist_info info; ++ char title[32]; ++ ++ if (chapters_update(id3) <= 0) ++ return 0; ++ snprintf(title, sizeof(title), "Chapters (%d)", ch_count); ++ simplelist_info_init(&info, title, ch_count, NULL); ++ info.get_name = chapter_list_name; ++ info.selection = chapters_index(id3->elapsed); ++ info.hide_theme = false; ++ simplelist_show_list(&info); ++ if (info.selection < 0 || info.selection >= ch_count) ++ return 0; ++ seek_to(ch_start[info.selection]); ++ return GO_TO_WPS; ++} +diff --git a/apps/chapters.h b/apps/chapters.h +new file mode 100644 +index 00000000..14e4d709 +--- /dev/null ++++ b/apps/chapters.h +@@ -0,0 +1,38 @@ ++/* SPDX-License-Identifier: GPL-2.0-or-later */ ++/* Chapters inside the playing audiobook file (MP4/M4B: a Nero 'chpl' list ++ * or a QuickTime chapter text track). See chapters.c. */ ++#ifndef _CHAPTERS_H_ ++#define _CHAPTERS_H_ ++ ++#include <stdbool.h> ++#include <stddef.h> ++#include "metadata.h" ++ ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++struct screen; ++ ++/* Chapters of this track, read from its file the first time it is asked ++ * for (UI thread only: file I/O). 0: none. */ ++int chapters_update(const struct mp3entry *id3); ++/* The chapter playing at elapsed (ms) of the loaded track. */ ++int chapters_index(unsigned long elapsed); ++unsigned long chapters_start(int i); ++/* Chapter i's title (from the file; "Chapter N" without one) in buf. */ ++const char *chapters_title(int i, char *buf, size_t size); ++/* The playing chapter's title, cached. */ ++const char *chapters_current_title(const struct mp3entry *id3); ++/* True once each time the playing chapter changes (skin refresh). */ ++bool chapters_changed(const struct mp3entry *id3); ++/* Ticks at the chapter starts on a progress bar. */ ++void chapters_draw_markers(struct screen *display, unsigned long length, ++ int x, int y, int w, int h); ++/* Previous (-1) / next (1) chapter of the playing track; without chapters, ++ * the previous / next file. Previous restarts the chapter unless it began ++ * less than threshold ms ago. */ ++void chapters_skip(const struct mp3entry *id3, int direction, ++ unsigned long threshold); ++/* The chapter list (WPS context menu): GO_TO_WPS after a jump, else 0. */ ++int chapters_browse(void); ++#endif ++ ++#endif /* _CHAPTERS_H_ */ +diff --git a/apps/gui/skin_engine/skin_display.c b/apps/gui/skin_engine/skin_display.c +index a2ae149c..6828b536 100644 +--- a/apps/gui/skin_engine/skin_display.c ++++ b/apps/gui/skin_engine/skin_display.c +@@ -55,6 +55,7 @@ + #endif + + #include "cuesheet.h" ++#include "chapters.h" + #include "playback.h" + #include "backdrop.h" + #include "viewport.h" +@@ -96,6 +97,11 @@ void skin_update(enum skinnable_screens skin, enum screen_type screen, + * This is also safe for skined screen which dont use the id3 */ + struct mp3entry *id3 = get_wps_state()->id3; + bool cuesheet_update = (id3 != NULL ? cuesheet_subtrack_changed(id3) : false); ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++ /* a new chapter title */ ++ if (skin == WPS && id3 != NULL && chapters_changed(id3)) ++ cuesheet_update = true; ++#endif + if (cuesheet_update) + skin_request_full_update(skin); + +@@ -381,6 +387,11 @@ void draw_progressbar(struct gui_wps *gwps, struct skin_viewport* skin_viewport, + if (id3->cuesheet) + cue_draw_markers(display, id3->cuesheet, id3->length, + x, y+1, width, height-2); ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++ else if (id3 == get_wps_state()->id3 && chapters_update(id3) > 1) ++ chapters_draw_markers(display, id3->length, ++ x, y+1, width, height-2); ++#endif + } + #if 0 /* disable for now CONFIG_TUNER */ + else if (in_radio_screen() || (get_radio_status() != FMRADIO_OFF)) +diff --git a/apps/gui/skin_engine/skin_tokens.c b/apps/gui/skin_engine/skin_tokens.c +index e80d9550..98d854ce 100644 +--- a/apps/gui/skin_engine/skin_tokens.c ++++ b/apps/gui/skin_engine/skin_tokens.c +@@ -34,6 +34,7 @@ + #include "sound.h" + #include "debug.h" + #include "cuesheet.h" ++#include "chapters.h" + #include "replaygain.h" + #include "core_alloc.h" + #include "abrepeat.h" +@@ -849,6 +850,35 @@ static const char *try_id3_token(struct wps_token *token, int offset, + } + + struct wps_state *state = get_wps_state(); ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++ /* An audiobook with chapters: the title is the chapter's, the track ++ number the chapter's of all, the album the book's if it has none */ ++ if (id3 && id3 == state->id3 && !token->next && offset == 0 && ++ chapters_update(id3) > 0) ++ { ++ out_text = NULL; ++ switch (token->type) ++ { ++ case SKIN_TOKEN_METADATA_TRACK_TITLE: ++ out_text = chapters_current_title(id3); ++ break; ++ case SKIN_TOKEN_METADATA_TRACK_NUMBER: ++ snprintf(buf, buf_size, "%d/%d", ++ chapters_index(id3->elapsed) + 1, ++ chapters_update(id3)); ++ out_text = buf; ++ break; ++ case SKIN_TOKEN_METADATA_ALBUM: ++ if (!id3->album || !*id3->album) ++ out_text = id3->title; ++ break; ++ default: ++ break; ++ } ++ if (out_text) ++ goto free_id3_outtext; ++ } ++#endif + if (id3 && id3 == state->id3 && id3->cuesheet) + { + out_text = get_cuesheetid3_token(token, id3, +diff --git a/apps/gui/wps.c b/apps/gui/wps.c +index 12fda688..714a44a4 100644 +--- a/apps/gui/wps.c ++++ b/apps/gui/wps.c +@@ -48,6 +48,7 @@ + #include "playback.h" + #include "splash.h" + #include "cuesheet.h" ++#include "chapters.h" + #include "ata_idle_notify.h" + #include "root_menu.h" + #include "backdrop.h" +@@ -988,6 +989,15 @@ long gui_wps_show(void) + change_dir(-1); + } + break; ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++ case ACTION_WPS_CHAPTERPREV: ++ case ACTION_WPS_CHAPTERNEXT: ++ chapters_skip(state->id3, ++ button == ACTION_WPS_CHAPTERNEXT ? 1 : -1, ++ DEFAULT_SKIP_THRESH); ++ update = true; ++ break; ++#endif + /* menu key functions */ + case ACTION_WPS_MENU: + gwps_leave_wps(true); +diff --git a/apps/keymaps/keymap-yp3.c b/apps/keymaps/keymap-yp3.c +index 09aa33ca..444bd47c 100644 +--- a/apps/keymaps/keymap-yp3.c ++++ b/apps/keymaps/keymap-yp3.c +@@ -24,7 +24,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. */ ++/* 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. */ + static const struct button_mapping wps[] = { + { ACTION_WPS_PLAY, BUTTON_SELECT|BUTTON_REL, BUTTON_SELECT }, + { ACTION_WPS_CONTEXT, BUTTON_SELECT|BUTTON_REPEAT, BUTTON_SELECT }, +@@ -37,7 +40,13 @@ static const struct button_mapping wps[] = { + BUTTON_RIGHT|BUTTON_REPEAT }, + { ACTION_WPS_VOLUP, BUTTON_UP|BUTTON_REL, BUTTON_UP }, + { ACTION_WPS_STOP, BUTTON_UP|BUTTON_REPEAT, BUTTON_UP }, ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++ { ACTION_WPS_VOLDOWN, BUTTON_DOWN|BUTTON_REL, BUTTON_DOWN }, ++ { ACTION_WPS_CHAPTERPREV, BUTTON_DOWN|BUTTON_LEFT, BUTTON_NONE }, ++ { ACTION_WPS_CHAPTERNEXT, BUTTON_DOWN|BUTTON_RIGHT, BUTTON_NONE }, ++#else + { ACTION_WPS_VOLDOWN, BUTTON_DOWN, BUTTON_NONE }, ++#endif + { ACTION_WPS_VOLDOWN, BUTTON_DOWN|BUTTON_REPEAT, BUTTON_NONE }, + { ACTION_WPS_MENU, BUTTON_UP|BUTTON_LEFT, BUTTON_NONE }, + LAST_ITEM_IN_LIST +diff --git a/apps/onplay.c b/apps/onplay.c +index 04278fe7..384a8376 100644 +--- a/apps/onplay.c ++++ b/apps/onplay.c +@@ -57,6 +57,7 @@ + #include "tagtree.h" + #endif + #include "cuesheet.h" ++#include "chapters.h" + #include "statusbar-skinned.h" + #include "pitchscreen.h" + #include "viewport.h" +@@ -839,6 +840,24 @@ static int view_cue_item_callback(int action, + MENUITEM_FUNCTION(view_cue_item, 0, ID2P(LANG_BROWSE_CUESHEET), + view_cue, view_cue_item_callback, Icon_NOICON); + ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++/* The playing audiobook's chapters, first in the context menu */ ++static int chapters_item_callback(int action, ++ const struct menu_item_ex *this_item, ++ struct gui_synclist *this_list) ++{ ++ (void)this_item; ++ (void)this_list; ++ if (action == ACTION_REQUEST_MENUITEM && ++ chapters_update(audio_current_track()) <= 0) ++ return ACTION_EXIT_MENUITEM; ++ return action; ++} ++MENUITEM_FUNCTION(chapters_item, MENU_FUNC_CHECK_RETVAL, ++ (unsigned char *)"Chapters", ++ chapters_browse, chapters_item_callback, Icon_NOICON); ++#endif ++ + + static int browse_id3_wrapper(void) + { +@@ -1250,6 +1269,9 @@ static int onplaymenu_callback(int action, + /* used when onplay() is called in the CONTEXT_WPS context */ + MAKE_ONPLAYMENU( wps_onplay_menu, ID2P(LANG_ONPLAY_MENU_TITLE), + onplaymenu_callback, Icon_Audio, ++#ifdef HAVE_AUDIOBOOK_CHAPTERS ++ &chapters_item, ++#endif + &wps_playlist_menu, &cat_playlist_menu, + &sound_settings, &playback_settings, + #ifdef HAVE_TAGCACHE +diff --git a/apps/settings_list.c b/apps/settings_list.c +index 4f5e2f58..b029aab0 100644 +--- a/apps/settings_list.c ++++ b/apps/settings_list.c +@@ -2521,7 +2521,9 @@ const struct settings_list settings[] = { + list_kinetic_load_from_cfg, list_kinetic_write_to_cfg, + list_kinetic_is_default, list_kinetic_set_default), + #endif +- OFFON_SETTING(0, prevent_skip, LANG_PREVENT_SKIPPING, false, "prevent track skip", NULL), ++ /* audiobook: Left/Right never leave the book (the chapter keys do) */ ++ OFFON_SETTING(0, prevent_skip, LANG_PREVENT_SKIPPING, ++ AUDIOBOOK_DEFAULT(true, false), "prevent track skip", NULL), + OFFON_SETTING(0, rewind_across_tracks, LANG_REWIND_ACROSS_TRACKS, false, "rewind across tracks", NULL), + #ifdef HAVE_PITCHCONTROL + OFFON_SETTING(0, pitch_mode_semitone, LANG_SEMITONE, false, +diff --git a/firmware/export/config/yp3.h b/firmware/export/config/yp3.h +index 07614b6b..d3e5742d 100644 +--- a/firmware/export/config/yp3.h ++++ b/firmware/export/config/yp3.h +@@ -257,6 +257,11 @@ + * (apps/playback.c, bookmark.c, filetree.c, settings_list.c). */ + #define HAVE_AUDIOBOOK_RESUME + #define AUDIOBOOK_RESUME_SAVE_TICKS (60 * HZ) ++/* Chapters inside M4B/MP4 files (apps/chapters.c): Down+Left / Down+Right ++ * in Now Playing go to the previous / next chapter (or file, in a book ++ * without them), the title shows the chapter, the progress bar marks ++ * them, and the context menu lists them. */ ++#define HAVE_AUDIOBOOK_CHAPTERS + /* No surround: its 31 KB delay buffer comes out of the playback buffer, + * which then stayed shrunk (stutter until a reset) */ + #define HAVE_NO_SURROUND +-- +2.56.1 + diff --git a/patches/0003-yp3-next-chapter-title-in-the-next-track-tokens.patch b/patches/0003-yp3-next-chapter-title-in-the-next-track-tokens.patch new file mode 100644 index 0000000..2613f36 --- /dev/null +++ b/patches/0003-yp3-next-chapter-title-in-the-next-track-tokens.patch @@ -0,0 +1,90 @@ +From 4a31d205984cb0ffa1d2381213b09d695adbd49d Mon Sep 17 00:00:00 2001 +From: YP3 Audiobook <noreply@example.invalid> +Date: Wed, 25 Nov 2026 20:39:34 +0000 +Subject: [PATCH 3/3] yp3: next chapter title in the next-track tokens + +--- + apps/chapters.c | 18 ++++++++++++++++++ + apps/chapters.h | 2 ++ + apps/gui/skin_engine/skin_tokens.c | 9 +++++++++ + 3 files changed, 29 insertions(+) + +diff --git a/apps/chapters.c b/apps/chapters.c +index 9b483653..d8306f5b 100644 +--- a/apps/chapters.c ++++ b/apps/chapters.c +@@ -47,6 +47,8 @@ static char ch_path[MAX_PATH]; /* the file they are from */ + static int ch_last_index = -1; /* chapters_changed() */ + static int ch_title_index = -1; /* cur_title holds this one */ + static char cur_title[TITLE_MAX + 1]; ++static int ch_next_index = -1; /* next_title holds this one */ ++static char next_title[TITLE_MAX + 1]; + + /* --- a small buffered reader: the atoms are walked with many tiny reads --- */ + +@@ -418,6 +420,7 @@ int chapters_update(const struct mp3entry *id3) + ch_count = 0; + ch_last_index = -1; + ch_title_index = -1; ++ ch_next_index = -1; + switch (id3->codectype) + { + case AFMT_MP4_AAC: +@@ -537,6 +540,21 @@ const char *chapters_current_title(const struct mp3entry *id3) + return cur_title; + } + ++const char *chapters_next_title(const struct mp3entry *id3) ++{ ++ if (chapters_update(id3) <= 0) ++ return NULL; ++ int i = chapters_index(id3->elapsed) + 1; ++ if (i >= ch_count) ++ return NULL; ++ if (i != ch_next_index) ++ { ++ chapters_title(i, next_title, sizeof(next_title)); ++ ch_next_index = i; ++ } ++ return next_title; ++} ++ + bool chapters_changed(const struct mp3entry *id3) + { + if (chapters_update(id3) <= 0) +diff --git a/apps/chapters.h b/apps/chapters.h +index 14e4d709..3bc633c6 100644 +--- a/apps/chapters.h ++++ b/apps/chapters.h +@@ -21,6 +21,8 @@ unsigned long chapters_start(int i); + const char *chapters_title(int i, char *buf, size_t size); + /* The playing chapter's title, cached. */ + const char *chapters_current_title(const struct mp3entry *id3); ++/* The next chapter's title, cached; NULL in the last chapter. */ ++const char *chapters_next_title(const struct mp3entry *id3); + /* True once each time the playing chapter changes (skin refresh). */ + bool chapters_changed(const struct mp3entry *id3); + /* Ticks at the chapter starts on a progress bar. */ +diff --git a/apps/gui/skin_engine/skin_tokens.c b/apps/gui/skin_engine/skin_tokens.c +index 98d854ce..4c1fbffb 100644 +--- a/apps/gui/skin_engine/skin_tokens.c ++++ b/apps/gui/skin_engine/skin_tokens.c +@@ -878,6 +878,15 @@ static const char *try_id3_token(struct wps_token *token, int offset, + if (out_text) + goto free_id3_outtext; + } ++ /* "next track" title: the next chapter's, until the last chapter */ ++ else if ((token->next || offset == 1) && state->id3 && ++ token->type == SKIN_TOKEN_METADATA_TRACK_TITLE && ++ chapters_update(state->id3) > 0) ++ { ++ out_text = chapters_next_title(state->id3); ++ if (out_text) ++ goto free_id3_outtext; ++ } + #endif + if (id3 && id3 == state->id3 && id3->cuesheet) + { +-- +2.56.1 + diff --git a/release/rockbox-yp3-audiobook-1.0.tar.xz b/release/rockbox-yp3-audiobook-1.0.tar.xz Binary files differnew file mode 100644 index 0000000..ef511ce --- /dev/null +++ b/release/rockbox-yp3-audiobook-1.0.tar.xz diff --git a/release/rockbox-yp3-audiobook-1.0.tar.xz.sha256 b/release/rockbox-yp3-audiobook-1.0.tar.xz.sha256 new file mode 100644 index 0000000..ea41a00 --- /dev/null +++ b/release/rockbox-yp3-audiobook-1.0.tar.xz.sha256 @@ -0,0 +1 @@ +a6ff3e99d5d53a5ae95b331683e4b1e8d404f2c1b188b4fd21f43c4859ad1248 rockbox-yp3-audiobook-1.0.tar.xz diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..6b7ff67 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,79 @@ +#!/bin/sh +# Rebuild the firmware package from the sources in this repository: +# vendor/rockbox-822b9f5e.tar.xz upstream YP3 Rockbox (jaydens-tasty-slop/rockbox) +# patches/*.patch the audiobook changes +# vendor/yp3-assets-1.tar.xz themes and models the upstream build adds +# Downloaded: the Arm GNU toolchain (~150 MB), pinned by sha256 in the +# upstream tools/yp3/release.sh and cached in $YP3_CACHE; and rockslop's +# source at ROCKSLOP_COMMIT for its packager, packaging/mkpkg.py (or set +# MKPKG to a mkpkg.py you have). +# +# scripts/build.sh [VERSION] default VERSION: audiobook-dev +# +# Everything goes in build/ (not in git; safe to delete): +# build/rockbox/ the patched source, a git repository +# build/rockslop/ rockslop, for its mkpkg.py +# build/out/ the package: rockbox-yp3-VERSION.tar.xz (+ .sha256) +# Copy the package to release/ when it has been tested on a player. +# +# Needs: sh, tar, xz, bzip2, zip, git, make, gcc (host tools), perl, python3, curl. +set -eu +die() { echo "build.sh: $*" >&2; exit 1; } + +REPO=$(cd "$(dirname "$0")/.." && pwd) +VERSION=${1:-audiobook-dev} +UPSTREAM=822b9f5e999122bc25738fc9746bb7a17bb48fa6 +# Upstream's commit time: the build's SOURCE_DATE_EPOCH base +UPSTREAM_DATE="2026-11-21T18:01:21-0700" +ROCKSLOP_URL=https://github.com/jaydens-tasty-slop/rockslop.git +ROCKSLOP_COMMIT=8715cfc99d392de62dd30ce06dc9703098e22c0f +W=$REPO/build # keep short: see below +CACHE=${YP3_CACHE:-$HOME/.cache/yp3-audiobook} + +for t in tar xz bzip2 zip git make gcc perl python3 curl sha256sum; do + command -v "$t" >/dev/null || die "needs $t" +done +(cd "$REPO/vendor" && sha256sum -c --quiet SHA256SUMS) || die "vendor/ checksums" + +# The upstream build reaches the tree through a symlink and breaks +# ("Argument list too long") when it, or the output path, is long. +case ${#W} in [0-9]|[1-5][0-9]) ;; *) + die "repository path too long for the build (${#W} chars in $W): clone it to a shorter path" ;; +esac + +rm -rf "$W/rockbox" "$W/rockslop" "$W/out" "$W/s" +mkdir -p "$W/rockbox" "$W/rockslop" "$W/out" + +# upstream tree as a git repository (the build reads versions from git), +# then the patches on top +tar -xJf "$REPO/vendor/rockbox-822b9f5e.tar.xz" -C "$W/rockbox" +( + cd "$W/rockbox" + export GIT_AUTHOR_NAME=upstream GIT_AUTHOR_EMAIL=upstream@localhost + export GIT_COMMITTER_NAME=upstream GIT_COMMITTER_EMAIL=upstream@localhost + export GIT_AUTHOR_DATE=$UPSTREAM_DATE GIT_COMMITTER_DATE=$UPSTREAM_DATE + git init -q + git add -A + git commit -q -m "upstream jaydens-tasty-slop/rockbox $UPSTREAM" + git am -q --committer-date-is-author-date "$REPO"/patches/*.patch +) +# rockslop's packager (its package format is the one the flasher reads) +if [ -z "${MKPKG:-}" ]; then + git -C "$W/rockslop" init -q + git -C "$W/rockslop" fetch -q --depth 1 "$ROCKSLOP_URL" "$ROCKSLOP_COMMIT" || + die "could not fetch rockslop $ROCKSLOP_COMMIT (or set MKPKG)" + git -C "$W/rockslop" checkout -q FETCH_HEAD + MKPKG=$W/rockslop/packaging/mkpkg.py +fi +[ -f "$MKPKG" ] || die "no mkpkg.py at $MKPKG" + +echo "building $VERSION (log: $W/out/build.log)" +MKPKG=$(realpath "$MKPKG") \ +YP3_ASSETS="file://$REPO/vendor/yp3-assets-1.tar.xz" \ +YP3_ASSETS_SHA256=$(sed -n 's/ yp3-assets-1.tar.xz$//p' "$REPO/vendor/SHA256SUMS") \ +YP3_SRC_LINK="$W/s" YP3_CACHE="$CACHE" \ + "$W/rockbox/tools/yp3/release.sh" "$VERSION" "$W/out" + +echo +echo "package: $W/out/rockbox-yp3-$VERSION.tar.xz" +echo "check: tools/rockslop-cli check $W/out/rockbox-yp3-$VERSION.tar.xz" diff --git a/tests/chapters/run.sh b/tests/chapters/run.sh new file mode 100755 index 0000000..0bb70b3 --- /dev/null +++ b/tests/chapters/run.sh @@ -0,0 +1,86 @@ +#!/bin/sh +# Host test of apps/chapters.c (the M4B chapter reader) against audiobooks +# made with ffmpeg: Nero 'chpl' chapters, QuickTime chapter tracks only, +# the index (moov) at the end or the start, UTF-8 / long / empty titles, +# and more chapters than the 255 kept. Built with AddressSanitizer and +# UBSan; the shim/ headers stand in for the Rockbox ones. +# +# tests/chapters/run.sh [ROCKBOX_TREE] default: build/rockbox (scripts/build.sh) +# +# Needs: gcc, ffmpeg, python3. +set -eu +HERE=$(cd "$(dirname "$0")" && pwd) +REPO=$(cd "$HERE/../.." && pwd) +TREE=${1:-$REPO/build/rockbox} +[ -f "$TREE/apps/chapters.c" ] || { + echo "no $TREE/apps/chapters.c: run scripts/build.sh first, or name a patched tree" >&2 + exit 1 +} +T=$(mktemp -d) +trap 'rm -rf "$T"' EXIT + +# chapters.c includes by quotes: compile copies next to the shims +mkdir -p "$T/src" "$T/media" +cp "$HERE"/shim/*.h "$TREE/apps/chapters.c" "$TREE/apps/chapters.h" "$T/src/" +gcc -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=all \ + -Wall -Wextra -Wno-unused-parameter -I"$T/src" \ + -o "$T/chtest" "$HERE/test.c" "$T/src/chapters.c" + +cd "$T/media" +python3 -I - <<'EOF' +def meta(name, chapters, total_ms): + with open(name, 'w') as f: + f.write(";FFMETADATA1\ntitle=Test Book\nartist=Some Author\n") + for i, (start, title) in enumerate(chapters): + end = chapters[i + 1][0] if i + 1 < len(chapters) else total_ms + f.write(f"[CHAPTER]\nTIMEBASE=1/1000\nSTART={start}\nEND={end}\ntitle={title}\n") +meta('basic.txt', [(0, 'Opening Credits'), (65000, 'Chapter One: The Beginning'), + (190500, 'Chäpter Twö – ünïcode ✓ 日本語'), (400000, 'X' * 200), + (500000, '')], 600000) +meta('many.txt', [(i * 2000, f'Part {i + 1}') for i in range(300)], 600000) +EOF +enc() { # metadata output [movflags] + ffmpeg -nostdin -loglevel error -f lavfi -i "sine=frequency=440:duration=600" \ + -i "$1" -map_metadata 1 -map_chapters 1 -c:a aac -b:a 32k -ac 1 \ + ${3:+-movflags "$3"} -y "$2" +} +for f in basic many; do + enc $f.txt $f.m4b # chpl + chapter track + enc $f.txt $f-qt.m4b +disable_chpl # chapter track only + enc $f.txt $f-qt-fast.m4b +faststart+disable_chpl # ... index first +done +ffmpeg -nostdin -loglevel error -f lavfi -i "sine=frequency=440:duration=60" \ + -c:a aac -b:a 32k -y nochap.m4b + +"$T/chtest" basic.m4b basic-qt.m4b basic-qt-fast.m4b many.m4b many-qt.m4b \ + many-qt-fast.m4b nochap.m4b /nonexistent.m4b > "$T/out.txt" +cat "$T/out.txt" + +# expected results +fail=0 +expect() { # file-section pattern + if ! awk -v f="== $1:" -v p="$2" ' + index($0, "== ") == 1 { on = (index($0, f) == 1) } + on && index($0, p) { found = 1 } END { exit !found }' "$T/out.txt"; then + echo "FAIL: $1: expected \"$2\""; fail=1 + fi +} +for f in basic.m4b basic-qt.m4b basic-qt-fast.m4b; do + expect $f "5 chapters" + expect $f "65000 ms [Chapter One: The Beginning]" + expect $f "190500 ms [Chäpter Twö – ünïcode ✓ 日本語]" + expect $f "500000 ms [Chapter 5]" + expect $f "prev 1s into ch2 -> 0" + expect $f "prev 10s into ch2 -> 65000" + expect $f "next from ch2 -> 190500" + expect $f "next from last -> nomore=1" + expect $f "next title [XXXX" +done +for f in many.m4b many-qt.m4b many-qt-fast.m4b; do + expect $f "255 chapters" + expect $f "508000 ms [Part 255]" +done +expect nochap.m4b "0 chapters" +expect nochap.m4b "prevs=1 next->nexts=1 prev@10s->seek 0" +expect /nonexistent.m4b "0 chapters" +[ $fail = 0 ] && echo "PASS" || { echo "FAILED"; exit 1; } diff --git a/tests/chapters/shim/audio.h b/tests/chapters/shim/audio.h new file mode 100644 index 0000000..4d27bcb --- /dev/null +++ b/tests/chapters/shim/audio.h @@ -0,0 +1,3 @@ +#pragma once +struct mp3entry *audio_current_track(void); +void audio_next(void); void audio_prev(void); diff --git a/tests/chapters/shim/config.h b/tests/chapters/shim/config.h new file mode 100644 index 0000000..9d471b9 --- /dev/null +++ b/tests/chapters/shim/config.h @@ -0,0 +1,7 @@ +#pragma once +#define HAVE_AUDIOBOOK_CHAPTERS +#define MAX_PATH 260 +#include <stdbool.h> +#include <stdint.h> +#include <stddef.h> +#include <sys/types.h> diff --git a/tests/chapters/shim/file.h b/tests/chapters/shim/file.h new file mode 100644 index 0000000..7601b39 --- /dev/null +++ b/tests/chapters/shim/file.h @@ -0,0 +1,5 @@ +#pragma once +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +static inline off_t ffilesize(int fd){ struct stat st; return fstat(fd,&st)?-1:st.st_size; } diff --git a/tests/chapters/shim/list.h b/tests/chapters/shim/list.h new file mode 100644 index 0000000..0df3489 --- /dev/null +++ b/tests/chapters/shim/list.h @@ -0,0 +1,6 @@ +#pragma once +struct gui_synclist; +typedef const char *list_get_name(int, void *, char *, size_t); +struct simplelist_info { const char *title; int count; int selection; bool hide_theme; list_get_name *get_name; }; +static inline void simplelist_info_init(struct simplelist_info *i, char *t, int c, void *d){(void)d; i->title=t;i->count=c;i->selection=0;} +static inline bool simplelist_show_list(struct simplelist_info *i){(void)i; return true;} diff --git a/tests/chapters/shim/logf.h b/tests/chapters/shim/logf.h new file mode 100644 index 0000000..e761f85 --- /dev/null +++ b/tests/chapters/shim/logf.h @@ -0,0 +1,2 @@ +#pragma once +#define logf(...) do{}while(0) diff --git a/tests/chapters/shim/metadata.h b/tests/chapters/shim/metadata.h new file mode 100644 index 0000000..11d7cb6 --- /dev/null +++ b/tests/chapters/shim/metadata.h @@ -0,0 +1,3 @@ +#pragma once +enum { AFMT_UNKNOWN, AFMT_MP4_ALAC, AFMT_MP4_AAC, AFMT_MP4_AAC_HE, AFMT_MPA_L3 }; +struct mp3entry { char path[260]; int codectype; unsigned long elapsed, length; char *title, *album; }; diff --git a/tests/chapters/shim/misc.h b/tests/chapters/shim/misc.h new file mode 100644 index 0000000..226e90b --- /dev/null +++ b/tests/chapters/shim/misc.h @@ -0,0 +1,3 @@ +#pragma once +enum system_sound { SOUND_TRACK_NO_MORE }; +void system_sound_play(enum system_sound s); diff --git a/tests/chapters/shim/playback.h b/tests/chapters/shim/playback.h new file mode 100644 index 0000000..55a40b4 --- /dev/null +++ b/tests/chapters/shim/playback.h @@ -0,0 +1,2 @@ +#pragma once +void audio_pre_ff_rewind(void); void audio_ff_rewind(long ms); diff --git a/tests/chapters/shim/playlist.h b/tests/chapters/shim/playlist.h new file mode 100644 index 0000000..5082522 --- /dev/null +++ b/tests/chapters/shim/playlist.h @@ -0,0 +1,3 @@ +#pragma once +#include <stdbool.h> +bool playlist_check(int steps); diff --git a/tests/chapters/shim/rbunicode.h b/tests/chapters/shim/rbunicode.h new file mode 100644 index 0000000..87b7e43 --- /dev/null +++ b/tests/chapters/shim/rbunicode.h @@ -0,0 +1,2 @@ +#pragma once +unsigned char* utf8encode(unsigned long ucs, unsigned char *utf8); diff --git a/tests/chapters/shim/root_menu.h b/tests/chapters/shim/root_menu.h new file mode 100644 index 0000000..27ec430 --- /dev/null +++ b/tests/chapters/shim/root_menu.h @@ -0,0 +1,2 @@ +#pragma once +#define GO_TO_WPS 2 diff --git a/tests/chapters/shim/screen_access.h b/tests/chapters/shim/screen_access.h new file mode 100644 index 0000000..061b8c7 --- /dev/null +++ b/tests/chapters/shim/screen_access.h @@ -0,0 +1,3 @@ +#pragma once +#define DRMODE_COMPLEMENT 0 +struct screen { void (*set_drawmode)(int); void (*vline)(int,int,int); }; diff --git a/tests/chapters/shim/string-extra.h b/tests/chapters/shim/string-extra.h new file mode 100644 index 0000000..3303faf --- /dev/null +++ b/tests/chapters/shim/string-extra.h @@ -0,0 +1,3 @@ +#pragma once +#include <string.h> +static inline char *strmemccpy(char *d, const char *s, size_t n){ char *r = memccpy(d,s,0,n); if(!r && n) d[n-1]=0; return r; } diff --git a/tests/chapters/shim/system.h b/tests/chapters/shim/system.h new file mode 100644 index 0000000..344be9e --- /dev/null +++ b/tests/chapters/shim/system.h @@ -0,0 +1,2 @@ +#pragma once +#define MIN(a,b) ((a)<(b)?(a):(b)) diff --git a/tests/chapters/test.c b/tests/chapters/test.c new file mode 100644 index 0000000..c6982e6 --- /dev/null +++ b/tests/chapters/test.c @@ -0,0 +1,37 @@ +#include <stdio.h> +#include <string.h> +#include "config.h" +#include "metadata.h" +#include "misc.h" +#include "chapters.h" +/* minimal UTF-8 encoder like rockbox's */ +unsigned char* utf8encode(unsigned long c, unsigned char *u){ + if(c<0x80){*u++=c;} else if(c<0x800){*u++=0xc0|(c>>6);*u++=0x80|(c&63);} + else {*u++=0xe0|(c>>12);*u++=0x80|((c>>6)&63);*u++=0x80|(c&63);} return u; } +long seeked=-2; int nexts, prevs, nomore; +void system_sound_play(enum system_sound s){(void)s; nomore++;} +struct mp3entry cur; struct mp3entry *audio_current_track(void){return &cur;} +void audio_next(void){nexts++;} void audio_prev(void){prevs++;} +void audio_pre_ff_rewind(void){} void audio_ff_rewind(long ms){seeked=ms;} +bool playlist_check(int s){(void)s; return true;} +int main(int argc, char **argv){ + for (int a=1;a<argc;a++){ + memset(&cur,0,sizeof cur); strcpy(cur.path, argv[a]); cur.codectype=AFMT_MP4_AAC; + int n = chapters_update(&cur); + printf("== %s: %d chapters\n", argv[a], n); + char buf[100]; + for (int i=0;i<n;i++) if (i<6 || i>n-3) printf(" %3d %8lu ms [%s]\n", i+1, chapters_start(i), chapters_title(i,buf,sizeof buf)); + if (n>2){ + cur.elapsed = chapters_start(1)+1000; chapters_skip(&cur,-1,3000); printf(" prev 1s into ch2 -> %ld\n", seeked); + cur.elapsed = chapters_start(1)+10000; chapters_skip(&cur,-1,3000); printf(" prev 10s into ch2 -> %ld\n", seeked); + cur.elapsed = chapters_start(1)+10000; chapters_skip(&cur,1,3000); printf(" next from ch2 -> %ld\n", seeked); + cur.elapsed = chapters_start(n-1)+10; nomore=0; chapters_skip(&cur,1,3000); printf(" next from last -> nomore=%d\n", nomore); + cur.elapsed = chapters_start(2)+5; printf(" index at ch3+5ms = %d, current title [%s]\n", chapters_index(cur.elapsed), chapters_current_title(&cur)); + printf(" next title [%s]\n", chapters_next_title(&cur)); printf(" changed: %d %d\n", chapters_changed(&cur), chapters_changed(&cur)); + } else if (n==0) { + nexts=prevs=0; cur.elapsed=1000; chapters_skip(&cur,-1,3000); chapters_skip(&cur,1,3000); + cur.elapsed=10000; seeked=-2; chapters_skip(&cur,-1,3000); + printf(" no chapters: prev@1s->prevs=%d next->nexts=%d prev@10s->seek %ld\n", prevs, nexts, seeked); + } + } +} diff --git a/vendor/SHA256SUMS b/vendor/SHA256SUMS new file mode 100644 index 0000000..58ec515 --- /dev/null +++ b/vendor/SHA256SUMS @@ -0,0 +1,2 @@ +af85598b5989285d16833ceb77ef845c9ba2cdeac675c1888e1bfe8d802baf2c rockbox-822b9f5e.tar.xz +da988dd1ed9f2b4e3f364326ad0f45716278e0942fad28e49492b9e3e4ed6fc4 yp3-assets-1.tar.xz diff --git a/vendor/rockbox-822b9f5e.tar.xz b/vendor/rockbox-822b9f5e.tar.xz Binary files differnew file mode 100644 index 0000000..34ff775 --- /dev/null +++ b/vendor/rockbox-822b9f5e.tar.xz diff --git a/vendor/yp3-assets-1.tar.xz b/vendor/yp3-assets-1.tar.xz Binary files differnew file mode 100644 index 0000000..993168b --- /dev/null +++ b/vendor/yp3-assets-1.tar.xz |
