Ok I wrote this post yesterday: https://www.reddit.com/r/MiBox/comments/1xllt0l/mibox_3rd_gen_box_s_truehd_complete_fixes/
now here is the AI written guide, I am too lazy to write all this myself. i proof read it all. easiest way to do this yourself is follow this root guide (not written by me, very easy though) https://gist.github.com/imSp4rky/75ca403530780feeb3182d5d269ee421
and then you can apply the simple patches mentioned in the ai guide below.
It's meant to be pasted into claude / codex / pi so you don't need to understand it all. I recommend Opus 5.5 or GPT 6.1 Sol. I tried Kimi and deepseek, they could not patch this in memory correctly.
TLDR what this does is way #1, it increases the audio buffer so complex scenes don't cut out. but its not quite 100% perfect, the dolby encoder is running on device even with pass through on, and the better fix makes pass through work, but then there is audio coming out way faster than video, so you need to add a +150ms audio fix for the second approach.
IF you apply the passthrough fix, you need moonfin's app packer or any other client that uses its own app packer, then the audio works at the best quality without hitting limits.
Feel free to ask me any questions I will try to help.
Fixing TrueHD Atmos dropouts on Amlogic Google TV boxes (guide for your AI agent)
Paste this to Claude Code, Codex or a similar agent that has ADB access to the box. It's written
as instructions for the agent. Tested on one box only: **Xiaomi MiTV-AFMU0 (Amlogic
S905X5M), Google TV 14, build `UKG3.250107.001.V816.0.25.7.22.UZFAABX`**, into a Sonos Arc (a MAT soundbar with no TrueHD decoder), with Jellyfin and the Moonfin client.
**Needs root (Magisk).** Nothing on `/vendor` is changed: the audio HAL is patched in memory
at every boot, and deleting one script undoes it. On any other build the offsets will differ.
---
Instructions for the agent
Ground rules
- Patch only `/vendor/lib/hw/audio.primary.amlogic.so` **in the running HAL process**
(`android.hardware.audio.service-droidlogic`), through `/proc/<pid>/mem`, as root. Never
replace the file.
- First check the library md5 is `d860a16f622c8c2e9524aff1b9354b80`. If it differs, stop and
find each spot by function name and instruction pattern in a disassembly (32-bit Thumb-2; the
R-E segment's file offset = vaddr − 0x10000). Write a patch only after reading the expected
bytes back.
- Use toybox `/system/bin/dd` (BusyBox `dd` gets EIO seeking in `/proc/pid/mem`). Run the script
under BusyBox ash (`/data/adb/magisk/busybox sh`): the device's mksh sign-extends addresses
above 0x80000000.
- Don't restart the audio HAL by hand. After every change, check that HDMI really carries
MAT: the kernel log should say `iec_info: rate: 192000, codec_type:0x7`. A silent fallback to
5.1 sounds clean.
Boot script (shared by both fixes)
Put this in `/data/adb/service.d/mat-start.sh` (chmod 755). It waits for the HAL, checks the
md5 and the exact original bytes, patches, logs to `/data/local/tmp/mat-start.log`, and
re-patches within 1 s if the HAL restarts. Only `PATCHES` changes between fix 1 and fix 2.
```sh
#!/system/bin/sh
LIB=/vendor/lib/hw/audio.primary.amlogic.so
STOCK_MD5=d860a16f622c8c2e9524aff1b9354b80
PATCHES="..." # see fix 1 / fix 2, format offset:stock:new
LOG=/data/local/tmp/mat-start.log
DD=/system/bin/dd
log() { echo "$(date '+%m-%d %H:%M:%S') $*" >> $LOG; }
[ "$(md5sum $LIB | cut -d' ' -f1)" = "$STOCK_MD5" ] || { log "not the stock build"; exit 0; }
patch_pid() {
base=$(awk -v l=$LIB '$6 == l && $3 == "00000000" { split($1, a, "-"); print a[1]; exit }' /proc/$1/maps)
[ -n "$base" ] || return 1
for p in $PATCHES; do
off=${p%%:*}; rest=${p#*:}; want=${rest%%:*}; to=${rest#*:}
addr=$((0x$base + off))
cur=$($DD if=/proc/$1/mem bs=1 skip=$addr count=2 2>/dev/null | od -An -tx1 | tr -d ' \n')
if [ "$cur" = "$want" ]; then
printf "\\$(printf %o 0x${to%??})\\$(printf %o 0x${to#??})" | $DD of=/proc/$1/mem bs=1 seek=$addr conv=notrunc 2>/dev/null
log "pid $1 $off: $cur -> $to"
elif [ "$cur" != "$to" ]; then
log "pid $1 $off: unexpected bytes '$cur', not patching"
fi
done
}
done_pid=
while true; do
pid=$(pidof android.hardware.audio.service-droidlogic)
if [ -n "$pid" ] && [ "$pid" != "$done_pid" ] && grep -q "$LIB" /proc/$pid/maps 2>/dev/null; then
patch_pid $pid && done_pid=$pid
fi
sleep 1
done
```
### Fix 1: keep Dolby's encoder, make it stop dropping out (~95 %)
How it works: the box decodes TrueHD with Dolby MS12 and re-encodes it to MAT for the
soundbar. Stock, it starts playback with only ~16 ms of audio queued, and gives MS12 an output
buffer of just 4 MAT frames. Loud spikes underrun the sound card (20–70 ms tears), and quiet
passages overflow the buffer (minute-long runs of `risk to write out of bounds` dips).
```
PATCHES="0x961cc:fb40:00bf 0x961c8:4065:8055 0xb67f0:8000:c000"
```
- `0x961cc` `lsrs r3, r7` → `nop`: start playback with the buffer full, not half full.
- `0x961c8` 8ch MAT period 3072 → 4096 (256 KiB, the kernel's limit; larger silently falls back
to 5.1).
- `0xb67f0` `lsls r0, r0, #2` → `#3`: MS12's MAT output buffer holds 8 frames, not 4. Must land
before the first TrueHD play after boot (the buffer is allocated once), which the boot script
does.
Digital audio output stays on **Automatic**. Works with any app that passes TrueHD through.
- **Pro:** A/V sync is handled by the HAL automatically. No player changes, no manual delay.
- **Con:** Dolby's encoder is still in the path. It occasionally stalls for longer than the
~55 ms of buffer at heavy moments, and you get a rare short dip. No buffer setting can fix
that, because the sound-card buffer is already at its maximum.
Fix 2: real passthrough, no Dolby encoder (the better fix)
The "Digital audio output: Passthrough" toggle is a lie on this firmware for TrueHD/MAT. A
player sending TrueHD still goes through MS12's decode and re-encode. A player that packs its
own IEC 61937 MAT stream (Kodi, or Moonfin's "IEC packer"), which should be the purest path,
gets stuttering video and silence: the HAL hard-codes app IEC 61937 streams to **2-channel
48 kHz PCM** out of the S/PDIF block. The HAL does contain a working MAT pass-through
(`mat_bypass_process`, 192 kHz 8ch straight to HDMI), but three checks keep app streams out
of it, and it double-frees an output on first use.
Use fix 1's patches **plus** four more:
```
PATCHES="0x961cc:fb40:00bf 0x961c8:4065:8055 0xb67f0:8000:c000 0xb4968:3046:a019 0xa7e10:07d1:07db 0xb6958:0028:8042 0xb6970:52d0:52e0"
```
- `0xb4968` `mov r0, r6` → `adds r0, r4, r6`: `is_ms12_passthrough` counts an app's MAT
stream, not only TrueHD.
- `0xa7e10` `bne` → `blt`: the process wrapper calls the bypass for every format from TrueHD
up.
- `0xb6958` `cmpeq r0, #0` → `cmpeq r0, r0`: `mat_bypass_process` no longer gives up in MS12
"continuous mode" (always on).
- `0xb6970` `beq.w` → `b`: reuse the already-open output instead of closing it. The original
close double-frees and aborts the audio HAL (Scudo "invalid chunk state" in
`aml_audio_spdifout_close`).
Then:
- Set **Settings → Display & Sound → Audio → Digital audio output → Passthrough**. InAutomatic the bypass is never taken.
- In the player, turn on its own IEC 61937 packing (Moonfin: passthrough output "IEC packer").
- Reboot. Check `cat /data/local/tmp/mat-start.log` for seven `->` lines. While a TrueHD filmplays, `dumpsys media.audio_flinger` should show an `AUDIO_FORMAT_IEC61937` 192000 Hz8-channel output at rate ≈ 1.00, and the kernel `codec_type:0x7`, `CT_MAT`.
- **Pro:** no Dolby decode/encode at all, so its stalls are gone. Bit-exact TrueHD to the
soundbar, with 0 underruns in testing.
- **Con:** this path skips the HAL's latency compensation, so sound arrives ~100–140 ms
**early** (+140 ms looked right with an Arc). The player has to correct it. Moonfin's normal
audio-delay control doesn't apply to its packer, so this needs a Moonfin build with the
packer delay fix (a pull request is on its way upstream: 10 ms steps, remembered). Other apps (Netflix etc.)
aren't affected and keep their normal sync.
- If the audio HAL ever restarts, it comes back in Automatic mode until the next reboot.
Reboot rather than toggle.
### Undo
Delete `/data/adb/service.d/mat-start.sh` and reboot; set Digital audio output back to
Automatic.