Build on Linux: compile locally, link/sign on the iPhone; FFmpeg audio decoders; SDK stub patching

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
astar76 2026-10-27 20:22:19 -05:00
commit 66f4e7e3ea
4 changed files with 700 additions and 48 deletions

50
Native/build-ffmpeg.sh Executable file
View file

@ -0,0 +1,50 @@
#!/bin/bash
# Cross-builds an audio-only FFmpeg (static libs) for iOS arm64 with the swift.org toolchain's clang.
# Output: Native/ffmpeg/{include,lib}. Run once; build.sh links it when present.
set -euo pipefail
cd "$(dirname "$0")"
SRC=${FFMPEG_SRC:-$HOME/.local/opt/dl/ffmpeg/ffmpeg-7.1.1}
TC=${SWIFT:-$HOME/.local/opt/swift/usr/bin}
SDK=${SDK:-$HOME/.local/opt/dl/sdk18/iPhoneOS18.6.sdk}
PREFIX=$PWD/ffmpeg
PHONE_LD=$(cd .. && pwd)/tools/phone-ld
WORK=$PWD/../build/ffmpeg-obj
mkdir -p "$WORK"
cd "$WORK"
CC="$TC/clang -target arm64-apple-ios17.0 -isysroot $SDK"
decoders=(mp3 mp3float mp2 mp2float mp1 aac aac_latm ac3 eac3 alac flac vorbis opus wmav1 wmav2 wmapro wmalossless
wmavoice ape wavpack mpc7 mpc8 tta tak shorten als truehd mlp dca atrac3 atrac3p cook ra_144 ra_288
dsd_lsbf dsd_msbf dsd_lsbf_planar dsd_msbf_planar amrnb amrwb gsm gsm_ms speex qdm2 qdmc nellymoser
adpcm_ima_wav adpcm_ms 'pcm_*')
demuxers=(ogg matroska webm_dash_manifest asf asf_o ape wv mpc mpc8 tta tak shorten dsf iff aiff wav w64 mp3 aac
ac3 eac3 flac mov amr caf truehd mlp dts xwma rm voc au sox)
parsers=(mpegaudio aac aac_latm ac3 flac vorbis opus dca mlp tak gsm amr)
join() { local IFS=,; echo "$*"; }
"$SRC/configure" \
--prefix="$PREFIX" \
--enable-cross-compile --target-os=darwin --arch=aarch64 \
--cc="$CC" --ld="$PHONE_LD" \
--ar="$TC/llvm-ar" --ranlib="$TC/llvm-ranlib" --nm="nm" --strip=true \
--extra-cflags="-mios-version-min=17.0 -fno-stack-check" \
--extra-ldflags="-mios-version-min=17.0" \
--disable-everything --disable-programs --disable-doc --disable-debug \
--disable-avdevice --disable-avfilter --disable-swscale --disable-network \
--disable-autodetect --enable-pic --enable-static --disable-shared \
--enable-avformat --enable-avcodec --enable-swresample \
--enable-protocol=file \
--enable-decoder="$(join "${decoders[@]}")" \
--enable-demuxer="$(join "${demuxers[@]}")" \
--enable-parser="$(join "${parsers[@]}")"
# configure finds the symbol prefix with nm, and GNU nm can't read Mach-O: Darwin uses "_".
sed -i -e 's/^#define EXTERN_PREFIX ""/#define EXTERN_PREFIX "_"/' -e 's/^#define EXTERN_ASM *$/#define EXTERN_ASM _/' config.h
grep -q 'EXTERN_PREFIX "_"' config.h
make -j"${JOBS:-3}"
make install
ls -la "$PREFIX/lib"

31
Native/compat.c Normal file
View file

@ -0,0 +1,31 @@
// Runtime helpers that Xcode's compiler-rt normally provides (absent from the Linux toolchain).
#include <stdint.h>
#include <stdlib.h>
#include <sys/sysctl.h>
static int32_t os_major = -1, os_minor, os_patch;
static void load_version(void) {
char buf[32] = {0};
size_t n = sizeof buf - 1;
os_major = 0;
if (sysctlbyname("kern.osproductversion", buf, &n, NULL, 0) != 0) return;
char *p = buf;
os_major = (int32_t)strtol(p, &p, 10);
if (*p == '.') os_minor = (int32_t)strtol(p + 1, &p, 10);
if (*p == '.') os_patch = (int32_t)strtol(p + 1, &p, 10);
}
/// Backs `if #available(...)` / `@available` checks. Platform is ignored: this binary only runs on iOS.
int32_t __isPlatformVersionAtLeast(uint32_t platform, uint32_t major, uint32_t minor, uint32_t subminor) {
(void)platform;
if (os_major < 0) load_version();
if (os_major != (int32_t)major) return os_major > (int32_t)major;
if (os_minor != (int32_t)minor) return os_minor > (int32_t)minor;
return os_patch >= (int32_t)subminor;
}
/// Older form used by `__builtin_available` in C.
int32_t __isOSVersionAtLeast(int32_t major, int32_t minor, int32_t subminor) {
return __isPlatformVersionAtLeast(2, (uint32_t)major, (uint32_t)minor, (uint32_t)subminor);
}

187
Native/ffdecode.c Normal file
View file

@ -0,0 +1,187 @@
#include "ffdecode.h"
#include <stdlib.h>
#include <string.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libswresample/swresample.h>
#include <libavutil/opt.h>
#include <libavutil/channel_layout.h>
#define OUT_RATE 48000
struct ff_dec {
AVFormatContext *fmt;
AVCodecContext *codec;
SwrContext *swr;
AVPacket *pkt;
AVFrame *frame;
int stream;
int eof;
/* converted samples not yet handed out */
float *bufL, *bufR;
int bufLen, bufPos, bufCap;
/* frames to drop after a seek so we land exactly */
int64_t skip;
};
static int setup_swr(ff_dec *d) {
AVChannelLayout out = AV_CHANNEL_LAYOUT_STEREO;
if (d->swr) swr_free(&d->swr);
AVChannelLayout in;
if (d->codec->ch_layout.nb_channels > 0) av_channel_layout_copy(&in, &d->codec->ch_layout);
else av_channel_layout_default(&in, 2);
int r = swr_alloc_set_opts2(&d->swr, &out, AV_SAMPLE_FMT_FLTP, OUT_RATE,
&in, d->codec->sample_fmt, d->codec->sample_rate, 0, NULL);
av_channel_layout_uninit(&in);
if (r < 0) return r;
return swr_init(d->swr);
}
ff_dec *ff_open(const char *path) {
ff_dec *d = calloc(1, sizeof *d);
if (!d) return NULL;
if (avformat_open_input(&d->fmt, path, NULL, NULL) < 0) goto fail;
if (avformat_find_stream_info(d->fmt, NULL) < 0) goto fail;
const AVCodec *dec = NULL;
d->stream = av_find_best_stream(d->fmt, AVMEDIA_TYPE_AUDIO, -1, -1, &dec, 0);
if (d->stream < 0 || !dec) goto fail;
d->codec = avcodec_alloc_context3(dec);
if (!d->codec) goto fail;
if (avcodec_parameters_to_context(d->codec, d->fmt->streams[d->stream]->codecpar) < 0) goto fail;
if (avcodec_open2(d->codec, dec, NULL) < 0) goto fail;
if (setup_swr(d) < 0) goto fail;
d->pkt = av_packet_alloc();
d->frame = av_frame_alloc();
if (!d->pkt || !d->frame) goto fail;
for (unsigned i = 0; i < d->fmt->nb_streams; i++)
if ((int)i != d->stream) d->fmt->streams[i]->discard = AVDISCARD_ALL;
return d;
fail:
ff_close(d);
return NULL;
}
int64_t ff_frames(ff_dec *d) {
AVStream *st = d->fmt->streams[d->stream];
if (st->duration > 0 && st->time_base.den)
return av_rescale_q(st->duration, st->time_base, (AVRational){1, OUT_RATE});
if (d->fmt->duration > 0) return av_rescale(d->fmt->duration, OUT_RATE, AV_TIME_BASE);
return 0;
}
static int ensure_cap(ff_dec *d, int n) {
if (n <= d->bufCap) return 0;
float *l = realloc(d->bufL, sizeof(float) * n);
if (!l) return -1;
d->bufL = l;
float *r = realloc(d->bufR, sizeof(float) * n);
if (!r) return -1;
d->bufR = r;
d->bufCap = n;
return 0;
}
/* Decodes the next chunk into the internal buffer. Returns >0 frames, 0 at EOF, <0 on error. */
static int refill(ff_dec *d) {
for (;;) {
int r = avcodec_receive_frame(d->codec, d->frame);
if (r == 0) {
if (d->frame->sample_rate != d->codec->sample_rate ||
av_channel_layout_compare(&d->frame->ch_layout, &d->codec->ch_layout)) {
d->codec->sample_rate = d->frame->sample_rate;
av_channel_layout_uninit(&d->codec->ch_layout);
av_channel_layout_copy(&d->codec->ch_layout, &d->frame->ch_layout);
d->codec->sample_fmt = d->frame->format;
if (setup_swr(d) < 0) return -1;
}
int want = swr_get_out_samples(d->swr, d->frame->nb_samples);
if (ensure_cap(d, want + 64) < 0) return -1;
uint8_t *out[2] = {(uint8_t *)d->bufL, (uint8_t *)d->bufR};
int n = swr_convert(d->swr, out, d->bufCap, (const uint8_t **)d->frame->extended_data, d->frame->nb_samples);
av_frame_unref(d->frame);
if (n < 0) return n;
d->bufLen = n; d->bufPos = 0;
if (n > 0) return n;
continue;
}
if (r == AVERROR_EOF) {
/* flush the resampler tail */
if (ensure_cap(d, 4096) < 0) return -1;
uint8_t *out[2] = {(uint8_t *)d->bufL, (uint8_t *)d->bufR};
int n = swr_convert(d->swr, out, d->bufCap, NULL, 0);
d->bufLen = n > 0 ? n : 0; d->bufPos = 0;
return d->bufLen;
}
if (r != AVERROR(EAGAIN)) return r;
if (d->eof) { avcodec_send_packet(d->codec, NULL); continue; }
r = av_read_frame(d->fmt, d->pkt);
if (r < 0) {
d->eof = 1;
avcodec_send_packet(d->codec, NULL);
continue;
}
if (d->pkt->stream_index == d->stream) avcodec_send_packet(d->codec, d->pkt);
av_packet_unref(d->pkt);
}
}
int ff_read(ff_dec *d, float *left, float *right, int max) {
int done = 0;
while (done < max) {
if (d->bufPos >= d->bufLen) {
int r = refill(d);
if (r < 0) return done > 0 ? done : r;
if (r == 0) break;
}
int avail = d->bufLen - d->bufPos;
if (d->skip > 0) {
int drop = avail < d->skip ? avail : (int)d->skip;
d->bufPos += drop; d->skip -= drop;
continue;
}
int n = avail < max - done ? avail : max - done;
memcpy(left + done, d->bufL + d->bufPos, sizeof(float) * n);
memcpy(right + done, d->bufR + d->bufPos, sizeof(float) * n);
d->bufPos += n; done += n;
}
return done;
}
void ff_seek(ff_dec *d, int64_t frame) {
AVStream *st = d->fmt->streams[d->stream];
int64_t ts = av_rescale_q(frame, (AVRational){1, OUT_RATE}, st->time_base);
if (st->start_time != AV_NOPTS_VALUE) ts += st->start_time;
if (av_seek_frame(d->fmt, d->stream, ts, AVSEEK_FLAG_BACKWARD) < 0)
av_seek_frame(d->fmt, -1, av_rescale(frame, AV_TIME_BASE, OUT_RATE), AVSEEK_FLAG_BACKWARD);
avcodec_flush_buffers(d->codec);
setup_swr(d);
d->eof = 0;
d->bufLen = d->bufPos = 0;
/* Decode forward to the exact frame: find where we landed from the first packet's pts. */
d->skip = 0;
for (;;) {
int r = av_read_frame(d->fmt, d->pkt);
if (r < 0) { d->eof = 1; break; }
if (d->pkt->stream_index != d->stream) { av_packet_unref(d->pkt); continue; }
if (d->pkt->pts != AV_NOPTS_VALUE) {
int64_t at = d->pkt->pts - (st->start_time != AV_NOPTS_VALUE ? st->start_time : 0);
int64_t landed = av_rescale_q(at, st->time_base, (AVRational){1, OUT_RATE});
if (frame > landed) d->skip = frame - landed;
}
avcodec_send_packet(d->codec, d->pkt);
av_packet_unref(d->pkt);
break;
}
}
void ff_close(ff_dec *d) {
if (!d) return;
if (d->swr) swr_free(&d->swr);
if (d->codec) avcodec_free_context(&d->codec);
if (d->fmt) avformat_close_input(&d->fmt);
if (d->pkt) av_packet_free(&d->pkt);
if (d->frame) av_frame_free(&d->frame);
free(d->bufL);
free(d->bufR);
free(d);
}

15
Native/ffdecode.h Normal file
View file

@ -0,0 +1,15 @@
// Minimal FFmpeg audio decoder for Mulberry: any input -> 48 kHz stereo float (planar).
#pragma once
#include <stdint.h>
typedef struct ff_dec ff_dec;
/// Opens a file; returns NULL if FFmpeg can't decode it.
ff_dec *ff_open(const char *path);
/// Total length in 48 kHz frames (0 when unknown).
int64_t ff_frames(ff_dec *d);
/// Reads up to `max` frames into left/right; returns frames written, 0 at end, <0 on error.
int ff_read(ff_dec *d, float *left, float *right, int max);
/// Seeks to a 48 kHz frame position.
void ff_seek(ff_dec *d, int64_t frame);
void ff_close(ff_dec *d);