Skip to content

Add AVX SIMD implementations of video_orc_chroma_down_h2_u8 and video_orc_chroma_down_v2_u8 - #64

Draft
havardgraff with Copilot wants to merge 1 commit into
mainfrom
copilot/add-avx-implementation-for-orc-functions
Draft

Add AVX SIMD implementations of video_orc_chroma_down_h2_u8 and video_orc_chroma_down_v2_u8#64
havardgraff with Copilot wants to merge 1 commit into
mainfrom
copilot/add-avx-implementation-for-orc-functions

Conversation

Copilot AI commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

With ORC disabled, video_orc_chroma_down_h2_u8 and video_orc_chroma_down_v2_u8 fall back to scalar C loops processing one element at a time. This adds AVX1 vectorized paths that process 32 bytes per iteration.

Approach

Both functions reduce to: average specific UV bytes, keep other bytes unchanged. This maps cleanly to VPAVGB + VPBLENDVB — average everything, then blend to select only the chroma bytes from the averaged result.

  • h2 (horizontal): UV bytes from the second pixel in each pair need to be shuffled (VPSHUFB) to align with the first pixel's UV position before averaging
  • v2 (vertical): Two source rows are already aligned — just VPAVGB the rows and blend

Implementation details

  • AVX1 onlyVMOVDQU (256-bit load/store), VEXTRACTF128/VINSERTF128 (lane split/merge), VPSHUFB, VPAVGB, VPBLENDVB (128-bit VEX-encoded SSE)
  • Three-tier loop: 32-byte AVX → 16-byte SSE → scalar remainder
  • Runtime dispatch via __builtin_cpu_supports("avx"), falls back to original scalar code on non-AVX CPUs
  • Guarded by DISABLE_ORC && (__GNUC__ || __clang__) && (x86_64 || i386)
  • Original scalar code is completely preserved as-is

Core pattern (v2 shown)

// Average all bytes, then blend to keep AY from s1 and averaged UV
__m128i avg = _mm_avg_epu8(s1_lo, s2_lo);
__m128i result = _mm_blendv_epi8(s1_lo, avg, blend_mask);
//                                ^^^^^ AY    ^^^ UV

Verification

Tested against scalar reference across 15 sizes (0–1024), edge cases, and in-place operation — bitwise identical results.

…_orc_chroma_down_v2_u8

Agent-Logs-Url: https://github.com/pexip/gstreamer/sessions/8be7ad13-5db3-442a-ad40-c034f5e0f7ef

Co-authored-by: havardgraff <1926313+havardgraff@users.noreply.github.com>
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.

2 participants