@@ -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 )
0 commit comments