|
| 1 | +import cython |
| 2 | +import cython.cimports.libav as lib |
| 3 | +from cython.cimports.av.option import ( |
| 4 | + Option, |
| 5 | + OptionChoice, |
| 6 | + wrap_option, |
| 7 | + wrap_option_choice, |
| 8 | +) |
| 9 | + |
| 10 | +_cinit_sentinel = cython.declare(object, object()) |
| 11 | + |
| 12 | + |
| 13 | +@cython.cfunc |
| 14 | +def wrap_avclass(ptr: cython.pointer[cython.const[lib.AVClass]]) -> Descriptor | None: |
| 15 | + if ptr == cython.NULL: |
| 16 | + return None |
| 17 | + obj: Descriptor = Descriptor(_cinit_sentinel) |
| 18 | + obj.ptr = ptr |
| 19 | + return obj |
| 20 | + |
| 21 | + |
| 22 | +@cython.cclass |
| 23 | +class Descriptor: |
| 24 | + def __cinit__(self, sentinel): |
| 25 | + if sentinel is not _cinit_sentinel: |
| 26 | + raise RuntimeError("Cannot construct av.Descriptor") |
| 27 | + |
| 28 | + @property |
| 29 | + def name(self): |
| 30 | + return self.ptr.class_name if self.ptr.class_name else None |
| 31 | + |
| 32 | + @property |
| 33 | + def options(self): |
| 34 | + ptr: cython.pointer[cython.const[lib.AVOption]] = self.ptr.option |
| 35 | + choice_ptr: cython.pointer[cython.const[lib.AVOption]] |
| 36 | + option: Option |
| 37 | + option_choice: OptionChoice |
| 38 | + choice_is_default: cython.bint |
| 39 | + if self._options is None: |
| 40 | + options: list = [] |
| 41 | + ptr = self.ptr.option |
| 42 | + while ptr != cython.NULL and ptr.name != cython.NULL: |
| 43 | + if ptr.type == lib.AV_OPT_TYPE_CONST: |
| 44 | + ptr += 1 |
| 45 | + continue |
| 46 | + choices: list = [] |
| 47 | + if ( |
| 48 | + ptr.unit != cython.NULL |
| 49 | + ): # option has choices (matching const options) |
| 50 | + choice_ptr = self.ptr.option |
| 51 | + while choice_ptr != cython.NULL and choice_ptr.name != cython.NULL: |
| 52 | + if ( |
| 53 | + choice_ptr.type != lib.AV_OPT_TYPE_CONST |
| 54 | + or choice_ptr.unit != ptr.unit |
| 55 | + ): |
| 56 | + choice_ptr += 1 |
| 57 | + continue |
| 58 | + choice_is_default = ( |
| 59 | + choice_ptr.default_val.i64 == ptr.default_val.i64 |
| 60 | + or ptr.type == lib.AV_OPT_TYPE_FLAGS |
| 61 | + and choice_ptr.default_val.i64 & ptr.default_val.i64 |
| 62 | + ) |
| 63 | + option_choice = wrap_option_choice( |
| 64 | + choice_ptr, choice_is_default |
| 65 | + ) |
| 66 | + choices.append(option_choice) |
| 67 | + choice_ptr += 1 |
| 68 | + option = wrap_option(tuple(choices), ptr) |
| 69 | + options.append(option) |
| 70 | + ptr += 1 |
| 71 | + self._options = tuple(options) |
| 72 | + return self._options |
| 73 | + |
| 74 | + def __repr__(self): |
| 75 | + return f"<{self.__class__.__name__} {self.name} at 0x{id(self):x}>" |
0 commit comments