| 1 |
import cython |
| 2 |
import cython.cimports.libav as lib |
| 3 |
from cython.cimports.libc.stdint import int64_t |
| 4 |
|
| 5 |
_cinit_bypass_sentinel = cython.declare(object, object()) |
| 6 |
|
| 7 |
|
| 8 |
@cython.cfunc |
| 9 |
def wrap_index_entry(ptr: cython.pointer[cython.const[lib.AVIndexEntry]]) -> IndexEntry: |
| 10 |
obj: IndexEntry = IndexEntry(_cinit_bypass_sentinel) |
| 11 |
obj._init(ptr) |
| 12 |
return obj |
| 13 |
|
| 14 |
|
| 15 |
@cython.final |
| 16 |
@cython.cclass |
| 17 |
class IndexEntry: |
| 18 |
"""A single entry from a stream's index. |
| 19 |
|
| 20 |
This is a thin wrapper around FFmpeg's ``AVIndexEntry``. |
| 21 |
|
| 22 |
The exact meaning of the fields depends on the container/demuxer. |
| 23 |
""" |
| 24 |
|
| 25 |
def __cinit__(self, sentinel): |
| 26 |
if sentinel is not _cinit_bypass_sentinel: |
| 27 |
raise RuntimeError("cannot manually instantiate IndexEntry") |
| 28 |
|
| 29 |
@cython.cfunc |
| 30 |
def _init(self, ptr: cython.pointer[cython.const[lib.AVIndexEntry]]): |
| 31 |
self.ptr = ptr |
| 32 |
|
| 33 |
def __repr__(self): |
| 34 |
return ( |
| 35 |
f"<av.IndexEntry pos={self.pos} timestamp={self.timestamp} flags={self.flags} " |
| 36 |
f"size={self.size} min_distance={self.min_distance}>" |
| 37 |
) |
| 38 |
|
| 39 |
@property |
| 40 |
def pos(self): |
| 41 |
return self.ptr.pos |
| 42 |
|
| 43 |
@property |
| 44 |
def timestamp(self): |
| 45 |
return self.ptr.timestamp |
| 46 |
|
| 47 |
@property |
| 48 |
def flags(self): |
| 49 |
return self.ptr.flags |
| 50 |
|
| 51 |
@property |
| 52 |
def is_keyframe(self): |
| 53 |
return bool(self.ptr.flags & lib.AVINDEX_KEYFRAME) |
| 54 |
|
| 55 |
@property |
| 56 |
def is_discard(self): |
| 57 |
return bool(self.ptr.flags & lib.AVINDEX_DISCARD_FRAME) |
| 58 |
|
| 59 |
@property |
| 60 |
def size(self): |
| 61 |
return self.ptr.size |
| 62 |
|
| 63 |
@property |
| 64 |
def min_distance(self): |
| 65 |
return self.ptr.min_distance |
| 66 |
|
| 67 |
|
| 68 |
@cython.cfunc |
| 69 |
def wrap_index_entries(ptr: cython.pointer[lib.AVStream]) -> IndexEntries: |
| 70 |
obj: IndexEntries = IndexEntries(_cinit_bypass_sentinel) |
| 71 |
obj._init(ptr) |
| 72 |
return obj |
| 73 |
|
| 74 |
|
| 75 |
@cython.final |
| 76 |
@cython.cclass |
| 77 |
class IndexEntries: |
| 78 |
"""A sequence-like view of FFmpeg's per-stream index entries. |
| 79 |
|
| 80 |
Exposed as :attr:`~av.stream.Stream.index_entries`. |
| 81 |
|
| 82 |
The index is provided by the demuxer and may be empty or incomplete depending |
| 83 |
on the container format. This is useful for fast multi-seek loops (e.g., decoding |
| 84 |
at a lower-than-native framerate). |
| 85 |
""" |
| 86 |
|
| 87 |
def __cinit__(self, sentinel): |
| 88 |
if sentinel is _cinit_bypass_sentinel: |
| 89 |
return |
| 90 |
raise RuntimeError("cannot manually instantiate IndexEntries") |
| 91 |
|
| 92 |
@cython.cfunc |
| 93 |
def _init(self, ptr: cython.pointer[lib.AVStream]): |
| 94 |
self.stream_ptr = ptr |
| 95 |
|
| 96 |
def __repr__(self): |
| 97 |
return f"<av.IndexEntries[{len(self)}]>" |
| 98 |
|
| 99 |
def __len__(self) -> int: |
| 100 |
with cython.nogil: |
| 101 |
return lib.avformat_index_get_entries_count(self.stream_ptr) |
| 102 |
|
| 103 |
def __iter__(self): |
| 104 |
for i in range(len(self)): |
| 105 |
yield self[i] |
| 106 |
|
| 107 |
def __getitem__(self, index): |
| 108 |
if isinstance(index, int): |
| 109 |
n = len(self) |
| 110 |
if index < 0: |
| 111 |
index += n |
| 112 |
if index < 0 or index >= n: |
| 113 |
raise IndexError(f"Index entries {index} out of bounds for size {n}") |
| 114 |
|
| 115 |
c_idx: cython.int = index |
| 116 |
entry: cython.pointer[cython.const[lib.AVIndexEntry]] |
| 117 |
with cython.nogil: |
| 118 |
entry = lib.avformat_index_get_entry(self.stream_ptr, c_idx) |
| 119 |
if entry == cython.NULL: |
| 120 |
raise IndexError("index entry not found") |
| 121 |
|
| 122 |
return wrap_index_entry(entry) |
| 123 |
elif isinstance(index, slice): |
| 124 |
start, stop, step = index.indices(len(self)) |
| 125 |
return [self[i] for i in range(start, stop, step)] |
| 126 |
else: |
| 127 |
raise TypeError("Index must be an integer or a slice") |
| 128 |
|
| 129 |
def search_timestamp( |
| 130 |
self, timestamp, *, backward: bool = True, any_frame: bool = False |
| 131 |
): |
| 132 |
"""Search the underlying index for ``timestamp``. |
| 133 |
|
| 134 |
This wraps FFmpeg's ``av_index_search_timestamp``. |
| 135 |
|
| 136 |
Returns an index into this object, or ``-1`` if no match is found. |
| 137 |
""" |
| 138 |
c_timestamp: int64_t = timestamp |
| 139 |
flags: cython.int = 0 |
| 140 |
|
| 141 |
if backward: |
| 142 |
flags |= lib.AVSEEK_FLAG_BACKWARD |
| 143 |
if any_frame: |
| 144 |
flags |= lib.AVSEEK_FLAG_ANY |
| 145 |
|
| 146 |
with cython.nogil: |
| 147 |
idx = lib.av_index_search_timestamp(self.stream_ptr, c_timestamp, flags) |
| 148 |
|
| 149 |
return idx |