Debian Codegraph @

av/format.py

raw
1 from enum import Flag
2
3 import cython
4 import cython.cimports.libav as lib
5
6 _cinit_bypass_sentinel = cython.declare(object, object())
7
8
9 @cython.cfunc
10 def build_container_format(
11 iptr: cython.pointer[cython.const[lib.AVInputFormat]],
12 optr: cython.pointer[cython.const[lib.AVOutputFormat]],
13 ) -> ContainerFormat:
14 if not iptr and not optr:
15 raise ValueError("needs input format or output format")
16 format: ContainerFormat = ContainerFormat.__new__(
17 ContainerFormat, _cinit_bypass_sentinel
18 )
19 format.iptr = iptr
20 format.optr = optr
21 format.name = optr.name if optr else iptr.name
22 return format
23
24
25 # fmt: off
26 class Flags(Flag):
27 no_file = lib.AVFMT_NOFILE
28 need_number: "Needs '%d' in filename." = lib.AVFMT_NEEDNUMBER
29 show_ids: "Show format stream IDs numbers." = lib.AVFMT_SHOW_IDS
30 global_header: "Format wants global header." = lib.AVFMT_GLOBALHEADER
31 no_timestamps: "Format does not need / have any timestamps." = lib.AVFMT_NOTIMESTAMPS
32 generic_index: "Use generic index building code." = lib.AVFMT_GENERIC_INDEX
33 ts_discont: "Format allows timestamp discontinuities" = lib.AVFMT_TS_DISCONT
34 variable_fps: "Format allows variable fps." = lib.AVFMT_VARIABLE_FPS
35 no_dimensions: "Format does not need width/height" = lib.AVFMT_NODIMENSIONS
36 no_streams: "Format does not require any streams" = lib.AVFMT_NOSTREAMS
37 no_bin_search: "Format does not allow to fall back on binary search via read_timestamp" = lib.AVFMT_NOBINSEARCH
38 no_gen_search: "Format does not allow to fall back on generic search" = lib.AVFMT_NOGENSEARCH
39 no_byte_seek: "Format does not allow seeking by bytes" = lib.AVFMT_NO_BYTE_SEEK
40 ts_nonstrict: "Format does not require strictly increasing timestamps, but they must still be monotonic." = lib.AVFMT_TS_NONSTRICT
41 ts_negative: "Format allows muxing negative timestamps." = lib.AVFMT_TS_NEGATIVE
42 # If not set the timestamp will be shifted in `av_write_frame()` and `av_interleaved_write_frame()`
43 # so they start from 0. The user or muxer can override this through AVFormatContext.avoid_negative_ts
44 seek_to_pts: "Seeking is based on PTS" = lib.AVFMT_SEEK_TO_PTS
45 # fmt: on
46
47
48 @cython.final
49 @cython.cclass
50 class ContainerFormat:
51 """Descriptor of a container format.
52
53 :param str name: The name of the format.
54 :param str mode: ``'r'`` or ``'w'`` for input and output formats; defaults
55 to None which will grab either.
56
57 """
58
59 def __cinit__(self, name, mode=None):
60 if name is _cinit_bypass_sentinel:
61 return
62
63 # We need to hold onto the original name because AVInputFormat.name is
64 # actually comma-separated, and so we need to remember which one this was.
65 self.name = name
66
67 # Searches comma-separated names.
68 if mode is None or mode == "r":
69 self.iptr = lib.av_find_input_format(name)
70
71 if mode is None or mode == "w":
72 self.optr = lib.av_guess_format(name, cython.NULL, cython.NULL)
73
74 if not self.iptr and not self.optr:
75 raise ValueError(f"no container format {name!r}")
76
77 def __repr__(self):
78 return f"<av.{self.__class__.__name__} {self.name!r}>"
79
80 @property
81 def input(self):
82 """An input-only view of this format."""
83 if self.iptr == cython.NULL:
84 return None
85 elif self.optr == cython.NULL:
86 return self
87 else:
88 return build_container_format(self.iptr, cython.NULL)
89
90 @property
91 def output(self):
92 """An output-only view of this format."""
93 if self.optr == cython.NULL:
94 return None
95 elif self.iptr == cython.NULL:
96 return self
97 else:
98 return build_container_format(cython.NULL, self.optr)
99
100 @property
101 def is_input(self):
102 return self.iptr != cython.NULL
103
104 @property
105 def is_output(self):
106 return self.optr != cython.NULL
107
108 @property
109 def long_name(self):
110 # We prefer the output names since the inputs may represent
111 # multiple formats.
112 return self.optr.long_name if self.optr else self.iptr.long_name
113
114 @property
115 def extensions(self):
116 exts: set = set()
117 if self.iptr and self.iptr.extensions:
118 exts.update(self.iptr.extensions.split(","))
119 if self.optr and self.optr.extensions:
120 exts.update(self.optr.extensions.split(","))
121 return exts
122
123 @property
124 def flags(self):
125 """
126 Get the flags bitmask for the format.
127
128 :rtype: int
129 """
130 return (self.iptr.flags if self.iptr else 0) | (
131 self.optr.flags if self.optr else 0
132 )
133
134 @property
135 def no_file(self):
136 return bool(self.flags & lib.AVFMT_NOFILE)
137
138
139 @cython.cfunc
140 def get_output_format_names() -> set:
141 names: set = set()
142 ptr: cython.pointer[cython.const[lib.AVOutputFormat]]
143 opaque: cython.p_void = cython.NULL
144 while True:
145 ptr = lib.av_muxer_iterate(cython.address(opaque))
146 if ptr:
147 names.add(ptr.name)
148 else:
149 break
150 return names
151
152
153 @cython.cfunc
154 def get_input_format_names() -> set:
155 names: set = set()
156 ptr: cython.pointer[cython.const[lib.AVInputFormat]]
157 opaque: cython.p_void = cython.NULL
158 while True:
159 ptr = lib.av_demuxer_iterate(cython.address(opaque))
160 if ptr:
161 names.add(ptr.name)
162 else:
163 break
164 return names
165
166
167 formats_available = get_output_format_names()
168 formats_available.update(get_input_format_names())