]> granicus.if.org Git - taglib/commitdiff
Use correct frame sizes when calculating length of MPEG 2 or 2.5 streams.
authorLukáš Lalinský <lalinsky@gmail.com>
Thu, 22 Nov 2007 18:55:47 +0000 (18:55 +0000)
committerLukáš Lalinský <lalinsky@gmail.com>
Thu, 22 Nov 2007 18:55:47 +0000 (18:55 +0000)
CCBUG:130185

git-svn-id: svn://anonsvn.kde.org/home/kde/trunk/kdesupport/taglib@740177 283d02a7-25f6-0310-bc7c-ecb5cbfe19da

taglib/mpeg/mpegproperties.cpp
tests/CMakeLists.txt
tests/Makefile.am
tests/data/mpeg2.mp3 [new file with mode: 0644]
tests/test_mpeg.cpp [new file with mode: 0644]

index ed1af124c00a668f22db05ed18373e2f967593f3..76286b9381726d69d9ca87ff28648d0c4bf9e0e2 100644 (file)
@@ -215,9 +215,17 @@ void MPEG::Properties::read()
      firstHeader.sampleRate() > 0 &&
      d->xingHeader->totalFrames() > 0)
   {
-      static const int blockSize[] = { 0, 384, 1152, 1152 };
-
-      double timePerFrame = double(blockSize[firstHeader.layer()]) / firstHeader.sampleRate();
+      static const int blockSize[2][4] = {
+        // Version 1
+        { 0, 384, 1152, 1152 },
+        // Version 2 or 2.5
+        { 0, 384, 1152, 576 }
+      };
+
+      int versionIndex = firstHeader.version() == Header::Version1 ? 0 : 1;
+      double timePerFrame =
+        double(blockSize[versionIndex][firstHeader.layer()]) /
+        firstHeader.sampleRate();
       d->length = int(timePerFrame * d->xingHeader->totalFrames());
       d->bitrate = d->length > 0 ? d->xingHeader->totalSize() * 8 / d->length / 1000 : 0;
   }
index 34aa7fab5a32638ee0101985493eb526cb2dd943..72040d0ff359ef176d5d8a18547a7bff1059ab68 100644 (file)
@@ -13,6 +13,7 @@ SET(test_runner_SRCS
   main.cpp
   test_list.cpp
   test_map.cpp
+  test_mpeg.cpp
   test_synchdata.cpp
   test_bytevector.cpp
   test_string.cpp
index 47a6b3f0f8d2d1f1a51ee204c0da6ca53e783b8f..1c24d94fd038fae62ea8aa8e1c50a81178134611 100644 (file)
@@ -10,6 +10,7 @@ test_runner_SOURCES = \
        main.cpp \
        test_list.cpp \
        test_map.cpp \
+       test_mpeg.cpp \
        test_synchdata.cpp \
        test_bytevector.cpp \
        test_string.cpp \
diff --git a/tests/data/mpeg2.mp3 b/tests/data/mpeg2.mp3
new file mode 100644 (file)
index 0000000..13e8d53
Binary files /dev/null and b/tests/data/mpeg2.mp3 differ
diff --git a/tests/test_mpeg.cpp b/tests/test_mpeg.cpp
new file mode 100644 (file)
index 0000000..6278ff5
--- /dev/null
@@ -0,0 +1,25 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <string>
+#include <stdio.h>
+#include <mpegfile.h>
+
+using namespace std;
+using namespace TagLib;
+
+class TestMPEG : public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE(TestMPEG);
+  CPPUNIT_TEST(testVersion2DurationWithXingHeader);
+  CPPUNIT_TEST_SUITE_END();
+
+public:
+
+  void testVersion2DurationWithXingHeader()
+  {
+    MPEG::File f("data/mpeg2.mp3");
+    CPPUNIT_ASSERT_EQUAL(5387, f.audioProperties()->length());
+  }
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(TestMPEG);