Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion av/audio/layout.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ cimport libav as lib

cdef class AudioLayout:
cdef lib.AVChannelLayout layout
cdef _init(self, lib.AVChannelLayout layout)

cdef AudioLayout get_audio_layout(lib.AVChannelLayout c_layout)
101 changes: 101 additions & 0 deletions av/audio/layout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
from dataclasses import dataclass

import cython
from cython.cimports import libav as lib
from cython.cimports.cpython.bytes import PyBytes_FromStringAndSize


@dataclass
class AudioChannel:
name: str
description: str

def __repr__(self):
return f"<av.AudioChannel '{self.name}' ({self.description})>"


_cinit_bypass_sentinel = cython.declare(object, object())


@cython.cfunc
def get_audio_layout(c_layout: lib.AVChannelLayout) -> AudioLayout:
"""Get an AudioLayout from Cython land."""
layout: AudioLayout = AudioLayout(_cinit_bypass_sentinel)
layout.layout = c_layout
return layout


@cython.cclass
class AudioLayout:
def __cinit__(self, layout):
if layout is _cinit_bypass_sentinel:
return

if type(layout) is str:
ret = lib.av_channel_layout_from_string(cython.address(c_layout), layout)
if ret != 0:
raise ValueError(f"Invalid layout: {layout}")
elif isinstance(layout, AudioLayout):
c_layout = cython.cast(AudioLayout, layout).layout
else:
raise TypeError(
f"layout must be of type: string | av.AudioLayout, got {type(layout)}"
)

self.layout = c_layout

def __repr__(self):
return f"<av.{self.__class__.__name__} {self.name!r}>"

def __eq__(self, other):
return (
isinstance(other, AudioLayout)
and self.name == other.name
and self.nb_channels == other.nb_channels
)

@property
def nb_channels(self):
return self.layout.nb_channels

@property
def channels(self):
buf: cython.char[16]
buf2: cython.char[128]

results: list = []
for index in range(self.layout.nb_channels):
size = lib.av_channel_name(
buf,
cython.sizeof(buf),
lib.av_channel_layout_channel_from_index(
cython.address(self.layout), index
),
)
size2 = lib.av_channel_description(
buf2,
cython.sizeof(buf2),
lib.av_channel_layout_channel_from_index(
cython.address(self.layout), index
),
)
results.append(
AudioChannel(
PyBytes_FromStringAndSize(buf, size - 1).decode("utf-8"),
PyBytes_FromStringAndSize(buf2, size2 - 1).decode("utf-8"),
)
)

return tuple(results)

@property
def name(self) -> str:
"""The canonical name of the audio layout."""
layout_name: cython.char[129]
ret: cython.int = lib.av_channel_layout_describe(
cython.address(self.layout), layout_name, cython.sizeof(layout_name)
)
if ret < 0:
raise RuntimeError(f"Failed to get layout name: {ret}")

return layout_name
82 changes: 0 additions & 82 deletions av/audio/layout.pyx

This file was deleted.

2 changes: 1 addition & 1 deletion av/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __iter__(self):
yield element.key

def __repr__(self):
return f"bv.Dictionary({dict(self)!r})"
return f"av.Dictionary({dict(self)!r})"

def copy(self):
other = cython.declare(_Dictionary, Dictionary())
Expand Down
Loading