| 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.final |
| 29 |
@cython.cclass |
| 30 |
class AudioLayout: |
| 31 |
def __dealloc__(self): |
| 32 |
lib.av_channel_layout_uninit(cython.address(self.layout)) |
| 33 |
|
| 34 |
def __cinit__(self, layout): |
| 35 |
if layout is _cinit_bypass_sentinel: |
| 36 |
return |
| 37 |
|
| 38 |
if type(layout) is str: |
| 39 |
ret = lib.av_channel_layout_from_string(cython.address(c_layout), layout) |
| 40 |
if ret != 0: |
| 41 |
raise ValueError(f"Invalid layout: {layout}") |
| 42 |
elif isinstance(layout, AudioLayout): |
| 43 |
c_layout = cython.cast(AudioLayout, layout).layout |
| 44 |
else: |
| 45 |
raise TypeError( |
| 46 |
f"layout must be of type: string | av.AudioLayout, got {type(layout)}" |
| 47 |
) |
| 48 |
|
| 49 |
self.layout = c_layout |
| 50 |
|
| 51 |
def __repr__(self): |
| 52 |
return f"<av.{self.__class__.__name__} {self.name!r}>" |
| 53 |
|
| 54 |
def __eq__(self, other): |
| 55 |
if not isinstance(other, AudioLayout): |
| 56 |
return False |
| 57 |
c_other: lib.AVChannelLayout = cython.cast(AudioLayout, other).layout |
| 58 |
return ( |
| 59 |
lib.av_channel_layout_compare( |
| 60 |
cython.address(self.layout), cython.address(c_other) |
| 61 |
) |
| 62 |
== 0 |
| 63 |
) |
| 64 |
|
| 65 |
@property |
| 66 |
def nb_channels(self): |
| 67 |
return self.layout.nb_channels |
| 68 |
|
| 69 |
@property |
| 70 |
def channels(self): |
| 71 |
buf: cython.char[16] |
| 72 |
buf2: cython.char[128] |
| 73 |
|
| 74 |
results: list = [] |
| 75 |
for index in range(self.layout.nb_channels): |
| 76 |
size = lib.av_channel_name( |
| 77 |
buf, |
| 78 |
cython.sizeof(buf), |
| 79 |
lib.av_channel_layout_channel_from_index( |
| 80 |
cython.address(self.layout), index |
| 81 |
), |
| 82 |
) |
| 83 |
size2 = lib.av_channel_description( |
| 84 |
buf2, |
| 85 |
cython.sizeof(buf2), |
| 86 |
lib.av_channel_layout_channel_from_index( |
| 87 |
cython.address(self.layout), index |
| 88 |
), |
| 89 |
) |
| 90 |
results.append( |
| 91 |
AudioChannel( |
| 92 |
PyBytes_FromStringAndSize(buf, size - 1).decode("utf-8"), |
| 93 |
PyBytes_FromStringAndSize(buf2, size2 - 1).decode("utf-8"), |
| 94 |
) |
| 95 |
) |
| 96 |
|
| 97 |
return tuple(results) |
| 98 |
|
| 99 |
@property |
| 100 |
def name(self) -> str: |
| 101 |
"""The canonical name of the audio layout.""" |
| 102 |
layout_name: cython.char[129] |
| 103 |
ret: cython.int = lib.av_channel_layout_describe( |
| 104 |
cython.address(self.layout), layout_name, cython.sizeof(layout_name) |
| 105 |
) |
| 106 |
if ret < 0: |
| 107 |
raise RuntimeError(f"Failed to get layout name: {ret}") |
| 108 |
|
| 109 |
return layout_name |