|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +import cython |
| 4 | +from cython.cimports import libav as lib |
| 5 | +from cython.cimports.cpython.bytes import PyBytes_FromStringAndSize |
| 6 | + |
| 7 | + |
| 8 | +@dataclass |
| 9 | +class AudioChannel: |
| 10 | + name: str |
| 11 | + description: str |
| 12 | + |
| 13 | + def __repr__(self): |
| 14 | + return f"<av.AudioChannel '{self.name}' ({self.description})>" |
| 15 | + |
| 16 | + |
| 17 | +_cinit_bypass_sentinel = cython.declare(object, object()) |
| 18 | + |
| 19 | + |
| 20 | +@cython.cfunc |
| 21 | +def get_audio_layout(c_layout: lib.AVChannelLayout) -> AudioLayout: |
| 22 | + """Get an AudioLayout from Cython land.""" |
| 23 | + layout: AudioLayout = AudioLayout(_cinit_bypass_sentinel) |
| 24 | + layout.layout = c_layout |
| 25 | + return layout |
| 26 | + |
| 27 | + |
| 28 | +@cython.cclass |
| 29 | +class AudioLayout: |
| 30 | + def __cinit__(self, layout): |
| 31 | + if layout is _cinit_bypass_sentinel: |
| 32 | + return |
| 33 | + |
| 34 | + if type(layout) is str: |
| 35 | + ret = lib.av_channel_layout_from_string(cython.address(c_layout), layout) |
| 36 | + if ret != 0: |
| 37 | + raise ValueError(f"Invalid layout: {layout}") |
| 38 | + elif isinstance(layout, AudioLayout): |
| 39 | + c_layout = cython.cast(AudioLayout, layout).layout |
| 40 | + else: |
| 41 | + raise TypeError( |
| 42 | + f"layout must be of type: string | av.AudioLayout, got {type(layout)}" |
| 43 | + ) |
| 44 | + |
| 45 | + self.layout = c_layout |
| 46 | + |
| 47 | + def __repr__(self): |
| 48 | + return f"<av.{self.__class__.__name__} {self.name!r}>" |
| 49 | + |
| 50 | + def __eq__(self, other): |
| 51 | + return ( |
| 52 | + isinstance(other, AudioLayout) |
| 53 | + and self.name == other.name |
| 54 | + and self.nb_channels == other.nb_channels |
| 55 | + ) |
| 56 | + |
| 57 | + @property |
| 58 | + def nb_channels(self): |
| 59 | + return self.layout.nb_channels |
| 60 | + |
| 61 | + @property |
| 62 | + def channels(self): |
| 63 | + channel: lib.AVChannel |
| 64 | + buf: cython.char[16] |
| 65 | + buf2: cython.char[128] |
| 66 | + results: list = [] |
| 67 | + |
| 68 | + for index in range(self.layout.nb_channels): |
| 69 | + channel = lib.av_channel_layout_channel_from_index( |
| 70 | + cython.address(self.layout), index |
| 71 | + ) |
| 72 | + |
| 73 | + size = lib.av_channel_name(buf, cython.sizeof(buf), channel) - 1 |
| 74 | + size2 = lib.av_channel_description(buf2, cython.sizeof(buf2), channel) - 1 |
| 75 | + results.append( |
| 76 | + AudioChannel( |
| 77 | + PyBytes_FromStringAndSize(buf, size).decode("utf-8"), |
| 78 | + PyBytes_FromStringAndSize(buf2, size2).decode("utf-8"), |
| 79 | + ) |
| 80 | + ) |
| 81 | + |
| 82 | + return tuple(results) |
| 83 | + |
| 84 | + @property |
| 85 | + def name(self) -> str: |
| 86 | + """The canonical name of the audio layout.""" |
| 87 | + layout_name: cython.char[129] |
| 88 | + ret: cython.int = lib.av_channel_layout_describe( |
| 89 | + cython.address(self.layout), layout_name, cython.sizeof(layout_name) |
| 90 | + ) |
| 91 | + if ret < 0: |
| 92 | + raise RuntimeError(f"Failed to get layout name: {ret}") |
| 93 | + |
| 94 | + return layout_name |
0 commit comments