Debian Codegraph @

av/audio/codeccontext.py

raw
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.final
11 @cython.cclass
12 class AudioCodecContext(CodecContext):
13 @cython.cfunc
14 def _prepare_frames_for_encode(self, input_frame: Frame | None) -> list:
15 frame: AudioFrame | None = input_frame
16 allow_var_frame_size: cython.bint = (
17 self.ptr.codec.capabilities & lib.AV_CODEC_CAP_VARIABLE_FRAME_SIZE
18 )
19
20 # Note that the resampler will simply return an input frame if there is
21 # no resampling to be done. The control flow was just a little easier this way.
22 if not self.resampler:
23 self.resampler = AudioResampler(
24 format=self.format,
25 layout=self.layout,
26 rate=self.ptr.sample_rate,
27 frame_size=None if allow_var_frame_size else self.ptr.frame_size,
28 )
29 frames = self.resampler.resample(frame)
30 if input_frame is None:
31 frames.append(None) # flush if input frame is None
32
33 return frames
34
35 @cython.cfunc
36 def _alloc_next_frame(self) -> Frame:
37 return alloc_audio_frame()
38
39 @cython.cfunc
40 def _setup_decoded_frame(self, frame: Frame, packet: Packet):
41 CodecContext._setup_decoded_frame(self, frame, packet)
42 aframe: AudioFrame = frame
43 aframe._init_user_attributes()
44
45 @property
46 def frame_size(self):
47 """
48 Number of samples per channel in an audio frame.
49
50 :type: int
51 """
52 return self.ptr.frame_size
53
54 @property
55 def sample_rate(self):
56 """
57 Sample rate of the audio data, in samples per second.
58
59 :type: int
60 """
61 return self.ptr.sample_rate
62
63 @sample_rate.setter
64 def sample_rate(self, value: cython.int):
65 self.ptr.sample_rate = value
66
67 @property
68 def rate(self):
69 """Another name for :attr:`sample_rate`."""
70 return self.sample_rate
71
72 @rate.setter
73 def rate(self, value):
74 self.sample_rate = value
75
76 @property
77 def channels(self):
78 return self.layout.nb_channels
79
80 @property
81 def layout(self):
82 """
83 The audio channel layout.
84
85 :type: AudioLayout
86 """
87 return get_audio_layout(self.ptr.ch_layout)
88
89 @layout.setter
90 def layout(self, value):
91 layout: AudioLayout = AudioLayout(value)
92 self.ptr.ch_layout = layout.layout
93
94 @property
95 def format(self):
96 """
97 The audio sample format.
98
99 :type: AudioFormat
100 """
101 return get_audio_format(self.ptr.sample_fmt)
102
103 @format.setter
104 def format(self, value):
105 format: AudioFormat = AudioFormat(value)
106 self.ptr.sample_fmt = format.sample_fmt