<div dir="ltr">use valgrind</div><div class="gmail_extra"><br><div class="gmail_quote">2015-03-05 10:50 GMT-03:00 Johannes Rehm <span dir="ltr"><<a href="mailto:hahne.rehm@gmx.de" target="_blank">hahne.rehm@gmx.de</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style="font-family:Verdana;font-size:12.0px"><div>
<div>So this is the function where the error happens (look for av_free_packet (&packet);):</div>

<div> </div>

<div>
<div>bool CvCapture_FFMPEG::grabFrame()<br>
{<br>
    bool valid = false;<br>
    int got_picture;</div>

<div>    int count_errs = 0;<br>
    const int max_number_of_attempts = 1 << 9;</div>

<div>    if( !ic || !video_st )  return false;</div>

<div>    if( ic->streams[video_stream]->nb_frames > 0 &&<br>
        frame_number > ic->streams[video_stream]->nb_frames )<br>
        return false;</div>

<div>    av_free_packet (&packet);</div>

<div>    picture_pts = AV_NOPTS_VALUE_;</div>

<div>    // get the next frame<br>
    while (!valid)<br>
    {<br>
        int ret = av_read_frame(ic, &packet);<br>
        if (ret == AVERROR(EAGAIN)) continue;</div>

<div>        /* else if (ret < 0) break; */</div>

<div>        if( packet.stream_index != video_stream )<br>
        {<br>
            av_free_packet (&packet);<br>
            count_errs++;<br>
            if (count_errs > max_number_of_attempts)<br>
                break;<br>
            continue;<br>
        }</div>

<div>        // Decode video frame<br>
        #if LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(53, 2, 0)<span class=""><br>
            avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);<br></span>
        #elif LIBAVFORMAT_BUILD > 4628<br>
                avcodec_decode_video(video_st->codec,<br>
                                     picture, &got_picture,<br>
                                     packet.data, packet.size);<br>
        #else<br>
                avcodec_decode_video(&video_st->codec,<br>
                                     picture, &got_picture,<br>
                                     packet.data, packet.size);<br>
        #endif</div>

<div>        // Did we get a video frame?<br>
        if(got_picture)<br>
        {<br>
            //picture_pts = picture->best_effort_timestamp;<br>
            if( picture_pts == AV_NOPTS_VALUE_ )<br>
                picture_pts = packet.pts != AV_NOPTS_VALUE_ && packet.pts != 0 ? packet.pts : packet.dts;<br>
            frame_number++;<br>
            valid = true;<br>
        }<br>
        else<br>
        {<br>
            count_errs++;<br>
            if (count_errs > max_number_of_attempts)<br>
                break;<br>
        }</div>

<div>        //I need to comment out the following line to prevent an segmentation fault when processing a raw video file without a codec:</div>

<div>        av_free_packet (&packet);<br>
    }</div>

<div>    if( valid && first_frame_number < 0 )<br>
        first_frame_number = dts_to_frame_number(picture_pts);</div>

<div>    // return if we have a new picture or not<br>
    return valid;<br>
}</div>
</div>

<div>
<div> </div>

<div>I am not even sure if there will be a memory leak if I delete av_free_packet (&packet);</div>

<div>But they might put this command for a reason and I do not want to create a new bug.</div>

<div>Sorry for asking such a basic question: do you know how I can verify that there will be no memory leak (with or without adding av_init_packet before)?</div>

<div> </div>

<div name="quote" style="margin:10px 5px 5px 10px;padding:10px 0 10px 10px;border-left:2px solid #c3d9e5;word-wrap:break-word">
<div style="margin:0 0 10px 0"><b>Gesendet:</b> Donnerstag, 05. März 2015 um 14:22 Uhr<br>
<b>Von:</b> "Rafael Lúcio" <<a href="mailto:rafael@inova.in" target="_blank">rafael@inova.in</a>><br>
<b>An:</b> "This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter." <<a href="mailto:libav-user@ffmpeg.org" target="_blank">libav-user@ffmpeg.org</a>><br>
<b>Betreff:</b> Re: [Libav-user] OpenCV VideoCapture FFmpeg Module Bug on Linux using Video without a Codec</div><div><div class="h5">

<div name="quoted-content">
<div>I'm not an expert but maybe I can give u a simpler solution.... just ignore if I'm talking nonsense stuffs..
<div> </div>

<div>maybe if u just av_init_packet before av_free_packet your problem is solved and you don't have memory leak.</div>

<div>I haven't tested this theory, its just an idea.</div>
</div>

<div class="gmail_extra"> 
<div class="gmail_quote">2015-03-05 8:30 GMT-03:00 Johannes Rehm <span><<a href="http://hahne.rehm@gmx.de" target="_blank">hahne.rehm@gmx.de</a>></span>:

<blockquote class="gmail_quote" style="margin:0 0 0 0.8ex;border-left:1.0px rgb(204,204,204) solid;padding-left:1.0ex">
<div>
<div style="font-family:Verdana;font-size:12.0px">
<div>
<div>Hi All,</div>

<div> </div>

<div>there is a known bug in the VideoCapture ffmpeg module of OpenCV. It occurs while loading videos without  a codec:</div>

<div><a href="http://code.opencv.org/issues/1915" target="_blank">http://code.opencv.org/issues/1915</a></div>

<div>I had some time now to debug this bug and found the issue in:</div>

<div>CvCapture_FFMPEG::grabFrame()</div>

<div>In the function</div>

<div>avcodec_decode_video2(video_st->codec, picture, &got_picture, &packet);</div>

<div>the AVFrame picture get set. But after</div>

<div>av_free_packet (&packet);</div>

<div>I get: "error: Cannot access memory at address..." for picture.data[0] which leads to a segmentation fault later in the function sws_scale().</div>

<div>I can encode this video with a video codec using ffmpeg or whatever and I get no segmentation fault (memory in picture.data[0] can still be accessed).</div>

<div>Of course there is an easy fix for this bug: Simply comment out / delete</div>

<div>av_free_packet (&packet);</div>

<div>This is working fine for me. Now I can process videos with and without codecs. But I am not sure if this will lead to memory issues if av_free_packet gets not called. Is there anybody who is familiar with the FFmpeg interface of OpenCV and can help me here?</div>

<div>Thanks a lot!</div>

<div> </div>

<div>Best regards,</div>

<div>Johannes Rehm</div>
</div>
</div>
</div>
<br>
_______________________________________________<br>
Libav-user mailing list<br>
<a href="http://Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
 </blockquote>
</div>
 

<div> </div>
--

<div>
<div>-----------------------------------------------------------------
<div>Rafael Lúcio</div>
</div>
</div>
</div>
_______________________________________________ Libav-user mailing list <a href="mailto:Libav-user@ffmpeg.org" target="_blank">Libav-user@ffmpeg.org</a> <a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a></div>
</div></div></div>
</div>
</div></div></div>
<br>_______________________________________________<br>
Libav-user mailing list<br>
<a href="mailto:Libav-user@ffmpeg.org">Libav-user@ffmpeg.org</a><br>
<a href="http://ffmpeg.org/mailman/listinfo/libav-user" target="_blank">http://ffmpeg.org/mailman/listinfo/libav-user</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><img src="https://lh3.googleusercontent.com/-jTO_Hn0p0VY/UUdsZPT_KGI/AAAAAAAAAN4/bpTrpfmgLRc/s389/Rafael.png"></div></div>
</div>