Skip to content

Commit 4baa626

Browse files
lgeigerWyattBlue
authored andcommitted
Reduce Python interactions
1 parent 0e7acc1 commit 4baa626

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

av/video/reformatter.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from enum import IntEnum
22

33
import cython
4+
import cython.cimports.libav as lib
45
from cython.cimports.av.error import err_check
56
from cython.cimports.av.video.format import VideoFormat
67
from cython.cimports.av.video.frame import alloc_video_frame
@@ -107,30 +108,25 @@ def _resolve_enum_value(
107108

108109
@cython.cfunc
109110
def _set_frame_colorspace(
110-
frame: VideoFrame, colorspace: cython.int, color_range: cython.int
111+
frame: cython.pointer(lib.AVFrame),
112+
colorspace: cython.int,
113+
color_range: cython.int,
111114
):
112115
"""Set AVFrame colorspace/range from SWS_CS_* and AVColorRange values."""
113-
if colorspace in _SWS_CS_TO_AVCOL_SPC:
114-
frame.ptr.colorspace = cython.cast(
115-
lib.AVColorSpace, _SWS_CS_TO_AVCOL_SPC[colorspace]
116-
)
117116
if color_range != lib.AVCOL_RANGE_UNSPECIFIED:
118-
frame.ptr.color_range = cython.cast(lib.AVColorRange, color_range)
119-
120-
121-
# Mapping from SWS_CS_* (swscale colorspace) to AVColorSpace (frame metadata).
122-
# Note: SWS_CS_ITU601, SWS_CS_ITU624, SWS_CS_SMPTE170M, and SWS_CS_DEFAULT all have
123-
# the same value (5), so we map 5 -> AVCOL_SPC_SMPTE170M as the most common case.
124-
# SWS_CS_DEFAULT is handled specially by not setting frame metadata.
125-
_SWS_CS_TO_AVCOL_SPC = cython.declare(
126-
dict,
127-
{
128-
SWS_CS_ITU709: lib.AVCOL_SPC_BT709,
129-
SWS_CS_FCC: lib.AVCOL_SPC_FCC,
130-
SWS_CS_ITU601: lib.AVCOL_SPC_SMPTE170M,
131-
SWS_CS_SMPTE240M: lib.AVCOL_SPC_SMPTE240M,
132-
},
133-
)
117+
frame.color_range = cython.cast(lib.AVColorRange, color_range)
118+
# Mapping from SWS_CS_* (swscale colorspace) to AVColorSpace (frame metadata).
119+
# Note: SWS_CS_ITU601, SWS_CS_ITU624, SWS_CS_SMPTE170M, and SWS_CS_DEFAULT all have
120+
# the same value (5), so we map 5 -> AVCOL_SPC_SMPTE170M as the most common case.
121+
# SWS_CS_DEFAULT is handled specially by not setting frame metadata.
122+
if colorspace == SWS_CS_ITU709:
123+
frame.colorspace = lib.AVCOL_SPC_BT709
124+
elif colorspace == SWS_CS_FCC:
125+
frame.colorspace = lib.AVCOL_SPC_FCC
126+
elif colorspace == SWS_CS_ITU601:
127+
frame.colorspace = lib.AVCOL_SPC_SMPTE170M
128+
elif colorspace == SWS_CS_SMPTE240M:
129+
frame.colorspace = lib.AVCOL_SPC_SMPTE240M
134130

135131

136132
@cython.cclass
@@ -300,8 +296,8 @@ def _reformat(
300296
# Set source frame colorspace/range so sws_scale_frame can read it
301297
frame_src_colorspace: lib.AVColorSpace = frame.ptr.colorspace
302298
frame_src_color_range: lib.AVColorRange = frame.ptr.color_range
303-
_set_frame_colorspace(frame, src_colorspace, src_color_range)
304-
_set_frame_colorspace(new_frame, dst_colorspace, dst_color_range)
299+
_set_frame_colorspace(frame.ptr, src_colorspace, src_color_range)
300+
_set_frame_colorspace(new_frame.ptr, dst_colorspace, dst_color_range)
305301

306302
with cython.nogil:
307303
ret = sws_scale_frame(self.ptr, new_frame.ptr, frame.ptr)

0 commit comments

Comments
 (0)