Debian Codegraph @

av/audio/format.py

raw
1 import sys
2
3 import cython
4
5 container_format_postfix = cython.declare(
6 str, "le" if sys.byteorder == "little" else "be"
7 )
8 _cinit_bypass_sentinel = cython.declare(object, object())
9
10
11 @cython.cfunc
12 def get_audio_format(c_format: lib.AVSampleFormat) -> AudioFormat:
13 """Get an AudioFormat without going through a string."""
14
15 if c_format < 0:
16 return None
17
18 format: AudioFormat = AudioFormat(_cinit_bypass_sentinel)
19 format.sample_fmt = c_format
20 return format
21
22
23 @cython.final
24 @cython.cclass
25 class AudioFormat:
26 """Descriptor of audio formats."""
27
28 def __cinit__(self, name):
29 if name is _cinit_bypass_sentinel:
30 return
31
32 sample_fmt: lib.AVSampleFormat
33 if isinstance(name, AudioFormat):
34 sample_fmt = cython.cast(AudioFormat, name).sample_fmt
35 else:
36 sample_fmt = lib.av_get_sample_fmt(name)
37
38 if sample_fmt < 0:
39 raise ValueError(f"Not a sample format: {name!r}")
40
41 self.sample_fmt = sample_fmt
42
43 def __repr__(self):
44 return f"<av.AudioFormat {self.name}>"
45
46 @property
47 def name(self):
48 """Canonical name of the sample format.
49
50 >>> AudioFormat('s16p').name
51 's16p'
52
53 """
54 return lib.av_get_sample_fmt_name(self.sample_fmt)
55
56 @property
57 def bytes(self):
58 """Number of bytes per sample.
59
60 >>> AudioFormat('s16p').bytes
61 2
62
63 """
64 return lib.av_get_bytes_per_sample(self.sample_fmt)
65
66 @property
67 def bits(self):
68 """Number of bits per sample.
69
70 >>> AudioFormat('s16p').bits
71 16
72
73 """
74 return lib.av_get_bytes_per_sample(self.sample_fmt) << 3
75
76 @property
77 def is_planar(self):
78 """Is this a planar format?
79
80 Strictly opposite of :attr:`is_packed`.
81
82 """
83 return bool(lib.av_sample_fmt_is_planar(self.sample_fmt))
84
85 @property
86 def is_packed(self):
87 """Is this a packed format?
88
89 Strictly opposite of :attr:`is_planar`.
90
91 """
92 return not lib.av_sample_fmt_is_planar(self.sample_fmt)
93
94 @property
95 def planar(self):
96 """The planar variant of this format.
97
98 Is itself when planar:
99
100 >>> fmt = AudioFormat('s16p')
101 >>> fmt.planar is fmt
102 True
103
104 """
105 if self.is_planar:
106 return self
107 return get_audio_format(lib.av_get_planar_sample_fmt(self.sample_fmt))
108
109 @property
110 def packed(self):
111 """The packed variant of this format.
112
113 Is itself when packed:
114
115 >>> fmt = AudioFormat('s16')
116 >>> fmt.packed is fmt
117 True
118
119 """
120 if self.is_packed:
121 return self
122 return get_audio_format(lib.av_get_packed_sample_fmt(self.sample_fmt))
123
124 @property
125 def container_name(self):
126 """The name of a :class:`ContainerFormat` which directly accepts this data.
127
128 :raises ValueError: when planar, since there are no such containers.
129
130 """
131 if self.is_planar:
132 raise ValueError("no planar container formats")
133
134 if self.sample_fmt == lib.AV_SAMPLE_FMT_U8:
135 return "u8"
136 elif self.sample_fmt == lib.AV_SAMPLE_FMT_S16:
137 return "s16" + container_format_postfix
138 elif self.sample_fmt == lib.AV_SAMPLE_FMT_S32:
139 return "s32" + container_format_postfix
140 elif self.sample_fmt == lib.AV_SAMPLE_FMT_FLT:
141 return "f32" + container_format_postfix
142 elif self.sample_fmt == lib.AV_SAMPLE_FMT_DBL:
143 return "f64" + container_format_postfix
144
145 raise ValueError("unknown layout")