package library import ( "encoding/binary" "errors" "io" "strings" ) // MP4 has the same gap as Vorbis comments. A tag writer stores several genres // as several "data" atoms inside one ©gen atom (mutagen and Picard do), or // occasionally as repeated ©gen atoms. dhowden/tag keeps atoms in a map keyed // by name (mp4.go: m.data[name] = data), so one value survives either way. // // Path to the values: moov > udta > meta > ilst > ©gen > data. A file that // carries only the numeric "gnre" atom has no ©gen; this reader returns (nil, // nil) for it, and extractGenres falls back to dhowden/tag, which resolves gnre // to a name. // maxMP4GenreAtomSize caps how much of one ©gen atom is buffered. Genre text is // bytes; the cap only stops a corrupt size from making the scanner allocate. const maxMP4GenreAtomSize = 1 << 20 var errMalformedAtom = errors.New("library: malformed MP4 atom") type mp4Atom struct { kind string body, limit int64 // body start and end offsets in the file } // readMP4Atom reads the atom header at pos, which must lie within [pos, limit). func readMP4Atom(rs io.ReadSeeker, pos, limit int64) (mp4Atom, error) { if _, err := rs.Seek(pos, io.SeekStart); err != nil { return mp4Atom{}, err } var hdr [8]byte if _, err := io.ReadFull(rs, hdr[:]); err != nil { return mp4Atom{}, errMalformedAtom } size := int64(binary.BigEndian.Uint32(hdr[0:4])) hdrLen := int64(8) switch size { case 0: // runs to the end of the enclosing atom size = limit - pos case 1: // 64-bit size follows the type var large [8]byte if _, err := io.ReadFull(rs, large[:]); err != nil { return mp4Atom{}, errMalformedAtom } size = int64(binary.BigEndian.Uint64(large[:])) hdrLen = 16 } if size < hdrLen || size > limit-pos { return mp4Atom{}, errMalformedAtom } return mp4Atom{kind: string(hdr[4:8]), body: pos + hdrLen, limit: pos + size}, nil } // findMP4Child returns the first child of the given kind in [start, end). func findMP4Child(rs io.ReadSeeker, start, end int64, kind string) (mp4Atom, bool, error) { for pos := start; pos+8 <= end; { a, err := readMP4Atom(rs, pos, end) if err != nil { return mp4Atom{}, false, err } if a.kind == kind { return a, true, nil } pos = a.limit } return mp4Atom{}, false, nil } // readMP4GenreValues returns every ©gen value, in file order. rs is seeked // as needed, so it is safe to call after dhowden/tag has consumed the reader. func readMP4GenreValues(rs io.ReadSeeker) ([]string, error) { ilst, found, err := findMP4Ilst(rs) if err != nil || !found { return nil, err } var out []string err = eachMP4Item(rs, ilst, func(kind string, body []byte) bool { if kind != "\xa9gen" { return true } values, ok := mp4DataValues(body) out = append(out, values...) return ok }) return out, err } // readMP4FreeformValues returns the values of the freeform "----" item called // name (com.apple.iTunes:ARTISTS is Picard's), in file order. Names compare // case-insensitively, as taggers differ in case. func readMP4FreeformValues(rs io.ReadSeeker, name string) ([]string, error) { ilst, found, err := findMP4Ilst(rs) if err != nil || !found { return nil, err } var out []string err = eachMP4Item(rs, ilst, func(kind string, body []byte) bool { if kind != "----" || !strings.EqualFold(mp4FreeformName(body), name) { return true } values, ok := mp4DataValues(body) out = append(out, values...) return ok }) return out, err } // mp4FreeformName is the text of the "name" atom in a freeform item's body: // size, "name", four bytes of version and flags, then the name. func mp4FreeformName(b []byte) string { for len(b) >= 8 { size := binary.BigEndian.Uint32(b[0:4]) if size < 8 || uint64(size) > uint64(len(b)) { return "" } if string(b[4:8]) == "name" && size >= 12 { return string(b[12:size]) } b = b[size:] } return "" } // eachMP4Item calls fn with the kind and body of each item in ilst, in file // order, until fn returns false (a malformed body). Each body is capped at // maxMP4GenreAtomSize. func eachMP4Item(rs io.ReadSeeker, ilst mp4Atom, fn func(kind string, body []byte) bool) error { for pos := ilst.body; pos+8 <= ilst.limit; { a, err := readMP4Atom(rs, pos, ilst.limit) if err != nil { return errNoGenreFrame } pos = a.limit n := a.limit - a.body if n > maxMP4GenreAtomSize { return errNoGenreFrame } if _, err := rs.Seek(a.body, io.SeekStart); err != nil { return errNoGenreFrame } body := make([]byte, n) if _, err := io.ReadFull(rs, body); err != nil { return errNoGenreFrame } if !fn(a.kind, body) { return errNoGenreFrame } } return nil } // findMP4Ilst finds the iTunes item list, moov > udta > meta > ilst. // found is false for a file with no iTunes metadata. func findMP4Ilst(rs io.ReadSeeker) (ilst mp4Atom, found bool, err error) { end, err := rs.Seek(0, io.SeekEnd) if err != nil { return mp4Atom{}, false, err } // moov may sit after mdat; walking headers and seeking past bodies finds // it either way without reading the audio. cur := mp4Atom{body: 0, limit: end} for _, kind := range []string{"moov", "udta", "meta"} { next, found, err := findMP4Child(rs, cur.body, cur.limit, kind) if err != nil { return mp4Atom{}, false, errNoGenreFrame } if !found { return mp4Atom{}, false, nil // no iTunes metadata at all } cur = next } // meta is a "full box" with four bytes of version and flags before its // children in the ISO layout. QuickTime-style files omit them, so the // first child (hdlr) starts straight away. Look before skipping. if _, err := rs.Seek(cur.body, io.SeekStart); err != nil { return mp4Atom{}, false, errNoGenreFrame } var peek [8]byte if _, err := io.ReadFull(rs, peek[:]); err != nil { return mp4Atom{}, false, errNoGenreFrame } if string(peek[4:8]) != "hdlr" { cur.body += 4 } ilst, found, err = findMP4Child(rs, cur.body, cur.limit, "ilst") if err != nil { return mp4Atom{}, false, errNoGenreFrame } return ilst, found, nil } // mp4DataValues reads the "data" atoms in a ©gen or freeform item's body; // a freeform item's "mean" and "name" atoms are skipped. Each is: size, "data", // a 4-byte type indicator (1 = UTF-8 text), a 4-byte locale, then the value. // Atoms of other types carry no text and are skipped. func mp4DataValues(b []byte) ([]string, bool) { var out []string for len(b) >= 8 { size := binary.BigEndian.Uint32(b[0:4]) if size < 8 || uint64(size) > uint64(len(b)) { return nil, false } atom := b[:size] b = b[size:] if string(atom[4:8]) != "data" || len(atom) < 16 { continue } if binary.BigEndian.Uint32(atom[8:12])&0x00FFFFFF != 1 { continue } if v := strings.TrimSpace(string(atom[16:])); v != "" { out = append(out, v) } } return out, true }