| 1 |
import cython |
| 2 |
from cython import uint as cuint |
| 3 |
|
| 4 |
_cinit_bypass_sentinel = cython.declare(object, object()) |
| 5 |
|
| 6 |
|
| 7 |
@cython.cfunc |
| 8 |
def get_video_format( |
| 9 |
c_format: lib.AVPixelFormat, width: cuint, height: cuint |
| 10 |
) -> VideoFormat | None: |
| 11 |
if c_format == lib.AV_PIX_FMT_NONE: |
| 12 |
return None |
| 13 |
|
| 14 |
format: VideoFormat = VideoFormat.__new__(VideoFormat, _cinit_bypass_sentinel) |
| 15 |
format._init(c_format, width, height) |
| 16 |
return format |
| 17 |
|
| 18 |
|
| 19 |
@cython.cfunc |
| 20 |
@cython.exceptval(lib.AV_PIX_FMT_NONE, check=False) |
| 21 |
def get_pix_fmt(name: cython.p_const_char) -> lib.AVPixelFormat: |
| 22 |
"""Wrapper for lib.av_get_pix_fmt with error checking.""" |
| 23 |
|
| 24 |
pix_fmt: lib.AVPixelFormat = lib.av_get_pix_fmt(name) |
| 25 |
if pix_fmt == lib.AV_PIX_FMT_NONE: |
| 26 |
raise ValueError("not a pixel format: %r" % name) |
| 27 |
return pix_fmt |
| 28 |
|
| 29 |
|
| 30 |
@cython.final |
| 31 |
@cython.cclass |
| 32 |
class VideoFormat: |
| 33 |
""" |
| 34 |
|
| 35 |
>>> format = VideoFormat('rgb24') |
| 36 |
>>> format.name |
| 37 |
'rgb24' |
| 38 |
|
| 39 |
""" |
| 40 |
|
| 41 |
def __cinit__(self, name, width=0, height=0): |
| 42 |
if name is _cinit_bypass_sentinel: |
| 43 |
return |
| 44 |
|
| 45 |
if isinstance(name, VideoFormat): |
| 46 |
other: VideoFormat = cython.cast(VideoFormat, name) |
| 47 |
self._init(other.pix_fmt, width or other.width, height or other.height) |
| 48 |
return |
| 49 |
|
| 50 |
pix_fmt: lib.AVPixelFormat = get_pix_fmt(name) |
| 51 |
self._init(pix_fmt, width, height) |
| 52 |
|
| 53 |
@cython.cfunc |
| 54 |
def _init(self, pix_fmt: lib.AVPixelFormat, width: cuint, height: cuint): |
| 55 |
self.pix_fmt = pix_fmt |
| 56 |
self.ptr = lib.av_pix_fmt_desc_get(pix_fmt) |
| 57 |
self.width = width |
| 58 |
self.height = height |
| 59 |
|
| 60 |
def __repr__(self): |
| 61 |
if self.width or self.height: |
| 62 |
return f"<av.{self.__class__.__name__} {self.name}, {self.width}x{self.height}>" |
| 63 |
else: |
| 64 |
return f"<av.{self.__class__.__name__} {self.name}>" |
| 65 |
|
| 66 |
def __int__(self): |
| 67 |
return int(self.pix_fmt) |
| 68 |
|
| 69 |
@property |
| 70 |
def name(self): |
| 71 |
"""Canonical name of the pixel format.""" |
| 72 |
return cython.cast(str, self.ptr.name) |
| 73 |
|
| 74 |
@property |
| 75 |
def components(self): |
| 76 |
return tuple( |
| 77 |
VideoFormatComponent(self, i) for i in range(self.ptr.nb_components) |
| 78 |
) |
| 79 |
|
| 80 |
@property |
| 81 |
def bits_per_pixel(self): |
| 82 |
return lib.av_get_bits_per_pixel(self.ptr) |
| 83 |
|
| 84 |
@property |
| 85 |
def padded_bits_per_pixel(self): |
| 86 |
return lib.av_get_padded_bits_per_pixel(self.ptr) |
| 87 |
|
| 88 |
@property |
| 89 |
def is_big_endian(self): |
| 90 |
"""Pixel format is big-endian.""" |
| 91 |
return bool(self.ptr.flags & lib.AV_PIX_FMT_FLAG_BE) |
| 92 |
|
| 93 |
@property |
| 94 |
def has_palette(self): |
| 95 |
"""Pixel format has a palette in data[1], values are indexes in this palette.""" |
| 96 |
return bool(self.ptr.flags & lib.AV_PIX_FMT_FLAG_PAL) |
| 97 |
|
| 98 |
@property |
| 99 |
def is_bit_stream(self): |
| 100 |
"""All values of a component are bit-wise packed end to end.""" |
| 101 |
return bool(self.ptr.flags & lib.AV_PIX_FMT_FLAG_BITSTREAM) |
| 102 |
|
| 103 |
@property |
| 104 |
def is_planar(self): |
| 105 |
"""At least one pixel component is not in the first data plane.""" |
| 106 |
return bool(self.ptr.flags & lib.AV_PIX_FMT_FLAG_PLANAR) |
| 107 |
|
| 108 |
@property |
| 109 |
def is_rgb(self): |
| 110 |
"""The pixel format contains RGB-like data (as opposed to YUV/grayscale).""" |
| 111 |
return bool(self.ptr.flags & lib.AV_PIX_FMT_FLAG_RGB) |
| 112 |
|
| 113 |
@property |
| 114 |
def is_bayer(self): |
| 115 |
"""The pixel format contains Bayer data.""" |
| 116 |
return bool(self.ptr.flags & lib.AV_PIX_FMT_FLAG_BAYER) |
| 117 |
|
| 118 |
@cython.ccall |
| 119 |
def chroma_width(self, luma_width: cython.int = 0): |
| 120 |
"""chroma_width(luma_width=0) |
| 121 |
|
| 122 |
Width of a chroma plane relative to a luma plane. |
| 123 |
|
| 124 |
:param int luma_width: Width of the luma plane; defaults to ``self.width``. |
| 125 |
|
| 126 |
""" |
| 127 |
luma_width = luma_width or self.width |
| 128 |
return -((-luma_width) >> self.ptr.log2_chroma_w) if luma_width else 0 |
| 129 |
|
| 130 |
@cython.ccall |
| 131 |
def chroma_height(self, luma_height: cython.int = 0): |
| 132 |
"""chroma_height(luma_height=0) |
| 133 |
|
| 134 |
Height of a chroma plane relative to a luma plane. |
| 135 |
|
| 136 |
:param int luma_height: Height of the luma plane; defaults to ``self.height``. |
| 137 |
|
| 138 |
""" |
| 139 |
luma_height = luma_height or self.height |
| 140 |
return -((-luma_height) >> self.ptr.log2_chroma_h) if luma_height else 0 |
| 141 |
|
| 142 |
|
| 143 |
@cython.final |
| 144 |
@cython.cclass |
| 145 |
class VideoFormatComponent: |
| 146 |
def __cinit__(self, format: VideoFormat, index: cython.size_t): |
| 147 |
self.format = format |
| 148 |
self.index = index |
| 149 |
self.ptr = cython.address(format.ptr.comp[index]) |
| 150 |
|
| 151 |
@property |
| 152 |
def plane(self): |
| 153 |
"""The index of the plane which contains this component.""" |
| 154 |
return self.ptr.plane |
| 155 |
|
| 156 |
@property |
| 157 |
def bits(self): |
| 158 |
"""Number of bits in the component.""" |
| 159 |
return self.ptr.depth |
| 160 |
|
| 161 |
@property |
| 162 |
def is_alpha(self): |
| 163 |
"""Is this component an alpha channel?""" |
| 164 |
return (self.index == 1 and self.format.ptr.nb_components == 2) or ( |
| 165 |
self.index == 3 and self.format.ptr.nb_components == 4 |
| 166 |
) |
| 167 |
|
| 168 |
@property |
| 169 |
def is_luma(self): |
| 170 |
"""Is this component a luma channel?""" |
| 171 |
return self.index == 0 and ( |
| 172 |
self.format.ptr.nb_components == 1 |
| 173 |
or self.format.ptr.nb_components == 2 |
| 174 |
or not self.format.is_rgb |
| 175 |
) |
| 176 |
|
| 177 |
@property |
| 178 |
def is_chroma(self): |
| 179 |
"""Is this component a chroma channel?""" |
| 180 |
return (self.index == 1 or self.index == 2) and ( |
| 181 |
self.format.ptr.log2_chroma_w or self.format.ptr.log2_chroma_h |
| 182 |
) |
| 183 |
|
| 184 |
@property |
| 185 |
def width(self): |
| 186 |
"""The width of this component's plane. |
| 187 |
|
| 188 |
Requires the parent :class:`VideoFormat` to have a width. |
| 189 |
|
| 190 |
""" |
| 191 |
return self.format.chroma_width() if self.is_chroma else self.format.width |
| 192 |
|
| 193 |
@property |
| 194 |
def height(self): |
| 195 |
"""The height of this component's plane. |
| 196 |
|
| 197 |
Requires the parent :class:`VideoFormat` to have a height. |
| 198 |
|
| 199 |
""" |
| 200 |
return self.format.chroma_height() if self.is_chroma else self.format.height |
| 201 |
|
| 202 |
|
| 203 |
names = set() |
| 204 |
desc = cython.declare(cython.pointer[cython.const[lib.AVPixFmtDescriptor]], cython.NULL) |
| 205 |
while True: |
| 206 |
desc = lib.av_pix_fmt_desc_next(desc) |
| 207 |
if not desc: |
| 208 |
break |
| 209 |
names.add(desc.name) |