forked from PyAV-Org/PyAV
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.pyx
More file actions
142 lines (94 loc) · 3.58 KB
/
Copy pathformat.pyx
File metadata and controls
142 lines (94 loc) · 3.58 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import sys
cdef str container_format_postfix = 'le' if sys.byteorder == 'little' else 'be'
cdef object _cinit_bypass_sentinel
cdef AudioFormat get_audio_format(lib.AVSampleFormat c_format):
"""Get an AudioFormat without going through a string."""
cdef AudioFormat format = AudioFormat.__new__(AudioFormat, _cinit_bypass_sentinel)
format._init(c_format)
return format
cdef class AudioFormat(object):
"""Descriptor of audio formats."""
def __cinit__(self, name):
if name is _cinit_bypass_sentinel:
return
cdef lib.AVSampleFormat sample_fmt = lib.av_get_sample_fmt(name)
if sample_fmt < 0:
raise ValueError('not a sample format: %r' % name)
self._init(sample_fmt)
cdef _init(self, lib.AVSampleFormat sample_fmt):
self.sample_fmt = sample_fmt
def __repr__(self):
return '<av.AudioFormat %s>' % (self.name)
property name:
"""Canonical name of the sample format.
>>> SampleFormat('s16p').name
's16p'
"""
def __get__(self):
return <str>lib.av_get_sample_fmt_name(self.sample_fmt)
property bytes:
"""Number of bytes per sample.
>>> SampleFormat('s16p').bytes
2
"""
def __get__(self):
return lib.av_get_bytes_per_sample(self.sample_fmt)
property bits:
"""Number of bits per sample.
>>> SampleFormat('s16p').bits
16
"""
def __get__(self):
return lib.av_get_bytes_per_sample(self.sample_fmt) << 3
property is_planar:
"""Is this a planar format?
Strictly opposite of :attr:`is_packed`.
"""
def __get__(self):
return bool(lib.av_sample_fmt_is_planar(self.sample_fmt))
property is_packed:
"""Is this a planar format?
Strictly opposite of :attr:`is_planar`.
"""
def __get__(self):
return not lib.av_sample_fmt_is_planar(self.sample_fmt)
property planar:
"""The planar variant of this format.
Is itself when planar:
>>> fmt = Format('s16p')
>>> fmt.planar is fmt
True
"""
def __get__(self):
if self.is_planar:
return self
return get_audio_format(lib.av_get_planar_sample_fmt(self.sample_fmt))
property packed:
"""The packed variant of this format.
Is itself when packed:
>>> fmt = Format('s16')
>>> fmt.packed is fmt
True
"""
def __get__(self):
if self.is_packed:
return self
return get_audio_format(lib.av_get_packed_sample_fmt(self.sample_fmt))
property container_name:
"""The name of a :class:`ContainerFormat` which directly accepts this data.
:raises ValueError: when planar, since there are no such containers.
"""
def __get__(self):
if self.is_planar:
raise ValueError('no planar container formats')
if self.sample_fmt == lib.AV_SAMPLE_FMT_U8:
return 'u8'
elif self.sample_fmt == lib.AV_SAMPLE_FMT_S16:
return 's16' + container_format_postfix
elif self.sample_fmt == lib.AV_SAMPLE_FMT_S32:
return 's32' + container_format_postfix
elif self.sample_fmt == lib.AV_SAMPLE_FMT_FLT:
return 'f32' + container_format_postfix
elif self.sample_fmt == lib.AV_SAMPLE_FMT_DBL:
return 'f64' + container_format_postfix
raise ValueError('unknown layout')