The music database¶
The iPod does not scan its music. iTunes writes a database onto the disk at every sync, and the
firmware believes it. Every list on the device, the name it shows for itself and the cover beside each
album come out of two files in iPod_Control: the iTunesDB and the ArtworkDB. Chikuma reads both
the way the firmware reads them, in chikuma/drivers/media/.
A stream of chunks¶
The iTunesDB is a tree of tagged chunks. A database header holds datasets, a dataset holds a list,
and the list holds tracks or playlists, each followed by string records. The format is well known
from the open-source sync tools, so Chikuma takes the layout as given. What it recovers from the
firmware is how the device walks it.
The firmware reads the file as a stream. It takes bytes forward and drops what it does not want, a kilobyte at a time, and it never seeks backwards. That is how a twenty-megabyte database gets walked through a buffer of a few dozen bytes. Chikuma keeps a cursor in the same way and skips whole datasets by their stated length instead of loading the file.
Each chunk kind has its own header read length. After reading that much, the reader skips whatever remains of the header the file declares. A database written by a newer iTunes, with longer headers, still walks.
| Dataset | What the device does with it |
|---|---|
| tracks | read, one record per track |
| playlists | read, but only the first of the two playlist datasets |
| albums | skipped, the device builds its own album rows |
| smart playlists, Genius id | skipped |
A string record carries an encoding word, and the reader branches on it. Both UTF-16 and UTF-8 occur.
The device's name is a playlist title¶
There is no file for the iPod's name. The name Settings shows is the title of the master playlist, the first playlist in the database. The playlist dataset appears twice, so the name is on disk twice, and the device only ever reads the first copy.
Track records¶
A track becomes a fixed-size record taken from a pool, not a plain allocation. The firmware keeps many more fields per track than Chikuma does. Chikuma's record is a subset of the firmware's, never an invention: title, album, artist, track and disc number, year, length, rating, media kind, compilation flag, release date, the database id, and the play and skip statistics Genius reads.
The model is sized from the database at load. A first guess comes from the file's size. If the guess is short, the parse doubles its blocks and runs again, then everything is compacted into exact blocks. A library of eleven thousand tracks takes under two megabytes this way.
Placeholders are rows¶
A track with no title, album or artist gets a placeholder at load. The reader fetches "Unknown Album", "Unknown Artist" or "Unknown Track" from the localized strings and files the track under it, like any other value. So every track without an album shares one "Unknown Album" row, the same row a track literally called "Unknown Album" lands in. Chikuma does the same.
The database is signed for one device¶
The header carries a signature computed over the file with the device's own FireWire id as the key. The firmware checks it before reading a single dataset. A database signed for another device, or not signed at all, is skipped as a whole. The caller treats an empty library as normal, so the screen says "No Music" and nothing else.
The id is the device's USB serial, which the boot chain hands to the OS in its SysInfo block. On the emulated machine the id has to be set to the one the disk image was synced for, or the original firmware shows an empty library.
The order is in the file¶
The order of the Songs list is not computed on the device. iTunes writes precomputed orders onto the master playlist, one index list per field: title, artist, album, genre, composer. The firmware reads them after the playlist's items and checks each one. An index whose count is not the track count gets refused.
Rewriting a track's title on a test image and re-signing the database leaves the row where it was, so the list is not sorted on the device. Leading articles, "The Rapture" filed under R, come from the sort keys iTunes wrote, and they reach the device through these orders. The firmware never parses the sort keys itself.
When the index is refused, the device sorts¶
Some sync tools write indexes the device refuses. The firmware then sorts at boot, with a quicksort over a descriptor per list kind. A descriptor is a list of fields, compared in turn until one differs.
| List | Fields, in order |
|---|---|
| Albums | album, disc, track number, artist, title, master playlist position |
| Artists, Cover Flow | artist, album, disc, track number, title, master playlist position |
Strings compare through a Unicode-weight collation. Punctuation comes first, then symbols, digits and letters, with case and accent as secondary weights. Chikuma's comparator was checked against the firmware's whole sorted vectors on a library of eleven thousand tracks, every position.
A distinct-value list, such as Albums, is then the first appearance of each value along the sorted tracks. Values compare case-sensitively, so "Live" and "LIVE" are two rows. Where the collation calls two names equal, the first track decides.
Cover Flow is a run per album¶
Cover Flow walks the Artists order and starts a new cover wherever the album changes. An album interrupted by another artist's tracks comes back as a second cover further on. Tracks flagged as compilation tracks are left out one by one, so an album with a few unflagged tracks still shows under their artist. Chikuma's first hundred covers on a real library match the original's, in order.
Cover art¶
The ArtworkDB uses the same chunk family with its own records, and it gets streamed the same way.
Its string header differs from the iTunesDB's, with the encoding stored as a byte, and mixing the
two silently yields empty names.
Each image record lists one entry per format. An entry gives a format id, a file name, a byte offset,
a size and the stored geometry. The pixels sit in .ithmb files as packed RGB565 with no header of
their own. A cover is one read at one offset.
| Format | Size | Used for |
|---|---|---|
| 1055 | 128 × 128 | Cover Flow |
| 1060 | 320 × 320 | the main menu preview |
| 1061 | 55 × 55 | list rows |
The device never resizes cover art. iTunes writes one file per format, and the firmware picks the format the screen wants. The 320-pixel thumbnail reaches the main menu's preview through a light horizontal filter and a crop. The firmware does not scale it.
A track finds its cover by the image link in its own record first, and by song id only when the link is empty. The database records one song id per image, so on a real library most tracks are not their image's recorded song. Looking up by song id alone leaves them with the placeholder note.
The database gets parsed once, when the library loads. The pixels are fetched later, on a cache thread of their own, so the interface never waits on the disk for a cover.
Tests¶
The tests run on real synced volumes, never on fixtures written to match the parser. They walk the partition map, mount HFS+, find the file in the catalog and stream it. The cover test hashes the pixels Chikuma reads and compares them with the bytes lifted out of the original's own read buffer on the same image.
Variants left out¶
A database whose words are byte-swapped is supported by the firmware behind a flag. With no such database to measure, Chikuma leaves it out for the moment. Compressed Genius blobs are in the same position, as Genius explains.