Bug 282188 - [NEW PORT] multimedia/ffmpeg3 (legacy ffmpeg as a dependency for emulators/ppsspp)
Summary: [NEW PORT] multimedia/ffmpeg3 (legacy ffmpeg as a dependency for emulators/pp...
Status: Closed FIXED
Alias: None
Product: Ports & Packages
Classification: Unclassified
Component: Individual Port(s) (show other bugs)
Version: Latest
Hardware: amd64 Any
: --- Affects Only Me
Assignee: Daniel Engberg
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-10-19 01:01 UTC by Kevin Reinholz
Modified: 2024-11-26 00:32 UTC (History)
5 users (show)

See Also:


Attachments
Ports tree patch adding multimedia/ffmpeg3 (35.97 KB, patch)
2024-10-19 01:01 UTC, Kevin Reinholz
no flags Details | Diff
Alternative version of new proposed ffmpeg3 port that only builds headers and libs required by emulators/ppsspp (34.23 KB, patch)
2024-10-22 19:15 UTC, Kevin Reinholz
no flags Details | Diff
Further cleaned up proposed new port--trimming unused and undesired options from Makefile (26.12 KB, patch)
2024-10-25 01:53 UTC, Kevin Reinholz
no flags Details | Diff
Updated patch - disables nasm, binutils, and other unnecessary build deps (12.78 KB, patch)
2024-10-27 23:41 UTC, Kevin Reinholz
no flags Details | Diff
Patch for ffmpeg3 v2 (10.13 KB, patch)
2024-10-28 19:03 UTC, Daniel Engberg
no flags Details | Diff
Updated patch - disables more aggressively to match ppsspp-ffmpeg (12.14 KB, patch)
2024-10-30 03:34 UTC, Kevin Reinholz
no flags Details | Diff
Related patch to make emulators/ppsspp build with ffmpeg3 static libs (3.31 KB, patch)
2024-10-30 03:43 UTC, Kevin Reinholz
no flags Details | Diff
Proposed update to Makefile--since this is a build dep of emulators/ppsspp, ARCH restrictions should match (472 bytes, patch)
2024-11-09 16:12 UTC, Kevin Reinholz
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Kevin Reinholz 2024-10-19 01:01:45 UTC
Created attachment 254346 [details]
Ports tree patch adding multimedia/ffmpeg3

This proposed legacy multimedia/ffmpeg3 port is intended solely for use as a dependency for emulators/ppsspp.

As documented in numerous places by the PPSSPP devs, any version of ffmpeg over 3.0.2 starts to break the emulator/ppsspp package's normally excellent PSP game compatibility:

https://github.com/hrydgard/ppsspp/issues/15308#issuecomment-1030655799
https://github.com/hrydgard/ppsspp/issues/17336
https://github.com/hrydgard/ppsspp/issues/6663
https://github.com/hrydgard/ppsspp/issues/15969
https://github.com/hrydgard/ppsspp/issues/15788
https://github.com/hrydgard/ppsspp/issues/11490#issuecomment-782735810
https://github.com/Homebrew/homebrew-core/issues/84737

With a few trivial hacks, it is quite simple to coax emulators/ppsspp into building against this proposed new port of legacy ffmpeg3-3.0.2, the version shipped with the Windows, Mac, and Linux ports of PPSSPP, which provides a dramatic improvement in PSP game compatibility on FreeBSD.

Default port options were set in order to pare down the build to just the bare minimum needed by emulators/ppsspp.

This is modeled after the multimedia/ffmpeg4 port, and installs files to ${LOCALBASE}/ffmpeg3/ so as not to conflict with either the multimedia/ffmpeg or the multimedia/ffmpeg4 ports. As such, I make no guarantees that enabling additional port options won't break the build.
Comment 1 Kevin Reinholz 2024-10-22 19:15:49 UTC
Created attachment 254451 [details]
Alternative version of new proposed ffmpeg3 port that only builds headers and libs required by emulators/ppsspp

This updated patch is for an alternative version of proposed multimedia/ffmpeg3 port that only builds headers and libs required by emulators/ppsspp, rather than installing programs as well.

The sole intended use for this proposed port is as a build/lib dependency of emulators/ppsspp--and provides drastically improved PSP game support compared to either multimedia/ffmpeg4 or multimedia/ffmpeg, essentially unbreaking the PPSSPP emulator on FreeBSD.
Comment 2 Kevin Reinholz 2024-10-25 01:53:13 UTC
Created attachment 254498 [details]
Further cleaned up proposed new port--trimming unused and undesired options from Makefile

This is hopefully the final version of the proposed new port. Trimmed unused and undesired options from Makefile. Removed Makefile.local, but moved LDFLAGS=+ applicable to all platforms into Makefile. Yes, it ignores undefined symbols, which is normally bad. However, without this flag, build fails due to disabling of network and build of ffserver, neither of which are used by emulators/ppsspp.

A possible alternative name for this port would be multimedia/ppsspp-ffmpeg, but that's potentially misleading as it's simply ffmpeg-3.0.2 with options turned off in an attempt to build only the bare minimal components required/used by emulators/ppsspp.
Comment 3 Daniel Engberg freebsd_committer freebsd_triage 2024-10-26 06:30:46 UTC
Hi Kevin,

So if I understand the issue correctly using anything else than ffmpeg3 in ppsspp basically breaks all video playback (which I guess is broken right now?)?

In general I'm not very keen on adding an ancient version of FFmpeg, mainly because it likely will break much more frequently due to being "abandonware"-ish  although I haven't managed to find an official source citing the current situation about 3.x. It's also troublesome to have multiple versions of FFmpeg installed the same time which further complicates things.

Having this in mind I think we short resort to trying to make it as contained as possible, basically VAAPI VDPAU X264 should be disabled as these shouldn't be needed for playback and that should also remove the need for svt_hevc patches. On that topic, given the rather low resolution and bitrate of the PSP I'm not sure if it's worth using a decade old assembly code or if we should just rely on the C implementation instead (mainly to avoid breakage).

It should also follow what the ffmpeg4 port does in terms of paths although I'm on the fence about somehow building it "within" the ppsspp port and preferably link it staticly so it's not ever visible to anything else. I'm haven't looked into how much of difference it would be from using their bundled(?) fork in that regard however.

Best regards,
Daniel
Comment 4 Kevin Reinholz 2024-10-26 20:27:38 UTC
Daniel,

Thanks for your comments! I share many of your concerns, which is why I debated submitting a proposed new (old) port in the first place. For the past 5-6 months I've been building/rebuilding my local ffmpeg-3.0.2 port and slightly modified emulators/ppsspp port with synth, and have been quite pleased with the results. It makes ppsspp usable again, without the need to skip in-game FMVs or cross my fingers there won't be a crash.

I observed frequent crashes with ppsspp-1.17.1 (and previous versions) when built against default ffmpeg (6.x)--typically near the end of an in-game FMV. It's not that the video wouldn't play--it's that a bad packet, missing header, or some other deviation between the PSP's video API and ffmpeg's would eventually get reached, and freeze the video/crash ppsspp. This behavior is not present when ppsspp is built against ffmpeg-3.0.2, the version recommended by the ppsspp devs.

From what I can tell, "bundled" ffmpeg used in the Mac, Linux, and Windows builds of ppsspp is just ffmpeg-3.0.2 with a lot of options turned off:

https://github.com/hrydgard/ppsspp-ffmpeg/blob/master/shared_options.sh

I had assumed x264 was needed for mp4 playback, but will try building ffmpeg with it disabled. A cursory look at the ffmpeg sources led me to (perhaps wrongly) conclude that libavcodec could take advantage of gpu acceleration, which is why I kept VAAPI and VDPAU enabled. It shouldn't be a problem to disable both of those options.

I'd debated between a quick-and-dirty hack to the existing ppsspp port to build against ffmpeg-3.0.2 shared libs, and hacking the ppsspp port more extensively to build ffmpeg-3.0.2 static libs and "bundle" them with ppsspp. The following discussion from 2022 made me wary of trying to bundle:

https://github.com/hrydgard/ppsspp/issues/15969

jbeich pointed out issues with attempting to build ppsspp with bundled-ffmpeg here:

https://github.com/hrydgard/ppsspp/issues/15308#issuecomment-1529076719

It seems the ppsspp devs have not been successful in incorporating their forked ffmpeg-3.0.2 sources into the ppsspp build process, but instead build ffmpeg-3.0.2 separately, dump the libs and header files into a folder, and then point ppsspp to the precompiled libs as part of their packing build process. There's no release tarball for their bundled-ffmpeg, either, making it easier to just grab the official ffmpeg-3.0.2 and build that.

That said, I'm very open to exploring the best way to do this. For starters, I'll try disabling ASM, VAAPI, VDPAU, and X264, and confirm whether ppsspp in-game FMV playback still works.
Comment 5 Kevin Reinholz 2024-10-27 15:39:48 UTC
Confirmed that building ffmpeg-3.0.2 with options ASM, VAAPI, VDPAU, and X264 disabled does not break in-game FMV playback with ppsspp-1.17.1 linked against recompiled ffmpeg-3.0.2.

I'm happy to disable the options by default and/or hide them from the user completely and resubmit. Or if there's a consensus on a better way to do this I'll wait to hear back rather than spamming updated patches.

This is what my `pkg info` looks like with latest tested working version of ffmpeg-3.0.2:

ffmpeg3-3.0.2
Name           : ffmpeg3
Version        : 3.0.2
Installed on   : Sun Oct 27 08:26:58 2024 PDT
Origin         : multimedia/ffmpeg3
Architecture   : FreeBSD:14:amd64
Prefix         : /usr/local/ffmpeg3
Categories     : net multimedia audio
Licenses       : LGPL21+ and GPLv2+
Maintainer     : kreinholz@gmail.com
WWW            : https://ffmpeg.org/
Comment        : Realtime audio/video encoder/converter and streaming server (legacy 3.* series)
Options        :
        ASM            : off
        OPTIMIZED_CFLAGS: on
        RTCPU          : on
        VAAPI          : off
        VDPAU          : off
        X264           : off
Shared Libs provided:
        libswscale.so.4
        libswresample.so.2
        libavutil.so.55
        libavformat.so.57
        libavcodec.so.57
Annotations    :
        FreeBSD_version: 1401502
        cpe            : cpe:2.3:a:ffmpeg:ffmpeg:3.0.2:::::freebsd14:x64
        repo_type      : binary
        repository     : Synth
Flat size      : 14.7MiB
Description    :
FFmpeg is a complete, cross-platform solution to record, convert and stream
audio and video. It includes libavcodec - the leading audio/video codec
library. This is a custom build of FFmpeg-3.0.2, a legacy version released in 2016, intended for use with PPSSPP.
Comment 6 Daniel Engberg freebsd_committer freebsd_triage 2024-10-27 21:53:21 UTC
Sounds great, it needs a PREFIX like ffmpeg4
https://cgit.freebsd.org/ports/tree/multimedia/ffmpeg4/Makefile#n31

Given it's for a specific port I think if possible it would be best if ppsspp would link ffmpeg using static libs so ffmpeg3 only would be a build dep but I'm not sure how much work that would involve on the pppsspp side.
Comment 7 Daniel Engberg freebsd_committer freebsd_triage 2024-10-27 21:54:09 UTC
Given that ASM is disabled I wonder if you also could drop binutils and nasm as a dependencies.
Comment 8 Kevin Reinholz 2024-10-27 23:41:49 UTC
Created attachment 254585 [details]
Updated patch - disables nasm, binutils, and other unnecessary build deps

Updated patch that drops binutils and nasm as dependencies: the only remaining build dependencies are gmake, pkgconf, and perl.

Deleted patches for ffmpeg3 that are no longer needed due to disabling of extraneous options.

PREFIX like multimedia/ffmpeg4's Makefile at lines 31-34 is at lines 23-26 of ffmpeg3 Makefile (lines 70-73 of unified diff patch to add multimedia/ffmpeg3 to the Ports tree).

I'll experiment with trying to get emulators/ppsspp to build with static ffmpeg3 libs, so this can become only a build dep.
Comment 9 Daniel Engberg freebsd_committer freebsd_triage 2024-10-28 00:47:36 UTC
I have a slightly reworked version of the Makefile to make the port a bit more tidy, I'll try to wrap it up tomorrow and post it here.
Comment 10 Daniel Engberg freebsd_committer freebsd_triage 2024-10-28 19:03:31 UTC
Created attachment 254601 [details]
Patch for ffmpeg3 v2

* Add missing option for LTO
* Fix RTCPU option
* Rework Makefile to follow Porters Handbook and have better readability
* Enable both LTO and OPTIMIZED_CFLAGS options by default
* Convert DEBUG option to rely on framework DEBUG option
* Limit port to aarch64 and amd64 as they're the only platforms that will run ppsspp at reasonable performance
* Remove DATADIR and DOCSDIR variables as they're unnecessary and unused
* Backport ATRAC3 patch imported into upstream's "fork" [1]

1: https://github.com/hrydgard/ppsspp-ffmpeg/commit/9c4f84d9d9ad147f4a44cff582829647a0c65420
Comment 11 Kevin Reinholz 2024-10-28 22:27:28 UTC
Daniel,

Thank you very much for reviewing and for your improved patch. (And Commit 9c4f84d from the PPSSPP devs!)

I tested building your version of ffmpeg3 both manually and with synth, and it works great. PPSSPP built against ffmpeg3 with your updates was able to play a game with an early FMV that reliably caused PPSSPP built against higher versions of ffmpeg to crash, so these updated options look good.

I did experiment with building ffmpeg3 static libs (just swapped CONFIGURE_ARGS to --enable-static and --disable-shared, and of course updated pkg-plist accordingly).

ffmpeg3 builds static libs fine, but PPSSPP gets 99% of the way through building against static ffmpeg3, then dies during the staging portion of the build with undefined symbol errors related to lzma and bz2:

ld: error: undefined symbol: lzma_stream_decoder
>>> referenced by tiff.c:396 (libavcodec/tiff.c:396)
>>>               tiff.o:(decode_frame) in archive /usr/local/ffmpeg3/lib/libavcodec.a

ld: error: undefined symbol: lzma_code
>>> referenced by tiff.c:401 (libavcodec/tiff.c:401)
>>>               tiff.o:(decode_frame) in archive /usr/local/ffmpeg3/lib/libavcodec.a

ld: error: undefined symbol: lzma_end
>>> referenced by tiff.c:402 (libavcodec/tiff.c:402)
>>>               tiff.o:(decode_frame) in archive /usr/local/ffmpeg3/lib/libavcodec.a

ld: error: undefined symbol: BZ2_bzDecompressInit
>>> referenced by matroskadec.c:1327 (libavformat/matroskadec.c:1327)
>>>               matroskadec.o:(matroska_decode_buffer) in archive /usr/local/ffmpeg3/lib/libavformat.a
ld: error: undefined symbol: BZ2_bzDecompress
>>> referenced by matroskadec.c:1342 (libavformat/matroskadec.c:1342)
>>>               matroskadec.o:(matroska_decode_buffer) in archive /usr/local/ffmpeg3/lib/libavformat.a

ld: error: undefined symbol: BZ2_bzDecompressEnd
>>> referenced by matroskadec.c:1345 (libavformat/matroskadec.c:1345)
>>>               matroskadec.o:(matroska_decode_buffer) in archive /usr/local/ffmpeg3/lib/libavformat.a
>>> referenced by matroskadec.c:1335 (libavformat/matroskadec.c:1335)
>>>               matroskadec.o:(matroska_decode_buffer) in archive /usr/local/ffmpeg3/lib/libavformat.a
c++: error: linker command failed with exit code 1 (use -v to see invocation)


I ran `ldd` on the shared lib versions of libavcodec.so and libavformat.so, and sure enough lzma and bz2 are properly linked:

/usr/local/ffmpeg3/lib/libavcodec.so:
        libswresample.so.2 => /usr/local/ffmpeg3/lib/libswresample.so.2 (0x18e4d4e54000)
        libavutil.so.55 => /usr/local/ffmpeg3/lib/libavutil.so.55 (0x18e4d505f000)
        libm.so.5 => /lib/libm.so.5 (0x18e4d570c000)
        liblzma.so.5 => /usr/lib/liblzma.so.5 (0x18e4d8153000)
        libz.so.6 => /lib/libz.so.6 (0x18e4d8f88000)
        libthr.so.3 => /lib/libthr.so.3 (0x18e4d9ae1000)
        libc.so.7 => /lib/libc.so.7 (0x18e4d4514000)
        libmd.so.6 => /lib/libmd.so.6 (0x18e4d9e3d000)

/usr/local/ffmpeg3/lib/libavformat.so:
        libavcodec.so.57 => /usr/local/ffmpeg3/lib/libavcodec.so.57 (0x995d3e00000)
        libavutil.so.55 => /usr/local/ffmpeg3/lib/libavutil.so.55 (0x995d18e1000)
        libm.so.5 => /lib/libm.so.5 (0x995d2b33000)
        libbz2.so.4 => /usr/lib/libbz2.so.4 (0x995d306c000)
        libz.so.6 => /lib/libz.so.6 (0x995d551a000)
        libthr.so.3 => /lib/libthr.so.3 (0x995d6a3b000)
        libc.so.7 => /lib/libc.so.7 (0x995d12c2000)
        libswresample.so.2 => /usr/local/ffmpeg3/lib/libswresample.so.2 (0x995d5ce2000)
        liblzma.so.5 => /usr/lib/liblzma.so.5 (0x995d7708000)
        libmd.so.6 => /lib/libmd.so.6 (0x995d7f81000)

So I'm not surprised those undefined symbol errors do not occur when building PPSSPP against shared ffmpeg3 libs.

The linker errors occur with static ffmpeg3 libs regardless of whether I hack PPSSPP's CMakeLists.txt to have it find ffmpeg in /usr/local/ffmpeg3 (system_ffmpeg, albeit static libs) or whether I manually copy over the contents of /usr/local/ffmpeg3/ to /usr/ports/emulators/ppsspp/work/ppsspp-1.17.1/ffmpeg/FreeBSD/x86_64/ and then add a conditional so cmake finds bundled_ffmpeg where it would be expected if PPSSPP shipped with precompiled ffmpeg binaries for FreeBSD.

I haven't been smart enough to pinpoint the exact problem yet, but I suspect it has to do with the way static ffmpeg3 is built, and it continuing to link against the shared version of lzma and bz2 rather than picking up the static versions (which I confirmed exist in /usr/lib alongside the shared versions), or perhaps omitting them altogether.

ffmpeg3 with shared libs is really easy to set as a dependency of emulators/ppsspp with minimal changes to the existing port; getting PPSSPP to build against static ffmpeg3 on FreeBSD is going to be more of a project. I'm not sure how much the juice is worth the squeeze to get ppsspp to build against static ffmpeg3 so it can serve solely as a build dependency and not hang around the system after ppsspp is built.
Comment 12 Kevin Reinholz 2024-10-30 03:24:02 UTC
After more playing around with ffmpeg3 and ppsspp, I have an updated version of the proposed multimedia/ffmpeg3 port. It builds static libs, and disables/enables only those specific things flagged by the PPSSPP devs in their ppsspp-ffmpeg fork.

One very nice result is this now builds without LDFLAGS+= -Wl,--undefined-version

That always bothered me. Additionally, emulators/ppsspp, with small modifications, builds against ffmpeg3 static libs and bundles them into the final PPSSPP--so synth or poudriere won't install ffmpeg3 onto the system and the resulting ppsspp package does not have any version of ffmpeg as a lib depends.
Comment 13 Kevin Reinholz 2024-10-30 03:34:28 UTC
Created attachment 254629 [details]
Updated patch - disables more aggressively to match ppsspp-ffmpeg

This latest update builds ffmpeg3 static libs, and eliminates additional options not needed by ppsspp. Undefined symbols are a thing of the past, so no LDFLAGS are needed to hide the mess. It builds cleanly now and installs static libs to /usr/local/ffmpeg3/lib. Intended solely as a build dependency of emulators/ppsspp.
Comment 14 Kevin Reinholz 2024-10-30 03:43:03 UTC
Created attachment 254630 [details]
Related patch to make emulators/ppsspp build with ffmpeg3 static libs

Related--but not part of the proposed multimedia/ffmpeg3 port.

This patch modifies the existing emulators/ppsspp port to remove multimedia/ffmpeg as a LIB_DEPENDS and instead pick up multimedia/ffmpeg3 as a BUILD_DEPENDS. Since ffmpeg3 is  built with static libs, resulting PPSSPP package does not depend on any version of ffmpeg.

Tested with both Ports and synth.

If multimedia/ffmpeg3 is accepted as a new port, a separate patch will be submitted against emulators/ppsspp--this is just for proof of concept if anyone would like to test now.
Comment 15 Kevin Reinholz 2024-11-09 16:12:56 UTC
Created attachment 255054 [details]
Proposed update to Makefile--since this is a build dep of emulators/ppsspp, ARCH restrictions should match

emulators/ppsspp has the following ARCH restrictions:

NOT_FOR_ARCHS=  mips mips64 powerpc powerpc64 powerpcspe
NOT_FOR_ARCHS_REASON=   only little-endian is supported, see \
                https://github.com/hrydgard/ppsspp/issues/8823

multimedia/ffmpeg3, as a build dep of emulators/ppsspp, should have matching restrictions, rather than be limited to aarch64 and amd64. (Either that or emulators/ppsspp should be limited to aarch64 and amd64).
Comment 16 Daniel Engberg freebsd_committer freebsd_triage 2024-11-10 18:57:54 UTC
Looks good, I'll try to find some time to review it during next week
Comment 17 Kevin Reinholz 2024-11-16 00:49:44 UTC
Created a new bug report/proposed patch under emulators/ppsspp, updating current version 1.17.1 to latest upstream, 1.18.1:

https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=282792
Comment 18 commit-hook freebsd_committer freebsd_triage 2024-11-23 08:54:40 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/ports/commit/?id=2438c611d73764c64186c48b2789036602f7bd21

commit 2438c611d73764c64186c48b2789036602f7bd21
Author:     Kevin Reinholz <kreinholz@gmail.com>
AuthorDate: 2024-11-17 05:16:29 +0000
Commit:     Daniel Engberg <diizzy@FreeBSD.org>
CommitDate: 2024-11-23 08:42:35 +0000

    multimedia/ffmpeg3: New port: Legacy branch of ffmpeg 3.*

    To be used exclusively with emulators/ppsspp as newer versions are
    incompatible due to how the hardware in the Sony PSP works.
    FFmpeg is stripped down to avoid incompatbilities with external
    dependencies, only provides static libraries to avoid getting
    pull in unintentionally and is statically linked to PPSSPP.

    PR:             282188, 282792

 multimedia/Makefile                                |   1 +
 multimedia/ffmpeg3/Makefile (new)                  | 128 +++++++++++++++++++++
 multimedia/ffmpeg3/distinfo (new)                  |   5 +
 multimedia/ffmpeg3/files/patch-configure (new)     |  17 +++
 .../ffmpeg3/files/patch-libavformat_rtsp.c (new)   |  44 +++++++
 .../ffmpeg3/files/patch-libavutil_common.h (new)   |  13 +++
 multimedia/ffmpeg3/override.mk (new)               |   8 ++
 multimedia/ffmpeg3/pkg-descr (new)                 |   3 +
 multimedia/ffmpeg3/pkg-plist (new)                 |  99 ++++++++++++++++
 9 files changed, 318 insertions(+)