Skip to content

Videocapture skip frames#16190

Merged
alalek merged 2 commits intoopencv:3.4from
pletessier:videocapture_skip_frames
Jan 26, 2020
Merged

Videocapture skip frames#16190
alalek merged 2 commits intoopencv:3.4from
pletessier:videocapture_skip_frames

Conversation

@pletessier
Copy link
Copy Markdown
Contributor

This pullrequest changes

Add the possibility to skip frames in VideoCapture by using the existing OPENCV_FFMPEG_CAPTURE_OPTIONS environment variable and the AVCodecContext::skip_frame method.

AVDiscard options are :
AVDISCARD_NONE : discard nothing
AVDISCARD_DEFAULT : discard useless packets like 0 size packets in avi
AVDISCARD_NONREF : discard all non reference
AVDISCARD_BIDIR : discard all bidirectional frames
AVDISCARD_NONKEY : discard all frames except keyframes
AVDISCARD_ALL : discard all

For example, if you want VideoCapture to capture only keyframes, you can set the env var :
OPENCV_FFMPEG_CAPTURE_OPTIONS=avdiscard;nonkey

@asmorkalov asmorkalov added category: videoio pr: needs test New functionality requires minimal tests set labels Dec 23, 2019
@asmorkalov
Copy link
Copy Markdown
Contributor

@pletessier Thanks for the PR. The changes looks good for me. Could you add simple test for the feature? The simplest solution I see is to use some existing video in opencv_extra, restrict FFmpeg provide key frames only with AVDISCARD_NONKEY option and check if cv::VideoCaptrue returns expected amount of frames. Also please, guard the test with FFMPEG specific macro to not break test for other backends.

@asmorkalov asmorkalov self-assigned this Dec 23, 2019
@pletessier
Copy link
Copy Markdown
Contributor Author

Hi @asmorkalov,
Thanks for taking this PR.
I wrote a test that works pretty well, but I don't know how to set the OPENCV_FFMPEG_CAPTURE_OPTIONS env var only for this single test.

Setting it in my shell before to run the test is breaking some tests using FFMpeg. That was expected.
Setting it in source code with the setenv method is breaking tests also, but only after my new one.
I tried to unset the var with the same setenv method and a empty value, or using the unsetenv method. It doesn't change anything.

Is there another approach I could try ?

@asmorkalov
Copy link
Copy Markdown
Contributor

OPENCV_FFMPEG_CAPTURE_OPTIONS is read at https://github.com/opencv/opencv/blob/master/modules/videoio/src/cap_ffmpeg_impl.hpp#L911. The option is read every time when VideoCapture object is created and opened, so unset should resolve side effects. Please push your test code to discuss it.

@pletessier
Copy link
Copy Markdown
Contributor Author

@asmorkalov Starting from a clean env, it works fine now !

@pletessier
Copy link
Copy Markdown
Contributor Author

@asmorkalov Windows tests are failing. Probably because of #9292 (comment).

I can't test on Windows. I am not even sure that the _WIN32 variable is defined...

Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pletessier I have Windows host in my hands and will check it in mean time. Thanks for the test!

@asmorkalov asmorkalov removed the pr: needs test New functionality requires minimal tests set label Jan 20, 2020
@asmorkalov
Copy link
Copy Markdown
Contributor

Hello @pletessier , I found out the failure reason. OpenCV links FFMPEG using wrapper code to prevent license conflict. When the library is built on Unix and wrapper code is built together with the library as we do not distribute FFMPEG itself. On Windows the wrapper is built as dedicated library and is loaded on the go, if FFMPEG is present in the distribution. Wrapper is built and updated manually and CI just uses old version of the wrapper lib without your skip option.

@asmorkalov
Copy link
Copy Markdown
Contributor

asmorkalov commented Jan 20, 2020

👍

@asmorkalov
Copy link
Copy Markdown
Contributor

@pletessier Please clean up test code and we can merge the path and update pre-built libs after it.

@asmorkalov
Copy link
Copy Markdown
Contributor

👍

@asmorkalov
Copy link
Copy Markdown
Contributor

@pletessier Could you squash your commits to have clean library commit history.

@alalek
Copy link
Copy Markdown
Member

alalek commented Jan 20, 2020

You need to skip Windows tests for now (until FFmpeg wrapper got new update) - we can't merge with red builders.

Add this guard for test code:

#ifndef _WIN32  // details: https://github.com/opencv/opencv/pull/16190
...
#endif

else if (strcmp(avdiscard_entry->value, "nonkey") == 0)
enc->skip_frame = AVDISCARD_NONKEY;
else if (strcmp(avdiscard_entry->value, "nonref") == 0)
enc->skip_frame = AVDISCARD_NONREF;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What FFmpeg / libav versions support that? (at least compilation)

Need to properly guard code through "#if"s.

BTW, access to OPENCV_FFMPEG_CAPTURE_OPTIONS is guarded by

#if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(52, 111, 0)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alalek All options (except AVDISCARD_NONINTRA) are in use since ffmpeg 0.7 (libavcodec 52.123.0)

First use of AVDISCARD_NONINTRA is in version 2.3 (libavcodec 55.69.100).

I added some checks on LIBAVCODEC_BUILD version in my last commit, and squashed previous ones.

@pletessier pletessier force-pushed the videocapture_skip_frames branch 2 times, most recently from fa3f3ec to 20f5063 Compare January 20, 2020 16:58
@asmorkalov
Copy link
Copy Markdown
Contributor

asmorkalov commented Jan 23, 2020

👍

@asmorkalov asmorkalov self-requested a review January 23, 2020 05:30
@asmorkalov
Copy link
Copy Markdown
Contributor

@alalek Could you take a look and merge the patch?

@alalek
Copy link
Copy Markdown
Member

alalek commented Jan 25, 2020

Rebased on 3.4 branch and "disabled" test:

  • currently there is no way to detect LIBAV or old FFmpeg versions from the test code (to skip / workaround)
  • test can be launched locally using "--gtest_also_run_disabled_tests" option.

@alalek alalek merged commit 0853085 into opencv:3.4 Jan 26, 2020
@alalek alalek mentioned this pull request Jan 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants