Skip to content

Commit 298fc7b

Browse files
authored
Merge pull request #3470 from cudawarped:cudacodec_fix_display_size
`cudacodec::VideoReader`: force output frame resolution to be display not coded size
2 parents bd546fa + 82e2792 commit 298fc7b

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

modules/cudacodec/src/video_parser.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ int CUDAAPI cv::cudacodec::detail::VideoParser::HandleVideoSequence(void* userDa
123123
newFormat.ulHeight = format->coded_height;
124124
newFormat.fps = format->frame_rate.numerator / static_cast<float>(format->frame_rate.denominator);
125125
newFormat.targetSz = thiz->videoDecoder_->getTargetSz();
126-
newFormat.width = newFormat.targetSz.width ? newFormat.targetSz.width : format->coded_width;
127-
newFormat.height = newFormat.targetSz.height ? newFormat.targetSz.height : format->coded_height;
128126
newFormat.srcRoi = thiz->videoDecoder_->getSrcRoi();
129127
if (newFormat.srcRoi.empty()) {
130-
format->display_area.right = format->coded_width;
131-
format->display_area.bottom = format->coded_height;
132128
newFormat.displayArea = Rect(Point(format->display_area.left, format->display_area.top), Point(format->display_area.right, format->display_area.bottom));
129+
if (newFormat.targetSz.empty())
130+
newFormat.targetSz = Size((format->display_area.right - format->display_area.left), (format->display_area.bottom - format->display_area.top));
133131
}
134132
else
135133
newFormat.displayArea = newFormat.srcRoi;
134+
newFormat.width = newFormat.targetSz.width ? newFormat.targetSz.width : format->coded_width;
135+
newFormat.height = newFormat.targetSz.height ? newFormat.targetSz.height : format->coded_height;
136136
newFormat.targetRoi = thiz->videoDecoder_->getTargetRoi();
137137
newFormat.ulNumDecodeSurfaces = min(!thiz->allowFrameDrop_ ? max(thiz->videoDecoder_->nDecodeSurfaces(), static_cast<int>(format->min_num_decode_surfaces)) :
138138
format->min_num_decode_surfaces * 2, 32);

modules/cudacodec/test/test_video.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ PARAM_TEST_CASE(Scaling, cv::cuda::DeviceInfo, std::string, Size2f, Rect2f, Rect
5858
{
5959
};
6060

61+
struct DisplayResolution : testing::TestWithParam<cv::cuda::DeviceInfo>
62+
{
63+
};
64+
6165
PARAM_TEST_CASE(Video, cv::cuda::DeviceInfo, std::string)
6266
{
6367
};
@@ -228,6 +232,37 @@ CUDA_TEST_P(Scaling, Reader)
228232
ASSERT_LT(mae, 2.75);
229233
}
230234

235+
CUDA_TEST_P(DisplayResolution, Reader)
236+
{
237+
cv::cuda::setDevice(GetParam().deviceID());
238+
std::string inputFile = std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/video/1920x1080.avi";
239+
const Rect displayArea(0, 0, 1920, 1080);
240+
GpuMat frame;
241+
242+
{
243+
// verify the output frame is the diplay size (1920x1080) and not the coded size (1920x1088)
244+
cv::Ptr<cv::cudacodec::VideoReader> reader = cv::cudacodec::createVideoReader(inputFile);
245+
reader->set(cudacodec::ColorFormat::GRAY);
246+
ASSERT_TRUE(reader->nextFrame(frame));
247+
const cudacodec::FormatInfo format = reader->format();
248+
ASSERT_TRUE(format.displayArea == displayArea);
249+
ASSERT_TRUE(frame.size() == displayArea.size() && frame.size() == format.targetSz);
250+
}
251+
252+
{
253+
// extra check to verify display frame has not been post-processed and is just a cropped version of the coded sized frame
254+
cudacodec::VideoReaderInitParams params;
255+
params.srcRoi = Rect(0, 0, 1920, 1088);
256+
cv::Ptr<cv::cudacodec::VideoReader> readerCodedSz = cv::cudacodec::createVideoReader(inputFile, {}, params);
257+
readerCodedSz->set(cudacodec::ColorFormat::GRAY);
258+
GpuMat frameCodedSz;
259+
ASSERT_TRUE(readerCodedSz->nextFrame(frameCodedSz));
260+
const cudacodec::FormatInfo formatCodedSz = readerCodedSz->format();
261+
const double err = cv::cuda::norm(frame, frameCodedSz(displayArea), NORM_INF);
262+
ASSERT_TRUE(err == 0);
263+
}
264+
}
265+
231266
CUDA_TEST_P(Video, Reader)
232267
{
233268
cv::cuda::setDevice(GET_PARAM(0).deviceID());
@@ -696,6 +731,8 @@ INSTANTIATE_TEST_CASE_P(CUDA_Codec, CheckSet, testing::Combine(
696731
INSTANTIATE_TEST_CASE_P(CUDA_Codec, Scaling, testing::Combine(
697732
ALL_DEVICES, testing::Values(VIDEO_SRC_SCALING), testing::Values(TARGET_SZ), testing::Values(SRC_ROI), testing::Values(TARGET_ROI)));
698733

734+
INSTANTIATE_TEST_CASE_P(CUDA_Codec, DisplayResolution, ALL_DEVICES);
735+
699736
#define VIDEO_SRC_R "highgui/video/big_buck_bunny.mp4", "cv/video/768x576.avi", "cv/video/1920x1080.avi", "highgui/video/big_buck_bunny.avi", \
700737
"highgui/video/big_buck_bunny.h264", "highgui/video/big_buck_bunny.h265", "highgui/video/big_buck_bunny.mpg", \
701738
"highgui/video/sample_322x242_15frames.yuv420p.libvpx-vp9.mp4", "highgui/video/sample_322x242_15frames.yuv420p.libaom-av1.mp4", \

0 commit comments

Comments
 (0)