1- cimport libav as lib
1+ import cython
2+ from cython .cimports import libav as lib
3+ from cython .cimports .av .bytesource import bytesource
4+ from cython .cimports .av .error import err_check
5+ from cython .cimports .av .opaque import opaque_container
6+ from cython .cimports .av .utils import avrational_to_fraction , to_avrational
27
3- from av.bytesource cimport bytesource
4- from av.error cimport err_check
5- from av.opaque cimport opaque_container
6- from av.utils cimport avrational_to_fraction, to_avrational
7-
8-
9- cdef class Packet(Buffer):
108
9+ @cython .cclass
10+ class Packet (Buffer ):
1111 """A packet of encoded data within a :class:`~av.format.Stream`.
1212
1313 This may, or may not include a complete object within a stream.
1414 :meth:`decode` must be called to extract encoded data.
15-
1615 """
1716
1817 def __cinit__ (self , input = None ):
19- with nogil:
18+ with cython . nogil :
2019 self .ptr = lib .av_packet_alloc ()
2120
21+ def __dealloc__ (self ):
22+ with cython .nogil :
23+ lib .av_packet_free (cython .address (self .ptr ))
24+
2225 def __init__ (self , input = None ):
23- cdef size_t size = 0
24- cdef ByteSource source = None
26+ size : cython . size_t = 0
27+ source : ByteSource = None
2528
2629 if input is None :
2730 return
@@ -41,24 +44,24 @@ def __init__(self, input=None):
4144 # instead of its data.
4245 # self.source = source
4346
44- def __dealloc__ (self ):
45- with nogil:
46- lib.av_packet_free(& self .ptr)
47-
4847 def __repr__ (self ):
4948 stream = self ._stream .index if self ._stream else 0
5049 return (
51- f" < av.{self.__class__.__name__} of #{stream}, dts={self.dts},"
50+ f"av.{ self .__class__ .__name__ } of #{ stream } , dts={ self .dts } ,"
5251 f" pts={ self .pts } ; { self .ptr .size } bytes at 0x{ id (self ):x} >"
5352 )
5453
5554 # Buffer protocol.
56- cdef size_t _buffer_size(self ):
55+ @cython .cfunc
56+ def _buffer_size (self ) -> cython .size_t :
5757 return self .ptr .size
58- cdef void * _buffer_ptr(self ):
58+
59+ @cython .cfunc
60+ def _buffer_ptr (self ) -> cython .p_void :
5961 return self .ptr .data
6062
61- cdef _rebase_time(self , lib.AVRational dst):
63+ @cython .cfunc
64+ def _rebase_time (self , dst : lib .AVRational ):
6265 if not dst .num :
6366 raise ValueError ("Cannot rebase to zero time." )
6467
@@ -92,7 +95,7 @@ def stream(self):
9295 return self ._stream
9396
9497 @stream .setter
95- def stream (self , Stream stream ):
98+ def stream (self , stream : Stream ):
9699 self ._stream = stream
97100 self .ptr .stream_index = stream .ptr .index
98101
@@ -103,11 +106,11 @@ def time_base(self):
103106
104107 :type: fractions.Fraction
105108 """
106- return avrational_to_fraction(& self ._time_base)
109+ return avrational_to_fraction (cython . address ( self ._time_base ) )
107110
108111 @time_base .setter
109112 def time_base (self , value ):
110- to_avrational(value, & self ._time_base)
113+ to_avrational (value , cython . address ( self ._time_base ) )
111114
112115 @property
113116 def pts (self ):
@@ -116,7 +119,7 @@ def pts(self):
116119
117120 This is the time at which the packet should be shown to the user.
118121
119- :type: int
122+ :type: int | None
120123 """
121124 if self .ptr .pts != lib .AV_NOPTS_VALUE :
122125 return self .ptr .pts
@@ -133,7 +136,7 @@ def dts(self):
133136 """
134137 The decoding timestamp in :attr:`time_base` units for this packet.
135138
136- :type: int
139+ :type: int | None
137140 """
138141 if self .ptr .dts != lib .AV_NOPTS_VALUE :
139142 return self .ptr .dts
@@ -152,7 +155,7 @@ def pos(self):
152155
153156 Returns `None` if it is not known.
154157
155- :type: int
158+ :type: int | None
156159 """
157160 if self .ptr .pos != - 1 :
158161 return self .ptr .pos
@@ -221,14 +224,15 @@ def is_disposable(self):
221224
222225 @property
223226 def opaque (self ):
224- if self .ptr.opaque_ref is not NULL :
225- return opaque_container.get(< char * > self .ptr.opaque_ref.data)
227+ if self .ptr .opaque_ref is not cython .NULL :
228+ return opaque_container .get (
229+ cython .cast (cython .p_char , self .ptr .opaque_ref .data )
230+ )
226231
227232 @opaque .setter
228233 def opaque (self , v ):
229- lib.av_buffer_unref(& self .ptr.opaque_ref)
234+ lib .av_buffer_unref (cython . address ( self .ptr .opaque_ref ) )
230235
231236 if v is None :
232237 return
233238 self .ptr .opaque_ref = opaque_container .add (v )
234-
0 commit comments