Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit eceffa1

Browse files
added colorspace conversion options
1 parent 122ccf0 commit eceffa1

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

‎av/video/frame.pxd‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ cdef class VideoFrame(Frame):
2020
cdef _init(self, lib.AVPixelFormat format, unsigned int width, unsigned int height)
2121
cdef _init_properties(self)
2222

23-
cdef _reformat(self, unsigned int width, unsigned int height, lib.AVPixelFormat format)
23+
cdef _reformat(self, unsigned int width, unsigned int height, lib.AVPixelFormat format, int src_colorspace, int dst_colorspace)
2424

2525

2626
cdef VideoFrame alloc_video_frame()

‎av/video/frame.pyx‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ cdef class VideoFrame(Frame):
9494
"""
9595
return self.reformat(self.width, self.height, "rgb24")
9696

97-
def reformat(self, unsigned int width, unsigned int height, char* dst_format_str):
97+
def reformat(self, unsigned int width, unsigned int height, char* dst_format_str, src_colorspace=None, dst_colorspace=None):
9898

9999
"""reformat(width, height, format)
100100
@@ -110,10 +110,28 @@ cdef class VideoFrame(Frame):
110110
cdef lib.AVPixelFormat dst_format = lib.av_get_pix_fmt(dst_format_str)
111111
if dst_format == lib.AV_PIX_FMT_NONE:
112112
raise ValueError("invalid format %s" % dst_format_str)
113+
114+
115+
colorspace_dict = {'itu709': lib.SWS_CS_ITU709,
116+
'fcc': lib.SWS_CS_FCC,
117+
'itu601': lib.SWS_CS_ITU601,
118+
'itu624': lib.SWS_CS_SMPTE170M,
119+
'smpte240,': lib.SWS_CS_SMPTE240M,
120+
'default': lib.SWS_CS_DEFAULT}
121+
122+
cdef int cs_src = lib.SWS_CS_DEFAULT
123+
cdef int cs_dst = lib.SWS_CS_DEFAULT
124+
125+
if not src_colorspace is None:
126+
cs_src = colorspace_dict[src_colorspace.lower()]
113127

114-
return self._reformat(width, height, dst_format)
128+
if not dst_colorspace is None:
129+
cs_dst = colorspace_dict[dst_colorspace.lower()]
130+
131+
132+
return self._reformat(width, height, dst_format, cs_src, cs_dst)
115133

116-
cdef _reformat(self, unsigned int width, unsigned int height, lib.AVPixelFormat dst_format):
134+
cdef _reformat(self, unsigned int width, unsigned int height, lib.AVPixelFormat dst_format, int src_colorspace, int dst_colorspace):
117135

118136
if self.ptr.format < 0:
119137
raise ValueError("invalid source format")
@@ -146,6 +164,25 @@ cdef class VideoFrame(Frame):
146164
NULL
147165
)
148166

167+
cdef int *inv_tbl = NULL
168+
cdef int *tbl = NULL
169+
cdef int *rgbTbl = NULL
170+
171+
cdef int srcRange, dstRange, brightness, contrast, saturation
172+
173+
cdef int ret
174+
175+
ret = lib.sws_getColorspaceDetails(self.reformatter.ptr, &inv_tbl, &srcRange, &tbl, &dstRange, &brightness, &contrast, &saturation)
176+
err_check(ret)
177+
178+
if src_colorspace != lib.SWS_CS_DEFAULT:
179+
inv_tbl = lib.sws_getCoefficients(src_colorspace)
180+
181+
if dst_colorspace != lib.SWS_CS_DEFAULT:
182+
tbl = lib.sws_getCoefficients(dst_colorspace)
183+
184+
lib.sws_setColorspaceDetails(self.reformatter.ptr, inv_tbl, srcRange, tbl, dstRange, brightness, contrast, saturation)
185+
149186
# Create a new VideoFrame
150187

151188
cdef VideoFrame frame = alloc_video_frame()
@@ -181,7 +218,7 @@ cdef class VideoFrame(Frame):
181218
property key_frame:
182219
"""Is this frame a key frame?"""
183220
def __get__(self): return self.ptr.key_frame
184-
221+
185222
def to_image(self):
186223
import PIL.Image
187224
return PIL.Image.frombuffer("RGB", (self.width, self.height), self.to_rgb().planes[0], "raw", "RGB", 0, 1)

‎av/video/stream.pyx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ cdef class VideoStream(Stream):
106106
self._codec_context.width,
107107
self._codec_context.height,
108108
self.format.pix_fmt,
109+
lib.SWS_CS_DEFAULT,
110+
lib.SWS_CS_DEFAULT
109111
)
110112

111113
else:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /