Skip to content

Default colorspace for CPU decoder is now ITU709 #5291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion test/test_videoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_frame_reading(self):
av_pts, vr_pts = [], []
# get av frames
for av_frame in av_reader.decode(av_reader.streams.video[0]):
av_frames.append(torch.tensor(av_frame.to_rgb().to_ndarray()).permute(2, 0, 1))
av_frames.append(torch.tensor(av_frame.to_rgb(src_colorspace="ITU709").to_ndarray()).permute(2, 0, 1))
av_pts.append(av_frame.pts * av_frame.time_base)

# get vr frames
Expand Down
43 changes: 42 additions & 1 deletion torchvision/csrc/io/decoder/video_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "util.h"

// www.ffmpeg.org/doxygen/0.5/swscale-example_8c-source.html

namespace ffmpeg {

namespace {
Expand Down Expand Up @@ -65,6 +64,48 @@ int transformImage(
if ((result = preparePlanes(outFormat, out, planes, lines)) < 0) {
return result;
}

// Here we're changing colourspace in order to maintain consistency with the
// GPU decoder Since SWSContext cannot be edited directly, we're borrowing
// pyAV approach from:
// https://github.com/PyAV-Org/PyAV/blob/9ac05d9ac902d71ecb2fe80f04dcae454008378c/av/video/reformatter.pyx#L141
// very sory for this, found no cleaner way of doing it
const int* inv_tbl;
const int* tbl;
int src_range, dst_range, brightness, contrast, saturation;
int ret_get, ret_set;

if ((ret_get = sws_getColorspaceDetails(
context,
(int**)&inv_tbl,
&src_range,
(int**)&tbl,
&dst_range,
&brightness,
&contrast,
&saturation)) >= 0) {
LOG(INFO) << "Successfuly got the information about colourspace \n "
<< "attempting to set a new one";

inv_tbl = sws_getCoefficients(SWS_CS_ITU709);

if ((ret_set = sws_setColorspaceDetails(
context,
inv_tbl,
src_range,
tbl,
dst_range,
brightness,
contrast,
saturation)) >= 0) {
LOG(INFO) << "Successfuly set new colourspace info";
} else {
LOG(ERROR) << "Failed to set new colourspace info";
}
} else {
LOG(ERROR) << "Failed to get new colourspace info";
}

// NOTE: srcY stride always 0: this is a parameter of YUV format
if ((result = sws_scale(
context, srcSlice, srcStride, 0, inFormat.height, planes, lines)) <
Expand Down