Skip to content

fix(ios): decode H.264 from a recovery point - #9

Open
andreialecu wants to merge 6 commits into
mainfrom
test/h264-recovery-point
Open

andreialecu wants to merge 6 commits into
mainfrom
test/h264-recovery-point

Conversation

@andreialecu

Copy link
Copy Markdown
Owner

Bug

Issue #2, first item, H.264 part. On iOS, decoding an open-GOP H.264 stream (for example x264 with open-gop=1) from its second sync sample fails, and the decoder closes. That sample is a recovery point: a non-IDR I picture behind a recovery point SEI. The failure happens after a seek. Since #4 it also happens when playback flushes at the GOP boundary and goes on.

Root cause

The B pictures that follow the recovery point in decoding order but are shown before it are leading pictures, and they reference the previous GOP. Since #4 (41e2613), the first key frame after configure or a flush is flagged ResetDecoderBeforeDecoding, so those references are gone. VideoToolbox reports this as -8969, not kVTVideoDecoderReferenceMissingErr (-17694), and -17694 is the only status the decoder drops a frame for.

The flush-then-continue case broke because of that reset. With the reset attachment removed from main's code (a diagnostic build, not committed), that test outputs all 32 frames. The seek test then fails with -12350 instead. Correction to the 842fae0 commit message: it says this case "worked before 41e2613", but ios/ from before 41e2613 was not run on that test. The diagnostic build above is what was observed.

Fix

This mirrors the existing HEVC RASL skip. When a stream starts at a key frame whose first picture NAL unit is not an IDR (types 1 to 4, read with the avcC NAL length size), the decoder skips, before decoding, the non-key samples with an earlier timestamp than that key frame. A stream starts at configure, at a flush, or at a key frame that goes back in time. The skip lasts until the next key frame. The reset stays: the decoder cannot tell a flush that goes on from a seek, and WebCodecs requires a key chunk after a flush. Chromium's WebCodecs also treats an H.264 chunk with a recovery point SEI as a key chunk (ParseH264KeyFrame). -8969 is still not dropped in general, because it can mean real corruption.

Trade-off: leading pictures are always left out, as FFmpeg leaves them out after a seek. That includes leading pictures that might decode, such as in a closed GOP whose key frame is a non-IDR I picture. Telling those apart would mean parsing their slices. VideoToolbox reports missing references inconsistently, and a picture that fails can leave later pictures decoded against a broken reference. In the flush-then-continue case, this means 30 frames instead of 32.

Tests

Two new e2e tests use an x264 open-GOP clip made at test time. The bitstream is checked with trace_headers, and every frame that comes out is compared with the whole clip's decode.

  • Seek: decode the whole clip, flush, then decode from the recovery point. Expected: no error, and the 30 frames from the key frame on.
  • Flush then continue: decode the first GOP, flush, then decode the second GOP. Expected: no error, and the frames from the key frame on, with or without the 2 leading pictures (the spec allows both).
  • Android: both tests assert the current known failure exactly, so any change flips them (see Known gaps).

Red and green runs, iOS Simulator (iPhone 17, iOS 26.5):

ios/ code Seek Flush then continue
Before #4 (0cd1af8) red, -12350 not run
main red, -8969, no frame after the key frame red, -8969, no frame after the key frame
This branch green, 30 frames green, 28 + 30 frames

Full e2e suite on the final commit:

  • iOS Simulator (iPhone 17, iOS 26.5): 67 of 67.
  • Android emulator (API 35, WebCodecs_Bench_A, arm64-v8a debug build): 67 of 67.

yarn tsc, yarn lint and yarn test:unit (65 tests) pass.

main's CI currently fails two tests unrelated to this change, in the iOS job: "starts an AudioDecoder over after a flush (iOS)" and "handles the app going to the background".

Known gaps

  • Mediabunny on React Native: its sink stamps the Nth output with the Nth queued input timestamp. Each skipped leading picture therefore shifts later timestamps one slot early until the next flush. Mediabunny drops HEVC RASL packets itself but has no such guard for H.264. This needs a change in Mediabunny.
  • Android: the emulator's decoders fail this clip from the recovery point, and the tests record it.
    • c2.goldfish.h264.decoder gives "EncodingError: no input buffer", then "flush: AbortError: the codec was reset or closed during flush", and no frame from the key frame on.
    • c2.android.avc.decoder (prefer-software, tried locally) outputs only some frames, with no error.
    • To be fixed separately.
  • Not run on a physical iPhone.
  • Not covered: avc3 without an SPS in the avcC. The NAL length size is read through the format description's parameter sets, so the skip may not apply there (not tested).

Refs #2

Adds an e2e test for issue #2's first item, H.264 part: a clip made at
test time with libx264 open-gop=1 (B-frames, keyint 30) whose second sync
sample (stss) is a non-IDR I picture (nal_unit_type 1, slice_type 7)
behind a recovery point SEI (recovery_frame_cnt 0). The test checks the
bitstream with trace_headers, decodes the whole clip, flushes, decodes
again from that sync sample, and expects no error, every frame from the
I picture on (the leading B pictures may be left out: FFmpeg's decoder
outputs the 30 frames from the I picture on), in order, each matching
the whole clip's picture.

iOS Simulator (iPhone 17, iOS 26.5 and 27.2):
- before 41e2613 (ios/ from 0cd1af8): red, the decoder fails with
  "EncodingError: VideoToolbox failed (-12350)" and outputs no frame
  after the seek;
- on main: still red. The I picture decodes, but VideoToolbox fails the
  first leading B picture with -8969 (codecBadDataErr), not
  kVTVideoDecoderReferenceMissingErr (-17694), the only status the
  decoder drops a frame for: "EncodingError: VideoToolbox failed
  (-8969)", no frame after the seek.

Android emulator (API 35, c2.goldfish.h264.decoder): red, "EncodingError:
no input buffer" after the seek.

The rest of the e2e suite passes on both (58 of 59).
…ts GOP

Adds an e2e test for the case that differs from a seek only in what came
before: the open-GOP x264 clip's first GOP, a flush, then its second GOP
from the recovery point (a non-IDR I picture with a recovery point SEI,
listed as a sync sample). The whole clip is decoded first, for what each
frame looks like.

The expectation comes from WebCodecs, not from what earlier code did:
flush() sets [[key chunk required]], so the next chunk must be a key
chunk, "an encoded chunk that does not depend on any other frames for
decoding", and the AVC registration expects an avc key chunk to be an
IDR picture. A decoder cannot tell this from a seek to the same chunk, so
it may start there as from any key chunk and leave out the two leading B
pictures, which reference the first GOP. Chromium does: its
H264Decoder::Flush() outputs what it holds and then clears its DPB
(media/gpu/h264_decoder.cc). Failing is not allowed: the chunks are valid
and in a valid order. So the test expects no error, the first GOP's 28
frames, then the 30 frames from the recovery point on (iOS) or those plus
the 2 leading ones (other platforms), each looking as in the whole clip.

Moves the clip's ffmpeg arguments and the picture matching into helpers
shared with the seek test.

iOS Simulator (iPhone 17, iOS 26.5), ios/ as on main: red, like the seek
test. The first GOP decodes, then "EncodingError: VideoToolbox failed
(-8969)" and no frame from the second GOP.
…failing

A seek in an open-GOP H.264 stream (x264 open-gop=1, say) can start at a
sync sample that is a recovery point: a non-IDR I picture behind a
recovery point SEI. The B pictures after it in decoding order and shown
before it (leading pictures) reference the GOP before it, which the
decoder does not have after the flush. Since 41e2613 the first key frame
after configure or a flush is flagged ResetDecoderBeforeDecoding, so the
leading pictures have no references and VideoToolbox fails the first one
with -8969 (codecBadDataErr), not kVTVideoDecoderReferenceMissingErr
(-17694), the only status the decoder drops a frame for: the decoder
closed with "decoder-failed". The same happens when playback just flushes
at the GOP boundary and goes on, which worked before 41e2613.

Mirroring the HEVC RASL skip, a stream that starts (configure, flush, or
a key frame going back in time) at a key frame whose first picture NAL
unit is not an IDR (types 1 to 4, read with the avcC's NAL length size)
now skips, before decoding, the non-key samples with an earlier
timestamp than that key frame, until the next key frame. FFmpeg leaves
the same pictures out after a seek, and Chromium clears its DPB on flush.
-8969 is not dropped in general: it can be real corruption. -17694 is
still dropped.

The reset stays on the first key frame of every stream, flush included:
the decoder cannot tell a flush that goes on from a seek, and WebCodecs
requires a key chunk after a flush. Without it, with ios/ otherwise as on
main (a diagnostic build), the flush-then-go-on test outputs all 32
frames, but the seek test fails with "VideoToolbox failed (-12350)", as
before 41e2613. With it, the 2 leading pictures are left out there too.

Skipped pictures produce no output, as with the HEVC skip and the -17694
drop. Mediabunny drops HEVC RASL packets itself before decode() and so
never sees that skip, but has no such guard for H.264: under its sink on
React Native, which stamps the Nth output with the Nth queued input
timestamp, each skipped leading picture still moves later timestamps one
slot early until the next flush (issue #2). That needs a change in
Mediabunny; it is not made worse than a dropped frame, and a closed
decoder was worse.

iOS Simulator, iPhone 17, iOS 26.5 (23F77):
- before (ios/ as on main): both recovery point tests red with
  "EncodingError: VideoToolbox failed (-8969)", no frame after the key
  frame;
- after: both green, 30 frames from the key frame on, each matching the
  whole clip's picture; the HEVC open-GOP (CRA) test green; full e2e
  suite 67 of 67.
With the iOS fix, both recovery point tests pass on iOS: no error, the 30
frames from the recovery point on, each as in the whole clip. Their
comments said they failed there; they now say how.

Android does not decode from the recovery point yet, and its native code
is not changed here, so on Android the tests check only what passes
there (the whole clip, and the first GOP before the flush), with the
observed errors in the comments. On the emulator (API 35, WebCodecs_Bench_A):
- c2.goldfish.h264.decoder (default): after the flush the decoder takes no
  more input: "EncodingError: no input buffer", then "flush: AbortError:
  the codec was reset or closed during flush", no frame after the key
  frame, in both tests;
- c2.android.avc.decoder (hardwareAcceleration "prefer-software", tried
  with a local change to the test helper, not committed): no error, but
  only 9 frames after the seek (933333 to 1233333, 1200000 missing) and
  11 after the flush that goes on, out of 30 or 32.

iOS Simulator (iPhone 17, iOS 26.5): e2e suite 67 of 67. Android emulator
(API 35, emulator-5590): e2e suite 67 of 67. yarn tsc, yarn lint and yarn
test:unit pass.
…s after a flush

Android: both recovery point tests now check the current outcome on the
emulator exactly, so any change, a fix included, flips them: the runs
before the recovery point complete, the run from it empty, and the errors
"EncodingError: no input buffer" then "flush: AbortError: the codec was
reset or closed during flush" (c2.goldfish.h264.decoder, API 35; the same
strings in both tests over two earlier runs). This is a known failure of
the emulator's decoders, to be fixed separately. Before, Android checked
only the runs before the recovery point.

The flush-then-continue test now accepts, on platforms other than
Android, either the frames from the key frame on or those plus the
leading pictures, as WebCodecs allows, each looking as in the whole
clip's decode. The seek test still expects the frames from the key frame
on.

Comments describe the expected behaviour rather than current failures.
The earlier comment said the AVC registration expects an avc key chunk
to be an IDR while calling the chunks valid; it now says why the recovery
point is a key chunk: the MP4 lists it as a sync sample, and Chromium's
WebCodecs takes an H.264 chunk with a recovery point SEI as a key chunk
(ParseH264KeyFrame, third_party/blink/renderer/modules/webcodecs/
video_decoder.cc).
…ays skipped

The comment on the skip now says that leading pictures are skipped even
when they might decode (a closed GOP whose key frame is a non-IDR I
picture): whether they reference earlier frames cannot be told without
parsing their slices, VideoToolbox reports missing references either as
kVTVideoDecoderReferenceMissingErr or as bad data (-8969), which could
also be real corruption, and a failed picture can leave later ones
decoded against a broken reference. No code change.

Correction to 842fae0's message: it says the flush-then-continue case
"worked before 41e2613". What was observed is that a diagnostic build of
main's ios/ with the ResetDecoderBeforeDecoding attachment removed output
all 32 frames in that test; ios/ from before 41e2613 was not run on it.

This branch has not been deployed

No deployments
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.

1 participant