Skip to content

[Feature Request][MP4] Support for arbitrary atom access #1245

Open

Description

@acolombier

It would be great if taglib could allow accessing MP4 atoms directly, including fetching, adding, updating and deleting.

I currently reused some of the private implementation of the library to do so as part of personal project, but if you would be interested, I'd like to make that part of the TagLib public library. This would help the Mixxx project to edit NI Stem track metadata

I appreciate this would expose quite a bit of the private library as I have currently implemented, so I'd be happy to rework that to some extend to make it manageable over time in the API.

Activity

  1. ufleisch commented on Oct 18, 2024

    @ufleisch
    Contributor

    This sounds interesting. TagLib already has some extension points, the TagLib::MP4::ItemFactory if you simply want to support additional atoms inside moov.udta.meta.ilst. Your use case goes beyond this, so there would be the possibility to extend TagLib::File::MP4 (and the dependencies like the corresponding Tag as needed) and register it with TagLib::FileRef::addFileTypeResolver(). This enhanced MP4 resolver would then be used in your application instead of the default implementation. If this is not possible or involves copying or duplicating a lot of code from TagLib because it is not accessible, a pull request fixing that would be welcome. Note, however, that binary compatibility (see e.g. Policies/Binary Compatibility Issues With C++ must be preserved.

  2. acolombier commented on Aug 27, 2025

    @acolombier
    ContributorAuthor

    Hi @ufleisch, coming back to this some time after my initial enquiries...

    Here was the minimal changes I had to do in order to render STEM atoms as part of the MP4. This is a change I currently use, tho this is obviously breaking the current metadata rendering.

    diff --git a/taglib/mp4/mp4tag.cpp b/taglib/mp4/mp4tag.cpp
    index 6e094963..b73d7d60 100644
    --- a/taglib/mp4/mp4tag.cpp
    +++ b/taglib/mp4/mp4tag.cpp
    @@ -98,16 +98,13 @@ MP4::Tag::renderAtom(const ByteVector &name, const ByteVector &data) const
     }
     
     bool
    -MP4::Tag::save()
    +MP4::Tag::save(TagLib::ByteVector stemData)
     {
       ByteVector data;
    -  for(const auto &[name, itm] : std::as_const(d->items)) {
    -    data.append(d->factory->renderItem(name, itm));
    -  }
    -  data = renderAtom("ilst", data);
    +  data = renderAtom("stem", stemData);
     
    -  AtomList path = d->atoms->path("moov", "udta", "meta", "ilst");
    -  if(path.size() == 4) {
    +  AtomList path = d->atoms->path("moov", "udta", "stem");
    +  if(path.size() == 3) {
         saveExisting(data, path);
       }
       else {
    @@ -227,10 +224,6 @@ MP4::Tag::updateOffsets(offset_t delta, offset_t offset)
     void
     MP4::Tag::saveNew(ByteVector data)
     {
    -  data = renderAtom("meta", ByteVector(4, '\0') +
    -                    renderAtom("hdlr", ByteVector(8, '\0') + ByteVector("mdirappl") +
    -                               ByteVector(9, '\0')) +
    -                    data + padIlst(data));
     
       AtomList path = d->atoms->path("moov", "udta");
       if(path.size() != 2) {
    @@ -281,11 +274,11 @@ MP4::Tag::saveExisting(ByteVector data, const AtomList &path)
       offset_t delta = data.size() - length;
       if(!data.isEmpty()) {
         if(delta > 0 || (delta < 0 && delta > -8)) {
    -      data.append(padIlst(data));
    +      data.append(padUdta(data));
           delta = data.size() - length;
         }
         else if(delta < 0) {
    -      data.append(padIlst(data, static_cast<int>(-delta - 8)));
    +      data.append(padUdta(data, static_cast<int>(-delta - 8)));
           delta = 0;
         }

    Trying to refactor those changes into something that would ensure the binary compatibility policy, I believe this could be an acceptable path, allowing minimal changes, while allowing UDTA atom customisation:

    diff --git a/taglib/mp4/mp4tag.cpp b/taglib/mp4/mp4tag.cpp
    index 6e094963..271fe3a4 100644
    --- a/taglib/mp4/mp4tag.cpp
    +++ b/taglib/mp4/mp4tag.cpp
    @@ -111,6 +111,10 @@ MP4::Tag::save()
         saveExisting(data, path);
       }
       else {
    +    data = renderAtom("meta", ByteVector(4, '\0') +
    +                      renderAtom("hdlr", ByteVector(8, '\0') + ByteVector("mdirappl") +
    +                                ByteVector(9, '\0')) +
    +                      data + padIlst(data));
         saveNew(data);
       }
     
    @@ -227,10 +231,6 @@ MP4::Tag::updateOffsets(offset_t delta, offset_t offset)
     void
     MP4::Tag::saveNew(ByteVector data)
     {
    -  data = renderAtom("meta", ByteVector(4, '\0') +
    -                    renderAtom("hdlr", ByteVector(8, '\0') + ByteVector("mdirappl") +
    -                               ByteVector(9, '\0')) +
    -                    data + padIlst(data));
     
       AtomList path = d->atoms->path("moov", "udta");
       if(path.size() != 2) {
    @@ -252,6 +252,12 @@ MP4::Tag::saveNew(ByteVector data)
     
     void
     MP4::Tag::saveExisting(ByteVector data, const AtomList &path)
    +{
    +  saveExisting(data, path, padIlst);
    +}
    +
    +void
    +MP4::Tag::saveExisting(ByteVector data, const AtomList &path, ByteVector(padFn*)(const ByteVector &, int = -1))
     {
       auto it = path.end();
     
    @@ -281,11 +287,11 @@ MP4::Tag::saveExisting(ByteVector data, const AtomList &path)
       offset_t delta = data.size() - length;
       if(!data.isEmpty()) {
         if(delta > 0 || (delta < 0 && delta > -8)) {
    -      data.append(padIlst(data));
    +      data.append(padFn(data));
           delta = data.size() - length;
         }
         else if(delta < 0) {
    -      data.append(padIlst(data, static_cast<int>(-delta - 8)));
    +      data.append(padFn(data, static_cast<int>(-delta - 8)));
           delta = 0;
         }
     
    diff --git a/taglib/mp4/mp4tag.h b/taglib/mp4/mp4tag.h
    index bae7d232..85dd4580 100644
    --- a/taglib/mp4/mp4tag.h
    +++ b/taglib/mp4/mp4tag.h
    @@ -115,6 +115,13 @@ namespace TagLib {
              */
             void setTextItem(const String &key, const String &value);
     
    +        /* Doc to be written! */
    +        void saveExisting(ByteVector data, const AtomList &path, ByteVector(padFn*)(const ByteVector &, int = -1)) const;
    +        
    +        /* Doc to be written! */
    +        // FIXME this likely break the compatibily policy!
    +        void saveNew(ByteVector data);
    +        
         private:
             ByteVector padIlst(const ByteVector &data, int length = -1) const;
             ByteVector renderAtom(const ByteVector &name, const ByteVector &data) const;
    @@ -123,7 +130,6 @@ namespace TagLib {
             void updateParents(const AtomList &path, offset_t delta, int ignore = 0);
             void updateOffsets(offset_t delta, offset_t offset);
     
    -        void saveNew(ByteVector data);
             void saveExisting(ByteVector data, const AtomList &path);
     
             void addItem(const String &name, const Item &value);

    This way, one could for example support the NI STEM like so:

    namespace {
    
    TagLib::ByteVector renderAtom(const TagLib::ByteVector &name,
                                  const TagLib::ByteVector &data) {
      return TagLib::ByteVector::fromUInt(data.size() + 8) + name + data;
    }
    
    TagLib::ByteVector padUdta(const TagLib::ByteVector &data, int length = -1) {
      if (length == -1) {
        length = ((data.size() + 1023) & ~1023) - data.size();
      }
      return renderAtom("free", TagLib::ByteVector(length, '\1'));
    }
    } // namespace
    
    class MyMP4WithStem: public TagLib::MP4::Tag {
    public:
    // ...
    bool save()
    {
      if (!TagLib::MP4::Tag::save()){
        return false;
      }
    
      ByteVector data = renderAtom("stem", m_stemData);
      // FIXME: doesn't exist
      AtomList path = findAtomByPath("moov", "udta", "stem");
      if(path.size() == 3) {
        saveExisting(data, path, padUdta);
      }
      else {
        saveNew(data);
      }
    
      return true;
    }

    Couple of things remaining to sort are:

    1. What do we do with TagLib::MP4::Tag::save? Currently we could keep it as it, but ideally we would want to make it virtual to allow polymorphism, but AFAICT, this could break compatibly.
    2. We need to find atom (referenced as findAtomByPath in the snippet above). I'm thinking about adding a protected method, that would proxy d->atoms->path. Does that sound like a viable option?

    It would be good to hear your thoughts here, before I potentially formulate these into a change request

  3. acolombier commented on Aug 30, 2025

    @acolombier
    ContributorAuthor

    FYI, I am working on an alternative to add first class support to STEM directly within TagLib.

    The reason for this change in direction is the desire to use this in the Rust wrapper, that only support the C TagLib.

    Appreciate this may not be a direction you would like to take tho so I have keep the PR on my repo so far.

  4. ufleisch commented on Sep 1, 2025

    @ufleisch
    Contributor

    Sorry for not answering earlier. I do not know anything about STEM, so I am probably not a great help here. Concerning TagLib::MP4::Tag::save(), making it virtual would probably break binary compatibility on Windows. But it is apparently only called from MP4::File::save(), which is virtual, so you could add a new non-virtual method to MP4::Tag and reimplement MP4::File overriding save() and using the reimplemented MP4::Tag class. Exposing d->atoms->path() should be possible.

    I just had a short look into your fork. Exposing new functionality in the C bindings with complex properties is the correct thing to do.

  5. acolombier commented on Sep 3, 2025

    @acolombier
    ContributorAuthor

    Great to hear! And thank for taking the time to look into this.

    For full disclosure, I am now using this on my project via this adapted rust binding (some more changes to be done before a formal release with license changes).
    Let me know if you would like me to stabilise this and submit a PR against TagLib to ad NI Stem support directly onto TagLib!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions