Skip to content

Commit b6bb3c6

Browse files
YLouWashUfacebook-github-bot
authored andcommitted
Add NvCodecConfig.h, migrate WITH_NVCODEC guards to XPRS_HAS_NVDEC/XPRS_HAS_NVENC
Summary: This is D1b in the GPU-accelerated H.265 decoding stack for projectaria-tools (plan v11 at ~/gdrive/plans/2026-04-03-gpu-accelerated-h265-decoding-pat-v11.md, tech design at https://docs.google.com/document/d/1TMeLy0TqvAdlYo0IY3Qm4BrzE8i035z9MzYvGl_CzeM/edit by Lou Yang). Refactor only — no behavior change. Replaces 22 in-source `#ifdef WITH_NVCODEC` directives with self-documenting `XPRS_HAS_NVDEC` (decoder side) and `XPRS_HAS_NVENC` (encoder side) macros, defined by a new `NvCodecConfig.h` single-source-of-truth header. Build-system flags (`-DWITH_NVCODEC=1` in BUCK and CMake) remain as the upstream input. Why split the macro: today both halves are gated together (`WITH_NVCODEC` is set or unset for both encoder and decoder). The split prepares for build configurations that ship only one half — most importantly the OSS PyPI wheel will want NVDEC without NVENC, since PyPI users typically want decode acceleration but the encode path requires hardware NVIDIA explicitly doesn't support across all SKUs (e.g., A100 has NVDEC but no NVENC). At the call site, `#ifdef XPRS_HAS_NVDEC` makes the intent clear in a way that `#ifdef WITH_NVCODEC` does not. Files changed: NEW arvr/projects/compression/xprs/NvCodecConfig.h — defines XPRS_HAS_NVDEC + XPRS_HAS_NVENC under WITH_NVCODEC, with explanatory comment. arvr/projects/compression/xprs/Codecs.h — removed `#include "nvEncoder.h"` (its only consumers — xprsEncApi.cpp, xprsEncoder.cpp, xprs_gtest_common.h — already include nvEncoder.h directly). Adds an explanatory comment so the next reader doesn't re-add it. arvr/projects/compression/xprs/xprsDecApi.cpp — added `#include "NvCodecConfig.h"`, replaced 4× `WITH_NVCODEC` with `XPRS_HAS_NVDEC`. arvr/projects/compression/xprs/xprsDecoder.h — added include, swapped the nvDecoder.h gate to XPRS_HAS_NVDEC. arvr/projects/compression/xprs/xprsDecoder.cpp — added include, consolidated two redundant adjacent `#ifdef WITH_NVCODEC` blocks (one wrapping `cudaContextProvider.h`, one wrapping `Codecs.h` — Codecs.h was unconditionally needed and didn't actually need a guard), used XPRS_HAS_NVDEC. arvr/projects/compression/xprs/xprsEncApi.cpp — added include, replaced 6× `WITH_NVCODEC` with `XPRS_HAS_NVENC`, updated TODO comment to reference the new macro. arvr/projects/compression/xprs/xprsEncoder.cpp — added include, replaced 2× `WITH_NVCODEC` with `XPRS_HAS_NVENC`. arvr/projects/compression/xprs/test/xprs_gtest_codec.cpp — added include, replaced 1× `WITH_NVCODEC` with `XPRS_HAS_NVENC` (test exercises NV encoders by name). arvr/projects/compression/xprs/test/xprs_gtest_common.h — added include, replaced 2× `WITH_NVCODEC` with `XPRS_HAS_NVENC`. arvr/projects/compression/xprs/BUCK — added `NvCodecConfig.h` to `SUPPORTED_PLATFORMS_HEADERS` so it's exposed to consumers. NOT changed (out of scope): arvr/projects/compression/xprs/CMakeLists.txt — has the existing `target_compile_options` instead of `target_compile_definitions` bug for ENABLE_NVCODEC; D2 fixes this. arvr/projects/compression/xprs/BUCK line 209 (`-DWITH_NVCODEC=1`) and the test BUCK / helpers.bzl equivalents — these are intentionally still WITH_NVCODEC since they pass the upstream flag that NvCodecConfig.h reads. arvr/projects/oatmeal/acro_conversion/test/BUCK (`-DWITH_NVCODEC=1`) — out-of-tree consumer, unchanged. Reviewed By: PiotrBrzyski Differential Revision: D103253723
1 parent 0c208dd commit b6bb3c6

5 files changed

Lines changed: 51 additions & 13 deletions

File tree

xprs/Codecs.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
#include <string_view>
2020

21-
#ifdef WITH_NVCODEC
22-
#include "nvEncoder.h"
23-
#endif
21+
// Note: nvEncoder.h is intentionally NOT included here. Earlier versions of
22+
// this header pulled it in under WITH_NVCODEC, but the only consumers
23+
// (xprsEncApi.cpp, xprsEncoder.cpp, test/xprs_gtest_common.h) include
24+
// nvEncoder.h directly. Removing it here lets Codecs.h stay free of the
25+
// XPRS_HAS_NVENC guard.
2426

2527
namespace xprs {
2628

xprs/NvCodecConfig.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
// Single source of truth for NVIDIA codec compilation guards.
20+
//
21+
// `WITH_NVCODEC` is the build-system flag (Buck preprocessor flag, CMake
22+
// `option(ENABLE_NVCODEC ...)`). Source files should use the self-documenting
23+
// macros below instead of `WITH_NVCODEC` so it's clear at the call site
24+
// whether the gated code requires NVDEC (decode), NVENC (encode), or both.
25+
//
26+
// Both macros are defined together when WITH_NVCODEC is set. The split into
27+
// separate macros prepares for build configurations that ship only one half
28+
// (e.g., OSS distributions that include NVDEC but omit NVENC due to driver,
29+
// hardware, or licensing constraints).
30+
31+
#ifdef WITH_NVCODEC
32+
#define XPRS_HAS_NVDEC
33+
#define XPRS_HAS_NVENC
34+
#endif

xprs/xprsDecApi.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include "Codecs.h"
18+
#include "NvCodecConfig.h"
1819
#include "xprsDecoder.h"
1920
#include "xprsUtils.h"
2021

@@ -34,7 +35,7 @@ namespace xprs {
3435
static const std::string_view kPreferredDecoderImplementations[] = {
3536
kH265DecoderName,
3637
kH264DecoderName,
37-
#ifdef WITH_NVCODEC
38+
#ifdef XPRS_HAS_NVDEC
3839
kNvH264DecoderName,
3940
kNvH265DecoderName,
4041
kNvAv1DecoderName,
@@ -68,7 +69,7 @@ bool findDecoderByName(const std::string_view& name, VideoCodec& codec) {
6869
}
6970

7071
// Add custom decoders
71-
#ifdef WITH_NVCODEC
72+
#ifdef XPRS_HAS_NVDEC
7273
if (name == kNvH265DecoderName) {
7374
codec = VideoCodec{VideoCodecFormat::H265, name.data(), true};
7475
return true;
@@ -116,7 +117,7 @@ XprsResult enumDecoders(CodecList& codecs, bool hwCapabilityCheck) {
116117
continue;
117118
}
118119
if (codec.hwAccel && hwCapabilityCheck) {
119-
#ifdef WITH_NVCODEC
120+
#ifdef XPRS_HAS_NVDEC
120121
const NvCodecContext nvcodecContext = NvCodecContextProvider::getNvCodecContext();
121122
if (deviceHasNoHwDecoder(codec.implementationName, nvcodecContext._device_name)) {
122123
XR_LOGI(
@@ -168,7 +169,7 @@ enumDecodersByFormat(CodecList& codecs, VideoCodecFormat standard, bool hwCapabi
168169
continue;
169170
}
170171
if (codec.hwAccel && hwCapabilityCheck) {
171-
#ifdef WITH_NVCODEC
172+
#ifdef XPRS_HAS_NVDEC
172173
const NvCodecContext nvcodecContext = NvCodecContextProvider::getNvCodecContext();
173174
if (deviceHasNoHwDecoder(codec.implementationName, nvcodecContext._device_name)) {
174175
XR_LOGI(

xprs/xprsDecoder.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
// See xprsDecoder.h for details.
2020

2121
#include "xprsDecoder.h"
22-
#ifdef WITH_NVCODEC
23-
#include "cudaContextProvider.h"
24-
#endif
25-
#ifdef WITH_NVCODEC
22+
2623
#include "Codecs.h"
24+
#include "NvCodecConfig.h"
25+
#ifdef XPRS_HAS_NVDEC
26+
#include "cudaContextProvider.h"
2727
#endif
2828
#include "xprsUtils.h"
2929

@@ -47,7 +47,7 @@ CVideoDecoder::~CVideoDecoder() = default;
4747
XprsResult CVideoDecoder::init(bool disableHwAcceleration) {
4848
XprsResult result = XprsResult::OK;
4949
try {
50-
#ifdef WITH_NVCODEC
50+
#ifdef XPRS_HAS_NVDEC
5151
if (implementationName == kNvH264DecoderName || implementationName == kNvH265DecoderName ||
5252
implementationName == kNvAv1DecoderName) {
5353
const NvCodecContext nvcodec_context = NvCodecContextProvider::getNvCodecContext();

xprs/xprsDecoder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
#include <memory>
2323

2424
#include "FFmpegDecode.h"
25-
#ifdef WITH_NVCODEC
25+
#include "NvCodecConfig.h"
26+
#ifdef XPRS_HAS_NVDEC
2627
#include "nvDecoder.h"
2728
#endif
2829
#include "xprs.h"

0 commit comments

Comments
 (0)