Skip to content

Saving corrupts some Ogg Vorbis files #864

Closed

Description

@jangler

Using this code (from @jacobvosmaer) and these Ogg Vorbis files (from @nguillaumin):

#include <iostream>
#include <taglib/fileref.h>

int main(int argc, char **argv) {
  if (argc != 2) {
    std::cout << "usage: " << argv[0] << " file.ogg" << std::endl;
    exit(1);
  }

  const char *filename = argv[1];

  TagLib::FileRef file(filename);

  std::cerr << "saving " << filename << " with taglib." << std::endl;

  if (!file.save()) {
    std::cerr << "file save failed" << std::endl;
    exit(1);
  }
}

The saved Ogg Vorbis files can't be decoded any program I've tried.

Activity

  1. scotchi commented on Mar 27, 2018

    @scotchi
    Member

    I can reproduce this with the code you've pasted here, but oddly not with the basic tagwriter example in the examples dir. There, for instance, just updating the track number doesn't corrupt the file.

  2. jacobvosmaer commented on Mar 27, 2018

    @jacobvosmaer
  3. scotchi commented on Mar 27, 2018

    @scotchi
    Member
  4. FestusHagen commented on Mar 29, 2018

    @FestusHagen
    Contributor

    I as well can reproduce in v1.11.1 (e36a9ca). i.e. last release.

    However appears to be fixed with later commits. (Like here: 9336c82)

    Our local repo is a bit behind (github ssl issues) and the issue is not reproducible at this level.
    v1 @ c8bcd15 - Tsuda Kageyu, 10 months ago : TableOfContentsFrame depends on ByteVectorList.
    v2 @ bd4a45b - Tsuda Kageyu, 1 year, 1 month ago : Merge branch 'master' into merge-master-to-taglib2

    Maybe it's time for a release ...

  5. Sigill commented on Apr 1, 2018

    @Sigill

    FYI, the issue was introduced in commit 02eac18 and fixed in commit 9336c82.
    I've also found that it impacts recent versions of VLC.
    I vote for a new release too.

  6. nguillaumin commented on May 19, 2018

    @nguillaumin

    A new release would be great. How can we make it happen?

  7. shymega commented on Aug 23, 2018

    @shymega

    i've also found that commit 9336c82 has resolved the OGG issue. I patched taglib on my Gentoo system using the diff of the aforementioned commit.

    Gentoo's ebuild for taglib also applies a CVE patch to the source code as well.. taking that [the CVE] into consideration, a new release would very much be in order, IMHO ..

  8. uklotzde commented on Jun 18, 2019

    @uklotzde
  9. Pegasus-RPG commented on Jun 18, 2019

    @Pegasus-RPG

    Does this only affect OGG/Vorbis or any format in an OGG container?

  10. Moonbase59 commented on Jul 10, 2019

    @Moonbase59

    This might also affect my loudgain normalizer. I wonder if a corrected version could be made that the distros pick up real quick?!

    People might compile loudgain against the buggy version and start replaygaining huge collections. At least that was my plan until I read this … about 150,000 titles (not all Ogg Vorbis, though).

  11. uklotzde commented on Jul 11, 2019

    @uklotzde

    This bug is a bomb and I wonder why no hotfix version has been released yet.

  12. Moonbase59 commented on Jul 12, 2019

    @Moonbase59

    I can reproduce the problem using @jangler’s test files and the current version 0.2.7 of loudgain (commit 737a3b2).

    Before: Good and playable using Audacious, VLC, spek, Audacity, others.

    After:
    loudgain: Could not open input: End of file.
    Audacity: Could not detect type of file.
    VLC: Shows tags, seems to play, but silence over the whole duration.
    vorbisgain: File does not contain Vorbis data
    spek: File contains no audio tracks

    This is the code that writes to the file (tag.cc lines 232+):

    void tag_write_vorbis(scan_result *scan, bool do_album, char mode, char *unit) {
    	char value[2048];
    
    	TagLib::Ogg::Vorbis::File f(scan -> file);
    	TagLib::Ogg::XiphComment *tag = f.tag();
    
    	snprintf(value, sizeof(value), "%.2f %s", scan -> track_gain, unit);
    	tag -> addField("REPLAYGAIN_TRACK_GAIN", value);
    
    	snprintf(value, sizeof(value), "%.6f", scan -> track_peak);
    	tag -> addField("REPLAYGAIN_TRACK_PEAK", value);
    
    	// Only write album tags if in album mode (would be zero otherwise)
    	if (do_album) {
    		snprintf(value, sizeof(value), "%.2f %s", scan -> album_gain, unit);
    		tag -> addField("REPLAYGAIN_ALBUM_GAIN", value);
    
    		snprintf(value, sizeof(value), "%.6f", scan -> album_peak);
    		tag -> addField("REPLAYGAIN_ALBUM_PEAK", value);
    	}
    
    	// extra tags mode -s e or -s l
    	if (mode == 'e' || mode == 'l') {
    		snprintf(value, sizeof(value), "%.2f LUFS", scan -> loudness_reference);
    		tag -> addField("REPLAYGAIN_REFERENCE_LOUDNESS", value);
    
    		snprintf(value, sizeof(value), "%.2f %s", scan -> track_loudness_range, unit);
    		tag -> addField("REPLAYGAIN_TRACK_RANGE", value);
    
    		if (do_album) {
    			snprintf(value, sizeof(value), "%.2f %s", scan -> album_loudness_range, unit);
    			tag -> addField("REPLAYGAIN_ALBUM_RANGE", value);
    		}
    	}
    
    	f.save();
    }

    and this is the code to remove tags only (tag.cc lines 270+):

    void tag_clear_vorbis(scan_result *scan) {
    	TagLib::Ogg::Vorbis::File f(scan -> file);
    	TagLib::Ogg::XiphComment *tag = f.tag();
    
    	tag -> removeField("REPLAYGAIN_TRACK_GAIN");
    	tag -> removeField("REPLAYGAIN_TRACK_PEAK");
    	tag -> removeField("REPLAYGAIN_TRACK_RANGE");
    	tag -> removeField("REPLAYGAIN_ALBUM_GAIN");
    	tag -> removeField("REPLAYGAIN_ALBUM_PEAK");
    	tag -> removeField("REPLAYGAIN_ALBUM_RANGE");
    	tag -> removeField("REPLAYGAIN_REFERENCE_LOUDNESS");
    
    	f.save();
    }

    Interestingly, I have other Ogg Vorbis albums where I can do this many times, and nothing gets corrupted!

    When I do a vorbiscomment on @jangler’s and my files, I find that @jangler’s files have empty comments CDDB=, ACOUSTID_FINGERPRINT= and ACOUSTID_ID= that are gone after trying to loudgain -r -k -se *.ogg them (which is expected behaviour). The files I used for testing all had NO empty tag fields (and weren’t corrupted).

    Could it have something to do with that?


    System: Linux Mint 19.1 x86_64

    $ dpkg -l | grep libtag
    ii  libtag1-dev:amd64                             1.11.1+dfsg.1-0.2build2                amd64        audio meta-data library - development files
    ii  libtag1v5:amd64                               1.11.1+dfsg.1-0.2build2                amd64        audio meta-data library
    ii  libtag1v5-vanilla:amd64                       1.11.1+dfsg.1-0.2build2                amd64        audio meta-data library - vanilla flavour
    
    
  13. Moonbase59 commented on Jul 12, 2019

    @Moonbase59

    Made another experiment:

    1. Use vorbistagedit to remove all empty tags in 01-Les jours sans moi.ogg.
    2. File still ok and plays.
    3. Use loudgain -r -k -se 01\ -\ Les\ Jours\ sans\ moi-good.ogg on it (which previously corrupted the file).
    4. Voilà! Success! ReplayGain tags written, file still playable and GOOD!

    So it must have something to do with empty tag fields. (I think.)

  14. 4 remaining items

  15. matthewslaney commented on May 4, 2020

    @matthewslaney

    @scotchi Is there anything I can do to assist in a new release? There is still a workaround for this taglib bug in Mixxx that caused me to lose a bunch of metadata since it fails silently. The Mixxx devs don't want to bundle taglib, so it seems the only way this will get fixed in Mixxx is if there's a new taglib release.

  16. haplo commented on May 20, 2020

    @haplo

    I have been bitten by this bug, it corrupted half of my music collection when using python-rgain and then later vorbisgain. I thought the issue was with python-rgain, and was surprised when it happened again with vorbisgain.

    I will try to test the beta, it's surprising that it hasn't been released for over 8 months.

  17. scotchi commented on May 21, 2020

    @scotchi
    Member

    The honest truth is that for the moment, for all practical purposes, TagLib doesn't have a maintainer.

    I handed over the reigns for TagLib 12 years ago. I jumped back in for a couple weeks of activity hoping to push out a new release since the two maintainers since me had since disappeared, but a few days after that beta, I found out I was moving, and then by the time the dust settled on the move, the current pandemic had set in. Right now, with two small kids at home all the time, where I struggle to find the time to do my actual job, the chances of me finding a few days to work on a TagLib release before schools / day-cares reopen is basically zero. That hopefully will change in the coming months, but unless another maintainer (ideally someone with a history with the project) emerges, it'll be a while yet.

  18. McOrvas commented on Feb 6, 2021

    @McOrvas

    Today I have used the current loudgain.static version with the bug fix to process 36879 ogg vorbis files. Loudgain did not report a problem, but after the whole process I used oggz-validate to test all files and to my big surprise I got errors on 27008 of these files! The error message is always almost the same:

    ./path/music.ogg: Error:
    serialno 1335062492: granulepos 0 on page with no completed packets, must be -1
    serialno 1335062492: granulepos 0 on page with no completed packets, must be -1
    serialno 1335062492: granulepos 0 on page with no completed packets, must be -1
    serialno 1335062492: granulepos 0 on page with no completed packets, must be -1
    serialno 1335062492: granulepos 0 on page with no completed packets, must be -1
    serialno 1335062492: granulepos 0 on page with no completed packets, must be -1
    serialno 1335062492: granulepos 0 on page with no completed packets, must be -1
    serialno 1335062492: granulepos 0 on page with no completed packets, must be -1
    

    The number of these messages per file is different (~8) and of course the reported serialno per file is different, too. Before starting loudgain.static I also executed this check and all files were ok. I don't know were the problem is, but because of this issue here I simply assume that it is a problem with taglib. If you need further information to reproduce this issue please don't hesitate to ask.

  19. ufleisch commented on Feb 7, 2021

    @ufleisch
    Contributor

    Unfortunately, I do not have a lot of Ogg files, but with the few I have, I used loudgain.static -s e *.ogg and then oggz-validate *.ogg, but no errors were reported. So it would be helpful if you could provide one or more example files with instructions how to reproduce the problem.

  20. ufleisch commented on Feb 7, 2021

    @ufleisch
    Contributor

    Now I found two candidates. Interesting is that both contain an embedded cover and that I do not get the granulepos 0 on page with no completed packets, must be -1 error when I remove the pictures before applying loudgain.

  21. McOrvas commented on Feb 7, 2021

    @McOrvas

    @ufleisch This is a very good hint! Although all my ogg files contain a cover, I've made some experiments with it. I have removed the cover of an example file which shows the described error after processing with loudgain.static, and after removing and reprocessing it with loudgain.static now everything was ok. I have added different covers and with some of it the error occurs and with others not, I cannot say what is the key difference between it. But also interesting: After editing any tag field in an already damaged file (with EasyTAG) and saving the file, oggz-validate does no longer report an error. I'm rellay confused about it.

  22. ufleisch commented on Feb 8, 2021

    @ufleisch
    Contributor

    You can reproduce this without the need of an external program. Just use a copy of empty.ogg from the TagLib sources (taglib/tests/data/) and add a large comment, which will fill more than a page. From Bash you can add a comment with 70000 'A' characters using

    examples/tagwriter -c $(printf 'A%.0s' {1..70000}) empty.ogg
    

    Then you get the error

    $ oggz-validate empty.ogg 
    empty.ogg: Error:
    serialno 1002429366: granulepos 0 on page with no completed packets, must be -1
    serialno 1002429366: granulepos 0 on page with no completed packets, must be -1
    serialno 1002429366: granulepos 0 on page with no completed packets, must be -1
    serialno 1002429366: granulepos 0 on page with no completed packets, must be -1
    serialno 1002429366: granulepos 0 on page with no completed packets, must be -1
    serialno 1002429366: granulepos 0 on page with no completed packets, must be -1
    serialno 1002429366: granulepos 0 on page with no completed packets, must be -1
    serialno 1002429366: granulepos 0 on page with no completed packets, must be -1
    

    The problem is that such a large comment (as is the case when a cover art picture is embedded) will make the packet with the Vorbis comments span more than one Ogg page.

    See https://xiph.org/ogg/doc/framing.html

    absolute granule position
    (..)

    A special value of '-1' (in two's complement) indicates that no packets finish on this page.

    This is a bit contradictory to https://xiph.org/vorbis/doc/Vorbis_I_spec.html

    A.2. Encapsulation
    Ogg encapsulation of a Vorbis packet stream is straightforward.

    The first Vorbis packet (the identification header), which uniquely identifies
    a stream as Vorbis audio, is placed alone in the first page of the logical Ogg
    stream. This results in a first Ogg page of exactly 58 bytes at the very
    beginning of the logical stream.
    This first page is marked ’beginning of stream’ in the page flags.
    The second and third vorbis packets (comment and setup headers) may span one
    or more pages beginning on the second page of the logical stream. However many
    pages they span, the third header packet finishes the page on which it
    ends. The next (first audio) packet must begin on a fresh page.
    The granule position of these first pages containing only headers is zero.

    Here the granule position of the page containing the Vorbis comment is said to be zero. Probably when this was written, the author did not expect that Vorbis comments, which "is meant for short, text comments, not arbitrary metadata" (https://xiph.org/vorbis/doc/v-comment.html), would be (mis)used for cover art, and therefore its packet would never exceed the maximum possible physical page size of 65307 bytes. Therefore I suggest that TagLib should use a value of -1 for the granule position is such a case. This is also what is done by the official libogg/libvorbis.

    I have added a pull request which should fix this issue.

    @McOrvas You could create a loudgain with the patched TagLib and check if the validation succeeds now. Alternatively, you could just build the patched TagLib and then "heal" your files by just passing them to examples/tagwriter.
    @scotchi Since you probably know Ogg/Vorbis much better than I do, you could have a look at the pull request.

  23. McOrvas commented on Feb 9, 2021

    @McOrvas

    @ufleisch Thanks a lot for your detailed research! I have built your patched taglib version and used tagwriter with some of my corrupted files and afterwards all were error-free again (according to oggz-validate). Now I hope that your pull request will be integrated quickly and that a new release of taglib with all patches of this thread will be released.

  24. scotchi commented on Feb 16, 2021

    @scotchi
    Member

    The new TagLib with this fix in is out now, so closing this issue.

  25. added a commit that references this issue on Aug 15, 2021
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions