1+ import cython
2+ from cython import uint as cuint
13
2- cdef object _cinit_bypass_sentinel = object ( )
4+ _cinit_bypass_sentinel = cython . declare ( object , object () )
35
4- cdef VideoFormat get_video_format(lib.AVPixelFormat c_format, unsigned int width, unsigned int height):
6+
7+ @cython .cfunc
8+ def get_video_format (
9+ c_format : lib .AVPixelFormat , width : cuint , height : cuint
10+ ) -> VideoFormat | None :
511 if c_format == lib .AV_PIX_FMT_NONE :
612 return None
713
8- cdef VideoFormat format = VideoFormat.__new__ (VideoFormat, _cinit_bypass_sentinel)
14+ format : VideoFormat = VideoFormat .__new__ (VideoFormat , _cinit_bypass_sentinel )
915 format ._init (c_format , width , height )
1016 return format
1117
12- cdef lib.AVPixelFormat get_pix_fmt(const char * name) except lib.AV_PIX_FMT_NONE:
13- """ Wrapper for lib.av_get_pix_fmt with error checking."""
1418
15- cdef lib.AVPixelFormat pix_fmt = lib.av_get_pix_fmt(name)
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."""
1623
24+ pix_fmt : lib .AVPixelFormat = lib .av_get_pix_fmt (name )
1725 if pix_fmt == lib .AV_PIX_FMT_NONE :
1826 raise ValueError ("not a pixel format: %r" % name )
19-
2027 return pix_fmt
2128
2229
23- cdef class VideoFormat:
30+ @cython .cclass
31+ class VideoFormat :
2432 """
2533
26- >>> format = VideoFormat('rgb24')
27- >>> format.name
28- 'rgb24'
34+ >>> format = VideoFormat('rgb24')
35+ >>> format.name
36+ 'rgb24'
2937
3038 """
3139
3240 def __cinit__ (self , name , width = 0 , height = 0 ):
3341 if name is _cinit_bypass_sentinel :
3442 return
3543
36- cdef VideoFormat other
3744 if isinstance (name , VideoFormat ):
38- other = < VideoFormat> name
45+ other : VideoFormat = cython . cast ( VideoFormat , name )
3946 self ._init (other .pix_fmt , width or other .width , height or other .height )
4047 return
4148
42- cdef lib.AVPixelFormat pix_fmt = get_pix_fmt(name)
49+ pix_fmt : lib .AVPixelFormat = get_pix_fmt (name )
4350 self ._init (pix_fmt , width , height )
4451
45- cdef _init(self , lib.AVPixelFormat pix_fmt, unsigned int width, unsigned int height):
52+ @cython .cfunc
53+ def _init (self , pix_fmt : lib .AVPixelFormat , width : cuint , height : cuint ):
4654 self .pix_fmt = pix_fmt
4755 self .ptr = lib .av_pix_fmt_desc_get (pix_fmt )
4856 self .width = width
4957 self .height = height
5058 self .components = tuple (
51- VideoFormatComponent(self , i)
52- for i in range (self .ptr.nb_components)
59+ VideoFormatComponent (self , i ) for i in range (self .ptr .nb_components )
5360 )
5461
5562 def __repr__ (self ):
@@ -64,54 +71,48 @@ def __int__(self):
6471 @property
6572 def name (self ):
6673 """Canonical name of the pixel format."""
67- return < str > self .ptr.name
74+ return cython . cast ( str , self .ptr .name )
6875
6976 @property
7077 def bits_per_pixel (self ):
7178 return lib .av_get_bits_per_pixel (self .ptr )
7279
7380 @property
74- def padded_bits_per_pixel (self ): return lib.av_get_padded_bits_per_pixel(self .ptr)
81+ def padded_bits_per_pixel (self ):
82+ return lib .av_get_padded_bits_per_pixel (self .ptr )
7583
7684 @property
7785 def is_big_endian (self ):
7886 """Pixel format is big-endian."""
7987 return bool (self .ptr .flags & lib .AV_PIX_FMT_FLAG_BE )
8088
81-
8289 @property
8390 def has_palette (self ):
8491 """Pixel format has a palette in data[1], values are indexes in this palette."""
8592 return bool (self .ptr .flags & lib .AV_PIX_FMT_FLAG_PAL )
8693
87-
8894 @property
8995 def is_bit_stream (self ):
9096 """All values of a component are bit-wise packed end to end."""
9197 return bool (self .ptr .flags & lib .AV_PIX_FMT_FLAG_BITSTREAM )
9298
93-
94- # Skipping PIX_FMT_HWACCEL
95- # """Pixel format is an HW accelerated format."""
96-
9799 @property
98100 def is_planar (self ):
99101 """At least one pixel component is not in the first data plane."""
100102 return bool (self .ptr .flags & lib .AV_PIX_FMT_FLAG_PLANAR )
101103
102-
103104 @property
104105 def is_rgb (self ):
105106 """The pixel format contains RGB-like data (as opposed to YUV/grayscale)."""
106107 return bool (self .ptr .flags & lib .AV_PIX_FMT_FLAG_RGB )
107-
108108
109109 @property
110110 def is_bayer (self ):
111111 """The pixel format contains Bayer data."""
112112 return bool (self .ptr .flags & lib .AV_PIX_FMT_FLAG_BAYER )
113113
114- cpdef chroma_width(self , int luma_width = 0 ):
114+ @cython .ccall
115+ def chroma_width (self , luma_width : cython .int = 0 ):
115116 """chroma_width(luma_width=0)
116117
117118 Width of a chroma plane relative to a luma plane.
@@ -122,7 +123,8 @@ def is_bayer(self):
122123 luma_width = luma_width or self .width
123124 return - ((- luma_width ) >> self .ptr .log2_chroma_w ) if luma_width else 0
124125
125- cpdef chroma_height(self , int luma_height = 0 ):
126+ @cython .ccall
127+ def chroma_height (self , luma_height : cython .int = 0 ):
126128 """chroma_height(luma_height=0)
127129
128130 Height of a chroma plane relative to a luma plane.
@@ -134,11 +136,12 @@ def is_bayer(self):
134136 return - ((- luma_height ) >> self .ptr .log2_chroma_h ) if luma_height else 0
135137
136138
137- cdef class VideoFormatComponent:
138- def __cinit__ (self , VideoFormat format , size_t index ):
139+ @cython .cclass
140+ class VideoFormatComponent :
141+ def __cinit__ (self , format : VideoFormat , index : cython .size_t ):
139142 self .format = format
140143 self .index = index
141- self .ptr = & format.ptr.comp[index]
144+ self .ptr = cython . address ( format .ptr .comp [index ])
142145
143146 @property
144147 def plane (self ):
@@ -153,22 +156,25 @@ def bits(self):
153156 @property
154157 def is_alpha (self ):
155158 """Is this component an alpha channel?"""
156- return ((self .index == 1 and self .format.ptr.nb_components == 2 ) or
157- (self .index == 3 and self .format.ptr.nb_components == 4 ))
159+ return (self .index == 1 and self .format .ptr .nb_components == 2 ) or (
160+ self .index == 3 and self .format .ptr .nb_components == 4
161+ )
158162
159163 @property
160164 def is_luma (self ):
161165 """Is this component a luma channel?"""
162166 return self .index == 0 and (
163- self .format.ptr.nb_components == 1 or
164- self .format.ptr.nb_components == 2 or
165- not self .format.is_rgb
167+ self .format .ptr .nb_components == 1
168+ or self .format .ptr .nb_components == 2
169+ or not self .format .is_rgb
166170 )
167171
168172 @property
169173 def is_chroma (self ):
170174 """Is this component a chroma channel?"""
171- return (self .index == 1 or self .index == 2 ) and (self .format.ptr.log2_chroma_w or self .format.ptr.log2_chroma_h)
175+ return (self .index == 1 or self .index == 2 ) and (
176+ self .format .ptr .log2_chroma_w or self .format .ptr .log2_chroma_h
177+ )
172178
173179 @property
174180 def width (self ):
@@ -190,7 +196,7 @@ def height(self):
190196
191197
192198names = set ()
193- cdef const lib.AVPixFmtDescriptor * desc = NULL
199+ desc = cython . declare ( cython . pointer [ lib .AVPixFmtDescriptor ], cython . NULL )
194200while True :
195201 desc = lib .av_pix_fmt_desc_next (desc )
196202 if not desc :
0 commit comments