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