0
Fork 0
mirror of https://github.com/Droid-VM/crosvm.git synced 2026-11-17 06:58:03 +00:00
crosvm/android_codec/src/bitstream.rs
HuJK 423ddccebf android_codec: the MediaCodec NDK crate and codec_probe (M6, design 7.2)
The decoder and encoder devices (VPU_DESIGN.md 7.2, 7.3) must map the
phone's real codec capabilities, not a table, and must drive MediaCodec in
asynchronous mode from a device worker. Before either device exists, a
standalone probe has to enumerate what the platform reports and prove decode
and encode round trips through the NDK under the app uid. This is that
crate, the android_camera pattern applied to libmediandk:

- lib.rs: ndk_api! bindings resolved with dlopen (libmediandk, libbinder_ndk;
  the binder thread pool is started at load because MediaCodec serves a
  BnResourceManagerClient). Everything up to API 28 is required; the API 36
  AMediaCodecStore_* / AMediaCodecInfo_* / ACodec*Capabilities_* surface and
  the AMediaCodecInfo_FEATURE_* data globals are optional groups, so the
  library loads on an older Android and list_codecs falls back to
  create*ByType per mime. list_codecs walks the Store (media types, every
  decoder and encoder with kind/type/vendor/instances, video ranges and
  alignment, per-size rate checks, performance-point coverage, encoder
  quality/complexity/bitrate modes, features) and asks isFormatSupported
  about a candidate profile/level table, since the NDK has no
  getProfileLevels (survey 2.3). Codec is one session: the four async
  callbacks only push onto a mutex-protected queue and signal a base::Event
  eventfd (design 7.2's rule), with flush_and_restart for the async seek.
  MediaFormat wraps AMediaFormat with the keys the survey names, including
  the literal "image-data".
- image.rs: MediaImage2 parsed from the 104-byte "image-data" blob, and
  tight_nv12, the one per-frame repack the decoder device will make
  (memcpy rows for NV12, byte swap for NV21, interleave for planar, generic
  walk otherwise), plus rebased() for survey open question 1.
- bitstream.rs: Annex-B access-unit splitter (H.264/H.265, spec boundary
  rules, csd extraction) and IVF reader/writer with VP8/VP9 key-frame bits.
- synth.rs: the moving test pattern, the padded-layout packer for encoder
  input (stride, slice-height, semiplanar or planar), luma PSNR and digest.
- probe.rs: codec_probe list [--json] / decode / encode / roundtrip, with
  --uid as camera_probe, --csd config|format|inband, the mid-stream
  flush-then-start, the mPlane[0].mOffset verdict, and a >= 30 dB luma
  PSNR round-trip check.

Workspace member; Android.bp mirrors android_camera (installable: false,
apex_available com.android.virt, no libmediandk in shared_libs, phony
codec_probe). Host build, 27 unit tests, clippy and rustfmt pass in the
scratch harness (logs/vpu_wp/scratch-m6); nothing has run on a phone yet.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011BkN7vYhuc1w9iVuqsHzSn
2026-10-28 06:23:28 +08:00

576 lines
21 KiB