# DO NOT EDIT! This file was auto-generated by crates/build/re_types_builder/src/codegen/python/mod.rs
# Based on "crates/store/re_sdk_types/definitions/rerun/components/video_codec.fbs".

# You can extend this class by creating a "VideoCodecExt" class in "video_codec_ext.py".

from __future__ import annotations

from collections.abc import Sequence
from typing import Literal

import pyarrow as pa

from .._baseclasses import (
    BaseBatch,
    ComponentBatchMixin,
)

__all__ = ["VideoCodec", "VideoCodecArrayLike", "VideoCodecBatch", "VideoCodecLike"]


from enum import Enum


class VideoCodec(Enum):
    """
    **Component**: The codec used to encode video stored in [`components.VideoSample`][rerun.components.VideoSample].

    Support of these codecs by the Rerun Viewer is platform dependent.
    For more details see check the [video reference](https://rerun.io/docs/reference/video).

    ⚠️ **This type is _unstable_ and may change significantly in a way that the data won't be backwards compatible.**
    """

    AV1 = 0x61763031
    """
    AOMedia Video 1 (AV1)

    See <https://en.wikipedia.org/wiki/AV1>

    [`components.VideoSample`][rerun.components.VideoSample]s using this codec should be formatted according the "Low overhead bitstream format",
    as specified in Section 5.2 of the [AV1 specification](https://aomediacodec.github.io/av1-spec/#low-overhead-bitstream-format).
    Each sample should be formatted as a sequence of OBUs (Open Bitstream Units) long enough to decode at least one video frame.
    Samples containing keyframes must include a sequence header OBU before the `KEY_FRAME` OBU to enable
    extraction of frame dimensions, bit depth, and color information. `INTRA_ONLY` frames are not treated
    as keyframes since they may reference existing decoder state.

    Enum value is the fourcc for 'av01' (the WebCodec string assigned to this codec) in big endian.
    """

    H264 = 0x61766331
    """
    Advanced Video Coding (AVC/H.264)

    See <https://en.wikipedia.org/wiki/Advanced_Video_Coding>

    [`components.VideoSample`][rerun.components.VideoSample]s using this codec should be formatted according to Annex B specification.
    (Note that this is different from AVCC format found in MP4 files.
    To learn more about Annex B, check for instance <https://membrane.stream/learn/h264/3>)
    Key frames (IDR) require inclusion of a SPS (Sequence Parameter Set)

    Enum value is the fourcc for 'avc1' (the WebCodec string assigned to this codec) in big endian.
    """

    H265 = 0x68657631
    """
    High Efficiency Video Coding (HEVC/H.265)

    See <https://en.wikipedia.org/wiki/High_Efficiency_Video_Coding>

    [`components.VideoSample`][rerun.components.VideoSample]s using this codec should be formatted according to Annex B specification.
    (Note that this is different from AVCC format found in MP4 files.
    To learn more about Annex B, check for instance <https://membrane.stream/learn/h264/3>)
    Key frames (IRAP) require inclusion of a SPS (Sequence Parameter Set)

    Enum value is the fourcc for 'hev1' (the WebCodec string assigned to this codec) in big endian.
    """

    VP8 = 0x76703038
    """
    VP8

    See <https://en.wikipedia.org/wiki/VP8>

    Enum value is the fourcc for 'vp08' (the WebCodec string assigned to this codec) in big endian.
    """

    VP9 = 0x76703039
    """
    VP9

    See <https://en.wikipedia.org/wiki/VP9>

    Enum value is the fourcc for 'vp09' (the WebCodec string assigned to this codec) in big endian.
    """

    @classmethod
    def auto(cls, val: str | int | VideoCodec) -> VideoCodec:
        """Best-effort converter, including a case-insensitive string matcher."""
        if isinstance(val, VideoCodec):
            return val
        if isinstance(val, int):
            return cls(val)
        try:
            return cls[val]
        except KeyError:
            val_lower = val.lower()
            for variant in cls:
                if variant.name.lower() == val_lower:
                    return variant
        raise ValueError(f"Cannot convert {val} to {cls.__name__}")

    def __str__(self) -> str:
        """Returns the variant name."""
        return self.name


VideoCodecLike = VideoCodec | Literal["AV1", "H264", "H265", "VP8", "VP9", "av1", "h264", "h265", "vp8", "vp9"] | int
"""A type alias for any VideoCodec-like object."""

VideoCodecArrayLike = (
    VideoCodec
    | Literal["AV1", "H264", "H265", "VP8", "VP9", "av1", "h264", "h265", "vp8", "vp9"]
    | int
    | Sequence[VideoCodecLike]
)
"""A type alias for any VideoCodec-like array object."""


class VideoCodecBatch(BaseBatch[VideoCodecArrayLike], ComponentBatchMixin):
    _ARROW_DATATYPE = pa.uint32()
    _COMPONENT_TYPE: str = "rerun.components.VideoCodec"

    @staticmethod
    def _native_to_pa_array(data: VideoCodecArrayLike, data_type: pa.DataType) -> pa.Array:
        if isinstance(data, (VideoCodec, int, str)):
            data = [data]

        pa_data = [VideoCodec.auto(v).value if v is not None else None for v in data]  # type: ignore[redundant-expr]  # ty: ignore[not-iterable]

        return pa.array(pa_data, type=data_type)
