Skip to content

fix(amd): Set decoder.sample_aspect_ratio to 1:1 if source_ctx.sample_aspect_ratio is none - #405

Open
zizouqi wants to merge 6 commits into
Kruk2:mainfrom
zizouqi:main
Open

zizouqi wants to merge 6 commits into
Kruk2:mainfrom
zizouqi:main

Conversation

@zizouqi

@zizouqi zizouqi commented Sep 14, 2026

Copy link
Copy Markdown

Two AMD (ROCm/AMF) fixes, both found on an RX 9070 XT:

1. 'NoneType' object has no attribute 'numerator' during AMF decode (jasna/media/video_decoder.py)

Cause: For videos with an undeclared sample aspect ratio (codecpar SAR 0/1 — common in MKV files), PyAV 18 returns None from sample_aspect_ratio. _setup_amf_decoder() copied that None onto the AMF decoder context, and PyAV's setter crashed reading None.numerator. AttributeError was also not caught by the software-decode fallback, so the job died. NVIDIA is unaffected (NVDEC path never copies the SAR).

Fix: Fall back to square pixels when the source SAR is unknown, and catch AttributeError in the fallback:

if source_ctx.sample_aspect_ratio is not None:
    decoder.sample_aspect_ratio = source_ctx.sample_aspect_ratio
else:
    decoder.sample_aspect_ratio = Fraction(1, 1)

SAR-less videos now decode normally through AMF hardware decoding, and any future setup error still degrades to software decoding as designed.

2. Smart rendering fails on H.264 sources with more than 3 consecutive B-frames (jasna/pipeline.py)

Cause: Smart rendering must match the source's encoding parameters in re-rendered segments. AMF's H.264 encoder caps at 3 consecutive B-frames (NVENC accepts more), so sources using 4+ (e.g. stock x264 bframes=4 encodes) raised SmartRenderCompatibilityError: AMF H.264 smart rendering supports at most 3 consecutive B-frames and the job failed.

Fix: In _run_smart(), detect the case (AMD device + H.264 + max_b_frames > 3 from the keyframe index) and fall back to _run_full() — a normal full re-encode — instead of failing:

if (
    vendor_for_device(self.device) is AcceleratorVendor.AMD
    and codec == "h264"
    and index.max_b_frames > 3
):
    log.warning(
        "%s uses %d consecutive B-frames; AMF H.264 smart rendering supports "
        "at most 3, falling back to a full re-encode",
        self.input_video,
        index.max_b_frames,
    )
    self._run_full(metadata)
    return

The original error in _amf_h264_settings() is kept as a safety net; the NVIDIA path and all other smart-render validations are unchanged.

Fix Bug: 'NoneType' object has no attribute 'numerator' (AMD / AMF decode)
Set decoder.sample_aspect_ratio to 1:1 if source_ctx.sample_aspect_ratio is none
This reverts commit 65c895c.
Set decoder.sample_aspect_ratio to 1:1 if source_ctx.sample_aspect_ratio is none.
Full re-encode for B-frames > 3
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