Skip to content

Commit 44d8095

Browse files
committed
Make video/stream pure
1 parent 5313c41 commit 44d8095

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed
Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
cimport libav as lib
1+
import cython
2+
from cython.cimports import libav as lib
3+
from cython.cimports.av.packet import Packet
4+
from cython.cimports.av.utils import avrational_to_fraction, to_avrational
5+
from cython.cimports.av.video.frame import VideoFrame
26

3-
from av.packet cimport Packet
4-
from av.utils cimport avrational_to_fraction, to_avrational
57

6-
from .frame cimport VideoFrame
7-
8-
9-
cdef class VideoStream(Stream):
8+
@cython.cclass
9+
class VideoStream(Stream):
1010
def __repr__(self):
1111
return (
1212
f"<av.VideoStream #{self.index} {self.name}, "
@@ -16,11 +16,14 @@ def __repr__(self):
1616

1717
def __getattr__(self, name):
1818
if name in ("framerate", "rate"):
19-
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
19+
raise AttributeError(
20+
f"'{type(self).__name__}' object has no attribute '{name}'"
21+
)
2022

2123
return getattr(self.codec_context, name)
2224

23-
cpdef encode(self, VideoFrame frame=None):
25+
@cython.ccall
26+
def encode(self, frame: VideoFrame | None = None):
2427
"""
2528
Encode an :class:`.VideoFrame` and return a list of :class:`.Packet`.
2629
@@ -30,23 +33,21 @@ def __getattr__(self, name):
3033
"""
3134

3235
packets = self.codec_context.encode(frame)
33-
cdef Packet packet
36+
packet: Packet
3437
for packet in packets:
3538
packet._stream = self
3639
packet.ptr.stream_index = self.ptr.index
37-
3840
return packets
3941

40-
41-
cpdef decode(self, Packet packet=None):
42+
@cython.ccall
43+
def decode(self, packet: Packet | None = None):
4244
"""
4345
Decode a :class:`.Packet` and return a list of :class:`.VideoFrame`.
4446
4547
:rtype: list[VideoFrame]
4648
4749
.. seealso:: This is a passthrough to :meth:`.CodecContext.decode`.
4850
"""
49-
5051
return self.codec_context.decode(packet)
5152

5253
@property
@@ -59,7 +60,7 @@ def average_rate(self):
5960
6061
:type: fractions.Fraction | None
6162
"""
62-
return avrational_to_fraction(&self.ptr.avg_frame_rate)
63+
return avrational_to_fraction(cython.address(self.ptr.avg_frame_rate))
6364

6465
@property
6566
def base_rate(self):
@@ -72,7 +73,7 @@ def base_rate(self):
7273
7374
:type: fractions.Fraction | None
7475
"""
75-
return avrational_to_fraction(&self.ptr.r_frame_rate)
76+
return avrational_to_fraction(cython.address(self.ptr.r_frame_rate))
7677

7778
@property
7879
def guessed_rate(self):
@@ -83,9 +84,10 @@ def guessed_rate(self):
8384
8485
:type: fractions.Fraction | None
8586
"""
86-
# The two NULL arguments aren't used in FFmpeg >= 4.0
87-
cdef lib.AVRational val = lib.av_guess_frame_rate(NULL, self.ptr, NULL)
88-
return avrational_to_fraction(&val)
87+
val: lib.AVRational = lib.av_guess_frame_rate(
88+
cython.NULL, self.ptr, cython.NULL
89+
)
90+
return avrational_to_fraction(cython.address(val))
8991

9092
@property
9193
def sample_aspect_ratio(self):
@@ -96,9 +98,11 @@ def sample_aspect_ratio(self):
9698
9799
:type: fractions.Fraction | None
98100
"""
99-
cdef lib.AVRational sar = lib.av_guess_sample_aspect_ratio(self.container.ptr, self.ptr, NULL)
100-
return avrational_to_fraction(&sar)
101-
101+
sar: lib.AVRational = lib.av_guess_sample_aspect_ratio(
102+
self.container.ptr, self.ptr, cython.NULL
103+
)
104+
return avrational_to_fraction(cython.address(sar))
105+
102106
@property
103107
def display_aspect_ratio(self):
104108
"""The guessed display aspect ratio (DAR) of this stream.
@@ -107,11 +111,13 @@ def display_aspect_ratio(self):
107111
108112
:type: fractions.Fraction | None
109113
"""
110-
cdef lib.AVRational dar
111-
114+
dar = cython.declare(lib.AVRational)
112115
lib.av_reduce(
113-
&dar.num, &dar.den,
116+
cython.address(dar.num),
117+
cython.address(dar.den),
114118
self.format.width * self.sample_aspect_ratio.num,
115-
self.format.height * self.sample_aspect_ratio.den, 1024*1024)
119+
self.format.height * self.sample_aspect_ratio.den,
120+
1024 * 1024,
121+
)
116122

117-
return avrational_to_fraction(&dar)
123+
return avrational_to_fraction(cython.address(dar))

0 commit comments

Comments
 (0)