Skip to content

Commit 0589a91

Browse files
committed
Make av/bitstream pure
1 parent 825aabb commit 0589a91

File tree

3 files changed

+115
-96
lines changed

3 files changed

+115
-96
lines changed

av/bitstream.py

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import cython
2+
import cython.cimports.libav as lib
3+
from cython.cimports.av.error import err_check
4+
from cython.cimports.av.packet import Packet
5+
from cython.cimports.av.stream import Stream
6+
from cython.cimports.libc.errno import EAGAIN
7+
8+
9+
@cython.cclass
10+
class BitStreamFilterContext:
11+
"""
12+
Initializes a bitstream filter: a way to directly modify packet data.
13+
14+
Wraps :ffmpeg:`AVBSFContext`
15+
16+
:param Stream in_stream: A stream that defines the input codec for the bitfilter.
17+
:param Stream out_stream: A stream whose codec is overwritten using the output parameters from the bitfilter.
18+
"""
19+
20+
def __cinit__(
21+
self,
22+
filter_description,
23+
in_stream: Stream | None = None,
24+
out_stream: Stream | None = None,
25+
):
26+
res: cython.int
27+
filter_str: cython.p_char = filter_description
28+
29+
with cython.nogil:
30+
res = lib.av_bsf_list_parse_str(filter_str, cython.address(self.ptr))
31+
err_check(res)
32+
33+
if in_stream is not None:
34+
with cython.nogil:
35+
res = lib.avcodec_parameters_copy(
36+
self.ptr.par_in, in_stream.ptr.codecpar
37+
)
38+
err_check(res)
39+
40+
with cython.nogil:
41+
res = lib.av_bsf_init(self.ptr)
42+
err_check(res)
43+
44+
if out_stream is not None:
45+
with cython.nogil:
46+
res = lib.avcodec_parameters_copy(
47+
out_stream.ptr.codecpar, self.ptr.par_out
48+
)
49+
err_check(res)
50+
lib.avcodec_parameters_to_context(
51+
out_stream.codec_context.ptr, out_stream.ptr.codecpar
52+
)
53+
54+
def __dealloc__(self):
55+
if self.ptr:
56+
lib.av_bsf_free(cython.address(self.ptr))
57+
58+
@cython.ccall
59+
def filter(self, packet: Packet | None = None):
60+
"""
61+
Processes a packet based on the filter_description set during initialization.
62+
Multiple packets may be created.
63+
64+
:type: list[Packet]
65+
"""
66+
res: cython.int
67+
new_packet: Packet
68+
69+
with cython.nogil:
70+
res = lib.av_bsf_send_packet(
71+
self.ptr, packet.ptr if packet is not None else cython.NULL
72+
)
73+
err_check(res)
74+
75+
output: list = []
76+
while True:
77+
new_packet = Packet()
78+
with cython.nogil:
79+
res = lib.av_bsf_receive_packet(self.ptr, new_packet.ptr)
80+
81+
if res == -EAGAIN or res == lib.AVERROR_EOF:
82+
return output
83+
84+
err_check(res)
85+
if res:
86+
return output
87+
88+
output.append(new_packet)
89+
90+
@cython.ccall
91+
def flush(self):
92+
"""
93+
Reset the internal state of the filter.
94+
Should be called e.g. when seeking.
95+
Can be used to make the filter usable again after draining it with EOF marker packet.
96+
"""
97+
lib.av_bsf_flush(self.ptr)
98+
99+
100+
@cython.cfunc
101+
def get_filter_names() -> set:
102+
names: set = set()
103+
ptr: cython.pointer[cython.const[lib.AVBitStreamFilter]]
104+
opaque: cython.p_void = cython.NULL
105+
while True:
106+
ptr = lib.av_bsf_iterate(cython.address(opaque))
107+
if ptr:
108+
names.add(ptr.name)
109+
else:
110+
break
111+
112+
return names
113+
114+
115+
bitstream_filters_available = get_filter_names()

av/bitstream.pyx

Lines changed: 0 additions & 95 deletions
This file was deleted.

av/container/streams.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class StreamContainer:
1111
subtitles: tuple[SubtitleStream, ...]
1212
attachments: tuple[AttachmentStream, ...]
1313
data: tuple[DataStream, ...]
14-
other: tuple[Stream, ...]
1514

1615
def __init__(self) -> None: ...
1716
def __len__(self) -> int: ...

0 commit comments

Comments
 (0)