Skip to content

Commit 642c6de

Browse files
committed
Make include/ smaller
Since almost everything needs libav, put as little as possible to reduce the need to recompile everything. `swscale` can be returned back to `include/` if another file besides `reformatter` needs it.
1 parent 1553e30 commit 642c6de

File tree

21 files changed

+232
-275
lines changed

21 files changed

+232
-275
lines changed

av/_core.pxd

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cdef extern from "libavdevice/avdevice.h" nogil:
2+
cdef int avdevice_version()
3+
cdef char* avdevice_configuration()
4+
cdef char* avdevice_license()
5+
void avdevice_register_all()
6+
7+
cdef extern from "libswscale/swscale.h" nogil:
8+
cdef int swscale_version()
9+
cdef char* swscale_configuration()
10+
cdef char* swscale_license()
11+
12+
cdef extern from "libswresample/swresample.h" nogil:
13+
cdef int swresample_version()
14+
cdef char* swresample_configuration()
15+
cdef char* swresample_license()

av/_core.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cython
22
import cython.cimports.libav as lib
33

4-
lib.avdevice_register_all()
4+
avdevice_register_all()
55

66
# Exports.
77
time_base = lib.AV_TIME_BASE
@@ -41,24 +41,24 @@ def decode_version(v):
4141
license=lib.avformat_license(),
4242
),
4343
"libavdevice": dict(
44-
version=decode_version(lib.avdevice_version()),
45-
configuration=lib.avdevice_configuration(),
46-
license=lib.avdevice_license(),
44+
version=decode_version(avdevice_version()),
45+
configuration=avdevice_configuration(),
46+
license=avdevice_license(),
4747
),
4848
"libavfilter": dict(
4949
version=decode_version(lib.avfilter_version()),
5050
configuration=lib.avfilter_configuration(),
5151
license=lib.avfilter_license(),
5252
),
5353
"libswscale": dict(
54-
version=decode_version(lib.swscale_version()),
55-
configuration=lib.swscale_configuration(),
56-
license=lib.swscale_license(),
54+
version=decode_version(swscale_version()),
55+
configuration=swscale_configuration(),
56+
license=swscale_license(),
5757
),
5858
"libswresample": dict(
59-
version=decode_version(lib.swresample_version()),
60-
configuration=lib.swresample_configuration(),
61-
license=lib.swresample_license(),
59+
version=decode_version(swresample_version()),
60+
configuration=swresample_configuration(),
61+
license=swresample_license(),
6262
),
6363
}
6464

av/logging.pxd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
cimport libav as lib
2+
3+
14
cdef extern from "Python.h" nogil:
25
void PyErr_PrintEx(int set_sys_last_vars)
36
int Py_IsInitialized()
47
void PyErr_Display(object, object, object)
58

9+
cdef extern from "stdio.h" nogil:
10+
cdef int vsnprintf(char *output, int n, const char *format, lib.va_list args)
11+
612
cpdef get_last_error()

av/logging.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
from threading import Lock, get_ident
4545

4646
import cython
47-
import cython.cimports.libav as lib
4847
from cython.cimports.libc.stdio import fprintf, stderr
4948
from cython.cimports.libc.stdlib import free, malloc
5049

@@ -338,7 +337,7 @@ def log_callback(
338337

339338
# Format the message.
340339
message: cython.char[1024]
341-
lib.vsnprintf(message, 1023, format, args)
340+
vsnprintf(message, 1023, format, args)
342341

343342
# Get the name.
344343
name: cython.p_const_char = cython.NULL

av/subtitles/subtitle.pxd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ cimport libav as lib
44
cdef class SubtitleProxy:
55
cdef lib.AVSubtitle struct
66

7-
87
cdef class SubtitleSet:
98
cdef SubtitleProxy proxy
109
cdef readonly tuple rects
1110

12-
1311
cdef class Subtitle:
1412
cdef SubtitleProxy proxy
1513
cdef lib.AVSubtitleRect *ptr

av/utils.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
from cython.cimports import libav as lib
66
from cython.cimports.av.error import err_check
77

8-
# === DICTIONARIES ===
9-
# ====================
10-
118

129
@cython.cfunc
1310
def _decode(s: cython.pointer[cython.char], encoding, errors) -> str:
@@ -47,10 +44,6 @@ def dict_to_avdict(
4744
)
4845

4946

50-
# === FRACTIONS ===
51-
# =================
52-
53-
5447
@cython.cfunc
5548
def avrational_to_fraction(
5649
input: cython.pointer[cython.const[lib.AVRational]],

av/video/reformatter.pxd

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,80 @@
11
cimport libav as lib
2+
from libc.stdint cimport uint8_t
23

34
from av.video.frame cimport VideoFrame
45

56

6-
cdef class VideoReformatter:
7+
cdef extern from "libswscale/swscale.h" nogil:
8+
cdef struct SwsContext:
9+
pass
10+
cdef struct SwsFilter:
11+
pass
12+
cdef int SWS_FAST_BILINEAR
13+
cdef int SWS_BILINEAR
14+
cdef int SWS_BICUBIC
15+
cdef int SWS_X
16+
cdef int SWS_POINT
17+
cdef int SWS_AREA
18+
cdef int SWS_BICUBLIN
19+
cdef int SWS_GAUSS
20+
cdef int SWS_SINC
21+
cdef int SWS_LANCZOS
22+
cdef int SWS_SPLINE
23+
cdef int SWS_CS_ITU709
24+
cdef int SWS_CS_FCC
25+
cdef int SWS_CS_ITU601
26+
cdef int SWS_CS_ITU624
27+
cdef int SWS_CS_SMPTE170M
28+
cdef int SWS_CS_SMPTE240M
29+
cdef int SWS_CS_DEFAULT
730

8-
cdef lib.SwsContext *ptr
31+
cdef int sws_scale(
32+
SwsContext *ctx,
33+
const uint8_t *const *src_slice,
34+
const int *src_stride,
35+
int src_slice_y,
36+
int src_slice_h,
37+
unsigned char *const *dst_slice,
38+
const int *dst_stride,
39+
)
40+
cdef void sws_freeContext(SwsContext *ctx)
41+
cdef SwsContext *sws_getCachedContext(
42+
SwsContext *context,
43+
int src_width,
44+
int src_height,
45+
lib.AVPixelFormat src_format,
46+
int dst_width,
47+
int dst_height,
48+
lib.AVPixelFormat dst_format,
49+
int flags,
50+
SwsFilter *src_filter,
51+
SwsFilter *dst_filter,
52+
double *param,
53+
)
54+
cdef const int* sws_getCoefficients(int colorspace)
55+
cdef int sws_getColorspaceDetails(
56+
SwsContext *context,
57+
int **inv_table,
58+
int *srcRange,
59+
int **table,
60+
int *dstRange,
61+
int *brightness,
62+
int *contrast,
63+
int *saturation
64+
)
65+
cdef int sws_setColorspaceDetails(
66+
SwsContext *context,
67+
const int inv_table[4],
68+
int srcRange,
69+
const int table[4],
70+
int dstRange,
71+
int brightness,
72+
int contrast,
73+
int saturation
74+
)
975

76+
cdef class VideoReformatter:
77+
cdef SwsContext *ptr
1078
cdef _reformat(self, VideoFrame frame, int width, int height,
1179
lib.AVPixelFormat format, int src_colorspace,
1280
int dst_colorspace, int interpolation,

av/video/reformatter.py

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,40 @@
11
from enum import IntEnum
22

33
import cython
4-
import cython.cimports.libav as lib
54
from cython.cimports.av.error import err_check
65
from cython.cimports.av.video.format import VideoFormat
76
from cython.cimports.av.video.frame import alloc_video_frame
87

98

109
class Interpolation(IntEnum):
11-
FAST_BILINEAR: "Fast bilinear" = lib.SWS_FAST_BILINEAR
12-
BILINEAR: "Bilinear" = lib.SWS_BILINEAR
13-
BICUBIC: "Bicubic" = lib.SWS_BICUBIC
14-
X: "Experimental" = lib.SWS_X
15-
POINT: "Nearest neighbor / point" = lib.SWS_POINT
16-
AREA: "Area averaging" = lib.SWS_AREA
17-
BICUBLIN: "Luma bicubic / chroma bilinear" = lib.SWS_BICUBLIN
18-
GAUSS: "Gaussian" = lib.SWS_GAUSS
19-
SINC: "Sinc" = lib.SWS_SINC
20-
LANCZOS: "Bicubic spline" = lib.SWS_LANCZOS
10+
FAST_BILINEAR: "Fast bilinear" = SWS_FAST_BILINEAR
11+
BILINEAR: "Bilinear" = SWS_BILINEAR
12+
BICUBIC: "Bicubic" = SWS_BICUBIC
13+
X: "Experimental" = SWS_X
14+
POINT: "Nearest neighbor / point" = SWS_POINT
15+
AREA: "Area averaging" = SWS_AREA
16+
BICUBLIN: "Luma bicubic / chroma bilinear" = SWS_BICUBLIN
17+
GAUSS: "Gaussian" = SWS_GAUSS
18+
SINC: "Sinc" = SWS_SINC
19+
LANCZOS: "Bicubic spline" = SWS_LANCZOS
2120

2221

2322
class Colorspace(IntEnum):
24-
ITU709 = lib.SWS_CS_ITU709
25-
FCC = lib.SWS_CS_FCC
26-
ITU601 = lib.SWS_CS_ITU601
27-
ITU624 = lib.SWS_CS_ITU624
28-
SMPTE170M = lib.SWS_CS_SMPTE170M
29-
SMPTE240M = lib.SWS_CS_SMPTE240M
30-
DEFAULT = lib.SWS_CS_DEFAULT
23+
ITU709 = SWS_CS_ITU709
24+
FCC = SWS_CS_FCC
25+
ITU601 = SWS_CS_ITU601
26+
ITU624 = SWS_CS_ITU624
27+
SMPTE170M = SWS_CS_SMPTE170M
28+
SMPTE240M = SWS_CS_SMPTE240M
29+
DEFAULT = SWS_CS_DEFAULT
3130
# Lowercase for b/c.
32-
itu709 = lib.SWS_CS_ITU709
33-
fcc = lib.SWS_CS_FCC
34-
itu601 = lib.SWS_CS_ITU601
35-
itu624 = lib.SWS_CS_ITU624
36-
smpte170m = lib.SWS_CS_SMPTE170M
37-
smpte240m = lib.SWS_CS_SMPTE240M
38-
default = lib.SWS_CS_DEFAULT
31+
itu709 = SWS_CS_ITU709
32+
fcc = SWS_CS_FCC
33+
itu601 = SWS_CS_ITU601
34+
itu624 = SWS_CS_ITU624
35+
smpte170m = SWS_CS_SMPTE170M
36+
smpte240m = SWS_CS_SMPTE240M
37+
default = SWS_CS_DEFAULT
3938

4039

4140
class ColorRange(IntEnum):
@@ -65,10 +64,10 @@ def _resolve_enum_value(value, enum_class, default):
6564
_SWS_CS_TO_AVCOL_SPC = cython.declare(
6665
dict,
6766
{
68-
lib.SWS_CS_ITU709: lib.AVCOL_SPC_BT709,
69-
lib.SWS_CS_FCC: lib.AVCOL_SPC_FCC,
70-
lib.SWS_CS_ITU601: lib.AVCOL_SPC_SMPTE170M,
71-
lib.SWS_CS_SMPTE240M: lib.AVCOL_SPC_SMPTE240M,
67+
SWS_CS_ITU709: lib.AVCOL_SPC_BT709,
68+
SWS_CS_FCC: lib.AVCOL_SPC_FCC,
69+
SWS_CS_ITU601: lib.AVCOL_SPC_SMPTE170M,
70+
SWS_CS_SMPTE240M: lib.AVCOL_SPC_SMPTE240M,
7271
},
7372
)
7473

@@ -84,7 +83,7 @@ class VideoReformatter:
8483

8584
def __dealloc__(self):
8685
with cython.nogil:
87-
lib.sws_freeContext(self.ptr)
86+
sws_freeContext(self.ptr)
8887

8988
def reformat(
9089
self,
@@ -212,7 +211,7 @@ def _reformat(
212211
return frame
213212

214213
with cython.nogil:
215-
self.ptr = lib.sws_getCachedContext(
214+
self.ptr = sws_getCachedContext(
216215
self.ptr,
217216
frame.ptr.width,
218217
frame.ptr.height,
@@ -239,7 +238,7 @@ def _reformat(
239238

240239
if src_colorspace != dst_colorspace or src_color_range != dst_color_range:
241240
with cython.nogil:
242-
ret = lib.sws_getColorspaceDetails(
241+
ret = sws_getColorspaceDetails(
243242
self.ptr,
244243
cython.address(inv_tbl),
245244
cython.address(src_colorspace_range),
@@ -254,16 +253,14 @@ def _reformat(
254253
with cython.nogil:
255254
# Grab the coefficients for the requested transforms.
256255
# The inv_table brings us to linear, and `tbl` to the new space.
257-
if src_colorspace != lib.SWS_CS_DEFAULT:
256+
if src_colorspace != SWS_CS_DEFAULT:
258257
inv_tbl = cython.cast(
259-
cython.p_int, lib.sws_getCoefficients(src_colorspace)
260-
)
261-
if dst_colorspace != lib.SWS_CS_DEFAULT:
262-
tbl = cython.cast(
263-
cython.p_int, lib.sws_getCoefficients(dst_colorspace)
258+
cython.p_int, sws_getCoefficients(src_colorspace)
264259
)
260+
if dst_colorspace != SWS_CS_DEFAULT:
261+
tbl = cython.cast(cython.p_int, sws_getCoefficients(dst_colorspace))
265262

266-
ret = lib.sws_setColorspaceDetails(
263+
ret = sws_setColorspaceDetails(
267264
self.ptr,
268265
inv_tbl,
269266
src_color_range,
@@ -290,7 +287,7 @@ def _reformat(
290287
)
291288

292289
with cython.nogil:
293-
lib.sws_scale(
290+
sws_scale(
294291
self.ptr,
295292
cython.cast("const unsigned char *const *", frame.ptr.data),
296293
cython.cast("const int *", frame.ptr.linesize),

0 commit comments

Comments
 (0)