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