Skip to content

Commit

Permalink
Add color_range to CodecContext/Frame
Browse files Browse the repository at this point in the history
Change-Id: I42451b3b84ea7ca4ae645566d1ef4709e0326f92
  • Loading branch information
Johan Jeppsson authored and johanjeppsson committed Aug 29, 2020
1 parent 1fa8dd6 commit 4d9386c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
7 changes: 7 additions & 0 deletions av/video/codeccontext.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,10 @@ cdef class VideoCodecContext(CodecContext):
property coded_height:
def __get__(self):
return self.ptr.coded_height

property color_range:
def __get__(self):
return self.ptr.color_range

def __set__(self, value):
self.ptr.color_range = value
21 changes: 21 additions & 0 deletions av/video/frame.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ PictureType = define_enum('PictureType', __name__, (
))


ColorRange = define_enum('ColorRange', __name__, (
('UNSPECIFIED', lib.AVCOL_RANGE_UNSPECIFIED, "Unspecified"),
('MPEG', lib.AVCOL_RANGE_MPEG, "MPEG (limited) YUV range, 219*2^(n-8)"),
('JPEG', lib.AVCOL_RANGE_JPEG, "JPEG (full) YUV range, 2^n-1"),
('NB', lib.AVCOL_RANGE_NB, "Not part of ABI"),
))


cdef copy_array_to_plane(array, VideoPlane plane, unsigned int bytes_per_pixel):
cdef bytes imgbytes = array.tobytes()
cdef const uint8_t[:] i_buf = imgbytes
Expand Down Expand Up @@ -184,6 +192,19 @@ cdef class VideoFrame(Frame):
def pict_type(self, value):
self.ptr.pict_type = PictureType[value].value

@property
def color_range(self):
"""Color range of frame.
Wraps :ffmpeg:`AVFrame.color_range`.
"""
return ColorRange.get(self.ptr.color_range, create=True)

@color_range.setter
def color_range(self, value):
self.ptr.color_range = ColorRange[value].value

def reformat(self, *args, **kwargs):
"""reformat(width=None, height=None, format=None, src_colorspace=None, dst_colorspace=None, interpolation=None)
Expand Down
10 changes: 8 additions & 2 deletions av/video/reformatter.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,10 @@ cdef class VideoReformatter(object):
cdef const int *tbl
cdef int src_range, dst_range, brightness, contrast, saturation
cdef int ret
if src_colorspace != dst_colorspace:

if (
src_colorspace != dst_colorspace or
frame.ptr.color_range != lib.AVCOL_RANGE_UNSPECIFIED
):
with nogil:

# Casts for const-ness, because Cython isn't expressive enough.
Expand All @@ -160,6 +162,10 @@ cdef class VideoReformatter(object):
if dst_colorspace != lib.SWS_CS_DEFAULT:
tbl = lib.sws_getCoefficients(dst_colorspace)

# Color range from user overrides the range from color space.
if frame.ptr.color_range != lib.AVCOL_RANGE_UNSPECIFIED:
src_range = frame.ptr.color_range

# Apply!
ret = lib.sws_setColorspaceDetails(
self.ptr,
Expand Down
2 changes: 2 additions & 0 deletions include/libavcodec/avcodec.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ cdef extern from "libavcodec/avcodec.h" nogil:
int gop_size # The number of pictures in a group of pictures, or 0 for intra_only.
int max_b_frames
int has_b_frames
AVColorRange color_range

# Audio.
AVSampleFormat sample_fmt
Expand Down Expand Up @@ -319,6 +320,7 @@ cdef extern from "libavcodec/avcodec.h" nogil:
AVDictionary *metadata
int flags
int decode_error_flags
AVColorRange color_range


cdef AVFrame* avcodec_alloc_frame()
Expand Down
5 changes: 5 additions & 0 deletions include/libavutil/avutil.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ cdef extern from "libavutil/avutil.h" nogil:
# This is nice, but only in FFMpeg:
# AV_ROUND_PASS_MINMAX

cdef enum AVColorRange:
AVCOL_RANGE_UNSPECIFIED
AVCOL_RANGE_MPEG
AVCOL_RANGE_JPEG
AVCOL_RANGE_NB

cdef double M_PI

Expand Down

0 comments on commit 4d9386c

Please sign in to comment.