Skip to content

Add new port: libtwolame - MPEG Audio Layer 2 encoder for broadcast-quality audio#51728

Open
Greisby wants to merge 9 commits into
microsoft:masterfrom
Greisby:new-libtwolame
Open

Add new port: libtwolame - MPEG Audio Layer 2 encoder for broadcast-quality audio#51728
Greisby wants to merge 9 commits into
microsoft:masterfrom
Greisby:new-libtwolame

Conversation

@Greisby
Copy link
Copy Markdown
Contributor

@Greisby Greisby commented May 13, 2026

Description

TwoLAME is a MPEG Audio Layer 2 encoder for broadcast-quality audio.
It is an optimised encoder based on tooLAME by Mike Cheng, which itself was based on the ISO reference encoder dist10.

MP2 remains the standard audio codec for European broadcasting (DVB, DAB) and is widely used in professional media workflows.
TwoLAME is the highest-quality open-source MP2 encoder available and an encoder used by FFmpeg for MP2 output (-c:a libtwolame).

Why this port is needed

  • FFmpeg integration: FFmpeg's libtwolame encoder wraps this library (port must be modified later).
    Building FFmpeg with --enable-libtwolame provides a broadcast-compliant MP2 encoder far superior to FFmpeg's built-in mp2 encoder.
  • Broadcast compliance: MP2 is mandatory in DVB (EN 300 468), DAB, and many contribution/distribution chains.
    TwoLAME produces ISO 11172-3 / 13818-3 compliant bitstreams.
  • Quality: TwoLAME implements psychoacoustic model improvements and produces better output than the reference encoder, particularly at lower bitrates (128–192 kbps stereo) used in broadcast.

Build strategy

Platform Build system Notes
Windows CMake Custom minimal CMakeLists.txt (library-only build)
Linux/macOS Autotools Native ./configure via vcpkg_configure_make(AUTOCONFIG)

Upstream ships an autotools build system only.
On Windows, a custom CMakeLists.txt is provided for MSVC compatibility (library build only — no CLI tool).

Patches

Patch Purpose
001-fix-tl-api-export.patch Fix TWOLAME_DLL / dllexport for shared builds
002-disable-doc-subdir.patch Remove doc subdir from build; fix pkgdoc_DATA filenames (README.md, NEWS.md)

Additional portfile fix (no upstream source patch):

  • Post-install pkg-config normalization for static Linux builds to expose -lm in Libs.

  • Changes comply with the maintainer guide.
  • The packaged project shows strong association with the chosen port name. Check this box if at least one of the following criteria is met:
    • The project is in Repology: https://repology.org/project//versions
    • The project is amongst the first web search results for "" or " C++". Include a screenshot of the search engine results in the PR.
    • The port name follows the 'GitHubOrg-GitHubRepo' form or equivalent Owner-Project form.
  • Optional dependencies of the build are all controlled by the port. A dependency is controlled if it is declared an unconditional dependency in vcpkg.json, or explicitly disabled through patches or build system arguments such as CMAKE_DISABLE_FIND_PACKAGE_Xxx or VCPKG_LOCK_FIND_PACKAGE
  • The versioning scheme in vcpkg.json matches what upstream says.
  • The license declaration in vcpkg.json matches what upstream says.
  • The installed as the "copyright" file matches what upstream says.
  • The source code of the component installed comes from an authoritative source.
  • The generated "usage text" is brief and accurate. See adding-usage for context. Don't add a usage file if the automatically generated usage is correct.
  • The version database is fixed by rerunning ./vcpkg x-add-version --all and committing the result.
  • Exactly one version is added in each modified versions file.

@Greisby
Copy link
Copy Markdown
Contributor Author

Greisby commented May 13, 2026

sorry, forgot to switch to master first...

@Greisby Greisby marked this pull request as draft May 13, 2026 12:44
Copy link
Copy Markdown
Contributor

@dg0yt dg0yt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same buildsystem comment as in the other PR.

Comment thread ports/libtwolame/portfile.cmake Outdated
Comment on lines +41 to +45

if("tool" IN_LIST FEATURES)
# twolame binary is installed in tools/libtwolame/bin by vcpkg_configure_make's custom bindir
vcpkg_copy_tools(TOOL_NAMES twolame AUTO_CLEAN SEARCH_DIR "${CURRENT_PACKAGES_DIR}/tools/libtwolame/bin")
endif()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if("tool" IN_LIST FEATURES)
# twolame binary is installed in tools/libtwolame/bin by vcpkg_configure_make's custom bindir
vcpkg_copy_tools(TOOL_NAMES twolame AUTO_CLEAN SEARCH_DIR "${CURRENT_PACKAGES_DIR}/tools/libtwolame/bin")
endif()

In general tools are not moved from tools/<port>/bin to tools/<port>.

Comment thread ports/libtwolame/portfile.cmake Outdated
Comment on lines +50 to +64
# For static builds, autotools puts -lm in Libs.Private which is only used with pkg-config --static.
# Consumers like FFmpeg use plain --libs, so the link test fails on math symbols (pow, cos, lrintf...).
# Move -lm into Libs.
if(NOT VCPKG_TARGET_IS_WINDOWS AND VCPKG_LIBRARY_LINKAGE STREQUAL "static")
foreach(_pc_suffix IN ITEMS "lib/pkgconfig/twolame.pc" "debug/lib/pkgconfig/twolame.pc")
set(_pc_file "${CURRENT_PACKAGES_DIR}/${_pc_suffix}")
if(EXISTS "${_pc_file}")
file(READ "${_pc_file}" _pc_content)
string(REPLACE "-ltwolame" "-ltwolame -lm" _pc_content "${_pc_content}")
string(REGEX REPLACE "Libs\\.Private:[^\n]*" "Libs.Private:" _pc_content "${_pc_content}")
file(WRITE "${_pc_file}" "${_pc_content}")
endif()
endforeach()
endif()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vcpkg_fixup_pkgconfig does the merging of xxx.Private into xxx.

@Greisby Greisby marked this pull request as ready for review May 14, 2026 12:16
Comment thread ports/libtwolame/CMakeLists.txt Outdated
Comment on lines +6 to +7
option(BUILD_SHARED_LIBS "Build shared library" OFF)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
option(BUILD_SHARED_LIBS "Build shared library" OFF)

Set by the vcpkg toolchain.

Comment thread ports/libtwolame/CMakeLists.txt Outdated
Comment on lines +39 to +51
install(TARGETS twolame
EXPORT twolame-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

install(EXPORT twolame-targets
FILE twolameConfig.cmake
NAMESPACE twolame::
DESTINATION share/twolame
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unofficial targets should be namespaced (unofficial::twolame)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Full rules covers the CMake package name, too: unofficial-<PORT>.

Comment thread ports/libtwolame/CMakeLists.txt Outdated

Name: TwoLAME
Description: Optimised MPEG Audio Layer 2 (MP2) encoder
Version: ${PROJECT_VERSION}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PROJECT_VERSION is not set

Comment thread ports/libtwolame/portfile.cmake Outdated
Comment on lines +23 to +24
vcpkg_cmake_build()
vcpkg_cmake_install()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
vcpkg_cmake_build()
vcpkg_cmake_install()
vcpkg_cmake_install()
vcpkg_cmake_fixup_config(PACKAGE_NAME unofficial-twolame)

Comment thread ports/libtwolame/portfile.cmake Outdated
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO njh/twolame
REF 0.4.0
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
REF 0.4.0
REF "${VERSION}"

VERSION is filled automatically and matches the version declared in vcpkg.json

Comment thread ports/libtwolame/portfile.cmake Outdated
Comment on lines +13 to +16
if(VCPKG_TARGET_IS_WINDOWS)
# Upstream has no CMakeLists.txt suitable for library-only builds.
# Use our own minimal CMakeLists.txt.
file(COPY "${CMAKE_CURRENT_LIST_DIR}/CMakeLists.txt" DESTINATION "${SOURCE_PATH}")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use vcpkg_configure_make on Windows? It should work alright but you will need to add pkgconfig to path for .pc files to work.

vcpkg_find_acquire_program(PKGCONFIG)
get_filename_component(PKGCONFIG_DIR "${PKGCONFIG}" DIRECTORY)
vcpkg_add_to_path("${PKGCONFIG_DIR}")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question.
This is also based on a port I made a few years ago.
I'll try to get it working with autotools.

Comment on lines +8 to +10
patches/001-fix-tl-api-export.patch
patches/002-disable-doc-subdir.patch
patches/003-fix-kr-declaration.patch
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you submitted patches 001 and 003 upstream?

Copy link
Copy Markdown
Contributor Author

@Greisby Greisby May 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but they were already submitted and merged upstream:

Since then (2021!), no new version was tagged, so I included them.

@vicroms vicroms marked this pull request as draft May 14, 2026 20:03
@Greisby
Copy link
Copy Markdown
Contributor Author

Greisby commented May 15, 2026

It was pretty straightforward, BUT
for any strange reason, vcpkg_fixup_pkgconfig() does not do its job.
The installed pc (on linux, static) contains this after vcpkg_fixup_pkgconfig():

prefix=${pcfiledir}/../..
exec_prefix=${prefix}
libdir=${prefix}/lib
includedir=${prefix}/include

Name: twosome
Description: An optimized MPEG Audio Layer 2 encoder
Version: 0.4.0
Libs.Private: -lm -lm 

Libs: "-L${libdir}" -ltwolame
Requires: 
Cflags: "-I${includedir}" -DLIBTWOLAME_STATIC

The -lm was not moved from Libs.Private to Libs.
I had to re-introduce the manual patching.

Any clue?

@dg0yt
Copy link
Copy Markdown
Contributor

dg0yt commented May 16, 2026

The -lm was not moved from Libs.Private to Libs. I had to re-introduce the manual patching.

Any clue?

Case issue. Libs.private is the usual spelling. (Admittedly I believe that I have seen other spellings.) Not sure it spec or tools are very strict.

@Greisby
Copy link
Copy Markdown
Contributor Author

Greisby commented May 16, 2026

This is a strange check error.
I followed the full process again:

git fetch https://github.com/microsoft/vcpkg master
git merge-base HEAD FETCH_HEAD
git restore --source=8...bb --staged --worktree -- versions
git clean -fd -- versions
vcpkg format-manifest ports/libtwolame/vcpkg.json
vcpkg x-add-version --all
git add -u & git add versions/l-/libtwolame.json
git status → no change

so my last commit was correct.

Comment thread ports/libtwolame/portfile.cmake
Greisby and others added 2 commits May 17, 2026 13:18
Co-authored-by: Kai Pastor <dg0yt@darc.de>
@Greisby
Copy link
Copy Markdown
Contributor Author

Greisby commented May 18, 2026

By the way, could the “check for common mistakes” failure be caused by the branch being out of date?

@dg0yt
Copy link
Copy Markdown
Contributor

dg0yt commented May 18, 2026

By the way, could the “check for common mistakes” failure be caused by the branch being out of date?

These checks are usually made agains merge commits. So either it sees a fresh picture, or it doesn't start due to merge conflict.

@vicroms
Copy link
Copy Markdown
Member

vicroms commented May 19, 2026

By the way, could the “check for common mistakes” failure be caused by the branch being out of date?

I've seen it happen when the branch is more than 50 commits out of date.

@Greisby
Copy link
Copy Markdown
Contributor Author

Greisby commented May 19, 2026

Thanks, I hesitated to do it myself.

@Greisby Greisby marked this pull request as ready for review May 19, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants