Metadata-Version: 2.4
Name: pixtreme-transport-nvdec
Version: 0.17.0
License-Expression: MIT AND BSD-3-Clause
License-File: LICENSE
License-File: licenses/cuda-runtime.txt
License-File: licenses/ffnvcodec.txt
License-File: licenses/hm-tables.txt
License-File: licenses/nanobind.txt
License-File: licenses/robin_map.txt
Requires-Python: >=3.12
Requires-Dist: pixtreme>=2.38.0
Description-Content-Type: text/markdown

# pixtreme-transport-nvdec

`pixtreme-transport-nvdec` provides `pixtreme.transport.nvdec`, a streaming decoder of H.265, H.264, VP8, VP9, AV1, MPEG-1, MPEG-2, MPEG-4 Part 2, VC-1 and JPEG for [pixtreme](https://github.com/sync-dev-org/pixtreme). It delivers the decoded pixels for each picture (a frame or, for H.265 field streams, a field). H.265 decoders also deliver motion vectors and prediction residual when requested at creation. The decoder accepts each codec's unit of one picture (a complete Annex B access unit for H.265 and H.264, a frame for VP8 and VP9, a temporal unit for AV1), independently of the protocol or file format carrying the stream.

One wheel includes the Python binding, `libpixtreme_nvdec.so`, public C headers, a header-only C++ wrapper and CMake package configuration. Python, C and C++ callers borrow each decoded picture as a pixtreme `PixelBuffer` without a copy.

## Install

Install from PyPI:

```bash
pip install pixtreme-transport-nvdec
```

For a uv project, use `uv add pixtreme-transport-nvdec`. The installer resolves the `pixtreme>=2.38.0` dependency from PyPI. CuPy is not a dependency of this package or of pixtreme; install it yourself only when your own code uses CuPy arrays. The transport core is not required for the decoder's Python API. The same wheel is also attached to each [GitHub Release](https://github.com/sync-dev-org/pixtreme-transport-nvdec/releases) with its SHA-256 checksum.

## Supported environments

The wheels support Linux x86_64 with glibc 2.28 or later (`manylinux_2_28_x86_64`) and CPython 3.12 or later through the stable Python ABI. Runtime decoding requires an NVIDIA GPU and a driver supporting the CUDA 13 runtime, including in WSL2 and containers. Native GPU code targets sm 86, sm 89 and sm 120. The NVIDIA driver is supplied by the runtime environment.

## C and C++ APIs

The shared library exposes C ABI version 9 through
`pixtreme/nvdec/c/decoder.h`. The header-only C++17 wrapper is
`pixtreme/nvdec/decoder.hpp`. Both interfaces are independent of the transport
core and require no CUDA SDK headers. They lend each picture as a
[pixtreme](https://github.com/sync-dev-org/pixtreme) `PixelBuffer`, so they
include pixtreme's public headers and link its shared library. Add the
installed `pixtreme/transport/nvdec` directory and pixtreme's `pixtreme/core`
directory to `CMAKE_PREFIX_PATH`, then use:

```cmake
find_package(pixtreme_transport_nvdec CONFIG REQUIRED)
target_link_libraries(your_target PRIVATE pixtreme::transport_nvdec)
```

### Get the native SDK

Each [GitHub Release](https://github.com/sync-dev-org/pixtreme-transport-nvdec/releases)
provides a prebuilt Linux x86_64 SDK. C and C++ applications can use its
installed CMake package without installing Python wheels or compiling either
library. Obtain pixtreme's SDK separately and extract both SDKs into one prefix.
For a release built with pixtreme 2.38.0, run the following commands in a new
directory, replacing `X.Y.Z` with the decoder release version:

```bash
nvdec_version=X.Y.Z
pixtreme_version=2.38.0
nvdec_release="https://github.com/sync-dev-org/pixtreme-transport-nvdec/releases/download/v$nvdec_version"
pixtreme_release="https://github.com/sync-dev-org/pixtreme/releases/download/v$pixtreme_version"
native_prefix="$PWD/native-sdk"
mkdir -p "$native_prefix" nvdec-download pixtreme-download
(
    cd nvdec-download
    curl -fsSLO "$nvdec_release/SHA256SUMS"
    curl -fsSLO "$nvdec_release/pixtreme_transport_nvdec-$nvdec_version-sdk-manylinux_2_28_x86_64.tar.gz"
    sha256sum --check --ignore-missing SHA256SUMS
    tar -xzf "pixtreme_transport_nvdec-$nvdec_version-sdk-manylinux_2_28_x86_64.tar.gz" -C "$native_prefix"
)
(
    cd pixtreme-download
    curl -fsSLO "$pixtreme_release/SHA256SUMS"
    curl -fsSLO "$pixtreme_release/pixtreme-sdk-$pixtreme_version-manylinux_2_28_x86_64.tar.gz"
    sha256sum --check --ignore-missing SHA256SUMS
    tar -xzf "pixtreme-sdk-$pixtreme_version-manylinux_2_28_x86_64.tar.gz" -C "$native_prefix"
)
cmake -S /path/to/your/application -B build -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    "-DCMAKE_PREFIX_PATH=$native_prefix/pixtreme/transport/nvdec;$native_prefix/pixtreme/core"
cmake --build build
```

The application uses `find_package(pixtreme_transport_nvdec CONFIG REQUIRED)`
and links `pixtreme::transport_nvdec` as shown above. Preserve the extracted
paths when deploying the SDKs together:
`pixtreme/transport/nvdec/libpixtreme_nvdec.so` and
`pixtreme/core/lib/libpixtreme.so.2`. The decoder retains its wheel filename,
SONAME and relative `$ORIGIN` runtime paths, and each process loads one
pixtreme shared library. Native applications need neither a Python runtime
nor CUDA SDK headers; decoding still needs the GPU and driver described above.

### Use the public interfaces

C callers check `pixtreme_transport_nvdec_abi_version()` against
`PIXTREME_TRANSPORT_NVDEC_ABI_VERSION` before other library calls. C++ callers
can use `require_compatible_abi()`; `Decoder` also checks during construction.
C functions use opaque handles, records beginning with `struct_size`, and
status/error outputs. Initialize `struct_size` to `sizeof(record)`. An error
handle describes the reason, whether it is fatal, and its message. The reason
`UNSUPPORTED` (C++ `ErrorReason::Unsupported`) reports a capability shortfall:
the GPU's NVDEC cannot decode the stream, or no `PixelBuffer` format represents
its pictures. Malformed input, rejected arguments and stream forms the decoder
excludes by design use `INVALID_INPUT`. Release owned handles with
`pixtreme_transport_nvdec_handle_release()`; the public C headers document
borrowed views and the required prefixes of sized records. The C++ wrapper
translates errors into `DecoderError` and manages references.

`pixtreme_transport_nvdec_frame_info` appends `has_profile`,
`general_profile_idc` (`int32_t`) and `general_profile_compatibility_flags`
(`uint32_t`). On H.265 frames these are the SPS profile syntax used for that
picture, including inherited VPS values for a multilayer SPS, whether or not
motion and residual output is enabled. `has_profile` is zero for other codecs.
Compatibility flag `general_profile_compatibility_flag[j]` occupies bit j of
the mask. Interpret the values using H.265 Annex A; the decoder does not infer
profile names. ABI version remains 9, and callers with the previous record's
`struct_size` receive only their original fields.

Rebuild native consumers against the ABI 9 headers and library together.
ABI 9 adds the creation option `output_motion_and_residual` and disables
H.265 motion and residual output by default. Consumers using those outputs
must explicitly enable the option.
C++ callers compile the wrapper into their own binary. C callers use the
`pixtreme_transport_nvdec_` functions directly. Consumers may use their own
C++ compiler and standard library.

In the C++ wrapper,
`codec_name(Codec)` returns the codec's lowercase creation name as a borrowed
static `const char*`, or `nullptr` for an unknown value. C callers use
`pixtreme_transport_nvdec_codec_name(int32_t codec)`, which returns `NULL` for
unknown values. Neither operation creates a decoder or initializes CUDA, and
the caller must not modify or free the string. The library defines the names
once for C, C++ and Python; this addition keeps ABI version 9.

`Decoder(device, output_surfaces, threads=0, output_layer_set_index=0, codec=Codec::Hevc, output_motion_and_residual=false, color_fallback={})`
requires an explicit positive number of pictures lent at once. A decoder
decodes the one codec chosen at creation; this version decodes H.265
(`Codec::Hevc`), H.264 (`Codec::H264`), VP8 (`Codec::Vp8`), VP9 (`Codec::Vp9`),
AV1 (`Codec::Av1`), MPEG-1 (`Codec::Mpeg1`), MPEG-2
(`Codec::Mpeg2`), MPEG-4 Part 2 (`Codec::Mpeg4`), VC-1 of the Simple, Main and
Advanced profiles (`Codec::Vc1`) and JPEG (`Codec::Jpeg`), and an unknown codec
value is invalid input. `output_layer_set_index` applies to H.265 and must be zero
for the other codecs. Hardware decoder creation occurs on
the first sequence; an unsupported number of surfaces fails that `decode` with
a fatal `DecoderError`. `AccessUnit` owns the bytes of one unit (an Annex B
access unit for H.265 and H.264; one frame for VP8 and VP9, a VP9 superframe
included; one temporal unit for AV1, with or without its temporal delimiter;
one picture for MPEG-1 and MPEG-2, one field
picture of a field pair; one VOP for MPEG-4 Part 2; one frame for VC-1, with or
without its frame start code; one image for JPEG), optional configuration
(Annex B start code headers, hvcC for H.265, avcC for H.264, av1C for AV1, or
STRUCT_C and STRUCT_A as `CodecConfigurationFormat::Rcv` for VC-1 Simple and
Main), optional timing and an optional `pixtreme_owner_t` record of the caller.
Bytes that hold more than one unit are invalid input. Decimal strings preserve timestamp precision. No packet type,
random-access flag, or discontinuity flag is required.
The decoder uses a unit's configuration only when its bytes or
`configuration_format` differ from the configuration it used last. Passing the
same configuration with every unit therefore keeps the parameter sets that
later units carry in band in effect, such as an SPS that changes the colour
declaration; a different configuration replaces the parameter sets that have
its identifiers. The decoder keeps the record of the configuration it used last
across `discontinuity`.

Set `output_motion_and_residual=true` to produce H.265 motion vectors and
residual. The default skips CABAC analysis, its uploads and the motion and
residual GPU work. `mv()` and `residual()` then return a recoverable
`InvalidState` error, and the picture remains usable. `missing_slices` and
`faulty_reference` have no value; `missing_reference` is still reported from
the reference picture sets. Other codecs accept and ignore the option. In C,
the `int64_t output_motion_and_residual` field uses 0 to disable and
any nonzero value to enable output. A structure too small to contain the
complete field defaults to 0, including the previous 40-byte structure.

`ColorFallback` holds three optional H.273 codes as `std::optional<std::int32_t>`:
`color_primaries`, `transfer_characteristics` and `matrix_coefficients`.
For example, `ColorFallback{9, 16, 9}` supplies BT.2020 primaries, PQ transfer
and the BT.2020 non-constant matrix. These values stay fixed for the decoder's
lifetime; create another decoder to change them. For each attribute, picture
syntax takes precedence, followed by the supplied fallback, the codec's
standard default, then unknown. Code 2 is unspecified. A declared code without
a pixtreme token stays unknown and is never replaced by fallback. The matrices
signalled by VP8 `color_space`, VP9 `color_config` and JFIF count as declarations.
The BT.601 matrix a VP9 profile-0 intra-only frame infers without `color_config`
is a standard default. Range and chroma siting do not change.

In C, the options append `has_fallback_color_primaries` (`uint8_t`) and
`fallback_color_primaries` (`int32_t`), then the corresponding
`has_fallback_transfer_characteristics` / `fallback_transfer_characteristics`
and `has_fallback_matrix_coefficients` / `fallback_matrix_coefficients` pairs.
An incomplete pair or a zero presence flag means absent: its value is neither
read nor validated. Present codes must be in 0..255; otherwise creation returns
`INVALID_INPUT`. Code 2 means absent. Matrix 0 is a present code, for which
pixtreme currently has no token; it gives unknown, as does any unmappable
fallback code. The previous 48-byte options supply no fallback.
This addition preserves ABI 9. Consumers that use fallback must set their
dependency minimum to a released version providing these fields, because an
older ABI-9 library ignores unknown trailing input bytes.

The decoder retains an access unit's owner record once when it queues the
input, never for an input it refuses before queueing, and releases it once
when no picture of that input remains in the decoder and every frame made from
it has been released. A `PixelBuffer` taken from such a frame does not delay
the release. An input that yields no frame releases its owner within the
operation that settles this; inputs left after a fatal error release theirs in
`close`. Releases run outside the decoder's locks with `cudaStreamLegacy`, on
the thread that calls `decode`, `discontinuity`, `flush` or `close`, or that
releases the last frame. Frames show the record read-only through `owner()`.

For same-size two-layer Scalable Main or Scalable Main 4:4:4 SHVC and two-view Multiview Main
MV-HEVC, submit each complete AU with all its layers. The fixed
`output_layer_set_index` selects the VPS output layer set. Its default, zero,
returns base-layer pictures through the single-layer decode path, including
field pictures. Each output exposes read-only `nuh_layer_id()` (Python:
`nuh_layer_id`); ordinary single-layer pictures also have layer ID zero.
Output AU bytes, AU indices and timestamps refer to the complete original
input, including NALs excluded from decoding. In C, the index and the codec
are trailing fields of `decoder_options`; a shorter options `struct_size`
omits them and selects zero, which is H.265.

Nonzero indices output the pictures selected by the VPS output flags,
including sets that output both layers or only the upper SHVC layer. Pictures
with the same POC are returned in increasing layer-ID order. Reference-only
pictures are decoded without lending a surface; the number of pictures lent
at once applies to the entire decoder. An index absent from a newly active
VPS reports its exact value and absence as a recoverable error. Later AUs
using a VPS that contains the index can still be decoded.
Scalable Main 4:4:4 accepts matching 8-bit 4:2:0, 4:2:2 or 4:4:4 layers,
subject to the active GPU's NVDEC capability query. Spatially different SHVC
layers, unequal layer chroma formats, non-eight-bit Scalable Main 4:4:4,
Scalable Main 10, ten-bit MV-HEVC, monochrome and more than two layers are
rejected at parameter sets.

`decode`, `discontinuity`, and `flush` return `DecodedFrame` values in display
order without waiting for GPU completion. A frame carries no CUDA event: the
decoder records its writes on its streams as pixtreme use records of the
picture's `PixelBuffer` and of each NDArray that `mv()` and `residual()`
return, and a reader orders its stream after them. Metadata defined by H.265 syntax is
optional: on H.265 frames `vui_timing()` and `pic_struct()` can be empty,
and `missing_slices` and `faulty_reference` are empty when analysis was not
requested. Every other H.265 field holds a value; frames of the other codecs hold none of
them, and their `mv()` and `residual()` throw a recoverable `InvalidState`
error. An H.264 or MPEG-2 stream whose fields are separate pictures passes one
field's unit per call; the frame of two fields carries its first field's input
values. As libvpx and libaom output, a VP9 input returns its last frame when
that frame is shown, and an AV1 temporal unit the last frame it shows; a hidden
frame returns none, and a frame shown again with `show_existing_frame` carries
the input values of the call that shows it. Its `PixelBuffer` shares a single
map with earlier loans of that picture, while each loan uses its own lending
slot and each frame keeps its own input values. Each frame's read-only `slice_type()`
returns its first slice segment's HEVC value (0 = B, 1 = P, 2 = I). Read-only
`nal_unit_type()` returns the picture's slice-segment NAL unit type as an
integer, including IDR (19, 20), CRA (21), BLA (16–18), and non-IRAP types.
Read-only `general_profile_idc()` and `general_profile_compatibility_flags()`
return the picture's SPS values as `std::optional<int>` and
`std::optional<std::uint32_t>`. Compatibility flag j is bit j. Both hold a
value for H.265, including fields and each output layer, and no value for
other codecs. H.265 Annex A defines their interpretation; the decoder does
not infer profile names.
Read-only `vui_timing()` returns the picture's SPS `num_units_in_tick` and
`time_scale` as unsigned 32-bit integers, or no value when VUI is absent or
does not signal timing; no default or frame-period conversion is applied.
`field_seq_flag()` is zero for an SPS without VUI, as H.265 specifies.

Each frame holds one reference to its picture. `take_picture()` (C:
`pixtreme_transport_nvdec_frame_take_picture`) moves that reference to the
caller once, as a `pixtreme::pixel_buffer` that lends the NVDEC surface
without a copy; a second take is a recoverable `InvalidState` error. The
`PixelBuffer` declares its pixtreme format, bit depth and alignment, the
offset and pitch of each plane, and the display size. 4:2:0 pictures use
`nv12`, `p010` and `p012`; 4:2:2 pictures use `nv16`, `p210` and `p212`; 4:4:4
pictures use `yuv444p`, `yuv444p10msb` and `yuv444p12msb`, except when their
H.273 matrix is 0 (Identity): those pictures use `gbrp`, `gbrp10msb` and
`gbrp12msb`. Their G, B and R planes share the Y, Cb and Cr surface,
offsets, pitches and samples, without conversion. GBR pictures declare no
matrix and keep the signalled range, colorspace and gamma; 4:4:4 siting stays
unknown. The optional matrix fallback selects GBR only when the bitstream
does not declare a matrix. A fallback of 0 on a non-4:4:4 picture leaves its
YUV (or gray) format and unknown matrix. H.265, H.264, AV1 and VP9 bitstreams
that declare Identity without 4:4:4 sampling are refused as recoverable
`invalid_input`, before decoding the picture; later valid access units can
still decode. VP9's Identity declaration is CS_RGB. This constraint is not
applied to other codecs.

GBR range stays unknown when the bitstream signals none. In particular,
H.265 without a range declaration and with fallback matrix 0 does not
acquire H.265's inferred legal range: pixtreme reads this unknown-range GBR
picture at full scale. To use legal scale, provide a stream that signals
range or explicitly declare `legal` through pixtreme. Two-byte samples
store d significant bits at bits 16-d through 15, with zero lower bits. The
surface holds the whole coded picture, and each plane's offset points at the
display region's top left. Its use records order reads after the decoder's
writes. Its colour declaration converts the picture's SPS VUI
`colour_primaries`, `transfer_characteristics`, `matrix_coeffs` and
`video_full_range_flag` to pixtreme tokens with pixtreme's H.273 conversion
(`pixtreme_color_h273_to_token`). A value the VUI does not signal, or a code
without a pixtreme token, stays unknown (null, `None` in Python) rather than a
default. A 4:2:0 picture's siting comes from `chroma_sample_loc_type_top_field`,
or from `chroma_sample_loc_type_bottom_field` for a field picture whose
`pic_struct` names the bottom field (2, 10 or 12); it is unknown when the VUI
signals no chroma location, and for 4:2:2 and 4:4:4 pictures.
Streams whose luma and chroma bit depths differ are refused as a
capability shortfall, because a `PixelBuffer` has one bit depth.
Pictures of the other codecs declare the colour values their syntax signals,
or those their standard specifies when it signals none (the C header lists
them per codec); gray JPEG images are lent as `gray8`. JPEG 4:2:2 and 4:4:4
images, which NVDEC outputs only subsampled to 4:2:0, and JPEG images other
than sequential Huffman (baseline and extended) ones, such as progressive images,
which NVDEC does not decode, are refused as a capability shortfall.
After a VP8, VP9 or AV1 frame is refused as a capability shortfall, subsequent inputs are
refused as `unsupported` until a key frame initializes all saved decoding
state or `discontinuity()` discards the references. A non-key frame that needs
the NVDEC decoder to be recreated, and a VP9 non-key frame whose width or
height exceeds the last accepted key frame's width or height, are refused this
way, even if the decoder reserved more space; a discontinuity also forgets the
last accepted key frame's size. Intra-only frames do not resume decoding after
this refusal. A frame that shows again a picture of another size than its
owning decoder's current output is refused for that input alone; this does not
start a wait for a key frame. After a discontinuity, frames that refer to
discarded references are refused as invalid input.

There is no return operation. Release the `PixelBuffer`'s last reference on
the stream of its last read (`pixel_buffer::release(stream)` or
`pixtreme_pixel_buffer_release`); the decoder unmaps the surface once every
loan of that picture is returned and all release streams' preceding work
completes, and only then reuses it. The destructor and
`pixtreme_pixel_buffer_destroy` release on the legacy default stream, so after
a last read on another stream, confirm on the host that the read finished
first. Releasing every reference to a frame whose picture was not taken returns
its surface as well. A taken `PixelBuffer` stays valid after its frame and the
decoder are released or closed. When every surface is lent, `decode` succeeds
and defers output without waiting for a return; after releasing a picture, call
`decode` or `flush` to collect deferred pictures. A VP9 or AV1 frame that shows
a picture again, and later frames, are delivered in display order while a
lending slot is available, even if an earlier `PixelBuffer` of that picture is
still held. The shared map is unmapped once all these `PixelBuffer`s are
returned and all their release streams' work has finished. Repeat `flush`
until it returns an empty vector.

`is_supported(device, codec, chroma_format, bit_depth, width, height)` (C:
`pixtreme_transport_nvdec_is_supported`) answers whether the device's NVDEC
can decode that codec, chroma format, luma bit depth and coded size and the
decoder can lend such a picture, applying the same conditions as the decode
errors with the reason `Unsupported`. Conditions decided while parsing, such
as the decode surface count, output layer sets and stream forms excluded by
design, are left to `decode`.

`mv()` (C: `pixtreme_transport_nvdec_frame_mv`) returns the processed
float32 motion array `(2, H, W)`, and `residual()` (C:
`pixtreme_transport_nvdec_frame_residual`) returns the processed float32
residual `(1, H, W)`, each as a new `pixtreme::ndarray` (C:
`pixtreme_ndarray_t`) in memory allocated for that call alone, so writing it
never changes a later result. `residual()` divides the picture's signed luma
residual before prediction, saturated to `[-32768, 32767]` after RDPCM and
cross-component prediction, by the picture's SPS VUI luma range:
`219 * 2^(bit_depth - 8)` for limited or unsignalled range, and
`255 * 2^(bit_depth - 8)` for full range. `mv()` and `residual()` produce the
values of version 4 of the pixtreme.transport `hevc-mv-residual` profile. The
call does not wait for the decoder's write, which the NDArray's use records
hold. Before reading on another CUDA stream, order that stream after the write
(`wait`), record the reads (`record_stream`), and release the last reference on
the stream of the last read (`release(stream)` or `pixtreme_ndarray_release`):
the decoder frees the memory after that stream's preceding work and never
waits for it on the host. The release orders the release stream after the
decoder's write, so work queued later on that stream waits for the write even
when the array was released without a read. The destructor and `pixtreme_ndarray_destroy`
release on the legacy default stream, so after a last read on another stream,
confirm on the host that the read finished first. The NDArray stays valid
after its frame and the decoder are released or closed.

`mv()` and `residual()` are the decoder's only motion and residual outputs.
The H.265 motion and the int16 Y, Cb and Cr residuals they are computed from
belong to no public interface; the tests read them through an uninstalled
verification binding to compare the decoder with HM, SHM and HTM. Both
operations stay available while the frame is valid, whether or not its
surface has been returned.

`close()` does not wait for lent pictures: it stops decoding and lending and
frees the resources that are not lent. The surface of a `PixelBuffer` still
referenced is neither reused nor unmapped until its last release; the decoder
then unmaps it after the release stream's work and frees the rest, NVDEC
included. Destroying the NVDEC decoder can block the host until GPU work
queued in the same CUDA context finishes. Host completion waits query CUDA
events or streams and sleep for 50 microseconds between pending queries.

## Python API

The Python boundary calls the C ABI through the C++ wrapper and waits for the
writes in each picture's use records before returning a tuple of
`PictureRecord` objects in display order:

```python
from pixtreme.transport.nvdec import DecoderSession

with DecoderSession(threads=0, device=0, output_layer_set_index=0,
                    output_surfaces=4, codec="hevc",
                    output_motion_and_residual=False,
                    fallback_color_primaries=None,
                    fallback_transfer_characteristics=None,
                    fallback_matrix_coefficients=None) as decoder:
    for record in decoder.decode(
        access_unit_bytes,
        configuration=configuration_bytes,  # optional
        configuration_format="annexb",       # "hvcc", "avcc", "av1c" or "rcv"
        pts=None, dts=None, duration=None, time_base=(1, 90000),
    ):
        picture = record.take_picture()  # a pixtreme.PixelBuffer
```

`output_surfaces` is required and names the number of pictures the decoder can
lend at once. `codec` defaults to `"hevc"`; the other names are `"h264"`,
`"vp8"`, `"vp9"`, `"av1"`, `"mpeg1"`, `"mpeg2"`, `"mpeg4"` (MPEG-4 Part 2),
`"vc1"` and `"jpeg"`, and another
name raises `DecoderError` with reason `"invalid_input"`.
The keyword `output_motion_and_residual` defaults to `False`. Set it to
`True` when using H.265 `mv()` or `residual()`. Without it, both operations
raise a recoverable `DecoderError` with reason `"invalid_state"`,
`missing_slices` and `faulty_reference` are `None`, and the picture and
`missing_reference` remain available. Other codecs ignore the keyword.
The keyword-only `fallback_color_primaries`,
`fallback_transfer_characteristics` and `fallback_matrix_coefficients` each
accept `int | None`, defaulting to `None`. They follow the C/C++ colour fallback
rules above, including code validation, picture precedence and the minimum
released library version. The caller supplies the codes; the decoder does not
parse a container.
`decode` accepts CPU bytes, bytearray, or memoryview. Missing timing stays
missing; `time_base` is an optional integer `(numerator, denominator)` tuple.
The decoder uses `configuration` only when its bytes or format differ from the
configuration it used last. Passing the same configuration with every call, as
in the example, therefore keeps the parameter sets that later AUs carry in
band in effect, such as an SPS that changes the colour declaration; a different
configuration replaces the parameter sets that have its identifiers.
Call `discontinuity()` before the first AU after a transport interruption; the
decoder keeps the record of the configuration it used last across it.

`take_picture()` moves the record's picture to the caller once, as a
`pixtreme.PixelBuffer` that lends the NVDEC surface without a copy; a second
call raises `DecoderError` with reason `"invalid_state"`. Reading attributes,
including `repr`, leaves the picture in the record. The surface returns when
the PixelBuffer's last reference is released, or when a record whose picture
was not taken is released. While `output_surfaces` pictures are lent, `decode`
and `flush` hold later pictures back; release one and call `decode` or `flush`
again, until `flush` returns an empty tuple. A PixelBuffer stays valid after
`close()` and after the session is released.

Records expose metadata, including read-only `slice_type` with the same HEVC
values, read-only integer `nal_unit_type`, and read-only `vui_timing` as an
integer `(num_units_in_tick, time_scale)` tuple or `None` when not signalled,
plus the functions `mv()` and `residual()`.
Read-only `general_profile_idc` and `general_profile_compatibility_flags` are
integers from that picture's SPS, with compatibility flag j at bit j, or
`None` for codecs other than H.265. They remain available without motion and
residual output, for field pictures and for every output layer, including
multilayer SPS values inherited from VPS. Interpret them using H.265 Annex A;
the decoder does not infer profile names.
Fields defined by H.265 syntax are typed as optional; H.265 records show a
value in each of them, except `vui_timing` and `pic_struct` when not signalled.
`mv()` and `residual()` return the wrapper's NDArrays as float32
`pixtreme.NDArray` values after the writes in their use records complete, and
are the only motion and residual outputs. The Python API does not import the
transport core and does not take an owner record.

`codec_name(codec: int) -> str | None` returns the lowercase creation name for
numeric codec values 0 through 9 (`hevc`, `h264`, `vp8`, `vp9`, `av1`, `mpeg1`,
`mpeg2`, `mpeg4`, `vc1`, `jpeg`), or `None` for an unknown value. It needs no
decoder or CUDA context. Creation and the support query use this same library
operation to recognize codec names.

`is_supported(codec, chroma_format, bit_depth, width, height, device=0)`
answers whether the device's NVDEC can decode such a stream and the decoder
can lend its pictures; `chroma_format` is `"4:0:0"`, `"4:2:0"`, `"4:2:2"` or
`"4:4:4"`, and `width` and `height` are the coded size. `DecoderError` has
`reason` (`"invalid_input"`, `"invalid_state"`, `"unsupported"`,
`"concurrent_operation"` or `"driver_failure"`) and `fatal` attributes;
`"unsupported"` reports the capability shortfall that `is_supported` answers.

For a stream with SPS `field_seq_flag=1`, each output contains one field's
rows at that field picture's display dimensions. Output remains in display
order and does not wait for the complementary field. The decoder does not
weave fields or insert rows. `field_seq_flag` is a read-only bool and
`pic_struct` comes from base picture timing SEI, or the upper layer's
frame-field information SEI. Picture timing directly inside scalable nesting
in that layer's SEI NAL is also supported. The value is `None` when not signalled;
parity is never inferred from POC. C uses `field_seq_flag`, `has_pic_struct`
and `pic_struct` in `frame_info`; C++ provides `field_seq_flag()` and
`pic_struct()` returning bool and `std::optional<int>`. VUI timing values
remain the signalled values without conversion to a field or frame rate.

Processed motion (`mv()`) uses the field's display dimensions and field rows
as its vertical unit. Each usable reference contributes its quarter-pixel
vector divided by 4 and by the POC distance. Opposite field parity does not
add a half-row correction. Two usable lists are averaged; one contributes
alone, and no usable list produces zero (SP-HEVCDEC-105). Only same-layer
temporal references with a known full POC and a nonzero POC distance contribute.

## Licenses

The decoder's own code is covered by the MIT license in [LICENSE](LICENSE). The wheel includes the complete license texts for nanobind (BSD-3-Clause), its bundled robin_map (MIT), the ffnvcodec headers (MIT) and the HM tables (BSD-3-Clause) in its `dist-info/licenses/` directory.

The C++ shared library and Python extension statically link the NVIDIA CUDA Runtime. That portion is governed by the [NVIDIA CUDA Toolkit EULA](https://docs.nvidia.com/cuda/eula/index.html) and is outside the permissions granted by this package's licenses. The wheel includes a CUDA runtime notice alongside the license texts.
