{"action":"opened","issue":{"url":"https://api.github.com/repos/mshabunin/test2/issues/493","labels_url":"https://api.github.com/repos/mshabunin/test2/issues/493/labels{/name}","comments_url":"https://api.github.com/repos/mshabunin/test2/issues/493/comments","events_url":"https://api.github.com/repos/mshabunin/test2/issues/493/events","html_url":"https://github.com/mshabunin/test2/issues/493","id":95195195,"number":493,"title":"ffmpeg decoder crash for raw (uncompressed) avi file","user":{"login":"mshabunin","id":3304494,"avatar_url":"https://avatars.githubusercontent.com/u/3304494?v=3","gravatar_id":"","url":"https://api.github.com/users/mshabunin","html_url":"https://github.com/mshabunin","followers_url":"https://api.github.com/users/mshabunin/followers","following_url":"https://api.github.com/users/mshabunin/following{/other_user}","gists_url":"https://api.github.com/users/mshabunin/gists{/gist_id}","starred_url":"https://api.github.com/users/mshabunin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mshabunin/subscriptions","organizations_url":"https://api.github.com/users/mshabunin/orgs","repos_url":"https://api.github.com/users/mshabunin/repos","events_url":"https://api.github.com/users/mshabunin/events{/privacy}","received_events_url":"https://api.github.com/users/mshabunin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-07-15T13:31:26Z","updated_at":"2015-07-15T13:31:26Z","closed_at":null,"body":"Converted from http://code.opencv.org/issues/3199\r\n```\n|| by Guillaume Dumont on 2013-08-06 02:47\n|| Status: Incomplete\n|| Priority: Normal\n|| Affected: 2.4.6 (latest release)\n|| Target: -\n|| Category: highgui-video\n|| Tracker: bug\n```\n\nffmpeg decoder crash for raw (uncompressed) avi file\n-----------\n```\nIn CvCapture_FFMPEG::grabFrame :\n\nThe call to avcodec_decode_video2 does not seem to allocate memory for\nthe AVFrame that is passed to it when a raw uncompressed video is\ndecoded. It seems like the call to av_free_packet frees the memory that\nthe AVFrame::data0 is pointing to.\n\nthen in CvCapture_FFMPEG::retrieveFrame :\n\nthe call to sws_scale crashes because ffmpeg tries to write to invalid\nmemory.\n\nAny idea how to fix this?\n```\n\nHistory\n-------\n##### by Victor Kocheganov on 2013-08-08 04:55\n```\nHello Guillaume Dumont,\n\nthank you for reporting the issue!   \nCould you please provide some more information to reproduce the issue:\nexample code, output logs.\n\nThanks.\n-   Status changed from New to Incomplete\n-   Category set to highgui-video\n```\n##### by Guillaume Dumont on 2013-08-12 04:02\n```\nHi,\n\nThanks for the response. Attached is the source code of two sample\napplications, one which uses ffmpeg 0.10.7 (which I think is the same\none, test, that opencv uses) I compiled myself with MinGW and one,\ntest2, that uses that one provided with opencv as a prebuilt binary. The\ncrash occurs in both applications.\n\nSee comments main2.cpp line 19 and cap_ffmpeg_impl.hpp lines 684-685. If\nline 685 is commented no crash occurs when sws_scale is called in\ncap_ffmpeg_impl.hpp at line 728. To me it seems like the memory freed by\nav_free_packet should not be freed or there should be a copy when  \navcodec_decode_video2 is called at line 657 in cap_ffmpeg_impl.hpp.\nPlease note that no crash occurs if we use the VFW backend instead of\nffmpeg.\n\nThe video I'm a using to test was too large to fit in the attachments\nand I'd rather not make it publicly available so let me know if I can\ntransfer it to you by some other mean.\n-   File opencv_ffmpeg.7z added\n```\n##### by Jaime  Alemany on 2013-08-17 01:40\n```\nHi\n\nWhat about removing the last call to av_free_packet in grabFrame?\n\nI have recently upgraded mi OS from Ubuntu 12 to 13.04. After that if i\ntry to play some uncompressed avi videos sometimes have a crash,\nsometimes i can see corrupted images on the window. I have commented\nthis call to the function and now seems to work fine (opencv 2.4.6)\n\nI have not tested for memory leaks but seems that the memory used when\nplaying very long videos is correctly managed.\n```\n##### by Jaime  Alemany on 2013-08-21 04:35\n```\nI can confirm that: after the call to av_decode_video2 the picture->data\nis the same pointer than packet.data. So, when calling av_free_packet\nthis memory is freed and after thet it cannot be referenced as it is\ndone in retrieveframe function. This is probably a bug (or a feature) in\nthe ffmpeg decode function. Attached a ddd debuger screen capture.\n\nRemoving the last call to av_free_packet allows to call retrievefreme()\nwithout accessing the freed memory but, i found a problem when seeking a\nframe (cvSetCaptureProperty) in a YUYV video file. After seeking\ngrabFrame returns 0 and frames cannot be read.\n\nEDIT: opencv 2.4.6.1 compiled with ffmpeg support (last version of\nffmpeg compiled from source 23-08-2013), works fine. Line 683 in\ncap_ffmpeg_impl.hpp commented (line 699 in the git version, call to\nav_free_packet) and no crashes, no access to freed memory (valgrind\ndoesnt report) and no seek issues in video files with YUY2 codec.\n-   File picture.png added\n```"}}