Do not scan full MPEG file for ID3v2 tag in Fast read style (#968) (#1151)

This is based on the patch used by VLC, but the full scan is only
skipped when the read style is explicitly set to Fast.
https://code.videolan.org/videolan/vlc/-/blob/master/contrib/src/taglib/0001-Implement-ID3v2-readStyle-avoid-worst-case.patch
This commit is contained in:
Urs Fleisch
2023-09-30 14:25:52 +02:00
committed by GitHub
parent 2154078187
commit c13a42021a
3 changed files with 60 additions and 14 deletions

View File

@ -68,6 +68,7 @@ class TestMPEG : public CppUnit::TestFixture
CPPUNIT_TEST(testEmptyAPE);
CPPUNIT_TEST(testIgnoreGarbage);
CPPUNIT_TEST(testExtendedHeader);
CPPUNIT_TEST(testReadStyleFast);
CPPUNIT_TEST_SUITE_END();
public:
@ -540,6 +541,45 @@ public:
}
}
void testReadStyleFast()
{
const ScopedFileCopy copy("lame_cbr", ".mp3");
{
MPEG::File f(copy.fileName().c_str(), true, MPEG::Properties::Fast);
CPPUNIT_ASSERT(f.audioProperties());
CPPUNIT_ASSERT_EQUAL(1887, f.audioProperties()->lengthInSeconds());
CPPUNIT_ASSERT_EQUAL(1887164, f.audioProperties()->lengthInMilliseconds());
CPPUNIT_ASSERT_EQUAL(64, f.audioProperties()->bitrate());
CPPUNIT_ASSERT_EQUAL(1, f.audioProperties()->channels());
CPPUNIT_ASSERT_EQUAL(44100, f.audioProperties()->sampleRate());
CPPUNIT_ASSERT(f.isValid());
CPPUNIT_ASSERT(f.hasID3v2Tag());
CPPUNIT_ASSERT_EQUAL(String(""), f.ID3v2Tag()->title());
PropertyMap properties = f.properties();
CPPUNIT_ASSERT_EQUAL(String("-1.020000 dB"), properties.value("REPLAYGAIN_TRACK_GAIN").front());
CPPUNIT_ASSERT_EQUAL(String("0.920032"), properties.value("REPLAYGAIN_TRACK_PEAK").front());
properties["TITLE"] = String("A Title");
properties["Artist"] = String("An Artist");
f.setProperties(properties);
f.save();
}
{
MPEG::File f(copy.fileName().c_str(), true, MPEG::Properties::Fast);
CPPUNIT_ASSERT(f.isValid());
CPPUNIT_ASSERT(f.hasID3v2Tag());
CPPUNIT_ASSERT_EQUAL(String("A Title"), f.ID3v2Tag()->title());
CPPUNIT_ASSERT_EQUAL(String("An Artist"), f.ID3v2Tag()->artist());
}
{
MPEG::File f(TEST_FILE_PATH_C("garbage.mp3"), true, MPEG::Properties::Fast);
CPPUNIT_ASSERT(f.isValid());
// Garbage prevents detection of ID3v2 with fast read style
CPPUNIT_ASSERT(!f.hasID3v2Tag());
CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(2255), f.firstFrameOffset());
CPPUNIT_ASSERT_EQUAL(static_cast<offset_t>(6015), f.lastFrameOffset());
}
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestMPEG);