|
| 1 | +import cython |
| 2 | +import cython.cimports.libav as lib |
| 3 | +from cython.cimports.av.indexentry import wrap_index_entry |
| 4 | +from cython.cimports.libc.stdint import int64_t |
| 5 | + |
| 6 | +_cinit_bypass_sentinel = cython.declare(object, object()) |
| 7 | + |
| 8 | + |
| 9 | +@cython.cfunc |
| 10 | +def wrap_index_entries(ptr: cython.pointer[lib.AVStream]) -> IndexEntries: |
| 11 | + obj: IndexEntries = IndexEntries(_cinit_bypass_sentinel) |
| 12 | + obj._init(ptr) |
| 13 | + return obj |
| 14 | + |
| 15 | + |
| 16 | +@cython.cclass |
| 17 | +class IndexEntries: |
| 18 | + """A sequence-like view of FFmpeg's per-stream index entries. |
| 19 | +
|
| 20 | + Exposed as :attr:`~av.stream.Stream.index_entries`. |
| 21 | +
|
| 22 | + The index is provided by the demuxer and may be empty or incomplete depending |
| 23 | + on the container format. This is useful for fast multi-seek loops (e.g., decoding |
| 24 | + at a lower-than-native framerate). |
| 25 | + """ |
| 26 | + |
| 27 | + def __cinit__(self, sentinel): |
| 28 | + if sentinel is _cinit_bypass_sentinel: |
| 29 | + return |
| 30 | + raise RuntimeError("cannot manually instantiate IndexEntries") |
| 31 | + |
| 32 | + @cython.cfunc |
| 33 | + def _init(self, ptr: cython.pointer[lib.AVStream]): |
| 34 | + self.stream_ptr = ptr |
| 35 | + |
| 36 | + def __repr__(self): |
| 37 | + return f"<av.IndexEntries[{len(self)}]>" |
| 38 | + |
| 39 | + def __len__(self) -> int: |
| 40 | + with cython.nogil: |
| 41 | + return lib.avformat_index_get_entries_count(self.stream_ptr) |
| 42 | + |
| 43 | + def __iter__(self): |
| 44 | + for i in range(len(self)): |
| 45 | + yield self[i] |
| 46 | + |
| 47 | + def __getitem__(self, index): |
| 48 | + if isinstance(index, int): |
| 49 | + n = len(self) |
| 50 | + if index < 0: |
| 51 | + index += n |
| 52 | + if index < 0 or index >= n: |
| 53 | + raise IndexError(f"Index entries {index} out of bounds for size {n}") |
| 54 | + |
| 55 | + c_idx = cython.declare(cython.int, index) |
| 56 | + with cython.nogil: |
| 57 | + entry = lib.avformat_index_get_entry(self.stream_ptr, c_idx) |
| 58 | + |
| 59 | + if entry == cython.NULL: |
| 60 | + raise IndexError("index entry not found") |
| 61 | + |
| 62 | + return wrap_index_entry(entry) |
| 63 | + |
| 64 | + elif isinstance(index, slice): |
| 65 | + start, stop, step = index.indices(len(self)) |
| 66 | + return [self[i] for i in range(start, stop, step)] |
| 67 | + |
| 68 | + else: |
| 69 | + raise TypeError("Index must be an integer or a slice") |
| 70 | + |
| 71 | + def search_timestamp( |
| 72 | + self, timestamp, *, backward: bool = True, any_frame: bool = False |
| 73 | + ): |
| 74 | + """Search the underlying index for ``timestamp``. |
| 75 | +
|
| 76 | + This wraps FFmpeg's ``av_index_search_timestamp``. |
| 77 | +
|
| 78 | + Returns an index into this object, or ``-1`` if no match is found. |
| 79 | + """ |
| 80 | + c_timestamp = cython.declare(int64_t, timestamp) |
| 81 | + flags = cython.declare(cython.int, 0) |
| 82 | + |
| 83 | + if backward: |
| 84 | + flags |= lib.AVSEEK_FLAG_BACKWARD |
| 85 | + if any_frame: |
| 86 | + flags |= lib.AVSEEK_FLAG_ANY |
| 87 | + |
| 88 | + with cython.nogil: |
| 89 | + idx = lib.av_index_search_timestamp(self.stream_ptr, c_timestamp, flags) |
| 90 | + |
| 91 | + return idx |
0 commit comments