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
15 changes: 15 additions & 0 deletions av/_core.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cdef extern from "libavdevice/avdevice.h" nogil:
cdef int avdevice_version()
cdef char* avdevice_configuration()
cdef char* avdevice_license()
void avdevice_register_all()

cdef extern from "libswscale/swscale.h" nogil:
cdef int swscale_version()
cdef char* swscale_configuration()
cdef char* swscale_license()

cdef extern from "libswresample/swresample.h" nogil:
cdef int swresample_version()
cdef char* swresample_configuration()
cdef char* swresample_license()
20 changes: 10 additions & 10 deletions av/_core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import cython
import cython.cimports.libav as lib

lib.avdevice_register_all()
avdevice_register_all()

# Exports.
time_base = lib.AV_TIME_BASE
Expand Down Expand Up @@ -41,24 +41,24 @@ def decode_version(v):
license=lib.avformat_license(),
),
"libavdevice": dict(
version=decode_version(lib.avdevice_version()),
configuration=lib.avdevice_configuration(),
license=lib.avdevice_license(),
version=decode_version(avdevice_version()),
configuration=avdevice_configuration(),
license=avdevice_license(),
),
"libavfilter": dict(
version=decode_version(lib.avfilter_version()),
configuration=lib.avfilter_configuration(),
license=lib.avfilter_license(),
),
"libswscale": dict(
version=decode_version(lib.swscale_version()),
configuration=lib.swscale_configuration(),
license=lib.swscale_license(),
version=decode_version(swscale_version()),
configuration=swscale_configuration(),
license=swscale_license(),
),
"libswresample": dict(
version=decode_version(lib.swresample_version()),
configuration=lib.swresample_configuration(),
license=lib.swresample_license(),
version=decode_version(swresample_version()),
configuration=swresample_configuration(),
license=swresample_license(),
),
}

Expand Down
6 changes: 6 additions & 0 deletions av/logging.pxd
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
cimport libav as lib


cdef extern from "Python.h" nogil:
void PyErr_PrintEx(int set_sys_last_vars)
int Py_IsInitialized()
void PyErr_Display(object, object, object)

cdef extern from "stdio.h" nogil:
cdef int vsnprintf(char *output, int n, const char *format, lib.va_list args)

cpdef get_last_error()
3 changes: 1 addition & 2 deletions av/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
from threading import Lock, get_ident

import cython
import cython.cimports.libav as lib
from cython.cimports.libc.stdio import fprintf, stderr
from cython.cimports.libc.stdlib import free, malloc

Expand Down Expand Up @@ -338,7 +337,7 @@ def log_callback(

# Format the message.
message: cython.char[1024]
lib.vsnprintf(message, 1023, format, args)
vsnprintf(message, 1023, format, args)

# Get the name.
name: cython.p_const_char = cython.NULL
Expand Down
2 changes: 0 additions & 2 deletions av/subtitles/subtitle.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ cimport libav as lib
cdef class SubtitleProxy:
cdef lib.AVSubtitle struct


cdef class SubtitleSet:
cdef SubtitleProxy proxy
cdef readonly tuple rects


cdef class Subtitle:
cdef SubtitleProxy proxy
cdef lib.AVSubtitleRect *ptr
Expand Down
7 changes: 0 additions & 7 deletions av/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
from cython.cimports import libav as lib
from cython.cimports.av.error import err_check

# === DICTIONARIES ===
# ====================


@cython.cfunc
def _decode(s: cython.pointer[cython.char], encoding, errors) -> str:
Expand Down Expand Up @@ -47,10 +44,6 @@ def dict_to_avdict(
)


# === FRACTIONS ===
# =================


@cython.cfunc
def avrational_to_fraction(
input: cython.pointer[cython.const[lib.AVRational]],
Expand Down
72 changes: 70 additions & 2 deletions av/video/reformatter.pxd
Original file line number Diff line number Diff line change
@@ -1,12 +1,80 @@
cimport libav as lib
from libc.stdint cimport uint8_t

from av.video.frame cimport VideoFrame


cdef class VideoReformatter:
cdef extern from "libswscale/swscale.h" nogil:
cdef struct SwsContext:
pass
cdef struct SwsFilter:
pass
cdef int SWS_FAST_BILINEAR
cdef int SWS_BILINEAR
cdef int SWS_BICUBIC
cdef int SWS_X
cdef int SWS_POINT
cdef int SWS_AREA
cdef int SWS_BICUBLIN
cdef int SWS_GAUSS
cdef int SWS_SINC
cdef int SWS_LANCZOS
cdef int SWS_SPLINE
cdef int SWS_CS_ITU709
cdef int SWS_CS_FCC
cdef int SWS_CS_ITU601
cdef int SWS_CS_ITU624
cdef int SWS_CS_SMPTE170M
cdef int SWS_CS_SMPTE240M
cdef int SWS_CS_DEFAULT

cdef lib.SwsContext *ptr
cdef int sws_scale(
SwsContext *ctx,
const uint8_t *const *src_slice,
const int *src_stride,
int src_slice_y,
int src_slice_h,
unsigned char *const *dst_slice,
const int *dst_stride,
)
cdef void sws_freeContext(SwsContext *ctx)
cdef SwsContext *sws_getCachedContext(
SwsContext *context,
int src_width,
int src_height,
lib.AVPixelFormat src_format,
int dst_width,
int dst_height,
lib.AVPixelFormat dst_format,
int flags,
SwsFilter *src_filter,
SwsFilter *dst_filter,
double *param,
)
cdef const int* sws_getCoefficients(int colorspace)
cdef int sws_getColorspaceDetails(
SwsContext *context,
int **inv_table,
int *srcRange,
int **table,
int *dstRange,
int *brightness,
int *contrast,
int *saturation
)
cdef int sws_setColorspaceDetails(
SwsContext *context,
const int inv_table[4],
int srcRange,
const int table[4],
int dstRange,
int brightness,
int contrast,
int saturation
)

cdef class VideoReformatter:
cdef SwsContext *ptr
cdef _reformat(self, VideoFrame frame, int width, int height,
lib.AVPixelFormat format, int src_colorspace,
int dst_colorspace, int interpolation,
Expand Down
77 changes: 37 additions & 40 deletions av/video/reformatter.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
from enum import IntEnum

import cython
import cython.cimports.libav as lib
from cython.cimports.av.error import err_check
from cython.cimports.av.video.format import VideoFormat
from cython.cimports.av.video.frame import alloc_video_frame


class Interpolation(IntEnum):
FAST_BILINEAR: "Fast bilinear" = lib.SWS_FAST_BILINEAR
BILINEAR: "Bilinear" = lib.SWS_BILINEAR
BICUBIC: "Bicubic" = lib.SWS_BICUBIC
X: "Experimental" = lib.SWS_X
POINT: "Nearest neighbor / point" = lib.SWS_POINT
AREA: "Area averaging" = lib.SWS_AREA
BICUBLIN: "Luma bicubic / chroma bilinear" = lib.SWS_BICUBLIN
GAUSS: "Gaussian" = lib.SWS_GAUSS
SINC: "Sinc" = lib.SWS_SINC
LANCZOS: "Bicubic spline" = lib.SWS_LANCZOS
FAST_BILINEAR: "Fast bilinear" = SWS_FAST_BILINEAR
BILINEAR: "Bilinear" = SWS_BILINEAR
BICUBIC: "Bicubic" = SWS_BICUBIC
X: "Experimental" = SWS_X
POINT: "Nearest neighbor / point" = SWS_POINT
AREA: "Area averaging" = SWS_AREA
BICUBLIN: "Luma bicubic / chroma bilinear" = SWS_BICUBLIN
GAUSS: "Gaussian" = SWS_GAUSS
SINC: "Sinc" = SWS_SINC
LANCZOS: "Bicubic spline" = SWS_LANCZOS


class Colorspace(IntEnum):
ITU709 = lib.SWS_CS_ITU709
FCC = lib.SWS_CS_FCC
ITU601 = lib.SWS_CS_ITU601
ITU624 = lib.SWS_CS_ITU624
SMPTE170M = lib.SWS_CS_SMPTE170M
SMPTE240M = lib.SWS_CS_SMPTE240M
DEFAULT = lib.SWS_CS_DEFAULT
ITU709 = SWS_CS_ITU709
FCC = SWS_CS_FCC
ITU601 = SWS_CS_ITU601
ITU624 = SWS_CS_ITU624
SMPTE170M = SWS_CS_SMPTE170M
SMPTE240M = SWS_CS_SMPTE240M
DEFAULT = SWS_CS_DEFAULT
# Lowercase for b/c.
itu709 = lib.SWS_CS_ITU709
fcc = lib.SWS_CS_FCC
itu601 = lib.SWS_CS_ITU601
itu624 = lib.SWS_CS_ITU624
smpte170m = lib.SWS_CS_SMPTE170M
smpte240m = lib.SWS_CS_SMPTE240M
default = lib.SWS_CS_DEFAULT
itu709 = SWS_CS_ITU709
fcc = SWS_CS_FCC
itu601 = SWS_CS_ITU601
itu624 = SWS_CS_ITU624
smpte170m = SWS_CS_SMPTE170M
smpte240m = SWS_CS_SMPTE240M
default = SWS_CS_DEFAULT


class ColorRange(IntEnum):
Expand Down Expand Up @@ -65,10 +64,10 @@ def _resolve_enum_value(value, enum_class, default):
_SWS_CS_TO_AVCOL_SPC = cython.declare(
dict,
{
lib.SWS_CS_ITU709: lib.AVCOL_SPC_BT709,
lib.SWS_CS_FCC: lib.AVCOL_SPC_FCC,
lib.SWS_CS_ITU601: lib.AVCOL_SPC_SMPTE170M,
lib.SWS_CS_SMPTE240M: lib.AVCOL_SPC_SMPTE240M,
SWS_CS_ITU709: lib.AVCOL_SPC_BT709,
SWS_CS_FCC: lib.AVCOL_SPC_FCC,
SWS_CS_ITU601: lib.AVCOL_SPC_SMPTE170M,
SWS_CS_SMPTE240M: lib.AVCOL_SPC_SMPTE240M,
},
)

Expand All @@ -84,7 +83,7 @@ class VideoReformatter:

def __dealloc__(self):
with cython.nogil:
lib.sws_freeContext(self.ptr)
sws_freeContext(self.ptr)

def reformat(
self,
Expand Down Expand Up @@ -212,7 +211,7 @@ def _reformat(
return frame

with cython.nogil:
self.ptr = lib.sws_getCachedContext(
self.ptr = sws_getCachedContext(
self.ptr,
frame.ptr.width,
frame.ptr.height,
Expand All @@ -239,7 +238,7 @@ def _reformat(

if src_colorspace != dst_colorspace or src_color_range != dst_color_range:
with cython.nogil:
ret = lib.sws_getColorspaceDetails(
ret = sws_getColorspaceDetails(
self.ptr,
cython.address(inv_tbl),
cython.address(src_colorspace_range),
Expand All @@ -254,16 +253,14 @@ def _reformat(
with cython.nogil:
# Grab the coefficients for the requested transforms.
# The inv_table brings us to linear, and `tbl` to the new space.
if src_colorspace != lib.SWS_CS_DEFAULT:
if src_colorspace != SWS_CS_DEFAULT:
inv_tbl = cython.cast(
cython.p_int, lib.sws_getCoefficients(src_colorspace)
)
if dst_colorspace != lib.SWS_CS_DEFAULT:
tbl = cython.cast(
cython.p_int, lib.sws_getCoefficients(dst_colorspace)
cython.p_int, sws_getCoefficients(src_colorspace)
)
if dst_colorspace != SWS_CS_DEFAULT:
tbl = cython.cast(cython.p_int, sws_getCoefficients(dst_colorspace))

ret = lib.sws_setColorspaceDetails(
ret = sws_setColorspaceDetails(
self.ptr,
inv_tbl,
src_color_range,
Expand All @@ -290,7 +287,7 @@ def _reformat(
)

with cython.nogil:
lib.sws_scale(
sws_scale(
self.ptr,
cython.cast("const unsigned char *const *", frame.ptr.data),
cython.cast("const int *", frame.ptr.linesize),
Expand Down
Loading
Loading