Skip to content

Commit 36b3978

Browse files
committed
Make utils pure
1 parent 6dd3dfd commit 36b3978

File tree

4 files changed

+94
-85
lines changed

4 files changed

+94
-85
lines changed

av/option.pyx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
cimport libav as lib
2-
3-
from av.utils cimport flag_in_bitfield
2+
from libc.stdint cimport uint64_t
43

54
from enum import Enum, Flag
65

@@ -16,6 +15,13 @@ cdef Option wrap_option(tuple choices, const lib.AVOption *ptr):
1615
return obj
1716

1817

18+
cdef flag_in_bitfield(uint64_t bitfield, uint64_t flag):
19+
# Not every flag exists in every version of FFMpeg, so we define them to 0.
20+
if not flag:
21+
return None
22+
return bool(bitfield & flag)
23+
24+
1925
class OptionType(Enum):
2026
FLAGS = lib.AV_OPT_TYPE_FLAGS
2127
INT = lib.AV_OPT_TYPE_INT

av/utils.pxd

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
cimport libav as lib
2-
from libc.stdint cimport uint64_t
3-
42

53
cdef dict avdict_to_dict(lib.AVDictionary *input, str encoding, str errors)
64
cdef dict_to_avdict(lib.AVDictionary **dst, dict src, str encoding, str errors)
75

86
cdef object avrational_to_fraction(const lib.AVRational *input)
97
cdef void to_avrational(object frac, lib.AVRational *input)
10-
11-
cdef check_ndarray(object array, object dtype, int ndim)
12-
cdef flag_in_bitfield(uint64_t bitfield, uint64_t flag)
8+
cdef void check_ndarray(object array, object dtype, int ndim)

av/utils.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# type: ignore
2+
from fractions import Fraction
3+
4+
import cython
5+
from cython.cimports import libav as lib
6+
from cython.cimports.av.error import err_check
7+
8+
# === DICTIONARIES ===
9+
# ====================
10+
11+
12+
@cython.cfunc
13+
def _decode(s: cython.pointer[cython.char], encoding, errors) -> str:
14+
return cython.cast(bytes, s).decode(encoding, errors)
15+
16+
17+
@cython.cfunc
18+
def _encode(s, encoding, errors) -> bytes:
19+
return s.encode(encoding, errors)
20+
21+
22+
@cython.cfunc
23+
def avdict_to_dict(
24+
input: cython.pointer[lib.AVDictionary], encoding: str, errors: str
25+
) -> dict:
26+
element: cython.pointer[lib.AVDictionaryEntry] = cython.NULL
27+
output: dict = {}
28+
while True:
29+
element = lib.av_dict_get(input, "", element, lib.AV_DICT_IGNORE_SUFFIX)
30+
if element == cython.NULL:
31+
break
32+
output[_decode(element.key, encoding, errors)] = _decode(
33+
element.value, encoding, errors
34+
)
35+
36+
return output
37+
38+
39+
@cython.cfunc
40+
def dict_to_avdict(
41+
dst: cython.pointer[cython.pointer[lib.AVDictionary]],
42+
src: dict,
43+
encoding: str,
44+
errors: str,
45+
):
46+
lib.av_dict_free(dst)
47+
for key, value in src.items():
48+
err_check(
49+
lib.av_dict_set(
50+
dst, key.encode(encoding, errors), value.encode(encoding, errors), 0
51+
)
52+
)
53+
54+
55+
# === FRACTIONS ===
56+
# =================
57+
58+
59+
@cython.cfunc
60+
def avrational_to_fraction(
61+
input: cython.pointer[cython.const[lib.AVRational]],
62+
) -> object:
63+
if input.num and input.den:
64+
return Fraction(input.num, input.den)
65+
return None
66+
67+
@cython.cfunc
68+
def to_avrational(frac: object, input: cython.pointer[lib.AVRational]) -> cython.void:
69+
input.num = frac.numerator
70+
input.den = frac.denominator
71+
72+
73+
@cython.cfunc
74+
def check_ndarray(array: object, dtype: object, ndim: cython.int) -> cython.void:
75+
"""
76+
Check a numpy array has the expected data type and number of dimensions.
77+
"""
78+
if array.dtype != dtype:
79+
raise ValueError(
80+
f"Expected numpy array with dtype `{dtype}` but got `{array.dtype}`"
81+
)
82+
if array.ndim != ndim:
83+
raise ValueError(
84+
f"Expected numpy array with ndim `{ndim}` but got `{array.ndim}`"
85+
)

av/utils.pyx

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)