Skip to content

Unify hardware decoding through DecoderOptions - #45

Merged
mcuadros merged 6 commits into
masterfrom
agent/unified-hardware-decoder
Aug 16, 2026
Merged

Unify hardware decoding through DecoderOptions#45
mcuadros merged 6 commits into
masterfrom
agent/unified-hardware-decoder

Conversation

@mcuadros

Copy link
Copy Markdown
Member

Summary

  • Route hardware acceleration through the normal Decoder and DecoderOptions.
  • Discover compatible hardware decoders and device configurations from FFmpeg capabilities.
  • Prefer platform-native devices while allowing automatic software fallback or required hardware mode.
  • Propagate decoder options through network and image-sequence constructors.
  • Expose decoder selection and fallback diagnostics through VideoDecoderInfo.
  • Reuse automatically created hardware devices by device type.
  • Extend the PureGo bindings and ABI layout checks required for codec and hardware configuration discovery.
  • Add policy, ownership, lifecycle, fallback, and stress coverage.

Behavior

DecoderOptions.Hardware controls video hardware acceleration:

  • nil keeps software decoding.
  • &HWDecoderConfig{} enables automatic platform-aware selection with software fallback.
  • HardwareAccelerationRequired returns ErrHardwareAccelerationUnavailable instead of falling back.
  • HardwareAccelerationDisabled explicitly requests software decoding.
  • Caller-provided HWDevice values are borrowed; automatically created devices are owned by the decoder.

Frames returned by the regular decoder API remain CPU-accessible. GPU-backed frames are transferred to reusable system memory, while wrapper decoders that already return CPU-backed frames avoid that transfer.

VideoDecoderInfo reports pending, selected, active, and fallback states because some FFmpeg accelerators initialize lazily during the first decode operation.

Breaking changes

The separate HWDecoder API and NewHWDecoder constructor are removed. Hardware acceleration now uses the regular decoder:

decoder, err := ffmpeg.NewDecoder(path, &ffmpeg.DecoderOptions{
    Hardware: &ffmpeg.HWDecoderConfig{},
})

Specialized constructors now receive normal decoder options separately from their specific configuration:

ffmpeg.NewNetworkDecoder(url, decoderOptions, protocolOptions)
ffmpeg.NewNetworkDecoderContext(ctx, url, decoderOptions, protocolOptions)
ffmpeg.NewImageSequenceDecoder(sequenceConfig, decoderOptions)

This keeps stream selection, probing, raw FFmpeg options, and hardware policy consistent across decoder entry points.

Validation

  • go test ./...
  • go test -race ./...
  • go vet ./...
  • golangci-lint 1.64.8
  • Windows amd64/arm64 cross-compilation
  • Darwin amd64/arm64 cross-compilation
  • Android API 33 arm64/amd64 cross-compilation
  • ABI layout validation against FFmpeg 6.0.1, 6.1.6, 7.0.3, 7.1.5, 8.0.3, 8.1.2, and 9.0.1
  • Runtime decoding tests across the supported FFmpeg release families
  • ASan/UBSan validation with FFmpeg 6.1.6, 7.1.5, and 8.1.2
  • Repeated automatic-selection lifecycle stress
  • Physical Android playback with MediaCodec active, including audio, pause, seek, loop, fallback diagnostics, and A/V synchronization

Network option merging and cancellation routing are covered deterministically. The test suite does not depend on an external streaming service.

Let network and image-sequence entry points accept the complete DecoderOptions contract while keeping protocol and sequence settings separate. Add cancellable network construction and verify merge precedence and caller ownership.

Assisted-by: OpenAI Codex
Bind codec iteration, decoder classification, hardware configuration discovery, hardware device enumeration, and frame property copying.

Keep every directly accessed AVCodec and AVCodecHWConfig field in the central ABI table and verify the offsets across supported release families.

Assisted-by: OpenAI Codex
Route hardware acceleration through DecoderOptions and the normal Decoder lifecycle. Enumerate compatible codecs and device configurations, prefer platform-native backends, transfer GPU frames to reusable system memory, and make automatic fallback observable while preserving a required mode.

Remove the duplicated public HWDecoder state machine. Decoder owns automatically created devices and borrows caller-provided devices through retained FFmpeg buffer references.

Assisted-by: OpenAI Codex
Cover deep option cloning, automatic fallback diagnostics, required-mode failures at open and lazy initialization, and the unified explicit-device path.

Document capability-driven selection, ownership, system-memory output, and the absence of a separate hardware decoder API.

Assisted-by: OpenAI Codex
Create at most one owned device for each hardware type while trying compatible decoder implementations. This avoids repeated native allocations and duplicate probe failures without changing borrowed-device ownership.

Assisted-by: OpenAI Codex
Exercise automatic hardware selection or software fallback while repeatedly opening, decoding, seeking, and closing audiovisual streams. Keep the extended torture loop opt-in for local validation.

Assisted-by: OpenAI Codex
@mcuadros
mcuadros marked this pull request as ready for review August 16, 2026 19:23
@mcuadros
mcuadros merged commit 78656d9 into master Aug 16, 2026
21 checks passed
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