diff --git a/src/framework/mlt_audio.c b/src/framework/mlt_audio.c index ae689d199..3a2bb0dca 100644 --- a/src/framework/mlt_audio.c +++ b/src/framework/mlt_audio.c @@ -320,8 +320,7 @@ void mlt_audio_shrink(mlt_audio self, int samples) } else if (plane_count == 1 || samples == 0) { // No need to move any data. self->samples = samples; - } - if (plane_count > 1) { + } else if (plane_count > 1) { // Move data to remove gaps between planes. size_t src_plane_size = mlt_audio_plane_size(self); self->samples = samples; diff --git a/src/tests/test_audio/test_audio.cpp b/src/tests/test_audio/test_audio.cpp index 738fbcc56..dc1361aee 100644 --- a/src/tests/test_audio/test_audio.cpp +++ b/src/tests/test_audio/test_audio.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Meltytech, LLC + * Copyright (C) 2020-2026 Meltytech, LLC * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -51,6 +51,41 @@ private Q_SLOTS: free(data); a.set_data(nullptr); } + + void ShrinkPlanarAudio_data() + { + QTest::addColumn("format"); + QTest::addColumn("samples"); + QTest::addColumn>("expected"); + + const QList unchanged = {11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34}; + const QList shrunk = {11, 12, 21, 22, 31, 32}; + for (auto format : {mlt_audio_s32, mlt_audio_float}) { + const QString name = mlt_audio_format_name(format); + QTest::newRow(qPrintable(name + "-negative")) << int(format) << -1 << unchanged; + QTest::newRow(qPrintable(name + "-same")) << int(format) << 4 << unchanged; + QTest::newRow(qPrintable(name + "-larger")) << int(format) << 5 << unchanged; + QTest::newRow(qPrintable(name + "-smaller")) << int(format) << 2 << shrunk; + QTest::newRow(qPrintable(name + "-zero")) << int(format) << 0 << QList(); + } + } + + void ShrinkPlanarAudio() + { + QFETCH(int, format); + QFETCH(int, samples); + QFETCH(QList, expected); + + int32_t data[] = {11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34}; + struct mlt_audio_s audio = {}; + mlt_audio_set_values(&audio, data, 48000, mlt_audio_format(format), 4, 3); + + mlt_audio_shrink(&audio, samples); + + QCOMPARE(audio.samples, int(expected.size()) / 3); + for (int i = 0; i < expected.size(); i++) + QCOMPARE(data[i], expected[i]); + } }; QTEST_APPLESS_MAIN(TestAudio)