media icon indicating copy to clipboard operation
media copied to clipboard

MatroskaExtractor refuses a file with a stray 0x80 element outside Chapters ("Element 128 must be in an EditionEntry")

Open AsemHub opened this issue 2 weeks ago • 2 comments

Version

Media3 1.11.1

More version details

The same code is on main today (MatroskaExtractor.startMasterElement, case ID_CHAPTER_DISPLAY calls getCurrentChapter(id)), so I expect 1.12.0-beta01 to behave the same; I have not run it.

Devices that reproduce the issue

  • Chromecast with Google TV (sabrina), Android 14 (API 34)
  • An Android phone (same app build, same file, same message)

Devices that do not reproduce the issue

None found. It is a parsing decision, not a device one.

Reproducible in the demo app?

Not tested. The app that hit it installs a subclass of MatroskaExtractor (for ASS subtitles); the exception is thrown by MatroskaExtractor's own code, see the stack below.

Reproduction steps

  1. Play a Matroska file whose Segment/Info holds an element with id 0x80. Files written by HandBrake 0.9.4 (libmkv 0.6.4, 2009) have one: the muxing application's name is stored under id 0x80 instead of 0x4D80: 80 8c 6c 69 62 6d 6b 76 20 30 2e 36 2e 34 (libmkv 0.6.4), between SegmentUID and WritingApp.
  2. A 44 KB file with the same defect can be made from any clean file (the element keeps its total length, so no size changes):
ffmpeg -f lavfi -i testsrc=size=320x240:rate=25:duration=3 -f lavfi -i sine=frequency=440:duration=3 \
  -c:v libx264 -pix_fmt yuv420p -c:a aac -shortest clean.mkv
python3 - clean.mkv stray80.mkv <<'PY'
import sys
b = bytearray(open(sys.argv[1], 'rb').read())
info = b.rfind(bytes.fromhex('1549a966'), 0, 4096)          # Info (the last match: the SeekHead names the id too)
i = b.find(bytes.fromhex('4d80'), info, info + 256)          # MuxingApp
n = b[i + 2] & 0x7f                                          # its one-byte size
text = bytes(b[i + 3:i + 3 + n])
b[i:i + 3 + n] = bytes([0x80, 0x80 | (n + 1)]) + text + b' ' # id 0x80, one byte more of text
open(sys.argv[2], 'wb').write(b)
PY

clean.mkv plays; stray80.mkv fails as below (both checked on the Chromecast). ffprobe reads both.

Expected result

The file plays. 0x80 is ChapterDisplay only inside a ChapterAtom; anywhere else it is an element this reader does not know there, and unknown elements are skipped by their size (as DefaultEbmlReader does for ELEMENT_TYPE_UNKNOWN).

Actual result

Playback fails before the first frame with ERROR_CODE_PARSING_CONTAINER_MALFORMED:

androidx.media3.exoplayer.ExoPlaybackException: Source error
Caused by: androidx.media3.common.ParserException: Element 128 must be in an EditionEntry {contentIsMalformed=true, dataType=1}
    at androidx.media3.extractor.mkv.MatroskaExtractor.assertInEditionEntry(MatroskaExtractor.java:1736)
    at androidx.media3.extractor.mkv.MatroskaExtractor.getCurrentChapter(MatroskaExtractor.java:1762)
    at androidx.media3.extractor.mkv.MatroskaExtractor.startMasterElement(MatroskaExtractor.java:879)
    ...
    at androidx.media3.extractor.mkv.DefaultEbmlReader.read(DefaultEbmlReader.java:116)
    at androidx.media3.extractor.mkv.MatroskaExtractor.read(MatroskaExtractor.java:659)

getElementType answers ELEMENT_TYPE_MASTER for ID_CHAPTER_DISPLAY wherever the element stands, and startMasterElement then requires a current chapter. The value ids read through getCurrentChapter (0x73C4, 0x91, 0x92, 0x98, 0x89, 0x85, 0x437C) would throw the same way outside a ChapterAtom. Before chapter parsing was added, such a file played, since 0x80 was unknown and skipped.

A possible fix: treat the chapter-only ids as unknown unless a ChapterAtom is open.

Media

The recipe above produces the file; I can also send the 44 KB stray80.mkv.

Bug Report

  • [ ] You will email the zip file produced by adb bugreport to [email protected] after filing this issue.

AsemHub avatar Nov 18 '26 13:11 AsemHub

  1. Files written by HandBrake 0.9.4 (libmkv 0.6.4, 2009) have one: the muxing application's name is stored under id 0x80 instead of 0x4D80:

This is a bug in libmkv (now fixed) and the resulting file is malformed - the 0x80 element ID shouldn't appear outside the \Segment\Chapters\EditionEntry\+ChapterAtom path. So it is expected that ExoPlayer fails to play it.

Since there may be many similarly broken files in the wild, I have sent a change for internal review that adds a special-case workaround for this muxer bug: 0x80 elements are ignored if they occur outside of a chapter context.

icbaker avatar Nov 18 '26 17:11 icbaker

The workaround is merged in the commit above.

icbaker avatar Nov 19 '26 16:11 icbaker