Skip to content

Commit dc0e6d0

Browse files
committed
Make audio/codeccontext pure
1 parent 29b44be commit dc0e6d0

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed
Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
cimport libav as lib
2-
3-
from av.audio.format cimport AudioFormat, get_audio_format
4-
from av.audio.frame cimport AudioFrame, alloc_audio_frame
5-
from av.audio.layout cimport AudioLayout, get_audio_layout
6-
from av.codec.hwaccel cimport HWAccel
7-
from av.frame cimport Frame
8-
from av.packet cimport Packet
9-
10-
11-
cdef class AudioCodecContext(CodecContext):
12-
cdef _init(self, lib.AVCodecContext *ptr, const lib.AVCodec *codec, HWAccel hwaccel):
13-
CodecContext._init(self, ptr, codec, hwaccel)
14-
15-
cdef _prepare_frames_for_encode(self, Frame input_frame):
16-
17-
cdef AudioFrame frame = input_frame
18-
cdef bint allow_var_frame_size = self.ptr.codec.capabilities & lib.AV_CODEC_CAP_VARIABLE_FRAME_SIZE
1+
import cython
2+
from cython.cimports import libav as lib
3+
from cython.cimports.av.audio.format import AudioFormat, get_audio_format
4+
from cython.cimports.av.audio.frame import AudioFrame, alloc_audio_frame
5+
from cython.cimports.av.audio.layout import AudioLayout, get_audio_layout
6+
from cython.cimports.av.frame import Frame
7+
from cython.cimports.av.packet import Packet
8+
9+
10+
@cython.cclass
11+
class AudioCodecContext(CodecContext):
12+
@cython.cfunc
13+
def _prepare_frames_for_encode(self, input_frame: Frame | None):
14+
frame: AudioFrame | None = input_frame
15+
allow_var_frame_size: cython.bint = (
16+
self.ptr.codec.capabilities & lib.AV_CODEC_CAP_VARIABLE_FRAME_SIZE
17+
)
1918

2019
# Note that the resampler will simply return an input frame if there is
2120
# no resampling to be done. The control flow was just a little easier this way.
@@ -24,22 +23,22 @@
2423
format=self.format,
2524
layout=self.layout,
2625
rate=self.ptr.sample_rate,
27-
frame_size=None if allow_var_frame_size else self.ptr.frame_size
26+
frame_size=None if allow_var_frame_size else self.ptr.frame_size,
2827
)
2928
frames = self.resampler.resample(frame)
30-
31-
# flush if input frame is None
3229
if input_frame is None:
33-
frames.append(None)
30+
frames.append(None) # flush if input frame is None
3431

3532
return frames
3633

37-
cdef Frame _alloc_next_frame(self):
34+
@cython.cfunc
35+
def _alloc_next_frame(self) -> Frame:
3836
return alloc_audio_frame()
3937

40-
cdef _setup_decoded_frame(self, Frame frame, Packet packet):
38+
@cython.cfunc
39+
def _setup_decoded_frame(self, frame: Frame, packet: Packet):
4140
CodecContext._setup_decoded_frame(self, frame, packet)
42-
cdef AudioFrame aframe = frame
41+
aframe: AudioFrame = frame
4342
aframe._init_user_attributes()
4443

4544
@property
@@ -61,7 +60,7 @@ def sample_rate(self):
6160
return self.ptr.sample_rate
6261

6362
@sample_rate.setter
64-
def sample_rate(self, int value):
63+
def sample_rate(self, value: cython.int):
6564
self.ptr.sample_rate = value
6665

6766
@property
@@ -88,7 +87,7 @@ def layout(self):
8887

8988
@layout.setter
9089
def layout(self, value):
91-
cdef AudioLayout layout = AudioLayout(value)
90+
layout: AudioLayout = AudioLayout(value)
9291
self.ptr.ch_layout = layout.layout
9392

9493
@property
@@ -102,5 +101,5 @@ def format(self):
102101

103102
@format.setter
104103
def format(self, value):
105-
cdef AudioFormat format = AudioFormat(value)
104+
format: AudioFormat = AudioFormat(value)
106105
self.ptr.sample_fmt = format.sample_fmt

0 commit comments

Comments
 (0)