Reading frame with VideoCapture hangs at last frame (Bug #3631)


Added by Frank Stähr almost 11 years ago. Updated almost 10 years ago.


Status:Done Start date:2014-04-03
Priority:Normal Due date:
Assignee:Alexander Smorkalov % Done:

100%

Category:highgui-video
Target version:-
Affected version:branch '2.4' (2.4-dev) Operating System:Windows
Difficulty: HW Platform:x86
Pull request:https://github.com/Itseez/opencv/pull/3225

Description

When reading frames from a VideoCapture with operator>>, the program hangs a few seconds after the last frame. See attached example. In fact, it does not crash, but I don’t see a reason for waiting this time. I tried different avi videos – always the same problem.

The documentation <http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-read> says, that an EOF test is automatically included, so there should be no check necessary for number of passed frames. Same problem with VideoCapture.read: Working, but hangs.


minexample.cpp (625 Bytes) Frank Stähr, 2014-04-03 10:46 am


Associated revisions

Revision 2fabe56b
Added by Vadim Pisarevsky about 10 years ago

Merge pull request #3631 from StevenPuttemans:fix_samples

History

Updated by Nikita Manovich almost 11 years ago

Hi,

Thank you for the bug report. I can easily reproduce the problem. I instestigated it and the root cause for the problem below. Could you please help us to fix the problem? Here you can find information how to contribute. If you are a developer please spend several hours to improve OpenCV and submit a pull request.

/workspace/opencv/modules/highgui/src/cap_ffmpeg_impl.hpp:

   const int max_number_of_attempts = 1 << 16;
   ...
   while (!valid)
   {
        ...
        // Decode video frame
        #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)
            avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);

        ...
        else
        {
            count_errs++;
            if (count_errs > max_number_of_attempts)
                break;
        }
        ...
   }

  • Assignee set to Alexander Smorkalov
  • Status changed from New to Open

Updated by Frank Stähr almost 11 years ago

No sorry, I cannot really help you. I am missing time and skills for that. Nevertheless here my only try: <http://www.ffmpeg.org/doxygen/trunk/group__lavf__decoding.html#ga4fdb3084415a82e3810de6ee60e46a61> says, that the return value of av_read_frame is < 0 in case of error or eof. I do not understand the sense of count_errs in the above code snippet, but in the same source file in method InputMediaStream_FFMPEG::read(unsigned char** data, int* size, int* endOfFile) there are the lines:

int ret = av_read_frame(ctx_, &pkt_);

if (ret == AVERROR(EAGAIN))
    continue;

if (ret < 0)
{
    if (ret == (int)AVERROR_EOF)
        *endOfFile = true;
    return false;
}

Looks nice, could it help?

Updated by Dmitry Retinskiy over 10 years ago

  • Pull request set to https://github.com/Itseez/opencv/pull/3225
  • Affected version changed from 2.4.8 (latest release) to branch '2.4' (2.4-dev)

Updated by Philip L almost 10 years ago

PR got merged ages ago and i will bring this into 3.0 if it isn't yet!

  • Status changed from Open to Done
  • % Done changed from 0 to 100

Also available in: Atom PDF